Cotton code
44 lines · 1.6 KB · TypeScriptRaw
// The variables git exports to a hook, dropped before a test can inherit them.//// WHY. Git runs a hook with `GIT_DIR` (and, in a worktree, `GIT_COMMON_DIR`,// `GIT_INDEX_FILE`, …) pointing at the repository being pushed. `pre-push`// runs `pnpm verify:fast`, which runs `scripts/__tests__`, and those tests// build scratch repositories with `git init` / `git commit` in a temp// directory. With `GIT_DIR` inherited, every one of those commands acts on THIS// repository instead: on 2026-09-27 a push wrote the fixtures' commits ("base",// "squash of both") onto `main`, set `core.bare = true` and `user.name = t` in// `.git/config`, added a remote at `/nonexistent/cotton.git` and overwrote// `origin/main`. A test must never be able to see the repository it was// launched from unless it names it.//// The list is `git rev-parse --local-env-vars`, written out so the config needs// no git to load.export const GIT_LOCAL_ENV = [  "GIT_ALTERNATE_OBJECT_DIRECTORIES",  "GIT_CONFIG",  "GIT_CONFIG_PARAMETERS",  "GIT_CONFIG_COUNT",  "GIT_OBJECT_DIRECTORY",  "GIT_DIR",  "GIT_WORK_TREE",  "GIT_IMPLICIT_WORK_TREE",  "GIT_GRAFT_FILE",  "GIT_INDEX_FILE",  "GIT_NO_REPLACE_OBJECTS",  "GIT_REPLACE_REF_BASE",  "GIT_PREFIX",  "GIT_SHALLOW_FILE",  "GIT_COMMON_DIR",];/** Drops them from `env` and answers the names it found set. */export function dropGitLocalEnv(env: NodeJS.ProcessEnv = process.env): string[] {  const dropped: string[] = [];  for (const name of GIT_LOCAL_ENV) {    if (env[name] === undefined) continue;    delete env[name];    dropped.push(name);  }  return dropped;}