9a22395e2f0e65c326f222fc773366834efc8754
[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 merge_with(self, other, user, message):
76         lock = self._library.lock(True)
77         try:
78             self._library._checkout(self._changectx.node())
79             status = self._library._merge(other._changectx.node())
80             if status.isclean():
81                 self._library._commit(user=user, message=message)
82                 return (True, True)
83             else:
84                 return (False, False)
85         finally:
86             lock.release()
87
88     def __eq__(self, other):
89         return self._changectx.node() == other._changectx.node()
90
91
92 from wlrepo.mercurial_backend.library import MercurialLibrary
93
94