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
//! What:
//! The grammar of `git cli-git <command>`,
//! the wrapper's own management namespace.
//! Why:
//! `check` and `fix` run policies over an explicit scope;
//! `trust`,
//! `untrust` and
//! `status` are retired with executable configuration and only explain that.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // parseManagementArgs(['check', '--all']) => { command: 'check', all: true, policies: [], pathspecs: [] }
//! ```
/// What:
/// `OsString` is owned operating-system text of raw OS bytes (sibling `String`
/// must be UTF-8).
/// Why:
/// Pathspecs are file names and are kept as the exact bytes the caller gave.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // string[] argv, but byte-preserving.
/// ```
use std::ffi::OsString;
/// What:
/// The complete grammar shown when an invocation is refused.
/// `&str` is borrowed text compiled into the executable.
/// Why:
/// The text states the enforced rule:
/// exactly one scope per direct command.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const MANAGEMENT_USAGE = 'Usage: git cli-git check ...';
/// ```
pub const MANAGEMENT_USAGE: &str = "\
Usage: git cli-git check (--all | -- <pathspec>...) [--policy <id>]...
git cli-git fix (--all | -- <pathspec>...) [--policy <id>]...
git cli-git --help
";
/// Successful namespace help,
/// shown without touching any repository.
pub const MANAGEMENT_HELP: &str = "\
Usage: git cli-git <command> [options]
Run repository policies directly.
Commands:
check Check policies over an explicit scope.
fix Apply policy fixes over an explicit scope.
Scope (exactly one):
--all The whole repository.
-- <pathspec>... The named pathspecs.
Options:
--policy <id> Run only this policy; may be repeated.
--help Show this help.
Policies are configured as data in cli-git.config.jsonc at the repository top level.
The trust, untrust and status commands are retired: configuration is no longer code,
so there is nothing to approve.
";
/// What:
/// The three commands that existed only to approve executable configuration.
/// `#[derive(...)]` generates copying,
/// debug printing and `==`.
/// Why:
/// Each still parses,
/// so old instructions get an explanation instead of a usage error.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type RetiredCommand = 'trust' | 'untrust' | 'status';
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RetiredCommand {
/// `git cli-git trust [--yes]`.
Trust,
/// `git cli-git untrust`.
Untrust,
/// `git cli-git status`.
Status,
}
/// What:
/// One understood management invocation.
/// `Vec<String>` is an owned list of owned UTF-8 text;
/// `Vec<OsString>` keeps raw bytes.
/// Why:
/// The runner acts on a typed value;
/// nothing downstream re-reads raw arguments.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type ManagementAction = { command: 'help' } | { command: 'retired'; ... } | { command: 'check' | 'fix'; ... };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ManagementAction {
/// `--help` or `-h`:
/// print the namespace help.
Help,
/// A retired trust command;
/// `help` is true for `trust --help`.
Retired {
/// Which retired command was named.
command: RetiredCommand,
/// Whether help was requested,
/// which selects standard output.
help: bool,
},
/// `check` or `fix` over exactly one scope.
Direct {
/// `true` for `fix`,
/// `false` for `check`.
fix: bool,
/// Whether the whole repository was selected with `--all`.
all: bool,
/// Selected policy IDs,
/// first occurrence order,
/// without repeats;
/// not yet validated.
policies: Vec<String>,
/// Pathspecs after `--`,
/// in order,
/// as raw bytes.
pathspecs: Vec<OsString>,
},
}
/// What:
/// Why a management invocation was refused.
/// Why:
/// Each refusal has its own remedy,
/// so each has its own message.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type ManagementRefusal = 'usage' | 'pathspecs-before-separator' | 'scope-required';
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ManagementRefusal {
/// Unknown command,
/// unknown option,
/// missing option value or stray argument.
Usage,
/// A pathspec appeared before `--`;
/// `fix` tells which direct command.
PathspecsBeforeSeparator {
/// `true` for `fix`,
/// `false` for `check`.
fix: bool,
},
/// Neither or both of `--all` and pathspecs were given.
ScopeRequired {
/// `true` for `fix`,
/// `false` for `check`.
fix: bool,
},
}
/// What:
/// Report whether a token starts an option rather than naming a path.
/// Why:
/// As in Git,
/// a lone `-` is a positional,
/// and anything else dash-led is an option.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function isOptionToken(token: string): boolean { return token.startsWith('-') && token !== '-'; }
/// ```
fn is_option_token(token: &OsString) -> bool {
// `&[u8]` borrows the token's raw bytes (`u8` is one byte).
let bytes: &[u8] = token.as_encoded_bytes();
return bytes.starts_with(b"-") && bytes != b"-";
}
/// What:
/// Parse a retired command's remaining arguments.
/// `&[OsString]` borrows the arguments after the command name.
/// `Result<T, E>` is "value or error":
/// `Ok(...)` or `Err(...)`.
/// Why:
/// The accepted forms are the ones the commands had:
/// `trust` takes `--yes` and
/// `--help`/`-h`;
/// `untrust` and `status` take nothing.
/// A lone `--` ends options.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseRetired(command: RetiredCommand, rest: string[]): ManagementAction;
/// ```
fn parse_retired(
command: RetiredCommand,
rest: &[OsString],
) -> Result<ManagementAction, ManagementRefusal> {
// `mut` allows the flags to be set while scanning.
let mut help: bool = false;
let mut terminated: bool = false;
// `for token in rest` borrows each argument in order.
for token in rest {
if terminated {
// What: `Err(...)` is the failure variant; `return` leaves early.
// Why: These commands never took positional arguments.
//
// In TS you'd write (pseudocode):
// ```ts
// return MANAGEMENT_REFUSED;
// ```
return Err(ManagementRefusal::Usage);
}
if token == "--" {
terminated = true;
} else if command == RetiredCommand::Trust && (token == "--help" || token == "-h") {
help = true;
} else if command == RetiredCommand::Trust && token == "--yes" {
// Noninteractive consent has nothing left to consent to; it is accepted and ignored.
continue;
} else {
return Err(ManagementRefusal::Usage);
}
}
// `Ok(...)` is the success variant carrying the understood action.
return Ok(ManagementAction::Retired { command, help });
}
/// What:
/// Add one selected policy ID to the list,
/// once.
/// `&mut Vec<String>` lends the list
/// for writing;
/// `&[u8]` borrows the value's raw bytes;
/// `Result<(), ManagementRefusal>`
/// is "nothing,
/// or a refusal".
/// Why:
/// A separated value and an attached value are recorded the same way:
/// policy IDs are
/// ASCII names,
/// so other bytes can never name a policy,
/// and a repeated ID counts once.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function recordPolicy(policies: string[], value: Uint8Array): void { const id = decodeUtf8OrRefuse(value); if (!policies.includes(id)) policies.push(id); }
/// ```
fn record_policy(policies: &mut Vec<String>, value: &[u8]) -> Result<(), ManagementRefusal> {
// What: `let Ok(text) = ... else { ... };` keeps valid UTF-8 or exits.
// Why: Bytes that are not text cannot be a policy ID.
//
// In TS you'd write (pseudocode):
// ```ts
// const id = decodeUtf8OrRefuse(value);
// ```
let Ok(id) = std::str::from_utf8(value) else {
return Err(ManagementRefusal::Usage);
};
let owned: String = String::from(id);
// `.contains(&owned)` borrows the new ID to keep only its first occurrence.
if !policies.contains(&owned) {
policies.push(owned);
}
// `Ok(())` is success carrying nothing.
return Ok(());
}
/// What:
/// Parse `check` or `fix` arguments in one forward pass.
/// `&[OsString]` borrows the arguments after the command word.
/// Why:
/// `--policy` takes its next argument (or an attached `=value`) even when that
/// value starts with a dash;
/// everything after `--` is a pathspec.
/// A pathspec
/// before `--` is refused so a mistyped option can never become a path,
/// and the
/// two scopes are mutually exclusive.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseDirect(fix: boolean, rest: string[]): ManagementAction;
/// ```
fn parse_direct(fix: bool, rest: &[OsString]) -> Result<ManagementAction, ManagementRefusal> {
let mut all: bool = false;
// `Vec::<String>::new()` is an empty owned list; `mut` allows pushing.
let mut policies: Vec<String> = Vec::<String>::new();
let mut pathspecs: Vec<OsString> = Vec::<OsString>::new();
let mut terminated: bool = false;
let mut pathspec_before_separator: bool = false;
// True while the token being visited is the value of the `--policy` before it.
let mut is_policy_value: bool = false;
// `for token in rest` borrows each argument once, in order, so the scan always ends.
for token in rest {
if is_policy_value {
is_policy_value = false;
// A trailing `?` returns the refusal to our caller, or continues.
record_policy(&mut policies, token.as_encoded_bytes())?;
continue;
}
if terminated {
// `.clone()` copies the pathspec bytes into the owned result.
pathspecs.push(token.clone());
continue;
}
if token == "--" {
terminated = true;
continue;
}
if !is_option_token(token) {
pathspec_before_separator = true;
continue;
}
if token == "--all" {
all = true;
continue;
}
// The policy value is the next argument, or attached after `--policy=`.
if token == "--policy" {
is_policy_value = true;
continue;
}
// `let Some(x) = ... else { ... };` unwraps the attached value or refuses the option.
let Some(attached) = token.as_encoded_bytes().strip_prefix(b"--policy=") else {
return Err(ManagementRefusal::Usage);
};
record_policy(&mut policies, attached)?;
}
// `--policy` was the last argument: its value is missing.
if is_policy_value {
return Err(ManagementRefusal::Usage);
}
if pathspec_before_separator {
return Err(ManagementRefusal::PathspecsBeforeSeparator { fix });
}
if all == !pathspecs.is_empty() {
return Err(ManagementRefusal::ScopeRequired { fix });
}
return Ok(ManagementAction::Direct {
fix,
all,
policies,
pathspecs,
});
}
/// What:
/// Parse the arguments after `git cli-git` into one action or refusal.
/// Why:
/// The leading command name selects one grammar,
/// so an invocation naming two
/// commands is refused for its stray argument rather than guessed at.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseManagementArguments(args: string[]): ManagementAction;
/// ```
pub fn parse_management_arguments(
arguments: &[OsString],
) -> Result<ManagementAction, ManagementRefusal> {
// What: `let Some(name) = arguments.first() else { ... };` unwraps the command name.
// Why: A bare `git cli-git` names no command.
//
// In TS you'd write (pseudocode):
// ```ts
// const [name, ...rest] = args; if (name === undefined) return MANAGEMENT_REFUSED;
// ```
let Some(name) = arguments.first() else {
return Err(ManagementRefusal::Usage);
};
// `&arguments[1..]` borrows every argument after the first.
let rest: &[OsString] = &arguments[1..];
if name == "--help" || name == "-h" {
if !rest.is_empty() {
return Err(ManagementRefusal::Usage);
}
return Ok(ManagementAction::Help);
}
if name == "trust" {
return parse_retired(RetiredCommand::Trust, rest);
}
if name == "untrust" {
return parse_retired(RetiredCommand::Untrust, rest);
}
if name == "status" {
return parse_retired(RetiredCommand::Status, rest);
}
if name == "check" {
return parse_direct(false, rest);
}
if name == "fix" {
return parse_direct(true, rest);
}
return Err(ManagementRefusal::Usage);
}
/// Grammar controls stay out of the release executable.
#[cfg(test)]
#[path = "management_arguments_tests.rs"]
mod tests;