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 (
48
from dulwich.objects import (
53
from dulwich.object_store import (
58
class RepoReconciler(object):
59
"""Reconciler that reconciles a repository.
63
def __init__(self, repo, other=None, thorough=False):
64
"""Construct a RepoReconciler.
66
:param thorough: perform a thorough check which may take longer but
67
will correct non-data loss issues such as incorrect
73
"""Perform reconciliation.
75
After reconciliation the following attributes document found issues:
76
inconsistent_parents: The number of revisions in the repository whose
77
ancestry was being reported incorrectly.
78
garbage_inventories: The number of inventory objects without revisions
79
that were garbage collected.
83
class GitCheck(check.Check):
85
def __init__(self, repository, check_repo=True):
86
self.repository = repository
87
self.checked_rev_cnt = 0
89
def check(self, callback_refs=None, check_repo=True):
90
if callback_refs is None:
92
self.repository.lock_read()
93
self.repository.unlock()
95
def report_results(self, verbose):
99
class GitRepository(ForeignRepository):
100
"""An adapter to git repositories for bzr."""
103
vcs = foreign_vcs_git
106
def __init__(self, gitdir, lockfiles):
107
ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, lockfiles)
108
from bzrlib.plugins.git import fetch, push
109
for optimiser in [fetch.InterRemoteGitNonGitRepository,
110
fetch.InterLocalGitNonGitRepository,
111
fetch.InterGitGitRepository,
112
push.InterToLocalGitRepository,
113
push.InterToRemoteGitRepository]:
114
repository.InterRepository.register_optimiser(optimiser)
116
def add_fallback_repository(self, basis_url):
117
raise errors.UnstackableRepositoryFormat(self._format, self.control_transport.base)
122
def reconcile(self, other=None, thorough=False):
123
"""Reconcile this repository."""
124
reconciler = RepoReconciler(self, thorough=thorough)
125
reconciler.reconcile()
128
def supports_rich_root(self):
131
def _warn_if_deprecated(self, branch=None): # for bzr < 2.4
132
# This class isn't deprecated
135
def get_mapping(self):
136
return default_mapping
138
def make_working_trees(self):
139
return not self._git.bare
141
def revision_graph_can_have_wrong_parents(self):
144
def dfetch(self, source, stop_revision):
145
interrepo = repository.InterRepository.get(source, self)
146
return interrepo.dfetch(stop_revision)
148
def add_signature_text(self, revid, signature):
149
raise errors.UnsupportedOperation(self.add_signature_text, self)
152
class LocalGitRepository(GitRepository):
153
"""Git repository on the file system."""
155
def __init__(self, gitdir, lockfiles):
156
GitRepository.__init__(self, gitdir, lockfiles)
157
self.base = gitdir.root_transport.base
158
self._git = gitdir._git
159
self.signatures = None
160
self.revisions = None
161
self.inventories = None
164
def get_commit_builder(self, branch, parents, config, timestamp=None,
165
timezone=None, committer=None, revprops=None,
166
revision_id=None, lossy=False):
167
"""Obtain a CommitBuilder for this repository.
169
:param branch: Branch to commit to.
170
:param parents: Revision ids of the parents of the new revision.
171
:param config: Configuration to use.
172
:param timestamp: Optional timestamp recorded for commit.
173
:param timezone: Optional timezone for timestamp.
174
:param committer: Optional committer to set for commit.
175
:param revprops: Optional dictionary of revision properties.
176
:param revision_id: Optional revision id.
177
:param lossy: Whether to discard data that can not be natively
178
represented, when pushing to a foreign VCS
180
self.start_write_group()
181
return GitCommitBuilder(self, parents, config,
182
timestamp, timezone, committer, revprops, revision_id,
185
def iter_files_bytes(self, desired_files):
186
"""Iterate through file versions.
188
Files will not necessarily be returned in the order they occur in
189
desired_files. No specific order is guaranteed.
191
Yields pairs of identifier, bytes_iterator. identifier is an opaque
192
value supplied by the caller as part of desired_files. It should
193
uniquely identify the file version in the caller's context. (Examples:
194
an index number or a TreeTransform trans_id.)
196
bytes_iterator is an iterable of bytestrings for the file. The
197
kind of iterable and length of the bytestrings are unspecified, but for
198
this implementation, it is a list of bytes produced by
199
VersionedFile.get_record_stream().
201
:param desired_files: a list of (file_id, revision_id, identifier)
205
for (file_id, revision_id, identifier) in desired_files:
206
per_revision.setdefault(revision_id, []).append((file_id, identifier))
207
for revid, files in per_revision.iteritems():
208
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
210
commit = self._git.object_store[commit_id]
212
raise errors.RevisionNotPresent(revid, self)
213
root_tree = commit.tree
214
for fileid, identifier in files:
215
path = mapping.parse_file_id(fileid)
217
obj = tree_lookup_path(
218
self._git.object_store.__getitem__, root_tree, path)
219
if isinstance(obj, tuple):
220
(mode, item_id) = obj
221
obj = self._git.object_store[item_id]
223
raise errors.RevisionNotPresent((fileid, revid), self)
225
if obj.type_name == "tree":
226
yield (identifier, [])
227
elif obj.type_name == "blob":
228
yield (identifier, obj.chunked)
230
raise AssertionError("file text resolved to %r" % obj)
233
def _iter_revision_ids(self):
234
mapping = self.get_mapping()
235
for sha in self._git.object_store:
236
o = self._git.object_store[sha]
237
if not isinstance(o, Commit):
239
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
240
mapping.revision_id_foreign_to_bzr)
241
yield o.id, rev.revision_id, roundtrip_revid
243
def all_revision_ids(self):
245
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
248
ret.add(roundtrip_revid)
251
def get_parent_map(self, revids):
253
for revision_id in revids:
254
assert isinstance(revision_id, str)
255
if revision_id == revision.NULL_REVISION:
256
parent_map[revision_id] = ()
258
hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
260
commit = self._git[hexsha]
264
self.lookup_foreign_revision_id(p, mapping)
265
for p in commit.parents]
267
parents = [revision.NULL_REVISION]
268
parent_map[revision_id] = tuple(parents)
271
def get_ancestry(self, revision_id, topo_sorted=True):
272
"""See Repository.get_ancestry().
274
if revision_id is None:
275
return [None, revision.NULL_REVISION] + self._all_revision_ids()
276
assert isinstance(revision_id, str)
278
graph = self.get_graph()
279
for rev, parents in graph.iter_ancestry([revision_id]):
281
if revision.NULL_REVISION in ancestry:
282
ancestry.remove(revision.NULL_REVISION)
284
return [None] + ancestry
286
def get_signature_text(self, revision_id):
287
raise errors.NoSuchRevision(self, revision_id)
289
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
290
result = GitCheck(self, check_repo=check_repo)
291
result.check(callback_refs)
294
def pack(self, hint=None, clean_obsolete_packs=False):
295
self._git.object_store.pack_loose_objects()
297
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
298
"""Lookup a revision id.
301
assert type(foreign_revid) is str
303
mapping = self.get_mapping()
304
if foreign_revid == ZERO_SHA:
305
return revision.NULL_REVISION
306
commit = self._git[foreign_revid]
307
while isinstance(commit, Tag):
308
commit = self._git[commit.object[1]]
309
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
310
mapping.revision_id_foreign_to_bzr)
311
# FIXME: check testament before doing this?
313
return roundtrip_revid
315
return rev.revision_id
317
def has_signature_for_revision_id(self, revision_id):
320
def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
322
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
323
except errors.InvalidRevisionId:
325
mapping = self.get_mapping()
327
return (self._git.refs[mapping.revid_as_refname(bzr_revid)], mapping)
329
# Update refs from Git commit objects
330
# FIXME: Hitting this a lot will be very inefficient...
331
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
332
if not roundtrip_revid:
334
refname = mapping.revid_as_refname(roundtrip_revid)
335
self._git.refs[refname] = git_sha
336
if roundtrip_revid == bzr_revid:
337
return git_sha, mapping
338
raise errors.NoSuchRevision(self, bzr_revid)
340
def get_revision(self, revision_id):
341
if not isinstance(revision_id, str):
342
raise errors.InvalidRevisionId(revision_id, self)
343
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
345
commit = self._git[git_commit_id]
347
raise errors.NoSuchRevision(self, revision_id)
348
revision, roundtrip_revid, verifiers = mapping.import_commit(
349
commit, self.lookup_foreign_revision_id)
350
assert revision is not None
351
# FIXME: check verifiers ?
353
revision.revision_id = roundtrip_revid
356
def has_revision(self, revision_id):
357
"""See Repository.has_revision."""
358
if revision_id == revision.NULL_REVISION:
361
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
362
except errors.NoSuchRevision:
364
return (git_commit_id in self._git)
366
def has_revisions(self, revision_ids):
367
"""See Repository.has_revisions."""
368
return set(filter(self.has_revision, revision_ids))
370
def get_revisions(self, revids):
371
"""See Repository.get_revisions."""
372
return [self.get_revision(r) for r in revids]
374
def revision_trees(self, revids):
375
"""See Repository.revision_trees."""
377
yield self.revision_tree(revid)
379
def revision_tree(self, revision_id):
380
"""See Repository.revision_tree."""
381
revision_id = revision.ensure_null(revision_id)
382
if revision_id == revision.NULL_REVISION:
383
inv = inventory.Inventory(root_id=None)
384
inv.revision_id = revision_id
385
return InventoryRevisionTree(self, inv, revision_id)
386
return GitRevisionTree(self, revision_id)
388
def get_inventory(self, revision_id):
389
raise NotImplementedError(self.get_inventory)
391
def set_make_working_trees(self, trees):
392
# TODO: Set bare= in the configuration bug=777065
393
raise NotImplementedError(self.set_make_working_trees)
395
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
397
return self._git.fetch_objects(determine_wants, graph_walker, progress)
400
class GitRepositoryFormat(repository.RepositoryFormat):
401
"""Git repository format."""
403
supports_tree_reference = False
404
rich_root_data = True
405
supports_leaving_lock = False
407
supports_funky_characters = True
408
supports_external_lookups = False
409
supports_full_versioned_files = False
410
supports_revision_signatures = False
411
revision_graph_can_have_wrong_parents = False
414
def _matchingbzrdir(self):
415
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
416
return LocalGitControlDirFormat()
418
def get_format_description(self):
419
return "Git Repository"
421
def initialize(self, controldir, shared=False, _internal=False):
422
from bzrlib.plugins.git.dir import GitDir
423
if not isinstance(controldir, GitDir):
424
raise errors.UninitializableFormat(self)
425
return controldir.open_repository()
427
def check_conversion_target(self, target_repo_format):
428
return target_repo_format.rich_root_data
430
def get_foreign_tests_repository_factory(self):
431
from bzrlib.plugins.git.tests.test_repository import (
432
ForeignTestsRepositoryFactory,
434
return ForeignTestsRepositoryFactory()
436
def network_name(self):