176
179
except IndexError:
177
180
base_tree = tree._repository.revision_tree(NULL_REVISION)
178
181
other_parent_trees = []
179
def find_unchanged_parent_ie(ie, parent_trees):
180
assert ie.kind in ("symlink", "file")
182
def find_unchanged_parent_ie(file_id, kind, other, parent_trees):
181
183
for ptree in parent_trees:
183
pie = ptree.inventory[ie.file_id]
185
pkind = ptree.kind(file_id)
184
186
except errors.NoSuchId:
187
if (pie.text_sha1 == ie.text_sha1 and
188
pie.kind == ie.kind and
189
pie.symlink_target == ie.symlink_target):
190
if (pkind == "file" and
191
ptree.get_file_sha1(file_id) == other):
193
if kind == "symlink":
194
if (pkind == "symlink" and
195
ptree.get_symlink_target(file_id) == other):
193
199
# Find all the changed blobs
194
200
for (file_id, path, changed_content, versioned, parent, name, kind,
195
201
executable) in tree.iter_changes(base_tree):
196
202
if kind[1] == "file":
197
ie = tree.inventory[file_id]
198
203
if changed_content:
200
pie = find_unchanged_parent_ie(ie, other_parent_trees)
205
pie = find_unchanged_parent_ie(file_id, kind[1], tree.get_file_sha1(file_id), other_parent_trees)
205
shamap[ie.file_id] = idmap.lookup_blob_id(
210
shamap[file_id] = idmap.lookup_blob_id(
206
211
pie.file_id, pie.revision)
208
213
# no-change merge ?
210
blob.data = tree.get_file_text(ie.file_id)
211
shamap[ie.file_id] = blob.id
215
blob.data = tree.get_file_text(file_id)
216
shamap[file_id] = blob.id
212
217
if not file_id in shamap:
213
new_blobs.append((path[1], ie))
218
new_blobs.append((path[1], file_id))
214
219
new_trees[posixpath.dirname(path[1])] = parent[1]
215
220
elif kind[1] == "symlink":
216
ie = tree.inventory[file_id]
217
221
if changed_content:
218
blob = symlink_to_blob(ie)
222
target = tree.get_symlink_target(file_id)
223
blob = symlink_to_blob(target)
219
224
shamap[file_id] = blob.id
221
find_unchanged_parent_ie(ie, other_parent_trees)
226
find_unchanged_parent_ie(file_id, kind[1], target, other_parent_trees)
223
yield path[1], blob, ie
228
yield path[1], blob, (file_id, tree.get_file_revision(file_id, path[1]))
224
229
new_trees[posixpath.dirname(path[1])] = parent[1]
225
230
elif kind[1] not in (None, "directory"):
226
231
raise AssertionError(kind[1])
227
232
if (path[0] not in (None, "") and
228
parent[0] in tree.inventory and
229
tree.inventory[parent[0]].kind == "directory"):
233
tree.has_id(parent[0]) and
234
tree.kind(parent[0]) == "directory"):
231
236
new_trees[posixpath.dirname(path[0])] = parent[0]
233
238
# Fetch contents of the blobs that were changed
234
for (path, ie), chunks in tree.iter_files_bytes(
235
[(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
239
for (path, file_id), chunks in tree.iter_files_bytes(
240
[(file_id, (path, file_id)) for (path, file_id) in new_blobs]):
237
242
obj.chunked = chunks
239
shamap[ie.file_id] = obj.id
243
yield path, obj, (file_id, tree.get_file_revision(file_id, path))
244
shamap[file_id] = obj.id
241
246
for path in unusual_modes:
242
247
parent_path = posixpath.dirname(path)
269
274
elif ie.kind == "directory":
270
275
# Not all cache backends store the tree information,
271
276
# calculate again from scratch
272
ret = directory_to_tree(ie, ie_to_hexsha, unusual_modes,
277
ret = directory_to_tree(ie.children, ie_to_hexsha,
278
unusual_modes, dummy_file_name, ie.parent_id is not None)
278
283
raise AssertionError
286
inv = tree.root_inventory
287
except AttributeError:
280
290
for path in sorted(trees.keys(), reverse=True):
281
ie = tree.inventory[trees[path]]
282
assert ie.kind == "directory"
283
obj = directory_to_tree(ie, ie_to_hexsha, unusual_modes,
291
file_id = trees[path]
292
assert tree.kind(file_id) == 'directory'
294
obj = directory_to_tree(ie.children, ie_to_hexsha, unusual_modes,
295
dummy_file_name, path == "")
285
296
if obj is not None:
287
shamap[ie.file_id] = obj.id
297
yield path, obj, (file_id, )
298
shamap[file_id] = obj.id
301
class PackTupleIterable(object):
303
def __init__(self, store):
305
self.store.lock_read()
311
def add(self, sha, path):
312
self.objects[sha] = path
315
return len(self.objects)
318
return ((self.store[object_id], path) for (object_id, path) in
319
self.objects.iteritems())
290
322
class BazaarObjectStore(BaseObjectStore):
293
325
def __init__(self, repository, mapping=None):
294
326
self.repository = repository
327
self._map_updated = False
295
329
if mapping is None:
296
330
self.mapping = default_mapping
298
332
self.mapping = mapping
299
333
self._cache = cache_from_repository(repository)
300
self._content_cache_types = ("tree")
334
self._content_cache_types = ("tree",)
301
335
self.start_write_group = self._cache.idmap.start_write_group
302
336
self.abort_write_group = self._cache.idmap.abort_write_group
303
337
self.commit_write_group = self._cache.idmap.commit_write_group
304
338
self.tree_cache = LRUTreeCache(self.repository)
339
self.unpeel_map = UnpeelMap.from_repository(self.repository)
341
def _missing_revisions(self, revisions):
342
return self._cache.idmap.missing_revisions(revisions)
306
344
def _update_sha_map(self, stop_revision=None):
345
if not self.is_locked():
346
raise AssertionError()
347
if self._map_updated:
349
if (stop_revision is not None and
350
not self._missing_revisions([stop_revision])):
307
352
graph = self.repository.get_graph()
308
353
if stop_revision is None:
309
heads = graph.heads(self.repository.all_revision_ids())
354
all_revids = self.repository.all_revision_ids()
355
missing_revids = self._missing_revisions(all_revids)
311
357
heads = set([stop_revision])
312
missing_revids = self._cache.idmap.missing_revisions(heads)
314
parents = graph.get_parent_map(heads)
316
for p in parents.values():
317
todo.update([x for x in p if x not in missing_revids])
318
heads = self._cache.idmap.missing_revisions(todo)
319
missing_revids.update(heads)
358
missing_revids = self._missing_revisions(heads)
360
parents = graph.get_parent_map(heads)
362
for p in parents.values():
363
todo.update([x for x in p if x not in missing_revids])
364
heads = self._missing_revisions(todo)
365
missing_revids.update(heads)
320
366
if NULL_REVISION in missing_revids:
321
367
missing_revids.remove(NULL_REVISION)
322
368
missing_revids = self.repository.has_revisions(missing_revids)
323
369
if not missing_revids:
370
if stop_revision is None:
371
self._map_updated = True
325
373
self.start_write_group()
342
392
self._update_sha_map()
343
393
return iter(self._cache.idmap.sha1s())
345
def _reconstruct_commit(self, rev, tree_sha, roundtrip, verifiers):
395
def _reconstruct_commit(self, rev, tree_sha, lossy, verifiers):
346
396
"""Reconstruct a Commit object.
348
398
:param rev: Revision object
349
399
:param tree_sha: SHA1 of the root tree object
350
:param roundtrip: Whether or not to roundtrip bzr metadata
400
:param lossy: Whether or not to roundtrip bzr metadata
351
401
:param verifiers: Verifiers for the commits
352
402
:return: Commit object
357
407
except errors.NoSuchRevision:
359
409
return self.mapping.export_commit(rev, tree_sha, parent_lookup,
360
roundtrip, verifiers)
362
def _create_fileid_map_blob(self, inv):
412
def _create_fileid_map_blob(self, tree):
363
413
# FIXME: This can probably be a lot more efficient,
364
414
# not all files necessarily have to be processed.
366
for (path, ie) in inv.iter_entries():
416
for (path, ie) in tree.inventory.iter_entries():
367
417
if self.mapping.generate_file_id(path) != ie.file_id:
368
418
file_ids[path] = ie.file_id
369
419
return self.mapping.export_fileid_map(file_ids)
371
def _revision_to_objects(self, rev, tree, roundtrip):
421
def _revision_to_objects(self, rev, tree, lossy):
372
422
"""Convert a revision to a set of git objects.
374
424
:param rev: Bazaar revision object
375
425
:param tree: Bazaar revision tree
376
:param roundtrip: Whether to roundtrip all Bazaar revision data
426
:param lossy: Whether to not roundtrip all Bazaar revision data
378
428
unusual_modes = extract_unusual_modes(rev)
379
429
present_parents = self.repository.has_revisions(rev.parent_ids)
380
430
parent_trees = self.tree_cache.revision_trees(
381
431
[p for p in rev.parent_ids if p in present_parents])
383
for path, obj, ie in _tree_to_objects(tree, parent_trees,
433
for path, obj, bzr_key_data in _tree_to_objects(tree, parent_trees,
384
434
self._cache.idmap, unusual_modes, self.mapping.BZR_DUMMY_FILE):
437
root_key_data = bzr_key_data
388
438
# Don't yield just yet
440
yield path, obj, bzr_key_data
391
441
if root_tree is None:
392
442
# Pointless commit - get the tree sha elsewhere
393
443
if not rev.parent_ids:
396
446
base_sha1 = self._lookup_revision_sha1(rev.parent_ids[0])
397
447
root_tree = self[self[base_sha1].tree]
398
root_ie = tree.inventory.root
399
if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
400
b = self._create_fileid_map_blob(tree.inventory)
448
root_key_data = (tree.get_root_id(), )
449
if not lossy and self.mapping.BZR_FILE_IDS_FILE is not None:
450
b = self._create_fileid_map_blob(tree)
401
451
if b is not None:
402
root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
452
root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
453
(stat.S_IFREG | 0644), b.id)
403
454
yield self.mapping.BZR_FILE_IDS_FILE, b, None
404
yield "", root_tree, root_ie
406
testament3 = StrictTestament3(rev, tree.inventory)
455
yield "", root_tree, root_key_data
457
testament3 = StrictTestament3(rev, tree)
407
458
verifiers = { "testament3-sha1": testament3.as_sha1() }
410
461
commit_obj = self._reconstruct_commit(rev, root_tree.id,
411
roundtrip=roundtrip, verifiers=verifiers)
462
lossy=lossy, verifiers=verifiers)
413
464
foreign_revid, mapping = mapping_registry.parse_revision_id(
425
476
rev = self.repository.get_revision(revid)
426
477
tree = self.tree_cache.revision_tree(rev.revision_id)
427
478
updater = self._get_updater(rev)
428
for path, obj, ie in self._revision_to_objects(rev, tree,
479
# FIXME JRV 2011-12-15: Shouldn't we try both values for lossy ?
480
for path, obj, ie in self._revision_to_objects(rev, tree, lossy=(not self.mapping.roundtripping)):
430
481
if isinstance(obj, Commit):
431
testament3 = StrictTestament3(rev, tree.inventory)
482
testament3 = StrictTestament3(rev, tree)
432
483
ie = { "testament3-sha1": testament3.as_sha1() }
433
484
updater.add_object(obj, ie, path)
434
485
commit_obj = updater.finish()
481
532
# no-change merge?
482
533
return self._reconstruct_blobs(
483
534
[(entry.file_id, entry.revision, None)]).next().id
535
elif entry.kind == 'tree-reference':
536
# FIXME: Make sure the file id is the root id
537
return self._lookup_revision_sha1(entry.reference_revision)
485
539
raise AssertionError("unknown entry kind '%s'" % entry.kind)
486
tree = directory_to_tree(inv[fileid], get_ie_sha1, unusual_modes,
487
self.mapping.BZR_DUMMY_FILE)
488
if (inv.root.file_id == fileid and
541
inv = bzr_tree.root_inventory
542
except AttributeError:
543
inv = bzr_tree.inventory
544
tree = directory_to_tree(inv[fileid].children,
545
get_ie_sha1, unusual_modes, self.mapping.BZR_DUMMY_FILE,
546
bzr_tree.get_root_id() == fileid)
547
if (bzr_tree.get_root_id() == fileid and
489
548
self.mapping.BZR_FILE_IDS_FILE is not None):
490
b = self._create_fileid_map_blob(inv)
551
b = self._create_fileid_map_blob(bzr_tree)
491
552
# If this is the root tree, add the file ids
492
tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
493
_check_expected_sha(expected_sha, tree)
553
tree[self.mapping.BZR_FILE_IDS_FILE] = (
554
(stat.S_IFREG | 0644), b.id)
556
_check_expected_sha(expected_sha, tree)
496
559
def get_parents(self, sha):
530
588
def __contains__(self, sha):
531
589
# See if sha is in map
533
(type, type_data) = self.lookup_git_sha(sha)
535
return self.repository.has_revision(type_data[0])
537
return self.repository.texts.has_key(type_data)
539
return self.repository.has_revision(type_data[1])
591
for (type, type_data) in self.lookup_git_sha(sha):
593
if self.repository.has_revision(type_data[0]):
596
if self.repository.texts.has_key(type_data):
599
if self.repository.has_revision(type_data[1]):
602
raise AssertionError("Unknown object type '%s'" % type)
541
raise AssertionError("Unknown object type '%s'" % type)
545
def lookup_git_shas(self, shas, update_map=True):
546
from dulwich.protocol import ZERO_SHA
610
self._map_updated = False
611
self.repository.lock_read()
612
return LogicalLockResult(self.unlock)
614
def lock_write(self):
616
self._map_updated = False
617
self.repository.lock_write()
618
return LogicalLockResult(self.unlock)
621
return (self._locked is not None)
625
self._map_updated = False
626
self.repository.unlock()
628
def lookup_git_shas(self, shas):
549
631
if sha == ZERO_SHA:
550
ret[sha] = ("commit", (NULL_REVISION, None, {}))
632
ret[sha] = [("commit", (NULL_REVISION, None, {}))]
553
ret[sha] = self._cache.idmap.lookup_git_sha(sha)
635
ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
556
# if not, see if there are any unconverted revisions and add
557
# them to the map, search for sha in map again
558
self._update_sha_map()
561
ret[sha] = self._cache.idmap.lookup_git_sha(sha)
637
# if not, see if there are any unconverted revisions and
638
# add them to the map, search for sha in map again
639
self._update_sha_map()
641
ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
566
def lookup_git_sha(self, sha, update_map=True):
567
return self.lookup_git_shas([sha], update_map=update_map)[sha]
646
def lookup_git_sha(self, sha):
647
return self.lookup_git_shas([sha])[sha]
569
649
def __getitem__(self, sha):
570
650
if self._cache.content_cache is not None:
572
652
return self._cache.content_cache[sha]
575
(type, type_data) = self.lookup_git_sha(sha)
576
# convert object to git object
578
(revid, tree_sha, verifiers) = type_data
580
rev = self.repository.get_revision(revid)
581
except errors.NoSuchRevision:
582
trace.mutter('entry for %s %s in shamap: %r, but not found in '
583
'repository', type, sha, type_data)
585
commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
587
_check_expected_sha(sha, commit)
590
(fileid, revision) = type_data
591
return self._reconstruct_blobs([(fileid, revision, sha)]).next()
593
(fileid, revid) = type_data
595
tree = self.tree_cache.revision_tree(revid)
596
rev = self.repository.get_revision(revid)
597
except errors.NoSuchRevision:
598
trace.mutter('entry for %s %s in shamap: %r, but not found in repository', type, sha, type_data)
600
unusual_modes = extract_unusual_modes(rev)
602
return self._reconstruct_tree(fileid, revid, tree.inventory,
603
unusual_modes, expected_sha=sha)
604
except errors.NoSuchRevision:
655
for (kind, type_data) in self.lookup_git_sha(sha):
656
# convert object to git object
658
(revid, tree_sha, verifiers) = type_data
660
rev = self.repository.get_revision(revid)
661
except errors.NoSuchRevision:
662
if revid == NULL_REVISION:
663
raise AssertionError(
664
"should not try to look up NULL_REVISION")
665
trace.mutter('entry for %s %s in shamap: %r, but not '
666
'found in repository', kind, sha, type_data)
668
# FIXME: the type data should say whether conversion was lossless
669
commit = self._reconstruct_commit(rev, tree_sha,
670
lossy=(not self.mapping.roundtripping), verifiers=verifiers)
671
_check_expected_sha(sha, commit)
674
(fileid, revision) = type_data
675
blobs = self._reconstruct_blobs([(fileid, revision, sha)])
678
(fileid, revid) = type_data
680
tree = self.tree_cache.revision_tree(revid)
681
rev = self.repository.get_revision(revid)
682
except errors.NoSuchRevision:
683
trace.mutter('entry for %s %s in shamap: %r, but not found in '
684
'repository', kind, sha, type_data)
686
unusual_modes = extract_unusual_modes(rev)
688
return self._reconstruct_tree(fileid, revid,
689
tree, unusual_modes, expected_sha=sha)
690
except errors.NoSuchRevision:
693
raise AssertionError("Unknown object type '%s'" % kind)
607
raise AssertionError("Unknown object type '%s'" % type)
609
697
def generate_lossy_pack_contents(self, have, want, progress=None,
610
698
get_tagged=None):
621
709
processed = set()
622
710
ret = self.lookup_git_shas(have + want)
623
711
for commit_sha in have:
712
commit_sha = self.unpeel_map.peel_tag(commit_sha, commit_sha)
625
(type, (revid, tree_sha)) = ret[commit_sha]
714
for (type, type_data) in ret[commit_sha]:
715
assert type == "commit"
716
processed.add(type_data[0])
629
assert type == "commit"
718
trace.mutter("unable to find remote ref %s", commit_sha)
632
720
for commit_sha in want:
633
721
if commit_sha in have:
636
(type, (revid, tree_sha)) = ret[commit_sha]
724
for (type, type_data) in ret[commit_sha]:
725
assert type == "commit"
726
pending.add(type_data[0])
640
assert type == "commit"
643
todo = _find_missing_bzr_revids(self.repository.get_parent_map,
645
trace.mutter('sending revisions %r', todo)
730
graph = self.repository.get_graph()
731
todo = _find_missing_bzr_revids(graph, pending, processed)
732
ret = PackTupleIterable(self)
647
733
pb = ui.ui_factory.nested_progress_bar()
649
735
for i, revid in enumerate(todo):
650
736
pb.update("generating git objects", i, len(todo))
651
rev = self.repository.get_revision(revid)
738
rev = self.repository.get_revision(revid)
739
except errors.NoSuchRevision:
652
741
tree = self.tree_cache.revision_tree(revid)
653
for path, obj, ie in self._revision_to_objects(rev, tree,
654
roundtrip=not lossy):
655
ret.append((obj, path))
742
for path, obj, ie in self._revision_to_objects(rev, tree, lossy=lossy):
743
ret.add(obj.id, path)
660
748
def add_thin_pack(self):