+
+ def commit(self, message, key=None, user=None, branch='default'):
+ return self.in_branch(lambda: self._commit(message, key=key, user=user), branch)
+
+ def in_branch(self, action, bname='default'):
+ 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 _switch_to_branch(self, bname):
+ wlock = self.repo.wlock()
+ try:
+ current = self.repo[None].branch()
+ if current == bname:
+ return current
+
+ tip = self.repo.branchtags()[bname]
+ upstats = mercurial.merge.update(self.repo, tip, False, True, None)
+ return current
+ except KeyError, ke:
+ raise RepositoryException("Can't switch to branch '%s': no such branch." % bname , ke)
+ except util.Abort, ae:
+ raise repositoryException("Can't switch to branch '%s': %s" % (bname, ae.message), ae)
+ finally:
+ wlock.release()
+
+ def write_lock(self):
+ """Returns w write lock to the repository."""
+ return self.repo.wlock()
+
+
+class RepositoryException(Exception):
+
+ def __init__(self, msg, cause=None):
+ Exception.__init__(self, msg)
+ self.cause = cause