git-sprout 0.1.0

A drop-in git worktree add that materialises the tree with filesystem copy-on-write clones
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
// ABOUTME: Parses the `git worktree add` command line and git's own global options.
// ABOUTME: Anything it does not fully understand becomes a delegation, never an error.

use std::ffi::{OsStr, OsString};

/// Git's global options that take a separate value argument.
const GLOBAL_WITH_VALUE: &[&str] = &["-C", "-c", "--git-dir", "--work-tree", "--namespace"];

/// Git's global options that stand alone.
const GLOBAL_STANDALONE: &[&str] = &[
    "-p",
    "--paginate",
    "-P",
    "--no-pager",
    "--bare",
    "--no-replace-objects",
    "--literal-pathspecs",
    "--glob-pathspecs",
    "--noglob-pathspecs",
    "--icase-pathspecs",
    "--no-optional-locks",
];

/// `git worktree add` long options that stand alone.
const ADD_STANDALONE: &[&str] = &[
    "--force",
    "--no-force",
    "--detach",
    "--no-detach",
    "--checkout",
    "--no-checkout",
    "--orphan",
    "--no-orphan",
    "--lock",
    "--no-lock",
    "--no-reason",
    "--quiet",
    "--no-quiet",
    "--track",
    "--no-track",
    "--guess-remote",
    "--no-guess-remote",
    "--relative-paths",
    "--no-relative-paths",
];

/// `git worktree add` long options that take a value.
const ADD_WITH_VALUE: &[&str] = &["--reason"];

/// A `git worktree add` invocation parsed well enough to consider accelerating.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddCommand {
    /// Git's global options, in order, to place before the `worktree` subcommand.
    pub globals: Vec<OsString>,
    /// The arguments for `git worktree add`, with sprout's own flags removed.
    pub passthrough: Vec<OsString>,
    /// Position of `--` within `passthrough`, if the user wrote one.
    pub double_dash: Option<usize>,
    /// The destination worktree path.
    pub path: OsString,
    /// The commit-ish to check out, when the user named one.
    pub commit_ish: Option<OsString>,
    pub quiet: bool,
    /// False when `--no-checkout` was given.
    pub checkout: bool,
    pub orphan: bool,
    /// True when the user asked for the plain `git worktree add` path.
    pub no_cow: bool,
}

impl AddCommand {
    /// The argument list that reproduces this request through `git` itself.
    pub fn git_args(&self) -> Vec<OsString> {
        let mut args = self.globals.clone();
        args.push(OsString::from("worktree"));
        args.push(OsString::from("add"));
        args.extend(self.passthrough.iter().cloned());
        args
    }

    /// The subcommand arguments for step 2, which creates the worktree without files.
    /// The globals are left out; whoever runs git puts them in front.
    pub fn worktree_add_args_no_checkout(&self) -> Vec<OsString> {
        let mut args = vec![OsString::from("worktree"), OsString::from("add")];
        let insert_at = self.double_dash.unwrap_or(self.passthrough.len());
        args.extend(self.passthrough[..insert_at].iter().cloned());
        args.push(OsString::from("--no-checkout"));
        args.extend(self.passthrough[insert_at..].iter().cloned());
        args
    }
}

/// What the command line asks the tool to do.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Invocation {
    /// Understood; acceleration may be attempted.
    Add(Box<AddCommand>),
    /// Not understood. Run `git` with these arguments and exit with its status.
    Delegate {
        git_args: Vec<OsString>,
        reason: &'static str,
    },
    /// Report the tool's own version.
    Version,
}

fn delegate(args: &[OsString], reason: &'static str) -> Invocation {
    let mut git_args = vec![OsString::from("worktree")];
    git_args.extend(args.iter().cloned());
    Invocation::Delegate { git_args, reason }
}

/// Splits `--name=value` into its two halves.
fn split_assignment(token: &str) -> Option<(&str, &str)> {
    token
        .strip_prefix("--")
        .and_then(|rest| rest.split_once('='))
        .map(|(name, value)| (&token[..name.len() + 2], value))
}

/// Consumes git's global options from the front of `args`, returning them and the rest.
fn take_globals(args: &[OsString]) -> Option<(Vec<OsString>, &[OsString])> {
    let mut globals = Vec::new();
    let mut rest = args;
    while let Some(first) = rest.first() {
        let Some(token) = first.to_str() else { break };
        if !token.starts_with('-') {
            break;
        }
        if GLOBAL_STANDALONE.contains(&token) {
            globals.push(first.clone());
            rest = &rest[1..];
        } else if GLOBAL_WITH_VALUE.contains(&token) {
            let value = rest.get(1)?;
            globals.push(first.clone());
            globals.push(value.clone());
            rest = &rest[2..];
        } else if split_assignment(token).is_some_and(|(name, _)| GLOBAL_WITH_VALUE.contains(&name))
        {
            globals.push(first.clone());
            rest = &rest[1..];
        } else {
            return None;
        }
    }
    Some((globals, rest))
}

/// Parses the argument tail the binary was invoked with.
pub fn parse(args: &[OsString]) -> Invocation {
    if args.len() == 1 && (args[0] == "--version" || args[0] == "-V") {
        return Invocation::Version;
    }

    let Some((globals, rest)) = take_globals(args) else {
        return delegate(args, "unrecognised git global option");
    };

    match rest.first().and_then(|arg| arg.to_str()) {
        Some("add") => {}
        _ => return delegate(args, "not a `worktree add` invocation"),
    }

    match parse_add(globals, &rest[1..]) {
        Ok(add) => Invocation::Add(Box::new(add)),
        Err(reason) => delegate(args, reason),
    }
}

/// The state built up while walking the `add` arguments.
struct AddParse {
    passthrough: Vec<OsString>,
    double_dash: Option<usize>,
    positionals: Vec<OsString>,
    quiet: bool,
    checkout: bool,
    orphan: bool,
    no_cow: bool,
}

fn parse_add(globals: Vec<OsString>, args: &[OsString]) -> Result<AddCommand, &'static str> {
    let mut state = AddParse {
        passthrough: Vec::new(),
        double_dash: None,
        positionals: Vec::new(),
        quiet: false,
        checkout: true,
        orphan: false,
        no_cow: false,
    };

    let mut index = 0;
    while index < args.len() {
        let arg = &args[index];
        index += 1;

        if state.double_dash.is_some() {
            state.positionals.push(arg.clone());
            state.passthrough.push(arg.clone());
            continue;
        }

        let Some(token) = arg.to_str() else {
            state.positionals.push(arg.clone());
            state.passthrough.push(arg.clone());
            continue;
        };

        if token == "--" {
            state.double_dash = Some(state.passthrough.len());
            state.passthrough.push(arg.clone());
            continue;
        }

        if token == "--no-cow" {
            state.no_cow = true;
            continue;
        }

        if token.starts_with("--") {
            parse_long(&mut state, token, args, &mut index)?;
            continue;
        }

        if token.len() > 1 && token.starts_with('-') {
            parse_shorts(&mut state, arg, token, args, &mut index)?;
            continue;
        }

        state.positionals.push(arg.clone());
        state.passthrough.push(arg.clone());
    }

    if state.positionals.is_empty() {
        return Err("no worktree path given");
    }
    if state.positionals.len() > 2 {
        return Err("more positional arguments than `git worktree add` takes");
    }

    Ok(AddCommand {
        globals,
        passthrough: state.passthrough,
        double_dash: state.double_dash,
        path: state.positionals[0].clone(),
        commit_ish: state.positionals.get(1).cloned(),
        quiet: state.quiet,
        checkout: state.checkout,
        orphan: state.orphan,
        no_cow: state.no_cow,
    })
}

fn parse_long(
    state: &mut AddParse,
    token: &str,
    args: &[OsString],
    index: &mut usize,
) -> Result<(), &'static str> {
    if ADD_STANDALONE.contains(&token) {
        match token {
            "--quiet" => state.quiet = true,
            "--no-quiet" => state.quiet = false,
            "--no-checkout" => state.checkout = false,
            "--checkout" => state.checkout = true,
            "--orphan" => state.orphan = true,
            "--no-orphan" => state.orphan = false,
            _ => {}
        }
        state.passthrough.push(OsString::from(token));
        return Ok(());
    }

    if ADD_WITH_VALUE.contains(&token) {
        let value = args.get(*index).ok_or("long option is missing its value")?;
        *index += 1;
        state.passthrough.push(OsString::from(token));
        state.passthrough.push(value.clone());
        return Ok(());
    }

    if let Some((name, _)) = split_assignment(token) {
        if ADD_WITH_VALUE.contains(&name) {
            state.passthrough.push(OsString::from(token));
            return Ok(());
        }
    }

    Err("unrecognised `git worktree add` option")
}

fn parse_shorts(
    state: &mut AddParse,
    arg: &OsStr,
    token: &str,
    args: &[OsString],
    index: &mut usize,
) -> Result<(), &'static str> {
    for (offset, flag) in token[1..].char_indices() {
        match flag {
            'f' => {}
            'd' => {}
            'q' => state.quiet = true,
            'b' | 'B' => {
                let sticky = &token[1 + offset + flag.len_utf8()..];
                if sticky.is_empty() {
                    let value = args
                        .get(*index)
                        .ok_or("short option is missing its value")?;
                    *index += 1;
                    state.passthrough.push(arg.to_os_string());
                    state.passthrough.push(value.clone());
                } else {
                    state.passthrough.push(arg.to_os_string());
                }
                return Ok(());
            }
            _ => return Err("unrecognised `git worktree add` option"),
        }
    }
    state.passthrough.push(arg.to_os_string());
    Ok(())
}

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

    fn argv(tokens: &[&str]) -> Vec<OsString> {
        tokens.iter().map(OsString::from).collect()
    }

    fn strings(tokens: &[OsString]) -> Vec<String> {
        tokens
            .iter()
            .map(|token| token.to_string_lossy().into_owned())
            .collect()
    }

    fn add_of(tokens: &[&str]) -> AddCommand {
        match parse(&argv(tokens)) {
            Invocation::Add(add) => *add,
            other => panic!("expected an add invocation, got {other:?}"),
        }
    }

    fn delegation_reason(tokens: &[&str]) -> &'static str {
        match parse(&argv(tokens)) {
            Invocation::Delegate { reason, .. } => reason,
            other => panic!("expected a delegation, got {other:?}"),
        }
    }

    #[test]
    fn takes_the_path_and_commit_ish() {
        let add = add_of(&["add", "../wt", "v1.0"]);
        assert_eq!(add.path, OsString::from("../wt"));
        assert_eq!(add.commit_ish, Some(OsString::from("v1.0")));
        assert!(add.checkout);
        assert!(!add.quiet);
    }

    #[test]
    fn keeps_git_options_in_the_passthrough() {
        let add = add_of(&["add", "-b", "feature", "../wt"]);
        assert_eq!(strings(&add.passthrough), ["-b", "feature", "../wt"]);
        assert_eq!(
            strings(&add.git_args()),
            ["worktree", "add", "-b", "feature", "../wt"]
        );
    }

    #[test]
    fn removes_its_own_flag_from_the_passthrough() {
        let add = add_of(&["add", "--no-cow", "../wt"]);
        assert!(add.no_cow);
        assert_eq!(strings(&add.passthrough), ["../wt"]);
    }

    #[test]
    fn understands_bundled_and_sticky_short_options() {
        let add = add_of(&["add", "-fq", "-bfeature", "../wt"]);
        assert!(add.quiet);
        assert_eq!(strings(&add.passthrough), ["-fq", "-bfeature", "../wt"]);
    }

    #[test]
    fn understands_a_long_option_with_an_attached_value() {
        let add = add_of(&["add", "--lock", "--reason=busy", "../wt"]);
        assert_eq!(
            strings(&add.passthrough),
            ["--lock", "--reason=busy", "../wt"]
        );
    }

    #[test]
    fn understands_a_long_option_with_a_separate_value() {
        let add = add_of(&["add", "--lock", "--reason", "busy", "../wt"]);
        assert_eq!(
            strings(&add.passthrough),
            ["--lock", "--reason", "busy", "../wt"]
        );
    }

    #[test]
    fn records_no_checkout_and_orphan() {
        assert!(!add_of(&["add", "--no-checkout", "../wt"]).checkout);
        assert!(add_of(&["add", "--orphan", "../wt"]).orphan);
    }

    #[test]
    fn a_later_checkout_flag_wins() {
        assert!(add_of(&["add", "--no-checkout", "--checkout", "../wt"]).checkout);
        assert!(!add_of(&["add", "--checkout", "--no-checkout", "../wt"]).checkout);
    }

    #[test]
    fn inserts_no_checkout_before_a_double_dash() {
        let add = add_of(&["add", "--", "../wt"]);
        assert_eq!(
            strings(&add.worktree_add_args_no_checkout()),
            ["worktree", "add", "--no-checkout", "--", "../wt"]
        );
    }

    #[test]
    fn appends_no_checkout_when_there_is_no_double_dash() {
        let add = add_of(&["add", "-b", "feature", "../wt"]);
        assert_eq!(
            strings(&add.worktree_add_args_no_checkout()),
            ["worktree", "add", "-b", "feature", "../wt", "--no-checkout"]
        );
    }

    #[test]
    fn carries_git_global_options_ahead_of_the_subcommand() {
        let add = add_of(&["-C", "/repo", "-c", "core.bare=false", "add", "../wt"]);
        assert_eq!(
            strings(&add.git_args()),
            [
                "-C",
                "/repo",
                "-c",
                "core.bare=false",
                "worktree",
                "add",
                "../wt"
            ]
        );
    }

    #[test]
    fn an_unknown_option_is_a_delegation_not_an_error() {
        assert_eq!(
            delegation_reason(&["add", "--tomorrows-flag", "../wt"]),
            "unrecognised `git worktree add` option"
        );
        assert_eq!(
            delegation_reason(&["add", "-Z", "../wt"]),
            "unrecognised `git worktree add` option"
        );
        assert_eq!(
            delegation_reason(&["--tomorrows-global", "add", "../wt"]),
            "unrecognised git global option"
        );
    }

    #[test]
    fn an_abbreviated_option_is_a_delegation() {
        assert_eq!(
            delegation_reason(&["add", "--deta", "../wt"]),
            "unrecognised `git worktree add` option"
        );
    }

    #[test]
    fn a_delegation_reproduces_the_original_argv() {
        match parse(&argv(&["add", "--tomorrows-flag", "../wt"])) {
            Invocation::Delegate { git_args, .. } => assert_eq!(
                strings(&git_args),
                ["worktree", "add", "--tomorrows-flag", "../wt"]
            ),
            other => panic!("expected a delegation, got {other:?}"),
        }
    }

    #[test]
    fn other_subcommands_go_straight_to_git() {
        assert_eq!(
            delegation_reason(&["list"]),
            "not a `worktree add` invocation"
        );
    }

    #[test]
    fn a_missing_or_extra_path_is_a_delegation() {
        assert_eq!(delegation_reason(&["add"]), "no worktree path given");
        assert_eq!(
            delegation_reason(&["add", "a", "b", "c"]),
            "more positional arguments than `git worktree add` takes"
        );
    }

    #[test]
    fn reports_its_version() {
        assert_eq!(parse(&argv(&["--version"])), Invocation::Version);
    }
}