Files
skyg-linux-tracker/dev/hooks/run.sh
T

243 lines
6.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Hook runner. One definition of every check, used by the devenv git-hooks
# entries and by the Nix flake checks.
#
# run.sh pre-commit staged files -> fast checks
# run.sh pre-push commits being pushed -> tests and slow checks
# run.sh lint-all every file in the tree, lint checks only (Nix)
# run.sh check NAME [FILE...] run one check
#
# Missing optional tools are skipped with a warning unless
# HOOKS_REQUIRE_TOOLS=1 (set by the Nix checks, where they are all provided).
set -euo pipefail
here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
root=$(cd "$here/../.." && pwd)
orig=$PWD
cd "$root"
fail=0
note() { printf ' %s\n' "$*" >&2; }
need() {
if command -v "$1" >/dev/null 2>&1; then return 0; fi
if [ "${HOOKS_REQUIRE_TOOLS:-0}" = 1 ]; then
echo "required tool missing: $1" >&2
exit 1
fi
note "skip: $1 not installed"
return 1
}
# Keep only the arguments matching a case pattern, one per line.
filter() {
local pat=$1 f
shift
for f in "$@"; do
# shellcheck disable=SC2254
case $f in $pat) [ -f "$f" ] && printf '%s\n' "$f" ;; esac
done
return 0
}
chk_hygiene() {
local f bad=0
for f in "$@"; do
[ -f "$f" ] || continue
case $f in Cargo.lock | flake.lock | devenv.lock) continue ;; esac
if [ "$(wc -c <"$f")" -gt 524288 ]; then
note "$f: larger than 512 KiB"
bad=1
fi
grep -qI . "$f" || continue
if grep -nE '^(<<<<<<< |>>>>>>> )' "$f" >&2; then
note "$f: merge conflict marker"
bad=1
fi
case $f in
*.md) ;;
*)
if grep -nE '[[:blank:]]+$' "$f" >&2; then
note "$f: trailing whitespace"
bad=1
fi
;;
esac
if [ -s "$f" ] && [ -n "$(tail -c1 "$f")" ]; then
note "$f: no newline at end of file"
bad=1
fi
done
return $bad
}
# Credentials must never reach the repo (AGENTS.md Safety). A line carrying
# the marker "secret-scan: allow" is exempt, for deliberate test fixtures.
chk_secrets() {
local f bad=0
local re='-----BEGIN ([A-Z]+ )?PRIVATE KEY-----'
re+='|[Bb]earer[[:space:]]+[A-Za-z0-9._~+/=-]{24,}'
re+='|gh[pousr]_[A-Za-z0-9]{30,}|AKIA[0-9A-Z]{16}|xox[abprs]-[A-Za-z0-9-]{10,}'
re+='|sk-[A-Za-z0-9]{32,}|glpat-[A-Za-z0-9_-]{20,}'
for f in "$@"; do
[ -f "$f" ] || continue
case $f in
*.pem | *.key | *.p12 | *.pfx | .env | .env.* | .mcp.json | *environment-file* | *.secret)
note "$f: file type that holds secrets must not be committed"
bad=1
continue
;;
esac
case $f in dev/hooks/*) continue ;; esac
if grep -nIE -e "$re" "$f" | grep -vE 'secret-scan: allow|(.)\1{9,}' >&2; then
note "$f: possible secret"
bad=1
fi
done
return $bad
}
# Repo rules from AGENTS.md that clippy cannot see.
chk_rules() {
local f bad=0
for f in "$@"; do
[ -f "$f" ] || continue
# Workstream, task and decision tags in comments.
if grep -nE '//.*\b(WS-[A-Za-z0-9]+|DEC-[0-9]+|G[0-9]+)\b' "$f" >&2; then
note "$f: workstream/task/decision tag in a comment"
bad=1
fi
# axum 0.8 panics at startup on ":name" path params.
if grep -nE '"/(:[A-Za-z_]|[^"]*/:[A-Za-z_])' "$f" >&2; then
note "$f: axum 0.8 route uses :name, write /{name}"
bad=1
fi
done
return $bad
}
chk_rust-fmt() { cargo fmt --check; }
chk_clippy() { cargo clippy --all-targets -- -D warnings; }
# The service must stay rustls-only.
chk_openssl() {
local out
out=$(cargo tree -i openssl 2>/dev/null || true)
if [ -n "$out" ]; then
printf '%s\n' "$out" >&2
note "openssl is in the dependency tree"
return 1
fi
}
chk_toml() {
need taplo || return 0
taplo check "$@"
}
chk_yaml() {
need yamllint || return 0
yamllint -d '{extends: relaxed, rules: {line-length: disable}}' "$@"
}
chk_markdown() {
need markdownlint || return 0
markdownlint "$@"
}
chk_shell() {
need shellcheck || return 0
shellcheck "$@"
}
chk_nushell() {
need nu || return 0
local f
for f in "$@"; do nu -c "nu-check '$f'" >/dev/null || return 1; done
}
chk_nix-parse() {
need nix-instantiate || return 0
local f
for f in "$@"; do nix-instantiate --parse "$f" >/dev/null || return 1; done
}
# Evaluates every flake output, including the checks, without building.
chk_nix-eval() {
need nix || return 0
nix flake check --no-build --no-update-lock-file
}
chk_hooks-selftest() { "$here/test-select.sh"; }
chk_test-all() { cargo test; }
chk_test-unit() { cargo test --lib "$1::"; }
chk_test-it() { cargo test --test "$1"; }
# Dispatch a selected check name (maybe "name:arg") with a file list.
dispatch() {
local name=$1 files=("${@:2}") arg=""
if [[ $name == *:* ]]; then
arg=${name#*:}
name=${name%%:*}
fi
local sel=() filebased=1
case $name in
test-* | clippy | rust-fmt | openssl | nix-eval | hooks-selftest) filebased=0 ;;
hygiene | secrets) sel=("${files[@]}") ;;
rules) mapfile -t sel < <(filter '*.rs' "${files[@]}") ;;
toml) mapfile -t sel < <(filter '*.toml' "${files[@]}") ;;
yaml) mapfile -t sel < <(filter '*.y*ml' "${files[@]}") ;;
markdown) mapfile -t sel < <(filter '*.md' "${files[@]}") ;;
shell) mapfile -t sel < <(filter '*.sh' "${files[@]}") ;;
nushell) mapfile -t sel < <(filter '*.nu' "${files[@]}") ;;
nix-parse) mapfile -t sel < <(filter '*.nix' "${files[@]}") ;;
esac
if [ "$filebased" = 1 ]; then
[ ${#sel[@]} -gt 0 ] || return 0
echo "== $name" >&2
"chk_$name" "${sel[@]}" || fail=1
else
echo "== $name${arg:+ $arg}" >&2
"chk_$name" ${arg:+"$arg"} || fail=1
fi
}
changed_files() {
case $1 in
pre-commit) git diff --cached --name-only --diff-filter=ACMR ;;
pre-push)
local base
base=$(git rev-parse --verify -q '@{u}' || git merge-base HEAD main || git rev-parse HEAD~1)
git diff --name-only --diff-filter=ACMR "$base"...HEAD
;;
esac
}
mode=${1:?usage: run.sh pre-commit|pre-push|lint-all|check NAME [FILE...]}
case $mode in
pre-commit | pre-push)
mapfile -t files < <(changed_files "$mode")
mapfile -t checks < <(printf '%s\n' "${files[@]}" | "$here/select.sh" "$mode")
for c in "${checks[@]}"; do
[ -n "$c" ] && dispatch "$c" "${files[@]}"
done
;;
lint-all)
if git rev-parse --git-dir >/dev/null 2>&1; then
mapfile -t files < <(git ls-files)
else
mapfile -t files < <(find . -type f -not -path './target/*' -not -path './.git/*' | sed 's|^\./||')
fi
for c in hygiene secrets rules toml yaml markdown shell nushell nix-parse hooks-selftest; do
dispatch "$c" "${files[@]}"
done
;;
check)
name=${2:?check NAME}
shift 2
# File arguments are relative to the caller's directory.
cd "$orig"
dispatch "$name" "$@"
;;
*)
echo "unknown mode $mode" >&2
exit 2
;;
esac
exit $fail