Skip to content

fix: safely delete dirty worktrees - #15

Merged
AgileIndustrialComplex merged 3 commits into
essafrom
claude/git-branch-untracked-files-f6c0c5
Jul 20, 2026
Merged

AgileIndustrialComplex merged 3 commits into
essafrom
claude/git-branch-untracked-files-f6c0c5

Conversation

@AgileIndustrialComplex

Copy link
Copy Markdown
Owner

Intent

Handle deleting a Git branch/worktree that has untracked or modified files the same way the tool already handles deleting branches with unmerged changes. Previously, deleting a marked worktree with untracked/modified files failed outright with a git error ('contains modified or untracked files, use --force to delete it') since action.DeleteWorktrees called plain 'git worktree remove' with no force option and no detection ahead of time. The fix mirrors the existing Unmerged-branch design exactly: gitdata.Item gets a new Dirty flag, computed during collection via 'git status --porcelain' scoped to each worktree's path (git status --porcelain -C ). The UI's mark-then-D-then-confirm delete flow now also routes into the existing phrase-typed ('delete') force-confirmation screen (modeConfirmForceDelete) when any marked item is Dirty, not just when one is Unmerged; the confirmation screen shows separate sections listing unmerged branches and dirty worktrees when applicable, only listing whichever are present. action.DeleteWorktrees runs 'git worktree remove --force' instead of the plain form for Dirty items, and only after that explicit typed consent - never inferred automatically. Deliberately did not add any new confirmation phrase or config option; reused the existing ForceDeleteConfirmPhrase constant and mode, since the user's request was explicitly to handle this 'similarly to how unmerged branches are handled' rather than introduce a separate flow. Updated SPEC.md and README.md to document the merged unmerged+dirty confirmation screen, and added corresponding unit tests (gitdata_test.go, action_test.go, ui_test.go) plus one integration test that creates a real untracked file in a worktree and drives the picker through the force-delete flow end to end.

What Changed

  • Detect modified and untracked files in each available worktree while safely skipping prunable worktree entries.
  • Route dirty worktree deletion through the existing typed delete confirmation flow, with separate warnings for dirty worktrees and unmerged branches.
  • Force-remove only confirmed dirty worktrees, with updated documentation and unit/integration coverage.

Risk Assessment

✅ Low: The fixes address both prior findings, and the complete change now consistently detects dirty worktrees, requires typed consent, and force-removes only flagged items.

Testing

Focused unit tests, the dirty-worktree integration test, and the full Go suite passed; manual terminal verification captured the dirty-worktree warning and typed delete flow, then confirmed the worktree and branch were removed with the source worktree left clean.

Evidence: Dirty-worktree CLI confirmation transcript
spawn sh -c cd '/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/manual-repo' && TERM=dumb '/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/wt' --no-color
Select branch or worktree (j/k, / to filter, c to mark, ? for help)
> ● main                         [worktree]           /private/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/manual-repo (current)
  ○ scratch                      [worktree]

  ● main                         [worktree] (current)
> ○ scratch                      [worktree]           /private/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/scratch-worktree

  [ ] ● main                         [worktree] (current)
> [x] ○ scratch                      [worktree]           /private/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/scratch-worktree
1 marked — [D] delete
Delete 1 branch(es) (and their worktrees, if any)?
  scratch  /private/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/scratch-worktree
[Enter] confirm  [Esc] cancel
The following worktree(s) have modified or untracked files that will be permanently lost:

Type "delete" and press Enter to proceed, or Esc to cancel:

> 




> delete
 Deleted branch scratch (was 8a94e76).
wt: deleted 1 branch(es)
Evidence: Post-deletion Git state

After typed confirmation, the scratch worktree directory no longer exists, the scratch branch is absent, and only the main worktree remains registered.

Manual end-to-end verification after typing the required phrase:
worktree directory exists: no
scratch branch listing: 
registered worktrees:
worktree /private/var/folders/6y/60sv_t9162q36tw66s1fnynm0000gn/T/no-mistakes-evidence/01KXY7GWGQAPFTBMC2XHET3ARF/manual-repo
HEAD 8a94e767fc21075575146f152f7dc97748c18443
branch refs/heads/main

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 2 issues found → auto-fixed ✅
  • 🚨 internal/gitdata/gitdata.go:225 - Required intent says Dirty must identify worktrees with “untracked or modified files,” but git status --porcelain respects status.showUntrackedFiles=no. In repositories with that setting, an untracked-only worktree is classified clean, bypasses phrase confirmation, and plain removal fails. Override the setting with --untracked-files=all (or equivalent).
  • ⚠️ internal/gitdata/gitdata.go:127 - Collection now aborts when a registered worktree path is missing or inaccessible. git worktree list can legitimately return prunable entries whose directories no longer exist, so the new git -C <path> status call prevents the picker from opening at all. Parse the prunable marker and avoid status probing for unavailable paths, or handle this failure separately.

🔧 Fix: Handle hidden untracked files and prunable worktrees
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • go test ./internal/gitdata ./internal/action ./internal/ui
  • go test . -run '^TestIntegrationDeleteDirtyWorktreeRequiresForcePhrase$' -v
  • go test ./...
  • Built ./cmd/wt, created a real Git worktree containing untracked.txt, drove the terminal picker through j, c, D, Enter, delete, Enter, and captured the rendered confirmation transcript.
  • Verified with test -e, git branch --list scratch, and git worktree list --porcelain that the dirty worktree directory and branch were removed while the main worktree remained.
  • git status --short after testing confirmed no transient files were left in the working tree.
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

AgileIndustrialComplex and others added 3 commits July 20, 2026 00:21
Mirrors the existing unmerged-branch force-delete flow: gitdata now
detects dirty worktrees via `git status --porcelain`, the picker routes
marked dirty worktrees through the same phrase-typed confirmation
screen as unmerged branches, and action force-removes them with
`git worktree remove --force` only after that explicit consent.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@AgileIndustrialComplex
AgileIndustrialComplex merged commit 723fd81 into essa Jul 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant