1
# Copyright (C) 2008-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
# Copyright (C) 2007 Canonical Ltd
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""An adapter between a Git Repository and a Bazaar Branch"""
20
from __future__ import absolute_import
28
revision as _mod_revision,
33
from ..decorators import only_raises
34
from ..foreign import (
37
from ..sixish import (
45
from .filegraph import (
46
GitFileLastChangeScanner,
47
GitFileParentProvider,
49
from .mapping import (
59
from dulwich.errors import (
62
from dulwich.objects import (
66
from dulwich.object_store import (
71
class RepoReconciler(object):
72
"""Reconciler that reconciles a repository.
76
def __init__(self, repo, other=None, thorough=False):
77
"""Construct a RepoReconciler.
79
:param thorough: perform a thorough check which may take longer but
80
will correct non-data loss issues such as incorrect
86
"""Perform reconciliation.
88
After reconciliation the following attributes document found issues:
89
inconsistent_parents: The number of revisions in the repository whose
90
ancestry was being reported incorrectly.
91
garbage_inventories: The number of inventory objects without revisions
92
that were garbage collected.
96
class GitCheck(check.Check):
98
def __init__(self, repository, check_repo=True):
99
self.repository = repository
100
self.check_repo = check_repo
101
self.checked_rev_cnt = 0
102
self.object_count = None
105
def check(self, callback_refs=None, check_repo=True):
106
if callback_refs is None:
108
with self.repository.lock_read(), \
109
ui.ui_factory.nested_progress_bar() as self.progress:
110
shas = set(self.repository._git.object_store)
111
self.object_count = len(shas)
112
# TODO(jelmer): Check more things
113
for i, sha in enumerate(shas):
114
self.progress.update('checking objects', i, self.object_count)
115
o = self.repository._git.object_store[sha]
118
except Exception as e:
119
self.problems.append((sha, e))
121
def _report_repo_results(self, verbose):
122
trace.note('checked repository {0} format {1}'.format(
123
self.repository.user_url,
124
self.repository._format))
125
trace.note('%6d objects', self.object_count)
126
for sha, problem in self.problems:
127
trace.note('%s: %s', sha, problem)
129
def report_results(self, verbose):
131
self._report_repo_results(verbose)
134
_optimisers_loaded = False
137
def lazy_load_optimisers():
138
global _optimisers_loaded
139
if _optimisers_loaded:
141
from . import interrepo
142
for optimiser in [interrepo.InterRemoteGitNonGitRepository,
143
interrepo.InterLocalGitNonGitRepository,
144
interrepo.InterLocalGitLocalGitRepository,
145
interrepo.InterRemoteGitLocalGitRepository,
146
interrepo.InterToLocalGitRepository,
147
interrepo.InterToRemoteGitRepository,
149
repository.InterRepository.register_optimiser(optimiser)
150
_optimisers_loaded = True
153
class GitRepository(ForeignRepository):
154
"""An adapter to git repositories for bzr."""
157
vcs = foreign_vcs_git
160
def __init__(self, gitdir):
161
self._transport = gitdir.root_transport
162
super(GitRepository, self).__init__(GitRepositoryFormat(),
163
gitdir, control_files=None)
164
self.base = gitdir.root_transport.base
165
lazy_load_optimisers()
166
self._lock_mode = None
169
def add_fallback_repository(self, basis_url):
170
raise errors.UnstackableRepositoryFormat(self._format,
171
self.control_transport.base)
176
def get_physical_lock_status(self):
179
def lock_write(self):
180
"""See Branch.lock_write()."""
182
if self._lock_mode != 'w':
183
raise errors.ReadOnlyError(self)
184
self._lock_count += 1
186
self._lock_mode = 'w'
188
self._transaction = transactions.WriteTransaction()
189
return repository.RepositoryWriteLockResult(self.unlock, None)
191
def break_lock(self):
192
raise NotImplementedError(self.break_lock)
194
def dont_leave_lock_in_place(self):
195
raise NotImplementedError(self.dont_leave_lock_in_place)
197
def leave_lock_in_place(self):
198
raise NotImplementedError(self.leave_lock_in_place)
202
if self._lock_mode not in ('r', 'w'):
204
self._lock_count += 1
206
self._lock_mode = 'r'
208
self._transaction = transactions.ReadOnlyTransaction()
209
return lock.LogicalLockResult(self.unlock)
211
@only_raises(errors.LockNotHeld, errors.LockBroken)
213
if self._lock_count == 0:
214
raise errors.LockNotHeld(self)
215
if self._lock_count == 1 and self._lock_mode == 'w':
216
if self._write_group is not None:
217
self.abort_write_group()
218
self._lock_count -= 1
219
self._lock_mode = None
220
raise errors.BzrError(
221
'Must end write groups before releasing write locks.')
222
self._lock_count -= 1
223
if self._lock_count == 0:
224
self._lock_mode = None
225
transaction = self._transaction
226
self._transaction = None
229
def is_write_locked(self):
230
return (self._lock_mode == 'w')
233
return (self._lock_mode is not None)
235
def get_transaction(self):
236
"""See Repository.get_transaction()."""
237
if self._transaction is None:
238
return transactions.PassThroughTransaction()
240
return self._transaction
242
def reconcile(self, other=None, thorough=False):
243
"""Reconcile this repository."""
244
reconciler = RepoReconciler(self, thorough=thorough)
245
reconciler.reconcile()
248
def supports_rich_root(self):
251
def get_mapping(self):
252
return default_mapping
254
def make_working_trees(self):
255
return not self._git.get_config().get_boolean(("core", ), "bare")
257
def revision_graph_can_have_wrong_parents(self):
260
def add_signature_text(self, revid, signature):
261
raise errors.UnsupportedOperation(self.add_signature_text, self)
263
def sign_revision(self, revision_id, gpg_strategy):
264
raise errors.UnsupportedOperation(self.add_signature_text, self)
267
class LocalGitRepository(GitRepository):
268
"""Git repository on the file system."""
270
def __init__(self, gitdir):
271
GitRepository.__init__(self, gitdir)
272
self._git = gitdir._git
273
self._file_change_scanner = GitFileLastChangeScanner(self)
274
self._transaction = None
276
def get_commit_builder(self, branch, parents, config, timestamp=None,
277
timezone=None, committer=None, revprops=None,
278
revision_id=None, lossy=False):
279
"""Obtain a CommitBuilder for this repository.
281
:param branch: Branch to commit to.
282
:param parents: Revision ids of the parents of the new revision.
283
:param config: Configuration to use.
284
:param timestamp: Optional timestamp recorded for commit.
285
:param timezone: Optional timezone for timestamp.
286
:param committer: Optional committer to set for commit.
287
:param revprops: Optional dictionary of revision properties.
288
:param revision_id: Optional revision id.
289
:param lossy: Whether to discard data that can not be natively
290
represented, when pushing to a foreign VCS
292
builder = GitCommitBuilder(
293
self, parents, config, timestamp, timezone, committer, revprops,
295
self.start_write_group()
298
def get_file_graph(self):
299
return _mod_graph.Graph(GitFileParentProvider(
300
self._file_change_scanner))
302
def iter_files_bytes(self, desired_files):
303
"""Iterate through file versions.
305
Files will not necessarily be returned in the order they occur in
306
desired_files. No specific order is guaranteed.
308
Yields pairs of identifier, bytes_iterator. identifier is an opaque
309
value supplied by the caller as part of desired_files. It should
310
uniquely identify the file version in the caller's context. (Examples:
311
an index number or a TreeTransform trans_id.)
313
bytes_iterator is an iterable of bytestrings for the file. The
314
kind of iterable and length of the bytestrings are unspecified, but for
315
this implementation, it is a list of bytes produced by
316
VersionedFile.get_record_stream().
318
:param desired_files: a list of (file_id, revision_id, identifier)
322
for (file_id, revision_id, identifier) in desired_files:
323
per_revision.setdefault(revision_id, []).append(
324
(file_id, identifier))
325
for revid, files in viewitems(per_revision):
327
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
328
except errors.NoSuchRevision:
329
raise errors.RevisionNotPresent(revid, self)
331
commit = self._git.object_store[commit_id]
333
raise errors.RevisionNotPresent(revid, self)
334
root_tree = commit.tree
335
for fileid, identifier in files:
337
path = mapping.parse_file_id(fileid)
339
raise errors.RevisionNotPresent((fileid, revid), self)
341
obj = tree_lookup_path(
342
self._git.object_store.__getitem__, root_tree,
343
path.encode('utf-8'))
344
if isinstance(obj, tuple):
345
(mode, item_id) = obj
346
obj = self._git.object_store[item_id]
348
raise errors.RevisionNotPresent((fileid, revid), self)
350
if obj.type_name == b"tree":
351
yield (identifier, [])
352
elif obj.type_name == b"blob":
353
yield (identifier, obj.chunked)
355
raise AssertionError("file text resolved to %r" % obj)
357
def gather_stats(self, revid=None, committers=None):
358
"""See Repository.gather_stats()."""
359
result = super(LocalGitRepository, self).gather_stats(
362
for sha in self._git.object_store:
363
o = self._git.object_store[sha]
364
if o.type_name == b"commit":
366
result['revisions'] = len(revs)
369
def _iter_revision_ids(self):
370
mapping = self.get_mapping()
371
for sha in self._git.object_store:
372
o = self._git.object_store[sha]
373
if not isinstance(o, Commit):
375
rev, roundtrip_revid, verifiers = mapping.import_commit(
376
o, mapping.revision_id_foreign_to_bzr)
377
yield o.id, rev.revision_id, roundtrip_revid
379
def all_revision_ids(self):
381
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
383
ret.add(roundtrip_revid)
388
def _get_parents(self, revid, no_alternates=False):
389
if type(revid) != bytes:
392
(hexsha, mapping) = self.lookup_bzr_revision_id(revid)
393
except errors.NoSuchRevision:
395
# FIXME: Honor no_alternates setting
397
commit = self._git.object_store[hexsha]
401
for p in commit.parents:
403
ret.append(self.lookup_foreign_revision_id(p, mapping))
405
ret.append(mapping.revision_id_foreign_to_bzr(p))
408
def _get_parent_map_no_fallbacks(self, revids):
409
return self.get_parent_map(revids, no_alternates=True)
411
def get_parent_map(self, revids, no_alternates=False):
413
for revision_id in revids:
414
parents = self._get_parents(
415
revision_id, no_alternates=no_alternates)
416
if revision_id == _mod_revision.NULL_REVISION:
417
parent_map[revision_id] = ()
421
if len(parents) == 0:
422
parents = [_mod_revision.NULL_REVISION]
423
parent_map[revision_id] = tuple(parents)
426
def get_known_graph_ancestry(self, revision_ids):
427
"""Return the known graph for a set of revision ids and their ancestors.
429
pending = set(revision_ids)
433
for revid in pending:
434
if revid == _mod_revision.NULL_REVISION:
436
parents = self._get_parents(revid)
437
if parents is not None:
438
this_parent_map[revid] = parents
439
parent_map.update(this_parent_map)
441
for values in viewvalues(this_parent_map):
442
pending.update(values)
443
pending = pending.difference(parent_map)
444
return _mod_graph.KnownGraph(parent_map)
446
def get_signature_text(self, revision_id):
447
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
449
commit = self._git.object_store[git_commit_id]
451
raise errors.NoSuchRevision(self, revision_id)
452
if commit.gpgsig is None:
453
raise errors.NoSuchRevision(self, revision_id)
456
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
457
result = GitCheck(self, check_repo=check_repo)
458
result.check(callback_refs)
461
def pack(self, hint=None, clean_obsolete_packs=False):
462
self._git.object_store.pack_loose_objects()
464
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
465
"""Lookup a revision id.
467
:param foreign_revid: Foreign revision id to look up
468
:param mapping: Mapping to use (use default mapping if not specified)
469
:raise KeyError: If foreign revision was not found
470
:return: bzr revision id
472
if not isinstance(foreign_revid, bytes):
473
raise TypeError(foreign_revid)
475
mapping = self.get_mapping()
476
if foreign_revid == ZERO_SHA:
477
return _mod_revision.NULL_REVISION
478
commit = self._git.object_store.peel_sha(foreign_revid)
479
if not isinstance(commit, Commit):
480
raise NotCommitError(commit.id)
481
rev, roundtrip_revid, verifiers = mapping.import_commit(
482
commit, mapping.revision_id_foreign_to_bzr)
483
# FIXME: check testament before doing this?
485
return roundtrip_revid
487
return rev.revision_id
489
def has_signature_for_revision_id(self, revision_id):
490
"""Check whether a GPG signature is present for this revision.
492
This is never the case for Git repositories.
495
self.get_signature_text(revision_id)
496
except errors.NoSuchRevision:
501
def verify_revision_signature(self, revision_id, gpg_strategy):
502
"""Verify the signature on a revision.
504
:param revision_id: the revision to verify
505
:gpg_strategy: the GPGStrategy object to used
507
:return: gpg.SIGNATURE_VALID or a failed SIGNATURE_ value
509
from breezy import gpg
510
with self.lock_read():
511
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
513
commit = self._git.object_store[git_commit_id]
515
raise errors.NoSuchRevision(self, revision_id)
517
if commit.gpgsig is None:
518
return gpg.SIGNATURE_NOT_SIGNED, None
520
without_sig = Commit.from_string(commit.as_raw_string())
521
without_sig.gpgsig = None
523
(result, key, plain_text) = gpg_strategy.verify(
524
without_sig.as_raw_string(), commit.gpgsig)
527
def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
528
"""Lookup a bzr revision id in a Git repository.
530
:param bzr_revid: Bazaar revision id
531
:param mapping: Optional mapping to use
532
:return: Tuple with git commit id, mapping that was used and supplement
536
(git_sha, mapping) = mapping_registry.revision_id_bzr_to_foreign(
538
except errors.InvalidRevisionId:
540
mapping = self.get_mapping()
542
return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
545
# Update refs from Git commit objects
546
# FIXME: Hitting this a lot will be very inefficient...
547
pb = ui.ui_factory.nested_progress_bar()
549
for i, (git_sha, revid, roundtrip_revid) in enumerate(
550
self._iter_revision_ids()):
551
if not roundtrip_revid:
553
pb.update("resolving revision id", i)
554
refname = mapping.revid_as_refname(roundtrip_revid)
555
self._git.refs[refname] = git_sha
556
if roundtrip_revid == bzr_revid:
557
return git_sha, mapping
560
raise errors.NoSuchRevision(self, bzr_revid)
562
return (git_sha, mapping)
564
def get_revision(self, revision_id):
565
if not isinstance(revision_id, bytes):
566
raise errors.InvalidRevisionId(revision_id, self)
567
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
569
commit = self._git.object_store[git_commit_id]
571
raise errors.NoSuchRevision(self, revision_id)
572
revision, roundtrip_revid, verifiers = mapping.import_commit(
573
commit, self.lookup_foreign_revision_id)
576
# FIXME: check verifiers ?
578
revision.revision_id = roundtrip_revid
581
def has_revision(self, revision_id):
582
"""See Repository.has_revision."""
583
if revision_id == _mod_revision.NULL_REVISION:
586
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
587
except errors.NoSuchRevision:
589
return (git_commit_id in self._git)
591
def has_revisions(self, revision_ids):
592
"""See Repository.has_revisions."""
593
return set(filter(self.has_revision, revision_ids))
595
def iter_revisions(self, revision_ids):
596
"""See Repository.get_revisions."""
597
for revid in revision_ids:
599
rev = self.get_revision(revid)
600
except errors.NoSuchRevision:
604
def revision_trees(self, revids):
605
"""See Repository.revision_trees."""
607
yield self.revision_tree(revid)
609
def revision_tree(self, revision_id):
610
"""See Repository.revision_tree."""
611
if revision_id is None:
612
raise ValueError('invalid revision id %s' % revision_id)
613
return GitRevisionTree(self, revision_id)
615
def get_deltas_for_revisions(self, revisions, specific_fileids=None):
616
"""Produce a generator of revision deltas.
618
Note that the input is a sequence of REVISIONS, not revision_ids.
619
Trees will be held in memory until the generator exits.
620
Each delta is relative to the revision's lefthand predecessor.
622
:param specific_fileids: if not None, the result is filtered
623
so that only those file-ids, their parents and their
624
children are included.
626
# Get the revision-ids of interest
627
required_trees = set()
628
for revision in revisions:
629
required_trees.add(revision.revision_id)
630
required_trees.update(revision.parent_ids[:1])
632
trees = dict((t.get_revision_id(), t) for
633
t in self.revision_trees(required_trees))
635
# Calculate the deltas
636
for revision in revisions:
637
if not revision.parent_ids:
638
old_tree = self.revision_tree(_mod_revision.NULL_REVISION)
640
old_tree = trees[revision.parent_ids[0]]
641
new_tree = trees[revision.revision_id]
642
if specific_fileids is not None:
643
specific_files = [new_tree.id2path(
644
fid) for fid in specific_fileids]
646
specific_files = None
647
yield new_tree.changes_from(
648
old_tree, specific_files=specific_files)
650
def set_make_working_trees(self, trees):
651
raise errors.UnsupportedOperation(self.set_make_working_trees, self)
653
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
654
progress=None, limit=None):
655
return self._git.fetch_objects(determine_wants, graph_walker, progress,
659
class GitRepositoryFormat(repository.RepositoryFormat):
660
"""Git repository format."""
662
supports_versioned_directories = False
663
supports_tree_reference = True
664
rich_root_data = True
665
supports_leaving_lock = False
667
supports_funky_characters = True
668
supports_external_lookups = False
669
supports_full_versioned_files = False
670
supports_revision_signatures = False
671
supports_nesting_repositories = False
672
revision_graph_can_have_wrong_parents = False
673
supports_unreferenced_revisions = True
674
supports_setting_revision_ids = False
675
supports_storing_branch_nick = False
676
supports_overriding_transport = False
677
supports_custom_revision_properties = False
678
records_per_file_revision = False
681
def _matchingcontroldir(self):
682
from .dir import LocalGitControlDirFormat
683
return LocalGitControlDirFormat()
685
def get_format_description(self):
686
return "Git Repository"
688
def initialize(self, controldir, shared=False, _internal=False):
689
from .dir import GitDir
690
if not isinstance(controldir, GitDir):
691
raise errors.UninitializableFormat(self)
692
return controldir.open_repository()
694
def check_conversion_target(self, target_repo_format):
695
return target_repo_format.rich_root_data
697
def get_foreign_tests_repository_factory(self):
698
from .tests.test_repository import (
699
ForeignTestsRepositoryFactory,
701
return ForeignTestsRepositoryFactory()
703
def network_name(self):
707
def get_extra_interrepo_test_combinations():
708
from ..bzr.groupcompress_repo import RepositoryFormat2a
709
from . import interrepo
711
(interrepo.InterLocalGitNonGitRepository,
712
GitRepositoryFormat(), RepositoryFormat2a()),
713
(interrepo.InterLocalGitLocalGitRepository,
714
GitRepositoryFormat(), GitRepositoryFormat()),
715
(interrepo.InterToLocalGitRepository,
716
RepositoryFormat2a(), GitRepositoryFormat()),