/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to fetch.py

Implement GitRevisionTree.get_file_sha1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
    Tag,
20
20
    Tree,
21
21
    S_ISGITLINK,
 
22
    ZERO_SHA,
22
23
    )
23
24
from dulwich.object_store import (
24
25
    tree_lookup_path,
53
54
from bzrlib.revision import (
54
55
    NULL_REVISION,
55
56
    )
56
 
from bzrlib.revisiontree import (
57
 
    RevisionTree,
58
 
    )
 
57
try:
 
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 (
60
62
    StrictTestament3,
61
63
    )
77
79
    LRUTreeCache,
78
80
    _tree_to_objects,
79
81
    )
 
82
from bzrlib.plugins.git.refs import extract_tags
80
83
from bzrlib.plugins.git.remote import (
81
84
    RemoteGitRepository,
82
85
    )
260
263
    # Remember for next time
261
264
    existing_children = set()
262
265
    child_modes = {}
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
305
308
 
306
309
 
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,
 
318
        verifiers)
315
319
    if rec_o != o:
316
320
        raise AssertionError("Reconstructed commit differs: %r != %r" % (
317
321
            rec_o, o))
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
 
353
    # roundtrip data.
 
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.
377
384
        base_inv = None
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
 
391
        # Check verifiers
 
392
        if getattr(StrictTestament3, "from_revision_tree", None):
 
393
            testament = StrictTestament3(rev, ret_tree)
 
394
        else: # bzr < 2.4
 
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
 
402
    else:
 
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,
399
 
            unusual_modes)
 
411
            unusual_modes, verifiers)
400
412
 
401
413
 
402
414
def import_git_objects(repo, mapping, object_iter,
429
441
            continue
430
442
        if isinstance(o, Commit):
431
443
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
432
 
                lambda x: None)
 
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))):
435
447
                continue
482
494
 
483
495
    _matching_repo_format = GitRepositoryFormat()
484
496
 
 
497
    def _target_has_shas(self, shas):
 
498
        raise NotImplementedError(self._target_has_shas)
 
499
 
 
500
    def get_determine_wants_heads(self, wants, include_tags=False):
 
501
        wants = set(wants)
 
502
        def determine_wants(refs):
 
503
            potential = set(wants)
 
504
            if include_tags:
 
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
 
508
 
 
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))
 
512
 
485
513
    @staticmethod
486
514
    def _get_repo_format_to_test():
487
515
        return None
495
523
    """Base InterRepository that copies revisions from a Git into a non-Git
496
524
    repository."""
497
525
 
 
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)
 
529
 
 
530
    def get_determine_wants_revids(self, revids, include_tags=False):
 
531
        wants = set()
 
532
        for revid in set(revids):
 
533
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
 
534
            wants.add(git_sha)
 
535
        return self.get_determine_wants_heads(wants, include_tags=include_tags)
 
536
 
498
537
    def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
499
538
        """Fetch objects from a remote server.
500
539
 
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]
 
558
            else:
 
559
                raise AssertionError("Unsupported search result type %s" % recipe[0])
517
560
        else:
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("^{}")]
522
 
            else:
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)
 
562
 
 
563
        if interesting_heads is not None:
 
564
            determine_wants = self.get_determine_wants_revids(interesting_heads,
 
565
                include_tags=False)
 
566
        else:
 
567
            determine_wants = self.determine_wants_all
 
568
 
 
569
        (pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
 
570
            mapping, pb)
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
529
575
 
530
576
 
569
615
        def progress(text):
570
616
            report_git_progress(pb, text)
571
617
        store = BazaarObjectStore(self.target, mapping)
572
 
        self.target.lock_write()
 
618
        store.lock_write()
573
619
        try:
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,
585
631
                    progress)
 
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)
590
637
                if create_pb:
591
638
                    create_pb.finished()
592
639
        finally:
593
 
            self.target.unlock()
 
640
            store.unlock()
594
641
 
595
642
    @staticmethod
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):
 
646
            return False
 
647
        if not target.supports_rich_root():
 
648
            return False
 
649
        if isinstance(target, GitRepository):
 
650
            return False
 
651
        if not target._format.supports_full_versioned_files:
 
652
            return False
 
653
        return True
602
654
 
603
655
 
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)
616
668
        try:
617
 
            self.target.lock_write()
 
669
            target_git_object_retriever.lock_write()
618
670
            try:
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)
623
675
            finally:
624
 
                self.target.unlock()
 
676
                target_git_object_retriever.unlock()
625
677
        finally:
626
678
            if create_pb:
627
679
                create_pb.finished()
629
681
    @staticmethod
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):
 
685
            return False
 
686
        if not target.supports_rich_root():
 
687
            return False
 
688
        if isinstance(target, GitRepository):
 
689
            return False
 
690
        if not target._format.supports_full_versioned_files:
 
691
            return False
 
692
        return True
636
693
 
637
694
 
638
695
class InterGitGitRepository(InterGitRepository):
664
721
        else:
665
722
            raise AssertionError
666
723
 
 
724
    def _target_has_shas(self, shas):
 
725
        return set([sha for sha in shas if self.target._git.object_store])
 
726
 
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"):
 
737
                heads = recipe[1]
 
738
            else:
 
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
680
745
        else:
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
683
750
 
684
751
    @staticmethod
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))
 
756
 
 
757
    def get_determine_wants_revids(self, revids, include_tags=False):
 
758
        wants = set()
 
759
        for revid in set(revids):
 
760
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
 
761
            wants.add(git_sha)
 
762
        return self.get_determine_wants_heads(wants, include_tags=include_tags)