Ignoruj netbeansy.
[redakcja.git] / lib / hg.py
1 # -*- coding: utf-8 -*-
2 import os
3 import codecs
4 from mercurial import localrepo, ui, match, node, encoding, util
5 import mercurial.merge, mercurial.error
6
7 encoding.encoding = 'utf-8'
8
9
10 class RepositoryDoesNotExist(Exception):
11     pass
12
13 class Repository(object):
14     """Abstrakcja repozytorium Mercurial. DziaƂa z Mercurial w wersji 1.3.1."""
15     
16     def __init__(self, path, create=False):
17         self.ui = ui.ui()
18         self.ui.config('ui', 'quiet', 'true')
19         self.ui.config('ui', 'interactive', 'false')
20         
21         self.real_path = os.path.realpath(path)
22         self.repo = self.open_repository(self.real_path, create)
23         self._pending_files = []
24     
25     def open_repository(self, path, create=False):
26         if os.path.isdir(path):
27             try:
28                 return localrepo.localrepository(self.ui, path)
29             except mercurial.error.RepoError:
30                 # dir is not an hg repo, we must init it
31                 if create:
32                     return localrepo.localrepository(self.ui, path, create=1)
33         elif create:
34             os.makedirs(path)
35             return localrepo.localrepository(self.ui, path, create=1)
36         raise RepositoryDoesNotExist("Repository %s does not exist." % path)
37         
38     def all_files(self, branch='default'):
39         return self.in_branch(lambda: self._all_files(), branch)
40
41     def _all_files(self):
42         return list(self.repo[None])
43     
44     def get_file(self, path, branch='default'):
45         return self.in_branch(lambda: self._get_file(path), branch)
46
47     def _get_file(self, path):
48         return self.repo.wread(path)
49     
50     def add_file(self, path, value, branch='default'):
51         return self.in_branch(lambda: self._add_file(path, value), branch)
52
53     def _add_file(self, path, value):
54         return self.repo.wwrite(path, value.encode('utf-8'), [])
55 #        f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8')
56 #        f.write(value)
57 #        f.close()
58 #        if path not in self._pending_files:
59 #            self._pending_files.append(path)
60
61     def _commit(self, message, user=None):
62         return self.repo.commit(text=message, user=user)
63     
64     def commit(self, message, user=None, branch='default'):
65         return self.in_branch(lambda: self._commit(message, key=key, user=user), branch)
66
67     def in_branch(self, action, bname='default'):
68         wlock = self.repo.wlock()
69         try:
70             old = self._switch_to_branch(bname)
71             try:
72                 # do some stuff
73                 return action()
74             finally:
75                 self._switch_to_branch(old)
76         finally:
77             wlock.release()
78
79     def _switch_to_branch(self, bname, create=True):
80         wlock = self.repo.wlock()
81         try:
82             current = self.repo[None].branch()
83             if current == bname:
84                 return current
85             try:
86                 tip = self.repo.branchtags()[bname]
87             except KeyError, ke:
88                 if not create: raise ke
89                 
90                 # create the branch on the fly
91
92                 # first switch to default branch
93                 default_tip = self.repo.branchtags()['default']
94                 mercurial.merge.update(self.repo, default_tip, False, True, None)
95
96                 # set the dirstate to new branch
97                 self.repo.dirstate.setbranch(bname)
98                 self._commit('Initial commit for automatic branch "%s".' % bname, user="django-admin")
99
100                 # collect the new tip
101                 tip = self.repo.branchtags()[bname]
102
103             upstats = mercurial.merge.update(self.repo, tip, False, True, None)
104             return current
105         except KeyError, ke:
106             raise RepositoryException("Can't switch to branch '%s': no such branch." % bname , ke)
107         except util.Abort, ae:
108             raise repositoryException("Can't switch to branch '%s': %s"  % (bname, ae.message), ae)
109         finally:
110             wlock.release()
111
112     def write_lock(self):
113         """Returns w write lock to the repository."""
114         return self.repo.wlock()
115
116
117 class RepositoryException(Exception):
118
119     def __init__(self, msg, cause=None):
120         Exception.__init__(self, msg)
121         self.cause = cause