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

+26
View File
@@ -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=<main>/.git/worktrees/<name> 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