71
75
self._cache = lru_cache.LRUSizeCache(max_size=MAX_TREE_CACHE_SIZE,
72
76
after_cleanup_size=None, compute_size=approx_tree_size)
74
def revision_tree(self, revid):
78
def revision_tree(self, revid):
76
return self._cache[revid]
80
tree = self._cache[revid]
78
82
tree = self.repository.revision_tree(revid)
84
assert tree.get_revision_id() == tree.inventory.revision_id
82
87
def iter_revision_trees(self, revids):
83
trees = dict([(k, self._cache.get(k)) for k in revids])
84
for tree in self.repository.revision_trees(
85
[r for r, v in trees.iteritems() if v is None]):
92
tree = self._cache[revid]
96
assert tree.get_revision_id() == revid
97
assert tree.inventory.revision_id == revid
99
for tree in self.repository.revision_trees(todo):
86
100
trees[tree.get_revision_id()] = tree
88
102
return (trees[r] for r in revids)
145
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes, dummy_file_name=None):
159
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes,
160
dummy_file_name=None):
146
161
"""Iterate over the objects that were introduced in a revision.
148
163
:param idmap: id map
149
:param unusual_modes: Unusual file modes
164
:param parent_trees: Parent revision trees
165
:param unusual_modes: Unusual file modes dictionary
150
166
:param dummy_file_name: File name to use for dummy files
151
167
in empty directories. None to skip empty directories
152
168
:return: Yields (path, object, ie) entries
173
189
pie.symlink_target == ie.symlink_target):
193
# Find all the changed blobs
176
194
for (file_id, path, changed_content, versioned, parent, name, kind,
177
195
executable) in tree.iter_changes(base_tree):
178
196
if kind[1] == "file":
206
224
new_trees[posixpath.dirname(path[1])] = parent[1]
207
225
elif kind[1] not in (None, "directory"):
208
226
raise AssertionError(kind[1])
209
if path[0] is not None:
227
if (path[0] not in (None, "") and
228
parent[0] in tree.inventory and
229
tree.inventory[parent[0]].kind == "directory"):
210
231
new_trees[posixpath.dirname(path[0])] = parent[0]
233
# Fetch contents of the blobs that were changed
212
234
for (path, ie), chunks in tree.iter_files_bytes(
213
235
[(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
219
241
for path in unusual_modes:
220
242
parent_path = posixpath.dirname(path)
221
243
new_trees[parent_path] = tree.path2id(parent_path)
225
247
items = new_trees.items()
227
249
for path, file_id in items:
229
parent_id = tree.inventory[file_id].parent_id
230
except errors.NoSuchId:
231
# Directory was removed recursively perhaps ?
250
parent_id = tree.inventory[file_id].parent_id
233
251
if parent_id is not None:
234
252
parent_path = urlutils.dirname(path)
235
253
new_trees[parent_path] = parent_id
324
342
self._update_sha_map()
325
343
return iter(self._cache.idmap.sha1s())
327
def _reconstruct_commit(self, rev, tree_sha, roundtrip):
345
def _reconstruct_commit(self, rev, tree_sha, roundtrip, verifiers):
346
"""Reconstruct a Commit object.
348
:param rev: Revision object
349
:param tree_sha: SHA1 of the root tree object
350
:param roundtrip: Whether or not to roundtrip bzr metadata
351
:param verifiers: Verifiers for the commits
352
:return: Commit object
328
354
def parent_lookup(revid):
330
356
return self._lookup_revision_sha1(revid)
331
357
except errors.NoSuchRevision:
332
trace.warning("Ignoring ghost parent %s", revid)
334
359
return self.mapping.export_commit(rev, tree_sha, parent_lookup,
360
roundtrip, verifiers)
362
def _create_fileid_map_blob(self, inv):
363
# FIXME: This can probably be a lot more efficient,
364
# not all files necessarily have to be processed.
366
for (path, ie) in inv.iter_entries():
367
if self.mapping.generate_file_id(path) != ie.file_id:
368
file_ids[path] = ie.file_id
369
return self.mapping.export_fileid_map(file_ids)
337
371
def _revision_to_objects(self, rev, tree, roundtrip):
338
372
"""Convert a revision to a set of git objects.
362
396
base_sha1 = self._lookup_revision_sha1(rev.parent_ids[0])
363
397
root_tree = self[self[base_sha1].tree]
364
398
root_ie = tree.inventory.root
366
# FIXME: This can probably be a lot more efficient,
367
# not all files necessarily have to be processed.
369
for (path, ie) in tree.inventory.iter_entries():
370
if self.mapping.generate_file_id(path) != ie.file_id:
371
file_ids[path] = ie.file_id
372
b = self.mapping.export_fileid_map(file_ids)
399
if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
400
b = self._create_fileid_map_blob(tree.inventory)
373
401
if b is not None:
374
402
root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
375
403
yield self.mapping.BZR_FILE_IDS_FILE, b, None
376
404
yield "", root_tree, root_ie
377
commit_obj = self._reconstruct_commit(rev, root_tree.id, roundtrip=roundtrip)
406
testament3 = StrictTestament3(rev, tree.inventory)
407
verifiers = { "testament3-sha1": testament3.as_sha1() }
410
commit_obj = self._reconstruct_commit(rev, root_tree.id,
411
roundtrip=roundtrip, verifiers=verifiers)
379
413
foreign_revid, mapping = mapping_registry.parse_revision_id(
393
427
updater = self._get_updater(rev)
394
428
for path, obj, ie in self._revision_to_objects(rev, tree,
396
updater.add_object(obj, ie)
430
if isinstance(obj, Commit):
431
testament3 = StrictTestament3(rev, tree.inventory)
432
ie = { "testament3-sha1": testament3.as_sha1() }
433
updater.add_object(obj, ie, path)
397
434
commit_obj = updater.finish()
398
435
return commit_obj.id
448
485
raise AssertionError("unknown entry kind '%s'" % entry.kind)
449
486
tree = directory_to_tree(inv[fileid], get_ie_sha1, unusual_modes,
450
487
self.mapping.BZR_DUMMY_FILE)
488
if (inv.root.file_id == fileid and
489
self.mapping.BZR_FILE_IDS_FILE is not None):
490
b = self._create_fileid_map_blob(inv)
491
# If this is the root tree, add the file ids
492
tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
451
493
_check_expected_sha(expected_sha, tree)
471
513
return mapping_registry.parse_revision_id(revid)[0]
472
514
except errors.InvalidRevisionId:
473
self._update_sha_map(revid)
515
self.repository.lock_read()
517
self._update_sha_map(revid)
519
self.repository.unlock()
474
520
return self._cache.idmap.lookup_commit(revid)
476
522
def get_raw(self, sha):
499
545
def lookup_git_shas(self, shas, update_map=True):
546
from dulwich.protocol import ZERO_SHA
550
ret[sha] = ("commit", (NULL_REVISION, None, {}))
503
553
ret[sha] = self._cache.idmap.lookup_git_sha(sha)
525
575
(type, type_data) = self.lookup_git_sha(sha)
526
576
# convert object to git object
527
577
if type == "commit":
528
(revid, tree_sha) = type_data
578
(revid, tree_sha, verifiers) = type_data
530
580
rev = self.repository.get_revision(revid)
531
581
except errors.NoSuchRevision:
532
582
trace.mutter('entry for %s %s in shamap: %r, but not found in '
533
583
'repository', type, sha, type_data)
534
584
raise KeyError(sha)
535
commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True)
585
commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
536
587
_check_expected_sha(sha, commit)
538
589
elif type == "blob":