1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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
18
"""An adapter between a Git Repository and a Bazaar Branch"""
27
from bzrlib.foreign import (
31
from bzrlib.plugins.git.commit import (
34
from bzrlib.plugins.git.mapping import (
39
from bzrlib.plugins.git.tree import (
42
from bzrlib.plugins.git.versionedfiles import (
48
from dulwich.objects import (
54
class GitRepository(ForeignRepository):
55
"""An adapter to git repositories for bzr."""
58
_commit_builder_class = GitCommitBuilder
61
def __init__(self, gitdir, lockfiles):
62
ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
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)
75
def supports_rich_root(self):
78
def _warn_if_deprecated(self, branch=None):
79
# This class isn't deprecated
82
def get_mapping(self):
83
return default_mapping
85
def make_working_trees(self):
86
return not self._git.bare
88
def revision_graph_can_have_wrong_parents(self):
91
def dfetch(self, source, stop_revision):
92
interrepo = repository.InterRepository.get(source, self)
93
return interrepo.dfetch(stop_revision)
96
class LocalGitRepository(GitRepository):
97
"""Git repository on the file system."""
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)
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):
114
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
115
self.lookup_foreign_revision_id)
116
yield o.id, rev.revision_id, roundtrip_revid
118
def all_revision_ids(self):
120
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
123
ret.add(roundtrip_revid)
126
def get_parent_map(self, revids):
128
for revision_id in revids:
129
assert isinstance(revision_id, str)
130
if revision_id == revision.NULL_REVISION:
131
parent_map[revision_id] = ()
133
hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
135
commit = self._git[hexsha]
138
parent_map[revision_id] = [
139
self.lookup_foreign_revision_id(p, mapping)
140
for p in commit.parents]
143
def get_ancestry(self, revision_id, topo_sorted=True):
144
"""See Repository.get_ancestry().
146
if revision_id is None:
147
return [None, revision.NULL_REVISION] + self._all_revision_ids()
148
assert isinstance(revision_id, str)
150
graph = self.get_graph()
151
for rev, parents in graph.iter_ancestry([revision_id]):
154
return [None] + ancestry
156
def get_signature_text(self, revision_id):
157
raise errors.NoSuchRevision(self, revision_id)
159
def pack(self, hint=None, clean_obsolete_packs=False):
160
self._git.object_store.pack_loose_objects()
162
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
163
"""Lookup a revision id.
166
assert type(foreign_revid) is str
168
mapping = self.get_mapping()
169
from dulwich.protocol import (
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,
179
# FIXME: check testament before doing this?
181
return roundtrip_revid
183
return rev.revision_id
185
def has_signature_for_revision_id(self, revision_id):
188
def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
190
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
191
except errors.InvalidRevisionId:
193
mapping = self.get_mapping()
195
return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
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:
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)
209
def get_revision(self, revision_id):
210
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
212
commit = self._git[git_commit_id]
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 ?
220
revision.revision_id = roundtrip_revid
223
def has_revision(self, revision_id):
225
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
226
except errors.NoSuchRevision:
228
return (git_commit_id in self._git)
230
def has_revisions(self, revision_ids):
231
return set(filter(self.has_revision, revision_ids))
233
def get_revisions(self, revids):
234
return [self.get_revision(r) for r in revids]
236
def revision_trees(self, revids):
238
yield self.revision_tree(revid)
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)
248
def get_inventory(self, revision_id):
249
assert revision_id != None
250
return self.revision_tree(revision_id).inventory
252
def set_make_working_trees(self, trees):
255
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
257
return self._git.fetch_objects(determine_wants, graph_walker, progress)
259
def _get_versioned_file_checker(self, text_key_references=None,
261
return GitVersionedFileChecker(self,
262
text_key_references=text_key_references, ancestors=ancestors)
265
class GitVersionedFileChecker(repository._VersionedFileChecker):
269
def _check_file_version_parents(self, texts, progress_bar):
273
class GitRepositoryFormat(repository.RepositoryFormat):
274
"""Git repository format."""
276
supports_tree_reference = False
277
rich_root_data = True
280
def _matchingbzrdir(self):
281
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
282
return LocalGitControlDirFormat()
284
def get_format_description(self):
285
return "Git Repository"
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()
293
def check_conversion_target(self, target_repo_format):
294
return target_repo_format.rich_root_data
296
def get_foreign_tests_repository_factory(self):
297
from bzrlib.plugins.git.tests.test_repository import (
298
ForeignTestsRepositoryFactory,
300
return ForeignTestsRepositoryFactory()
302
def network_name(self):