1 # -*- coding: utf-8 -*-
4 from mercurial import localrepo, ui, error, match, node
7 class RepositoryDoesNotExist(Exception):
11 class Repository(object):
12 """Abstrakcja repozytorium Mercurial. DziaĆa z Mercurial w wersji 1.3.1."""
14 def __init__(self, path, create=False):
16 self.ui.config('ui', 'quiet', 'true')
17 self.ui.config('ui', 'interactive', 'false')
19 self.real_path = os.path.realpath(path)
20 self.repo = self.open_repository(self.real_path, create)
21 self._pending_files = []
23 def open_repository(self, path, create=False):
24 if os.path.isdir(path):
26 return localrepo.localrepository(self.ui, path)
27 except error.RepoError:
28 # dir is not an hg repo, we must init it
30 return localrepo.localrepository(self.ui, path, create=1)
33 return localrepo.localrepository(self.ui, path, create=1)
34 raise RepositoryDoesNotExist("Repository %s does not exist." % path)
37 return list(self.repo['tip'])
39 def get_file(self, path):
40 ctx = self.repo.changectx(None)
41 return ctx.filectx(path)
43 def add_file(self, path, value):
44 f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8')
48 if path not in self._pending_files:
49 self._pending_files.append(path)
51 def commit(self, message='hgshelve auto commit', key=None, user=None):
53 Commit unsynchronized data to disk.
56 - message: mercurial's changeset message
57 - key: supply to sync only one key
65 # first of all, add absent data and clean removed
67 # will commit all keys
68 pending_files = self._pending_files
70 if key not in self._pending_files:
75 for path in pending_files:
76 files_to_commit.append(path)
77 if path in self.all_files():
78 if not os.path.exists(os.path.join(self.real_path, path)):
80 files_to_remove.append(path)
83 files_to_add.append(path)
86 self.repo.add(files_to_add)
89 self.repo.forget(files_to_remove)
92 matcher = match.match(self.repo.root, self.repo.root, files_to_commit, default='path')
93 rev = self.repo.commit(message, user=user, match=matcher)
96 for key in pending_files:
97 self._pending_files.remove(key)
100 # self._keys = self.get_persisted_objects_keys()
101 # return node.hex(rev)