177
174
class SubmodulesRequireSubtrees(BzrError):
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'.")
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'."""
184
179
def import_git_submodule(texts, mapping, path, name, (base_hexsha, hexsha),
185
180
base_inv, parent_id, revision_id, parent_invs, lookup_object,
186
181
(base_mode, mode), store_updater, lookup_file_id):
187
"""Import a git submodule."""
188
182
if base_hexsha == hexsha and base_mode == mode:
190
184
file_id = lookup_file_id(path)
266
260
# Remember for next time
267
261
existing_children = set()
269
for name, child_mode, child_hexsha in tree.iteritems():
263
for child_mode, name, child_hexsha in tree.entries():
270
264
existing_children.add(name)
271
265
child_path = posixpath.join(path, name)
272
266
if type(base_tree) is Tree:
281
275
if stat.S_ISDIR(child_mode):
282
276
subinvdelta, grandchildmodes = import_git_tree(texts, mapping,
283
277
child_path, name, (child_base_hexsha, child_hexsha), base_inv,
284
file_id, revision_id, parent_invs, lookup_object,
278
file_id, revision_id, parent_invs, lookup_object,
285
279
(child_base_mode, child_mode), store_updater, lookup_file_id,
286
280
allow_submodules=allow_submodules)
287
281
elif S_ISGITLINK(child_mode): # submodule
292
286
file_id, revision_id, parent_invs, lookup_object,
293
287
(child_base_mode, child_mode), store_updater, lookup_file_id)
295
if not mapping.is_special_file(name):
296
subinvdelta = import_git_blob(texts, mapping, child_path, name,
297
(child_base_hexsha, child_hexsha), base_inv, file_id,
298
revision_id, parent_invs, lookup_object,
299
(child_base_mode, child_mode), store_updater, lookup_file_id)
289
subinvdelta = import_git_blob(texts, mapping, child_path, name,
290
(child_base_hexsha, child_hexsha), base_inv, file_id,
291
revision_id, parent_invs, lookup_object,
292
(child_base_mode, child_mode), store_updater, lookup_file_id)
302
293
grandchildmodes = {}
303
294
child_modes.update(grandchildmodes)
304
295
invdelta.extend(subinvdelta)
316
307
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
317
o, rev, ret_tree, parent_trees, mapping, unusual_modes, verifiers):
308
o, rev, ret_tree, parent_trees, mapping, unusual_modes):
318
309
new_unusual_modes = mapping.export_unusual_file_modes(rev)
319
310
if new_unusual_modes != unusual_modes:
320
311
raise AssertionError("unusual modes don't match: %r != %r" % (
321
312
unusual_modes, new_unusual_modes))
322
313
# Verify that we can reconstruct the commit properly
323
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True,
314
rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True)
326
316
raise AssertionError("Reconstructed commit differs: %r != %r" % (
330
320
for path, obj, ie in _tree_to_objects(ret_tree, parent_trees,
331
target_git_object_retriever._cache.idmap, unusual_modes,
332
mapping.BZR_DUMMY_FILE):
321
target_git_object_retriever._cache.idmap, unusual_modes, mapping.BZR_DUMMY_FILE):
333
322
old_obj_id = tree_lookup_path(lookup_object, o.tree, path)[1]
334
323
new_objs[path] = obj
335
324
if obj.id != old_obj_id:
355
344
def import_git_commit(repo, mapping, head, lookup_object,
356
345
target_git_object_retriever, trees_cache):
357
346
o = lookup_object(head)
358
# Note that this uses mapping.revision_id_foreign_to_bzr. If the parents
359
# were bzr roundtripped revisions they would be specified in the
361
rev, roundtrip_revid, verifiers = mapping.import_commit(
362
o, mapping.revision_id_foreign_to_bzr)
363
if roundtrip_revid is not None:
364
original_revid = rev.revision_id
365
rev.revision_id = roundtrip_revid
347
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
348
lambda x: target_git_object_retriever.lookup_git_sha(x)[1][0])
366
349
# We have to do this here, since we have to walk the tree and
367
350
# we need to make sure to import the blobs / trees with the right
368
351
# path; this may involve adding them more than once.
376
359
base_tree = lookup_object(o.parents[0]).tree
377
360
base_mode = stat.S_IFDIR
378
361
store_updater = target_git_object_retriever._get_updater(rev)
379
tree_supplement = mapping.get_fileid_map(lookup_object, o.tree)
362
fileid_map = mapping.get_fileid_map(lookup_object, o.tree)
380
363
inv_delta, unusual_modes = import_git_tree(repo.texts,
381
364
mapping, "", "", (base_tree, o.tree), base_inv,
382
365
None, rev.revision_id, [p.inventory for p in parent_trees],
383
366
lookup_object, (base_mode, stat.S_IFDIR), store_updater,
384
tree_supplement.lookup_file_id,
385
allow_submodules=getattr(repo._format, "supports_tree_reference",
367
fileid_map.lookup_file_id,
368
allow_submodules=getattr(repo._format, "supports_tree_reference", False))
387
369
if unusual_modes != {}:
388
370
for path, mode in unusual_modes.iteritems():
389
371
warn_unusual_mode(rev.foreign_revid, path, mode)
396
378
rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
397
379
inv_delta, rev.revision_id, rev.parent_ids, base_inv)
398
ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
400
if verifiers and roundtrip_revid is not None:
401
if getattr(StrictTestament3, "from_revision_tree", None):
402
testament = StrictTestament3(rev, ret_tree)
404
testament = StrictTestament3(rev, inv)
405
calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
380
# FIXME: Check verifiers
381
testament = StrictTestament3(rev, inv)
382
calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
383
if roundtrip_revid is not None:
384
original_revid = rev.revision_id
385
rev.revision_id = roundtrip_revid
406
386
if calculated_verifiers != verifiers:
407
387
trace.mutter("Testament SHA1 %r for %r did not match %r.",
408
388
calculated_verifiers["testament3-sha1"],
409
389
rev.revision_id, verifiers["testament3-sha1"])
410
390
rev.revision_id = original_revid
411
rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
412
inv_delta, rev.revision_id, rev.parent_ids, base_inv)
413
ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
415
calculated_verifiers = {}
416
391
store_updater.add_object(o, calculated_verifiers, None)
417
392
store_updater.finish()
393
ret_tree = RevisionTree(repo, inv, rev.revision_id)
418
394
trees_cache.add(ret_tree)
419
395
repo.add_revision(rev.revision_id, rev)
420
396
if "verify" in debug.debug_flags:
421
397
verify_commit_reconstruction(target_git_object_retriever,
422
398
lookup_object, o, rev, ret_tree, parent_trees, mapping,
423
unusual_modes, verifiers)
426
402
def import_git_objects(repo, mapping, object_iter,
454
430
if isinstance(o, Commit):
455
431
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
456
mapping.revision_id_foreign_to_bzr)
457
433
if (repo.has_revision(rev.revision_id) or
458
434
(roundtrip_revid and repo.has_revision(roundtrip_revid))):
507
483
_matching_repo_format = GitRepositoryFormat()
509
def _target_has_shas(self, shas):
510
raise NotImplementedError(self._target_has_shas)
512
def get_determine_wants_heads(self, wants, include_tags=False):
514
def determine_wants(refs):
515
potential = set(wants)
518
[v[1] or v[0] for v in extract_tags(refs).itervalues()])
519
return list(potential - self._target_has_shas(potential))
520
return determine_wants
522
def determine_wants_all(self, refs):
523
potential = set([sha for (ref, sha) in refs.iteritems() if not
524
ref.endswith("^{}")])
525
return list(potential - self._target_has_shas(potential))
528
486
def _get_repo_format_to_test():
537
495
"""Base InterRepository that copies revisions from a Git into a non-Git
540
def _target_has_shas(self, shas):
541
revids = [self.source.lookup_foreign_revision_id(sha) for sha in shas]
542
return self.target.has_revisions(revids)
544
def get_determine_wants_revids(self, revids, include_tags=False):
546
for revid in set(revids):
547
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
549
return self.get_determine_wants_heads(wants,
550
include_tags=include_tags)
552
498
def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
553
499
"""Fetch objects from a remote server.
567
513
if revision_id is not None:
568
514
interesting_heads = [revision_id]
569
515
elif fetch_spec is not None:
570
recipe = fetch_spec.get_recipe()
571
if recipe[0] in ("search", "proxy-search"):
572
interesting_heads = recipe[1]
574
raise AssertionError("Unsupported search result type %s" %
516
interesting_heads = fetch_spec.heads
577
518
interesting_heads = None
579
if interesting_heads is not None:
580
determine_wants = self.get_determine_wants_revids(
581
interesting_heads, include_tags=False)
583
determine_wants = self.determine_wants_all
585
(pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
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)
587
526
if pack_hint is not None and self.target._format.pack_compresses:
588
527
self.target.pack(hint=pack_hint)
589
assert isinstance(remote_refs, dict)
590
528
return remote_refs
645
583
objects_iter = self.source.fetch_objects(
646
584
wants_recorder, graph_walker, store.get_raw,
648
trace.mutter("Importing %d new revisions",
649
len(wants_recorder.wants))
650
(pack_hint, last_rev) = import_git_objects(self.target,
651
mapping, objects_iter, store, wants_recorder.wants, pb,
586
(pack_hint, last_rev) = import_git_objects(self.target, mapping,
587
objects_iter, store, wants_recorder.wants, pb, limit)
653
588
return (pack_hint, last_rev, wants_recorder.remote_refs)
656
591
create_pb.finished()
661
596
def is_compatible(source, target):
662
597
"""Be compatible with GitRepository."""
663
if not isinstance(source, RemoteGitRepository):
665
if not target.supports_rich_root():
667
if isinstance(target, GitRepository):
669
if not getattr(target._format, "supports_full_versioned_files", True):
598
return (isinstance(source, RemoteGitRepository) and
599
target.supports_rich_root() and
600
not isinstance(target, GitRepository) and
601
target.texts is not None)
674
604
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
684
614
create_pb = pb = ui.ui_factory.nested_progress_bar()
685
615
target_git_object_retriever = BazaarObjectStore(self.target, mapping)
687
target_git_object_retriever.lock_write()
617
self.target.lock_write()
689
(pack_hint, last_rev) = import_git_objects(self.target,
690
mapping, self.source._git.object_store,
619
(pack_hint, last_rev) = import_git_objects(self.target, mapping,
620
self.source._git.object_store,
691
621
target_git_object_retriever, wants, pb, limit)
692
622
return (pack_hint, last_rev, remote_refs)
694
target_git_object_retriever.unlock()
697
627
create_pb.finished()
700
630
def is_compatible(source, target):
701
631
"""Be compatible with GitRepository."""
702
if not isinstance(source, LocalGitRepository):
704
if not target.supports_rich_root():
706
if isinstance(target, GitRepository):
708
if not getattr(target._format, "supports_full_versioned_files", True):
632
return (isinstance(source, LocalGitRepository) and
633
target.supports_rich_root() and
634
not isinstance(target, GitRepository) and
635
target.texts is not None)
713
638
class InterGitGitRepository(InterGitRepository):
740
665
raise AssertionError
742
def _target_has_shas(self, shas):
743
return set([sha for sha in shas if self.target._git.object_store])
745
667
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
746
668
mapping=None, fetch_spec=None, branches=None):
747
669
if mapping is None:
748
670
mapping = self.source.get_mapping()
749
671
r = self.target._git
750
672
if revision_id is not None:
751
args = [self.source.lookup_bzr_revision_id(revision_id)[0]]
673
args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
752
674
elif fetch_spec is not None:
753
recipe = fetch_spec.get_recipe()
754
if recipe[0] in ("search", "proxy-search"):
757
raise AssertionError(
758
"Unsupported search result type %s" % recipe[0])
759
args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in
675
args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in fetch_spec.heads]
761
676
if branches is not None:
762
determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store and x[y] != ZERO_SHA]
677
determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store]
763
678
elif fetch_spec is None and revision_id is None:
764
determine_wants = self.determine_wants_all
679
determine_wants = r.object_store.determine_wants_all
766
determine_wants = lambda x: [y for y in args if not y in r.object_store and y != ZERO_SHA]
767
wants_recorder = DetermineWantsRecorder(determine_wants)
768
self.fetch_objects(wants_recorder, mapping)
769
return wants_recorder.remote_refs
681
determine_wants = lambda x: [y for y in args if not y in r.object_store]
682
self.fetch_objects(determine_wants, mapping)
772
685
def is_compatible(source, target):
773
686
"""Be compatible with GitRepository."""
774
687
return (isinstance(source, GitRepository) and
775
688
isinstance(target, GitRepository))
777
def get_determine_wants_revids(self, revids, include_tags=False):
779
for revid in set(revids):
780
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
782
return self.get_determine_wants_heads(wants,
783
include_tags=include_tags)