48
53
extract_bzr_metadata,
49
54
inject_bzr_metadata,
56
deserialize_fileid_map,
61
from urllib.parse import quote
63
from urllib import quote
54
65
DEFAULT_FILE_MODE = stat.S_IFREG | 0o644
55
HG_RENAME_SOURCE = b"HG:rename-source"
56
HG_EXTRA = b"HG:extra"
66
HG_RENAME_SOURCE = "HG:rename-source"
58
69
# This HG extra is used to indicate the commit that this commit was based on.
59
HG_EXTRA_AMEND_SOURCE = b"amend_source"
70
HG_EXTRA_AMEND_SOURCE = "amend_source"
61
72
FILE_ID_PREFIX = b'git:'
64
ROOT_ID = b"TREE_ROOT"
67
class UnknownCommitExtra(errors.BzrError):
68
_fmt = "Unknown extra fields in %(object)r: %(fields)r."
70
def __init__(self, object, fields):
71
errors.BzrError.__init__(self)
73
self.fields = ",".join(fields)
76
class UnknownMercurialCommitExtra(errors.BzrError):
77
_fmt = "Unknown mercurial extra fields in %(object)r: %(fields)r."
79
def __init__(self, object, fields):
80
errors.BzrError.__init__(self)
82
self.fields = b",".join(fields)
85
75
def escape_file_id(file_id):
86
file_id = file_id.replace(b'_', b'__')
87
file_id = file_id.replace(b' ', b'_s')
88
file_id = file_id.replace(b'\x0c', b'_c')
76
return file_id.replace('_', '__').replace(' ', '_s').replace('\x0c', '_c')
92
79
def unescape_file_id(file_id):
95
82
while i < len(file_id):
96
if file_id[i:i + 1] != b'_':
97
84
ret.append(file_id[i])
99
if file_id[i + 1:i + 2] == b'_':
101
elif file_id[i + 1:i + 2] == b's':
103
elif file_id[i + 1:i + 2] == b'c':
104
ret.append(b"\x0c"[0])
86
if file_id[i+1] == '_':
88
elif file_id[i+1] == 's':
90
elif file_id[i+1] == 'c':
106
93
raise ValueError("unknown escape character %s" %
107
file_id[i + 1:i + 2])
113
100
def fix_person_identifier(text):
114
if b"<" not in text and b">" not in text:
101
if not "<" in text and not ">" in text:
117
elif b">" not in text:
120
if text.rindex(b">") < text.rindex(b"<"):
105
if text.rindex(">") < text.rindex("<"):
121
106
raise ValueError(text)
122
username, email = text.split(b"<", 2)[-2:]
123
email = email.split(b">", 1)[0]
124
if username.endswith(b" "):
107
username, email = text.split("<", 2)[-2:]
108
email = email.split(">", 1)[0]
109
if username.endswith(" "):
125
110
username = username[:-1]
126
return b"%s <%s>" % (username, email)
129
def decode_git_path(path):
130
"""Take a git path and decode it."""
132
return path.decode('utf-8')
133
except UnicodeDecodeError:
135
return path.decode('utf-8', 'surrogateescape')
139
def encode_git_path(path):
140
"""Take a regular path and encode it for git."""
142
return path.encode('utf-8')
143
except UnicodeEncodeError:
145
return path.encode('utf-8', 'surrogateescape')
111
return "%s <%s>" % (username, email)
149
114
def warn_escaped(commit, num_escaped):
160
125
"""Class that maps between Git and Bazaar semantics."""
161
126
experimental = False
128
BZR_FILE_IDS_FILE = None
163
130
BZR_DUMMY_FILE = None
165
132
def is_special_file(self, filename):
166
return (filename in (self.BZR_DUMMY_FILE, ))
133
return (filename in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE))
168
135
def __init__(self):
169
136
super(BzrGitMapping, self).__init__(foreign_vcs_git)
171
138
def __eq__(self, other):
172
return (type(self) == type(other)
173
and self.revid_prefix == other.revid_prefix)
139
return (type(self) == type(other) and
140
self.revid_prefix == other.revid_prefix)
176
143
def revision_id_foreign_to_bzr(cls, git_rev_id):
178
145
from dulwich.protocol import ZERO_SHA
179
146
if git_rev_id == ZERO_SHA:
180
147
return NULL_REVISION
181
return b"%s:%s" % (cls.revid_prefix, git_rev_id)
148
return "%s:%s" % (cls.revid_prefix, git_rev_id)
184
151
def revision_id_bzr_to_foreign(cls, bzr_rev_id):
185
152
"""Convert a Bazaar revision id to a git revision id handle."""
186
if not bzr_rev_id.startswith(b"%s:" % cls.revid_prefix):
153
if not bzr_rev_id.startswith("%s:" % cls.revid_prefix):
187
154
raise errors.InvalidRevisionId(bzr_rev_id, cls)
188
return bzr_rev_id[len(cls.revid_prefix) + 1:], cls()
155
return bzr_rev_id[len(cls.revid_prefix)+1:], cls()
190
157
def generate_file_id(self, path):
191
158
# Git paths are just bytestrings
192
159
# We must just hope they are valid UTF-8..
193
if isinstance(path, str):
162
if type(path) is unicode:
194
163
path = path.encode("utf-8")
197
164
return FILE_ID_PREFIX + escape_file_id(path)
199
166
def parse_file_id(self, file_id):
200
167
if file_id == ROOT_ID:
202
169
if not file_id.startswith(FILE_ID_PREFIX):
204
return decode_git_path(unescape_file_id(file_id[len(FILE_ID_PREFIX):]))
171
return unescape_file_id(file_id[len(FILE_ID_PREFIX):])
173
def revid_as_refname(self, revid):
174
return "refs/bzr/%s" % quote(revid)
206
176
def import_unusual_file_modes(self, rev, unusual_file_modes):
207
177
if unusual_file_modes:
208
178
ret = [(path, unusual_file_modes[path])
209
179
for path in sorted(unusual_file_modes.keys())]
210
rev.properties[u'file-modes'] = bencode.bencode(ret)
180
rev.properties['file-modes'] = bencode.bencode(ret)
212
182
def export_unusual_file_modes(self, rev):
214
file_modes = rev.properties[u'file-modes']
184
file_modes = rev.properties['file-modes']
231
201
branch = 'default'
232
202
for name in rev.properties:
233
if name == u'hg:extra:branch':
234
branch = rev.properties[u'hg:extra:branch']
235
elif name.startswith(u'hg:extra'):
236
extra[name[len(u'hg:extra:'):]] = base64.b64decode(
203
if name == 'hg:extra:branch':
204
branch = rev.properties['hg:extra:branch']
205
elif name.startswith('hg:extra'):
206
extra[name[len('hg:extra:'):]] = base64.b64decode(
237
207
rev.properties[name])
238
elif name == u'hg:renames':
208
elif name == 'hg:renames':
239
209
renames = bencode.bdecode(base64.b64decode(
240
rev.properties[u'hg:renames']))
210
rev.properties['hg:renames']))
241
211
# TODO: Export other properties as 'bzr:' extras?
242
212
ret = format_hg_metadata(renames, branch, extra)
243
if not isinstance(ret, bytes):
213
if type(ret) is not str:
244
214
raise TypeError(ret)
247
217
def _extract_git_svn_metadata(self, rev, message):
248
218
lines = message.split("\n")
249
if not (lines[-1] == "" and len(lines) >= 2 and
250
lines[-2].startswith("git-svn-id:")):
219
if not (lines[-1] == "" and len(lines) >= 2 and lines[-2].startswith("git-svn-id:")):
252
221
git_svn_id = lines[-2].split(": ", 1)[1]
253
rev.properties[u'git-svn-id'] = git_svn_id
222
rev.properties['git-svn-id'] = git_svn_id
254
223
(url, rev, uuid) = parse_git_svn_id(git_svn_id)
255
224
# FIXME: Convert this to converted-from property somehow..
256
225
return "\n".join(lines[:-2])
258
227
def _extract_hg_metadata(self, rev, message):
259
228
(message, renames, branch, extra) = extract_hg_metadata(message)
260
229
if branch is not None:
261
rev.properties[u'hg:extra:branch'] = branch
262
for name, value in extra.items():
263
rev.properties[u'hg:extra:' + name] = base64.b64encode(value)
230
rev.properties['hg:extra:branch'] = branch
231
for name, value in extra.iteritems():
232
rev.properties['hg:extra:' + name] = base64.b64encode(value)
265
rev.properties[u'hg:renames'] = base64.b64encode(bencode.bencode(
266
[(new, old) for (old, new) in renames.items()]))
234
rev.properties['hg:renames'] = base64.b64encode(bencode.bencode(
235
[(new, old) for (old, new) in renames.iteritems()]))
269
238
def _extract_bzr_metadata(self, rev, message):
276
245
def _encode_commit_message(self, rev, message, encoding):
277
246
return message.encode(encoding)
248
def export_fileid_map(self, fileid_map):
249
"""Export a file id map to a fileid map.
251
:param fileid_map: File id map, mapping paths to file ids
252
:return: A Git blob object (or None if there are no entries)
254
from dulwich.objects import Blob
256
b.set_raw_chunks(serialize_fileid_map(fileid_map))
279
259
def export_commit(self, rev, tree_sha, parent_lookup, lossy,
281
261
"""Turn a Bazaar revision in to a Git commit
309
289
parents.append(git_p)
310
290
commit.parents = parents
312
encoding = rev.properties[u'git-explicit-encoding']
292
encoding = rev.properties['git-explicit-encoding']
314
encoding = rev.properties.get(u'git-implicit-encoding', 'utf-8')
294
encoding = rev.properties.get('git-implicit-encoding', 'utf-8')
316
commit.encoding = rev.properties[u'git-explicit-encoding'].encode(
296
commit.encoding = rev.properties['git-explicit-encoding'].encode('ascii')
320
299
commit.committer = fix_person_identifier(rev.committer.encode(
322
301
commit.author = fix_person_identifier(
323
302
rev.get_apparent_authors()[0].encode(encoding))
324
# TODO(jelmer): Don't use this hack.
325
long = getattr(__builtins__, 'long', int)
326
303
commit.commit_time = long(rev.timestamp)
327
if u'author-timestamp' in rev.properties:
328
commit.author_time = long(rev.properties[u'author-timestamp'])
304
if 'author-timestamp' in rev.properties:
305
commit.author_time = long(rev.properties['author-timestamp'])
330
307
commit.author_time = commit.commit_time
331
commit._commit_timezone_neg_utc = (
332
u"commit-timezone-neg-utc" in rev.properties)
308
commit._commit_timezone_neg_utc = "commit-timezone-neg-utc" in rev.properties
333
309
commit.commit_timezone = rev.timezone
334
commit._author_timezone_neg_utc = (
335
u"author-timezone-neg-utc" in rev.properties)
336
if u'author-timezone' in rev.properties:
337
commit.author_timezone = int(rev.properties[u'author-timezone'])
310
commit._author_timezone_neg_utc = "author-timezone-neg-utc" in rev.properties
311
if 'author-timezone' in rev.properties:
312
commit.author_timezone = int(rev.properties['author-timezone'])
339
314
commit.author_timezone = commit.commit_timezone
340
if u'git-gpg-signature' in rev.properties:
341
commit.gpgsig = rev.properties[u'git-gpg-signature'].encode(
342
'utf-8', 'surrogateescape')
315
if 'git-gpg-signature' in rev.properties:
316
commit.gpgsig = rev.properties['git-gpg-signature'].encode('ascii')
343
317
commit.message = self._encode_commit_message(rev, rev.message,
345
if not isinstance(commit.message, bytes):
319
if type(commit.message) is not str:
346
320
raise TypeError(commit.message)
347
321
if metadata is not None:
350
324
except errors.InvalidRevisionId:
351
325
metadata.revision_id = rev.revision_id
352
326
mapping_properties = set(
353
[u'author', u'author-timezone', u'author-timezone-neg-utc',
354
u'commit-timezone-neg-utc', u'git-implicit-encoding',
355
u'git-gpg-signature', u'git-explicit-encoding',
356
u'author-timestamp', u'file-modes'])
357
for k, v in rev.properties.items():
358
if k not in mapping_properties:
327
['author', 'author-timezone', 'author-timezone-neg-utc',
328
'commit-timezone-neg-utc', 'git-implicit-encoding',
329
'git-gpg-signature', 'git-explicit-encoding',
330
'author-timestamp', 'file-modes'])
331
for k, v in rev.properties.iteritems():
332
if not k in mapping_properties:
359
333
metadata.properties[k] = v
360
334
if not lossy and metadata:
361
335
if self.roundtripping:
362
336
commit.message = inject_bzr_metadata(commit.message, metadata,
366
None, None, self, revision_id=rev.revision_id)
367
if not isinstance(commit.message, bytes):
339
raise NoPushSupport()
340
if type(commit.message) is not str:
368
341
raise TypeError(commit.message)
370
propname = u'git-mergetag-0'
343
propname = 'git-mergetag-0'
371
344
while propname in rev.properties:
372
commit.mergetag.append(Tag.from_string(rev.properties[propname]))
345
commit.mergetag.append(Tag.from_string(rev.properties[propname].encode(encoding)))
374
propname = u'git-mergetag-%d' % i
375
if u'git-extra' in rev.properties:
378
for l in rev.properties[u'git-extra'].splitlines()])
347
propname = 'git-mergetag-%d' % i
348
if 'git-extra' in rev.properties:
349
commit.extra.extend([l.split(' ', 1) for l in rev.properties['git-extra'].splitlines()])
381
def get_revision_id(self, commit):
383
encoding = commit.encoding.decode('ascii')
387
message, metadata = self._decode_commit_message(
388
None, commit.message, encoding)
389
except UnicodeDecodeError:
392
if metadata.revision_id:
393
return metadata.revision_id
394
return self.revision_id_foreign_to_bzr(commit.id)
396
def import_commit(self, commit, lookup_parent_revid, strict=True):
352
def import_fileid_map(self, blob):
353
"""Convert a git file id map blob.
355
:param blob: Git blob object with fileid map
356
:return: Dictionary mapping paths to file ids
358
return deserialize_fileid_map(blob.data)
360
def import_commit(self, commit, lookup_parent_revid):
397
361
"""Convert a git commit to a bzr revision.
399
363
:return: a `breezy.revision.Revision` object, foreign revid and a
402
366
if commit is None:
403
367
raise AssertionError("Commit object can't be None")
404
368
rev = ForeignRevision(commit.id, self,
405
self.revision_id_foreign_to_bzr(commit.id))
369
self.revision_id_foreign_to_bzr(commit.id))
406
370
rev.git_metadata = None
408
371
def decode_using_encoding(rev, commit, encoding):
409
rev.committer = commit.committer.decode(encoding)
372
rev.committer = str(commit.committer).decode(encoding)
410
373
if commit.committer != commit.author:
411
rev.properties[u'author'] = commit.author.decode(encoding)
374
rev.properties['author'] = str(commit.author).decode(encoding)
412
375
rev.message, rev.git_metadata = self._decode_commit_message(
413
376
rev, commit.message, encoding)
414
377
if commit.encoding is not None:
415
rev.properties[u'git-explicit-encoding'] = commit.encoding.decode(
417
decode_using_encoding(rev, commit, commit.encoding.decode('ascii'))
378
rev.properties['git-explicit-encoding'] = commit.encoding
379
decode_using_encoding(rev, commit, commit.encoding)
419
381
for encoding in ('utf-8', 'latin1'):
425
387
if encoding != 'utf-8':
426
rev.properties[u'git-implicit-encoding'] = encoding
388
rev.properties['git-implicit-encoding'] = encoding
428
390
if commit.commit_time != commit.author_time:
429
rev.properties[u'author-timestamp'] = str(commit.author_time)
391
rev.properties['author-timestamp'] = str(commit.author_time)
430
392
if commit.commit_timezone != commit.author_timezone:
431
rev.properties[u'author-timezone'] = "%d" % commit.author_timezone
393
rev.properties['author-timezone'] = "%d" % commit.author_timezone
432
394
if commit._author_timezone_neg_utc:
433
rev.properties[u'author-timezone-neg-utc'] = ""
395
rev.properties['author-timezone-neg-utc'] = ""
434
396
if commit._commit_timezone_neg_utc:
435
rev.properties[u'commit-timezone-neg-utc'] = ""
397
rev.properties['commit-timezone-neg-utc'] = ""
436
398
if commit.gpgsig:
437
rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
438
'utf-8', 'surrogateescape')
399
rev.properties['git-gpg-signature'] = commit.gpgsig.decode('ascii')
439
400
if commit.mergetag:
440
401
for i, tag in enumerate(commit.mergetag):
441
rev.properties[u'git-mergetag-%d' % i] = tag.as_raw_string()
402
rev.properties['git-mergetag-%d' % i] = tag.as_raw_string()
442
403
rev.timestamp = commit.commit_time
443
404
rev.timezone = commit.commit_timezone
444
405
rev.parent_ids = None
459
420
parents.append(lookup_parent_revid(p))
461
422
parents.append(self.revision_id_foreign_to_bzr(p))
462
rev.parent_ids = list(parents)
423
rev.parent_ids = tuple(parents)
463
424
unknown_extra_fields = []
465
426
for k, v in commit.extra:
466
427
if k == HG_RENAME_SOURCE:
467
extra_lines.append(k + b' ' + v + b'\n')
428
extra_lines.append(k + ' ' + v + '\n')
468
429
elif k == HG_EXTRA:
469
hgk, hgv = v.split(b':', 1)
470
if hgk not in (HG_EXTRA_AMEND_SOURCE, ) and strict:
471
raise UnknownMercurialCommitExtra(commit, [hgk])
472
extra_lines.append(k + b' ' + v + b'\n')
430
hgk, hgv = v.split(':', 1)
431
if hgk not in (HG_EXTRA_AMEND_SOURCE, ):
432
raise UnknownMercurialCommitExtra(commit, hgk)
433
extra_lines.append(k + ' ' + v + '\n')
474
435
unknown_extra_fields.append(k)
475
if unknown_extra_fields and strict:
476
raise UnknownCommitExtra(
478
[f.decode('ascii', 'replace') for f in unknown_extra_fields])
436
if unknown_extra_fields:
437
raise UnknownCommitExtra(commit, unknown_extra_fields)
480
rev.properties[u'git-extra'] = b''.join(extra_lines)
439
rev.properties['git-extra'] = ''.join(extra_lines)
481
440
return rev, roundtrip_revid, verifiers
442
def get_fileid_map(self, lookup_object, tree_sha):
443
"""Obtain a fileid map for a particular tree.
445
:param lookup_object: Function for looking up an object
446
:param tree_sha: SHA of the root tree
447
:return: GitFileIdMap instance
450
file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
454
file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
455
return GitFileIdMap(file_ids, self)
484
458
class BzrGitMappingv1(BzrGitMapping):
485
revid_prefix = b'git-v1'
459
revid_prefix = 'git-v1'
486
460
experimental = False
488
462
def __str__(self):
492
466
class BzrGitMappingExperimental(BzrGitMappingv1):
493
revid_prefix = b'git-experimental'
467
revid_prefix = 'git-experimental'
494
468
experimental = True
495
roundtripping = False
471
BZR_FILE_IDS_FILE = '.bzrfileids'
497
473
BZR_DUMMY_FILE = '.bzrdummy'
499
475
def _decode_commit_message(self, rev, message, encoding):
502
476
message = self._extract_hg_metadata(rev, message)
503
477
message = self._extract_git_svn_metadata(rev, message)
504
478
message, metadata = self._extract_bzr_metadata(rev, message)
510
484
ret += self._generate_git_svn_metadata(rev, encoding)
513
def import_commit(self, commit, lookup_parent_revid, strict=True):
514
rev, roundtrip_revid, verifiers = super(
515
BzrGitMappingExperimental, self).import_commit(
516
commit, lookup_parent_revid, strict)
517
rev.properties[u'converted_revision'] = "git %s\n" % commit.id
487
def import_commit(self, commit, lookup_parent_revid):
488
rev, roundtrip_revid, verifiers = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
489
rev.properties['converted_revision'] = "git %s\n" % commit.id
518
490
return rev, roundtrip_revid, verifiers
525
497
if bzr_revid == NULL_REVISION:
526
498
from dulwich.protocol import ZERO_SHA
527
499
return ZERO_SHA, None
528
if not bzr_revid.startswith(b"git-"):
500
if not bzr_revid.startswith("git-"):
529
501
raise errors.InvalidRevisionId(bzr_revid, None)
530
(mapping_version, git_sha) = bzr_revid.split(b":", 1)
502
(mapping_version, git_sha) = bzr_revid.split(":", 1)
531
503
mapping = self.get(mapping_version)
532
504
return mapping.revision_id_bzr_to_foreign(bzr_revid)
537
509
mapping_registry = GitMappingRegistry()
538
mapping_registry.register_lazy(b'git-v1', __name__,
540
mapping_registry.register_lazy(b'git-experimental',
541
__name__, "BzrGitMappingExperimental")
510
mapping_registry.register_lazy('git-v1', __name__,
512
mapping_registry.register_lazy('git-experimental',
513
__name__, "BzrGitMappingExperimental")
542
514
# Uncomment the next line to enable the experimental bzr-git mappings.
543
515
# This will make sure all bzr metadata is pushed into git, allowing for
544
516
# full roundtripping later.
545
517
# NOTE: THIS IS EXPERIMENTAL. IT MAY EAT YOUR DATA OR CORRUPT
546
518
# YOUR BZR OR GIT REPOSITORIES. USE WITH CARE.
547
# mapping_registry.set_default('git-experimental')
548
mapping_registry.set_default(b'git-v1')
519
#mapping_registry.set_default('git-experimental')
520
mapping_registry.set_default('git-v1')
551
523
class ForeignGit(ForeignVcs):
656
628
return (full_url, int(rev), uuid)
631
class GitFileIdMap(object):
633
def __init__(self, file_ids, mapping):
634
self.file_ids = file_ids
636
self.mapping = mapping
638
def all_file_ids(self):
639
return self.file_ids.values()
641
def set_file_id(self, path, file_id):
642
if type(path) is not str:
643
raise TypeError(path)
644
if type(file_id) is not str:
645
raise TypeError(file_id)
646
self.file_ids[path] = file_id
648
def lookup_file_id(self, path):
649
if type(path) is not str:
650
raise TypeError(path)
652
file_id = self.file_ids[path]
654
file_id = self.mapping.generate_file_id(path)
655
if type(file_id) is not str:
656
raise TypeError(file_id)
659
def lookup_path(self, file_id):
660
if self.paths is None:
662
for k, v in self.file_ids.iteritems():
665
path = self.paths[file_id]
667
return self.mapping.parse_file_id(file_id)
669
if type(path) is not str:
670
raise TypeError(path)
674
return self.__class__(dict(self.file_ids), self.mapping)
659
677
def needs_roundtripping(repo, revid):
661
679
mapping_registry.parse_revision_id(revid)