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
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)
107
335
def _iter_revision_ids(self):
108
336
mapping = self.get_mapping()
111
339
if not isinstance(o, Commit):
113
341
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
self.lookup_foreign_revision_id)
342
mapping.revision_id_foreign_to_bzr)
115
343
yield o.id, rev.revision_id, roundtrip_revid
117
345
def all_revision_ids(self):
119
347
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
121
348
if roundtrip_revid:
122
349
ret.add(roundtrip_revid)
354
def _get_parents(self, revid):
355
if type(revid) != str:
358
(hexsha, mapping) = self.lookup_bzr_revision_id(revid)
359
except errors.NoSuchRevision:
362
commit = self._git[hexsha]
366
self.lookup_foreign_revision_id(p, mapping)
367
for p in commit.parents]
125
369
def get_parent_map(self, revids):
127
371
for revision_id in revids:
128
assert isinstance(revision_id, str)
372
parents = self._get_parents(revision_id)
129
373
if revision_id == revision.NULL_REVISION:
130
374
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]
378
if len(parents) == 0:
379
parents = [revision.NULL_REVISION]
380
parent_map[revision_id] = tuple(parents)
140
381
return parent_map
142
def get_ancestry(self, revision_id, topo_sorted=True):
143
"""See Repository.get_ancestry().
383
def get_known_graph_ancestry(self, revision_ids):
384
"""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
386
pending = set(revision_ids)
390
for revid in pending:
391
if revid == revision.NULL_REVISION:
393
parents = self._get_parents(revid)
394
if parents is not None:
395
this_parent_map[revid] = parents
396
parent_map.update(this_parent_map)
398
map(pending.update, this_parent_map.itervalues())
399
pending = pending.difference(parent_map)
400
return _mod_graph.KnownGraph(parent_map)
155
402
def get_signature_text(self, revision_id):
156
403
raise errors.NoSuchRevision(self, revision_id)
405
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
406
result = GitCheck(self, check_repo=check_repo)
407
result.check(callback_refs)
158
410
def pack(self, hint=None, clean_obsolete_packs=False):
159
411
self._git.object_store.pack_loose_objects()
161
413
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
162
414
"""Lookup a revision id.
416
:param foreign_revid: Foreign revision id to look up
417
:param mapping: Mapping to use (use default mapping if not specified)
418
:raise KeyError: If foreign revision was not found
419
:return: bzr revision id
165
421
assert type(foreign_revid) is str
166
422
if mapping is None:
167
423
mapping = self.get_mapping()
168
from dulwich.protocol import (
171
424
if foreign_revid == ZERO_SHA:
172
425
return revision.NULL_REVISION
173
commit = self._git[foreign_revid]
426
commit = self._git.object_store.peel_sha(foreign_revid)
427
if not isinstance(commit, Commit):
428
raise NotCommitError(commit.id)
174
429
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
430
mapping.revision_id_foreign_to_bzr)
176
431
# FIXME: check testament before doing this?
177
432
if roundtrip_revid:
178
433
return roundtrip_revid
225
498
return (git_commit_id in self._git)
227
500
def has_revisions(self, revision_ids):
501
"""See Repository.has_revisions."""
228
502
return set(filter(self.has_revision, revision_ids))
230
504
def get_revisions(self, revids):
505
"""See Repository.get_revisions."""
231
506
return [self.get_revision(r) for r in revids]
233
508
def revision_trees(self, revids):
509
"""See Repository.revision_trees."""
234
510
for revid in revids:
235
511
yield self.revision_tree(revid)
237
513
def revision_tree(self, revision_id):
514
"""See Repository.revision_tree."""
238
515
revision_id = revision.ensure_null(revision_id)
239
516
if revision_id == revision.NULL_REVISION:
240
517
inv = inventory.Inventory(root_id=None)
241
518
inv.revision_id = revision_id
242
return revisiontree.RevisionTree(self, inv, revision_id)
519
return InventoryRevisionTree(self, inv, revision_id)
243
520
return GitRevisionTree(self, revision_id)
245
522
def get_inventory(self, revision_id):
246
assert revision_id != None
247
return self.revision_tree(revision_id).inventory
523
raise NotImplementedError(self.get_inventory)
249
525
def set_make_working_trees(self, trees):
526
raise errors.UnsupportedOperation(self.set_make_working_trees, self)
527
# TODO: Set bare= in the configuration bug=777065
252
529
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
254
531
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
534
class GitRepositoryFormat(repository.RepositoryFormat):
271
535
"""Git repository format."""
537
supports_versioned_directories = False
273
538
supports_tree_reference = False
274
539
rich_root_data = True
540
supports_leaving_lock = False
542
supports_funky_characters = True
543
supports_external_lookups = False
544
supports_full_versioned_files = False
545
supports_revision_signatures = False
546
supports_nesting_repositories = False
547
revision_graph_can_have_wrong_parents = False
548
supports_unreferenced_revisions = True
551
def _matchingbzrdir(self):
552
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
553
return LocalGitControlDirFormat()
276
555
def get_format_description(self):
277
556
return "Git Repository"
279
def initialize(self, url, shared=False, _internal=False):
280
raise errors.UninitializableFormat(self)
558
def initialize(self, controldir, shared=False, _internal=False):
559
from bzrlib.plugins.git.dir import GitDir
560
if not isinstance(controldir, GitDir):
561
raise errors.UninitializableFormat(self)
562
return controldir.open_repository()
282
564
def check_conversion_target(self, target_repo_format):
283
565
return target_repo_format.rich_root_data