Ver Fonte

Cope with pack checksum in index file.

Jelmer Vernooij há 16 anos atrás
pai
commit
b0c7a4335b
2 ficheiros alterados com 14 adições e 8 exclusões
  1. 10 6
      dulwich/pack.py
  2. 4 2
      dulwich/tests/test_pack.py

+ 10 - 6
dulwich/pack.py

@@ -158,18 +158,19 @@ class PackIndex(object):
 
   def check(self):
     """Check that the stored checksum matches the actual checksum."""
-    return self.get_checksum() == self.get_stored_checksum()
+    return self.calculate_checksum() == self.get_stored_checksums()[1]
 
-  def get_checksum(self):
+  def calculate_checksum(self):
     f = open(self._filename, 'r')
     try:
         return hashlib.sha1(self._contents[:-20]).digest()
     finally:
         f.close()
 
-  def get_stored_checksum(self):
-    """Return the SHA1 checksum stored for this header file itself."""
-    return str(self._contents[-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 object_index(self, sha):
     """Return the index in to the corresponding packfile for the object.
@@ -320,11 +321,12 @@ def write_pack(filename, objects):
         f.close()
 
 
-def write_pack_index(filename, entries):
+def write_pack_index(filename, entries, pack_checksum):
     """Write a new pack index file.
 
     :param filename: The filename of the new pack index file.
     :param entries: List of tuples with offset_in_pack and object name (sha).
+    :param pack_checksum: Checksum of the pack file.
     """
     # Sort entries first
     def cmp_entry((offset1, name1), (offset2, name2)):
@@ -343,5 +345,7 @@ def write_pack_index(filename, entries):
         write(struct.pack(">L", fan_out_table[i]))
     for (offset, name) in entries:
         write(struct.pack(">L20s", offset, name))
+    assert len(pack_checksum) == 20
+    f.write(pack_checksum)
     f.write(sha1.digest())
     f.close()

+ 4 - 2
dulwich/tests/test_pack.py

@@ -91,7 +91,8 @@ class TestPackData(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_checksum()))
+    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]))
 
   def test_check(self):
     p = self.get_pack_index(pack1_sha)
@@ -117,5 +118,6 @@ class TestMultiOrd(unittest.TestCase):
 class TestPackIndexWriting(unittest.TestCase):
 
     def test_empty(self):
-        write_pack_index("empty.idx", [])
+        write_pack_index("empty.idx", [], 
+                'r\x19\x80\xe8f\xaf\x9a_\x93\xadgAD\xe1E\x9b\x8b\xa3\xe7\xb7')
         PackIndex("empty.idx").check()