X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/115d9ab4d4ca222139e5b0db87e59248bfb6410e..392a2d2ef7e9445ebdb238a4d43c8893875b02d0:/lib/hg.py diff --git a/lib/hg.py b/lib/hg.py index a2702f6c..60e9f80e 100644 --- a/lib/hg.py +++ b/lib/hg.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- import os import codecs -from mercurial import localrepo, ui, error, match, node, encoding - +from mercurial import localrepo, ui, match, node, encoding, util +import mercurial.merge, mercurial.error encoding.encoding = 'utf-8' @@ -10,7 +10,6 @@ encoding.encoding = 'utf-8' class RepositoryDoesNotExist(Exception): pass - class Repository(object): """Abstrakcja repozytorium Mercurial. Działa z Mercurial w wersji 1.3.1.""" @@ -27,7 +26,7 @@ class Repository(object): if os.path.isdir(path): try: return localrepo.localrepository(self.ui, path) - except error.RepoError: + except mercurial.error.RepoError: # dir is not an hg repo, we must init it if create: return localrepo.localrepository(self.ui, path, create=1) @@ -36,22 +35,33 @@ class Repository(object): return localrepo.localrepository(self.ui, path, create=1) raise RepositoryDoesNotExist("Repository %s does not exist." % path) - def all_files(self): - return list(self.repo['tip']) + def all_files(self, branch='default'): + return self.in_branch(lambda: self._all_files(), branch) + + def _all_files(self): + return list(self.repo[None]) - def get_file(self, path): - ctx = self.repo.changectx(None) - return ctx.filectx(path) + def get_file(self, path, branch='default'): + return self.in_branch(lambda: self._get_file(path), branch) + + def _get_file(self, path): + return self.repo.wread(path) - def add_file(self, path, value): - f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8') - f.write(value) - f.close() + def add_file(self, path, value, branch='default'): + return self.in_branch(lambda: self._add_file(path, value), branch) - if path not in self._pending_files: - self._pending_files.append(path) + def _add_file(self, path, value): + return self.repo.wwrite(path, value.encode('utf-8'), []) +# f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8') +# f.write(value) +# f.close() +# if path not in self._pending_files: +# self._pending_files.append(path) + + def _commit(self, message, user=None, key=None): + return self.repo.commit(text=message, user=user) - def commit(self, message=u'hgshelve auto commit', key=None, user=None): + def _commit2(self, message, key=None, user=None): """ Commit unsynchronized data to disk. Arguments:: @@ -75,7 +85,7 @@ class Repository(object): # will commit all keys pending_files = self._pending_files else: - if key not in self._pending_files: + if keys not in self._pending_files: # key isn't changed return None else: @@ -97,6 +107,9 @@ class Repository(object): self.repo.forget(files_to_remove) # ---- hg commit if files_to_commit: + for i, f in enumerate(files_to_commit): + if isinstance(f, unicode): + files_to_commit[i] = f.encode('utf-8') matcher = match.match(self.repo.root, self.repo.root, files_to_commit, default='path') rev = self.repo.commit(message, user=user, match=matcher) commited = True @@ -107,4 +120,46 @@ class Repository(object): # reread keys # self._keys = self.get_persisted_objects_keys() # return node.hex(rev) - \ No newline at end of file + + 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