8b5849cc4a74657d501cef93dc12f8a40d7f53e4
[redakcja.git] / lib / wlrepo / mercurial_backend / __init__.py
1 # -*- encoding: utf-8 -*-
2
3 __author__= "Ɓukasz Rekucki"
4 __date__ = "$2009-09-25 09:20:22$"
5 __doc__ = "Module documentation."
6
7 import wlrepo
8 from mercurial.node import nullid
9
10 class MercurialRevision(wlrepo.Revision):
11
12     def __init__(self, lib, changectx):
13         super(MercurialRevision, self).__init__(lib)
14         self._changectx = changectx
15         
16         branchname = self._changectx.branch()
17         if branchname.startswith("$doc:"):
18             self._docname = branchname[5:]
19             self._username = None
20         elif branchname.startswith("$user:"):
21             idx = branchname.find("$doc:")
22             if(idx < 0):
23                 raise ValueError("Revision %s is not a valid document revision." % changectx.hex());
24             self._username = branchname[6:idx]
25             self._docname = branchname[idx+5:]
26         else:
27             raise ValueError("Revision %s is not a valid document revision." % changectx.hex());
28         
29     @property
30     def document_name(self):
31         return self._docname.decode('utf-8')
32
33     @property
34     def user_name(self):
35         return self._username.decode('utf-8')
36
37     def hgrev(self):
38         return self._changectx.node()
39         
40     def hgcontext(self):
41         return self._changectx
42
43     def hgbranch(self):
44         return self._changectx.branch()
45
46     @property
47     def timestamp(self):
48         return self._changectx.date()[0]
49
50     def __unicode__(self):
51         return u"%s" % self._changectx.hex()
52
53     def __str__(self):
54         return self.__unicode__().encode('utf-8')
55
56     def __repr__(self):
57         return "%s" % self._changectx.hex()
58
59     def ancestorof(self, other):
60         nodes = list(other._changectx._parents)
61         while nodes[0].node() != nullid:
62             v = nodes.pop(0)
63             if v == self._changectx:
64                 return True
65             nodes.extend( v._parents )
66         return False
67
68     def parentof(self, other):
69         return self._changectx in other._changectx._parents
70
71     def has_common_ancestor(self, other):
72         a = self._changectx.ancestor(other._changectx)       
73         return (a.branch() == self._changectx.branch())
74
75     def has_children(self):
76         return bool(self._library._hgrepo.changelog.children(self.hgrev()))
77
78     def merge_with(self, other, user, message):
79         lock = self._library.lock(True)
80         try:
81             self._library._checkout(self._changectx.node())
82             status = self._library._merge(other._changectx.node())
83             if status.isclean():
84                 self._library._commit(user=user, message=message)
85                 return (True, True)
86             else:
87                 return (False, False)
88         finally:
89             lock.release()
90
91     def __eq__(self, other):
92         return self._changectx.node() == other._changectx.node()
93
94
95 from wlrepo.mercurial_backend.library import MercurialLibrary
96
97