Skip to content

Commit 7c29f2d

Browse files
perf(server): git status runs in parallel and repo detection spawns 3x fewer git processes (#27103)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent facaa08 commit 7c29f2d

7 files changed

Lines changed: 194 additions & 80 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,24 @@ it.effect("GitVcsDriver treats a missing git executable as outside any repositor
10061006
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
10071007
);
10081008

1009+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1010+
"GitVcsDriver detects a repository whose path contains a newline",
1011+
() =>
1012+
Effect.gen(function* () {
1013+
const fileSystem = yield* FileSystem.FileSystem;
1014+
const path = yield* Path.Path;
1015+
const parent = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-newline-repo-" });
1016+
const cwd = path.join(parent, "multi\nline");
1017+
yield* fileSystem.makeDirectory(cwd);
1018+
yield* runGit(cwd, ["init"]);
1019+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
1020+
1021+
const repository = yield* driver.detectRepository(cwd);
1022+
assert.strictEqual(repository?.rootPath, yield* fileSystem.realPath(cwd));
1023+
assert.strictEqual(repository?.metadataPath, ".git");
1024+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1025+
);
1026+
10091027
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
10101028
"GitVcsDriver still fails detection when git exists but cannot be spawned",
10111029
() =>

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -587,25 +587,43 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
587587
const detectRepository: VcsDriver.VcsDriver["Service"]["detectRepository"] = Effect.fn(
588588
"detectRepository",
589589
)(function* (cwd) {
590-
if (!(yield* isInsideWorkTree(cwd))) {
591-
return null;
592-
}
593-
594-
const root = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository.root", cwd, [
595-
"rev-parse",
596-
"--show-toplevel",
597-
]);
598-
const gitCommonDir = yield* gitCommand(
590+
// One spawn answers all three; outside a work tree it exits non-zero or prints "false".
591+
const result = yield* gitCommand(
599592
vcsProcess,
600-
"GitVcsDriver.detectRepository.commonDir",
593+
"GitVcsDriver.detectRepository",
601594
cwd,
602-
["rev-parse", "--git-common-dir"],
603-
).pipe(Effect.orElseSucceed(() => null));
595+
["rev-parse", "--is-inside-work-tree", "--show-toplevel", "--git-common-dir"],
596+
{
597+
allowNonZeroExit: true,
598+
timeoutMs: 5_000,
599+
maxOutputBytes: 16_384,
600+
},
601+
).pipe(Effect.catchIf(isMissingExecutable, () => Effect.succeed(null)));
602+
if (result === null || result.exitCode !== 0) return null;
603+
const lines = result.stdout.replace(/\r?\n$/u, "").split(/\r?\n/u);
604+
if (lines[0]?.trim() !== "true") return null;
605+
// A newline inside a path breaks the line framing; ask for each path on its own.
606+
if (lines.length !== 3) {
607+
const root = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository", cwd, [
608+
"rev-parse",
609+
"--show-toplevel",
610+
]);
611+
const gitCommonDir = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository", cwd, [
612+
"rev-parse",
613+
"--git-common-dir",
614+
]).pipe(Effect.orElseSucceed(() => null));
615+
return {
616+
kind: "git" as const,
617+
rootPath: root.stdout.trim(),
618+
metadataPath: gitCommonDir?.stdout.trim() || null,
619+
freshness: yield* nowFreshness(),
620+
};
621+
}
604622

605623
return {
606624
kind: "git" as const,
607-
rootPath: root.stdout.trim(),
608-
metadataPath: gitCommonDir?.stdout.trim() || null,
625+
rootPath: lines[1]!.trim(),
626+
metadataPath: lines[2]!.trim() || null,
609627
freshness: yield* nowFreshness(),
610628
};
611629
});

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

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
20912091
return yield* lockError;
20922092
}
20932093
}
2094-
const statusResult = yield* executeGitWithStableDiagnostics(
2094+
const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
2095+
Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
2096+
);
2097+
const statusCacheKey = repositoryPaths?.gitCommonDir;
2098+
const readStatus = executeGitWithStableDiagnostics(
20952099
"GitVcsDriver.statusDetails.status",
20962100
cwd,
20972101
statusArgs,
@@ -2105,34 +2109,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
21052109
isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),
21062110
}),
21072111
);
2108-
2109-
if (statusResult === null) {
2110-
return NON_REPOSITORY_STATUS_DETAILS;
2111-
}
2112-
2113-
if (statusResult.exitCode !== 0) {
2114-
if (isNonRepositoryGitStderr(statusResult.stderr)) {
2115-
return NON_REPOSITORY_STATUS_DETAILS;
2116-
}
2117-
return yield* new GitCommandError({
2118-
...gitCommandContext({
2119-
operation: "GitVcsDriver.statusDetails.status",
2120-
cwd,
2121-
args: statusArgs,
2122-
}),
2123-
detail: "Git status failed.",
2124-
exitCode: statusResult.exitCode,
2125-
stdoutLength: statusResult.stdout.length,
2126-
stderrLength: statusResult.stderr.length,
2127-
});
2128-
}
2129-
2130-
const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
2131-
Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
2132-
);
2133-
const statusCacheKey = repositoryPaths?.gitCommonDir;
2134-
const [numstatEntries, defaultBranch, hasPrimaryRemote] = yield* Effect.all(
2112+
// diff-index does not read status output, so both spawn together; a non-repository discards it.
2113+
const [statusResult, numstatResult, defaultBranch, hasPrimaryRemote] = yield* Effect.all(
21352114
[
2115+
readStatus,
21362116
// Porcelain diff rewrites indexes despite GIT_OPTIONAL_LOCKS=0; plumbing with -M preserves rename detection.
21372117
executeGitWithStableDiagnostics(
21382118
"GitVcsDriver.statusDetails.numstat",
@@ -2178,6 +2158,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
21782158
}),
21792159
);
21802160
}),
2161+
Effect.result,
21812162
),
21822163
statusCacheKey
21832164
? Cache.get(defaultBranchCache, statusCacheKey).pipe(Effect.orElseSucceed(() => null))
@@ -2188,6 +2169,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
21882169
],
21892170
{ concurrency: "unbounded" },
21902171
);
2172+
if (statusResult === null) {
2173+
return NON_REPOSITORY_STATUS_DETAILS;
2174+
}
2175+
2176+
if (statusResult.exitCode !== 0) {
2177+
if (isNonRepositoryGitStderr(statusResult.stderr)) {
2178+
return NON_REPOSITORY_STATUS_DETAILS;
2179+
}
2180+
return yield* new GitCommandError({
2181+
...gitCommandContext({
2182+
operation: "GitVcsDriver.statusDetails.status",
2183+
cwd,
2184+
args: statusArgs,
2185+
}),
2186+
detail: "Git status failed.",
2187+
exitCode: statusResult.exitCode,
2188+
stdoutLength: statusResult.stdout.length,
2189+
stderrLength: statusResult.stderr.length,
2190+
});
2191+
}
2192+
2193+
const numstatEntries = yield* Effect.fromResult(numstatResult);
21912194
const statusStdout = statusResult.stdout;
21922195

21932196
let refName: string | null = null;
@@ -2231,25 +2234,34 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22312234
}
22322235
}
22332236

2234-
const fallbackAheadCount =
2235-
includeDivergence && !upstreamRef && refName
2236-
? yield* computeAheadCountAgainstBase(cwd, refName).pipe(Effect.orElseSucceed(() => 0))
2237-
: null;
2238-
2239-
if (fallbackAheadCount !== null) {
2240-
aheadCount = fallbackAheadCount;
2241-
behindCount = 0;
2242-
}
2243-
22442237
const isDefaultBranch =
22452238
refName !== null &&
22462239
(refName === defaultBranch ||
22472240
(defaultBranch === null && (refName === "main" || refName === "master")));
2248-
if (includeDivergence && refName && !isDefaultBranch) {
2249-
aheadOfDefaultCount =
2250-
fallbackAheadCount !== null
2251-
? fallbackAheadCount
2252-
: yield* computeAheadCountAgainstBase(cwd, refName).pipe(Effect.orElseSucceed(() => 0));
2241+
const needsFallbackAhead = includeDivergence && !upstreamRef && refName !== null;
2242+
const needsAheadOfDefault = includeDivergence && refName !== null && !isDefaultBranch;
2243+
const [baseAheadCount, branchChanges] = yield* Effect.all(
2244+
[
2245+
refName !== null && (needsFallbackAhead || needsAheadOfDefault)
2246+
? computeAheadCountAgainstBase(cwd, refName, {
2247+
knownOriginDefaultBranch: defaultBranch,
2248+
}).pipe(Effect.orElseSucceed(() => 0))
2249+
: Effect.succeed(0),
2250+
options?.includeBranchChanges
2251+
? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, {
2252+
knownOriginDefaultBranch: defaultBranch,
2253+
}).pipe(Effect.orElseSucceed(() => undefined))
2254+
: Effect.undefined,
2255+
],
2256+
{ concurrency: "unbounded" },
2257+
);
2258+
2259+
if (needsFallbackAhead) {
2260+
aheadCount = baseAheadCount;
2261+
behindCount = 0;
2262+
}
2263+
if (needsAheadOfDefault) {
2264+
aheadOfDefaultCount = baseAheadCount;
22532265
}
22542266

22552267
const fileStatMap = new Map<string, { insertions: number; deletions: number }>();
@@ -2276,12 +2288,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22762288
}
22772289
files.sort((a, b) => a.path.localeCompare(b.path));
22782290

2279-
const branchChanges = options?.includeBranchChanges
2280-
? yield* readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName).pipe(
2281-
Effect.orElseSucceed(() => undefined),
2282-
)
2283-
: undefined;
2284-
22852291
return {
22862292
isRepo: true,
22872293
hasOriginRemote: hasPrimaryRemote,
@@ -3014,6 +3020,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30143020
cwd: string,
30153021
branch: string | null,
30163022
explicitBaseRef?: string,
3023+
knownOriginDefaultBranch: string | null = null,
30173024
) {
30183025
const base =
30193026
explicitBaseRef !== undefined
@@ -3025,9 +3032,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30253032
: explicitBaseRef,
30263033
})
30273034
: branch
3028-
? yield* resolveBaseBranchForNoUpstream(cwd, branch, { allowRemoteOfCurrent: true }).pipe(
3029-
Effect.orElseSucceed(() => null),
3030-
)
3035+
? yield* resolveBaseBranchForNoUpstream(cwd, branch, {
3036+
allowRemoteOfCurrent: true,
3037+
knownOriginDefaultBranch,
3038+
}).pipe(Effect.orElseSucceed(() => null))
30313039
: null;
30323040
if (base === null) return { baseRef: null, mergeBase: "HEAD" };
30333041
const { baseRef, comparisonRef } = base;
@@ -3058,8 +3066,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30583066
const readBranchChangeTotals = Effect.fn("readBranchChangeTotals")(function* (
30593067
cwd: string,
30603068
branch: string | null,
3069+
options?: { readonly knownOriginDefaultBranch: string | null },
30613070
) {
3062-
const { baseRef, mergeBase } = yield* resolveReviewMergeBase(cwd, branch);
3071+
const { baseRef, mergeBase } = yield* resolveReviewMergeBase(
3072+
cwd,
3073+
branch,
3074+
undefined,
3075+
options?.knownOriginDefaultBranch ?? null,
3076+
);
30633077
const untracked = yield* prepareUntrackedReviewIndex(cwd, []);
30643078
if (untracked === null) {
30653079
return yield* new GitCommandError({

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
2121
const normalizeGitArgs = (args: ReadonlyArray<string>): ReadonlyArray<string> =>
2222
args[0] === "-C" && args.length >= 2 ? args.slice(2) : args;
2323

24+
const DETECT_COMMAND = "rev-parse --is-inside-work-tree --show-toplevel --git-common-dir";
25+
2426
describe("VcsDriverRegistry", () => {
2527
const makeDiskBackedLayer = (rootDir: string, calls: string[]) =>
2628
Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
@@ -49,10 +51,10 @@ describe("VcsDriverRegistry", () => {
4951
stderr: "fatal: not a git repository",
5052
};
5153
}
52-
if (command === "rev-parse --is-inside-work-tree") return processOutput("true\n");
53-
if (command === "rev-parse --show-toplevel") return processOutput(`${repoDir}\n`);
54-
if (command === "rev-parse --git-common-dir") {
55-
return processOutput(`${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`);
54+
if (command === DETECT_COMMAND) {
55+
return processOutput(
56+
`true\n${repoDir}\n${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`,
57+
);
5658
}
5759
return processOutput("");
5860
}).pipe(Effect.provide(NodeServices.layer), Effect.orDie),
@@ -75,7 +77,7 @@ describe("VcsDriverRegistry", () => {
7577
yield* fs.remove(path.join(repoDir, ".git"), { recursive: true });
7678

7779
assert.equal(yield* registry.detect({ cwd: repoDir }), null);
78-
assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
80+
assert.equal(calls.filter((call) => call === DETECT_COMMAND).length, 2);
7981
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
8082
}).pipe(Effect.provide(NodeServices.layer)),
8183
);
@@ -118,7 +120,7 @@ describe("VcsDriverRegistry", () => {
118120
yield* fs.makeDirectory(path.join(repoDir, ".git"));
119121

120122
assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
121-
assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
123+
assert.equal(calls.filter((call) => call === DETECT_COMMAND).length, 2);
122124
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
123125
}).pipe(Effect.provide(NodeServices.layer)),
124126
);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ const gitMetricOperations = new Set([
8383
"GitVcsDriver.checkpoints.resolveGitCommonDir",
8484
"GitVcsDriver.checkpoints.resolveHeadCommit",
8585
"GitVcsDriver.checkpoints.restoreCheckpoint",
86-
"GitVcsDriver.detectRepository.commonDir",
87-
"GitVcsDriver.detectRepository.root",
86+
"GitVcsDriver.detectRepository",
8887
"GitVcsDriver.filterIgnoredPaths",
8988
"GitVcsDriver.initRepository",
9089
"GitVcsDriver.isInsideWorkTree",

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function layerTestFor(state: {
7676
backgroundWorkEnabled?: boolean;
7777
automaticFetchInterval?: Effect.Effect<Duration.Duration>;
7878
beforeRemoteStatus?: Effect.Effect<void>;
79+
beforeLocalStatus?: Effect.Effect<void>;
7980
fileSystem?: FileSystem.FileSystem;
8081
pullCurrentBranch?: GitWorkflowService.GitWorkflowService["Service"]["pullCurrentBranch"];
8182
}) {
@@ -95,9 +96,11 @@ function layerTestFor(state: {
9596
Layer.provide(
9697
Layer.mock(GitWorkflowService.GitWorkflowService)({
9798
localStatus: () =>
98-
Effect.sync(() => {
99+
Effect.suspend(() => {
99100
state.localStatusCalls += 1;
100-
return state.currentLocalStatus;
101+
return (state.beforeLocalStatus ?? Effect.void).pipe(
102+
Effect.as(state.currentLocalStatus),
103+
);
101104
}),
102105
currentBranch: () => Effect.sync(() => state.currentLocalStatus.refName),
103106
remoteStatus: (_input, options) =>
@@ -163,6 +166,47 @@ function layerBackgroundPolicy(shouldRunScopeWork: (scope: BackgroundScope) => b
163166
}
164167

165168
describe("VcsStatusBroadcaster", () => {
169+
it.effect("refreshes queued behind a running one share the next git status read", () => {
170+
const state: Parameters<typeof layerTestFor>[0] = {
171+
currentLocalStatus: baseLocalStatus,
172+
currentRemoteStatus: baseRemoteStatus,
173+
localStatusCalls: 0,
174+
remoteStatusCalls: 0,
175+
localInvalidationCalls: 0,
176+
remoteInvalidationCalls: 0,
177+
fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }),
178+
};
179+
180+
return Effect.gen(function* () {
181+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
182+
const started = yield* Deferred.make<void>();
183+
const release = yield* Deferred.make<void>();
184+
state.beforeLocalStatus = Deferred.succeed(started, undefined).pipe(
185+
Effect.andThen(Deferred.await(release)),
186+
);
187+
const running = yield* broadcaster
188+
.refreshLocalStatus("/repo")
189+
.pipe(Effect.forkScoped({ startImmediately: true }));
190+
yield* Deferred.await(started);
191+
// Requested while the first read runs, so that read may predate their change.
192+
state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true };
193+
const queued = yield* Effect.all(
194+
[broadcaster.refreshLocalStatus("/repo"), broadcaster.refreshLocalStatus("/repo")],
195+
{ concurrency: "unbounded" },
196+
).pipe(Effect.forkScoped({ startImmediately: true }));
197+
yield* Deferred.succeed(release, undefined);
198+
199+
assert.isFalse((yield* Fiber.join(running)).hasWorkingTreeChanges);
200+
const [second, third] = yield* Fiber.join(queued);
201+
assert.isTrue(second.hasWorkingTreeChanges);
202+
assert.isTrue(third.hasWorkingTreeChanges);
203+
assert.strictEqual(state.localStatusCalls, 2);
204+
205+
yield* broadcaster.refreshLocalStatus("/repo");
206+
assert.strictEqual(state.localStatusCalls, 3);
207+
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
208+
});
209+
166210
it.effect("publishes a terminal branch switch without reading remote status", () => {
167211
const state = {
168212
currentLocalStatus: baseLocalStatus,

0 commit comments

Comments
 (0)