cleanupWorktree removes a worktree created for an interactive run once it ends. A clean worktree (no uncommitted changes, untracked files, or new commits) is removed automatically. A dirty one is kept unless the user explicitly asks to remove it, so work is never discarded silently. Failures are rep
(ctx context.Context, out *cli.Printer, wt *worktree.Worktree)
| 628 | // Failures are reported but never abort the command — the run already |
| 629 | // succeeded. |
| 630 | func (f *runExecFlags) cleanupWorktree(ctx context.Context, out *cli.Printer, wt *worktree.Worktree) { |
| 631 | st, err := wt.Status(ctx) |
| 632 | if err != nil { |
| 633 | out.Println("Could not inspect git worktree " + wt.Dir + ": " + err.Error()) |
| 634 | out.Println("Leaving it in place. Remove it manually with: git -C " + wt.SourceDir + " worktree remove " + wt.Dir) |
| 635 | return |
| 636 | } |
| 637 | |
| 638 | if st.IsDirty() { |
| 639 | if !promptRemoveDirtyWorktree(ctx, out, wt, st) { |
| 640 | out.Println("Keeping git worktree " + wt.Dir + " (branch " + wt.Branch + ").") |
| 641 | return |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | if err := wt.Remove(ctx); err != nil { |
| 646 | out.Println("Failed to remove git worktree " + wt.Dir + ": " + err.Error()) |
| 647 | return |
| 648 | } |
| 649 | out.Println("Removed git worktree " + wt.Dir + " (branch " + wt.Branch + ").") |
| 650 | } |
| 651 | |
| 652 | // promptRemoveDirtyWorktree asks the user whether to discard a worktree that |
| 653 | // still holds work. It defaults to keeping (returns false) on any non-yes |