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
//! What:
//! The one fact the wrapper needs about `git worktree`:
//! whether the invocation
//! creates or moves a worktree.
//! Why:
//! Only `git worktree add` and `git worktree move` are sources of ignored-state
//! synchronization,
//! and the worktree-copy opt-out is written after that second word.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // changesWorktreeRegistrations(['add', '../topic']) === true
//! ```
/// What:
/// `OsString` is owned operating-system text of raw bytes.
/// Sibling the reader might
/// expect:
/// `String`,
/// which must be valid UTF-8.
/// Why:
/// The region is compared as bytes and never decoded.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // string, but byte-preserving.
/// ```
use std::ffi::OsString;
/// What:
/// The `git worktree` subcommand words that register or re-register a worktree path.
/// `&[&[u8]]` is a borrowed list of byte spellings baked into the program.
/// Why:
/// Git 2.56.0 `cmd_worktree` declares only subcommands and no option of its own
/// (builtin/worktree.c),
/// so the word directly after `worktree` selects the action
/// and is never abbreviated.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const REGISTRATION_SUBCOMMANDS = ['add', 'move'];
/// ```
const REGISTRATION_SUBCOMMANDS: &[&[u8]] = &[b"add", b"move"];
/// What:
/// Whether the tokens after `worktree` start with `add` or `move`.
/// `&[OsString]`
/// borrows those tokens;
/// `bool` is true or false.
/// Why:
/// The caller decides from this alone,
/// without reading the subcommand's options,
/// so
/// a mistaken option table can never hide a worktree creation.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function changesWorktreeRegistrations(region: string[]): boolean;
/// ```
pub fn changes_worktree_registrations(region: &[OsString]) -> bool {
// `let Some(x) = ... else { ... };` unwraps the first token or returns for an empty region.
let Some(first) = region.first() else {
return false;
};
// `.as_encoded_bytes()` lends the raw bytes; `.contains(&x)` borrows them for the lookup.
return REGISTRATION_SUBCOMMANDS.contains(&first.as_encoded_bytes());
}
/// Word-position cases and a real Git 2.56.0 control stay out of the release executable.
#[cfg(test)]
#[path = "command_worktree_tests.rs"]
mod tests;