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