Skip to content

Commit 79f4643

Browse files
fix(server): projects read their own Git repository when the server inherited GIT_DIR
Git child processes inherited repository selectors such as GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE from the server, so a server started from a Git hook showed that repository's branch and changes in every project. Unset the inherited selectors for every Git spawn; a command's own env may still set them. Fixes #35099 AI-assisted; verified with the tests and tools listed above.
1 parent f27e513 commit 79f4643

4 files changed

Lines changed: 42 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;
@@ -137,12 +137,17 @@ class RepositoryIdentityCacheKey extends Data.Class<{
137137
readonly isBare: boolean;
138138
}> {}
139139

140+
const gitEnv = (platform: NodeJS.Platform): NodeJS.ProcessEnv => ({
141+
...unsetInheritedRepositoryEnv(process.env, platform),
142+
...explicitBareRepositoryEnv(process.env, platform),
143+
});
144+
140145
const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.resolveCacheKey")(
141146
function* (cwd: string) {
142147
const processRunner = yield* ProcessRunner.ProcessRunner;
143148
const fileSystem = yield* FileSystem.FileSystem;
144149
const path = yield* Path.Path;
145-
const env = explicitBareRepositoryEnv(process.env, yield* HostProcess.Platform);
150+
const env = gitEnv(yield* HostProcess.Platform);
146151

147152
// `git -C` cannot enter a missing folder or a file, so every probe below would fail.
148153
const isFolder = yield* fileSystem.stat(cwd).pipe(
@@ -231,7 +236,7 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
231236
.run({
232237
command: "git",
233238
args: ["-C", cacheKey.rootPath, ...(cacheKey.isBare ? ["--git-dir=."] : []), "remote", "-v"],
234-
env: explicitBareRepositoryEnv(process.env, yield* HostProcess.Platform),
239+
env: gitEnv(yield* HostProcess.Platform),
235240
timeoutBehavior: "timedOutResult",
236241
})
237242
.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.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)