90
92
"property. ", mode, path, commit)
93
def squash_revision(target_repo, rev):
94
"""Remove characters that can't be stored from a revision, if necessary.
96
:param target_repo: Repository in which the revision will be stored
97
:param rev: Revision object, will be modified in-place
99
if not getattr(target_repo._serializer, "squashes_xml_invalid_characters", True):
101
from bzrlib.xml_serializer import escape_invalid_chars
102
rev.message, num_escaped = escape_invalid_chars(rev.message)
104
warn_escaped(rev.foreign_revid, num_escaped)
105
if 'author' in rev.properties:
106
rev.properties['author'], num_escaped = escape_invalid_chars(
107
rev.properties['author'])
109
warn_escaped(rev.foreign_revid, num_escaped)
110
rev.committer, num_escaped = escape_invalid_chars(rev.committer)
112
warn_escaped(rev.foreign_revid, num_escaped)
115
95
class BzrGitMapping(foreign.VcsMapping):
116
96
"""Class that maps between Git and Bazaar semantics."""
117
97
experimental = False
99
BZR_FILE_IDS_FILE = None
101
BZR_DUMMY_FILE = None
119
103
def __init__(self):
120
104
super(BzrGitMapping, self).__init__(foreign_git)
142
127
# We must just hope they are valid UTF-8..
130
if type(path) is unicode:
131
path = path.encode("utf-8")
145
132
return escape_file_id(path)
134
def is_control_file(self, path):
135
return path in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE)
147
137
def parse_file_id(self, file_id):
148
138
if file_id == ROOT_ID:
150
140
return unescape_file_id(file_id)
142
def revid_as_refname(self, revid):
144
return "refs/bzr/%s" % urllib.quote(revid)
152
146
def import_unusual_file_modes(self, rev, unusual_file_modes):
153
147
if unusual_file_modes:
154
ret = [(name, unusual_file_modes[name])
155
for name in sorted(unusual_file_modes.keys())]
148
ret = [(path, unusual_file_modes[path])
149
for path in sorted(unusual_file_modes.keys())]
156
150
rev.properties['file-modes'] = bencode.bencode(ret)
158
152
def export_unusual_file_modes(self, rev):
160
return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(rev.properties['file-modes'].encode("utf-8"))])
154
file_modes = rev.properties['file-modes']
158
return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(file_modes.encode("utf-8"))])
164
160
def _generate_git_svn_metadata(self, rev, encoding):
166
return "\ngit-svn-id: %s\n" % rev.properties["git-svn-id"].encode(
162
git_svn_id = rev.properties["git-svn-id"]
166
return "\ngit-svn-id: %s\n" % git_svn_id.encode(encoding)
171
168
def _generate_hg_message_tail(self, rev):
209
206
[(new, old) for (old, new) in renames.iteritems()]))
209
def _extract_bzr_metadata(self, rev, message):
210
(message, metadata) = extract_bzr_metadata(message)
211
return message, metadata
212
213
def _decode_commit_message(self, rev, message, encoding):
213
return message.decode(encoding)
214
return message.decode(encoding), BzrGitRevisionMetadata()
215
216
def _encode_commit_message(self, rev, message, encoding):
216
217
return message.encode(encoding)
218
def export_commit(self, rev, tree_sha, parent_lookup):
219
def export_fileid_map(self, fileid_map):
220
"""Export a file id map to a fileid map.
222
:param fileid_map: File id map, mapping paths to file ids
223
:return: A Git blob object
225
from dulwich.objects import Blob
227
b.set_raw_chunks(serialize_fileid_map(fileid_map))
230
def export_commit(self, rev, tree_sha, parent_lookup, roundtrip):
219
231
"""Turn a Bazaar revision in to a Git commit
221
233
:param tree_sha: Tree sha for the commit
248
268
commit.author_time = long(rev.properties['author-timestamp'])
250
270
commit.author_time = commit.commit_time
271
commit._commit_timezone_neg_utc = "commit-timezone-neg-utc" in rev.properties
251
272
commit.commit_timezone = rev.timezone
273
commit._author_timezone_neg_utc = "author-timezone-neg-utc" in rev.properties
252
274
if 'author-timezone' in rev.properties:
253
275
commit.author_timezone = int(rev.properties['author-timezone'])
255
277
commit.author_timezone = commit.commit_timezone
256
278
commit.message = self._encode_commit_message(rev, rev.message,
280
assert type(commit.message) == str
281
if metadata is not None:
283
mapping_registry.parse_revision_id(rev.revision_id)
284
except errors.InvalidRevisionId:
285
metadata.revision_id = rev.revision_id
286
mapping_properties = set(
287
['author', 'author-timezone', 'author-timezone-neg-utc',
288
'commit-timezone-neg-utc', 'git-implicit-encoding',
289
'git-explicit-encoding', 'author-timestamp', 'file-modes'])
290
for k, v in rev.properties.iteritems():
291
if not k in mapping_properties:
292
metadata.properties[k] = v
293
if self.roundtripping:
294
commit.message = inject_bzr_metadata(commit.message, metadata,
296
assert type(commit.message) == str
260
def import_commit(self, commit):
299
def import_fileid_map(self, blob):
300
"""Convert a git file id map blob.
302
:param blob: Git blob object with fileid map
303
:return: Dictionary mapping paths to file ids
305
return deserialize_fileid_map(blob.data)
307
def import_commit(self, commit, lookup_parent_revid):
261
308
"""Convert a git commit to a bzr revision.
263
:return: a `bzrlib.revision.Revision` object.
310
:return: a `bzrlib.revision.Revision` object and a
311
dictionary of path -> file ids
265
313
if commit is None:
266
314
raise AssertionError("Commit object can't be None")
267
315
rev = ForeignRevision(commit.id, self,
268
316
self.revision_id_foreign_to_bzr(commit.id))
269
rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
317
rev.parent_ids = tuple([lookup_parent_revid(p) for p in commit.parents])
318
rev.git_metadata = None
270
319
def decode_using_encoding(rev, commit, encoding):
271
320
rev.committer = str(commit.committer).decode(encoding)
272
321
if commit.committer != commit.author:
273
322
rev.properties['author'] = str(commit.author).decode(encoding)
274
rev.message = self._decode_commit_message(rev, commit.message,
323
rev.message, rev.git_metadata = self._decode_commit_message(
324
rev, commit.message, encoding)
276
325
if commit.encoding is not None:
277
326
rev.properties['git-explicit-encoding'] = commit.encoding
278
327
decode_using_encoding(rev, commit, commit.encoding)
290
339
rev.properties['author-timestamp'] = str(commit.author_time)
291
340
if commit.commit_timezone != commit.author_timezone:
292
341
rev.properties['author-timezone'] = "%d" % commit.author_timezone
342
if commit._author_timezone_neg_utc:
343
rev.properties['author-timezone-neg-utc'] = ""
344
if commit._commit_timezone_neg_utc:
345
rev.properties['commit-timezone-neg-utc'] = ""
293
346
rev.timestamp = commit.commit_time
294
347
rev.timezone = commit.commit_timezone
348
if rev.git_metadata is not None:
349
md = rev.git_metadata
351
rev.revision_id = md.revision_id
352
if md.explicit_parent_ids:
353
rev.parent_ids = md.explicit_parent_ids
354
rev.properties.update(md.properties)
357
def get_fileid_map(self, lookup_object, tree_sha):
358
"""Obtain a fileid map for a particular tree.
360
:param lookup_object: Function for looking up an object
361
:param tree_sha: SHA of the root tree
362
:return: GitFileIdMap instance
365
file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
369
file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
370
return GitFileIdMap(file_ids, self)
298
373
class BzrGitMappingv1(BzrGitMapping):
299
374
revid_prefix = 'git-v1'
306
381
class BzrGitMappingExperimental(BzrGitMappingv1):
307
382
revid_prefix = 'git-experimental'
308
383
experimental = True
386
BZR_FILE_IDS_FILE = '.bzrfileids'
388
BZR_DUMMY_FILE = '.bzrdummy'
310
390
def _decode_commit_message(self, rev, message, encoding):
311
391
message = self._extract_hg_metadata(rev, message)
312
392
message = self._extract_git_svn_metadata(rev, message)
313
return message.decode(encoding)
393
message, metadata = self._extract_bzr_metadata(rev, message)
394
return message.decode(encoding), metadata
315
396
def _encode_commit_message(self, rev, message, encoding):
316
397
ret = message.encode(encoding)
318
399
ret += self._generate_git_svn_metadata(rev, encoding)
321
def import_commit(self, commit):
322
rev = super(BzrGitMappingExperimental, self).import_commit(commit)
402
def import_commit(self, commit, lookup_parent_revid):
403
rev, file_ids = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
323
404
rev.properties['converted_revision'] = "git %s\n" % commit.id
327
408
class GitMappingRegistry(VcsMappingRegistry):
438
520
return object_mode(entry.kind, entry.executable)
441
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes):
442
from dulwich.objects import Tree
523
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes, empty_file_name):
524
"""Create a Git Tree object from a Bazaar directory.
526
:param entry: Inventory entry
527
:param lookup_ie_sha1: Lookup the Git SHA1 for a inventory entry
528
:param unusual_modes: Dictionary with unusual file modes by file ids
529
:param empty_file_name: Name to use for dummy files in empty directories,
530
None to ignore empty directories.
532
from dulwich.objects import Blob, Tree
444
534
for name, value in entry.children.iteritems():
445
535
ie = entry.children[name]
466
560
return mapping.export_unusual_file_modes(rev)
469
def inventory_to_tree_and_blobs(inventory, texts, mapping, unusual_modes,
471
"""Convert a Bazaar tree to a Git tree.
473
:return: Yields tuples with object sha1, object and path
475
from dulwich.objects import Tree
482
# stack contains the set of trees that we haven't
483
# finished constructing
484
for path, entry in inventory.iter_entries():
485
while stack and not path.startswith(osutils.pathjoin(cur, "")):
486
# We've hit a file that's not a child of the previous path
488
yield sha, tree, cur.encode("utf-8")
489
mode = unusual_modes.get(cur.encode("utf-8"), stat.S_IFDIR)
490
t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
491
cur, tree = stack.pop()
494
if entry.kind == "directory":
495
stack.append((cur, tree))
499
if entry.kind == "file":
500
from dulwich.objects import Blob
501
stream = texts.get_record_stream(
502
[(entry.file_id, entry.revision)], 'unordered', True)
504
blob.chunked = stream.next().get_bytes_as('chunks')
505
elif entry.kind == "symlink":
506
blob = symlink_to_blob(entry)
508
raise AssertionError("Unknown kind %s" % entry.kind)
510
yield sha, blob, path.encode("utf-8")
511
name = urlutils.basename(path).encode("utf-8")
512
mode = unusual_modes.get(path.encode("utf-8"), entry_mode(entry))
513
tree.add(mode, name, sha)
515
while len(stack) > 1:
517
yield sha, tree, cur.encode("utf-8")
518
mode = unusual_modes.get(cur.encode('utf-8'), stat.S_IFDIR)
519
t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
520
cur, tree = stack.pop()
523
yield tree.id, tree, cur.encode("utf-8")
526
563
def parse_git_svn_id(text):
527
564
(head, uuid) = text.rsplit(" ", 1)
528
565
(full_url, rev) = head.rsplit("@", 1)
529
566
return (full_url, int(rev), uuid)
569
class GitFileIdMap(object):
571
def __init__(self, file_ids, mapping):
572
self.file_ids = file_ids
574
self.mapping = mapping
576
def lookup_file_id(self, path):
577
assert type(path) is str
579
file_id = self.file_ids[path]
581
file_id = self.mapping.generate_file_id(path)
582
assert type(file_id) is str
585
def lookup_path(self, file_id):
586
if self.paths is None:
588
for k, v in self.file_ids.iteritems():
591
path = self.paths[file_id]
593
return self.mapping.parse_file_id(file_id)
595
assert type(path) is str