Repository navigation
chore: fmt hook — skip delete pushes; gate auto-commit on HEAD-in-push #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,82 @@ | ||
| #!/usr/bin/env bash | ||
| # Pre-push hook: auto-fix fmt drift and commit it as a chore. | ||
| # Pre-push hook: auto-fix fmt drift and commit it as a chore, but ONLY | ||
| # when the push actually includes the current HEAD (so the auto-commit | ||
| # lands on the ref being pushed). Skips entirely on delete-only pushes. | ||
| # | ||
| # Rationale: three fmt-fail CI incidents in the last week. Running | ||
| # `cargo fmt --all --check` locally at push time catches drift before | ||
| # it reaches CI. Auto-fixing + auto-committing means the developer's | ||
| # intent (their original commits) flows unchanged; the fmt fix lands | ||
| # as a distinct chore commit on top, preserving audit clarity. | ||
| # Decision table: | ||
| # Push is delete-only → skip entirely (nothing to fmt-check) | ||
| # Push includes current HEAD → auto-commit fmt fix on top of HEAD | ||
| # Push excludes current HEAD → fmt-check + fail with actionable msg | ||
| # (don't auto-commit to a ref that | ||
| # isn't being pushed; the fix | ||
| # wouldn't reach the remote) | ||
| # | ||
| # Enable via: scripts/install-hooks.sh (sets core.hooksPath .githooks) | ||
|
|
||
| set -e | ||
|
|
||
| # Pre-push should run on a clean tree. Unstaged or staged-but-uncommitted | ||
| # changes indicate the developer hasn't finished the work they want to | ||
| # push; bail rather than silently sweep those up into a fmt commit. | ||
| if ! git diff --quiet || ! git diff --cached --quiet; then | ||
| echo "[pre-push] uncommitted changes in working tree — commit or stash before pushing." | ||
| exit 1 | ||
| ZERO_SHA="0000000000000000000000000000000000000000" | ||
| current_head=$(git rev-parse HEAD 2>/dev/null || echo "") | ||
|
|
||
| pushes_content=0 | ||
| current_head_in_push=0 | ||
|
|
||
| # Pre-push stdin format (one line per ref being pushed): | ||
| # <local_ref> <local_sha> <remote_ref> <remote_sha> | ||
| # All-zero local_sha means delete (no content pushed for this ref). | ||
| while IFS=' ' read -r local_ref local_sha remote_ref remote_sha; do | ||
| if [[ "$local_sha" == "$ZERO_SHA" ]]; then | ||
| continue # delete, no content to check | ||
| fi | ||
| pushes_content=1 | ||
| if [[ "$local_sha" == "$current_head" ]]; then | ||
| current_head_in_push=1 | ||
| fi | ||
| done | ||
|
|
||
| # Delete-only push (e.g., `git push origin --delete branch-name`): skip. | ||
| if [[ "$pushes_content" == "0" ]]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # If auto-commit is in scope (HEAD is being pushed), require a clean tree. | ||
| # Don't silently sweep uncommitted work into a fmt commit. | ||
| if [[ "$current_head_in_push" == "1" ]]; then | ||
| if ! git diff --quiet || ! git diff --cached --quiet; then | ||
| echo "[pre-push] uncommitted changes in working tree — commit or stash before pushing." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| echo "[pre-push] cargo fmt --all --check" | ||
| if cargo fmt --all --check 2>/dev/null; then | ||
| exit 0 # clean, nothing to do | ||
| exit 0 # clean | ||
| fi | ||
|
|
||
| # Fmt drift detected. Two paths: | ||
| if [[ "$current_head_in_push" == "0" ]]; then | ||
| # Push doesn't include current HEAD — auto-committing would land on a | ||
| # ref that isn't being pushed, so the fmt fix wouldn't reach the remote | ||
| # anyway. Fail with an actionable message; let the developer decide. | ||
| echo "" | ||
| echo "[pre-push] fmt drift detected, but the push doesn't include the current HEAD." | ||
| echo "[pre-push] auto-commit would land on a ref that isn't being pushed." | ||
| echo "" | ||
| echo "Either:" | ||
| echo " - switch to the branch you intend to push and push again (hook will auto-fix), or" | ||
| echo " - run 'cargo fmt --all' now, commit, and include the fmt fix in your push." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # HEAD is being pushed → safe to auto-commit on top of it. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BLOCKING: The "HEAD is being pushed -> safe to auto-commit" branch is incorrect because Git snapshots the refs-to-push before pre-push runs, so the new fmt commit is not sent and the hook succeeds with unresolved drift (Fail-Closed). |
||
| echo "[pre-push] fmt drift detected; running cargo fmt --all and committing as chore" | ||
| cargo fmt --all | ||
|
|
||
| # Only stage tracked files; never sweep up untracked files. | ||
| git add -u | ||
|
|
||
| if git diff --cached --quiet; then | ||
| # cargo fmt reported drift via --check but --all produced no diff? | ||
| # cargo fmt --check reported drift but --all produced no diff? | ||
| # Shouldn't happen; exit clean rather than commit nothing. | ||
| exit 0 | ||
| fi | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current_head_in_pushis flipped whenlocal_sha == current_head, but that is also true when pushing a different ref that happens to point at the same commit (for example, pushingfeaturethat shares the current tip, or a lightweight tag at HEAD). In that case the hook takes the auto-commit path and createschore: apply cargo fmton the checked-out branch even though that new commit is not in the pushed refset, which reintroduces the unintended “commit on unrelated branch” behavior this change is meant to prevent.Useful? React with 👍 / 👎.