53
54
from bzrlib.revision import (
56
from bzrlib.revisiontree import (
58
from bzrlib.revisiontree import InventoryRevisionTree
59
except ImportError: # bzr < 2.4
60
from bzrlib.revisiontree import RevisionTree as InventoryRevisionTree
59
61
from bzrlib.testament import (
260
263
# Remember for next time
261
264
existing_children = set()
263
for child_mode, name, child_hexsha in tree.entries():
266
for name, child_mode, child_hexsha in tree.iteritems():
264
267
existing_children.add(name)
265
268
child_path = posixpath.join(path, name)
266
269
if type(base_tree) is Tree:
275
278
if stat.S_ISDIR(child_mode):
276
279
subinvdelta, grandchildmodes = import_git_tree(texts, mapping,
277
280
child_path, name, (child_base_hexsha, child_hexsha), base_inv,
278
file_id, revision_id, parent_invs, lookup_object,
281
file_id, revision_id, parent_invs, lookup_object,
279
282
(child_base_mode, child_mode), store_updater, lookup_file_id,
280
283
allow_submodules=allow_submodules)
281
284
elif S_ISGITLINK(child_mode): # submodule
307
310
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
308
o, rev, ret_tree, parent_trees, mapping, unusual_modes):
311
o, rev, ret_tree, parent_trees, mapping, unusual_modes, verifiers):
309
312
new_unusual_modes = mapping.export_unusual_file_modes(rev)
310
313
if new_unusual_modes != unusual_modes:
311
314
raise AssertionError("unusual modes don't match: %r != %r" % (
312
315
unusual_modes, new_unusual_modes))
313
316
# Verify that we can reconstruct the commit properly
314
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True)
317
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True,
316
320
raise AssertionError("Reconstructed commit differs: %r != %r" % (
344
348
def import_git_commit(repo, mapping, head, lookup_object,
345
349
target_git_object_retriever, trees_cache):
346
350
o = lookup_object(head)
347
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
348
lambda x: target_git_object_retriever.lookup_git_sha(x)[1][0])
351
# Note that this uses mapping.revision_id_foreign_to_bzr. If the parents
352
# were bzr roundtripped revisions they would be specified in the
354
rev, roundtrip_revid, verifiers = mapping.import_commit(
355
o, mapping.revision_id_foreign_to_bzr)
349
356
# We have to do this here, since we have to walk the tree and
350
357
# we need to make sure to import the blobs / trees with the right
351
358
# path; this may involve adding them more than once.
378
385
rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
379
386
inv_delta, rev.revision_id, rev.parent_ids, base_inv)
380
# FIXME: Check verifiers
381
testament = StrictTestament3(rev, inv)
382
calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
387
ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
383
388
if roundtrip_revid is not None:
384
389
original_revid = rev.revision_id
385
390
rev.revision_id = roundtrip_revid
392
if getattr(StrictTestament3, "from_revision_tree", None):
393
testament = StrictTestament3(rev, ret_tree)
395
testament = StrictTestament3(rev, inv)
396
calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
386
397
if calculated_verifiers != verifiers:
387
398
trace.mutter("Testament SHA1 %r for %r did not match %r.",
388
399
calculated_verifiers["testament3-sha1"],
389
400
rev.revision_id, verifiers["testament3-sha1"])
390
401
rev.revision_id = original_revid
403
calculated_verifiers = {}
391
404
store_updater.add_object(o, calculated_verifiers, None)
392
405
store_updater.finish()
393
ret_tree = RevisionTree(repo, inv, rev.revision_id)
394
406
trees_cache.add(ret_tree)
395
407
repo.add_revision(rev.revision_id, rev)
396
408
if "verify" in debug.debug_flags:
397
409
verify_commit_reconstruction(target_git_object_retriever,
398
410
lookup_object, o, rev, ret_tree, parent_trees, mapping,
411
unusual_modes, verifiers)
402
414
def import_git_objects(repo, mapping, object_iter,
430
442
if isinstance(o, Commit):
431
443
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
444
mapping.revision_id_foreign_to_bzr)
433
445
if (repo.has_revision(rev.revision_id) or
434
446
(roundtrip_revid and repo.has_revision(roundtrip_revid))):
483
495
_matching_repo_format = GitRepositoryFormat()
497
def _target_has_shas(self, shas):
498
raise NotImplementedError(self._target_has_shas)
500
def get_determine_wants_heads(self, wants, include_tags=False):
502
def determine_wants(refs):
503
potential = set(wants)
505
potential.update([v[1] or v[0] for v in extract_tags(refs).itervalues()])
506
return list(potential - self._target_has_shas(potential))
507
return determine_wants
509
def determine_wants_all(self, refs):
510
potential = set([sha for (ref, sha) in refs.iteritems() if not ref.endswith("^{}")])
511
return list(potential - self._target_has_shas(potential))
486
514
def _get_repo_format_to_test():
495
523
"""Base InterRepository that copies revisions from a Git into a non-Git
526
def _target_has_shas(self, shas):
527
revids = [self.source.lookup_foreign_revision_id(sha) for sha in shas]
528
return self.target.has_revisions(revids)
530
def get_determine_wants_revids(self, revids, include_tags=False):
532
for revid in set(revids):
533
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
535
return self.get_determine_wants_heads(wants, include_tags=include_tags)
498
537
def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
499
538
"""Fetch objects from a remote server.
513
552
if revision_id is not None:
514
553
interesting_heads = [revision_id]
515
554
elif fetch_spec is not None:
516
interesting_heads = fetch_spec.heads
555
recipe = fetch_spec.get_recipe()
556
if recipe[0] in ("search", "proxy-search"):
557
interesting_heads = recipe[1]
559
raise AssertionError("Unsupported search result type %s" % recipe[0])
518
561
interesting_heads = None
519
def determine_wants(refs):
520
if interesting_heads is None:
521
ret = [sha for (ref, sha) in refs.iteritems() if not ref.endswith("^{}")]
523
ret = [self.source.lookup_bzr_revision_id(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
524
return [rev for rev in ret if not self.target.has_revision(self.source.lookup_foreign_revision_id(rev))]
525
(pack_hint, _, remote_refs) = self.fetch_objects(determine_wants, mapping, pb)
563
if interesting_heads is not None:
564
determine_wants = self.get_determine_wants_revids(interesting_heads,
567
determine_wants = self.determine_wants_all
569
(pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
526
571
if pack_hint is not None and self.target._format.pack_compresses:
527
572
self.target.pack(hint=pack_hint)
573
assert isinstance(remote_refs, dict)
528
574
return remote_refs
569
615
def progress(text):
570
616
report_git_progress(pb, text)
571
617
store = BazaarObjectStore(self.target, mapping)
572
self.target.lock_write()
574
620
heads = self.get_target_heads()
575
621
graph_walker = store.get_graph_walker(
583
629
objects_iter = self.source.fetch_objects(
584
630
wants_recorder, graph_walker, store.get_raw,
632
trace.mutter("Importing %d new revisions", len(wants_recorder.wants))
586
633
(pack_hint, last_rev) = import_git_objects(self.target, mapping,
587
634
objects_iter, store, wants_recorder.wants, pb, limit)
588
635
return (pack_hint, last_rev, wants_recorder.remote_refs)
591
638
create_pb.finished()
596
643
def is_compatible(source, target):
597
644
"""Be compatible with GitRepository."""
598
return (isinstance(source, RemoteGitRepository) and
599
target.supports_rich_root() and
600
not isinstance(target, GitRepository) and
601
target.texts is not None)
645
if not isinstance(source, RemoteGitRepository):
647
if not target.supports_rich_root():
649
if isinstance(target, GitRepository):
651
if not getattr(target._format, "supports_full_versioned_files", True):
604
656
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
614
666
create_pb = pb = ui.ui_factory.nested_progress_bar()
615
667
target_git_object_retriever = BazaarObjectStore(self.target, mapping)
617
self.target.lock_write()
669
target_git_object_retriever.lock_write()
619
671
(pack_hint, last_rev) = import_git_objects(self.target, mapping,
620
672
self.source._git.object_store,
621
673
target_git_object_retriever, wants, pb, limit)
622
674
return (pack_hint, last_rev, remote_refs)
676
target_git_object_retriever.unlock()
627
679
create_pb.finished()
630
682
def is_compatible(source, target):
631
683
"""Be compatible with GitRepository."""
632
return (isinstance(source, LocalGitRepository) and
633
target.supports_rich_root() and
634
not isinstance(target, GitRepository) and
635
target.texts is not None)
684
if not isinstance(source, LocalGitRepository):
686
if not target.supports_rich_root():
688
if isinstance(target, GitRepository):
690
if not getattr(target._format, "supports_full_versioned_files", True):
638
695
class InterGitGitRepository(InterGitRepository):
665
722
raise AssertionError
724
def _target_has_shas(self, shas):
725
return set([sha for sha in shas if self.target._git.object_store])
667
727
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
668
728
mapping=None, fetch_spec=None, branches=None):
669
729
if mapping is None:
672
732
if revision_id is not None:
673
733
args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
674
734
elif fetch_spec is not None:
675
args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in fetch_spec.heads]
735
recipe = fetch_spec.get_recipe()
736
if recipe[0] in ("search", "proxy-search"):
739
raise AssertionError("Unsupported search result type %s" % recipe[0])
740
args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in heads]
676
741
if branches is not None:
677
determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store]
742
determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store and x[y] != ZERO_SHA]
678
743
elif fetch_spec is None and revision_id is None:
679
determine_wants = r.object_store.determine_wants_all
744
determine_wants = self.determine_wants_all
681
determine_wants = lambda x: [y for y in args if not y in r.object_store]
682
self.fetch_objects(determine_wants, mapping)
746
determine_wants = lambda x: [y for y in args if not y in r.object_store and y != ZERO_SHA]
747
wants_recorder = DetermineWantsRecorder(determine_wants)
748
self.fetch_objects(wants_recorder, mapping)
749
return wants_recorder.remote_refs
685
752
def is_compatible(source, target):
686
753
"""Be compatible with GitRepository."""
687
754
return (isinstance(source, GitRepository) and
688
755
isinstance(target, GitRepository))
757
def get_determine_wants_revids(self, revids, include_tags=False):
759
for revid in set(revids):
760
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
762
return self.get_determine_wants_heads(wants, include_tags=include_tags)