Skip to content

support sha256 zero oid in pre-push hook#3694

Closed
D0NMEGA wants to merge 1 commit into
pre-commit:mainfrom
D0NMEGA:warmup/sha256-zero-oid
Closed

support sha256 zero oid in pre-push hook#3694
D0NMEGA wants to merge 1 commit into
pre-commit:mainfrom
D0NMEGA:warmup/sha256-zero-oid

Conversation

@D0NMEGA
Copy link
Copy Markdown

@D0NMEGA D0NMEGA commented May 18, 2026

Resolves #3664.

The pre-push hook compared local_sha and remote_sha against a hardcoded 40-char Z40 sentinel. On sha256 object-format repositories git's pre-push protocol emits 64-char zero oids instead, so the deletion-only and new-branch short-circuits never fired. git push origin --delete <branch> fell through to a git diff <old>...<64-zeros> invocation that produced fatal: Invalid revision range and pre-commit exited non-zero.

Replace the equality comparisons with an _is_zero_oid predicate that accepts all-zero oids of either supported length (40 or 64). Z40 stays around for backward compatibility with the existing tests.

Reproducer from the issue body confirmed locally on git 2.53; added test_run_ns_pre_push_sha256_deleting_branch which fails on main and passes with this change. Also added test_run_ns_pre_push_sha256_new_branch for forward compatibility on the creation path. Unit tests for the new _is_zero_oid predicate cover both supported lengths and a parametrized set of rejection cases. The new SHA-256 fixture skips with pytest.skip if the running git lacks --object-format=sha256 support, keeping the test backward compatible with git < 2.29.

AI assistance (Claude) was used while drafting parts of this change. The diff was reviewed manually before submission.

Resolves pre-commit#3664.

The pre-push hook compared local_sha and remote_sha against a hardcoded
40-char Z40 sentinel. On sha256 object-format repositories git's pre-push
protocol emits 64-char zero oids instead, so the deletion-only and new-branch
short-circuits never fired. `git push origin --delete <branch>` fell through
to a `git diff <old>...<64-zeros>` invocation that produced
`fatal: Invalid revision range` and pre-commit exited non-zero.

Replace the equality comparisons with an _is_zero_oid predicate that accepts
all-zero oids of either supported length (40 or 64). Z40 stays around for
backward compatibility with existing tests and any external importers.
Copilot AI review requested due to automatic review settings May 18, 2026 07:58
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the pre-push hook logic to correctly recognize “zero object IDs” in both SHA-1 and SHA-256 object-format repositories, preventing invalid git diff ranges during branch deletion / creation in SHA-256 repos (fixes #3664).

Changes:

  • Add _is_zero_oid() helper to detect all-zero OIDs of length 40 (SHA-1) or 64 (SHA-256).
  • Use _is_zero_oid() in the pre-push parsing logic instead of hardcoded Z40 equality checks.
  • Add unit/regression tests covering _is_zero_oid() behavior and SHA-256 pre-push scenarios (new branch + deleting branch), with a compatibility skip for older git versions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pre_commit/commands/hook_impl.py Introduces _is_zero_oid() and updates pre-push zero-OID handling to support SHA-256 repos.
tests/commands/hook_impl_test.py Adds predicate unit tests and SHA-256 regression tests with a skip when git lacks --object-format=sha256.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@asottile
Copy link
Copy Markdown
Member

we don't accept ai slop contributions

@asottile asottile closed this May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

3 participants