Skip to content

Commit c1c6be8

Browse files
perf(server): avoid remote enumeration during status reads (pingdotgg#19802)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 236be55 commit c1c6be8

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,14 +3542,16 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
35423542
yield* git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/trunk"]);
35433543

35443544
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3545+
const commands: ReadonlyArray<string>[] = [];
35453546
let originHeadReads = 0;
35463547
let failFirstOriginHead = false;
35473548
const spawner = ChildProcessSpawner.make((command) =>
35483549
Effect.gen(function* () {
3549-
if (
3550-
ChildProcess.isStandardCommand(command) &&
3551-
command.args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD"
3552-
) {
3550+
if (!ChildProcess.isStandardCommand(command)) {
3551+
return yield* Effect.die("expected a standard Git command");
3552+
}
3553+
commands.push(command.args);
3554+
if (command.args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD") {
35533555
originHeadReads++;
35543556
if (failFirstOriginHead && originHeadReads === 1) return makeNonRepositoryHandle();
35553557
}
@@ -3565,12 +3567,19 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
35653567
assert.equal(status.isRepo, true);
35663568
assert.equal(status.branch, "feature/remote-status");
35673569
assert.equal(status.defaultBranch, "trunk");
3570+
assert.equal(status.isDefaultBranch, false);
3571+
assert.equal(status.upstreamRef, "origin/feature/remote-status");
35683572
assert.equal(status.hasUpstream, true);
35693573
assert.equal(status.aheadCount, 0);
35703574
assert.equal(status.behindCount, 0);
35713575
assert.equal(status.aheadOfDefaultCount, 1);
35723576
assert.notProperty(status, "workingTree");
35733577
assert.notProperty(status, "hasWorkingTreeChanges");
3578+
assert.deepEqual(
3579+
commands.filter((args) => args.length === 1 && args[0] === "remote"),
3580+
[],
3581+
`cached remote status should not enumerate remotes: ${JSON.stringify(commands)}`,
3582+
);
35743583
assert.equal(originHeadReads, 1);
35753584

35763585
// With no main/master fallback, the retry must resolve trunk to preserve the ahead count.

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
12331233
});
12341234
});
12351235

1236-
const resolveCurrentUpstream = Effect.fn("resolveCurrentUpstream")(function* (cwd: string) {
1236+
const readCurrentUpstreamRef = Effect.fn("readCurrentUpstreamRef")(function* (cwd: string) {
12371237
const upstreamRef = yield* runGitStdout(
12381238
"GitVcsDriver.resolveCurrentUpstream",
12391239
cwd,
@@ -1244,6 +1244,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
12441244
if (upstreamRef.length === 0 || upstreamRef === "@{upstream}") {
12451245
return null;
12461246
}
1247+
return upstreamRef;
1248+
});
1249+
1250+
const resolveCurrentUpstream = Effect.fn("resolveCurrentUpstream")(function* (
1251+
cwd: string,
1252+
knownUpstreamRef?: string,
1253+
) {
1254+
const upstreamRef = knownUpstreamRef ?? (yield* readCurrentUpstreamRef(cwd));
1255+
if (upstreamRef === null) {
1256+
return null;
1257+
}
12471258

12481259
const remoteNames = yield* runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe(
12491260
Effect.map(parseRemoteNames),
@@ -1923,8 +1934,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19231934
const branchValue = branchResult.stdout.trim();
19241935
branch = branchValue.length > 0 && branchValue !== "HEAD" ? branchValue : null;
19251936
}
1926-
const upstream = yield* resolveCurrentUpstream(cwd);
1927-
const upstreamRef = upstream?.upstreamRef ?? null;
1937+
const currentUpstreamRef = yield* readCurrentUpstreamRef(cwd);
1938+
let upstreamRef =
1939+
currentUpstreamRef === null
1940+
? null
1941+
: (parseUpstreamRefByFirstSeparator(currentUpstreamRef)?.upstreamRef ?? null);
1942+
// Trimming a remote that starts with Unicode whitespace can leave a leading slash.
1943+
if (currentUpstreamRef?.startsWith("/")) {
1944+
upstreamRef = (yield* resolveCurrentUpstream(cwd, currentUpstreamRef))?.upstreamRef ?? null;
1945+
}
19281946
let aheadCount = 0;
19291947
let behindCount = 0;
19301948

0 commit comments

Comments
 (0)