Skip to content

fix(pre-push): treat SHA-256 all-zero OIDs as null object#3724

Closed
Solaris-star wants to merge 1 commit into
pre-commit:mainfrom
Solaris-star:fix/3664-sha256-zero-oid
Closed

fix(pre-push): treat SHA-256 all-zero OIDs as null object#3724
Solaris-star wants to merge 1 commit into
pre-commit:mainfrom
Solaris-star:fix/3664-sha256-zero-oid

Conversation

@Solaris-star

Copy link
Copy Markdown

Description

pre_commit/commands/hook_impl.py hardcodes:

Z40 = '0' * 40

and compares local_sha / remote_sha against it to detect deletion-only pushes and new branches.

On repositories initialized with --object-format=sha256, git's pre-push protocol emits 64-character zero OIDs. The equality never matches, deletion short-circuit never fires, and pre-commit ends up running git rev-list / diff against invalid ranges, exiting non-zero — so git push origin --delete <branch> is refused.

Fix

  • Add _is_zero_oid(sha) that accepts any non-empty all-zero hex string (SHA-1 or SHA-256).
  • Use it for both local and remote zero checks in _pre_push_ns.
  • Keep Z40 for existing tests; add Z64 + regression tests for SHA-256 deletion stdin.

Tests

pytest tests/commands/hook_impl_test.py::test_run_ns_pre_push_deleting_branch \
  tests/commands/hook_impl_test.py::test_run_ns_pre_push_deleting_branch_sha256_zero_oid \
  tests/commands/hook_impl_test.py::test_is_zero_oid \
  tests/commands/hook_impl_test.py::test_run_ns_pre_push_new_branch -q
# 4 passed

Linked Issue

Closes #3664

Z40 only matched SHA-1 zero OIDs. On object-format=sha256 repos, git
emits 64-char zero OIDs for deletions/new branches, so pre-push fell
through into invalid rev-list ranges and blocked git push --delete.

Treat any all-zero hex string as the null OID.

Fixes pre-commit#3664

Signed-off-by: Solaris-star <820622658@qq.com>
@asottile asottile closed this Jul 21, 2026
@pre-commit pre-commit locked as spam and limited conversation to collaborators Jul 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Z40 = '0' * 40 hardcoding breaks git push --delete on SHA-256 repos

2 participants