Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix: harden pre-push against GIT_DIR-leak core.bare corruption (#2984)

Nick Gomezdb37cd09f399
diff --git a/.github/scripts/bridge-public-pr-to-monorepo.mjs b/.github/scripts/bridge-public-pr-to-monorepo.mjs
index 1c3958f60..38a046704 100644
--- a/.github/scripts/bridge-public-pr-to-monorepo.mjs
+++ b/.github/scripts/bridge-public-pr-to-monorepo.mjs
@@ -36,7 +36,7 @@ function sanitizeErrorMessage(value) {
}
function run(command, args, options = {}) {
- // Drop inherited GIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE: every git spawn in
+ // Drop inherited GIT_* repo-targeting vars: every git spawn in
// this script targets an explicit clone/worktree via cwd, never the repo a
// calling git hook belongs to. In CI these variables are unset (no-op);
// locally they leak from pre-push/pre-commit hooks into harnesses that
@@ -46,7 +46,12 @@ function run(command, args, options = {}) {
const {
GIT_DIR: _d,
GIT_WORK_TREE: _w,
+ GIT_COMMON_DIR: _c,
GIT_INDEX_FILE: _i,
+ GIT_OBJECT_DIRECTORY: _o,
+ GIT_ALTERNATE_OBJECT_DIRECTORIES: _a,
+ GIT_NAMESPACE: _n,
+ GIT_PREFIX: _p,
...cleanEnv
} = { ...process.env, ...options.env };
try {
@@ -997,7 +1002,6 @@ async function syncPublicPr() {
internalPr,
forceDraft: hasConflicts,
});
-
}
async function closeLinkedInternalPr() {
diff --git a/.github/scripts/select-beta-to-promote.mjs b/.github/scripts/select-beta-to-promote.mjs
index 7ea3ac6e8..bdff2ae55 100644
--- a/.github/scripts/select-beta-to-promote.mjs
+++ b/.github/scripts/select-beta-to-promote.mjs
@@ -47,6 +47,7 @@ import { execFileSync, spawnSync } from 'node:child_process';
import { appendFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
import { computeStablePromotion, realGit } from '../../scripts/compute-stable-version.mjs';
+import { gitCleanEnv } from '../../scripts/git-clean-env.mjs';
const BETA_TAG_RE = /^v\d+\.\d+\.\d+-beta\.\d+$/;
const STABLE_TAG_RE = /^v\d+\.\d+\.\d+$/;
@@ -302,12 +303,14 @@ export function resolveTier({ armed, verdict, standardTarget, fastTarget }) {
function resolveLatestStableSha() {
const out = execFileSync('git', ['tag', '--list', 'v*', '--sort=-version:refname'], {
encoding: 'utf8',
+ env: gitCleanEnv(),
});
for (const line of out.split('\n')) {
const t = line.trim();
if (STABLE_TAG_RE.test(t)) {
return execFileSync('git', ['rev-parse', '--verify', `${t}^{commit}`], {
encoding: 'utf8',
+ env: gitCleanEnv(),
}).trim();
}
}
@@ -319,11 +322,13 @@ function makeRealIsAlreadyShipped(latestStableSha) {
if (!latestStableSha) return false; // no stable yet -> nothing is shipped
const betaSha = execFileSync('git', ['rev-parse', '--verify', `${betaTag}^{commit}`], {
encoding: 'utf8',
+ env: gitCleanEnv(),
}).trim();
// Distinguish a clean "not an ancestor" (exit 1) from an infra failure (any
// other non-zero), which must fail loud rather than read as "not shipped".
const res = spawnSync('git', ['merge-base', '--is-ancestor', betaSha, latestStableSha], {
encoding: 'utf8',
+ env: gitCleanEnv(),
});
if (res.status === 0) return true;
if (res.status === 1) return false;
@@ -369,7 +374,7 @@ export function makeResolveChangesetPrUrl(linkRepo) {
const subject = execFileSync(
'git',
['log', '-1', '--diff-filter=A', '--format=%s', '--', `.changeset/${changesetId}.md`],
- { encoding: 'utf8' },
+ { encoding: 'utf8', env: gitCleanEnv() },
).trim();
const m = /\(#(\d+)\)$/.exec(subject);
return m ? `https://github.com/${linkRepo}/pull/${m[1]}` : null;
@@ -438,6 +443,7 @@ async function main() {
const armed = process.env.FAST_TIER_ARMED === 'true';
const rawTags = execFileSync('git', ['tag', '--list', 'v*-beta.*', '--sort=-version:refname'], {
encoding: 'utf8',
+ env: gitCleanEnv(),
});
const betaTags = parseBetaTags(rawTags);
const latestStableSha = resolveLatestStableSha();
diff --git a/.github/scripts/select-beta-to-promote.test.mjs b/.github/scripts/select-beta-to-promote.test.mjs
index cefdd8b82..348beff70 100644
--- a/.github/scripts/select-beta-to-promote.test.mjs
+++ b/.github/scripts/select-beta-to-promote.test.mjs
@@ -4,6 +4,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, test } from 'vitest';
import { computeStablePromotion } from '../../scripts/compute-stable-version.mjs';
+import { gitCleanEnv } from '../../scripts/git-clean-env.mjs';
import {
evaluateFastTier,
makeResolveChangesetPrUrl,
@@ -582,6 +583,10 @@ afterEach(() => {
function makeRepoWithChangesets(commits) {
const dir = mkdtempSync(join(tmpdir(), 'ok-fast-tier-'));
tempRepos.push(dir);
+ // gitCleanEnv: git hooks export GIT_DIR, which overrides `cwd` repo
+ // discovery — without the scrub, this helper's `git init` re-initialises
+ // the CALLING hook's repo admin dir and corrupts its shared .git/config
+ // (core.bare=true). Same bug class as the bridge suite's helper.
const git = (...args) =>
execFileSync(
'git',
@@ -594,7 +599,7 @@ function makeRepoWithChangesets(commits) {
'commit.gpgsign=false',
...args,
],
- { cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] },
+ { cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], env: gitCleanEnv() },
);
git('init', '--quiet', '--initial-branch=main');
execFileSync('mkdir', ['-p', join(dir, '.changeset')]);
diff --git a/scripts/compute-stable-version.mjs b/scripts/compute-stable-version.mjs
index a28a91cea..ecb7335fb 100644
--- a/scripts/compute-stable-version.mjs
+++ b/scripts/compute-stable-version.mjs
@@ -46,6 +46,7 @@
import { spawnSync } from 'node:child_process';
import { appendFileSync, readFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
+import { gitCleanEnv } from './git-clean-env.mjs';
import { FIXED_GROUP_ANCHOR, bumpSemver, maxBumpType, parseFrontmatterBumpType } from './compute-next-beta.mjs';
const BETA_TAG_RE = /^v\d+\.\d+\.\d+-beta\.\d+$/;
@@ -296,7 +297,7 @@ function compareVersions(a, b) {
// git error.
export function runGit(args) {
- const res = spawnSync('git', args, { encoding: 'utf8' });
+ const res = spawnSync('git', args, { encoding: 'utf8', env: gitCleanEnv() });
if (res.status !== 0) {
throw new Error(`git ${args.join(' ')} failed (exit ${res.status}): ${String(res.stderr || '').trim()}`);
}
@@ -323,7 +324,7 @@ export const realGit = {
isAncestor: (a, b) => {
// Distinguish a clean "not an ancestor" (exit 1) from an infra failure
// (any other non-zero) — the latter must fail loud, not read as "false".
- const res = spawnSync('git', ['merge-base', '--is-ancestor', a, b], { encoding: 'utf8' });
+ const res = spawnSync('git', ['merge-base', '--is-ancestor', a, b], { encoding: 'utf8', env: gitCleanEnv() });
if (res.status === 0) return true;
if (res.status === 1) return false;
throw new Error(
diff --git a/scripts/git-clean-env.mjs b/scripts/git-clean-env.mjs
new file mode 100644
index 000000000..f7e2ed680
--- /dev/null
+++ b/scripts/git-clean-env.mjs
@@ -0,0 +1,31 @@
+/**
+ * Environment for spawning `git` with an explicit `cwd`/`-C` target.
+ *
+ * Git hooks (pre-push, pre-commit) export GIT_DIR — and in a linked
+ * worktree that value contradicts any explicit working directory the
+ * subprocess sets, failing with "fatal: this operation must be run in a
+ * work tree". Worse: with GIT_DIR inherited, `git init <path>` ignores its
+ * target and re-initialises the CALLER's worktree admin dir, writing
+ * core.bare=true into the shared .git/config. Every git spawn in these
+ * scripts means "the repo at the given path", never "whatever repo the
+ * calling hook belongs to", so the inherited GIT_* variables are always
+ * wrong here.
+ *
+ * Deletion list mirrors GIT_SCRUB_VARS in the root repo's
+ * scripts/check-git-env-scrub.mjs (the authoritative list; see its comment
+ * for the full set of helper copies) — keep in sync.
+ */
+export function gitCleanEnv(base = process.env) {
+ const {
+ GIT_DIR: _d,
+ GIT_WORK_TREE: _w,
+ GIT_COMMON_DIR: _c,
+ GIT_INDEX_FILE: _i,
+ GIT_OBJECT_DIRECTORY: _o,
+ GIT_ALTERNATE_OBJECT_DIRECTORIES: _a,
+ GIT_NAMESPACE: _n,
+ GIT_PREFIX: _p,
+ ...env
+ } = base;
+ return env;
+}