1 # -*- coding: utf-8 -*-
3 from mercurial import localrepo, ui, encoding, util
4 import mercurial.merge, mercurial.error
6 encoding.encoding = 'utf-8'
8 X = 'g\xc5\xbceg\xc5\xbc\xc3\xb3\xc5\x82ka'
10 def sanitize_string(path):
11 if isinstance(path, unicode): #
12 return path.encode('utf-8')
13 else: # it's a string, so we have no idea what encoding it is
16 class Repository(object):
17 """Abstrakcja repozytorium Mercurial. Działa z Mercurial w wersji 1.3.1."""
19 def __init__(self, path, create=False):
21 self.ui.config('ui', 'quiet', 'true')
22 self.ui.config('ui', 'interactive', 'false')
24 self.real_path = sanitize_string(os.path.realpath(path))
25 self.repo = self._open_repository(self.real_path, create)
27 def _open_repository(self, path, create=False):
28 if os.path.isdir(path):
30 return localrepo.localrepository(self.ui, path)
31 except mercurial.error.RepoError:
32 # dir is not an hg repo, we must init it
34 return localrepo.localrepository(self.ui, path, create=1)
37 return localrepo.localrepository(self.ui, path, create=1)
38 raise RepositoryDoesNotExist("Repository %s does not exist." % path)
40 def file_list(self, branch):
41 return self.in_branch(lambda: self._file_list(), branch)
44 return list(self.repo[None])
46 def get_file(self, path, branch):
47 return self.in_branch(lambda: self._get_file(path), branch)
49 def _get_file(self, path):
50 path = sanitize_string(path)
51 if not self._file_exists(path):
52 raise RepositoryException("File not availble in this branch.")
54 return self.repo.wread(path)
56 def file_exists(self, path, branch):
57 return self.in_branch(lambda: self._file_exists(path), branch)
59 def _file_exists(self, path):
60 path = sanitize_string(path)
61 return self.repo.dirstate[path] != "?"
63 def write_file(self, path, value, branch):
64 return self.in_branch(lambda: self._write_file(path, value), branch)
66 def _write_file(self, path, value):
67 path = sanitize_string(path)
68 return self.repo.wwrite(path, value, [])
70 def add_file(self, path, value, branch):
71 return self.in_branch(lambda: self._add_file(path, value), branch)
73 def _add_file(self, path, value):
74 path = sanitize_string(path)
75 self._write_file(path, value)
76 return self.repo.add( [path] )
78 def _commit(self, message, user=None):
79 return self.repo.commit(text=sanitize_string(message), user=sanitize_string(user))
81 def commit(self, message, branch, user=None):
82 return self.in_branch(lambda: self._commit(message, key=key, user=user), branch)
84 def in_branch(self, action, bname):
85 wlock = self.repo.wlock()
87 old = self._switch_to_branch(bname)
92 self._switch_to_branch(old)
96 def merge_branches(self, bnameA, bnameB, user, message):
97 wlock = self.repo.wlock()
99 return self.merge_revisions(self.get_branch_tip(bnameA),
100 self.get_branch_tip(bnameB), user, message)
104 def diff(self, revA, revB):
105 return UpdateStatus(self.repo.status(revA, revB))
107 def merge_revisions(self, revA, revB, user, message):
108 wlock = self.repo.wlock()
110 old = self.repo[None]
113 mergestatus = self._merge(revB)
114 if not mergestatus.isclean():
115 # revert the failed merge
117 raise UncleanMerge(u'Failed to merge %d files.' % len(mergestatus.unresolved))
119 # commit the clean merge
120 self._commit(message, user)
122 # cleanup after yourself
123 self._checkout(old.rev())
124 except util.Abort, ae:
125 raise RepositoryException(u'Failed merge: ' + ae.message)
129 def common_ancestor(self, revA, revB):
130 return self.repo[revA].ancestor(self.repo[revB])
132 def _checkout(self, rev, force=True):
133 return MergeStatus(mercurial.merge.update(self.repo, rev, False, force, None))
135 def _merge(self, rev):
136 """ Merge the revision into current working directory """
137 return MergeStatus(mercurial.merge.update(self.repo, rev, True, False, None))
139 def _switch_to_branch(self, bname):
140 bname = sanitize_string(bname)
141 wlock = self.repo.wlock()
143 current = self.repo[None].branch()
147 tip = self.get_branch_tip(bname)
148 status = self._checkout(tip)
150 if not status.isclean():
151 raise RepositoryException("Unclean branch switch. This IS REALLY bad.")
155 raise RepositoryException((u"Can't switch to branch '%s': no such branch." % bname) , ke)
156 except util.Abort, ae:
157 raise RepositoryException(u"Can't switch to branch '%s': %s" % (bname, ae.message), ae)
161 def with_wlock(self, action):
162 wlock = self.repo.wlock()
168 def _create_branch(self, name, parent_rev, msg=None, before_commit=None):
169 """WARNING: leaves the working directory in the new branch"""
170 name = sanitize_string(name)
172 if self.has_branch(name): return # just exit
174 self._checkout(parent_rev)
175 self.repo.dirstate.setbranch(name)
178 msg = "Initial commit for branch '%s'." % name
180 if before_commit: before_commit()
181 self._commit(msg, user='platform')
182 return self.get_branch_tip(name)
184 def write_lock(self):
185 """Returns w write lock to the repository."""
186 return self.repo.wlock()
188 def has_branch(self, name):
189 name = sanitize_string(name)
190 return (name in self.repo.branchmap().keys())
192 def get_branch_tip(self, name):
193 name = sanitize_string(name)
194 return self.repo.branchtags()[name]
196 def getnode(self, rev):
197 return self.repo[rev]
199 class MergeStatus(object):
201 def __init__(self, mstatus):
202 self.updated = mstatus[0]
203 self.merged = mstatus[1]
204 self.removed = mstatus[2]
205 self.unresolved = mstatus[3]
208 return self.unresolved == 0
210 class UpdateStatus(object):
212 def __init__(self, mstatus):
213 self.modified = mstatus[0]
214 self.added = mstatus[1]
215 self.removed = mstatus[2]
216 self.deleted = mstatus[3]
217 self.untracked = mstatus[4]
218 self.ignored = mstatus[5]
219 self.clean = mstatus[6]
221 def has_changes(self):
222 return bool( len(self.modified) + len(self.added) + \
223 len(self.removed) + len(self.deleted) )
225 class RepositoryException(Exception):
226 def __init__(self, msg, cause=None):
227 Exception.__init__(self, msg)
230 class UncleanMerge(RepositoryException):
233 class RepositoryDoesNotExist(RepositoryException):