Rev 2422: Make sure adding a duplicate file_id raises DuplicateFileId. in http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate

John Arbash Meinel john at arbash-meinel.com
Mon Feb 26 22:09:22 GMT 2007


At http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate

------------------------------------------------------------
revno: 2422
revision-id: john at arbash-meinel.com-20070226220814-i6a95mlsdsv3yrf8
parent: john at arbash-meinel.com-20070226215104-1bv5mq6ac140jc15
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dirstate
timestamp: Mon 2007-02-26 16:08:14 -0600
message:
  Make sure adding a duplicate file_id raises DuplicateFileId.
  This adds an explicit test for it, updates Inventory to raise the right error,
  and updates Dirstate.add() to also check for a duplicated file_id.
modified:
  bzrlib/dirstate.py             dirstate.py-20060728012006-d6mvoihjb3je9peu-1
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/inventory.py            inventory.py-20050309040759-6648b84ca2005b37
  bzrlib/tests/test_errors.py    test_errors.py-20060210110251-41aba2deddf936a8
  bzrlib/tests/workingtree_implementations/test_add.py test_add.py-20070226165239-4vo178spkrnhavc7-1
-------------- next part --------------
=== modified file 'bzrlib/dirstate.py'
--- a/bzrlib/dirstate.py	2007-02-26 21:17:03 +0000
+++ b/bzrlib/dirstate.py	2007-02-26 22:08:14 +0000
@@ -320,6 +320,13 @@
         dirname, basename = osutils.split(utf8path)
         assert file_id.__class__ == str, \
             "must be a utf8 file_id not %s" % (type(file_id))
+        # Make sure the file_id does not exist in this tree
+        file_id_entry = self._get_entry(0, fileid_utf8=file_id)
+        if file_id_entry != (None, None):
+            path = osutils.pathjoin(file_id_entry[0][0], file_id_entry[0][1])
+            kind = DirState._minikind_to_kind[file_id_entry[1][0][0]]
+            info = '%s:%s' % (kind, path)
+            raise errors.DuplicateFileId(file_id, info)
         entry_key = (dirname, basename, file_id)
         self._read_dirblocks_if_needed()
         block_index, present = self._find_block_index_from_key(entry_key)

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2007-02-25 22:50:34 +0000
+++ b/bzrlib/errors.py	2007-02-26 22:08:14 +0000
@@ -1377,6 +1377,16 @@
         self.file_id = file_id
 
 
+class DuplicateFileId(BzrError):
+
+    _fmt = "File id {%(file_id)s} already exists in inventory as %(entry)s"
+
+    def __init__(self, file_id, entry):
+        BzrError.__init__(self)
+        self.file_id = file_id
+        self.entry = entry
+
+
 class DuplicateKey(BzrError):
 
     _fmt = "Key %(key)s is already present in map"

=== modified file 'bzrlib/inventory.py'
--- a/bzrlib/inventory.py	2007-02-22 15:04:35 +0000
+++ b/bzrlib/inventory.py	2007-02-26 22:08:14 +0000
@@ -1095,7 +1095,8 @@
         Returns the new entry object.
         """
         if entry.file_id in self._byid:
-            raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
+            raise errors.DuplicateFileId(entry.file_id,
+                                         self._byid[entry.file_id])
 
         if entry.parent_id is None:
             assert self.root is None and len(self._byid) == 0

=== modified file 'bzrlib/tests/test_errors.py'
--- a/bzrlib/tests/test_errors.py	2007-02-25 22:50:34 +0000
+++ b/bzrlib/tests/test_errors.py	2007-02-26 22:08:14 +0000
@@ -30,6 +30,11 @@
 
 class TestErrors(TestCaseWithTransport):
 
+    def test_duplicate_file_id(self):
+        error = errors.DuplicateFileId('a_file_id', 'foo')
+        self.assertEqualDiff('File id {a_file_id} already exists in inventory'
+                             ' as foo', str(error))
+
     def test_inventory_modified(self):
         error = errors.InventoryModified("a tree to be repred")
         self.assertEqualDiff("The current inventory for the tree 'a tree to "

=== modified file 'bzrlib/tests/workingtree_implementations/test_add.py'
--- a/bzrlib/tests/workingtree_implementations/test_add.py	2007-02-26 16:53:16 +0000
+++ b/bzrlib/tests/workingtree_implementations/test_add.py	2007-02-26 22:08:14 +0000
@@ -51,6 +51,17 @@
 
         self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
 
+    def test_add_existing_id(self):
+        """Adding an entry with a pre-existing id raises DuplicateFileId"""
+        tree = self.make_branch_and_tree('.')
+        self.build_tree(['a', 'b'])
+        tree.add(['a'], ['an-id'])
+        self.assertRaises(errors.DuplicateFileId,
+                          tree.add, ['b'], ['an-id'])
+        root_id = tree.get_root_id()
+        # And the entry should not have been added.
+        self.assertTreeLayout([('', root_id), ('a', 'an-id')], tree)
+
     def test_add_one_list(self):
         tree = self.make_branch_and_tree('.')
         self.build_tree(['one'])



More information about the bazaar-commits mailing list