From 8bbb73b0bd56e226fc6ddf19ff02c93259a8724c Mon Sep 17 00:00:00 2001 From: Ven Root Date: Wed, 21 Oct 2026 14:20:21 +0200 Subject: [PATCH] fix: isolate pre-push Git environment --- tools/scripts/git-hooks/pre-push | 6 +++++- tools/scripts/tests/test-pre-push-hook.sh | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tools/scripts/git-hooks/pre-push b/tools/scripts/git-hooks/pre-push index 32f7042..f3601ac 100755 --- a/tools/scripts/git-hooks/pre-push +++ b/tools/scripts/git-hooks/pre-push @@ -26,9 +26,13 @@ refs="$(cat 2>/dev/null || true)" # from a pre-commit gate run would point the mutation leg at an empty diff. unset MULL_DIFF_BASE top="$(git rev-parse --show-toplevel)" +# Git exports its repository context to hooks. The gate creates fixture +# repositories; they must not inherit this worktree's GIT_DIR or index. +mapfile -t repository_environment < <(git rev-parse --local-env-vars) +unset "${repository_environment[@]}" +cd "$top" || exit 1 if [[ "${WEK_GATE_FORCE:-0}" != "1" && -f "$top/tools/scripts/lib/gate_marker.sh" ]]; then - cd "$top" source tools/scripts/lib/gate_marker.sh pushed=0 covered=1 diff --git a/tools/scripts/tests/test-pre-push-hook.sh b/tools/scripts/tests/test-pre-push-hook.sh index d44f4ee..ee6ef43 100755 --- a/tools/scripts/tests/test-pre-push-hook.sh +++ b/tools/scripts/tests/test-pre-push-hook.sh @@ -64,6 +64,9 @@ make_repo() { echo run >> "$(dirname "${BASH_SOURCE[0]}")/../../build/gate-runs" [[ "$(readlink /proc/self/fd/0)" == /dev/null ]] && echo detached >> "$(dirname "${BASH_SOURCE[0]}")/../../build/gate-stdin" echo "base=${MULL_DIFF_BASE:-}" >> "$(dirname "${BASH_SOURCE[0]}")/../../build/gate-env" +if [[ -n ${STUB_FOREIGN_REPO:-} ]]; then + git -C "$STUB_FOREIGN_REPO" rev-parse --absolute-git-dir > "$(dirname "${BASH_SOURCE[0]}")/../../build/gate-foreign-dir" +fi exit "${STUB_GATE_RC:-0}" STUB chmod +x "$root/tools/scripts/preflight.sh" "$root/tools/scripts/git-hooks/pre-push" @@ -156,6 +159,21 @@ check "a delete push exits 0 without running the gate" "$(yes_if "$rc$(runs "$ro run_hook "$root" "" X=1 >/dev/null check "an empty ref list still runs the gate" "$(yes_if "$(runs "$root")" 1)" +# A real linked-worktree hook receives GIT_DIR from Git. The gate's fixture +# repositories must resolve their own Git context, not the pushed worktree's. +linked="$(mktemp -d /tmp/wek-hook-linked-XXXXXX)" +foreign="$(mktemp -d /tmp/wek-hook-foreign-XXXXXX)" +TMPS+=("$linked" "$foreign") +git -C "$root" worktree add -q --detach "$linked" HEAD +git -C "$foreign" init -q +mkdir -p "$linked/build" +out="$(cd "$linked" && printf '%s' "$(refline "$sha2" "$ZERO")" | + STUB_FOREIGN_REPO="$foreign" WEK_GATE_FORCE=1 \ + git -c core.hooksPath=tools/scripts/git-hooks hook run pre-push 2>&1)"; rc=$? +check "a real linked-worktree hook runs the gate once" "$(yes_if "$rc$(runs "$linked")" 01)" "$out" +check "the gate can resolve a foreign repository independently" \ + "$(yes_if "$(cat "$linked/build/gate-foreign-dir" 2>/dev/null)" "$foreign/.git")" "$out" + echo if [[ "$FAIL" -gt 0 ]]; then printf '%s%d passed, %d failed%s\n' "$RED" "$PASS" "$FAIL" "$RESET"