Skip to content

Commit be21de4

Browse files
perf(server): skip unused default-branch lookups (pingdotgg#19825)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 1e659c9 commit be21de4

2 files changed

Lines changed: 72 additions & 12 deletions

File tree

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

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,34 +2963,85 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
29632963
aheadOfDefaultCount: 0,
29642964
});
29652965
}
2966+
2967+
yield* git(cwd, ["branch", "-f", initialBranch, "HEAD"]);
2968+
yield* git(cwd, ["update-ref", "refs/remotes/origin/trunk", "HEAD"]);
2969+
yield* git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/trunk"]);
2970+
for (const base of [initialBranch, "missing-base"]) {
2971+
yield* git(cwd, ["config", "branch.feature/local-only.gh-merge-base", base]);
2972+
commands.length = 0;
2973+
const status = yield* driver.statusDetailsLocal(cwd, {
2974+
includeDivergence: false,
2975+
includeBranchChanges: true,
2976+
});
2977+
assert.deepEqual(status.branchChanges, {
2978+
baseRef: `origin/${base === initialBranch ? initialBranch : "trunk"}`,
2979+
insertions: base === initialBranch ? 3 : 2,
2980+
deletions: 0,
2981+
});
2982+
assert.lengthOf(
2983+
commands.filter((args) => args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD"),
2984+
base === initialBranch ? 0 : 1,
2985+
);
2986+
}
29662987
}),
29672988
);
29682989

29692990
it.effect("reports remote divergence without reading working-tree details", () =>
29702991
Effect.gen(function* () {
29712992
const cwd = yield* makeTmpDir();
29722993
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
2973-
const { initialBranch } = yield* initRepoWithCommit(cwd);
2994+
yield* initRepoWithCommit(cwd);
2995+
yield* git(cwd, ["branch", "-M", "trunk"]);
29742996
yield* git(remote, ["init", "--bare"]);
29752997
yield* git(cwd, ["remote", "add", "origin", remote]);
2976-
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
2998+
yield* git(cwd, ["push", "-u", "origin", "trunk"]);
29772999
yield* git(cwd, ["checkout", "-b", "feature/remote-status"]);
29783000
yield* writeTextFile(cwd, "feature.txt", "feature\n");
29793001
yield* git(cwd, ["add", "feature.txt"]);
29803002
yield* git(cwd, ["commit", "-m", "feature commit"]);
29813003
yield* git(cwd, ["push", "-u", "origin", "feature/remote-status"]);
29823004
yield* writeTextFile(cwd, "untracked.txt", "local-only\n");
3005+
yield* git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/trunk"]);
29833006

2984-
const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetailsRemote(cwd);
3007+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3008+
let originHeadReads = 0;
3009+
let failFirstOriginHead = false;
3010+
const spawner = ChildProcessSpawner.make((command) =>
3011+
Effect.gen(function* () {
3012+
if (
3013+
ChildProcess.isStandardCommand(command) &&
3014+
command.args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD"
3015+
) {
3016+
originHeadReads++;
3017+
if (failFirstOriginHead && originHeadReads === 1) return makeNonRepositoryHandle();
3018+
}
3019+
return yield* delegate.spawn(command);
3020+
}),
3021+
);
3022+
const driver = yield* makeGitVcsDriverCore().pipe(
3023+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
3024+
Effect.provide(layerServerConfig),
3025+
);
3026+
const status = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
29853027

29863028
assert.equal(status.isRepo, true);
29873029
assert.equal(status.branch, "feature/remote-status");
3030+
assert.equal(status.defaultBranch, "trunk");
29883031
assert.equal(status.hasUpstream, true);
29893032
assert.equal(status.aheadCount, 0);
29903033
assert.equal(status.behindCount, 0);
29913034
assert.equal(status.aheadOfDefaultCount, 1);
29923035
assert.notProperty(status, "workingTree");
29933036
assert.notProperty(status, "hasWorkingTreeChanges");
3037+
assert.equal(originHeadReads, 1);
3038+
3039+
// With no main/master fallback, the retry must resolve trunk to preserve the ahead count.
3040+
originHeadReads = 0;
3041+
failFirstOriginHead = true;
3042+
const retried = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
3043+
assert.deepEqual(retried, { ...status, defaultBranch: null });
3044+
assert.equal(originHeadReads, 2);
29943045
}),
29953046
);
29963047

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17011701
const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* (
17021702
cwd: string,
17031703
refName: string,
1704-
options?: { readonly allowRemoteOfCurrent?: boolean },
1704+
options?: {
1705+
readonly allowRemoteOfCurrent?: boolean;
1706+
readonly knownOriginDefaultBranch?: string | null;
1707+
},
17051708
) {
17061709
const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`);
17071710
if (
@@ -1735,15 +1738,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17351738
const primaryRemoteName = yield* resolvePrimaryRemoteName(cwd).pipe(
17361739
Effect.orElseSucceed(() => null),
17371740
);
1738-
const defaultBranch =
1739-
primaryRemoteName === null ? null : yield* resolveDefaultBranchName(cwd, primaryRemoteName);
17401741
const candidates = [
1741-
configuredBaseBranch.length > 0 ? configuredBaseBranch : null,
1742-
defaultBranch,
1743-
...DEFAULT_BASE_BRANCH_CANDIDATES,
1742+
Effect.succeed(configuredBaseBranch),
1743+
primaryRemoteName === null
1744+
? Effect.succeed(null)
1745+
: primaryRemoteName === "origin" && options?.knownOriginDefaultBranch != null
1746+
? Effect.succeed(options.knownOriginDefaultBranch)
1747+
: resolveDefaultBranchName(cwd, primaryRemoteName),
1748+
...DEFAULT_BASE_BRANCH_CANDIDATES.map(Effect.succeed),
17441749
];
17451750

1746-
for (const candidate of candidates) {
1751+
for (const readCandidate of candidates) {
1752+
const candidate = yield* readCandidate;
17471753
if (!candidate) {
17481754
continue;
17491755
}
@@ -1804,8 +1810,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
18041810
const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* (
18051811
cwd: string,
18061812
refName: string,
1813+
options?: { readonly knownOriginDefaultBranch: string | null },
18071814
) {
1808-
const base = yield* resolveBaseBranchForNoUpstream(cwd, refName);
1815+
const base = yield* resolveBaseBranchForNoUpstream(cwd, refName, options);
18091816
if (!base) {
18101817
return 0;
18111818
}
@@ -1903,7 +1910,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19031910
branch && !isDefaultBranch
19041911
? upstreamRef === null
19051912
? aheadCount
1906-
: yield* computeAheadCountAgainstBase(cwd, branch).pipe(Effect.orElseSucceed(() => 0))
1913+
: yield* computeAheadCountAgainstBase(cwd, branch, {
1914+
knownOriginDefaultBranch: defaultBranch,
1915+
}).pipe(Effect.orElseSucceed(() => 0))
19071916
: 0;
19081917

19091918
return {

0 commit comments

Comments
 (0)