X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/12e7b7cbf07c8633f3093df7b4898156cca768f6..d798a69cb177ba4dabd2d917e1da6ab94707aa7d:/lib/hg.py diff --git a/lib/hg.py b/lib/hg.py index b8795b71..07ec9a79 100644 --- a/lib/hg.py +++ b/lib/hg.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- import os -import codecs -from mercurial import localrepo, ui, match, node, encoding, util +from mercurial import localrepo, ui, encoding, util import mercurial.merge, mercurial.error encoding.encoding = 'utf-8' -class RepositoryDoesNotExist(Exception): - pass +def clearpath(path): + return unicode(path).encode("utf-8") class Repository(object): """Abstrakcja repozytorium Mercurial. Działa z Mercurial w wersji 1.3.1.""" @@ -35,36 +34,52 @@ class Repository(object): return localrepo.localrepository(self.ui, path, create=1) raise RepositoryDoesNotExist("Repository %s does not exist." % path) - def all_files(self, branch='default'): - return self.in_branch(lambda: self._all_files(), branch) + def file_list(self, branch): + return self.in_branch(lambda: self._file_list(), branch) - def _all_files(self): + def _file_list(self): return list(self.repo[None]) - def get_file(self, path, branch='default'): + def get_file(self, path, branch): return self.in_branch(lambda: self._get_file(path), branch) def _get_file(self, path): + path = clearpath(path) + + if not self._file_exists(path): + raise RepositoryException("File not availble in this branch.") + return self.repo.wread(path) - - def add_file(self, path, value, branch='default'): + + def file_exists(self, path, branch): + return self.in_branch(lambda: self._file_exists(path), branch) + + def _file_exists(self, path): + path = clearpath(path) + return self.repo.dirstate[path] != "?" + + def write_file(self, path, value, branch): + return self.in_branch(lambda: self._write_file(path, value), branch) + + def _write_file(self, path, value): + path = clearpath(path) + return self.repo.wwrite(path, value, []) + + def add_file(self, path, value, branch): return self.in_branch(lambda: self._add_file(path, value), branch) 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) + path = clearpath(path) + self._write_file(path, value) + return self.repo.add( [path] ) def _commit(self, message, user=None): return self.repo.commit(text=message, user=user) - def commit(self, message, user=None, branch='default'): + 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='default'): + def in_branch(self, action, bname): wlock = self.repo.wlock() try: old = self._switch_to_branch(bname) @@ -105,7 +120,7 @@ class Repository(object): 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) + raise RepositoryException("Can't switch to branch '%s': %s" % (bname, ae.message), ae) finally: wlock.release() @@ -119,3 +134,6 @@ class RepositoryException(Exception): def __init__(self, msg, cause=None): Exception.__init__(self, msg) self.cause = cause + +class RepositoryDoesNotExist(RepositoryException): + pass