Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dulwich/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
14 changes: 9 additions & 5 deletions dulwich/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,9 +1605,13 @@ 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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the ZERO_SHA constant

):
raise ChecksumMismatch(self.sha1.hexdigest(), sha_to_hex(stored))

def close(self):
Expand Down Expand Up @@ -2485,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:
Expand Down
Binary file added testdata/indexes/index_skip_hash
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def test_len(self) -> 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)],
Expand Down
6 changes: 3 additions & 3 deletions tests/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading