Files
Holocron/tests/test-apm-hooks-git-env.sh
T
Defame1297andClaude Opus 5.5 190d9fb322 fix(hooks): keep git -c overrides when scrubbing apm's git env
Why: the `git rev-parse --local-env-vars` loop in clean-git-env.sh also
unset GIT_CONFIG_PARAMETERS / GIT_CONFIG_COUNT, silently dropping `git -c`
overrides (extraHeader, credentials, safe.directory) that apm's remote
clones may need. Only the repo-locating vars caused #204.

Implementation Notes:
- clean-git-env.sh now unsets an explicit list only: GIT_DIR,
  GIT_WORK_TREE, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_COMMON_DIR,
  GIT_PREFIX, GIT_IMPLICIT_WORK_TREE; gates.md updated to match.
- apm-pack-check-clean.sh points at the real gates.md paragraph.
- run-tests.sh's comment now says git exports GIT_DIR to hooks only from
  a linked worktree, matching LESSONS.md.
- test-apm-hooks-git-env.sh isolates from global/system git config.
- Reflow the "Two of these shell out to apm" paragraph in gates.md.

Refs #204

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
2026-11-26 23:35:31 +00:00

123 lines
4.0 KiB
Bash

#!/usr/bin/env bash
# Regression test for issue #204: the apm pre-push hooks must not hand a
# hook-exported GIT_DIR to apm.
#
# `git push` from a LINKED worktree exports GIT_DIR=<main>/.git/worktrees/<name>
# (absolute) to its hooks; from the main checkout it exports none. apm 0.28.0's
# drift replay runs `git init` / `git checkout` in a temp dir with an env copied
# from os.environ, so the inherited GIT_DIR wins: `git init` wrote
# core.bare=true into the shared .git/config, and a later checkout moved the
# worktree's HEAD to the lock-pinned commit.
#
# The stub `apm` below reproduces that pattern -- record GIT_DIR, then `git init`
# and `git checkout` inside a fresh `mktemp -d` -- and each hook script is run
# from a linked worktree of a throwaway repo with GIT_DIR exported the way git
# exports it. Nothing here touches the repo this suite lives in.
set -euo pipefail
# This suite builds its own repos; it must never inherit the caller's.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
FIXTURES=()
cleanup() { [[ ${#FIXTURES[@]} -eq 0 ]] || rm -rf "${FIXTURES[@]}"; }
trap cleanup EXIT
g() { git -c user.name=fixture -c user.email=[email protected] "$@"; }
# Builds <dir>/main (two commits on `main`) and <dir>/wt, a linked worktree on
# branch `feature`, with a plugins/demo/ package dir so the audit script's
# per-package loop has something to iterate. Also writes <dir>/bin/apm, the stub.
make_fixture() {
local dir="$1"
mkdir -p "$dir/bin"
g init -q -b main "$dir/main"
echo one > "$dir/main/f"
g -C "$dir/main" add f
g -C "$dir/main" commit -q -m one
echo two > "$dir/main/f"
g -C "$dir/main" commit -q -am two
g -C "$dir/main" worktree add -q -b feature "$dir/wt"
mkdir -p "$dir/wt/plugins/demo"
# The "locked sha" the replay checks out: the first commit.
local locked
locked="$(g -C "$dir/main" rev-parse HEAD~1)"
cat > "$dir/bin/apm" <<EOF
#!/usr/bin/env bash
printf '%s\n' "\${GIT_DIR-<unset>}" >> "$dir/apm-saw-git-dir"
tmp="\$(mktemp -d)"
( cd "\$tmp" && git init -q . >/dev/null 2>&1; git checkout -q --detach $locked >/dev/null 2>&1 ) || true
rm -rf "\$tmp"
exit 0
EOF
chmod +x "$dir/bin/apm"
}
# check_hook <label> <script>
check_hook() {
local label="$1" script="$2" dir rc=0
dir="$(mktemp -d)"
FIXTURES+=("$dir")
make_fixture "$dir"
echo ""
echo "--- $label: run from a linked worktree with GIT_DIR exported ---"
if [[ ! -f "$script" ]]; then
fail "$label: $script does not exist"
return
fi
(
cd "$dir/wt"
export GIT_DIR="$dir/main/.git/worktrees/wt"
export PATH="$dir/bin:$PATH"
bash "$script"
) > "$dir/out" 2>&1 || rc=$?
if [[ "$rc" -eq 0 ]]; then
pass "$label: exits 0 when apm passes"
else
fail "$label: exited $rc; output: $(cat "$dir/out")"
fi
local bare
bare="$(git -C "$dir/main" config --get core.bare || true)"
if [[ "$bare" == "false" ]]; then
pass "$label: main repo core.bare is still false"
else
fail "$label: main repo core.bare is '$bare', expected false"
fi
local branch
branch="$(git -C "$dir/wt" symbolic-ref -q --short HEAD || echo '<detached>')"
if [[ "$branch" == "feature" ]]; then
pass "$label: worktree still on its branch"
else
fail "$label: worktree HEAD is '$branch', expected feature"
fi
if [[ ! -s "$dir/apm-saw-git-dir" ]]; then
fail "$label: stub apm never ran"
elif grep -qvxF '<unset>' "$dir/apm-saw-git-dir"; then
fail "$label: apm inherited GIT_DIR: $(sort -u "$dir/apm-saw-git-dir" | tr '\n' ' ')"
else
pass "$label: apm saw no GIT_DIR"
fi
}
check_hook apm-audit-ci "$REPO_ROOT/scripts/apm-audit-ci.sh"
check_hook apm-pack-check-clean "$REPO_ROOT/scripts/apm-pack-check-clean.sh"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ "$FAIL" -eq 0 ]]