Skip to content

Commit 80cccdc

Browse files
fix(server): projects read their own Git repository when the server inherited GIT_DIR (#35119)
1 parent cb931fc commit 80cccdc

5 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎apps/server/src/project/RepositoryIdentityResolver.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as Path from "effect/Path";
1212

1313
import * as ProcessRunner from "../processRunner.ts";
1414
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
15-
import { explicitBareRepositoryEnv } from "../vcs/VcsProcess.ts";
15+
import { explicitBareRepositoryEnv, unsetInheritedRepositoryEnv } from "../vcs/VcsProcess.ts";
1616
import * as HostProcess from "@t3tools/shared/HostProcess";
1717

1818
const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512;
@@ -147,12 +147,17 @@ class RepositoryIdentityCacheKey extends Data.Class<{
147147
readonly isBare: boolean;
148148
}> {}
149149

150+
const gitEnv = (platform: NodeJS.Platform): NodeJS.ProcessEnv => ({
151+
...unsetInheritedRepositoryEnv(process.env, platform),
152+
...explicitBareRepositoryEnv(process.env, platform),
153+
});
154+
150155
const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.resolveCacheKey")(
151156
function* (cwd: string) {
152157
const processRunner = yield* ProcessRunner.ProcessRunner;
153158
const fileSystem = yield* FileSystem.FileSystem;
154159
const path = yield* Path.Path;
155-
const env = explicitBareRepositoryEnv(process.env, yield* HostProcess.Platform);
160+
const env = gitEnv(yield* HostProcess.Platform);
156161

157162
// `git -C` cannot enter a missing folder or a file, so every probe below would fail.
158163
const isFolder = yield* fileSystem.stat(cwd).pipe(
@@ -241,7 +246,7 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
241246
.run({
242247
command: "git",
243248
args: ["-C", cacheKey.rootPath, ...(cacheKey.isBare ? ["--git-dir=."] : []), "remote", "-v"],
244-
env: explicitBareRepositoryEnv(process.env, yield* HostProcess.Platform),
249+
env: gitEnv(yield* HostProcess.Platform),
245250
timeoutBehavior: "timedOutResult",
246251
})
247252
.pipe(Effect.option);

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
986986
const tempIndexPath = path.join(gitCommonDir, `t3-checkpoint-index-${indexId}`);
987987
const hostEnv = yield* HostProcess.Environment;
988988
const commitEnv: NodeJS.ProcessEnv = {
989-
...hostEnv,
990989
GIT_INDEX_FILE: tempIndexPath,
991990
GIT_AUTHOR_NAME: "T3 Code",
992991
GIT_AUTHOR_EMAIL: "[email protected]",

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
66
import * as NodeServices from "@effect/platform-node/NodeServices";
77
import * as HostProcess from "@t3tools/shared/HostProcess";
88
import { assert, it, describe } from "@effect/vitest";
9+
import { vi } from "vite-plus/test";
910
import * as Cause from "effect/Cause";
1011
import * as Clock from "effect/Clock";
1112
import * as Deferred from "effect/Deferred";
@@ -649,6 +650,25 @@ it.effect.each(["bare", "unborn"] as const)(
649650
).pipe(Effect.provide(layerTest)),
650651
);
651652

653+
it.effect("reads the project's own repository when the server inherited GIT_DIR", () =>
654+
Effect.scoped(
655+
Effect.gen(function* () {
656+
const path = yield* Path.Path;
657+
const cwd = yield* makeTmpDir();
658+
const other = yield* makeTmpDir();
659+
const { initialBranch } = yield* initRepoWithCommit(cwd);
660+
yield* initRepoWithCommit(other);
661+
yield* git(other, ["switch", "-c", "other-repository"]);
662+
const driver = yield* GitVcsDriver.GitVcsDriver;
663+
yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
664+
vi.stubEnv("GIT_DIR", path.join(other, ".git"));
665+
assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, initialBranch);
666+
const refs = yield* driver.listRefs({ cwd });
667+
assert.isFalse(refs.refs.some((ref) => ref.name === "other-repository"));
668+
}),
669+
).pipe(Effect.provide(layerTest)),
670+
);
671+
652672
it.effect.each(["on a branch", "detached", "unborn"] as const)(
653673
"reads only the repository root for review requests that ignore the branch when %s",
654674
(state) =>

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings
4242
import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
4343
import * as GitVcsDriver from "./GitVcsDriver.ts";
4444
import { removeEmptyDirectory, type RemoveEmptyDirectoryError } from "./removeEmptyDirectory.ts";
45-
import { explicitBareRepositoryEnv, gitConfigEnv } from "./VcsProcess.ts";
45+
import {
46+
explicitBareRepositoryEnv,
47+
gitConfigEnv,
48+
unsetInheritedRepositoryEnv,
49+
} from "./VcsProcess.ts";
4650
import { makeWorktreeCheckoutCache } from "./worktreeCheckoutCache.ts";
4751
import { removeLinkedWorktree } from "./worktreeRemoval.ts";
4852
import { resolveWorktreesDirectory } from "../worktreesDirectory.ts";
@@ -993,8 +997,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
993997
}),
994998
),
995999
);
1000+
const hostEnv = yield* HostProcess.Environment;
9961001
const baseEnv = {
997-
...(yield* HostProcess.Environment),
1002+
...hostEnv,
1003+
...unsetInheritedRepositoryEnv(hostEnv, hostPlatform),
9981004
// Concurrent status polls must not take index.lock to save refreshed indexes.
9991005
GIT_OPTIONAL_LOCKS: "0",
10001006
...input.env,

‎apps/server/src/vcs/VcsProcess.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ export const gitConfigEnv = (
134134
export const explicitBareRepositoryEnv = (env: NodeJS.ProcessEnv, platform: NodeJS.Platform) =>
135135
gitConfigEnv(env, "safe.bareRepository", "explicit", platform);
136136

137+
const REPOSITORY_SELECTORS = new Set([
138+
"GIT_DIR",
139+
"GIT_WORK_TREE",
140+
"GIT_COMMON_DIR",
141+
"GIT_INDEX_FILE",
142+
"GIT_OBJECT_DIRECTORY",
143+
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
144+
]);
145+
146+
/**
147+
* Unsets repository selectors the server inherited, such as a Git hook's `GIT_DIR`, so Git finds
148+
* the repository from cwd. Spread it before a command's own env, which may still set them.
149+
*/
150+
export const unsetInheritedRepositoryEnv = (
151+
inherited: NodeJS.ProcessEnv,
152+
platform: NodeJS.Platform,
153+
): NodeJS.ProcessEnv =>
154+
Object.fromEntries(
155+
Object.keys(inherited)
156+
.filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name))
157+
.map((name) => [name, undefined]),
158+
);
159+
137160
const classifyNonZeroExit = (
138161
stderr: string,
139162
classifyFailure: VcsProcessInput["classifyFailure"],
@@ -210,6 +233,9 @@ export const make = Effect.gen(function* () {
210233
...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
211234
...(input.onStdoutChunk !== undefined ? { onStdoutChunk: input.onStdoutChunk } : {}),
212235
env: {
236+
...(input.command === "git"
237+
? unsetInheritedRepositoryEnv(process.env, hostPlatform)
238+
: {}),
213239
// Failures are read by English patterns; without LANG, macOS Git follows the system language.
214240
// LANGUAGE leaves LC_CTYPE, which hooks inherit, alone.
215241
...(input.command === "git" ? { LANGUAGE: "C" } : {}),

0 commit comments

Comments
 (0)