fix(server): cancelling worktree setup during branch creation removes the branch #47619
OpenStiensWout(GitHub profile, opens in a new tab)wants to merge 6 commits into
main fromrefs/pull/47619/headopened View on GitHubtanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -6763,0 +6794,21 @@6794+ ],6795+ { concurrency: "unbounded" },6796+ );6797+ const ownedRef = `refs/heads/${newRefName}`;6798+ // Hooks that hold up a cancel could hold up the rollback too, so it runs without them.6799+ const releaseBranch = Effect.gen(function* () {6800+ if (existed || startCommit === null) return;6801+ // A non-empty worktree path keeps Git's refusal to delete a checked-out branch.6802+ const current = yield* runGitStdout(6803+ "GitVcsDriver.createWorktree.inspectCancelledBranch",6804+ input.cwd,6805+ ["for-each-ref", "--format=%(objectname) %(worktreepath)", ownedRef],6806+ );6807+ if (trimAsciiWhitespace(current) !== startCommit) return;6808+ // The old value makes the delete fail if another writer moved the ref meanwhile.6809+ yield* runGit("GitVcsDriver.createWorktree.releaseCancelledBranch", input.cwd, [6810+ "update-ref",6811+ "-d",6812+ ownedRef,6813+ startCommit,6814+ ]);@@ -6763,0 +6799,21 @@6799+ const releaseBranch = Effect.gen(function* () {6800+ if (existed || startCommit === null) return;6801+ // A non-empty worktree path keeps Git's refusal to delete a checked-out branch.6802+ const current = yield* runGitStdout(6803+ "GitVcsDriver.createWorktree.inspectCancelledBranch",6804+ input.cwd,6805+ ["for-each-ref", "--format=%(objectname) %(worktreepath)", ownedRef],6806+ );6807+ if (trimAsciiWhitespace(current) !== startCommit) return;6808+ // The old value makes the delete fail if another writer moved the ref meanwhile.6809+ yield* runGit("GitVcsDriver.createWorktree.releaseCancelledBranch", input.cwd, [6810+ "update-ref",6811+ "-d",6812+ ownedRef,6813+ startCommit,6814+ ]);6815+ yield* executeGit(6816+ "GitVcsDriver.createWorktree.releaseCancelledBranchConfig",6817+ input.cwd,6818+ ["config", "--remove-section", `branch.${newRefName}`],6819+ { allowNonZeroExit: true, writesSharedConfig: true },@@ -6763,0 +6810,21 @@6810+ "update-ref",6811+ "-d",6812+ ownedRef,6813+ startCommit,6814+ ]);6815+ yield* executeGit(6816+ "GitVcsDriver.createWorktree.releaseCancelledBranchConfig",6817+ input.cwd,6818+ ["config", "--remove-section", `branch.${newRefName}`],6819+ { allowNonZeroExit: true, writesSharedConfig: true },6820+ );6821+ }).pipe(6822+ Effect.provideService(GitVcsDriver.RepositoryHooks, { _tag: "none" }),6823+ Effect.catch((cause) =>6824+ Effect.logWarning("could not clean up a cancelled worktree branch", { cause }),6825+ ),6826+ );6827+ // Kept apart from the checkout so the shared-config lock only covers the tracking write.6828+ return yield* createBranchAt(6829+ "GitVcsDriver.createWorktree.createBranch",6830+ { cwd: input.cwd, branch: newRefName, startPoint: input.refName },
- @@ -6763,0 +6789,21 @@6789+ ).pipe(6790+ Effect.map((result) =>6791+ result.exitCode === 0 ? trimAsciiWhitespace(result.stdout) : null,6792+ ),6793+ ),6794+ ],6795+ { concurrency: "unbounded" },6796+ );6797+ const ownedRef = `refs/heads/${newRefName}`;6798+ // Hooks that hold up a cancel could hold up the rollback too, so it runs without them.6799+ const releaseBranch = Effect.gen(function* () {6800+ if (existed || startCommit === null) return;6801+ // A non-empty worktree path keeps Git's refusal to delete a checked-out branch.6802+ const current = yield* runGitStdout(6803+ "GitVcsDriver.createWorktree.inspectCancelledBranch",6804+ input.cwd,6805+ ["for-each-ref", "--format=%(objectname) %(worktreepath)", ownedRef],6806+ );6807+ if (trimAsciiWhitespace(current) !== startCommit) return;6808+ // The old value makes the delete fail if another writer moved the ref meanwhile.6809+ yield* runGit("GitVcsDriver.createWorktree.releaseCancelledBranch", input.cwd, [
juliusmarminge added labelstakeover:in-progress
Pushedf971fd0
juliusmarminge added labelstakeover:needs-julius
juliusmarminge removed labelstakeover:in-progress
Sign in to join this conversation.












No actionable comments were generated in the recent review.
Walkthrough
Worktree creation now reads checkout settings and cache generation in its branch-creation flow. If setup is interrupted, it removes a newly created branch only when the branch still points to its recorded starting commit. Unit and end-to-end tests cover cancellation and concurrent branch updates.
Changes: Worktree cancellation
apps/server/src/vcs/GitVcsDriverCore.ts,apps/server/src/vcs/GitVcsDriverCore.test.tscreateWorktreerecords branch state before creation and reads checkout settings and cache generation in the branch-creation flow. On interruption, it removes the branch and its config only if the branch is new and unchanged. Cleanup disables repository hooks. Unit tests cover branch deletion, concurrent updates, worktree removal, and hook behavior.scripts/scenarios/cancelled-worktree-releases-branch-name.e2e.tsshowcaseifdevReplayrejects thedefaultmodel as unadvertised.Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Sequence Diagram(s)#
Suggested reviewers:
iamshadmantaqi,smireaMerge Risk:
Low · up to
f971fCancelling worktree setup now removes the branch it just created, so a retry with the same name works. In rare races with another Git writer, cleanup may leave the branch behind, delete a branch another worktree has just checked out, or drop upstream settings from a recreated branch. These cases are narrow and low-impact, so the change is mergeable with owner awareness.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: cancelling worktree setup during branch creation now removes the newly created branch. It is specific, uses the conventional commit form, and matches the GitVcsDriverCore.ts changes and the new test. The 80-character length is acceptable.
Full details: Description check
Explanation
The description covers the problem, the change, reproduction steps, verification with commit references and recordings, and attribution. It follows the template sections: Problem, Change, Scope, Verification. The Scope section links related issues and the reproduction, but it does not clearly state the linked bug issue or explain why no issue is needed. The verification also includes a broad repo check, and the proof run is on a VM. These gaps are minor. The description also includes a Not in this change section that is not in the template, which is acceptable.
Finishing Touches
Generate unit tests (beta)
8aeb1459-9fab-47fa-858e-826079c07ca6Reviewing files that changed from the base of the PR and between 60e9fa10192475e279561126fd234d9ac00fb70a and f971fd0b4f57ded47f34e1f18a7cb5d36e006e86.
apps/server/src/vcs/GitVcsDriverCore.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.