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)
53 self._library._commit(message, user)
55 return self._library.document(docid=self.id, user=user)
57 # rollback the last commit
58 self._library._rollback()
63 # def commit(self, message, user):
64 # """Make a new commit."""
65 # self.invoke_and_commit(message, user, lambda *a: True)
68 return self._revision.user_name is None
74 return self._library.document(docid=self.id)
77 return self._library.document(docid=self.id, user=self.owner)
80 fullid = self._library.fulldocid(self.id, user)
82 def take_action(library, resolve):
84 library._create_branch(fullid, parent=self._revision)
86 if not self._library.has_revision(fullid):
87 self.invoke_and_commit(take_action, \
88 lambda d: ("$AUTO$ File checkout.", user) )
90 return self._library.document_for_rev(fullid)
92 def update(self, user):
93 """Update parts of the document."""
94 lock = self.library.lock()
97 # main revision of the document
100 if self._revision.has_children():
101 print 'Update failed: has children.'
102 # can't update non-latest revision
103 return (False, False)
107 if self.parentof(sv):
110 if sv.ancestorof(self):
114 return self._revision.merge_with(sv._revision, user=user)
118 def share(self, message):
119 lock = self.library.lock()
122 return (True, False) # always shared
124 user = self._revision.user_name
125 main = self.shared()._revision
126 local = self._revision
131 # * <- can also be here!
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
141 if main.ancestorof(local):
143 success, changed = main.merge_with(local, user=user, message=message)
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):
156 if not local.parentof(main):
157 success, changed = main.merge_with(local, user=user, message=message)
162 # * <- this case overlaps with previos one
168 # There was a recent merge to the defaul branch and
169 # no changes to local branch recently.
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):
175 if not local.parentof(main):
176 success, changed = local.merge_with(main, user=user, \
177 message='$AUTO$ Local branch update during share.')
181 success, changed = local.merge_with(main, user=user, \
182 message='$AUTO$ Local branch update during share.')
188 local = local.latest()
190 success, changed = main.merge_with(local, user=user,\
193 return success, changed
197 def __unicode__(self):
198 return u"Document(%s:%s)" % (self.name, self.owner)
201 return self.__unicode__().encode('utf-8')
203 def __eq__(self, other):
204 return (self._revision == other._revision) and (self.name == other.name)