git-worktree-manager 0.0.39

CLI tool integrating git worktree with AI coding assistants
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Batch deletion orchestration for `gw delete`.
//!
//! Multi-target deletion pipeline: resolve-all → plan (busy) → summary →
//! confirm → execute → exit code. Reuses `worktree::delete_one` for per-target
//! execution.

use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};

use console::style;

use crate::error::{CwError, Result};
use crate::git;
use crate::operations::busy::{self, BusyInfo};
use crate::operations::busy_messages;
use crate::operations::worktree::{self, DeleteFlags};

/// Result of the interactive multi-select flow.
///
/// - `Selected(v)` — user confirmed with at least one pick; `v` is non-empty.
/// - `Nothing` — no feature worktrees, or user confirmed with zero selections.
///   Nothing-to-do is not an error; the orchestrator exits 0.
/// - `Cancelled` — user pressed Esc / q / Ctrl-C. Orchestrator exits 1.
enum InteractiveOutcome {
    Selected(Vec<String>),
    Nothing,
    Cancelled,
}

/// Open the multi-select TUI to let the user choose which feature worktrees
/// to delete. Distinguishes Selected / Nothing / Cancelled so the caller can
/// map each to the exit code the spec requires.
fn interactive_select(main_repo: &Path) -> Result<InteractiveOutcome> {
    let feature_worktrees = git::get_feature_worktrees(Some(main_repo))?;
    if feature_worktrees.is_empty() {
        eprintln!("No feature worktrees to delete.");
        return Ok(InteractiveOutcome::Nothing);
    }
    let labels: Vec<String> = feature_worktrees
        .iter()
        .map(|(branch, path)| format!("{:<30} {}", branch, path.display()))
        .collect();
    match crate::tui::multi_select::multi_select(&labels, "Select worktrees to delete:") {
        Some(indices) if indices.is_empty() => {
            eprintln!("Nothing selected.");
            Ok(InteractiveOutcome::Nothing)
        }
        Some(indices) => {
            let selected: Vec<String> = indices
                .into_iter()
                .map(|i| feature_worktrees[i].0.clone())
                .collect();
            Ok(InteractiveOutcome::Selected(selected))
        }
        None => {
            eprintln!("Cancelled.");
            Ok(InteractiveOutcome::Cancelled)
        }
    }
}

/// Resolved worktree target (path + optional branch).
#[derive(Debug, Clone)]
pub struct Resolved {
    pub input: String,
    pub path: PathBuf,
    pub branch: Option<String>,
}

/// A single entry in the batch execution plan.
#[derive(Debug)]
pub enum PlanEntry {
    Ready(Resolved),
    Busy {
        resolved: Resolved,
        hard: Vec<BusyInfo>,
        soft: Vec<BusyInfo>,
    },
    Unresolved {
        input: String,
        reason: String,
    },
}

/// Resolve a list of user inputs against the main repository.
///
/// Inputs may be branch names, worktree directory names, or filesystem paths.
/// Anything that does not resolve becomes a `PlanEntry::Unresolved`.
pub fn resolve_all(inputs: &[String], lookup_mode: Option<&str>) -> Result<Vec<PlanEntry>> {
    let main_repo = git::get_main_repo_root(None)?;
    let mut out = Vec::with_capacity(inputs.len());
    for input in inputs {
        match resolve_one(input, &main_repo, lookup_mode) {
            Some(resolved) => out.push(PlanEntry::Ready(resolved)),
            None => out.push(PlanEntry::Unresolved {
                input: input.clone(),
                reason: "not found".into(),
            }),
        }
    }
    Ok(out)
}

fn resolve_one(input: &str, main_repo: &Path, lookup_mode: Option<&str>) -> Option<Resolved> {
    // 1) filesystem path
    let p = PathBuf::from(input);
    if p.exists() {
        let resolved = p.canonicalize().unwrap_or(p);
        let branch = crate::operations::helpers::get_branch_for_worktree(main_repo, &resolved);
        return Some(Resolved {
            input: input.to_string(),
            path: resolved,
            branch,
        });
    }

    // 2) branch lookup
    if lookup_mode != Some("worktree") {
        if let Ok(Some(path)) = git::find_worktree_by_intended_branch(main_repo, input) {
            return Some(Resolved {
                input: input.to_string(),
                path,
                branch: Some(input.to_string()),
            });
        }
    }

    // 3) worktree name lookup
    if lookup_mode != Some("branch") {
        if let Ok(Some(path)) = git::find_worktree_by_name(main_repo, input) {
            let branch = crate::operations::helpers::get_branch_for_worktree(main_repo, &path);
            return Some(Resolved {
                input: input.to_string(),
                path,
                branch,
            });
        }
    }

    None
}

/// Annotate resolved entries with busy status. Unresolved entries pass through.
pub fn plan_busy(entries: Vec<PlanEntry>, allow_busy: bool) -> Vec<PlanEntry> {
    if allow_busy {
        return entries;
    }
    entries
        .into_iter()
        .map(|entry| match entry {
            PlanEntry::Ready(r) => {
                let (hard, soft) = busy::detect_busy_tiered(&r.path);
                if hard.is_empty() && soft.is_empty() {
                    PlanEntry::Ready(r)
                } else {
                    PlanEntry::Busy {
                        resolved: r,
                        hard,
                        soft,
                    }
                }
            }
            other => other,
        })
        .collect()
}

/// Counters for summary output.
struct PlanCounts {
    ready: usize,
    busy: usize,
    unresolved: usize,
}

fn count(entries: &[PlanEntry]) -> PlanCounts {
    let mut c = PlanCounts {
        ready: 0,
        busy: 0,
        unresolved: 0,
    };
    for e in entries {
        match e {
            PlanEntry::Ready(_) => c.ready += 1,
            PlanEntry::Busy { .. } => c.busy += 1,
            PlanEntry::Unresolved { .. } => c.unresolved += 1,
        }
    }
    c
}

/// Print the batch summary. Goes to stdout to match the convention used by
/// `gw clean` (summary/progress → stdout, errors/prompts → stderr).
pub fn print_summary(entries: &[PlanEntry], dry_run: bool) {
    let counts = count(entries);
    let header = if dry_run {
        format!("Would delete {} worktree(s):", counts.ready)
    } else {
        let busy_note = if counts.busy > 0 {
            format!(" ({} busy, will skip without --force)", counts.busy)
        } else {
            String::new()
        };
        format!("Deleting {} worktree(s){}:", counts.ready, busy_note)
    };
    println!("\n{}", style(header).yellow().bold());
    for e in entries {
        match e {
            PlanEntry::Ready(r) => {
                let label = r.branch.as_deref().unwrap_or(&r.input);
                println!("  {:<30} {}", label, r.path.display());
            }
            PlanEntry::Busy {
                resolved,
                hard,
                soft,
            } => {
                let label = resolved.branch.as_deref().unwrap_or(&resolved.input);
                let detail = hard
                    .first()
                    .or_else(|| soft.first())
                    .map(|b| format!("PID {} {}", b.pid, b.cmd))
                    .unwrap_or_default();
                println!("  {:<30} (busy: {})  [skip]", label, detail);
            }
            PlanEntry::Unresolved { input, reason } => {
                println!("  {:<30} [{}] [skip]", input, reason);
            }
        }
    }
    println!(
        "Total: {} planned, {} not found, {} busy",
        counts.ready, counts.unresolved, counts.busy
    );
    if dry_run {
        println!("(dry-run; nothing deleted)");
    }
    println!();
}

/// Ask for a single y/N confirmation on the whole batch. Only invoked when
/// planned.ready > 1 (or planned.ready >= 1 combined with skips worth
/// surfacing). Returns true if the user confirmed.
pub fn confirm_batch() -> bool {
    if !(std::io::stdin().is_terminal() && std::io::stderr().is_terminal()) {
        return true; // non-interactive: assume confirmed (scripted usage)
    }
    eprint!("Proceed? (y/N): ");
    let _ = std::io::stderr().flush();
    let mut buf = String::new();
    if std::io::stdin().read_line(&mut buf).is_err() {
        return false;
    }
    let ans = buf.trim().to_lowercase();
    ans == "y" || ans == "yes"
}

/// Final outcome used to compute exit code and summary.
///
/// Fields retain label/reason/error for Debug output and future per-item
/// reporting. The current summary only counts variants, so dead-code lint is
/// silenced here.
#[derive(Debug)]
#[allow(dead_code)]
enum ItemResult {
    Deleted(String),
    Skipped { label: String, reason: String },
    Failed { label: String, error: CwError },
}

fn label_of(entry: &PlanEntry) -> String {
    match entry {
        PlanEntry::Ready(r) => r.branch.clone().unwrap_or_else(|| r.input.clone()),
        PlanEntry::Busy { resolved, .. } => resolved
            .branch
            .clone()
            .unwrap_or_else(|| resolved.input.clone()),
        PlanEntry::Unresolved { input, .. } => input.clone(),
    }
}

/// Execute the plan sequentially. Best-effort: one failure does not abort.
fn execute_all(entries: Vec<PlanEntry>, flags: DeleteFlags) -> Result<Vec<ItemResult>> {
    let main_repo = git::get_main_repo_root(None)?;
    let mut results = Vec::with_capacity(entries.len());
    for entry in entries {
        let label = label_of(&entry);
        match entry {
            PlanEntry::Ready(r) => {
                // progress line → stdout
                println!("{} Deleting {}", style("").cyan().bold(), label);
                match worktree::delete_one(&r.path, r.branch.as_deref(), &main_repo, flags) {
                    worktree::DeletionOutcome::Deleted { .. } => {
                        results.push(ItemResult::Deleted(label));
                    }
                    worktree::DeletionOutcome::Skipped { reason } => {
                        results.push(ItemResult::Skipped { label, reason });
                    }
                    worktree::DeletionOutcome::Failed { error } => {
                        // failure → stderr
                        eprintln!(
                            "{} Failed to delete {}: {}",
                            style("x").red().bold(),
                            label,
                            error
                        );
                        results.push(ItemResult::Failed { label, error });
                    }
                }
            }
            PlanEntry::Busy { hard, soft, .. } => {
                // Summary line → stdout.
                println!("{} Skipped {} (busy)", style("~").yellow(), label);
                // Error mirror → stderr. Required so non-TTY `gw delete`
                // against a busy worktree emits a stderr hint matching the
                // legacy single-target flow (see tests/busy_detection.rs).
                eprint!(
                    "{} {}",
                    style("error:").red().bold(),
                    busy_messages::render_refusal(&label, &hard, &soft)
                );
                results.push(ItemResult::Skipped {
                    label,
                    reason: "busy".into(),
                });
            }
            PlanEntry::Unresolved { input, reason } => {
                println!("{} Skipped {} ({})", style("~").yellow(), input, reason);
                results.push(ItemResult::Skipped {
                    label: input,
                    reason,
                });
            }
        }
    }
    Ok(results)
}

fn print_results(results: &[ItemResult]) {
    let deleted = results
        .iter()
        .filter(|r| matches!(r, ItemResult::Deleted(_)))
        .count();
    let skipped = results
        .iter()
        .filter(|r| matches!(r, ItemResult::Skipped { .. }))
        .count();
    let failed = results
        .iter()
        .filter(|r| matches!(r, ItemResult::Failed { .. }))
        .count();
    println!(
        "\nSummary: {} deleted, {} skipped, {} failed",
        deleted, skipped, failed
    );
}

fn exit_code_from(results: &[ItemResult]) -> i32 {
    let any_bad = results
        .iter()
        .any(|r| matches!(r, ItemResult::Failed { .. } | ItemResult::Skipped { .. }));
    if any_bad {
        2
    } else {
        0
    }
}

/// If cwd lives inside any Ready/Busy target path, chdir to the main repo
/// root. Prevents the current `gw` process from being flagged as a busy holder
/// of the worktree it is being asked to remove.
///
/// Canonicalize failures on either side are treated as "skip this comparison"
/// rather than falling back to the raw path. On filesystems with symlinked
/// tempdirs (e.g. `/var` -> `/private/var` on macOS) an asymmetric fallback
/// could mis-classify and leave cwd in the target.
fn move_cwd_out_of_targets(entries: &[PlanEntry]) {
    let Ok(cwd) = std::env::current_dir() else {
        return;
    };
    let Ok(cwd_canon) = cwd.canonicalize() else {
        return;
    };
    for e in entries {
        let path = match e {
            PlanEntry::Ready(r) => &r.path,
            PlanEntry::Busy { resolved, .. } => &resolved.path,
            PlanEntry::Unresolved { .. } => continue,
        };
        let Ok(wt_canon) = path.canonicalize() else {
            continue;
        };
        if cwd_canon.starts_with(&wt_canon) {
            if let Ok(main_repo) = git::get_main_repo_root(None) {
                let _ = std::env::set_current_dir(&main_repo);
            }
            return;
        }
    }
}

/// Top-level orchestrator for `gw delete`.
///
/// `inputs` is empty for the legacy "current worktree" case and for the
/// `-i` interactive case — the caller passes `interactive=true` to trigger the
/// selector.
pub fn delete_worktrees(
    inputs: Vec<String>,
    interactive: bool,
    dry_run: bool,
    flags: DeleteFlags,
    lookup_mode: Option<&str>,
) -> Result<i32> {
    // 1) Decide the initial input set.
    let initial_inputs: Vec<String> = if interactive {
        debug_assert!(
            inputs.is_empty(),
            "clap should have rejected -i with positionals"
        );
        let main_repo = git::get_main_repo_root(None)?;
        match interactive_select(&main_repo)? {
            InteractiveOutcome::Selected(v) => v,
            InteractiveOutcome::Nothing => return Ok(0),
            InteractiveOutcome::Cancelled => return Ok(1),
        }
    } else if inputs.is_empty() {
        // Legacy path: delegate to the single-target shim and return its exit
        // code. Keeps the "no-args inside a worktree deletes current" behavior
        // and its busy prompt exactly as today.
        return legacy_single_current(flags, lookup_mode);
    } else {
        inputs
    };

    // 2) Resolve all inputs against the repo.
    let entries = resolve_all(&initial_inputs, lookup_mode)?;

    // 2.5) If cwd is inside any resolved target, move to the main repo *before*
    // busy detection so the running `gw` process doesn't register as a busy
    // holder of a worktree it's trying to delete. Mirrors the legacy
    // `delete_worktree` behavior.
    move_cwd_out_of_targets(&entries);

    // 3) Plan busy status.
    let entries = plan_busy(entries, flags.allow_busy);

    // 4) Print summary.
    print_summary(&entries, dry_run);

    // 5) Dry-run short-circuits before execution.
    if dry_run {
        return Ok(0);
    }

    // 6) Batch confirmation when the plan has more than one entry
    //    (Ready + Busy + Unresolved combined). Single-entry explicit
    //    positional goes straight to execute.
    if entries.len() >= 2 && !confirm_batch() {
        eprintln!("Cancelled.");
        return Ok(1);
    }

    // 7) Execute.
    let results = execute_all(entries, flags)?;
    print_results(&results);
    Ok(exit_code_from(&results))
}

fn legacy_single_current(flags: DeleteFlags, lookup_mode: Option<&str>) -> Result<i32> {
    match worktree::delete_worktree(
        None,
        flags.keep_branch,
        flags.delete_remote,
        flags.git_force,
        flags.allow_busy,
        lookup_mode,
    ) {
        Ok(()) => Ok(0),
        Err(e) => {
            eprintln!("{} {}", style("error:").red().bold(), e);
            Ok(2)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn plan_busy_passthrough_when_allowed() {
        let entries = vec![PlanEntry::Unresolved {
            input: "x".into(),
            reason: "not found".into(),
        }];
        let out = plan_busy(entries, true);
        assert_eq!(out.len(), 1);
        assert!(matches!(out[0], PlanEntry::Unresolved { .. }));
    }

    #[test]
    fn plan_busy_passes_unresolved_through_when_not_allowed() {
        let entries = vec![PlanEntry::Unresolved {
            input: "x".into(),
            reason: "not found".into(),
        }];
        let out = plan_busy(entries, false);
        assert_eq!(out.len(), 1);
        assert!(matches!(out[0], PlanEntry::Unresolved { .. }));
    }

    #[test]
    fn count_buckets_entries_correctly() {
        let entries = vec![
            PlanEntry::Ready(Resolved {
                input: "a".into(),
                path: PathBuf::from("/tmp/a"),
                branch: Some("a".into()),
            }),
            PlanEntry::Busy {
                resolved: Resolved {
                    input: "b".into(),
                    path: PathBuf::from("/tmp/b"),
                    branch: Some("b".into()),
                },
                hard: vec![],
                soft: vec![],
            },
            PlanEntry::Unresolved {
                input: "c".into(),
                reason: "not found".into(),
            },
        ];
        let c = count(&entries);
        assert_eq!(c.ready, 1);
        assert_eq!(c.busy, 1);
        assert_eq!(c.unresolved, 1);
    }
}