Skip to content

Commit 16cbcf3

Browse files
committed
perf(server): reuse origin default in remote status
1 parent b9d289b commit 16cbcf3

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2583,8 +2583,33 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
25832583
yield* git(cwd, ["commit", "-m", "feature commit"]);
25842584
yield* git(cwd, ["push", "-u", "origin", "feature/remote-status"]);
25852585
yield* writeTextFile(cwd, "untracked.txt", "local-only\n");
2586+
yield* git(cwd, [
2587+
"symbolic-ref",
2588+
"refs/remotes/origin/HEAD",
2589+
`refs/remotes/origin/${initialBranch}`,
2590+
]);
25862591

2587-
const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetailsRemote(cwd);
2592+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2593+
let originHeadReads = 0;
2594+
let failFirstOriginHead = false;
2595+
const spawner = ChildProcessSpawner.make((command) =>
2596+
Effect.gen(function* () {
2597+
if (
2598+
ChildProcess.isStandardCommand(command) &&
2599+
command.args.join(" ") === "symbolic-ref refs/remotes/origin/HEAD"
2600+
) {
2601+
originHeadReads++;
2602+
if (failFirstOriginHead && originHeadReads === 1) return makeNonRepositoryHandle();
2603+
}
2604+
return yield* delegate.spawn(command);
2605+
}),
2606+
);
2607+
const driver = yield* makeGitVcsDriverCore().pipe(
2608+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
2609+
Effect.provide(layerServerConfig),
2610+
);
2611+
const status = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
2612+
const successfulHeadReads = originHeadReads;
25882613

25892614
assert.equal(status.isRepo, true);
25902615
assert.equal(status.branch, "feature/remote-status");
@@ -2594,6 +2619,14 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
25942619
assert.equal(status.aheadOfDefaultCount, 1);
25952620
assert.notProperty(status, "workingTree");
25962621
assert.notProperty(status, "hasWorkingTreeChanges");
2622+
2623+
// An unavailable first read must retain the comparison lookup's retry.
2624+
originHeadReads = 0;
2625+
failFirstOriginHead = true;
2626+
const retried = yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
2627+
assert.deepEqual(retried, { ...status, defaultBranch: null });
2628+
assert.equal(originHeadReads, 2);
2629+
assert.equal(successfulHeadReads, 1);
25972630
}),
25982631
);
25992632

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16811681
const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* (
16821682
cwd: string,
16831683
refName: string,
1684-
options?: { readonly allowRemoteOfCurrent?: boolean },
1684+
options?: {
1685+
readonly allowRemoteOfCurrent?: boolean;
1686+
/** Null can mean a failed lookup, so only a successful value skips the retry. */
1687+
readonly knownOriginDefaultBranch?: string | null;
1688+
},
16851689
) {
16861690
const configuredBaseBranch = yield* runGitStdout(
16871691
"GitVcsDriver.resolveBaseBranchForNoUpstream.config",
@@ -1697,7 +1701,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16971701
Effect.succeed(configuredBaseBranch.length > 0 ? configuredBaseBranch : null),
16981702
primaryRemoteName === null
16991703
? Effect.succeed(null)
1700-
: resolveDefaultBranchName(cwd, primaryRemoteName),
1704+
: primaryRemoteName === "origin" && options?.knownOriginDefaultBranch != null
1705+
? Effect.succeed(options.knownOriginDefaultBranch)
1706+
: resolveDefaultBranchName(cwd, primaryRemoteName),
17011707
...DEFAULT_BASE_BRANCH_CANDIDATES.map(Effect.succeed),
17021708
];
17031709

@@ -1755,8 +1761,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17551761
const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* (
17561762
cwd: string,
17571763
refName: string,
1764+
options?: { readonly knownOriginDefaultBranch: string | null },
17581765
) {
1759-
const baseRef = yield* resolveBaseBranchForNoUpstream(cwd, refName);
1766+
const baseRef = yield* resolveBaseBranchForNoUpstream(cwd, refName, options);
17601767
if (!baseRef) {
17611768
return 0;
17621769
}
@@ -1854,7 +1861,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
18541861
branch && !isDefaultBranch
18551862
? upstreamRef === null
18561863
? aheadCount
1857-
: yield* computeAheadCountAgainstBase(cwd, branch).pipe(Effect.orElseSucceed(() => 0))
1864+
: yield* computeAheadCountAgainstBase(cwd, branch, {
1865+
knownOriginDefaultBranch: defaultBranch,
1866+
}).pipe(Effect.orElseSucceed(() => 0))
18581867
: 0;
18591868

18601869
return {

0 commit comments

Comments
 (0)