Skip to main content

max / makenotwork

4.3 KB · 110 lines History Blame Raw
1 #!/bin/bash
2 # rustfmt gate: blocks a commit whose staged Rust files are not formatted.
3 #
4 # Activate in a fresh clone (one-time):
5 # git config core.hooksPath scripts/githooks
6 #
7 # Bypass for a work-in-progress commit: git commit --no-verify
8 #
9 # Only crates with staged .rs changes are checked, so the hook stays fast on a
10 # large repo. Each file maps to the nearest enclosing Cargo.toml, and the check
11 # runs as `cargo fmt` there, which picks up that crate's edition and any
12 # rustfmt.toml rather than guessing.
13 set -euo pipefail
14
15 ROOT="$(git rev-parse --show-toplevel)"
16 cd "$ROOT"
17
18 # --- secret scan (gitleaks) -------------------------------------------------
19 # Independent guardrail: blocks a commit whose staged changes contain a secret,
20 # regardless of whether a human judged the value "safe". Shared ruleset lives at
21 # ~/Code/.gitleaks.toml. Degrades gracefully if gitleaks is not installed (the
22 # astra pre-receive hook is the backstop that always runs). Task: infra 97ffeda0.
23 if command -v gitleaks >/dev/null 2>&1; then
24 GL_CFG=""
25 if [ -f "$ROOT/.gitleaks.toml" ]; then
26 GL_CFG="$ROOT/.gitleaks.toml"
27 elif [ -f "$HOME/Code/.gitleaks.toml" ]; then
28 GL_CFG="$HOME/Code/.gitleaks.toml"
29 fi
30 gl_args=(git --staged --no-banner --redact)
31 [ -n "$GL_CFG" ] && gl_args+=(-c "$GL_CFG")
32 if ! gitleaks "${gl_args[@]}"; then
33 echo "pre-commit: gitleaks found a secret in the staged changes."
34 echo " remove it (or allowlist a false positive), then restage."
35 echo " bypass: git commit --no-verify."
36 exit 1
37 fi
38 echo "pre-commit: gitleaks clean."
39 else
40 echo "pre-commit: gitleaks not installed; skipping secret scan (astra gates on push)."
41 fi
42
43 # --- migration immutability -------------------------------------------------
44 # sqlx checksums a migration's whole file when it runs it and refuses one whose
45 # bytes changed since. So editing a migration that has already been applied
46 # anywhere breaks every deploy against that database with "previously applied
47 # but modified" — for a comment edit, and for a line-ending change, exactly as
48 # much as for a schema change.
49 #
50 # The 2026-07-27 exorcise sweep rewrote comments in 29 applied migrations and
51 # converted one from CRLF to LF. Nothing in the test suite checksums a
52 # migration, so it stayed invisible while it blocked every server deploy for
53 # three days. Hence a gate at commit time: a migration that already exists in
54 # HEAD may not be modified, renamed, or deleted. Adding a new one is always fine.
55 touched="$(git diff --cached --name-only --diff-filter=MDR -- '*migrations/*.sql')"
56 if [ -n "$touched" ]; then
57 echo "pre-commit: these already-committed migrations were modified, renamed, or deleted:"
58 while IFS= read -r m; do
59 [ -n "$m" ] && echo " $m"
60 done <<< "$touched"
61 echo " A migration is immutable once applied; sqlx checksums the whole"
62 echo " file, comments included. Write a new migration instead."
63 echo " Bypass ONLY if it has never been applied anywhere, including"
64 echo " prod, staging, and your dev database: git commit --no-verify."
65 exit 1
66 fi
67 echo "pre-commit: migrations unmodified."
68
69 # Paths the gate ignores (extended regex, matched against repo-relative paths).
70 # Empty means check everything.
71 SKIP_PATHS="${SKIP_PATHS:-}"
72
73 staged="$(git diff --cached --name-only --diff-filter=ACMR -- '*.rs')"
74 if [ -n "$SKIP_PATHS" ]; then
75 staged="$(printf '%s\n' "$staged" | grep -Ev "$SKIP_PATHS" || true)"
76 fi
77 [ -n "$staged" ] || exit 0
78
79 # Map each staged file to the directory of its nearest Cargo.toml.
80 crates=""
81 while IFS= read -r f; do
82 [ -n "$f" ] || continue
83 d="$(dirname "$f")"
84 while [ "$d" != "." ] && [ ! -f "$d/Cargo.toml" ]; do
85 d="$(dirname "$d")"
86 done
87 [ -f "$d/Cargo.toml" ] || continue
88 crates="$crates$d"$'\n'
89 done <<< "$staged"
90
91 crates="$(printf '%s' "$crates" | sort -u)"
92 [ -n "$crates" ] || exit 0
93
94 failed=0
95 while IFS= read -r c; do
96 [ -n "$c" ] || continue
97 if ! (cd "$c" && cargo fmt --check >/dev/null 2>&1); then
98 echo "pre-commit: rustfmt gate failed in $c"
99 failed=1
100 fi
101 done <<< "$crates"
102
103 if [ "$failed" -ne 0 ]; then
104 echo "pre-commit: run 'cargo fmt' in the crates above, then restage."
105 echo "pre-commit: commit aborted (use --no-verify to bypass)."
106 exit 1
107 fi
108
109 echo "pre-commit: rustfmt gate clean."
110