Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(worktree): the removal reports what happened to the branch, truthfully

enes9fd3c9589e42
diff --git a/src/shared/worktree-ops.test.ts b/src/shared/worktree-ops.test.ts
index 48e12ef7d..f8d74f7a5 100644
--- a/src/shared/worktree-ops.test.ts
+++ b/src/shared/worktree-ops.test.ts
@@ -279,6 +279,47 @@ describe('worktreeRemove', () => {
expect(r.ok).toBe(true)
expect(calls.some((c) => c[0] === 'branch')).toBe(false)
})
+ // The confirm dialog promises the branch is deleted. `git branch -d` REFUSES an unmerged branch
+ // (deliberately — we never escalate to -D), and that refusal used to be swallowed: the removal
+ // reported plain "Worktree removed." and the user walked away believing a branch was gone while it
+ // was still sitting in `git branch`. The result must report what git actually did.
+ it('says the branch was deleted when git deletes it', async () => {
+ const list = 'worktree /repo\nbranch refs/heads/main\n\nworktree /wt/x\nbranch refs/heads/feature/x\n'
+ const { git } = fakeGit({ 'worktree list --porcelain': ok(list) })
+ const r = await worktreeRemove(git, '/repo', '/wt/x', '/home', true)
+ expect(r.ok).toBe(true)
+ expect(r.message).toContain('Branch feature/x deleted.')
+ })
+ it('says the branch was KEPT when `git branch -d` refuses it, and never claims it was deleted', async () => {
+ const list = 'worktree /repo\nbranch refs/heads/main\n\nworktree /wt/x\nbranch refs/heads/feature/x\n'
+ const { git, calls } = fakeGit({
+ 'worktree list --porcelain': ok(list),
+ 'branch -d feature/x': ko('error: the branch is not fully merged')
+ })
+ const r = await worktreeRemove(git, '/repo', '/wt/x', '/home', true)
+ // The worktree itself DID go — the failure is the branch's alone, and must not fail the removal.
+ expect(r.ok).toBe(true)
+ expect(r.message).toContain('kept')
+ expect(r.message).not.toContain('Branch feature/x deleted.')
+ // Never escalated to -D: an unmerged branch is unpublished work.
+ expect(calls.some((c) => c.join(' ') === 'branch -D feature/x')).toBe(false)
+ })
+ it('reports the branch outcome on the already-gone path too', async () => {
+ const list =
+ 'worktree /repo\nbranch refs/heads/main\n\nworktree /wt/x\nbranch refs/heads/feature/x\nprunable gitdir file points to non-existent location\n'
+ const { git } = fakeGit({
+ 'worktree list --porcelain': ok(list),
+ 'branch -d feature/x': ko('error: the branch is not fully merged')
+ })
+ const r = await worktreeRemove(git, '/repo', '/wt/x', '/home', true)
+ expect(r.message).toContain('kept')
+ })
+ it('says nothing about a branch it was never asked to delete', async () => {
+ const list = 'worktree /repo\nbranch refs/heads/main\n\nworktree /wt/x\nbranch refs/heads/feature/x\n'
+ const { git } = fakeGit({ 'worktree list --porcelain': ok(list) })
+ const r = await worktreeRemove(git, '/repo', '/wt/x', '/home', false)
+ expect(r.message).toBe('Worktree removed.')
+ })
it('prunes (and never removes) a worktree whose directory is already gone', async () => {
const list =
'worktree /repo\nbranch refs/heads/main\n\nworktree /wt/x\nbranch refs/heads/feature/x\nprunable gitdir file points to non-existent location\n'
diff --git a/src/shared/worktree-ops.ts b/src/shared/worktree-ops.ts
index 3ed8ec995..265ef7f16 100644
--- a/src/shared/worktree-ops.ts
+++ b/src/shared/worktree-ops.ts
@@ -23,6 +23,35 @@ export interface WorktreeOpResult {
worktreeGone?: boolean
}
+/**
+ * Delete the worktree's branch — and report what git ACTUALLY did.
+ *
+ * `git branch -d` refuses a branch that is not fully merged, and that refusal is the safety: an
+ * unmerged branch is unpublished work, and `-D` would throw it away. We keep the refusal (we never
+ * escalate to `-D`) but we no longer SWALLOW it. The confirm dialog tells the user the branch is
+ * deleted; when git declines, the result has to say so, or the user walks away believing a branch
+ * is gone while it is still sitting in `git branch` — the one thing worse than not deleting it.
+ *
+ * The refusal is hedged in the wording ("git would not delete it") rather than asserted as
+ * "unmerged": -d also refuses for other reasons (the branch is checked out in another worktree), and
+ * claiming a specific cause we did not verify is the same sin in the other direction.
+ *
+ * Returns a sentence to append to the caller's message, or '' when no branch deletion was asked for.
+ */
+async function deleteWorktreeBranch(
+ git: GitExecutor,
+ repoPath: string,
+ /** `null` = the worktree has a detached HEAD, so there is no branch to delete. */
+ branch: string | null | undefined,
+ deleteBranch: boolean
+): Promise<string> {
+ if (!deleteBranch || !branch || !isValidGitRef(branch)) return ''
+ const r = await git(repoPath, ['branch', '-d', branch])
+ return r.ok
+ ? ` Branch ${branch} deleted.`
+ : ` Branch ${branch} was kept — git would not delete it (it may have unmerged commits).`
+}
+
export async function repoRoot(git: GitExecutor, cwd: string): Promise<string | null> {
if (!cwd) return null
const r = await git(cwd, ['rev-parse', '--show-toplevel'])
@@ -232,13 +261,11 @@ export async function worktreeRemove(
// The directory is gone. `worktree remove` would fail; prune the registration instead and
// touch no files.
await git(repoPath, ['worktree', 'prune'])
- if (!pruneOnly && deleteBranch && branch && isValidGitRef(branch)) {
- await git(repoPath, ['branch', '-d', branch])
- }
+ const branchNote = await deleteWorktreeBranch(git, repoPath, branch, !pruneOnly && deleteBranch)
return {
ok: true,
worktreeGone: true,
- message: 'The worktree directory was already gone; its registration was pruned.'
+ message: `The worktree directory was already gone; its registration was pruned.${branchNote}`
}
}
if (pruneOnly) {
@@ -248,9 +275,6 @@ export async function worktreeRemove(
const rm = await git(repoPath, ['worktree', 'remove', '--force', '--', wtPath])
if (!rm.ok) return { ok: false, message: rm.err }
await git(repoPath, ['worktree', 'prune'])
- if (deleteBranch && branch && isValidGitRef(branch)) {
- // -d refuses unmerged; the renderer decides whether to escalate to -D.
- await git(repoPath, ['branch', '-d', branch])
- }
- return { ok: true, message: 'Worktree removed.' }
+ const branchNote = await deleteWorktreeBranch(git, repoPath, branch, deleteBranch)
+ return { ok: true, message: `Worktree removed.${branchNote}` }
}