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
//! What:
//! The wrapper-only control tokens,
//! what each one means,
//! and their removal from the
//! arguments that come before the Git subcommand.
//! Why:
//! Git refuses options it does not know,
//! so every control must be gone before Git runs,
//! and every policy decision must read the arguments without them.
//! A control is
//! recognized only where Git itself would look for an option,
//! and it is removed by its
//! position,
//! never by its spelling,
//! so an equal-looking value or path is kept.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // const { args, keepGoing, escapedPolicyIds } = parsePolicyControls({ args: rawArgs });
//! ```
/// What:
/// `use` brings names from sibling files into this file;
/// `super::` means "the parent
/// module",
/// where every sibling file of this crate is declared.
/// Why:
/// The scan asks the Git 2.56.0 global-option reader where an unknown option sits,
/// and removes tokens through the one position-based removal function.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// import { withoutTokens } from './command_options_query.ts';
/// import { globalLayout } from './global_arguments.ts';
/// ```
use super::command_add::ADD_ESCAPE_HATCH;
use super::command_options_query::without_tokens;
use super::escape_hatch::{
BRANCH_WORKTREE_ESCAPE_HATCH, WORKTREE_COPY_ESCAPE_HATCH, WORKTREE_ENFORCEMENT_ESCAPE_HATCH,
};
use super::global_arguments::{GlobalLayout, GlobalOutcome, global_layout};
use super::policy_registry::PolicyId;
/// What:
/// `OsString` is owned operating-system text of raw bytes.
/// Sibling the reader might
/// expect:
/// `String`,
/// which must be valid UTF-8.
/// Why:
/// Arguments may hold bytes that are not UTF-8 and are forwarded unchanged.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // string, but byte-preserving.
/// ```
use std::ffi::OsString;
/// What:
/// The control that lets later policies run after an error finding.
/// `&str` is borrowed
/// text;
/// here it is baked into the program.
/// Why:
/// A caller who wants every finding of one command in a single run passes it.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const KEEP_GOING_FLAG = '--cli-git-keep-going';
/// ```
pub const KEEP_GOING_FLAG: &str = "--cli-git-keep-going";
/// What:
/// What one control asks for.
/// An `enum` is a closed set of named alternatives;
/// `Escape` carries the policy it switches off.
/// `#[derive(...)]` asks the compiler to
/// generate copying (`Clone`,
/// `Copy`),
/// debug printing (`Debug`) and `==`.
/// Why:
/// Recognition (a spelling) and effect (a meaning) are kept apart,
/// so two spellings
/// of one escape cannot drift.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type ControlMeaning = { kind: 'keep-going' } | { kind: 'escape'; policy: PolicyId } | { kind: 'skip-worktree-copy' };
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ControlMeaning {
/// Continue with later policies after an error finding.
KeepGoing,
/// Skip one policy for this invocation.
Escape(PolicyId),
/// Skip ignored-state synchronization for this invocation.
SkipWorktreeCopy,
}
/// What:
/// One accepted control:
/// its exact spelling and its meaning.
/// A `struct` is a record
/// with named fields;
/// `&'static str` is text baked into the program for its whole run.
/// Why:
/// The table below is the single list of spellings the wrapper recognizes.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type ControlSpelling = { flag: string; meaning: ControlMeaning };
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ControlSpelling {
/// The exact token;
/// controls are never abbreviated and never carry `=value`.
pub flag: &'static str,
/// What the token asks for.
pub meaning: ControlMeaning,
}
/// What:
/// Every control spelling.
/// `&[ControlSpelling]` is a borrowed,
/// read-only list (siblings:
/// `Vec<T>`,
/// owned and growable;
/// `[T; N]`,
/// fixed length in the type).
/// Why:
/// One `--no-enforce-<policy id>` per shipped policy,
/// the three spellings that predate
/// policy ids,
/// the keep-going flag and the worktree-copy opt-out.
/// A compiled-in list
/// cannot be reordered or extended at run time.
/// Gotcha:
/// `wrapper_controls_tests.rs` pins that every shipped policy has exactly the
/// spelling `--no-enforce-` followed by its registry name.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const CONTROL_SPELLINGS: readonly ControlSpelling[] = [ ... ];
/// ```
pub const CONTROL_SPELLINGS: &[ControlSpelling] = &[
ControlSpelling {
flag: KEEP_GOING_FLAG,
meaning: ControlMeaning::KeepGoing,
},
ControlSpelling {
flag: "--no-enforce-require-root",
meaning: ControlMeaning::Escape(PolicyId::RequireRoot),
},
ControlSpelling {
flag: "--no-enforce-linked-worktree-only",
meaning: ControlMeaning::Escape(PolicyId::LinkedWorktreeOnly),
},
ControlSpelling {
flag: "--no-enforce-branch-worktree-only",
meaning: ControlMeaning::Escape(PolicyId::BranchWorktreeOnly),
},
ControlSpelling {
flag: "--no-enforce-add-explicit",
meaning: ControlMeaning::Escape(PolicyId::AddExplicit),
},
ControlSpelling {
flag: "--no-enforce-final-newline",
meaning: ControlMeaning::Escape(PolicyId::FinalNewline),
},
ControlSpelling {
flag: "--no-enforce-markdown/autofix",
meaning: ControlMeaning::Escape(PolicyId::MarkdownAutofix),
},
ControlSpelling {
flag: "--no-enforce-mono/forbidden-root-context",
meaning: ControlMeaning::Escape(PolicyId::ForbiddenRootContext),
},
ControlSpelling {
flag: "--no-enforce-mono/dependent-version-bump",
meaning: ControlMeaning::Escape(PolicyId::DependentVersionBump),
},
ControlSpelling {
flag: "--no-enforce-security/forbidden-strings",
meaning: ControlMeaning::Escape(PolicyId::ForbiddenStrings),
},
ControlSpelling {
flag: WORKTREE_ENFORCEMENT_ESCAPE_HATCH,
meaning: ControlMeaning::Escape(PolicyId::LinkedWorktreeOnly),
},
ControlSpelling {
flag: BRANCH_WORKTREE_ESCAPE_HATCH,
meaning: ControlMeaning::Escape(PolicyId::BranchWorktreeOnly),
},
ControlSpelling {
flag: ADD_ESCAPE_HATCH,
meaning: ControlMeaning::Escape(PolicyId::AddExplicit),
},
ControlSpelling {
flag: WORKTREE_COPY_ESCAPE_HATCH,
meaning: ControlMeaning::SkipWorktreeCopy,
},
];
/// What:
/// Everything the controls of one invocation asked for.
/// `bool` is true or false;
/// `Vec<PolicyId>` is an owned,
/// growable list of policy identities.
/// Why:
/// The engine reads the effect of the removed tokens from this record,
/// because the
/// tokens themselves are gone from the arguments it sees.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type Controls = { keepGoing: boolean; escaped: PolicyId[]; commitOnlyEscaped: boolean; skipWorktreeCopy: boolean };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Controls {
/// Later policies run after an error finding.
pub keep_going: bool,
/// Policies skipped for this invocation,
/// each listed once,
/// in first-use order.
pub escaped: Vec<PolicyId>,
/// `--no-enforce-only` was written in option position of `git commit`.
pub commit_only_escaped: bool,
/// Ignored-state synchronization is skipped.
pub skip_worktree_copy: bool,
}
/// What:
/// The record of an invocation that used no control.
/// `Vec::<PolicyId>::new()` is an
/// empty owned list.
/// Why:
/// Every scan starts from "nothing was asked" and only adds.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const noControls = (): Controls => ({ keepGoing: false, escaped: [], commitOnlyEscaped: false, skipWorktreeCopy: false });
/// ```
pub fn no_controls() -> Controls {
return Controls {
keep_going: false,
escaped: Vec::<PolicyId>::new(),
commit_only_escaped: false,
skip_worktree_copy: false,
};
}
/// What:
/// The meaning of a token that is exactly a control spelling,
/// or nothing.
/// `&[u8]`
/// borrows raw bytes;
/// `Option<T>` is "a value or nothing".
/// Why:
/// Arguments are compared as bytes,
/// so a non-UTF-8 argument is never decoded.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const meaning = CONTROL_SPELLINGS.find(s => s.flag === token)?.meaning;
/// ```
pub fn control_meaning(token: &[u8]) -> Option<ControlMeaning> {
// `for spelling in CONTROL_SPELLINGS` borrows each row in table order.
for spelling in CONTROL_SPELLINGS {
// `.as_bytes()` lends the spelling's bytes for the comparison.
if spelling.flag.as_bytes() == token {
// `Some(x)` is the "present" case of `Option`.
return Some(spelling.meaning);
}
}
// `None` is the "absent" case of `Option`.
return None;
}
/// What:
/// The control spellings as byte strings,
/// in table order.
/// `Vec<&'static [u8]>` is an
/// owned list of borrowed byte strings baked into the program.
/// Why:
/// Every Git option-table reader takes this list,
/// so it reports a control found in
/// option position instead of refusing it as an unknown option.
/// The position in this
/// list is the position in `CONTROL_SPELLINGS`.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const flags = CONTROL_SPELLINGS.map(s => s.flag);
/// ```
pub fn control_flags() -> Vec<&'static [u8]> {
// `with_capacity(n)` reserves room for `n` items; `mut` allows pushing.
let mut flags: Vec<&'static [u8]> =
Vec::<&'static [u8]>::with_capacity(CONTROL_SPELLINGS.len());
for spelling in CONTROL_SPELLINGS {
flags.push(spelling.flag.as_bytes());
}
return flags;
}
/// What:
/// Add one control's effect to the record.
/// `&mut Controls` lends the record for
/// writing (sibling `&Controls` lends it read-only).
/// Why:
/// A repeated escape must not list its policy twice:
/// the list is compared in tests
/// and printed in diagnostics.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function recordControl(controls: Controls, meaning: ControlMeaning): void;
/// ```
pub fn record_control(controls: &mut Controls, meaning: ControlMeaning) {
// `match` picks one arm per variant and unpacks the policy an `Escape` carries; the
// compiler refuses the code if a variant is forgotten.
match meaning {
ControlMeaning::KeepGoing => controls.keep_going = true,
ControlMeaning::SkipWorktreeCopy => controls.skip_worktree_copy = true,
ControlMeaning::Escape(policy) => {
// `.contains(&policy)` borrows the identity for the lookup.
if !controls.escaped.contains(&policy) {
controls.escaped.push(policy);
}
}
}
}
/// What:
/// Whether a policy is skipped for this invocation.
/// Why:
/// The engine asks by typed identity instead of scanning the list itself.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const escaped = controls.escaped.includes(policy);
/// ```
pub fn is_escaped(controls: &Controls, policy: PolicyId) -> bool {
return controls.escaped.contains(&policy);
}
/// What:
/// Copy the arguments without the controls written before the subcommand,
/// adding
/// their effect to `controls`.
/// `&[OsString]` borrows the argument list;
/// `Vec<OsString>`
/// is the owned result.
/// Why:
/// Git 2.56.0's global-option reader stops at the first option it does not know.
/// When that option is exactly a control,
/// it sits where Git would have read an
/// option,
/// so it is removed by its position and the reader is asked again.
/// A value
/// of `-C`,
/// `-c` or another value-taking option is skipped by that reader and is
/// therefore never removed,
/// even when it spells a control.
/// Gotcha:
/// Each round removes one token,
/// so the loop runs at most once per argument.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function stripGlobalControls(args: string[], controls: Controls): string[];
/// ```
pub fn strip_global_controls(arguments: &[OsString], controls: &mut Controls) -> Vec<OsString> {
// `.to_vec()` copies the borrowed list into an owned one that later rounds replace.
let mut current: Vec<OsString> = arguments.to_vec();
// `0..arguments.len()` counts the rounds; `_round` is unused because only the count matters.
for _round in 0..arguments.len() {
// `.as_slice()` lends the owned list as a borrowed view.
let layout: GlobalLayout = global_layout(current.as_slice());
if layout.outcome != GlobalOutcome::InvalidOption {
break;
}
// `.as_encoded_bytes()` lends the raw bytes of the unknown option.
let unknown: &[u8] = current[layout.prefix_len].as_encoded_bytes();
// `let Some(x) = ... else { ... };` unwraps the meaning or leaves the loop.
let Some(meaning) = control_meaning(unknown) else {
break;
};
record_control(controls, meaning);
// `&[layout.prefix_len]` is a one-item borrowed list of positions to drop.
current = without_tokens(current.as_slice(), 0, &[layout.prefix_len]);
}
return current;
}
/// Spelling,
/// meaning and global-prefix controls stay out of the release executable.
#[cfg(test)]
#[path = "wrapper_controls_tests.rs"]
mod tests;