`git rebase <upstream> <branch>` checks out <branch>, so a later bare push was refused as if main were still checked out. Pushes through xargs get refspecs the hook cannot see, so only an explicit main is refused there. Refs: #178 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
248 lines
8.9 KiB
Bash
Executable File
248 lines
8.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Tests for plugins/git/.apm/hooks/refuse-push-to-main.py, the PreToolUse hook
|
|
# that refuses a `git push` to `main` (#178).
|
|
#
|
|
# The hook acts only when run from a .claude/hooks/ path, so it is copied to the
|
|
# path apm deploys it to and driven with real payloads against fixture repos.
|
|
set -euo pipefail
|
|
|
|
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
|
|
unset GIT_ALLOW_PUSH_TO_MAIN
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
HOOKS_DIR="$REPO_ROOT/plugins/git/.apm/hooks"
|
|
SRC="$HOOKS_DIR/refuse-push-to-main.py"
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
pass() { PASS=$((PASS + 1)); }
|
|
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
|
|
|
|
command -v python3 > /dev/null 2>&1 || { echo "python3 required"; exit 77; }
|
|
[[ -f "$SRC" ]] || { echo "FAIL: $SRC does not exist"; exit 1; }
|
|
|
|
WORK="$(mktemp -d)"
|
|
trap 'rm -rf "$WORK"' EXIT
|
|
|
|
for d in .claude/hooks/git/.apm/hooks .github/hooks/scripts/git/.apm/hooks \
|
|
.codex/hooks/git/.apm/hooks elsewhere; do
|
|
mkdir -p "$WORK/$d"
|
|
cp "$SRC" "$WORK/$d/"
|
|
done
|
|
HOOK="$WORK/.claude/hooks/git/.apm/hooks/refuse-push-to-main.py"
|
|
|
|
git init -q -b main "$WORK/on-main"
|
|
git init -q -b feat "$WORK/on-feat"
|
|
|
|
# decision <script> <cwd> <command> -> allow|refuse|error(...)
|
|
# The payload is escaped in bash because a Python start per case doubles the run
|
|
# time. The timeout is the one in hooks.json.
|
|
decision() {
|
|
local command="$3" out rc=0
|
|
command="${command//\\/\\\\}"
|
|
command="${command//\"/\\\"}"
|
|
command="${command//$'\n'/\\n}"
|
|
command="${command//$'\t'/\\t}"
|
|
out="$(timeout 10 "$1" 2>&1 <<< "{\"tool_name\":\"Bash\",\"cwd\":\"$2\",\"tool_input\":{\"command\":\"$command\"}}")" || rc=$?
|
|
if [[ $rc -eq 0 && -z "$out" ]]; then
|
|
echo allow
|
|
elif [[ $rc -eq 2 && "$out" == *main* ]]; then
|
|
echo refuse
|
|
else
|
|
echo "error($rc: $out)"
|
|
fi
|
|
}
|
|
|
|
# expect <allow|refuse> <main|feat> <command>
|
|
expect() {
|
|
local got
|
|
got="$(decision "$HOOK" "$WORK/on-$2" "$3")"
|
|
if [[ "$got" == "$1" ]]; then pass; else fail "on $2: want $1, got $got: ${3:0:200}"; fi
|
|
}
|
|
|
|
echo "== Refused from any branch"
|
|
while IFS= read -r args; do
|
|
expect refuse feat "git push $args"
|
|
done << 'CASES'
|
|
origin main
|
|
origin +main
|
|
origin heads/main
|
|
origin refs/heads/main
|
|
origin HEAD:main
|
|
origin feat:main
|
|
origin feat:refs/heads/main
|
|
origin main --tags
|
|
origin :main
|
|
-d origin main
|
|
--delete origin main
|
|
origin --all
|
|
origin --branches
|
|
--mirror origin
|
|
origin :
|
|
origin +:
|
|
origin refs/heads/*:refs/heads/*
|
|
origin 'refs/heads/*:refs/heads/*'
|
|
origin feat main
|
|
-o ci.skip origin "main"
|
|
CASES
|
|
expect refuse main "git push origin main"
|
|
|
|
# Every prefix with an explicit destination and with a branch-dependent form.
|
|
for prefix in "git push --force" "git push -f" "git push --force-with-lease" \
|
|
"git push --force-with-lease=main:abc123" "rtk git push" "git -c k=v push" \
|
|
"git -C . push" "git add . && git push"; do
|
|
expect refuse feat "$prefix origin main"
|
|
expect refuse feat "$prefix origin HEAD:main"
|
|
expect refuse main "$prefix"
|
|
expect refuse main "$prefix origin HEAD"
|
|
expect allow feat "$prefix"
|
|
expect allow feat "$prefix origin HEAD"
|
|
done
|
|
|
|
echo "== Refused while on main, allowed on a feature branch"
|
|
while IFS= read -r args; do
|
|
expect refuse main "git push $args"
|
|
expect allow feat "git push $args"
|
|
done << 'CASES'
|
|
|
|
origin
|
|
--follow-tags
|
|
-o ci.skip origin
|
|
--repo=origin
|
|
origin HEAD
|
|
origin @
|
|
origin 2>&1 | tail -5
|
|
CASES
|
|
expect refuse feat "git -C $WORK/on-main push"
|
|
expect refuse feat "git -C ../on-main push origin HEAD"
|
|
expect allow main "git -C $WORK/on-feat push"
|
|
|
|
echo "== Allowed from any branch"
|
|
while IFS= read -r cmd; do
|
|
expect allow main "$cmd"
|
|
done << 'CASES'
|
|
git push origin feat
|
|
git push -u origin feat
|
|
git push origin main:feat
|
|
git push origin main-fix
|
|
git push origin feature/main
|
|
git push --tags
|
|
git push origin --tags
|
|
git push origin tag v1
|
|
git push origin tag main
|
|
git push origin refs/tags/main
|
|
git push --dry-run origin main
|
|
git push --help
|
|
git push -h
|
|
git push -n origin main
|
|
git push -o main origin feat
|
|
git push origin 'refs/heads/feat/*:refs/heads/feat/*'
|
|
git status
|
|
git log origin main
|
|
git commit -m "git push origin main"
|
|
git commit -m "wip; git push origin main"
|
|
echo git push origin main
|
|
echo "git push origin main"
|
|
git switch -c feat && git push -u origin HEAD
|
|
git checkout -b feat && git push
|
|
cd /somewhere/else && git push
|
|
git push origin "main
|
|
CASES
|
|
|
|
echo "== Earlier branch or directory changes"
|
|
while IFS=$'\t' read -r want branch cmd; do
|
|
expect "$want" "$branch" "${cmd//WORK/$WORK}"
|
|
done << 'CASES'
|
|
allow main gh pr checkout 5 && git push
|
|
allow main tea pr checkout 5 && git push
|
|
allow main rtk gh pr checkout 5 && git push
|
|
allow main git branch -m feat && git push -u origin HEAD
|
|
allow main git branch -M feat && git push -u origin HEAD
|
|
allow main git branch --move feat && git push -u origin HEAD
|
|
allow main git switch feat && cd sub && git push
|
|
allow main git checkout main && git pull && git checkout -b x && git push -u origin HEAD
|
|
refuse main git branch -a && git push
|
|
allow feat git switch main && git pull && git rebase main feat && git push
|
|
allow main git fetch && git rebase origin/main feat && git push --force-with-lease
|
|
allow main git rebase --onto main old feat && git push
|
|
refuse main git rebase origin/main && git push
|
|
refuse main git rebase --onto origin/main old && git push
|
|
refuse main git rebase -x true origin/main && git push
|
|
allow feat cd WORK/on-main && git switch main && git pull; cd WORK/on-feat && git push
|
|
allow main echo feat | xargs git push origin
|
|
allow main echo feat | xargs -n 1 git push origin HEAD
|
|
refuse feat git checkout main && git merge feat && git push
|
|
refuse feat git switch main && git pull && git push origin HEAD
|
|
refuse feat git -C WORK/on-feat switch main && git -C WORK/on-feat push
|
|
refuse feat cd WORK/on-main && git push
|
|
refuse feat cd ../on-main && git push origin HEAD
|
|
refuse feat cd WORK && cd on-main && git push
|
|
allow main cd WORK/on-feat && git push
|
|
allow main cd "$(git rev-parse --show-toplevel)" && git push
|
|
allow main cd $REPO/.. && git push
|
|
allow main cd - && git push
|
|
allow feat (cd WORK/on-main && git pull) && git push
|
|
refuse main (cd WORK/on-feat && git pull) && git push
|
|
allow main git --git-dir=WORK/on-feat/.git push
|
|
allow main git --git-dir WORK/on-feat/.git push origin HEAD
|
|
allow main GIT_DIR=WORK/on-feat/.git git push
|
|
refuse feat git --git-dir=WORK/on-feat/.git push origin main
|
|
CASES
|
|
|
|
echo "== Shell shapes"
|
|
expect refuse feat "exec git push origin main"
|
|
expect refuse feat "echo x | xargs git push origin main"
|
|
expect refuse feat $'git push origin ma\\\nin'
|
|
expect allow feat $'cat > a <<\'A\' && cat > b <<\'B\'\none\nA\ngit push origin main\nB'
|
|
expect allow feat $'cat > a <<\'END OF\'\ngit push origin main\nEND OF'
|
|
expect refuse feat $'git commit -m "explain <<EOF"\ngit push origin main'
|
|
expect refuse feat $'echo $((1<<2))\ngit push origin main'
|
|
printf -v many_heredocs 'cat <<EOF%d\n' {1..5000}
|
|
expect allow feat "${many_heredocs}git log; echo push"
|
|
expect refuse feat "cd /somewhere/else && git push origin main"
|
|
expect refuse feat "git switch -c x && git push origin HEAD:main"
|
|
expect refuse main "git checkout -- file.txt && git push"
|
|
expect refuse feat "timeout 60 git push origin main"
|
|
expect refuse feat "if true; then git push origin main; fi"
|
|
expect refuse feat "(git push origin main)"
|
|
expect refuse feat "git push origin main 2>&1 | tail -5"
|
|
expect refuse feat $'git push \\\n origin main'
|
|
expect refuse feat $'# don\'t forget to push\ngit push origin main'
|
|
expect refuse feat $'git status\ngit push origin main'
|
|
expect allow feat $'cat > notes.md <<\'EOF\'\ngit push origin main\nit\'s text\nEOF'
|
|
expect allow feat $'cat <<-EOF\n\tgit push origin main\n\tEOF'
|
|
expect allow feat $'git commit -m "$(cat <<\'EOF\'\nfix: x\n\ngit push origin main\nEOF\n)"'
|
|
expect allow feat $'git commit -m "fix: x\n\ngit push origin main"'
|
|
expect refuse feat $'cat > notes.md <<EOF\nit\'s text\nEOF\ngit push origin main'
|
|
|
|
echo "== Runtime: acts only from .claude/hooks/"
|
|
for d in .github/hooks/scripts/git/.apm/hooks .codex/hooks/git/.apm/hooks elsewhere; do
|
|
got="$(decision "$WORK/$d/refuse-push-to-main.py" "$WORK/on-main" "git push origin main")"
|
|
if [[ "$got" == allow ]]; then pass; else fail "$d: want allow, got $got"; fi
|
|
done
|
|
|
|
echo "== Opt-out: only from the hook's own environment"
|
|
export GIT_ALLOW_PUSH_TO_MAIN=1
|
|
expect allow main "git push origin main"
|
|
export GIT_ALLOW_PUSH_TO_MAIN=0
|
|
expect refuse main "git push origin main"
|
|
unset GIT_ALLOW_PUSH_TO_MAIN
|
|
expect refuse main "GIT_ALLOW_PUSH_TO_MAIN=1 git push origin main"
|
|
expect refuse main "env GIT_ALLOW_PUSH_TO_MAIN=1 git push origin main"
|
|
expect refuse main "export GIT_ALLOW_PUSH_TO_MAIN=1 && git push origin main"
|
|
|
|
echo "== hooks.json"
|
|
if python3 - "$HOOKS_DIR/hooks.json" << 'PY'
|
|
import json, sys
|
|
(entry,) = json.load(open(sys.argv[1]))["hooks"]["PreToolUse"]
|
|
(hook,) = entry["hooks"]
|
|
assert entry["matcher"] == "Bash"
|
|
assert hook["command"] == "${CLAUDE_PLUGIN_ROOT}/.apm/hooks/refuse-push-to-main.py"
|
|
assert "onFailure" not in hook
|
|
PY
|
|
then pass; else fail "hooks.json does not wire the script as expected"; fi
|
|
|
|
echo
|
|
echo "Passed: $PASS Failed: $FAIL"
|
|
[[ $FAIL -eq 0 && $PASS -gt 0 ]]
|