Skip to content

Commit 3a71e07

Browse files
committed
fix: handle SHA-256 zero OIDs in pre-push hook (closes #3664)
1 parent edad968 commit 3a71e07

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

pre_commit/commands/hook_impl.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
from pre_commit.parse_shebang import normalize_cmd
1212
from pre_commit.store import Store
1313

14+
15+
def _is_zero_oid(oid: str) -> bool:
16+
"""Returns True if oid is an all-zero OID of a supported git hash length."""
17+
return len(oid) in (40, 64) and set(oid) == {'0'}
18+
19+
1420
Z40 = '0' * 40
1521

1622

@@ -128,9 +134,9 @@ def _pre_push_ns(
128134
for line in stdin.decode().splitlines():
129135
parts = line.rsplit(maxsplit=3)
130136
local_branch, local_sha, remote_branch, remote_sha = parts
131-
if local_sha == Z40:
137+
if _is_zero_oid(local_sha):
132138
continue
133-
elif remote_sha != Z40 and _rev_exists(remote_sha):
139+
elif not _is_zero_oid(remote_sha) and _rev_exists(remote_sha):
134140
return _ns(
135141
'pre-push', color,
136142
from_ref=remote_sha, to_ref=local_sha,

tests/commands/hook_impl_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ def test_run_ns_pre_push_deleting_branch(push_example):
347347
assert ns is None
348348

349349

350+
def test_run_ns_pre_push_deleting_branch_sha256(push_example):
351+
"""Regression test for #3664 — deletion push with SHA-256 zero OID."""
352+
src, src_head, clone, _ = push_example
353+
354+
with cwd(clone):
355+
args = ('origin', src)
356+
# SHA-256 zero OID (64 chars) instead of SHA-1 zero OID (40 chars)
357+
sha256_zero = '0' * 64
358+
stdin = f'(delete) {sha256_zero} refs/heads/b {src_head}'.encode()
359+
ns = hook_impl._run_ns('pre-push', False, args, stdin)
360+
361+
assert ns is None
362+
363+
350364
def test_hook_impl_main_noop_pre_push(cap_out, store, push_example):
351365
src, src_head, clone, _ = push_example
352366

0 commit comments

Comments
 (0)