+ def commit(self, message, branch, user=None):
+ return self.in_branch(lambda: self._commit(message, key=key, user=user), branch)
+
+ def in_branch(self, action, bname):
+ wlock = self.repo.wlock()
+ try:
+ old = self._switch_to_branch(bname)
+ try:
+ # do some stuff
+ return action()
+ finally:
+ self._switch_to_branch(old)
+ finally:
+ wlock.release()
+
+ 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))