Răsfoiți Sursa

Improve fix for ShaFile.id behaviour after as_raw_chunks.

Jelmer Vernooij 9 ani în urmă
părinte
comite
9451655113
3 a modificat fișierele cu 12 adăugiri și 12 ștergeri
  1. 5 0
      NEWS
  2. 2 6
      dulwich/objects.py
  3. 5 6
      dulwich/tests/test_objects.py

+ 5 - 0
NEWS

@@ -1,5 +1,10 @@
 0.14.0	UNRELEASED
 
+ BUG FIXES
+
+  * Fix ShaFile.id after modification of a copied ShaFile.
+    (Félix Mattrat, Jelmer Vernooij)
+
 0.13.0	2016-04-24
 
  IMPROVEMENTS

+ 2 - 6
dulwich/objects.py

@@ -270,6 +270,7 @@ class ShaFile(object):
         :return: List of strings, not necessarily one per line
         """
         if self._needs_serialization:
+            self._sha = None
             self._chunked_text = self._serialize()
             self._needs_serialization = False
         return self._chunked_text
@@ -463,15 +464,10 @@ class ShaFile(object):
     def copy(self):
         """Create a new copy of this SHA1 object from its raw string"""
         obj_class = object_class(self.get_type())
-        # The id need to be retrieved before calling as_raw_string because
-        # that method can overwrite the flag _needs_serialization and by
-        # side effect the self.id property can return an outdated id.
-        hex_sha_id = self.id
-
         return obj_class.from_raw_string(
             self.get_type(),
             self.as_raw_string(),
-            hex_sha_id)
+            self.id)
 
     @property
     def id(self):

+ 5 - 6
dulwich/tests/test_objects.py

@@ -938,10 +938,8 @@ class TagParseTests(ShaFileCheckTests):
                 self.assertCheckFails(Tag, text)
 
     def test_tree_copy_after_update(self):
-        """Check if the id of the Tree is correctly
-           updated when the tree is copied after being
-           updated
-        """   
+        """Check Tree.id is correctly updated when the tree is copied after updated.
+        """
         shas = []
         tree = Tree()
         shas.append(tree.id)
@@ -950,8 +948,9 @@ class TagParseTests(ShaFileCheckTests):
         shas.append(tree.id)
         shas.append(copied.id)
 
-        self.assertTrue(shas[0] not in shas[1:])
-        self.assertTrue(shas[1] == shas[2])
+        self.assertNotIn(shas[0], shas[1:])
+        self.assertEqual(shas[1], shas[2])
+
 
 class CheckTests(TestCase):