git-worktree-manager 0.0.40

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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! CLI entrypoint — shared between the `gw` and `cw` binaries.
//!
//! Both binary targets (`src/bin/gw.rs`, `src/bin/cw.rs`) delegate to
//! [`run`]. Keeping the logic here avoids compiling the same source file
//! twice, which triggered Cargo's "file present in multiple build targets"
//! warning under the previous layout.

use clap::Parser;

use crate::cli::{BackupAction, Cli, Commands, ConfigAction, HookAction, StashAction};
use crate::config;
use crate::console as cwconsole;
use crate::constants;
use crate::cwshare_setup;
use crate::error::{CwError, Result};
use crate::hooks;
use crate::operations::{
    ai_tools, backup, clean, config_ops, diagnostics, display, git_ops, global_ops, guard, helpers,
    path_cmd, setup_claude, shell, spawn_spec, stash, worktree,
};
use crate::resolve_prompt;
use crate::shell_functions;
use crate::tui;
use crate::update;
use std::io::Read;

pub fn run() {
    tui::install_panic_hook();
    let cli = Cli::parse();

    if let Some(ref shell_name) = cli.generate_completion {
        generate_completions(shell_name);
        return;
    }

    // Skip startup checks for internal commands (shell-completion helpers,
    // cache refresh) — they are invoked by the shell on every keystroke, so
    // paying for update-check / prompts would compound latency and risk
    // recursive re-entry into the update flow.
    let is_internal = matches!(
        &cli.command,
        Some(
            Commands::UpdateCache
                | Commands::ConfigKeys
                | Commands::TermValues
                | Commands::PresetNames
                | Commands::HookEvents
                | Commands::Path { .. }
                | Commands::ShellFunction { .. }
                | Commands::SpawnAi { .. }
                | Commands::Guard { .. }
        )
    );

    if !is_internal {
        crate::operations::spawn_spec::sweep_stale();
        update::check_for_update_if_needed();
    }

    if !is_internal {
        config::prompt_shell_completion_setup();
    }

    helpers::set_global_mode(cli.global);

    let result = match cli.command {
        Some(Commands::List { cache }) => {
            let no_cache = cache.no_cache;
            if cli.global {
                global_ops::global_list_worktrees(no_cache)
            } else {
                display::list_worktrees(no_cache)
            }
        }
        Some(Commands::Status { cache }) => display::show_status(cache.no_cache),
        Some(Commands::Tree { cache }) => display::show_tree(cache.no_cache),
        Some(Commands::Stats { cache }) => display::show_stats(cache.no_cache),
        Some(Commands::Diff {
            branch1,
            branch2,
            summary,
            files,
        }) => display::diff_worktrees(&branch1, &branch2, summary, files),

        Some(Commands::Config { action }) => match action {
            ConfigAction::Show => config::show_config().map(|output| println!("{}", output)),
            ConfigAction::List => config::list_config(),
            ConfigAction::Get { key } => config::get_config_value(&key),
            ConfigAction::Set { key, value } => config::set_config_value(&key, &value),
            ConfigAction::UsePreset { name } => config::use_preset(&name),
            ConfigAction::ListPresets => {
                println!("{}", config::list_presets());
                Ok(())
            }
            ConfigAction::Reset => config::reset_config(),
        },

        Some(Commands::New {
            name,
            path,
            base,
            no_term,
            term,
            bg: _,
            prompt,
            prompt_file,
            prompt_stdin,
        }) => (|| -> Result<()> {
            // Resolve the prompt first so a missing file or unreadable stdin
            // fails before any interactive side effects (worktree creation,
            // AI-tool launch) leave the tree in a half-configured state.
            let resolved = resolve_prompt(prompt, prompt_file.as_deref(), prompt_stdin, || {
                let mut buf = String::new();
                std::io::stdin().read_to_string(&mut buf)?;
                Ok(buf)
            })?;

            cwshare_setup::prompt_cwshare_setup();

            worktree::create_worktree(
                &name,
                base.as_deref(),
                path.as_deref(),
                term.as_deref(),
                no_term,
                resolved.as_deref(),
            )?;
            Ok(())
        })(),

        Some(Commands::Pr {
            branch,
            title,
            body,
            draft,
            no_push,
            worktree: is_worktree,
            by_branch,
        }) => {
            let lookup_mode = resolve_lookup_mode(is_worktree, by_branch);
            git_ops::create_pr_worktree(
                branch.as_deref(),
                !no_push,
                title.as_deref(),
                body.as_deref(),
                draft,
                lookup_mode,
            )
        }

        Some(Commands::Merge {
            branch,
            interactive,
            dry_run,
            push,
            ai_merge,
            worktree: is_worktree,
        }) => {
            let lookup_mode = if is_worktree { Some("worktree") } else { None };
            git_ops::merge_worktree(
                branch.as_deref(),
                push,
                interactive,
                dry_run,
                ai_merge,
                lookup_mode,
            )
        }

        Some(Commands::Resume {
            branch,
            term,
            bg: _,
            worktree: is_worktree,
            by_branch,
        }) => {
            let lookup_mode = resolve_lookup_mode(is_worktree, by_branch);
            ai_tools::resume_worktree(branch.as_deref(), term.as_deref(), lookup_mode)
        }

        Some(Commands::Shell { worktree, args }) => {
            let cmd = if args.is_empty() { None } else { Some(args) };
            shell::shell_worktree(worktree.as_deref(), cmd)
        }

        Some(Commands::Delete {
            targets,
            interactive,
            dry_run,
            keep_branch,
            delete_remote,
            force,
            no_force,
            worktree: is_worktree,
            branch: is_branch,
        }) => {
            let lookup_mode = resolve_lookup_mode(is_worktree, is_branch);
            let flags = crate::operations::worktree::DeleteFlags {
                keep_branch,
                delete_remote,
                git_force: !no_force,
                allow_busy: force,
            };
            match crate::operations::delete_batch::delete_worktrees(
                targets,
                interactive,
                dry_run,
                flags,
                lookup_mode,
            ) {
                Ok(0) => Ok(()),
                Ok(code) => Err(crate::error::CwError::ExitCode(code)),
                Err(e) => Err(e),
            }
        }

        Some(Commands::Clean {
            cache,
            merged,
            older_than,
            interactive,
            dry_run,
            force,
        }) => clean::clean_worktrees(
            cache.no_cache,
            merged,
            older_than,
            interactive,
            dry_run,
            force,
        ),

        Some(Commands::Sync {
            branch,
            all,
            fetch_only,
            ai_merge,
            worktree: is_worktree,
            by_branch,
        }) => {
            let lookup_mode = resolve_lookup_mode(is_worktree, by_branch);
            worktree::sync_worktree(branch.as_deref(), all, fetch_only, ai_merge, lookup_mode)
        }

        Some(Commands::ChangeBase {
            new_base,
            branch,
            dry_run,
            interactive,
            worktree: is_worktree,
            by_branch,
        }) => {
            let lookup_mode = resolve_lookup_mode(is_worktree, by_branch);
            config_ops::change_base_branch(
                &new_base,
                branch.as_deref(),
                dry_run,
                interactive,
                lookup_mode,
            )
        }

        Some(Commands::Backup { action }) => match action {
            BackupAction::Create {
                branch,
                all,
                output: _,
            } => backup::backup_worktree(branch.as_deref(), all),
            BackupAction::List { branch, all } => backup::list_backups(branch.as_deref(), all),
            BackupAction::Restore { branch, path, id } => {
                backup::restore_worktree(&branch, path.as_deref(), id.as_deref())
            }
        },

        Some(Commands::Stash { action }) => match action {
            StashAction::Save { message } => stash::stash_save(message.as_deref()),
            StashAction::List => stash::stash_list(),
            StashAction::Apply {
                target_branch,
                stash: stash_ref,
            } => stash::stash_apply(&target_branch, &stash_ref),
        },

        Some(Commands::Hook { action }) => match action {
            HookAction::Add {
                event,
                command,
                id,
                description,
            } => hooks::add_hook(&event, &command, id.as_deref(), description.as_deref()).map(
                |hook_id| {
                    println!("* Added hook '{}' for {}", hook_id, event);
                },
            ),
            HookAction::Remove { event, hook_id } => hooks::remove_hook(&event, &hook_id),
            HookAction::List { event } => {
                list_hooks(event.as_deref());
                Ok(())
            }
            HookAction::Enable { event, hook_id } => {
                hooks::set_hook_enabled(&event, &hook_id, true)
            }
            HookAction::Disable { event, hook_id } => {
                hooks::set_hook_enabled(&event, &hook_id, false)
            }
            HookAction::Run { event, dry_run } => run_hooks_manual(&event, dry_run),
        },

        Some(Commands::Export { output }) => config_ops::export_config(output.as_deref()),
        Some(Commands::Import { import_file, apply }) => {
            config_ops::import_config(&import_file, apply)
        }

        Some(Commands::Scan { dir }) => global_ops::global_scan(dir.as_deref()),
        Some(Commands::Prune) => global_ops::global_prune(),
        Some(Commands::Doctor {
            session_start,
            quiet,
        }) => diagnostics::doctor(session_start, quiet),
        Some(Commands::Guard { tool_input }) => guard::run(&tool_input),
        Some(Commands::SetupClaude) => setup_claude::setup_claude(),

        Some(Commands::Upgrade { yes }) => {
            update::upgrade(yes);
            Ok(())
        }

        Some(Commands::ShellSetup) => {
            shell_setup();
            Ok(())
        }

        Some(Commands::Path {
            branch,
            list_branches,
            interactive,
        }) => path_cmd::worktree_path(branch.as_deref(), cli.global, list_branches, interactive),

        Some(Commands::ShellFunction { shell }) => match shell_functions::generate(&shell) {
            Some(output) => {
                print!("{}", output);
                Ok(())
            }
            None => Err(CwError::Config(format!(
                "Unsupported shell: {}. Use bash, zsh, fish, or powershell.",
                shell
            ))),
        },

        Some(Commands::UpdateCache) => {
            update::refresh_cache();
            Ok(())
        }

        Some(Commands::ConfigKeys) => {
            for (key, _desc) in config::CONFIG_KEYS {
                println!("{}", key);
            }
            Ok(())
        }

        Some(Commands::TermValues) => {
            for v in constants::all_term_values() {
                println!("{}", v);
            }
            Ok(())
        }

        Some(Commands::PresetNames) => {
            for name in constants::PRESET_NAMES {
                println!("{}", name);
            }
            Ok(())
        }

        Some(Commands::HookEvents) => {
            for evt in constants::HOOK_EVENTS {
                println!("{}", evt);
            }
            Ok(())
        }

        Some(Commands::SpawnAi { spec }) => {
            // Pre-spawn failures (read/parse/chdir) exit 127 — the shell
            // "command not found / could not start" convention. Post-spawn
            // failures exit from inside `execute` directly, also with 127.
            // Inner errors already carry the "spawn-ai:" prefix via their
            // CwError::Other messages, so we print them verbatim.
            if let Err(e) = spawn_spec::execute(&spec) {
                eprintln!("{}", e);
                std::process::exit(127);
            }
            Ok(())
        }

        None => Ok(()),
    };

    if let Err(e) = result {
        // ExitCode carries a specific exit status from callers that have
        // already produced their own user-facing output (e.g. the multi-target
        // delete orchestrator). Exit silently with that code instead of the
        // generic "Error: …" print.
        if let CwError::ExitCode(code) = e {
            std::process::exit(code);
        }
        cwconsole::print_error(&format!("Error: {}", e));
        std::process::exit(1);
    }
}

fn resolve_lookup_mode(is_worktree: bool, is_branch: bool) -> Option<&'static str> {
    if is_worktree {
        Some("worktree")
    } else if is_branch {
        Some("branch")
    } else {
        None
    }
}

fn generate_completions(shell_name: &str) {
    use clap::CommandFactory;
    use clap_complete::{generate, Shell};

    let shell = match shell_name.to_lowercase().as_str() {
        "bash" => Shell::Bash,
        "zsh" => Shell::Zsh,
        "fish" => Shell::Fish,
        "powershell" | "pwsh" => Shell::PowerShell,
        "elvish" => Shell::Elvish,
        _ => {
            eprintln!(
                "Unsupported shell: {}. Use bash, zsh, fish, powershell, or elvish.",
                shell_name
            );
            std::process::exit(1);
        }
    };

    let mut cmd = Cli::command();
    generate(shell, &mut cmd, "gw", &mut std::io::stdout());
}

fn list_hooks(event: Option<&str>) {
    let events: Vec<&str> = if let Some(e) = event {
        vec![e]
    } else {
        hooks::HOOK_EVENTS.to_vec()
    };

    let mut has_any = false;
    for evt in &events {
        let hook_list = hooks::get_hooks(evt, None);
        if hook_list.is_empty() && event.is_none() {
            continue;
        }
        if !hook_list.is_empty() {
            has_any = true;
            println!("\n{}:", evt);
            for h in &hook_list {
                let status = if h.enabled { "enabled" } else { "disabled" };
                let desc = if h.description.is_empty() {
                    String::new()
                } else {
                    format!(" - {}", h.description)
                };
                println!("  {} [{}]: {}{}", h.id, status, h.command, desc);
            }
        } else {
            println!("\n{}:", evt);
            println!("  (no hooks)");
        }
    }

    if event.is_none() && !has_any {
        println!("No hooks configured. Use 'gw hook add' to add one.");
    }
}

fn run_hooks_manual(event: &str, dry_run: bool) -> Result<()> {
    let hook_list = hooks::get_hooks(event, None);
    if hook_list.is_empty() {
        println!("No hooks configured for {}", event);
        return Ok(());
    }

    let enabled: Vec<_> = hook_list.iter().filter(|h| h.enabled).collect();
    if enabled.is_empty() {
        println!("All hooks for {} are disabled", event);
        return Ok(());
    }

    if dry_run {
        println!("Would run {} hook(s) for {}:", enabled.len(), event);
        for h in &hook_list {
            let status = if h.enabled {
                "enabled"
            } else {
                "disabled (skipped)"
            };
            let desc = if h.description.is_empty() {
                String::new()
            } else {
                format!(" - {}", h.description)
            };
            println!("  {} [{}]: {}{}", h.id, status, h.command, desc);
        }
        return Ok(());
    }

    let cwd = std::env::current_dir().unwrap_or_default();
    let context = helpers::build_hook_context("", "", &cwd, &cwd, event, "manual");

    hooks::run_hooks(event, &context, Some(&cwd), None)?;
    Ok(())
}

fn shell_setup() {
    let shell_env = std::env::var("SHELL").unwrap_or_default();
    let is_powershell = cfg!(target_os = "windows") || std::env::var("PSModulePath").is_ok();

    let home = constants::home_dir_or_fallback();
    let (shell_name, profile_path) = if shell_env.contains("zsh") {
        ("zsh", Some(home.join(".zshrc")))
    } else if shell_env.contains("bash") {
        ("bash", Some(home.join(".bashrc")))
    } else if shell_env.contains("fish") {
        (
            "fish",
            Some(home.join(".config").join("fish").join("config.fish")),
        )
    } else if is_powershell {
        ("powershell", None::<std::path::PathBuf>)
    } else {
        println!("Could not detect your shell automatically.\n");
        println!("Please manually add the gw-cd function to your shell:\n");
        println!("  bash/zsh:    source <(gw _shell-function bash)");
        println!("  fish:        gw _shell-function fish | source");
        println!("  PowerShell:  gw _shell-function powershell | Out-String | Invoke-Expression");
        return;
    };

    println!("Detected shell: {}\n", shell_name);

    if shell_name == "powershell" {
        println!("To enable gw-cd in PowerShell, add the following to your $PROFILE:\n");
        println!("  gw _shell-function powershell | Out-String | Invoke-Expression\n");
        println!("To find your PowerShell profile location, run: $PROFILE");
        println!(
            "\nIf the profile file doesn't exist, create it with: New-Item -Path $PROFILE -ItemType File -Force"
        );
        return;
    }

    let shell_function_line = match shell_name {
        "fish" => "gw _shell-function fish | source".to_string(),
        _ => format!("source <(gw _shell-function {})", shell_name),
    };

    if let Some(ref path) = profile_path {
        if path.exists() {
            if let Ok(content) = std::fs::read_to_string(path) {
                if content.contains("gw _shell-function") || content.contains("gw-cd") {
                    println!(
                        "{}",
                        console::style("Shell integration is already installed.").green()
                    );
                    println!("  Found in: {}\n", path.display());

                    refresh_shell_cache(shell_name);

                    println!("\nRestart your shell or run: source {}", path.display());
                    return;
                }
            }
        }
    }

    println!("Setup shell integration?\n");
    println!(
        "This will add the following to {}:",
        profile_path
            .as_ref()
            .map(|p| p.display().to_string())
            .unwrap_or("your profile".to_string())
    );

    println!(
        "\n  # git-worktree-manager shell integration{}",
        if matches!(shell_name, "zsh" | "bash") {
            " (gw-cd + tab completion)"
        } else {
            ""
        }
    );
    println!("  {}\n", shell_function_line);

    print!("Add to your shell profile? [Y/n]: ");
    use std::io::Write;
    let _ = std::io::stdout().flush();

    let mut input = String::new();
    let _ = std::io::stdin().read_line(&mut input);
    let input = input.trim().to_lowercase();

    if !input.is_empty() && input != "y" && input != "yes" {
        println!("\nSetup cancelled.");
        return;
    }

    let Some(ref path) = profile_path else {
        return;
    };

    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }

    let comment_suffix = if matches!(shell_name, "zsh" | "bash") {
        " (gw-cd + tab completion)"
    } else {
        ""
    };
    let append = format!(
        "\n# git-worktree-manager shell integration{}\n{}\n",
        comment_suffix, shell_function_line
    );

    match std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
    {
        Ok(mut f) => {
            let _ = f.write_all(append.as_bytes());

            if let Ok(mut cfg) = config::load_config() {
                cfg.shell_completion.installed = true;
                cfg.shell_completion.prompted = true;
                let _ = config::save_config(&cfg);
            }

            println!("\n* Successfully added to {}", path.display());

            refresh_shell_cache(shell_name);

            println!("\nNext steps:");
            println!("  1. Restart your shell or run: source {}", path.display());
            println!("  2. Try directory navigation: gw-cd <branch-name>");
            println!("  3. Try tab completion: gw <TAB> or gw new <TAB>");
        }
        Err(e) => {
            println!("\nError: Failed to update {}: {}", path.display(), e);
            println!("\nTo install manually, add the lines shown above to your profile");
        }
    }
}

/// Refresh cached shell function files to pick up new features.
fn refresh_shell_cache(shell_name: &str) {
    let home = constants::home_dir_or_fallback();

    let cache_paths = [
        home.join(".cache").join("gw-shell-function.zsh"),
        home.join(".cache").join("gw-shell-function.bash"),
        home.join(".cache").join("gw-shell-function.fish"),
    ];

    let mut refreshed = false;
    for cache_path in &cache_paths {
        if !cache_path.exists() {
            continue;
        }
        let cache_shell = cache_path
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        if let Some(content) = shell_functions::generate(cache_shell) {
            if std::fs::write(cache_path, content).is_ok() {
                println!(
                    "  {} {}",
                    console::style("Refreshed cache:").dim(),
                    cache_path.display()
                );
                refreshed = true;
            }
        }
    }

    if refreshed {
        return;
    }

    let cache_path = home
        .join(".cache")
        .join(format!("gw-shell-function.{}", shell_name));
    if let Some(content) = shell_functions::generate(shell_name) {
        let _ = std::fs::create_dir_all(cache_path.parent().unwrap_or(&home));
        if std::fs::write(&cache_path, &content).is_ok() {
            println!(
                "  {} {}",
                console::style("Created cache:").dim(),
                cache_path.display()
            );
        }
    }
}