From 267fbf94ef839e061f2367cb76b62df06b3d3101 Mon Sep 17 00:00:00 2001 From: Sean Doherty Date: Sat, 16 May 2026 20:08:05 -0500 Subject: [PATCH] Handle sha256 zero oid in pre-push --- pre_commit/commands/hook_impl.py | 10 ++++++++-- tests/commands/hook_impl_test.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py index 7b806f3b8..c295b8b1e 100644 --- a/pre_commit/commands/hook_impl.py +++ b/pre_commit/commands/hook_impl.py @@ -12,6 +12,8 @@ from pre_commit.store import Store Z40 = '0' * 40 +Z64 = '0' * 64 +ZERO_OIDS = frozenset((Z40, Z64)) def _run_legacy( @@ -117,6 +119,10 @@ def _rev_exists(rev: str) -> bool: return not subprocess.call(('git', 'rev-list', '--quiet', rev)) +def _is_zero_oid(oid: str) -> bool: + return oid in ZERO_OIDS + + def _pre_push_ns( color: bool, args: Sequence[str], @@ -128,9 +134,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_zero_oid(local_sha): continue - elif remote_sha != Z40 and _rev_exists(remote_sha): + elif not _is_zero_oid(remote_sha) and _rev_exists(remote_sha): return _ns( 'pre-push', color, from_ref=remote_sha, to_ref=local_sha, diff --git a/tests/commands/hook_impl_test.py b/tests/commands/hook_impl_test.py index 9aa93af53..3c9a20693 100644 --- a/tests/commands/hook_impl_test.py +++ b/tests/commands/hook_impl_test.py @@ -347,6 +347,17 @@ def test_run_ns_pre_push_deleting_branch(push_example): assert ns is None +def test_run_ns_pre_push_deleting_branch_sha256(push_example): + src, src_head, clone, _ = push_example + + with cwd(clone): + args = ('origin', src) + stdin = f'(delete) {hook_impl.Z64} refs/heads/b {src_head}'.encode() + ns = hook_impl._run_ns('pre-push', False, args, stdin) + + assert ns is None + + def test_hook_impl_main_noop_pre_push(cap_out, store, push_example): src, src_head, clone, _ = push_example