Missing templates.
[redakcja.git] / lib / wlrepo / mercurial_backend / document.py
1 # -*- encoding: utf-8 -*-
2
3 __author__ = "Ɓukasz Rekucki"
4 __date__ = "$2009-09-25 09:35:06$"
5 __doc__ = "Module documentation."
6
7 import wlrepo
8
9 class MercurialDocument(wlrepo.Document):
10
11     def data(self, entry):
12         path = self._revision._docname + '.' + entry            
13         return self._library._filectx(path, \
14             self._revision.hgrev()).data()   
15
16     def quickwrite(self, entry, data, msg, user=None):
17         user = user or self.owner
18
19         if isinstance(data, unicode):
20             data = data.encode('utf-8')
21             
22         user = self._library._sanitize_string(user)
23         msg = self._library._sanitize_string(msg)
24         entry = self._library._sanitize_string(entry)
25
26         if user is None:
27             raise ValueError("Can't determine user.")
28         
29         def write(l, r):
30             f = l._fileopen(r(entry), "w+")
31             f.write(data)
32             f.close()
33             l._fileadd(r(entry))            
34
35         return self.invoke_and_commit(write, lambda d: (msg, self.owner))
36
37     def invoke_and_commit(self, ops,
38             commit_info):
39         lock = self._library.lock()
40         try:            
41             self._library._checkout(self._revision.hgrev())
42
43             def entry_path(entry):
44                 return self.id + '.' + entry
45             
46             ops(self._library, entry_path)
47             message, user = commit_info(self)
48             self._library._commit(message, user)
49             try:
50                 return self._library.document(docid=self.id, user=user)
51             except Exception, e:
52                 # rollback the last commit
53                 self._library._rollback()
54                 raise e
55         finally:
56             lock.release()
57         
58     # def commit(self, message, user):
59     #    """Make a new commit."""
60     #    self.invoke_and_commit(message, user, lambda *a: True)
61
62     def ismain(self):
63         return self._revision.user_name is None
64
65     def shared(self):
66         if self.ismain():
67             return self
68         
69         return self._library.document(docid=self.id)
70
71     def latest(self):
72         return self._library.document(docid=self.id, user=self.owner)
73
74     def take(self, user):
75         fullid = self._library.fulldocid(self.id, user)
76
77         def take_action(library, resolve):
78             # branch from latest 
79             library._create_branch(fullid, parent=self._revision)            
80
81         if not self._library.has_revision(fullid):
82             self.invoke_and_commit(take_action, \
83                 lambda d: ("$AUTO$ File checkout.", user) )
84
85         return self._library.document_for_rev(fullid)
86             
87     def update(self, user):
88         """Update parts of the document."""
89         lock = self.library.lock()
90         try:
91             if self.ismain():
92                 # main revision of the document
93                 return (True, False)
94             
95             if self._revision.has_children():
96                 # can't update non-latest revision
97                 return (False, False)
98
99             sv = self.shared()
100             
101             if not sv.ancestorof(self) and not self.parentof(sv):
102                 return self._revision.merge_with(sv._revision, user=user)
103
104             return (False, False)
105         finally:
106             lock.release()  
107
108     def share(self, message):
109         lock = self.library.lock()
110         try:            
111             if self.ismain():
112                 return (True, False) # always shared
113
114             user = self._revision.user_name
115             main = self.shared()._revision
116             local = self._revision            
117
118             # Case 1:
119             #         * local
120             #         |
121             #         * <- can also be here!
122             #        /|
123             #       / |
124             # main *  *
125             #      |  |
126             # The local branch has been recently updated,
127             # so we don't need to update yet again, but we need to
128             # merge down to default branch, even if there was
129             # no commit's since last update
130
131             if main.ancestorof(local):
132                 print "case 1"
133                 success, changed = main.merge_with(local, user=user, message=message)                
134             # Case 2:
135             #
136             # main *  * local
137             #      |\ |
138             #      | \|
139             #      |  *
140             #      |  |
141             #
142             # Default has no changes, to update from this branch
143             # since the last merge of local to default.
144             elif local.has_common_ancestor(main):
145                 print "case 2"
146                 if not local.parentof(main):
147                     success, changed = main.merge_with(local, user=user, message=message)
148
149             # Case 3:
150             # main *
151             #      |
152             #      * <- this case overlaps with previos one
153             #      |\
154             #      | \
155             #      |  * local
156             #      |  |
157             #
158             # There was a recent merge to the defaul branch and
159             # no changes to local branch recently.
160             #
161             # Use the fact, that user is prepared to see changes, to
162             # update his branch if there are any
163             elif local.ancestorof(main):
164                 print "case 3"
165                 if not local.parentof(main):
166                     success, changed = local.merge_with(main, user=user, \
167                         message='$AUTO$ Local branch update during share.')
168                     
169             else:
170                 print "case 4"
171                 success, changed = local.merge_with(main, user=user, \
172                         message='$AUTO$ Local branch update during share.')
173
174                 if not success:
175                     return False
176
177                 if changed:
178                     local = local.latest()
179                     
180                 success, changed = main.merge_with(local, user=user,\
181                     message=message)
182             
183             return success, changed
184         finally:
185             lock.release()     
186
187     def __str__(self):
188         return u"Document(%s:%s)" % (self.name, self.owner)
189
190     def __eq__(self, other):
191         return (self._revision == other._revision) and (self.name == other.name)
192