git-workty 0.3.3

Git worktrees as daily-driver workspaces
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
pub mod commands;
pub mod config;
pub mod gh;
pub mod git;
pub mod shell;
pub mod status;
pub mod ui;
pub mod worktree;

use clap::{Parser, Subcommand};
use clap_complete::Shell;
use std::path::PathBuf;

use crate::commands::{
    clean, completions, doctor, fetch, go, init, install_man, list, new, pick, pr, rm, sync,
};
use crate::git::GitRepo;
use crate::ui::UiOptions;

pub const ABOUT: &str = "Git worktrees as daily-driver workspaces

workty makes Git worktrees feel like workspaces/tabs. Switch context without
stashing or WIP commits, see everything in flight with a dashboard, and clean
up merged work safely.";

pub const AFTER_HELP: &str = "EXAMPLES:
    git workty                    Show dashboard of all worktrees
    git workty new feat/login     Create new workspace for feat/login
    git workty go feat/login      Print path to feat/login worktree
    git workty pick               Fuzzy select a worktree (interactive)
    git workty rm feat/login      Remove the feat/login worktree
    git workty clean --merged     Remove all merged worktrees

SHELL INTEGRATION:
    Add to your shell config:
        eval \"$(git workty init zsh)\"

    This provides:
        wcd   - fuzzy select and cd to a worktree
        wnew  - create new worktree and cd into it
        wgo   - go to a worktree by name";

#[derive(Parser)]
#[command(name = "git-workty", bin_name = "git workty")]
#[command(author, version, about = ABOUT, after_help = AFTER_HELP)]
#[command(propagate_version = true)]
pub struct Cli {
    /// Disable colored output
    #[arg(long, global = true, env = "NO_COLOR")]
    pub no_color: bool,

    /// Use ASCII-only symbols
    #[arg(long, global = true)]
    pub ascii: bool,

    /// Output in JSON format
    #[arg(long, global = true)]
    pub json: bool,

    /// Run as if started in <PATH>
    #[arg(short = 'C', global = true, value_name = "PATH")]
    pub directory: Option<PathBuf>,

    /// Assume yes to prompts
    #[arg(long, short = 'y', global = true)]
    pub yes: bool,

    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Show dashboard of all worktrees (default)
    #[command(visible_alias = "ls")]
    List {
        /// Skip dirty file check for faster output
        #[arg(long)]
        fast: bool,
    },

    /// Create a new workspace
    #[command(after_help = "EXAMPLES:
    git workty new feat/login
    git workty new hotfix --from main
    git workty new feature --no-fetch --no-push")]
    New {
        /// Branch name for the new workspace
        name: String,

        /// Base branch or commit to create from
        #[arg(long, short = 'f')]
        from: Option<String>,

        /// Custom path for the worktree
        #[arg(long, short = 'p')]
        path: Option<PathBuf>,

        /// Print only the created path to stdout
        #[arg(long)]
        print_path: bool,

        /// Open the worktree in configured editor
        #[arg(long, short = 'o')]
        open: bool,

        /// Skip fetching from remote before creating
        #[arg(long)]
        no_fetch: bool,

        /// Skip pushing to set upstream after creating
        #[arg(long)]
        no_push: bool,
    },

    /// Print path to a worktree by name
    #[command(after_help = "EXAMPLES:
    cd \"$(git workty go feat/login)\"
    git workty go main")]
    Go {
        /// Worktree name (branch name or directory name)
        name: String,
    },

    /// Interactively select a worktree (fuzzy finder)
    #[command(after_help = "EXAMPLES:
    cd \"$(git workty pick)\"")]
    Pick,

    /// Remove a workspace
    #[command(after_help = "EXAMPLES:
    git workty rm feat/login
    git workty rm feat/login --delete-branch
    git workty rm feat/login --force")]
    Rm {
        /// Worktree name to remove
        name: String,

        /// Remove even if worktree has uncommitted changes
        #[arg(long, short = 'f')]
        force: bool,

        /// Also delete the branch after removing worktree
        #[arg(long, short = 'd')]
        delete_branch: bool,
    },

    /// Remove merged or stale worktrees
    #[command(after_help = "EXAMPLES:
    git workty clean --merged --dry-run
    git workty clean --gone --yes
    git workty clean --stale 30")]
    Clean {
        /// Remove worktrees whose branch is merged into base
        #[arg(long)]
        merged: bool,

        /// Remove worktrees whose upstream branch was deleted
        #[arg(long)]
        gone: bool,

        /// Remove worktrees not touched in N days
        #[arg(long, value_name = "DAYS")]
        stale: Option<u32>,

        /// Show what would be removed without removing
        #[arg(long, short = 'n')]
        dry_run: bool,
    },

    /// Print shell integration script
    #[command(after_help = "EXAMPLES:
    eval \"$(git workty init zsh)\"
    git workty init bash >> ~/.bashrc")]
    Init {
        /// Shell to generate script for (bash, zsh, fish, powershell)
        shell: String,

        /// Generate git wrapper that auto-cds
        #[arg(long)]
        wrap_git: bool,

        /// Disable cd helpers (completions only)
        #[arg(long)]
        no_cd: bool,
    },

    /// Diagnose common issues
    Doctor,

    /// Generate shell completions
    #[command(after_help = "EXAMPLES:
    git workty completions zsh > _git-workty
    git workty completions bash > /etc/bash_completion.d/git-workty")]
    Completions {
        /// Shell to generate completions for
        shell: Shell,
    },

    /// Create a worktree for a GitHub PR (requires gh CLI)
    #[command(after_help = "EXAMPLES:
    git workty pr 123
    cd \"$(git workty pr 123 --print-path)\"")]
    Pr {
        /// PR number
        number: u32,

        /// Print only the created path to stdout
        #[arg(long)]
        print_path: bool,

        /// Open the worktree in configured editor
        #[arg(long, short = 'o')]
        open: bool,
    },

    /// Fetch from remotes (updates tracking info for all worktrees)
    #[command(after_help = "EXAMPLES:
    git workty fetch
    git workty fetch --all")]
    Fetch {
        /// Fetch from all remotes, not just origin
        #[arg(long, short = 'a')]
        all: bool,
    },

    /// Rebase all clean worktrees onto their upstream
    #[command(after_help = "EXAMPLES:
    git workty sync --dry-run
    git workty sync --fetch")]
    Sync {
        /// Show what would be done without doing it
        #[arg(long, short = 'n')]
        dry_run: bool,

        /// Fetch from origin before syncing
        #[arg(long, short = 'f')]
        fetch: bool,
    },

    /// Install manpage to ~/.local/share/man/man1
    InstallMan,
}

pub fn run_cli() {
    let cli = Cli::parse();

    let ui_opts = UiOptions {
        color: !cli.no_color && supports_color(),
        ascii: cli.ascii,
        json: cli.json,
    };

    let result = run(cli, &ui_opts);

    if let Err(e) = result {
        ui::print_error(&format!("{:#}", e), None);
        std::process::exit(1);
    }
}

fn run(cli: Cli, ui_opts: &UiOptions) -> anyhow::Result<()> {
    let start_path = cli.directory.as_deref();

    match cli.command {
        None => {
            let repo = GitRepo::discover(start_path)?;
            list::execute(&repo, ui_opts, false)
        }

        Some(Commands::List { fast }) => {
            let repo = GitRepo::discover(start_path)?;
            list::execute(&repo, ui_opts, fast)
        }

        Some(Commands::New {
            name,
            from,
            path,
            print_path,
            open,
            no_fetch,
            no_push,
        }) => {
            let repo = GitRepo::discover(start_path)?;
            new::execute(
                &repo,
                new::NewOptions {
                    name,
                    from,
                    path,
                    print_path,
                    open,
                    no_fetch,
                    no_push,
                },
            )
        }

        Some(Commands::Go { name }) => {
            let repo = GitRepo::discover(start_path)?;
            go::execute(&repo, &name)
        }

        Some(Commands::Pick) => {
            let repo = GitRepo::discover(start_path)?;
            pick::execute(&repo, ui_opts)
        }

        Some(Commands::Rm {
            name,
            force,
            delete_branch,
        }) => {
            let repo = GitRepo::discover(start_path)?;
            rm::execute(
                &repo,
                rm::RmOptions {
                    name,
                    force,
                    delete_branch,
                    yes: cli.yes,
                },
            )
        }

        Some(Commands::Clean {
            merged,
            gone,
            stale,
            dry_run,
        }) => {
            let repo = GitRepo::discover(start_path)?;
            clean::execute(
                &repo,
                clean::CleanOptions {
                    merged,
                    gone,
                    stale_days: stale,
                    dry_run,
                    yes: cli.yes,
                },
            )
        }

        Some(Commands::Init {
            shell,
            wrap_git,
            no_cd,
        }) => {
            init::execute(init::InitOptions {
                shell,
                wrap_git,
                no_cd,
            });
            Ok(())
        }

        Some(Commands::Doctor) => {
            doctor::execute(start_path);
            Ok(())
        }

        Some(Commands::Completions { shell }) => {
            completions::execute::<Cli>(shell);
            Ok(())
        }

        Some(Commands::Pr {
            number,
            print_path,
            open,
        }) => {
            let repo = GitRepo::discover(start_path)?;
            pr::execute(
                &repo,
                pr::PrOptions {
                    number,
                    print_path,
                    open,
                },
            )
        }

        Some(Commands::Fetch { all }) => {
            let repo = GitRepo::discover(start_path)?;
            fetch::execute(&repo, all)
        }

        Some(Commands::Sync { dry_run, fetch }) => {
            let repo = GitRepo::discover(start_path)?;
            sync::execute(&repo, sync::SyncOptions { dry_run, fetch })
        }

        Some(Commands::InstallMan) => install_man::execute(cli.yes),
    }
}

fn supports_color() -> bool {
    use is_terminal::IsTerminal;

    if std::env::var("NO_COLOR").is_ok() {
        return false;
    }

    std::io::stdout().is_terminal()
}