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)
122 def share(self, message):
123 lock = self.library.lock()
126 return (True, False) # always shared
128 user = self._revision.user_name
129 main = self.shared()._revision
130 local = self._revision
135 # * <- can also be here!
140 # The local branch has been recently updated,
141 # so we don't need to update yet again, but we need to
142 # merge down to default branch, even if there was
143 # no commit's since last update
145 if main.ancestorof(local):
147 success, changed = main.merge_with(local, user=user, message=message)
156 # Default has no changes, to update from this branch
157 # since the last merge of local to default.
158 elif local.has_common_ancestor(main):
160 if not local.parentof(main):
161 success, changed = main.merge_with(local, user=user, message=message)
166 # * <- this case overlaps with previos one
172 # There was a recent merge to the defaul branch and
173 # no changes to local branch recently.
175 # Use the fact, that user is prepared to see changes, to
176 # update his branch if there are any
177 elif local.ancestorof(main):
179 if not local.parentof(main):
180 success, changed = local.merge_with(main, user=user, \
181 message='$AUTO$ Local branch update during share.')
185 success, changed = local.merge_with(main, user=user, \
186 message='$AUTO$ Local branch update during share.')
192 local = self.latest()._revision
194 success, changed = main.merge_with(local, user=user,\
197 return success, changed
201 def __unicode__(self):
202 return u"Document(%s:%s)" % (self.name, self.owner)
205 return self.__unicode__().encode('utf-8')
207 def __eq__(self, other):
208 return (self._revision == other._revision) and (self.name == other.name)