[merge] add tests, docs for set_merge_modified; add _must_be_locked

Martin Pool mbp at canonical.com
Mon Feb 26 04:37:17 GMT 2007


As the subject says.

_put_rio should always be called from inside something that holds a
lock, so doesn't need to check or take one.  For things where the caller
is meant to know what they're doing, and for internal methods, it seems
better just to require the caller to take the lock.

-- 
Martin
-------------- next part --------------
=== modified file 'bzrlib/tests/workingtree_implementations/test_workingtree.py'
--- bzrlib/tests/workingtree_implementations/test_workingtree.py	2007-02-17 01:42:57 +0000
+++ bzrlib/tests/workingtree_implementations/test_workingtree.py	2007-02-26 04:31:06 +0000
@@ -548,11 +548,29 @@
         self.assertEqual(master_tree.branch.revision_history(),
             tree.branch.revision_history())
 
-    def test_merge_modified(self):
+    def test_merge_modified_detects_corruption(self):
+        # FIXME: This doesn't really test that it works; also this is not
+        # implementation-independent. mbp 20070226
         tree = self.make_branch_and_tree('master')
         tree._control_files.put('merge-hashes', StringIO('asdfasdf'))
         self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
 
+    def test_merge_modified(self):
+        # merge_modified stores a map from file id to hash
+        tree = self.make_branch_and_tree('tree')
+        d = {'file-id': osutils.sha_string('hello')}
+        self.build_tree_contents([('tree/somefile', 'hello')])
+        tree.lock_write()
+        try:
+            tree.add(['somefile'], ['file-id'])
+            tree.set_merge_modified(d)
+            mm = tree.merge_modified()
+            self.assertEquals(mm, d)
+        finally:
+            tree.unlock()
+        mm = tree.merge_modified()
+        self.assertEquals(mm, d)
+
     def test_conflicts(self):
         from bzrlib.tests.test_conflicts import example_conflicts
         tree = self.make_branch_and_tree('master')

=== modified file 'bzrlib/workingtree.py'
--- bzrlib/workingtree.py	2007-02-18 00:22:24 +0000
+++ bzrlib/workingtree.py	2007-02-26 04:34:22 +0000
@@ -784,8 +784,8 @@
                 yield Stanza(file_id=file_id.decode('utf8'), hash=hash)
         self._put_rio('merge-hashes', iter_stanzas(), MERGE_MODIFIED_HEADER_1)
 
-    @needs_tree_write_lock
     def _put_rio(self, filename, stanzas, header):
+        self._must_be_locked()
         my_file = rio_file(stanzas, header)
         self._control_files.put(filename, my_file)
 
@@ -836,6 +836,15 @@
 
     @needs_read_lock
     def merge_modified(self):
+        """Return a dictionary of files modified by a merge.
+
+        The list is initialized by WorkingTree.set_merge_modified, which is 
+        typically called after we make some automatic updates to the tree
+        because of a merge.
+
+        This returns a map of file_id->sha1, containing only files which are
+        still in the working inventory and have that text hash.
+        """
         try:
             hashfile = self._control_files.get('merge-hashes')
         except errors.NoSuchFile:
@@ -850,9 +859,9 @@
             file_id = s.get("file_id")
             if file_id not in self.inventory:
                 continue
-            hash = s.get("hash")
-            if hash == self.get_file_sha1(file_id):
-                merge_hashes[file_id] = hash
+            text_hash = s.get("hash")
+            if text_hash == self.get_file_sha1(file_id):
+                merge_hashes[file_id] = text_hash
         return merge_hashes
 
     @needs_write_lock
@@ -1515,6 +1524,10 @@
     def is_locked(self):
         return self._control_files.is_locked()
 
+    def _must_be_locked(self):
+        if not self.is_locked():
+            raise errors.ObjectNotLocked(self)
+
     def lock_read(self):
         """See Branch.lock_read, and WorkingTree.unlock."""
         self.branch.lock_read()

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20070226/04f6c083/attachment-0001.pgp 


More information about the bazaar mailing list