Files
headroom/scripts/install-git-hooks.sh
Kenneth WongandKenneth Wong cbcf8d8d9b fix(scripts): skip ci-precheck in the pre-push hook when a push only deletes refs (#4702)
## Description

The pre-push hook that `make install-git-hooks` writes runs the full
`make ci-precheck` (cargo fmt, clippy and the workspace tests, plus the
Python suite) even when a push only deletes branches. Its loop over the
refs on stdin skips deleted refs with `continue`, but the precheck runs
after the loop regardless, so `git push origin --delete <branch>` waits
several minutes for checks on code it doesn't send.

This PR tracks whether any ref in the push is an update, and exits 0
when none is.

No tracking issue.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `scripts/install-git-hooks.sh` (the generated pre-push hook): set
`has_update=1` for every non-delete ref and `exit 0` after the loop when
no ref was an update. A push that mixes a delete with an update still
runs the precheck.
- The delete check now matches an all-zero sha of any length (`^0+$`),
so SHA-256 repositories (64-zero null id) are also recognised. The old
check compared against exactly 40 zeros.
- Removed the hook comment about gating only "main tracking branches";
the hook has no such pattern.
- `tests/test_install_git_hooks_script.py`: three end-to-end tests that
install the hooks into a temp repo and push to a bare remote with `make`
stubbed to log its calls. A branch update runs `make ci-precheck`; a
delete-only push does not (and the remote branch is gone); a push that
mixes a delete and an update runs it once.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`) (not run: no `headroom/`
code changed; the pre-commit mypy hook passed on the changed test file)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ pytest -q tests/test_install_git_hooks_script.py            # this branch
.......                                                         [100%]
7 passed in 3.97s

$ pytest -q tests/test_install_git_hooks_script.py            # same tests, origin/main's script
E   AssertionError: assert ['ci-precheck'] == []
FAILED tests/test_install_git_hooks_script.py::test_pre_push_skips_ci_precheck_for_a_delete_only_push
1 failed, 6 passed in 3.40s

$ ruff check tests/test_install_git_hooks_script.py
All checks passed!
$ ruff format --check tests/test_install_git_hooks_script.py
1 file already formatted
$ shellcheck -s bash scripts/install-git-hooks.sh
(no output)
$ npx commitlint --from origin/main --to HEAD
(exit 0)
```

pre-commit (ruff, ruff format, mypy, commitlint) passed on the commit.

## Real Behavior Proof

- Environment: macOS 27.2 arm64, git 2.x, bash 5; hooks installed by
each version of `scripts/install-git-hooks.sh` into a throwaway repo
with a bare remote; `make` stubbed to print instead of running the
precheck.
- Exact command / steps: install the hooks, push a `feature` branch with
`--no-verify`, then run `git push origin --delete feature` and `git push
origin HEAD:refs/heads/feature`, once with origin/main's script
(bb8d18a56) and once with this branch's.
- Observed result: origin/main runs `make ci-precheck` for the delete;
this branch skips it and still runs it for the branch push:

```text
== origin/main bb8d18a56
$ git push origin --delete feature
  [make ci-precheck] would run the full Rust + Python precheck
$ git push origin HEAD:refs/heads/feature
── pre-push: running 'make ci-precheck' before pushing HEAD → refs/heads/feature
  [make ci-precheck] would run the full Rust + Python precheck
== this branch
$ git push origin --delete feature
$ git push origin HEAD:refs/heads/feature
── pre-push: running 'make ci-precheck' before pushing HEAD → refs/heads/feature
  [make ci-precheck] would run the full Rust + Python precheck
```

- Not tested: a SHA-256 repository end to end (the regex change is
covered by reading only); Windows (the hook test module skips without
bash and git, as before); the full `make ci-precheck` itself (pushed
with `--no-verify`, since this PR changes only the hook script and its
test, and the precheck's `pip install -e .` would overwrite the local
venv used for the tests above).

## Runtime Rollout Safety

- Rollout-managed feature(s): none; contributor git hook only.
- Minimum rollout channel: N/A (not shipped in the package).
- Stable/default behavior changed: only for delete-only pushes, which no
longer run `make ci-precheck`. Every push that sends commits behaves as
before.
- Kill switch / disable path: N/A; contributors re-run `make
install-git-hooks` to pick up the change.
- Unsafe override required: no.
- Qualification impact: none; CI does not use this hook.
- Rollback path: revert this commit and re-run `make install-git-hooks`.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

N/A

## Additional Notes

Documentation: N/A. `CONTRIBUTING.md` says the pre-push hook runs
ci-precheck "on every push"; a push that only deletes branches sends no
code, so I left that wording alone. Happy to add a note if you prefer.

The three existing shellcheck SC2034 warnings in the hook body
(`remote`, `url`, `remote_sha` unused) are unchanged by this PR.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4702"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

Co-authored-by: Kenneth Wong <17061293+KennethWKZ@users.noreply.github.com>
2026-11-09 12:24:49 -06:00

118 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install git hooks for the Headroom repo:
# 1. pre-commit — repo pre-commit checks (ruff, mypy, sync-plugin-versions)
# 2. commit-msg — conventional-commit enforcement via commitlint
# 3. pre-push — full ci-precheck (cargo fmt/clippy/test + python suite)
#
# Why pre-push was added: the 2026-04-27 push hit five CI failures that could
# all have been caught locally — cargo fmt drift, an x86_64-apple-darwin wheel
# that the project doesn't actually need, missing Rust extension in two CI
# lanes, and a commitlint warning treated as an error.
#
# Why pre-commit was added: PR #772 merged with inline-comment spacing and
# import-order violations because the ruff pre-commit hook in
# .pre-commit-config.yaml was never installed for contributors.
#
# Idempotent. Re-running is safe. Exits with an error when run outside a git
# checkout. Works from linked worktrees too: there `.git` is a file, so the
# hooks directory is resolved through git rather than assumed to be
# `.git/hooks/`.
set -euo pipefail
cd "$(dirname "$0")/.."
# Install only into the checkout this script belongs to. A source copy nested
# inside another repository would otherwise resolve that repository's hooks.
if [[ "$(git rev-parse --show-toplevel 2>/dev/null)" != "$(pwd -P)" ]]; then
echo "error: $(pwd -P) is not the root of a git checkout — run from a Headroom git checkout" >&2
exit 1
fi
# Ask git for the effective hooks directory. In a linked worktree `.git` is a
# file and hooks live in the shared common dir, so `.git/hooks` doesn't exist.
HOOKS_DIR="$(git rev-parse --git-path hooks)"
mkdir -p "$HOOKS_DIR"
if ! command -v npx &>/dev/null; then
echo "error: npx not found — install Node 18+ before installing Headroom's git hooks." >&2
exit 1
fi
HOOK_PATH="$HOOKS_DIR/pre-push"
cat > "$HOOK_PATH" <<'HOOK_EOF'
#!/usr/bin/env bash
# Headroom pre-push hook — runs `make ci-precheck` so CI never finds a
# bug a local check could have caught.
#
# Skip with: `git push --no-verify`. Use sparingly — every skip is a roll
# of the dice on a CI break.
set -euo pipefail
remote="$1"
url="$2"
# git passes one line per ref on stdin. An all-zero local sha is a delete
# (`git push --delete`): it sends no commits, so there is nothing to check.
has_update=0
while IFS=' ' read -r local_ref local_sha remote_ref remote_sha; do
if [[ "$local_sha" =~ ^0+$ ]]; then
continue
fi
has_update=1
echo "── pre-push: running 'make ci-precheck' before pushing $local_ref → $remote_ref"
done
if [[ "$has_update" -eq 0 ]]; then
exit 0
fi
if [[ -z "${VIRTUAL_ENV:-}" ]]; then
if [[ -f .venv/bin/activate ]]; then
# shellcheck disable=SC1091
source .venv/bin/activate
else
echo "warn: no VIRTUAL_ENV set and no .venv/ found — python checks may use the wrong interpreter" >&2
fi
fi
if make ci-precheck; then
exit 0
else
echo ""
echo "❌ pre-push: 'make ci-precheck' failed. Fix the issues above before pushing."
echo " To bypass (NOT recommended): git push --no-verify"
exit 1
fi
HOOK_EOF
chmod +x "$HOOK_PATH"
echo "✅ installed: $HOOK_PATH"
echo " Runs 'make ci-precheck' before every git push."
echo " Bypass (use sparingly): git push --no-verify"
# Install pre-commit hooks (repo checks on every commit).
# Prefer the project venv over a global install so contributors always run the
# pinned version. Resolution order: active $VIRTUAL_ENV → .venv → global PATH.
PRE_COMMIT_BIN=""
if [[ -n "${VIRTUAL_ENV:-}" && -x "${VIRTUAL_ENV}/bin/pre-commit" ]]; then
PRE_COMMIT_BIN="${VIRTUAL_ENV}/bin/pre-commit"
elif [[ -x .venv/bin/pre-commit ]]; then
PRE_COMMIT_BIN=".venv/bin/pre-commit"
elif command -v pre-commit &>/dev/null; then
PRE_COMMIT_BIN="pre-commit"
fi
if [[ -n "$PRE_COMMIT_BIN" ]]; then
"$PRE_COMMIT_BIN" install
"$PRE_COMMIT_BIN" install --hook-type commit-msg
echo "✅ installed: $HOOKS_DIR/pre-commit (repo pre-commit checks via pre-commit)"
echo "✅ installed: $HOOKS_DIR/commit-msg (conventional commit enforcement via commitlint)"
else
echo "error: pre-commit not found — run 'pip install -e .[dev]' first, then re-run this script." >&2
exit 1
fi