~ubuntu-branches/debian/jessie/bzr-git/jessie

« back to all changes in this revision

Viewing changes to object_store.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2011-04-12 23:13:28 UTC
  • mfrom: (1.1.15 upstream) (8.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110412231328-13ms0mp32vbyzbkn
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    Commit,
22
22
    Tree,
23
23
    sha_to_hex,
 
24
    ZERO_SHA,
24
25
    )
25
26
from dulwich.object_store import (
26
27
    BaseObjectStore,
493
494
 
494
495
    def _lookup_revision_sha1(self, revid):
495
496
        """Return the SHA1 matching a Bazaar revision."""
496
 
        from dulwich.protocol import ZERO_SHA
497
497
        if revid == NULL_REVISION:
498
498
            return ZERO_SHA
499
499
        try:
520
520
    def __contains__(self, sha):
521
521
        # See if sha is in map
522
522
        try:
523
 
            (type, type_data) = self.lookup_git_sha(sha)
524
 
            if type == "commit":
525
 
                return self.repository.has_revision(type_data[0])
526
 
            elif type == "blob":
527
 
                return self.repository.texts.has_key(type_data)
528
 
            elif type == "tree":
529
 
                return self.repository.has_revision(type_data[1])
 
523
            for (type, type_data) in self.lookup_git_sha(sha):
 
524
                if type == "commit":
 
525
                    if self.repository.has_revision(type_data[0]):
 
526
                        return True
 
527
                elif type == "blob":
 
528
                    if self.repository.texts.has_key(type_data):
 
529
                        return True
 
530
                elif type == "tree":
 
531
                    if self.repository.has_revision(type_data[1]):
 
532
                        return True
 
533
                else:
 
534
                    raise AssertionError("Unknown object type '%s'" % type)
530
535
            else:
531
 
                raise AssertionError("Unknown object type '%s'" % type)
 
536
                return False
532
537
        except KeyError:
533
538
            return False
534
539
 
535
540
    def lookup_git_shas(self, shas, update_map=True):
536
 
        from dulwich.protocol import ZERO_SHA
537
541
        ret = {}
538
542
        for sha in shas:
539
543
            if sha == ZERO_SHA:
540
 
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
 
544
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
541
545
                continue
542
546
            try:
543
 
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
 
547
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
544
548
            except KeyError:
545
549
                if update_map:
546
550
                    # if not, see if there are any unconverted revisions and add
548
552
                    self._update_sha_map()
549
553
                    update_map = False
550
554
                    try:
551
 
                        ret[sha] = self._cache.idmap.lookup_git_sha(sha)
 
555
                        ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
552
556
                    except KeyError:
553
557
                        pass
554
558
        return ret
562
566
                return self._cache.content_cache[sha]
563
567
            except KeyError:
564
568
                pass
565
 
        (type, type_data) = self.lookup_git_sha(sha)
566
 
        # convert object to git object
567
 
        if type == "commit":
568
 
            (revid, tree_sha, verifiers) = type_data
569
 
            try:
570
 
                rev = self.repository.get_revision(revid)
571
 
            except errors.NoSuchRevision:
572
 
                trace.mutter('entry for %s %s in shamap: %r, but not found in '
573
 
                             'repository', type, sha, type_data)
574
 
                raise KeyError(sha)
575
 
            commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
576
 
                verifiers=verifiers)
577
 
            _check_expected_sha(sha, commit)
578
 
            return commit
579
 
        elif type == "blob":
580
 
            (fileid, revision) = type_data
581
 
            return self._reconstruct_blobs([(fileid, revision, sha)]).next()
582
 
        elif type == "tree":
583
 
            (fileid, revid) = type_data
584
 
            try:
585
 
                tree = self.tree_cache.revision_tree(revid)
586
 
                rev = self.repository.get_revision(revid)
587
 
            except errors.NoSuchRevision:
588
 
                trace.mutter('entry for %s %s in shamap: %r, but not found in repository', type, sha, type_data)
589
 
                raise KeyError(sha)
590
 
            unusual_modes = extract_unusual_modes(rev)
591
 
            try:
592
 
                return self._reconstruct_tree(fileid, revid, tree.inventory,
593
 
                    unusual_modes, expected_sha=sha)
594
 
            except errors.NoSuchRevision:
595
 
                raise KeyError(sha)
 
569
        for (kind, type_data) in self.lookup_git_sha(sha):
 
570
            # convert object to git object
 
571
            if kind == "commit":
 
572
                (revid, tree_sha, verifiers) = type_data
 
573
                try:
 
574
                    rev = self.repository.get_revision(revid)
 
575
                except errors.NoSuchRevision:
 
576
                    trace.mutter('entry for %s %s in shamap: %r, but not '
 
577
                                 'found in repository', kind, sha, type_data)
 
578
                    raise KeyError(sha)
 
579
                commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
 
580
                    verifiers=verifiers)
 
581
                _check_expected_sha(sha, commit)
 
582
                return commit
 
583
            elif kind == "blob":
 
584
                (fileid, revision) = type_data
 
585
                return self._reconstruct_blobs([(fileid, revision, sha)]).next()
 
586
            elif kind == "tree":
 
587
                (fileid, revid) = type_data
 
588
                try:
 
589
                    tree = self.tree_cache.revision_tree(revid)
 
590
                    rev = self.repository.get_revision(revid)
 
591
                except errors.NoSuchRevision:
 
592
                    trace.mutter('entry for %s %s in shamap: %r, but not found in repository', kind, sha, type_data)
 
593
                    raise KeyError(sha)
 
594
                unusual_modes = extract_unusual_modes(rev)
 
595
                try:
 
596
                    return self._reconstruct_tree(fileid, revid,
 
597
                        tree.inventory, unusual_modes, expected_sha=sha)
 
598
                except errors.NoSuchRevision:
 
599
                    raise KeyError(sha)
 
600
            else:
 
601
                raise AssertionError("Unknown object type '%s'" % kind)
596
602
        else:
597
 
            raise AssertionError("Unknown object type '%s'" % type)
 
603
            raise KeyError(sha)
598
604
 
599
605
    def generate_lossy_pack_contents(self, have, want, progress=None,
600
606
            get_tagged=None):
612
618
        ret = self.lookup_git_shas(have + want)
613
619
        for commit_sha in have:
614
620
            try:
615
 
                (type, (revid, tree_sha, verifiers)) = ret[commit_sha]
 
621
                for (type, type_data) in ret[commit_sha]:
 
622
                    assert type == "commit"
 
623
                    processed.add(type_data[0])
616
624
            except KeyError:
617
625
                pass
618
 
            else:
619
 
                assert type == "commit"
620
 
                processed.add(revid)
621
626
        pending = set()
622
627
        for commit_sha in want:
623
628
            if commit_sha in have:
624
629
                continue
625
630
            try:
626
 
                (type, (revid, tree_sha, verifiers)) = ret[commit_sha]
 
631
                for (type, type_data) in ret[commit_sha]:
 
632
                    assert type == "commit"
 
633
                    pending.add(type_data[0])
627
634
            except KeyError:
628
635
                pass
629
 
            else:
630
 
                assert type == "commit"
631
 
                pending.add(revid)
632
636
 
633
637
        graph = self.repository.get_graph()
634
638
        todo = _find_missing_bzr_revids(graph, pending, processed)