#!/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