/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

Update docstrings.

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
    )
151
154
        assert ie.revision is not None
152
155
        if ie.kind == 'symlink':
153
156
            chunks = []
154
 
        else: 
 
157
        else:
155
158
            chunks = blob.chunked
156
159
        texts.insert_record_stream([
157
160
            ChunkedContentFactory((file_id, ie.revision),
172
175
 
173
176
 
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'.")
176
181
    internal = False
177
182
 
178
183
 
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:
183
189
        return [], {}
184
190
    file_id = lookup_file_id(path)
260
266
    # Remember for next time
261
267
    existing_children = set()
262
268
    child_modes = {}
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
286
292
                file_id, revision_id, parent_invs, lookup_object,
287
293
                (child_base_mode, child_mode), store_updater, lookup_file_id)
288
294
        else:
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)
 
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)
 
300
            else:
 
301
                subinvdelta = []
293
302
            grandchildmodes = {}
294
303
        child_modes.update(grandchildmodes)
295
304
        invdelta.extend(subinvdelta)
305
314
 
306
315
 
307
316
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
308
 
    o, rev, ret_tree, parent_trees, mapping, unusual_modes):
 
317
    o, rev, ret_tree, parent_trees, mapping, unusual_modes, verifiers):
309
318
    new_unusual_modes = mapping.export_unusual_file_modes(rev)
310
319
    if new_unusual_modes != unusual_modes:
311
320
        raise AssertionError("unusual modes don't match: %r != %r" % (
312
321
            unusual_modes, new_unusual_modes))
313
322
    # Verify that we can reconstruct the commit properly
314
 
    rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True)
 
323
    rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True,
 
324
        verifiers)
315
325
    if rec_o != o:
316
326
        raise AssertionError("Reconstructed commit differs: %r != %r" % (
317
327
            rec_o, o))
318
328
    diff = []
319
329
    new_objs = {}
320
330
    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):
 
331
        target_git_object_retriever._cache.idmap, unusual_modes,
 
332
        mapping.BZR_DUMMY_FILE):
322
333
        old_obj_id = tree_lookup_path(lookup_object, o.tree, path)[1]
323
334
        new_objs[path] = obj
324
335
        if obj.id != old_obj_id:
344
355
def import_git_commit(repo, mapping, head, lookup_object,
345
356
                      target_git_object_retriever, trees_cache):
346
357
    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])
 
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
 
360
    # roundtrip data.
 
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
349
366
    # We have to do this here, since we have to walk the tree and
350
367
    # we need to make sure to import the blobs / trees with the right
351
368
    # path; this may involve adding them more than once.
359
376
        base_tree = lookup_object(o.parents[0]).tree
360
377
        base_mode = stat.S_IFDIR
361
378
    store_updater = target_git_object_retriever._get_updater(rev)
362
 
    fileid_map = mapping.get_fileid_map(lookup_object, o.tree)
 
379
    tree_supplement = mapping.get_fileid_map(lookup_object, o.tree)
363
380
    inv_delta, unusual_modes = import_git_tree(repo.texts,
364
381
            mapping, "", "", (base_tree, o.tree), base_inv,
365
382
            None, rev.revision_id, [p.inventory for p in parent_trees],
366
383
            lookup_object, (base_mode, stat.S_IFDIR), store_updater,
367
 
            fileid_map.lookup_file_id,
368
 
            allow_submodules=getattr(repo._format, "supports_tree_reference", False))
 
384
            tree_supplement.lookup_file_id,
 
385
            allow_submodules=getattr(repo._format, "supports_tree_reference",
 
386
                False))
369
387
    if unusual_modes != {}:
370
388
        for path, mode in unusual_modes.iteritems():
371
389
            warn_unusual_mode(rev.foreign_revid, path, mode)
377
395
        base_inv = None
378
396
    rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
379
397
              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() }
383
 
    if roundtrip_revid is not None:
384
 
        original_revid = rev.revision_id
385
 
        rev.revision_id = roundtrip_revid
 
398
    ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
 
399
    # Check verifiers
 
400
    if verifiers and roundtrip_revid is not None:
 
401
        if getattr(StrictTestament3, "from_revision_tree", None):
 
402
            testament = StrictTestament3(rev, ret_tree)
 
403
        else: # bzr < 2.4
 
404
            testament = StrictTestament3(rev, inv)
 
405
        calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
386
406
        if calculated_verifiers != verifiers:
387
407
            trace.mutter("Testament SHA1 %r for %r did not match %r.",
388
408
                         calculated_verifiers["testament3-sha1"],
389
409
                         rev.revision_id, verifiers["testament3-sha1"])
390
410
            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)
 
414
    else:
 
415
        calculated_verifiers = {}
391
416
    store_updater.add_object(o, calculated_verifiers, None)
392
417
    store_updater.finish()
393
 
    ret_tree = RevisionTree(repo, inv, rev.revision_id)
394
418
    trees_cache.add(ret_tree)
395
419
    repo.add_revision(rev.revision_id, rev)
396
420
    if "verify" in debug.debug_flags:
397
421
        verify_commit_reconstruction(target_git_object_retriever, 
398
422
            lookup_object, o, rev, ret_tree, parent_trees, mapping,
399
 
            unusual_modes)
 
423
            unusual_modes, verifiers)
400
424
 
401
425
 
402
426
def import_git_objects(repo, mapping, object_iter,
429
453
            continue
430
454
        if isinstance(o, Commit):
431
455
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
432
 
                lambda x: None)
 
456
                mapping.revision_id_foreign_to_bzr)
433
457
            if (repo.has_revision(rev.revision_id) or
434
458
                (roundtrip_revid and repo.has_revision(roundtrip_revid))):
435
459
                continue
482
506
 
483
507
    _matching_repo_format = GitRepositoryFormat()
484
508
 
 
509
    def _target_has_shas(self, shas):
 
510
        raise NotImplementedError(self._target_has_shas)
 
511
 
 
512
    def get_determine_wants_heads(self, wants, include_tags=False):
 
513
        wants = set(wants)
 
514
        def determine_wants(refs):
 
515
            potential = set(wants)
 
516
            if include_tags:
 
517
                potential.update(
 
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
 
521
 
 
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))
 
526
 
485
527
    @staticmethod
486
528
    def _get_repo_format_to_test():
487
529
        return None
495
537
    """Base InterRepository that copies revisions from a Git into a non-Git
496
538
    repository."""
497
539
 
 
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)
 
543
 
 
544
    def get_determine_wants_revids(self, revids, include_tags=False):
 
545
        wants = set()
 
546
        for revid in set(revids):
 
547
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
 
548
            wants.add(git_sha)
 
549
        return self.get_determine_wants_heads(wants,
 
550
            include_tags=include_tags)
 
551
 
498
552
    def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
499
553
        """Fetch objects from a remote server.
500
554
 
513
567
        if revision_id is not None:
514
568
            interesting_heads = [revision_id]
515
569
        elif fetch_spec is not None:
516
 
            interesting_heads = fetch_spec.heads
 
570
            recipe = fetch_spec.get_recipe()
 
571
            if recipe[0] in ("search", "proxy-search"):
 
572
                interesting_heads = recipe[1]
 
573
            else:
 
574
                raise AssertionError("Unsupported search result type %s" %
 
575
                        recipe[0])
517
576
        else:
518
577
            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)
 
578
 
 
579
        if interesting_heads is not None:
 
580
            determine_wants = self.get_determine_wants_revids(
 
581
                interesting_heads, include_tags=False)
 
582
        else:
 
583
            determine_wants = self.determine_wants_all
 
584
 
 
585
        (pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
 
586
            mapping, pb)
526
587
        if pack_hint is not None and self.target._format.pack_compresses:
527
588
            self.target.pack(hint=pack_hint)
 
589
        assert isinstance(remote_refs, dict)
528
590
        return remote_refs
529
591
 
530
592
 
569
631
        def progress(text):
570
632
            report_git_progress(pb, text)
571
633
        store = BazaarObjectStore(self.target, mapping)
572
 
        self.target.lock_write()
 
634
        store.lock_write()
573
635
        try:
574
636
            heads = self.get_target_heads()
575
637
            graph_walker = store.get_graph_walker(
583
645
                objects_iter = self.source.fetch_objects(
584
646
                    wants_recorder, graph_walker, store.get_raw,
585
647
                    progress)
586
 
                (pack_hint, last_rev) = import_git_objects(self.target, mapping,
587
 
                    objects_iter, store, wants_recorder.wants, pb, limit)
 
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,
 
652
                    limit)
588
653
                return (pack_hint, last_rev, wants_recorder.remote_refs)
589
654
            finally:
590
655
                if create_pb:
591
656
                    create_pb.finished()
592
657
        finally:
593
 
            self.target.unlock()
 
658
            store.unlock()
594
659
 
595
660
    @staticmethod
596
661
    def is_compatible(source, target):
597
662
        """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)
 
663
        if not isinstance(source, RemoteGitRepository):
 
664
            return False
 
665
        if not target.supports_rich_root():
 
666
            return False
 
667
        if isinstance(target, GitRepository):
 
668
            return False
 
669
        if not getattr(target._format, "supports_full_versioned_files", True):
 
670
            return False
 
671
        return True
602
672
 
603
673
 
604
674
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
614
684
            create_pb = pb = ui.ui_factory.nested_progress_bar()
615
685
        target_git_object_retriever = BazaarObjectStore(self.target, mapping)
616
686
        try:
617
 
            self.target.lock_write()
 
687
            target_git_object_retriever.lock_write()
618
688
            try:
619
 
                (pack_hint, last_rev) = import_git_objects(self.target, mapping,
620
 
                    self.source._git.object_store,
 
689
                (pack_hint, last_rev) = import_git_objects(self.target,
 
690
                    mapping, self.source._git.object_store,
621
691
                    target_git_object_retriever, wants, pb, limit)
622
692
                return (pack_hint, last_rev, remote_refs)
623
693
            finally:
624
 
                self.target.unlock()
 
694
                target_git_object_retriever.unlock()
625
695
        finally:
626
696
            if create_pb:
627
697
                create_pb.finished()
629
699
    @staticmethod
630
700
    def is_compatible(source, target):
631
701
        """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)
 
702
        if not isinstance(source, LocalGitRepository):
 
703
            return False
 
704
        if not target.supports_rich_root():
 
705
            return False
 
706
        if isinstance(target, GitRepository):
 
707
            return False
 
708
        if not getattr(target._format, "supports_full_versioned_files", True):
 
709
            return False
 
710
        return True
636
711
 
637
712
 
638
713
class InterGitGitRepository(InterGitRepository):
664
739
        else:
665
740
            raise AssertionError
666
741
 
 
742
    def _target_has_shas(self, shas):
 
743
        return set([sha for sha in shas if self.target._git.object_store])
 
744
 
667
745
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
668
746
              mapping=None, fetch_spec=None, branches=None):
669
747
        if mapping is None:
670
748
            mapping = self.source.get_mapping()
671
749
        r = self.target._git
672
750
        if revision_id is not None:
673
 
            args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
 
751
            args = [self.source.lookup_bzr_revision_id(revision_id)[0]]
674
752
        elif fetch_spec is not None:
675
 
            args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in fetch_spec.heads]
 
753
            recipe = fetch_spec.get_recipe()
 
754
            if recipe[0] in ("search", "proxy-search"):
 
755
                heads = recipe[1]
 
756
            else:
 
757
                raise AssertionError(
 
758
                    "Unsupported search result type %s" % recipe[0])
 
759
            args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in
 
760
                    heads]
676
761
        if branches is not None:
677
 
            determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store]
 
762
            determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store and x[y] != ZERO_SHA]
678
763
        elif fetch_spec is None and revision_id is None:
679
 
            determine_wants = r.object_store.determine_wants_all
 
764
            determine_wants = self.determine_wants_all
680
765
        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)
 
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
683
770
 
684
771
    @staticmethod
685
772
    def is_compatible(source, target):
686
773
        """Be compatible with GitRepository."""
687
774
        return (isinstance(source, GitRepository) and
688
775
                isinstance(target, GitRepository))
 
776
 
 
777
    def get_determine_wants_revids(self, revids, include_tags=False):
 
778
        wants = set()
 
779
        for revid in set(revids):
 
780
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
 
781
            wants.add(git_sha)
 
782
        return self.get_determine_wants_heads(wants,
 
783
            include_tags=include_tags)