|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Pre-push gate: catches classes of CI failure locally before they burn a CI run. |
| 3 | +# Cheapest/most-certain checks run first and fail fast. |
| 4 | +# |
| 5 | +# Escape hatch: SKIP_HOOKS=1 git push (or git push --no-verify) |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +if [[ "${SKIP_HOOKS:-0}" == "1" ]]; then |
| 9 | + echo "[pre-push] SKIP_HOOKS=1 set, skipping all checks." |
| 10 | + exit 0 |
| 11 | +fi |
| 12 | + |
| 13 | +cd "$(git rev-parse --show-toplevel)" |
| 14 | + |
| 15 | +start_ts=$(date +%s) |
| 16 | + |
| 17 | +# git's pre-push protocol feeds "<local ref> <local oid> <remote ref> <remote oid>" |
| 18 | +# lines on stdin. Capture once so any sub-check that needs it (the AI |
| 19 | +# co-authorship scan) can reuse it. |
| 20 | +stdin_file="$(mktemp)" |
| 21 | +trap 'rm -f "$stdin_file"' EXIT |
| 22 | +cat > "$stdin_file" |
| 23 | + |
| 24 | +fail() { |
| 25 | + local job="$1" msg="$2" |
| 26 | + local elapsed=$(( $(date +%s) - start_ts )) |
| 27 | + echo "❌ pre-push: would fail CI job \`${job}\` — ${msg}" >&2 |
| 28 | + echo " Escape hatch: SKIP_HOOKS=1 git push (or git push --no-verify)" >&2 |
| 29 | + echo "[pre-push] failed after ${elapsed}s" >&2 |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +step() { |
| 34 | + local name="$1" |
| 35 | + shift |
| 36 | + echo "[pre-push] running: ${name}" |
| 37 | + "$@" |
| 38 | +} |
| 39 | + |
| 40 | +# What's already on the remote — used to scope diff-based checks (nix hash, |
| 41 | +# changed-package typecheck, changed-test selection). Bootstrap-safe: falls |
| 42 | +# back to origin/HEAD, then to skipping those checks entirely. |
| 43 | +resolve_base_ref() { |
| 44 | + local upstream |
| 45 | + upstream=$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null || true) |
| 46 | + if [[ -n "$upstream" ]] && git rev-parse --verify --quiet "$upstream" >/dev/null 2>&1; then |
| 47 | + echo "$upstream" |
| 48 | + return 0 |
| 49 | + fi |
| 50 | + if git show-ref --verify --quiet refs/remotes/origin/HEAD; then |
| 51 | + echo "origin/HEAD" |
| 52 | + return 0 |
| 53 | + fi |
| 54 | + return 1 |
| 55 | +} |
| 56 | +base_ref="$(resolve_base_ref || true)" |
| 57 | + |
| 58 | +# 1. AI co-authorship attribution — sub-second, blocks known-bad commits outright. |
| 59 | +step "no-ai-coauthorship" bash scripts/git-hooks/pre-push-no-ai-coauthorship.sh < "$stdin_file" || |
| 60 | + fail "pre-push-no-ai-coauthorship" "a commit contains forbidden AI agent attribution" |
| 61 | + |
| 62 | +# 2. sherif — monorepo dependency-version skew (CI: `pnpm run sherif`). |
| 63 | +step "sherif" pnpm run sherif || fail "sherif" "monorepo dependency versions are out of sync" |
| 64 | + |
| 65 | +# 3. oxlint (CI: `pnpm run lint`). |
| 66 | +step "lint" pnpm run lint || fail "lint" "oxlint reported errors" |
| 67 | + |
| 68 | +# 4. Nix fetchPnpmDeps hash freshness proxy (CI: nix build). |
| 69 | +step "nix-hash-freshness" node scripts/check-nix-hash-fresh.mjs || |
| 70 | + fail "nix build (flake.nix)" "pnpm-lock.yaml changed without refreshing flake.nix's pnpmDeps hash" |
| 71 | + |
| 72 | +# 5. Typecheck, scoped to packages changed since $base_ref (CI: `pnpm run |
| 73 | +# typecheck`). Full typecheck builds every package first and takes |
| 74 | +# minutes — not affordable pre-push, so this narrows to what pnpm's own |
| 75 | +# dependency graph says was actually touched. |
| 76 | +if [[ -n "$base_ref" ]]; then |
| 77 | + step "typecheck (changed packages)" pnpm -r --filter "...[${base_ref}]" --if-present run typecheck || |
| 78 | + fail "typecheck" "type errors in a package changed on this branch" |
| 79 | +else |
| 80 | + echo "[pre-push] no base ref (new branch, no origin/HEAD) — skipping scoped typecheck." |
| 81 | + echo "[pre-push] run 'pnpm run typecheck' manually before pushing if you touched types." |
| 82 | +fi |
| 83 | + |
| 84 | +# 6. Tests, scoped via vitest's own changed-file impact analysis (CI: `pnpm test`). |
| 85 | +if [[ -n "$base_ref" ]]; then |
| 86 | + step "test (changed)" pnpm exec vitest run --changed "$base_ref" --passWithNoTests || |
| 87 | + fail "test" "tests affected by changed files are failing" |
| 88 | +else |
| 89 | + echo "[pre-push] no base ref — skipping scoped tests." |
| 90 | + echo "[pre-push] run 'pnpm test' manually before pushing if you touched behavior." |
| 91 | +fi |
| 92 | + |
| 93 | +elapsed=$(( $(date +%s) - start_ts )) |
| 94 | +echo "[pre-push] all checks passed in ${elapsed}s" |
0 commit comments