X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/0249ff0d913dfaef2a6f5e8518e5736545a23c28..d798a69cb177ba4dabd2d917e1da6ab94707aa7d:/lib/hg.py diff --git a/lib/hg.py b/lib/hg.py index 2b885b3c..07ec9a79 100644 --- a/lib/hg.py +++ b/lib/hg.py @@ -1,13 +1,13 @@ # -*- coding: utf-8 -*- import os -import codecs -from mercurial import localrepo, ui, error, match, node, encoding +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.""" @@ -25,7 +25,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) @@ -34,91 +34,93 @@ 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 get_file(self, path): - ctx = self.repo.changectx(None) - return ctx.filectx(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 file_list(self, branch): + return self.in_branch(lambda: self._file_list(), branch) - if path not in self._pending_files: - self._pending_files.append(path) + def _file_list(self): + return list(self.repo[None]) - def commit(self, message=u'hgshelve auto commit', key=None, user=None): - """ - Commit unsynchronized data to disk. - Arguments:: - - - message: mercurial's changeset message - - key: supply to sync only one key - """ - if isinstance(message, unicode): - message = message.encode('utf-8') - if isinstance(user, unicode): - user = user.encode('utf-8') + 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.") - commited = False - rev = None - files_to_add = [] - files_to_remove = [] - files_to_commit = [] - - # first of all, add absent data and clean removed - if key is None: - # will commit all keys - pending_files = self._pending_files - else: - if keys not in self._pending_files: - # key isn't changed - return None - else: - pending_files = [key] - for path in pending_files: - files_to_commit.append(path) - if path in self.all_files(): - if not os.path.exists(os.path.join(self.real_path, path)): - # file removed - files_to_remove.append(path) - else: - # file added - files_to_add.append(path) - # hg add - if files_to_add: - self.repo.add(files_to_add) - # hg forget - if files_to_remove: - 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 - # clean pending keys - for key in pending_files: - self._pending_files.remove(key) - # if commited: - # reread keys - # self._keys = self.get_persisted_objects_keys() - # return node.hex(rev) - - def in_branch(self, branch_name, action): + return self.repo.wread(path) + + 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): + 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, 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: - current_branch = self.repo[None].branch() - self.repo.dirstate.setbranch(branch_name) + old = self._switch_to_branch(bname) try: # do some stuff - action() + return action() finally: - self.repo.dirstate.setbranch(current_branch) + self._switch_to_branch(old) + finally: + wlock.release() + + def _switch_to_branch(self, bname, create=True): + wlock = self.repo.wlock() + try: + current = self.repo[None].branch() + if current == bname: + return current + try: + tip = self.repo.branchtags()[bname] + except KeyError, ke: + if not create: raise ke + + # create the branch on the fly + + # first switch to default branch + default_tip = self.repo.branchtags()['default'] + mercurial.merge.update(self.repo, default_tip, False, True, None) + + # set the dirstate to new branch + self.repo.dirstate.setbranch(bname) + self._commit('Initial commit for automatic branch "%s".' % bname, user="django-admin") + + # collect the new tip + 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() @@ -126,4 +128,12 @@ class Repository(object): """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 + +class RepositoryDoesNotExist(RepositoryException): + pass