fix(hooks): scrub git env before apm pre-push hooks run #229

Merged
Defame1297 merged 3 commits from fix/apm-audit-git-env into main 2026-11-27 08:04:31 +00:00
5 changed files with 16 additions and 18 deletions
Showing only changes of commit 190d9fb322 - Show all commits

No files matched your search

+6 -5
View File
@@ -89,9 +89,9 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)).
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`.
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`.
### `check-skill-version-bump`
@@ -1166,8 +1166,9 @@ 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
`GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, `GIT_COMMON_DIR`, `GIT_PREFIX` and
`GIT_IMPLICIT_WORK_TREE`. It deliberately keeps the config-passing vars (`GIT_CONFIG_PARAMETERS`,
`GIT_CONFIG_COUNT`), because they carry `git -c` overrides apm's remote clones may need. The reason: `git push` from a **linked worktree** exports an
absolute `GIT_DIR=<main>/.git/worktrees/<name>` 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
+2 -2
View File
@@ -2,8 +2,8 @@
# 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".
# scripts/lib/clean-git-env.sh and issue #204, and docs/spec/gates.md,
# "apm-audit-ci", the "apm runs with git's repo-locating env scrubbed" paragraph.
set -euo pipefail
# shellcheck source=scripts/lib/clean-git-env.sh
+3 -9
View File
@@ -12,15 +12,9 @@
# 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.
# Config-passing vars (GIT_CONFIG_PARAMETERS, GIT_CONFIG_COUNT) are kept on purpose: they carry `git -c` overrides apm's remote clones may need.
#
# 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
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR \
GIT_PREFIX GIT_IMPLICIT_WORK_TREE
+4 -2
View File
@@ -33,8 +33,10 @@
# TEST_DIR — override root to search for test-*.sh (default: REPO_ROOT); used by tests.
set -euo pipefail
# Git exports GIT_DIR (and can export GIT_WORK_TREE / GIT_INDEX_FILE) to the
# hooks it runs, so under the run-tests pre-push hook every `git -C "$tmp" ...`
# When the push runs from a linked worktree, git exports an absolute GIT_DIR
# (and can export GIT_WORK_TREE / GIT_INDEX_FILE) to the hooks it runs (#204;
# from the main checkout it exports none), so under the run-tests pre-push
# hook every `git -C "$tmp" ...`
# a fixture runs resolves to THIS repo, not the temp one -- the
# validate-provenance-skill.bats commit_as_base helper flipped core.bare=true and
# emptied the index here on every push. Suites build their own throwaway repos;
+1
View File
@@ -17,6 +17,7 @@ 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
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PASS=0