Skip to content
TypeScript

fix(server): worktree threads get their generated branch name when a branch named t3 exists (#27815)

MarcoDotIO1917c8a33a74
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index 404842ada91..d9a48457f8a 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -3695,6 +3695,47 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);
+ it.effect("flattens a generated name when a branch at its parent path blocks it", () =>
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ yield* initRepoWithCommit(cwd);
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ // A local `t3` branch is why launches start on the flat `t3-<hex>` temporary branch.
+ yield* git(cwd, ["branch", "t3"]);
+ yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
+ yield* git(cwd, ["branch", "t3-fix-login"]);
+
+ const renamed = yield* driver.renameBranch({
+ cwd,
+ oldBranch: "t3-abcd1234",
+ newBranch: "t3/fix-login",
+ });
+
+ assert.equal(renamed.branch, "t3-fix-login-1");
+ assert.equal(yield* git(cwd, ["branch", "--show-current"]), "t3-fix-login-1");
+ }),
+ );
+
+ it.effect("skips a flattened name that a nested branch blocks", () =>
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ yield* initRepoWithCommit(cwd);
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ yield* git(cwd, ["branch", "t3"]);
+ yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
+ yield* git(cwd, ["branch", "t3-fix-login/child"]);
+
+ const renamed = yield* driver.renameBranch({
+ cwd,
+ oldBranch: "t3-abcd1234",
+ newBranch: "t3/fix-login",
+ });
+
+ assert.equal(renamed.branch, "t3-fix-login-1");
+ assert.equal(yield* git(cwd, ["branch", "--show-current"]), "t3-fix-login-1");
+ }),
+ );
+
it.effect("rejects a missing branch without restoring a matching dirty file", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index bea65a0f42a..11580f4c26e 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -1247,16 +1247,37 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const resolveAvailableBranchName = Effect.fn("resolveAvailableBranchName")(function* (
cwd: string,
- desiredBranch: string,
+ requestedBranch: string,
) {
- const isDesiredTaken = yield* branchExists(cwd, desiredBranch);
+ // A branch at a parent path (such as `t3`) blocks every ref nested under it, so no suffix
+ // would help; flatten to a sibling the way temporary worktree branches already do.
+ const segments = requestedBranch.split("/");
+ let blockedByParent = false;
+ for (let index = 1; index < segments.length && !blockedByParent; index += 1) {
+ blockedByParent = yield* branchExists(cwd, segments.slice(0, index).join("/"));
+ }
+ const desiredBranch = blockedByParent ? segments.join("-") : requestedBranch;
+ // A branch nested under the name (`t3-fix/child`) blocks it as surely as one with the name.
+ const isBranchNameTaken = (name: string) =>
+ Effect.zipWith(
+ branchExists(cwd, name),
+ runGitStdout(
+ "GitVcsDriver.branchExists",
+ cwd,
+ ["for-each-ref", "--count=1", "--format=%(refname)", `refs/heads/${name}/`],
+ true,
+ ),
+ (exists, nested) => exists || nested.trim().length > 0,
+ );
+
+ const isDesiredTaken = yield* isBranchNameTaken(desiredBranch);
if (!isDesiredTaken) {
return desiredBranch;
}
for (let suffix = 1; suffix <= 100; suffix += 1) {
const candidate = `${desiredBranch}-${suffix}`;
- const isCandidateTaken = yield* branchExists(cwd, candidate);
+ const isCandidateTaken = yield* isBranchNameTaken(candidate);
if (!isCandidateTaken) {
return candidate;
}