grov 0.5.1

An opinionated bare-repo-only git worktree manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
193
194
195
196
197
198
199
200
201
202
203
204
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::io::{self, Write};

use console::style;
use crossterm::event::{self, Event};
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;

use crate::cli::RemoveMatchMode;
use crate::git::repo::find_bare_repo;
use crate::git::status::is_dirty;
use crate::git::worktree::{
    WorktreeInfo, delete_branch, list_worktrees, matches_branch_name, matches_dir_name,
    remove_worktree, safe_delete_branch, worktree_dir_name,
};
use crate::tui::FlowOutcome;
use crate::tui::confirm::{self, Confirm};
use crate::tui::select_list::{self, SelectList, SelectResult};
use crate::tui::terminal::run_tui;
use crate::tui::theme;

#[derive(Debug, PartialEq)]
pub(crate) struct RemoveChoice {
    pub worktree_index: usize,
    pub delete_branch: bool,
}

pub fn execute(
    name: Option<&str>,
    match_mode: RemoveMatchMode,
    do_delete_branch: bool,
    force: bool,
) -> anyhow::Result<()> {
    use std::io::IsTerminal;

    let cwd = std::env::current_dir()?;
    let repo = find_bare_repo(&cwd)?;
    let worktrees = list_worktrees(&repo)?;
    let is_tty = std::io::stdin().is_terminal();

    // Resolve which worktree to remove
    let (wt_index, should_delete_branch) = match name {
        Some(name) => (
            resolve_by_name(&worktrees, name, match_mode)?,
            do_delete_branch,
        ),
        None => {
            if !is_tty {
                anyhow::bail!("worktree name is required when stdin is not a terminal");
            }
            let choice = resolve_by_picker(&worktrees, do_delete_branch)?;
            (choice.worktree_index, choice.delete_branch)
        }
    };
    let wt = &worktrees[wt_index];

    if wt.is_bare {
        anyhow::bail!("cannot remove the bare repository entry");
    }

    // Check for dirty state
    if !force && is_dirty(&wt.path).unwrap_or(false) {
        anyhow::bail!("worktree has uncommitted changes (use --force to override)");
    }

    let branch_name = wt.branch.clone();
    let wt_path = wt.path.clone();

    remove_worktree(&repo, &wt_path, force)?;

    println!(
        "{} Removed worktree at {}",
        style("\u{2713}").green().bold(),
        style(wt_path.display()).bold(),
    );

    // Branch deletion logic
    if let Some(ref branch) = branch_name
        && should_delete_branch
    {
        match safe_delete_branch(&repo, branch) {
            Ok(()) => {
                println!(
                    "{} Deleted branch {}",
                    style("\u{2713}").green().bold(),
                    style(branch).cyan().bold(),
                );
            }
            Err(e) => {
                let msg = format!("{e:#}");
                if msg.contains("not fully merged") {
                    // Inline y/n prompt (no full TUI — this is a follow-up to an error)
                    eprint!(
                        "{} Branch has unmerged changes. Force delete? [y/N] ",
                        style("!").yellow().bold()
                    );
                    io::stderr().flush()?;
                    let mut answer = String::new();
                    io::stdin().read_line(&mut answer)?;
                    if answer.trim().eq_ignore_ascii_case("y") {
                        delete_branch(&repo, branch)?;
                        println!(
                            "{} Deleted branch {}",
                            style("\u{2713}").green().bold(),
                            style(branch).cyan().bold(),
                        );
                    }
                } else {
                    eprintln!(
                        "{} Could not delete branch: {e:#}",
                        style("!").yellow().bold()
                    );
                }
            }
        }
    }

    Ok(())
}

/// Resolve the worktree by name/match-mode. Returns the index into `worktrees`.
fn resolve_by_name(
    worktrees: &[WorktreeInfo],
    name: &str,
    match_mode: RemoveMatchMode,
) -> anyhow::Result<usize> {
    let matches: Vec<usize> = worktrees
        .iter()
        .enumerate()
        .filter(|(_, worktree)| match match_mode {
            RemoveMatchMode::Auto => {
                matches_branch_name(worktree, name) || matches_dir_name(worktree, name)
            }
            RemoveMatchMode::Branch => matches_branch_name(worktree, name),
            RemoveMatchMode::Dir => matches_dir_name(worktree, name),
        })
        .map(|(i, _)| i)
        .collect();

    if matches.is_empty() {
        anyhow::bail!("worktree not found: {name}");
    }
    if matches.len() > 1 {
        let candidates = matches
            .iter()
            .map(|&i| {
                let worktree = &worktrees[i];
                let branch = worktree.branch.as_deref().unwrap_or("<none>");
                let dir = worktree_dir_name(worktree);
                format!(
                    "  - branch={branch} dir={dir} path={}",
                    worktree.path.display()
                )
            })
            .collect::<Vec<_>>()
            .join("\n");
        anyhow::bail!(
            "ambiguous worktree name '{name}' matched multiple worktrees:\n{candidates}\nrerun with --match branch or --match dir"
        );
    }
    Ok(matches[0])
}

#[derive(Debug)]
enum PickerStep {
    SelectWorktree,
    ConfirmDeleteBranch { wt_index: usize },
}

pub(crate) struct RemovePicker {
    step: PickerStep,
    candidates: Vec<(usize, String)>,
    worktrees: Vec<WorktreeInfo>,
    flag_delete_branch: bool,
    select: SelectList,
    confirm: Confirm,
}

impl RemovePicker {
    pub(crate) fn new(worktrees: Vec<WorktreeInfo>, flag_delete_branch: bool) -> Self {
        let candidates: Vec<(usize, String)> = worktrees
            .iter()
            .enumerate()
            .filter(|(_, wt)| !wt.is_bare)
            .map(|(i, wt)| {
                let branch = wt.branch.as_deref().unwrap_or("<detached>");
                let dir = worktree_dir_name(wt);
                (i, format!("{branch} ({dir})"))
            })
            .collect();

        let display: Vec<String> = candidates.iter().map(|(_, label)| label.clone()).collect();
        let select = SelectList::new("Select a worktree to remove", display, vec![]);
        let confirm = Confirm::new("");

        Self {
            step: PickerStep::SelectWorktree,
            candidates,
            worktrees,
            flag_delete_branch,
            select,
            confirm,
        }
    }

    pub(crate) fn handle_event(
        &mut self,
        event: &Event,
    ) -> anyhow::Result<FlowOutcome<RemoveChoice>> {
        match &self.step {
            PickerStep::SelectWorktree => match self.select.handle_event(event) {
                select_list::Action::Selected(SelectResult::Item(i)) => {
                    let wt_index = self.candidates[i].0;
                    let wt = &self.worktrees[wt_index];

                    // If --delete-branch flag was passed, skip confirm
                    if self.flag_delete_branch {
                        return Ok(FlowOutcome::Done(RemoveChoice {
                            worktree_index: wt_index,
                            delete_branch: true,
                        }));
                    }

                    // If worktree has a branch, ask about deletion
                    if let Some(ref branch) = wt.branch {
                        self.confirm = Confirm::new(format!("Delete branch '{branch}' too?"));
                        self.step = PickerStep::ConfirmDeleteBranch { wt_index };
                    } else {
                        return Ok(FlowOutcome::Done(RemoveChoice {
                            worktree_index: wt_index,
                            delete_branch: false,
                        }));
                    }
                }
                select_list::Action::Selected(SelectResult::Extra(_)) => unreachable!(),
                select_list::Action::Cancel => anyhow::bail!("cancelled"),
                select_list::Action::Continue => {}
            },
            PickerStep::ConfirmDeleteBranch { wt_index } => {
                let wt_index = *wt_index;
                match self.confirm.handle_event(event) {
                    confirm::Action::Confirmed(yes) => {
                        return Ok(FlowOutcome::Done(RemoveChoice {
                            worktree_index: wt_index,
                            delete_branch: yes,
                        }));
                    }
                    confirm::Action::Cancel => {
                        let display: Vec<String> = self
                            .candidates
                            .iter()
                            .map(|(_, label)| label.clone())
                            .collect();
                        self.select =
                            SelectList::new("Select a worktree to remove", display, vec![]);
                        self.step = PickerStep::SelectWorktree;
                    }
                    confirm::Action::Continue => {}
                }
            }
        }
        Ok(FlowOutcome::Continue)
    }

    pub(crate) fn render(&self, frame: &mut Frame) {
        let area = frame.area();
        let widget_area = Rect::new(0, 1, area.width, area.height.saturating_sub(3));

        match &self.step {
            PickerStep::SelectWorktree => {
                self.select.render(frame, widget_area);
            }
            PickerStep::ConfirmDeleteBranch { .. } => {
                self.confirm.render(frame, widget_area);
            }
        }

        let help = match &self.step {
            PickerStep::SelectWorktree => theme::HELP_SELECT,
            PickerStep::ConfirmDeleteBranch { .. } => theme::HELP_CONFIRM,
        };
        let help_y = area.height.saturating_sub(1);
        let help_line = Line::from(Span::styled(format!("  {help}"), theme::DIM));
        frame.render_widget(
            Paragraph::new(help_line),
            Rect::new(0, help_y, area.width, 1),
        );
    }
}

/// Show an interactive TUI picker for worktree selection + optional branch deletion confirm.
fn resolve_by_picker(
    worktrees: &[WorktreeInfo],
    flag_delete_branch: bool,
) -> anyhow::Result<RemoveChoice> {
    if worktrees.iter().filter(|wt| !wt.is_bare).count() == 0 {
        anyhow::bail!("no worktrees to remove");
    }

    run_tui(|terminal| {
        let mut picker = RemovePicker::new(worktrees.to_vec(), flag_delete_branch);

        loop {
            terminal.draw(|frame| picker.render(frame))?;

            if event::poll(std::time::Duration::from_millis(100))? {
                match picker.handle_event(&event::read()?)? {
                    FlowOutcome::Continue => {}
                    FlowOutcome::Done(result) => return Ok(result),
                }
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::tui::test_helpers::*;

    fn feed_picker(
        picker: &mut RemovePicker,
        events: &[Event],
    ) -> anyhow::Result<FlowOutcome<RemoveChoice>> {
        for ev in events {
            match picker.handle_event(ev)? {
                FlowOutcome::Done(r) => return Ok(FlowOutcome::Done(r)),
                FlowOutcome::Continue => {}
            }
        }
        Ok(FlowOutcome::Continue)
    }

    fn make_worktrees() -> Vec<WorktreeInfo> {
        vec![
            WorktreeInfo {
                path: PathBuf::from("/project/repo.git"),
                head: "abc1234".to_string(),
                branch: None,
                is_bare: true,
            },
            WorktreeInfo {
                path: PathBuf::from("/project/dev_main"),
                head: "def5678".to_string(),
                branch: Some("main".to_string()),
                is_bare: false,
            },
            WorktreeInfo {
                path: PathBuf::from("/project/dev_feature"),
                head: "ghi9012".to_string(),
                branch: Some("feature".to_string()),
                is_bare: false,
            },
            WorktreeInfo {
                path: PathBuf::from("/project/dev_detached"),
                head: "jkl3456".to_string(),
                branch: None,
                is_bare: false,
            },
        ]
    }

    #[test]
    fn select_with_branch_shows_confirm() {
        let mut picker = RemovePicker::new(make_worktrees(), false);
        // First non-bare is index 0 in candidates (worktree index 1, "main")
        // Press Enter to select it
        let result = feed_picker(&mut picker, &[enter()]).unwrap();
        // Should move to confirm step, not return Done yet
        assert!(matches!(result, FlowOutcome::Continue));
        assert!(matches!(
            picker.step,
            PickerStep::ConfirmDeleteBranch { .. }
        ));
    }

    #[test]
    fn select_without_branch_returns() {
        let mut picker = RemovePicker::new(make_worktrees(), false);
        // Navigate down to the detached worktree (3rd non-bare = index 2 in candidates)
        let events = vec![
            key_event(crossterm::event::KeyCode::Down), // feature
            key_event(crossterm::event::KeyCode::Down), // detached
            enter(),
        ];

        let result = feed_picker(&mut picker, &events).unwrap();
        match result {
            FlowOutcome::Done(choice) => {
                assert_eq!(choice.worktree_index, 3);
                assert!(!choice.delete_branch);
            }
            FlowOutcome::Continue => panic!("expected Done"),
        }
    }

    #[test]
    fn flag_delete_branch_skips_confirm() {
        let mut picker = RemovePicker::new(make_worktrees(), true);
        // Select first (main) — should skip confirm because flag is set
        let result = feed_picker(&mut picker, &[enter()]).unwrap();
        match result {
            FlowOutcome::Done(choice) => {
                assert_eq!(choice.worktree_index, 1);
                assert!(choice.delete_branch);
            }
            FlowOutcome::Continue => panic!("expected Done"),
        }
    }

    #[test]
    fn confirm_yes() {
        let mut picker = RemovePicker::new(make_worktrees(), false);
        // Select main, then confirm with 'y'
        let events = vec![
            enter(),       // select main
            key_char('y'), // confirm yes
        ];

        let result = feed_picker(&mut picker, &events).unwrap();
        match result {
            FlowOutcome::Done(choice) => {
                assert_eq!(choice.worktree_index, 1);
                assert!(choice.delete_branch);
            }
            FlowOutcome::Continue => panic!("expected Done"),
        }
    }

    #[test]
    fn confirm_no() {
        let mut picker = RemovePicker::new(make_worktrees(), false);
        // Select main, then confirm with 'n'
        let events = vec![
            enter(),       // select main
            key_char('n'), // confirm no
        ];

        let result = feed_picker(&mut picker, &events).unwrap();
        match result {
            FlowOutcome::Done(choice) => {
                assert_eq!(choice.worktree_index, 1);
                assert!(!choice.delete_branch);
            }
            FlowOutcome::Continue => panic!("expected Done"),
        }
    }

    #[test]
    fn esc_from_confirm_returns_to_list() {
        let mut picker = RemovePicker::new(make_worktrees(), false);
        // Select main, then Esc from confirm
        let events = vec![
            enter(), // select main
            esc(),   // back from confirm
        ];

        let result = feed_picker(&mut picker, &events).unwrap();
        assert!(matches!(result, FlowOutcome::Continue));
        assert!(matches!(picker.step, PickerStep::SelectWorktree));
    }
}