1 # -*- coding: utf-8 -*-
4 from mercurial import localrepo, ui, match, node, encoding, util
5 import mercurial.merge, mercurial.error
7 encoding.encoding = 'utf-8'
12 class Repository(object):
13 """Abstrakcja repozytorium Mercurial. DziaĆa z Mercurial w wersji 1.3.1."""
15 def __init__(self, path, create=False):
17 self.ui.config('ui', 'quiet', 'true')
18 self.ui.config('ui', 'interactive', 'false')
20 self.real_path = os.path.realpath(path)
21 self.repo = self.open_repository(self.real_path, create)
22 self._pending_files = []
24 def open_repository(self, path, create=False):
25 if os.path.isdir(path):
27 return localrepo.localrepository(self.ui, path)
28 except mercurial.error.RepoError:
29 # dir is not an hg repo, we must init it
31 return localrepo.localrepository(self.ui, path, create=1)
34 return localrepo.localrepository(self.ui, path, create=1)
35 raise RepositoryDoesNotExist("Repository %s does not exist." % path)
37 def file_list(self, branch):
38 return self.in_branch(lambda: self._file_list(), branch)
41 return list(self.repo[None])
43 def get_file(self, path, branch):
44 return self.in_branch(lambda: self._get_file(path), branch)
46 def _get_file(self, path):
47 return self.repo.wread(path)
49 def add_file(self, path, value, branch):
50 return self.in_branch(lambda: self._add_file(path, value), branch)
52 def _add_file(self, path, value):
53 return self.repo.wwrite(path, value.encode('utf-8'), [])
55 def _commit(self, message, user=None):
56 return self.repo.commit(text=message, user=user)
58 def commit(self, message, branch, user=None):
59 return self.in_branch(lambda: self._commit(message, key=key, user=user), branch)
61 def in_branch(self, action, bname):
62 wlock = self.repo.wlock()
64 old = self._switch_to_branch(bname)
69 self._switch_to_branch(old)
73 def _switch_to_branch(self, bname, create=True):
74 wlock = self.repo.wlock()
76 current = self.repo[None].branch()
80 tip = self.repo.branchtags()[bname]
82 if not create: raise ke
84 # create the branch on the fly
86 # first switch to default branch
87 default_tip = self.repo.branchtags()['default']
88 mercurial.merge.update(self.repo, default_tip, False, True, None)
90 # set the dirstate to new branch
91 self.repo.dirstate.setbranch(bname)
92 self._commit('Initial commit for automatic branch "%s".' % bname, user="django-admin")
95 tip = self.repo.branchtags()[bname]
97 upstats = mercurial.merge.update(self.repo, tip, False, True, None)
100 raise RepositoryException("Can't switch to branch '%s': no such branch." % bname , ke)
101 except util.Abort, ae:
102 raise RepositoryException("Can't switch to branch '%s': %s" % (bname, ae.message), ae)
106 def write_lock(self):
107 """Returns w write lock to the repository."""
108 return self.repo.wlock()
111 class RepositoryException(Exception):
113 def __init__(self, msg, cause=None):
114 Exception.__init__(self, msg)
117 class RepositoryDoesNotExist(RepositoryException):