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,
32
version_info as breezy_version,
34
from ...decorators import only_raises
35
from ...foreign import (
42
from .filegraph import (
43
GitFileLastChangeScanner,
44
GitFileParentProvider,
46
from .mapping import (
56
from dulwich.errors import (
59
from dulwich.objects import (
63
from dulwich.object_store import (
68
class RepoReconciler(object):
69
"""Reconciler that reconciles a repository.
73
def __init__(self, repo, other=None, thorough=False):
74
"""Construct a RepoReconciler.
76
:param thorough: perform a thorough check which may take longer but
77
will correct non-data loss issues such as incorrect
83
"""Perform reconciliation.
85
After reconciliation the following attributes document found issues:
86
inconsistent_parents: The number of revisions in the repository whose
87
ancestry was being reported incorrectly.
88
garbage_inventories: The number of inventory objects without revisions
89
that were garbage collected.
93
class GitCheck(check.Check):
95
def __init__(self, repository, check_repo=True):
96
self.repository = repository
97
self.check_repo = check_repo
98
self.checked_rev_cnt = 0
99
self.object_count = None
102
def check(self, callback_refs=None, check_repo=True):
103
if callback_refs is None:
105
with self.repository.lock_read(), ui.ui_factory.nested_progress_bar() as self.progress:
106
shas = set(self.repository._git.object_store)
107
self.object_count = len(shas)
108
# TODO(jelmer): Check more things
109
for i, sha in enumerate(shas):
110
self.progress.update('checking objects', i, self.object_count)
111
o = self.repository._git.object_store[sha]
114
except Exception as e:
115
self.problems.append((sha, e))
117
def _report_repo_results(self, verbose):
118
trace.note('checked repository {0} format {1}'.format(
119
self.repository.user_url,
120
self.repository._format))
121
trace.note('%6d objects', self.object_count)
122
for sha, problem in self.problems:
123
trace.note('%s: %s', sha, problem)
125
def report_results(self, verbose):
127
self._report_repo_results(verbose)
130
_optimisers_loaded = False
132
def lazy_load_optimisers():
133
global _optimisers_loaded
134
if _optimisers_loaded:
136
from . import interrepo
137
for optimiser in [interrepo.InterRemoteGitNonGitRepository,
138
interrepo.InterLocalGitNonGitRepository,
139
interrepo.InterLocalGitLocalGitRepository,
140
interrepo.InterRemoteGitLocalGitRepository,
141
interrepo.InterToLocalGitRepository,
142
interrepo.InterToRemoteGitRepository,
144
repository.InterRepository.register_optimiser(optimiser)
145
_optimisers_loaded = True
148
class GitRepository(ForeignRepository):
149
"""An adapter to git repositories for bzr."""
152
vcs = foreign_vcs_git
155
def __init__(self, gitdir):
156
self._transport = gitdir.root_transport
157
super(GitRepository, self).__init__(GitRepositoryFormat(),
158
gitdir, control_files=None)
159
self.base = gitdir.root_transport.base
160
lazy_load_optimisers()
161
self._lock_mode = None
164
def add_fallback_repository(self, basis_url):
165
raise errors.UnstackableRepositoryFormat(self._format,
166
self.control_transport.base)
171
def get_physical_lock_status(self):
174
def lock_write(self):
175
"""See Branch.lock_write()."""
177
if self._lock_mode != 'w':
178
raise errors.ReadOnlyError(self)
179
self._lock_count += 1
181
self._lock_mode = 'w'
183
self._transaction = transactions.WriteTransaction()
184
return repository.RepositoryWriteLockResult(self.unlock, None)
186
def break_lock(self):
187
raise NotImplementedError(self.break_lock)
189
def dont_leave_lock_in_place(self):
190
raise NotImplementedError(self.dont_leave_lock_in_place)
192
def leave_lock_in_place(self):
193
raise NotImplementedError(self.leave_lock_in_place)
197
if self._lock_mode not in ('r', 'w'):
199
self._lock_count += 1
201
self._lock_mode = 'r'
203
self._transaction = transactions.ReadOnlyTransaction()
204
return lock.LogicalLockResult(self.unlock)
206
@only_raises(errors.LockNotHeld, errors.LockBroken)
208
if self._lock_count == 0:
209
raise errors.LockNotHeld(self)
210
if self._lock_count == 1 and self._lock_mode == 'w':
211
if self._write_group is not None:
212
self.abort_write_group()
213
self._lock_count -= 1
214
self._lock_mode = None
215
raise errors.BzrError(
216
'Must end write groups before releasing write locks.')
217
self._lock_count -= 1
218
if self._lock_count == 0:
219
self._lock_mode = None
220
transaction = self._transaction
221
self._transaction = None
224
def is_write_locked(self):
225
return (self._lock_mode == 'w')
228
return (self._lock_mode is not None)
230
def get_transaction(self):
231
"""See Repository.get_transaction()."""
232
if self._transaction is None:
233
return transactions.PassThroughTransaction()
235
return self._transaction
237
def reconcile(self, other=None, thorough=False):
238
"""Reconcile this repository."""
239
reconciler = RepoReconciler(self, thorough=thorough)
240
reconciler.reconcile()
243
def supports_rich_root(self):
246
def get_mapping(self):
247
return default_mapping
249
def make_working_trees(self):
250
return not self._git.get_config().get_boolean(("core", ), "bare")
252
def revision_graph_can_have_wrong_parents(self):
255
def add_signature_text(self, revid, signature):
256
raise errors.UnsupportedOperation(self.add_signature_text, self)
258
def sign_revision(self, revision_id, gpg_strategy):
259
raise errors.UnsupportedOperation(self.add_signature_text, self)
262
class LocalGitRepository(GitRepository):
263
"""Git repository on the file system."""
265
def __init__(self, gitdir):
266
GitRepository.__init__(self, gitdir)
267
self._git = gitdir._git
268
self._file_change_scanner = GitFileLastChangeScanner(self)
269
self._transaction = None
271
def get_commit_builder(self, branch, parents, config, timestamp=None,
272
timezone=None, committer=None, revprops=None,
273
revision_id=None, lossy=False):
274
"""Obtain a CommitBuilder for this repository.
276
:param branch: Branch to commit to.
277
:param parents: Revision ids of the parents of the new revision.
278
:param config: Configuration to use.
279
:param timestamp: Optional timestamp recorded for commit.
280
:param timezone: Optional timezone for timestamp.
281
:param committer: Optional committer to set for commit.
282
:param revprops: Optional dictionary of revision properties.
283
:param revision_id: Optional revision id.
284
:param lossy: Whether to discard data that can not be natively
285
represented, when pushing to a foreign VCS
287
builder = GitCommitBuilder(self, parents, config,
288
timestamp, timezone, committer, revprops, revision_id,
290
self.start_write_group()
293
def get_file_graph(self):
294
return _mod_graph.Graph(GitFileParentProvider(
295
self._file_change_scanner))
297
def iter_files_bytes(self, desired_files):
298
"""Iterate through file versions.
300
Files will not necessarily be returned in the order they occur in
301
desired_files. No specific order is guaranteed.
303
Yields pairs of identifier, bytes_iterator. identifier is an opaque
304
value supplied by the caller as part of desired_files. It should
305
uniquely identify the file version in the caller's context. (Examples:
306
an index number or a TreeTransform trans_id.)
308
bytes_iterator is an iterable of bytestrings for the file. The
309
kind of iterable and length of the bytestrings are unspecified, but for
310
this implementation, it is a list of bytes produced by
311
VersionedFile.get_record_stream().
313
:param desired_files: a list of (file_id, revision_id, identifier)
317
for (file_id, revision_id, identifier) in desired_files:
318
per_revision.setdefault(revision_id, []).append(
319
(file_id, identifier))
320
for revid, files in per_revision.iteritems():
322
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
323
except errors.NoSuchRevision:
324
raise errors.RevisionNotPresent(revid, self)
326
commit = self._git.object_store[commit_id]
328
raise errors.RevisionNotPresent(revid, self)
329
root_tree = commit.tree
330
for fileid, identifier in files:
332
path = mapping.parse_file_id(fileid)
334
raise errors.RevisionNotPresent((fileid, revid), self)
336
obj = tree_lookup_path(
337
self._git.object_store.__getitem__, root_tree, path)
338
if isinstance(obj, tuple):
339
(mode, item_id) = obj
340
obj = self._git.object_store[item_id]
342
raise errors.RevisionNotPresent((fileid, revid), self)
344
if obj.type_name == "tree":
345
yield (identifier, [])
346
elif obj.type_name == "blob":
347
yield (identifier, obj.chunked)
349
raise AssertionError("file text resolved to %r" % obj)
351
def gather_stats(self, revid=None, committers=None):
352
"""See Repository.gather_stats()."""
353
result = super(LocalGitRepository, self).gather_stats(revid, committers)
355
for sha in self._git.object_store:
356
o = self._git.object_store[sha]
357
if o.type_name == "commit":
359
result['revisions'] = len(revs)
362
def _iter_revision_ids(self):
363
mapping = self.get_mapping()
364
for sha in self._git.object_store:
365
o = self._git.object_store[sha]
366
if not isinstance(o, Commit):
368
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
369
mapping.revision_id_foreign_to_bzr)
370
yield o.id, rev.revision_id, roundtrip_revid
372
def all_revision_ids(self):
374
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
376
ret.add(roundtrip_revid)
381
def _get_parents(self, revid, no_alternates=False):
382
if type(revid) != bytes:
385
(hexsha, mapping) = self.lookup_bzr_revision_id(revid)
386
except errors.NoSuchRevision:
388
# FIXME: Honor no_alternates setting
390
commit = self._git.object_store[hexsha]
394
for p in commit.parents:
396
ret.append(self.lookup_foreign_revision_id(p, mapping))
398
ret.append(mapping.revision_id_foreign_to_bzr(p))
401
def _get_parent_map_no_fallbacks(self, revids):
402
return self.get_parent_map(revids, no_alternates=True)
404
def get_parent_map(self, revids, no_alternates=False):
406
for revision_id in revids:
407
parents = self._get_parents(revision_id, no_alternates=no_alternates)
408
if revision_id == _mod_revision.NULL_REVISION:
409
parent_map[revision_id] = ()
413
if len(parents) == 0:
414
parents = [_mod_revision.NULL_REVISION]
415
parent_map[revision_id] = tuple(parents)
418
def get_known_graph_ancestry(self, revision_ids):
419
"""Return the known graph for a set of revision ids and their ancestors.
421
pending = set(revision_ids)
425
for revid in pending:
426
if revid == _mod_revision.NULL_REVISION:
428
parents = self._get_parents(revid)
429
if parents is not None:
430
this_parent_map[revid] = parents
431
parent_map.update(this_parent_map)
433
map(pending.update, this_parent_map.itervalues())
434
pending = pending.difference(parent_map)
435
return _mod_graph.KnownGraph(parent_map)
437
def get_signature_text(self, revision_id):
438
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
440
commit = self._git.object_store[git_commit_id]
442
raise errors.NoSuchRevision(self, revision_id)
443
if commit.gpgsig is None:
444
raise errors.NoSuchRevision(self, revision_id)
447
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
448
result = GitCheck(self, check_repo=check_repo)
449
result.check(callback_refs)
452
def pack(self, hint=None, clean_obsolete_packs=False):
453
self._git.object_store.pack_loose_objects()
455
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
456
"""Lookup a revision id.
458
:param foreign_revid: Foreign revision id to look up
459
:param mapping: Mapping to use (use default mapping if not specified)
460
:raise KeyError: If foreign revision was not found
461
:return: bzr revision id
463
if type(foreign_revid) is not str:
464
raise TypeError(foreign_revid)
466
mapping = self.get_mapping()
467
if foreign_revid == ZERO_SHA:
468
return _mod_revision.NULL_REVISION
469
commit = self._git.object_store.peel_sha(foreign_revid)
470
if not isinstance(commit, Commit):
471
raise NotCommitError(commit.id)
472
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
473
mapping.revision_id_foreign_to_bzr)
474
# FIXME: check testament before doing this?
476
return roundtrip_revid
478
return rev.revision_id
480
def has_signature_for_revision_id(self, revision_id):
481
"""Check whether a GPG signature is present for this revision.
483
This is never the case for Git repositories.
486
self.get_signature_text(revision_id)
487
except errors.NoSuchRevision:
492
def verify_revision_signature(self, revision_id, gpg_strategy):
493
"""Verify the signature on a revision.
495
:param revision_id: the revision to verify
496
:gpg_strategy: the GPGStrategy object to used
498
:return: gpg.SIGNATURE_VALID or a failed SIGNATURE_ value
500
from breezy import gpg
501
with self.lock_read():
502
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
504
commit = self._git.object_store[git_commit_id]
506
raise errors.NoSuchRevision(self, revision_id)
508
if commit.gpgsig is None:
509
return gpg.SIGNATURE_NOT_SIGNED, None
511
without_sig = Commit.from_string(commit.as_raw_string())
512
without_sig.gpgsig = None
514
(result, key, plain_text) = gpg_strategy.verify(without_sig.as_raw_string(), commit.gpgsig)
517
def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
518
"""Lookup a bzr revision id in a Git repository.
520
:param bzr_revid: Bazaar revision id
521
:param mapping: Optional mapping to use
522
:return: Tuple with git commit id, mapping that was used and supplement
526
(git_sha, mapping) = mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
527
except errors.InvalidRevisionId:
529
mapping = self.get_mapping()
531
return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
534
# Update refs from Git commit objects
535
# FIXME: Hitting this a lot will be very inefficient...
536
pb = ui.ui_factory.nested_progress_bar()
538
for i, (git_sha, revid, roundtrip_revid) in enumerate(self._iter_revision_ids()):
539
if not roundtrip_revid:
541
pb.update("resolving revision id", i)
542
refname = mapping.revid_as_refname(roundtrip_revid)
543
self._git.refs[refname] = git_sha
544
if roundtrip_revid == bzr_revid:
545
return git_sha, mapping
548
raise errors.NoSuchRevision(self, bzr_revid)
550
return (git_sha, mapping)
552
def get_revision(self, revision_id):
553
if not isinstance(revision_id, str):
554
raise errors.InvalidRevisionId(revision_id, self)
555
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
557
commit = self._git.object_store[git_commit_id]
559
raise errors.NoSuchRevision(self, revision_id)
560
revision, roundtrip_revid, verifiers = mapping.import_commit(
561
commit, self.lookup_foreign_revision_id)
564
# FIXME: check verifiers ?
566
revision.revision_id = roundtrip_revid
569
def has_revision(self, revision_id):
570
"""See Repository.has_revision."""
571
if revision_id == _mod_revision.NULL_REVISION:
574
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
575
except errors.NoSuchRevision:
577
return (git_commit_id in self._git)
579
def has_revisions(self, revision_ids):
580
"""See Repository.has_revisions."""
581
return set(filter(self.has_revision, revision_ids))
583
def iter_revisions(self, revision_ids):
584
"""See Repository.get_revisions."""
585
for revid in revision_ids:
587
rev = self.get_revision(revid)
588
except errors.NoSuchRevision:
592
def revision_trees(self, revids):
593
"""See Repository.revision_trees."""
595
yield self.revision_tree(revid)
597
def revision_tree(self, revision_id):
598
"""See Repository.revision_tree."""
599
if revision_id is None:
600
raise ValueError('invalid revision id %s' % revision_id)
601
return GitRevisionTree(self, revision_id)
603
def get_deltas_for_revisions(self, revisions, specific_fileids=None):
604
"""Produce a generator of revision deltas.
606
Note that the input is a sequence of REVISIONS, not revision_ids.
607
Trees will be held in memory until the generator exits.
608
Each delta is relative to the revision's lefthand predecessor.
610
:param specific_fileids: if not None, the result is filtered
611
so that only those file-ids, their parents and their
612
children are included.
614
# Get the revision-ids of interest
615
required_trees = set()
616
for revision in revisions:
617
required_trees.add(revision.revision_id)
618
required_trees.update(revision.parent_ids[:1])
620
trees = dict((t.get_revision_id(), t) for
621
t in self.revision_trees(required_trees))
623
# Calculate the deltas
624
for revision in revisions:
625
if not revision.parent_ids:
626
old_tree = self.revision_tree(_mod_revision.NULL_REVISION)
628
old_tree = trees[revision.parent_ids[0]]
629
new_tree = trees[revision.revision_id]
630
if specific_fileids is not None:
631
specific_files = [new_tree.id2path(fid) for fid in specific_fileids]
633
specific_files = None
634
yield new_tree.changes_from(old_tree, specific_files=specific_files)
636
def set_make_working_trees(self, trees):
637
raise errors.UnsupportedOperation(self.set_make_working_trees, self)
639
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
640
progress=None, limit=None):
641
return self._git.fetch_objects(determine_wants, graph_walker, progress,
645
class GitRepositoryFormat(repository.RepositoryFormat):
646
"""Git repository format."""
648
supports_versioned_directories = False
649
supports_tree_reference = True
650
rich_root_data = True
651
supports_leaving_lock = False
653
supports_funky_characters = True
654
supports_external_lookups = False
655
supports_full_versioned_files = False
656
supports_revision_signatures = False
657
supports_nesting_repositories = False
658
revision_graph_can_have_wrong_parents = False
659
supports_unreferenced_revisions = True
660
supports_setting_revision_ids = False
661
supports_storing_branch_nick = False
662
supports_overriding_transport = False
663
supports_custom_revision_properties = False
664
records_per_file_revision = False
667
def _matchingcontroldir(self):
668
from .dir import LocalGitControlDirFormat
669
return LocalGitControlDirFormat()
671
def get_format_description(self):
672
return "Git Repository"
674
def initialize(self, controldir, shared=False, _internal=False):
675
from .dir import GitDir
676
if not isinstance(controldir, GitDir):
677
raise errors.UninitializableFormat(self)
678
return controldir.open_repository()
680
def check_conversion_target(self, target_repo_format):
681
return target_repo_format.rich_root_data
683
def get_foreign_tests_repository_factory(self):
684
from .tests.test_repository import (
685
ForeignTestsRepositoryFactory,
687
return ForeignTestsRepositoryFactory()
689
def network_name(self):
693
def get_extra_interrepo_test_combinations():
694
from ...bzr.groupcompress_repo import RepositoryFormat2a
695
from . import interrepo
697
(interrepo.InterLocalGitNonGitRepository, GitRepositoryFormat(), RepositoryFormat2a()),
698
(interrepo.InterLocalGitLocalGitRepository, GitRepositoryFormat(), GitRepositoryFormat()),
699
(interrepo.InterToLocalGitRepository, RepositoryFormat2a(), GitRepositoryFormat()),