Rev 3166: Start bringing up parsing support. in http://people.ubuntu.com/~robertc/baz2.0/inventory.journalled

Robert Collins robertc at robertcollins.net
Thu Jan 3 22:08:23 GMT 2008


At http://people.ubuntu.com/~robertc/baz2.0/inventory.journalled

------------------------------------------------------------
revno: 3166
revision-id:robertc at robertcollins.net-20080103220818-ivwzayoouj30tlvn
parent: robertc at robertcollins.net-20080103213744-t4j7nsulqr2enn94
committer: Robert Collins <robertc at robertcollins.net>
branch nick: inventory.journalled
timestamp: Fri 2008-01-04 09:08:18 +1100
message:
  Start bringing up parsing support.
modified:
  bzrlib/journalled_inventory.py journalled_inventory-20080103020931-0ht5n40kwc0p7fy1-1
  bzrlib/tests/test_journalled_inv.py test_journalled_inv.-20080103012121-ny2w9slze5jgty8i-1
=== modified file 'bzrlib/journalled_inventory.py'
--- a/bzrlib/journalled_inventory.py	2008-01-03 21:37:44 +0000
+++ b/bzrlib/journalled_inventory.py	2008-01-03 22:08:18 +0000
@@ -70,6 +70,20 @@
     return "tree %s" % tree_revision
 
 
+class _JournalEntry(object):
+    """An individual entry in a journalled inventory."""
+
+    def __init__(self, parent_revision, tuples):
+        """Create a _JournalEntry.
+
+        :param parent_revision: The parent revision this entry is written
+            against. null: indicates the start of a new delta chain.
+        :param tuples: The text-split items in this entry.
+        """
+        self.parent_revision = parent_revision
+        self.tuples = tuples
+
+
 class EntryAccess(object):
     """Provide access to named bytesequences of the journal entries."""
 
@@ -143,3 +157,18 @@
             content = self._entry_to_content[entry.kind](entry)
         return ("%s %s %s %s %s\n" %
             (newpath_utf8, file_id, parent_id, last_modified, content))
+
+    def parse_text_bytes(self, bytes):
+        """Parse the text bytes of a journal entry.
+
+        :param bytes: The bytes to parse. This can be obtained by calling
+            delta_to_lines and then doing ''.join(delta_lines).
+        :return: A _JournalEntry for the bytes.
+        """
+        lines = bytes.split('\n')[:-1] # discard the last empty line
+        if not lines or lines[0] != 'format: %s' % InventoryJournal.FORMAT_1:
+            raise errors.BzrError('unknown format %r' % lines[0:1])
+        if len(lines) < 2 or not lines[1].startswith('parent: '):
+            raise errors.BzrError('missing parent: marker')
+        parent_id = lines[1][8:]
+        return _JournalEntry(parent_id, [])

=== modified file 'bzrlib/tests/test_journalled_inv.py'
--- a/bzrlib/tests/test_journalled_inv.py	2008-01-03 21:37:44 +0000
+++ b/bzrlib/tests/test_journalled_inv.py	2008-01-03 22:08:18 +0000
@@ -34,26 +34,29 @@
 empty_lines = """format: bzr journalled inventory v1 (bzr 1.1)
 parent: null:
 """
-empty_lines_validator = ""
 
 root_only_lines = """format: bzr journalled inventory v1 (bzr 1.1)
 parent: null:
-/ TREE_ROOT  a at e\xe5ample.com--2004 dir
+/ an-id  a at e\xe5ample.com--2004 dir
 """
-root_only_lines_validator = ""
 
 root_only_unversioned = """format: bzr journalled inventory v1 (bzr 1.1)
 parent: null:
 / TREE_ROOT  null: dir
 """
-root_only_lines_validator = ""
-
-reference_lines = """format: bzr journalled inventory v1 (bzr 1.1)
-parent: null:
-/ TREE_ROOT  a at e\xe5ample.com--2004 dir
-/foo id TREE_ROOT changed tree subtree-version
-"""
-root_only_lines_validator = ""
+
+reference_lines = """format: bzr journalled inventory v1 (bzr 1.1)
+parent: null:
+/ TREE_ROOT  a at e\xe5ample.com--2004 dir
+/foo id TREE_ROOT changed tree subtree-version
+"""
+
+reference_lines = """format: bzr journalled inventory v1 (bzr 1.1)
+parent: null:
+/ TREE_ROOT  a at e\xe5ample.com--2004 dir
+/foo id TREE_ROOT changed tree subtree-version
+"""
+
 
 class TestSerializer(TestCase):
     """Test journalled inventory serialisation."""
@@ -88,7 +91,7 @@
     def test_root_only_to_lines(self):
         old_inv = Inventory(None)
         new_inv = Inventory(None)
-        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
+        root = new_inv.make_entry('directory', '', None, 'an-id')
         root.revision = 'a at e\xe5ample.com--2004'
         new_inv.add(root)
         delta = self.make_inv_delta(old_inv, new_inv)
@@ -97,6 +100,17 @@
         self.assertEqual(StringIO(root_only_lines).readlines(),
             journal.delta_to_lines(NULL_REVISION, delta))
 
+    def test_unversioned_root(self):
+        old_inv = Inventory(None)
+        new_inv = Inventory(None)
+        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
+        new_inv.add(root)
+        delta = self.make_inv_delta(old_inv, new_inv)
+        journal = journalled_inventory.InventoryJournal(versioned_root=False,
+            tree_references=False)
+        self.assertEqual(StringIO(root_only_unversioned).readlines(),
+            journal.delta_to_lines(NULL_REVISION, delta))
+
     def test_unversioned_non_root_errors(self):
         old_inv = Inventory(None)
         new_inv = Inventory(None)
@@ -199,6 +213,31 @@
         self.assertEqual(StringIO(reference_lines).readlines(),
             journal.delta_to_lines(NULL_REVISION, delta))
 
+    def test_parse_no_bytes(self):
+        journal = journalled_inventory.InventoryJournal(versioned_root=True,
+            tree_references=True)
+        self.assertRaises(errors.BzrError, journal.parse_text_bytes, '')
+
+    def test_parse_bad_format(self):
+        journal = journalled_inventory.InventoryJournal(versioned_root=True,
+            tree_references=True)
+        self.assertRaises(errors.BzrError,
+            journal.parse_text_bytes, 'format: foo\n')
+
+    def test_parse_no_parent(self):
+        journal = journalled_inventory.InventoryJournal(versioned_root=True,
+            tree_references=True)
+        self.assertRaises(errors.BzrError,
+            journal.parse_text_bytes,
+            'format: bzr journalled inventory v1 (bzr 1.1)\n')
+
+    def test_parse_empty(self):
+        journal = journalled_inventory.InventoryJournal(versioned_root=True,
+            tree_references=True)
+        journal_entry = journal.parse_text_bytes(empty_lines)
+        self.assertEqual(NULL_REVISION, journal_entry.parent_revision)
+        self.assertEqual([], journal_entry.tuples)
+
 
 class TestContent(TestCase):
 



More information about the bazaar-commits mailing list