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.
This commit is contained in:
parent
20bfd82079
commit
c171bf3943
5 changed files with 216 additions and 0 deletions
57
.githooks/pre-commit
Executable file
57
.githooks/pre-commit
Executable file
|
|
@ -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
|
||||
49
scripts/agent-commit.sh
Executable file
49
scripts/agent-commit.sh
Executable file
|
|
@ -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 "<conventional commit message>"
|
||||
#
|
||||
# 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 \"<conventional commit message>\"" >&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"
|
||||
50
scripts/check-secrets.sh
Executable file
50
scripts/check-secrets.sh
Executable file
|
|
@ -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
|
||||
45
scripts/publish.sh
Executable file
45
scripts/publish.sh
Executable file
|
|
@ -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 <PUBLIC_REPO_URL>" >&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)"
|
||||
15
scripts/setup-hooks.sh
Executable file
15
scripts/setup-hooks.sh
Executable file
|
|
@ -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)"
|
||||
Loading…
Add table
Reference in a new issue