1 # -*- coding: utf-8 -*-
4 from mercurial import localrepo, ui, error, match, node, encoding
7 encoding.encoding = 'utf-8'
10 class RepositoryDoesNotExist(Exception):
14 class Repository(object):
15 """Abstrakcja repozytorium Mercurial. DziaĆa z Mercurial w wersji 1.3.1."""
17 def __init__(self, path, create=False):
19 self.ui.config('ui', 'quiet', 'true')
20 self.ui.config('ui', 'interactive', 'false')
22 self.real_path = os.path.realpath(path)
23 self.repo = self.open_repository(self.real_path, create)
24 self._pending_files = []
26 def open_repository(self, path, create=False):
27 if os.path.isdir(path):
29 return localrepo.localrepository(self.ui, path)
30 except error.RepoError:
31 # dir is not an hg repo, we must init it
33 return localrepo.localrepository(self.ui, path, create=1)
36 return localrepo.localrepository(self.ui, path, create=1)
37 raise RepositoryDoesNotExist("Repository %s does not exist." % path)
40 return list(self.repo['tip'])
42 def get_file(self, path):
43 ctx = self.repo.changectx(None)
44 return ctx.filectx(path)
46 def add_file(self, path, value):
47 f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8')
51 if path not in self._pending_files:
52 self._pending_files.append(path)
54 def commit(self, message=u'hgshelve auto commit', key=None, user=None):
56 Commit unsynchronized data to disk.
59 - message: mercurial's changeset message
60 - key: supply to sync only one key
62 if isinstance(message, unicode):
63 message = message.encode('utf-8')
64 if isinstance(user, unicode):
65 user = user.encode('utf-8')
73 # first of all, add absent data and clean removed
75 # will commit all keys
76 pending_files = self._pending_files
78 if keys not in self._pending_files:
83 for path in pending_files:
84 files_to_commit.append(path)
85 if path in self.all_files():
86 if not os.path.exists(os.path.join(self.real_path, path)):
88 files_to_remove.append(path)
91 files_to_add.append(path)
94 self.repo.add(files_to_add)
97 self.repo.forget(files_to_remove)
100 for i, f in enumerate(files_to_commit):
101 if isinstance(f, unicode):
102 files_to_commit[i] = f.encode('utf-8')
103 matcher = match.match(self.repo.root, self.repo.root, files_to_commit, default='path')
104 rev = self.repo.commit(message, user=user, match=matcher)
107 for key in pending_files:
108 self._pending_files.remove(key)
111 # self._keys = self.get_persisted_objects_keys()
112 # return node.hex(rev)