155
144
return escape_file_id(path)
157
def is_control_file(self, path):
158
return path in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE)
160
146
def parse_file_id(self, file_id):
161
147
if file_id == ROOT_ID:
163
149
return unescape_file_id(file_id)
165
def revid_as_refname(self, revid):
167
return "refs/bzr/%s" % urllib.quote(revid)
169
151
def import_unusual_file_modes(self, rev, unusual_file_modes):
170
152
if unusual_file_modes:
171
ret = [(path, unusual_file_modes[path])
172
for path in sorted(unusual_file_modes.keys())]
153
ret = [(name, unusual_file_modes[name])
154
for name in sorted(unusual_file_modes.keys())]
173
155
rev.properties['file-modes'] = bencode.bencode(ret)
175
157
def export_unusual_file_modes(self, rev):
177
file_modes = rev.properties['file-modes']
159
return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(rev.properties['file-modes'].encode("utf-8"))])
181
return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(file_modes.encode("utf-8"))])
183
163
def _generate_git_svn_metadata(self, rev, encoding):
185
git_svn_id = rev.properties["git-svn-id"]
165
return "\ngit-svn-id: %s\n" % rev.properties["git-svn-id"].encode(encoding)
189
return "\ngit-svn-id: %s\n" % git_svn_id.encode(encoding)
191
169
def _generate_hg_message_tail(self, rev):
225
201
for name, value in extra.iteritems():
226
202
rev.properties['hg:extra:' + name] = base64.b64encode(value)
228
rev.properties['hg:renames'] = base64.b64encode(bencode.bencode(
229
[(new, old) for (old, new) in renames.iteritems()]))
204
rev.properties['hg:renames'] = base64.b64encode(bencode.bencode([(new, old) for (old, new) in renames.iteritems()]))
232
def _extract_bzr_metadata(self, rev, message):
233
(message, metadata) = extract_bzr_metadata(message)
234
return message, metadata
236
207
def _decode_commit_message(self, rev, message, encoding):
237
message, metadata = self._extract_bzr_metadata(rev, message)
238
return message.decode(encoding), metadata
208
return message.decode(encoding)
240
210
def _encode_commit_message(self, rev, message, encoding):
241
211
return message.encode(encoding)
243
def export_fileid_map(self, fileid_map):
244
"""Export a file id map to a fileid map.
246
:param fileid_map: File id map, mapping paths to file ids
247
:return: A Git blob object
249
from dulwich.objects import Blob
251
b.set_raw_chunks(serialize_fileid_map(fileid_map))
254
def export_commit(self, rev, tree_sha, parent_lookup, roundtrip):
213
def export_commit(self, rev, tree_sha, parent_lookup):
255
214
"""Turn a Bazaar revision in to a Git commit
257
216
:param tree_sha: Tree sha for the commit
258
:param parent_lookup: Function for looking up the GIT sha equiv of a
217
:param parent_lookup: Function for looking up the GIT sha equiv of a bzr revision
260
218
:return dulwich.objects.Commit represent the revision:
262
220
from dulwich.objects import Commit
263
221
commit = Commit()
264
222
commit.tree = tree_sha
266
metadata = BzrGitRevisionMetadata()
269
223
for p in rev.parent_ids:
271
225
git_p = parent_lookup(p)
274
if metadata is not None:
275
metadata.explicit_parent_ids = rev.parent_ids
276
228
if git_p is not None:
277
229
assert len(git_p) == 40, "unexpected length for %r" % git_p
278
230
commit.parents.append(git_p)
290
242
commit.author_time = long(rev.properties['author-timestamp'])
292
244
commit.author_time = commit.commit_time
293
commit._commit_timezone_neg_utc = "commit-timezone-neg-utc" in rev.properties
294
245
commit.commit_timezone = rev.timezone
295
commit._author_timezone_neg_utc = "author-timezone-neg-utc" in rev.properties
296
246
if 'author-timezone' in rev.properties:
297
247
commit.author_timezone = int(rev.properties['author-timezone'])
299
249
commit.author_timezone = commit.commit_timezone
300
250
commit.message = self._encode_commit_message(rev, rev.message,
302
if metadata is not None:
304
mapping_registry.parse_revision_id(rev.revision_id)
305
except errors.InvalidRevisionId:
306
metadata.revision_id = rev.revision_id
307
mapping_properties = set(
308
['author', 'author-timezone', 'author-timezone-neg-utc',
309
'commit-timezone-neg-utc', 'git-implicit-encoding',
310
'git-explicit-encoding', 'author-timestamp', 'file-modes'])
311
for k, v in rev.properties.iteritems():
312
if not k in mapping_properties:
313
metadata.properties[k] = v
314
commit.message = inject_bzr_metadata(commit.message, metadata)
317
def import_fileid_map(self, blob):
318
"""Convert a git file id map blob.
320
:param blob: Git blob object with fileid map
321
:return: Dictionary mapping paths to file ids
323
return deserialize_fileid_map(blob.data)
325
254
def import_commit(self, commit):
326
255
"""Convert a git commit to a bzr revision.
328
:return: a `bzrlib.revision.Revision` object and a
329
dictionary of path -> file ids
257
:return: a `bzrlib.revision.Revision` object.
331
259
if commit is None:
332
260
raise AssertionError("Commit object can't be None")
333
rev = ForeignRevision(commit.id, self,
334
self.revision_id_foreign_to_bzr(commit.id))
261
rev = ForeignRevision(commit.id, self, self.revision_id_foreign_to_bzr(commit.id))
335
262
rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
336
rev.git_metadata = None
337
263
def decode_using_encoding(rev, commit, encoding):
338
264
rev.committer = str(commit.committer).decode(encoding)
339
265
if commit.committer != commit.author:
340
266
rev.properties['author'] = str(commit.author).decode(encoding)
341
rev.message, rev.git_metadata = self._decode_commit_message(
342
rev, commit.message, encoding)
267
rev.message = self._decode_commit_message(rev, commit.message,
343
269
if commit.encoding is not None:
344
270
rev.properties['git-explicit-encoding'] = commit.encoding
345
271
decode_using_encoding(rev, commit, commit.encoding)
356
282
if commit.commit_time != commit.author_time:
357
283
rev.properties['author-timestamp'] = str(commit.author_time)
358
284
if commit.commit_timezone != commit.author_timezone:
359
rev.properties['author-timezone'] = "%d" % commit.author_timezone
360
if commit._author_timezone_neg_utc:
361
rev.properties['author-timezone-neg-utc'] = ""
362
if commit._commit_timezone_neg_utc:
363
rev.properties['commit-timezone-neg-utc'] = ""
285
rev.properties['author-timezone'] = "%d" % (commit.author_timezone, )
364
286
rev.timestamp = commit.commit_time
365
287
rev.timezone = commit.commit_timezone
366
if rev.git_metadata is not None:
367
md = rev.git_metadata
369
rev.revision_id = md.revision_id
370
if md.explicit_parent_ids:
371
rev.parent_ids = md.explicit_parent_ids
372
rev.properties.update(md.properties)
518
436
return object_mode(entry.kind, entry.executable)
521
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes, empty_file_name):
522
"""Create a Git Tree object from a Bazaar directory.
524
:param entry: Inventory entry
525
:param lookup_ie_sha1: Lookup the Git SHA1 for a inventory entry
526
:param unusual_modes: Dictionary with unusual file modes by file ids
527
:param empty_file_name: Name to use for dummy files in empty directories,
528
None to ignore empty directories.
530
from dulwich.objects import Blob, Tree
439
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes):
440
from dulwich.objects import Tree
532
for name, value in entry.children.iteritems():
442
for name in sorted(entry.children.keys()):
533
443
ie = entry.children[name]
535
445
mode = unusual_modes[ie.file_id]
537
447
mode = entry_mode(ie)
538
hexsha = lookup_ie_sha1(ie)
448
if ie.kind == 'directory':
449
subtree = directory_to_tree(ie, lookup_ie_sha1, unusual_modes)
455
hexsha = lookup_ie_sha1(ie)
539
456
if hexsha is not None:
540
457
tree.add(mode, name.encode("utf-8"), hexsha)
541
458
if entry.parent_id is not None and len(tree) == 0:
542
459
# Only the root can be an empty tree
543
if empty_file_name is not None:
544
tree.add(stat.S_IFREG | 0644, empty_file_name,
551
465
def extract_unusual_modes(rev):
553
foreign_revid, mapping = mapping_registry.parse_revision_id(
467
foreign_revid, mapping = mapping_registry.parse_revision_id(rev.revision_id)
555
468
except errors.InvalidRevisionId:
558
471
return mapping.export_unusual_file_modes(rev)
474
def inventory_to_tree_and_blobs(inventory, texts, mapping, unusual_modes, cur=None):
475
"""Convert a Bazaar tree to a Git tree.
477
:return: Yields tuples with object sha1, object and path
479
from dulwich.objects import Tree
486
# stack contains the set of trees that we haven't
487
# finished constructing
488
for path, entry in inventory.iter_entries():
489
while stack and not path.startswith(osutils.pathjoin(cur, "")):
490
# We've hit a file that's not a child of the previous path
493
yield sha, tree, cur.encode("utf-8")
494
mode = unusual_modes.get(cur.encode("utf-8"), stat.S_IFDIR)
495
t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
496
cur, tree = stack.pop()
499
if entry.kind == "directory":
500
stack.append((cur, tree))
504
if entry.kind == "file":
505
blob = text_to_blob(texts, entry)
506
elif entry.kind == "symlink":
507
blob = symlink_to_blob(entry)
509
raise AssertionError("Unknown kind %s" % entry.kind)
511
yield sha, blob, path.encode("utf-8")
512
name = urlutils.basename(path).encode("utf-8")
513
mode = unusual_modes.get(path.encode("utf-8"), entry_mode(entry))
514
tree.add(mode, name, sha)
516
while len(stack) > 1:
519
yield sha, tree, cur.encode("utf-8")
520
mode = unusual_modes.get(cur.encode('utf-8'), stat.S_IFDIR)
521
t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
522
cur, tree = stack.pop()
526
yield tree.id, tree, cur.encode("utf-8")
561
529
def parse_git_svn_id(text):
562
530
(head, uuid) = text.rsplit(" ", 1)
563
531
(full_url, rev) = head.rsplit("@", 1)
564
532
return (full_url, int(rev), uuid)
567
class GitFileIdMap(object):
569
def __init__(self, file_ids, mapping):
570
self.file_ids = file_ids
572
self.mapping = mapping
574
def lookup_file_id(self, path):
576
return self.file_ids[path]
578
return self.mapping.generate_file_id(path)
580
def lookup_path(self, file_id):
581
if self.paths is None:
583
for k, v in self.file_ids.iteritems():
586
return self.paths[file_id]
588
return self.mapping.parse_file_id(file_id)