Skip to content

Commit af0ff88

Browse files
fix(server): a failed worktree add no longer leaves its new branch behind
createWorktree creates the new branch before running git worktree add. When the add failed or was interrupted, the branch stayed, and a retry with the same name failed because the branch already existed. Delete the branch when the add fails, then surface the original error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4fdc853 commit af0ff88

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4106,19 +4106,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41064106
}
41074107
worktreePath = path.join(parentDir, repoName, directoryName);
41084108
}
4109-
if (input.newRefName) {
4109+
const args = ["worktree", "add", "--", worktreePath, input.newRefName ?? input.refName];
4110+
const progress = options?.progress;
4111+
const onCheckoutProgress = progress?.onCheckoutProgress;
4112+
4113+
const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";
4114+
const newRefName = input.newRefName;
4115+
if (newRefName) {
41104116
// Kept apart from the checkout so the shared-config lock only covers the tracking write.
41114117
yield* createBranchAt(
41124118
"GitVcsDriver.createWorktree.createBranch",
4113-
{ cwd: input.cwd, branch: input.newRefName, startPoint: input.refName },
4119+
{ cwd: input.cwd, branch: newRefName, startPoint: input.refName },
41144120
"git worktree add failed",
41154121
);
41164122
}
4117-
const args = ["worktree", "add", "--", worktreePath, input.newRefName ?? input.refName];
4118-
const progress = options?.progress;
4119-
const onCheckoutProgress = progress?.onCheckoutProgress;
4120-
4121-
const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";
41224123
yield* executeGitWithStableDiagnostics(
41234124
"GitVcsDriver.createWorktree",
41244125
input.cwd,
@@ -4142,6 +4143,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41424143
}
41434144
: {}),
41444145
},
4146+
).pipe(
4147+
Effect.onError(() =>
4148+
newRefName
4149+
? deleteLocalBranch({ cwd: input.cwd, refName: newRefName, force: true }).pipe(
4150+
Effect.catch((cause) =>
4151+
Effect.logWarning("could not remove the branch of a failed worktree", { cause }),
4152+
),
4153+
)
4154+
: Effect.void,
4155+
),
41454156
);
41464157

41474158
if (progress?.onWorktreeClaimed) {
@@ -4223,7 +4234,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
42234234
);
42244235
}
42254236

4226-
const newRefName = input.newRefName;
42274237
if (newRefName && input.baseRefName) {
42284238
const base = yield* resolveBranchBase(input.cwd, input.baseRefName);
42294239
if (base) {

0 commit comments

Comments
 (0)