support sha256 zero oid in pre-push hook#3694
Closed
D0NMEGA wants to merge 1 commit into
Closed
Conversation
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.
There was a problem hiding this comment.
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 hardcodedZ40equality 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.
Member
|
we don't accept ai slop contributions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #3664.
The pre-push hook compared
local_shaandremote_shaagainst a hardcoded 40-char Z40 sentinel. Onsha256object-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 agit diff <old>...<64-zeros>invocation that producedfatal: Invalid revision rangeand pre-commit exited non-zero.Replace the equality comparisons with an
_is_zero_oidpredicate that accepts all-zero oids of either supported length (40 or 64).Z40stays 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_branchwhich fails onmainand passes with this change. Also addedtest_run_ns_pre_push_sha256_new_branchfor forward compatibility on the creation path. Unit tests for the new_is_zero_oidpredicate cover both supported lengths and a parametrized set of rejection cases. The new SHA-256 fixture skips withpytest.skipif the running git lacks--object-format=sha256support, 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.