| 1 |
# bzr.py - bzr support for the convert extension |
| 2 |
# |
| 3 |
# Copyright 2008, 2009 Marek Kubica <[email protected]> and others |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
# This module is for handling Breezy imports or `brz`, but it's also compatible |
| 9 |
# with Bazaar or `bzr`, that was formerly known as Bazaar-NG; |
| 10 |
# it cannot access `bar` repositories, but they were never used very much. |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import os |
| 15 |
|
| 16 |
from mercurial.i18n import _ |
| 17 |
from mercurial import ( |
| 18 |
demandimport, |
| 19 |
error, |
| 20 |
util, |
| 21 |
) |
| 22 |
from . import common |
| 23 |
|
| 24 |
|
| 25 |
# these do not work with demandimport, blacklist |
| 26 |
demandimport.IGNORES.update( |
| 27 |
[ |
| 28 |
'breezy.transactions', |
| 29 |
'breezy.urlutils', |
| 30 |
'ElementPath', |
| 31 |
] |
| 32 |
) |
| 33 |
|
| 34 |
try: |
| 35 |
# bazaar imports |
| 36 |
# pytype: disable=import-error |
| 37 |
import breezy.bzr.bzrdir |
| 38 |
import breezy.errors |
| 39 |
import breezy.revision |
| 40 |
import breezy.revisionspec |
| 41 |
|
| 42 |
# pytype: enable=import-error |
| 43 |
|
| 44 |
bzrdir = breezy.bzr.bzrdir |
| 45 |
errors = breezy.errors |
| 46 |
revision = breezy.revision |
| 47 |
revisionspec = breezy.revisionspec |
| 48 |
revisionspec.RevisionSpec |
| 49 |
|
| 50 |
try: |
| 51 |
# brz 3.3.0 (revno: 7614.2.2) |
| 52 |
from breezy.transport import NoSuchFile |
| 53 |
except ImportError: |
| 54 |
from breezy.errors import NoSuchFile |
| 55 |
except ImportError: |
| 56 |
pass |
| 57 |
|
| 58 |
supportedkinds = ('file', 'symlink') |
| 59 |
|
| 60 |
|
| 61 |
class bzr_source(common.converter_source): |
| 62 |
"""Reads Bazaar repositories by using the Bazaar Python libraries""" |
| 63 |
|
| 64 |
def __init__(self, ui, repotype, path, revs=None): |
| 65 |
super().__init__(ui, repotype, path, revs=revs) |
| 66 |
|
| 67 |
if not os.path.exists(os.path.join(path, b'.bzr')): |
| 68 |
raise common.NoRepo( |
| 69 |
_(b'%s does not look like a Bazaar repository') % path |
| 70 |
) |
| 71 |
|
| 72 |
try: |
| 73 |
# access breezy stuff |
| 74 |
bzrdir |
| 75 |
except NameError: |
| •76 |
raise common.NoRepo(_(b'Bazaar modules could not be loaded') + |
| 77 |
_(b' (try installing the %s package)') % b'bzr') |
| 78 |
|
| 79 |
path = util.abspath(path) |
| 80 |
self._checkrepotype(path) |
| 81 |
try: |
| 82 |
bzr_dir = bzrdir.BzrDir.open(path.decode()) |
| 83 |
self.sourcerepo = bzr_dir.open_repository() |
| 84 |
except errors.NoRepositoryPresent: |
| 85 |
raise common.NoRepo( |
| 86 |
_(b'%s does not look like a Bazaar repository') % path |
| 87 |
) |
| 88 |
self._parentids = {} |
| 89 |
self._saverev = ui.configbool(b'convert', b'bzr.saverev') |
| 90 |
|
| 91 |
def _checkrepotype(self, path): |
| 92 |
# Lightweight checkouts detection is informational but probably |
| 93 |
# fragile at API level. It should not terminate the conversion. |
| 94 |
try: |
| 95 |
dir = bzrdir.BzrDir.open_containing(path.decode())[0] |
| 96 |
try: |
| 97 |
tree = dir.open_workingtree(recommend_upgrade=False) |
| 98 |
branch = tree.branch |
| 99 |
except (errors.NoWorkingTree, errors.NotLocalUrl): |
| 100 |
tree = None |
| 101 |
branch = dir.open_branch() |
| 102 |
if ( |
| 103 |
tree is not None |
| 104 |
and tree.controldir.root_transport.base |
| 105 |
!= branch.controldir.root_transport.base |
| 106 |
): |
| 107 |
self.ui.warn( |
| 108 |
_( |
| 109 |
b'warning: lightweight checkouts may cause ' |
| 110 |
b'conversion failures, try with a regular ' |
| 111 |
b'branch instead.\n' |
| 112 |
) |
| 113 |
) |
| 114 |
except Exception: |
| 115 |
self.ui.note(_(b'bzr source type could not be determined\n')) |
| 116 |
|
| 117 |
def before(self): |
| 118 |
"""Before the conversion begins, acquire a read lock |
| 119 |
for all the operations that might need it. Fortunately |
| 120 |
read locks don't block other reads or writes to the |
| 121 |
repository, so this shouldn't have any impact on the usage of |
| 122 |
the source repository. |
| 123 |
|
| 124 |
The alternative would be locking on every operation that |
| 125 |
needs locks (there are currently two: getting the file and |
| 126 |
getting the parent map) and releasing immediately after, |
| 127 |
but this approach can take even 40% longer.""" |
| 128 |
self.sourcerepo.lock_read() |
| 129 |
|
| 130 |
def after(self): |
| 131 |
self.sourcerepo.unlock() |
| 132 |
|
| 133 |
def _bzrbranches(self): |
| 134 |
return self.sourcerepo.find_branches(using=True) |
| 135 |
|
| 136 |
def getheads(self): |
| 137 |
if not self.revs: |
| 138 |
# Set using=True to avoid nested repositories (see issue3254) |
| 139 |
heads = sorted([b.last_revision() for b in self._bzrbranches()]) |
| 140 |
else: |
| 141 |
revid = None |
| 142 |
for branch in self._bzrbranches(): |
| 143 |
try: |
| 144 |
revspec = self.revs[0].decode() |
| 145 |
r = revisionspec.RevisionSpec.from_string(revspec) |
| 146 |
info = r.in_history(branch) |
| 147 |
except errors.BzrError: |
| 148 |
pass |
| 149 |
revid = info.rev_id |
| 150 |
if revid is None: |
| 151 |
raise error.Abort( |
| 152 |
_(b'%s is not a valid revision') % self.revs[0] |
| 153 |
) |
| 154 |
heads = [revid] |
| 155 |
# Empty repositories return 'null:', which cannot be retrieved |
| 156 |
heads = [h for h in heads if h != b'null:'] |
| 157 |
return heads |
| 158 |
|
| 159 |
def getfile(self, name, rev): |
| 160 |
name = name.decode() |
| 161 |
revtree = self.sourcerepo.revision_tree(rev) |
| 162 |
|
| 163 |
try: |
| 164 |
kind = revtree.kind(name) |
| 165 |
except NoSuchFile: |
| 166 |
return None, None |
| 167 |
if kind not in supportedkinds: |
| 168 |
# the file is not available anymore - was deleted |
| 169 |
return None, None |
| 170 |
mode = self._modecache[(name.encode(), rev)] |
| 171 |
if kind == 'symlink': |
| 172 |
target = revtree.get_symlink_target(name) |
| 173 |
if target is None: |
| 174 |
raise error.Abort( |
| 175 |
_(b'%s.%s symlink has no target') % (name, rev) |
| 176 |
) |
| 177 |
return target.encode(), mode |
| 178 |
else: |
| 179 |
sio = revtree.get_file(name) |
| 180 |
return sio.read(), mode |
| 181 |
|
| 182 |
def getchanges(self, version, full): |
| 183 |
if full: |
| 184 |
raise error.Abort(_(b"convert from cvs does not support --full")) |
| 185 |
self._modecache = {} |
| 186 |
self._revtree = self.sourcerepo.revision_tree(version) |
| 187 |
# get the parentids from the cache |
| 188 |
parentids = self._parentids.pop(version) |
| 189 |
# only diff against first parent id |
| 190 |
prevtree = self.sourcerepo.revision_tree(parentids[0]) |
| 191 |
files, changes = self._gettreechanges(self._revtree, prevtree) |
| 192 |
return files, changes, set() |
| 193 |
|
| 194 |
def getcommit(self, version): |
| 195 |
rev = self.sourcerepo.get_revision(version) |
| 196 |
# populate parent id cache |
| 197 |
if not rev.parent_ids: |
| 198 |
parents = [] |
| 199 |
self._parentids[version] = (revision.NULL_REVISION,) |
| 200 |
else: |
| 201 |
parents = self._filterghosts(rev.parent_ids) |
| 202 |
self._parentids[version] = parents |
| 203 |
|
| 204 |
branch = rev.properties.get('branch-nick', 'default') |
| 205 |
if branch == 'trunk': |
| 206 |
branch = 'default' |
| 207 |
return common.commit( |
| 208 |
parents=parents, |
| 209 |
date=b'%d %d' % (rev.timestamp, -rev.timezone), |
| 210 |
author=self.recode(rev.committer), |
| 211 |
desc=self.recode(rev.message), |
| 212 |
branch=branch.encode('utf8'), |
| 213 |
rev=version, |
| 214 |
saverev=self._saverev, |
| 215 |
) |
| 216 |
|
| 217 |
def gettags(self): |
| 218 |
bytetags = {} |
| 219 |
for branch in self._bzrbranches(): |
| 220 |
if not branch.supports_tags(): |
| 221 |
return {} |
| 222 |
tagdict = branch.tags.get_tag_dict() |
| 223 |
for name, rev in tagdict.items(): |
| 224 |
bytetags[self.recode(name)] = rev |
| 225 |
return bytetags |
| 226 |
|
| 227 |
def getchangedfiles(self, rev, i): |
| 228 |
self._modecache = {} |
| 229 |
curtree = self.sourcerepo.revision_tree(rev) |
| 230 |
if i is not None: |
| 231 |
parentid = self._parentids[rev][i] |
| 232 |
else: |
| 233 |
# no parent id, get the empty revision |
| 234 |
parentid = revision.NULL_REVISION |
| 235 |
|
| 236 |
prevtree = self.sourcerepo.revision_tree(parentid) |
| 237 |
changes = [e[0] for e in self._gettreechanges(curtree, prevtree)[0]] |
| 238 |
return changes |
| 239 |
|
| 240 |
def _gettreechanges(self, current, origin): |
| 241 |
revid = current._revision_id |
| 242 |
changes = [] |
| 243 |
renames = {} |
| 244 |
seen = set() |
| 245 |
|
| 246 |
# Fall back to the deprecated attribute for legacy installations. |
| 247 |
try: |
| 248 |
inventory = origin.root_inventory |
| 249 |
except AttributeError: |
| 250 |
inventory = origin.inventory |
| 251 |
|
| 252 |
# Process the entries by reverse lexicographic name order to |
| 253 |
# handle nested renames correctly, most specific first. |
| 254 |
|
| 255 |
def key(c): |
| 256 |
return c.path[0] or c.path[1] or "" |
| 257 |
|
| 258 |
curchanges = sorted( |
| 259 |
current.iter_changes(origin), |
| 260 |
key=key, |
| 261 |
reverse=True, |
| 262 |
) |
| 263 |
for change in curchanges: |
| 264 |
paths = change.path |
| 265 |
kind = change.kind |
| 266 |
executable = change.executable |
| 267 |
if paths[0] == '' or paths[1] == '': |
| 268 |
# ignore changes to tree root |
| 269 |
continue |
| 270 |
|
| 271 |
# bazaar tracks directories, mercurial does not, so |
| 272 |
# we have to rename the directory contents |
| 273 |
if kind[1] == 'directory': |
| 274 |
if kind[0] not in (None, 'directory'): |
| 275 |
# Replacing 'something' with a directory, record it |
| 276 |
# so it can be removed. |
| 277 |
changes.append((self.recode(paths[0]), revid)) |
| 278 |
|
| 279 |
if kind[0] == 'directory' and None not in paths: |
| 280 |
renaming = paths[0] != paths[1] |
| 281 |
# neither an add nor an delete - a move |
| 282 |
# rename all directory contents manually |
| 283 |
subdir = inventory.path2id(paths[0]) |
| 284 |
# get all child-entries of the directory |
| 285 |
for name, entry in inventory.iter_entries(subdir): |
| 286 |
# hg does not track directory renames |
| 287 |
if entry.kind == 'directory': |
| 288 |
continue |
| 289 |
frompath = self.recode(paths[0] + '/' + name) |
| 290 |
if frompath in seen: |
| 291 |
# Already handled by a more specific change entry |
| 292 |
# This is important when you have: |
| 293 |
# a => b |
| 294 |
# a/c => a/c |
| 295 |
# Here a/c must not be renamed into b/c |
| 296 |
continue |
| 297 |
seen.add(frompath) |
| 298 |
if not renaming: |
| 299 |
continue |
| 300 |
topath = self.recode(paths[1] + '/' + name) |
| 301 |
# register the files as changed |
| 302 |
changes.append((frompath, revid)) |
| 303 |
changes.append((topath, revid)) |
| 304 |
# add to mode cache |
| 305 |
mode = ( |
| 306 |
(entry.executable and b'x') |
| 307 |
or (entry.kind == 'symlink' and b's') |
| 308 |
or b'' |
| 309 |
) |
| 310 |
self._modecache[(topath, revid)] = mode |
| 311 |
# register the change as move |
| 312 |
renames[topath] = frompath |
| 313 |
|
| 314 |
# no further changes, go to the next change |
| 315 |
continue |
| 316 |
|
| 317 |
# we got unicode paths, need to convert them |
| 318 |
path, topath = paths |
| 319 |
if path is not None: |
| 320 |
path = self.recode(path) |
| 321 |
if topath is not None: |
| 322 |
topath = self.recode(topath) |
| 323 |
seen.add(path or topath) |
| 324 |
|
| 325 |
if topath is None: |
| 326 |
# file deleted |
| 327 |
changes.append((path, revid)) |
| 328 |
continue |
| 329 |
|
| 330 |
# renamed |
| 331 |
if path and path != topath: |
| 332 |
renames[topath] = path |
| 333 |
changes.append((path, revid)) |
| 334 |
|
| 335 |
# populate the mode cache |
| 336 |
kind, executable = (e[1] for e in (kind, executable)) |
| 337 |
mode = (executable and b'x') or (kind == 'symlink' and b'l') or b'' |
| 338 |
self._modecache[(topath, revid)] = mode |
| 339 |
changes.append((topath, revid)) |
| 340 |
|
| 341 |
return changes, renames |
| 342 |
|
| 343 |
def _filterghosts(self, ids): |
| 344 |
"""Filters out ghost revisions which hg does not support, see |
| 345 |
<http://bazaar-vcs.org/GhostRevision> |
| 346 |
""" |
| 347 |
parentmap = self.sourcerepo.get_parent_map(ids) |
| 348 |
parents = tuple([parent for parent in ids if parent in parentmap]) |
| 349 |
return parents |