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
//! 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 crateADD_ESCAPE_HATCH;
use cratewithout_tokens;
use crate;
use crate;
use cratePolicyId;
/// 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 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' };
/// ```
/// 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 };
/// ```
/// 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: & = &;
/// 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 };
/// ```
/// 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 });
/// ```
/// 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;
/// ```
/// 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);
/// ```
/// 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;
/// ```
/// 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);
/// ```
/// 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[];
/// ```
/// Spelling,
/// meaning and global-prefix controls stay out of the release executable.