36
31
from bzrlib.plugins.git.commit import (
39
from bzrlib.plugins.git.filegraph import (
40
GitFileLastChangeScanner,
41
GitFileParentProvider,
43
34
from bzrlib.plugins.git.mapping import (
48
39
from bzrlib.plugins.git.tree import (
42
from bzrlib.plugins.git.versionedfiles import (
53
48
from dulwich.objects import (
58
from dulwich.object_store import (
63
class RepoReconciler(object):
64
"""Reconciler that reconciles a repository.
68
def __init__(self, repo, other=None, thorough=False):
69
"""Construct a RepoReconciler.
71
:param thorough: perform a thorough check which may take longer but
72
will correct non-data loss issues such as incorrect
78
"""Perform reconciliation.
80
After reconciliation the following attributes document found issues:
81
inconsistent_parents: The number of revisions in the repository whose
82
ancestry was being reported incorrectly.
83
garbage_inventories: The number of inventory objects without revisions
84
that were garbage collected.
88
class GitCheck(check.Check):
90
def __init__(self, repository, check_repo=True):
91
self.repository = repository
92
self.checked_rev_cnt = 0
94
def check(self, callback_refs=None, check_repo=True):
95
if callback_refs is None:
97
self.repository.lock_read()
98
self.repository.unlock()
100
def report_results(self, verbose):
104
53
class GitRepository(ForeignRepository):
105
54
"""An adapter to git repositories for bzr."""
107
56
_serializer = None
108
vcs = foreign_vcs_git
57
_commit_builder_class = GitCommitBuilder
111
60
def __init__(self, gitdir, lockfiles):
112
super(GitRepository, self).__init__(GitRepositoryFormat(),
61
ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
114
63
from bzrlib.plugins.git import fetch, push
115
64
for optimiser in [fetch.InterRemoteGitNonGitRepository,
116
65
fetch.InterLocalGitNonGitRepository,
119
68
push.InterToRemoteGitRepository]:
120
69
repository.InterRepository.register_optimiser(optimiser)
122
def add_fallback_repository(self, basis_url):
123
raise errors.UnstackableRepositoryFormat(self._format,
124
self.control_transport.base)
126
71
def is_shared(self):
129
def reconcile(self, other=None, thorough=False):
130
"""Reconcile this repository."""
131
reconciler = RepoReconciler(self, thorough=thorough)
132
reconciler.reconcile()
135
74
def supports_rich_root(self):
138
def _warn_if_deprecated(self, branch=None): # for bzr < 2.4
77
def _warn_if_deprecated(self, branch=None):
139
78
# This class isn't deprecated
163
99
GitRepository.__init__(self, gitdir, lockfiles)
164
100
self.base = gitdir.root_transport.base
165
101
self._git = gitdir._git
166
self._file_change_scanner = GitFileLastChangeScanner(self)
168
def get_commit_builder(self, branch, parents, config, timestamp=None,
169
timezone=None, committer=None, revprops=None,
170
revision_id=None, lossy=False):
171
"""Obtain a CommitBuilder for this repository.
173
:param branch: Branch to commit to.
174
:param parents: Revision ids of the parents of the new revision.
175
:param config: Configuration to use.
176
:param timestamp: Optional timestamp recorded for commit.
177
:param timezone: Optional timezone for timestamp.
178
:param committer: Optional committer to set for commit.
179
:param revprops: Optional dictionary of revision properties.
180
:param revision_id: Optional revision id.
181
:param lossy: Whether to discard data that can not be natively
182
represented, when pushing to a foreign VCS
184
self.start_write_group()
185
return GitCommitBuilder(self, parents, config,
186
timestamp, timezone, committer, revprops, revision_id,
189
def get_file_graph(self):
190
return _mod_graph.Graph(GitFileParentProvider(
191
self._file_change_scanner))
193
def iter_files_bytes(self, desired_files):
194
"""Iterate through file versions.
196
Files will not necessarily be returned in the order they occur in
197
desired_files. No specific order is guaranteed.
199
Yields pairs of identifier, bytes_iterator. identifier is an opaque
200
value supplied by the caller as part of desired_files. It should
201
uniquely identify the file version in the caller's context. (Examples:
202
an index number or a TreeTransform trans_id.)
204
bytes_iterator is an iterable of bytestrings for the file. The
205
kind of iterable and length of the bytestrings are unspecified, but for
206
this implementation, it is a list of bytes produced by
207
VersionedFile.get_record_stream().
209
:param desired_files: a list of (file_id, revision_id, identifier)
213
for (file_id, revision_id, identifier) in desired_files:
214
per_revision.setdefault(revision_id, []).append(
215
(file_id, identifier))
216
for revid, files in per_revision.iteritems():
217
(commit_id, mapping) = self.lookup_bzr_revision_id(revid)
219
commit = self._git.object_store[commit_id]
221
raise errors.RevisionNotPresent(revid, self)
222
root_tree = commit.tree
223
for fileid, identifier in files:
224
path = mapping.parse_file_id(fileid)
226
obj = tree_lookup_path(
227
self._git.object_store.__getitem__, root_tree, path)
228
if isinstance(obj, tuple):
229
(mode, item_id) = obj
230
obj = self._git.object_store[item_id]
232
raise errors.RevisionNotPresent((fileid, revid), self)
234
if obj.type_name == "tree":
235
yield (identifier, [])
236
elif obj.type_name == "blob":
237
yield (identifier, obj.chunked)
239
raise AssertionError("file text resolved to %r" % obj)
102
self.signatures = None
103
self.revisions = GitRevisions(self, self._git.object_store)
104
self.inventories = None
105
self.texts = GitTexts(self)
242
107
def _iter_revision_ids(self):
243
108
mapping = self.get_mapping()
246
111
if not isinstance(o, Commit):
248
113
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
249
mapping.revision_id_foreign_to_bzr)
114
self.lookup_foreign_revision_id)
250
115
yield o.id, rev.revision_id, roundtrip_revid
252
117
def all_revision_ids(self):
254
119
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
255
121
if roundtrip_revid:
256
122
ret.add(roundtrip_revid)
261
def _get_parents(self, revid):
262
if type(revid) != str:
265
hexsha, mapping = self.lookup_bzr_revision_id(revid)
266
except errors.NoSuchRevision:
269
commit = self._git[hexsha]
273
self.lookup_foreign_revision_id(p, mapping)
274
for p in commit.parents]
276
125
def get_parent_map(self, revids):
278
127
for revision_id in revids:
279
parents = self._get_parents(revision_id)
128
assert isinstance(revision_id, str)
280
129
if revision_id == revision.NULL_REVISION:
281
130
parent_map[revision_id] = ()
132
hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
134
commit = self._git[hexsha]
285
if len(parents) == 0:
286
parents = [revision.NULL_REVISION]
287
parent_map[revision_id] = tuple(parents)
137
parent_map[revision_id] = [
138
self.lookup_foreign_revision_id(p, mapping)
139
for p in commit.parents]
288
140
return parent_map
290
def get_known_graph_ancestry(self, revision_ids):
291
"""Return the known graph for a set of revision ids and their ancestors.
142
def get_ancestry(self, revision_id, topo_sorted=True):
143
"""See Repository.get_ancestry().
293
pending = set(revision_ids)
297
for revid in pending:
298
if revid == revision.NULL_REVISION:
300
parents = self._get_parents(revid)
301
if parents is not None:
302
this_parent_map[revid] = parents
303
parent_map.update(this_parent_map)
305
map(pending.update, this_parent_map.itervalues())
306
pending = pending.difference(parent_map)
307
return _mod_graph.KnownGraph(parent_map)
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
309
155
def get_signature_text(self, revision_id):
310
156
raise errors.NoSuchRevision(self, revision_id)
312
def check(self, revision_ids=None, callback_refs=None, check_repo=True):
313
result = GitCheck(self, check_repo=check_repo)
314
result.check(callback_refs)
317
158
def pack(self, hint=None, clean_obsolete_packs=False):
318
159
self._git.object_store.pack_loose_objects()
324
165
assert type(foreign_revid) is str
325
166
if mapping is None:
326
167
mapping = self.get_mapping()
168
from dulwich.protocol import (
327
171
if foreign_revid == ZERO_SHA:
328
172
return revision.NULL_REVISION
329
commit = self._git.object_store[foreign_revid]
330
while isinstance(commit, Tag):
331
commit = self._git[commit.object[1]]
173
commit = self._git[foreign_revid]
332
174
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
333
mapping.revision_id_foreign_to_bzr)
334
176
# FIXME: check testament before doing this?
335
177
if roundtrip_revid:
336
178
return roundtrip_revid
388
225
return (git_commit_id in self._git)
390
227
def has_revisions(self, revision_ids):
391
"""See Repository.has_revisions."""
392
228
return set(filter(self.has_revision, revision_ids))
394
230
def get_revisions(self, revids):
395
"""See Repository.get_revisions."""
396
231
return [self.get_revision(r) for r in revids]
398
233
def revision_trees(self, revids):
399
"""See Repository.revision_trees."""
400
234
for revid in revids:
401
235
yield self.revision_tree(revid)
403
237
def revision_tree(self, revision_id):
404
"""See Repository.revision_tree."""
405
238
revision_id = revision.ensure_null(revision_id)
406
239
if revision_id == revision.NULL_REVISION:
407
240
inv = inventory.Inventory(root_id=None)
408
241
inv.revision_id = revision_id
409
return InventoryRevisionTree(self, inv, revision_id)
242
return revisiontree.RevisionTree(self, inv, revision_id)
410
243
return GitRevisionTree(self, revision_id)
412
245
def get_inventory(self, revision_id):
413
raise NotImplementedError(self.get_inventory)
246
assert revision_id != None
247
return self.revision_tree(revision_id).inventory
415
249
def set_make_working_trees(self, trees):
416
raise errors.UnsupportedOperation(self.set_make_working_trees, self)
417
# TODO: Set bare= in the configuration bug=777065
419
252
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
421
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):
424
270
class GitRepositoryFormat(repository.RepositoryFormat):
425
271
"""Git repository format."""
427
supports_versioned_directories = False
428
273
supports_tree_reference = False
429
274
rich_root_data = True
430
supports_leaving_lock = False
432
supports_funky_characters = True
433
supports_external_lookups = False
434
supports_full_versioned_files = False
435
supports_revision_signatures = False
436
revision_graph_can_have_wrong_parents = False
439
def _matchingbzrdir(self):
440
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
441
return LocalGitControlDirFormat()
443
276
def get_format_description(self):
444
277
return "Git Repository"
446
def initialize(self, controldir, shared=False, _internal=False):
447
from bzrlib.plugins.git.dir import GitDir
448
if not isinstance(controldir, GitDir):
449
raise errors.UninitializableFormat(self)
450
return controldir.open_repository()
279
def initialize(self, url, shared=False, _internal=False):
280
raise errors.UninitializableFormat(self)
452
282
def check_conversion_target(self, target_repo_format):
453
283
return target_repo_format.rich_root_data