14
14
# You should have received a copy of the GNU General Public License
15
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
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,
27
from bzrlib.foreign import (
34
from ...decorators import only_raises
35
from ...foreign import (
31
from bzrlib.plugins.git.commit import (
34
from bzrlib.plugins.git.mapping import (
42
from .filegraph import (
43
GitFileLastChangeScanner,
44
GitFileParentProvider,
46
from .mapping import (
39
from bzrlib.plugins.git.tree import (
42
from bzrlib.plugins.git.versionedfiles import (
56
from dulwich.errors import (
48
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
53
148
class GitRepository(ForeignRepository):
54
149
"""An adapter to git repositories for bzr."""
56
151
_serializer = None
57
_commit_builder_class = GitCommitBuilder
60
def __init__(self, gitdir, lockfiles):
61
ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
63
from bzrlib.plugins.git import fetch, push
64
for optimiser in [fetch.InterRemoteGitNonGitRepository,
65
fetch.InterLocalGitNonGitRepository,
66
fetch.InterGitGitRepository,
67
push.InterToLocalGitRepository,
68
push.InterToRemoteGitRepository]:
69
repository.InterRepository.register_optimiser(optimiser)
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)
71
168
def is_shared(self):
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()
74
243
def supports_rich_root(self):
77
def _warn_if_deprecated(self, branch=None):
78
# This class isn't deprecated
81
246
def get_mapping(self):
82
247
return default_mapping
84
249
def make_working_trees(self):
85
return not self._git.bare
250
return not self._git.get_config().get_boolean(("core", ), "bare")
87
252
def revision_graph_can_have_wrong_parents(self):
90
def dfetch(self, source, stop_revision):
91
interrepo = repository.InterRepository.get(source, self)
92
return interrepo.dfetch(stop_revision)
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)
95
262
class LocalGitRepository(GitRepository):
96
263
"""Git repository on the file system."""
98
def __init__(self, gitdir, lockfiles):
99
GitRepository.__init__(self, gitdir, lockfiles)
100
self.base = gitdir.root_transport.base
265
def __init__(self, gitdir):
266
GitRepository.__init__(self, gitdir)
101
267
self._git = gitdir._git
102
self.signatures = None
103
self.revisions = GitRevisions(self, self._git.object_store)
104
self.inventories = None
105
self.texts = GitTexts(self)
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)
107
362
def _iter_revision_ids(self):
108
363
mapping = self.get_mapping()
111
366
if not isinstance(o, Commit):
113
368
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
self.lookup_foreign_revision_id)
369
mapping.revision_id_foreign_to_bzr)
115
370
yield o.id, rev.revision_id, roundtrip_revid
117
372
def all_revision_ids(self):
119
374
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
121
375
if roundtrip_revid:
122
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))
125
def get_parent_map(self, revids):
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):
127
406
for revision_id in revids:
128
assert isinstance(revision_id, str)
129
if revision_id == revision.NULL_REVISION:
407
parents = self._get_parents(revision_id, no_alternates=no_alternates)
408
if revision_id == _mod_revision.NULL_REVISION:
130
409
parent_map[revision_id] = ()
132
hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
134
commit = self._git[hexsha]
137
parent_map[revision_id] = [
138
self.lookup_foreign_revision_id(p, mapping)
139
for p in commit.parents]
413
if len(parents) == 0:
414
parents = [_mod_revision.NULL_REVISION]
415
parent_map[revision_id] = tuple(parents)
140
416
return parent_map
142
def get_ancestry(self, revision_id, topo_sorted=True):
143
"""See Repository.get_ancestry().
418
def get_known_graph_ancestry(self, revision_ids):
419
"""Return the known graph for a set of revision ids and their ancestors.
145
if revision_id is None:
146
return [None, revision.NULL_REVISION] + self._all_revision_ids()
147
assert isinstance(revision_id, str)
149
graph = self.get_graph()
150
for rev, parents in graph.iter_ancestry([revision_id]):
153
return [None] + ancestry
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)
155
437
def get_signature_text(self, revision_id):
156
raise errors.NoSuchRevision(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)
158
452
def pack(self, hint=None, clean_obsolete_packs=False):
159
453
self._git.object_store.pack_loose_objects()
225
577
return (git_commit_id in self._git)
227
579
def has_revisions(self, revision_ids):
580
"""See Repository.has_revisions."""
228
581
return set(filter(self.has_revision, revision_ids))
230
def get_revisions(self, revids):
231
return [self.get_revision(r) for r in revids]
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:
233
592
def revision_trees(self, revids):
593
"""See Repository.revision_trees."""
234
594
for revid in revids:
235
595
yield self.revision_tree(revid)
237
597
def revision_tree(self, revision_id):
238
revision_id = revision.ensure_null(revision_id)
239
if revision_id == revision.NULL_REVISION:
240
inv = inventory.Inventory(root_id=None)
241
inv.revision_id = revision_id
242
return revisiontree.RevisionTree(self, inv, revision_id)
598
"""See Repository.revision_tree."""
599
if revision_id is None:
600
raise ValueError('invalid revision id %s' % revision_id)
243
601
return GitRevisionTree(self, revision_id)
245
def get_inventory(self, revision_id):
246
assert revision_id != None
247
return self.revision_tree(revision_id).inventory
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)
249
636
def set_make_working_trees(self, trees):
637
raise errors.UnsupportedOperation(self.set_make_working_trees, self)
252
639
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
254
return self._git.fetch_objects(determine_wants, graph_walker, progress)
256
def _get_versioned_file_checker(self, text_key_references=None,
258
return GitVersionedFileChecker(self,
259
text_key_references=text_key_references, ancestors=ancestors)
262
class GitVersionedFileChecker(repository._VersionedFileChecker):
266
def _check_file_version_parents(self, texts, progress_bar):
640
progress=None, limit=None):
641
return self._git.fetch_objects(determine_wants, graph_walker, progress,
270
645
class GitRepositoryFormat(repository.RepositoryFormat):
271
646
"""Git repository format."""
273
supports_tree_reference = False
648
supports_versioned_directories = False
649
supports_tree_reference = True
274
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()
276
671
def get_format_description(self):
277
672
return "Git Repository"
279
def initialize(self, url, shared=False, _internal=False):
280
raise errors.UninitializableFormat(self)
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()
282
680
def check_conversion_target(self, target_repo_format):
283
681
return target_repo_format.rich_root_data
285
683
def get_foreign_tests_repository_factory(self):
286
from bzrlib.plugins.git.tests.test_repository import (
684
from .tests.test_repository import (
287
685
ForeignTestsRepositoryFactory,
289
687
return ForeignTestsRepositoryFactory()