TypeScript
perf(server): skip unused probes when switching Git refs (#20140)
IfkumRfnld6888999dba6
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex 6646b2e8aa5..0cb7beb361b 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -3062,23 +3062,67 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {const cwd = yield* makeTmpDir();const remote = yield* makeTmpDir("git-remote-");const { initialBranch } = yield* initRepoWithCommit(cwd);- const driver = yield* GitVcsDriver.GitVcsDriver;+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;+ const commands: Array<ReadonlyArray<string>> = [];+ const spawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ if (ChildProcess.isStandardCommand(command)) commands.push(command.args);+ return yield* delegate.spawn(command);+ }),+ );+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ Effect.provide(layerServerConfig),+ );+ const assertCheckout = (+ checkoutArgs: ReadonlyArray<string>,+ existenceReads: number,+ trackingReads: number,+ ) => {+ assert.deepEqual(+ commands.filter((args) => args[0] === "checkout"),+ [checkoutArgs],+ );+ assert.equal(commands.filter((args) => args[0] === "show-ref").length, existenceReads);+ assert.equal(commands.filter((args) => args[0] === "for-each-ref").length, trackingReads);+ assert.deepEqual(+ commands.filter((args) => args[0] === "branch"),+ [["branch", "--show-current"]],+ );+ };yield* git(remote, ["init", "--bare"]);yield* git(cwd, ["remote", "add", "origin", remote]);yield* git(cwd, ["push", "origin", "HEAD:refs/heads/remote-only"]);for (let attempt = 0; attempt < 2; attempt += 1) {+ commands.length = 0;const result = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });- assert.equal(result.refName, "remote-only");+ assert.equal(result.refName, attempt === 0 ? "remote-only" : "review/renamed");+ assertCheckout(+ attempt === 0+ ? ["checkout", "--track", "origin/remote-only", "--"]+ : ["checkout", "review/renamed", "--"],+ attempt === 0 ? 3 : 2,+ 1,+ );assert.equal(yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),"origin/remote-only",);+ if (attempt === 0) yield* git(cwd, ["branch", "-m", "review/renamed"]);yield* driver.switchRef({ cwd, refName: initialBranch });}+ yield* git(cwd, ["branch", "origin/remote-only"]);+ commands.length = 0;+ const shadowed = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });+ assert.equal(shadowed.refName, "origin/remote-only");+ assertCheckout(["checkout", "origin/remote-only", "--"], 2, 0);+const commit = yield* git(cwd, ["rev-parse", "HEAD"]);+ commands.length = 0;const detached = yield* driver.switchRef({ cwd, refName: commit });assert.equal(detached.refName, null);+ assertCheckout(["checkout", commit, "--"], 2, 0);assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), commit);}),);diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 77b8306c1e4..ffab840f815 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -3933,27 +3933,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*{ concurrency: "unbounded" },);- const localTrackingBranch = remoteExists- ? yield* executeGit(- "GitVcsDriver.switchRef.localTrackingBranch",- input.cwd,- ["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],- {- timeoutMs: 5_000,- allowNonZeroExit: true,- },- ).pipe(- Effect.map((result) =>- result.exitCode === 0- ? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)- : null,- ),- )- : null;+ const localTrackingBranch =+ remoteExists && !localInputExists+ ? yield* executeGit(+ "GitVcsDriver.switchRef.localTrackingBranch",+ input.cwd,+ ["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],+ {+ timeoutMs: 5_000,+ allowNonZeroExit: true,+ },+ ).pipe(+ Effect.map((result) =>+ result.exitCode === 0+ ? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)+ : null,+ ),+ )+ : null;const localTrackedBranchCandidate = deriveLocalBranchNameFromRemoteRef(input.refName);const localTrackedBranchTargetExists =- remoteExists && localTrackedBranchCandidate+ remoteExists && !localInputExists && !localTrackingBranch && localTrackedBranchCandidate? yield* executeGit("GitVcsDriver.switchRef.localTrackedBranchTargetExists",input.cwd,