From f2c483ca2c63b95e1fd119768212b5cecf7f449c Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Thu, 26 Nov 2026 23:21:41 +0000 Subject: [PATCH] fix(hooks): scrub git env before apm pre-push hooks run Why: git push from a linked worktree exports an absolute GIT_DIR=
/.git/worktrees/ 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 Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi --- .pre-commit-config.yaml | 4 +- LESSONS.md | 8 ++- docs/spec/gates.md | 18 ++++- scripts/apm-audit-ci.sh | 7 ++ scripts/apm-pack-check-clean.sh | 12 ++++ scripts/lib/clean-git-env.sh | 26 +++++++ tests/test-adr0020-contract.sh | 2 +- tests/test-apm-hooks-git-env.sh | 121 ++++++++++++++++++++++++++++++++ 8 files changed, 191 insertions(+), 7 deletions(-) create mode 100755 scripts/apm-pack-check-clean.sh create mode 100644 scripts/lib/clean-git-env.sh create mode 100644 tests/test-apm-hooks-git-env.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b7ca9e6..060bb5b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -173,8 +173,8 @@ repos: - id: apm-pack-check-clean name: apm pack --check-clean - description: Release gate -- verify .claude-plugin/marketplace.json still matches what apm.yml + .apm/ would currently generate, and that per-package versions agree with the per_package versioning strategy. Closes issue #90's deferred item 3 (a check-clean-equivalent gate) using apm's own flag instead of custom drift logic. - entry: apm pack --check-versions --check-clean --dry-run + description: Release gate -- verify .claude-plugin/marketplace.json still matches what apm.yml + .apm/ would currently generate, and that per-package versions agree with the per_package versioning strategy. Closes issue #90's deferred item 3 (a check-clean-equivalent gate) using apm's own flag instead of custom drift logic. Runs via scripts/apm-pack-check-clean.sh so apm gets a scrubbed env (issue #204). + entry: scripts/apm-pack-check-clean.sh language: system stages: [pre-push] pass_filenames: false diff --git a/LESSONS.md b/LESSONS.md index 55a97c2..239a4c8 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -144,8 +144,14 @@ The guard added above (`tests/test-no-pipefail-early-exit-grep.sh`) reported the ## 2026-11-13 — Hook-exported GIT_DIR turned test fixtures loose on the real repo -Under the `run-tests` pre-push hook, git exports `GIT_DIR` to the hook, so a bats fixture's `git -C "$tmp" init/commit/update-ref` hit this repo instead of its temp dir: 84 failures, `core.bare=true` and an emptied index on every push. The 2026-07-05 "repo root is a bare checkout" entry was likely this bug's symptom, not the repo's real shape. Fix: `tests/run-tests.sh` unsets git's repo-locating env vars before any suite runs. Rule: a test that builds its own repo must never inherit the caller's git environment. +Under the `run-tests` pre-push hook, git exported `GIT_DIR` to the hook, so a bats fixture's `git -C "$tmp" init/commit/update-ref` hit this repo instead of its temp dir: 84 failures, `core.bare=true` and an emptied index on every push. The 2026-07-05 "repo root is a bare checkout" entry was likely this bug's symptom, not the repo's real shape. Fix: `tests/run-tests.sh` unsets git's repo-locating env vars before any suite runs. Rule: a test that builds its own repo must never inherit the caller's git environment. + +*Corrected 2026-11-26 (#204):* git exports `GIT_DIR` to hooks only when the push runs from a **linked worktree**, where it is the absolute `
/.git/worktrees/`. From the main checkout it exports none. So the bug reproduces only on pushes from a worktree. ## 2026-11-22 — A local-path install skips the validation a remote install runs #205 shipped `ansible` and `k8s` as `apm.yml`-only skeletons after a scratch consumer installed all four new packages from `../pkgs/` with exit 0. Fetched from the remote, the same packages fail: apm accepts a remote subdirectory only when it has `.apm/`, declared dependencies or a `skills/` bundle (`detect_package_type` in `apm_cli/models/validation.py`). `gitops` failed too, through its dependency on `k8s`. So `main` published uninstallable packages until #206 added `.apm/.gitkeep`. A green local-path install says nothing about whether consumers can install. Fix: before merging a new or restructured package, push the branch and install it with `git:`/`path:`/`ref: `, or run `validate_apm_package` on a `git archive` export. `file://` URLs are rejected in the object form, so they can't stand in for the remote. + +## 2026-11-26 — A hook's GIT_DIR leaks through any tool that runs git + +#204: pushing from a linked worktree left the shared `.git/config` with `core.bare=true` and the worktree on a detached HEAD at the lock-pinned commit. git exports an absolute `GIT_DIR` to hooks run from a linked worktree. `apm-audit-ci` ran `apm audit --ci`, and its drift replay (apm 0.28.0) runs `git init` and `git checkout` in a temp dir with an env copied from `os.environ`. The inherited `GIT_DIR` won, so both operations hit this repo instead of the temp dir. The 2026-11-13 fix did not cover this: it scrubbed the env only inside `tests/run-tests.sh`, and the apm hooks never pass through that file. Fix: `scripts/lib/clean-git-env.sh` unsets the repo-locating vars, and both `scripts/apm-audit-ci.sh` and the new `scripts/apm-pack-check-clean.sh` source it before calling apm. `tests/test-apm-hooks-git-env.sh` reproduces the leak with a stub `apm`. Rule: any hook that shells out to a tool that runs git must scrub `GIT_*` first. Scrubbing one entry point does not protect the others. diff --git a/docs/spec/gates.md b/docs/spec/gates.md index fb04948..f708842 100644 --- a/docs/spec/gates.md +++ b/docs/spec/gates.md @@ -73,7 +73,7 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)). | Hook | Guards | |---|---| | `apm-audit-ci` | `scripts/apm-audit-ci.sh` — `apm audit --ci` once per manifest, root plus each plugin package under `plugins/`, waiving only a package's `lockfile-exists` (see [below](#apm-audit-ci)) | -| `apm-pack-check-clean` | `apm pack --check-versions --check-clean --dry-run` — the compiled marketplace still matches what `apm.yml` + `.apm/` would generate, and per-package versions agree with the `per_package` strategy | +| `apm-pack-check-clean` | `scripts/apm-pack-check-clean.sh` — `apm pack --check-versions --check-clean --dry-run` with a scrubbed git env (see [`apm-audit-ci`](#apm-audit-ci)) — the compiled marketplace still matches what `apm.yml` + `.apm/` would generate, and per-package versions agree with the `per_package` strategy | **Host validators** (needs the `claude` CLI on PATH) @@ -87,8 +87,9 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)). |---|---| | `check-skill-version-bump` | fails if a skill directory changed since the pushed commit's merge-base with `main` without its `metadata.version` rising above both the merge-base's and `main`'s tip's (see [below](#check-skill-version-bump)) | -Two of these shell out to `apm`: `apm-audit-ci` and `apm-pack-check-clean`. The second is a bare -`apm …` entry and the first is `scripts/apm-audit-ci.sh`, which calls `apm` once per manifest, so +Two of these shell out to `apm`: `apm-audit-ci` and `apm-pack-check-clean`. The second is +`scripts/apm-pack-check-clean.sh`, a one-call wrapper, and the first is `scripts/apm-audit-ci.sh`, +which calls `apm` once per manifest, so without the CLI on PATH the push dies on a "command not found" from inside the hook. Install with `apm-install`, or `curl -sSL https://aka.ms/apm-unix | sh`; verify with `apm --version`. @@ -1163,6 +1164,17 @@ Verified: a malformed dependency entry passes `apm pack --check-versions --check-clean --dry-run` and fails `apm audit --ci` in that package's directory. Costs ~0.5s per package. +**apm runs with git's repo-locating env scrubbed.** Both apm hooks source +`scripts/lib/clean-git-env.sh` before calling `apm`, which unsets `GIT_DIR`, `GIT_WORK_TREE`, +`GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, `GIT_COMMON_DIR` and everything else +`git rev-parse --local-env-vars` lists. The reason: `git push` from a **linked worktree** exports an +absolute `GIT_DIR=
/.git/worktrees/` to its hooks, and from the main checkout it exports +none. `drift`'s install replay (apm 0.28.0) runs `git init` and `git checkout` in a temp dir with an +env copied from `os.environ`, so the inherited `GIT_DIR` wins. The `init` wrote `core.bare=true` into +the shared `.git/config`, and the `checkout` moved the worktree's HEAD to the lock-pinned commit +(issue #204). Pre-commit runs hooks from the worktree root, so once the vars are gone plain +discovery finds the right repo. `tests/test-apm-hooks-git-env.sh` pins this for both hooks. + **What it actually runs is asymmetric**, and the two manifest classes are not comparable. Verified by running `apm audit --ci` (apm 0.28.0) at the repo root and in `plugins/lint/`, reading the check names straight off its own compliance table: diff --git a/scripts/apm-audit-ci.sh b/scripts/apm-audit-ci.sh index ec06fff..27c2c31 100755 --- a/scripts/apm-audit-ci.sh +++ b/scripts/apm-audit-ci.sh @@ -55,6 +55,13 @@ set -uo pipefail +# Scrub git's repo-locating env before apm runs: pushed from a linked worktree, +# the hook inherits an absolute GIT_DIR, and apm's drift replay runs `git init` +# / `git checkout` in a temp dir with it -- flipping core.bare=true and moving +# the worktree's HEAD (issue #204). See the helper for the full account. +# shellcheck source=scripts/lib/clean-git-env.sh +source "$(dirname "${BASH_SOURCE[0]}")/lib/clean-git-env.sh" + readonly WAIVED_CHECK='declares dependencies but apm.lock.yaml is absent' readonly SOLE_FAILURE='1 of 1 check(s) failed' diff --git a/scripts/apm-pack-check-clean.sh b/scripts/apm-pack-check-clean.sh new file mode 100755 index 0000000..39cf327 --- /dev/null +++ b/scripts/apm-pack-check-clean.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# apm-pack-check-clean pre-push hook: `apm pack --check-versions --check-clean +# --dry-run`, run with git's repo-locating env scrubbed first. A script rather +# than a bare `apm ...` entry only so it can source that scrub -- see +# scripts/lib/clean-git-env.sh and issue #204. Full rationale for the gate +# itself: docs/spec/gates.md, "apm-pack-check-clean". +set -euo pipefail + +# shellcheck source=scripts/lib/clean-git-env.sh +source "$(dirname "${BASH_SOURCE[0]}")/lib/clean-git-env.sh" + +exec apm pack --check-versions --check-clean --dry-run diff --git a/scripts/lib/clean-git-env.sh b/scripts/lib/clean-git-env.sh new file mode 100644 index 0000000..afd143a --- /dev/null +++ b/scripts/lib/clean-git-env.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Scrubs git's repo-locating environment variables from the current shell, so +# every git process it spawns -- directly or through another tool -- discovers +# its repository from its own working directory. Issue #204. +# +# Why: `git push` from a LINKED worktree exports an absolute +# GIT_DIR=
/.git/worktrees/ to its hooks (from the main checkout it +# exports none). A hook that runs a tool which itself runs git in a temp dir -- +# apm's drift replay does `git init` / `git checkout` there with os.environ -- +# hands that GIT_DIR on, and the temp-dir git operates on THIS repo instead: +# core.bare=true in the shared .git/config, and the worktree's HEAD moved to a +# lock-pinned commit. Hooks run from the worktree root, so plain discovery +# finds the right repo once these are gone. +# +# The explicit list runs first so the scrub holds even if `git rev-parse +# --local-env-vars` is unavailable; that call then adds anything else git +# considers repo-local (GIT_CONFIG_PARAMETERS, GIT_PREFIX, ...), making the +# result a superset of the explicit list. +# +# Not meant to be executed directly -- source it before any git call. + +unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR +for _clean_git_env_var in $(git rev-parse --local-env-vars 2>/dev/null); do + unset "$_clean_git_env_var" +done +unset _clean_git_env_var diff --git a/tests/test-adr0020-contract.sh b/tests/test-adr0020-contract.sh index 6b97779..6b86ed4 100755 --- a/tests/test-adr0020-contract.sh +++ b/tests/test-adr0020-contract.sh @@ -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': ( diff --git a/tests/test-apm-hooks-git-env.sh b/tests/test-apm-hooks-git-env.sh new file mode 100644 index 0000000..b86b87b --- /dev/null +++ b/tests/test-apm-hooks-git-env.sh @@ -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=
/.git/worktrees/ +# (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=fixture@example.invalid "$@"; } + +# Builds /main (two commits on `main`) and /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 /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" <}" >> "$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