~bzr/ubuntu/jaunty/bzr-git/bzr-ppa

17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
17.1.235 by Jelmer Vernooij
Clarify history, copyright.
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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 (
17.1.26 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
21
    errors,
17.1.21 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
22
    inventory,
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
23
    repository,
17.1.12 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
24
    revision,
17.1.22 by David Allouche
Black-box text for "bzr log" in a git tree. Further simplification of GitRevisionTree.
25
    revisiontree,
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
26
    )
17.1.98 by Jelmer Vernooij
Pass mapping object.
27
from bzrlib.foreign import (
17.1.275 by Jelmer Vernooij
Fix formatting.
28
    ForeignRepository,
29
    )
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
30
17.1.370 by Jelmer Vernooij
Initial work on supporting commit in git trees.
31
from bzrlib.plugins.git.commit import (
32
    GitCommitBuilder,
33
    )
17.1.239 by Jelmer Vernooij
Add tests for import_revision_gist.
34
from bzrlib.plugins.git.mapping import (
35
    default_mapping,
17.1.378 by Jelmer Vernooij
Set vcs attribute on GitRepository.
36
    foreign_git,
17.1.239 by Jelmer Vernooij
Add tests for import_revision_gist.
37
    mapping_registry,
38
    )
17.25.58 by Jelmer Vernooij
Add custom InterTree for use between git revision trees.
39
from bzrlib.plugins.git.tree import (
40
    GitRevisionTree,
41
    )
17.1.275 by Jelmer Vernooij
Fix formatting.
42
from bzrlib.plugins.git.versionedfiles import (
17.1.489 by Jelmer Vernooij
Remove bzr-foreign.
43
    GitRevisions,
17.1.275 by Jelmer Vernooij
Fix formatting.
44
    GitTexts,
45
    )
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
46
47
17.40.21 by Jelmer Vernooij
Fix GitRepository.all_revision_ids() to find all revisions.
48
from dulwich.objects import (
49
    Commit,
50
    )
51
52
17.1.98 by Jelmer Vernooij
Pass mapping object.
53
class GitRepository(ForeignRepository):
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
54
    """An adapter to git repositories for bzr."""
55
17.1.24 by David Allouche
Define _serializer = None in GitRepository.
56
    _serializer = None
17.1.370 by Jelmer Vernooij
Initial work on supporting commit in git trees.
57
    _commit_builder_class = GitCommitBuilder
17.1.378 by Jelmer Vernooij
Set vcs attribute on GitRepository.
58
    vcs = foreign_git
17.1.24 by David Allouche
Define _serializer = None in GitRepository.
59
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
60
    def __init__(self, gitdir, lockfiles):
17.25.117 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
61
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
17.1.272 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
62
            lockfiles)
17.1.274 by Jelmer Vernooij
Print proper error about not supporting push.
63
        from bzrlib.plugins.git import fetch, push
17.25.117 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
64
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
17.1.289 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
65
                          fetch.InterLocalGitNonGitRepository,
17.1.439 by Jelmer Vernooij
Fix git -> git fetching.
66
                          fetch.InterGitGitRepository,
17.1.408 by Jelmer Vernooij
Split out push to remote git repositories.
67
                          push.InterToLocalGitRepository,
68
                          push.InterToRemoteGitRepository]:
17.1.259 by Jelmer Vernooij
Improve formatting.
69
            repository.InterRepository.register_optimiser(optimiser)
17.1.122 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
70
71
    def is_shared(self):
17.25.327 by Jelmer Vernooij
Git repositories are not shared.
72
        return False
17.1.122 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
73
74
    def supports_rich_root(self):
75
        return True
76
17.25.164 by Jelmer Vernooij
cope with branch argument to _warn_if_deprecated
77
    def _warn_if_deprecated(self, branch=None):
17.1.122 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
78
        # This class isn't deprecated
79
        pass
80
81
    def get_mapping(self):
82
        return default_mapping
83
17.1.130 by Jelmer Vernooij
Merge new dulwich; fetching objects from local repository works now; they aren't converted yet though.
84
    def make_working_trees(self):
17.41.110 by Jelmer Vernooij
Don't claim to support working trees for bare repositories.
85
        return not self._git.bare
17.1.122 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
86
17.24.29 by Jelmer Vernooij
Implement GitRepository.revision_graph_can_have_wrong_parents().
87
    def revision_graph_can_have_wrong_parents(self):
88
        return False
89
17.1.408 by Jelmer Vernooij
Split out push to remote git repositories.
90
    def dfetch(self, source, stop_revision):
91
        interrepo = repository.InterRepository.get(source, self)
92
        return interrepo.dfetch(stop_revision)
93
17.1.122 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
94
95
class LocalGitRepository(GitRepository):
17.1.259 by Jelmer Vernooij
Improve formatting.
96
    """Git repository on the file system."""
17.1.122 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
97
98
    def __init__(self, gitdir, lockfiles):
99
        GitRepository.__init__(self, gitdir, lockfiles)
17.1.44 by Jelmer Vernooij
Fix tests.
100
        self.base = gitdir.root_transport.base
17.1.73 by Jelmer Vernooij
Basic support for opening working trees.
101
        self._git = gitdir._git
17.1.489 by Jelmer Vernooij
Remove bzr-foreign.
102
        self.signatures = None
17.25.90 by Jelmer Vernooij
Make GitRevisions VF implementation behave as the interface expects.
103
        self.revisions = GitRevisions(self, self._git.object_store)
17.1.489 by Jelmer Vernooij
Remove bzr-foreign.
104
        self.inventories = None
17.1.192 by Jelmer Vernooij
Pass repository object to versionedfiles.
105
        self.texts = GitTexts(self)
17.1.28 by David Allouche
More performance hacking, introduce sqlite cache, escape characters in commits that break serializers.
106
17.40.46 by Jelmer Vernooij
Generate refs/bzr/* if not set yet.
107
    def _iter_revision_ids(self):
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
108
        mapping = self.get_mapping()
17.40.21 by Jelmer Vernooij
Fix GitRepository.all_revision_ids() to find all revisions.
109
        for sha in self._git.object_store:
110
            o = self._git.object_store[sha]
111
            if not isinstance(o, Commit):
112
                continue
17.41.106 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
113
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
17.40.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
114
                self.lookup_foreign_revision_id)
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
115
            yield o.id, rev.revision_id, roundtrip_revid
17.40.46 by Jelmer Vernooij
Generate refs/bzr/* if not set yet.
116
117
    def all_revision_ids(self):
118
        ret = set([])
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
119
        for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
17.40.46 by Jelmer Vernooij
Generate refs/bzr/* if not set yet.
120
            ret.add(revid)
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
121
            if roundtrip_revid:
122
                ret.add(roundtrip_revid)
17.1.57 by Jelmer Vernooij
Implement Repository.all_revision_ids().
123
        return ret
124
17.1.114 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
125
    def get_parent_map(self, revids):
126
        parent_map = {}
127
        for revision_id in revids:
128
            assert isinstance(revision_id, str)
129
            if revision_id == revision.NULL_REVISION:
130
                parent_map[revision_id] = ()
131
                continue
17.25.91 by Jelmer Vernooij
Use standard names for lookup functions.
132
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
17.25.53 by Jelmer Vernooij
Cope with Dulwich returning KeyError when a commit is not found.
133
            try:
17.25.273 by Jelmer Vernooij
Update to newer version of Dulwich, saner branch names.
134
                commit = self._git[hexsha]
17.25.53 by Jelmer Vernooij
Cope with Dulwich returning KeyError when a commit is not found.
135
            except KeyError:
136
                continue
17.41.99 by Jelmer Vernooij
Fix formatting.
137
            parent_map[revision_id] = [
138
                self.lookup_foreign_revision_id(p, mapping)
139
                for p in commit.parents]
17.1.114 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
140
        return parent_map
141
142
    def get_ancestry(self, revision_id, topo_sorted=True):
143
        """See Repository.get_ancestry().
144
        """
145
        if revision_id is None:
17.1.220 by Jelmer Vernooij
Fix get_ancestry() contents.
146
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
17.1.114 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
147
        assert isinstance(revision_id, str)
148
        ancestry = []
149
        graph = self.get_graph()
150
        for rev, parents in graph.iter_ancestry([revision_id]):
151
            ancestry.append(rev)
152
        ancestry.reverse()
17.1.220 by Jelmer Vernooij
Fix get_ancestry() contents.
153
        return [None] + ancestry
17.1.26 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
154
155
    def get_signature_text(self, revision_id):
156
        raise errors.NoSuchRevision(self, revision_id)
157
17.46.1 by Jelmer Vernooij
use transport repo objects even for local access.
158
    def pack(self, hint=None, clean_obsolete_packs=False):
159
        self._git.object_store.pack_loose_objects()
160
17.25.91 by Jelmer Vernooij
Use standard names for lookup functions.
161
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
17.1.107 by Jelmer Vernooij
Add lookup_revision_id stub.
162
        """Lookup a revision id.
17.25.117 by Jelmer Vernooij
Avoid iterating over all keys in the tdb database.
163
17.1.107 by Jelmer Vernooij
Add lookup_revision_id stub.
164
        """
17.41.110 by Jelmer Vernooij
Don't claim to support working trees for bare repositories.
165
        assert type(foreign_revid) is str
17.25.90 by Jelmer Vernooij
Make GitRevisions VF implementation behave as the interface expects.
166
        if mapping is None:
167
            mapping = self.get_mapping()
17.25.356 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
168
        from dulwich.protocol import (
169
            ZERO_SHA,
170
            )
17.25.355 by Jelmer Vernooij
Fix tests.
171
        if foreign_revid == ZERO_SHA:
172
            return revision.NULL_REVISION
17.40.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
173
        commit = self._git[foreign_revid]
17.41.106 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
174
        rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
175
            lambda x: None)
176
        # FIXME: check testament before doing this?
177
        if roundtrip_revid:
178
            return roundtrip_revid
179
        else:
180
            return rev.revision_id
17.1.107 by Jelmer Vernooij
Add lookup_revision_id stub.
181
17.1.43 by Jelmer Vernooij
Support signature functions.
182
    def has_signature_for_revision_id(self, revision_id):
183
        return False
184
17.25.354 by Jelmer Vernooij
Fix tests.
185
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
17.1.88 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
186
        try:
17.1.178 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
187
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
17.1.88 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
188
        except errors.InvalidRevisionId:
17.25.354 by Jelmer Vernooij
Fix tests.
189
            if mapping is None:
190
                mapping = self.get_mapping()
17.40.6 by Jelmer Vernooij
Roundtripping support for revision ids works.
191
            try:
17.41.99 by Jelmer Vernooij
Fix formatting.
192
                return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
193
                        mapping)
17.40.6 by Jelmer Vernooij
Roundtripping support for revision ids works.
194
            except KeyError:
17.40.46 by Jelmer Vernooij
Generate refs/bzr/* if not set yet.
195
                # Update refs from Git commit objects
196
                # FIXME: Hitting this a lot will be very inefficient...
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
197
                for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
198
                    if not roundtrip_revid:
199
                        continue
17.41.99 by Jelmer Vernooij
Fix formatting.
200
                    refname = mapping.revid_as_refname(roundtrip_revid)
201
                    self._git.refs[refname] = git_sha
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
202
                    if roundtrip_revid == bzr_revid:
17.25.354 by Jelmer Vernooij
Fix tests.
203
                        return git_sha, mapping
204
                raise errors.NoSuchRevision(self, bzr_revid)
17.1.88 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
205
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
206
    def get_revision(self, revision_id):
17.25.91 by Jelmer Vernooij
Use standard names for lookup functions.
207
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
17.1.130 by Jelmer Vernooij
Merge new dulwich; fetching objects from local repository works now; they aren't converted yet though.
208
        try:
17.25.273 by Jelmer Vernooij
Update to newer version of Dulwich, saner branch names.
209
            commit = self._git[git_commit_id]
17.1.130 by Jelmer Vernooij
Merge new dulwich; fetching objects from local repository works now; they aren't converted yet though.
210
        except KeyError:
211
            raise errors.NoSuchRevision(self, revision_id)
17.41.106 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
212
        revision, roundtrip_revid, verifiers = mapping.import_commit(
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
213
            commit, self.lookup_foreign_revision_id)
17.1.114 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
214
        assert revision is not None
17.41.106 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
215
        # FIXME: check verifiers ?
17.41.98 by Jelmer Vernooij
Put testament sha1 in revisions.
216
        if roundtrip_revid:
217
            revision.revision_id = roundtrip_revid
17.1.26 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
218
        return revision
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
219
220
    def has_revision(self, revision_id):
221
        try:
17.25.343 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
222
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
17.1.113 by Jelmer Vernooij
Make most tree inspection tests succeed.
223
        except errors.NoSuchRevision:
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
224
            return False
17.25.343 by Jelmer Vernooij
Fix Repository.has_revision{s,}.
225
        return (git_commit_id in self._git)
226
227
    def has_revisions(self, revision_ids):
17.25.354 by Jelmer Vernooij
Fix tests.
228
        return set(filter(self.has_revision, revision_ids))
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
229
17.1.114 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
230
    def get_revisions(self, revids):
17.1.117 by Jelmer Vernooij
Fix get_revisions().
231
        return [self.get_revision(r) for r in revids]
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
232
233
    def revision_trees(self, revids):
234
        for revid in revids:
235
            yield self.revision_tree(revid)
236
237
    def revision_tree(self, revision_id):
17.1.40 by Jelmer Vernooij
Fix more tests.
238
        revision_id = revision.ensure_null(revision_id)
239
        if revision_id == revision.NULL_REVISION:
240
            inv = inventory.Inventory(root_id=None)
241
            inv.revision_id = revision_id
242
            return revisiontree.RevisionTree(self, inv, revision_id)
17.1.178 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
243
        return GitRevisionTree(self, revision_id)
17.1.1 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
244
245
    def get_inventory(self, revision_id):
17.1.40 by Jelmer Vernooij
Fix more tests.
246
        assert revision_id != None
247
        return self.revision_tree(revision_id).inventory
17.1.26 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
248
17.1.91 by Jelmer Vernooij
Support bzr init --git.
249
    def set_make_working_trees(self, trees):
250
        pass
251
17.1.259 by Jelmer Vernooij
Improve formatting.
252
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
253
        progress=None):
17.1.129 by Jelmer Vernooij
Merge dulwich.
254
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
17.1.121 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
255
17.25.142 by Jelmer Vernooij
Fix check in git repos.
256
    def _get_versioned_file_checker(self, text_key_references=None,
257
                        ancestors=None):
258
        return GitVersionedFileChecker(self,
259
            text_key_references=text_key_references, ancestors=ancestors)
17.41.99 by Jelmer Vernooij
Fix formatting.
260
17.25.142 by Jelmer Vernooij
Fix check in git repos.
261
262
class GitVersionedFileChecker(repository._VersionedFileChecker):
263
264
    file_ids = []
265
266
    def _check_file_version_parents(self, texts, progress_bar):
267
        return {}, []
268
17.1.26 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
269
17.1.272 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
270
class GitRepositoryFormat(repository.RepositoryFormat):
17.1.412 by Jelmer Vernooij
get remote dpush to a point where we now what to send.
271
    """Git repository format."""
17.3.1 by Aaron Bentley
Make checkouts work
272
273
    supports_tree_reference = False
17.1.116 by Jelmer Vernooij
Unmark as deprecated.
274
    rich_root_data = True
17.1.54 by Jelmer Vernooij
Implement GitRepositoryFormat.get_format_description.
275
276
    def get_format_description(self):
277
        return "Git Repository"
17.1.116 by Jelmer Vernooij
Unmark as deprecated.
278
279
    def initialize(self, url, shared=False, _internal=False):
17.25.27 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
280
        raise errors.UninitializableFormat(self)
17.1.116 by Jelmer Vernooij
Unmark as deprecated.
281
282
    def check_conversion_target(self, target_repo_format):
283
        return target_repo_format.rich_root_data
17.24.8 by Jelmer Vernooij
Implement network name.
284
17.25.99 by Jelmer Vernooij
Provide right infrastructure for foreign repository tests from bzrlib.
285
    def get_foreign_tests_repository_factory(self):
17.25.154 by Jelmer Vernooij
Improve formatting.
286
        from bzrlib.plugins.git.tests.test_repository import (
287
            ForeignTestsRepositoryFactory,
288
            )
17.25.99 by Jelmer Vernooij
Provide right infrastructure for foreign repository tests from bzrlib.
289
        return ForeignTestsRepositoryFactory()
290
17.24.8 by Jelmer Vernooij
Implement network name.
291
    def network_name(self):
292
        return "git"