greenbelay 6.2.0

A guard for Claude Code's tool calls: it reads each one before it runs, the work-process verdicts (plan review, implementation review, preview) included
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
//! `amont-agent suppressions --staged`: the commit gate for lint suppressions.
//!
//! It judges what the staged change adds, never the working tree, and never
//! what a file already held. Every failure to look is exit 2 (fail closed);
//! the only way past a blocking hit is a command-scoped
//! `git -c greenbelay.suppressions=allow commit`, which the person types and
//! the `suppressions-override` rule keeps from an agent.

use std::ffi::{OsStr, OsString};
use std::fmt;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};

use greenbelay_guard::rules::lint_suppression_added::{
    self as lint, FileChange, Hit, Origin, ReasonForm, RULE,
};
use greenbelay_guard::rules::Stance;
use greenbelay_guard::stance;

const OVERRIDE_KEY: &str = greenbelay_guard::config_key!("suppressions");
/// A blob over this is never read.
const MAX_BLOB: u64 = 4 * 1024 * 1024;
/// The sum of every requested blob; over it the scan is incomplete.
const BUDGET: u64 = 64 * 1024 * 1024;
/// Room for one `cat-file` header and its trailing newline.
const HEADER_ALLOWANCE: usize = 160;
const SKIPPED_NOTE: &str = "over 4 MiB";
/// How long any one git call gets.
const GIT_DEADLINE: Duration = Duration::from_secs(30);

/// Whether the person's command line carried `greenbelay.suppressions=allow`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Override {
    CommandScoped,
    Absent,
}

/// Why the staged change could not be fully looked at. `Display` is the line
/// printed on stderr.
#[derive(Debug)]
enum ScanError {
    /// A git call that failed, with its leading arguments and what it said.
    Git {
        command: String,
        detail: String,
    },
    BudgetExceeded,
    /// A `cat-file` answer shorter or odder than the names sent.
    IncompleteAnswer {
        answer: &'static str,
    },
    MissingName {
        name: String,
    },
    NewlineInPath,
    IndexHint {
        /// The variable that named it: `GREENGATE_INDEX_FILE` or 3.x's
        /// `AMONT_INDEX_FILE`.
        variable: &'static str,
        path: String,
    },
}

impl fmt::Display for ScanError {
    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScanError::Git { command, detail } => {
                write!(out, "suppressions: git {command} failed: {detail}")
            }
            ScanError::BudgetExceeded => {
                write!(
                    out,
                    "suppressions: scan budget exceeded (64 MiB); scan incomplete"
                )
            }
            ScanError::IncompleteAnswer { answer } => {
                write!(
                    out,
                    "suppressions: git cat-file failed: incomplete {answer} answer"
                )
            }
            ScanError::MissingName { name } => {
                write!(out, "suppressions: git cat-file failed: {name} missing")
            }
            ScanError::IndexHint { variable, path } => {
                write!(
                    out,
                    "suppressions: {variable} '{path}' is not an absolute path to an existing index file"
                )
            }
            ScanError::NewlineInPath => {
                write!(
                    out,
                    "suppressions: a staged path holds a newline; scan incomplete"
                )
            }
        }
    }
}

fn git_failed(command: &str, detail: impl Into<String>) -> ScanError {
    ScanError::Git {
        command: command.to_string(),
        detail: detail.into(),
    }
}

pub const HELP: &str = concat!("\
usage: ", greenbelay_guard::bin!(), " suppressions --staged

Judge the staged change: a lint suppression it adds (net of those it removes
anywhere) blocks unless it carries a reason in its tool's own reason form.
Exit 0: nothing blocking. 1: a blocking hit. 2: usage error or git failure.
A command-scoped `git -c greenbelay.suppressions=allow commit` passes; the
same key in repository or global config is ignored.
`GREENGATE_INDEX_FILE` is a bound pair with `GREENGATE_INDEX_GIT_DIR`, the
absolute per-worktree `git rev-parse --absolute-git-dir` of the owner
(greengate 4.0 supplies both; greengate 3.x and amont >= 1.70.0 supply the
same pair as `AMONT_INDEX_FILE` and `AMONT_INDEX_GIT_DIR`, read only when
`GREENGATE_INDEX_FILE` is unset or empty). A non-empty index hint is honoured over `GIT_INDEX_FILE` only
when that owner, canonicalized, equals this command's own canonical
`git rev-parse --absolute-git-dir` (the per-worktree git-dir, never the common
dir or the index location). A hint whose owner is absent, empty, relative,
nonexistent or foreign is ignored with one stderr note and `GIT_INDEX_FILE`
(or the default index) is kept. A matching but missing, relative or non-file
index exits 2 unless the override applies; so does a failed identity query.
The selected index, an external alternate one included, is only read.
A wrapper that switches repositories must clear and recompute both hints.
`--staged` may be repeated.
");

/// Entry point. `git_index` is `GIT_INDEX_FILE` as it was before `main`
/// scrubbed the environment: git hands a hook a temporary index on
/// `commit -a`, `commit <path>` and `--only`. `handoff` is
/// `GREENGATE_INDEX_FILE` with its owner `GREENGATE_INDEX_GIT_DIR` (or 3.x's
/// `AMONT_INDEX_*` pair): when non-empty and
/// owned by this repository's worktree it must name an existing file by an
/// absolute path, and it takes precedence over `git_index`; otherwise the
/// direct Git-hook fallback stays.
pub fn run(args: &[OsString], handoff: IndexHandoff, git_index: Option<OsString>) -> ExitCode {
    if args.iter().any(|a| a == "--help" || a == "-h") {
        emit(format_args!("{HELP}"));
        return ExitCode::SUCCESS;
    }
    if let Some(unknown) = args.iter().find(|a| *a != "--staged") {
        eprintln!(
            concat!(
                greenbelay_guard::bin!(),
                ": suppressions: unknown argument '{}'\n"
            ),
            unknown.to_string_lossy()
        );
        eprint!("{HELP}");
        return ExitCode::from(2);
    }
    if args.is_empty() {
        eprintln!(concat!(
            greenbelay_guard::bin!(),
            ": suppressions needs --staged\n"
        ));
        eprint!("{HELP}");
        return ExitCode::from(2);
    }
    let allowed = command_scoped_allow(
        std::env::var("GIT_CONFIG_PARAMETERS").ok().as_deref(),
        &|name| std::env::var(name).ok(),
    );
    if allowed == Override::Absent {
        note_ignored_override();
    }
    let scanned =
        select_index(handoff, git_index).and_then(|index_file| scan(index_file.as_deref()));
    match scanned {
        Err(why) if allowed == Override::CommandScoped => {
            eprintln!("{why}");
            eprintln!("suppressions: allowed by command-scoped {OVERRIDE_KEY}=allow");
            ExitCode::SUCCESS
        }
        Err(why) if stance::resolve(&RULE) == Stance::Observe => {
            eprintln!("{why}");
            eprintln!("suppressions: stance observe, not blocking");
            ExitCode::SUCCESS
        }
        Err(why) => {
            eprintln!("{why}");
            ExitCode::from(2)
        }
        Ok(scan) => judge(&scan, allowed),
    }
}

/// `GREENGATE_INDEX_FILE` and `GREENGATE_INDEX_GIT_DIR` as `main` captured
/// them: the index the gate's commit check selected, and the absolute
/// per-worktree git-dir of the repository that owns it. The pair is one hint;
/// the index is honoured only where the owner is this command's own
/// repository. greengate 3.x and amont hand the same pair as `AMONT_INDEX_*`,
/// read only when the new file name is unset or empty, and always as a pair,
/// so one name of each never mixes.
#[derive(Debug, Default)]
pub struct IndexHandoff {
    pub index_file: Option<OsString>,
    pub owner_git_dir: Option<OsString>,
    /// The pair's variable names, for what stderr says of a bad hint.
    pub names: (&'static str, &'static str),
}

/// The pair names, greengate 4.0's first. The old pair goes at the census of
/// greengate's hard rename.
const INDEX_PAIRS: [(&str, &str); 2] = [
    ("GREENGATE_INDEX_FILE", "GREENGATE_INDEX_GIT_DIR"),
    ("AMONT_INDEX_FILE", "AMONT_INDEX_GIT_DIR"),
];

impl IndexHandoff {
    /// The first pair whose file variable is set and non-empty in this
    /// process's environment, else none.
    pub fn from_env() -> Self {
        Self::from_lookup(|name| std::env::var_os(name))
    }

    fn from_lookup(lookup: impl Fn(&str) -> Option<OsString>) -> Self {
        INDEX_PAIRS
            .iter()
            .find_map(|&(file, dir)| {
                lookup(file)
                    .filter(|value| !value.is_empty())
                    .map(|index_file| Self {
                        index_file: Some(index_file),
                        owner_git_dir: lookup(dir),
                        names: (file, dir),
                    })
            })
            .unwrap_or_default()
    }
}

/// The index the scan reads. An absent or empty index hint keeps
/// `GIT_INDEX_FILE`. A non-empty one is honoured when its owning git-dir is
/// this command's own per-worktree git-dir, and then it must be an absolute
/// path to an existing file; an unbound or foreign hint is ignored with one
/// stderr note and `GIT_INDEX_FILE` is kept.
fn select_index(
    handoff: IndexHandoff,
    git_index: Option<OsString>,
) -> Result<Option<OsString>, ScanError> {
    let Some(index) = handoff.index_file.filter(|path| !path.is_empty()) else {
        return Ok(git_index);
    };
    let (variable, dir_variable) = handoff.names;
    let Some(owner) = usable_owner(handoff.owner_git_dir) else {
        note_ignored_hint(
            variable,
            &format!("{dir_variable} is absent, empty, relative or missing"),
        );
        return Ok(git_index);
    };
    if own_git_dir()? != owner {
        note_ignored_hint(variable, "it belongs to another repository");
        return Ok(git_index);
    }
    if Path::new(&index).is_absolute() && Path::new(&index).is_file() {
        Ok(Some(index))
    } else {
        Err(ScanError::IndexHint {
            variable,
            path: index.to_string_lossy().into_owned(),
        })
    }
}

/// The canonical owner git-dir, when the hint names an absolute existing one.
fn usable_owner(owner: Option<OsString>) -> Option<PathBuf> {
    let owner = PathBuf::from(owner.filter(|path| !path.is_empty())?);
    if !owner.is_absolute() {
        return None;
    }
    owner.canonicalize().ok()
}

fn note_ignored_hint(variable: &str, reason: &str) {
    eprintln!("suppressions: {variable} ignored: {reason}; keeping the direct index");
}

/// This command's own per-worktree git-dir, canonical, from git's bytes with
/// the one protocol newline removed.
fn own_git_dir() -> Result<PathBuf, ScanError> {
    let mut answer = checked(&["rev-parse", "--absolute-git-dir"], None, None, 1 << 16)?;
    if answer.last() == Some(&b'\n') {
        answer.pop();
    }
    let reported = path_from_git_bytes(answer)?;
    reported
        .canonicalize()
        .map_err(|error| git_failed("rev-parse", format!("{}: {error}", reported.display())))
}

#[cfg(unix)]
fn path_from_git_bytes(bytes: Vec<u8>) -> Result<PathBuf, ScanError> {
    use std::os::unix::ffi::OsStringExt;
    Ok(PathBuf::from(OsString::from_vec(bytes)))
}

#[cfg(not(unix))]
fn path_from_git_bytes(bytes: Vec<u8>) -> Result<PathBuf, ScanError> {
    String::from_utf8(bytes)
        .map(PathBuf::from)
        .map_err(|_| git_failed("rev-parse", "git-dir is not valid UTF-8"))
}

/// The gate's stdout, written by the module that owns it rather than by a
/// print macro, so the hook path's one-writer check stays true.
fn emit(text: std::fmt::Arguments<'_>) {
    let _ = std::io::stdout().write_fmt(text); // a closed stdout leaves the exit status as the answer
}

struct Scan {
    files: usize,
    skipped: usize,
    blocking: Vec<(PathBuf, Hit)>,
    kept: usize,
}

fn judge(scan: &Scan, allowed: Override) -> ExitCode {
    if scan.blocking.is_empty() {
        let mut line = format!(
            "suppressions: {} files, 0 added ({} kept with a reason)",
            scan.files, scan.kept
        );
        if scan.skipped > 0 {
            line.push_str(&format!(", {} skipped ({SKIPPED_NOTE})", scan.skipped));
        }
        emit(format_args!("{line}\n"));
        return ExitCode::SUCCESS;
    }
    for (path, hit) in &scan.blocking {
        emit(format_args!(
            "{}:{}: {}: {}\n",
            path.display(),
            hit.line,
            shown(hit),
            fix(hit)
        ));
    }
    if allowed == Override::CommandScoped {
        eprintln!(
            "suppressions: {} blocking hit(s) let through by command-scoped {OVERRIDE_KEY}=allow",
            scan.blocking.len()
        );
        return ExitCode::SUCCESS;
    }
    if stance::resolve(&RULE) == Stance::Observe {
        eprintln!("suppressions: stance observe, not blocking");
        return ExitCode::SUCCESS;
    }
    eprintln!("fix the cause, or stop and tell the person");
    eprintln!("for the person only (an agent's attempt is refused):");
    eprintln!("  git -c {OVERRIDE_KEY}=allow commit");
    ExitCode::from(1)
}

fn shown(hit: &Hit) -> String {
    match &hit.origin {
        Origin::Comment { shown } => shown.clone(),
        Origin::Setting { section } => {
            let section = if section.is_empty() {
                String::new()
            } else {
                format!("[{section}] ")
            };
            format!("loosens {section}{} = {}", hit.kind, hit.codes)
        }
    }
}

/// What turns a blocking marker into a passing one, in its tool's own form.
fn fix(hit: &Hit) -> &'static str {
    if matches!(hit.origin, Origin::Setting { .. }) {
        return "loosened lint configuration, never passes";
    }
    match lint::reason_form(hit) {
        ReasonForm::Never => "blanket, never passes",
        ReasonForm::Attribute => "add reason = \"…\"",
        ReasonForm::AfterTag => "add the reason as text after the tag",
        ReasonForm::SlashSlash => "add // <why>",
        ReasonForm::Hash => "add # <why>",
        ReasonForm::TrailingDashes => "add -- <why>",
    }
}

/// Whether the command line itself said `greenbelay.suppressions=allow`:
/// `git -c` arrives in `GIT_CONFIG_PARAMETERS`, the environment form in
/// `GIT_CONFIG_COUNT` with `GIT_CONFIG_KEY_<n>` / `GIT_CONFIG_VALUE_<n>`.
/// The last value set wins.
fn command_scoped_allow(
    parameters: Option<&str>,
    env: &dyn Fn(&str) -> Option<String>,
) -> Override {
    let mut last: Option<String> = None;
    for word in parameters.map(shell_words).unwrap_or_default() {
        if let Some((key, value)) = word.split_once('=') {
            if key.eq_ignore_ascii_case(OVERRIDE_KEY) {
                last = Some(value.to_string());
            }
        }
    }
    let count = env("GIT_CONFIG_COUNT")
        .and_then(|c| c.trim().parse::<usize>().ok())
        .unwrap_or(0);
    for n in 0..count {
        let key = env(&format!("GIT_CONFIG_KEY_{n}"));
        if key.is_some_and(|k| k.eq_ignore_ascii_case(OVERRIDE_KEY)) {
            last = env(&format!("GIT_CONFIG_VALUE_{n}"));
        }
    }
    if last.is_some_and(|value| value.trim().eq_ignore_ascii_case("allow")) {
        Override::CommandScoped
    } else {
        Override::Absent
    }
}

/// Words of git's single-quote-escaped `GIT_CONFIG_PARAMETERS`
/// (`'key=value' 'key'='value'`).
fn shell_words(text: &str) -> Vec<String> {
    let mut words = Vec::new();
    let mut word = String::new();
    let mut started = false;
    let mut quoted = false;
    let mut chars = text.chars();
    while let Some(c) = chars.next() {
        match (quoted, c) {
            (true, '\'') => quoted = false,
            (true, c) => word.push(c),
            (false, '\'') => {
                quoted = true;
                started = true;
            }
            (false, '\\') => {
                if let Some(next) = chars.next() {
                    word.push(next);
                    started = true;
                }
            }
            (false, c) if c.is_whitespace() => {
                if started {
                    words.push(std::mem::take(&mut word));
                    started = false;
                }
            }
            (false, c) => {
                word.push(c);
                started = true;
            }
        }
    }
    if started {
        words.push(word);
    }
    words
}

/// A value in repository, global or system config is ignored; say so.
fn note_ignored_override() {
    let Ok(out) = run_git(
        &["config", "--show-scope", "--get-all", OVERRIDE_KEY],
        None,
        None,
        1 << 16,
    ) else {
        return; // a note about ignored config is a courtesy; the scan reports git failures itself
    };
    for line in String::from_utf8_lossy(&out.stdout).lines() {
        let Some((scope, value)) = line.split_once('\t') else {
            continue;
        };
        if scope != "command" {
            eprintln!(
                "suppressions: {OVERRIDE_KEY}={value} in {scope} config is ignored; only `git -c {OVERRIDE_KEY}=allow` counts"
            );
        }
    }
}

struct GitOutput {
    code: i32,
    stdout: Vec<u8>,
    stderr: String,
}

/// Run git in the current directory with `GIT_INDEX_FILE` as the hook was
/// given it. stdout is read under `ceiling` bytes; more kills git and is an
/// error, never a short answer. The call has [`GIT_DEADLINE`] to finish.
fn run_git(
    args: &[&str],
    input: Option<Vec<u8>>,
    index_file: Option<&OsStr>,
    ceiling: usize,
) -> Result<GitOutput, ScanError> {
    let command = args.iter().take(2).copied().collect::<Vec<_>>().join(" ");
    let mut cmd = Command::new("git");
    cmd.args(args)
        .env("GIT_OPTIONAL_LOCKS", "0")
        .env("GIT_TERMINAL_PROMPT", "0")
        .stdin(if input.is_some() {
            Stdio::piped()
        } else {
            Stdio::null()
        })
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    if let Some(index) = index_file {
        cmd.env("GIT_INDEX_FILE", index);
    }
    let deadline = Instant::now() + GIT_DEADLINE;
    let mut child = cmd
        .spawn()
        .map_err(|error| git_failed(&command, error.to_string()))?;
    if let (Some(bytes), Some(mut stdin)) = (input, child.stdin.take()) {
        // Detached: a grandchild holding the pipe open must not hold this call.
        std::thread::spawn(move || {
            let _ = stdin.write_all(&bytes); // git stopping early is read from its exit status
        });
    }
    let stderr_answer = child.stderr.take().map(|mut pipe| {
        let (sender, receiver) = mpsc::channel();
        std::thread::spawn(move || {
            let mut text = String::new();
            let _ = pipe.by_ref().take(1 << 16).read_to_string(&mut text); // diagnostics only; a lossy read is enough
            let _ = sender.send(text); // a receiver past its deadline has gone
        });
        receiver
    });
    let stdout_answer = child.stdout.take().map(|pipe| {
        let (sender, receiver) = mpsc::channel();
        std::thread::spawn(move || {
            let mut bytes = Vec::new();
            let read = pipe.take(ceiling as u64 + 1).read_to_end(&mut bytes);
            let _ = sender.send((bytes, read.is_ok())); // a receiver past its deadline has gone
        });
        receiver
    });
    let timed_out = || git_failed(&command, "timed out");
    let status = loop {
        match child.try_wait() {
            Ok(Some(status)) => break status,
            Ok(None) => {}
            Err(error) => return Err(git_failed(&command, error.to_string())),
        }
        if Instant::now() >= deadline {
            let _ = child.kill(); // the answer is already refused
            let _ = child.wait(); // reaping only
            return Err(timed_out());
        }
        std::thread::sleep(Duration::from_millis(2));
    };
    let remaining = || deadline.saturating_duration_since(Instant::now());
    let (stdout, read_whole) = match stdout_answer {
        Some(receiver) => receiver
            .recv_timeout(remaining())
            .map_err(|_| timed_out())?,
        None => (Vec::new(), true),
    };
    if !read_whole || stdout.len() > ceiling {
        return Err(git_failed(
            &command,
            format!("output over its {ceiling}-byte ceiling"),
        ));
    }
    let stderr = match stderr_answer {
        Some(receiver) => receiver
            .recv_timeout(remaining())
            .map_err(|_| timed_out())?,
        None => String::new(),
    };
    let Some(code) = status.code() else {
        return Err(git_failed(&command, "killed by a signal"));
    };
    Ok(GitOutput {
        code,
        stdout,
        stderr,
    })
}

fn first_line(text: &str) -> String {
    text.lines().next().unwrap_or("").trim().to_string()
}

fn checked(
    args: &[&str],
    input: Option<Vec<u8>>,
    index_file: Option<&OsStr>,
    ceiling: usize,
) -> Result<Vec<u8>, ScanError> {
    let out = run_git(args, input, index_file, ceiling)?;
    if out.code != 0 {
        let command = args.iter().take(2).copied().collect::<Vec<_>>().join(" ");
        return Err(git_failed(&command, first_line(&out.stderr)));
    }
    Ok(out.stdout)
}

/// What `git diff --name-status` says happened to a path.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Staged {
    Added,
    Modified,
    Deleted,
    Renamed,
}

impl Staged {
    /// `C` (a copy) is not asked for and is not accepted.
    fn from_letter(letter: u8) -> Option<Staged> {
        match letter {
            b'A' => Some(Staged::Added),
            b'M' => Some(Staged::Modified),
            b'D' => Some(Staged::Deleted),
            b'R' => Some(Staged::Renamed),
            _ => None,
        }
    }

    /// Whether HEAD holds the old path.
    fn has_before(self) -> bool {
        match self {
            Staged::Added => false,
            Staged::Modified | Staged::Deleted | Staged::Renamed => true,
        }
    }

    fn has_after(self) -> bool {
        match self {
            Staged::Deleted => false,
            Staged::Added | Staged::Modified | Staged::Renamed => true,
        }
    }
}

struct Entry {
    status: Staged,
    old: Vec<u8>,
    new: Vec<u8>,
}

#[derive(Clone, Copy, PartialEq)]
enum Side {
    Before,
    After,
}

struct Request {
    entry: usize,
    side: Side,
    name: Vec<u8>,
}

fn parse_name_status(raw: &[u8]) -> Result<Vec<Entry>, ScanError> {
    let mut tokens = raw.split(|b| *b == 0).filter(|t| !t.is_empty());
    let mut entries = Vec::new();
    while let Some(token) = tokens.next() {
        let letter = token[0];
        let Some(status) = Staged::from_letter(letter) else {
            return Err(git_failed(
                "diff",
                format!("unexpected status {}", letter as char),
            ));
        };
        let mut next = || {
            tokens
                .next()
                .map(<[u8]>::to_vec)
                .ok_or_else(|| git_failed("diff", "truncated name-status"))
        };
        let (old, new) = match status {
            Staged::Renamed => {
                let old = next()?;
                let new = next()?;
                (old, new)
            }
            Staged::Added | Staged::Modified | Staged::Deleted => {
                let path = next()?;
                (path.clone(), path)
            }
        };
        entries.push(Entry { status, old, new });
    }
    Ok(entries)
}

fn path_of(bytes: &[u8]) -> PathBuf {
    PathBuf::from(String::from_utf8_lossy(bytes).into_owned())
}

/// An entry is worth reading when either side is an examined kind or has no
/// extension (a shell script can only be told by its content).
fn wanted(entry: &Entry) -> bool {
    [&entry.old, &entry.new].iter().any(|bytes| {
        let path = path_of(bytes);
        lint::examined(&path) || path.extension().is_none()
    })
}

fn scan(index_file: Option<&OsStr>) -> Result<Scan, ScanError> {
    let head = run_git(
        &["rev-parse", "--verify", "--quiet", "HEAD"],
        None,
        index_file,
        1 << 12,
    )?;
    let head_exists = match head.code {
        0 => true,
        1 => false,
        _ => return Err(git_failed("rev-parse", first_line(&head.stderr))),
    };
    let listing = checked(
        &[
            "diff",
            "--cached",
            "--name-status",
            "-z",
            "-M",
            "--diff-filter=AMRD",
        ],
        None,
        index_file,
        64 * 1024 * 1024,
    )?;
    let entries: Vec<Entry> = parse_name_status(&listing)?
        .into_iter()
        .filter(wanted)
        .collect();

    let mut requests = Vec::new();
    for (entry_index, entry) in entries.iter().enumerate() {
        if head_exists && entry.status.has_before() {
            let mut name = b"HEAD:".to_vec();
            name.extend_from_slice(&entry.old);
            requests.push(Request {
                entry: entry_index,
                side: Side::Before,
                name,
            });
        }
        if entry.status.has_after() {
            let mut name = b":".to_vec();
            name.extend_from_slice(&entry.new);
            requests.push(Request {
                entry: entry_index,
                side: Side::After,
                name,
            });
        }
    }
    if requests.iter().any(|r| r.name.contains(&b'\n')) {
        return Err(ScanError::NewlineInPath);
    }

    let sizes = blob_sizes(&requests, index_file)?;
    let mut skipped_entries = vec![false; entries.len()];
    for (request, size) in requests.iter().zip(&sizes) {
        if *size > MAX_BLOB {
            skipped_entries[request.entry] = true;
        }
    }
    for (entry, skipped) in entries.iter().zip(&skipped_entries) {
        if *skipped {
            eprintln!(
                "suppressions: skipped {} ({SKIPPED_NOTE})",
                path_of(&entry.new).display()
            );
        }
    }
    let wanted_reads: Vec<(&Request, u64)> = requests
        .iter()
        .zip(sizes.iter().copied())
        .filter(|(r, _)| !skipped_entries[r.entry])
        .collect();
    let total: u64 = wanted_reads.iter().map(|(_, size)| *size).sum();
    if total > BUDGET {
        return Err(ScanError::BudgetExceeded);
    }
    let texts = read_blobs(&wanted_reads, total, index_file)?;

    let mut before = vec![String::new(); entries.len()];
    let mut after = vec![String::new(); entries.len()];
    for ((request, _), text) in wanted_reads.iter().zip(texts) {
        match request.side {
            Side::Before => before[request.entry] = text,
            Side::After => after[request.entry] = text,
        }
    }
    let kept_entries: Vec<usize> = (0..entries.len())
        .filter(|i| !skipped_entries[*i])
        .collect();
    let paths: Vec<(PathBuf, PathBuf)> = entries
        .iter()
        .map(|e| (path_of(&e.old), path_of(&e.new)))
        .collect();
    let changes: Vec<FileChange<'_>> = kept_entries
        .iter()
        .map(|&i| FileChange {
            old_path: paths[i].0.as_path(),
            before: before[i].as_str(),
            new_path: paths[i].1.as_path(),
            after: after[i].as_str(),
        })
        .collect();

    let mut blocking = Vec::new();
    let mut kept = 0;
    for added in lint::net_added(&changes) {
        if added.reasoned {
            kept += 1;
        } else {
            blocking.push((added.path, added.hit));
        }
    }
    Ok(Scan {
        files: kept_entries.len(),
        skipped: skipped_entries.iter().filter(|s| **s).count(),
        blocking,
        kept,
    })
}

/// One `cat-file --batch-check` for every requested name. A name git reports
/// missing is an error: the diff said it exists.
fn blob_sizes(requests: &[Request], index_file: Option<&OsStr>) -> Result<Vec<u64>, ScanError> {
    if requests.is_empty() {
        return Ok(Vec::new());
    }
    let input = names_input(requests.iter().map(|r| r.name.as_slice()));
    let ceiling = input.len() + requests.len() * HEADER_ALLOWANCE + 1024;
    let out = checked(
        &["cat-file", "--batch-check"],
        Some(input),
        index_file,
        ceiling,
    )?;
    let text = String::from_utf8_lossy(&out);
    let lines: Vec<&str> = text.lines().collect();
    if lines.len() != requests.len() {
        return Err(ScanError::IncompleteAnswer {
            answer: "batch-check",
        });
    }
    let mut sizes = Vec::with_capacity(requests.len());
    for (request, line) in requests.iter().zip(lines) {
        if line.ends_with(" missing") {
            return Err(ScanError::MissingName {
                name: String::from_utf8_lossy(&request.name).into_owned(),
            });
        }
        let size = line
            .rsplit(' ')
            .next()
            .and_then(|s| s.parse::<u64>().ok())
            .ok_or_else(|| git_failed("cat-file", format!("unreadable answer `{line}`")))?;
        sizes.push(size);
    }
    Ok(sizes)
}

fn names_input<'a>(names: impl Iterator<Item = &'a [u8]>) -> Vec<u8> {
    let mut input = Vec::new();
    for name in names {
        input.extend_from_slice(name);
        input.push(b'\n');
    }
    input
}

/// The bounded content read. The ceiling covers payloads and protocol
/// headers; anything beyond, or short of, what the sizes promised is exit 2.
fn read_blobs(
    reads: &[(&Request, u64)],
    total: u64,
    index_file: Option<&OsStr>,
) -> Result<Vec<String>, ScanError> {
    if reads.is_empty() {
        return Ok(Vec::new());
    }
    let input = names_input(reads.iter().map(|(r, _)| r.name.as_slice()));
    let ceiling = usize::try_from(total).unwrap_or(usize::MAX / 2)
        + input.len()
        + reads.len() * HEADER_ALLOWANCE
        + 1024;
    let out = checked(&["cat-file", "--batch"], Some(input), index_file, ceiling)?;
    let incomplete = || ScanError::IncompleteAnswer { answer: "batch" };
    let mut at = 0;
    let mut texts = Vec::with_capacity(reads.len());
    for (_, size) in reads {
        let newline = out[at..]
            .iter()
            .position(|b| *b == b'\n')
            .ok_or_else(incomplete)?;
        let header = String::from_utf8_lossy(&out[at..at + newline]).into_owned();
        at += newline + 1;
        let announced = header
            .rsplit(' ')
            .next()
            .and_then(|s| s.parse::<u64>().ok())
            .ok_or_else(incomplete)?;
        if announced != *size {
            return Err(incomplete());
        }
        let length = usize::try_from(announced).map_err(|_| incomplete())?;
        if at + length + 1 > out.len() {
            return Err(incomplete());
        }
        texts.push(String::from_utf8_lossy(&out[at..at + length]).into_owned());
        at += length + 1;
    }
    Ok(texts)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn handoff_of(vars: &[(&str, &str)]) -> IndexHandoff {
        IndexHandoff::from_lookup(|name| {
            vars.iter()
                .find(|(k, _)| *k == name)
                .map(|(_, v)| OsString::from(v))
        })
    }

    #[test]
    fn the_new_index_pair_wins_and_the_old_pair_is_read_alone() {
        let new = handoff_of(&[
            ("GREENGATE_INDEX_FILE", "/n/index"),
            ("GREENGATE_INDEX_GIT_DIR", "/n/.git"),
            ("AMONT_INDEX_FILE", "/o/index"),
            ("AMONT_INDEX_GIT_DIR", "/o/.git"),
        ]);
        assert_eq!(new.index_file.as_deref(), Some("/n/index".as_ref()));
        assert_eq!(new.owner_git_dir.as_deref(), Some("/n/.git".as_ref()));

        let old = handoff_of(&[
            ("AMONT_INDEX_FILE", "/o/index"),
            ("AMONT_INDEX_GIT_DIR", "/o/.git"),
        ]);
        assert_eq!(old.index_file.as_deref(), Some("/o/index".as_ref()));
        assert_eq!(old.owner_git_dir.as_deref(), Some("/o/.git".as_ref()));

        assert_eq!(
            new.names,
            ("GREENGATE_INDEX_FILE", "GREENGATE_INDEX_GIT_DIR")
        );
        assert_eq!(old.names, ("AMONT_INDEX_FILE", "AMONT_INDEX_GIT_DIR"));

        // Never one name of each: the new file without its git-dir keeps none.
        let mixed = handoff_of(&[
            ("GREENGATE_INDEX_FILE", "/n/index"),
            ("AMONT_INDEX_GIT_DIR", "/o/.git"),
        ]);
        assert_eq!(mixed.owner_git_dir, None);

        // An empty new name is unset: the old pair is read.
        let cleared = handoff_of(&[
            ("GREENGATE_INDEX_FILE", ""),
            ("AMONT_INDEX_FILE", "/o/index"),
            ("AMONT_INDEX_GIT_DIR", "/o/.git"),
        ]);
        assert_eq!(cleared.index_file.as_deref(), Some("/o/index".as_ref()));
        assert_eq!(cleared.names.0, "AMONT_INDEX_FILE");

        assert!(handoff_of(&[]).index_file.is_none());
    }

    fn no_env(_: &str) -> Option<String> {
        None
    }

    #[test]
    fn both_parameter_formats_are_read() {
        assert_eq!(
            command_scoped_allow(Some("'greenbelay.suppressions=allow'"), &no_env),
            Override::CommandScoped
        );
        assert_eq!(
            command_scoped_allow(Some("'greenbelay.suppressions'='allow'"), &no_env),
            Override::CommandScoped
        );
        assert_eq!(
            command_scoped_allow(Some("'greenbelay.suppressions=deny'"), &no_env),
            Override::Absent
        );
        assert_eq!(command_scoped_allow(None, &no_env), Override::Absent);
    }

    #[test]
    fn the_numbered_environment_form_is_read() {
        let env = |name: &str| match name {
            "GIT_CONFIG_COUNT" => Some("2".to_string()),
            "GIT_CONFIG_KEY_1" => Some("greenbelay.suppressions".to_string()),
            "GIT_CONFIG_VALUE_1" => Some("allow".to_string()),
            "GIT_CONFIG_KEY_0" => Some("user.name".to_string()),
            "GIT_CONFIG_VALUE_0" => Some("x".to_string()),
            _ => None,
        };
        assert_eq!(command_scoped_allow(None, &env), Override::CommandScoped);
    }
}