Wielolinijkowy toolbar z dostosowywaniem wysokosci edytora.
[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
11
12 class Repository(object):
13     """Abstrakcja repozytorium Mercurial. DziaƂa z Mercurial w wersji 1.3.1."""
14     
15     def __init__(self, path, create=False):
16         self.ui = ui.ui()
17         self.ui.config('ui', 'quiet', 'true')
18         self.ui.config('ui', 'interactive', 'false')
19         
20         self.real_path = os.path.realpath(path)
21         self.repo = self.open_repository(self.real_path, create)
22         self._pending_files = []
23     
24     def open_repository(self, path, create=False):
25         if os.path.isdir(path):
26             try:
27                 return localrepo.localrepository(self.ui, path)
28             except mercurial.error.RepoError:
29                 # dir is not an hg repo, we must init it
30                 if create:
31                     return localrepo.localrepository(self.ui, path, create=1)
32         elif create:
33             os.makedirs(path)
34             return localrepo.localrepository(self.ui, path, create=1)
35         raise RepositoryDoesNotExist("Repository %s does not exist." % path)
36         
37     def file_list(self, branch):
38         return self.in_branch(lambda: self._file_list(), branch)
39
40     def _file_list(self):
41         return list(self.repo[None])
42     
43     def get_file(self, path, branch):
44         return self.in_branch(lambda: self._get_file(path), branch)
45
46     def _get_file(self, path):
47         if not self._file_exists(path):
48             raise RepositoryException("File not availble in this branch.")
49         
50         return self.repo.wread(path)
51
52     def file_exists(self, path, branch):
53         return self.in_branch(lambda: self._file_exists(path), branch)
54
55     def _file_exists(self, path):
56         return self.repo.dirstate[path] != "?"
57
58     def write_file(self, path, value, branch):
59         return self.in_branch(lambda: self._write_file(path, value), branch)
60
61     def _write_file(self, path, value):        
62         return self.repo.wwrite(path, value, [])
63
64     def add_file(self, path, value, branch):
65         return self.in_branch(lambda: self._add_file(path, value), branch)
66
67     def _add_file(self, path, value):
68         self._write_file(path, value)
69         return self.repo.add( [path] )
70
71     def _commit(self, message, user=None):
72         return self.repo.commit(text=message, user=user)
73     
74     def commit(self, message, branch, user=None):
75         return self.in_branch(lambda: self._commit(message, key=key, user=user), branch)
76
77     def in_branch(self, action, bname):
78         wlock = self.repo.wlock()
79         try:
80             old = self._switch_to_branch(bname)
81             try:
82                 # do some stuff
83                 return action()
84             finally:
85                 self._switch_to_branch(old)
86         finally:
87             wlock.release()
88
89     def _switch_to_branch(self, bname, create=True):
90         wlock = self.repo.wlock()
91         try:
92             current = self.repo[None].branch()
93             if current == bname:
94                 return current
95             try:
96                 tip = self.repo.branchtags()[bname]
97             except KeyError, ke:
98                 if not create: raise ke
99                 
100                 # create the branch on the fly
101
102                 # first switch to default branch
103                 default_tip = self.repo.branchtags()['default']
104                 mercurial.merge.update(self.repo, default_tip, False, True, None)
105
106                 # set the dirstate to new branch
107                 self.repo.dirstate.setbranch(bname)
108                 self._commit('Initial commit for automatic branch "%s".' % bname, user="django-admin")
109
110                 # collect the new tip
111                 tip = self.repo.branchtags()[bname]
112
113             upstats = mercurial.merge.update(self.repo, tip, False, True, None)
114             return current
115         except KeyError, ke:
116             raise RepositoryException("Can't switch to branch '%s': no such branch." % bname , ke)
117         except util.Abort, ae:
118             raise RepositoryException("Can't switch to branch '%s': %s"  % (bname, ae.message), ae)
119         finally:
120             wlock.release()
121
122     def write_lock(self):
123         """Returns w write lock to the repository."""
124         return self.repo.wlock()
125
126
127 class RepositoryException(Exception):
128
129     def __init__(self, msg, cause=None):
130         Exception.__init__(self, msg)
131         self.cause = cause
132
133 class RepositoryDoesNotExist(RepositoryException):
134     pass