/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 repository.py

  • Committer: Jelmer Vernooij
  • Date: 2010-06-28 23:48:36 UTC
  • mto: (0.200.953 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100628234836-96qxjh7g2xxedblp
Fix transportgit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from bzrlib import (
21
21
    errors,
22
 
    graph,
23
22
    inventory,
24
23
    repository,
25
24
    revision,
46
45
    )
47
46
 
48
47
 
 
48
from dulwich.objects import (
 
49
    Commit,
 
50
    )
 
51
 
 
52
 
49
53
class GitRepository(ForeignRepository):
50
54
    """An adapter to git repositories for bzr."""
51
55
 
65
69
            repository.InterRepository.register_optimiser(optimiser)
66
70
 
67
71
    def is_shared(self):
68
 
        return True
 
72
        return False
69
73
 
70
74
    def supports_rich_root(self):
71
75
        return True
72
76
 
73
 
    def _warn_if_deprecated(self):
 
77
    def _warn_if_deprecated(self, branch=None):
74
78
        # This class isn't deprecated
75
79
        pass
76
80
 
87
91
        interrepo = repository.InterRepository.get(source, self)
88
92
        return interrepo.dfetch(stop_revision)
89
93
 
90
 
    def dfetch_refs(self, source, stop_revision):
91
 
        interrepo = repository.InterRepository.get(source, self)
92
 
        return interrepo.dfetch_refs(stop_revision)
93
 
 
94
94
 
95
95
class LocalGitRepository(GitRepository):
96
96
    """Git repository on the file system."""
97
97
 
98
98
    def __init__(self, gitdir, lockfiles):
99
 
        # FIXME: This also caches negatives. Need to be more careful
100
 
        # about this once we start writing to git
101
 
        self._parents_provider = graph.CachingParentsProvider(self)
102
99
        GitRepository.__init__(self, gitdir, lockfiles)
103
100
        self.base = gitdir.root_transport.base
104
101
        self._git = gitdir._git
107
104
        self.inventories = None
108
105
        self.texts = GitTexts(self)
109
106
 
 
107
    def _iter_revision_ids(self):
 
108
        for sha in self._git.object_store:
 
109
            o = self._git.object_store[sha]
 
110
            if not isinstance(o, Commit):
 
111
                continue
 
112
            rev = self.get_mapping().import_commit(o,
 
113
                self.lookup_foreign_revision_id)
 
114
            yield o.id, rev.revision_id
 
115
 
110
116
    def all_revision_ids(self):
111
117
        ret = set([])
112
 
        heads = self._git.refs.as_dict('refs/heads')
113
 
        if heads == {}:
114
 
            return ret
115
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
116
 
        ret = set(bzr_heads)
117
 
        graph = self.get_graph()
118
 
        for rev, parents in graph.iter_ancestry(bzr_heads):
119
 
            ret.add(rev)
 
118
        for git_sha, revid in self._iter_revision_ids():
 
119
            ret.add(revid)
120
120
        return ret
121
121
 
122
 
    def _make_parents_provider(self):
123
 
        """See Repository._make_parents_provider()."""
124
 
        return self._parents_provider
125
 
 
126
122
    def get_parent_map(self, revids):
127
123
        parent_map = {}
128
124
        for revision_id in revids:
132
128
                continue
133
129
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
134
130
            try:
135
 
                commit = self._git.commit(hexsha)
 
131
                commit = self._git[hexsha]
136
132
            except KeyError:
137
133
                continue
138
 
            if commit is None:
139
 
                # Older versions of Dulwich used to return None rather than
140
 
                # raise KeyError.
141
 
                continue
142
 
            else:
143
 
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
 
134
            parent_map[revision_id] = [self.lookup_foreign_revision_id(p, mapping) for p in commit.parents]
144
135
        return parent_map
145
136
 
146
137
    def get_ancestry(self, revision_id, topo_sorted=True):
159
150
    def get_signature_text(self, revision_id):
160
151
        raise errors.NoSuchRevision(self, revision_id)
161
152
 
 
153
    def pack(self, hint=None, clean_obsolete_packs=False):
 
154
        self._git.object_store.pack_loose_objects()
 
155
 
162
156
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
163
157
        """Lookup a revision id.
164
158
 
165
 
        :param revid: Bazaar revision id.
166
 
        :return: Tuple with git revisionid and mapping.
167
159
        """
168
160
        if mapping is None:
169
161
            mapping = self.get_mapping()
170
 
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
 
162
        from dulwich.protocol import (
 
163
            ZERO_SHA,
 
164
            )
 
165
        if foreign_revid == ZERO_SHA:
 
166
            return revision.NULL_REVISION
 
167
        commit = self._git[foreign_revid]
 
168
        rev = mapping.import_commit(commit, lambda x: None)
 
169
        return rev.revision_id
171
170
 
172
171
    def has_signature_for_revision_id(self, revision_id):
173
172
        return False
174
173
 
175
 
    def lookup_bzr_revision_id(self, bzr_revid):
 
174
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
176
175
        try:
177
176
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
178
177
        except errors.InvalidRevisionId:
179
 
            raise errors.NoSuchRevision(self, bzr_revid)
 
178
            if mapping is None:
 
179
                mapping = self.get_mapping()
 
180
            try:
 
181
                return self._git.refs[mapping.revid_as_refname(bzr_revid)], mapping
 
182
            except KeyError:
 
183
                # Update refs from Git commit objects
 
184
                # FIXME: Hitting this a lot will be very inefficient...
 
185
                for git_sha, revid in self._iter_revision_ids():
 
186
                    self._git.refs[mapping.revid_as_refname(revid)] = git_sha
 
187
                    if revid == bzr_revid:
 
188
                        return git_sha, mapping
 
189
                raise errors.NoSuchRevision(self, bzr_revid)
180
190
 
181
191
    def get_revision(self, revision_id):
182
192
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
183
193
        try:
184
 
            commit = self._git.commit(git_commit_id)
 
194
            commit = self._git[git_commit_id]
185
195
        except KeyError:
186
196
            raise errors.NoSuchRevision(self, revision_id)
187
197
        # print "fetched revision:", git_commit_id
188
 
        revision = mapping.import_commit(commit)
 
198
        revision = mapping.import_commit(commit,
 
199
            self.lookup_foreign_revision_id)
189
200
        assert revision is not None
190
201
        return revision
191
202
 
192
203
    def has_revision(self, revision_id):
193
204
        try:
194
 
            self.get_revision(revision_id)
 
205
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
195
206
        except errors.NoSuchRevision:
196
207
            return False
197
 
        else:
198
 
            return True
 
208
        return (git_commit_id in self._git)
 
209
 
 
210
    def has_revisions(self, revision_ids):
 
211
        return set(filter(self.has_revision, revision_ids))
199
212
 
200
213
    def get_revisions(self, revids):
201
214
        return [self.get_revision(r) for r in revids]