monochromatic-git-policy-cli 0.1.4

Repository-owned Git policy wrapper: forwards Git commands and checks commits against configured policies.
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//! What:
//!  The per-invocation owner of candidate versions and candidate bytes.
//! Why:
//!  Policies read committed or staged content,
//!  never whatever is in the live
//!      worktree.
//!  A version costs one listing process,
//!  its bytes come from one
//!      long-lived object reader,
//!  and neither count grows with the number of files.
//!      After a fix or replay changed the index,
//!  `invalidate` retires every version.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // const store = new CandidateStore(git, prefix, overlay);
//! // const version = await store.version({ kind: 'staged-against-head' });
//! // const bytes = await store.bytes(version.candidates[0]);
//! ```

/// Import the layer's failure type and its closed list of causes.
use super::candidate_error::{CandidateError, CandidateFailure};
/// Import the candidate modes,
///  the validated object name and its parser.
use super::candidate_object::{CandidateMode, ObjectId, parse_object_id};
/// Import the long-lived object reader.
use super::candidate_reader::{ObjectReader, start_object_reader};
/// Import the listing parser and its records.
use super::candidate_record::{CandidateRecord, parse_raw_records};
/// Import the stage-record parser of `git ls-files --stage`.
use super::candidate_stage::{StageRecord, parse_stage_records};
/// Import the tree-record parser of `git ls-tree -r`.
use super::candidate_tree::{TreeRecord, parse_tree_records};
/// Import the version types and builder.
use super::candidate_version::{Candidate, CandidateSource, CandidateVersion, build_version};
/// Import the captured-query runner and its line helper.
use super::git_metadata::{MetadataOutput, run_metadata_git, strip_git_line};
/// `OsString` is owned operating-system text of raw OS bytes (sibling `String` must be UTF-8).
use std::ffi::OsString;
/// `Path`/`PathBuf` are borrowed/owned filesystem paths of raw OS bytes.
use std::path::{Path, PathBuf};
/// What:
///  `Rc<T>` is a shared,
///  read-only handle to one heap value,
///  counted so the value
///       lives until its last handle is gone (siblings:
///  `Box<T>`,
///  a single owner,
///  and
///       `Arc<T>`,
///  the thread-safe form).
/// Why:
///   Several policies hold the same version and the same bytes at once.
///  Nothing
///       here crosses threads,
///  so `Arc` would add cost for no benefit.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // Every object reference in TS already behaves like this.
/// ```
use std::rc::Rc;

/// What:
///  Everything one invocation needs to list and read candidates.
///       `Option<ObjectReader>` is "a running reader or nothing";
///  `u64` is an unsigned
///       64-bit counter (siblings `u32`,
///  `usize`).
/// Why:
///   The reader is started only when something needs it,
///  so an invocation that
///       reads no object starts no reader.
///  `u64` cannot run out within a process.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// class CandidateStore { #reader?: ObjectReader; #generation = 0n; #versions: [CandidateSource, CandidateVersion][] = [] }
/// ```
pub struct CandidateStore {
    /// The real Git executable every listing and the reader start.
    real_git: PathBuf,
    /// The caller's arguments before the subcommand,
    ///  replayed so Git selects the same repository.
    global_prefix: Vec<OsString>,
    /// Added environment pairs,
    ///  such as a private index file or object directory.
    overlay: Vec<(OsString, OsString)>,
    /// The long-lived object reader,
    ///  once started.
    reader: Option<ObjectReader>,
    /// Counts invalidations;
    ///  candidates of an earlier generation are refused.
    generation: u64,
    /// Versions already listed in this generation,
    ///  by source.
    versions: Vec<(CandidateSource, Rc<CandidateVersion>)>,
    /// The commit every "against `HEAD`" listing compares with instead of the live `HEAD`:
    ///  `Some(Some(commit))` for a recorded base, `Some(None)` for a recorded unborn branch, `None` for live `HEAD`.
    baseline: Option<Option<ObjectId>>,
}

/// What:
///  Build the failure of a listing command that could not be started or exited unsuccessfully.
/// Why:
///   The message names the Git operation,
///  so the person can rerun it.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function listingFailure(failure: CandidateFailure, operation: string, detail: string): CandidateError;
/// ```
fn listing_failure(failure: CandidateFailure, operation: &str, detail: &str) -> CandidateError {
    return CandidateError::new(
        failure,
        format!("cli-git could not list candidate files: git {operation} {detail}").as_str(),
    );
}

/// What:
///  `impl CandidateStore { ... }` attaches the store's operations,
///  like class methods.
/// Why:
///   The store is the only place that starts Git for candidates,
///  which is what
///       keeps the process count bounded.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// class CandidateStore { version(source) {} bytes(candidate) {} invalidate() {} }
/// ```
impl CandidateStore {
    /// What:
    ///  Create a store without starting any process.
    ///       `&[OsString]` borrows the global arguments;
    ///  `&[(OsString, OsString)]` borrows
    ///       environment pairs;
    ///  `.to_vec()` and `.to_path_buf()` copy them into owned storage.
    /// Why:
    ///   The store outlives the caller's borrowed arguments,
    ///  and commands that
    ///       never ask for candidates pay nothing.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// constructor(realGit: string, globalPrefix: string[], overlay: [string, string][])
    /// ```
    pub fn new(
        real_git: &Path,
        global_prefix: &[OsString],
        overlay: &[(OsString, OsString)],
    ) -> CandidateStore {
        return CandidateStore {
            real_git: real_git.to_path_buf(),
            global_prefix: global_prefix.to_vec(),
            overlay: overlay.to_vec(),
            // `None` is the "absent" variant: no reader yet.
            reader: None,
            generation: 0,
            versions: Vec::new(),
            baseline: None,
        };
    }

    /// What:
    ///  Compare every "against `HEAD`" listing with a recorded base instead of the live `HEAD`.
    /// Why:
    ///   A commit transaction reads its candidates against the base it recorded at invocation,
    ///  because another landing may have moved `HEAD` since.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// listChangedIndexPaths({ baseRevision }) // never live HEAD
    /// ```
    #[must_use]
    pub fn with_baseline(mut self, baseline: Option<ObjectId>) -> CandidateStore {
        self.baseline = Some(baseline);
        return self;
    }

    /// What:
    ///  The commit the "against `HEAD`" listings compare with:
    ///  the recorded baseline when one was set, otherwise the commit `HEAD` names now.
    /// Why:
    ///   One place decides the baseline, so the listings and the `HEAD` tree always agree.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// const baseline = recorded ?? await headCommit();
    /// ```
    fn baseline_commit(&mut self) -> Result<Option<ObjectId>, CandidateError> {
        if let Some(recorded) = &self.baseline {
            return Ok(recorded.clone());
        }
        return self.reader()?.head_commit();
    }

    /// What:
    ///  The running reader,
    ///  started on first use.
    ///  `&mut ObjectReader` lends it for changing.
    /// Why:
    ///   One reader serves every object read of the invocation.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// #reader(): ObjectReader { return (this.#reader ??= startObjectReader(...)); }
    /// ```
    fn reader(&mut self) -> Result<&mut ObjectReader, CandidateError> {
        if self.reader.is_none() {
            // A trailing `?` returns a start failure to our caller, or unwraps the reader.
            let started: ObjectReader = start_object_reader(
                self.real_git.as_path(),
                self.global_prefix.as_slice(),
                self.overlay.as_slice(),
            )?;
            // `Some(...)` is the "present" variant.
            self.reader = Some(started);
        }
        // `.as_mut()` lends the stored reader; `let Some(..) = .. else` unwraps it.
        let Some(reader) = self.reader.as_mut() else {
            // `Err(...)` is the failure variant. The reader was stored just above, so this is not expected.
            return Err(CandidateError::new(
                CandidateFailure::ReaderEnded,
                "cli-git could not read a Git object: the object reader was not started.",
            ));
        };
        // `Ok(...)` is the success variant.
        return Ok(reader);
    }

    /// What:
    ///  Run one listing command and return its standard output.
    ///       `Vec<u8>` is an owned byte list (sibling `String` would require UTF-8).
    /// Why:
    ///   Listings print raw pathname bytes.
    ///  A command that fails lists nothing
    ///       trustworthy,
    ///  so its output is discarded and Git's own message is reported.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async #runListing(operation: string, args: string[]): Promise<Buffer>
    /// ```
    fn run_listing(
        &self,
        operation: &str,
        command: &[OsString],
    ) -> Result<Vec<u8>, CandidateError> {
        // `.clone()` copies the stored prefix; `.extend_from_slice(..)` appends the command after it.
        let mut arguments: Vec<OsString> = self.global_prefix.clone();
        arguments.extend_from_slice(command);
        // `match` on the run's `Result`: `Err(error)` means Git could not be started.
        let output: MetadataOutput = match run_metadata_git(
            self.real_git.as_path(),
            arguments.as_slice(),
            self.overlay.as_slice(),
        ) {
            Ok(captured) => captured,
            Err(error) => {
                return Err(listing_failure(
                    CandidateFailure::GitNotStarted,
                    operation,
                    format!("could not be started: {error}.").as_str(),
                ));
            }
        };
        if !output.success {
            // `String::from_utf8_lossy` decodes Git's message for display, replacing invalid bytes.
            return Err(listing_failure(
                CandidateFailure::GitFailed,
                operation,
                format!(
                    "failed: {}",
                    String::from_utf8_lossy(output.stderr.as_slice()).trim()
                )
                .as_str(),
            ));
        }
        return Ok(output.stdout);
    }

    /// What:
    ///  The name of the empty tree in this repository's hash format.
    /// Why:
    ///   A repository without commits has no `HEAD` to compare the index with;
    ///  the
    ///       empty tree makes every staged entry an addition.
    ///  Git computes the name
    ///       from empty input,
    ///  so SHA-1 and SHA-256 repositories both work.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async #emptyTree(): Promise<ObjectId>
    /// ```
    fn empty_tree(&self) -> Result<ObjectId, CandidateError> {
        // The query runner gives Git no standard input, which is the empty tree's content.
        let output: Vec<u8> = self.run_listing(
            "hash-object",
            &[
                OsString::from("hash-object"),
                OsString::from("-t"),
                OsString::from("tree"),
                OsString::from("--stdin"),
            ],
        )?;
        let Some(tree) = parse_object_id(strip_git_line(output.as_slice())) else {
            return Err(listing_failure(
                CandidateFailure::ListingMalformed,
                "hash-object",
                "did not print the empty tree's object name.",
            ));
        };
        return Ok(tree);
    }

    /// What:
    ///  List the index against a commit,
    ///  or against the empty tree when there is none,
    ///       limited to `pathspecs`.
    ///  `Option<&ObjectId>` is "a borrowed commit name or
    ///       nothing";
    ///  an empty `pathspecs` lists the whole index.
    /// Why:
    ///   `diff-index --cached` reads only the index and the tree,
    ///  never the
    ///       worktree.
    ///  Rename detection is off,
    ///  so a renamed file is one deleted path
    ///       and one added path,
    ///  each judged on its own.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async #listStaged(baseline: ObjectId | undefined, pathspecs: string[]): Promise<Buffer>
    /// ```
    fn list_staged(
        &self,
        baseline: Option<&ObjectId>,
        pathspecs: &[OsString],
    ) -> Result<Vec<u8>, CandidateError> {
        let tree: ObjectId = match baseline {
            Some(commit) => commit.clone(),
            None => self.empty_tree()?,
        };
        // `vec![...]` builds the fixed part of the command; the pathspecs follow `--`.
        let mut command: Vec<OsString> = vec![
            OsString::from("diff-index"),
            OsString::from("--cached"),
            OsString::from("--raw"),
            OsString::from("-z"),
            OsString::from("--no-renames"),
            OsString::from("--no-abbrev"),
            OsString::from(tree.as_str()),
            OsString::from("--"),
        ];
        command.extend_from_slice(pathspecs);
        return self.run_listing("diff-index", command.as_slice());
    }

    /// What:
    ///  List what one commit changed against each of its parents.
    /// Why:
    ///   `-m` lists a merge once per parent and `--root` lists a first commit
    ///       against nothing,
    ///  so every commit shape yields its changed paths.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async #listCommitted(commit: ObjectId): Promise<Buffer>
    /// ```
    fn list_committed(&self, commit: &ObjectId) -> Result<Vec<u8>, CandidateError> {
        return self.run_listing(
            "diff-tree",
            &[
                OsString::from("diff-tree"),
                OsString::from("--root"),
                OsString::from("--no-commit-id"),
                OsString::from("-r"),
                OsString::from("-z"),
                OsString::from("-m"),
                OsString::from("--no-renames"),
                OsString::from("--no-abbrev"),
                OsString::from(commit.as_str()),
            ],
        );
    }

    /// What:
    ///  The version of one source,
    ///  listed once per generation and shared afterwards.
    ///       `Rc::clone(..)` makes another handle to the same version without copying it.
    /// Why:
    ///   Every policy of a pass sees the same paths,
    ///  and asking again costs no process.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async version(source: CandidateSource): Promise<CandidateVersion>
    /// ```
    pub fn version(
        &mut self,
        source: &CandidateSource,
    ) -> Result<Rc<CandidateVersion>, CandidateError> {
        // `for (known, version) in &self.versions` destructures each borrowed pair.
        for (known, version) in &self.versions {
            if known == source {
                return Ok(Rc::clone(version));
            }
        }
        let output: Vec<u8> = match source {
            CandidateSource::StagedAgainstHead => {
                let head: Option<ObjectId> = self.baseline_commit()?;
                // `.as_ref()` turns `&Option<ObjectId>` into `Option<&ObjectId>`; `&[]` is no pathspec.
                self.list_staged(head.as_ref(), &[])?
            }
            CandidateSource::StagedAgainstCommit(commit) => self.list_staged(Some(commit), &[])?,
            CandidateSource::Committed(commit) => self.list_committed(commit)?,
        };
        let records: Vec<CandidateRecord> = parse_raw_records(output.as_slice())?;
        // `Rc::new(..)` moves the built version into shared storage.
        let version: Rc<CandidateVersion> = Rc::new(build_version(self.generation, records));
        self.versions.push((source.clone(), Rc::clone(&version)));
        return Ok(version);
    }

    /// What:
    ///  The index entries `pathspecs` select,
    ///  every conflict stage included,
    ///  with
    ///       repository-relative pathnames.
    /// Why:
    ///   `ls-files --stage` reads only the index,
    ///  so comparing two of these listings
    ///       shows exactly which entries an operation on that index changed.
    ///  `--full-name`
    ///       keeps pathnames repository-relative from any directory.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async stageRecords(pathspecs: string[]): Promise<StageRecord[]>
    /// ```
    pub fn stage_records(
        &mut self,
        pathspecs: &[OsString],
    ) -> Result<Vec<StageRecord>, CandidateError> {
        let mut command: Vec<OsString> = vec![
            OsString::from("ls-files"),
            OsString::from("--stage"),
            OsString::from("-z"),
            OsString::from("--full-name"),
            OsString::from("--"),
        ];
        command.extend_from_slice(pathspecs);
        let output: Vec<u8> = self.run_listing("ls-files", command.as_slice())?;
        return parse_stage_records(output.as_slice());
    }

    /// What:
    ///  The changed-path records of the index against `HEAD`,
    ///  limited to `pathspecs`;
    ///       against the empty tree when no commit exists yet.
    /// Why:
    ///   Which selected paths `HEAD` lacks makes them additions,
    ///  and which removed
    ///       paths it has makes them deletions.
    ///  The limit keeps a conflict elsewhere in the
    ///       index out of the listing.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async headRecords(pathspecs: string[]): Promise<CandidateRecord[]>
    /// ```
    pub fn head_records(
        &mut self,
        pathspecs: &[OsString],
    ) -> Result<Vec<CandidateRecord>, CandidateError> {
        let head: Option<ObjectId> = self.baseline_commit()?;
        let output: Vec<u8> = self.list_staged(head.as_ref(), pathspecs)?;
        return parse_raw_records(output.as_slice());
    }

    /// What: Every file of the `HEAD` commit with its mode and object; nothing when the
    ///       branch has no commit yet.
    /// Why:  A policy that compares the candidate state with `HEAD` reads `HEAD`'s copy of
    ///       a file without naming the path in an object request, and learns its mode.
    ///       `--full-tree` lists from the top level whatever directory Git runs in.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async headTreeRecords(): Promise<TreeRecord[]>
    /// ```
    pub fn head_tree_records(&mut self) -> Result<Vec<TreeRecord>, CandidateError> {
        let Some(head) = self.baseline_commit()? else {
            return Ok(Vec::new());
        };
        let output: Vec<u8> = self.run_listing(
            "ls-tree",
            &[
                OsString::from("ls-tree"),
                OsString::from("-r"),
                OsString::from("-z"),
                OsString::from("--full-tree"),
                OsString::from(head.as_str()),
            ],
        )?;
        return parse_tree_records(output.as_slice());
    }

    /// What: A version of the current generation built from records the caller derived.
    ///       `Vec<CandidateRecord>` is taken by value: the records move into the version.
    /// Why:  A predicted staging operation has no single listing command; its candidates
    ///       come from comparing index states. The version is not remembered by source,
    ///       because no source names it.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// derivedVersion(records: CandidateRecord[]): CandidateVersion
    /// ```
    pub fn derived_version(&mut self, records: Vec<CandidateRecord>) -> Rc<CandidateVersion> {
        return Rc::new(build_version(self.generation, records));
    }

    /// What:
    ///  The records of one commit's own delta, each against every parent (or the empty tree for a root commit).
    /// Why:
    ///   A pushed commit's changes are the content a manual-push scan reads; the listing is the one the
    ///  landed-delta source already uses.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// committedRecords(commit: ObjectId): CandidateRecord[]
    /// ```
    pub fn committed_records(
        &mut self,
        commit: &ObjectId,
    ) -> Result<Vec<CandidateRecord>, CandidateError> {
        let output: Vec<u8> = self.list_committed(commit)?;
        return parse_raw_records(output.as_slice());
    }

    /// What:
    ///  Every blob entry of one tree, as records that add each path.
    /// Why:
    ///   A push with no known published base scans the pushed tip's whole tree.
    ///   Submodule entries are not listed: their content is a commit name, which no content policy reads.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// treeRecords(tree: ObjectId): CandidateRecord[] // ls-tree -r, every blob as added
    /// ```
    pub fn tree_records(
        &mut self,
        tree: &ObjectId,
    ) -> Result<Vec<CandidateRecord>, CandidateError> {
        let listing: Vec<u8> = self.run_listing(
            "ls-tree",
            &[
                OsString::from("ls-tree"),
                OsString::from("--full-tree"),
                OsString::from("-r"),
                OsString::from("-z"),
                OsString::from(tree.as_str()),
            ],
        )?;
        let raw: Vec<u8> = added_raw_from_ls_tree(listing.as_slice())?;
        return parse_raw_records(raw.as_slice());
    }

    /// What:
    ///  The exact bytes a candidate holds in its version.
    ///       `Rc<[u8]>` is a shared read-only byte list.
    /// Why:
    ///   Bytes come from the object the listing named,
    ///  so an edit to the worktree
    ///       file after staging is invisible here.
    ///  A deleted path has no bytes.
    ///  A
    ///       gitlink has no blob in this repository,
    ///  so its bytes are the submodule
    ///       commit's name,
    ///  as in the TypeScript wrapper.
    ///  A candidate of a retired
    ///       version is refused instead of answered with bytes that may be outdated.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// async bytes(candidate: Candidate): Promise<Uint8Array>
    /// ```
    pub fn bytes(&mut self, candidate: &Candidate) -> Result<Rc<[u8]>, CandidateError> {
        if candidate.identity.generation != self.generation {
            return Err(CandidateError::new(
                CandidateFailure::StaleCandidate,
                "cli-git refused to read a candidate from a version that was invalidated by a fix or replay; \
                 list the version again and use its candidates.",
            ));
        }
        let Some(object) = candidate.object.as_ref() else {
            // `Rc::from(Vec::new())` is a shared empty byte list.
            return Ok(Rc::from(Vec::new()));
        };
        if candidate.mode == CandidateMode::Gitlink {
            // `.as_bytes()` views the name's text as bytes; `Rc::from(..)` copies them into shared storage.
            return Ok(Rc::from(object.as_str().as_bytes()));
        }
        return self.reader()?.blob(object);
    }

    /// What:
    ///  Retire every version listed so far.
    ///  `+= 1` advances the generation.
    /// Why:
    ///   A fix or replay rewrites index entries,
    ///  so earlier listings no longer
    ///       describe what would be committed.
    ///  Blob bytes stay remembered in the
    ///       reader:
    ///  an object name always denotes the same bytes.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// invalidate(): void { this.#versions = []; this.#generation += 1n; }
    /// ```
    pub fn invalidate(&mut self) {
        self.versions.clear();
        self.generation += 1;
    }
}

/// Repository controls stay out of the release executable.
#[cfg(test)]
#[path = "candidate_store_tests.rs"]
mod tests;

/// Committed versions,
///  invalidation and listing failures.
#[cfg(test)]
#[path = "candidate_store_lifecycle_tests.rs"]
mod lifecycle_tests;

/// The process-count bound and its per-file positive control.
#[cfg(test)]
#[path = "candidate_process_count_tests.rs"]
mod process_count_tests;

/// Unborn,
///  SHA-256 and private-index listings.
#[cfg(test)]
#[path = "candidate_store_baseline_tests.rs"]
mod baseline_tests;

/// Listings that must be refused.
#[cfg(test)]
#[path = "candidate_store_failure_tests.rs"]
mod failure_tests;

/// What:
///  Turn NUL-separated `ls-tree -r -z` records into raw diff records that add each blob.
/// Why:
///   The record parser is the one the diff listings use, so tree entries follow the same path and mode rules.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function addedRawFromLsTree(output: Uint8Array): Uint8Array;
/// ```
fn added_raw_from_ls_tree(output: &[u8]) -> Result<Vec<u8>, CandidateError> {
    let mut raw: Vec<u8> = Vec::new();
    for record in output
        .split(|byte| {
            return *byte == 0;
        })
        .filter(|record| {
            return !record.is_empty();
        })
    {
        let Some(tab) = record.iter().position(|byte| { return *byte == b'\t'; }) else {
            return Err(CandidateError::new(
                super::candidate_error::CandidateFailure::ListingMalformed,
                "cli-git could not list the pushed tree: an entry lacks its path separator.",
            ));
        };
        // The mode, type and object name are ASCII; the path stays the exact bytes Git printed.
        let metadata = String::from_utf8_lossy(&record[..tab]);
        let path: &[u8] = &record[tab + 1..];
        let fields: Vec<&str> = metadata.split(' ').collect();
        if fields.len() != 3 {
            return Err(CandidateError::new(
                super::candidate_error::CandidateFailure::ListingMalformed,
                "cli-git could not list the pushed tree: an entry lacks its mode, type or object name.",
            ));
        }
        if fields[1] != "blob" {
            continue;
        }
        let zeros = "0".repeat(fields[2].len());
        raw.extend_from_slice(format!(":000000 {} {zeros} {} A", fields[0], fields[2]).as_bytes());
        raw.push(0);
        raw.extend_from_slice(path);
        raw.push(0);
    }
    return Ok(raw);
}
/// The `HEAD` tree listing.
#[cfg(test)]
#[path = "candidate_store_tree_tests.rs"]
mod tree_tests;