fix(hooks): scrub git env before apm pre-push hooks run
Why: git push from a linked worktree exports an absolute GIT_DIR=<main>/.git/worktrees/<name> to its hooks. apm audit --ci's drift replay (apm 0.28.0) runs git init / git checkout in a temp dir with an env copied from os.environ, so the inherited GIT_DIR won: init wrote core.bare=true into the shared .git/config and checkout moved the worktree's HEAD to the lock-pinned commit. tests/run-tests.sh already scrubbed these vars for itself; the apm hooks never pass through it. Implementation Notes: - scripts/lib/clean-git-env.sh unsets GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_COMMON_DIR, then everything git rev-parse --local-env-vars lists (a superset). - scripts/apm-audit-ci.sh sources it; apm-pack-check-clean now runs via the new scripts/apm-pack-check-clean.sh wrapper, which sources it too. - tests/test-apm-hooks-git-env.sh runs both hooks from a linked worktree of a throwaway repo against a stub apm that runs git init/checkout in a temp dir, asserting core.bare stays false, the worktree keeps its branch, and apm sees no GIT_DIR. - LESSONS.md gets the incident entry and a correction to 2026-11-13: git exports GIT_DIR to hooks only from a linked worktree. gates.md documents the scrubbed env. Fixes #204 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
1 parent
bf86679d42
commit
f2c483ca2c
8 files changed
+191
-7
No files matched your search
@@ -741,7 +741,7 @@ EXPECTED = {
|
||||
'check-apm-agents-valid': (
|
||||
'bash scripts/check-apm-agents-valid.sh', ['pre-push']),
|
||||
'apm-pack-check-clean': (
|
||||
'apm pack --check-versions --check-clean --dry-run', ['pre-push']),
|
||||
'scripts/apm-pack-check-clean.sh', ['pre-push']),
|
||||
'check-scope-walkup-sync': (
|
||||
'bash scripts/check-scope-walkup-sync.sh', ['pre-push']),
|
||||
'check-provenance-corpus': (
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
#!/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
|
||||
|
||||
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 ]]
|
||||
Reference in new issue
Block a user