31
36
from bzrlib.plugins.git.commit import (
39
from bzrlib.plugins.git.errors import (
42
from bzrlib.plugins.git.filegraph import (
43
GitFileLastChangeScanner,
44
GitFileParentProvider,
34
46
from bzrlib.plugins.git.mapping import (
39
51
from bzrlib.plugins.git.tree import (
42
from bzrlib.plugins.git.versionedfiles import (
48
56
from dulwich.objects import (
60
from dulwich.object_store import (
65
class RepoReconciler(object):
66
"""Reconciler that reconciles a repository.
70
def __init__(self, repo, other=None, thorough=False):
71
"""Construct a RepoReconciler.
73
:param thorough: perform a thorough check which may take longer but
74
will correct non-data loss issues such as incorrect
80
"""Perform reconciliation.
82
After reconciliation the following attributes document found issues:
83
inconsistent_parents: The number of revisions in the repository whose
84
ancestry was being reported incorrectly.
85
garbage_inventories: The number of inventory objects without revisions
86
that were garbage collected.
90
class GitCheck(check.Check):
92
def __init__(self, repository, check_repo=True):
93
self.repository = repository
94
self.checked_rev_cnt = 0
96
def check(self, callback_refs=None, check_repo=True):
97
if callback_refs is None:
99
self.repository.lock_read()
100
self.repository.unlock()
102
def report_results(self, verbose):
106
_optimisers_loaded = False
108
def lazy_load_optimisers():
109
global _optimisers_loaded
110
if _optimisers_loaded:
112
from bzrlib.plugins.git import fetch, push
113
for optimiser in [fetch.InterRemoteGitNonGitRepository,
114
fetch.InterLocalGitNonGitRepository,
115
fetch.InterGitGitRepository,
116
push.InterToLocalGitRepository,
117
push.InterToRemoteGitRepository]:
118
repository.InterRepository.register_optimiser(optimiser)
119
_optimisers_loaded = True
53
122
class GitRepository(ForeignRepository):
54
123
"""An adapter to git repositories for bzr."""
56
125
_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)
126
vcs = foreign_vcs_git
129
def __init__(self, gitdir):
130
if bzrlib_version >= (2, 5):
133
class DummyControlFiles(object):
135
self._transport = gitdir.root_transport
136
control_files = DummyControlFiles()
137
self._transport = gitdir.root_transport
138
super(GitRepository, self).__init__(GitRepositoryFormat(),
139
gitdir, control_files)
140
self.base = gitdir.root_transport.base
141
lazy_load_optimisers()
142
self._lock_mode = None
145
def add_fallback_repository(self, basis_url):
146
raise errors.UnstackableRepositoryFormat(self._format,
147
self.control_transport.base)
71
149
def is_shared(self):
152
def get_physical_lock_status(self):
155
def lock_write(self):
156
"""See Branch.lock_write()."""
158
assert self._lock_mode == 'w'
159
self._lock_count += 1
161
self._lock_mode = 'w'
163
return GitRepositoryLock(self)
165
def break_lock(self):
166
raise NotImplementedError(self.break_lock)
168
def dont_leave_lock_in_place(self):
169
raise NotImplementedError(self.dont_leave_lock_in_place)
171
def leave_lock_in_place(self):
172
raise NotImplementedError(self.leave_lock_in_place)
176
assert self._lock_mode in ('r', 'w')
177
self._lock_count += 1
179
self._lock_mode = 'r'
183
@only_raises(errors.LockNotHeld, errors.LockBroken)
185
if self._lock_count == 0:
186
raise errors.LockNotHeld(self)
187
if self._lock_count == 1 and self._lock_mode == 'w':
188
if self._write_group is not None:
189
self.abort_write_group()
190
self._lock_count -= 1
191
self._lock_mode = None
192
raise errors.BzrError(
193
'Must end write groups before releasing write locks.')
194
self._lock_count -= 1
195
if self._lock_count == 0:
196
self._lock_mode = None
198
def is_write_locked(self):
199
return (self._lock_mode == 'w')
202
return (self._lock_mode is not None)
204
def get_transaction(self):
205
"""See Repository.get_transaction()."""
206
if self._write_group is None:
207
return transactions.PassThroughTransaction()
209
return self._write_group
211
def reconcile(self, other=None, thorough=False):
212
"""Reconcile this repository."""
213
reconciler = RepoReconciler(self, thorough=thorough)
214
reconciler.reconcile()
74
217
def supports_rich_root(self):
77
def _warn_if_deprecated(self, branch=None):
78
# This class isn't deprecated
81
220
def get_mapping(self):
82
221
return default_mapping
84
223
def make_working_trees(self):
85
return not self._git.bare
224
return not self._git.get_config().get_boolean(("core", ), "bare")
87
226
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)
229
def add_signature_text(self, revid, signature):
230
raise errors.UnsupportedOperation(self.add_signature_text, self)
232
def sign_revision(self, revision_id, gpg_strategy):
233
raise errors.UnsupportedOperation(self.add_signature_text, self)
236
class GitRepositoryLock(object):
237
"""Subversion lock."""
239
def __init__(self, repository):
240
self.repository_token = None
241
self.repository = repository
244
self.repository.unlock()
95
247
class LocalGitRepository(GitRepository):
96
248
"""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
250
def __init__(self, gitdir):
251
GitRepository.__init__(self, gitdir)
101
252
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)
253
self._file_change_scanner = GitFileLastChangeScanner(self)
255
def get_commit_builder(self, branch, parents, config, timestamp=None,
256
timezone=None, committer=None, revprops=None,
257
revision_id=None, lossy=False):
258
"""Obtain a CommitBuilder for this repository.
260
:param branch: Branch to commit to.
261
:param parents: Revision ids of the parents of the new revision.
262
:param config: Configuration to use.
263
:param timestamp: Optional timestamp recorded for commit.
264
:param timezone: Optional timezone for timestamp.
265
:param committer: Optional committer to set for commit.
266
:param revprops: Optional dictionary of revision properties.
267
:param revision_id: Optional revision id.
268
:param lossy: Whether to discard data that can not be natively
269
represented, when pushing to a foreign VCS
271
self.start_write_group()
272
return GitCommitBuilder(self, parents, config,
273
timestamp, timezone, committer, revprops, revision_id,
276
def get_file_graph(self):
277
return _mod_graph.Graph(GitFileParentProvider(
278
self._file_change_scanner))
280
def iter_files_bytes(self, desired_files):
281
"""Iterate through file versions.
283
Files will not necessarily be returned in the order they occur in
284
desired_files. No specific order is guaranteed.
286
Yields pairs of identifier, bytes_iterator. identifier is an opaque
287
value supplied by the caller as part of desired_files. It should
288
uniquely identify the file version in the caller's context. (Examples:
289
an index number or a TreeTransform trans_id.)
291
bytes_iterator is an iterable of bytestrings for the file. The
292
kind of iterable and length of the bytestrings are unspecified, but for
293
this implementation, it is a list of bytes produced by
294
VersionedFile.get_record_stream().
296
:param desired_files: a list of (file_id, revision_id, identifier)
300
for (file_id, revision_id, identifier) in desired_files:
301
per_revision.setdefault(revision_id, []).append(
302
(file_id, identifier))
303
for revid, files in per_revision.iteritems():
304
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
306
commit = self._git.object_store[commit_id]
308
raise errors.RevisionNotPresent(revid, self)
309
root_tree = commit.tree
310
for fileid, identifier in files:
311
path = mapping.parse_file_id(fileid)
313
obj = tree_lookup_path(
314
self._git.object_store.__getitem__, root_tree, path)
315
if isinstance(obj, tuple):
316
(mode, item_id) = obj
317
obj = self._git.object_store[item_id]
319
raise errors.RevisionNotPresent((fileid, revid), self)
321
if obj.type_name == "tree":
322
yield (identifier, [])
323
elif obj.type_name == "blob":
324
yield (identifier, obj.chunked)
326
raise AssertionError("file text resolved to %r" % obj)
328
def gather_stats(self, revid=None, committers=None):
329
"""See Repository.gather_stats()."""
330
result = super(LocalGitRepository, self).gather_stats(revid, committers)
332
for sha in self._git.object_store:
333
o = self._git.object_store[sha]
334
if o.type_name == "commit":
336
result['revisions'] = len(revs)
107
339
def _iter_revision_ids(self):
108
340
mapping = self.get_mapping()
111
343
if not isinstance(o, Commit):
113
345
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
self.lookup_foreign_revision_id)
346
mapping.revision_id_foreign_to_bzr)
115
347
yield o.id, rev.revision_id, roundtrip_revid
117
349
def all_revision_ids(self):
119
351
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
121
352
if roundtrip_revid:
122
353
ret.add(roundtrip_revid)
125
def get_parent_map(self, revids):
358
def _get_parents(self, revid, no_alternates=False):
359
if type(revid) != str:
362
(hexsha, mapping) = self.lookup_bzr_revision_id(revid)
363
except errors.NoSuchRevision:
365
# FIXME: Honor no_alternates setting
367
commit = self._git.object_store[hexsha]
371
self.lookup_foreign_revision_id(p, mapping)
372
for p in commit.parents]
374
def _get_parent_map_no_fallbacks(self, revids):
375
return self.get_parent_map(revids, no_alternates=True)
377
def get_parent_map(self, revids, no_alternates=False):
127
379
for revision_id in revids:
128
assert isinstance(revision_id, str)
380
parents = self._get_parents(revision_id, no_alternates=no_alternates)
129
381
if revision_id == revision.NULL_REVISION:
130
382
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]
386
if len(parents) == 0:
387
parents = [revision.NULL_REVISION]
388
parent_map[revision_id] = tuple(parents)
140
389
return parent_map
142
def get_ancestry(self, revision_id, topo_sorted=True):
143
"""See Repository.get_ancestry().
391
def get_known_graph_ancestry(self, revision_ids):
392
"""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
394
pending = set(revision_ids)
398
for revid in pending:
399
if revid == revision.NULL_REVISION:
401
parents = self._get_parents(revid)
402
if parents is not None:
403
this_parent_map[revid] = parents
404
parent_map.update(this_parent_map)
406
map(pending.update, this_parent_map.itervalues())
407
pending = pending.difference(parent_map)
408
return _mod_graph.KnownGraph(parent_map)
155
410
def get_signature_text(self, revision_id):
156
411
raise errors.NoSuchRevision(self, revision_id)
413
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
414
result = GitCheck(self, check_repo=check_repo)
415
result.check(callback_refs)
158
418
def pack(self, hint=None, clean_obsolete_packs=False):
159
419
self._git.object_store.pack_loose_objects()
161
421
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
162
422
"""Lookup a revision id.
424
:param foreign_revid: Foreign revision id to look up
425
:param mapping: Mapping to use (use default mapping if not specified)
426
:raise KeyError: If foreign revision was not found
427
:return: bzr revision id
165
429
assert type(foreign_revid) is str
166
430
if mapping is None:
167
431
mapping = self.get_mapping()
168
from dulwich.protocol import (
171
432
if foreign_revid == ZERO_SHA:
172
433
return revision.NULL_REVISION
173
commit = self._git[foreign_revid]
434
commit = self._git.object_store.peel_sha(foreign_revid)
435
if not isinstance(commit, Commit):
436
raise NotCommitError(commit.id)
174
437
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
438
mapping.revision_id_foreign_to_bzr)
176
439
# FIXME: check testament before doing this?
177
440
if roundtrip_revid:
178
441
return roundtrip_revid
225
506
return (git_commit_id in self._git)
227
508
def has_revisions(self, revision_ids):
509
"""See Repository.has_revisions."""
228
510
return set(filter(self.has_revision, revision_ids))
230
512
def get_revisions(self, revids):
513
"""See Repository.get_revisions."""
231
514
return [self.get_revision(r) for r in revids]
233
516
def revision_trees(self, revids):
517
"""See Repository.revision_trees."""
234
518
for revid in revids:
235
519
yield self.revision_tree(revid)
237
521
def revision_tree(self, revision_id):
522
"""See Repository.revision_tree."""
238
523
revision_id = revision.ensure_null(revision_id)
239
524
if revision_id == revision.NULL_REVISION:
240
525
inv = inventory.Inventory(root_id=None)
241
526
inv.revision_id = revision_id
242
return revisiontree.RevisionTree(self, inv, revision_id)
527
return InventoryRevisionTree(self, inv, revision_id)
243
528
return GitRevisionTree(self, revision_id)
245
530
def get_inventory(self, revision_id):
246
assert revision_id != None
247
return self.revision_tree(revision_id).inventory
531
raise NotImplementedError(self.get_inventory)
249
533
def set_make_working_trees(self, trees):
535
self._git.get_config().set(("core", ), "bare", "false")
537
self._git.get_config().set(("core", ), "bare", "true")
252
539
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
254
541
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
544
class GitRepositoryFormat(repository.RepositoryFormat):
271
545
"""Git repository format."""
547
supports_versioned_directories = False
273
548
supports_tree_reference = False
274
549
rich_root_data = True
550
supports_leaving_lock = False
552
supports_funky_characters = True
553
supports_external_lookups = False
554
supports_full_versioned_files = False
555
supports_revision_signatures = False
556
supports_nesting_repositories = False
557
revision_graph_can_have_wrong_parents = False
558
supports_unreferenced_revisions = True
561
def _matchingbzrdir(self):
562
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
563
return LocalGitControlDirFormat()
276
565
def get_format_description(self):
277
566
return "Git Repository"
279
def initialize(self, url, shared=False, _internal=False):
280
raise errors.UninitializableFormat(self)
568
def initialize(self, controldir, shared=False, _internal=False):
569
from bzrlib.plugins.git.dir import GitDir
570
if not isinstance(controldir, GitDir):
571
raise errors.UninitializableFormat(self)
572
return controldir.open_repository()
282
574
def check_conversion_target(self, target_repo_format):
283
575
return target_repo_format.rich_root_data