pub const APERION_HOOK_MARKER: &str = "# APERION-SHIELD-HOOK v1 -- managed by `aperion-shield --install-hooks`";
pub fn pre_commit_script() -> String {
format!(
r#"#!/bin/sh
{marker}
#
# What this does:
# * Asks `aperion-shield --check-staged` to scan the lines being
# ADDED / MODIFIED in this commit.
# * Blocks the commit (exit 1) if any line trips a destructive rule
# (DROP DATABASE, rm -rf /, git push --force, etc.).
# * No-ops cleanly when `aperion-shield` isn't on $PATH.
#
# Bypass switches (in order of preference):
# git commit --no-verify # skip all hooks for this commit
# SHIELD_HOOKS_DISABLE=1 git ... # env override; works in CI
#
# To remove this hook entirely:
# aperion-shield --uninstall-hooks
set -e
if [ "${{SHIELD_HOOKS_DISABLE:-}}" = "1" ]; then
exit 0
fi
if ! command -v aperion-shield >/dev/null 2>&1; then
echo "[aperion-shield] binary not on \$PATH; skipping pre-commit guardrail" >&2
echo "[aperion-shield] install: brew install AperionAI/tap/aperion-shield" >&2
exit 0
fi
exec aperion-shield --check-staged
"#,
marker = APERION_HOOK_MARKER,
)
}
pub fn pre_push_script() -> String {
format!(
r#"#!/bin/sh
{marker}
#
# What this does:
# * Reads git's standard pre-push stdin (one `local_ref local_sha
# remote_ref remote_sha` line per ref being pushed).
# * Asks `aperion-shield --check-pushed-refs` whether any ref is a
# destructive force-push or branch-deletion of a protected branch
# (main, master, prod, release/*, by default).
# * Blocks the push (exit 1) if any ref is destructive.
# * No-ops cleanly when `aperion-shield` isn't on $PATH.
#
# Bypass switches:
# git push --no-verify
# SHIELD_HOOKS_DISABLE=1 git push ...
#
# To remove this hook entirely:
# aperion-shield --uninstall-hooks
set -e
if [ "${{SHIELD_HOOKS_DISABLE:-}}" = "1" ]; then
exit 0
fi
if ! command -v aperion-shield >/dev/null 2>&1; then
echo "[aperion-shield] binary not on \$PATH; skipping pre-push guardrail" >&2
exit 0
fi
# git supplies pre-push refs on stdin; pipe straight through.
exec aperion-shield --check-pushed-refs
"#,
marker = APERION_HOOK_MARKER,
)
}