Browse Source

add support for the git option "index.skipHash" (#1488)

Here is the comparable implementation in another git library for adding
support for the same feature:
https://github.com/libgit2/libgit2/pull/6738/files

---------

Co-authored-by: Jan Rüegg <jrueegg@apple.com>
Jan Rüegg 1 month ago
parent
commit
84c1d51a91
4 changed files with 10 additions and 3 deletions
  1. 1 1
      dulwich/index.py
  2. 6 2
      dulwich/pack.py
  3. BIN
      testdata/indexes/index_skip_hash
  4. 3 0
      tests/test_index.py

+ 1 - 1
dulwich/index.py

@@ -451,7 +451,7 @@ class Index:
             self.update(read_index_dict(f))
             # FIXME: Additional data?
             f.read(os.path.getsize(self._filename) - f.tell() - 20)
-            f.check_sha()
+            f.check_sha(allow_empty=True)
         finally:
             f.close()
 

+ 6 - 2
dulwich/pack.py

@@ -1605,9 +1605,13 @@ class SHA1Reader:
         self.sha1.update(data)
         return data
 
-    def check_sha(self) -> None:
+    def check_sha(self, allow_empty: bool = False) -> None:
         stored = self.f.read(20)
-        if stored != self.sha1.digest():
+        # If git option index.skipHash is set the index will be empty
+        if stored != self.sha1.digest() and (
+            not allow_empty
+            or sha_to_hex(stored) != b"0000000000000000000000000000000000000000"
+        ):
             raise ChecksumMismatch(self.sha1.hexdigest(), sha_to_hex(stored))
 
     def close(self):

BIN
testdata/indexes/index_skip_hash


+ 3 - 0
tests/test_index.py

@@ -84,6 +84,9 @@ class SimpleIndexTestCase(IndexTestCase):
     def test_iter(self) -> None:
         self.assertEqual([b"bla"], list(self.get_simple_index("index")))
 
+    def test_iter_skip_hash(self) -> None:
+        self.assertEqual([b"bla"], list(self.get_simple_index("index_skip_hash")))
+
     def test_iterobjects(self) -> None:
         self.assertEqual(
             [(b"bla", b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 33188)],