17
17
"""Map from Git sha's to Bazaar objects."""
23
from bzrlib.errors import NoSuchRevision
25
from bzrlib.plugins.git.mapping import (
26
inventory_to_tree_and_blobs,
29
from bzrlib.plugins.git.shamap import GitShaMap
31
19
from dulwich.objects import (
36
class GitObjectConverter(object):
24
from dulwich.object_store import (
35
from bzrlib.revision import (
39
from bzrlib.plugins.git.mapping import (
42
extract_unusual_modes,
46
from bzrlib.plugins.git.shamap import (
47
from_repository as cache_from_repository,
53
def get_object_store(repo, mapping=None):
54
git = getattr(repo, "_git", None)
56
return git.object_store
57
return BazaarObjectStore(repo, mapping)
60
MAX_TREE_CACHE_SIZE = 50 * 1024 * 1024
63
class LRUTreeCache(object):
65
def __init__(self, repository):
66
def approx_tree_size(tree):
67
# Very rough estimate, 1k per inventory entry
68
return len(tree.inventory) * 1024
69
self.repository = repository
70
self._cache = lru_cache.LRUSizeCache(max_size=MAX_TREE_CACHE_SIZE,
71
after_cleanup_size=None, compute_size=approx_tree_size)
73
def revision_tree(self, revid):
75
return self._cache[revid]
77
tree = self.repository.revision_tree(revid)
81
def iter_revision_trees(self, revids):
82
trees = dict([(k, self._cache.get(k)) for k in revids])
83
for tree in self.repository.revision_trees(
84
[r for r, v in trees.iteritems() if v is None]):
85
trees[tree.get_revision_id()] = tree
87
return (trees[r] for r in revids)
89
def revision_trees(self, revids):
90
return list(self.iter_revision_trees(revids))
93
self._cache.add(tree.get_revision_id(), tree)
96
def _check_expected_sha(expected_sha, object):
97
"""Check whether an object matches an expected SHA.
99
:param expected_sha: None or expected SHA as either binary or as hex digest
100
:param object: Object to verify
102
if expected_sha is None:
104
if len(expected_sha) == 40:
105
if expected_sha != object.sha().hexdigest():
106
raise AssertionError("Invalid sha for %r: %s" % (object,
108
elif len(expected_sha) == 20:
109
if expected_sha != object.sha().digest():
110
raise AssertionError("Invalid sha for %r: %s" % (object,
111
sha_to_hex(expected_sha)))
113
raise AssertionError("Unknown length %d for %r" % (len(expected_sha),
117
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes):
118
"""Iterate over the objects that were introduced in a revision.
121
:param unusual_modes: Unusual file modes
122
:return: Yields (path, object, ie) entries
128
base_tree = parent_trees[0]
129
other_parent_trees = parent_trees[1:]
131
base_tree = tree._repository.revision_tree(NULL_REVISION)
132
other_parent_trees = []
133
def find_unchanged_parent_ie(ie, parent_trees):
134
assert ie.kind in ("symlink", "file")
135
for ptree in parent_trees:
137
pie = ptree.inventory[ie.file_id]
138
except errors.NoSuchId:
141
if (pie.text_sha1 == ie.text_sha1 and
142
pie.kind == ie.kind and
143
pie.symlink_target == ie.symlink_target):
146
for (file_id, path, changed_content, versioned, parent, name, kind,
147
executable) in tree.iter_changes(base_tree):
148
if kind[1] == "file":
149
ie = tree.inventory[file_id]
153
pie = find_unchanged_parent_ie(ie, other_parent_trees)
157
shamap[ie.file_id] = idmap.lookup_blob_id(
158
pie.file_id, pie.revision)
159
if not file_id in shamap:
160
new_blobs.append((path[1], ie))
161
new_trees[posixpath.dirname(path[1])] = parent[1]
162
elif kind[1] == "symlink":
163
ie = tree.inventory[file_id]
165
blob = symlink_to_blob(ie)
166
shamap[file_id] = blob.id
168
find_unchanged_parent_ie(ie, other_parent_trees)
170
yield path[1], blob, ie
171
new_trees[posixpath.dirname(path[1])] = parent[1]
172
elif kind[1] not in (None, "directory"):
173
raise AssertionError(kind[1])
174
if path[0] is not None:
175
new_trees[posixpath.dirname(path[0])] = parent[0]
177
for (path, ie), chunks in tree.iter_files_bytes(
178
[(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
182
shamap[ie.file_id] = obj.id
184
for fid in unusual_modes:
185
new_trees[posixpath.dirname(tree.inventory.id2path(path))] = tree.inventory[fid].parent_id
189
items = new_trees.items()
191
for path, file_id in items:
193
parent_id = tree.inventory[file_id].parent_id
194
except errors.NoSuchId:
195
# Directory was removed recursively perhaps ?
197
if parent_id is not None:
198
parent_path = urlutils.dirname(path)
199
new_trees[parent_path] = parent_id
200
trees[path] = file_id
202
def ie_to_hexsha(ie):
204
return shamap[ie.file_id]
206
if ie.kind in ("file", "symlink"):
208
return idmap.lookup_blob_id(ie.file_id, ie.revision)
212
blob.data = tree.get_file_text(ie.file_id)
214
elif ie.kind == "directory":
215
# Not all cache backends store the tree information,
216
# calculate again from scratch
217
ret = directory_to_tree(ie, ie_to_hexsha, unusual_modes)
224
for path in sorted(trees.keys(), reverse=True):
225
ie = tree.inventory[trees[path]]
226
assert ie.kind == "directory"
227
obj = directory_to_tree(ie, ie_to_hexsha, unusual_modes)
230
shamap[ie.file_id] = obj.id
233
class BazaarObjectStore(BaseObjectStore):
234
"""A Git-style object store backed onto a Bazaar repository."""
38
236
def __init__(self, repository, mapping=None):
39
237
self.repository = repository
40
238
if mapping is None:
41
self.mapping = self.repository.get_mapping()
239
self.mapping = default_mapping
43
241
self.mapping = mapping
44
self._idmap = GitShaMap(self.repository._transport)
242
self._cache = cache_from_repository(repository)
243
self._content_cache_types = ("tree")
244
self.start_write_group = self._cache.idmap.start_write_group
245
self.abort_write_group = self._cache.idmap.abort_write_group
246
self.commit_write_group = self._cache.idmap.commit_write_group
247
self.tree_cache = LRUTreeCache(self.repository)
46
def _update_sha_map(self):
47
all_revids = self.repository.all_revision_ids()
249
def _update_sha_map(self, stop_revision=None):
48
250
graph = self.repository.get_graph()
49
present_revids = set(self._idmap.revids())
50
pb = ui.ui_factory.nested_progress_bar()
52
for i, revid in enumerate(graph.iter_topo_order(all_revids)):
53
if revid in present_revids:
55
pb.update("updating git map", i, len(all_revids))
56
self._update_sha_map_revision(revid)
251
if stop_revision is None:
252
heads = graph.heads(self.repository.all_revision_ids())
254
heads = set([stop_revision])
255
missing_revids = self._cache.idmap.missing_revisions(heads)
257
parents = graph.get_parent_map(heads)
259
for p in parents.values():
260
todo.update([x for x in p if x not in missing_revids])
261
heads = self._cache.idmap.missing_revisions(todo)
262
missing_revids.update(heads)
263
if NULL_REVISION in missing_revids:
264
missing_revids.remove(NULL_REVISION)
265
missing_revids = self.repository.has_revisions(missing_revids)
266
if not missing_revids:
268
self.start_write_group()
270
pb = ui.ui_factory.nested_progress_bar()
272
for i, revid in enumerate(graph.iter_topo_order(missing_revids)):
273
trace.mutter('processing %r', revid)
274
pb.update("updating git map", i, len(missing_revids))
275
self._update_sha_map_revision(revid)
279
self.abort_write_group()
282
self.commit_write_group()
285
self._update_sha_map()
286
return iter(self._cache.idmap.sha1s())
288
def _reconstruct_commit(self, rev, tree_sha):
289
def parent_lookup(revid):
291
return self._lookup_revision_sha1(revid)
292
except errors.NoSuchRevision:
293
trace.warning("Ignoring ghost parent %s", revid)
295
return self.mapping.export_commit(rev, tree_sha, parent_lookup)
297
def _revision_to_objects(self, rev, tree):
298
unusual_modes = extract_unusual_modes(rev)
299
present_parents = self.repository.has_revisions(rev.parent_ids)
300
parent_trees = self.tree_cache.revision_trees(
301
[p for p in rev.parent_ids if p in present_parents])
303
for path, obj, ie in _tree_to_objects(tree, parent_trees,
304
self._cache.idmap, unusual_modes):
309
# Pointless commit - get the tree sha elsewhere
310
if not rev.parent_ids:
313
base_sha1 = self._lookup_revision_sha1(rev.parent_ids[0])
314
tree_sha = self[base_sha1].tree
315
commit_obj = self._reconstruct_commit(rev, tree_sha)
317
foreign_revid, mapping = mapping_registry.parse_revision_id(
319
except errors.InvalidRevisionId:
322
_check_expected_sha(foreign_revid, commit_obj)
323
yield None, commit_obj, None
325
def _get_updater(self, rev):
326
return self._cache.get_updater(rev)
60
328
def _update_sha_map_revision(self, revid):
61
inv = self.repository.get_inventory(revid)
62
objects = inventory_to_tree_and_blobs(self.repository, self.mapping, revid)
63
for sha, o, path in objects:
66
ie = inv[inv.path2id(path)]
67
if ie.kind in ("file", "symlink"):
68
self._idmap.add_entry(sha, "blob", (ie.file_id, ie.revision))
70
self._idmap.add_entry(sha, "tree", (ie.file_id, ie.revision))
71
rev = self.repository.get_revision(revid)
72
commit_obj = revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
73
self._idmap.add_entry(commit_obj.sha().hexdigest(), "commit", (revid, tree_sha))
75
def _get_blob(self, fileid, revision):
76
text = self.repository.texts.get_record_stream([(fileid, revision)], "unordered", True).next().get_bytes_as("fulltext")
81
def _get_tree(self, fileid, revid):
82
raise NotImplementedError(self._get_tree)
84
def _get_commit(self, revid, tree_sha):
85
rev = self.repository.get_revision(revid)
86
return revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
88
def __getitem__(self, sha):
89
# See if sha is in map
91
(type, type_data) = self._idmap.lookup_git_sha(sha)
93
# if not, see if there are any unconverted revisions and add them
329
rev = self.repository.get_revision(revid)
330
tree = self.tree_cache.revision_tree(rev.revision_id)
331
updater = self._get_updater(rev)
332
for path, obj, ie in self._revision_to_objects(rev, tree):
333
updater.add_object(obj, ie)
334
commit_obj = updater.finish()
337
def _reconstruct_blobs(self, keys):
338
"""Return a Git Blob object from a fileid and revision stored in bzr.
340
:param fileid: File id of the text
341
:param revision: Revision of the text
343
stream = self.repository.iter_files_bytes(
344
((key[0], key[1], key) for key in keys))
345
for (fileid, revision, expected_sha), chunks in stream:
347
blob.chunked = chunks
348
if blob.id != expected_sha and blob.data == "":
349
# Perhaps it's a symlink ?
350
tree = self.tree_cache.revision_tree(revision)
351
entry = tree.inventory[fileid]
352
if entry.kind == 'symlink':
353
blob = symlink_to_blob(entry)
354
_check_expected_sha(expected_sha, blob)
357
def _reconstruct_tree(self, fileid, revid, inv, unusual_modes,
359
"""Return a Git Tree object from a file id and a revision stored in bzr.
361
:param fileid: fileid in the tree.
362
:param revision: Revision of the tree.
364
def get_ie_sha1(entry):
365
if entry.kind == "directory":
367
return self._cache.idmap.lookup_tree_id(entry.file_id,
369
except (NotImplementedError, KeyError):
370
obj = self._reconstruct_tree(entry.file_id, revid, inv,
376
elif entry.kind in ("file", "symlink"):
378
return self._cache.idmap.lookup_blob_id(entry.file_id,
382
return self._reconstruct_blobs(
383
[(entry.file_id, entry.revision, None)]).next().id
385
raise AssertionError("unknown entry kind '%s'" % entry.kind)
386
tree = directory_to_tree(inv[fileid], get_ie_sha1, unusual_modes)
387
_check_expected_sha(expected_sha, tree)
390
def get_parents(self, sha):
391
"""Retrieve the parents of a Git commit by SHA1.
393
:param sha: SHA1 of the commit
394
:raises: KeyError, NotCommitError
396
return self[sha].parents
398
def _lookup_revision_sha1(self, revid):
399
"""Return the SHA1 matching a Bazaar revision."""
400
if revid == NULL_REVISION:
403
return self._cache.idmap.lookup_commit(revid)
406
return mapping_registry.parse_revision_id(revid)[0]
407
except errors.InvalidRevisionId:
408
self._update_sha_map(revid)
409
return self._cache.idmap.lookup_commit(revid)
411
def get_raw(self, sha):
412
"""Get the raw representation of a Git object by SHA1.
414
:param sha: SHA1 of the git object
417
return (obj.type, obj.as_raw_string())
419
def __contains__(self, sha):
420
# See if sha is in map
422
(type, type_data) = self._lookup_git_sha(sha)
424
return self.repository.has_revision(type_data[0])
426
return self.repository.texts.has_version(type_data)
428
return self.repository.has_revision(type_data[1])
430
raise AssertionError("Unknown object type '%s'" % type)
434
def _lookup_git_sha(self, sha):
435
# See if sha is in map
437
return self._cache.idmap.lookup_git_sha(sha)
439
# if not, see if there are any unconverted revisions and add them
94
440
# to the map, search for sha in map again
95
441
self._update_sha_map()
96
(type, type_data) = self._idmap.lookup_git_sha(sha)
442
return self._cache.idmap.lookup_git_sha(sha)
444
def __getitem__(self, sha):
445
if self._cache.content_cache is not None:
447
return self._cache.content_cache[sha]
450
(type, type_data) = self._lookup_git_sha(sha)
97
451
# convert object to git object
98
452
if type == "commit":
99
return self._get_commit(*type_data)
453
(revid, tree_sha) = type_data
455
rev = self.repository.get_revision(revid)
456
except errors.NoSuchRevision:
457
trace.mutter('entry for %s %s in shamap: %r, but not found in '
458
'repository', type, sha, type_data)
460
commit = self._reconstruct_commit(rev, tree_sha)
461
_check_expected_sha(sha, commit)
100
463
elif type == "blob":
101
return self._get_blob(*type_data)
464
(fileid, revision) = type_data
465
return self._reconstruct_blobs([(fileid, revision, sha)]).next()
102
466
elif type == "tree":
103
return self._get_tree(*type_data)
467
(fileid, revid) = type_data
469
tree = self.tree_cache.revision_tree(revid)
470
rev = self.repository.get_revision(revid)
471
except errors.NoSuchRevision:
472
trace.mutter('entry for %s %s in shamap: %r, but not found in repository', type, sha, type_data)
474
unusual_modes = extract_unusual_modes(rev)
476
return self._reconstruct_tree(fileid, revid, tree.inventory,
477
unusual_modes, expected_sha=sha)
478
except errors.NoSuchRevision:
105
481
raise AssertionError("Unknown object type '%s'" % type)
483
def generate_pack_contents(self, have, want, progress=None, get_tagged=None):
484
"""Iterate over the contents of a pack file.
486
:param have: List of SHA1s of objects that should not be sent
487
:param want: List of SHA1s of objects that should be sent
490
for commit_sha in have:
492
(type, (revid, tree_sha)) = self._lookup_git_sha(commit_sha)
496
assert type == "commit"
499
for commit_sha in want:
500
if commit_sha in have:
502
(type, (revid, tree_sha)) = self._lookup_git_sha(commit_sha)
503
assert type == "commit"
507
processed.update(pending)
508
next_map = self.repository.get_parent_map(pending)
510
for item in next_map.iteritems():
512
next_pending.update(p for p in item[1] if p not in processed)
513
pending = next_pending
514
if NULL_REVISION in todo:
515
todo.remove(NULL_REVISION)
516
trace.mutter('sending revisions %r', todo)
518
pb = ui.ui_factory.nested_progress_bar()
520
for i, revid in enumerate(todo):
521
pb.update("generating git objects", i, len(todo))
522
rev = self.repository.get_revision(revid)
523
tree = self.tree_cache.revision_tree(revid)
524
for path, obj, ie in self._revision_to_objects(rev, tree):
525
ret.append((obj, path))
530
def add_thin_pack(self):
533
fd, path = tempfile.mkstemp(suffix=".pack")
534
f = os.fdopen(fd, 'wb')
536
from dulwich.pack import PackData, Pack
537
from bzrlib.plugins.git.fetch import import_git_objects
540
if os.path.getsize(path) == 0:
543
pd.create_index_v2(path[:-5]+".idx", self.object_store.get_raw)
546
self.repository.lock_write()
548
self.repository.start_write_group()
550
import_git_objects(self.repository, self.mapping,
551
p.iterobjects(get_raw=self.get_raw),
554
self.repository.abort_write_group()
557
self.repository.commit_write_group()
559
self.repository.unlock()
562
# The pack isn't kept around anyway, so no point
563
# in treating full packs different from thin packs
564
add_pack = add_thin_pack