| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
set -euo pipefail |
| 26 |
|
| 27 |
ROOT="$(git rev-parse --show-toplevel)" |
| 28 |
cd "$ROOT" |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
export PATH="$HOME/.local/bin:$HOME/.cargo/bin:/opt/homebrew/bin:/usr/local/bin:$PATH" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
if command -v gitleaks >/dev/null 2>&1; then |
| 42 |
GL_CFG="" |
| 43 |
if [ -f "$ROOT/.gitleaks.toml" ]; then |
| 44 |
GL_CFG="$ROOT/.gitleaks.toml" |
| 45 |
elif [ -f "$HOME/Code/.gitleaks.toml" ]; then |
| 46 |
GL_CFG="$HOME/Code/.gitleaks.toml" |
| 47 |
fi |
| 48 |
gl_args=(git --staged --no-banner --redact) |
| 49 |
[ -n "$GL_CFG" ] && gl_args+=(-c "$GL_CFG") |
| 50 |
if ! gitleaks "${gl_args[@]}"; then |
| 51 |
echo "pre-commit: gitleaks found a secret in the staged changes." |
| 52 |
echo " remove it (or allowlist a false positive), then restage." |
| 53 |
echo " bypass: git commit --no-verify." |
| 54 |
exit 1 |
| 55 |
fi |
| 56 |
echo "pre-commit: gitleaks clean." |
| 57 |
else |
| 58 |
echo "pre-commit: gitleaks not installed; skipping secret scan (astra gates on push)." |
| 59 |
fi |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
touched="$(git diff --cached --name-only --diff-filter=MDR -- '*migrations/*.sql')" |
| 77 |
if [ -n "$touched" ]; then |
| 78 |
echo "pre-commit: these already-committed migrations were modified, renamed, or deleted:" |
| 79 |
while IFS= read -r m; do |
| 80 |
[ -n "$m" ] && echo " $m" |
| 81 |
done <<< "$touched" |
| 82 |
echo " A migration is immutable once applied; the runner checksums the" |
| 83 |
echo " whole file, comments included. Write a new migration instead." |
| 84 |
echo " Bypass ONLY if it has never been applied anywhere, including" |
| 85 |
echo " prod, staging, and your dev database: git commit --no-verify." |
| 86 |
exit 1 |
| 87 |
fi |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
staged_fe="$(git diff --cached --name-only --diff-filter=ACMR -- '*.js' '*.css' '*.html')" |
| 101 |
if [ -n "$staged_fe" ]; then |
| 102 |
while IFS= read -r lint; do |
| 103 |
[ -n "$lint" ] || continue |
| 104 |
if ! fe_out=$(bash "$lint" 2>&1); then |
| 105 |
echo "$fe_out" |
| 106 |
echo "pre-commit: frontend lint failed ($lint)." |
| 107 |
echo " fix the rules above, then restage." |
| 108 |
echo " bypass: git commit --no-verify." |
| 109 |
exit 1 |
| 110 |
fi |
| 111 |
echo "pre-commit: frontend lint clean ($lint)." |
| 112 |
done <<< "$(find . -maxdepth 3 -path ./target -prune -o \ |
| 113 |
-path '*/scripts/lint-frontend.sh' -print 2>/dev/null | sort)" |
| 114 |
fi |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
SKIP_PATHS="${SKIP_PATHS:-}" |
| 126 |
|
| 127 |
staged="$(git diff --cached --name-only --diff-filter=ACMR -- '*.rs')" |
| 128 |
if [ -n "$SKIP_PATHS" ]; then |
| 129 |
staged="$(printf '%s\n' "$staged" | grep -Ev "$SKIP_PATHS" || true)" |
| 130 |
fi |
| 131 |
|
| 132 |
if [ -n "$staged" ]; then |
| 133 |
|
| 134 |
crates="" |
| 135 |
while IFS= read -r f; do |
| 136 |
[ -n "$f" ] || continue |
| 137 |
d="$(dirname "$f")" |
| 138 |
while [ "$d" != "." ] && [ ! -f "$d/Cargo.toml" ]; do |
| 139 |
d="$(dirname "$d")" |
| 140 |
done |
| 141 |
[ -f "$d/Cargo.toml" ] || continue |
| 142 |
crates="$crates$d"$'\n' |
| 143 |
done <<< "$staged" |
| 144 |
|
| 145 |
crates="$(printf '%s' "$crates" | sort -u)" |
| 146 |
|
| 147 |
failed=0 |
| 148 |
while IFS= read -r c; do |
| 149 |
[ -n "$c" ] || continue |
| 150 |
if ! (cd "$c" && cargo fmt --check >/dev/null 2>&1); then |
| 151 |
echo "pre-commit: rustfmt gate failed in $c" |
| 152 |
failed=1 |
| 153 |
fi |
| 154 |
done <<< "$crates" |
| 155 |
|
| 156 |
if [ "$failed" -ne 0 ]; then |
| 157 |
echo "pre-commit: run 'cargo fmt' in the crates above, then restage." |
| 158 |
echo "pre-commit: commit aborted (use --no-verify to bypass)." |
| 159 |
exit 1 |
| 160 |
fi |
| 161 |
echo "pre-commit: rustfmt gate clean." |
| 162 |
fi |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if [ -f "$ROOT/server/openapi.json" ]; then |
| 181 |
specish="$(git diff --cached --name-only --diff-filter=ACMR \ |
| 182 |
-- 'server/Cargo.toml' 'server/src/*.rs' 'server/src/**/*.rs' 'server/openapi.json')" |
| 183 |
if [ -n "$specish" ]; then |
| 184 |
echo "pre-commit: checking openapi.json against the generated spec..." |
| 185 |
gen="$(mktemp)" |
| 186 |
trap 'rm -f "$gen"' EXIT |
| 187 |
if (cd "$ROOT/server" && cargo run --quiet --bin export-openapi -- --stdout) > "$gen" 2>/dev/null; then |
| 188 |
if ! git show :server/openapi.json 2>/dev/null | diff -q - "$gen" >/dev/null; then |
| 189 |
echo "pre-commit: server/openapi.json is stale (or regenerated but not staged)." |
| 190 |
echo " cd server && cargo run --bin export-openapi" |
| 191 |
echo " git add server/openapi.json" |
| 192 |
echo " Then vendor the same bytes into the OTHER repo, which this" |
| 193 |
echo " commit cannot carry and Sando will fail on:" |
| 194 |
echo " cp server/openapi.json ../synckit/synckit-client/tests/openapi.json" |
| 195 |
echo " Bypass: git commit --no-verify." |
| 196 |
exit 1 |
| 197 |
fi |
| 198 |
echo "pre-commit: openapi.json current." |
| 199 |
else |
| 200 |
echo "pre-commit: could not build export-openapi; skipping spec check." |
| 201 |
echo " cargo_test in Sando is the backstop, 15 minutes into the build." |
| 202 |
fi |
| 203 |
fi |
| 204 |
fi |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if [ -f "$ROOT/scripts/githooks/pre-commit.local" ]; then |
| 211 |
bash "$ROOT/scripts/githooks/pre-commit.local" || exit 1 |
| 212 |
fi |
| 213 |
|