Skip to content

Commit a5e7e5b

Browse files
Yuruzuuclaudemaria-rcks
authored
perf(server): loading a file's diff skips unused repository lookups (#29115)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent dfbafee commit a5e7e5b

2 files changed

Lines changed: 183 additions & 46 deletions

File tree

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

Lines changed: 146 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,119 @@ it.effect.each(["bare", "unborn"] as const)(
566566
).pipe(Effect.provide(layerTest)),
567567
);
568568

569+
it.effect.each(["on a branch", "detached", "unborn"] as const)(
570+
"reads only the repository root for review requests that ignore the branch when %s",
571+
(state) =>
572+
Effect.scoped(
573+
Effect.gen(function* () {
574+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
575+
const spawnedArgs: Array<ReadonlyArray<string>> = [];
576+
const spawner = ChildProcessSpawner.make((command) => {
577+
if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args.map(String));
578+
return delegate.spawn(command);
579+
});
580+
const driver = yield* makeGitVcsDriverCore().pipe(
581+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
582+
);
583+
const discoveries = () => {
584+
const commands = spawnedArgs.filter(
585+
(args) =>
586+
args.includes("--show-toplevel") ||
587+
args.includes("--git-common-dir") ||
588+
(args.includes("symbolic-ref") && args.includes("HEAD")),
589+
);
590+
spawnedArgs.length = 0;
591+
return commands.length;
592+
};
593+
const cwd = yield* makeTmpDir();
594+
if (state === "unborn") {
595+
yield* git(cwd, ["init", "-b", "main"]);
596+
} else {
597+
yield* initRepoWithCommit(cwd);
598+
yield* writeTextFile(cwd, "committed.txt", "committed\n");
599+
yield* git(cwd, ["add", "."]);
600+
yield* git(cwd, ["commit", "-m", "add file"]);
601+
if (state === "detached") yield* git(cwd, ["checkout", "--detach"]);
602+
}
603+
yield* writeTextFile(cwd, "new.txt", "new\n");
604+
spawnedArgs.length = 0;
605+
606+
const workingTree = yield* driver.getReviewDiffPreview({
607+
cwd,
608+
file: { path: "new.txt", previousPath: null, sourceKind: "working-tree" },
609+
});
610+
assert.equal(discoveries(), 1);
611+
assert.deepStrictEqual(
612+
workingTree.sources
613+
.find((source) => source.kind === "working-tree")!
614+
.files!.map((file) => file.path),
615+
["new.txt"],
616+
);
617+
618+
if (state !== "unborn") {
619+
const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]);
620+
spawnedArgs.length = 0;
621+
const commit = yield* driver.getReviewDiffPreview({
622+
cwd,
623+
commitSha,
624+
file: { path: "committed.txt", previousPath: null, sourceKind: "commit" },
625+
});
626+
assert.equal(discoveries(), 1);
627+
assert.include(commit.sources[0]!.diff, "+committed");
628+
}
629+
630+
const general = yield* driver.getReviewDiffPreview({ cwd });
631+
assert.equal(discoveries(), 3);
632+
assert.equal(
633+
general.sources.find((source) => source.kind === "branch-range")!.headRef,
634+
state === "detached" ? "HEAD" : yield* git(cwd, ["branch", "--show-current"]),
635+
);
636+
}),
637+
).pipe(Effect.provide(layerTest)),
638+
);
639+
640+
it.effect.each([
641+
{ kind: "working-tree", request: { sourceKind: "working-tree" } },
642+
{ kind: "commit", request: { sourceKind: "commit", commitSha: "HEAD" } },
643+
] as const)("keeps $kind review requests safe without a worktree root", ({ request }) =>
644+
Effect.scoped(
645+
Effect.gen(function* () {
646+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
647+
const bareRoot = yield* makeTmpDir("git-review-bare-");
648+
const spawner = ChildProcessSpawner.make((command) =>
649+
delegate.spawn(
650+
ChildProcess.isStandardCommand(command) && command.options.cwd === bareRoot
651+
? ChildProcess.setEnv(command, { GIT_DIR: bareRoot })
652+
: command,
653+
),
654+
);
655+
const driver = yield* makeGitVcsDriverCore().pipe(
656+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
657+
);
658+
const path = yield* Path.Path;
659+
const file = { path: "README.md", previousPath: null, sourceKind: request.sourceKind };
660+
const input = (cwd: string) => ({
661+
cwd,
662+
file,
663+
...("commitSha" in request ? { commitSha: request.commitSha } : {}),
664+
});
665+
const source = yield* makeTmpDir();
666+
yield* initRepoWithCommit(source);
667+
yield* git(source, ["clone", "--bare", source, bareRoot]);
668+
const nonRepository = yield* makeTmpDir();
669+
for (const cwd of [bareRoot, nonRepository, path.join(nonRepository, "missing")]) {
670+
assert.isEmpty((yield* driver.getReviewDiffPreview(input(cwd))).sources);
671+
}
672+
673+
const brokenConfig = yield* makeTmpDir();
674+
yield* initRepoWithCommit(brokenConfig);
675+
yield* writeTextFile(brokenConfig, ".git/config", "[broken");
676+
const error = yield* driver.getReviewDiffPreview(input(brokenConfig)).pipe(Effect.flip);
677+
assert.equal(error.operation, "GitVcsDriver.resolveRepositoryPaths.commonDir");
678+
}),
679+
).pipe(Effect.provide(layerTest)),
680+
);
681+
569682
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
570683
"preserves embedded newlines in repository paths after worktree creation",
571684
() =>
@@ -2150,37 +2263,49 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
21502263
}),
21512264
);
21522265

2153-
it.effect("loads repository-relative files from a nested project directory", () =>
2266+
it.effect("loads repository-relative files from nested directories and aliases", () =>
21542267
Effect.gen(function* () {
2155-
const cwd = yield* makeTmpDir();
2268+
const directory = yield* makeTmpDir();
2269+
const path = yield* Path.Path;
2270+
const fileSystem = yield* FileSystem.FileSystem;
2271+
const platform = yield* HostProcess.Platform;
2272+
const cwd = path.join(directory, platform === "win32" ? "repository" : "repository\n ");
2273+
yield* fileSystem.makeDirectory(cwd);
21562274
const { initialBranch } = yield* initRepoWithCommit(cwd);
21572275
yield* git(cwd, ["checkout", "-b", "feature/nested"]);
21582276
yield* writeTextFile(cwd, "nested/tracked.txt", "committed\n");
21592277
yield* git(cwd, ["add", "."]);
21602278
yield* git(cwd, ["commit", "-m", "nested file"]);
21612279
yield* writeTextFile(cwd, "nested/tracked.txt", "changed\n");
21622280
yield* writeTextFile(cwd, "untracked.txt", "new\n");
2163-
const path = yield* Path.Path;
21642281
const driver = yield* GitVcsDriver.GitVcsDriver;
21652282
const nestedCwd = path.join(cwd, "nested");
2166-
const preview = yield* driver.getReviewDiffPreview({
2167-
cwd: nestedCwd,
2168-
baseRef: initialBranch,
2169-
});
2170-
assert.equal(
2171-
preview.sources.find((source) => source.kind === "working-tree")!.files!.length,
2172-
2,
2173-
);
2174-
for (const source of preview.sources) {
2175-
for (const file of source.files ?? []) {
2176-
const scoped = yield* driver.getReviewDiffPreview({
2177-
cwd: nestedCwd,
2178-
baseRef: initialBranch,
2179-
file: { path: file.path, previousPath: file.previousPath, sourceKind: source.kind },
2180-
});
2181-
const patch = scoped.sources.find((item) => item.kind === source.kind)!;
2182-
assert.deepStrictEqual(patch.files, [file]);
2183-
assert.include(patch.diff, `b/${file.path}`);
2283+
const projectDirectories = [nestedCwd];
2284+
if (symlinksSupported) {
2285+
const alias = path.join(directory, "alias");
2286+
yield* fileSystem.symlink(nestedCwd, alias);
2287+
projectDirectories.push(alias);
2288+
}
2289+
for (const projectDirectory of projectDirectories) {
2290+
const preview = yield* driver.getReviewDiffPreview({
2291+
cwd: projectDirectory,
2292+
baseRef: initialBranch,
2293+
});
2294+
assert.equal(
2295+
preview.sources.find((source) => source.kind === "working-tree")!.files!.length,
2296+
2,
2297+
);
2298+
for (const source of preview.sources) {
2299+
for (const file of source.files ?? []) {
2300+
const scoped = yield* driver.getReviewDiffPreview({
2301+
cwd: projectDirectory,
2302+
baseRef: initialBranch,
2303+
file: { path: file.path, previousPath: file.previousPath, sourceKind: source.kind },
2304+
});
2305+
const patch = scoped.sources.find((item) => item.kind === source.kind)!;
2306+
assert.deepStrictEqual(patch.files, [file]);
2307+
assert.include(patch.diff, `b/${file.path}`);
2308+
}
21842309
}
21852310
}
21862311
}),

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,22 +1432,33 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14321432
.pipe(Effect.orElseSucceed(() => resolvedGitCommonDir));
14331433
});
14341434

1435+
const readWorktreeRoot = Effect.fn("readWorktreeRoot")(function* (cwd: string) {
1436+
const result = yield* executeGit(
1437+
"GitVcsDriver.resolveRepositoryPaths.worktreeRoot",
1438+
cwd,
1439+
["rev-parse", "--show-toplevel"],
1440+
{
1441+
timeoutMs: 5_000,
1442+
allowNonZeroExit: true,
1443+
},
1444+
);
1445+
// Git terminates --show-toplevel with exactly one "\n" on every platform (stdout is
1446+
// binary mode on Windows). Whitespace before it belongs to the path and must survive,
1447+
// because this root becomes the editor target for every repository-relative status path.
1448+
const output = result.stdout.endsWith("\n") ? result.stdout.slice(0, -1) : result.stdout;
1449+
return result.exitCode === 0 && output.length > 0
1450+
? path.normalize(path.isAbsolute(output) ? output : path.resolve(cwd, output))
1451+
: null;
1452+
});
1453+
14351454
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
14361455
cwd: string,
14371456
) {
14381457
const gitCommonDir = yield* readGitCommonDir(cwd);
14391458
if (gitCommonDir === null) return null;
1440-
const [worktreeRootResult, currentBranchResult] = yield* Effect.all(
1459+
const [worktreeRoot, currentBranchResult] = yield* Effect.all(
14411460
[
1442-
executeGit(
1443-
"GitVcsDriver.resolveRepositoryPaths.worktreeRoot",
1444-
cwd,
1445-
["rev-parse", "--show-toplevel"],
1446-
{
1447-
timeoutMs: 5_000,
1448-
allowNonZeroExit: true,
1449-
},
1450-
),
1461+
readWorktreeRoot(cwd),
14511462
executeGit(
14521463
"GitVcsDriver.resolveRepositoryPaths.currentBranch",
14531464
cwd,
@@ -1460,20 +1471,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14601471
],
14611472
{ concurrency: 2 },
14621473
);
1463-
// Git terminates --show-toplevel with exactly one "\n" on every platform (stdout is
1464-
// binary mode on Windows). Whitespace before it belongs to the path and must survive,
1465-
// because this root becomes the editor target for every repository-relative status path.
1466-
const worktreeRootOutput = worktreeRootResult.stdout.endsWith("\n")
1467-
? worktreeRootResult.stdout.slice(0, -1)
1468-
: worktreeRootResult.stdout;
1469-
const worktreeRoot =
1470-
worktreeRootResult.exitCode === 0 && worktreeRootOutput.length > 0
1471-
? path.normalize(
1472-
path.isAbsolute(worktreeRootOutput)
1473-
? worktreeRootOutput
1474-
: path.resolve(cwd, worktreeRootOutput),
1475-
)
1476-
: null;
14771474
// Git reports physical roots, so resolve project aliases before comparing.
14781475
const isAtRepositoryRoot =
14791476
worktreeRoot === null
@@ -1493,6 +1490,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14931490
} satisfies GitRepositoryPaths;
14941491
});
14951492

1493+
const resolveReviewWorktreeRoot = Effect.fn("resolveReviewWorktreeRoot")(function* (cwd: string) {
1494+
const worktreeRoot = yield* readWorktreeRoot(cwd);
1495+
if (worktreeRoot !== null) return { worktreeRoot, currentBranch: null };
1496+
// Full discovery tells bare and non-repository directories apart from real Git failures.
1497+
return yield* resolveRepositoryPathsUncached(cwd);
1498+
});
1499+
14961500
const repositoryPathsCache = yield* Cache.makeWith(
14971501
(cwd: string) => resolveRepositoryPathsUncached(cwd),
14981502
{
@@ -3369,7 +3373,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
33693373
const patchLimit = input.file
33703374
? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES
33713375
: REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES;
3372-
const repository = yield* resolveRepositoryPathsUncached(input.cwd).pipe(
3376+
const needsCurrentBranch =
3377+
input.commitSha === undefined &&
3378+
input.file?.sourceKind !== "commit" &&
3379+
input.file?.sourceKind !== "working-tree";
3380+
const repository = yield* (
3381+
needsCurrentBranch
3382+
? resolveRepositoryPathsUncached(input.cwd)
3383+
: resolveReviewWorktreeRoot(input.cwd)
3384+
).pipe(
33733385
Effect.catchTags({
33743386
GitCommandError: (error) =>
33753387
isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),

0 commit comments

Comments
 (0)