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:
Defame1297andClaude Opus 5.5 committed 2026-11-26 23:21:41 +00:00
1 parent bf86679d42
commit f2c483ca2c
8 files changed
+191 -7

No files matched your search

+7 -1
View File
@@ -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 `<main>/.git/worktrees/<name>`. 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/<name>` 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: <branch>`, 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.