Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions .githooks/pre-push
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Gate auto-commit on pushed ref, not only matching SHA

current_head_in_push is flipped when local_sha == current_head, but that is also true when pushing a different ref that happens to point at the same commit (for example, pushing feature that shares the current tip, or a lightweight tag at HEAD). In that case the hook takes the auto-commit path and creates chore: apply cargo fmt on 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 👍 / 👎.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
Loading