diff --git a/README.md b/README.md index 987e44e..6b61f17 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,9 @@ Navigate with the arrow keys, `j`/`k`, or `Ctrl-N`/`Ctrl-P`. Jump to the top or Highlight a branch or worktree and press `c` to mark or unmark it as part of the selection. After the first mark, a `[x]`/`[ ]` marker column appears and remains visible until the last mark is removed. Marked state persists as you move the cursor or filter. The current worktree and locked worktrees cannot be marked. -Once at least one item is marked, a hint line appears below the list telling you which key acts on the marked set. Press `D` to delete every marked branch: a confirmation screen lists the branches (and worktree paths, where applicable) about to be removed, `Enter` runs the deletion (`git worktree remove` for items with a worktree, then `git branch -d` for each), and any cancel key (`Esc`, `Ctrl-C`, `q`) backs out without touching Git state or losing your marked selection. +Once at least one item is marked, a hint line appears below the list telling you which key acts on the marked set. Press `D` to delete every marked branch: a confirmation screen lists the branches (and worktree paths, where applicable) about to be removed. For a batch containing only merged branches, `Enter` runs the deletion (`git worktree remove` for items with a worktree, then `git branch -d` for each); any cancel key (`Esc`, `Ctrl-C`, `q`) backs out without touching Git state or losing your marked selection. -If any marked branch has unmerged changes, confirming does not delete anything yet — `git branch -d` would refuse it anyway. Instead a second screen lists the unmerged branch(es) and requires you to type the exact phrase `Yes, I want to remove the branch that has changes that have been unmerged.` and press `Enter` before the whole batch (merged and unmerged branches alike) is deleted, force-deleting only the unmerged ones (`git branch -D`). `Esc`/`Ctrl-C` cancels the entire batch and returns to the list without touching Git state. +If any marked branch has unmerged changes, confirming does not delete anything yet — `git branch -d` would refuse it anyway. Instead a second screen lists the unmerged branch(es) and requires you to type `delete` and press `Enter` before the whole batch (merged and unmerged branches alike) is deleted, force-deleting only the unmerged ones (`git branch -D`). `Esc`/`Ctrl-C` cancels the entire batch and returns to the list without touching Git state. The picker uses color to distinguish the selected row, current worktree, marked items, worktree tags, locked worktrees, and filter prompt. Pass `--no-color` to disable all ANSI styling. @@ -98,7 +98,7 @@ keymap = "vim" # vim | emacs | arrows-only `wt` is written in Go and compiles to a single static binary, so there is no interpreter or runtime to install alongside it. The interactive list is built with the `bubbletea` terminal UI framework, which handles raw terminal mode, key events, and resizing consistently across Linux, macOS, and Windows. -`wt` never reimplements Git logic. It shells out to the real `git` binary for every read (`git branch`, `git worktree list --porcelain`) and every write (`git switch`, `git worktree add`, `git worktree remove`, `git branch -d`), so behavior always matches what you would get running those commands yourself, including your existing hooks and credential helpers. A small collector step parses both outputs and merges them into one list, tagging each branch with whether it already has a worktree and whether that worktree is locked. That merged view is what makes the "already checked out elsewhere" case visible instead of being an error you hit after the fact. +`wt` never reimplements Git logic. It shells out to the real `git` binary for every read (`git branch`, including its merged-branch query, and `git worktree list --porcelain`) and every write (`git switch`, `git worktree add`, `git worktree remove`, `git branch -d`, and confirmed `git branch -D`), so behavior always matches what you would get running those commands yourself, including your existing hooks and credential helpers. A small collector step parses those outputs and merges them into one list, tagging each branch with its merge status, whether it already has a worktree, and whether that worktree is locked. That merged view is what makes the "already checked out elsewhere" case visible instead of being an error you hit after the fact. Because a subprocess cannot change its parent shell's working directory, `wt` prints the destination path and relies on a thin shell function, installed once via `wt init `, to perform the actual `cd`. This keeps your working directory untouched unless a switch actually happens. diff --git a/SPEC.md b/SPEC.md index 035a7e5..32d08da 100644 --- a/SPEC.md +++ b/SPEC.md @@ -2,7 +2,7 @@ ## 1. Overview -`wt` is a single-binary CLI tool that presents a unified, interactively navigable list of local branches and Git worktrees, and switches the user's shell context to whichever one they select — either by `cd`-ing into an existing worktree or by checking out a branch in the current repository. It can also delete one or more branches (and their worktrees, where present) in bulk, via a mark-then-confirm flow. It never mutates Git state on its own; every write (checkout, `worktree add`, `worktree remove`, `branch -d`) happens only after explicit user confirmation, and only via standard `git` invocations. +`wt` is a single-binary CLI tool that presents a unified, interactively navigable list of local branches and Git worktrees, and switches the user's shell context to whichever one they select — either by `cd`-ing into an existing worktree or by checking out a branch in the current repository. It can also delete one or more branches (and their worktrees, where present) in bulk, via a mark-then-confirm flow. It never mutates Git state on its own; every write (checkout, `worktree add`, `worktree remove`, `branch -d`, or explicitly confirmed `branch -D`) happens only after user confirmation, and only via standard `git` invocations. ## 2. Technology Choice @@ -50,14 +50,14 @@ No `fzf` dependency is required, but the design deliberately keeps the same *int │ - plain branch → git switch (in cwd repo) │ - worktree path → emit cd path to wrapper (no git mutation) │ - existing path → return it to the shell wrapper -│ - marked set → git worktree remove (if any) + git branch -d, per item +│ - marked set → git worktree remove (if any) + git branch -d/-D, per item └──────────────────────┘ ``` **Data flow:** -1. `gitdata` runs `git branch --list --format='%(refname:short)'` and `git worktree list --porcelain -z`, parses both, and produces a single normalized list of items, cross-referencing which branches are already checked out in a worktree (the exact information `git switch` uses to produce its "already checked out" error — surfacing it up front removes the dead end). +1. `gitdata` runs `git branch --list --format='%(refname:short)'`, `git branch --format='%(refname:short)' --merged`, and `git worktree list --porcelain -z`, parses their output, and produces a single normalized list of items, cross-referencing merge status and which branches are already checked out in a worktree (the exact information `git switch` uses to produce its "already checked out" error — surfacing it up front removes the dead end). 2. `ui` renders the list, handles keystrokes purely as state transitions (no side effects) until the user confirms. -3. `action` executes exactly one of: `git switch`, `git worktree add`, a directory change (for worktrees), or — after the mark-then-`D`-then-confirm flow — `git worktree remove` (only for marked items that have a worktree) followed by `git branch -d` for each marked item. +3. `action` executes exactly one of: `git switch`, `git worktree add`, a directory change (for worktrees), or — after the mark-then-`D`-then-confirm flow — `git worktree remove` (only for marked items that have a worktree) followed by `git branch -d` for merged branches or, after phrase-typed confirmation, `git branch -D` for unmerged branches. **Directory-change trick:** a subprocess cannot change its parent shell's `cwd`. `wt` handles this the same way `zoxide`/`fzf`-based `cd` wrappers do — see §5. @@ -89,8 +89,8 @@ Selecting a plain branch with no worktree and confirming prompts one extra line: This directly resolves the stated pain point: instead of `git switch` erroring out, the tool offers the two valid resolutions inline. **Deleting branches with unmerged changes:** `git branch -d` (the non-force -delete `wt` uses) refuses branches not fully merged, so by default such a -branch is simply left in place. `wt` detects this ahead of time — each +delete `wt` uses for merged branches) refuses branches not fully merged. +`wt` detects this ahead of time — each `Item` carries an `Unmerged` flag computed from `git branch --merged` during collection — and if any branch in the marked set is unmerged, confirming the normal delete screen (`D` then `Enter`) does not delete anything yet. @@ -99,9 +99,7 @@ Instead a second screen appears: ``` The following branch(es) have unmerged changes and will be permanently lost: feature/wip-thing -Type the phrase below exactly and press Enter to proceed, or Esc to cancel: - -Yes, I want to remove the branch that has changes that have been unmerged. +Type "delete" and press Enter to proceed, or Esc to cancel: > _ ``` @@ -109,13 +107,11 @@ Yes, I want to remove the branch that has changes that have been unmerged. - This screen only appears when at least one marked branch is unmerged; marked sets containing only merged branches delete exactly as before, with no extra step. -- The user must type the phrase **"Yes, I want to remove the branch that has - changes that have been unmerged."** exactly (case-sensitive, full - punctuation) and press `Enter` to proceed; any mismatch is rejected and - the input stays open for correction. `Esc` or `Ctrl-C` cancels the whole - batch — including any merged branches marked alongside the unmerged - one(s) — and returns to the list without touching Git state or losing - the marked selection. +- The user must type **"delete"** exactly (case-sensitive) and press `Enter` + to proceed; any mismatch is rejected and the input stays open for + correction. `Esc` or `Ctrl-C` cancels the whole batch — including any + merged branches marked alongside the unmerged one(s) — and returns to the + list without touching Git state or losing the marked selection. - Once confirmed, `wt` deletes the batch in one pass: merged branches via `git branch -d` as before, unmerged branches via `git branch -D` (force), since explicit, phrase-level consent was already obtained. diff --git a/internal/gitdata/gitdata.go b/internal/gitdata/gitdata.go index 4bff5a1..644f094 100644 --- a/internal/gitdata/gitdata.go +++ b/internal/gitdata/gitdata.go @@ -100,13 +100,15 @@ func collect(run runnerFunc) ([]Item, error) { } branches := parseLines(brOut) - mergedOut, err := run("branch", "--format=%(refname:short)", "--merged") - if err != nil { - return nil, err - } merged := make(map[string]bool, len(branches)) - for _, b := range parseLines(mergedOut) { - merged[b] = true + if len(branches) > 0 { + mergedOut, err := run("branch", "--format=%(refname:short)", "--merged") + if err != nil { + return nil, err + } + for _, b := range parseLines(mergedOut) { + merged[b] = true + } } top, err := toplevel(run) diff --git a/internal/gitdata/gitdata_test.go b/internal/gitdata/gitdata_test.go index c2a412e..cf7c3e7 100644 --- a/internal/gitdata/gitdata_test.go +++ b/internal/gitdata/gitdata_test.go @@ -202,6 +202,22 @@ func TestCollectMarksUnmergedBranches(t *testing.T) { } } +func TestCollectSkipsMergedQueryWhenThereAreNoBranches(t *testing.T) { + fr := fakeRunner{ + "worktree list --porcelain -z": "", + "branch --list --format=%(refname:short)": "", + "rev-parse --show-toplevel": "/repo/proj\n", + } + + items, err := collect(fr.run) + if err != nil { + t.Fatalf("collect() error = %v", err) + } + if len(items) != 0 { + t.Fatalf("collect() = %#v, want no items", items) + } +} + func TestParseWorktreePorcelainPreservesSpecialPath(t *testing.T) { out := "worktree /repo/café folder\\name\x00HEAD abc123\x00detached\x00\x00" got := parseWorktreePorcelain(out) diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 6c59340..194a1e5 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -29,10 +29,10 @@ const ( ) // ForceDeleteConfirmPhrase is the exact text a user must type to delete a -// branch with unmerged changes. It must match verbatim (case-sensitive, -// including punctuation) — this is deliberate friction against losing -// unmerged work, so it is never abbreviated or made configurable. -const ForceDeleteConfirmPhrase = "Yes, I want to remove the branch that has changes that have been unmerged." +// branch with unmerged changes. It must match verbatim (case-sensitive) — +// this is deliberate friction against losing unmerged work, so it is never +// made configurable. +const ForceDeleteConfirmPhrase = "delete" // Result is what the picker produced when the program exited. type Result struct { @@ -529,8 +529,7 @@ func (m Model) View() string { fmt.Fprintf(&b, " %s\n", item.Branch) } } - b.WriteString("Type the phrase below exactly and press Enter to proceed, or Esc to cancel:\n\n") - fmt.Fprintf(&b, " %s\n\n", ForceDeleteConfirmPhrase) + fmt.Fprintf(&b, "Type %q and press Enter to proceed, or Esc to cancel:\n\n", ForceDeleteConfirmPhrase) fmt.Fprintf(&b, "> %s\n", m.forceConfirmInput) return b.String() } diff --git a/internal/ui/ui_test.go b/internal/ui/ui_test.go index adfa299..ec60749 100644 --- a/internal/ui/ui_test.go +++ b/internal/ui/ui_test.go @@ -432,22 +432,32 @@ func TestForceDeleteBackspaceEditsInput(t *testing.T) { } } -func TestForceDeleteEscCancelsWithoutQuittingAndClearsInput(t *testing.T) { - items := testItems() - items[1].Unmerged = true - m := New(items, "/repo/proj", "", true) - m = send(t, m, key('j'), key('c'), key('D'), keyType(tea.KeyEnter), key('y'), keyType(tea.KeyEsc)) - if m.mode != modeList { - t.Fatalf("mode after Esc in modeConfirmForceDelete = %v, want modeList", m.mode) - } - if m.quitting { - t.Fatal("Esc in modeConfirmForceDelete should not quit the program") - } - if len(m.Marked()) != 1 { - t.Fatalf("Marked() after cancelling force-delete = %+v, want [feature/login] (cancel should not clear marks)", m.Marked()) - } - if m.forceConfirmInput != "" { - t.Fatalf("forceConfirmInput after cancel = %q, want empty", m.forceConfirmInput) +func TestForceDeleteCancelPreservesMarksAndClearsInput(t *testing.T) { + for _, tc := range []struct { + name string + key tea.KeyType + }{ + {name: "Esc", key: tea.KeyEsc}, + {name: "Ctrl-C", key: tea.KeyCtrlC}, + } { + t.Run(tc.name, func(t *testing.T) { + items := testItems() + items[1].Unmerged = true + m := New(items, "/repo/proj", "", true) + m = send(t, m, key('j'), key('c'), key('D'), keyType(tea.KeyEnter), key('y'), keyType(tc.key)) + if m.mode != modeList { + t.Fatalf("mode after %s in modeConfirmForceDelete = %v, want modeList", tc.name, m.mode) + } + if m.quitting { + t.Fatalf("%s in modeConfirmForceDelete should not quit the program", tc.name) + } + if len(m.Marked()) != 1 { + t.Fatalf("Marked() after cancelling force-delete = %+v, want [feature/login] (cancel should not clear marks)", m.Marked()) + } + if m.forceConfirmInput != "" { + t.Fatalf("forceConfirmInput after cancel = %q, want empty", m.forceConfirmInput) + } + }) } }