flybrain/tools/install-hooks.sh
acamilo 05324293ee tools: pii-scan guard with a private pattern list, tag-release gate and pre-push hook
tools/pii-scan.sh scans the tree, a commit range (added lines and messages) or a push
(pre-push protocol, incl. annotated tag messages). Generic shapes stay in the script; the
proper nouns move to the operator's private list ($FLY_PII_PATTERNS), which can also name
env files whose values are matched as literals and never printed. lint.sh section 5 now
runs it (a missing list is a note, so public CI still passes) and no longer spells the
names with one bracketed character. tag-release.sh fails closed without the list and scans
the tree and every line since the previous tag. tools/install-hooks.sh installs a pre-push
hook that scans pushes to github. Tests in infra/tests/pii-scan-test.sh.
2026-09-29 15:52:35 +00:00

51 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# tools/install-hooks.sh [--force]
#
# Installs the flybrain pre-push hook into this clone's hooks directory (shared by every
# worktree). On a push to a PUBLIC remote — the remote named `github`, any name listed in
# $FLY_PII_PUBLIC_REMOTES, or any github.com URL — the hook runs tools/pii-scan.sh --push
# over every commit the push would publish (added lines and messages), annotated tag
# messages, and each pushed tip's tree. It fails closed: without the operator's private
# pattern list ($FLY_PII_PATTERNS, default ~/.config/flybrain/pii-patterns.txt) the push is
# refused. Pushes to other remotes are not scanned.
#
# A copy of the scanner is installed next to the hook so branches that predate
# tools/pii-scan.sh are still scanned; re-run this script after the scanner changes.
# `git push --no-verify` skips the hook, as for any git hook; do not.
set -euo pipefail
FORCE=0
[ "${1:-}" != --force ] || FORCE=1
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
hooks_dir="$(git -C "$SELF_DIR" rev-parse --path-format=absolute --git-path hooks)"
mkdir -p "$hooks_dir"
hook="$hooks_dir/pre-push"
marker="# flybrain pii-guard pre-push hook"
if [ -e "$hook" ] && ! grep -qF "$marker" "$hook" && [ "$FORCE" -ne 1 ]; then
echo "install-hooks: $hook exists and is not ours; merge it by hand or re-run with --force" >&2
exit 1
fi
install -m 0755 "$SELF_DIR/pii-scan.sh" "$hooks_dir/flybrain-pii-scan.sh"
cat > "$hook" <<'HOOK'
#!/usr/bin/env bash
# flybrain pii-guard pre-push hook (installed by tools/install-hooks.sh; re-run it to update)
set -euo pipefail
remote="$1" url="${2:-}"
public=0
for r in github ${FLY_PII_PUBLIC_REMOTES:-}; do [ "$remote" = "$r" ] && public=1; done
case "$url" in *github.com[:/]*) public=1 ;; esac
[ "$public" -eq 1 ] || exit 0
top="$(git rev-parse --show-toplevel)"
scan="$top/tools/pii-scan.sh"
[ -x "$scan" ] || scan="$(dirname "${BASH_SOURCE[0]}")/flybrain-pii-scan.sh"
allow="$top/infra/tests/de-pii-allow.txt"
if ! FLY_PII_REPO="$top" "$scan" --require-private --allow "$allow" --push "$remote"; then
echo "pre-push: refusing to publish to '${remote}': fix the findings above (or restore the private pattern list)." >&2
exit 1
fi
HOOK
chmod 0755 "$hook"
echo "install-hooks: installed $hook (public remotes: github ${FLY_PII_PUBLIC_REMOTES:-}, and github.com URLs)"