Rev 3746: Teach CHKInventory how to make a new inventory from an inventory delta. in http://people.ubuntu.com/~robertc/baz2.0/repository
Robert Collins
robertc at robertcollins.net
Fri Oct 10 03:27:45 BST 2008
At http://people.ubuntu.com/~robertc/baz2.0/repository
------------------------------------------------------------
revno: 3746
revision-id: robertc at robertcollins.net-20081010022735-y3nzojp92ur6k30r
parent: robertc at robertcollins.net-20081009045037-cbgz8f749b93oe87
committer: Robert Collins <robertc at robertcollins.net>
branch nick: repository
timestamp: Fri 2008-10-10 13:27:35 +1100
message:
Teach CHKInventory how to make a new inventory from an inventory delta.
modified:
bzrlib/chk_map.py chk_map.py-20081001014447-ue6kkuhofvdecvxa-1
bzrlib/inventory.py inventory.py-20050309040759-6648b84ca2005b37
bzrlib/tests/test_inv.py testinv.py-20050722220913-1dc326138d1a5892
=== modified file 'bzrlib/chk_map.py'
--- a/bzrlib/chk_map.py 2008-10-09 04:50:37 +0000
+++ b/bzrlib/chk_map.py 2008-10-10 02:27:35 +0000
@@ -130,6 +130,7 @@
def __init__(self):
self._nodes = {}
+ self._key = None
def add_child(self, name, child):
"""Add a child to the node.
=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py 2008-10-09 04:50:37 +0000
+++ b/bzrlib/inventory.py 2008-10-10 02:27:35 +0000
@@ -917,7 +917,7 @@
When neither new_path nor old_path are None, the change is a
modification to an entry, such as a rename, reparent, kind change
- etc.
+ etc.
The children attribute of new_entry is ignored. This is because
this method preserves children automatically across alterations to
@@ -1374,6 +1374,38 @@
result.parent_id = None
return result
+ def create_by_apply_delta(self, inventory_delta, new_revision_id):
+ """Create a new CHKInventory by applying inventory_delta to this one.
+
+ :param inventory_delta: The inventory delta to apply. See
+ Inventory.apply_delta for details.
+ :param new_revision_id: The revision id of the resulting CHKInventory.
+ :return: The new CHKInventory.
+ """
+ result = CHKInventory()
+ result.revision_id = new_revision_id
+ result.id_to_entry = chk_map.CHKMap(self.id_to_entry._store,
+ self.id_to_entry._root_node)
+ result.root_id = self.root_id
+ id_to_entry_delta = []
+ for old_path, new_path, file_id, entry in inventory_delta:
+ if new_path == '':
+ result.root_id = file_id
+ if new_path is None:
+ # Make a delete:
+ new_key = None
+ new_value = None
+ else:
+ new_key = file_id
+ new_value = result._entry_to_bytes(entry)
+ if old_path is None:
+ old_key = None
+ else:
+ old_key = file_id
+ id_to_entry_delta.append((old_key, new_key, new_value))
+ result.id_to_entry.apply_delta(id_to_entry_delta)
+ return result
+
@classmethod
def deserialise(klass, chk_store, bytes, expected_revision_id):
"""Deserialise a CHKInventory.
=== modified file 'bzrlib/tests/test_inv.py'
--- a/bzrlib/tests/test_inv.py 2008-10-09 04:50:37 +0000
+++ b/bzrlib/tests/test_inv.py 2008-10-10 02:27:35 +0000
@@ -330,3 +330,25 @@
chk_bytes = self.get_chk_bytes()
chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
self.assertFalse(chk_inv.has_id('fileid'))
+
+ def test_create_by_apply_delta_empty_add_child(self):
+ inv = Inventory()
+ inv.revision_id = "revid"
+ inv.root.revision = "rootrev"
+ chk_bytes = self.get_chk_bytes()
+ base_inv = CHKInventory.from_inventory(chk_bytes, inv)
+ a_entry = InventoryFile("A-id", "A", inv.root.file_id)
+ a_entry.revision = "filerev"
+ a_entry.executable = True
+ a_entry.text_sha1 = "ffff"
+ a_entry.text_size = 1
+ inv.add(a_entry)
+ inv.revision_id = "expectedid"
+ reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
+ delta = [(None, "A", "A-id", a_entry)]
+ new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
+ # new_inv should be the same as reference_inv.
+ self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
+ self.assertEqual(reference_inv.root_id, new_inv.root_id)
+ self.assertEqual(reference_inv.id_to_entry._root_node._key,
+ new_inv.id_to_entry._root_node._key)
More information about the bazaar-commits
mailing list