Skip to content

Commit f15fbde

Browse files
fix(server): branch switch no longer kills a slow git checkout (pingdotgg#23539)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent cc6c0d2 commit f15fbde

2 files changed

Lines changed: 79 additions & 8 deletions

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,54 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
20222022
}),
20232023
);
20242024

2025+
it.effect("lets a slow remote branch checkout finish without blocking config writes", () =>
2026+
Effect.gen(function* () {
2027+
const cwd = yield* makeTmpDir();
2028+
yield* initRepoWithCommit(cwd);
2029+
const remote = yield* makeTmpDir("git-remote-");
2030+
yield* git(remote, ["init", "--bare"]);
2031+
yield* git(cwd, ["remote", "add", "origin", remote]);
2032+
yield* git(cwd, ["push", "origin", "HEAD:refs/heads/feature"]);
2033+
yield* git(cwd, ["fetch", "origin"]);
2034+
const checkoutStarted = yield* Deferred.make<void>();
2035+
const releaseCheckout = yield* Deferred.make<void>();
2036+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2037+
// Holds the real checkout open like a slow LFS smudge filter would.
2038+
const spawner = ChildProcessSpawner.make((command) =>
2039+
Effect.gen(function* () {
2040+
const handle = yield* delegate.spawn(command);
2041+
if (!ChildProcess.isStandardCommand(command) || command.args[0] !== "checkout") {
2042+
return handle;
2043+
}
2044+
yield* Deferred.succeed(checkoutStarted, undefined);
2045+
return ChildProcessSpawner.makeHandle({
2046+
...handle,
2047+
exitCode: Deferred.await(releaseCheckout).pipe(Effect.andThen(handle.exitCode)),
2048+
});
2049+
}),
2050+
);
2051+
const driver = yield* makeGitVcsDriverCore().pipe(
2052+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
2053+
Effect.provide(layerServerConfig),
2054+
);
2055+
2056+
const switching = yield* driver
2057+
.switchRef({ cwd, refName: "origin/feature" })
2058+
.pipe(Effect.forkChild({ startImmediately: true }));
2059+
yield* Deferred.await(checkoutStarted);
2060+
yield* TestClock.adjust("60 seconds");
2061+
yield* driver.createRef({ cwd, refName: "other" });
2062+
yield* Deferred.succeed(releaseCheckout, undefined);
2063+
2064+
const result = yield* Fiber.join(switching);
2065+
assert.equal(result.refName, "feature");
2066+
assert.equal(
2067+
yield* git(cwd, ["rev-parse", "--abbrev-ref", "feature@{upstream}"]),
2068+
"origin/feature",
2069+
);
2070+
}),
2071+
);
2072+
20252073
it.effect.each([
20262074
// Git cannot tell a missing revision from a missing file in these.
20272075
{ label: "an argument that may be a path", args: ["log", "-1", "missing"] },

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5036,28 +5036,51 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
50365036
).pipe(Effect.map((result) => result.exitCode === 0))
50375037
: false;
50385038

5039+
const newTrackingBranch =
5040+
!localInputExists && remoteExists && !localTrackingBranch && !localTrackedBranchTargetExists
5041+
? localTrackedBranchCandidate
5042+
: null;
5043+
50395044
const checkoutArgs = localInputExists
50405045
? ["checkout", input.refName]
50415046
: remoteExists && !localTrackingBranch && localTrackedBranchTargetExists
50425047
? ["checkout", input.refName]
5043-
: remoteExists && !localTrackingBranch
5044-
? ["checkout", "--track", input.refName]
5045-
: remoteExists && localTrackingBranch
5046-
? ["checkout", localTrackingBranch]
5047-
: ["checkout", input.refName];
5048+
: newTrackingBranch
5049+
? ["checkout", newTrackingBranch]
5050+
: remoteExists && !localTrackingBranch
5051+
? ["checkout", "--track", input.refName]
5052+
: remoteExists && localTrackingBranch
5053+
? ["checkout", localTrackingBranch]
5054+
: ["checkout", input.refName];
50485055

50495056
yield* refuseOptionLikeRefName("GitVcsDriver.switchRef", input.cwd, checkoutArgs.at(-1));
50505057

5058+
// Created before the checkout so the shared-config lock is not held while files are written.
5059+
// Not rolled back on failure: Git may already have switched HEAD to it (post-checkout hooks).
5060+
if (newTrackingBranch) {
5061+
yield* executeGit(
5062+
"GitVcsDriver.switchRef.createTrackingBranch",
5063+
input.cwd,
5064+
["branch", "--track", "--", newTrackingBranch, input.refName],
5065+
{
5066+
timeoutMs: 10_000,
5067+
fallbackErrorDetail: "git branch create failed",
5068+
writesSharedConfig: true,
5069+
},
5070+
);
5071+
}
5072+
50515073
// A stale ref must not turn into a path checkout that discards local edits.
50525074
yield* executeGitWithStableDiagnostics(
50535075
"GitVcsDriver.switchRef.checkout",
50545076
input.cwd,
50555077
[...checkoutArgs, "--"],
50565078
{
5057-
timeoutMs: 10_000,
5079+
// Killing a checkout midway (large or LFS repos) leaves a half-updated working tree.
5080+
timeoutMs: null,
50585081
fallbackErrorDetail: "git checkout failed",
5059-
// Checking out a remote branch creates a local one that tracks it.
5060-
writesSharedConfig: !localInputExists,
5082+
// Creating a branch writes its upstream; Git also guesses a remote branch for a missing name.
5083+
writesSharedConfig: checkoutArgs[1] === "--track" || (!localInputExists && !remoteExists),
50615084
},
50625085
);
50635086

0 commit comments

Comments
 (0)