Skip to content

Commit 3d67cc1

Browse files
tcraptsclaude
andcommitted
fix(server): retry the branch name when a concurrent launch takes it
Two launches that ask for the same taken branch name can both pick the same free suffix. The one that loses `git branch` now picks the next free name and tries again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6f53f84 commit 3d67cc1

2 files changed

Lines changed: 55 additions & 19 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
2+
import * as NodeChildProcess from "node:child_process";
23
import * as NodeFS from "node:fs";
34
import * as NodeOS from "node:os";
45
import * as NodeURL from "node:url";
@@ -4026,6 +4027,33 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
40264027
yield* git(cwd, ["config", "branch.feature/taken-1.t3-merge-base"]),
40274028
initialBranch,
40284029
);
4030+
4031+
// Another launch takes the free name between the check and `git branch`.
4032+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
4033+
const spawner = ChildProcessSpawner.make((command) => {
4034+
if (
4035+
ChildProcess.isStandardCommand(command) &&
4036+
command.args.includes("--no-track") &&
4037+
command.args.includes("feature/taken-2")
4038+
) {
4039+
NodeChildProcess.execFileSync("git", ["-C", cwd, "branch", "feature/taken-2"]);
4040+
}
4041+
return delegate.spawn(command);
4042+
});
4043+
const racingDriver = yield* makeGitVcsDriverCore().pipe(
4044+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
4045+
Effect.provide(layerServerConfig),
4046+
);
4047+
const raced = yield* racingDriver.createWorktree(
4048+
{
4049+
cwd,
4050+
path: path.join(yield* makeTmpDir("git-worktrees-"), "raced"),
4051+
refName: initialBranch,
4052+
newRefName: "feature/taken",
4053+
},
4054+
{ renameBranchIfTaken: true },
4055+
);
4056+
assert.equal(raced.worktree.refName, "feature/taken-3");
40294057
}),
40304058
);
40314059
it.effect.each(["commit", "short-commit", "tag", "revision"] as const)(

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,29 +4120,45 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41204120
)(function* (input, options) {
41214121
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.refName);
41224122
yield* refuseOptionLikeRefName("GitVcsDriver.createWorktree", input.cwd, input.newRefName);
4123-
const newRefName =
4124-
input.newRefName && options?.renameBranchIfTaken
4125-
? yield* resolveAvailableBranchName(input.cwd, input.newRefName)
4126-
: input.newRefName;
4127-
const targetBranch = newRefName ?? input.refName;
4128-
// Resolve sub/.. before taking the basename.
4129-
const repoName = path.basename(path.resolve(input.cwd));
4130-
let worktreePath = input.path;
4131-
if (worktreePath == null) {
4132-
const parentDir = resolveWorktreesDirectory(
4123+
let parentDir = "";
4124+
if (input.path == null) {
4125+
const resolvedParentDir = resolveWorktreesDirectory(
41334126
options?.worktreesDirectory ?? "",
41344127
worktreesDir,
41354128
path,
41364129
yield* HostProcess.HomeDirectory,
41374130
);
4138-
if (parentDir === null) {
4131+
if (resolvedParentDir === null) {
41394132
return yield* new GitCommandError({
41404133
operation: "GitVcsDriver.createWorktree",
41414134
command: "git worktree add",
41424135
cwd: input.cwd,
41434136
detail: `The worktree location "${options?.worktreesDirectory}" must be an absolute folder on this machine, not a drive root. Change it in Settings → Worktrees.`,
41444137
});
41454138
}
4139+
parentDir = resolvedParentDir;
4140+
}
4141+
const createBranchNamed = (branch: string) =>
4142+
// Kept apart from the checkout so the shared-config lock only covers the tracking write.
4143+
createBranchAt(
4144+
"GitVcsDriver.createWorktree.createBranch",
4145+
{ cwd: input.cwd, branch, startPoint: input.refName },
4146+
"git worktree add failed",
4147+
).pipe(Effect.as(branch));
4148+
const newRefName = !input.newRefName
4149+
? undefined
4150+
: options?.renameBranchIfTaken
4151+
? yield* resolveAvailableBranchName(input.cwd, input.newRefName).pipe(
4152+
Effect.flatMap(createBranchNamed),
4153+
// A concurrent launch can take the free name between the check and `git branch`.
4154+
Effect.retry({ while: (error) => error.reason === "branch_already_exists", times: 5 }),
4155+
)
4156+
: yield* createBranchNamed(input.newRefName);
4157+
const targetBranch = newRefName ?? input.refName;
4158+
let worktreePath = input.path;
4159+
if (worktreePath == null) {
4160+
// Resolve sub/.. before taking the basename.
4161+
const repoName = path.basename(path.resolve(input.cwd));
41464162
let directoryName = targetBranch.replace(/\//g, "-");
41474163
// Leave room for files beneath the worktree on Windows, regardless of branch length.
41484164
if (directoryName.length > 40) {
@@ -4155,14 +4171,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41554171
}
41564172
worktreePath = path.join(parentDir, repoName, directoryName);
41574173
}
4158-
if (newRefName) {
4159-
// Kept apart from the checkout so the shared-config lock only covers the tracking write.
4160-
yield* createBranchAt(
4161-
"GitVcsDriver.createWorktree.createBranch",
4162-
{ cwd: input.cwd, branch: newRefName, startPoint: input.refName },
4163-
"git worktree add failed",
4164-
);
4165-
}
41664174
const args = ["worktree", "add", "--", worktreePath, targetBranch];
41674175
const progress = options?.progress;
41684176
const onCheckoutProgress = progress?.onCheckoutProgress;

0 commit comments

Comments
 (0)