Skip to content

Commit b0d2a86

Browse files
authored
perf(server): reuse untracked paths in Git status (pingdotgg#28374)
1 parent 5806aff commit b0d2a86

2 files changed

Lines changed: 65 additions & 16 deletions

File tree

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

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,13 +3120,14 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
31203120
yield* writeTextFile(cwd, "README.md", "# test\ncommitted\n");
31213121
yield* git(cwd, ["commit", "-am", "commit edit"]);
31223122
yield* writeTextFile(cwd, "nested/untracked.txt", "one\ntwo\n");
3123+
yield* writeTextFile(cwd, "new [1].txt", "literal\n");
31233124

31243125
const status = yield* driver.statusDetailsLocal(pathService.join(cwd, "nested"), {
31253126
includeBranchChanges: true,
31263127
});
31273128
assert.deepStrictEqual(status.branchChanges, {
31283129
baseRef: "main",
3129-
insertions: 3,
3130+
insertions: 4,
31303131
deletions: 0,
31313132
});
31323133
assert.isUndefined((yield* driver.statusDetailsLocal(cwd)).branchChanges);
@@ -3183,25 +3184,58 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
31833184
Effect.gen(function* () {
31843185
const cwd = yield* makeTmpDir();
31853186
yield* initRepoWithCommit(cwd);
3186-
const driver = yield* GitVcsDriver.GitVcsDriver;
3187+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3188+
const untrackedScanRoots: string[][] = [];
3189+
const spawner = ChildProcessSpawner.make((command) => {
3190+
if (
3191+
ChildProcess.isStandardCommand(command) &&
3192+
command.args.includes("ls-files") &&
3193+
command.args.includes("--others")
3194+
) {
3195+
untrackedScanRoots.push(command.args.slice(command.args.indexOf("--") + 1));
3196+
}
3197+
return delegate.spawn(command);
3198+
});
3199+
const driver = yield* makeGitVcsDriverCore().pipe(
3200+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
3201+
);
31873202
yield* git(cwd, ["branch", "-M", "main"]);
31883203
yield* git(cwd, ["checkout", "-b", "feature/many-untracked"]);
3204+
yield* writeTextFile(cwd, ".git/info/exclude", "ignored.txt\n");
3205+
yield* writeTextFile(cwd, "bulk[1]/ignored.txt", "ignored\n");
31893206
yield* Effect.forEach(
3190-
Array.from({ length: 1_001 }, (_, index) => `bulk/${index}.txt`),
3207+
Array.from({ length: 1_001 }, (_, index) => `bulk[1]/${index}.txt`),
31913208
(file) => writeTextFile(cwd, file, "one\ntwo\n"),
31923209
{ concurrency: 32, discard: true },
31933210
);
31943211

3195-
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
3196-
assert.deepStrictEqual(status.branchChanges, {
3197-
baseRef: "main",
3198-
insertions: 2_002,
3199-
deletions: 0,
3200-
});
3201-
assert.deepStrictEqual(
3202-
status.workingTree.files.map((file) => file.path),
3203-
["bulk/"],
3204-
);
3212+
for (const scenario of ["normal", "all", "no", "long names"]) {
3213+
if (scenario === "long names") {
3214+
yield* Effect.forEach(
3215+
Array.from({ length: 50 }, (_, index) => `${"a".repeat(180)}-${index}.txt`),
3216+
(file) => writeTextFile(cwd, file, "long name\n"),
3217+
{ concurrency: 32, discard: true },
3218+
);
3219+
}
3220+
const mode = scenario === "long names" ? "normal" : scenario;
3221+
yield* git(cwd, ["config", "status.showUntrackedFiles", mode]);
3222+
untrackedScanRoots.length = 0;
3223+
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
3224+
assert.deepStrictEqual(status.branchChanges, {
3225+
baseRef: "main",
3226+
insertions: scenario === "long names" ? 2_052 : 2_002,
3227+
deletions: 0,
3228+
});
3229+
assert.deepStrictEqual(untrackedScanRoots, [
3230+
scenario === "normal" ? [":(top,literal)bulk[1]/"] : [],
3231+
]);
3232+
if (scenario === "normal") {
3233+
assert.deepStrictEqual(
3234+
status.workingTree.files.map((file) => file.path),
3235+
["bulk[1]/"],
3236+
);
3237+
}
3238+
}
32053239
}),
32063240
);
32073241

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22322232
let aheadOfDefaultCount = 0;
22332233
let hasWorkingTreeChanges = false;
22342234
const changedFilesWithoutNumstat = new Set<string>();
2235+
const untrackedRoots: string[] = [];
22352236

22362237
const statusRecords = statusStdout.split("\0");
22372238
for (let index = 0; index < statusRecords.length; index++) {
@@ -2256,7 +2257,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22562257
if (line.length > 0 && !line.startsWith("#")) {
22572258
hasWorkingTreeChanges = true;
22582259
const pathValue = parsePorcelainPath(line);
2259-
if (pathValue) changedFilesWithoutNumstat.add(pathValue);
2260+
if (pathValue) {
2261+
changedFilesWithoutNumstat.add(pathValue);
2262+
if (line.startsWith("? ")) untrackedRoots.push(pathValue);
2263+
}
22602264
// A rename record is followed by its original path as a separate record. Listing it
22612265
// keeps the deletion selectable when committing only some files.
22622266
if (line.startsWith("2 ")) {
@@ -2282,6 +2286,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22822286
options?.includeBranchChanges
22832287
? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, {
22842288
knownOriginDefaultBranch: defaultBranch,
2289+
untrackedRoots,
22852290
}).pipe(Effect.orElseSucceed(() => undefined))
22862291
: Effect.undefined,
22872292
],
@@ -3133,15 +3138,25 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
31333138
const readBranchChangeTotals = Effect.fn("readBranchChangeTotals")(function* (
31343139
cwd: string,
31353140
branch: string | null,
3136-
options?: { readonly knownOriginDefaultBranch: string | null },
3141+
options?: {
3142+
readonly knownOriginDefaultBranch: string | null;
3143+
readonly untrackedRoots: ReadonlyArray<string>;
3144+
},
31373145
) {
31383146
const { baseRef, mergeBase } = yield* resolveReviewMergeBase(
31393147
cwd,
31403148
branch,
31413149
undefined,
31423150
options?.knownOriginDefaultBranch ?? null,
31433151
);
3144-
const untracked = yield* prepareUntrackedReviewIndex(cwd, []);
3152+
const roots = options?.untrackedRoots ?? [];
3153+
// Bound pathspec matching work and stay below Windows' command-line limit.
3154+
const pathspecs =
3155+
roots.length <= 128 && roots.reduce((length, root) => length + root.length + 16, 0) < 8_000
3156+
? roots.map((root) => `:(top,literal)${root}`)
3157+
: [];
3158+
// Empty status can mean untracked files are hidden; retain the full scan.
3159+
const untracked = yield* prepareUntrackedReviewIndex(cwd, pathspecs);
31453160
if (untracked === null) {
31463161
return yield* new GitCommandError({
31473162
operation: "GitVcsDriver.readBranchChangeTotals",

0 commit comments

Comments
 (0)