Skip to content
TypeScript

perf(server): skip unused default-branch lookups (#19825)

IfkumRfnlbe21de48bc48
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index 6b05860b5ab..db48a5e9526 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -2963,6 +2963,27 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
aheadOfDefaultCount: 0,
});
}
+
+ yield* git(cwd, ["branch", "-f", initialBranch, "HEAD"]);
+ yield* git(cwd, ["update-ref", "refs/remotes/origin/trunk", "HEAD"]);
+ yield* git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/trunk"]);
+ for (const base of [initialBranch, "missing-base"]) {
+ yield* git(cwd, ["config", "branch.feature/local-only.gh-merge-base", base]);
+ commands.length = 0;
+ const status = yield* driver.statusDetailsLocal(cwd, {
+ includeDivergence: false,
+ includeBranchChanges: true,
+ });
+ assert.deepEqual(status.branchChanges, {
+ baseRef: `origin/${base === initialBranch ? initialBranch : "trunk"}`,
+ insertions: base === initialBranch ? 3 : 2,
+ deletions: 0,
+ });
+ assert.lengthOf(
+ commands.filter((args) => args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD"),
+ base === initialBranch ? 0 : 1,
+ );
+ }
}),
);
@@ -2970,27 +2991,57 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
- const { initialBranch } = yield* initRepoWithCommit(cwd);
+ yield* initRepoWithCommit(cwd);
+ yield* git(cwd, ["branch", "-M", "trunk"]);
yield* git(remote, ["init", "--bare"]);
yield* git(cwd, ["remote", "add", "origin", remote]);
- yield* git(cwd, ["push", "-u", "origin", initialBranch]);
+ yield* git(cwd, ["push", "-u", "origin", "trunk"]);
yield* git(cwd, ["checkout", "-b", "feature/remote-status"]);
yield* writeTextFile(cwd, "feature.txt", "feature\n");
yield* git(cwd, ["add", "feature.txt"]);
yield* git(cwd, ["commit", "-m", "feature commit"]);
yield* git(cwd, ["push", "-u", "origin", "feature/remote-status"]);
yield* writeTextFile(cwd, "untracked.txt", "local-only\n");
+ yield* git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/trunk"]);
- const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetailsRemote(cwd);
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
+ let originHeadReads = 0;
+ let failFirstOriginHead = false;
+ const spawner = ChildProcessSpawner.make((command) =>
+ Effect.gen(function* () {
+ if (
+ ChildProcess.isStandardCommand(command) &&
+ command.args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD"
+ ) {
+ originHeadReads++;
+ if (failFirstOriginHead && originHeadReads === 1) return makeNonRepositoryHandle();
+ }
+ return yield* delegate.spawn(command);
+ }),
+ );
+ const driver = yield* makeGitVcsDriverCore().pipe(
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ Effect.provide(layerServerConfig),
+ );
+ const status = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
assert.equal(status.isRepo, true);
assert.equal(status.branch, "feature/remote-status");
+ assert.equal(status.defaultBranch, "trunk");
assert.equal(status.hasUpstream, true);
assert.equal(status.aheadCount, 0);
assert.equal(status.behindCount, 0);
assert.equal(status.aheadOfDefaultCount, 1);
assert.notProperty(status, "workingTree");
assert.notProperty(status, "hasWorkingTreeChanges");
+ assert.equal(originHeadReads, 1);
+
+ // With no main/master fallback, the retry must resolve trunk to preserve the ahead count.
+ originHeadReads = 0;
+ failFirstOriginHead = true;
+ const retried = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
+ assert.deepEqual(retried, { ...status, defaultBranch: null });
+ assert.equal(originHeadReads, 2);
}),
);
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index f2c32a94a28..475ba3d2a95 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -1701,7 +1701,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* (
cwd: string,
refName: string,
- options?: { readonly allowRemoteOfCurrent?: boolean },
+ options?: {
+ readonly allowRemoteOfCurrent?: boolean;
+ readonly knownOriginDefaultBranch?: string | null;
+ },
) {
const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`);
if (
@@ -1735,15 +1738,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const primaryRemoteName = yield* resolvePrimaryRemoteName(cwd).pipe(
Effect.orElseSucceed(() => null),
);
- const defaultBranch =
- primaryRemoteName === null ? null : yield* resolveDefaultBranchName(cwd, primaryRemoteName);
const candidates = [
- configuredBaseBranch.length > 0 ? configuredBaseBranch : null,
- defaultBranch,
- ...DEFAULT_BASE_BRANCH_CANDIDATES,
+ Effect.succeed(configuredBaseBranch),
+ primaryRemoteName === null
+ ? Effect.succeed(null)
+ : primaryRemoteName === "origin" && options?.knownOriginDefaultBranch != null
+ ? Effect.succeed(options.knownOriginDefaultBranch)
+ : resolveDefaultBranchName(cwd, primaryRemoteName),
+ ...DEFAULT_BASE_BRANCH_CANDIDATES.map(Effect.succeed),
];
- for (const candidate of candidates) {
+ for (const readCandidate of candidates) {
+ const candidate = yield* readCandidate;
if (!candidate) {
continue;
}
@@ -1804,8 +1810,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* (
cwd: string,
refName: string,
+ options?: { readonly knownOriginDefaultBranch: string | null },
) {
- const base = yield* resolveBaseBranchForNoUpstream(cwd, refName);
+ const base = yield* resolveBaseBranchForNoUpstream(cwd, refName, options);
if (!base) {
return 0;
}
@@ -1903,7 +1910,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
branch && !isDefaultBranch
? upstreamRef === null
? aheadCount
- : yield* computeAheadCountAgainstBase(cwd, branch).pipe(Effect.orElseSucceed(() => 0))
+ : yield* computeAheadCountAgainstBase(cwd, branch, {
+ knownOriginDefaultBranch: defaultBranch,
+ }).pipe(Effect.orElseSucceed(() => 0))
: 0;
return {