+ def merge_branches(self, bnameA, bnameB, user, message):
+ wlock = self.repo.wlock()
+ try:
+ return self.merge_revisions(self.get_branch_tip(bnameA),
+ self.get_branch_tip(bnameB), user, message)
+ finally:
+ wlock.release()
+
+ def diff(self, revA, revB):
+ return UpdateStatus(self.repo.status(revA, revB))
+
+ def merge_revisions(self, revA, revB, user, message):
+ wlock = self.repo.wlock()
+ try:
+ old = self.repo[None]
+
+ self._checkout(revA)
+ mergestatus = self._merge(revB)
+ if not mergestatus.isclean():
+ # revert the failed merge
+ self.repo.recover()
+ raise UncleanMerge(u'Failed to merge %d files.' % len(mergestatus.unresolved))
+
+ # commit the clean merge
+ self._commit(message, user)
+
+ # cleanup after yourself
+ self._checkout(old.rev())
+ except util.Abort, ae:
+ raise RepositoryException(u'Failed merge: ' + ae.message)
+ finally:
+ wlock.release()
+
+ def common_ancestor(self, revA, revB):
+ return self.repo[revA].ancestor(self.repo[revB])
+
+ def _checkout(self, rev, force=True):
+ return MergeStatus(mercurial.merge.update(self.repo, rev, False, force, None))
+
+ def _merge(self, rev):
+ """ Merge the revision into current working directory """
+ return MergeStatus(mercurial.merge.update(self.repo, rev, True, False, None))
+
+ def _switch_to_branch(self, bname):
+ bname = sanitize_string(bname)
+ wlock = self.repo.wlock()
+ try:
+ current = self.repo[None].branch()
+ if current == bname:
+ return current
+
+ tip = self.get_branch_tip(bname)
+ status = self._checkout(tip)
+
+ if not status.isclean():
+ raise RepositoryException("Unclean branch switch. This IS REALLY bad.")
+
+ return current
+ except KeyError, ke:
+ raise RepositoryException((u"Can't switch to branch '%s': no such branch." % bname) , ke)
+ except util.Abort, ae:
+ raise RepositoryException(u"Can't switch to branch '%s': %s" % (bname, ae.message), ae)
+ finally:
+ wlock.release()
+
+ def with_wlock(self, action):
+ wlock = self.repo.wlock()
+ try:
+ action()
+ finally:
+ wlock.release()
+
+ def _create_branch(self, name, parent_rev, msg=None, before_commit=None):
+ """WARNING: leaves the working directory in the new branch"""
+ name = sanitize_string(name)
+
+ if self.has_branch(name): return # just exit
+
+ self._checkout(parent_rev)
+ self.repo.dirstate.setbranch(name)
+
+ if msg is None:
+ msg = "Initial commit for branch '%s'." % name
+
+ if before_commit: before_commit()
+ self._commit(msg, user='platform')
+ return self.get_branch_tip(name)
+