Skip to content

Commit 98f334f

Browse files
Gourav224claude
andcommitted
fix(server): switching branches no longer kills a slow checkout and leaves the tree half-switched
switchRef ran git checkout with a fixed 10 second timeout. In a large repo the checkout can take longer, and killing it after it has written part of the target branch's files but before it updates the index and HEAD leaves the old branch checked out over a mixed working tree that git cannot roll back. Run the checkout without a timeout, as push and pull already do, so it either completes or fails on its own. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 67c0fc0 commit 98f334f

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,40 @@ it.effect.each([{ timeoutMs: null }, { timeoutMs: 30_001 }])(
264264
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
265265
);
266266

267+
it.effect("lets a slow branch checkout finish instead of killing it partway", () =>
268+
Effect.gen(function* () {
269+
const checkoutGate = yield* Deferred.make<void>();
270+
const checkoutStarted = yield* Deferred.make<void>();
271+
const spawner = ChildProcessSpawner.make((command) =>
272+
Effect.gen(function* () {
273+
if (!ChildProcess.isStandardCommand(command) || command.args[0] !== "checkout") {
274+
return makeSuccessfulHandle("feature");
275+
}
276+
yield* Deferred.succeed(checkoutStarted, undefined);
277+
return ChildProcessSpawner.makeHandle({
278+
...makeSuccessfulHandle(""),
279+
exitCode: Deferred.await(checkoutGate).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
280+
});
281+
}),
282+
);
283+
const driver = yield* makeGitVcsDriverCore().pipe(
284+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
285+
);
286+
287+
const switching = yield* driver
288+
.switchRef({ cwd: "/repo", refName: "feature" })
289+
.pipe(Effect.forkChild);
290+
yield* Deferred.await(checkoutStarted);
291+
// A checkout rewriting thousands of files can outlast any fixed limit, and git
292+
// cannot roll back one that is killed after it has written part of the tree.
293+
yield* TestClock.adjust("1 hour");
294+
assert.isUndefined(switching.pollUnsafe(), "the checkout is still running");
295+
296+
yield* Deferred.succeed(checkoutGate, undefined);
297+
assert.deepEqual(yield* Fiber.join(switching), { refName: "feature" });
298+
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
299+
);
300+
267301
it.effect.each(["root", "nested", "worktree"] as const)(
268302
"skips clean filters while the %s index is locked and resumes after unlock",
269303
(location) =>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4357,12 +4357,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
43574357
yield* refuseOptionLikeRefName("GitVcsDriver.switchRef", input.cwd, checkoutArgs.at(-1));
43584358

43594359
// A stale ref must not turn into a path checkout that discards local edits.
4360+
// No timeout: a checkout of thousands of files can outlast any fixed limit, and git
4361+
// cannot roll back one that is killed after writing part of the tree but before
4362+
// updating the index and HEAD, which leaves the old branch checked out over the new
4363+
// branch's files.
43604364
yield* executeGitWithStableDiagnostics(
43614365
"GitVcsDriver.switchRef.checkout",
43624366
input.cwd,
43634367
[...checkoutArgs, "--"],
43644368
{
4365-
timeoutMs: 10_000,
4369+
timeoutMs: null,
43664370
fallbackErrorDetail: "git checkout failed",
43674371
},
43684372
);

0 commit comments

Comments
 (0)