Skip to content

Commit d7eac71

Browse files
committed
perf(server): skip unused probes when switching Git refs
1 parent fa1184d commit d7eac71

2 files changed

Lines changed: 65 additions & 20 deletions

File tree

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,23 +2974,67 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
29742974
const cwd = yield* makeTmpDir();
29752975
const remote = yield* makeTmpDir("git-remote-");
29762976
const { initialBranch } = yield* initRepoWithCommit(cwd);
2977-
const driver = yield* GitVcsDriver.GitVcsDriver;
2977+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2978+
const commands: Array<ReadonlyArray<string>> = [];
2979+
const spawner = ChildProcessSpawner.make((command) =>
2980+
Effect.gen(function* () {
2981+
if (ChildProcess.isStandardCommand(command)) commands.push(command.args);
2982+
return yield* delegate.spawn(command);
2983+
}),
2984+
);
2985+
const driver = yield* makeGitVcsDriverCore().pipe(
2986+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
2987+
Effect.provide(layerServerConfig),
2988+
);
2989+
const assertCheckout = (
2990+
checkoutArgs: ReadonlyArray<string>,
2991+
existenceReads: number,
2992+
trackingReads: number,
2993+
) => {
2994+
assert.deepEqual(
2995+
commands.filter((args) => args[0] === "checkout"),
2996+
[checkoutArgs],
2997+
);
2998+
assert.equal(commands.filter((args) => args[0] === "show-ref").length, existenceReads);
2999+
assert.equal(commands.filter((args) => args[0] === "for-each-ref").length, trackingReads);
3000+
assert.deepEqual(
3001+
commands.filter((args) => args[0] === "branch"),
3002+
[["branch", "--show-current"]],
3003+
);
3004+
};
29783005
yield* git(remote, ["init", "--bare"]);
29793006
yield* git(cwd, ["remote", "add", "origin", remote]);
29803007
yield* git(cwd, ["push", "origin", "HEAD:refs/heads/remote-only"]);
29813008

29823009
for (let attempt = 0; attempt < 2; attempt += 1) {
3010+
commands.length = 0;
29833011
const result = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });
2984-
assert.equal(result.refName, "remote-only");
3012+
assert.equal(result.refName, attempt === 0 ? "remote-only" : "review/renamed");
3013+
assertCheckout(
3014+
attempt === 0
3015+
? ["checkout", "--track", "origin/remote-only", "--"]
3016+
: ["checkout", "review/renamed", "--"],
3017+
attempt === 0 ? 3 : 2,
3018+
1,
3019+
);
29853020
assert.equal(
29863021
yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
29873022
"origin/remote-only",
29883023
);
3024+
if (attempt === 0) yield* git(cwd, ["branch", "-m", "review/renamed"]);
29893025
yield* driver.switchRef({ cwd, refName: initialBranch });
29903026
}
3027+
yield* git(cwd, ["branch", "origin/remote-only"]);
3028+
commands.length = 0;
3029+
const shadowed = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });
3030+
assert.equal(shadowed.refName, "origin/remote-only");
3031+
assertCheckout(["checkout", "origin/remote-only", "--"], 2, 0);
3032+
29913033
const commit = yield* git(cwd, ["rev-parse", "HEAD"]);
3034+
commands.length = 0;
29923035
const detached = yield* driver.switchRef({ cwd, refName: commit });
29933036
assert.equal(detached.refName, null);
3037+
assertCheckout(["checkout", commit, "--"], 2, 0);
29943038
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), commit);
29953039
}),
29963040
);

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,27 +3972,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
39723972
{ concurrency: "unbounded" },
39733973
);
39743974

3975-
const localTrackingBranch = remoteExists
3976-
? yield* executeGit(
3977-
"GitVcsDriver.switchRef.localTrackingBranch",
3978-
input.cwd,
3979-
["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
3980-
{
3981-
timeoutMs: 5_000,
3982-
allowNonZeroExit: true,
3983-
},
3984-
).pipe(
3985-
Effect.map((result) =>
3986-
result.exitCode === 0
3987-
? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
3988-
: null,
3989-
),
3990-
)
3991-
: null;
3975+
const localTrackingBranch =
3976+
remoteExists && !localInputExists
3977+
? yield* executeGit(
3978+
"GitVcsDriver.switchRef.localTrackingBranch",
3979+
input.cwd,
3980+
["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
3981+
{
3982+
timeoutMs: 5_000,
3983+
allowNonZeroExit: true,
3984+
},
3985+
).pipe(
3986+
Effect.map((result) =>
3987+
result.exitCode === 0
3988+
? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
3989+
: null,
3990+
),
3991+
)
3992+
: null;
39923993

39933994
const localTrackedBranchCandidate = deriveLocalBranchNameFromRemoteRef(input.refName);
39943995
const localTrackedBranchTargetExists =
3995-
remoteExists && localTrackedBranchCandidate
3996+
remoteExists && !localInputExists && !localTrackingBranch && localTrackedBranchCandidate
39963997
? yield* executeGit(
39973998
"GitVcsDriver.switchRef.localTrackedBranchTargetExists",
39983999
input.cwd,

0 commit comments

Comments
 (0)