Commit ce3bb39
authored
🤖 fix: delete local branch on workspace delete (#1292)
This changes workspace deletion for **local worktree** workspaces to
also delete the associated **local git branch**.
- Normal delete uses `git branch -d` (safe; only deletes if merged)
- Force delete uses `git branch -D`
- Adds guardrails to avoid deleting protected branches (e.g. `main`) or
branches still checked out elsewhere
UI copy is updated (desktop + mobile) so users understand that deleting
a workspace also deletes the branch.
---
<details>
<summary>📋 Implementation Plan</summary>
# Plan: Delete workspace also deletes its git branch
## Summary (what will change)
- **Archive** remains non-destructive (metadata timestamp only).
- **Delete workspace** will remove:
1) the git worktree directory *(existing)*
2) the associated **local git branch** by default *(new)*
Behavior details (per decisions captured):
- **Safe by default**: normal delete uses `git branch -d <branch>` (only
deletes if git considers it merged).
- **Force delete** uses `git branch -D <branch>` (matches existing
`force: true` semantics).
- **No remote deletion**: we will not delete `origin/<branch>`.
- **SSH runtimes**: keep simplest safe behavior (directory removal only;
no explicit remote branch deletion).
**Net LoC estimate (prod code): ~80–140 LoC** (mostly `WorktreeRuntime`
+ UI copy).
## Current behavior (confirmed in repo)
- Delete call flow:
- `WorkspaceContext.removeWorkspace()` → `api.workspace.remove()` →
`workspaceService.remove()` → `runtime.deleteWorkspace()`
- `WorktreeRuntime.deleteWorkspace()` already does **best-effort branch
deletion**, but **only** when the workspace name starts with `agent_`.
- `SSHRuntime.deleteWorkspace()` deletes the remote directory only (no
explicit `git branch -d/-D`).
## Recommended approach
### 1) Backend: generalize branch deletion in
`WorktreeRuntime.deleteWorkspace`
Files:
- `src/node/runtime/WorktreeRuntime.ts`
- (optional helper) `src/node/git.ts`
Steps:
1. **Change the branch-deletion gate**
- Today:
- `shouldDeleteBranch = !isInPlace &&
workspaceName.startsWith("agent_")`
- Proposed:
- `shouldDeleteBranch = !isInPlace` (delete branch for all worktree
workspaces, not just `agent_*`).
2. **Add safety checks before deleting the branch** (defensive, and errs
on “skip deletion” rather than “delete the wrong thing”):
- If branch does **not** exist locally (`await
listLocalBranches(projectPath)`): skip.
- If branch is the repo’s **detected trunk**
(`detectDefaultTrunkBranch(projectPath)`) or the **current branch** in
the main worktree (`getCurrentBranch(projectPath)`): skip.
- If branch is still **checked out by another worktree** (parse `git -C
<projectPath> worktree list --porcelain` and see if any remaining
worktree reports `branch refs/heads/<name>`): skip.
3. **Deletion command**
- Keep existing `deleteFlag` behavior:
- `force=false` → `git branch -d "<branch>"`
- `force=true` → `git branch -D "<branch>"`
4. **Keep deletion best-effort**
- Continue treating branch cleanup as **non-blocking** (never fail
workspace delete just because `git branch -d` failed).
- Upgrade logging so it’s clear whether we skipped due to guardrails vs
git failure.
### 2) UI: update delete UX copy to reflect branch deletion
Goal: make it obvious that “Delete” now also targets the branch.
Files / call sites to update:
- Desktop command palette: `src/browser/utils/commands/sources.ts`
(`confirm("Remove current workspace…")`)
- Archived view:
- `src/browser/components/ArchivedWorkspaces.tsx` tooltip/copy around
“Delete permanently”
- `src/browser/components/ForceDeleteModal.tsx` warning text (“Force
delete” should mention branch deletion too)
- Mobile: `mobile/src/screens/ProjectsScreen.tsx` delete confirmation +
force delete wording
Copy guidance:
- Mention **local branch** by name where feasible.
- Mention that normal delete is “safe” (may not delete branch if
unmerged) and that Force Delete is destructive.
### 3) Tests
1. **Unit tests** (`src/node/runtime/WorktreeRuntime.test.ts`)
- Add coverage for a non-`agent_` branch:
- Force delete (`force=true`) removes the branch.
- Add coverage for safe delete (`force=false`) on a merged branch:
- Create branch → commit → merge into main → delete workspace → branch
removed with `-d`.
- Add a guardrail test:
- Workspace on `main` can be deleted as a worktree, but **`main` branch
must not be deleted**.
2. **Integration test** (optional but valuable)
- Extend `tests/ipc/removeWorkspace.test.ts` for `local` runtime:
- Create workspace branch, then remove with `{ options: { force: true }
}`, assert `git branch --list <branch>` is empty in the main repo.
<details>
<summary>Alternatives considered (not recommended for this
iteration)</summary>
- **Add a “keep branch” checkbox / option to workspace.remove**
- Pros: avoids deleting user-provided existing branches.
- Cons: requires API/schema changes, UI affordances across desktop +
mobile, and new persistence semantics.
- **Delete only “Mux-created” branches** (e.g. track whether branch
existed at creation time)
- Pros: safest.
- Cons: needs new metadata field + backfill semantics for existing
workspaces.
Given the explicit desire for “delete means delete” now that archiving
exists, the simplest consistent behavior is: delete branch by default,
with strong guardrails and clear UI copy.
</details>
</details>
---
_Generated with [`mux`](https://github.com/coder/mux) • Model:
`openai:gpt-5.2` • Thinking: `xhigh`_
Signed-off-by: Thomas Kosiewski <tk@coder.com>1 parent 0c6d24a commit ce3bb39
File tree
7 files changed
+254
-20
lines changed- mobile/src/screens
- src
- browser
- components
- utils/commands
- node/runtime
- tests/ipc
7 files changed
+254
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
263 | | - | |
| 263 | + | |
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
| 283 | + | |
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
518 | 518 | | |
519 | 519 | | |
520 | 520 | | |
521 | | - | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
522 | 524 | | |
523 | 525 | | |
524 | 526 | | |
| |||
556 | 558 | | |
557 | 559 | | |
558 | 560 | | |
559 | | - | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
560 | 564 | | |
561 | 565 | | |
562 | 566 | | |
| |||
680 | 684 | | |
681 | 685 | | |
682 | 686 | | |
683 | | - | |
| 687 | + | |
684 | 688 | | |
685 | 689 | | |
686 | 690 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
157 | 163 | | |
158 | 164 | | |
159 | 165 | | |
| |||
305 | 311 | | |
306 | 312 | | |
307 | 313 | | |
308 | | - | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
309 | 318 | | |
310 | 319 | | |
311 | 320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
119 | 123 | | |
120 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
121 | 136 | | |
122 | 137 | | |
123 | 138 | | |
124 | 139 | | |
125 | 140 | | |
126 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
127 | 146 | | |
128 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
129 | 193 | | |
130 | 194 | | |
131 | 195 | | |
| |||
139 | 203 | | |
140 | 204 | | |
141 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
142 | 265 | | |
0 commit comments