author | Patrick Mezard <pmezard@gmail.com> |
Fri, 15 May 2009 16:12:09 +0200 | |
changeset 8423 | eb7be0e752d9 |
parent 8165 | 78658990c725 |
child 8434 | 4a3e7c380834 |
child 8783 | 6556d4145122 |
permissions | -rw-r--r-- |
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 |
||
32 |
try: |
|
33 |
# access bzrlib stuff |
|
34 |
branch |
|
35 |
except NameError: |
|
36 |
raise NoRepo('Bazaar modules could not be loaded') |
|
37 |
||
38 |
if not os.path.exists(os.path.join(path, '.bzr')): |
|
39 |
raise NoRepo('%s does not look like a Bazaar repo' % path) |
|
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) |
|
8423
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
76 |
kind = None |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
77 |
if fileid is not None: |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
78 |
kind = revtree.kind(fileid) |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
79 |
if kind not in supportedkinds: |
7053 | 80 |
# the file is not available anymore - was deleted |
81 |
raise IOError(_('%s is not available in %s anymore') % |
|
82 |
(name, rev)) |
|
8423
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
83 |
if kind == 'symlink': |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
84 |
target = revtree.get_symlink_target(fileid) |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
85 |
if target is None: |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
86 |
raise util.Abort(_('%s.%s symlink has no target') |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
87 |
% (name, rev)) |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
88 |
return target |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
89 |
else: |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
90 |
sio = revtree.get_file(fileid) |
eb7be0e752d9
convert/bzr: fix symlinks target (issue1626/2)
Patrick Mezard <pmezard@gmail.com>
parents:
8165
diff
changeset
|
91 |
return sio.read() |
7053 | 92 |
|
93 |
def getmode(self, name, rev): |
|
94 |
return self._modecache[(name, rev)] |
|
95 |
||
96 |
def getchanges(self, version): |
|
97 |
# set up caches: modecache and revtree |
|
98 |
self._modecache = {} |
|
99 |
self._revtree = self.sourcerepo.revision_tree(version) |
|
100 |
# get the parentids from the cache |
|
101 |
parentids = self._parentids.pop(version) |
|
102 |
# only diff against first parent id |
|
103 |
prevtree = self.sourcerepo.revision_tree(parentids[0]) |
|
104 |
return self._gettreechanges(self._revtree, prevtree) |
|
105 |
||
106 |
def getcommit(self, version): |
|
107 |
rev = self.sourcerepo.get_revision(version) |
|
108 |
# populate parent id cache |
|
109 |
if not rev.parent_ids: |
|
110 |
parents = [] |
|
111 |
self._parentids[version] = (revision.NULL_REVISION,) |
|
112 |
else: |
|
113 |
parents = self._filterghosts(rev.parent_ids) |
|
114 |
self._parentids[version] = parents |
|
115 |
||
116 |
return commit(parents=parents, |
|
117 |
# bzr uses 1 second timezone precision |
|
118 |
date='%d %d' % (rev.timestamp, rev.timezone / 3600), |
|
119 |
author=self.recode(rev.committer), |
|
120 |
# bzr returns bytestrings or unicode, depending on the content |
|
121 |
desc=self.recode(rev.message), |
|
122 |
rev=version) |
|
123 |
||
124 |
def gettags(self): |
|
125 |
if not self.branch.supports_tags(): |
|
126 |
return {} |
|
127 |
tagdict = self.branch.tags.get_tag_dict() |
|
128 |
bytetags = {} |
|
129 |
for name, rev in tagdict.iteritems(): |
|
130 |
bytetags[self.recode(name)] = rev |
|
131 |
return bytetags |
|
132 |
||
133 |
def getchangedfiles(self, rev, i): |
|
134 |
self._modecache = {} |
|
135 |
curtree = self.sourcerepo.revision_tree(rev) |
|
136 |
if i is not None: |
|
8165
78658990c725
convert/bzr: make it work with filemaps (issue1631)
Patrick Mezard <pmezard@gmail.com>
parents:
8148
diff
changeset
|
137 |
parentid = self._parentids[rev][i] |
7053 | 138 |
else: |
139 |
# no parent id, get the empty revision |
|
140 |
parentid = revision.NULL_REVISION |
|
141 |
||
142 |
prevtree = self.sourcerepo.revision_tree(parentid) |
|
143 |
changes = [e[0] for e in self._gettreechanges(curtree, prevtree)[0]] |
|
144 |
return changes |
|
145 |
||
146 |
def _gettreechanges(self, current, origin): |
|
147 |
revid = current._revision_id; |
|
148 |
changes = [] |
|
149 |
renames = {} |
|
150 |
for (fileid, paths, changed_content, versioned, parent, name, |
|
151 |
kind, executable) in current.iter_changes(origin): |
|
152 |
||
153 |
if paths[0] == u'' or paths[1] == u'': |
|
154 |
# ignore changes to tree root |
|
155 |
continue |
|
156 |
||
157 |
# bazaar tracks directories, mercurial does not, so |
|
158 |
# we have to rename the directory contents |
|
159 |
if kind[1] == 'directory': |
|
8126
13b36eb14324
convert/bzr: handle files replaced by directories (issue1623)
Patrick Mezard <pmezard@gmail.com>
parents:
8045
diff
changeset
|
160 |
if kind[0] not in (None, 'directory'): |
13b36eb14324
convert/bzr: handle files replaced by directories (issue1623)
Patrick Mezard <pmezard@gmail.com>
parents:
8045
diff
changeset
|
161 |
# Replacing 'something' with a directory, record it |
13b36eb14324
convert/bzr: handle files replaced by directories (issue1623)
Patrick Mezard <pmezard@gmail.com>
parents:
8045
diff
changeset
|
162 |
# so it can be removed. |
13b36eb14324
convert/bzr: handle files replaced by directories (issue1623)
Patrick Mezard <pmezard@gmail.com>
parents:
8045
diff
changeset
|
163 |
changes.append((self.recode(paths[0]), revid)) |
13b36eb14324
convert/bzr: handle files replaced by directories (issue1623)
Patrick Mezard <pmezard@gmail.com>
parents:
8045
diff
changeset
|
164 |
|
7053 | 165 |
if None not in paths and paths[0] != paths[1]: |
166 |
# neither an add nor an delete - a move |
|
167 |
# rename all directory contents manually |
|
168 |
subdir = origin.inventory.path2id(paths[0]) |
|
169 |
# get all child-entries of the directory |
|
170 |
for name, entry in origin.inventory.iter_entries(subdir): |
|
171 |
# hg does not track directory renames |
|
172 |
if entry.kind == 'directory': |
|
173 |
continue |
|
174 |
frompath = self.recode(paths[0] + '/' + name) |
|
175 |
topath = self.recode(paths[1] + '/' + name) |
|
176 |
# register the files as changed |
|
177 |
changes.append((frompath, revid)) |
|
178 |
changes.append((topath, revid)) |
|
179 |
# add to mode cache |
|
180 |
mode = ((entry.executable and 'x') or (entry.kind == 'symlink' and 's') |
|
181 |
or '') |
|
182 |
self._modecache[(topath, revid)] = mode |
|
183 |
# register the change as move |
|
184 |
renames[topath] = frompath |
|
185 |
||
186 |
# no futher changes, go to the next change |
|
187 |
continue |
|
188 |
||
189 |
# we got unicode paths, need to convert them |
|
190 |
path, topath = [self.recode(part) for part in paths] |
|
191 |
||
192 |
if topath is None: |
|
193 |
# file deleted |
|
194 |
changes.append((path, revid)) |
|
195 |
continue |
|
196 |
||
197 |
# renamed |
|
198 |
if path and path != topath: |
|
199 |
renames[topath] = path |
|
8035
cb77c0fbec39
convert: remove renamed source files (issue1505)
Xavier ALT <dex@phoenix-ind.net>
parents:
7060
diff
changeset
|
200 |
changes.append((path, revid)) |
7053 | 201 |
|
202 |
# populate the mode cache |
|
203 |
kind, executable = [e[1] for e in (kind, executable)] |
|
8148
adce97d28389
convert/bzr: fix symlink handling (issue1626)
Patrick Mezard <pmezard@gmail.com>
parents:
8126
diff
changeset
|
204 |
mode = ((executable and 'x') or (kind == 'symlink' and 'l') |
7053 | 205 |
or '') |
206 |
self._modecache[(topath, revid)] = mode |
|
207 |
changes.append((topath, revid)) |
|
208 |
||
209 |
return changes, renames |
|
210 |
||
211 |
def _filterghosts(self, ids): |
|
212 |
"""Filters out ghost revisions which hg does not support, see |
|
213 |
<http://bazaar-vcs.org/GhostRevision> |
|
214 |
""" |
|
215 |
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
|
216 |
parents = tuple([parent for parent in ids if parent in parentmap]) |
7053 | 217 |
return parents |
218 |
||
219 |
def recode(self, s, encoding=None): |
|
220 |
"""This version of recode tries to encode unicode to bytecode, |
|
221 |
and preferably using the UTF-8 codec. |
|
222 |
Other types than Unicode are silently returned, this is by |
|
223 |
intention, e.g. the None-type is not going to be encoded but instead |
|
224 |
just passed through |
|
225 |
""" |
|
226 |
if not encoding: |
|
227 |
encoding = self.encoding or 'utf-8' |
|
228 |
||
229 |
if isinstance(s, unicode): |
|
230 |
return s.encode(encoding) |
|
231 |
else: |
|
232 |
# leave it alone |
|
233 |
return s |