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 (
174
177
class SubmodulesRequireSubtrees(BzrError):
175
_fmt = """The repository you are fetching from contains submodules. To continue, upgrade your Bazaar repository to a format that supports nested trees, such as 'development-subtree'."""
178
_fmt = ("The repository you are fetching from contains submodules. "
179
"To continue, upgrade your Bazaar repository to a format that "
180
"supports nested trees, such as 'development-subtree'.")
179
184
def import_git_submodule(texts, mapping, path, name, (base_hexsha, hexsha),
180
185
base_inv, parent_id, revision_id, parent_invs, lookup_object,
181
186
(base_mode, mode), store_updater, lookup_file_id):
187
"""Import a git submodule."""
182
188
if base_hexsha == hexsha and base_mode == mode:
184
190
file_id = lookup_file_id(path)
260
266
# Remember for next time
261
267
existing_children = set()
263
for child_mode, name, child_hexsha in tree.entries():
269
for name, child_mode, child_hexsha in tree.iteritems():
264
270
existing_children.add(name)
265
271
child_path = posixpath.join(path, name)
266
272
if type(base_tree) is Tree:
275
281
if stat.S_ISDIR(child_mode):
276
282
subinvdelta, grandchildmodes = import_git_tree(texts, mapping,
277
283
child_path, name, (child_base_hexsha, child_hexsha), base_inv,
278
file_id, revision_id, parent_invs, lookup_object,
284
file_id, revision_id, parent_invs, lookup_object,
279
285
(child_base_mode, child_mode), store_updater, lookup_file_id,
280
286
allow_submodules=allow_submodules)
281
287
elif S_ISGITLINK(child_mode): # submodule
307
313
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
308
o, rev, ret_tree, parent_trees, mapping, unusual_modes):
314
o, rev, ret_tree, parent_trees, mapping, unusual_modes, verifiers):
309
315
new_unusual_modes = mapping.export_unusual_file_modes(rev)
310
316
if new_unusual_modes != unusual_modes:
311
317
raise AssertionError("unusual modes don't match: %r != %r" % (
312
318
unusual_modes, new_unusual_modes))
313
319
# Verify that we can reconstruct the commit properly
314
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True)
320
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True,
316
323
raise AssertionError("Reconstructed commit differs: %r != %r" % (
320
327
for path, obj, ie in _tree_to_objects(ret_tree, parent_trees,
321
target_git_object_retriever._cache.idmap, unusual_modes, mapping.BZR_DUMMY_FILE):
328
target_git_object_retriever._cache.idmap, unusual_modes,
329
mapping.BZR_DUMMY_FILE):
322
330
old_obj_id = tree_lookup_path(lookup_object, o.tree, path)[1]
323
331
new_objs[path] = obj
324
332
if obj.id != old_obj_id:
344
352
def import_git_commit(repo, mapping, head, lookup_object,
345
353
target_git_object_retriever, trees_cache):
346
354
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])
355
# Note that this uses mapping.revision_id_foreign_to_bzr. If the parents
356
# were bzr roundtripped revisions they would be specified in the
358
rev, roundtrip_revid, verifiers = mapping.import_commit(
359
o, mapping.revision_id_foreign_to_bzr)
349
360
# We have to do this here, since we have to walk the tree and
350
361
# we need to make sure to import the blobs / trees with the right
351
362
# path; this may involve adding them more than once.
365
376
None, rev.revision_id, [p.inventory for p in parent_trees],
366
377
lookup_object, (base_mode, stat.S_IFDIR), store_updater,
367
378
fileid_map.lookup_file_id,
368
allow_submodules=getattr(repo._format, "supports_tree_reference", False))
379
allow_submodules=getattr(repo._format, "supports_tree_reference",
369
381
if unusual_modes != {}:
370
382
for path, mode in unusual_modes.iteritems():
371
383
warn_unusual_mode(rev.foreign_revid, path, mode)
378
390
rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
379
391
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() }
392
ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
383
393
if roundtrip_revid is not None:
384
394
original_revid = rev.revision_id
385
395
rev.revision_id = roundtrip_revid
397
if getattr(StrictTestament3, "from_revision_tree", None):
398
testament = StrictTestament3(rev, ret_tree)
400
testament = StrictTestament3(rev, inv)
401
calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
386
402
if calculated_verifiers != verifiers:
387
403
trace.mutter("Testament SHA1 %r for %r did not match %r.",
388
404
calculated_verifiers["testament3-sha1"],
389
405
rev.revision_id, verifiers["testament3-sha1"])
390
406
rev.revision_id = original_revid
408
calculated_verifiers = {}
391
409
store_updater.add_object(o, calculated_verifiers, None)
392
410
store_updater.finish()
393
ret_tree = RevisionTree(repo, inv, rev.revision_id)
394
411
trees_cache.add(ret_tree)
395
412
repo.add_revision(rev.revision_id, rev)
396
413
if "verify" in debug.debug_flags:
397
414
verify_commit_reconstruction(target_git_object_retriever,
398
415
lookup_object, o, rev, ret_tree, parent_trees, mapping,
416
unusual_modes, verifiers)
402
419
def import_git_objects(repo, mapping, object_iter,
430
447
if isinstance(o, Commit):
431
448
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
449
mapping.revision_id_foreign_to_bzr)
433
450
if (repo.has_revision(rev.revision_id) or
434
451
(roundtrip_revid and repo.has_revision(roundtrip_revid))):
483
500
_matching_repo_format = GitRepositoryFormat()
502
def _target_has_shas(self, shas):
503
raise NotImplementedError(self._target_has_shas)
505
def get_determine_wants_heads(self, wants, include_tags=False):
507
def determine_wants(refs):
508
potential = set(wants)
511
[v[1] or v[0] for v in extract_tags(refs).itervalues()])
512
return list(potential - self._target_has_shas(potential))
513
return determine_wants
515
def determine_wants_all(self, refs):
516
potential = set([sha for (ref, sha) in refs.iteritems() if not
517
ref.endswith("^{}")])
518
return list(potential - self._target_has_shas(potential))
486
521
def _get_repo_format_to_test():
495
530
"""Base InterRepository that copies revisions from a Git into a non-Git
533
def _target_has_shas(self, shas):
534
revids = [self.source.lookup_foreign_revision_id(sha) for sha in shas]
535
return self.target.has_revisions(revids)
537
def get_determine_wants_revids(self, revids, include_tags=False):
539
for revid in set(revids):
540
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
542
return self.get_determine_wants_heads(wants,
543
include_tags=include_tags)
498
545
def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
499
546
"""Fetch objects from a remote server.
513
560
if revision_id is not None:
514
561
interesting_heads = [revision_id]
515
562
elif fetch_spec is not None:
516
interesting_heads = fetch_spec.heads
563
recipe = fetch_spec.get_recipe()
564
if recipe[0] in ("search", "proxy-search"):
565
interesting_heads = recipe[1]
567
raise AssertionError("Unsupported search result type %s" %
518
570
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)
572
if interesting_heads is not None:
573
determine_wants = self.get_determine_wants_revids(
574
interesting_heads, include_tags=False)
576
determine_wants = self.determine_wants_all
578
(pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
526
580
if pack_hint is not None and self.target._format.pack_compresses:
527
581
self.target.pack(hint=pack_hint)
582
assert isinstance(remote_refs, dict)
528
583
return remote_refs
569
624
def progress(text):
570
625
report_git_progress(pb, text)
571
626
store = BazaarObjectStore(self.target, mapping)
572
self.target.lock_write()
574
629
heads = self.get_target_heads()
575
630
graph_walker = store.get_graph_walker(
583
638
objects_iter = self.source.fetch_objects(
584
639
wants_recorder, graph_walker, store.get_raw,
586
(pack_hint, last_rev) = import_git_objects(self.target, mapping,
587
objects_iter, store, wants_recorder.wants, pb, limit)
641
trace.mutter("Importing %d new revisions",
642
len(wants_recorder.wants))
643
(pack_hint, last_rev) = import_git_objects(self.target,
644
mapping, objects_iter, store, wants_recorder.wants, pb,
588
646
return (pack_hint, last_rev, wants_recorder.remote_refs)
591
649
create_pb.finished()
596
654
def is_compatible(source, target):
597
655
"""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)
656
if not isinstance(source, RemoteGitRepository):
658
if not target.supports_rich_root():
660
if isinstance(target, GitRepository):
662
if not getattr(target._format, "supports_full_versioned_files", True):
604
667
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
614
677
create_pb = pb = ui.ui_factory.nested_progress_bar()
615
678
target_git_object_retriever = BazaarObjectStore(self.target, mapping)
617
self.target.lock_write()
680
target_git_object_retriever.lock_write()
619
(pack_hint, last_rev) = import_git_objects(self.target, mapping,
620
self.source._git.object_store,
682
(pack_hint, last_rev) = import_git_objects(self.target,
683
mapping, self.source._git.object_store,
621
684
target_git_object_retriever, wants, pb, limit)
622
685
return (pack_hint, last_rev, remote_refs)
687
target_git_object_retriever.unlock()
627
690
create_pb.finished()
630
693
def is_compatible(source, target):
631
694
"""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)
695
if not isinstance(source, LocalGitRepository):
697
if not target.supports_rich_root():
699
if isinstance(target, GitRepository):
701
if not getattr(target._format, "supports_full_versioned_files", True):
638
706
class InterGitGitRepository(InterGitRepository):
665
733
raise AssertionError
735
def _target_has_shas(self, shas):
736
return set([sha for sha in shas if self.target._git.object_store])
667
738
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
668
739
mapping=None, fetch_spec=None, branches=None):
669
740
if mapping is None:
672
743
if revision_id is not None:
673
744
args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
674
745
elif fetch_spec is not None:
675
args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in fetch_spec.heads]
746
recipe = fetch_spec.get_recipe()
747
if recipe[0] in ("search", "proxy-search"):
750
raise AssertionError(
751
"Unsupported search result type %s" % recipe[0])
752
args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in
676
754
if branches is not None:
677
determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store]
755
determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store and x[y] != ZERO_SHA]
678
756
elif fetch_spec is None and revision_id is None:
679
determine_wants = r.object_store.determine_wants_all
757
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)
759
determine_wants = lambda x: [y for y in args if not y in r.object_store and y != ZERO_SHA]
760
wants_recorder = DetermineWantsRecorder(determine_wants)
761
self.fetch_objects(wants_recorder, mapping)
762
return wants_recorder.remote_refs
685
765
def is_compatible(source, target):
686
766
"""Be compatible with GitRepository."""
687
767
return (isinstance(source, GitRepository) and
688
768
isinstance(target, GitRepository))
770
def get_determine_wants_revids(self, revids, include_tags=False):
772
for revid in set(revids):
773
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
775
return self.get_determine_wants_heads(wants,
776
include_tags=include_tags)