Skip to content

Commit d688899

Browse files
authored
perf(server): skip unused probes when switching Git refs (pingdotgg#20140)
1 parent cd6b8b7 commit d688899

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
@@ -3062,23 +3062,67 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
30623062
const cwd = yield* makeTmpDir();
30633063
const remote = yield* makeTmpDir("git-remote-");
30643064
const { initialBranch } = yield* initRepoWithCommit(cwd);
3065-
const driver = yield* GitVcsDriver.GitVcsDriver;
3065+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
3066+
const commands: Array<ReadonlyArray<string>> = [];
3067+
const spawner = ChildProcessSpawner.make((command) =>
3068+
Effect.gen(function* () {
3069+
if (ChildProcess.isStandardCommand(command)) commands.push(command.args);
3070+
return yield* delegate.spawn(command);
3071+
}),
3072+
);
3073+
const driver = yield* makeGitVcsDriverCore().pipe(
3074+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
3075+
Effect.provide(layerServerConfig),
3076+
);
3077+
const assertCheckout = (
3078+
checkoutArgs: ReadonlyArray<string>,
3079+
existenceReads: number,
3080+
trackingReads: number,
3081+
) => {
3082+
assert.deepEqual(
3083+
commands.filter((args) => args[0] === "checkout"),
3084+
[checkoutArgs],
3085+
);
3086+
assert.equal(commands.filter((args) => args[0] === "show-ref").length, existenceReads);
3087+
assert.equal(commands.filter((args) => args[0] === "for-each-ref").length, trackingReads);
3088+
assert.deepEqual(
3089+
commands.filter((args) => args[0] === "branch"),
3090+
[["branch", "--show-current"]],
3091+
);
3092+
};
30663093
yield* git(remote, ["init", "--bare"]);
30673094
yield* git(cwd, ["remote", "add", "origin", remote]);
30683095
yield* git(cwd, ["push", "origin", "HEAD:refs/heads/remote-only"]);
30693096

30703097
for (let attempt = 0; attempt < 2; attempt += 1) {
3098+
commands.length = 0;
30713099
const result = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });
3072-
assert.equal(result.refName, "remote-only");
3100+
assert.equal(result.refName, attempt === 0 ? "remote-only" : "review/renamed");
3101+
assertCheckout(
3102+
attempt === 0
3103+
? ["checkout", "--track", "origin/remote-only", "--"]
3104+
: ["checkout", "review/renamed", "--"],
3105+
attempt === 0 ? 3 : 2,
3106+
1,
3107+
);
30733108
assert.equal(
30743109
yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
30753110
"origin/remote-only",
30763111
);
3112+
if (attempt === 0) yield* git(cwd, ["branch", "-m", "review/renamed"]);
30773113
yield* driver.switchRef({ cwd, refName: initialBranch });
30783114
}
3115+
yield* git(cwd, ["branch", "origin/remote-only"]);
3116+
commands.length = 0;
3117+
const shadowed = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });
3118+
assert.equal(shadowed.refName, "origin/remote-only");
3119+
assertCheckout(["checkout", "origin/remote-only", "--"], 2, 0);
3120+
30793121
const commit = yield* git(cwd, ["rev-parse", "HEAD"]);
3122+
commands.length = 0;
30803123
const detached = yield* driver.switchRef({ cwd, refName: commit });
30813124
assert.equal(detached.refName, null);
3125+
assertCheckout(["checkout", commit, "--"], 2, 0);
30823126
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), commit);
30833127
}),
30843128
);

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

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

3936-
const localTrackingBranch = remoteExists
3937-
? yield* executeGit(
3938-
"GitVcsDriver.switchRef.localTrackingBranch",
3939-
input.cwd,
3940-
["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
3941-
{
3942-
timeoutMs: 5_000,
3943-
allowNonZeroExit: true,
3944-
},
3945-
).pipe(
3946-
Effect.map((result) =>
3947-
result.exitCode === 0
3948-
? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
3949-
: null,
3950-
),
3951-
)
3952-
: null;
3936+
const localTrackingBranch =
3937+
remoteExists && !localInputExists
3938+
? yield* executeGit(
3939+
"GitVcsDriver.switchRef.localTrackingBranch",
3940+
input.cwd,
3941+
["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
3942+
{
3943+
timeoutMs: 5_000,
3944+
allowNonZeroExit: true,
3945+
},
3946+
).pipe(
3947+
Effect.map((result) =>
3948+
result.exitCode === 0
3949+
? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
3950+
: null,
3951+
),
3952+
)
3953+
: null;
39533954

39543955
const localTrackedBranchCandidate = deriveLocalBranchNameFromRemoteRef(input.refName);
39553956
const localTrackedBranchTargetExists =
3956-
remoteExists && localTrackedBranchCandidate
3957+
remoteExists && !localInputExists && !localTrackingBranch && localTrackedBranchCandidate
39573958
? yield* executeGit(
39583959
"GitVcsDriver.switchRef.localTrackedBranchTargetExists",
39593960
input.cwd,

0 commit comments

Comments
 (0)