From dd6f703ea0b67298a5de042b1dde0d110a864f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20R=C3=BCegg?= Date: Tue, 17 Dec 2024 13:45:52 +0100 Subject: [PATCH 1/2] Support skip hash --- dulwich/index.py | 2 +- dulwich/pack.py | 5 +++-- testdata/indexes/index_skip_hash | Bin 0 -> 104 bytes tests/test_index.py | 3 +++ 4 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 testdata/indexes/index_skip_hash diff --git a/dulwich/index.py b/dulwich/index.py index 8e6f4aa89..f1fb49795 100644 --- a/dulwich/index.py +++ b/dulwich/index.py @@ -451,7 +451,7 @@ def read(self) -> None: 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() diff --git a/dulwich/pack.py b/dulwich/pack.py index d576d0cd5..1ec954fa7 100644 --- a/dulwich/pack.py +++ b/dulwich/pack.py @@ -1605,9 +1605,10 @@ def read(self, num=None): 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): diff --git a/testdata/indexes/index_skip_hash b/testdata/indexes/index_skip_hash new file mode 100644 index 0000000000000000000000000000000000000000..76c79a2e16ea1969b810e0e181a8e88f9cd918a7 GIT binary patch literal 104 zcmZ?q402{*U|<4bM$f1XbAU7hjAmfqU}CULu3=zkT*AP>`~rd*7@p02G<(y None: 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)], From ec0d73d7e28deb41e105547869688827e1ee28cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20R=C3=BCegg?= Date: Tue, 4 Feb 2025 09:50:28 +0100 Subject: [PATCH 2/2] Formatting --- dulwich/pack.py | 11 +++++++---- tests/test_pack.py | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/dulwich/pack.py b/dulwich/pack.py index be172e87d..fc1544b4f 100644 --- a/dulwich/pack.py +++ b/dulwich/pack.py @@ -1608,7 +1608,10 @@ def read(self, num=None): def check_sha(self, allow_empty: bool = False) -> None: stored = self.f.read(20) # 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'): + 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): @@ -2486,9 +2489,9 @@ def __iter__(self): def check_length_and_checksum(self) -> None: """Sanity check the length and checksum of the pack index and data.""" - assert len(self.index) == len(self.data), ( - f"Length mismatch: {len(self.index)} (index) != {len(self.data)} (data)" - ) + assert len(self.index) == len( + self.data + ), f"Length mismatch: {len(self.index)} (index) != {len(self.data)} (data)" idx_stored_checksum = self.index.get_pack_checksum() data_stored_checksum = self.data.get_stored_checksum() if idx_stored_checksum != data_stored_checksum: diff --git a/tests/test_pack.py b/tests/test_pack.py index 7b0097af9..68f2773d6 100644 --- a/tests/test_pack.py +++ b/tests/test_pack.py @@ -1001,9 +1001,9 @@ def _result(self, unpacked): ) def _resolve_object(self, offset, pack_type_num, base_chunks): - assert offset not in self._unpacked_offsets, ( - f"Attempted to re-inflate offset {offset}" - ) + assert ( + offset not in self._unpacked_offsets + ), f"Attempted to re-inflate offset {offset}" self._unpacked_offsets.add(offset) return super()._resolve_object(offset, pack_type_num, base_chunks)