1 # -*- encoding: utf-8 -*-
3 __author__ = "Ćukasz Rekucki"
4 __date__ = "$2009-09-25 09:35:06$"
5 __doc__ = "Module documentation."
10 class MercurialDocument(wlrepo.Document):
12 def data(self, entry):
13 path = self._library._sanitize_string(self.id + u'.' + entry)
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)
21 def quickwrite(self, entry, data, msg, user=None):
22 user = user or self.owner
24 if isinstance(data, unicode):
25 data = data.encode('utf-8')
27 user = self._library._sanitize_string(user)
28 msg = self._library._sanitize_string(msg)
29 entry = self._library._sanitize_string(entry)
32 raise ValueError("Can't determine user.")
35 f = l._fileopen(r(entry), "w+")
40 return self.invoke_and_commit(write, lambda d: (msg, \
41 self._library._sanitize_string(self.owner)) )
43 def invoke_and_commit(self, ops, commit_info):
44 lock = self._library.lock()
46 self._library._checkout(self._revision.hgrev())
48 def entry_path(entry):
49 return self._library._sanitize_string(self.id + u'.' + entry)
51 ops(self._library, entry_path)
52 message, user = commit_info(self)
54 message = self._library._sanitize_string(message)
55 user = self._library._sanitize_string(user)
57 self._library._commit(message, user)
59 return self._library.document(docid=self.id, user=user)
61 # rollback the last commit
62 self._library._rollback()
67 # def commit(self, message, user):
68 # """Make a new commit."""
69 # self.invoke_and_commit(message, user, lambda *a: True)
72 return self._revision.user_name is None
78 return self._library.document(docid=self.id)
81 return self._library.document(docid=self.id, user=self.owner)
84 fullid = self._library.fulldocid(self.id, user)
86 def take_action(library, resolve):
88 library._create_branch(fullid, parent=self._revision)
90 if not self._library.has_revision(fullid):
91 self.invoke_and_commit(take_action, \
92 lambda d: ("$AUTO$ File checkout.", user) )
94 return self._library.document_for_rev(fullid)
96 def update(self, user):
97 """Update parts of the document."""
98 lock = self.library.lock()
101 # main revision of the document
104 if self._revision.has_children():
105 print 'Update failed: has children.'
106 # can't update non-latest revision
107 return (False, False)
111 if self.parentof(sv):
114 if sv.ancestorof(self):
118 return self._revision.merge_with(sv._revision, user=user,
119 message="$AUTO$ Personal branch update.")
123 def share(self, message):
124 lock = self.library.lock()
127 return (True, False) # always shared
129 user = self._revision.user_name
130 main = self.shared()._revision
131 local = self._revision
136 # * <- can also be here!
141 # The local branch has been recently updated,
142 # so we don't need to update yet again, but we need to
143 # merge down to default branch, even if there was
144 # no commit's since last update
146 if main.ancestorof(local):
148 success, changed = main.merge_with(local, user=user, message=message)
157 # Default has no changes, to update from this branch
158 # since the last merge of local to default.
159 elif local.has_common_ancestor(main):
161 if not local.parentof(main):
162 success, changed = main.merge_with(local, user=user, message=message)
170 # * <- this case overlaps with previos one
176 # There was a recent merge to the defaul branch and
177 # no changes to local branch recently.
179 # Use the fact, that user is prepared to see changes, to
180 # update his branch if there are any
181 elif local.ancestorof(main):
183 if not local.parentof(main):
184 success, changed = local.merge_with(main, user=user, \
185 message='$AUTO$ Local branch update during share.')
192 success, changed = local.merge_with(main, user=user, \
193 message='$AUTO$ Local branch update during share.')
199 local = self.latest()._revision
201 success, changed = main.merge_with(local, user=user,\
204 return success, changed
208 def __unicode__(self):
209 return u"Document(%s:%s)" % (self.name, self.owner)
212 return self.__unicode__().encode('utf-8')
214 def __eq__(self, other):
215 return (self._revision == other._revision) and (self.name == other.name)