@devries-cameron20 / t3code Mirror · read-only / commits / d06eaff2f4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

perf(server): batch Git status path lookups (#38707)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Vallone committed Nov 18, 2026 at 20:05 UTC d06eaff2f488ac1640df9744659b3acf2654411b
4 files changed +125 -29
apps/server/src/vcs/GitVcsDriverCore.test.ts
+25 -1
@@ -482,6 +482,22 @@ it.effect.each(["origin", "github"] as const)(
482 }).pipe(Effect.provide(layerTest)),
483 );
484
485 +it.effect(
486 + "refreshes remote status after external init while refs still cache a non-repository",
487 + () =>
488 + Effect.gen(function* () {
489 + const driver = yield* GitVcsDriver.GitVcsDriver;
490 + const fs = yield* FileSystem.FileSystem;
491 + const cwd = yield* fs.realPath(yield* makeTmpDir());
492 + assert.isFalse((yield* driver.listRefs({ cwd })).isRepo);
493 + yield* git(cwd, ["init"]);
494 +
495 + assert.isFalse((yield* driver.statusDetailsLocal(cwd)).hasOriginRemote);
496 + yield* driver.ensureRemote({ cwd, preferredName: "origin", url: cwd });
497 + assert.isTrue((yield* driver.statusDetailsLocal(cwd)).hasOriginRemote);
498 + }).pipe(Effect.provide(layerTest)),
499 +);
500 +
501 it.effect.each([
502 { hookExitCode: 0, commonDirFailure: "none" },
503 { hookExitCode: 1, commonDirFailure: "none" },
@@ -4904,7 +4920,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4920 yield* writeTextFile(cwd, "nested/untracked.txt", "one\ntwo\n");
4921 yield* writeTextFile(cwd, "new [1].txt", "literal\n");
4922
4907 - const status = yield* driver.statusDetailsLocal(pathService.join(cwd, "nested"), {
4923 + const nested = pathService.join(cwd, "nested");
4924 + const alias = pathService.join(yield* makeTmpDir(), "alias");
4925 + const platform = yield* HostProcess.Platform;
4926 + yield* Effect.promise(() =>
4927 + NodeFS.promises.symlink(nested, alias, platform === "win32" ? "junction" : "dir"),
4928 + );
4929 + const status = yield* driver.statusDetailsLocal(alias, {
4930 includeBranchChanges: true,
4931 });
4932 assert.deepStrictEqual(status.branchChanges, {
@@ -4912,6 +4934,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4934 insertions: 4,
4935 deletions: 0,
4936 });
4937 + yield* driver.ensureRemote({ cwd: alias, preferredName: "origin", url: cwd });
4938 + assert.isTrue((yield* driver.statusDetailsLocal(alias)).hasOriginRemote);
4939 assert.isUndefined((yield* driver.statusDetailsLocal(cwd)).branchChanges);
4940 }),
4941 );
apps/server/src/vcs/GitVcsDriverCore.ts
+88 -28
@@ -1557,7 +1557,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1557 });
1558 }
1559
1560 - const commonDirOutput = commonDirResult.stdout.trim();
1560 + const commonDirOutput = commonDirResult.stdout.replace(/\n$/, "");
1561 const physicalCwd = yield* fileSystem.realPath(cwd).pipe(Effect.orElseSucceed(() => cwd));
1562 const resolvedGitCommonDir = path.isAbsolute(commonDirOutput)
1563 ? path.normalize(commonDirOutput)
@@ -1717,9 +1717,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1717 function* (cwd: string, gitCommonDir?: string | null) {
1718 const commonDir =
1719 gitCommonDir === undefined
1720 - ? (yield* resolveRepositoryPaths(cwd).pipe(
1720 + ? yield* readGitCommonDir(cwd).pipe(
1721 Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
1722 - ))?.gitCommonDir
1722 + )
1723 : gitCommonDir;
1724 const cacheKey = commonDir ?? normalizeRepositoryPathsCacheKey(cwd);
1725 yield* Cache.invalidate(defaultBranchCache, cacheKey);
@@ -2381,20 +2381,38 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2381 ...(untrackedFilesHidden ? ["--untracked-files=normal"] : []),
2382 ...(includeDivergence ? [] : ["--no-ahead-behind"]),
2383 ];
2384 - const indexResult = yield* executeGitWithStableDiagnostics(
2385 - "GitVcsDriver.statusDetails.indexPath",
2386 - cwd,
2387 - ["rev-parse", "--git-path", "index"],
2388 - { allowNonZeroExit: true },
2389 - ).pipe(
2390 - Effect.catchTags({
2391 - GitCommandError: (error) =>
2392 - isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),
2393 - }),
2384 + const readPaths = (...args: string[]) =>
2385 + executeGitWithStableDiagnostics(
2386 + "GitVcsDriver.statusDetails.indexPath",
2387 + cwd,
2388 + ["rev-parse", ...args],
2389 + { allowNonZeroExit: true },
2390 + ).pipe(
2391 + Effect.catchTags({
2392 + GitCommandError: (error) =>
2393 + isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),
2394 + }),
2395 + );
2396 + const paths = yield* readPaths(
2397 + "--path-format=absolute",
2398 + "--git-path",
2399 + "index",
2400 + "--git-common-dir",
2401 + "--show-toplevel",
2402 );
2403 + if (paths === null || (paths.exitCode !== 0 && isNonRepositoryGitStderr(paths.stderr))) {
2404 + return NON_REPOSITORY_STATUS_DETAILS;
2405 + }
2406 + // Older Git may echo the flag. Newlines in paths also require the separate lookups.
2407 + const fields = paths.stdout.replace(/\n$/, "").split("\n");
2408 + const [index, commonDir, root] =
2409 + paths.exitCode === 0 && fields.length === 3 && fields.every(path.isAbsolute) ? fields : [];
2410 + const indexResult = index === undefined ? yield* readPaths("--git-path", "index") : undefined;
2411 if (indexResult === null) return NON_REPOSITORY_STATUS_DETAILS;
2396 - if (indexResult.exitCode === 0) {
2397 - const lockPath = `${path.resolve(cwd, indexResult.stdout.trim())}.lock`;
2412 + const indexPath =
2413 + index ?? (indexResult?.exitCode === 0 ? indexResult.stdout.replace(/\n$/, "") : undefined);
2414 + if (indexPath) {
2415 + const lockPath = `${path.resolve(cwd, indexPath)}.lock`;
2416 const lockError = new GitCommandError({
2417 operation: "GitVcsDriver.statusDetails.indexPath",
2418 command: "git",
@@ -2417,10 +2435,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2435 return yield* lockError;
2436 }
2437 }
2420 - const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
2421 - Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
2422 - );
2438 + const repositoryPaths =
2439 + commonDir && root
2440 + ? {
2441 + gitCommonDir: yield* fileSystem
2442 + .realPath(commonDir)
2443 + .pipe(Effect.orElseSucceed(() => path.normalize(commonDir))),
2444 + worktreeRoot: path.normalize(root),
2445 + }
2446 + : yield* resolveRepositoryPaths(cwd).pipe(
2447 + Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
2448 + );
2449 const statusCacheKey = repositoryPaths?.gitCommonDir;
2450 + const refsGeneration =
2451 + statusCacheKey === undefined ? undefined : currentListRefsGeneration(statusCacheKey);
2452 const readStatus = executeGitWithStableDiagnostics(
2453 "GitVcsDriver.statusDetails.status",
2454 cwd,
@@ -2569,6 +2597,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2597 }
2598 }
2599
2600 + if (commonDir && root && repositoryPaths?.worktreeRoot) {
2601 + const { gitCommonDir, worktreeRoot } = repositoryPaths;
2602 + const cacheKey = normalizeRepositoryPathsCacheKey(cwd);
2603 + if (!(yield* Cache.has(repositoryPathsCache, cacheKey))) {
2604 + const isAtRepositoryRoot = yield* fileSystem.realPath(cwd).pipe(
2605 + Effect.map((physicalCwd) => path.relative(worktreeRoot, physicalCwd) === ""),
2606 + Effect.orElseSucceed(() => undefined),
2607 + );
2608 + if (
2609 + currentListRefsGeneration(gitCommonDir) === refsGeneration &&
2610 + !(yield* Cache.has(repositoryPathsCache, cacheKey))
2611 + ) {
2612 + yield* Cache.set(repositoryPathsCache, cacheKey, {
2613 + gitCommonDir,
2614 + worktreeRoot,
2615 + isAtRepositoryRoot,
2616 + currentBranch: refName,
2617 + });
2618 + }
2619 + }
2620 + }
2621 +
2622 if (!upstreamRefExists && !isUnbornHead) upstreamRef = null;
2623
2624 const isDefaultBranch =
@@ -2589,6 +2639,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2639 knownOriginDefaultBranch: defaultBranch,
2640 untrackedRoots,
2641 headOid,
2642 + ...(index ? { indexPath: index } : {}),
2643 }).pipe(Effect.orElseSucceed(() => undefined))
2644 : Effect.undefined,
2645 ],
@@ -3649,6 +3700,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3700 cwd: string,
3701 untrackedPaths: ReadonlyArray<string>,
3702 allowPartial: boolean,
3703 + knownIndexPath?: string,
3704 ) {
3705 const [stagedDeletionsStdout, indexValue] = yield* Effect.all(
3706 [
@@ -3667,11 +3719,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3719 ],
3720 { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES },
3721 ),
3670 - runGitStdout("GitVcsDriver.readUnifiedWorkingTreeReviewDiff.indexPath", cwd, [
3671 - "rev-parse",
3672 - "--git-path",
3673 - "index",
3674 - ]),
3722 + knownIndexPath === undefined
3723 + ? runGitStdout("GitVcsDriver.readUnifiedWorkingTreeReviewDiff.indexPath", cwd, [
3724 + "rev-parse",
3725 + "--git-path",
3726 + "index",
3727 + ])
3728 + : Effect.succeed(knownIndexPath),
3729 ],
3730 { concurrency: 2 },
3731 );
@@ -3713,9 +3767,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3767 const omitted = pathsToAdd.length < candidates.length;
3768 if (pathsToAdd.length === 0 || (omitted && !allowPartial)) return { env: undefined, omitted };
3769
3716 - const indexPath = path.isAbsolute(indexValue.trim())
3717 - ? indexValue.trim()
3718 - : path.resolve(cwd, indexValue.trim());
3770 + const indexPath = knownIndexPath ?? path.resolve(cwd, indexValue.replace(/\n$/, ""));
3771 const tempIndexPath = yield* fileSystem.makeTempFileScoped({
3772 prefix: `t3code-review-index-${process.pid}-`,
3773 });
@@ -3782,6 +3834,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3834 pathArgs: ReadonlyArray<string>,
3835 onlyPath: string | undefined,
3836 allowPartial: boolean,
3837 + knownIndexPath?: string,
3838 ) {
3839 const untracked = yield* executeGit(
3840 "GitVcsDriver.review.listUntracked",
@@ -3803,7 +3856,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3856 if (paths.length === 0) return { env: undefined, omitted: false };
3857 if (paths.length > REVIEW_UNTRACKED_MAX_FILES)
3858 return { unavailable: "Too many untracked files to count." };
3806 - const index = yield* prepareReviewIndex(cwd, paths, allowPartial).pipe(
3859 + const index = yield* prepareReviewIndex(cwd, paths, allowPartial, knownIndexPath).pipe(
3860 Effect.catchTags({
3861 PlatformError: (cause) =>
3862 Effect.fail(
@@ -3884,6 +3937,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3937 readonly knownOriginDefaultBranch: string | null;
3938 readonly untrackedRoots: ReadonlyArray<string>;
3939 readonly headOid: string | null;
3940 + readonly indexPath?: string;
3941 },
3942 ) {
3943 const resolved = yield* resolveReviewMergeBase(
@@ -3937,7 +3991,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3991 const untracked =
3992 roots.length === 0
3993 ? { env: undefined, omitted: false }
3940 - : yield* prepareUntrackedReviewIndexOrReason(cwd, pathspecs, undefined, false);
3994 + : yield* prepareUntrackedReviewIndexOrReason(
3995 + cwd,
3996 + pathspecs,
3997 + undefined,
3998 + false,
3999 + options?.indexPath,
4000 + );
4001 if ("unavailable" in untracked) {
4002 const count = remembered?.roots === rootsKey ? remembered.count + 1 : 1;
4003 untrackedTotalsFailures.delete(cwd);
packages/shared/src/shell.test.ts
+11
@@ -666,6 +666,17 @@ describe("runsGitWithoutConsole", () => {
666
667 it.each([
668 [["rev-parse", "--git-path", "index"], true],
669 + [
670 + [
671 + "rev-parse",
672 + "--path-format=absolute",
673 + "--git-path",
674 + "index",
675 + "--git-common-dir",
676 + "--show-toplevel",
677 + ],
678 + true,
679 + ],
680 [["--git-dir", "C:\\repo\\.git", "symbolic-ref", "refs/remotes/origin/HEAD"], true],
681 [["remote", "get-url", "--", "origin"], true],
682 [["config", "--get", "branch.main.pushRemote"], true],
packages/shared/src/shell.ts
+1
@@ -929,6 +929,7 @@ const CONSOLE_FREE_GIT_READS = [
929 "rev-parse --git-path index",
930 "rev-parse --git-common-dir",
931 "rev-parse --show-toplevel",
932 + "rev-parse --path-format=absolute --git-path index --git-common-dir --show-toplevel",
933 "rev-parse --symbolic-full-name HEAD",
934 "rev-parse --abbrev-ref --symbolic-full-name @{upstream}",
935 "symbolic-ref --quiet HEAD",