oakum 0.3.2

A polyglot release tool that derives dependent version bumps from the dependency graph
Documentation
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
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
//! Every git child oakum spawns.
//!
//! The environment, the outcome vocabulary, and the stdout decoding live here
//! rather than at each call site. Written per site, all three drift: prompt
//! suppression reached three of sixteen children before okm-6mz, and whether a
//! failure was `unverified` or a plain error was decided by which module the
//! caller happened to be in.
//!
//! [`Op`] and the axes that describe one live in [`op`]. A call here turns an
//! operation into a shape once and reads every axis off that one value, so the
//! argv a child runs and the class its failure takes cannot come from two
//! different answers.

mod env;
#[cfg(test)]
mod fake;
mod op;
mod reach;

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Mutex, OnceLock};

use cap_std::fs::Dir;

use super::CliError;
pub(super) use op::Op;
use op::{Answer, Contact, Direction, OpShape, SKIP_CHECKS_ATOM};

/// How an accessor decides whether stdout carries an answer. The two disagree,
/// and the guard has to ask the one doing the reading: `text` and
/// `optional_text` trim, so a lone `\x0B` reaches the caller as `""`, while
/// `paths` keeps every non-empty NUL record byte-for-byte, because a file named
/// `" "` is a filename and not silence.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Reads {
    Text,
    Paths,
}

/// The trace2 channels, whose destination git config can set even when the
/// environment does not.
const TRACE2: [&str; 3] = ["GIT_TRACE2", "GIT_TRACE2_EVENT", "GIT_TRACE2_PERF"];

/// Whether a trace setting sends its output somewhere other than our stderr.
/// Measured on git 2.55: `1`, `2`, and `true` all print to stderr, an
/// unrecognised value warns and then prints to stderr, and only an absolute
/// path is written to a file. Anything that is not a path is dropped, so a
/// caller tracing to a file — `tests/reachable_tags.rs` counts children that
/// way — keeps it.
fn traces_to_a_file(value: &std::ffi::OsStr) -> bool {
    Path::new(value).is_absolute()
}

/// Drops inherited trace settings that would land on our stderr, where every
/// rule above would read them as a diagnostic: with `GIT_TRACE=1` exported a
/// healthy repository reports `unverified`, and `GIT_TRACE_PACKET=1` writes
/// 2704 bytes during an `ls-remote` that legitimately found no tags.
///
/// Matched by prefix rather than against a list. Git ships more than fifteen of
/// these and adds more; a list is a thing to be caught out by, one variable at
/// a time.
pub(super) fn untrace(command: &mut Command) {
    untrace_from(command, std::env::vars_os());
}

/// Split from [`untrace`] so a test can state an environment instead of
/// mutating the process's own: `cargo test` runs tests as threads of one
/// process, and a test that sets `GIT_TRACE` reaches every git child any other
/// test spawns concurrently.
fn untrace_from(
    command: &mut Command,
    env: impl IntoIterator<Item = (std::ffi::OsString, std::ffi::OsString)>,
) {
    let mut keeps_a_file = [false; TRACE2.len()];
    for (name, value) in env {
        let Some(name) = name.to_str() else { continue };
        if !name.starts_with("GIT_TRACE") {
            continue;
        }
        let to_a_file = traces_to_a_file(&value);
        if let Some(at) = TRACE2.iter().position(|trace2| *trace2 == name) {
            keeps_a_file[at] = to_a_file;
        }
        if !to_a_file {
            command.env_remove(name);
        }
    }
    // Trace2 also takes its destination from git config, which no edit to the
    // environment reaches and which `-c` is too late to change: trace2
    // initialises before the option is parsed. Setting the variable off does
    // reach it. Measured with `trace2.normalTarget=2` in global config: a
    // `status` writes 585 bytes to stderr, removing the variable leaves it at
    // 585, `-c trace2.normalTarget=0` raises it to 610 by tracing the option
    // itself, and setting the variable to 0 takes it to nothing.
    //
    // Read from `env` rather than the process, so this stays a function of its
    // argument: consulting both let an exported `GIT_TRACE2_EVENT` decide the
    // answer for an environment that never mentioned it.
    for (at, name) in TRACE2.iter().enumerate() {
        if !keeps_a_file[at] {
            command.env(name, "0");
        }
    }
}

/// Stderr that says git could not read part of what it was walking, as against
/// the noise it makes while answering completely.
///
/// [`Answer::Whole`] is fail-closed on a short listing and open on a whole one,
/// because the two are measurably different here. An unreadable
/// `.git/info/exclude` warns twice and lists exactly what a readable one does;
/// refusing on that turns an unrelated permission bit into a red gate, with a
/// message that says part of the tree went unread when none of it did.
///
/// One form is reachable and the set holds four, matched without case. In git
/// 2.55.0's `dir.c` the only diagnostic that shortens the walk is
/// `warning_errno(_("could not open directory '%s'"))` at line 2587; its
/// siblings there report sparse-checkout and pattern-file trouble and leave the
/// listing whole. The other spellings in the binary — `cannot opendir`,
/// `cannot lstat` — are `die_errno` in `entry.c`, so they arrive with a
/// non-zero exit that never reaches this branch.
///
/// Kept as a set because a translated or re-spelled message is the failure mode
/// that matters, and the set costs nothing. What holds it honest is
/// `a_partial_worktree_listing_is_not_read_as_a_whole_one`, which seals a real
/// directory and runs the installed git: a git that words this differently
/// turns that test red rather than opening the guard quietly.
///
/// Unmatched stderr is passed over exactly as it was before [`Answer::Whole`]
/// existed, so a form nobody has seen costs the silence that was already there
/// rather than a false refusal on the command this repository gates with.
fn hides_part_of_the_tree(said: &str) -> bool {
    let said = said.to_ascii_lowercase();
    [
        "could not open directory",
        "cannot opendir",
        "opendir(",
        "cannot lstat",
    ]
    .iter()
    .any(|form| said.contains(form))
}

/// `None` when a record is not UTF-8. Kept separate from the run so the decoding
/// is testable without a repository.
fn split_nul_paths(stdout: &[u8]) -> Option<Vec<String>> {
    stdout
        .split(|byte| *byte == 0)
        .filter(|record| !record.is_empty())
        .map(|record| String::from_utf8(record.to_vec()).ok())
        .collect()
}

/// What a git child said.
///
/// `std::process::Output` would do, except that `ExitStatus` has no portable
/// constructor: `ExitStatusExt::from_raw` is a per-target extension trait, so a
/// fake built on it needs a `cfg` branch per platform — the portability problem
/// [`fake`] exists to escape. Every rule below reads the exit code, so carrying
/// our own leaves them exercisable without a process.
#[derive(Debug)]
pub(super) struct Reply {
    /// `None` when a signal killed the child before it could exit.
    code: Option<i32>,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
}

impl Reply {
    fn succeeded(&self) -> bool {
        self.code == Some(0)
    }

    /// Whether stdout carries anything the calling accessor would keep, judged
    /// exactly as that accessor judges it. Any disagreement is a hole in one
    /// direction or the other, and both have been live here.
    fn spoke(&self, reads: Reads) -> bool {
        match reads {
            Reads::Text => !String::from_utf8_lossy(&self.stdout).trim().is_empty(),
            Reads::Paths => self
                .stdout
                .split(|byte| *byte == 0)
                .any(|record| !record.is_empty()),
        }
    }

    /// Git reports "absent" or "no" as exit 1 with both streams empty. A wrapper
    /// that exits 1 with a diagnostic did not look, which is not the same thing.
    fn said_no(&self) -> bool {
        self.code == Some(1) && self.stdout.is_empty() && self.stderr.is_empty()
    }

    /// Git's own words, when it wrote any. A child that failed after explaining
    /// itself on stdout counts: reading stderr alone rendered that as `exit 128
    /// with no diagnostic`, which tells a reader git said nothing and sends
    /// them to inspect a git that did explain itself. Only for a failure, so a
    /// successful op's stdout data never leaks into a message.
    fn diagnostic(&self) -> Option<String> {
        let wrote = |bytes: &[u8]| {
            let text = String::from_utf8_lossy(bytes).trim().to_owned();
            (!text.is_empty()).then_some(text)
        };
        wrote(&self.stderr).or_else(|| (!self.succeeded()).then(|| wrote(&self.stdout)).flatten())
    }

    /// The status first, then git's own words when it wrote any: `git push`
    /// writes its success banner to stderr before a signal can kill it, so a
    /// diagnostic alone renders a signal death as that banner (measured).
    fn detail(&self) -> String {
        let status = match self.code {
            Some(code) => format!("exit {code}"),
            None => String::from("terminated by a signal"),
        };
        match self.diagnostic() {
            Some(said) => format!("{status}: {said}"),
            None => format!("{status} with no diagnostic"),
        }
    }
}

impl From<std::process::Output> for Reply {
    fn from(output: std::process::Output) -> Self {
        Self {
            code: output.status.code(),
            stdout: output.stdout,
            stderr: output.stderr,
        }
    }
}

/// The answers a test scripts. Named for what git did, not for the exit code,
/// so a script reads as the situation it stands for.
#[cfg(test)]
impl Reply {
    /// Exit 0, having written this.
    pub(super) fn said(stdout: impl Into<Vec<u8>>) -> Self {
        Self {
            code: Some(0),
            stdout: stdout.into(),
            stderr: Vec::new(),
        }
    }

    /// Exit 1 with both streams empty: how git says "no" or "absent".
    pub(super) fn absent() -> Self {
        Self {
            code: Some(1),
            stdout: Vec::new(),
            stderr: Vec::new(),
        }
    }

    /// Exit 0 with a warning and no answer — the shape that must not read as an
    /// empty result.
    pub(super) fn warned(stderr: &str) -> Self {
        Self {
            code: Some(0),
            stdout: Vec::new(),
            stderr: stderr.as_bytes().to_vec(),
        }
    }

    /// Exit 0 having answered and warned, which git does: `for-each-ref` lists
    /// the good tags and reports a broken ref on stderr in the same run. The
    /// answer stands.
    pub(super) fn said_and_warned(stdout: impl Into<Vec<u8>>, stderr: &str) -> Self {
        Self {
            code: Some(0),
            stdout: stdout.into(),
            stderr: stderr.as_bytes().to_vec(),
        }
    }

    /// Exit non-zero with a diagnostic, which is a failure to look rather than
    /// an answer of "no".
    ///
    /// # Panics
    ///
    /// On a shape that contradicts that: exit 0 is a success, and exit 1 with
    /// nothing written is [`Self::absent`]. Both were constructible, and each
    /// hands the code under test the opposite of what the script says.
    pub(super) fn failed(code: i32, stderr: &str) -> Self {
        assert!(
            code != 0,
            "a failure exits non-zero; exit 0 is `said` or `warned`"
        );
        assert!(
            !stderr.is_empty(),
            "a diagnosed failure writes a diagnostic; exit 1 with nothing written is `absent`"
        );
        Self {
            code: Some(code),
            stdout: Vec::new(),
            stderr: stderr.as_bytes().to_vec(),
        }
    }

    /// A shape with no name: the wrapper behaviour a classifier has to reject,
    /// rather than anything git is known to produce.
    pub(super) fn exactly(code: Option<i32>, stdout: &[u8], stderr: &[u8]) -> Self {
        Self {
            code,
            stdout: stdout.to_vec(),
            stderr: stderr.to_vec(),
        }
    }

    /// Killed before it could exit, so there is no code to read.
    pub(super) fn was_signalled() -> Self {
        Self {
            code: None,
            stdout: Vec::new(),
            stderr: Vec::new(),
        }
    }
}

/// Where a child's answer comes from. Only [`Runner::Child`] ships. The fake
/// stands in at the spawn, below the transport guard and the remote notes, so
/// what [`Git::child`] decides before one runs against the script instead of
/// being bypassed by it.
enum Runner {
    Child,
    #[cfg(test)]
    Fake(fake::Fake),
}

/// A commit id, kept distinct from a ref name so the two cannot be transposed
/// at a call site: `git check-ref-format` accepts a bare sha, so validating the
/// name catches nothing. Minted by [`Git::head`], [`Git::tag_commit`], and
/// [`Self::from_advertised`] (ls-remote tips), so every value is what a
/// commit-naming git read produced.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct Commit(String);

/// [`Git::blob_kind`]'s answer. `Other` is any entry that is not a symlink:
/// a regular blob, but also a tree or a gitlink, which `cat-file blob` then
/// refuses on its own.
#[derive(Debug)]
pub(super) enum BlobKind {
    Absent,
    Symlink(String),
    Other,
}

impl Commit {
    pub(super) fn as_str(&self) -> &str {
        &self.0
    }

    /// Same provenance rule as [`Git::head`]: the string came from a
    /// commit-naming git read.
    pub(super) fn from_advertised(sha: &str) -> Result<Self, CliError> {
        if sha.is_empty() {
            return Err(CliError::unverified(
                "unverified: ls-remote advertised an empty commit id",
            ));
        }
        Ok(Self(sha.to_owned()))
    }
}

/// A commit's message and git's parse of its `skip-checks` trailer values,
/// one value per line.
#[derive(Debug)]
pub(super) struct CommitText {
    pub(super) message: String,
    pub(super) skip_checks: String,
}

/// Runs git in one repository.
pub(super) struct Git {
    repo: PathBuf,
    /// Each child re-checks that `repo` still names this directory.
    /// [`Self::at`] leaves this empty (cwd plumbing, tests).
    held: Option<Dir>,
    runner: Runner,
    /// Resolved on the first child and reused. The answer comes from the
    /// process environment and the repository config, neither of which changes
    /// while oakum runs, so resolving it per child costs a `git config` spawn
    /// each time. A scripted [`Git`] carries its own in the fake and leaves
    /// this empty.
    ///
    /// The failure is cached too, and travels as the bare reason so the caller
    /// phrases it: an operation that needed the transport turns it into an
    /// `unverified` error, one that did not says it plainly. Pre-wrapped, both
    /// phrasings land in the same line and contradict each other.
    transport: OnceLock<std::sync::Arc<Result<env::BatchSsh, env::TransportUnknown>>>,
    /// What each named remote's listed URLs established, per direction, so
    /// the notes are asked about the remote in hand.
    reach_by_remote: Mutex<BTreeMap<(String, Direction), reach::Reach>>,
    /// Notes already said, keyed by their text: distinct fetch and push notes
    /// each land, while a byte-identical one says itself once.
    warned: Mutex<BTreeSet<String>>,
}

impl Git {
    pub(super) fn at(repo: impl Into<PathBuf>) -> Self {
        Self::new(repo.into(), Runner::Child, None)
    }

    pub(super) fn at_repository(
        repo: &super::repository::Repository,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let path = repo.ambient_path()?.to_path_buf();
        let held = repo.dir().try_clone().map_err(|err| {
            CliError::new(format!("failed to clone the repository capability: {err}"))
        })?;
        Ok(Self::new(path, Runner::Child, Some(held)))
    }

    /// Answers from a script instead of a repository, each keyed by the command
    /// it answers, over a composed ssh transport. The path is never read.
    #[cfg(test)]
    pub(super) fn answering(replies: impl IntoIterator<Item = (&'static str, Reply)>) -> Self {
        Self::answering_over(
            Ok(env::BatchSsh::Composed(String::from(
                "ssh -o BatchMode=yes",
            ))),
            replies,
        )
    }

    /// [`Self::answering`] with the transport the probe would have resolved,
    /// so the guard every child passes through is drivable both ways. Private
    /// to this module, whose types the transport is made of.
    #[cfg(test)]
    fn answering_over(
        transport: Result<env::BatchSsh, env::TransportUnknown>,
        replies: impl IntoIterator<Item = (&'static str, Reply)>,
    ) -> Self {
        Self::new(
            PathBuf::new(),
            Runner::Fake(fake::Fake::answering(transport, replies)),
            None,
        )
    }

    fn new(repo: PathBuf, runner: Runner, held: Option<Dir>) -> Self {
        Self {
            repo,
            held,
            runner,
            transport: OnceLock::new(),
            reach_by_remote: Mutex::new(BTreeMap::new()),
            warned: Mutex::new(BTreeSet::new()),
        }
    }

    /// Each operation the caller asked for, in order, named as [`OpShape::name`]
    /// names it — the same phrase a failure quotes.
    ///
    /// # Panics
    ///
    /// When called on a [`Git`] that runs real children, which cannot report
    /// what it spawned.
    #[cfg(test)]
    pub(super) fn asked(&self) -> Vec<String> {
        match &self.runner {
            Runner::Fake(fake) => fake.asked(),
            Runner::Child => panic!("only a scripted Git records what it was asked"),
        }
    }

    /// Each note a child said, in order; a scripted [`Git`] hears them instead
    /// of writing them to stderr.
    ///
    /// # Panics
    ///
    /// When called on a [`Git`] that runs real children.
    #[cfg(test)]
    pub(super) fn said(&self) -> Vec<String> {
        match &self.runner {
            Runner::Fake(fake) => fake.heard(),
            Runner::Child => panic!("only a scripted Git records what it said"),
        }
    }

    /// What `path` is in `commit`'s tree, so a caller never parses `ls-tree`
    /// output itself. A symlink carries its target, which is the blob's text.
    ///
    /// # Errors
    ///
    /// The operations' own outcome classes.
    pub(super) fn blob_kind(&self, commit: &Commit, path: &str) -> Result<BlobKind, CliError> {
        let entries = self.paths(Op::TreeEntry { commit, path })?;
        let Some(entry) = entries.first() else {
            return Ok(BlobKind::Absent);
        };
        if entry.starts_with("120000 ") {
            let target = self.text(Op::BlobText { commit, path })?;
            return Ok(BlobKind::Symlink(target));
        }
        Ok(BlobKind::Other)
    }

    /// [`Self::text`] without the trim: a file body whose last line ends in
    /// significant whitespace (a Markdown hard break) keeps it.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn raw_text(&self, op: Op<'_>) -> Result<String, CliError> {
        let shape = op.shape();
        let reply = self.checked(&shape, Reads::Text)?;
        String::from_utf8(reply.stdout).map_err(|_| shape.fail("output is not valid UTF-8"))
    }

    /// Trimmed stdout.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn text(&self, op: Op<'_>) -> Result<String, CliError> {
        let shape = op.shape();
        let reply = self.checked(&shape, Reads::Text)?;
        if shape.spec.lossy {
            return Ok(String::from_utf8_lossy(&reply.stdout).trim().to_owned());
        }
        String::from_utf8(reply.stdout)
            .map(|text| text.trim().to_owned())
            .map_err(|_| shape.fail("output is not valid UTF-8"))
    }

    /// HEAD's commit id. With [`Self::tag_commit`], one of the two mints for
    /// [`Commit`]: kept to the commit-naming reads, so a listing or URL read
    /// cannot be wrapped as a commit.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn head(&self) -> Result<Commit, CliError> {
        self.text(Op::Head).map(Commit)
    }

    /// The commit a local tag points at, peeled; `None` when the tag is
    /// absent.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn tag_commit(&self, tag: &str) -> Result<Option<Commit>, CliError> {
        Ok(self.optional_text(Op::LocalTagCommit { tag })?.map(Commit))
    }

    /// A blob exactly as git stored it.
    ///
    /// [`Self::text`] trims, which is right for a hash or a ref name and wrong
    /// for a file whose parser reads from byte zero: a bump file with a blank
    /// line before its frontmatter is malformed on disk and would parse here,
    /// so the two copies of one file would be judged by different rules.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class, or invalid UTF-8.
    pub(super) fn blob(&self, op: Op<'_>) -> Result<String, CliError> {
        let shape = op.shape();
        let reply = self.checked(&shape, Reads::Text)?;
        String::from_utf8(reply.stdout).map_err(|_| shape.fail("blob is not valid UTF-8"))
    }

    /// NUL-separated paths. `-z` turns quoting off, so a path carrying newlines,
    /// boundary whitespace, or non-ASCII bytes arrives byte-for-byte and
    /// package-prefix attribution stays exact. A non-UTF-8 path cannot be
    /// compared against manifest-derived package directories, so it fails loudly
    /// rather than being lossily rewritten into one that misses its package.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn paths(&self, op: Op<'_>) -> Result<Vec<String>, CliError> {
        let shape = op.shape();
        let stdout = self.checked(&shape, Reads::Paths)?.stdout;
        split_nul_paths(&stdout).ok_or_else(|| {
            shape.fail(
                "listed a path that is not valid UTF-8; oakum cannot attribute it to a package",
            )
        })
    }

    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn run(&self, op: Op<'_>) -> Result<(), CliError> {
        self.checked(&op.shape(), Reads::Text)?;
        Ok(())
    }

    /// For the queries git answers with an exit code. `Ok(false)` only for exit 1
    /// with nothing written, which is how git says "no"; a diagnosed failure is
    /// an error, so "we could not look" never becomes a verdict.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn predicate(&self, op: Op<'_>) -> Result<bool, CliError> {
        let shape = op.shape();
        let reply = self.answered(&shape, Reads::Text)?;
        if reply.succeeded() {
            return Ok(true);
        }
        if reply.said_no() {
            return Ok(false);
        }
        Err(shape.fail(&reply.detail()))
    }

    /// `Ok(None)` for a search that matched nothing: exit 1 with both streams
    /// silent, which is how `git grep` says no. Unlike [`Self::optional_text`]
    /// the answer is split on NUL rather than trimmed, so a path that is or ends
    /// in whitespace survives.
    ///
    /// A search that matched nothing and one that searched nothing look alike
    /// here; only the caller knows whether there was anything to search.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn matched_paths(&self, op: Op<'_>) -> Result<Option<Vec<String>>, CliError> {
        let shape = op.shape();
        let reply = self.answered(&shape, Reads::Paths)?;
        if !reply.succeeded() {
            if reply.said_no() {
                return Ok(None);
            }
            return Err(shape.fail(&reply.detail()));
        }
        split_nul_paths(&reply.stdout).map(Some).ok_or_else(|| {
            shape.fail("listed a path that is not valid UTF-8; oakum cannot name it as a gate")
        })
    }

    /// `Ok(None)` for the queries that report "absent" as exit 1 with nothing
    /// written — `config --get-regexp` with no match, `rev-parse --verify
    /// --quiet` on a missing ref. Any other failure is still an error: a
    /// wrapper that exits 1 with a diagnostic did not look, which is not the
    /// same as looking and finding nothing.
    ///
    /// # Errors
    ///
    /// The operation's own outcome class.
    pub(super) fn optional_text(&self, op: Op<'_>) -> Result<Option<String>, CliError> {
        let shape = op.shape();
        let reply = self.answered(&shape, Reads::Text)?;
        if !reply.succeeded() {
            if reply.said_no() {
                return Ok(None);
            }
            return Err(shape.fail(&reply.detail()));
        }
        let text =
            String::from_utf8(reply.stdout).map_err(|_| shape.fail("output is not valid UTF-8"))?;
        Ok(Some(text.trim().to_owned()).filter(|text| !text.is_empty()))
    }

    /// The answer, with a failed child already turned into the operation's own
    /// error. Callers that read an exit code as data go through [`Self::ask`].
    fn checked(&self, shape: &OpShape<'_>, reads: Reads) -> Result<Reply, CliError> {
        let reply = self.answered(shape, reads)?;
        if reply.succeeded() {
            return Ok(reply);
        }
        Err(shape.fail(&reply.detail()))
    }

    /// The answer, refusing a child that exited 0 without giving one. What
    /// silence means is a property of the operation, not of the accessor
    /// reading it: keyed on the outcome class instead, this reached neither
    /// `predicate` nor `optional_text`, and a config read that never ran came
    /// back as "no remote suppresses tags".
    fn answered(&self, shape: &OpShape<'_>, reads: Reads) -> Result<Reply, CliError> {
        let reply = self.child(shape)?;
        // Before the `spoke` shortcut, because a listing that must be whole is
        // disqualified by its diagnostic whether or not it also wrote records —
        // one record of stdout would otherwise turn a partial walk into a
        // complete answer. Only for a child that exited 0: one that failed
        // listed none of the tree, and `checked` says that better below.
        if shape.spec.answer == Answer::Whole && reply.succeeded() {
            if let Some(said) = reply
                .diagnostic()
                .filter(|said| hides_part_of_the_tree(said))
            {
                return Err(shape.phrase(|what| {
                    format!("git {what} may have listed only part of the tree, reporting: {said}")
                }));
            }
        }
        if !reply.succeeded() || reply.spoke(reads) {
            return Ok(reply);
        }
        match shape.spec.answer {
            Answer::Always => Err(Self::unanswered(shape, &reply)),
            // Any stderr disqualifies, benign text included: an `ls-remote`
            // that found no tags while ssh wrote `Warning: Permanently added
            // ... to the list of known hosts` is refused. Deliberate — nothing
            // reliably separates a benign line from a consequential one, and
            // unlike `core.fsmonitor` no setting drops the diagnostic without
            // dropping the check with it. The refusal quotes the warning, so a
            // second run resolves it.
            Answer::Sometimes if reply.diagnostic().is_some() => {
                Err(Self::unanswered(shape, &reply))
            }
            // A `Whole` child that reached here wrote no diagnostic, so its
            // silence is the same real answer `Sometimes` reads it as.
            Answer::Sometimes | Answer::Never | Answer::Whole => Ok(reply),
        }
    }

    /// One commit's message and its `skip-checks` trailer values, split
    /// where [`Op::CommitMessage`]'s format puts the separator. Parsed here,
    /// beside the format that creates the framing, so no caller re-inherits
    /// the contract — and a reply that lacks the separator, or still carries
    /// the unexpanded specifier (a git too old to parse trailers), is an
    /// error rather than a silent "no trailers": guessing there would release
    /// a commit whose workflow GitHub suppresses.
    pub(super) fn commit_text(&self, commit: &str) -> Result<CommitText, CliError> {
        let raw = self.text(Op::CommitMessage { commit })?;
        let Some((message, trailers)) = raw.split_once('\0') else {
            return Err(CliError::new(format!(
                "git log for `{commit}` omitted the trailer separator oakum's \
                 format requests"
            )));
        };
        if trailers.trim() == SKIP_CHECKS_ATOM && !message.contains(SKIP_CHECKS_ATOM) {
            return Err(CliError::new(format!(
                "this git did not parse the skip-checks trailers for \
                 `{commit}`; oakum cannot tell whether the commit suppresses \
                 CI"
            )));
        }
        Ok(CommitText {
            message: message.to_owned(),
            skip_checks: trailers.to_owned(),
        })
    }

    /// The ssh transport for this repository, resolved once per process: it
    /// is a property of the environment and the repository config, neither of
    /// which changes while oakum runs, and `Git` values are constructed
    /// throughout the cli — per-instance caching re-probed on every one now
    /// that every child carries the transport.
    fn transport(&self) -> Result<&env::BatchSsh, &env::TransportUnknown> {
        use std::collections::HashMap;
        use std::sync::{Arc, Mutex, OnceLock};
        type Resolved =
            Mutex<HashMap<std::path::PathBuf, Arc<Result<env::BatchSsh, env::TransportUnknown>>>>;
        static RESOLVED: OnceLock<Resolved> = OnceLock::new();
        #[cfg(test)]
        if let Runner::Fake(fake) = &self.runner {
            return fake.transport();
        }
        self.transport
            .get_or_init(|| {
                let mut resolved = RESOLVED
                    .get_or_init(|| Mutex::new(HashMap::new()))
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                resolved
                    .entry(self.repo.clone())
                    .or_insert_with(|| Arc::new(env::batch_transport(&self.repo)))
                    .clone()
            })
            .as_ref()
            .as_ref()
    }

    /// Each distinct note lands once. The transport resolves the same way
    /// for every child, so without this an N-tag release repeats it 1 + 2N
    /// times.
    fn say_once(&self, note: &str) {
        match &self.runner {
            Runner::Child => {
                self.say_once_with(note, env::warn);
            }
            #[cfg(test)]
            Runner::Fake(fake) => {
                self.say_once_with(note, |note| fake.hear(note));
            }
        }
    }

    /// Takes the sayer, because the rollback below is otherwise the one branch
    /// no test can drive: `say_once` reaches stderr through `env::warn`, and a
    /// refused write there is not reproducible in process.
    ///
    /// The lock spans the write: released first, a second caller skips on a
    /// reservation about to be rolled back and both stay silent. `warn`
    /// re-enters nothing, so holding it cannot deadlock the way it would around
    /// a spawn, and with `Git` driven from one thread it orders a race that
    /// cannot yet happen.
    fn say_once_with(&self, note: &str, say: impl FnOnce(&str) -> bool) -> bool {
        let key = note.to_owned();
        let mut warned = self
            .warned
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if !warned.insert(key.clone()) {
            return false;
        }
        // A stderr that could not take the line must not consume the one chance
        // to say it. A write that failed partway still leaves its fragment, and
        // the retry then says the whole note again; a fragment plus the note
        // beats losing the note.
        let said = say(note);
        if !said {
            warned.remove(&key);
        }
        said
    }

    /// Whether the note about ssh prompts applies to the remote this operation
    /// contacts. One `remote -v` child lists every remote's fetch and push
    /// URLs — pushurl included, so a remote that fetches over https and
    /// pushes over ssh still warns on the push — and fills the cache for all
    /// of them, so the unconditional read costs one spawn per run rather than
    /// one per operation. Neither URL changes while oakum runs.
    ///
    /// A failed read is cached alongside a settled one, which is safe only
    /// while the sole consumer is an advisory note — an unread verdict from a
    /// transient signal is cached exactly like an established one.
    fn remote_reach(&self, contact: Contact<'_>) -> reach::Reach {
        let key = contact.key();
        if let Some(answer) = self.remembered_reach(&key) {
            return answer;
        }
        // The listing child spawns before the lock is taken: a `Mutex` is not
        // reentrant, so holding it across the spawn deadlocks outright — no
        // error, no output — if the listing operation is ever itself classed
        // as contacting a remote. Two callers racing here duplicate one cheap
        // read rather than hanging.
        let parsed = self.text(Op::RemoteNames).and_then(|names| {
            let names: Vec<&str> = names.lines().collect();
            let listing = self.text(Op::RemoteUrls)?;
            reach::parse_remote_urls(&listing, &names)
        });
        let mut cache = self
            .reach_by_remote
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let answer = match parsed {
            Ok(urls) => {
                for (entry, urls) in urls {
                    cache.insert(entry, reach::classify(Ok(&urls)));
                }
                cache.get(&key).cloned().unwrap_or_else(|| {
                    let direction = match contact.direction {
                        Direction::Fetch => "fetch",
                        Direction::Push => "push",
                    };
                    reach::classify(Err(&CliError::new(format!(
                        "the listing shows no {direction} URL for remote {:?}",
                        contact.remote
                    ))))
                })
            }
            Err(err) => reach::classify(Err(&err)),
        };
        cache.insert(key, answer.clone());
        answer
    }

    /// A cache, so a torn entry is not a correctness problem: a poisoned lock is
    /// recovered rather than turned into a second panic that hides the first.
    fn remembered_reach(&self, key: &(String, Direction)) -> Option<reach::Reach> {
        self.reach_by_remote
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .get(key)
            .cloned()
    }

    /// Everything one child carries, whichever runner answers it.
    fn child(&self, shape: &OpShape<'_>) -> Result<Reply, CliError> {
        if let Some(held) = &self.held {
            super::repository::confirm_ambient(held, &self.repo)
                .map_err(|err| shape.fail(&err.to_string()))?;
        }
        // Every child carries the transport: git opens sockets on its own
        // schedule — a partial clone's `diff` lazily fetches over ssh from an
        // op typed local (measured) — so protection cannot key on the
        // classification, and an unreadable ssh configuration stops every
        // operation rather than guessing away the user's key or proxy.
        let batch = self
            .transport()
            .map_err(|unknown| shape.unreadable_transport(unknown))?;
        if let Some(contact) = shape.contact {
            // Unconditional: a helper remote owes its note even when the
            // transport composed, because `BatchMode` never reaches what
            // a helper runs.
            let reach = self.remote_reach(contact);
            for note in reach::notes_for(contact, &reach, batch) {
                self.say_once(&note);
            }
        }
        self.spawn(shape, batch)
    }

    /// Where the fake answers from its script; the transport and the notes
    /// reach it above.
    fn spawn(&self, shape: &OpShape<'_>, batch: &env::BatchSsh) -> Result<Reply, CliError> {
        match &self.runner {
            Runner::Child => {
                let args: Vec<&str> = shape.argv.iter().map(String::as_str).collect();
                env::deadlined_command(&self.repo, &args, batch)
                    .output()
                    .map(Reply::from)
                    .map_err(|failure| shape.fail(&failure.to_string()))
            }
            #[cfg(test)]
            Runner::Fake(fake) => Ok(fake.answer(shape.name)),
        }
    }

    /// Separate from [`OpShape::fail`] because the child exited 0: a reader told
    /// that git "failed" checks the exit code, finds success, and concludes
    /// oakum is wrong.
    fn unanswered(shape: &OpShape<'_>, reply: &Reply) -> CliError {
        shape.phrase(|what| match reply.diagnostic() {
            Some(said) => format!("git {what} answered nothing while reporting: {said}"),
            None => format!("git {what} exited 0 without answering"),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::env::{BatchSsh, TransportUnknown};
    use super::op::fixture_commit;
    use super::{split_nul_paths, CliError, Contact, Direction, Git, Op, Reply};

    const SSH_URL: &str = "git@example.invalid:demo/demo.git";
    const HTTPS_URL: &str = "https://example.invalid/demo/demo.git";
    const HELPER_URL: &str = "ext::ssh -p 22 git@example.invalid %S demo.git";

    const WORKTREE: &str = "status --porcelain -z";

    /// The listing is short and git said why, so the look did not happen.
    #[test]
    fn a_whole_listing_that_names_unread_tree_is_refused() {
        let err = Git::answering([(
            WORKTREE,
            Reply::said_and_warned(
                " M alpha/src/lib.rs\0",
                "warning: could not open directory 'beta/hidden/': Permission denied",
            ),
        )])
        .paths(Op::UncommittedPaths)
        .expect_err("refused");
        assert_eq!(err.exit_code(), 2, "{err}");
        assert!(err.to_string().contains("only part of the tree"), "{err}");
    }

    /// The listing is whole and the warning is about something else, so the
    /// answer stands. Measured on git 2.55: an unreadable `.git/info/exclude`
    /// warns twice and lists byte-for-byte what a readable one lists, and
    /// refusing on it would fail the gate this repository runs in CI over a
    /// permission bit on an unrelated file.
    #[test]
    fn a_whole_listing_is_kept_when_the_warning_is_not_about_unread_tree() {
        assert_eq!(
            Git::answering([(
                WORKTREE,
                Reply::said_and_warned(
                    " M alpha/src/lib.rs\0",
                    "warning: unable to access '.git/info/exclude': Permission denied",
                ),
            )])
            .paths(Op::UncommittedPaths)
            .expect("kept"),
            [" M alpha/src/lib.rs"]
        );
    }

    /// Every spelling of the failure the 2.55.0 binary carries, whether or not
    /// `status` can reach it: the matcher is what is under test here, and a
    /// case-sensitive one reaches five of the seven.
    #[test]
    fn every_shape_of_unread_tree_is_recognised() {
        for said in [
            "cannot lstat 'beta/x'",
            "Cannot lstat 'beta/x'",
            "cannot opendir 'beta/hidden'",
            "cannot opendir beta/hidden",
            "warning: could not open directory 'beta/hidden/': Permission denied",
            "Could not open directory beta/hidden",
            "opendir('beta/hidden') failed",
        ] {
            let err = Git::answering([(
                WORKTREE,
                Reply::said_and_warned(" M alpha/src/lib.rs\0", said),
            )])
            .paths(Op::UncommittedPaths)
            .expect_err(said);
            assert!(
                err.to_string().contains("only part of the tree"),
                "{said}: {err}"
            );
        }
    }

    /// A child that failed listed none of the tree, not part of it, so it takes
    /// the failure wording rather than the truncation wording — both are exit
    /// 2, which is why asserting the class alone would not tell them apart.
    ///
    /// The diagnostic has to name unread tree for this to discriminate:
    /// against stderr the set does not match, dropping the `succeeded` gate
    /// leaves the wording right by accident.
    #[test]
    fn a_whole_listing_that_failed_outright_is_not_called_partial() {
        let err = Git::answering([(
            WORKTREE,
            Reply::failed(128, "fatal: cannot opendir 'beta/hidden'"),
        )])
        .paths(Op::UncommittedPaths)
        .expect_err("refused");
        assert_eq!(err.exit_code(), 2, "{err}");
        assert!(err.to_string().contains("failed: exit 128"), "{err}");
        assert!(
            !err.to_string().contains("only part of the tree"),
            "it listed none of it: {err}"
        );
    }

    /// A scripted `Git` whose one remote, `origin`, lists `url` both ways, so
    /// a remote child finds the listing its notes read.
    fn at_remote(url: &str, replies: impl IntoIterator<Item = (&'static str, Reply)>) -> Git {
        Git::answering(listing(url).into_iter().chain(replies))
    }

    fn listing(url: &str) -> [(&'static str, Reply); 2] {
        [
            ("remote", Reply::said("origin")),
            (
                "remote -v",
                Reply::said(format!("origin\t{url} (fetch)\norigin\t{url} (push)")),
            ),
        ]
    }

    fn advertised() -> (&'static str, Reply) {
        ("ls-remote --tags", Reply::said("cafe\trefs/tags/v1.0.0\n"))
    }

    /// A transport `BatchMode` cannot reach, with the reason the probe gives.
    fn opaque() -> BatchSsh {
        BatchSsh::Unprotected(String::from(
            "GIT_SSH names `my-ssh`, which takes its arguments from git, not from oakum",
        ))
    }

    /// The shapes below all arrive as "git exited non-zero" or "git printed
    /// nothing", and telling them apart is the whole of the three-outcome rule.
    /// A real repository produces them only through a shell shim on `PATH`,
    /// which `tests/check.rs` does and which runs on unix alone.
    #[test]
    fn exit_one_with_nothing_written_is_how_git_says_absent() {
        assert_eq!(
            Git::answering([("config --get-regexp tagopt", Reply::absent())])
                .optional_text(Op::TagOptRemotes)
                .expect("absent"),
            None
        );
        assert!(!Git::answering([("rev-parse --verify", Reply::absent())])
            .predicate(Op::RefExists {
                reference: "refs/tags/v1.0.0",
            })
            .expect("absent"));
    }

    /// The other three ways a child can exit without answering. Each was a
    /// surviving mutant: drop any conjunct of `said_no` and a git that could not
    /// look reports "absent", which decides whether a tag exists.
    #[test]
    fn only_exit_one_with_both_streams_empty_means_absent() {
        let diagnosed = Git::answering([(
            "config --get-regexp tagopt",
            Reply::failed(1, "fatal: not a git repository"),
        )])
        .optional_text(Op::TagOptRemotes)
        .expect_err("a diagnosed exit must not read as absence");
        assert!(
            matches!(diagnosed, CliError::Unverified { .. }),
            "{diagnosed:?}"
        );
        assert!(
            diagnosed.to_string().contains("not a git repository"),
            "{diagnosed}"
        );

        let silent = Git::answering([(
            "config --get-regexp tagopt",
            Reply::exactly(Some(128), b"", b""),
        )])
        .optional_text(Op::TagOptRemotes)
        .expect_err("exit 128 is not exit 1");
        assert!(silent.to_string().contains("exit 128"), "{silent}");

        let spoke = Git::answering([(
            "config --get-regexp tagopt",
            Reply::exactly(Some(1), b"remote.origin.tagopt --no-tags", b""),
        )])
        .optional_text(Op::TagOptRemotes)
        .expect_err("a child that wrote an answer did not say no");
        assert!(spoke.to_string().contains("exit 1"), "{spoke}");

        let killed = Git::answering([("rev-parse --verify", Reply::was_signalled())])
            .predicate(Op::RefExists {
                reference: "refs/tags/v1.0.0",
            })
            .expect_err("a signal is not an answer of no");
        assert!(
            killed.to_string().contains("terminated by a signal"),
            "{killed}"
        );
    }

    /// A cached failure is handed to later callers unchanged, and the class is
    /// decided where it is needed rather than carried: an operation that wanted
    /// the transport reports `unverified`, one that did not says it plainly.
    #[test]
    fn a_cached_transport_failure_is_repeated_verbatim() {
        let git = Git::at("/nonexistent");
        git.transport
            .set(
                Err(super::env::TransportUnknown::SshConfig(String::from(
                    "git config was killed by a signal",
                )))
                .into(),
            )
            .expect("the cache starts empty");
        for _ in 0..3 {
            assert_eq!(
                git.transport().expect_err("a cached failure").detail(),
                "git config was killed by a signal"
            );
        }
        let raised = Op::AdvertisedTags { remote: "origin" }
            .shape()
            .unreadable_transport(&super::env::TransportUnknown::SshConfig(String::from(
                "git config was killed by a signal",
            )));
        assert!(matches!(raised, CliError::Unverified { .. }), "{raised:?}");
        assert!(
            raised.to_string().contains("killed by a signal"),
            "{raised}"
        );
    }

    /// Git's trace settings write to stderr unless pointed at a file, and every
    /// rule above treats stderr as a diagnostic. Measured on git 2.55: `1`,
    /// `2`, and `true` all print to stderr, an unrecognised value warns and
    /// then prints to stderr, and only an absolute path is written to a file.
    #[test]
    fn only_a_trace_that_names_a_file_is_left_in_place() {
        for value in ["1", "2", "true", "relative.log", ""] {
            assert!(
                !super::traces_to_a_file(std::ffi::OsStr::new(value)),
                "{value:?} reaches our stderr"
            );
        }
        let file = std::env::temp_dir().join("oakum-trace.event");
        assert!(super::traces_to_a_file(file.as_os_str()));
    }

    /// Asserted against the command `untrace` actually builds. Written against
    /// its own literals instead, this test passed with `untrace` gutted to an
    /// empty body — the whole suite did.
    #[test]
    fn untrace_silences_every_channel_and_leaves_the_rest_alone() {
        let file = std::env::temp_dir().join("oakum-trace.perf");
        let inherited = [
            (
                std::ffi::OsString::from("GIT_TRACE"),
                std::ffi::OsString::from("1"),
            ),
            (
                std::ffi::OsString::from("GIT_TRACE_PACKET"),
                std::ffi::OsString::from("1"),
            ),
            (
                std::ffi::OsString::from("GIT_TRACE_A_CHANNEL_ADDED_LATER"),
                std::ffi::OsString::from("1"),
            ),
            (
                std::ffi::OsString::from("GIT_TRACE2_EVENT"),
                std::ffi::OsString::from("relative.log"),
            ),
            (
                std::ffi::OsString::from("GIT_TRACE2_PERF"),
                file.into_os_string(),
            ),
        ];
        let mut command = std::process::Command::new("git");
        command.env("GIT_TERMINAL_PROMPT", "0");
        super::untrace_from(&mut command, inherited);

        let envs: Vec<(String, Option<String>)> = command
            .get_envs()
            .map(|(name, value)| {
                (
                    name.to_string_lossy().into_owned(),
                    value.map(|value| value.to_string_lossy().into_owned()),
                )
            })
            .collect();
        let setting = |name: &str| {
            envs.iter()
                .find(|(key, _)| key == name)
                .map(|(_, value)| value.clone())
        };

        // Removed outright, including one nobody enumerated.
        for name in [
            "GIT_TRACE",
            "GIT_TRACE_PACKET",
            "GIT_TRACE_A_CHANNEL_ADDED_LATER",
        ] {
            assert_eq!(setting(name), Some(None), "{name} still reaches the child");
        }
        // Trace2 is set off rather than removed, because git config can turn it
        // back on where an environment edit cannot reach.
        assert_eq!(
            setting("GIT_TRACE2_EVENT"),
            Some(Some(String::from("0"))),
            "config-driven trace2 is still live"
        );
        // A trace2 channel the caller pointed at a file is left entirely alone —
        // the answer has to come from the environment handed in, not the
        // process's own, or an exported value decides for an environment that
        // never mentioned it.
        assert_eq!(
            setting("GIT_TRACE2_PERF"),
            None,
            "a trace2 file target was overridden"
        );
        assert_eq!(
            setting("GIT_TERMINAL_PROMPT"),
            Some(Some(String::from("0")))
        );
    }

    /// And matching stops at token boundaries. `RefExists` asks for
    /// `refs/tags/v1.0.0`; `LocalTagCommit` asks for the same string with
    /// `^{}` appended, so a character-wise prefix would let a script for one
    /// answer the other's child — the substitution this fake exists to expose.
    #[test]
    #[should_panic(expected = "nothing scripted answers")]
    fn a_command_that_only_shares_a_character_prefix_does_not_answer() {
        let git = Git::answering([(
            "rev-parse --verify --quiet refs/tags/v1.0.0",
            Reply::said("cafebabe"),
        )]);
        let _ = git.optional_text(Op::LocalTagCommit { tag: "v1.0.0" });
    }

    /// One of the two rules: a child that always prints and printed nothing did
    /// not answer, whatever it exited with. Keying this on the outcome class
    /// left it off `predicate` and `optional_text` entirely, which is how a
    /// config read that never ran came back as "no remote suppresses tags".
    #[test]
    fn a_child_that_always_prints_and_printed_nothing_is_refused_on_every_accessor() {
        let warning = "warning: unable to access '/etc/gitconfig': Permission denied";
        let asked = Git::answering([("config --get-regexp tagopt", Reply::warned(warning))])
            .optional_text(Op::TagOptRemotes)
            .expect_err("optional_text");
        assert!(matches!(asked, CliError::Unverified { .. }), "{asked:?}");
        assert!(asked.to_string().contains("Permission denied"), "{asked}");

        let voted = Git::answering([("rev-parse --verify", Reply::warned(warning))])
            .predicate(Op::RefExists {
                reference: "refs/tags/v1.0.0",
            })
            .expect_err("predicate");
        assert!(voted.to_string().contains("answered nothing"), "{voted}");

        let read = Git::answering([("rev-parse HEAD", Reply::warned(warning))])
            .text(Op::Head)
            .expect_err("text");
        assert!(read.to_string().contains("Permission denied"), "{read}");

        // Nothing on either stream: `rev-parse --verify --quiet` reports absence
        // as exit 1, so exit 0 in silence is a wrapper, not an answer.
        let mute = Git::answering([(
            "config --get-regexp tagopt",
            Reply::exactly(Some(0), b"", b""),
        )])
        .optional_text(Op::TagOptRemotes)
        .expect_err("silence is not absence");
        assert!(
            mute.to_string().contains("exited 0 without answering"),
            "{mute}"
        );
    }

    /// The second rule. `for-each-ref` legitimately prints nothing — a
    /// repository can have no reachable tags — but a diagnostic alongside that
    /// silence means the emptiness was never established (ADR-0014).
    #[test]
    fn a_verification_that_reported_nothing_while_warning_is_not_an_empty_look() {
        let err = Git::answering([(
            "for-each-ref --merged HEAD",
            Reply::warned("warning: refname 'v1.0.0' is ambiguous"),
        )])
        .text(Op::ReachableTags)
        .expect_err("a warned look must not read as no tags");
        assert!(matches!(err, CliError::Unverified { .. }), "{err:?}");
        assert!(err.to_string().contains("ambiguous"), "{err}");
    }

    /// `Answer::Sometimes`: the emptiness is a real answer, and a diagnostic
    /// alongside it is not. These two gate `oakum release`, and a `status` or
    /// `log` that never ran must not read as "clean" and "no skip-ci marker".
    #[test]
    fn a_refusal_gate_that_never_looked_is_not_a_clean_answer() {
        for (command, op) in [
            ("status --porcelain", Op::WorktreeStatus),
            ("log -1", Op::CommitMessage { commit: "HEAD" }),
            ("diff-tree", Op::CommitPaths { hash: "cafebabe" }),
        ] {
            let err = Git::answering([(command, Reply::warned("fatal: could not read index"))])
                .text(op)
                .expect_err("{op:?} must not read as an empty answer");
            assert!(err.to_string().contains("could not read index"), "{err}");
        }
    }

    /// `Answer::Never`: silence proves nothing either way, so no rule is drawn
    /// from it. A successful `git push` writes its whole report to stderr and
    /// nothing to stdout, and `check-ref-format` writes nothing on either
    /// verdict — refusing these would fail every release.
    #[test]
    fn work_that_answers_through_its_exit_code_is_not_refused_for_writing_to_stderr() {
        at_remote(
            SSH_URL,
            [(
                "push",
                Reply::warned("To github.com:oakoss/oakum.git\n * [new tag] v1.0.0 -> v1.0.0"),
            )],
        )
        .run(Op::PushTag {
            remote: "origin",
            tag: "v1.0.0",
        })
        .expect("push reports through stderr on success");

        assert!(Git::answering([(
            "check-ref-format",
            Reply::warned("warning: unable to access '/etc/gitconfig'"),
        )])
        .predicate(Op::ValidRefName {
            reference: "v1.0.0",
        })
        .expect("check-ref-format reports through its exit code"));
    }

    /// A path git listed but oakum cannot read as UTF-8 fails in the
    /// operation's own voice rather than being lossily rewritten into a path
    /// that misses its package. `split_nul_paths` refusing the record is
    /// tested directly; this drives one through the accessor.
    #[test]
    fn a_non_utf8_path_fails_in_the_operations_own_voice() {
        let err = Git::answering([("diff --name-only", Reply::said(b"pkg/\xff.bin\0".to_vec()))])
            .paths(Op::ChangedPaths { from: "v1.0.0" })
            .expect_err("a non-UTF-8 path cannot name a package");
        assert!(
            err.to_string().contains("cannot attribute it to a package"),
            "{err}"
        );
        assert!(
            matches!(err, CliError::Unverified { .. }),
            "`diff --name-only` is a verification: {err:?}"
        );
    }

    /// The guard asks the accessor doing the reading, because the two draw the
    /// line in different places and one rule is wrong for one of them. `text`
    /// trims — `str::trim`, so Unicode `White_Space`, not the ASCII subset —
    /// while `paths` keeps every non-empty NUL record byte-for-byte.
    #[test]
    fn each_accessor_draws_the_silence_line_where_it_reads() {
        // Silence to `str::trim`, an answer to anything narrower: a vertical
        // tab, a no-break space, a line separator. `\x0B` is the one that was
        // live — `u8::is_ascii_whitespace` rejects it, and a `status` writing it
        // alongside a fatal passed the dirty-worktree gate.
        for stdout in ["\u{0B}", "\u{A0}", "\u{2028}"] {
            let err = Git::answering([(
                "status --porcelain",
                Reply::exactly(
                    Some(0),
                    stdout.as_bytes(),
                    b"fatal: could not read the index",
                ),
            )])
            .text(Op::WorktreeStatus)
            .expect_err("whitespace is not a clean worktree");
            assert!(
                err.to_string().contains("could not read the index"),
                "{err}"
            );
        }

        // `-z` turns quoting off, so a file named " " arrives as a one-byte
        // record and is a filename, not silence. Only a genuinely empty record
        // means nothing was listed.
        let listed = Git::answering([(
            "diff --name-only",
            Reply::exactly(
                Some(0),
                b" \0",
                b"warning: unable to access '/etc/gitconfig'",
            ),
        )])
        .paths(Op::ChangedPaths { from: "v1.0.0" })
        .expect("a whitespace filename is a filename");
        assert_eq!(listed, [" "]);

        let none = Git::answering([(
            "diff --name-only",
            Reply::exactly(
                Some(0),
                b"\0\0",
                b"warning: unable to access '/etc/gitconfig'",
            ),
        )])
        .paths(Op::ChangedPaths { from: "v1.0.0" })
        .expect_err("empty records listed nothing");
        assert!(none.to_string().contains("answered nothing"), "{none}");

        let absent = Git::answering([("rev-parse --verify refs/tags", Reply::said("  \n"))])
            .optional_text(Op::LocalTagCommit { tag: "v1.0.0" })
            .expect_err("whitespace is not a commit");
        assert!(absent.to_string().contains("without answering"), "{absent}");

        // The same trimming on the other stream: a diagnostic of only
        // whitespace has reported nothing, so an empty look stays an answer.
        assert_eq!(
            Git::answering([("remote", Reply::exactly(Some(0), b"", b" \n"))])
                .text(Op::RemoteNames)
                .expect("a blank diagnostic is not a diagnostic"),
            ""
        );
    }

    /// Every accessor trims, so a lone newline reaches the caller as `""`. The
    /// guard has to judge emptiness the same way or it is one byte wide.
    #[test]
    fn whitespace_on_stdout_is_not_an_answer() {
        let err = Git::answering([(
            "config --get-regexp tagopt",
            Reply::exactly(Some(0), b" \n\t", b"fatal: bad config line 9"),
        )])
        .optional_text(Op::TagOptRemotes)
        .expect_err("whitespace is not an answer");
        assert!(err.to_string().contains("bad config line 9"), "{err}");

        let looked = Git::answering([(
            "status --porcelain",
            Reply::exactly(Some(0), b"\n", b"fatal: could not read index"),
        )])
        .text(Op::WorktreeStatus)
        .expect_err("nor for a gate whose empty answer means clean");
        assert!(
            looked.to_string().contains("could not read index"),
            "{looked}"
        );
    }

    /// The other side of the same rule, and the reason it is not simply "any
    /// stderr is a failure". Both shapes are ones git produces: `for-each-ref`
    /// lists the good tags while reporting a broken ref, and a successful `git
    /// push` writes its whole report to stderr.
    #[test]
    fn a_child_that_answered_or_had_nothing_to_answer_is_not_refused() {
        let listed = Git::answering([(
            "for-each-ref --merged HEAD",
            Reply::said_and_warned(
                "refs/tags/v1.0.0\0commit\0cafebabe\0\0",
                "warning: ignoring broken ref refs/tags/junk",
            ),
        )])
        .text(Op::ReachableTags)
        .expect("a warning alongside an answer leaves the answer standing");
        assert!(listed.contains("v1.0.0"), "{listed}");

        at_remote(
            SSH_URL,
            [(
                "push",
                Reply::warned("To github.com:oakoss/oakum.git\n * [new tag] v1.0.0"),
            )],
        )
        .run(Op::PushTag {
            remote: "origin",
            tag: "v1.0.0",
        })
        .expect("push reports through stderr on success");
    }

    #[test]
    fn a_silent_failure_and_a_signal_do_not_render_alike() {
        let silent = Git::answering([("rev-parse HEAD", Reply::exactly(Some(128), b"", b""))])
            .text(Op::Head)
            .expect_err("exit 128");
        assert!(
            silent.to_string().contains("exit 128 with no diagnostic"),
            "{silent}"
        );
        let killed = Git::answering([("rev-parse HEAD", Reply::was_signalled())])
            .text(Op::Head)
            .expect_err("signalled");
        assert!(
            killed.to_string().contains("terminated by a signal"),
            "{killed}"
        );
        // `git push` writes its success banner to stderr before a signal can
        // kill it; the banner must not stand in for the death (measured — the
        // banner alone was the whole reported reason).
        let banner = at_remote(
            SSH_URL,
            [(
                "push",
                Reply::exactly(None, b"", b"To /private/tmp/origin.git\n"),
            )],
        )
        .run(Op::PushTag {
            remote: "origin",
            tag: "v1.0.0",
        })
        .expect_err("a signal death with stderr");
        assert!(
            banner.to_string().contains("terminated by a signal"),
            "{banner}"
        );
        assert!(
            banner.to_string().contains("To /private/tmp/origin.git"),
            "git's words stay as evidence: {banner}"
        );
    }

    /// `Spec::lossy` decides this, and both sides of it are worth pinning: a
    /// commit message survives a stray byte, an object name does not.
    #[test]
    fn only_a_lossy_read_accepts_bytes_that_are_not_utf8() {
        let message = Git::answering([("log -1", Reply::said(b"fix: caf\xff\n".to_vec()))])
            .text(Op::CommitMessage { commit: "HEAD" })
            .expect("a commit message is read lossily");
        assert_eq!(message, "fix: caf\u{fffd}");
        let err = Git::answering([("rev-parse HEAD", Reply::said(b"\xff".to_vec()))])
            .text(Op::Head)
            .expect_err("an object name must be valid UTF-8");
        assert!(err.to_string().contains("not valid UTF-8"), "{err}");
    }

    /// A failed action stays a plain error even though the same runner handles
    /// the verifications above.
    #[test]
    fn a_failed_action_is_not_unverified() {
        let err = Git::answering([("tag", Reply::failed(128, "fatal: tag already exists"))])
            .run(Op::AnnotatedTag {
                name: "v1.0.0",
                commit: fixture_commit(),
            })
            .expect_err("tag failed");
        assert!(matches!(err, CliError::Other(_)), "{err:?}");
    }

    /// oakum empties the askpass chain on every git child, so a credential
    /// failure there is a state oakum caused; the report must name the way
    /// out while keeping git's text as the evidence.
    #[test]
    fn a_credential_starved_remote_failure_names_the_fix() {
        // One case per matched phrasing, each carrying only its own pattern,
        // so dropping any single arm fails a distinct assertion.
        for starved in [
            "fatal: could not read Username for 'https://gitlab.com': prompts off",
            "fatal: could not read Password for 'https://gitlab.com': prompts off",
            "fatal: Authentication failed for 'https://gitlab.com/'",
            "fatal: Username not available: terminal prompts disabled",
        ] {
            let err = at_remote(
                HTTPS_URL,
                [("ls-remote --tags", Reply::failed(128, starved))],
            )
            .text(Op::AdvertisedTags { remote: "origin" })
            .expect_err("a starved remote read fails");
            let text = err.to_string();
            assert!(text.contains(starved), "{text}");
            assert!(text.contains("credential helper"), "{text}");
            assert!(text.contains("gh auth setup-git"), "{text}");
            assert!(text.starts_with("unverified:"), "{text}");
        }

        let err = at_remote(
            SSH_URL,
            [(
                "ls-remote --tags",
                Reply::failed(128, "fatal: repository not found"),
            )],
        )
        .text(Op::AdvertisedTags { remote: "origin" })
        .expect_err("an unrelated remote failure");
        assert!(!err.to_string().contains("credential helper"), "{err}");

        let starved =
            "fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled";
        let err = Git::answering([("log -1", Reply::failed(128, starved))])
            .text(Op::CommitMessage { commit: "HEAD" })
            .expect_err("a local child never gets the note");
        assert!(!err.to_string().contains("credential helper"), "{err}");
    }

    /// An old git echoes the trailer atom verbatim; a genuine trailer value
    /// quoting the atom also reproduces it — the difference is that the
    /// genuine one carries the atom in the message half too.
    #[test]
    fn the_unparsed_trailer_guard_spares_a_value_that_quotes_the_atom() {
        let old_git = Git::answering([(
            "log -1",
            Reply::said(format!("chore: release\0{}\n", super::SKIP_CHECKS_ATOM)),
        )]);
        let err = old_git.commit_text("cafe").expect_err("old git refuses");
        assert!(err.to_string().contains("did not parse"), "{err}");

        let quoting = Git::answering([(
            "log -1",
            Reply::said(format!(
                "chore: x\n\nskip-checks: {atom}\0{atom}\n",
                atom = super::SKIP_CHECKS_ATOM
            )),
        )]);
        let text = quoting
            .commit_text("cafe")
            .expect("a quoting value is a value");
        assert_eq!(text.skip_checks.trim(), super::SKIP_CHECKS_ATOM);
    }

    /// A remote the listing does not name was never looked at, and "we didn't
    /// look" must not become "it's fine": the fallback is unread, not safe.
    #[test]
    fn an_unlisted_remote_is_unread_not_safe() {
        let git = Git::answering([
            ("remote", Reply::said("other")),
            (
                "remote -v",
                Reply::said("other\thttps://host/r.git (fetch)"),
            ),
        ]);
        let reach = git.remote_reach(Contact {
            remote: "origin",
            direction: Direction::Fetch,
        });
        let unread = reach.unread.expect("an unlisted remote is unread");
        assert!(!reach.ssh && !reach.helper);
        assert!(
            unread.to_string().contains("shows no fetch URL"),
            "{unread}"
        );
    }

    /// A refused write leaves the note owed, so the next remote child says it.
    #[test]
    fn a_refused_note_stays_owed_until_it_lands() {
        let git = Git::answering([]);

        let mut offered = String::new();
        assert!(
            !git.say_once_with("a note", |note| {
                offered.push_str(note);
                false
            }),
            "a refused write must not report the note as said"
        );
        assert_eq!(offered, "a note", "the sayer is given the note verbatim");

        assert!(
            git.say_once_with("a note", |_| true),
            "a refused note is still owed, so the next child may say it"
        );
        assert!(
            !git.say_once_with("a note", |_| panic!("said twice")),
            "once it lands it must not repeat, once per remote child"
        );
    }

    /// A remote that pushes elsewhere owes two notes, not one — while the
    /// byte-identical note, owed by both directions, says itself once.
    #[test]
    fn distinct_notes_say_themselves_and_identical_ones_do_not_repeat() {
        let git = Git::answering([]);
        assert!(git.say_once_with("fetch note", |_| true));
        assert!(git.say_once_with("push note", |_| true));
        assert!(!git.say_once_with("fetch note", |_| panic!("repeated")));
    }

    /// A transport oakum could not read stops every child before it spawns,
    /// in the operation's own outcome class: a verification that could not
    /// look is `unverified`, a push that never ran is a plain failure, and a
    /// local read stops too, because git dials sockets from local-classed
    /// children. Refusing after the spawn would read the same from stderr and
    /// still be the prompt hang.
    #[test]
    fn an_unreadable_transport_stops_every_child_before_it_spawns() {
        let git = Git::answering_over(
            Err(TransportUnknown::SshConfig(String::from(
                "fatal: unable to read config file",
            ))),
            [
                advertised(),
                ("push", Reply::warned("To example.invalid:demo/demo.git")),
                ("log -1", Reply::said("fix: typo\0\n")),
            ],
        );

        let looked = git
            .text(Op::AdvertisedTags { remote: "origin" })
            .expect_err("a look whose transport could not be resolved");
        assert!(matches!(looked, CliError::Unverified { .. }), "{looked:?}");
        let text = looked.to_string();
        assert!(
            text.contains(
                "git ls-remote --tags origin needs an ssh configuration oakum could not read"
            ),
            "{text}"
        );
        assert!(text.contains("unable to read config file"), "{text}");
        assert!(text.contains("will not guess a transport"), "{text}");

        let acted = git
            .run(Op::PushTag {
                remote: "origin",
                tag: "v1.0.0",
            })
            .expect_err("a push that never ran");
        assert!(matches!(acted, CliError::Other(_)), "{acted:?}");

        let local = git
            .commit_text("HEAD")
            .expect_err("a local child carries the transport too");
        assert!(
            local
                .to_string()
                .contains("git log -1 HEAD needs an ssh configuration"),
            "{local}"
        );

        assert!(
            git.asked().is_empty(),
            "no child may spawn without a resolved transport: {:?}",
            git.asked()
        );
        assert!(git.said().is_empty(), "{:?}", git.said());
    }

    /// The remote listing is read for the child that contacts a remote, not
    /// for every child: a local read that asked for it would spawn two extra
    /// children per run — and, with the notes unchanged, pass every test that
    /// reads only the notes.
    #[test]
    fn a_local_child_reads_no_remote_listing() {
        let git = Git::answering([("log -1", Reply::said("fix: typo\0\n"))]);
        git.commit_text("HEAD").expect("a local read");
        assert_eq!(git.asked(), ["log -1"]);
        assert!(git.said().is_empty(), "{:?}", git.said());
    }

    /// A helper remote owes its note under every transport: `BatchMode` never
    /// reaches what a helper runs, so a composed transport is not evidence the
    /// remote is safe, and "does not reach ssh" would assert something untrue.
    /// oakum read this URL fine; what it could not establish is the transport.
    #[test]
    fn a_helper_remote_owes_its_note_under_a_composed_transport() {
        let git = at_remote(HELPER_URL, [advertised()]);
        git.text(Op::AdvertisedTags { remote: "origin" })
            .expect("the look runs; the note is advisory");
        let said = git.said();
        assert_eq!(said.len(), 1, "{said:?}");
        assert!(
            said[0].contains("\"origin\" runs a helper command oakum cannot inspect"),
            "{said:?}"
        );
        assert!(
            !said[0].contains("could not be protected"),
            "a composed transport owes no transport rider: {said:?}"
        );
        assert!(
            !said[0].contains("could not read that remote's URL"),
            "the note must not claim a read failed when it did not: {said:?}"
        );
    }

    /// The note about ssh prompts is gated on the remote's URL, not on the
    /// transport alone: the transport resolves from the environment before any
    /// URL is known, and gating on it alone printed the note for an `https://`
    /// remote, where ssh is never invoked. The ssh remote is the control — an
    /// absent note reads the same when no note is ever said.
    #[test]
    fn only_an_ssh_remote_is_told_about_ssh_prompts() {
        let quiet = Git::answering_over(
            Ok(opaque()),
            listing(HTTPS_URL).into_iter().chain([advertised()]),
        );
        quiet
            .text(Op::AdvertisedTags { remote: "origin" })
            .expect("an https look");
        assert_eq!(
            quiet.asked(),
            ["remote", "remote -v", "ls-remote --tags"],
            "an absent note reads the same when no remote child ran"
        );
        assert!(quiet.said().is_empty(), "{:?}", quiet.said());

        let told = Git::answering_over(
            Ok(opaque()),
            listing(SSH_URL).into_iter().chain([advertised()]),
        );
        told.text(Op::AdvertisedTags { remote: "origin" })
            .expect("an ssh look");
        let said = told.said();
        assert_eq!(said.len(), 1, "{said:?}");
        assert!(
            said[0].contains("cannot refuse ssh prompts for the transport \"origin\" uses"),
            "{said:?}"
        );
        assert!(said[0].contains("GIT_SSH names `my-ssh`"), "{said:?}");
    }

    /// One listing per run and one note per text, under the seam: the reach
    /// is cached for every remote the listing names, and a note owed by two
    /// children lands once.
    #[test]
    fn the_listing_is_read_once_and_a_note_said_once_per_run() {
        let git = at_remote(
            HELPER_URL,
            [
                advertised(),
                ("push", Reply::warned("To example.invalid:demo/demo.git")),
            ],
        );
        git.text(Op::AdvertisedTags { remote: "origin" })
            .expect("the look");
        git.run(Op::PushTag {
            remote: "origin",
            tag: "v1.0.0",
        })
        .expect("the push");
        assert_eq!(
            git.asked(),
            ["remote", "remote -v", "ls-remote --tags", "push"]
        );
        assert_eq!(git.said().len(), 1, "{:?}", git.said());
    }

    /// A URL oakum could not read is unestablished rather than not-ssh. The
    /// note is advisory, so it is still said: hedged, naming the remote,
    /// carrying git's reason, and saying the read failed rather than claiming
    /// the remote is ssh. Under a composed transport the hedge stays, because
    /// oakum cannot rule a helper out, without the transport rider.
    #[test]
    fn an_unreadable_listing_still_gets_a_hedged_note() {
        let unread = |transport| {
            Git::answering_over(
                transport,
                [
                    ("remote", Reply::said("origin")),
                    ("remote -v", Reply::failed(2, "fatal: unreadable")),
                    advertised(),
                ],
            )
        };

        let unprotected = unread(Ok(opaque()));
        unprotected
            .text(Op::AdvertisedTags { remote: "origin" })
            .expect("the look still runs");
        let said = unprotected.said();
        assert_eq!(said.len(), 1, "{said:?}");
        assert!(
            said[0].contains("could not establish what transport \"origin\" uses"),
            "{said:?}"
        );
        assert!(
            said[0].contains("could not read that remote's URL"),
            "{said:?}"
        );
        assert!(said[0].contains("fatal: unreadable"), "{said:?}");
        assert!(
            said[0].contains("could not be protected either: GIT_SSH names"),
            "{said:?}"
        );
        assert!(
            !said[0].contains("cannot refuse ssh prompts"),
            "an unread URL is not an ssh URL: {said:?}"
        );

        let composed = unread(Ok(BatchSsh::Composed(String::from("ssh -o BatchMode=yes"))));
        composed
            .text(Op::AdvertisedTags { remote: "origin" })
            .expect("the look still runs");
        let said = composed.said();
        assert_eq!(said.len(), 1, "{said:?}");
        assert!(
            said[0].contains("could not establish what transport"),
            "{said:?}"
        );
        assert!(
            !said[0].contains("could not be protected"),
            "a composed transport owes no transport rider: {said:?}"
        );
    }

    /// A repository git will not open is not an ssh problem:
    /// [`super::env::TransportUnknown`] splits the causes apart so the message
    /// stops offering a remedy that was measured to move the failure without
    /// fixing it.
    #[test]
    fn a_repository_git_cannot_open_is_not_reported_as_an_ssh_problem() {
        let refused = Op::AdvertisedTags { remote: "origin" }
            .shape()
            .unreadable_transport(&super::env::TransportUnknown::Repository(String::from(
                "exit 128: fatal: Expected git repo version <= 1, found 99",
            )))
            .to_string();
        assert!(
            refused.contains("could not read this repository"),
            "{refused}"
        );
        assert!(refused.contains("found 99"), "{refused}");
        assert!(!refused.contains("ssh"), "{refused}");
        assert!(!refused.contains("GIT_SSH_COMMAND"), "{refused}");
    }

    /// The remedy for an unreadable ssh variable cannot be to set that
    /// variable. Measured before the arm existed: an invalid-UTF-8
    /// `GIT_SSH_COMMAND` was answered by advising that both it and
    /// `GIT_SSH_VARIANT` be set.
    #[test]
    fn an_unreadable_ssh_variable_is_not_answered_by_setting_it() {
        let said = Op::AdvertisedTags { remote: "origin" }
            .shape()
            .unreadable_transport(&super::env::TransportUnknown::SshVariable(String::from(
                "GIT_SSH_COMMAND is not valid UTF-8",
            )))
            .to_string();
        assert!(
            said.contains("GIT_SSH_COMMAND is not valid UTF-8"),
            "{said}"
        );
        assert!(said.contains("repair or unset that variable"), "{said}");
        assert!(
            !said.contains("set both GIT_SSH_COMMAND and GIT_SSH_VARIANT"),
            "circular: {said}"
        );
    }

    /// A probe that never reached git establishes nothing — not about ssh, and
    /// not about the repository. Both remedies would be diagnoses nobody made,
    /// and the ssh one was measured being offered for a git that is not
    /// installed.
    #[test]
    fn a_probe_that_never_reached_git_claims_neither_cause() {
        let unasked = Op::AdvertisedTags { remote: "origin" }
            .shape()
            .unreadable_transport(&super::env::TransportUnknown::Unasked(String::from(
                "could not run git: No such file or directory (os error 2)",
            )))
            .to_string();
        assert!(unasked.contains("could not ask git"), "{unasked}");
        assert!(unasked.contains("No such file or directory"), "{unasked}");
        assert!(!unasked.contains("GIT_SSH_COMMAND"), "{unasked}");
        assert!(
            !unasked.contains("could not read this repository"),
            "a probe that did not run says nothing about the repository: {unasked}"
        );
    }

    /// `-z` turns quoting off, so a path carrying newlines, boundary
    /// whitespace, or non-ASCII bytes must arrive byte-for-byte.
    #[test]
    fn nul_records_are_preserved_exactly() {
        let parsed = split_nul_paths(b"pkg/a b\0pkg/\n weird \0pkg/caf\xc3\xa9.rs\0")
            .expect("valid utf-8 records");
        assert_eq!(parsed, ["pkg/a b", "pkg/\n weird ", "pkg/caf\u{e9}.rs"]);
        assert_eq!(split_nul_paths(b"").expect("empty"), Vec::<String>::new());
    }

    #[test]
    fn a_non_utf8_record_is_refused_rather_than_lossily_rewritten() {
        assert_eq!(split_nul_paths(b"pkg/\xff.bin\0"), None);
    }
}