Zmiana $.log na console.log (IMHO nie warto używać tak skomplikowanego wrappera,...
[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                 print 'Update failed: has children.'
102                 # can't update non-latest revision
103                 return (False, False)
104
105             sv = self.shared()
106
107             if self.parentof(sv):
108                 return (True, False)
109
110             if sv.ancestorof(self):
111                 return (True, False)
112
113
114             return self._revision.merge_with(sv._revision, user=user)
115         finally:
116             lock.release()  
117
118     def share(self, message):
119         lock = self.library.lock()
120         try:            
121             if self.ismain():
122                 return (True, False) # always shared
123
124             user = self._revision.user_name
125             main = self.shared()._revision
126             local = self._revision            
127
128             # Case 1:
129             #         * local
130             #         |
131             #         * <- can also be here!
132             #        /|
133             #       / |
134             # main *  *
135             #      |  |
136             # The local branch has been recently updated,
137             # so we don't need to update yet again, but we need to
138             # merge down to default branch, even if there was
139             # no commit's since last update
140
141             if main.ancestorof(local):
142                 print "case 1"
143                 success, changed = main.merge_with(local, user=user, message=message)                
144             # Case 2:
145             #
146             # main *  * local
147             #      |\ |
148             #      | \|
149             #      |  *
150             #      |  |
151             #
152             # Default has no changes, to update from this branch
153             # since the last merge of local to default.
154             elif local.has_common_ancestor(main):
155                 print "case 2"
156                 if not local.parentof(main):
157                     success, changed = main.merge_with(local, user=user, message=message)
158
159             # Case 3:
160             # main *
161             #      |
162             #      * <- this case overlaps with previos one
163             #      |\
164             #      | \
165             #      |  * local
166             #      |  |
167             #
168             # There was a recent merge to the defaul branch and
169             # no changes to local branch recently.
170             #
171             # Use the fact, that user is prepared to see changes, to
172             # update his branch if there are any
173             elif local.ancestorof(main):
174                 print "case 3"
175                 if not local.parentof(main):
176                     success, changed = local.merge_with(main, user=user, \
177                         message='$AUTO$ Local branch update during share.')
178                     
179             else:
180                 print "case 4"
181                 success, changed = local.merge_with(main, user=user, \
182                         message='$AUTO$ Local branch update during share.')
183
184                 if not success:
185                     return False
186
187                 if changed:
188                     local = local.latest()
189                     
190                 success, changed = main.merge_with(local, user=user,\
191                     message=message)
192             
193             return success, changed
194         finally:
195             lock.release()     
196
197     def __unicode__(self):
198         return u"Document(%s:%s)" % (self.name, self.owner)
199
200     def __str__(self):
201         return self.__unicode__().encode('utf-8')
202     
203     def __eq__(self, other):
204         return (self._revision == other._revision) and (self.name == other.name)
205