31
39
from bzrlib.plugins.git.commit import (
42
from bzrlib.plugins.git.errors import (
45
from bzrlib.plugins.git.filegraph import (
46
GitFileLastChangeScanner,
47
GitFileParentProvider,
34
49
from bzrlib.plugins.git.mapping import (
39
54
from bzrlib.plugins.git.tree import (
42
from bzrlib.plugins.git.versionedfiles 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.checked_rev_cnt = 0
99
def check(self, callback_refs=None, check_repo=True):
100
if callback_refs is None:
102
self.repository.lock_read()
103
self.repository.unlock()
105
def report_results(self, verbose):
109
_optimisers_loaded = False
111
def lazy_load_optimisers():
112
global _optimisers_loaded
113
if _optimisers_loaded:
115
from bzrlib.plugins.git import fetch, push
116
for optimiser in [fetch.InterRemoteGitNonGitRepository,
117
fetch.InterLocalGitNonGitRepository,
118
fetch.InterGitGitRepository,
119
push.InterToLocalGitRepository,
120
push.InterToRemoteGitRepository]:
121
repository.InterRepository.register_optimiser(optimiser)
122
_optimisers_loaded = True
53
125
class GitRepository(ForeignRepository):
54
126
"""An adapter to git repositories for bzr."""
56
128
_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)
129
vcs = foreign_vcs_git
132
def __init__(self, gitdir):
133
if bzrlib_version >= (2, 5):
136
class DummyControlFiles(object):
138
self._transport = gitdir.root_transport
139
control_files = DummyControlFiles()
140
self._transport = gitdir.root_transport
141
super(GitRepository, self).__init__(GitRepositoryFormat(),
142
gitdir, control_files)
143
self.base = gitdir.root_transport.base
144
lazy_load_optimisers()
145
self._lock_mode = None
148
def add_fallback_repository(self, basis_url):
149
raise errors.UnstackableRepositoryFormat(self._format,
150
self.control_transport.base)
71
152
def is_shared(self):
155
def get_physical_lock_status(self):
158
def lock_write(self):
159
"""See Branch.lock_write()."""
161
assert self._lock_mode == 'w'
162
self._lock_count += 1
164
self._lock_mode = 'w'
166
return GitRepositoryLock(self)
168
def break_lock(self):
169
raise NotImplementedError(self.break_lock)
171
def dont_leave_lock_in_place(self):
172
raise NotImplementedError(self.dont_leave_lock_in_place)
174
def leave_lock_in_place(self):
175
raise NotImplementedError(self.leave_lock_in_place)
179
assert self._lock_mode in ('r', 'w')
180
self._lock_count += 1
182
self._lock_mode = 'r'
186
@only_raises(errors.LockNotHeld, errors.LockBroken)
188
if self._lock_count == 0:
189
raise errors.LockNotHeld(self)
190
if self._lock_count == 1 and self._lock_mode == 'w':
191
if self._write_group is not None:
192
self.abort_write_group()
193
self._lock_count -= 1
194
self._lock_mode = None
195
raise errors.BzrError(
196
'Must end write groups before releasing write locks.')
197
self._lock_count -= 1
198
if self._lock_count == 0:
199
self._lock_mode = None
201
def is_write_locked(self):
202
return (self._lock_mode == 'w')
205
return (self._lock_mode is not None)
207
def get_transaction(self):
208
"""See Repository.get_transaction()."""
209
if self._write_group is None:
210
return transactions.PassThroughTransaction()
212
return self._write_group
214
def reconcile(self, other=None, thorough=False):
215
"""Reconcile this repository."""
216
reconciler = RepoReconciler(self, thorough=thorough)
217
reconciler.reconcile()
74
220
def supports_rich_root(self):
77
def _warn_if_deprecated(self, branch=None):
223
def _warn_if_deprecated(self, branch=None): # for bzr < 2.4
78
224
# This class isn't deprecated
82
228
return default_mapping
84
230
def make_working_trees(self):
85
return not self._git.bare
231
return not self._git.get_config().get_boolean(("core", ), "bare")
87
233
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)
236
def add_signature_text(self, revid, signature):
237
raise errors.UnsupportedOperation(self.add_signature_text, self)
239
def sign_revision(self, revision_id, gpg_strategy):
240
raise errors.UnsupportedOperation(self.add_signature_text, self)
243
class GitRepositoryLock(object):
244
"""Subversion lock."""
246
def __init__(self, repository):
247
self.repository_token = None
248
self.repository = repository
251
self.repository.unlock()
95
254
class LocalGitRepository(GitRepository):
96
255
"""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
257
def __init__(self, gitdir):
258
GitRepository.__init__(self, gitdir)
101
259
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)
260
self._file_change_scanner = GitFileLastChangeScanner(self)
262
def get_commit_builder(self, branch, parents, config, timestamp=None,
263
timezone=None, committer=None, revprops=None,
264
revision_id=None, lossy=False):
265
"""Obtain a CommitBuilder for this repository.
267
:param branch: Branch to commit to.
268
:param parents: Revision ids of the parents of the new revision.
269
:param config: Configuration to use.
270
:param timestamp: Optional timestamp recorded for commit.
271
:param timezone: Optional timezone for timestamp.
272
:param committer: Optional committer to set for commit.
273
:param revprops: Optional dictionary of revision properties.
274
:param revision_id: Optional revision id.
275
:param lossy: Whether to discard data that can not be natively
276
represented, when pushing to a foreign VCS
278
self.start_write_group()
279
return GitCommitBuilder(self, parents, config,
280
timestamp, timezone, committer, revprops, revision_id,
283
def get_file_graph(self):
284
return _mod_graph.Graph(GitFileParentProvider(
285
self._file_change_scanner))
287
def iter_files_bytes(self, desired_files):
288
"""Iterate through file versions.
290
Files will not necessarily be returned in the order they occur in
291
desired_files. No specific order is guaranteed.
293
Yields pairs of identifier, bytes_iterator. identifier is an opaque
294
value supplied by the caller as part of desired_files. It should
295
uniquely identify the file version in the caller's context. (Examples:
296
an index number or a TreeTransform trans_id.)
298
bytes_iterator is an iterable of bytestrings for the file. The
299
kind of iterable and length of the bytestrings are unspecified, but for
300
this implementation, it is a list of bytes produced by
301
VersionedFile.get_record_stream().
303
:param desired_files: a list of (file_id, revision_id, identifier)
307
for (file_id, revision_id, identifier) in desired_files:
308
per_revision.setdefault(revision_id, []).append(
309
(file_id, identifier))
310
for revid, files in per_revision.iteritems():
311
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
313
commit = self._git.object_store[commit_id]
315
raise errors.RevisionNotPresent(revid, self)
316
root_tree = commit.tree
317
for fileid, identifier in files:
318
path = mapping.parse_file_id(fileid)
320
obj = tree_lookup_path(
321
self._git.object_store.__getitem__, root_tree, path)
322
if isinstance(obj, tuple):
323
(mode, item_id) = obj
324
obj = self._git.object_store[item_id]
326
raise errors.RevisionNotPresent((fileid, revid), self)
328
if obj.type_name == "tree":
329
yield (identifier, [])
330
elif obj.type_name == "blob":
331
yield (identifier, obj.chunked)
333
raise AssertionError("file text resolved to %r" % obj)
335
def gather_stats(self, revid=None, committers=None):
336
"""See Repository.gather_stats()."""
337
result = super(LocalGitRepository, self).gather_stats(revid, committers)
339
for sha in self._git.object_store:
340
o = self._git.object_store[sha]
341
if o.type_name == "commit":
343
result['revisions'] = len(revs)
107
346
def _iter_revision_ids(self):
108
347
mapping = self.get_mapping()
111
350
if not isinstance(o, Commit):
113
352
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
self.lookup_foreign_revision_id)
353
mapping.revision_id_foreign_to_bzr)
115
354
yield o.id, rev.revision_id, roundtrip_revid
117
356
def all_revision_ids(self):
119
358
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
121
359
if roundtrip_revid:
122
360
ret.add(roundtrip_revid)
125
def get_parent_map(self, revids):
365
def _get_parents(self, revid, no_alternates=False):
366
if type(revid) != str:
369
(hexsha, mapping) = self.lookup_bzr_revision_id(revid)
370
except errors.NoSuchRevision:
372
# FIXME: Honor no_alternates setting
374
commit = self._git.object_store[hexsha]
378
self.lookup_foreign_revision_id(p, mapping)
379
for p in commit.parents]
381
def _get_parent_map_no_fallbacks(self, revids):
382
return self.get_parent_map(revids, no_alternates=True)
384
def get_parent_map(self, revids, no_alternates=False):
127
386
for revision_id in revids:
128
assert isinstance(revision_id, str)
387
parents = self._get_parents(revision_id, no_alternates=no_alternates)
129
388
if revision_id == revision.NULL_REVISION:
130
389
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]
393
if len(parents) == 0:
394
parents = [revision.NULL_REVISION]
395
parent_map[revision_id] = tuple(parents)
140
396
return parent_map
142
def get_ancestry(self, revision_id, topo_sorted=True):
143
"""See Repository.get_ancestry().
398
def get_known_graph_ancestry(self, revision_ids):
399
"""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
401
pending = set(revision_ids)
405
for revid in pending:
406
if revid == revision.NULL_REVISION:
408
parents = self._get_parents(revid)
409
if parents is not None:
410
this_parent_map[revid] = parents
411
parent_map.update(this_parent_map)
413
map(pending.update, this_parent_map.itervalues())
414
pending = pending.difference(parent_map)
415
return _mod_graph.KnownGraph(parent_map)
155
417
def get_signature_text(self, revision_id):
156
418
raise errors.NoSuchRevision(self, revision_id)
420
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
421
result = GitCheck(self, check_repo=check_repo)
422
result.check(callback_refs)
158
425
def pack(self, hint=None, clean_obsolete_packs=False):
159
426
self._git.object_store.pack_loose_objects()
161
428
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
162
429
"""Lookup a revision id.
431
:param foreign_revid: Foreign revision id to look up
432
:param mapping: Mapping to use (use default mapping if not specified)
433
:raise KeyError: If foreign revision was not found
434
:return: bzr revision id
165
436
assert type(foreign_revid) is str
166
437
if mapping is None:
167
438
mapping = self.get_mapping()
168
from dulwich.protocol import (
171
439
if foreign_revid == ZERO_SHA:
172
440
return revision.NULL_REVISION
173
commit = self._git[foreign_revid]
441
commit = self._git.object_store.peel_sha(foreign_revid)
442
if not isinstance(commit, Commit):
443
raise NotCommitError(commit.id)
174
444
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
445
mapping.revision_id_foreign_to_bzr)
176
446
# FIXME: check testament before doing this?
177
447
if roundtrip_revid:
178
448
return roundtrip_revid
225
513
return (git_commit_id in self._git)
227
515
def has_revisions(self, revision_ids):
516
"""See Repository.has_revisions."""
228
517
return set(filter(self.has_revision, revision_ids))
230
519
def get_revisions(self, revids):
520
"""See Repository.get_revisions."""
231
521
return [self.get_revision(r) for r in revids]
233
523
def revision_trees(self, revids):
524
"""See Repository.revision_trees."""
234
525
for revid in revids:
235
526
yield self.revision_tree(revid)
237
528
def revision_tree(self, revision_id):
529
"""See Repository.revision_tree."""
238
530
revision_id = revision.ensure_null(revision_id)
239
531
if revision_id == revision.NULL_REVISION:
240
532
inv = inventory.Inventory(root_id=None)
241
533
inv.revision_id = revision_id
242
return revisiontree.RevisionTree(self, inv, revision_id)
534
return InventoryRevisionTree(self, inv, revision_id)
243
535
return GitRevisionTree(self, revision_id)
245
537
def get_inventory(self, revision_id):
246
assert revision_id != None
247
return self.revision_tree(revision_id).inventory
538
raise NotImplementedError(self.get_inventory)
249
540
def set_make_working_trees(self, trees):
542
self._git.get_config().set(("core", ), "bare", "false")
544
self._git.get_config().set(("core", ), "bare", "true")
252
546
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
254
548
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):
270
551
class GitRepositoryFormat(repository.RepositoryFormat):
271
552
"""Git repository format."""
554
supports_versioned_directories = False
273
555
supports_tree_reference = False
274
556
rich_root_data = True
557
supports_leaving_lock = False
559
supports_funky_characters = True
560
supports_external_lookups = True
561
supports_full_versioned_files = False
562
supports_revision_signatures = False
563
supports_nesting_repositories = False
564
revision_graph_can_have_wrong_parents = False
565
supports_unreferenced_revisions = True
568
def _matchingbzrdir(self):
569
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
570
return LocalGitControlDirFormat()
276
572
def get_format_description(self):
277
573
return "Git Repository"
279
def initialize(self, url, shared=False, _internal=False):
280
raise errors.UninitializableFormat(self)
575
def initialize(self, controldir, shared=False, _internal=False):
576
from bzrlib.plugins.git.dir import GitDir
577
if not isinstance(controldir, GitDir):
578
raise errors.UninitializableFormat(self)
579
return controldir.open_repository()
282
581
def check_conversion_target(self, target_repo_format):
283
582
return target_repo_format.rich_root_data