TypeScript
perf(server): reuse untracked paths in Git status (#28374)
Guanying Chenb0d2a86dd8bf
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex d9a48457f8a..e2e78a02015 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -3120,13 +3120,14 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {yield* writeTextFile(cwd, "README.md", "# test\ncommitted\n");yield* git(cwd, ["commit", "-am", "commit edit"]);yield* writeTextFile(cwd, "nested/untracked.txt", "one\ntwo\n");+ yield* writeTextFile(cwd, "new [1].txt", "literal\n");const status = yield* driver.statusDetailsLocal(pathService.join(cwd, "nested"), {includeBranchChanges: true,});assert.deepStrictEqual(status.branchChanges, {baseRef: "main",- insertions: 3,+ insertions: 4,deletions: 0,});assert.isUndefined((yield* driver.statusDetailsLocal(cwd)).branchChanges);@@ -3183,25 +3184,58 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {Effect.gen(function* () {const cwd = yield* makeTmpDir();yield* initRepoWithCommit(cwd);- const driver = yield* GitVcsDriver.GitVcsDriver;+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;+ const untrackedScanRoots: string[][] = [];+ const spawner = ChildProcessSpawner.make((command) => {+ if (+ ChildProcess.isStandardCommand(command) &&+ command.args.includes("ls-files") &&+ command.args.includes("--others")+ ) {+ untrackedScanRoots.push(command.args.slice(command.args.indexOf("--") + 1));+ }+ return delegate.spawn(command);+ });+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ );yield* git(cwd, ["branch", "-M", "main"]);yield* git(cwd, ["checkout", "-b", "feature/many-untracked"]);+ yield* writeTextFile(cwd, ".git/info/exclude", "ignored.txt\n");+ yield* writeTextFile(cwd, "bulk[1]/ignored.txt", "ignored\n");yield* Effect.forEach(- Array.from({ length: 1_001 }, (_, index) => `bulk/${index}.txt`),+ Array.from({ length: 1_001 }, (_, index) => `bulk[1]/${index}.txt`),(file) => writeTextFile(cwd, file, "one\ntwo\n"),{ concurrency: 32, discard: true },);- const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });- assert.deepStrictEqual(status.branchChanges, {- baseRef: "main",- insertions: 2_002,- deletions: 0,- });- assert.deepStrictEqual(- status.workingTree.files.map((file) => file.path),- ["bulk/"],- );+ for (const scenario of ["normal", "all", "no", "long names"]) {+ if (scenario === "long names") {+ yield* Effect.forEach(+ Array.from({ length: 50 }, (_, index) => `${"a".repeat(180)}-${index}.txt`),+ (file) => writeTextFile(cwd, file, "long name\n"),+ { concurrency: 32, discard: true },+ );+ }+ const mode = scenario === "long names" ? "normal" : scenario;+ yield* git(cwd, ["config", "status.showUntrackedFiles", mode]);+ untrackedScanRoots.length = 0;+ const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });+ assert.deepStrictEqual(status.branchChanges, {+ baseRef: "main",+ insertions: scenario === "long names" ? 2_052 : 2_002,+ deletions: 0,+ });+ assert.deepStrictEqual(untrackedScanRoots, [+ scenario === "normal" ? [":(top,literal)bulk[1]/"] : [],+ ]);+ if (scenario === "normal") {+ assert.deepStrictEqual(+ status.workingTree.files.map((file) => file.path),+ ["bulk[1]/"],+ );+ }+ }}),);diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 11580f4c26e..cc6699ff3d3 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -2232,6 +2232,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*let aheadOfDefaultCount = 0;let hasWorkingTreeChanges = false;const changedFilesWithoutNumstat = new Set<string>();+ const untrackedRoots: string[] = [];const statusRecords = statusStdout.split("\0");for (let index = 0; index < statusRecords.length; index++) {@@ -2256,7 +2257,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*if (line.length > 0 && !line.startsWith("#")) {hasWorkingTreeChanges = true;const pathValue = parsePorcelainPath(line);- if (pathValue) changedFilesWithoutNumstat.add(pathValue);+ if (pathValue) {+ changedFilesWithoutNumstat.add(pathValue);+ if (line.startsWith("? ")) untrackedRoots.push(pathValue);+ }// A rename record is followed by its original path as a separate record. Listing it// keeps the deletion selectable when committing only some files.if (line.startsWith("2 ")) {@@ -2282,6 +2286,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*options?.includeBranchChanges? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, {knownOriginDefaultBranch: defaultBranch,+ untrackedRoots,}).pipe(Effect.orElseSucceed(() => undefined)): Effect.undefined,],@@ -3133,7 +3138,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*const readBranchChangeTotals = Effect.fn("readBranchChangeTotals")(function* (cwd: string,branch: string | null,- options?: { readonly knownOriginDefaultBranch: string | null },+ options?: {+ readonly knownOriginDefaultBranch: string | null;+ readonly untrackedRoots: ReadonlyArray<string>;+ },) {const { baseRef, mergeBase } = yield* resolveReviewMergeBase(cwd,@@ -3141,7 +3149,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*undefined,options?.knownOriginDefaultBranch ?? null,);- const untracked = yield* prepareUntrackedReviewIndex(cwd, []);+ const roots = options?.untrackedRoots ?? [];+ // Bound pathspec matching work and stay below Windows' command-line limit.+ const pathspecs =+ roots.length <= 128 && roots.reduce((length, root) => length + root.length + 16, 0) < 8_000+ ? roots.map((root) => `:(top,literal)${root}`)+ : [];+ // Empty status can mean untracked files are hidden; retain the full scan.+ const untracked = yield* prepareUntrackedReviewIndex(cwd, pathspecs);if (untracked === null) {return yield* new GitCommandError({operation: "GitVcsDriver.readBranchChangeTotals",