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