1 # -*- coding: utf-8 -*-
4 from mercurial import localrepo, ui, error, match, node, encoding
6 encoding.encoding = 'utf-8'
8 class RepositoryDoesNotExist(Exception):
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 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)
38 return list(self.repo['tip'])
40 def get_file(self, path):
41 ctx = self.repo.changectx(None)
42 return ctx.filectx(path)
44 def add_file(self, path, value):
45 f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8')
49 if path not in self._pending_files:
50 self._pending_files.append(path)
52 def commit(self, message=u'hgshelve auto commit', key=None, user=None):
54 Commit unsynchronized data to disk.
57 - message: mercurial's changeset message
58 - key: supply to sync only one key
60 if isinstance(message, unicode):
61 message = message.encode('utf-8')
62 if isinstance(user, unicode):
63 user = user.encode('utf-8')
71 # first of all, add absent data and clean removed
73 # will commit all keys
74 pending_files = self._pending_files
76 if keys not in self._pending_files:
81 for path in pending_files:
82 files_to_commit.append(path)
83 if path in self.all_files():
84 if not os.path.exists(os.path.join(self.real_path, path)):
86 files_to_remove.append(path)
89 files_to_add.append(path)
92 self.repo.add(files_to_add)
95 self.repo.forget(files_to_remove)
98 for i, f in enumerate(files_to_commit):
99 if isinstance(f, unicode):
100 files_to_commit[i] = f.encode('utf-8')
101 matcher = match.match(self.repo.root, self.repo.root, files_to_commit, default='path')
102 rev = self.repo.commit(message, user=user, match=matcher)
105 for key in pending_files:
106 self._pending_files.remove(key)
109 # self._keys = self.get_persisted_objects_keys()
110 # return node.hex(rev)
112 def in_branch(self, branch_name, action):
113 wlock = self.repo.wlock()
115 current_branch = self.repo[None].branch()
116 self.repo.dirstate.setbranch(branch_name)
121 self.repo.dirstate.setbranch(current_branch)
125 def write_lock(self):
126 """Returns w write lock to the repository."""
127 return self.repo.wlock()