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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! What:
//! Decide whether one PATH candidate is real Git,
//! a cli-git wrapper,
//! or unusable.
//! Why:
//! The wrapper shadows `git` on PATH and must never select itself,
//! a copy of
//! itself,
//! a toolchain launcher that replaces itself with the wrapper,
//! or a script that starts the TypeScript wrapper.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // classifyCandidate('/usr/bin/git', ownExecutable) === 'real-git'
//! ```
/// What:
/// Import the trait that gives files `.read(..)`.
/// A trait is an interface;
/// its methods exist only while it is in scope.
/// Why:
/// Candidates are inspected through bounded reads,
/// never loaded whole by default.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// import { open } from 'node:fs/promises';
/// ```
use Read;
/// `Path` is a borrowed filesystem path of raw OS bytes (owned sibling:
/// `PathBuf`).
use Path;
/// What:
/// Largest script inspected for wrapper markers,
/// 64 kibibytes.
/// `usize` is the platform's index and length type (siblings `u32`,
/// `u64`).
/// Why:
/// Generated command shims are tiny launchers;
/// a fixed bound keeps an arbitrary
/// PATH file from controlling how much the resolver reads.
/// `usize` matches the
/// buffer lengths it is compared with.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const MAX_SCRIPT_INSPECTION_BYTES = 64 * 1024;
/// ```
pub const MAX_SCRIPT_INSPECTION_BYTES: usize = 64 * 1024;
/// What:
/// Leading bytes of ELF,
/// PE,
/// Mach-O and universal Mach-O executables.
/// `&[&[u8]]` is a borrowed list of borrowed byte strings compiled into the program.
/// Why:
/// A native executable that is not this wrapper is treated as Git after one
/// four-byte read;
/// only scripts are searched for wrapper markers.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const NATIVE_EXECUTABLE_PREFIXES = ['7f454c46', '4d5a', 'feedface', ...];
/// ```
const NATIVE_EXECUTABLE_PREFIXES: & = &;
/// What:
/// Text that identifies a script delegating to the TypeScript cli-git wrapper:
/// its package name and bundled entry path,
/// in POSIX and Windows spellings.
/// Why:
/// Such a script is the other wrapper;
/// selecting it as real Git would make the
/// two wrappers call each other.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const SELF_SHIM_MARKERS = ['@monochromatic-dev/git-policy-cli', ...];
/// ```
const WRAPPER_SCRIPT_MARKERS: & = &;
/// What:
/// The three verdicts for one candidate path.
/// `#[derive(...)]` generates copying,
/// debug printing and `==`.
/// Why:
/// The resolver counts skipped wrappers separately from unusable entries so its
/// failure message can say why nothing was selected.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type CandidateKind = 'real-git' | 'wrapper' | 'unusable';
/// ```
/// What:
/// Report whether the bytes begin with a native executable signature.
/// Why:
/// Native candidates skip the script-marker search entirely.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function isNativeHeader(header: Uint8Array): boolean;
/// ```
/// What:
/// Report whether `needle` occurs anywhere inside `haystack`.
/// Why:
/// Byte slices have no built-in substring search,
/// and scripts are searched as
/// bytes so a non-UTF-8 script cannot hide a marker.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function containsBytes(haystack: Buffer, needle: Buffer): boolean { return haystack.includes(needle); }
/// ```
/// What:
/// Report whether script bytes contain a TypeScript-wrapper marker.
/// Why:
/// This is the version-independent recognition of the other wrapper's launchers.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function hasWrapperMarker(content: Buffer): boolean;
/// ```
/// What:
/// Report whether file metadata says the file can be run.
/// `#[cfg(unix)]` and `#[cfg(not(unix))]` each compile one of the two inner blocks.
/// Why:
/// PATH lookup only runs executable files;
/// a non-executable `git` is skipped.
/// On Unix this checks the mode bits,
/// not the calling user's access as `access(2)`
/// would.
/// Other systems have no execute bit:
/// every regular file named by PATHEXT
/// can run.
/// The Markdown policy's linter lookup applies the same test.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function isExecutable(stats: Stats): boolean { return process.platform === 'win32' || (stats.mode & 0o111) !== 0; }
/// ```
/// What:
/// Report whether two metadata records name the same file on the same device.
/// Why:
/// On Unix,
/// device and inode numbers identify a file through any symbolic link,
/// hard link or repeated PATH entry.
/// Other systems expose no stable inode here,
/// so the answer is "not proven the same" and canonical paths decide instead.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function sameInode(a: Stats, b: Stats): boolean { return process.platform !== 'win32' && a.dev === b.dev && a.ino === b.ino; }
/// ```
/// What:
/// Report whether two paths name the same existing file.
/// Why:
/// Self-exclusion must hold through symbolic links,
/// hard links,
/// relative
/// spellings and repeated PATH directories.
/// Either check alone suffices;
/// an
/// inspection failure means "not proven the same".
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function sameFile(first: string, second: string): Promise<boolean>;
/// ```
/// What:
/// Report whether two files have the same length and the same bytes.
/// Why:
/// A copy of this executable under another name or directory is still this
/// wrapper,
/// although its inode differs.
/// Lengths are compared first,
/// so real Git
/// is never read in full for this check.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function identicalContent(first: string, second: string): Promise<boolean>;
/// ```
/// What:
/// Read from a file until `limit` bytes are collected or the file ends.
/// `&mut std::fs::File` lends the open file for reading (reading moves its position).
/// `Option<Vec<u8>>` is the owned bytes,
/// or `None` when the read failed.
/// Why:
/// One `read` call may return fewer bytes than requested.
/// The standard library's
/// bounded reader repeats the read until the limit or the end of the file,
/// so the
/// bound is exact and there is no loop here to keep in step.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function readUpTo(file: FileHandle, limit: number): Promise<Buffer | undefined>;
/// ```
/// What:
/// The suffix of the record a toolchain launcher keeps beside it, `<launcher>.launch`.
/// `&str` is a borrowed text baked into the program.
/// Why:
/// The repository's `mpm-shadow` writes the record when it installs a launcher
/// (`package/mpm-shadow/cli/src/toolchain/launcher.rs`, `RECORD_SUFFIX`); the wrapper reads the same file to learn what
/// a launcher on PATH leads to.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const LAUNCH_RECORD_SUFFIX = '.launch';
/// ```
pub const LAUNCH_RECORD_SUFFIX: &str = ".launch";
/// What:
/// Report whether a launch record's text names this wrapper: its tool is this crate, or the file it replaces itself
/// with is this executable.
/// `Option<&Path>` is "this executable's path, or nothing when the caller has none".
/// `serde_json::Value` is a parsed JSON document (siblings: a typed struct, which would need a derive macro).
/// Why:
/// The launcher of another release of the wrapper has a record naming a different file, so the file the record names
/// cannot decide alone; the tool id is the same for every release. Both are read from the record; neither starts
/// anything.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function recordNamesThisWrapper(record: string, own?: string): boolean {
/// const { toolId, real } = JSON.parse(record);
/// return toolId.split(':').at(-1) === 'monochromatic-git-policy-cli' || (own !== undefined && sameFile(real, own));
/// }
/// ```
/// What:
/// Read the launch record at `record` if there is one, bounded.
/// `Option<Vec<u8>>` is the owned bytes, or nothing when there is no readable record.
/// Why:
/// Most candidates (the real Git among them) have no record, and the open of a file that is not there is the whole
/// cost. A record is a few hundred bytes; the same bound as a script's keeps an odd file from controlling the read.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function readRecord(path: string): Promise<Buffer | undefined>;
/// ```
/// What:
/// Report whether the candidate is a toolchain launcher of this wrapper: a file with a launch record beside it that
/// names this wrapper.
/// The record is looked for beside the candidate's own name and beside the file its links finally lead to.
/// Why:
/// A launcher replaces itself with the wrapper's executable, which is another file with other bytes, so the file
/// checks of the identity test miss it; the record beside it says what it leads to. A symlink named `git` that points
/// at a launcher has no record of its own, so the file it leads to is checked as well.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function isWrapperLauncher(candidate: string, own?: string): boolean {
/// return [candidate, realpathOrUndefined(candidate)].some((p) => p && recordNamesThisWrapper(readOrUndefined(p + '.launch'), own));
/// }
/// ```
/// What:
/// Classify one candidate against this wrapper's own executable.
/// Why:
/// This is the whole self-exclusion decision:
/// the same file (any link or
/// spelling),
/// a toolchain launcher of this wrapper,
/// an identical copy,
/// or a TypeScript-wrapper launcher is a wrapper;
/// any other executable regular file is Git.
/// Another release of the wrapper behind no record and no marker string cannot be told here; the marker every
/// real-Git start carries (`started_as_git.rs`) stops it when it arrives.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function classifyCandidate(candidate: string, ownExecutable: string): Promise<CandidateKind>;
/// ```
/// Controls for the header,
/// marker,
/// identity and content primitives.
/// Disposable-directory classification controls stay out of the release executable.