|
| 1 | +import * as vscode from 'vscode'; |
| 2 | + |
| 3 | +import { AnalyticsEvent, capture, captureException } from '../../analytics/analytics'; |
| 4 | +import { GitExecutor } from '../../common/git/gitExecutor'; |
| 5 | +import { VscodeGitProvider } from '../../common/git/vscodeGitProvider'; |
| 6 | +import { LoggingService } from '../../logging/loggingService'; |
| 7 | +import { |
| 8 | + collectPrunableWorktrees, |
| 9 | + formatPruneConfirmationDetail, |
| 10 | + PrunableWorktree, |
| 11 | +} from '../../services/worktreePruneService'; |
| 12 | +import { refreshRemoveMultipleWorktreesVisibility } from '../utils/worktreeCommandVisibility'; |
| 13 | +import { |
| 14 | + getWorktreeLabel, |
| 15 | + getWorktreeStashName, |
| 16 | + removeWorkspaceFoldersForPath, |
| 17 | +} from '../utils/worktreeRemoval'; |
| 18 | +import { BaseCommand } from '../command'; |
| 19 | + |
| 20 | +const ACTION_PRUNE = 'Prune Worktrees'; |
| 21 | +const ACTION_STASH_AND_PRUNE = 'Stash Changes and Prune'; |
| 22 | +const ACTION_RESET_AND_PRUNE = 'Reset Changes and Prune'; |
| 23 | +const ACTION_CANCEL = 'Cancel'; |
| 24 | +const ACTION_FORCE_DELETE_BRANCHES = 'Force Delete Branches'; |
| 25 | +const ACTION_KEEP_BRANCHES = 'Keep Branches'; |
| 26 | + |
| 27 | +type DirtyAction = 'clean' | 'stash' | 'reset'; |
| 28 | +type FailedPrune = { candidate: PrunableWorktree; error: string }; |
| 29 | + |
| 30 | +/** `git branch -d` refuses a branch that still holds commits the upstream never got. */ |
| 31 | +function isNotFullyMergedError(error: unknown): boolean { |
| 32 | + const stderr = typeof (error as { stderr?: unknown })?.stderr === 'string' |
| 33 | + ? (error as { stderr: string }).stderr |
| 34 | + : ''; |
| 35 | + const message = error instanceof Error ? error.message : String(error); |
| 36 | + return /not fully merged/i.test(`${message}\n${stderr}`); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Removes every worktree whose branch's upstream is gone (the remote branch was |
| 41 | + * deleted, typically once its PR merged) and deletes the now-dead local branch |
| 42 | + * along with it. |
| 43 | + */ |
| 44 | +export class PruneWorktreesCommand extends BaseCommand { |
| 45 | + constructor( |
| 46 | + logService: LoggingService, |
| 47 | + private vscodeGitProvider?: VscodeGitProvider |
| 48 | + ) { |
| 49 | + super(logService); |
| 50 | + } |
| 51 | + |
| 52 | + async execute(): Promise<void> { |
| 53 | + try { |
| 54 | + const git = await this.getGitExecutor(this.vscodeGitProvider, 'Prune Worktrees'); |
| 55 | + const candidates = await vscode.window.withProgress( |
| 56 | + { |
| 57 | + location: vscode.ProgressLocation.Notification, |
| 58 | + title: 'Git Smart Checkout: Looking for worktrees to prune...', |
| 59 | + cancellable: false, |
| 60 | + }, |
| 61 | + async () => collectPrunableWorktrees(git, this.logService, this.vscodeGitProvider) |
| 62 | + ); |
| 63 | + |
| 64 | + if (candidates.length === 0) { |
| 65 | + await vscode.window.showInformationMessage( |
| 66 | + 'No worktrees to prune. A worktree is prunable once its branch’s upstream is gone — ' + |
| 67 | + 'that is, the remote branch has been deleted.', |
| 68 | + 'OK' |
| 69 | + ); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const dirtyCount = candidates.filter(({ dirtyFiles }) => dirtyFiles.length > 0).length; |
| 74 | + const dirtyAction = await this.confirmPrune(candidates, dirtyCount); |
| 75 | + if (!dirtyAction) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const { pruned, unmerged, failed } = await vscode.window.withProgress( |
| 80 | + { |
| 81 | + location: vscode.ProgressLocation.Notification, |
| 82 | + title: 'Git Smart Checkout: Prune Worktrees', |
| 83 | + cancellable: false, |
| 84 | + }, |
| 85 | + async (progress) => this.pruneWorktrees(git, candidates, dirtyAction, progress) |
| 86 | + ); |
| 87 | + |
| 88 | + for (const { worktree } of pruned) { |
| 89 | + await removeWorkspaceFoldersForPath(worktree.path); |
| 90 | + } |
| 91 | + |
| 92 | + const forceDeleted = await this.resolveUnmergedBranches(git, unmerged); |
| 93 | + |
| 94 | + capture(AnalyticsEvent.WorktreesPruned, { |
| 95 | + count: pruned.length, |
| 96 | + had_dirty: dirtyCount > 0, |
| 97 | + dirty_action: dirtyAction, |
| 98 | + force_deleted_branches: forceDeleted.length, |
| 99 | + }); |
| 100 | + |
| 101 | + await refreshRemoveMultipleWorktreesVisibility(this.logService, this.vscodeGitProvider); |
| 102 | + |
| 103 | + await this.reportResult(pruned, unmerged, forceDeleted, failed); |
| 104 | + } catch (error) { |
| 105 | + captureException(error); |
| 106 | + const message = error instanceof Error ? error.message : String(error); |
| 107 | + message && (await vscode.window.showErrorMessage(message, 'OK')); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private async confirmPrune( |
| 112 | + candidates: PrunableWorktree[], |
| 113 | + dirtyCount: number |
| 114 | + ): Promise<DirtyAction | undefined> { |
| 115 | + const detail = formatPruneConfirmationDetail(candidates); |
| 116 | + const countLabel = `${candidates.length} worktree${candidates.length === 1 ? '' : 's'}`; |
| 117 | + |
| 118 | + if (dirtyCount === 0) { |
| 119 | + const choice = await vscode.window.showWarningMessage( |
| 120 | + `Prune ${countLabel} whose upstream branch is gone? Each worktree is removed and its local branch deleted.`, |
| 121 | + { modal: true, detail }, |
| 122 | + ACTION_PRUNE, |
| 123 | + ACTION_CANCEL |
| 124 | + ); |
| 125 | + |
| 126 | + return choice === ACTION_PRUNE ? 'clean' : undefined; |
| 127 | + } |
| 128 | + |
| 129 | + const choice = await vscode.window.showWarningMessage( |
| 130 | + `${countLabel} to prune, ${dirtyCount} with uncommitted changes. ` + |
| 131 | + 'What would you like to do with the changes before pruning?', |
| 132 | + { modal: true, detail }, |
| 133 | + ACTION_STASH_AND_PRUNE, |
| 134 | + ACTION_RESET_AND_PRUNE, |
| 135 | + ACTION_CANCEL |
| 136 | + ); |
| 137 | + |
| 138 | + if (choice === ACTION_STASH_AND_PRUNE) { |
| 139 | + return 'stash'; |
| 140 | + } |
| 141 | + |
| 142 | + if (choice === ACTION_RESET_AND_PRUNE) { |
| 143 | + return 'reset'; |
| 144 | + } |
| 145 | + |
| 146 | + return undefined; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Removes each worktree then deletes its branch with a non-force `git branch -d`. |
| 151 | + * A branch git refuses as not-fully-merged is collected rather than forced — the |
| 152 | + * caller asks before any commits are dropped. One worktree failing does not stop |
| 153 | + * the rest. |
| 154 | + */ |
| 155 | + private async pruneWorktrees( |
| 156 | + git: GitExecutor, |
| 157 | + candidates: PrunableWorktree[], |
| 158 | + dirtyAction: DirtyAction, |
| 159 | + progress: vscode.Progress<{ message?: string }> |
| 160 | + ): Promise<{ pruned: PrunableWorktree[]; unmerged: PrunableWorktree[]; failed: FailedPrune[] }> { |
| 161 | + const pruned: PrunableWorktree[] = []; |
| 162 | + const unmerged: PrunableWorktree[] = []; |
| 163 | + const failed: FailedPrune[] = []; |
| 164 | + |
| 165 | + for (const [index, candidate] of candidates.entries()) { |
| 166 | + const { worktree, branch, dirtyFiles } = candidate; |
| 167 | + progress.report({ |
| 168 | + message: `Pruning ${getWorktreeLabel(worktree)} (${index + 1}/${candidates.length})...`, |
| 169 | + }); |
| 170 | + |
| 171 | + try { |
| 172 | + if (dirtyFiles.length > 0 && dirtyAction !== 'clean') { |
| 173 | + const worktreeGit = new GitExecutor(worktree.path, this.logService, this.vscodeGitProvider); |
| 174 | + if (dirtyAction === 'stash') { |
| 175 | + await worktreeGit.createStash(getWorktreeStashName(worktree)); |
| 176 | + } else { |
| 177 | + await worktreeGit.discardAllWorktreeChanges(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + await git.worktreeRemove(worktree.path, false); |
| 182 | + pruned.push(candidate); |
| 183 | + } catch (error) { |
| 184 | + failed.push({ candidate, error: error instanceof Error ? error.message : String(error) }); |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + try { |
| 189 | + await git.deleteBranch(branch, false); |
| 190 | + } catch (error) { |
| 191 | + if (isNotFullyMergedError(error)) { |
| 192 | + unmerged.push(candidate); |
| 193 | + } else { |
| 194 | + failed.push({ candidate, error: error instanceof Error ? error.message : String(error) }); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return { pruned, unmerged, failed }; |
| 200 | + } |
| 201 | + |
| 202 | + /** Offers a single force-delete for the branches `git branch -d` held back. */ |
| 203 | + private async resolveUnmergedBranches( |
| 204 | + git: GitExecutor, |
| 205 | + unmerged: PrunableWorktree[] |
| 206 | + ): Promise<string[]> { |
| 207 | + if (unmerged.length === 0) { |
| 208 | + return []; |
| 209 | + } |
| 210 | + |
| 211 | + const detail = unmerged.map(({ branch }) => `• ${branch}`).join('\n'); |
| 212 | + const choice = await vscode.window.showWarningMessage( |
| 213 | + `${unmerged.length} branch${unmerged.length === 1 ? ' was' : 'es were'} not fully merged, ` + |
| 214 | + 'so the worktree was removed but the branch kept. Delete them anyway? Their unmerged commits will be lost.', |
| 215 | + { modal: true, detail }, |
| 216 | + ACTION_FORCE_DELETE_BRANCHES, |
| 217 | + ACTION_KEEP_BRANCHES |
| 218 | + ); |
| 219 | + |
| 220 | + if (choice !== ACTION_FORCE_DELETE_BRANCHES) { |
| 221 | + return []; |
| 222 | + } |
| 223 | + |
| 224 | + const deleted: string[] = []; |
| 225 | + for (const { branch } of unmerged) { |
| 226 | + try { |
| 227 | + await git.deleteBranch(branch, true); |
| 228 | + deleted.push(branch); |
| 229 | + } catch (error) { |
| 230 | + this.logService.warn( |
| 231 | + `[Prune Worktrees] Failed to force-delete branch ${branch}: ${ |
| 232 | + error instanceof Error ? error.message : String(error) |
| 233 | + }` |
| 234 | + ); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + return deleted; |
| 239 | + } |
| 240 | + |
| 241 | + private async reportResult( |
| 242 | + pruned: PrunableWorktree[], |
| 243 | + unmerged: PrunableWorktree[], |
| 244 | + forceDeleted: string[], |
| 245 | + failed: FailedPrune[] |
| 246 | + ): Promise<void> { |
| 247 | + if (pruned.length > 0) { |
| 248 | + const keptBranches = unmerged |
| 249 | + .map(({ branch }) => branch) |
| 250 | + .filter((branch) => !forceDeleted.includes(branch)); |
| 251 | + const deletedCount = pruned.length - keptBranches.length; |
| 252 | + const lines = [ |
| 253 | + `Pruned ${pruned.length} worktree${pruned.length === 1 ? '' : 's'}, ` + |
| 254 | + `deleted ${deletedCount} branch${deletedCount === 1 ? '' : 'es'}.`, |
| 255 | + ]; |
| 256 | + if (keptBranches.length > 0) { |
| 257 | + lines.push(`Kept unmerged branch${keptBranches.length === 1 ? '' : 'es'}: ${keptBranches.join(', ')}.`); |
| 258 | + } |
| 259 | + await vscode.window.showInformationMessage(lines.join(' '), 'OK'); |
| 260 | + } |
| 261 | + |
| 262 | + if (failed.length > 0) { |
| 263 | + const detail = failed |
| 264 | + .map(({ candidate, error }) => `${getWorktreeLabel(candidate.worktree)}: ${error}`) |
| 265 | + .join('\n'); |
| 266 | + await vscode.window.showErrorMessage( |
| 267 | + `Failed to prune ${failed.length} worktree${failed.length === 1 ? '' : 's'}:\n${detail}`, |
| 268 | + 'OK' |
| 269 | + ); |
| 270 | + } |
| 271 | + } |
| 272 | +} |
0 commit comments