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