Skip to content

Commit 1917c8a

Browse files
MarcoDotIOclaude
andauthored
fix(server): worktree threads get their generated branch name when a branch named t3 exists (#27815)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 6843eff commit 1917c8a

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,6 +3695,47 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
36953695
}),
36963696
);
36973697

3698+
it.effect("flattens a generated name when a branch at its parent path blocks it", () =>
3699+
Effect.gen(function* () {
3700+
const cwd = yield* makeTmpDir();
3701+
yield* initRepoWithCommit(cwd);
3702+
const driver = yield* GitVcsDriver.GitVcsDriver;
3703+
// A local `t3` branch is why launches start on the flat `t3-<hex>` temporary branch.
3704+
yield* git(cwd, ["branch", "t3"]);
3705+
yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
3706+
yield* git(cwd, ["branch", "t3-fix-login"]);
3707+
3708+
const renamed = yield* driver.renameBranch({
3709+
cwd,
3710+
oldBranch: "t3-abcd1234",
3711+
newBranch: "t3/fix-login",
3712+
});
3713+
3714+
assert.equal(renamed.branch, "t3-fix-login-1");
3715+
assert.equal(yield* git(cwd, ["branch", "--show-current"]), "t3-fix-login-1");
3716+
}),
3717+
);
3718+
3719+
it.effect("skips a flattened name that a nested branch blocks", () =>
3720+
Effect.gen(function* () {
3721+
const cwd = yield* makeTmpDir();
3722+
yield* initRepoWithCommit(cwd);
3723+
const driver = yield* GitVcsDriver.GitVcsDriver;
3724+
yield* git(cwd, ["branch", "t3"]);
3725+
yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
3726+
yield* git(cwd, ["branch", "t3-fix-login/child"]);
3727+
3728+
const renamed = yield* driver.renameBranch({
3729+
cwd,
3730+
oldBranch: "t3-abcd1234",
3731+
newBranch: "t3/fix-login",
3732+
});
3733+
3734+
assert.equal(renamed.branch, "t3-fix-login-1");
3735+
assert.equal(yield* git(cwd, ["branch", "--show-current"]), "t3-fix-login-1");
3736+
}),
3737+
);
3738+
36983739
it.effect("rejects a missing branch without restoring a matching dirty file", () =>
36993740
Effect.gen(function* () {
37003741
const cwd = yield* makeTmpDir();

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,16 +1247,37 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
12471247

12481248
const resolveAvailableBranchName = Effect.fn("resolveAvailableBranchName")(function* (
12491249
cwd: string,
1250-
desiredBranch: string,
1250+
requestedBranch: string,
12511251
) {
1252-
const isDesiredTaken = yield* branchExists(cwd, desiredBranch);
1252+
// A branch at a parent path (such as `t3`) blocks every ref nested under it, so no suffix
1253+
// would help; flatten to a sibling the way temporary worktree branches already do.
1254+
const segments = requestedBranch.split("/");
1255+
let blockedByParent = false;
1256+
for (let index = 1; index < segments.length && !blockedByParent; index += 1) {
1257+
blockedByParent = yield* branchExists(cwd, segments.slice(0, index).join("/"));
1258+
}
1259+
const desiredBranch = blockedByParent ? segments.join("-") : requestedBranch;
1260+
// A branch nested under the name (`t3-fix/child`) blocks it as surely as one with the name.
1261+
const isBranchNameTaken = (name: string) =>
1262+
Effect.zipWith(
1263+
branchExists(cwd, name),
1264+
runGitStdout(
1265+
"GitVcsDriver.branchExists",
1266+
cwd,
1267+
["for-each-ref", "--count=1", "--format=%(refname)", `refs/heads/${name}/`],
1268+
true,
1269+
),
1270+
(exists, nested) => exists || nested.trim().length > 0,
1271+
);
1272+
1273+
const isDesiredTaken = yield* isBranchNameTaken(desiredBranch);
12531274
if (!isDesiredTaken) {
12541275
return desiredBranch;
12551276
}
12561277

12571278
for (let suffix = 1; suffix <= 100; suffix += 1) {
12581279
const candidate = `${desiredBranch}-${suffix}`;
1259-
const isCandidateTaken = yield* branchExists(cwd, candidate);
1280+
const isCandidateTaken = yield* isBranchNameTaken(candidate);
12601281
if (!isCandidateTaken) {
12611282
return candidate;
12621283
}

0 commit comments

Comments
 (0)