From c171bf39430aabe095a1b4c838714b8c74e124e5 Mon Sep 17 00:00:00 2001 From: Phoebus Date: Sun, 20 Sep 2026 02:07:47 -0700 Subject: [PATCH] build: add git hooks, secret scan and publish tooling Adds .githooks/pre-commit (secret scan, large-file guard, fast tests), scripts/check-secrets.sh, scripts/agent-commit.sh, scripts/publish.sh and scripts/setup-hooks.sh so every work session leaves a verifiable commit. --- .githooks/pre-commit | 57 ++++++++++++++++++++++++++++++++++++++++ scripts/agent-commit.sh | 49 ++++++++++++++++++++++++++++++++++ scripts/check-secrets.sh | 50 +++++++++++++++++++++++++++++++++++ scripts/publish.sh | 45 +++++++++++++++++++++++++++++++ scripts/setup-hooks.sh | 15 +++++++++++ 5 files changed, 216 insertions(+) create mode 100755 .githooks/pre-commit create mode 100755 scripts/agent-commit.sh create mode 100755 scripts/check-secrets.sh create mode 100755 scripts/publish.sh create mode 100755 scripts/setup-hooks.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..fea26c9 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# Phoebus pre-commit hook. +# +# 1. refuse large/binary image files; +# 2. scan staged content for secrets; +# 3. run the fast Bash test suite (set PHOEBUS_PRECOMMIT_TESTS=0 to skip); +# 4. optionally run the Rust suite (set PHOEBUS_PRECOMMIT_RUST=1 to enable). +# +# Bypass with: git commit --no-verify or SKIP_PHOEBUS_HOOKS=1 git commit ... + +set -uo pipefail + +[[ "${SKIP_PHOEBUS_HOOKS:-0}" == "1" ]] && exit 0 + +root="$(git rev-parse --show-toplevel 2>/dev/null)" || exit 0 +cd "$root" || exit 0 + +mapfile -t staged < <(git diff --cached --name-only --diff-filter=ACM) + +for file in "${staged[@]}"; do + [[ -n "$file" ]] || continue + case "$file" in + *.iso|*.qcow2|*.img|*.raw) + printf 'pre-commit: refusing to commit image file %s\n' "$file" >&2 + exit 1 + ;; + esac + if [[ -f "$file" ]]; then + size="$(wc -c <"$file" 2>/dev/null || echo 0)" + if (( size > 5242880 )); then + printf 'pre-commit: %s is %s bytes (>5 MiB); refusing\n' "$file" "$size" >&2 + exit 1 + fi + fi +done + +if [[ -x scripts/check-secrets.sh ]]; then + scripts/check-secrets.sh "${staged[@]}" || exit 1 +fi + +if [[ "${PHOEBUS_PRECOMMIT_TESTS:-1}" == "1" && -x installer/tests/run_tests.sh ]]; then + log="/tmp/phoebus-precommit-tests.log" + if ! bash installer/tests/run_tests.sh >"$log" 2>&1; then + printf 'pre-commit: tests failed; see %s\n' "$log" >&2 + exit 1 + fi +fi + +if [[ "${PHOEBUS_PRECOMMIT_RUST:-0}" == "1" ]] && command -v cargo >/dev/null 2>&1; then + log="/tmp/phoebus-precommit-rust.log" + if ! (cd installer-rs && cargo test --workspace) >"$log" 2>&1; then + printf 'pre-commit: cargo tests failed; see %s\n' "$log" >&2 + exit 1 + fi +fi + +exit 0 diff --git a/scripts/agent-commit.sh b/scripts/agent-commit.sh new file mode 100755 index 0000000..7452d0a --- /dev/null +++ b/scripts/agent-commit.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# Commit all working-tree changes with a conventional message. +# +# Intended for AI work sessions so that every session leaves a trace in git. +# It stages everything, runs the safety checks, then commits. It does not push; +# use scripts/publish.sh for that. +# +# Usage: scripts/agent-commit.sh "" +# +# Environment: +# PHOEBUS_COMMIT_TEST=0 skip the fast test run before committing + +set -euo pipefail + +message="${1:-}" +if [[ -z "$message" ]]; then + echo "usage: scripts/agent-commit.sh \"\"" >&2 + exit 2 +fi + +root="$(git rev-parse --show-toplevel)" +cd "$root" + +if [[ -z "$(git status --porcelain)" ]]; then + echo "nothing to commit (working tree clean)" + exit 0 +fi + +git add -A + +scripts/check-secrets.sh + +if [[ "${PHOEBUS_COMMIT_TEST:-1}" == "1" && -x installer/tests/run_tests.sh ]]; then + echo "running fast test suite..." + bash installer/tests/run_tests.sh >/tmp/phoebus-agent-commit-tests.log 2>&1 || { + echo "tests failed; see /tmp/phoebus-agent-commit-tests.log" >&2 + exit 1 + } +fi + +echo "staged changes:" +git diff --cached --stat | tail -n 40 + +# The checks above already ran; skip the hook's duplicate work. +SKIP_PHOEBUS_HOOKS=1 git commit -m "$message" + +echo +echo "committed: $(git log -1 --oneline)" +echo "push with: scripts/publish.sh" diff --git a/scripts/check-secrets.sh b/scripts/check-secrets.sh new file mode 100755 index 0000000..7f67e48 --- /dev/null +++ b/scripts/check-secrets.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Scan files (or the staged set) for high-signal secret material. +# +# This is a safety net, not a guarantee. It deliberately matches only +# high-confidence patterns so it does not false-positive on sha256 digests in +# the OpenCode manifest or on documentation that merely mentions secrets. +# +# Usage: scripts/check-secrets.sh [FILE...] +# With no arguments, scans files staged for commit. + +set -uo pipefail + +if [[ $# -gt 0 ]]; then + files=("$@") +else + mapfile -t files < <(git diff --cached --name-only --diff-filter=ACM) +fi + +patterns=( + '-----BEGIN [A-Z ]*PRIVATE KEY-----' + 'AKIA[0-9A-Z]{16}' + 'ASIA[0-9A-Z]{16}' + 'sk-[A-Za-z0-9]{20,}' + 'ghp_[A-Za-z0-9]{36}' + 'github_pat_[A-Za-z0-9_]{20,}' + 'xox[baprs]-[A-Za-z0-9-]{10,}' + 'AIza[0-9A-Za-z_-]{35}' +) + +found=0 +for file in "${files[@]}"; do + [[ -n "$file" ]] || continue + [[ -f "$file" ]] || continue + case "$file" in + *.iso|*.qcow2|*.img|*.raw) continue ;; + esac + for pattern in "${patterns[@]}"; do + if grep -EqI -- "$pattern" "$file"; then + printf 'potential secret: %s matches /%s/\n' "$file" "$pattern" >&2 + found=1 + fi + done +done + +if (( found )); then + printf '\nRefusing to proceed: remove the secret from the staged content.\n' >&2 + exit 1 +fi + +exit 0 diff --git a/scripts/publish.sh b/scripts/publish.sh new file mode 100755 index 0000000..de43c3f --- /dev/null +++ b/scripts/publish.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Publish the current commit to the configured public remote. +# +# Usage: scripts/publish.sh [REMOTE] [BRANCH] +# REMOTE defaults to origin +# BRANCH defaults to the current branch +# +# Refuses to push an unclean working tree or a failing test suite. + +set -euo pipefail + +root="$(git rev-parse --show-toplevel)" +cd "$root" + +remote="${1:-origin}" +branch="${2:-$(git rev-parse --abbrev-ref HEAD)}" + +if ! git remote get-url "$remote" >/dev/null 2>&1; then + echo "error: remote '$remote' is not configured." >&2 + echo "add it once with: git remote add $remote " >&2 + exit 1 +fi + +if [[ -n "$(git status --porcelain)" ]]; then + echo "error: working tree is not clean; commit first (scripts/agent-commit.sh)." >&2 + exit 1 +fi + +echo "running fast test suite..." +bash installer/tests/run_tests.sh >/tmp/phoebus-publish-tests.log 2>&1 || { + echo "tests failed; see /tmp/phoebus-publish-tests.log" >&2 + exit 1 +} + +if command -v cargo >/dev/null 2>&1 && [[ -f installer-rs/Cargo.toml ]]; then + echo "running Rust test suite..." + (cd installer-rs && cargo test --workspace) || { + echo "cargo tests failed" >&2 + exit 1 + } +fi + +echo "pushing $branch to $remote ($(git remote get-url "$remote"))" +git push "$remote" "$branch" +echo "published: $(git log -1 --oneline)" diff --git a/scripts/setup-hooks.sh b/scripts/setup-hooks.sh new file mode 100755 index 0000000..52ce516 --- /dev/null +++ b/scripts/setup-hooks.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Enable the committed git hooks and make the scripts executable. +# +# Run once after cloning: scripts/setup-hooks.sh + +set -euo pipefail + +root="$(git rev-parse --show-toplevel)" +cd "$root" + +chmod +x scripts/*.sh .githooks/* 2>/dev/null || true +git config core.hooksPath .githooks + +echo "core.hooksPath -> .githooks" +echo "hooks enabled: $(git config --get core.hooksPath)"