Skip to main content

git_worktree_manager/operations/
complete.rs

1//! Internal helper for shell tab completion: prints valid positional
2//! target tokens (worktree names + branch names) one per line.
3//!
4//! Consumed by the bash/zsh/fish/powershell completion scripts in
5//! shell_functions.rs to populate dynamic completions for `gw rm`,
6//! `gw resume`, `gw spawn`, and `gw exec`.
7
8use std::collections::BTreeSet;
9
10use crate::error::Result;
11
12pub fn print_completion_targets() -> Result<()> {
13    // cwd-scoped: only the repo we're inside (or the current scope, by
14    // discover_scope semantics). Errors are silenced — the shell doesn't
15    // care WHY there's nothing to complete; it just gets an empty list.
16    let cwd = match std::env::current_dir() {
17        Ok(p) => p,
18        Err(_) => return Ok(()),
19    };
20    let scope = match crate::scope::discover_scope(&cwd) {
21        Ok(s) => s,
22        Err(_) => return Ok(()),
23    };
24
25    let mut tokens: BTreeSet<String> = BTreeSet::new();
26    for w in scope.worktrees() {
27        // worktree name (file_name of the worktree path)
28        if let Some(name) = w.path.file_name().and_then(|n| n.to_str()) {
29            if !name.is_empty() {
30                tokens.insert(name.to_string());
31            }
32        }
33        // branch name (skip detached)
34        if let Some(branch) = &w.branch {
35            tokens.insert(branch.clone());
36        }
37    }
38
39    for tok in tokens {
40        println!("{}", tok);
41    }
42    Ok(())
43}