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"""
28
from bzrlib.revisiontree import InventoryRevisionTree
29
except ImportError: # bzr < 2.4
30
from bzrlib.revisiontree import RevisionTree as InventoryRevisionTree
31
from bzrlib.foreign import (
35
from bzrlib.plugins.git.commit import (
38
from bzrlib.plugins.git.mapping import (
43
from bzrlib.plugins.git.tree import (
46
from bzrlib.plugins.git.versionedfiles import (
51
from dulwich.objects import (
56
from dulwich.object_store import (
61
class RepoReconciler(object):
62
"""Reconciler that reconciles a repository.
66
def __init__(self, repo, other=None, thorough=False):
67
"""Construct a RepoReconciler.
69
:param thorough: perform a thorough check which may take longer but
70
will correct non-data loss issues such as incorrect
76
"""Perform reconciliation.
78
After reconciliation the following attributes document found issues:
79
inconsistent_parents: The number of revisions in the repository whose
80
ancestry was being reported incorrectly.
81
garbage_inventories: The number of inventory objects without revisions
82
that were garbage collected.
86
class GitCheck(check.Check):
88
def __init__(self, repository, check_repo=True):
89
self.repository = repository
90
self.checked_rev_cnt = 0
92
def check(self, callback_refs=None, check_repo=True):
93
if callback_refs is None:
95
self.repository.lock_read()
96
self.repository.unlock()
98
def report_results(self, verbose):
102
class GitRepository(ForeignRepository):
103
"""An adapter to git repositories for bzr."""
109
def __init__(self, gitdir, lockfiles):
110
ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, lockfiles)
111
from bzrlib.plugins.git import fetch, push
112
for optimiser in [fetch.InterRemoteGitNonGitRepository,
113
fetch.InterLocalGitNonGitRepository,
114
fetch.InterGitGitRepository,
115
push.InterToLocalGitRepository,
116
push.InterToRemoteGitRepository]:
117
repository.InterRepository.register_optimiser(optimiser)
119
def add_fallback_repository(self, basis_url):
120
raise errors.UnstackableRepositoryFormat(self._format, self.control_transport.base)
125
def reconcile(self, other=None, thorough=False):
126
"""Reconcile this repository."""
127
reconciler = RepoReconciler(self, thorough=thorough)
128
reconciler.reconcile()
131
def supports_rich_root(self):
134
def _warn_if_deprecated(self, branch=None): # for bzr < 2.4
135
# This class isn't deprecated
138
def get_mapping(self):
139
return default_mapping
141
def make_working_trees(self):
142
return not self._git.bare
144
def revision_graph_can_have_wrong_parents(self):
147
def dfetch(self, source, stop_revision):
148
interrepo = repository.InterRepository.get(source, self)
149
return interrepo.dfetch(stop_revision)
151
def add_signature_text(self, revid, signature):
152
raise errors.UnsupportedOperation(self.add_signature_text, self)
155
class LocalGitRepository(GitRepository):
156
"""Git repository on the file system."""
158
def __init__(self, gitdir, lockfiles):
159
GitRepository.__init__(self, gitdir, lockfiles)
160
self.base = gitdir.root_transport.base
161
self._git = gitdir._git
162
self.signatures = None
163
self.revisions = None
164
self.inventories = None
165
self.texts = GitTexts(self)
167
def get_commit_builder(self, branch, parents, config, timestamp=None,
168
timezone=None, committer=None, revprops=None,
169
revision_id=None, lossy=False):
170
"""Obtain a CommitBuilder for this repository.
172
:param branch: Branch to commit to.
173
:param parents: Revision ids of the parents of the new revision.
174
:param config: Configuration to use.
175
:param timestamp: Optional timestamp recorded for commit.
176
:param timezone: Optional timezone for timestamp.
177
:param committer: Optional committer to set for commit.
178
:param revprops: Optional dictionary of revision properties.
179
:param revision_id: Optional revision id.
180
:param lossy: Whether to discard data that can not be natively
181
represented, when pushing to a foreign VCS
183
self.start_write_group()
184
return GitCommitBuilder(self, parents, config,
185
timestamp, timezone, committer, revprops, revision_id,
188
def iter_files_bytes(self, desired_files):
189
"""Iterate through file versions.
191
Files will not necessarily be returned in the order they occur in
192
desired_files. No specific order is guaranteed.
194
Yields pairs of identifier, bytes_iterator. identifier is an opaque
195
value supplied by the caller as part of desired_files. It should
196
uniquely identify the file version in the caller's context. (Examples:
197
an index number or a TreeTransform trans_id.)
199
bytes_iterator is an iterable of bytestrings for the file. The
200
kind of iterable and length of the bytestrings are unspecified, but for
201
this implementation, it is a list of bytes produced by
202
VersionedFile.get_record_stream().
204
:param desired_files: a list of (file_id, revision_id, identifier)
208
for (file_id, revision_id, identifier) in desired_files:
209
per_revision.setdefault(revision_id, []).append((file_id, identifier))
210
for revid, files in per_revision.iteritems():
211
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
213
commit = self._git.object_store[commit_id]
215
raise errors.RevisionNotPresent(revid, self)
216
root_tree = commit.tree
217
for fileid, identifier in files:
218
path = mapping.parse_file_id(fileid)
220
obj = tree_lookup_path(
221
self._git.object_store.__getitem__, root_tree, path)
222
if isinstance(obj, tuple):
223
(mode, item_id) = obj
224
obj = self._git.object_store[item_id]
226
raise errors.RevisionNotPresent((fileid, revid), self)
228
if obj.type_name == "tree":
229
yield (identifier, [])
230
elif obj.type_name == "blob":
231
yield (identifier, obj.chunked)
233
raise AssertionError("file text resolved to %r" % obj)
236
def _iter_revision_ids(self):
237
mapping = self.get_mapping()
238
for sha in self._git.object_store:
239
o = self._git.object_store[sha]
240
if not isinstance(o, Commit):
242
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
243
mapping.revision_id_foreign_to_bzr)
244
yield o.id, rev.revision_id, roundtrip_revid
246
def all_revision_ids(self):
248
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
251
ret.add(roundtrip_revid)
254
def get_parent_map(self, revids):
256
for revision_id in revids:
257
assert isinstance(revision_id, str)
258
if revision_id == revision.NULL_REVISION:
259
parent_map[revision_id] = ()
261
hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
263
commit = self._git[hexsha]
267
self.lookup_foreign_revision_id(p, mapping)
268
for p in commit.parents]
270
parents = [revision.NULL_REVISION]
271
parent_map[revision_id] = tuple(parents)
274
def get_ancestry(self, revision_id, topo_sorted=True):
275
"""See Repository.get_ancestry().
277
if revision_id is None:
278
return [None, revision.NULL_REVISION] + self._all_revision_ids()
279
assert isinstance(revision_id, str)
281
graph = self.get_graph()
282
for rev, parents in graph.iter_ancestry([revision_id]):
284
if revision.NULL_REVISION in ancestry:
285
ancestry.remove(revision.NULL_REVISION)
287
return [None] + ancestry
289
def get_signature_text(self, revision_id):
290
raise errors.NoSuchRevision(self, revision_id)
292
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
293
result = GitCheck(self, check_repo=check_repo)
294
result.check(callback_refs)
297
def pack(self, hint=None, clean_obsolete_packs=False):
298
self._git.object_store.pack_loose_objects()
300
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
301
"""Lookup a revision id.
304
assert type(foreign_revid) is str
306
mapping = self.get_mapping()
307
if foreign_revid == ZERO_SHA:
308
return revision.NULL_REVISION
309
commit = self._git[foreign_revid]
310
while isinstance(commit, Tag):
311
commit = self._git[commit.object[1]]
312
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
313
mapping.revision_id_foreign_to_bzr)
314
# FIXME: check testament before doing this?
316
return roundtrip_revid
318
return rev.revision_id
320
def has_signature_for_revision_id(self, revision_id):
323
def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
325
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
326
except errors.InvalidRevisionId:
328
mapping = self.get_mapping()
330
return (self._git.refs[mapping.revid_as_refname(bzr_revid)], mapping)
332
# Update refs from Git commit objects
333
# FIXME: Hitting this a lot will be very inefficient...
334
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
335
if not roundtrip_revid:
337
refname = mapping.revid_as_refname(roundtrip_revid)
338
self._git.refs[refname] = git_sha
339
if roundtrip_revid == bzr_revid:
340
return git_sha, mapping
341
raise errors.NoSuchRevision(self, bzr_revid)
343
def get_revision(self, revision_id):
344
if not isinstance(revision_id, str):
345
raise errors.InvalidRevisionId(revision_id, self)
346
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
348
commit = self._git[git_commit_id]
350
raise errors.NoSuchRevision(self, revision_id)
351
revision, roundtrip_revid, verifiers = mapping.import_commit(
352
commit, self.lookup_foreign_revision_id)
353
assert revision is not None
354
# FIXME: check verifiers ?
356
revision.revision_id = roundtrip_revid
359
def has_revision(self, revision_id):
360
"""See Repository.has_revision."""
361
if revision_id == revision.NULL_REVISION:
364
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
365
except errors.NoSuchRevision:
367
return (git_commit_id in self._git)
369
def has_revisions(self, revision_ids):
370
"""See Repository.has_revisions."""
371
return set(filter(self.has_revision, revision_ids))
373
def get_revisions(self, revids):
374
"""See Repository.get_revisions."""
375
return [self.get_revision(r) for r in revids]
377
def revision_trees(self, revids):
378
"""See Repository.revision_trees."""
380
yield self.revision_tree(revid)
382
def revision_tree(self, revision_id):
383
"""See Repository.revision_tree."""
384
revision_id = revision.ensure_null(revision_id)
385
if revision_id == revision.NULL_REVISION:
386
inv = inventory.Inventory(root_id=None)
387
inv.revision_id = revision_id
388
return InventoryRevisionTree(self, inv, revision_id)
389
return GitRevisionTree(self, revision_id)
391
def get_inventory(self, revision_id):
392
raise NotImplementedError(self.get_inventory)
394
def set_make_working_trees(self, trees):
395
# TODO: Set bare= in the configuration bug=777065
396
raise NotImplementedError(self.set_make_working_trees)
398
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
400
return self._git.fetch_objects(determine_wants, graph_walker, progress)
403
class GitRepositoryFormat(repository.RepositoryFormat):
404
"""Git repository format."""
406
supports_tree_reference = False
407
rich_root_data = True
408
supports_leaving_lock = False
410
supports_funky_characters = True
411
supports_external_lookups = False
412
supports_full_versioned_files = False
413
supports_revision_signatures = False
414
revision_graph_can_have_wrong_parents = False
417
def _matchingbzrdir(self):
418
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
419
return LocalGitControlDirFormat()
421
def get_format_description(self):
422
return "Git Repository"
424
def initialize(self, controldir, shared=False, _internal=False):
425
from bzrlib.plugins.git.dir import GitDir
426
if not isinstance(controldir, GitDir):
427
raise errors.UninitializableFormat(self)
428
return controldir.open_repository()
430
def check_conversion_target(self, target_repo_format):
431
return target_repo_format.rich_root_data
433
def get_foreign_tests_repository_factory(self):
434
from bzrlib.plugins.git.tests.test_repository import (
435
ForeignTestsRepositoryFactory,
437
return ForeignTestsRepositoryFactory()
439
def network_name(self):