When git push runs from a linked worktree, git exports an absolute GIT_DIR=<main>/.git/worktrees/<name> to its hooks. From the main checkout it exports none, which is why pushing from the main checkout never reproduced the bug.
apm audit --ci (apm 0.28.0) has a drift replay that runs git init and git checkout in temp dirs. It builds their environment from os.environ, so the inherited GIT_DIR takes over:
core.bare flip:git init in the temp dir reinitialises the worktree's gitdir. Git judges that repo to be bare and writes core.bare=true to the shared .git/config, which breaks the main checkout.
Detached HEAD: a later git checkout <locked sha> moves the linked worktree's HEAD and index to the commit pinned in the lock.
Change
scripts/lib/clean-git-env.sh (new): a sourced helper that unsets GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_COMMON_DIR, GIT_PREFIX and GIT_IMPLICIT_WORK_TREE. It deliberately keeps GIT_CONFIG_PARAMETERS and GIT_CONFIG_COUNT, so any git -c overrides still reach apm.
scripts/apm-audit-ci.sh: sources the helper before any apm call. pre-commit runs hooks from the worktree root, so normal repo discovery still finds the right repo.
scripts/apm-pack-check-clean.sh (new): a wrapper around the apm-pack-check-clean hook, which used to call apm directly. .pre-commit-config.yaml and the ADR-0020 contract test are updated to match.
tests/test-apm-hooks-git-env.sh (new): an offline regression test.
Setup: a stub apm runs git init and a checkout in a temp dir, and each hook runs from a linked worktree with GIT_DIR exported.
It asserts that core.bare stays false, the worktree stays on its branch, and the stub never sees GIT_DIR.
Docs:
LESSONS.md gets a new entry, and its 2026-11-13 entry is corrected (git exports GIT_DIR only from linked worktrees).
docs/spec/gates.md now explains the scrubbed environment.
The tests/run-tests.sh comment is aligned with the corrected wording.
Verification
New test: 8/8 pass. Against main's scripts it fails 4 of 5 assertions, reproducing core.bare=true, the detached HEAD and the leaked GIT_DIR.
test-adr0020-contract.sh passes 45/45, test-run-tests.sh 24/24, and shellcheck -x is clean.
Every pre-push hook passed on push.
Clean-context review: nothing blocking. Its non-blocking findings are addressed in 190d9fb.
Out of scope / follow-ups
The real bug is in apm: _try_sparse_checkout and the GitPython checkout should use apm's own git_subprocess_env(). Worth reporting upstream.
This PR doesn't touch AGENTS.md. Until apm is fixed, running apm install in a new worktree before its first push avoids the clone path entirely.
Fixes #204 (and its duplicate #198)
## Root cause (reproduced in a throwaway clone)
When `git push` runs from a **linked worktree**, git exports an absolute `GIT_DIR=<main>/.git/worktrees/<name>` to its hooks. From the main checkout it exports none, which is why pushing from the main checkout never reproduced the bug.
`apm audit --ci` (apm 0.28.0) has a drift replay that runs `git init` and `git checkout` in temp dirs. It builds their environment from `os.environ`, so the inherited `GIT_DIR` takes over:
1. **`core.bare` flip:** `git init` in the temp dir reinitialises the worktree's gitdir. Git judges that repo to be bare and writes `core.bare=true` to the shared `.git/config`, which breaks the main checkout.
2. **Detached HEAD:** a later `git checkout <locked sha>` moves the linked worktree's HEAD and index to the commit pinned in the lock.
## Change
- **`scripts/lib/clean-git-env.sh` (new):** a sourced helper that unsets `GIT_DIR`, `GIT_WORK_TREE`, `GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, `GIT_COMMON_DIR`, `GIT_PREFIX` and `GIT_IMPLICIT_WORK_TREE`. It deliberately keeps `GIT_CONFIG_PARAMETERS` and `GIT_CONFIG_COUNT`, so any `git -c` overrides still reach apm.
- **`scripts/apm-audit-ci.sh`:** sources the helper before any apm call. pre-commit runs hooks from the worktree root, so normal repo discovery still finds the right repo.
- **`scripts/apm-pack-check-clean.sh` (new):** a wrapper around the `apm-pack-check-clean` hook, which used to call `apm` directly. `.pre-commit-config.yaml` and the ADR-0020 contract test are updated to match.
- **`tests/test-apm-hooks-git-env.sh` (new):** an offline regression test.
- Setup: a stub `apm` runs `git init` and a checkout in a temp dir, and each hook runs from a linked worktree with `GIT_DIR` exported.
- It asserts that `core.bare` stays false, the worktree stays on its branch, and the stub never sees `GIT_DIR`.
- **Docs:**
- `LESSONS.md` gets a new entry, and its 2026-11-13 entry is corrected (git exports `GIT_DIR` only from linked worktrees).
- `docs/spec/gates.md` now explains the scrubbed environment.
- The `tests/run-tests.sh` comment is aligned with the corrected wording.
## Verification
- New test: 8/8 pass. Against `main`'s scripts it fails 4 of 5 assertions, reproducing `core.bare=true`, the detached HEAD and the leaked `GIT_DIR`.
- `test-adr0020-contract.sh` passes 45/45, `test-run-tests.sh` 24/24, and `shellcheck -x` is clean.
- Every pre-push hook passed on push.
- Clean-context review: nothing blocking. Its non-blocking findings are addressed in 190d9fb.
## Out of scope / follow-ups
- The real bug is in apm: `_try_sparse_checkout` and the GitPython checkout should use apm's own `git_subprocess_env()`. Worth reporting upstream.
- This PR doesn't touch AGENTS.md. Until apm is fixed, running `apm install` in a new worktree before its first push avoids the clone path entirely.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
Claude
added the Kind/Bug label 2026-11-26 23:50:37 +00:00
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
Why: the `git rev-parse --local-env-vars` loop in clean-git-env.sh also
unset GIT_CONFIG_PARAMETERS / GIT_CONFIG_COUNT, silently dropping `git -c`
overrides (extraHeader, credentials, safe.directory) that apm's remote
clones may need. Only the repo-locating vars caused #204.
Implementation Notes:
- clean-git-env.sh now unsets an explicit list only: GIT_DIR,
GIT_WORK_TREE, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_COMMON_DIR,
GIT_PREFIX, GIT_IMPLICIT_WORK_TREE; gates.md updated to match.
- apm-pack-check-clean.sh points at the real gates.md paragraph.
- run-tests.sh's comment now says git exports GIT_DIR to hooks only from
a linked worktree, matching LESSONS.md.
- test-apm-hooks-git-env.sh isolates from global/system git config.
- Reflow the "Two of these shell out to apm" paragraph in gates.md.
Refs #204
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Fixes #204 (and its duplicate #198)
Root cause (reproduced in a throwaway clone)
When
git pushruns from a linked worktree, git exports an absoluteGIT_DIR=<main>/.git/worktrees/<name>to its hooks. From the main checkout it exports none, which is why pushing from the main checkout never reproduced the bug.apm audit --ci(apm 0.28.0) has a drift replay that runsgit initandgit checkoutin temp dirs. It builds their environment fromos.environ, so the inheritedGIT_DIRtakes over:core.bareflip:git initin the temp dir reinitialises the worktree's gitdir. Git judges that repo to be bare and writescore.bare=trueto the shared.git/config, which breaks the main checkout.git checkout <locked sha>moves the linked worktree's HEAD and index to the commit pinned in the lock.Change
scripts/lib/clean-git-env.sh(new): a sourced helper that unsetsGIT_DIR,GIT_WORK_TREE,GIT_INDEX_FILE,GIT_OBJECT_DIRECTORY,GIT_COMMON_DIR,GIT_PREFIXandGIT_IMPLICIT_WORK_TREE. It deliberately keepsGIT_CONFIG_PARAMETERSandGIT_CONFIG_COUNT, so anygit -coverrides still reach apm.scripts/apm-audit-ci.sh: sources the helper before any apm call. pre-commit runs hooks from the worktree root, so normal repo discovery still finds the right repo.scripts/apm-pack-check-clean.sh(new): a wrapper around theapm-pack-check-cleanhook, which used to callapmdirectly..pre-commit-config.yamland the ADR-0020 contract test are updated to match.tests/test-apm-hooks-git-env.sh(new): an offline regression test.apmrunsgit initand a checkout in a temp dir, and each hook runs from a linked worktree withGIT_DIRexported.core.barestays false, the worktree stays on its branch, and the stub never seesGIT_DIR.LESSONS.mdgets a new entry, and its 2026-11-13 entry is corrected (git exportsGIT_DIRonly from linked worktrees).docs/spec/gates.mdnow explains the scrubbed environment.tests/run-tests.shcomment is aligned with the corrected wording.Verification
main's scripts it fails 4 of 5 assertions, reproducingcore.bare=true, the detached HEAD and the leakedGIT_DIR.test-adr0020-contract.shpasses 45/45,test-run-tests.sh24/24, andshellcheck -xis clean.190d9fb.Out of scope / follow-ups
_try_sparse_checkoutand the GitPython checkout should use apm's owngit_subprocess_env(). Worth reporting upstream.apm installin a new worktree before its first push avoids the clone path entirely.🤖 Generated with Claude Code
https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi