فهرست منبع

Split up get_stored_checksums.

Jelmer Vernooij 16 سال پیش
والد
کامیت
d369aec537
2فایلهای تغییر یافته به همراه30 افزوده شده و 14 حذف شده
  1. 10 6
      dulwich/pack.py
  2. 20 8
      dulwich/tests/test_pack.py

+ 10 - 6
dulwich/pack.py

@@ -272,7 +272,7 @@ class PackIndex(object):
   
     def check(self):
         """Check that the stored checksum matches the actual checksum."""
-        return self.calculate_checksum() == self.get_stored_checksums()[1]
+        return self.calculate_checksum() == self.get_stored_checksum()
   
     def calculate_checksum(self):
         f = open(self._filename, 'r')
@@ -280,11 +280,14 @@ class PackIndex(object):
             return make_sha(self._contents[:-20]).digest()
         finally:
             f.close()
+
+    def get_pack_checksum(self):
+        """Return the SHA1 checksum stored for the corresponding packfile."""
+        return str(self._contents[-40:-20])
   
-    def get_stored_checksums(self):
-        """Return the SHA1 checksums stored for the corresponding packfile and 
-        this header file itself."""
-        return str(self._contents[-40:-20]), str(self._contents[-20:])
+    def get_stored_checksum(self):
+        """Return the SHA1 checksum stored for this index."""
+        return str(self._contents[-20:])
   
     def object_index(self, sha):
         """Return the index in to the corresponding packfile for the object.
@@ -815,6 +818,7 @@ class Pack(object):
         self._idx = None
 
     def name(self):
+        """The SHA over the SHAs of the objects in this pack."""
         return self.idx.objects_sha1()
 
     @property
@@ -822,7 +826,7 @@ class Pack(object):
         if self._data is None:
             self._data = PackData(self._data_path)
             assert len(self.idx) == len(self._data)
-            idx_stored_checksum = self.idx.get_stored_checksums()[0]
+            idx_stored_checksum = self.idx.get_pack_checksum()
             data_stored_checksum = self._data.get_stored_checksum()
             if idx_stored_checksum != data_stored_checksum:
                 raise ChecksumMismatch(sha_to_hex(idx_stored_checksum), 

+ 20 - 8
dulwich/tests/test_pack.py

@@ -77,8 +77,8 @@ class PackIndexTests(PackTests):
   
     def test_get_stored_checksum(self):
         p = self.get_pack_index(pack1_sha)
-        self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksums()[1]))
-        self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_stored_checksums()[0]))
+        self.assertEquals("\xf2\x84\x8e*\xd1o2\x9a\xe1\xc9.;\x95\xe9\x18\x88\xda\xa5\xbd\x01", str(p.get_stored_checksum()))
+        self.assertEquals( 'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7' , str(p.get_pack_checksum()))
   
     def test_index_check(self):
         p = self.get_pack_index(pack1_sha)
@@ -188,14 +188,26 @@ class TestPack(PackTests):
         self.assertEqual(obj.sha().hexdigest(), commit_sha)
 
     def test_copy(self):
-        p = self.get_pack(pack1_sha)
-        write_pack("Elch", [(x, "") for x in p.iterobjects()], len(p))
-        self.assertEquals(p, Pack("Elch"))
+        origpack = self.get_pack(pack1_sha)
+        self.assertEquals(True, origpack.idx.check())
+        write_pack("Elch", [(x, "") for x in origpack.iterobjects()], 
+            len(origpack))
+        newpack = Pack("Elch")
+        self.assertEquals(origpack, newpack)
+        self.assertEquals(True, newpack.idx.check())
+        self.assertEquals(origpack.name(), newpack.name())
+        self.assertEquals(origpack.idx.get_pack_checksum(), 
+                          newpack.idx.get_pack_checksum())
+        
+        self.assertTrue(
+                (origpack.idx.version != newpack.idx.version) or
+                (origpack.idx.get_stored_checksum() == newpack.idx.get_stored_checksum()))
 
     def test_commit_obj(self):
         p = self.get_pack(pack1_sha)
         commit = p[commit_sha]
-        self.assertEquals("James Westby <jw+debian@jameswestby.net>", commit.author)
+        self.assertEquals("James Westby <jw+debian@jameswestby.net>",
+            commit.author)
         self.assertEquals([], commit.parents)
 
     def test_name(self):
@@ -219,7 +231,7 @@ class BaseTestPackIndexWriting(object):
         self._write_fn("empty.idx", [], pack_checksum)
         idx = PackIndex("empty.idx")
         self.assertTrue(idx.check())
-        self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
+        self.assertEquals(idx.get_pack_checksum(), pack_checksum)
         self.assertEquals(0, len(idx))
 
     def test_single(self):
@@ -230,7 +242,7 @@ class BaseTestPackIndexWriting(object):
         idx = PackIndex("single.idx")
         self.assertEquals(idx.version, self._expected_version)
         self.assertTrue(idx.check())
-        self.assertEquals(idx.get_stored_checksums()[0], pack_checksum)
+        self.assertEquals(idx.get_pack_checksum(), pack_checksum)
         self.assertEquals(1, len(idx))
         actual_entries = list(idx.iterentries())
         self.assertEquals(len(my_entries), len(actual_entries))