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