rargo 0.7.6

The rargo client: syncs a worktree and its environment to a rargo build server and brings back the results
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
use rargo_proto::Digest;
use rargo_proto::tree::{Node, Tree};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::ffi::OsStr;
use std::io::{self, ErrorKind, Read};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entry {
    pub size: u64,
    pub mtime_ns: i64,
    pub ctime_ns: i64,
    pub ino: u64,
    pub hash: Digest,
    pub hashed_at_ns: i64,
}

const RACY_NS: i64 = 50_000_000;

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Index {
    pub files: HashMap<Vec<u8>, Entry>,
}

fn index_from_bytes(raw: Option<&[u8]>) -> Index {
    let Some(raw) = raw else { return Index::default() };
    let Ok(plain) = zstd::stream::decode_all(raw) else { return Index::default() };
    bincode::deserialize(&plain).unwrap_or_default()
}

fn index_bytes(index: &Index) -> io::Result<Vec<u8>> {
    let plain = bincode::serialize(index).map_err(io::Error::other)?;
    zstd::stream::encode_all(&plain[..], 1)
}

impl Index {
    pub fn load(path: &Path) -> Index {
        index_from_bytes(std::fs::read(path).ok().as_deref())
    }

    pub fn save(&self, path: &Path) -> io::Result<()> {
        crate::write_atomic(path, &index_bytes(self)?)
    }
}

#[derive(Clone)]
pub struct Scan {
    pub root: Digest,
    pub trees: HashMap<Digest, Vec<u8>>,
    pub files: HashMap<Digest, (PathBuf, u64)>,
    refs: HashMap<Digest, u32>,
    grafted: Vec<Digest>,
    pub dirs: Vec<PathBuf>,
    pub dir_trees: HashMap<Vec<u8>, Digest>,
    worktree_root: Vec<u8>,
    pub listed_dirs: u64,
    pub index: Index,
    pub index_changed: bool,
    pub file_count: u64,
    pub hashed_files: u64,
    pub hashed_bytes: u64,
}

#[derive(Default)]
struct ScanState {
    trees: HashMap<Digest, Vec<u8>>,
    files: HashMap<Digest, (PathBuf, u64)>,
    refs: HashMap<Digest, u32>,
    dir_trees: HashMap<Vec<u8>, Digest>,
    index: HashMap<Vec<u8>, Entry>,
    relink: HashSet<Digest>,
    listed_dirs: u64,
    removed: u64,
    hashed_files: u64,
    hashed_bytes: u64,
}

impl ScanState {
    fn add_file(&mut self, hash: Digest, path: PathBuf, size: u64) {
        *self.refs.entry(hash).or_insert(0) += 1;
        self.files.entry(hash).or_insert((path, size));
    }

    fn drop_file(&mut self, hash: Digest, path: &Path) {
        let Some(n) = self.refs.get_mut(&hash) else { return };
        assert!(*n > 0, "a counted content has a reference");
        *n -= 1;
        assert!(self.files.contains_key(&hash), "a counted content has a file");
        if *n == 0 {
            self.refs.remove(&hash);
            self.files.remove(&hash);
        } else if self.files.get(&hash).is_some_and(|(p, _)| p == path) {
            self.relink.insert(hash);
        }
    }
}

fn now_ns() -> i64 {
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos() as i64).unwrap_or(0)
}

pub fn hash_file(path: &Path, size: u64) -> io::Result<Digest> {
    let mut h = blake3::Hasher::new();
    if size >= 4 << 20 {
        h.update_mmap_rayon(path)?;
    } else {
        let mut f = std::fs::File::open(path)?;
        let mut buf = Vec::with_capacity(size as usize + 1);
        f.read_to_end(&mut buf)?;
        h.update(&buf);
    }
    Ok(*h.finalize().as_bytes())
}

fn join(rel: &[u8], name: &[u8]) -> Vec<u8> {
    assert!(!name.is_empty());
    let mut v = Vec::with_capacity(rel.len() + 1 + name.len());
    if !rel.is_empty() {
        v.extend_from_slice(rel);
        v.push(b'/');
    }
    v.extend_from_slice(name);
    assert_eq!(v.len(), rel.len() + name.len() + usize::from(!rel.is_empty()));
    v
}

fn excluded(patterns: &[String], rel: &[u8], name: &[u8]) -> bool {
    patterns.iter().any(|p| {
        let anchored = p.trim_end_matches('/').contains('/');
        let p = p.trim_matches('/').as_bytes();
        if anchored { p == rel } else { p == name }
    })
}

#[derive(Debug, Default, PartialEq)]
pub struct Ignored {
    paths: HashSet<Vec<u8>>,
    include: Vec<String>,
    on_way: HashSet<Vec<u8>>,
}

#[derive(Debug, PartialEq)]
enum Take {
    Skip,
    Keep,
    OnlyTheWay,
}

impl Ignored {
    pub fn from_git(root: &Path, include: &[String]) -> Option<Ignored> {
        let listed = crate::git::ignored(root)?;
        Some(Ignored::new(listed.iter().map(|p| p.as_slice()), include))
    }

    fn new<'p>(listed: impl Iterator<Item = &'p [u8]>, include: &[String]) -> Ignored {
        let mut me = Ignored { include: include.to_vec(), ..Ignored::default() };
        for p in listed {
            me.paths.insert(p.strip_suffix(b"/").unwrap_or(p).to_vec());
        }
        for p in include.iter().filter(|p| p.trim_end_matches('/').contains('/')) {
            let mut way = p.trim_matches('/').as_bytes();
            while let Some(i) = way.iter().rposition(|&b| b == b'/') {
                way = &way[..i];
                me.on_way.insert(way.to_vec());
            }
        }
        me
    }

    #[cfg(test)]
    pub fn for_test(listed: &[&str], include: &[String]) -> Ignored {
        Ignored::new(listed.iter().map(|p| p.as_bytes()), include)
    }

    fn included(&self, rel: &[u8], name: &[u8]) -> bool {
        excluded(&self.include, rel, name)
    }

    fn take(&self, rel: &[u8], name: &[u8], inside: bool) -> Take {
        if !inside && !self.paths.contains(rel) {
            return Take::Keep;
        }
        if self.included(rel, name) {
            Take::Keep
        } else if self.on_way.contains(rel) {
            Take::OnlyTheWay
        } else {
            Take::Skip
        }
    }

    pub fn left_out(&self, rel: &[u8]) -> bool {
        let mut inside = false;
        let mut at = 0;
        loop {
            let end = rel[at..].iter().position(|&b| b == b'/').map(|i| at + i).unwrap_or(rel.len());
            let (here, name) = (&rel[..end], &rel[at..end]);
            match self.take(here, name, inside) {
                Take::Skip => return true,
                Take::Keep => inside = false,
                Take::OnlyTheWay => inside = true,
            }
            if end == rel.len() {
                return false;
            }
            at = end + 1;
        }
    }
}

enum Directory {
    Reuse(Digest),
    Listing(Vec<(Vec<u8>, Observed)>),
}

enum Observed {
    Directory(Directory),
    Symlink(Vec<u8>),
    File { entry: Entry, exec: bool, hashed: bool },
}

struct ScanPlan<'a> {
    old: Option<&'a Index>,
    previous: Option<&'a Scan>,
    on_path: Option<&'a HashSet<Vec<u8>>>,
    exclude: &'a [String],
    ignored: Option<&'a Ignored>,
}

impl ScanPlan<'_> {
    fn reuse(&self, rel: &[u8]) -> Option<Digest> {
        if self.on_path?.contains(rel) {
            return None;
        }
        self.previous?.dir_trees.get(rel).copied()
    }

    fn take(&self, dir: &[u8], name: &[u8], inside: bool) -> Option<(Vec<u8>, Take)> {
        let child = join(dir, name);
        if name == b".git" || excluded(self.exclude, &child, name) || (dir.is_empty() && name == b"target") {
            return None;
        }
        let take = self.ignored.map_or(Take::Keep, |i| i.take(&child, name, inside));
        (take != Take::Skip).then_some((child, take))
    }

    fn reuse_file(&self, rel: &[u8], stamp: Stamp) -> Option<Entry> {
        let index = self.old.or_else(|| self.previous.map(|scan| &scan.index))?;
        let old = index.files.get(rel).copied()?;
        let same = old.size == stamp.size && old.mtime_ns == stamp.mtime_ns && old.ctime_ns == stamp.ctime_ns && old.ino == stamp.ino;
        let settled = stamp.mtime_ns.max(stamp.ctime_ns) + RACY_NS < old.hashed_at_ns;
        (same && settled).then_some(old)
    }
}

#[derive(Clone, Copy)]
struct Stamp {
    size: u64,
    mtime_ns: i64,
    ctime_ns: i64,
    ino: u64,
}

impl ScanState {
    fn dir(&mut self, root: &Path, rel: &[u8], directory: Directory, root_before: &[u8]) -> io::Result<Digest> {
        let listing = match directory {
            Directory::Reuse(digest) => return Ok(digest),
            Directory::Listing(listing) => listing,
        };
        self.listed_dirs += 1;
        let before = self.dir_trees.get(rel).map(|d| {
            if rel.is_empty() {
                Tree::decode(root_before)
            } else {
                self.decode(d)
            }
        }).transpose()?;
        let old: BTreeMap<_, _> = before.iter().flat_map(|t| t.entries.iter().map(|(n, x)| (n.as_slice(), x))).collect();
        let mut entries = Vec::with_capacity(listing.len());
        let mut index_add = Vec::new();
        for (name, observed) in listing {
            let child = join(rel, &name);
            let node = match observed {
                Observed::Directory(dir) => Node::Dir(self.dir(root, &child, dir, root_before)?),
                Observed::Symlink(target) => Node::Symlink(target),
                Observed::File { entry, exec, hashed } => {
                    if hashed {
                        self.hashed_files += 1;
                        self.hashed_bytes += entry.size;
                    }
                    index_add.push((name.clone(), child, entry));
                    Node::File { hash: entry.hash, size: entry.size, exec }
                }
            };
            entries.push((name, node));
        }
        entries.sort_by(|a, b| a.0.cmp(&b.0));
        assert!(entries.windows(2).all(|w| w[0].0 < w[1].0), "names in a directory are unique");
        for (name, was) in &old {
            let now = entries.binary_search_by(|(n, _)| n.as_slice().cmp(name)).ok().map(|i| &entries[i].1);
            let child = join(rel, name);
            match (was, now) {
                (Node::File { hash, .. }, Some(Node::File { hash: h, .. })) if h == hash => {}
                (Node::File { hash, .. }, _) => self.forget_file(root, &child, *hash),
                (Node::Dir(_), Some(Node::Dir(_))) => {}
                (Node::Dir(d), _) => self.forget(root, &child, *d)?,
                (Node::Symlink(_), _) => {}
            }
        }
        index_add.sort_unstable_by(|a, b| a.0.cmp(&b.0));
        for (name, child, entry) in index_add {
            if !matches!(old.get(name.as_slice()), Some(Node::File { hash, .. }) if *hash == entry.hash) {
                self.add_file(entry.hash, root.join(OsStr::from_bytes(&child)), entry.size);
            }
            self.index.insert(child, entry);
        }
        let encoded = Tree { entries }.encode();
        debug_assert!(Tree::decode(&encoded).is_ok());
        let digest = rargo_proto::tree::digest(&encoded);
        self.trees.insert(digest, encoded);
        self.dir_trees.insert(rel.to_vec(), digest);
        Ok(digest)
    }

    fn decode(&self, digest: &Digest) -> io::Result<Tree> {
        Tree::decode(self.trees.get(digest).ok_or_else(|| io::Error::other("a tree of the last scan is missing"))?)
    }

    fn forget_file(&mut self, root: &Path, child: &[u8], hash: Digest) {
        self.drop_file(hash, &root.join(OsStr::from_bytes(child)));
        self.index.remove(child);
        self.removed += 1;
    }

    fn forget(&mut self, root: &Path, rel: &[u8], digest: Digest) -> io::Result<()> {
        for (name, node) in self.decode(&digest)?.entries {
            let child = join(rel, &name);
            match node {
                Node::Dir(d) => self.forget(root, &child, d)?,
                Node::File { hash, .. } => self.forget_file(root, &child, hash),
                Node::Symlink(_) => {}
            }
        }
        self.dir_trees.remove(rel);
        Ok(())
    }
}

pub fn scan(root: &Path, old: &Index, exclude: &[String], ignored: Option<&Ignored>) -> io::Result<Scan> {
    drive(ScanCore::whole(root, Some(old), exclude, ignored))
}

pub fn rescan(root: &Path, prev: Scan, changed: &HashSet<PathBuf>, exclude: &[String], ignored: Option<&Ignored>) -> io::Result<Scan> {
    drive(ScanCore::rescan(root, prev, changed, exclude, ignored))
}

pub fn scan_store(dir: &Path, old: &Index) -> io::Result<Scan> {
    drive(ScanCore::whole(dir, Some(old), &[], None))
}

pub fn graft_git(scan: &mut Scan, root: &Path, git: &Path, history: Option<Scan>) -> io::Result<()> {
    let fresh = Index::default();
    let sub = drive(ScanCore::whole(git, Some(&fresh), &[], None))?;
    let gitfile = if root.join(".git").is_file() {
        let text = format!("gitdir: {}\n", rargo_proto::GIT_SIDE);
        let path = git.parent().unwrap_or(git).join("gitfile-v1");
        if std::fs::read(&path).ok().as_deref() != Some(text.as_bytes()) {
            crate::write_atomic(&path, text.as_bytes())?;
        }
        Some((path, text))
    } else {
        None
    };
    graft_into(scan, sub, history, gitfile)
}

fn graft_into(scan: &mut Scan, mut sub: Scan, history: Option<Scan>, gitfile: Option<(PathBuf, String)>) -> io::Result<()> {
    let put = |top: &mut Tree, name: &[u8], node: Node| {
        match top.entries.binary_search_by(|(n, _)| n.as_slice().cmp(name)) {
            Ok(i) => top.entries[i].1 = node,
            Err(i) => top.entries.insert(i, (name.to_vec(), node)),
        }
    };
    if let Some(h) = history {
        let mut top = Tree::decode(&sub.trees[&sub.root])?;
        let objects = top.entries.iter().find(|(n, _)| n == b"objects").map(|(_, node)| node.clone());
        let Some(Node::Dir(objects)) = objects else { return Err(io::Error::other("the git stand-in has no objects directory")) };
        sub.trees.extend(h.trees);
        let digest = merge(&mut sub.trees, objects, h.root)?;
        put(&mut top, b"objects", Node::Dir(digest));
        let encoded = top.encode();
        sub.root = rargo_proto::tree::digest(&encoded);
        sub.trees.insert(sub.root, encoded);
        for (hash, at) in h.files {
            sub.files.entry(hash).or_insert(at);
        }
    }
    let mut top = Tree::decode(&scan.trees[&scan.root])?;
    if let Some((path, text)) = gitfile {
        let hash = *blake3::hash(text.as_bytes()).as_bytes();
        scan.files.entry(hash).or_insert((path, text.len() as u64));
        scan.grafted.push(hash);
        put(&mut top, b".git", Node::File { hash, size: text.len() as u64, exec: false });
        put(&mut top, rargo_proto::GIT_SIDE.as_bytes(), Node::Dir(sub.root));
    } else {
        put(&mut top, b".git", Node::Dir(sub.root));
    }
    let encoded = top.encode();
    scan.trees.remove(&scan.root);
    scan.root = rargo_proto::tree::digest(&encoded);
    scan.trees.insert(scan.root, encoded);
    scan.trees.extend(sub.trees);
    for (h, at) in sub.files {
        scan.files.entry(h).or_insert(at);
        scan.grafted.push(h);
    }
    Ok(())
}

fn merge(trees: &mut HashMap<Digest, Vec<u8>>, a: Digest, b: Digest) -> io::Result<Digest> {
    let mut out = Tree::decode(&trees[&a])?;
    for (name, node) in Tree::decode(&trees[&b])?.entries {
        let node = match (out.get(&name).cloned(), node) {
            (Some(Node::Dir(x)), Node::Dir(y)) => Node::Dir(merge(trees, x, y)?),
            (_, n) => n,
        };
        match out.entries.binary_search_by(|(n, _)| n.as_slice().cmp(&name)) {
            Ok(i) => out.entries[i].1 = node,
            Err(i) => out.entries.insert(i, (name, node)),
        }
    }
    assert!(out.entries.windows(2).all(|w| w[0].0 < w[1].0), "a merged directory stays sorted");
    let encoded = out.encode();
    let digest = rargo_proto::tree::digest(&encoded);
    trees.insert(digest, encoded);
    Ok(digest)
}

fn finish_scan(root: &Path, old: Option<&Index>, previous: Option<Scan>, observed: Directory) -> io::Result<Scan> {
    let (mut state, root_before, files_before) = match previous {
        Some(mut scan) => {
            for hash in scan.grafted.drain(..) {
                if !scan.refs.contains_key(&hash) {
                    scan.files.remove(&hash);
                }
            }
            let count = scan.index.files.len();
            let state = ScanState {
                trees: scan.trees,
                files: scan.files,
                refs: scan.refs,
                dir_trees: scan.dir_trees,
                index: scan.index.files,
                ..ScanState::default()
            };
            (state, scan.worktree_root, count)
        }
        None => (ScanState::default(), Vec::new(), old.map_or(0, |index| index.files.len())),
    };
    let root_digest = state.dir(root, b"", observed, &root_before)?;
    if !state.relink.is_empty() {
        let mut candidates: BTreeMap<Digest, (&Vec<u8>, u64)> = BTreeMap::new();
        for (path, entry) in &state.index {
            if state.relink.contains(&entry.hash) {
                let candidate = candidates.entry(entry.hash).or_insert((path, entry.size));
                if path < candidate.0 {
                    *candidate = (path, entry.size);
                }
            }
        }
        for (hash, (path, size)) in candidates {
            state.files.insert(hash, (root.join(OsStr::from_bytes(path)), size));
        }
    }
    let worktree_root = state.trees.get(&root_digest).cloned().unwrap_or_default();
    if state.trees.len() > 2 * state.dir_trees.len() + 256 {
        let live: HashSet<_> = state.dir_trees.values().collect();
        state.trees.retain(|d, _| live.contains(d));
    }
    let mut dirs: Vec<_> = state.dir_trees.keys().map(|rel| if rel.is_empty() { root.to_path_buf() } else { root.join(OsStr::from_bytes(rel)) }).collect();
    dirs.sort_unstable();
    let index_changed = state.hashed_files > 0 || state.removed > 0 || state.index.len() != files_before;
    Ok(Scan {
        root: root_digest,
        trees: state.trees,
        files: state.files,
        refs: state.refs,
        grafted: Vec::new(),
        dirs,
        dir_trees: state.dir_trees,
        worktree_root,
        listed_dirs: state.listed_dirs,
        file_count: state.index.len() as u64,
        index: Index { files: state.index },
        index_changed,
        hashed_files: state.hashed_files,
        hashed_bytes: state.hashed_bytes,
    })
}

enum Ask {
    List { rel: Vec<u8> },
    Meta { rel: Vec<u8> },
    Cache { rel: Vec<u8> },
    Link { rel: Vec<u8> },
    Content { rel: Vec<u8>, size: u64 },
}

enum Answer {
    List(io::Result<Vec<Vec<u8>>>),
    Meta(io::Result<Meta>),
    Cache(Option<Vec<u8>>),
    Link(Option<Vec<u8>>),
    Content { at_ns: i64, hash: io::Result<Digest> },
}

struct Meta {
    kind: Kind,
    size: u64,
    mtime_ns: i64,
    ctime_ns: i64,
    ino: u64,
    exec: bool,
}

enum Kind {
    Dir,
    Symlink,
    File,
    Other,
}

enum Step {
    Ask(Vec<Ask>),
    Done,
}

struct DirBuild {
    inside: bool,
    phase: Phase,
}

enum Phase {
    NeedList,
    Listing(Vec<Row>),
    Done(Directory),
    Folded,
    Gone,
}

enum Row {
    Meta { name: Vec<u8>, child: Vec<u8>, take: Take },
    Cache { name: Vec<u8>, child: Vec<u8>, inside: bool },
    Link { name: Vec<u8>, child: Vec<u8> },
    Content { name: Vec<u8>, child: Vec<u8>, stamp: Stamp, exec: bool },
    Ready { name: Vec<u8>, observed: Observed },
    Omit { name: Vec<u8> },
    Child { name: Vec<u8>, child: Vec<u8> },
}

struct Pending {
    rel: Vec<u8>,
    kind: Pend,
}

enum Pend {
    List,
    Row(usize),
}

struct ScanCore<'a> {
    root_path: &'a Path,
    old: Option<&'a Index>,
    previous: Option<Scan>,
    on_path: Option<HashSet<Vec<u8>>>,
    exclude: &'a [String],
    ignored: Option<&'a Ignored>,
    dirs: BTreeMap<Vec<u8>, DirBuild>,
    pending: Vec<Pending>,
    outcome: Option<Scan>,
}

impl<'a> ScanCore<'a> {
    fn whole(root_path: &'a Path, old: Option<&'a Index>, exclude: &'a [String], ignored: Option<&'a Ignored>) -> Self {
        Self::open(root_path, old, None, None, exclude, ignored)
    }

    fn rescan(root_path: &'a Path, previous: Scan, changed: &HashSet<PathBuf>, exclude: &'a [String], ignored: Option<&'a Ignored>) -> Self {
        let on_path = Some(changed_directories(root_path, changed));
        Self::open(root_path, None, Some(previous), on_path, exclude, ignored)
    }

    fn open(
        root_path: &'a Path,
        old: Option<&'a Index>,
        previous: Option<Scan>,
        on_path: Option<HashSet<Vec<u8>>>,
        exclude: &'a [String],
        ignored: Option<&'a Ignored>,
    ) -> Self {
        let mut dirs = BTreeMap::new();
        dirs.insert(Vec::new(), DirBuild { inside: false, phase: Phase::NeedList });
        Self { root_path, old, previous, on_path, exclude, ignored, dirs, pending: Vec::new(), outcome: None }
    }

    fn plan(&self) -> ScanPlan<'_> {
        ScanPlan {
            old: self.old,
            previous: self.previous.as_ref(),
            on_path: self.on_path.as_ref(),
            exclude: self.exclude,
            ignored: self.ignored,
        }
    }

    fn advance(&mut self, answers: Vec<Answer>) -> io::Result<Step> {
        self.apply(answers)?;
        self.fold();
        if self.root_done() {
            self.outcome = Some(self.finish()?);
            return Ok(Step::Done);
        }
        let asks = self.collect_asks();
        if asks.is_empty() {
            return Err(io::Error::other("the scan stopped before the tree was built"));
        }
        Ok(Step::Ask(asks))
    }

    fn apply(&mut self, answers: Vec<Answer>) -> io::Result<()> {
        if answers.len() != self.pending.len() {
            return Err(io::Error::other("a scan step answered the wrong number of requests"));
        }
        let pending = std::mem::take(&mut self.pending);
        for (pend, answer) in pending.into_iter().zip(answers) {
            self.apply_one(pend, answer)?;
        }
        Ok(())
    }

    fn apply_one(&mut self, pend: Pending, answer: Answer) -> io::Result<()> {
        match pend.kind {
            Pend::List => self.apply_list(pend.rel, answer),
            Pend::Row(index) => self.apply_row(&pend.rel, index, answer),
        }
    }

    fn apply_list(&mut self, rel: Vec<u8>, answer: Answer) -> io::Result<()> {
        let Answer::List(names) = answer else { return Err(io::Error::other("a scan answer does not match its request")) };
        let names = match names {
            Ok(names) => names,
            Err(e) if e.kind() == ErrorKind::NotFound && !rel.is_empty() => {
                if let Some(dir) = self.dirs.get_mut(&rel) {
                    dir.phase = Phase::Gone;
                }
                return Ok(());
            }
            Err(e) => return Err(e),
        };
        let inside = self.dirs.get(&rel).ok_or_else(|| io::Error::other("a listing arrived for an unknown directory"))?.inside;
        if !matches!(self.dirs[&rel].phase, Phase::NeedList) {
            return Err(io::Error::other("a listing arrived twice"));
        }
        let mut names = names;
        names.sort();
        let mut rows = Vec::new();
        {
            let plan = self.plan();
            for name in names {
                if let Some((child, take)) = plan.take(&rel, &name, inside) {
                    rows.push(Row::Meta { name, child, take });
                }
            }
        }
        self.dirs.get_mut(&rel).unwrap().phase = Phase::Listing(rows);
        Ok(())
    }

    fn apply_row(&mut self, rel: &[u8], index: usize, answer: Answer) -> io::Result<()> {
        let row = {
            let dir = self.dirs.get_mut(rel).ok_or_else(|| io::Error::other("a scan answer names an unknown directory"))?;
            let Phase::Listing(rows) = &mut dir.phase else { return Err(io::Error::other("a scan answer arrived after its directory closed")) };
            if index >= rows.len() {
                return Err(io::Error::other("a scan answer names an unknown entry"));
            }
            std::mem::replace(&mut rows[index], Row::Omit { name: Vec::new() })
        };
        let next = self.reduce(row, answer)?;
        let Phase::Listing(rows) = &mut self.dirs.get_mut(rel).unwrap().phase else { unreachable!("the directory is still listing") };
        rows[index] = next;
        Ok(())
    }

    fn reduce(&mut self, row: Row, answer: Answer) -> io::Result<Row> {
        match (row, answer) {
            (Row::Meta { name, child, take }, Answer::Meta(meta)) => self.reduce_meta(name, child, take, meta),
            (Row::Cache { name, child, inside }, Answer::Cache(bytes)) => Ok(self.reduce_cache(name, child, inside, bytes.as_deref())),
            (Row::Link { name, .. }, Answer::Link(target)) => Ok(match target {
                Some(target) => Row::Ready { name, observed: Observed::Symlink(target) },
                None => Row::Omit { name },
            }),
            (Row::Content { name, child, stamp, exec }, Answer::Content { at_ns, hash }) => self.reduce_content(name, child, stamp, exec, at_ns, hash),
            _ => Err(io::Error::other("a scan answer does not match its request")),
        }
    }

    fn reduce_meta(&self, name: Vec<u8>, child: Vec<u8>, take: Take, meta: io::Result<Meta>) -> io::Result<Row> {
        let meta = match meta {
            Ok(meta) => meta,
            Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Row::Omit { name }),
            Err(e) => return Err(e),
        };
        match meta.kind {
            Kind::Dir => Ok(Row::Cache { name, child, inside: take == Take::OnlyTheWay }),
            Kind::Symlink => Ok(Row::Link { name, child }),
            Kind::File if take != Take::OnlyTheWay => {
                let stamp = Stamp { size: meta.size, mtime_ns: meta.mtime_ns, ctime_ns: meta.ctime_ns, ino: meta.ino };
                if let Some(entry) = self.plan().reuse_file(&child, stamp) {
                    Ok(Row::Ready { name, observed: Observed::File { entry, exec: meta.exec, hashed: false } })
                } else {
                    Ok(Row::Content { name, child, stamp, exec: meta.exec })
                }
            }
            Kind::File | Kind::Other => Ok(Row::Omit { name }),
        }
    }

    fn reduce_cache(&mut self, name: Vec<u8>, child: Vec<u8>, inside: bool, bytes: Option<&[u8]>) -> Row {
        if bytes.is_some_and(|b| b == rargo_proto::CACHEDIR_SIGNATURE) {
            return Row::Omit { name };
        }
        if let Some(digest) = self.plan().reuse(&child) {
            return Row::Ready { name, observed: Observed::Directory(Directory::Reuse(digest)) };
        }
        self.dirs.insert(child.clone(), DirBuild { inside, phase: Phase::NeedList });
        Row::Child { name, child }
    }

    fn reduce_content(&self, name: Vec<u8>, child: Vec<u8>, stamp: Stamp, exec: bool, at_ns: i64, hash: io::Result<Digest>) -> io::Result<Row> {
        let hash = match hash {
            Ok(hash) => hash,
            Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Row::Omit { name }),
            Err(e) => return Err(io::Error::new(e.kind(), format!("{}: {e}", abs(self.root_path, &child).display()))),
        };
        let entry = Entry { size: stamp.size, mtime_ns: stamp.mtime_ns, ctime_ns: stamp.ctime_ns, ino: stamp.ino, hash, hashed_at_ns: at_ns };
        Ok(Row::Ready { name, observed: Observed::File { entry, exec, hashed: true } })
    }

    fn fold(&mut self) {
        let rels: Vec<Vec<u8>> = self.dirs.keys().cloned().collect();
        for _ in 0..rels.len().saturating_add(1) {
            let mut progressed = false;
            for rel in &rels {
                if self.close_dir(rel) {
                    progressed = true;
                }
            }
            if !progressed {
                return;
            }
        }
        panic!("a scan fold did not finish");
    }

    fn close_dir(&mut self, rel: &[u8]) -> bool {
        let Phase::Listing(_) = &self.dirs[rel].phase else { return false };
        let ready: Vec<(usize, Vec<u8>, Vec<u8>, bool)> = {
            let Phase::Listing(rows) = &self.dirs[rel].phase else { return false };
            rows.iter().enumerate().filter_map(|(i, row)| {
                let Row::Child { name, child } = row else { return None };
                let done = match self.dirs.get(child).map(|dir| &dir.phase) {
                    Some(Phase::Done(_)) => true,
                    Some(Phase::Gone) => false,
                    _ => return None,
                };
                Some((i, name.clone(), child.clone(), done))
            }).collect()
        };
        let mut changed = false;
        for (index, name, child, done) in ready {
            let phase = std::mem::replace(&mut self.dirs.get_mut(&child).unwrap().phase, Phase::Folded);
            let directory = if done {
                let Phase::Done(directory) = phase else { unreachable!("the child was done") };
                Some(directory)
            } else {
                None
            };
            let Phase::Listing(rows) = &mut self.dirs.get_mut(rel).unwrap().phase else { unreachable!("the parent is listing") };
            rows[index] = match directory {
                Some(directory) => Row::Ready { name, observed: Observed::Directory(directory) },
                None => Row::Omit { name },
            };
            changed = true;
        }
        let finished = match &self.dirs[rel].phase {
            Phase::Listing(rows) => rows.iter().all(|row| matches!(row, Row::Ready { .. } | Row::Omit { .. })),
            _ => false,
        };
        if !finished {
            return changed;
        }
        let Phase::Listing(rows) = std::mem::replace(&mut self.dirs.get_mut(rel).unwrap().phase, Phase::Folded) else { unreachable!("the parent is listing") };
        let listing = rows.into_iter().filter_map(|row| match row {
            Row::Ready { name, observed } => Some((name, observed)),
            Row::Omit { .. } => None,
            Row::Meta { .. } | Row::Cache { .. } | Row::Link { .. } | Row::Content { .. } | Row::Child { .. } => None,
        }).collect();
        self.dirs.get_mut(rel).unwrap().phase = Phase::Done(Directory::Listing(listing));
        true
    }

    fn root_done(&self) -> bool {
        matches!(self.dirs.get(b"".as_slice()).map(|dir| &dir.phase), Some(Phase::Done(_)))
    }

    fn finish(&mut self) -> io::Result<Scan> {
        let Some(DirBuild { phase: Phase::Done(directory), .. }) = self.dirs.remove(b"".as_slice()) else {
            return Err(io::Error::other("the scan finished without a root"));
        };
        finish_scan(self.root_path, self.old, self.previous.take(), directory)
    }

    fn collect_asks(&mut self) -> Vec<Ask> {
        let mut asks = Vec::new();
        let mut pending = Vec::new();
        for (rel, dir) in &self.dirs {
            match &dir.phase {
                Phase::NeedList => {
                    asks.push(Ask::List { rel: rel.clone() });
                    pending.push(Pending { rel: rel.clone(), kind: Pend::List });
                }
                Phase::Listing(rows) => {
                    for (i, row) in rows.iter().enumerate() {
                        let ask = match row {
                            Row::Meta { child, .. } => Some(Ask::Meta { rel: child.clone() }),
                            Row::Cache { child, .. } => Some(Ask::Cache { rel: child.clone() }),
                            Row::Link { child, .. } => Some(Ask::Link { rel: child.clone() }),
                            Row::Content { child, stamp, .. } => Some(Ask::Content { rel: child.clone(), size: stamp.size }),
                            Row::Ready { .. } | Row::Omit { .. } | Row::Child { .. } => None,
                        };
                        if let Some(ask) = ask {
                            asks.push(ask);
                            pending.push(Pending { rel: rel.clone(), kind: Pend::Row(i) });
                        }
                    }
                }
                Phase::Done(_) | Phase::Folded | Phase::Gone => {}
            }
        }
        self.pending = pending;
        asks
    }

    fn invariants(&self) -> bool {
        if self.on_path.as_ref().is_some_and(|paths| !paths.contains(b"".as_slice())) {
            return false;
        }
        if self.pending.iter().any(|pend| !self.pending_ok(pend)) {
            return false;
        }
        for (rel, dir) in &self.dirs {
            if !rel_well_formed(rel) {
                return false;
            }
            match &dir.phase {
                Phase::Listing(rows) => {
                    if !rows_ok(rel, rows, &self.dirs) {
                        return false;
                    }
                }
                Phase::Done(Directory::Listing(listing)) => {
                    if !names_increase(listing.iter().map(|(name, _)| name.as_slice())) {
                        return false;
                    }
                }
                Phase::Done(Directory::Reuse(_)) | Phase::NeedList | Phase::Folded | Phase::Gone => {}
            }
        }
        if self.outcome.is_none() && self.dirs.values().any(|dir| matches!(dir.phase, Phase::Done(_))) && !self.root_done() {
            return false;
        }
        if let Some(prev) = &self.previous {
            if prev.dir_trees.iter().any(|(rel, hash)| !rel.is_empty() && !prev.trees.contains_key(hash)) {
                return false;
            }
        }
        self.outcome.as_ref().is_none_or(|scan| scan_holds(scan, self.root_path))
    }

    fn pending_ok(&self, pend: &Pending) -> bool {
        let Some(dir) = self.dirs.get(&pend.rel) else { return false };
        match pend.kind {
            Pend::List => matches!(dir.phase, Phase::NeedList),
            Pend::Row(i) => match &dir.phase {
                Phase::Listing(rows) => rows.get(i).is_some_and(|row| {
                    matches!(row, Row::Meta { .. } | Row::Cache { .. } | Row::Link { .. } | Row::Content { .. })
                }),
                _ => false,
            },
        }
    }
}

fn changed_directories(root: &Path, changed: &HashSet<PathBuf>) -> HashSet<Vec<u8>> {
    let mut on_path = HashSet::from([Vec::new()]);
    for dir in changed {
        let Ok(rel) = dir.strip_prefix(root) else { continue };
        let mut rel = rel.as_os_str().as_bytes();
        while !rel.is_empty() && on_path.insert(rel.to_vec()) {
            rel = match rel.iter().rposition(|&c| c == b'/') {
                Some(i) => &rel[..i],
                None => b"",
            };
        }
    }
    on_path
}

fn drive(mut core: ScanCore) -> io::Result<Scan> {
    let mut answers = Vec::new();
    loop {
        match core.advance(std::mem::take(&mut answers))? {
            Step::Ask(asks) => {
                assert!(core.invariants());
                answers = read_host(core.root_path, &asks);
            }
            Step::Done => {
                assert!(core.invariants());
                return Ok(core.outcome.take().expect("a finished scan is present"));
            }
        }
    }
}

fn read_host(root: &Path, asks: &[Ask]) -> Vec<Answer> {
    asks.par_iter().map(|ask| host_one(root, ask)).collect()
}

fn host_one(root: &Path, ask: &Ask) -> Answer {
    match ask {
        Ask::List { rel } => Answer::List(list_dir(&abs(root, rel))),
        Ask::Meta { rel } => Answer::Meta(match std::fs::symlink_metadata(abs(root, rel)) {
            Ok(md) => Ok(meta_of(&md)),
            Err(e) => Err(e),
        }),
        Ask::Cache { rel } => Answer::Cache(cache_tag(&abs(root, rel))),
        Ask::Link { rel } => Answer::Link(std::fs::read_link(abs(root, rel)).ok().map(|target| target.as_os_str().as_bytes().to_vec())),
        Ask::Content { rel, size } => Answer::Content { at_ns: now_ns(), hash: hash_file(&abs(root, rel), *size) },
    }
}

fn list_dir(abs_path: &Path) -> io::Result<Vec<Vec<u8>>> {
    let mut names = Vec::new();
    for entry in std::fs::read_dir(abs_path)? {
        names.push(entry?.file_name().as_bytes().to_vec());
    }
    Ok(names)
}

fn meta_of(md: &std::fs::Metadata) -> Meta {
    let kind = if md.is_symlink() {
        Kind::Symlink
    } else if md.is_dir() {
        Kind::Dir
    } else if md.is_file() {
        Kind::File
    } else {
        Kind::Other
    };
    Meta {
        kind,
        size: md.len(),
        mtime_ns: md.mtime() * 1_000_000_000 + md.mtime_nsec(),
        ctime_ns: md.ctime() * 1_000_000_000 + md.ctime_nsec(),
        ino: md.ino(),
        exec: md.mode() & 0o111 != 0,
    }
}

fn cache_tag(dir: &Path) -> Option<Vec<u8>> {
    let mut head = vec![0u8; rargo_proto::CACHEDIR_SIGNATURE.len()];
    let mut file = std::fs::File::open(dir.join("CACHEDIR.TAG")).ok()?;
    file.read_exact(&mut head).ok()?;
    Some(head)
}

fn abs(root: &Path, rel: &[u8]) -> PathBuf {
    if rel.is_empty() { root.to_path_buf() } else { root.join(OsStr::from_bytes(rel)) }
}

fn rel_well_formed(rel: &[u8]) -> bool {
    rel.is_empty() || rel.split(|b| *b == b'/').all(|part| !part.is_empty() && part != b"." && part != b"..")
}

fn child_matches(parent: &[u8], name: &[u8], child: &[u8]) -> bool {
    if parent.is_empty() {
        child == name
    } else {
        child.len() == parent.len() + 1 + name.len() && &child[..parent.len()] == parent && child[parent.len()] == b'/' && &child[parent.len() + 1..] == name
    }
}

fn row_name(row: &Row) -> &[u8] {
    match row {
        Row::Meta { name, .. } | Row::Cache { name, .. } | Row::Link { name, .. } | Row::Content { name, .. }
        | Row::Ready { name, .. } | Row::Omit { name, .. } | Row::Child { name, .. } => name,
    }
}

fn row_child(row: &Row) -> Option<&[u8]> {
    match row {
        Row::Meta { child, .. } | Row::Cache { child, .. } | Row::Link { child, .. } | Row::Content { child, .. } | Row::Child { child, .. } => Some(child),
        Row::Ready { .. } | Row::Omit { .. } => None,
    }
}

fn names_increase<'b>(names: impl Iterator<Item = &'b [u8]>) -> bool {
    let mut prev: Option<&[u8]> = None;
    for name in names {
        if name.is_empty() || name.contains(&b'/') {
            return false;
        }
        if prev.is_some_and(|prev| prev >= name) {
            return false;
        }
        prev = Some(name);
    }
    true
}

fn rows_ok(parent: &[u8], rows: &[Row], dirs: &BTreeMap<Vec<u8>, DirBuild>) -> bool {
    if !names_increase(rows.iter().map(row_name)) {
        return false;
    }
    rows.iter().all(|row| {
        let child_ok = row_child(row).is_none_or(|child| child_matches(parent, row_name(row), child));
        let link_ok = match row {
            Row::Child { child, .. } => matches!(dirs.get(child).map(|dir| &dir.phase), Some(Phase::NeedList | Phase::Listing(_))),
            _ => true,
        };
        child_ok && link_ok
    })
}

fn scan_holds(scan: &Scan, root: &Path) -> bool {
    scan.dir_trees.get(b"".as_slice()) == Some(&scan.root)
        && scan.dir_trees.values().all(|hash| scan.trees.contains_key(hash))
        && scan.refs.values().map(|&n| u64::from(n)).sum::<u64>() == scan.file_count
        && scan.files.iter().all(|(hash, (path, size))| {
            path.strip_prefix(root).ok().and_then(|rel| scan.index.files.get(rel.as_os_str().as_bytes())).is_some_and(|entry| &entry.hash == hash && &entry.size == size)
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeSet;
    use std::io::ErrorKind;

    const SETTLED: i64 = 1_000_000_000;

    enum Mem {
        Dir(BTreeMap<Vec<u8>, Mem>),
        File { bytes: Vec<u8>, mtime_ns: i64, ctime_ns: i64, ino: u64, exec: bool },
        Link(Vec<u8>),
        Other,
        Vanish,
        Race,
        Blocked { size: u64, mtime_ns: i64, ctime_ns: i64, ino: u64 },
    }

    struct Model {
        root: Mem,
        at_ns: i64,
    }

    fn dir() -> Mem {
        Mem::Dir(BTreeMap::new())
    }

    fn file(bytes: &str, ino: u64) -> Mem {
        Mem::File { bytes: bytes.as_bytes().to_vec(), mtime_ns: 0, ctime_ns: 0, ino, exec: false }
    }

    fn put(node: &mut Mem, rel: &str, child: Mem) {
        let Some((name, rest)) = rel.split_once('/') else {
            let Mem::Dir(map) = node else { panic!("a file is not a directory") };
            map.insert(rel.as_bytes().to_vec(), child);
            return;
        };
        let Mem::Dir(map) = node else { panic!("a file is not a directory") };
        let next = map.entry(name.as_bytes().to_vec()).or_insert_with(dir);
        put(next, rest, child);
    }

    fn drop_at(node: &mut Mem, rel: &str) {
        let Some((name, rest)) = rel.split_once('/') else {
            let Mem::Dir(map) = node else { panic!("a file is not a directory") };
            map.remove(rel.as_bytes());
            return;
        };
        let Mem::Dir(map) = node else { panic!("a file is not a directory") };
        if let Some(next) = map.get_mut(name.as_bytes()) {
            drop_at(next, rest);
        }
    }

    fn get<'a>(node: &'a Mem, rel: &[u8]) -> Option<&'a Mem> {
        if rel.is_empty() {
            return Some(node);
        }
        let mut cur = node;
        for part in rel.split(|b| *b == b'/') {
            match cur {
                Mem::Dir(map) => cur = map.get(part)?,
                _ => return None,
            }
        }
        Some(cur)
    }

    fn mem_mut<'a>(node: &'a mut Mem, rel: &str) -> &'a mut Mem {
        let Some((name, rest)) = rel.split_once('/') else {
            let Mem::Dir(map) = node else { panic!("a file is not a directory") };
            return map.get_mut(rel.as_bytes()).unwrap();
        };
        let Mem::Dir(map) = node else { panic!("a file is not a directory") };
        mem_mut(map.get_mut(name.as_bytes()).unwrap(), rest)
    }

    fn file_bytes<'a>(node: &'a Mem, rel: &[u8]) -> &'a [u8] {
        match get(node, rel) {
            Some(Mem::File { bytes, .. }) => bytes,
            _ => panic!("no file at {}", String::from_utf8_lossy(rel)),
        }
    }

    impl Model {
        fn answer(&self, asks: &[Ask]) -> Vec<Answer> {
            asks.iter().map(|ask| self.one(ask)).collect()
        }

        fn one(&self, ask: &Ask) -> Answer {
            match ask {
                Ask::List { rel } => Answer::List(self.list(rel)),
                Ask::Meta { rel } => Answer::Meta(self.meta(rel)),
                Ask::Cache { rel } => Answer::Cache(self.cache(rel)),
                Ask::Link { rel } => Answer::Link(self.link(rel)),
                Ask::Content { rel, .. } => self.content(rel),
            }
        }

        fn list(&self, rel: &[u8]) -> io::Result<Vec<Vec<u8>>> {
            match get(&self.root, rel) {
                Some(Mem::Dir(map)) => {
                    let mut names: Vec<_> = map.keys().cloned().collect();
                    names.reverse();
                    Ok(names)
                }
                _ => Err(io::Error::new(ErrorKind::NotFound, "missing")),
            }
        }

        fn meta(&self, rel: &[u8]) -> io::Result<Meta> {
            let Some(node) = get(&self.root, rel) else { return Err(io::Error::new(ErrorKind::NotFound, "missing")) };
            let meta = |kind, size, mtime_ns, ctime_ns, ino, exec| Meta { kind, size, mtime_ns, ctime_ns, ino, exec };
            Ok(match node {
                Mem::Dir(_) | Mem::Vanish => meta(Kind::Dir, 0, 0, 0, 0, false),
                Mem::File { bytes, mtime_ns, ctime_ns, ino, exec } => meta(Kind::File, bytes.len() as u64, *mtime_ns, *ctime_ns, *ino, *exec),
                Mem::Link(_) => meta(Kind::Symlink, 0, 0, 0, 0, false),
                Mem::Other => meta(Kind::Other, 0, 0, 0, 0, false),
                Mem::Race => meta(Kind::File, 1, 0, 0, 0, false),
                Mem::Blocked { size, mtime_ns, ctime_ns, ino } => meta(Kind::File, *size, *mtime_ns, *ctime_ns, *ino, false),
            })
        }

        fn cache(&self, rel: &[u8]) -> Option<Vec<u8>> {
            let path = join(rel, b"CACHEDIR.TAG");
            match get(&self.root, &path) {
                Some(Mem::File { bytes, .. }) if bytes.len() >= rargo_proto::CACHEDIR_SIGNATURE.len() => {
                    Some(bytes[..rargo_proto::CACHEDIR_SIGNATURE.len()].to_vec())
                }
                _ => None,
            }
        }

        fn link(&self, rel: &[u8]) -> Option<Vec<u8>> {
            match get(&self.root, rel) {
                Some(Mem::Link(target)) => Some(target.clone()),
                _ => None,
            }
        }

        fn content(&self, rel: &[u8]) -> Answer {
            let hash = match get(&self.root, rel) {
                Some(Mem::File { bytes, .. }) => Ok(*blake3::hash(bytes).as_bytes()),
                None | Some(Mem::Race) => Err(io::Error::new(ErrorKind::NotFound, "missing")),
                Some(Mem::Blocked { .. }) => Err(io::Error::new(ErrorKind::PermissionDenied, "unreadable")),
                Some(_) => Err(io::Error::other("not a file")),
            };
            Answer::Content { at_ns: self.at_ns, hash }
        }
    }

    fn run(core: &mut ScanCore, model: &Model) -> io::Result<Scan> {
        assert!(core.invariants(), "invariants before the first step");
        let mut answers = Vec::new();
        let mut steps = 0u32;
        loop {
            let step = core.advance(std::mem::take(&mut answers))?;
            steps += 1;
            assert!(core.invariants(), "invariants after step {steps}");
            match step {
                Step::Ask(asks) => answers = model.answer(&asks),
                Step::Done => return Ok(core.outcome.take().expect("a finished scan")),
            }
        }
    }

    fn scan_of(root: &Path, model: &Model, old: &Index, exclude: &[String], ignored: Option<&Ignored>) -> Scan {
        run(&mut ScanCore::whole(root, Some(old), exclude, ignored), model).unwrap()
    }

    fn rescan_of(root: &Path, model: &Model, prev: Scan, changed: &HashSet<PathBuf>, exclude: &[String], ignored: Option<&Ignored>) -> Scan {
        run(&mut ScanCore::rescan(root, prev, changed, exclude, ignored), model).unwrap()
    }

    fn tree_of(scan: &Scan, digest: &Digest) -> Tree {
        Tree::decode(&scan.trees[digest]).unwrap()
    }

    fn names(scan: &Scan, digest: &Digest) -> Vec<String> {
        tree_of(scan, digest).entries.iter().map(|(name, _)| String::from_utf8(name.clone()).unwrap()).collect()
    }

    fn sub(scan: &Scan, digest: &Digest, name: &str) -> Digest {
        match tree_of(scan, digest).get(name.as_bytes()) {
            Some(Node::Dir(digest)) => *digest,
            other => panic!("{name}: {other:?}"),
        }
    }

    fn rels(scan: &Scan, root: &Path) -> BTreeSet<Vec<u8>> {
        scan.dirs.iter().map(|dir| dir.strip_prefix(root).unwrap().as_os_str().as_bytes().to_vec()).collect()
    }

    fn rel_of(root: &Path, path: &Path) -> String {
        String::from_utf8(path.strip_prefix(root).unwrap().as_os_str().as_bytes().to_vec()).unwrap()
    }

    fn same(a: &Scan, b: &Scan, root: &Path, model: &Mem) {
        assert_eq!(a.root, b.root);
        assert_eq!(a.dirs, b.dirs);
        assert_eq!(a.dir_trees, b.dir_trees);
        assert_eq!(a.file_count, b.file_count);
        assert_eq!(a.refs, b.refs);
        assert_eq!(a.index.files.len(), b.index.files.len());
        for (path, entry) in &a.index.files {
            let other = &b.index.files[path];
            assert_eq!((entry.hash, entry.size, entry.mtime_ns, entry.ctime_ns, entry.ino), (other.hash, other.size, other.mtime_ns, other.ctime_ns, other.ino));
        }
        assert_eq!(a.files.len(), b.files.len());
        for (hash, (path, size)) in &a.files {
            let bytes = file_bytes(model, path.strip_prefix(root).unwrap().as_os_str().as_bytes());
            assert_eq!(*size, bytes.len() as u64);
            assert_eq!(*hash, *blake3::hash(bytes).as_bytes());
            assert_eq!(b.files.get(hash), Some(&(path.clone(), *size)));
        }
        assert!(a.dir_trees.values().all(|digest| a.trees.contains_key(digest)));
    }

    #[test]
    fn rescan_reads_only_the_directories_it_is_told_about() {
        let root = Path::new("/mem");
        let exclude = vec!["node_modules".to_string()];
        let mut tree = dir();
        put(&mut tree, "Cargo.toml", file("[package]", 1));
        put(&mut tree, "src/lib.rs", file("pub fn a() {}", 2));
        put(&mut tree, "src/deep/x.rs", file("x", 3));
        put(&mut tree, "src/deep/same.rs", file("same", 4));
        put(&mut tree, "docs/same.md", file("same", 5));
        put(&mut tree, "docs/a.md", file("a", 6));
        put(&mut tree, "docs/old/b.md", file("b", 7));
        put(&mut tree, "target/debug/out", file("o", 8));
        put(&mut tree, "node_modules/m/i.js", file("m", 9));
        let mut model = Model { root: tree, at_ns: SETTLED };
        let whole = |model: &Model| scan_of(root, model, &Index::default(), &exclude, None);

        let s0 = whole(&model);
        assert_eq!(rels(&s0, root), BTreeSet::from([b"".to_vec(), b"src".to_vec(), b"src/deep".to_vec(), b"docs".to_vec(), b"docs/old".to_vec()]));
        assert_eq!(s0.listed_dirs, 5);

        drop_at(&mut model.root, "src/deep/x.rs");
        put(&mut model.root, "src/deep/x.rs", Mem::File { bytes: b"changed".to_vec(), mtime_ns: SETTLED, ctime_ns: SETTLED, ino: 3, exec: false });
        put(&mut model.root, "src/deep/new/y.rs", file("y", 10));
        drop_at(&mut model.root, "docs/old");
        let told: HashSet<PathBuf> = [root.join("src/deep"), root.join("docs"), root.join("docs/old")].into_iter().collect();
        let s1 = rescan_of(root, &model, s0, &told, &exclude, None);
        same(&s1, &whole(&model), root, &model.root);
        assert_eq!(s1.listed_dirs, 5);
        assert_eq!(s1.hashed_files, 2, "only the changed and the new file are read");
        assert!(s1.index_changed);

        let Mem::File { bytes, mtime_ns, ctime_ns, .. } = mem_mut(&mut model.root, "docs/a.md") else { panic!("a.md") };
        *bytes = b"unseen".to_vec();
        *mtime_ns = SETTLED;
        *ctime_ns = SETTLED;
        let s2 = rescan_of(root, &model, s1.clone(), &[root.join("src")].into_iter().collect(), &exclude, None);
        assert_eq!(s2.root, s1.root);
        assert_eq!(s2.listed_dirs, 2, "the root and src; src/deep and src/deep/new stay as they were");
        assert!(!s2.index_changed);
        let s3 = rescan_of(root, &model, s2, &[root.join("docs")].into_iter().collect(), &exclude, None);
        same(&s3, &whole(&model), root, &model.root);

        let shared = *blake3::hash(b"same").as_bytes();
        let recorded = s3.files[&shared].0.clone();
        drop_at(&mut model.root, &rel_of(root, &recorded));
        let s4 = rescan_of(root, &model, s3, &[recorded.parent().unwrap().to_path_buf()].into_iter().collect(), &exclude, None);
        same(&s4, &whole(&model), root, &model.root);
        assert_ne!(s4.files[&shared].0, recorded);
        let other = s4.files[&shared].0.clone();
        drop_at(&mut model.root, &rel_of(root, &other));
        let s5 = rescan_of(root, &model, s4, &[other.parent().unwrap().to_path_buf()].into_iter().collect(), &exclude, None);
        same(&s5, &whole(&model), root, &model.root);
        assert!(!s5.files.contains_key(&shared));

        let s6 = rescan_of(root, &model, s5.clone(), &HashSet::new(), &exclude, None);
        same(&s6, &s5, root, &model.root);
        assert_eq!(s6.listed_dirs, 1);
        let outside: HashSet<PathBuf> = [PathBuf::from("/elsewhere")].into_iter().collect();
        let skipped = rescan_of(root, &model, s6.clone(), &outside, &exclude, None);
        assert_eq!(skipped.root, s6.root);
        assert_eq!(skipped.listed_dirs, 1);

        let mut git = dir();
        put(&mut git, "HEAD", file("ref: refs/heads/main", 1));
        let git_scan = scan_of(Path::new("/git"), &Model { root: git, at_ns: SETTLED }, &Index::default(), &[], None);
        let mut grafted = s6.clone();
        graft_into(&mut grafted, git_scan, None, None).unwrap();
        assert_ne!(grafted.root, s6.root);
        same(&rescan_of(root, &model, grafted, &HashSet::new(), &exclude, None), &s6, root, &model.root);
    }

    #[test]
    fn ignore_rules_keep_included_paths_and_record_symlinks_on_the_way() {
        let root = Path::new("/mem");
        let mut tree = dir();
        put(&mut tree, ".gitignore", file("node_modules/\n*.log\ndata/\n.env\n", 1));
        put(&mut tree, "src/lib.rs", file("pub fn a() {}", 2));
        put(&mut tree, "node_modules/a/b.js", file("x", 3));
        put(&mut tree, "x.log", file("x", 4));
        put(&mut tree, "src/y.log", file("x", 5));
        put(&mut tree, "kept.log", file("tracked although it matches", 6));
        put(&mut tree, "data/big.bin", file("x", 7));
        put(&mut tree, "data/schema.sql", file("create table t;", 8));
        put(&mut tree, "data/sub/z", file("x", 9));
        put(&mut tree, "data/noise.txt", file("x", 10));
        put(&mut tree, ".env", file("A=1", 11));
        put(&mut tree, "linkdir", Mem::Link(b"inner".to_vec()));
        put(&mut tree, "wayfile", file("not descended", 12));
        let model = Model { root: tree, at_ns: SETTLED };
        let listed = ["node_modules", "x.log", "src/y.log", "data", ".env", "linkdir", "wayfile"];
        let open = scan_of(root, &model, &Index::default(), &[], None);
        assert!(names(&open, &open.root).contains(&"node_modules".to_string()));

        let include = ["data/schema.sql".to_string(), ".env".to_string(), "linkdir/kept".to_string(), "wayfile/nope".to_string()];
        let ignored = Ignored::for_test(&listed, &include);
        let scan = scan_of(root, &model, &Index::default(), &[], Some(&ignored));
        assert_eq!(names(&scan, &scan.root), [".env", ".gitignore", "data", "kept.log", "linkdir", "src"]);
        assert_eq!(tree_of(&scan, &scan.root).get(b"linkdir"), Some(&Node::Symlink(b"inner".to_vec())));
        assert_eq!(names(&scan, &sub(&scan, &scan.root, "src")), ["lib.rs"]);
        assert_eq!(names(&scan, &sub(&scan, &scan.root, "data")), ["schema.sql"], "only the way to the included file");
        assert!(!names(&scan, &scan.root).iter().any(|name| name == "wayfile" || name == "node_modules" || name == "x.log"));
        assert!(!scan.dirs.iter().any(|dir| dir.ends_with("node_modules") || dir.ends_with("data/sub")), "not looked into, so not watched");
        for (path, out) in [
            ("node_modules/a/b.js", true),
            ("x.log", true),
            ("src/y.log", true),
            ("data/big.bin", true),
            ("data/sub/z", true),
            ("data/noise.txt", true),
            ("data/schema.sql", false),
            (".env", false),
            ("kept.log", false),
            ("src/lib.rs", false),
        ] {
            assert_eq!(ignored.left_out(path.as_bytes()), out, "{path}");
        }
        assert!(!ignored.left_out(b"data"), "data/ is not left out whole");

        let ignored = Ignored::for_test(&listed, &[]);
        let scan = scan_of(root, &model, &Index::default(), &[], Some(&ignored));
        assert_eq!(names(&scan, &scan.root), [".gitignore", "kept.log", "src"]);
    }

    #[test]
    fn skips_caches_excludes_and_non_files() {
        let root = Path::new("/tree");
        let mut tree = dir();
        put(&mut tree, "Cargo.toml", file("[package]", 1));
        put(&mut tree, "src/lib.rs", file("pub fn a() {}", 2));
        put(&mut tree, ".git/HEAD", file("ref: refs/heads/main", 3));
        put(&mut tree, "target/debug/app", file("binary", 4));
        put(&mut tree, "sub/out/CACHEDIR.TAG", Mem::File { bytes: rargo_proto::CACHEDIR_TAG.as_bytes().to_vec(), mtime_ns: 0, ctime_ns: 0, ino: 5, exec: false });
        put(&mut tree, "sub/out/debug/x", file("binary", 6));
        put(&mut tree, "sub/Cargo.lock", file("ignored by git, synced anyway", 7));
        put(&mut tree, "node_modules/big/file", file("junk", 8));
        put(&mut tree, "docs/node_modules/x", file("junk", 9));
        put(&mut tree, "vendor/skip/me", file("junk", 10));
        put(&mut tree, "vendor/keep/me", Mem::File { bytes: b"kept".to_vec(), mtime_ns: 0, ctime_ns: 0, ino: 11, exec: true });
        put(&mut tree, "cache/x", file("at the root: excluded", 12));
        put(&mut tree, "docs/cache/y", file("deeper: kept", 13));
        put(&mut tree, "link", Mem::Link(b"src".to_vec()));
        put(&mut tree, "empty", dir());
        put(&mut tree, "weird", Mem::Other);
        put(&mut tree, "ghost", Mem::Vanish);
        put(&mut tree, "ghostfile", Mem::Race);
        put(&mut tree, "noted/CACHEDIR.TAG", file("nope", 14));
        put(&mut tree, "noted/a", file("a", 15));
        let model = Model { root: tree, at_ns: SETTLED };
        let exclude = vec!["node_modules".to_string(), "vendor/skip".to_string(), "/cache".to_string()];
        let scan = scan_of(root, &model, &Index::default(), &exclude, None);
        let top = tree_of(&scan, &scan.root);
        assert_eq!(names(&scan, &scan.root), ["Cargo.toml", "docs", "empty", "link", "noted", "src", "sub", "vendor"]);
        assert_eq!(top.get(b"link"), Some(&Node::Symlink(b"src".to_vec())));
        assert_eq!(top.get(b"weird"), None);
        assert_eq!(top.get(b"ghost"), None);
        assert_eq!(top.get(b"ghostfile"), None);
        assert_eq!(tree_of(&scan, &sub(&scan, &scan.root, "sub")).entries.len(), 1);
        assert_eq!(tree_of(&scan, &sub(&scan, &scan.root, "docs")).entries.len(), 1, "only docs/cache");
        assert_eq!(tree_of(&scan, &sub(&scan, &scan.root, "vendor")).entries.len(), 1);
        assert_eq!(names(&scan, &sub(&scan, &scan.root, "noted")), ["CACHEDIR.TAG", "a"]);
        assert!(tree_of(&scan, &sub(&scan, &scan.root, "empty")).entries.is_empty());
        let keep = sub(&scan, &sub(&scan, &scan.root, "vendor"), "keep");
        assert!(matches!(tree_of(&scan, &keep).get(b"me"), Some(Node::File { exec: true, .. })));
        assert_eq!(scan.file_count, 7);
        assert_eq!(scan.hashed_files, 7);
        assert!(scan.index_changed);
        let lib = *blake3::hash(b"pub fn a() {}").as_bytes();
        assert_eq!(scan.files[&lib], (root.join("src/lib.rs"), 13));
        let kept = *blake3::hash(b"kept").as_bytes();
        assert_eq!(scan.files[&kept], (root.join("vendor/keep/me"), 4));
    }

    #[test]
    fn graft_edits_the_tree_without_reading_the_worktree() {
        let root = Path::new("/wt");
        let stand = Path::new("/stand");
        let store = Path::new("/store");
        let mut work = dir();
        put(&mut work, "src/lib.rs", file("pub fn a() {}", 1));
        let work = Model { root: work, at_ns: SETTLED };
        let base = scan_of(root, &work, &Index::default(), &[], None);

        let mut head = dir();
        put(&mut head, "HEAD", file("ref: refs/heads/main\n", 1));
        let head_scan = scan_of(stand, &Model { root: head, at_ns: SETTLED }, &Index::default(), &[], None);
        let mut normal = base.clone();
        graft_into(&mut normal, head_scan, None, None).unwrap();
        let top = tree_of(&normal, &normal.root);
        assert!(matches!(top.get(b".git"), Some(Node::Dir(_))));
        assert!(top.get(rargo_proto::GIT_SIDE.as_bytes()).is_none());
        assert_eq!(top.entries[0].0, b".git");
        let git = sub(&normal, &normal.root, ".git");
        assert_eq!(tree_of(&normal, &git).entries.len(), 1);
        let head_hash = *blake3::hash(b"ref: refs/heads/main\n").as_bytes();
        assert_eq!(normal.files[&head_hash].0, stand.join("HEAD"));

        let mut stand_in = dir();
        put(&mut stand_in, "HEAD", file("ref: refs/heads/main\n", 1));
        put(&mut stand_in, "objects/pack/pack-1.pack", file("pack", 2));
        let mut history = dir();
        put(&mut history, "pack/pack-2.pack", file("history", 3));
        let sub_scan = scan_of(stand, &Model { root: stand_in, at_ns: SETTLED }, &Index::default(), &[], None);
        let history_scan = scan_of(store, &Model { root: history, at_ns: SETTLED }, &Index::default(), &[], None);
        let mut merged = base.clone();
        graft_into(&mut merged, sub_scan, Some(history_scan), None).unwrap();
        let git = sub(&merged, &merged.root, ".git");
        let objects = sub(&merged, &git, "objects");
        let pack = sub(&merged, &objects, "pack");
        let packs = tree_of(&merged, &pack);
        assert!(packs.get(b"pack-1.pack").is_some() && packs.get(b"pack-2.pack").is_some(), "both packs side by side");
        let Some(Node::File { hash, .. }) = packs.get(b"pack-2.pack").cloned() else { panic!("pack-2") };
        assert_eq!(merged.files[&hash].0, store.join("pack/pack-2.pack"), "sent from the store");
        assert_eq!(hash, *blake3::hash(b"history").as_bytes());

        let mut bare = dir();
        put(&mut bare, "HEAD", file("ref: refs/heads/main\n", 1));
        let bare_scan = scan_of(stand, &Model { root: bare, at_ns: SETTLED }, &Index::default(), &[], None);
        let mut history = dir();
        put(&mut history, "pack/pack-2.pack", file("history", 3));
        let history_scan = scan_of(store, &Model { root: history, at_ns: SETTLED }, &Index::default(), &[], None);
        let before = base.root;
        let mut unchanged = base.clone();
        let err = graft_into(&mut unchanged, bare_scan, Some(history_scan), None).unwrap_err();
        assert_eq!(err.to_string(), "the git stand-in has no objects directory");
        assert_eq!(unchanged.root, before);

        let mut side = dir();
        put(&mut side, "HEAD", file("ref: refs/heads/main\n", 1));
        let side_scan = scan_of(stand, &Model { root: side, at_ns: SETTLED }, &Index::default(), &[], None);
        let text = format!("gitdir: {}\n", rargo_proto::GIT_SIDE);
        let gitfile = PathBuf::from("/stand/gitfile-v1");
        let mut linked = base;
        graft_into(&mut linked, side_scan, None, Some((gitfile.clone(), text.clone()))).unwrap();
        let top = tree_of(&linked, &linked.root);
        let Some(Node::File { hash, size, exec }) = top.get(b".git").cloned() else { panic!("`.git` is not a file") };
        assert!(!exec);
        assert_eq!(size, text.len() as u64);
        assert_eq!(hash, *blake3::hash(text.as_bytes()).as_bytes());
        assert_eq!(linked.files[&hash], (gitfile, text.len() as u64));
        let side = sub(&linked, &linked.root, rargo_proto::GIT_SIDE);
        assert!(tree_of(&linked, &side).get(b"HEAD").is_some());
    }

    #[test]
    fn mtime_size_and_ino_decide_reuse() {
        let root = Path::new("/mem");
        let mut tree = dir();
        put(&mut tree, "hello", Mem::File { bytes: b"hello".to_vec(), mtime_ns: 1, ctime_ns: 2, ino: 3, exec: false });
        let boundary = 2 + RACY_NS;
        let mut model = Model { root: tree, at_ns: boundary };
        let first = scan_of(root, &model, &Index::default(), &[], None);
        assert_eq!(first.hashed_files, 1);
        assert_eq!(first.index.files[b"hello".as_slice()].hashed_at_ns, boundary);

        let second = scan_of(root, &model, &first.index, &[], None);
        assert_eq!(second.hashed_files, 1, "a hash taken at the racy boundary is not settled");

        model.at_ns = boundary + 1;
        let third = scan_of(root, &model, &second.index, &[], None);
        assert_eq!(third.hashed_files, 1);
        assert_eq!(third.index.files[b"hello".as_slice()].hashed_at_ns, boundary + 1);
        let fourth = scan_of(root, &model, &third.index, &[], None);
        assert_eq!(fourth.hashed_files, 0);
        assert_eq!(fourth.root, third.root);

        let Mem::File { ino, .. } = mem_mut(&mut model.root, "hello") else { panic!("hello") };
        *ino = 9;
        let fifth = scan_of(root, &model, &fourth.index, &[], None);
        assert_eq!(fifth.hashed_files, 1);
        assert_eq!(fifth.root, fourth.root);
        assert_eq!(fifth.index.files[b"hello".as_slice()].hash, *blake3::hash(b"hello").as_bytes());

        let Mem::File { bytes, .. } = mem_mut(&mut model.root, "hello") else { panic!("hello") };
        bytes.push(b'!');
        let sixth = scan_of(root, &model, &fifth.index, &[], None);
        assert_eq!(sixth.hashed_files, 1);
        assert_eq!(sixth.index.files[b"hello".as_slice()].hash, *blake3::hash(b"hello!").as_bytes());
        assert_ne!(sixth.root, fifth.root);
    }

    #[test]
    fn index_bytes_round_trip_without_a_file() {
        let root = Path::new("/mem");
        let mut tree = dir();
        put(&mut tree, "a", file("x", 1));
        let scan = scan_of(root, &Model { root: tree, at_ns: SETTLED }, &Index::default(), &[], None);
        let bytes = index_bytes(&scan.index).unwrap();
        assert_eq!(index_from_bytes(Some(&bytes)).files, scan.index.files);
        assert!(index_from_bytes(None).files.is_empty());
        assert!(index_from_bytes(Some(b"garbage")).files.is_empty());
    }

    #[test]
    fn a_missing_root_is_an_error_and_an_unreadable_file_names_its_path() {
        let empty = Index::default();
        let mut core = ScanCore::whole(Path::new("/missing"), Some(&empty), &[], None);
        assert!(core.invariants());
        let Step::Ask(asks) = core.advance(Vec::new()).unwrap() else { panic!("the root is listed") };
        assert!(core.invariants());
        assert!(matches!(asks.as_slice(), [Ask::List { rel }] if rel.is_empty()));
        let err = match core.advance(vec![Answer::List(Err(io::Error::new(ErrorKind::NotFound, "gone")))]) {
            Err(err) => err,
            Ok(_) => panic!("a missing root is an error"),
        };
        assert_eq!(err.kind(), ErrorKind::NotFound);
        assert!(core.invariants());

        let root = Path::new("/mem");
        let mut tree = dir();
        put(&mut tree, "secret", Mem::Blocked { size: 4, mtime_ns: 0, ctime_ns: 0, ino: 1 });
        let err = match run(&mut ScanCore::whole(root, Some(&Index::default()), &[], None), &Model { root: tree, at_ns: SETTLED }) {
            Err(err) => err,
            Ok(_) => panic!("an unreadable file fails the scan"),
        };
        assert_eq!(err.kind(), ErrorKind::PermissionDenied);
        assert!(err.to_string().contains("/mem/secret"), "{err}");
    }
}