Visitar URL original
Detect push deletions on SHA-256 repos by 00200200 · Pull Request #3760 · pre-commit/pre-commit · GitHub
Skip to content
Closed
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
11 changes: 9 additions & 2 deletions pre_commit/commands/hook_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from pre_commit.store import Store

Z40 = '0' * 40
Z64 = '0' * 64
NULL_OIDS = frozenset((Z40, Z64))


def _is_null_oid(sha: str) -> bool:
"""True for the all-zero OID (SHA-1 or SHA-256 object format)."""
return sha in NULL_OIDS


def _run_legacy(
Expand Down Expand Up @@ -128,9 +135,9 @@ def _pre_push_ns(
for line in stdin.decode().splitlines():
parts = line.rsplit(maxsplit=3)
local_branch, local_sha, remote_branch, remote_sha = parts
if local_sha == Z40:
if _is_null_oid(local_sha):
continue
elif remote_sha != Z40 and _rev_exists(remote_sha):
elif not _is_null_oid(remote_sha) and _rev_exists(remote_sha):
return _ns(
'pre-push', color,
from_ref=remote_sha, to_ref=local_sha,
Expand Down
7 changes: 7 additions & 0 deletions tests/commands/hook_impl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,10 @@ def test_hook_impl_main_runs_hooks(cap_out, tempdir_factory, store):
Block if "DO NOT COMMIT" is found....................(no files to check)Skipped
'''
assert cap_out.get() == expected


def test_null_oid_accepts_sha1_and_sha256_zeros():
assert hook_impl._is_null_oid('0' * 40)
assert hook_impl._is_null_oid('0' * 64)
assert not hook_impl._is_null_oid('0' * 39)
assert not hook_impl._is_null_oid('a' * 40)
Loading