Mercurial > hg
annotate hgext/convert/bzr.py @ 8096:a110d7a20f3f
run-tests: upper-case global PYTHON variable
author | Martin Geisler <mg@lazybytes.net> |
---|---|
date | Tue, 21 Apr 2009 10:53:42 +0200 |
parents | f3ef8a352d83 |
children | 17ab4dab50a6 |
rev | line source |
---|---|
7053 | 1 # bzr support for the convert extension |
2 # This module is for handling 'bzr', that was formerly known as Bazaar-NG; | |
3 # it cannot access 'bar' repositories, but they were never used very much | |
4 | |
5 import os | |
6 from mercurial import demandimport | |
7 # these do not work with demandimport, blacklist | |
8 demandimport.ignore.extend([ | |
9 'bzrlib.transactions', | |
10 'bzrlib.urlutils', | |
11 ]) | |
12 | |
13 from mercurial.i18n import _ | |
14 from mercurial import util | |
15 from common import NoRepo, commit, converter_source | |
16 | |
17 try: | |
18 # bazaar imports | |
19 from bzrlib import branch, revision, errors | |
20 from bzrlib.revisionspec import RevisionSpec | |
21 except ImportError: | |
22 pass | |
23 | |
8045
e09a2f2ef85d
convert/bzr: fix file rename replaced by a dir case (issue1583)
Patrick Mezard <pmezard@gmail.com>
parents:
8035
diff
changeset
|
24 supportedkinds = ('file', 'symlink') |
e09a2f2ef85d
convert/bzr: fix file rename replaced by a dir case (issue1583)
Patrick Mezard <pmezard@gmail.com>
parents:
8035
diff
changeset
|
25 |
7053 | 26 class bzr_source(converter_source): |
27 """Reads Bazaar repositories by using the Bazaar Python libraries""" | |
28 | |
29 def __init__(self, ui, path, rev=None): | |
30 super(bzr_source, self).__init__(ui, path, rev=rev) | |
31 | |
7973
db3a68fd9387
convert: attempt to check repo type before checking for tool
Matt Mackall <mpm@selenic.com>
parents:
7060
diff
changeset
|
32 if not os.path.exists(os.path.join(path, '.bzr')): |
db3a68fd9387
convert: attempt to check repo type before checking for tool
Matt Mackall <mpm@selenic.com>
parents:
7060
diff
changeset
|
33 raise NoRepo('%s does not look like a Bazaar repo' % path) |
db3a68fd9387
convert: attempt to check repo type before checking for tool
Matt Mackall <mpm@selenic.com>
parents:
7060
diff
changeset
|
34 |
7053 | 35 try: |
36 # access bzrlib stuff | |
37 branch | |
38 except NameError: | |
39 raise NoRepo('Bazaar modules could not be loaded') | |
40 | |
41 path = os.path.abspath(path) | |
42 self.branch = branch.Branch.open(path) | |
43 self.sourcerepo = self.branch.repository | |
44 self._parentids = {} | |
45 | |
46 def before(self): | |
47 """Before the conversion begins, acquire a read lock | |
48 for all the operations that might need it. Fortunately | |
49 read locks don't block other reads or writes to the | |
50 repository, so this shouldn't have any impact on the usage of | |
51 the source repository. | |
52 | |
53 The alternative would be locking on every operation that | |
54 needs locks (there are currently two: getting the file and | |
55 getting the parent map) and releasing immediately after, | |
56 but this approach can take even 40% longer.""" | |
57 self.sourcerepo.lock_read() | |
58 | |
59 def after(self): | |
60 self.sourcerepo.unlock() | |
61 | |
62 def getheads(self): | |
63 if not self.rev: | |
64 return [self.branch.last_revision()] | |
65 try: | |
66 r = RevisionSpec.from_string(self.rev) | |
67 info = r.in_history(self.branch) | |
68 except errors.BzrError: | |
69 raise util.Abort(_('%s is not a valid revision in current branch') | |
70 % self.rev) | |
71 return [info.rev_id] | |
72 | |
73 def getfile(self, name, rev): | |
74 revtree = self.sourcerepo.revision_tree(rev) | |
75 fileid = revtree.path2id(name) | |
8045
e09a2f2ef85d
convert/bzr: fix file rename replaced by a dir case (issue1583)
Patrick Mezard <pmezard@gmail.com>
parents:
8035
diff
changeset
|
76 if fileid is None or revtree.kind(fileid) not in supportedkinds: |
7053 | 77 # the file is not available anymore - was deleted |
78 raise IOError(_('%s is not available in %s anymore') % | |
79 (name, rev)) | |
80 sio = revtree.get_file(fileid) | |
81 return sio.read() | |
82 | |
83 def getmode(self, name, rev): | |
84 return self._modecache[(name, rev)] | |
85 | |
86 def getchanges(self, version): | |
87 # set up caches: modecache and revtree | |
88 self._modecache = {} | |
89 self._revtree = self.sourcerepo.revision_tree(version) | |
90 # get the parentids from the cache | |
91 parentids = self._parentids.pop(version) | |
92 # only diff against first parent id | |
93 prevtree = self.sourcerepo.revision_tree(parentids[0]) | |
94 return self._gettreechanges(self._revtree, prevtree) | |
95 | |
96 def getcommit(self, version): | |
97 rev = self.sourcerepo.get_revision(version) | |
98 # populate parent id cache | |
99 if not rev.parent_ids: | |
100 parents = [] | |
101 self._parentids[version] = (revision.NULL_REVISION,) | |
102 else: | |
103 parents = self._filterghosts(rev.parent_ids) | |
104 self._parentids[version] = parents | |
105 | |
106 return commit(parents=parents, | |
107 # bzr uses 1 second timezone precision | |
108 date='%d %d' % (rev.timestamp, rev.timezone / 3600), | |
109 author=self.recode(rev.committer), | |
110 # bzr returns bytestrings or unicode, depending on the content | |
111 desc=self.recode(rev.message), | |
112 rev=version) | |
113 | |
114 def gettags(self): | |
115 if not self.branch.supports_tags(): | |
116 return {} | |
117 tagdict = self.branch.tags.get_tag_dict() | |
118 bytetags = {} | |
119 for name, rev in tagdict.iteritems(): | |
120 bytetags[self.recode(name)] = rev | |
121 return bytetags | |
122 | |
123 def getchangedfiles(self, rev, i): | |
124 self._modecache = {} | |
125 curtree = self.sourcerepo.revision_tree(rev) | |
126 parentids = self._parentids.pop(rev) | |
127 if i is not None: | |
128 parentid = parentids[i] | |
129 else: | |
130 # no parent id, get the empty revision | |
131 parentid = revision.NULL_REVISION | |
132 | |
133 prevtree = self.sourcerepo.revision_tree(parentid) | |
134 changes = [e[0] for e in self._gettreechanges(curtree, prevtree)[0]] | |
135 return changes | |
136 | |
137 def _gettreechanges(self, current, origin): | |
138 revid = current._revision_id; | |
139 changes = [] | |
140 renames = {} | |
141 for (fileid, paths, changed_content, versioned, parent, name, | |
142 kind, executable) in current.iter_changes(origin): | |
143 | |
144 if paths[0] == u'' or paths[1] == u'': | |
145 # ignore changes to tree root | |
146 continue | |
147 | |
148 # bazaar tracks directories, mercurial does not, so | |
149 # we have to rename the directory contents | |
150 if kind[1] == 'directory': | |
151 if None not in paths and paths[0] != paths[1]: | |
152 # neither an add nor an delete - a move | |
153 # rename all directory contents manually | |
154 subdir = origin.inventory.path2id(paths[0]) | |
155 # get all child-entries of the directory | |
156 for name, entry in origin.inventory.iter_entries(subdir): | |
157 # hg does not track directory renames | |
158 if entry.kind == 'directory': | |
159 continue | |
160 frompath = self.recode(paths[0] + '/' + name) | |
161 topath = self.recode(paths[1] + '/' + name) | |
162 # register the files as changed | |
163 changes.append((frompath, revid)) | |
164 changes.append((topath, revid)) | |
165 # add to mode cache | |
166 mode = ((entry.executable and 'x') or (entry.kind == 'symlink' and 's') | |
167 or '') | |
168 self._modecache[(topath, revid)] = mode | |
169 # register the change as move | |
170 renames[topath] = frompath | |
171 | |
172 # no futher changes, go to the next change | |
173 continue | |
174 | |
175 # we got unicode paths, need to convert them | |
176 path, topath = [self.recode(part) for part in paths] | |
177 | |
178 if topath is None: | |
179 # file deleted | |
180 changes.append((path, revid)) | |
181 continue | |
182 | |
183 # renamed | |
184 if path and path != topath: | |
185 renames[topath] = path | |
8035
cb77c0fbec39
convert: remove renamed source files (issue1505)
Xavier ALT <dex@phoenix-ind.net>
parents:
7060
diff
changeset
|
186 changes.append((path, revid)) |
7053 | 187 |
188 # populate the mode cache | |
189 kind, executable = [e[1] for e in (kind, executable)] | |
190 mode = ((executable and 'x') or (kind == 'symlink' and 's') | |
191 or '') | |
192 self._modecache[(topath, revid)] = mode | |
193 changes.append((topath, revid)) | |
194 | |
195 return changes, renames | |
196 | |
197 def _filterghosts(self, ids): | |
198 """Filters out ghost revisions which hg does not support, see | |
199 <http://bazaar-vcs.org/GhostRevision> | |
200 """ | |
201 parentmap = self.sourcerepo.get_parent_map(ids) | |
7060
972cce34f345
convert: fixed python2.3 incompatibility in bzr source (generator expression)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7053
diff
changeset
|
202 parents = tuple([parent for parent in ids if parent in parentmap]) |
7053 | 203 return parents |
204 | |
205 def recode(self, s, encoding=None): | |
206 """This version of recode tries to encode unicode to bytecode, | |
207 and preferably using the UTF-8 codec. | |
208 Other types than Unicode are silently returned, this is by | |
209 intention, e.g. the None-type is not going to be encoded but instead | |
210 just passed through | |
211 """ | |
212 if not encoding: | |
213 encoding = self.encoding or 'utf-8' | |
214 | |
215 if isinstance(s, unicode): | |
216 return s.encode(encoding) | |
217 else: | |
218 # leave it alone | |
219 return s |