Rev 2299: Add test for merge_modified in http://sourcefrog.net/bzr/trivial

Martin Pool mbp at sourcefrog.net
Mon Feb 26 04:40:17 GMT 2007


At http://sourcefrog.net/bzr/trivial

------------------------------------------------------------
revno: 2299
revision-id: mbp at sourcefrog.net-20070226044016-hp3hp0yitdt7krph
parent: pqm at pqm.ubuntu.com-20070222144505-5f7551602cad9332
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: trivial
timestamp: Mon 2007-02-26 15:40:16 +1100
message:
  Add test for merge_modified
modified:
  bzrlib/tests/workingtree_implementations/test_workingtree.py test_workingtree.py-20060203003124-817757d3e31444fb
  bzrlib/workingtree.py          workingtree.py-20050511021032-29b6ec0a681e02e3
=== modified file 'bzrlib/tests/workingtree_implementations/test_workingtree.py'
--- a/bzrlib/tests/workingtree_implementations/test_workingtree.py	2007-02-17 01:42:57 +0000
+++ b/bzrlib/tests/workingtree_implementations/test_workingtree.py	2007-02-26 04:40:16 +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'
--- a/bzrlib/workingtree.py	2007-02-18 00:22:24 +0000
+++ b/bzrlib/workingtree.py	2007-02-26 04:40:16 +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()




More information about the bazaar-commits mailing list