gwm 0.3.4

Git Worktree Manager - A CLI tool for managing Git worktrees with an interactive TUI
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
//! Shell completion script generator.
//!
//! Generates shell completion scripts for bash, zsh, and fish.
//! Supports both static (clap-based) and dynamic (runtime) completion.

use clap::CommandFactory;
use clap_complete::{generate, Shell};

use crate::cli::{Cli, CompletionShell, ShellType};
use crate::error::Result;
use crate::shell::get_gwm_bin_expr;

/// Editor options for completion (code, cursor, zed).
const EDITOR_OPTIONS: &str = "code cursor zed";

/// Generate and print shell completion script to stdout.
///
/// # Arguments
/// * `shell` - Target shell type
/// * `with_dynamic` - Enable dynamic completion for worktree names
pub fn run_completion(shell: CompletionShell, with_dynamic: bool) -> Result<()> {
    let script = generate_completion(shell, with_dynamic);
    print!("{}", script);
    Ok(())
}

/// Generate completion script string.
fn generate_completion(shell: CompletionShell, with_dynamic: bool) -> String {
    match shell {
        CompletionShell::Bash => generate_bash(with_dynamic),
        CompletionShell::Zsh => generate_zsh(with_dynamic),
        CompletionShell::Fish => generate_fish(with_dynamic),
    }
}

/// Generate dynamic completion script for shell integration.
///
/// This function is called by `gwm init` to include completion
/// in the shell integration output. Always uses dynamic completion.
pub fn generate_for_shell_type(shell: ShellType) -> String {
    match shell {
        ShellType::Bash => generate_bash(true),
        ShellType::Zsh => generate_zsh(true),
        ShellType::Fish => generate_fish(true),
    }
}

/// Generate static completion using clap_complete
fn generate_static_completion(shell: Shell) -> String {
    let mut cmd = Cli::command();
    let mut buf = Vec::new();
    generate(shell, &mut cmd, "gwm", &mut buf);
    match String::from_utf8(buf) {
        Ok(script) => script,
        Err(e) => {
            eprintln!("Warning: Failed to generate completion script: {}", e);
            "# Error: Failed to generate completion script\n".to_string()
        }
    }
}

/// Generate Bash completion script.
fn generate_bash(with_dynamic: bool) -> String {
    if with_dynamic {
        generate_bash_dynamic()
    } else {
        generate_static_completion(Shell::Bash)
    }
}

/// Generate Bash dynamic completion script.
fn generate_bash_dynamic() -> String {
    let gwm_bin = get_gwm_bin_expr();

    format!(
        r#"# gwm - Git Worktree Manager completion script (with dynamic completion)

_gwm_worktrees() {{
    local gwm_bin={gwm_bin}
    "$gwm_bin" list --format=names 2>/dev/null
}}

_gwm_branches() {{
    git branch --format='%(refname:short)' 2>/dev/null
}}

_gwm() {{
    local cur="${{COMP_WORDS[COMP_CWORD]}}"
    local prev="${{COMP_WORDS[COMP_CWORD-1]}}"
    local cmd="${{COMP_WORDS[1]}}"

    # First argument: subcommand
    if [[ $COMP_CWORD -eq 1 ]]; then
        COMPREPLY=($(compgen -W "add go init list ls sync remove rm clean completion help" -- "$cur"))
        return 0
    fi

    # Handle subcommand-specific completions
    case "$cmd" in
        go)
            case "$prev" in
                -o|--open)
                    COMPREPLY=($(compgen -W "{EDITOR_OPTIONS}" -- "$cur"))
                    return 0
                    ;;
                *)
                    if [[ "$cur" == -* ]]; then
                        COMPREPLY=($(compgen -W "-o --open" -- "$cur"))
                    else
                        local worktrees
                        worktrees=$(_gwm_worktrees)
                        COMPREPLY=($(compgen -W "$worktrees" -- "$cur"))
                    fi
                    return 0
                    ;;
            esac
            ;;
        remove|rm)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "-f --force" -- "$cur"))
            else
                local worktrees
                worktrees=$(_gwm_worktrees)
                COMPREPLY=($(compgen -W "$worktrees" -- "$cur"))
            fi
            return 0
            ;;
        add)
            case "$prev" in
                -f|--from)
                    local branches
                    branches=$(_gwm_branches)
                    COMPREPLY=($(compgen -W "$branches" -- "$cur"))
                    return 0
                    ;;
                -o|--open)
                    COMPREPLY=($(compgen -W "{EDITOR_OPTIONS}" -- "$cur"))
                    return 0
                    ;;
                *)
                    if [[ "$cur" == -* ]]; then
                        COMPREPLY=($(compgen -W "-r --remote --from -o --open --no-cd --skip-hooks" -- "$cur"))
                    fi
                    return 0
                    ;;
            esac
            ;;
        list|ls)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "-c --compact --format" -- "$cur"))
            elif [[ "$prev" == "--format" ]]; then
                COMPREPLY=($(compgen -W "table json names" -- "$cur"))
            fi
            return 0
            ;;
        clean)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "-f --force -n --dry-run" -- "$cur"))
            fi
            return 0
            ;;
        init|completion)
            if [[ "$cur" == -* ]]; then
                if [[ "$cmd" == "completion" ]]; then
                    COMPREPLY=($(compgen -W "--with-dynamic" -- "$cur"))
                fi
            else
                COMPREPLY=($(compgen -W "bash zsh fish" -- "$cur"))
            fi
            return 0
            ;;
        help)
            COMPREPLY=($(compgen -W "add go init list sync remove clean completion" -- "$cur"))
            return 0
            ;;
    esac
}}

complete -F _gwm gwm
"#
    )
}

/// Generate Zsh completion script.
fn generate_zsh(with_dynamic: bool) -> String {
    if with_dynamic {
        generate_zsh_dynamic()
    } else {
        generate_static_completion(Shell::Zsh)
    }
}

/// Generate Zsh dynamic completion script.
fn generate_zsh_dynamic() -> String {
    let gwm_bin = get_gwm_bin_expr();

    format!(
        r##"#compdef gwm

# gwm - Git Worktree Manager completion script (with dynamic completion)

_gwm_worktrees() {{
    local gwm_bin={gwm_bin}
    local -a worktrees
    worktrees=(${{(f)"$("$gwm_bin" list --format=names 2>/dev/null)"}})
    if [[ $#worktrees -gt 0 ]]; then
        _describe -t worktrees 'worktree' worktrees
    fi
}}

_gwm_branches() {{
    local -a branches
    branches=(${{(f)"$(git branch --format='%(refname:short)' 2>/dev/null)"}})
    if [[ $#branches -gt 0 ]]; then
        _describe -t branches 'branch' branches
    fi
}}

_gwm_editors() {{
    local -a editors
    editors=(
        'code:Visual Studio Code'
        'cursor:Cursor'
        'zed:Zed'
    )
    _describe -t editors 'editor' editors
}}

_gwm_shells() {{
    local -a shells
    shells=(bash zsh fish)
    _describe -t shells 'shell' shells
}}

_gwm() {{
    local context state state_descr line
    typeset -A opt_args

    _arguments -C \
        '1: :->command' \
        '*:: :->args'

    case $state in
        command)
            local -a commands
            commands=(
                'add:Create a new worktree'
                'go:Go to a worktree directory or open it in an editor'
                'init:Print shell integration script'
                'list:List all worktrees for the current project'
                'ls:List all worktrees (alias for list)'
                'sync:Update the main branch worktrees'
                'remove:Remove one or more worktrees'
                'rm:Remove worktrees (alias for remove)'
                'clean:Clean up safe-to-delete worktrees'
                'completion:Generate shell completion scripts'
                'help:Show help for gwm or a specific command'
            )
            _describe -t commands 'gwm command' commands
            ;;
        args)
            case $words[1] in
                go)
                    _arguments \
                        '(-o --open)'{{-o,--open}}'[Open in editor]:editor:_gwm_editors' \
                        '1: :_gwm_worktrees'
                    ;;
                remove|rm)
                    _arguments \
                        '(-f --force)'{{-f,--force}}'[Force removal without confirmation]' \
                        '*: :_gwm_worktrees'
                    ;;
                add)
                    _arguments \
                        '(-r --remote)'{{-r,--remote}}'[Use a remote branch]' \
                        '--from[Base branch to create from]:branch:_gwm_branches' \
                        '(-o --open)'{{-o,--open}}'[Open in editor after creation]:editor:_gwm_editors' \
                        '--no-cd[Show success message instead of path output]' \
                        '--skip-hooks[Skip post_create hooks]' \
                        '1:branch name:'
                    ;;
                list|ls)
                    _arguments \
                        '(-c --compact)'{{-c,--compact}}'[Compact output]' \
                        '--format[Output format]:(table json names)'
                    ;;
                clean)
                    _arguments \
                        '(-f --force)'{{-f,--force}}'[Force cleanup without confirmation]' \
                        '(-n --dry-run)'{{-n,--dry-run}}'[Show what would be deleted]'
                    ;;
                init)
                    _arguments \
                        '1:shell:_gwm_shells'
                    ;;
                completion)
                    _arguments \
                        '--with-dynamic[Enable dynamic completion]' \
                        '1:shell:_gwm_shells'
                    ;;
                help)
                    local -a help_topics
                    help_topics=(add go init list sync remove clean completion)
                    _describe -t topics 'topic' help_topics
                    ;;
            esac
            ;;
    esac
}}

# Register the completion function (required for eval)
compdef _gwm gwm
"##
    )
}

/// Generate Fish completion script.
fn generate_fish(with_dynamic: bool) -> String {
    let static_completion = generate_static_completion(Shell::Fish);

    if !with_dynamic {
        return static_completion;
    }

    let gwm_bin = get_gwm_bin_expr();

    format!(
        r#"{static_completion}

# Dynamic completion for gwm (worktree names)
function __gwm_worktrees
    set -l gwm_bin {gwm_bin}
    $gwm_bin list --format=names 2>/dev/null
end

function __gwm_branches
    git branch --format='%(refname:short)' 2>/dev/null
end

# Add dynamic completion for go and remove commands
complete -c gwm -n "__fish_seen_subcommand_from go" -f -a "(__gwm_worktrees)"
complete -c gwm -n "__fish_seen_subcommand_from remove rm" -f -a "(__gwm_worktrees)"
complete -c gwm -n "__fish_contains_opt from" -f -a "(__gwm_branches)"
complete -c gwm -n "__fish_contains_opt o open" -f -a "{EDITOR_OPTIONS}"
"#
    )
}

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

    #[test]
    fn test_generate_bash_static() {
        let script = generate_bash(false);
        assert!(script.contains("_gwm"));
        // 静的補完には動的ワークツリー関数は含まれない
        assert!(!script.contains("_gwm_worktrees"));
    }

    #[test]
    fn test_generate_bash_dynamic() {
        let script = generate_bash(true);
        // 動的補完には worktree 取得関数が含まれる
        assert!(script.contains("_gwm_worktrees"));
        assert!(script.contains("list --format=names"));
        // complete -F _gwm gwm の形式
        assert!(script.contains("complete -F _gwm gwm"));
    }

    #[test]
    fn test_generate_zsh_static() {
        let script = generate_zsh(false);
        assert!(script.contains("#compdef gwm"));
    }

    #[test]
    fn test_generate_zsh_dynamic() {
        let script = generate_zsh(true);
        assert!(script.contains("_gwm_worktrees"));
        assert!(script.contains("list --format=names"));
    }

    #[test]
    fn test_generate_fish_static() {
        let script = generate_fish(false);
        assert!(script.contains("complete -c gwm"));
    }

    #[test]
    fn test_generate_fish_dynamic() {
        let script = generate_fish(true);
        assert!(script.contains("__gwm_worktrees"));
        assert!(script.contains("list --format=names"));
    }

    #[test]
    fn test_run_completion_bash() {
        // Should not panic
        let result = run_completion(CompletionShell::Bash, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_completion_zsh_with_dynamic() {
        // Should not panic
        let result = run_completion(CompletionShell::Zsh, true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_generate_for_shell_type() {
        use crate::cli::ShellType;

        // All shells should produce dynamic completion
        let bash = generate_for_shell_type(ShellType::Bash);
        assert!(bash.contains("_gwm_worktrees"));
        assert!(bash.contains("complete -F _gwm gwm"));

        let zsh = generate_for_shell_type(ShellType::Zsh);
        assert!(zsh.contains("_gwm_worktrees"));
        assert!(zsh.contains("#compdef gwm"));

        let fish = generate_for_shell_type(ShellType::Fish);
        assert!(fish.contains("__gwm_worktrees"));
        assert!(fish.contains("complete -c gwm"));
    }
}