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
//! What:
//! The complete `git add` option table of Git 2.56.0 and the bulk-staging facts read
//! from it.
//! Why:
//! The add-explicit policy rejects staging that names no explicit path.
//! Git accepts
//! `-A` inside any cluster (`-vA`),
//! by abbreviation (`--al`) and through
//! `--no-ignore-removal`;
//! a list of exact tokens misses those.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // parseAddRegion(postSubcommandArgs): AddRegion (throws OptionError)
//! ```
/// What:
/// Bring the tokenizer,
/// its table builder and its questions into this file.
/// Why:
/// This module only declares Git's table and interprets what the tokenizer found.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// import { parseOptions, row } from './command_options.ts';
/// ```
use super::command_options::{
Arity, DEFAULT_MODE, Occurrence, OptionError, OptionSpec, ParsedOptions, UNREAD, parse_options,
row,
};
use super::command_options_query::{
WrapperFlags, is_enabled, last_occurrence, positional_tokens, split_wrapper_flags,
};
/// `OsString` is owned operating-system text of raw bytes (sibling `String` must be UTF-8).
use std::ffi::OsString;
/// Wrapper-only flag that suppresses bulk-add enforcement for one invocation.
pub const ADD_ESCAPE_HATCH: &str = "--no-enforce-bulk-add";
/// `-A`,
/// `--all`.
pub const ALL: u16 = 1;
/// `--ignore-removal`,
/// the same variable as `--no-all` (builtin/add.c:252-259).
pub const IGNORE_REMOVAL: u16 = 2;
/// `-u`,
/// `--update`.
pub const UPDATE: u16 = 3;
/// `--resolved`:
/// add every conflict-resolved tracked file.
pub const RESOLVED: u16 = 4;
/// What:
/// Rows of `builtin_add_options` (builtin/add.c:261-292),
/// in source order.
/// `&[OptionSpec]` is a borrowed table baked into the program.
/// Why:
/// Only `--chmod`,
/// `--pathspec-from-file`,
/// `-U` and `--inter-hunk-context` take a
/// value;
/// every other token after an option is a pathspec.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// export const ADD_TABLE = [row(UNREAD, 'n', 'dry-run', 'none', true), /* ... */] as const;
/// ```
pub const ADD_TABLE: &[OptionSpec] = &[
row(UNREAD, Some(b'n'), Some("dry-run"), Arity::None, true),
row(UNREAD, Some(b'v'), Some("verbose"), Arity::None, true),
row(UNREAD, Some(b'i'), Some("interactive"), Arity::None, true),
row(UNREAD, Some(b'p'), Some("patch"), Arity::None, true),
row(UNREAD, None, Some("auto-advance"), Arity::None, true),
row(UNREAD, Some(b'U'), Some("unified"), Arity::Required, false),
row(
UNREAD,
None,
Some("inter-hunk-context"),
Arity::Required,
false,
),
row(UNREAD, Some(b'e'), Some("edit"), Arity::None, true),
row(UNREAD, Some(b'f'), Some("force"), Arity::None, true),
row(UPDATE, Some(b'u'), Some("update"), Arity::None, true),
row(UNREAD, None, Some("renormalize"), Arity::None, true),
row(RESOLVED, None, Some("resolved"), Arity::None, true),
row(UNREAD, Some(b'N'), Some("intent-to-add"), Arity::None, true),
row(ALL, Some(b'A'), Some("all"), Arity::None, true),
row(
IGNORE_REMOVAL,
None,
Some("ignore-removal"),
Arity::None,
true,
),
row(UNREAD, None, Some("refresh"), Arity::None, true),
row(UNREAD, None, Some("ignore-errors"), Arity::None, true),
row(UNREAD, None, Some("ignore-missing"), Arity::None, true),
row(UNREAD, None, Some("sparse"), Arity::None, true),
row(UNREAD, None, Some("chmod"), Arity::Required, true),
row(UNREAD, None, Some("warn-embedded-repo"), Arity::None, true),
row(
UNREAD,
None,
Some("pathspec-from-file"),
Arity::Required,
true,
),
row(UNREAD, None, Some("pathspec-file-nul"), Arity::None, true),
];
/// Pathspecs that match every changed path under the working directory or repository.
const BULK_PATHSPECS: &[&[u8]] = &[b".", b"./", b"*", b":/"];
/// What:
/// Why a token stages in bulk.
/// An `enum` is a closed set of named alternatives.
/// Why:
/// The policy names the matched tokens;
/// the kind says which Git meaning matched.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type BulkKind = 'all-flag' | 'update-flag' | 'pathspec';
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BulkKind {
/// `-A`/`--all` is on after every option was applied.
AllFlag,
/// `-u`/`--update` is on after every option was applied.
UpdateFlag,
/// A positional pathspec equal to `.`,
/// `./`,
/// `*` or `:/`.
Pathspec,
}
/// What:
/// One bulk-staging match.
/// `usize` is the list index type.
/// Why:
/// The token index lets the diagnostic quote the caller's exact spelling.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type BulkMatch = { kind: BulkKind; token: number };
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BulkMatch {
/// Which Git meaning matched.
pub kind: BulkKind,
/// Region token index of the token that turned the meaning on.
pub token: usize,
}
/// What:
/// Facts of one `git add` region.
/// `Vec<BulkMatch>` is an owned list in argument order.
/// Why:
/// The add-explicit policy rejects when the list is non-empty and no hatch is given.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type AddRegion = { bulkMatches: string[]; hasEscapeHatch: boolean };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AddRegion {
/// Bulk-staging matches in argument order.
pub bulk_matches: Vec<BulkMatch>,
/// `--resolved` is on.
/// Not a bulk match in the shipped policy;
/// reported for its owner.
pub resolved: bool,
/// Wrapper-only flags in option position;
/// `escape` is `--no-enforce-bulk-add`.
pub wrapper: WrapperFlags,
}
/// What:
/// The token that left `-A` on,
/// or nothing.
/// `Option<usize>` is "an index or nothing".
/// Why:
/// `--all` and `--ignore-removal` write one variable in opposite senses,
/// so they are
/// applied together in argument order (`ignore_removal_cb`,
/// add.c:252-259).
///
/// In TS you'd write (pseudocode):
/// ```ts
/// let on; for (const o of occurrences) { if (o.id === ALL) on = o.negated ? undefined : o.token; /* ... */ }
/// ```
fn all_token(parsed: &ParsedOptions) -> Option<usize> {
let mut on: Option<usize> = None;
// `for occurrence in &parsed.occurrences` borrows each record in the order Git applied it.
for occurrence in &parsed.occurrences {
// `--all` turns it on; `--no-all` turns it off.
let all_sets: bool = occurrence.id == ALL && !occurrence.negated;
// `--no-ignore-removal` turns it on; `--ignore-removal` turns it off.
let removal_sets: bool = occurrence.id == IGNORE_REMOVAL && occurrence.negated;
if all_sets || removal_sets {
// `Some(x)` is the "present" case of `Option`.
on = Some(occurrence.token);
} else if occurrence.id == ALL || occurrence.id == IGNORE_REMOVAL {
// `None` is the "absent" case of `Option`.
on = None;
}
}
return on;
}
/// What:
/// The token that left `-u` on,
/// or nothing.
/// Why:
/// `--update --no-update` leaves it off,
/// as Git's `OPT_BOOL` does.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const token = last !== undefined && !last.negated ? last.token : undefined;
/// ```
fn update_token(parsed: &ParsedOptions) -> Option<usize> {
let last: Option<Occurrence> = last_occurrence(parsed, UPDATE);
// `if let Some(found) = last` runs only when the option was written.
if let Some(found) = last {
if found.negated {
return None;
}
return Some(found.token);
}
return None;
}
/// What:
/// The sort key of a match:
/// its token index.
/// `&BulkMatch` borrows one match.
/// Why:
/// Matches are reported in the order the caller wrote them.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// matches.sort((a, b) => a.token - b.token);
/// ```
fn match_token(found: &BulkMatch) -> usize {
return found.token;
}
/// What:
/// Parse the region after `add`.
/// `Result<A, B>` is "either success `A` or failure `B`".
/// Why:
/// A region Git itself would refuse yields the refusal,
/// never a guessed fact set.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseAddRegion(region: string[], wrapperFlags: string[]): AddRegion; // throws
/// ```
pub fn parse_add_region(
region: &[OsString],
wrapper_flags: &[&[u8]],
) -> Result<AddRegion, OptionError> {
// The command's own escape hatch is flag 0; the caller's flags follow.
let mut flags: Vec<&[u8]> = Vec::<&[u8]>::with_capacity(wrapper_flags.len() + 1);
flags.push(ADD_ESCAPE_HATCH.as_bytes());
for flag in wrapper_flags {
// `*flag` reads the `&[u8]` the loop variable points at.
flags.push(*flag);
}
// `?` returns Git's refusal to our caller, or unwraps the tokenized region.
let parsed: ParsedOptions = parse_options(region, ADD_TABLE, DEFAULT_MODE, flags.as_slice())?;
let mut bulk_matches: Vec<BulkMatch> = Vec::<BulkMatch>::new();
// `if let Some(token) = ...` runs only when `-A` is on.
if let Some(token) = all_token(&parsed) {
bulk_matches.push(BulkMatch {
kind: BulkKind::AllFlag,
token,
});
}
if let Some(token) = update_token(&parsed) {
bulk_matches.push(BulkMatch {
kind: BulkKind::UpdateFlag,
token,
});
}
for token in positional_tokens(&parsed, region.len()) {
// `.as_encoded_bytes()` lends the raw bytes; nothing is decoded or copied.
if BULK_PATHSPECS.contains(®ion[token].as_encoded_bytes()) {
bulk_matches.push(BulkMatch {
kind: BulkKind::Pathspec,
token,
});
}
}
// `sort_by_key` orders the list by the value `match_token` returns for each item.
bulk_matches.sort_by_key(match_token);
// `Ok(x)` is the success case of `Result`.
return Ok(AddRegion {
bulk_matches,
resolved: is_enabled(&parsed, RESOLVED),
wrapper: split_wrapper_flags(parsed.wrapper.as_slice()),
});
}
/// Bulk matches in every accepted spelling,
/// and real-Git controls of the table.
#[cfg(test)]
#[path = "command_add_tests.rs"]
mod tests;