/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

In .testr.conf; run all git-relevant tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""An adapter between a Git Repository and a Bazaar Branch"""
 
19
 
 
20
from bzrlib import (
 
21
    errors,
 
22
    inventory,
 
23
    repository,
 
24
    revision,
 
25
    revisiontree,
 
26
    )
 
27
from bzrlib.foreign import (
 
28
    ForeignRepository,
 
29
    )
 
30
 
 
31
from bzrlib.plugins.git.commit import (
 
32
    GitCommitBuilder,
 
33
    )
 
34
from bzrlib.plugins.git.mapping import (
 
35
    default_mapping,
 
36
    foreign_git,
 
37
    mapping_registry,
 
38
    )
 
39
from bzrlib.plugins.git.tree import (
 
40
    GitRevisionTree,
 
41
    )
 
42
from bzrlib.plugins.git.versionedfiles import (
 
43
    GitRevisions,
 
44
    GitTexts,
 
45
    )
 
46
 
 
47
 
 
48
from dulwich.objects import (
 
49
    Commit,
 
50
    Tag,
 
51
    )
 
52
 
 
53
 
 
54
class GitRepository(ForeignRepository):
 
55
    """An adapter to git repositories for bzr."""
 
56
 
 
57
    _serializer = None
 
58
    _commit_builder_class = GitCommitBuilder
 
59
    vcs = foreign_git
 
60
 
 
61
    def __init__(self, gitdir, lockfiles):
 
62
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
 
63
            lockfiles)
 
64
        from bzrlib.plugins.git import fetch, push
 
65
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
 
66
                          fetch.InterLocalGitNonGitRepository,
 
67
                          fetch.InterGitGitRepository,
 
68
                          push.InterToLocalGitRepository,
 
69
                          push.InterToRemoteGitRepository]:
 
70
            repository.InterRepository.register_optimiser(optimiser)
 
71
 
 
72
    def is_shared(self):
 
73
        return False
 
74
 
 
75
    def supports_rich_root(self):
 
76
        return True
 
77
 
 
78
    def _warn_if_deprecated(self, branch=None):
 
79
        # This class isn't deprecated
 
80
        pass
 
81
 
 
82
    def get_mapping(self):
 
83
        return default_mapping
 
84
 
 
85
    def make_working_trees(self):
 
86
        return not self._git.bare
 
87
 
 
88
    def revision_graph_can_have_wrong_parents(self):
 
89
        return False
 
90
 
 
91
    def dfetch(self, source, stop_revision):
 
92
        interrepo = repository.InterRepository.get(source, self)
 
93
        return interrepo.dfetch(stop_revision)
 
94
 
 
95
 
 
96
class LocalGitRepository(GitRepository):
 
97
    """Git repository on the file system."""
 
98
 
 
99
    def __init__(self, gitdir, lockfiles):
 
100
        GitRepository.__init__(self, gitdir, lockfiles)
 
101
        self.base = gitdir.root_transport.base
 
102
        self._git = gitdir._git
 
103
        self.signatures = None
 
104
        self.revisions = GitRevisions(self, self._git.object_store)
 
105
        self.inventories = None
 
106
        self.texts = GitTexts(self)
 
107
 
 
108
    def _iter_revision_ids(self):
 
109
        mapping = self.get_mapping()
 
110
        for sha in self._git.object_store:
 
111
            o = self._git.object_store[sha]
 
112
            if not isinstance(o, Commit):
 
113
                continue
 
114
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
 
115
                self.lookup_foreign_revision_id)
 
116
            yield o.id, rev.revision_id, roundtrip_revid
 
117
 
 
118
    def all_revision_ids(self):
 
119
        ret = set([])
 
120
        for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
 
121
            ret.add(revid)
 
122
            if roundtrip_revid:
 
123
                ret.add(roundtrip_revid)
 
124
        return ret
 
125
 
 
126
    def get_parent_map(self, revids):
 
127
        parent_map = {}
 
128
        for revision_id in revids:
 
129
            assert isinstance(revision_id, str)
 
130
            if revision_id == revision.NULL_REVISION:
 
131
                parent_map[revision_id] = ()
 
132
                continue
 
133
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
 
134
            try:
 
135
                commit = self._git[hexsha]
 
136
            except KeyError:
 
137
                continue
 
138
            parent_map[revision_id] = [
 
139
                self.lookup_foreign_revision_id(p, mapping)
 
140
                for p in commit.parents]
 
141
        return parent_map
 
142
 
 
143
    def get_ancestry(self, revision_id, topo_sorted=True):
 
144
        """See Repository.get_ancestry().
 
145
        """
 
146
        if revision_id is None:
 
147
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
 
148
        assert isinstance(revision_id, str)
 
149
        ancestry = []
 
150
        graph = self.get_graph()
 
151
        for rev, parents in graph.iter_ancestry([revision_id]):
 
152
            ancestry.append(rev)
 
153
        ancestry.reverse()
 
154
        return [None] + ancestry
 
155
 
 
156
    def get_signature_text(self, revision_id):
 
157
        raise errors.NoSuchRevision(self, revision_id)
 
158
 
 
159
    def pack(self, hint=None, clean_obsolete_packs=False):
 
160
        self._git.object_store.pack_loose_objects()
 
161
 
 
162
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
 
163
        """Lookup a revision id.
 
164
 
 
165
        """
 
166
        assert type(foreign_revid) is str
 
167
        if mapping is None:
 
168
            mapping = self.get_mapping()
 
169
        from dulwich.protocol import (
 
170
            ZERO_SHA,
 
171
            )
 
172
        if foreign_revid == ZERO_SHA:
 
173
            return revision.NULL_REVISION
 
174
        commit = self._git[foreign_revid]
 
175
        while isinstance(commit, Tag):
 
176
            commit = self._git[commit.object[1]]
 
177
        rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
 
178
            lambda x: None)
 
179
        # FIXME: check testament before doing this?
 
180
        if roundtrip_revid:
 
181
            return roundtrip_revid
 
182
        else:
 
183
            return rev.revision_id
 
184
 
 
185
    def has_signature_for_revision_id(self, revision_id):
 
186
        return False
 
187
 
 
188
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
 
189
        try:
 
190
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
191
        except errors.InvalidRevisionId:
 
192
            if mapping is None:
 
193
                mapping = self.get_mapping()
 
194
            try:
 
195
                return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
 
196
                        mapping)
 
197
            except KeyError:
 
198
                # Update refs from Git commit objects
 
199
                # FIXME: Hitting this a lot will be very inefficient...
 
200
                for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
 
201
                    if not roundtrip_revid:
 
202
                        continue
 
203
                    refname = mapping.revid_as_refname(roundtrip_revid)
 
204
                    self._git.refs[refname] = git_sha
 
205
                    if roundtrip_revid == bzr_revid:
 
206
                        return git_sha, mapping
 
207
                raise errors.NoSuchRevision(self, bzr_revid)
 
208
 
 
209
    def get_revision(self, revision_id):
 
210
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
211
        try:
 
212
            commit = self._git[git_commit_id]
 
213
        except KeyError:
 
214
            raise errors.NoSuchRevision(self, revision_id)
 
215
        revision, roundtrip_revid, verifiers = mapping.import_commit(
 
216
            commit, self.lookup_foreign_revision_id)
 
217
        assert revision is not None
 
218
        # FIXME: check verifiers ?
 
219
        if roundtrip_revid:
 
220
            revision.revision_id = roundtrip_revid
 
221
        return revision
 
222
 
 
223
    def has_revision(self, revision_id):
 
224
        try:
 
225
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
226
        except errors.NoSuchRevision:
 
227
            return False
 
228
        return (git_commit_id in self._git)
 
229
 
 
230
    def has_revisions(self, revision_ids):
 
231
        return set(filter(self.has_revision, revision_ids))
 
232
 
 
233
    def get_revisions(self, revids):
 
234
        return [self.get_revision(r) for r in revids]
 
235
 
 
236
    def revision_trees(self, revids):
 
237
        for revid in revids:
 
238
            yield self.revision_tree(revid)
 
239
 
 
240
    def revision_tree(self, revision_id):
 
241
        revision_id = revision.ensure_null(revision_id)
 
242
        if revision_id == revision.NULL_REVISION:
 
243
            inv = inventory.Inventory(root_id=None)
 
244
            inv.revision_id = revision_id
 
245
            return revisiontree.RevisionTree(self, inv, revision_id)
 
246
        return GitRevisionTree(self, revision_id)
 
247
 
 
248
    def get_inventory(self, revision_id):
 
249
        assert revision_id != None
 
250
        return self.revision_tree(revision_id).inventory
 
251
 
 
252
    def set_make_working_trees(self, trees):
 
253
        pass
 
254
 
 
255
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
256
        progress=None):
 
257
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
 
258
 
 
259
    def _get_versioned_file_checker(self, text_key_references=None,
 
260
                        ancestors=None):
 
261
        return GitVersionedFileChecker(self,
 
262
            text_key_references=text_key_references, ancestors=ancestors)
 
263
 
 
264
 
 
265
class GitVersionedFileChecker(repository._VersionedFileChecker):
 
266
 
 
267
    file_ids = []
 
268
 
 
269
    def _check_file_version_parents(self, texts, progress_bar):
 
270
        return {}, []
 
271
 
 
272
 
 
273
class GitRepositoryFormat(repository.RepositoryFormat):
 
274
    """Git repository format."""
 
275
 
 
276
    supports_tree_reference = False
 
277
    rich_root_data = True
 
278
 
 
279
    @property
 
280
    def _matchingbzrdir(self):
 
281
        from bzrlib.plugins.git.dir import LocalGitControlDirFormat
 
282
        return LocalGitControlDirFormat()
 
283
 
 
284
    def get_format_description(self):
 
285
        return "Git Repository"
 
286
 
 
287
    def initialize(self, controldir, shared=False, _internal=False):
 
288
        from bzrlib.plugins.git.dir import GitDir
 
289
        if not isinstance(controldir, GitDir):
 
290
            raise errors.UninitializableFormat(self)
 
291
        return controldir.open_repository()
 
292
 
 
293
    def check_conversion_target(self, target_repo_format):
 
294
        return target_repo_format.rich_root_data
 
295
 
 
296
    def get_foreign_tests_repository_factory(self):
 
297
        from bzrlib.plugins.git.tests.test_repository import (
 
298
            ForeignTestsRepositoryFactory,
 
299
            )
 
300
        return ForeignTestsRepositoryFactory()
 
301
 
 
302
    def network_name(self):
 
303
        return "git"