grit-lib 0.5.25

Core library for the grit Git implementation
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
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
//! Directory-oriented worktree stat reads for index comparison.
//!
//! Groups indexed blob paths by parent directory. On Windows each directory is listed once with
//! `read_dir` and tracked files reuse the listing's `DirEntry` metadata, because a path stat
//! opens a handle per file there. Elsewhere each tracked path gets one metadata probe:
//! listing a directory costs more than probing its tracked files (`readdir` on ext4
//! dominates large directories). Literal paths on 64-bit Linux can share a parent handle
//! and use `fstatat` without following file symlinks, avoiding repeated absolute path
//! construction and lookup. Small directories and paths requiring case or Unicode
//! resolution keep the per-path metadata lookup.
//!
//! When [`WorktreeBlobScanOptions::preload_index`] is true and the tracked blob count is at
//! least [`PARALLEL_STAT_MIN_ENTRIES`], stat collection preloads disk lookups in parallel by
//! partitioning sorted index entries across a small thread pool (`std::thread::scope`). Cuts
//! prefer parent-directory boundaries unless one directory holds more entries than a fair
//! worker share (Git `preload-index` style). On Windows each parent directory stays on one
//! worker because listing is shared via `read_dir`. Visit order stays directory-sorted so diff
//! output matches the serial path; only the metadata probes are parallelized. A later shared
//! thread pool can replace the scoped helper without changing callers.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

use std::sync::mpsc::{self, RecvTimeoutError};

use crate::error::{Error, Result};
use crate::progress::{poll_watch, StatScanCancel, CANCEL_POLL_STRIDE};

type DirAbsFn = dyn Fn(&[u8]) -> PathBuf + Send + Sync;

#[derive(Clone, Copy)]
enum StatBoundaryPoll<'a> {
    Owner(&'a StatScanCancel),
    Worker(Option<&'a Arc<AtomicBool>>),
}

impl StatBoundaryPoll<'_> {
    fn poll_directory(&self) -> Result<()> {
        match self {
            Self::Owner(cancel) => cancel.poll(),
            Self::Worker(watch) => {
                if let Some(w) = watch {
                    poll_watch(w)?;
                }
                Ok(())
            }
        }
    }

    fn poll_stride(&self, counter: &mut usize) -> Result<()> {
        *counter = counter.wrapping_add(1);
        if !counter.is_multiple_of(CANCEL_POLL_STRIDE) {
            return Ok(());
        }
        match self {
            Self::Owner(cancel) => cancel.poll(),
            Self::Worker(watch) => {
                if let Some(w) = watch {
                    poll_watch(w)?;
                }
                Ok(())
            }
        }
    }
}

/// Whether tracked files are stat-ed through one `read_dir` of their parent (see module docs).
/// On NTFS, one listing per parent beats a path stat per indexed file even for single-file
/// leaf directories (issue #1231).
const LIST_PARENT_DIRECTORIES: bool = cfg!(windows);

/// Test hook: incremented once per worktree metadata syscall used by directory scans.
static WORKTREE_METADATA_PROBE: AtomicUsize = AtomicUsize::new(0);

/// Test hook: `read_dir` calls during directory-grouped blob scans.
static WORKTREE_READ_DIR_PROBE: AtomicUsize = AtomicUsize::new(0);

/// Test hook: `core.ignorecase` worktree path resolutions that probe the filesystem.
static WORKTREE_PATH_RESOLVE_PROBE: AtomicUsize = AtomicUsize::new(0);

/// Only syscalls under this root are counted, so tests scanning other worktrees in
/// parallel do not inflate a probing test's counts.
static WORKTREE_PROBE_ACTIVE: AtomicUsize = AtomicUsize::new(0);
// hygiene: test-only probe root for integration switch regressions
static WORKTREE_PROBE_ROOT: std::sync::Mutex<Option<PathBuf>> = std::sync::Mutex::new(None);

/// Serializes probing tests; each owns the counters while its scope is alive.
// hygiene: test-only probe serialization for integration switch regressions
static WORKTREE_PROBE_SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(());

/// Keeps worktree syscall probing armed for one test; disarms on drop.
#[doc(hidden)]
pub struct WorktreeProbeScope {
    _serial: std::sync::MutexGuard<'static, ()>,
}

impl Drop for WorktreeProbeScope {
    fn drop(&mut self) {
        *WORKTREE_PROBE_ROOT
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = None;
        WORKTREE_PROBE_ACTIVE.store(0, Ordering::Release);
    }
}

/// Zero the probe counters and count only syscalls on paths under `root`.
#[doc(hidden)]
pub fn reset_worktree_metadata_probe(root: &Path) -> WorktreeProbeScope {
    let serial = WORKTREE_PROBE_SERIAL
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    *WORKTREE_PROBE_ROOT
        .lock()
        .unwrap_or_else(|e| e.into_inner()) = Some(root.to_path_buf());
    WORKTREE_METADATA_PROBE.store(0, Ordering::Relaxed);
    WORKTREE_READ_DIR_PROBE.store(0, Ordering::Relaxed);
    WORKTREE_PATH_RESOLVE_PROBE.store(0, Ordering::Relaxed);
    WORKTREE_PROBE_ACTIVE.store(1, Ordering::Release);
    WorktreeProbeScope { _serial: serial }
}

fn probe_counts(path: &Path) -> bool {
    if WORKTREE_PROBE_ACTIVE.load(Ordering::Acquire) == 0 {
        return false;
    }
    WORKTREE_PROBE_ROOT
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .as_deref()
        .is_some_and(|root| path.starts_with(root))
}

/// Returns metadata syscall probes recorded since [`reset_worktree_metadata_probe`].
#[doc(hidden)]
pub fn worktree_metadata_probe_count() -> usize {
    WORKTREE_METADATA_PROBE.load(Ordering::Relaxed)
}

/// Returns `read_dir` probes recorded since [`reset_worktree_metadata_probe`].
#[cfg(test)]
pub(crate) fn worktree_read_dir_probe_count() -> usize {
    WORKTREE_READ_DIR_PROBE.load(Ordering::Relaxed)
}

/// Returns `core.ignorecase` path resolutions recorded since [`reset_worktree_metadata_probe`].
#[doc(hidden)]
pub fn worktree_path_resolve_probe_count() -> usize {
    WORKTREE_PATH_RESOLVE_PROBE.load(Ordering::Relaxed)
}

/// Counts one filesystem-probing `core.ignorecase` path resolution for `path`.
pub(crate) fn probe_worktree_path_resolution(path: &Path) {
    if probe_counts(path) {
        WORKTREE_PATH_RESOLVE_PROBE.fetch_add(1, Ordering::Relaxed);
    }
}

/// Minimum tracked blob entries before parallel stat preload is considered.
pub const PARALLEL_STAT_MIN_ENTRIES: usize = 1000;

/// Minimum entries per parallel slice so Linux amortizes one `open_stat_directory` handle.
#[cfg(not(windows))]
const PARALLEL_STAT_MIN_SLICE_ENTRIES: usize = 8;

/// Parallelism controls for directory-grouped blob scans (honors `core.preloadIndex`).
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct WorktreeBlobScanOptions {
    /// When false, stat preload never uses more than one thread.
    pub preload_index: bool,
    /// When `Some(1)`, forces a serial scan. When `Some(n > 1)`, caps workers at `n`.
    /// When `None`, uses [`crate::parallel::ThreadPolicy::index_stat_preload_threads`].
    pub stat_parallel_threads: Option<usize>,
}

/// Returns how many threads a blob scan should use for `blob_count` entries.
#[must_use]
pub(crate) fn stat_scan_thread_count(
    policy: &crate::parallel::ThreadPolicy,
    config: &crate::config::ConfigSet,
    options: WorktreeBlobScanOptions,
    blob_count: usize,
) -> usize {
    if !options.preload_index || blob_count < PARALLEL_STAT_MIN_ENTRIES {
        return 1;
    }
    policy.index_stat_preload_threads(config, blob_count, options.stat_parallel_threads)
}

fn probe_worktree_metadata_call(_path: &Path) {
    if probe_counts(_path) {
        WORKTREE_METADATA_PROBE.fetch_add(1, Ordering::Relaxed);
    }
}

fn dir_entry_metadata(entry: &fs::DirEntry) -> io::Result<fs::Metadata> {
    if WORKTREE_PROBE_ACTIVE.load(Ordering::Acquire) != 0 {
        probe_worktree_metadata_call(&entry.path());
    }
    entry.metadata()
}

fn symlink_metadata_path(path: &Path) -> io::Result<fs::Metadata> {
    probe_worktree_metadata_call(path);
    fs::symlink_metadata(path)
}

/// What a worktree path is on disk, without following a final symlink.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AncestorKind {
    /// A real directory.
    Directory,
    /// A symlink (or, on Windows, a reparse point classified as one).
    Symlink,
    /// Missing, unreadable, or not a directory.
    Other,
}

/// Classify `path` with one metadata query, without following a final symlink.
///
/// On Windows, `symlink_metadata` opens a handle per call, which dominates status on large
/// trees. A single attribute query classifies the common case (no reparse point) instead;
/// only reparse points fall back to `symlink_metadata` to classify the reparse tag.
#[must_use]
pub(crate) fn ancestor_kind(path: &Path) -> AncestorKind {
    #[cfg(windows)]
    {
        use std::os::windows::ffi::OsStrExt;
        use windows_sys::Win32::Storage::FileSystem::{
            GetFileAttributesW, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_REPARSE_POINT,
            INVALID_FILE_ATTRIBUTES,
        };
        let mut wide: Vec<u16> = path.as_os_str().encode_wide().collect();
        if !wide.contains(&0) {
            wide.push(0);
            // SAFETY: `wide` is a NUL-terminated UTF-16 path that outlives the call.
            let attrs = unsafe { GetFileAttributesW(wide.as_ptr()) };
            if attrs == INVALID_FILE_ATTRIBUTES {
                return AncestorKind::Other;
            }
            if attrs & FILE_ATTRIBUTE_REPARSE_POINT == 0 {
                return if attrs & FILE_ATTRIBUTE_DIRECTORY != 0 {
                    AncestorKind::Directory
                } else {
                    AncestorKind::Other
                };
            }
        }
    }
    match fs::symlink_metadata(path) {
        Ok(meta) if meta.file_type().is_symlink() => AncestorKind::Symlink,
        Ok(meta) if meta.is_dir() => AncestorKind::Directory,
        _ => AncestorKind::Other,
    }
}

#[cfg(windows)]
fn sparse_stat_policy(sparse: BlobScanSparseMode) -> crate::index::StatMatchPolicy {
    match sparse {
        BlobScanSparseMode::StatusDiff { stat_policy, .. }
        | BlobScanSparseMode::WorktreeGuard { stat_policy, .. }
        | BlobScanSparseMode::StatRefresh { stat_policy, .. } => stat_policy,
        _ => crate::index::StatMatchPolicy::default(),
    }
}

/// When a directory scan stat disagrees with the index but path `symlink_metadata` agrees,
/// prefer the path probe (Git `mingw_stat` / post-close semantics on Windows).
///
/// `target_abs` is only called for that fallback probe: resolving the indexed path can cost
/// its own probe under `core.ignorecase`, which clean entries must not pay (#2167).
#[cfg(windows)]
fn align_lookup_stat_with_index_path_windows(
    index: &Index,
    entry_index: usize,
    lookup: BlobDiskLookup,
    target_abs: impl FnOnce() -> PathBuf,
    stat_policy: crate::index::StatMatchPolicy,
) -> BlobDiskLookup {
    let BlobDiskLookup::Present(stat) = lookup else {
        return lookup;
    };
    let ie = &index.entries[entry_index];
    if crate::diff::stat_matches_blob_stat(ie, &stat, stat_policy) {
        return BlobDiskLookup::Present(stat);
    }
    // NTFS read_dir metadata can disagree with the index on ctime/inode while mtime+size still
    // match; avoid a second path stat in that case (#1622). A disagreement in the sub-second
    // fractions compared under `core.usenanosec` still takes the path probe, or every such
    // entry would be re-hashed.
    if stat.fields.size_and_times_match_index(ie, stat_policy) {
        return BlobDiskLookup::Present(stat);
    }
    match symlink_metadata_path(&target_abs()) {
        Ok(meta) => {
            let path_stat = BlobDiskStat::from_metadata(&meta);
            if crate::diff::stat_matches_blob_stat(ie, &path_stat, stat_policy) {
                BlobDiskLookup::Present(path_stat)
            } else {
                BlobDiskLookup::Present(stat)
            }
        }
        // `read_dir` already found the file; keep the scan stat when the path probe fails.
        Err(_) => BlobDiskLookup::Present(stat),
    }
}

use std::collections::BTreeSet;

use crate::index::{Index, MODE_GITLINK};

/// Directory-grouped blob scan plan: index entry indices grouped by parent directory.
///
/// Built in one pass over the path-sorted index (or a sorted path subset) without a
/// per-directory hash map of owned path strings.
#[derive(Debug, Clone, Default)]
pub(crate) struct BlobScanPlan {
    /// Parent directories in lexicographic order (empty is the repository root).
    pub dirs: Vec<Vec<u8>>,
    /// For each [`Self::dirs`] element, a range into [`Self::indices`].
    pub dir_ranges: Vec<std::ops::Range<usize>>,
    /// [`Index::entries`] indices in visit order (sorted by parent dir, then path).
    pub indices: Vec<usize>,
}

impl BlobScanPlan {
    #[must_use]
    pub fn len(&self) -> usize {
        self.indices.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.indices.is_empty()
    }

    fn push_entry(&mut self, dir: &[u8], entry_index: usize) {
        if self.dirs.last().is_none_or(|d| d.as_slice() != dir) {
            if !self.dirs.is_empty() {
                let end = self.indices.len();
                if let Some(range) = self.dir_ranges.last_mut() {
                    range.end = end;
                }
            }
            self.dirs.push(dir.to_vec());
            self.dir_ranges.push(self.indices.len()..self.indices.len());
        }
        self.indices.push(entry_index);
        if let Some(range) = self.dir_ranges.last_mut() {
            range.end = self.indices.len();
        }
    }

    fn finish(mut self) -> Self {
        if !self.dirs.is_empty() {
            let end = self.indices.len();
            if let Some(range) = self.dir_ranges.last_mut() {
                range.end = end;
            }
        }
        self
    }
}

fn index_blob_scan_eligible(
    ie: &crate::index::IndexEntry,
    index: &Index,
    honor_fsmonitor_valid: bool,
) -> bool {
    if ie.stage() != 0 {
        return false;
    }
    if ie.skip_worktree() || ie.assume_unchanged() || ie.intent_to_add() {
        return false;
    }
    if ie.mode == MODE_GITLINK {
        return false;
    }
    if honor_fsmonitor_valid && index.fsmonitor_last_update.is_some() && ie.fsmonitor_valid() {
        return false;
    }
    true
}

fn dir_entry_key(entry: &fs::DirEntry) -> Vec<u8> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStringExt;
        entry.file_name().into_vec()
    }
    #[cfg(not(unix))]
    {
        entry
            .file_name()
            .to_string_lossy()
            .into_owned()
            .into_bytes()
    }
}

fn entry_rel_path(index: &Index, entry_index: usize) -> Result<&[u8]> {
    index
        .entries
        .get(entry_index)
        .map(|ie| ie.path.as_slice())
        .ok_or_else(|| Error::Message(format!("index entry {entry_index} out of range")))
}

fn entry_file_name(index: &Index, entry_index: usize) -> Result<&[u8]> {
    let rel = entry_rel_path(index, entry_index)?;
    Ok(crate::repo_relpath::file_name(rel))
}

/// Borrowed form of [`crate::repo_relpath::parent_dir`].
fn parent_dir_bytes(path: &[u8]) -> &[u8] {
    match path.iter().rposition(|&b| b == b'/') {
        Some(pos) => &path[..pos],
        None => &[],
    }
}

/// Group stage-0 blob index entries by parent directory for bulk `read_dir`.
#[must_use]
pub(crate) fn plan_blob_entries_by_dir(index: &Index, honor_fsmonitor_valid: bool) -> BlobScanPlan {
    let mut plan = BlobScanPlan {
        dirs: Vec::new(),
        dir_ranges: Vec::new(),
        indices: Vec::new(),
    };
    for (entry_index, ie) in index.entries.iter().enumerate() {
        if !index_blob_scan_eligible(ie, index, honor_fsmonitor_valid) {
            continue;
        }
        plan.push_entry(parent_dir_bytes(&ie.path), entry_index);
    }
    plan.finish()
}

/// Like [`plan_blob_entries_by_dir`], but only for the given index entry positions.
#[must_use]
pub(crate) fn plan_blob_entries_by_dir_for_positions(
    index: &Index,
    positions: &[usize],
    honor_fsmonitor_valid: bool,
) -> BlobScanPlan {
    let mut plan = BlobScanPlan {
        dirs: Vec::new(),
        dir_ranges: Vec::new(),
        indices: Vec::new(),
    };
    for &entry_index in positions {
        let Some(ie) = index.entries.get(entry_index) else {
            continue;
        };
        if !index_blob_scan_eligible(ie, index, honor_fsmonitor_valid) {
            continue;
        }
        plan.push_entry(parent_dir_bytes(&ie.path), entry_index);
    }
    plan.finish()
}

/// Like [`plan_blob_entries_by_dir`], but only for repository-relative paths in `paths`.
#[must_use]
pub(crate) fn plan_blob_entries_by_dir_for_paths(
    index: &Index,
    paths: &BTreeSet<String>,
) -> BlobScanPlan {
    let mut plan = BlobScanPlan {
        dirs: Vec::new(),
        dir_ranges: Vec::new(),
        indices: Vec::new(),
    };
    for path in paths {
        let path_bytes = crate::repo_relpath::storage_bytes(path);
        let Some(ie) = index.get(path_bytes.as_ref(), 0) else {
            continue;
        };
        if !index_blob_scan_eligible(ie, index, false) {
            continue;
        }
        let path_bytes_ref = path_bytes.as_ref();
        let Ok(entry_index) = index.entries.binary_search_by(|e| {
            e.path
                .as_slice()
                .cmp(path_bytes_ref)
                .then_with(|| e.stage().cmp(&0))
        }) else {
            continue;
        };
        plan.push_entry(parent_dir_bytes(path_bytes_ref), entry_index);
    }
    plan.finish()
}

/// Group stage-0 blob index entries by parent directory for bulk `read_dir`.
#[must_use]
pub(crate) fn group_blob_entries_by_dir(
    index: &Index,
    honor_fsmonitor_valid: bool,
) -> BlobScanPlan {
    plan_blob_entries_by_dir(index, honor_fsmonitor_valid)
}

/// Like [`group_blob_entries_by_dir`], but only for repository-relative paths in `paths`.
#[must_use]
pub(crate) fn group_blob_entries_by_dir_for_paths(
    index: &Index,
    paths: &BTreeSet<String>,
) -> BlobScanPlan {
    plan_blob_entries_by_dir_for_paths(index, paths)
}

/// Compact worktree stat captured during a directory-grouped blob scan.
///
/// Holds the index-relevant stat tuple without retaining full [`fs::Metadata`] for every
/// tracked path (large status/diff scans).
#[derive(Debug, Clone, Copy)]
pub(crate) struct BlobDiskStat {
    pub(crate) fields: crate::index::IndexStatFields,
    pub(crate) git_mode: u32,
    pub(crate) is_dir: bool,
    /// Worktree byte length before index munging (`st_size` / `metadata.len()`).
    pub(crate) raw_size: u64,
}

impl BlobDiskStat {
    #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
    fn from_linux_stat(stat: &libc::stat) -> Self {
        let kind = stat.st_mode & libc::S_IFMT;
        Self {
            fields: crate::index::IndexStatFields::from_linux_stat(stat),
            git_mode: if kind == libc::S_IFLNK {
                crate::index::MODE_SYMLINK
            } else if stat.st_mode & 0o111 != 0 {
                crate::index::MODE_EXECUTABLE
            } else {
                crate::index::MODE_REGULAR
            },
            is_dir: kind == libc::S_IFDIR,
            raw_size: stat.st_size as u64,
        }
    }

    #[must_use]
    pub(crate) fn from_metadata(meta: &fs::Metadata) -> Self {
        Self {
            fields: crate::index::IndexStatFields::from_metadata(meta),
            git_mode: crate::diff::mode_from_metadata(meta),
            is_dir: meta.is_dir() && !meta.file_type().is_symlink(),
            raw_size: meta.len(),
        }
    }

    #[must_use]
    pub(crate) fn worktree_size(&self) -> u32 {
        self.fields.index_size()
    }
}

/// Result of looking up one indexed blob on disk during a directory scan.
pub(crate) enum BlobDiskLookup {
    /// Stat fields for the indexed path.
    Present(BlobDiskStat),
    /// Missing path or non-directory parent (`NotFound` / `ENOTDIR`).
    Missing,
    /// Other I/O error.
    Io(io::Error),
}

/// Controls whether a scanned lookup is kept in parallel preload batches.
///
/// Dense mode retains every entry (tests and callers that visit all paths). Sparse modes
/// keep only missing paths, errors, and entries that still need hash, refresh, or staging work.
#[derive(Debug, Clone, Copy)]
pub(crate) enum BlobScanSparseMode {
    Dense,
    StatusDiff {
        stat_policy: crate::index::StatMatchPolicy,
        index_mtime: Option<(u32, u32)>,
        mode_policy: crate::index::WorktreeModePolicy,
        for_status: bool,
        refresh_index_stat_in_pass: bool,
        parallel_blob_work: bool,
        honor_fsmonitor_stat_skip: bool,
    },
    StageTracked {
        index_mtime: Option<(u32, u32)>,
        mode_policy: crate::index::WorktreeModePolicy,
    },
    WorktreeGuard {
        stat_policy: crate::index::StatMatchPolicy,
        index_mtime: Option<(u32, u32)>,
    },
    StatRefresh {
        stat_policy: crate::index::StatMatchPolicy,
        index_mtime: Option<(u32, u32)>,
    },
}

impl BlobScanSparseMode {
    fn retain_lookup(self, index: &Index, entry_index: usize, lookup: &BlobDiskLookup) -> bool {
        match self {
            Self::Dense => true,
            Self::StatusDiff {
                stat_policy,
                index_mtime,
                mode_policy,
                for_status,
                refresh_index_stat_in_pass,
                parallel_blob_work,
                honor_fsmonitor_stat_skip,
            } => retain_status_diff_lookup(
                index,
                entry_index,
                lookup,
                stat_policy,
                index_mtime,
                mode_policy,
                for_status,
                refresh_index_stat_in_pass,
                parallel_blob_work,
                honor_fsmonitor_stat_skip,
            ),
            Self::StageTracked {
                index_mtime,
                mode_policy,
            } => retain_stage_tracked_lookup(index, entry_index, lookup, index_mtime, mode_policy),
            Self::WorktreeGuard {
                stat_policy,
                index_mtime,
            } => retain_worktree_guard_lookup(index, entry_index, lookup, stat_policy, index_mtime),
            Self::StatRefresh {
                stat_policy,
                index_mtime,
            } => retain_stat_refresh_lookup(index, entry_index, lookup, stat_policy, index_mtime),
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn retain_status_diff_lookup(
    index: &Index,
    entry_index: usize,
    lookup: &BlobDiskLookup,
    stat_policy: crate::index::StatMatchPolicy,
    index_mtime: Option<(u32, u32)>,
    mode_policy: crate::index::WorktreeModePolicy,
    for_status: bool,
    refresh_index_stat_in_pass: bool,
    parallel_blob_work: bool,
    honor_fsmonitor_stat_skip: bool,
) -> bool {
    let ie = &index.entries[entry_index];
    match lookup {
        BlobDiskLookup::Missing | BlobDiskLookup::Io(_) => true,
        BlobDiskLookup::Present(stat) => {
            if honor_fsmonitor_stat_skip && crate::fsmonitor::entry_skips_worktree_stat(ie, index) {
                return false;
            }
            if refresh_index_stat_in_pass
                && crate::diff::worktree_diff_refreshes_entry(
                    ie,
                    stat,
                    index_mtime,
                    stat_policy,
                    for_status,
                    // Attributes are not resolved during the scan; a changed size is kept
                    // either way, so assume a conversion and let the visit pass decide.
                    || false,
                )
            {
                return true;
            }
            if parallel_blob_work {
                // Keep everything the visit pass must hash or report; drop only entries whose
                // stat data proves them clean (the same test the visit pass applies).
                return !(crate::diff::stat_matches_blob_stat(ie, stat, stat_policy)
                    && mode_policy.worktree_mode_matches(stat.git_mode, ie.mode)
                    && crate::diff::index_entry_skips_worktree_content_hash_after_stat_match(
                        ie,
                        index_mtime,
                        stat_policy,
                    ));
            }
            true
        }
    }
}

fn retain_stage_tracked_lookup(
    index: &Index,
    entry_index: usize,
    lookup: &BlobDiskLookup,
    index_mtime: Option<(u32, u32)>,
    mode_policy: crate::index::WorktreeModePolicy,
) -> bool {
    match lookup {
        BlobDiskLookup::Missing | BlobDiskLookup::Io(_) => true,
        BlobDiskLookup::Present(stat) => {
            let ie = &index.entries[entry_index];
            if ie.mode == crate::index::MODE_GITLINK {
                return true;
            }
            let stat_same = stat
                .fields
                .matches_index_entry(ie, crate::index::StatMatchPolicy::default());
            let racy = crate::diff::entry_is_racy(ie, index_mtime);
            if !ie.intent_to_add()
                && ie.size != 0
                && stat_same
                && !racy
                && mode_policy.worktree_mode_matches(stat.git_mode, ie.mode)
            {
                return false;
            }
            true
        }
    }
}

fn retain_worktree_guard_lookup(
    index: &Index,
    entry_index: usize,
    lookup: &BlobDiskLookup,
    stat_policy: crate::index::StatMatchPolicy,
    index_mtime: Option<(u32, u32)>,
) -> bool {
    let ie = &index.entries[entry_index];
    if ie.stage() != 0 || ie.intent_to_add() || ie.skip_worktree() {
        return false;
    }
    match lookup {
        BlobDiskLookup::Missing => true,
        BlobDiskLookup::Io(_) => true,
        BlobDiskLookup::Present(stat) => {
            if ie.mode == crate::index::MODE_GITLINK {
                return !stat.is_dir;
            }
            if stat.is_dir
                || stat.git_mode != crate::index::MODE_SYMLINK
                    && stat.git_mode != crate::index::MODE_REGULAR
                    && stat.git_mode != crate::index::MODE_EXECUTABLE
            {
                return true;
            }
            // Racily clean entries are kept too: the guard must compare their content.
            !crate::diff::stat_matches_blob_stat(ie, stat, stat_policy)
                || crate::diff::entry_is_racy_with_policy(ie, index_mtime, stat_policy)
        }
    }
}

fn retain_stat_refresh_lookup(
    index: &Index,
    entry_index: usize,
    lookup: &BlobDiskLookup,
    stat_policy: crate::index::StatMatchPolicy,
    index_mtime: Option<(u32, u32)>,
) -> bool {
    let ie = &index.entries[entry_index];
    if ie.stage() != 0 || ie.skip_worktree() || ie.assume_unchanged() || ie.intent_to_add() {
        return false;
    }
    match lookup {
        BlobDiskLookup::Missing | BlobDiskLookup::Io(_) => false,
        BlobDiskLookup::Present(stat) => {
            !crate::diff::stat_matches_blob_stat(ie, stat, stat_policy)
                || crate::diff::entry_is_racy_with_policy(ie, index_mtime, stat_policy)
        }
    }
}

struct BlobLookupCollectSink<'a> {
    out: &'a mut Vec<(usize, BlobDiskLookup)>,
    sparse: BlobScanSparseMode,
    scanned: &'a mut usize,
}

impl BlobLookupCollectSink<'_> {
    fn push(&mut self, index: &Index, entry_index: usize, lookup: BlobDiskLookup) -> Result<()> {
        *self.scanned += 1;
        if self.sparse.retain_lookup(index, entry_index, &lookup) {
            self.out.push((entry_index, lookup));
        }
        Ok(())
    }
}

#[allow(clippy::too_many_arguments)]
fn collect_blob_lookups_in_directory(
    index: &Index,
    dir: &[u8],
    entry_indices: &[usize],
    dir_abs: &dyn Fn(&[u8]) -> PathBuf,
    file_abs: &dyn Fn(&[u8]) -> PathBuf,
    literal_paths: bool,
    under_symlink: bool,
    out: &mut Vec<(usize, BlobDiskLookup)>,
    sparse: BlobScanSparseMode,
    scanned: &mut usize,
    boundary: StatBoundaryPoll<'_>,
    cancel_stride: &mut usize,
) -> Result<()> {
    let mut sink = BlobLookupCollectSink {
        out,
        sparse,
        scanned,
    };
    for_each_blob_in_one_directory(
        index,
        dir,
        entry_indices,
        dir_abs,
        file_abs,
        literal_paths,
        under_symlink,
        boundary,
        cancel_stride,
        &mut |entry_index, rel_path, lookup| {
            #[cfg(not(windows))]
            let _ = rel_path;
            #[cfg(windows)]
            let lookup = align_lookup_stat_with_index_path_windows(
                index,
                entry_index,
                lookup,
                || file_abs(rel_path),
                sparse_stat_policy(sparse),
            );
            sink.push(index, entry_index, lookup)
        },
    )?;
    Ok(())
}

/// Whether the directory holding `entry_indices` sits under a symlinked ancestor.
///
/// Every entry in one plan directory shares the same ancestors, so one sample path decides.
fn dir_has_symlink_ancestor(
    index: &Index,
    entry_indices: &[usize],
    has_symlink_ancestor: &mut dyn FnMut(&[u8]) -> bool,
) -> bool {
    entry_indices
        .first()
        .and_then(|&i| entry_rel_path(index, i).ok())
        .is_some_and(has_symlink_ancestor)
}

/// `under_symlink` reports every entry as [`BlobDiskLookup::Missing`] without I/O: a symlink
/// replaced a directory on the path, so the indexed files are gone even if the link target
/// holds files with the same names.
#[allow(clippy::too_many_arguments)]
fn for_each_blob_in_one_directory<F>(
    index: &Index,
    dir: &[u8],
    entry_indices: &[usize],
    dir_abs: &dyn Fn(&[u8]) -> PathBuf,
    file_abs: &dyn Fn(&[u8]) -> PathBuf,
    literal_paths: bool,
    under_symlink: bool,
    boundary: StatBoundaryPoll<'_>,
    cancel_stride: &mut usize,
    visit: &mut F,
) -> Result<()>
where
    F: FnMut(usize, &[u8], BlobDiskLookup) -> Result<()>,
{
    if under_symlink {
        for &entry_index in entry_indices {
            boundary.poll_stride(cancel_stride)?;
            let rel_path = entry_rel_path(index, entry_index)?;
            visit(entry_index, rel_path, BlobDiskLookup::Missing)?;
        }
        return Ok(());
    }

    #[cfg(not(all(target_os = "linux", target_pointer_width = "64")))]
    let _ = literal_paths;
    #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
    // Amortize opening and closing a parent across several file lookups.
    if literal_paths && entry_indices.len() >= 8 {
        let abs_dir = dir_abs(dir);
        if let Ok(handle) = open_stat_directory(&abs_dir) {
            return for_each_blob_at_directory(
                index,
                entry_indices,
                &handle,
                &abs_dir,
                boundary,
                cancel_stride,
                visit,
            );
        }
    }

    if !LIST_PARENT_DIRECTORIES {
        // `dir_abs` already resolved the parent's case/Unicode spelling, so the indexed name
        // under it is the path `file_abs` would pick whenever it exists. Only a miss pays for
        // `file_abs`, which may probe and list the directory to find another spelling.
        let abs_dir = dir_abs(dir);
        for &entry_index in entry_indices {
            boundary.poll_stride(cancel_stride)?;
            let rel_path = entry_rel_path(index, entry_index)?;
            let file_name = entry_file_name(index, entry_index)?;
            let direct = crate::index::worktree_path_from_index_rel(&abs_dir, file_name);
            let lookup = match symlink_metadata_blob_lookup(&direct) {
                BlobDiskLookup::Missing => {
                    let target = file_abs(rel_path);
                    if target == direct {
                        BlobDiskLookup::Missing
                    } else {
                        symlink_metadata_blob_lookup(&target)
                    }
                }
                lookup => lookup,
            };
            if let BlobDiskLookup::Io(e) = lookup {
                return Err(Error::Io(e));
            }
            visit(entry_index, rel_path, lookup)?;
        }
        return Ok(());
    }

    let abs_dir = dir_abs(dir);
    #[cfg(windows)]
    if entry_indices.len() == 1 {
        boundary.poll_stride(cancel_stride)?;
        let entry_index = entry_indices[0];
        let rel_path = entry_rel_path(index, entry_index)?;
        let file_name = entry_file_name(index, entry_index)?;
        let lookup = lookup_lone_blob_in_directory(&abs_dir, file_name, || file_abs(rel_path));
        visit(entry_index, rel_path, lookup)?;
        return Ok(());
    }

    let (dir_readable, on_disk) = read_directory_map(&abs_dir, entry_indices.len())?;
    for &entry_index in entry_indices {
        boundary.poll_stride(cancel_stride)?;
        let rel_path = entry_rel_path(index, entry_index)?;
        let file_name = entry_file_name(index, entry_index)?;
        let lookup = if !dir_readable {
            BlobDiskLookup::Missing
        } else {
            lookup_blob_in_directory(&on_disk, file_name, || file_abs(rel_path))
        };
        visit(entry_index, rel_path, lookup)?;
    }
    Ok(())
}

/// O_PATH permits metadata lookups in searchable directories without requiring read access.
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
fn open_stat_directory(path: &Path) -> io::Result<fs::File> {
    use std::os::unix::fs::OpenOptionsExt;
    fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_PATH | libc::O_DIRECTORY | libc::O_NOFOLLOW)
        .open(path)
}

/// Confirms in one path walk that every component of a work-tree-relative directory is a
/// real directory, using `openat2` with `RESOLVE_NO_SYMLINKS` from a handle on the work tree.
///
/// Probing each ancestor with its own `lstat` walks the path from the root every time, which
/// is quadratic in depth. This answers the common case (no symlink, nothing missing) with one
/// walk; any other outcome is left to a per-component probe.
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub(crate) struct RealDirChain {
    work_tree: PathBuf,
    /// `None` when the work tree cannot be opened or the kernel lacks `openat2`.
    root: Option<fs::File>,
    /// Reused NUL-terminated copy of the queried path.
    c_path: Vec<u8>,
}

#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
impl RealDirChain {
    /// Open a handle on `work_tree` (following symlinks in the work tree's own path).
    pub(crate) fn open(work_tree: &Path) -> Self {
        use std::os::unix::fs::OpenOptionsExt;
        let root = fs::OpenOptions::new()
            .read(true)
            .custom_flags(libc::O_PATH | libc::O_DIRECTORY)
            .open(work_tree)
            .ok();
        Self {
            work_tree: work_tree.to_path_buf(),
            root,
            c_path: Vec::new(),
        }
    }

    /// The work tree this handle was opened on.
    pub(crate) fn work_tree(&self) -> &Path {
        &self.work_tree
    }

    /// Whether every component of `rel_dir` (raw index bytes) is known to be a real directory.
    ///
    /// `false` means "not confirmed": a component is a symlink, missing or not a directory, or
    /// the check is unavailable. Callers then probe component by component.
    pub(crate) fn confirms(&mut self, rel_dir: &[u8]) -> bool {
        use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
        let Some(root) = &self.root else {
            return false;
        };
        if rel_dir.is_empty() || rel_dir.contains(&0) {
            return false;
        }
        self.c_path.clear();
        self.c_path.extend_from_slice(rel_dir);
        self.c_path.push(0);
        // SAFETY: `open_how` is plain integers, for which all-zero is valid (and means "no
        // flags, no mode, no resolve restrictions" to the kernel).
        let mut how: libc::open_how = unsafe { std::mem::zeroed() };
        how.flags = (libc::O_PATH | libc::O_DIRECTORY | libc::O_CLOEXEC) as u64;
        how.resolve = libc::RESOLVE_NO_SYMLINKS;
        // SAFETY: `root` is an open descriptor, `c_path` is NUL-terminated and outlives the
        // call, and `how` is a fully initialized `open_how` whose size is passed alongside.
        let fd = unsafe {
            libc::syscall(
                libc::SYS_openat2,
                root.as_raw_fd(),
                self.c_path.as_ptr(),
                &how as *const libc::open_how,
                std::mem::size_of::<libc::open_how>(),
            )
        };
        if fd >= 0 {
            let Ok(fd) = i32::try_from(fd) else {
                return false;
            };
            // SAFETY: the kernel returned a new descriptor that nothing else owns.
            drop(unsafe { OwnedFd::from_raw_fd(fd) });
            return true;
        }
        let errno = io::Error::last_os_error().raw_os_error();
        if matches!(errno, Some(libc::ENOSYS | libc::EPERM | libc::E2BIG)) {
            self.root = None;
        }
        false
    }
}

/// Resolve each file relative to an already opened parent, without following file symlinks.
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
fn for_each_blob_at_directory<F>(
    index: &Index,
    entry_indices: &[usize],
    directory: &fs::File,
    abs_dir: &Path,
    boundary: StatBoundaryPoll<'_>,
    cancel_stride: &mut usize,
    visit: &mut F,
) -> Result<()>
where
    F: FnMut(usize, &[u8], BlobDiskLookup) -> Result<()>,
{
    use std::os::fd::AsRawFd;
    let count_probes = probe_counts(abs_dir);
    let mut name = Vec::new();
    for &entry_index in entry_indices {
        boundary.poll_stride(cancel_stride)?;
        let rel = entry_rel_path(index, entry_index)?;
        let file_name = entry_file_name(index, entry_index)?;
        if file_name.contains(&0) {
            return Err(Error::Io(io::Error::new(
                io::ErrorKind::InvalidInput,
                "file name contains NUL",
            )));
        }
        name.clear();
        name.reserve(file_name.len() + 1);
        name.extend_from_slice(file_name);
        name.push(0);
        let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
        if count_probes {
            WORKTREE_METADATA_PROBE.fetch_add(1, Ordering::Relaxed);
        }
        // SAFETY: directory owns a valid fd; name is NUL-terminated and stat points to
        // writable storage. AT_SYMLINK_NOFOLLOW preserves lstat semantics for the file.
        let result = unsafe {
            libc::fstatat(
                directory.as_raw_fd(),
                name.as_ptr().cast(),
                stat.as_mut_ptr(),
                libc::AT_SYMLINK_NOFOLLOW,
            )
        };
        let lookup = if result == 0 {
            // SAFETY: a successful fstatat initialized the entire stat structure.
            let stat = unsafe { stat.assume_init() };
            BlobDiskLookup::Present(BlobDiskStat::from_linux_stat(&stat))
        } else {
            let error = io::Error::last_os_error();
            match error.kind() {
                io::ErrorKind::NotFound | io::ErrorKind::NotADirectory => BlobDiskLookup::Missing,
                _ => return Err(Error::Io(error)),
            }
        };
        visit(entry_index, rel, lookup)?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn collect_blob_lookups_serial(
    index: &Index,
    plan: &BlobScanPlan,
    has_symlink_ancestor: &mut dyn FnMut(&[u8]) -> bool,
    dir_abs: &dyn Fn(&[u8]) -> PathBuf,
    file_abs: &dyn Fn(&[u8]) -> PathBuf,
    literal_paths: bool,
    sparse: BlobScanSparseMode,
    cancel: &StatScanCancel,
) -> Result<(Vec<(usize, BlobDiskLookup)>, usize)> {
    let mut out = match sparse {
        BlobScanSparseMode::Dense => Vec::with_capacity(plan.len()),
        _ => Vec::new(),
    };
    let mut scanned = 0usize;
    let mut cancel_stride = 0usize;
    let boundary = StatBoundaryPoll::Owner(cancel);
    for (dir_idx, dir) in plan.dirs.iter().enumerate() {
        boundary.poll_directory()?;
        let Some(range) = plan.dir_ranges.get(dir_idx) else {
            continue;
        };
        let entry_indices = &plan.indices[range.clone()];
        let under_symlink = dir_has_symlink_ancestor(index, entry_indices, has_symlink_ancestor);
        collect_blob_lookups_in_directory(
            index,
            dir,
            entry_indices,
            dir_abs,
            file_abs,
            literal_paths,
            under_symlink,
            &mut out,
            sparse,
            &mut scanned,
            boundary,
            &mut cancel_stride,
        )?;
    }
    Ok((out, scanned))
}

/// Apply a visit callback to lookup batches produced by [`collect_blob_lookup_batches`].
pub(crate) fn visit_blob_lookup_batches<F>(
    batches: Vec<Vec<(usize, BlobDiskLookup)>>,
    mut visit: F,
) -> Result<()>
where
    F: FnMut(usize, BlobDiskLookup) -> Result<()>,
{
    for batch in batches {
        for (entry_index, lookup) in batch {
            visit(entry_index, lookup)?;
        }
    }
    Ok(())
}

/// One indexed file under `abs_dir`: a single `read_dir` without building a map (Windows leaf dirs).
#[cfg(windows)]
fn lookup_lone_blob_in_directory(
    abs_dir: &Path,
    index_file_name: &[u8],
    target_abs: impl FnOnce() -> PathBuf,
) -> BlobDiskLookup {
    if probe_counts(abs_dir) {
        WORKTREE_READ_DIR_PROBE.fetch_add(1, Ordering::Relaxed);
    }
    let rd = match fs::read_dir(abs_dir) {
        Ok(rd) => rd,
        Err(e)
            if e.kind() == io::ErrorKind::NotFound
                || e.kind() == io::ErrorKind::PermissionDenied
                || e.kind() == io::ErrorKind::NotADirectory =>
        {
            return BlobDiskLookup::Missing;
        }
        Err(e) => return BlobDiskLookup::Io(e),
    };
    for item in rd {
        let Ok(entry) = item else { continue };
        let name = entry.file_name();
        if name == ".git" {
            continue;
        }
        if dir_entry_name_matches(index_file_name, &entry) {
            return blob_lookup_from_dir_entry_or_path(Some(&entry), target_abs);
        }
    }
    symlink_metadata_blob_lookup(&target_abs())
}

#[cfg(not(windows))]
fn dir_range_for_index_pos(plan: &BlobScanPlan, index_pos: usize) -> std::ops::Range<usize> {
    for range in &plan.dir_ranges {
        if range.contains(&index_pos) {
            return range.clone();
        }
    }
    index_pos..index_pos.saturating_add(1)
}

fn first_dir_index_at_or_after(plan: &BlobScanPlan, index_pos: usize) -> usize {
    plan.dir_ranges
        .partition_point(|range| range.end <= index_pos)
        .min(plan.dirs.len().saturating_sub(1))
}

/// Move an index cut off a small directory interior onto the nearest directory boundary.
#[cfg(not(windows))]
fn snap_index_parallel_cut(plan: &BlobScanPlan, cut: usize, fair_share: usize) -> usize {
    if cut == 0 || cut >= plan.len() {
        return cut;
    }
    let dir_range = dir_range_for_index_pos(plan, cut - 1);
    if cut <= dir_range.start || cut >= dir_range.end {
        return cut;
    }
    let dir_len = dir_range.end - dir_range.start;
    if dir_len > fair_share {
        return cut;
    }
    if cut - dir_range.start <= dir_range.end - cut {
        dir_range.start
    } else {
        dir_range.end
    }
}

#[cfg(windows)]
fn partition_blob_lookup_slices(
    plan: &BlobScanPlan,
    threads: usize,
) -> Vec<std::ops::Range<usize>> {
    let num_dirs = plan.dirs.len();
    if num_dirs == 0 {
        return Vec::new();
    }
    if threads <= 1 {
        let n = plan.len();
        return if n == 0 {
            Vec::new()
        } else {
            std::iter::once(0..n).collect()
        };
    }
    let chunk_size = num_dirs.div_ceil(threads);
    let num_chunks = num_dirs.div_ceil(chunk_size);
    (0..num_chunks)
        .map(|chunk_idx| {
            let chunk_start = chunk_idx * chunk_size;
            let chunk_end = (chunk_start + chunk_size).min(num_dirs);
            let start = plan.dir_ranges[chunk_start].start;
            let end = plan.dir_ranges[chunk_end - 1].end;
            start..end
        })
        .collect()
}

#[cfg(not(windows))]
fn partition_blob_lookup_slices(
    plan: &BlobScanPlan,
    threads: usize,
) -> Vec<std::ops::Range<usize>> {
    let n = plan.len();
    if n == 0 {
        return Vec::new();
    }
    if threads <= 1 {
        return if n == 0 {
            Vec::new()
        } else {
            std::iter::once(0..n).collect()
        };
    }
    let fair_share = n.div_ceil(threads);
    let mut cuts = vec![0usize];
    for worker in 1..threads {
        let mut cut = worker * n / threads;
        cut = snap_index_parallel_cut(plan, cut, fair_share);
        let prev = cuts[cuts.len() - 1];
        let min_step = PARALLEL_STAT_MIN_SLICE_ENTRIES.min(n.saturating_sub(prev));
        if min_step == 0 {
            break;
        }
        cut = cut.max(prev + min_step).min(n);
        if cut <= prev {
            break;
        }
        cuts.push(cut);
    }
    if cuts.last().copied() != Some(n) {
        cuts.push(n);
    }
    cuts.windows(2)
        .map(|pair| pair[0]..pair[1])
        .filter(|range| !range.is_empty())
        .collect()
}

#[allow(clippy::too_many_arguments)]
fn collect_blob_lookups_for_index_slice<S, P>(
    index: &Index,
    plan: &BlobScanPlan,
    slice: std::ops::Range<usize>,
    symlink_probe: &S,
    dir_abs: &DirAbsFn,
    file_abs: &DirAbsFn,
    literal_paths: bool,
    sparse: BlobScanSparseMode,
    local: &mut Vec<(usize, BlobDiskLookup)>,
    scanned: &mut usize,
    worker_watch: Option<&Arc<AtomicBool>>,
    cancel_stride: &mut usize,
) -> Result<()>
where
    S: Fn() -> P,
    P: FnMut(&[u8]) -> bool,
{
    if slice.is_empty() {
        return Ok(());
    }
    let boundary = StatBoundaryPoll::Worker(worker_watch);
    let mut has_symlink_ancestor = symlink_probe();
    let mut dir_idx = first_dir_index_at_or_after(plan, slice.start);
    while dir_idx < plan.dirs.len() {
        boundary.poll_directory()?;
        let Some(dir_range) = plan.dir_ranges.get(dir_idx) else {
            dir_idx += 1;
            continue;
        };
        if dir_range.start >= slice.end {
            break;
        }
        let overlap_start = dir_range.start.max(slice.start);
        let overlap_end = dir_range.end.min(slice.end);
        if overlap_start < overlap_end {
            let dir = &plan.dirs[dir_idx];
            let entry_indices = &plan.indices[overlap_start..overlap_end];
            let under_symlink =
                dir_has_symlink_ancestor(index, entry_indices, &mut has_symlink_ancestor);
            collect_blob_lookups_in_directory(
                index,
                dir,
                entry_indices,
                dir_abs,
                file_abs,
                literal_paths,
                under_symlink,
                local,
                sparse,
                scanned,
                boundary,
                cancel_stride,
            )?;
        }
        dir_idx += 1;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn preload_blob_lookups_parallel<S, P>(
    index: &Index,
    plan: &BlobScanPlan,
    threads: usize,
    symlink_probe: &S,
    dir_abs: Arc<DirAbsFn>,
    file_abs: Arc<DirAbsFn>,
    literal_paths: bool,
    sparse: BlobScanSparseMode,
    cancel: StatScanCancel,
) -> Result<Vec<Vec<(usize, BlobDiskLookup)>>>
where
    S: Fn() -> P + Sync,
    P: FnMut(&[u8]) -> bool,
{
    let blob_count = plan.len();
    if plan.dirs.is_empty() {
        return Ok(Vec::new());
    }
    let slices = partition_blob_lookup_slices(plan, threads);
    let worker_count = slices.len();
    let mut batches = Vec::with_capacity(worker_count);
    let (done_tx, done_rx) = mpsc::channel();
    std::thread::scope(|scope| {
        let worker_watch = cancel.watch.clone();
        for slice in slices {
            let dir_abs = Arc::clone(&dir_abs);
            let file_abs = Arc::clone(&file_abs);
            let worker_watch = worker_watch.clone();
            let done_tx = done_tx.clone();
            scope.spawn(move || {
                let result = (|| {
                    let mut local = Vec::new();
                    let mut scanned = 0usize;
                    let mut cancel_stride = 0usize;
                    collect_blob_lookups_for_index_slice(
                        index,
                        plan,
                        slice,
                        symlink_probe,
                        dir_abs.as_ref(),
                        file_abs.as_ref(),
                        literal_paths,
                        sparse,
                        &mut local,
                        &mut scanned,
                        worker_watch.as_ref(),
                        &mut cancel_stride,
                    )?;
                    Ok((local, scanned))
                })();
                let _ = done_tx.send(result);
            });
        }
        drop(done_tx);

        let mut scanned_total = 0usize;
        while batches.len() < worker_count {
            match done_rx.recv_timeout(Duration::from_millis(5)) {
                Ok(Ok((local, scanned))) => {
                    scanned_total += scanned;
                    batches.push(local);
                }
                Ok(Err(e)) => return Err(e),
                Err(RecvTimeoutError::Timeout) => cancel.poll()?,
                Err(RecvTimeoutError::Disconnected) => {
                    if batches.len() < worker_count {
                        return Err(Error::Message(
                            "parallel blob stat worker exited before reporting".to_owned(),
                        ));
                    }
                }
            }
        }
        if scanned_total != blob_count {
            return Err(Error::Message(format!(
                "parallel blob stat preload expected {blob_count} scanned entries, got {scanned_total}"
            )));
        }
        Ok(())
    })?;
    Ok(batches)
}

/// Visit grouped blob entries with one `read_dir` per distinct parent directory.
///
/// When `has_symlink_ancestor(rel_path)` is true, the entry is reported as [`BlobDiskLookup::Missing`]
/// without extra I/O (symlink replaced a directory on the path).
pub(crate) fn for_each_blob_by_directory<F, H>(
    index: &Index,
    plan: &BlobScanPlan,
    dir_abs: impl Fn(&[u8]) -> PathBuf,
    file_abs: impl Fn(&[u8]) -> PathBuf,
    mut has_symlink_ancestor: H,
    visit: F,
) -> Result<()>
where
    F: FnMut(usize, BlobDiskLookup) -> Result<()>,
    H: FnMut(&[u8]) -> bool,
{
    let empty_cancel = empty_stat_cancel();
    let (batch, scanned) = collect_blob_lookups_serial(
        index,
        plan,
        &mut has_symlink_ancestor,
        &dir_abs,
        &file_abs,
        false,
        BlobScanSparseMode::Dense,
        &empty_cancel,
    )?;
    if scanned != plan.len() {
        return Err(Error::Message(format!(
            "blob scan expected {} entries, scanned {scanned}",
            plan.len()
        )));
    }
    visit_blob_lookup_batches(vec![batch], visit)
}

fn empty_stat_cancel() -> StatScanCancel {
    StatScanCancel {
        watch: None,
        owner_poll: None,
    }
}

/// Collect disk lookups for every entry in `plan`, optionally using parallel preload.
///
/// Returns one batch per parallel worker (or a single batch when serial). Callers should
/// consume batches with [`visit_blob_lookup_batches`] so index mutations are not blocked
/// by an active immutable borrow on [`Index`].
///
/// `symlink_probe()` runs once per scan worker and returns that worker's
/// `has_symlink_ancestor(rel_path)` check, which is then asked once per plan directory in
/// index order. Workers own their probe, so it may keep state without locking; pass
/// [`no_symlink_probe`] to skip the check.
#[allow(clippy::too_many_arguments)]
pub(crate) fn collect_blob_lookup_batches<S, P>(
    index: &Index,
    plan: &BlobScanPlan,
    scan_options: WorktreeBlobScanOptions,
    thread_policy: &crate::parallel::ThreadPolicy,
    config: &crate::config::ConfigSet,
    dir_abs: impl Fn(&[u8]) -> PathBuf + Send + Sync + 'static,
    file_abs: impl Fn(&[u8]) -> PathBuf + Send + Sync + 'static,
    symlink_probe: S,
    sparse: BlobScanSparseMode,
) -> Result<Vec<Vec<(usize, BlobDiskLookup)>>>
where
    S: Fn() -> P + Sync,
    P: FnMut(&[u8]) -> bool,
{
    collect_blob_lookup_batches_with_literal_paths(
        index,
        plan,
        scan_options,
        thread_policy,
        config,
        dir_abs,
        file_abs,
        false,
        symlink_probe,
        sparse,
        None,
        None,
    )
}

/// Symlink probe factory for scans that skip the ancestor-symlink check.
pub(crate) fn no_symlink_probe() -> impl FnMut(&[u8]) -> bool {
    |_| false
}

/// Collect lookups with directory-relative stats when `literal_paths` is true.
///
/// Only enable this when `file_abs` joins the raw index path without case or Unicode
/// resolution. On 64-bit Linux multi-file directories then share one parent handle;
/// other directories and platforms use `file_abs` as usual. `symlink_probe` is as in
/// [`collect_blob_lookup_batches`].
#[allow(clippy::too_many_arguments)]
pub(crate) fn collect_blob_lookup_batches_with_literal_paths<S, P>(
    index: &Index,
    plan: &BlobScanPlan,
    scan_options: WorktreeBlobScanOptions,
    thread_policy: &crate::parallel::ThreadPolicy,
    config: &crate::config::ConfigSet,
    dir_abs: impl Fn(&[u8]) -> PathBuf + Send + Sync + 'static,
    file_abs: impl Fn(&[u8]) -> PathBuf + Send + Sync + 'static,
    literal_paths: bool,
    symlink_probe: S,
    sparse: BlobScanSparseMode,
    cancel_watch: Option<Arc<AtomicBool>>,
    cancel_poll: Option<Arc<crate::progress::StackCancelPoll>>,
) -> Result<Vec<Vec<(usize, BlobDiskLookup)>>>
where
    S: Fn() -> P + Sync,
    P: FnMut(&[u8]) -> bool,
{
    let blob_count = plan.len();
    let cancel = StatScanCancel {
        watch: cancel_watch,
        owner_poll: cancel_poll,
    };
    // Keep small scans on the existing metadata path: setting up descriptor-relative
    // probes pays off on the larger indexes that also justify stat preload.
    let literal_paths = literal_paths && blob_count >= PARALLEL_STAT_MIN_ENTRIES;
    let threads = stat_scan_thread_count(thread_policy, config, scan_options, blob_count);
    let dir_abs_arc: Arc<DirAbsFn> = Arc::new(dir_abs);
    let file_abs_arc: Arc<DirAbsFn> = Arc::new(file_abs);
    let batches = if threads > 1 {
        preload_blob_lookups_parallel(
            index,
            plan,
            threads,
            &symlink_probe,
            Arc::clone(&dir_abs_arc),
            Arc::clone(&file_abs_arc),
            literal_paths,
            sparse,
            cancel,
        )?
    } else {
        let (batch, scanned) = collect_blob_lookups_serial(
            index,
            plan,
            &mut symlink_probe(),
            dir_abs_arc.as_ref(),
            file_abs_arc.as_ref(),
            literal_paths,
            sparse,
            &cancel,
        )?;
        if scanned != blob_count {
            return Err(Error::Message(format!(
                "blob scan expected {blob_count} scanned entries, got {scanned}"
            )));
        }
        vec![batch]
    };
    Ok(batches)
}

/// Like [`for_each_blob_by_directory`], optionally preloading disk lookups in parallel.
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
pub(crate) fn for_each_blob_by_directory_parallel<F, S, P>(
    index: &Index,
    plan: &BlobScanPlan,
    scan_options: WorktreeBlobScanOptions,
    thread_policy: &crate::parallel::ThreadPolicy,
    config: &crate::config::ConfigSet,
    dir_abs: impl Fn(&[u8]) -> PathBuf + Send + Sync + 'static,
    file_abs: impl Fn(&[u8]) -> PathBuf + Send + Sync + 'static,
    symlink_probe: S,
    visit: F,
) -> Result<()>
where
    F: FnMut(usize, BlobDiskLookup) -> Result<()>,
    S: Fn() -> P + Sync,
    P: FnMut(&[u8]) -> bool,
{
    let batches = collect_blob_lookup_batches(
        index,
        plan,
        scan_options,
        thread_policy,
        config,
        dir_abs,
        file_abs,
        symlink_probe,
        BlobScanSparseMode::Dense,
    )?;
    visit_blob_lookup_batches(batches, visit)
}

fn dir_entry_name_matches(index_file_name: &[u8], entry: &fs::DirEntry) -> bool {
    let disk = entry.file_name();
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        let disk = disk.as_os_str().as_bytes();
        if disk == index_file_name {
            return true;
        }
    }
    #[cfg(not(unix))]
    {
        let disk = disk.to_string_lossy();
        if disk.as_bytes() == index_file_name {
            return true;
        }
    }
    #[cfg(windows)]
    {
        let disk = disk.to_string_lossy();
        if let Ok(index) = std::str::from_utf8(index_file_name) {
            return disk.eq_ignore_ascii_case(index);
        }
    }
    false
}

/// Resolve a directory listing entry for `index_file_name`, including case-insensitive matches.
#[cfg(windows)]
fn matching_dir_entry<'a>(
    on_disk: &'a DirectoryMap,
    index_file_name: &[u8],
) -> Option<&'a fs::DirEntry> {
    if let Some(entry) = on_disk.get(index_file_name) {
        return Some(entry);
    }
    on_disk
        .values()
        .find(|entry| dir_entry_name_matches(index_file_name, entry))
}

fn symlink_metadata_blob_lookup(target_abs: &Path) -> BlobDiskLookup {
    match symlink_metadata_path(target_abs) {
        Ok(meta) => BlobDiskLookup::Present(BlobDiskStat::from_metadata(&meta)),
        Err(e)
            if e.kind() == io::ErrorKind::NotFound || e.kind() == io::ErrorKind::NotADirectory =>
        {
            BlobDiskLookup::Missing
        }
        Err(e) => BlobDiskLookup::Io(e),
    }
}

/// Prefer `DirEntry::metadata` from the directory scan (no extra handle open on Windows);
/// fall back to path `symlink_metadata` when the name was not listed or dir-entry stat fails
/// (#1518, #1622). Index disagreements with the scan stat are corrected later by
/// [`align_lookup_stat_with_index_path_windows`].
#[cfg(windows)]
fn blob_lookup_from_dir_entry_or_path(
    dir_entry: Option<&fs::DirEntry>,
    target_abs: impl FnOnce() -> PathBuf,
) -> BlobDiskLookup {
    if let Some(entry) = dir_entry {
        match dir_entry_metadata(entry) {
            Ok(m) => BlobDiskLookup::Present(BlobDiskStat::from_metadata(&m)),
            Err(e)
                if e.kind() == io::ErrorKind::NotFound
                    || e.kind() == io::ErrorKind::NotADirectory =>
            {
                symlink_metadata_blob_lookup(&target_abs())
            }
            Err(e) => BlobDiskLookup::Io(e),
        }
    } else {
        symlink_metadata_blob_lookup(&target_abs())
    }
}

/// `target_abs` builds the indexed absolute path for a fallback stat when the name is absent
/// from the directory listing.
fn lookup_blob_in_directory(
    on_disk: &DirectoryMap,
    index_file_name: &[u8],
    target_abs: impl FnOnce() -> PathBuf,
) -> BlobDiskLookup {
    #[cfg(windows)]
    if let Some(entry) = matching_dir_entry(on_disk, index_file_name) {
        return blob_lookup_from_dir_entry_or_path(Some(entry), target_abs);
    }
    if let Some(entry) = on_disk.get(index_file_name) {
        return match dir_entry_metadata(entry) {
            Ok(m) => BlobDiskLookup::Present(BlobDiskStat::from_metadata(&m)),
            Err(e) => BlobDiskLookup::Io(e),
        };
    }
    for entry in on_disk.values() {
        if dir_entry_name_matches(index_file_name, entry) {
            return match dir_entry_metadata(entry) {
                Ok(m) => BlobDiskLookup::Present(BlobDiskStat::from_metadata(&m)),
                Err(e) => BlobDiskLookup::Io(e),
            };
        }
    }
    symlink_metadata_blob_lookup(&target_abs())
}

/// One directory listing keyed by raw file name (`.git` excluded).
type DirectoryMap = hashbrown::HashMap<Vec<u8>, fs::DirEntry>;

/// `expected` sizes the map up front (the tracked entries in this directory).
fn read_directory_map(abs_dir: &Path, expected: usize) -> Result<(bool, DirectoryMap)> {
    if probe_counts(abs_dir) {
        WORKTREE_READ_DIR_PROBE.fetch_add(1, Ordering::Relaxed);
    }
    match fs::read_dir(abs_dir) {
        Ok(rd) => {
            let mut on_disk = DirectoryMap::with_capacity(expected);
            for item in rd {
                let Ok(entry) = item else { continue };
                let name = dir_entry_key(&entry);
                if name.as_slice() == b".git" {
                    continue;
                }
                on_disk.insert(name, entry);
            }
            Ok((true, on_disk))
        }
        Err(e)
            if e.kind() == io::ErrorKind::NotFound
                || e.kind() == io::ErrorKind::PermissionDenied
                || e.kind() == io::ErrorKind::NotADirectory =>
        {
            Ok((false, DirectoryMap::new()))
        }
        Err(e) => Err(Error::Io(e)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::{entry_from_stat, Index};
    use crate::objects::{ObjectId, ObjectKind};
    use crate::odb::Odb;
    use std::fs;
    use tempfile::TempDir;

    fn write_blob_file(dir: &Path, rel: &str, bytes: &[u8]) -> ObjectId {
        let odb = Odb::new(&dir.join(".git/objects"));
        let abs = dir.join(rel);
        if let Some(parent) = abs.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(&abs, bytes).unwrap();
        odb.write(ObjectKind::Blob, bytes).unwrap()
    }

    #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
    #[test]
    fn directory_relative_stats_match_metadata_fields_and_file_types() {
        use std::os::unix::fs::{symlink, PermissionsExt};
        use std::os::unix::net::UnixListener;

        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        let oid = ObjectId::null(crate::objects::HashAlgo::Sha1);
        let mut index = Index::new();
        for name in [
            "file",
            "executable",
            "directory",
            "link",
            "socket",
            "missing",
        ] {
            let path = root.join(name);
            fs::write(&path, b"file\n").unwrap();
            index.add_or_replace(entry_from_stat(&path, name.as_bytes(), oid, 0o100644).unwrap());
        }
        fs::OpenOptions::new()
            .write(true)
            .open(root.join("file"))
            .unwrap()
            .set_len(1_u64 << 32)
            .unwrap();
        filetime::set_file_mtime(
            root.join("file"),
            filetime::FileTime::from_unix_time(-1, 123_456),
        )
        .unwrap();
        fs::set_permissions(root.join("executable"), fs::Permissions::from_mode(0o755)).unwrap();
        for name in ["directory", "link", "socket", "missing"] {
            fs::remove_file(root.join(name)).unwrap();
        }
        fs::create_dir(root.join("directory")).unwrap();
        symlink("absent", root.join("link")).unwrap();
        let _socket = UnixListener::bind(root.join("socket")).unwrap();
        // Search permission suffices for lstat: the directory need not be readable.
        fs::set_permissions(root, fs::Permissions::from_mode(0o111)).unwrap();
        let handle = open_stat_directory(root).unwrap();
        let indices: Vec<_> = (0..index.entries.len()).collect();
        let mut cancel_stride = 0usize;
        let boundary = StatBoundaryPoll::Worker(None);
        let result = for_each_blob_at_directory(
            &index,
            &indices,
            &handle,
            root,
            boundary,
            &mut cancel_stride,
            &mut |i, rel, lookup| {
                let expected =
                    fs::symlink_metadata(crate::index::worktree_path_from_index_rel(root, rel));
                match (lookup, expected) {
                    (BlobDiskLookup::Present(actual), Ok(metadata)) => {
                        let expected = BlobDiskStat::from_metadata(&metadata);
                        assert_eq!(actual.git_mode, expected.git_mode);
                        assert_eq!(actual.is_dir, expected.is_dir);
                        assert_eq!(
                            actual.fields.index_entry(rel, oid, actual.git_mode),
                            expected.fields.index_entry(rel, oid, expected.git_mode),
                            "entry {i}"
                        );
                    }
                    (BlobDiskLookup::Missing, Err(error)) => {
                        assert_eq!(error.kind(), io::ErrorKind::NotFound)
                    }
                    _ => panic!("relative stat disagrees with path metadata for entry {i}"),
                }
                Ok(())
            },
        );
        fs::set_permissions(root, fs::Permissions::from_mode(0o755)).unwrap();
        result.unwrap();
    }

    #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
    #[test]
    fn directory_relative_stats_reject_embedded_nul() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("file");
        fs::write(&path, b"file\n").unwrap();
        let oid = ObjectId::null(crate::objects::HashAlgo::Sha1);
        let mut index = Index::new();
        index.add_or_replace(entry_from_stat(&path, b"file\0suffix", oid, 0o100644).unwrap());
        let handle = open_stat_directory(tmp.path()).unwrap();
        let mut cancel_stride = 0usize;
        let boundary = StatBoundaryPoll::Worker(None);
        let error = for_each_blob_at_directory(
            &index,
            &[0],
            &handle,
            tmp.path(),
            boundary,
            &mut cancel_stride,
            &mut |_, _, _| panic!("invalid name must not be truncated and visited"),
        )
        .unwrap_err();
        assert!(matches!(error, Error::Io(error) if error.kind() == io::ErrorKind::InvalidInput));
    }

    #[test]
    fn dir_grouped_scan_matches_clean_tree() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let oid = write_blob_file(wt, "d0/f0.txt", b"hello\n");
        let mut index = Index::new();
        index.add_or_replace(
            entry_from_stat(&wt.join("d0/f0.txt"), b"d0/f0.txt", oid, 0o100644).unwrap(),
        );
        let plan = group_blob_entries_by_dir(&index, false);
        assert_eq!(plan.dirs.len(), 1);
        assert_eq!(plan.dirs[0], b"d0".to_vec());

        let mut seen = 0;
        let dir_abs = |dir: &[u8]| {
            if dir.is_empty() {
                wt.to_path_buf()
            } else {
                crate::index::worktree_path_from_index_rel(wt, dir)
            }
        };
        let file_abs = |rel: &[u8]| crate::index::worktree_path_from_index_rel(wt, rel);
        for_each_blob_by_directory(
            &index,
            &plan,
            dir_abs,
            file_abs,
            |_| false,
            |entry_index, lookup| {
                let path = std::str::from_utf8(&index.entries[entry_index].path).unwrap();
                assert_eq!(path, "d0/f0.txt");
                assert_eq!(index.entries[entry_index].oid, oid);
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                seen += 1;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(seen, 1);
    }

    #[test]
    fn dir_grouped_scan_reports_missing_file() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let oid = ObjectId::zero();
        let mut index = Index::new();
        fs::write(wt.join("gone.txt"), b"x").unwrap();
        let entry = entry_from_stat(&wt.join("gone.txt"), b"gone.txt", oid, 0o100644).unwrap();
        index.add_or_replace(entry);
        fs::remove_file(wt.join("gone.txt")).unwrap();

        let plan = group_blob_entries_by_dir(&index, false);
        let mut missing = false;
        let dir_abs = |dir: &[u8]| {
            if dir.is_empty() {
                wt.to_path_buf()
            } else {
                crate::index::worktree_path_from_index_rel(wt, dir)
            }
        };
        let file_abs = |rel: &[u8]| crate::index::worktree_path_from_index_rel(wt, rel);
        for_each_blob_by_directory(
            &index,
            &plan,
            dir_abs,
            file_abs,
            |_| false,
            |_, lookup| {
                missing = matches!(lookup, BlobDiskLookup::Missing);
                Ok(())
            },
        )
        .unwrap();
        assert!(missing);
    }

    #[test]
    fn deep_linear_paths_stat_directly_without_read_dir() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        const CHAINS: usize = 120;
        const DEPTH: usize = 10;
        let mut index = Index::new();
        for chain in 0..CHAINS {
            let mut rel = format!("root{chain:03}");
            for d in 0..DEPTH {
                rel.push_str(&format!("/dir{d}"));
            }
            rel.push_str("/leaf.txt");
            let oid = write_blob_file(wt, &rel, b"payload\n");
            index.add_or_replace(
                entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
            );
        }
        let plan = group_blob_entries_by_dir(&index, false);
        let _probe = reset_worktree_metadata_probe(wt);
        let dir_abs = |dir: &[u8]| {
            if dir.is_empty() {
                wt.to_path_buf()
            } else {
                crate::index::worktree_path_from_index_rel(wt, dir)
            }
        };
        let file_abs = |rel: &[u8]| crate::index::worktree_path_from_index_rel(wt, rel);
        let mut seen = 0;
        for_each_blob_by_directory(
            &index,
            &plan,
            dir_abs,
            file_abs,
            |_| false,
            |_, lookup| {
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                seen += 1;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(seen, CHAINS);
        #[cfg(windows)]
        {
            assert!(
                worktree_read_dir_probe_count() >= CHAINS,
                "Windows deep leaf dirs should read_dir each parent (got {} read_dirs)",
                worktree_read_dir_probe_count()
            );
            assert!(
                worktree_metadata_probe_count() <= CHAINS + 5,
                "read_dir scan should stat via DirEntry metadata, got {}",
                worktree_metadata_probe_count()
            );
        }
        #[cfg(not(windows))]
        {
            assert_eq!(
                worktree_read_dir_probe_count(),
                0,
                "Unix single-file leaf dirs should use direct stat, not read_dir"
            );
            assert!(
                worktree_metadata_probe_count() <= CHAINS + 5,
                "direct stat scan budget exceeded: {}",
                worktree_metadata_probe_count()
            );
        }
    }

    #[test]
    fn parallel_scan_multi_file_directories_follow_platform_stat_strategy() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path().to_path_buf();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        const DIRS: usize = 450;
        const FILES_PER_DIR: usize = 3;
        let mut index = Index::new();
        for d in 0..DIRS {
            let dir = format!("leaf{d:05}");
            for f in 0..FILES_PER_DIR {
                let rel = format!("{dir}/part{f}.txt");
                let oid = write_blob_file(&wt, &rel, format!("{d}-{f}\n").as_bytes());
                index.add_or_replace(
                    entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
                );
            }
        }
        let plan = group_blob_entries_by_dir(&index, false);
        let _probe = reset_worktree_metadata_probe(&wt);
        let wt_dir = wt.clone();
        let dir_abs = move |dir: &[u8]| {
            if dir.is_empty() {
                wt_dir.clone()
            } else {
                crate::index::worktree_path_from_index_rel(&wt_dir, dir)
            }
        };
        let wt_file = wt.clone();
        let file_abs = move |rel: &[u8]| crate::index::worktree_path_from_index_rel(&wt_file, rel);
        let policy = crate::parallel::ThreadPolicy::default();
        let config = crate::config::ConfigSet::new();
        let mut seen = 0;
        for_each_blob_by_directory_parallel(
            &index,
            &plan,
            WorktreeBlobScanOptions {
                preload_index: true,
                stat_parallel_threads: Some(4),
            },
            &policy,
            &config,
            dir_abs,
            file_abs,
            no_symlink_probe,
            |_, lookup| {
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                seen += 1;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(seen, DIRS * FILES_PER_DIR);
        #[cfg(windows)]
        assert!(
            worktree_read_dir_probe_count() >= DIRS,
            "parallel preload must read_dir each multi-file parent (got {} read_dirs)",
            worktree_read_dir_probe_count()
        );
        #[cfg(not(windows))]
        assert_eq!(
            worktree_read_dir_probe_count(),
            0,
            "Unix stats tracked paths directly instead of listing parents"
        );
        let tracked = DIRS * FILES_PER_DIR;
        assert!(
            worktree_metadata_probe_count() <= tracked + DIRS + 8,
            "metadata probes {} exceeded budget for grouped scan",
            worktree_metadata_probe_count()
        );
    }

    #[test]
    fn lookup_blob_present_when_read_dir_lists_file_but_path_probe_misses() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().join("leaf");
        fs::create_dir_all(&dir).unwrap();
        fs::write(dir.join("tracked.txt"), b"payload\n").unwrap();
        let (readable, map) = read_directory_map(&dir, 1).unwrap();
        assert!(readable);
        let lookup = lookup_blob_in_directory(&map, b"tracked.txt", || {
            dir.join("wrong-parent/tracked.txt")
        });
        assert!(
            matches!(lookup, BlobDiskLookup::Present(_)),
            "read_dir listing must win over a failed path stat (issue #1518 / Bugbot)"
        );
    }

    #[test]
    fn listed_blob_lookup_one_metadata_probe_per_file() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path().to_path_buf();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        const FILES: usize = 40;
        let mut index = Index::new();
        for f in 0..FILES {
            let rel = format!("flat/f{f}.txt");
            let oid = write_blob_file(&wt, &rel, format!("{f}\n").as_bytes());
            index.add_or_replace(
                entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
            );
        }
        let plan = group_blob_entries_by_dir(&index, false);
        let _probe = reset_worktree_metadata_probe(&wt);
        let wt_dir = wt.clone();
        let dir_abs = move |dir: &[u8]| {
            if dir.is_empty() {
                wt_dir.clone()
            } else {
                crate::index::worktree_path_from_index_rel(&wt_dir, dir)
            }
        };
        let file_abs = {
            let wt_dir = wt.clone();
            move |rel: &[u8]| crate::index::worktree_path_from_index_rel(&wt_dir, rel)
        };
        let mut seen = 0usize;
        for_each_blob_by_directory(
            &index,
            &plan,
            dir_abs,
            file_abs,
            |_| false,
            |_, lookup| {
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                seen += 1;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(seen, FILES);
        assert!(
            worktree_metadata_probe_count() <= FILES + 4,
            "listed files should stat via read_dir entries, not a path probe each (got {} probes)",
            worktree_metadata_probe_count()
        );
    }

    #[test]
    fn dense_multi_file_dirs_list_each_parent_at_most_once() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path().to_path_buf();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        const DIRS: usize = 80;
        const FILES_PER_DIR: usize = 50;
        let mut index = Index::new();
        for d in 0..DIRS {
            let dir = format!("p/a/b/leaf{d:04}");
            for f in 0..FILES_PER_DIR {
                let rel = format!("{dir}/f{f}-file.txt");
                let oid = write_blob_file(&wt, &rel, format!("{d}-{f}\n").as_bytes());
                index.add_or_replace(
                    entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
                );
            }
        }
        let plan = group_blob_entries_by_dir(&index, false);
        let _probe = reset_worktree_metadata_probe(&wt);
        let wt_dir = wt.clone();
        let dir_abs = move |dir: &[u8]| {
            if dir.is_empty() {
                wt_dir.clone()
            } else {
                crate::index::worktree_path_from_index_rel(&wt_dir, dir)
            }
        };
        let wt_file = wt.clone();
        let file_abs = move |rel: &[u8]| crate::index::worktree_path_from_index_rel(&wt_file, rel);
        let policy = crate::parallel::ThreadPolicy::default();
        let config = crate::config::ConfigSet::new();
        let mut seen = 0;
        for_each_blob_by_directory_parallel(
            &index,
            &plan,
            WorktreeBlobScanOptions {
                preload_index: true,
                stat_parallel_threads: Some(4),
            },
            &policy,
            &config,
            dir_abs,
            file_abs,
            no_symlink_probe,
            |_, lookup| {
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                seen += 1;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(seen, DIRS * FILES_PER_DIR);
        let expected_read_dirs = if LIST_PARENT_DIRECTORIES { DIRS } else { 0 };
        assert_eq!(
            worktree_read_dir_probe_count(),
            expected_read_dirs,
            "each dense parent directory is listed once on Windows and never elsewhere"
        );
        let tracked = DIRS * FILES_PER_DIR;
        assert!(
            worktree_metadata_probe_count() <= tracked + DIRS + 8,
            "metadata probes {} exceeded budget for grouped scan",
            worktree_metadata_probe_count()
        );
    }

    #[test]
    fn fat_single_directory_splits_parallel_stat_batches() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path().to_path_buf();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        const FILES: usize = PARALLEL_STAT_MIN_ENTRIES + 500;
        let mut index = Index::new();
        for i in 0..FILES {
            let rel = format!("data/file_{i:07}.txt");
            let oid = write_blob_file(&wt, &rel, format!("content {i}\n").as_bytes());
            index.add_or_replace(
                entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
            );
        }
        let plan = group_blob_entries_by_dir(&index, false);
        assert_eq!(plan.dirs.len(), 1);
        let wt_dir = wt.clone();
        let dir_abs = move |dir: &[u8]| {
            if dir.is_empty() {
                wt_dir.clone()
            } else {
                crate::index::worktree_path_from_index_rel(&wt_dir, dir)
            }
        };
        let wt_file = wt.clone();
        let file_abs = move |rel: &[u8]| crate::index::worktree_path_from_index_rel(&wt_file, rel);
        let policy = crate::parallel::ThreadPolicy::default();
        let config = crate::config::ConfigSet::new();
        let batches = collect_blob_lookup_batches(
            &index,
            &plan,
            WorktreeBlobScanOptions {
                preload_index: true,
                stat_parallel_threads: Some(4),
            },
            &policy,
            &config,
            dir_abs,
            file_abs,
            no_symlink_probe,
            BlobScanSparseMode::Dense,
        )
        .unwrap();
        assert!(
            batches.len() > 1,
            "one huge directory should split across workers, got {} batches",
            batches.len()
        );
        let mut seen = std::collections::BTreeSet::new();
        for batch in &batches {
            for (entry_index, lookup) in batch {
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                assert!(seen.insert(*entry_index));
            }
        }
        assert_eq!(seen.len(), FILES);
        let _probe = reset_worktree_metadata_probe(&wt);
        let mut rescanned = 0usize;
        for_each_blob_by_directory_parallel(
            &index,
            &plan,
            WorktreeBlobScanOptions {
                preload_index: true,
                stat_parallel_threads: Some(4),
            },
            &policy,
            &config,
            {
                let wt_dir = wt.clone();
                move |dir: &[u8]| {
                    if dir.is_empty() {
                        wt_dir.clone()
                    } else {
                        crate::index::worktree_path_from_index_rel(&wt_dir, dir)
                    }
                }
            },
            {
                let wt_file = wt.clone();
                move |rel: &[u8]| crate::index::worktree_path_from_index_rel(&wt_file, rel)
            },
            no_symlink_probe,
            |_, lookup| {
                assert!(matches!(lookup, BlobDiskLookup::Present(_)));
                rescanned += 1;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(rescanned, FILES);
        assert!(
            worktree_metadata_probe_count() <= FILES + 4,
            "parallel fat-dir scan should stat about once per file, got {}",
            worktree_metadata_probe_count()
        );
    }

    #[test]
    fn core_preload_index_false_uses_single_thread() {
        let threads = stat_scan_thread_count(
            &crate::parallel::ThreadPolicy::default(),
            &crate::config::ConfigSet::new(),
            WorktreeBlobScanOptions {
                preload_index: false,
                stat_parallel_threads: Some(4),
            },
            PARALLEL_STAT_MIN_ENTRIES,
        );
        assert_eq!(threads, 1);
    }

    #[cfg(unix)]
    #[test]
    fn direct_parallel_scan_detects_directory_replaced_by_symlink() {
        use crate::diff::{
            diff_index_to_worktree_with_options, DiffIndexToWorktreeOptions, DiffStatus,
        };

        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        const FILES: usize = 1005;
        let mut index = Index::new();
        for i in 0..FILES {
            let rel = format!("dir/f{i:04}.txt");
            let oid = write_blob_file(wt, &rel, b"x\n");
            index.add_or_replace(
                entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
            );
        }
        fs::remove_dir_all(wt.join("dir")).unwrap();
        let external = wt.join("external");
        fs::create_dir_all(&external).unwrap();
        for i in 0..FILES {
            fs::write(external.join(format!("f{i:04}.txt")), b"y\n").unwrap();
        }
        std::os::unix::fs::symlink(&external, wt.join("dir")).unwrap();

        let odb = Odb::new(&wt.join(".git/objects"));
        let (diffs, _) = diff_index_to_worktree_with_options(
            &odb,
            &mut index,
            wt,
            DiffIndexToWorktreeOptions {
                for_status: true,
                ..DiffIndexToWorktreeOptions::default()
            },
        )
        .expect("diff");
        assert_eq!(diffs.len(), FILES);
        assert!(diffs.iter().all(|d| d.status == DiffStatus::Deleted));
    }

    #[cfg(unix)]
    #[test]
    fn parallel_unstaged_diff_detects_directory_replaced_by_symlink() {
        use crate::diff::{
            diff_index_to_worktree_with_options, DiffIndexToWorktreeOptions, DiffStatus,
        };
        use crate::parallel::ThreadPolicy;

        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let mut index = Index::new();
        let tracked_oid = write_blob_file(wt, "d/file.txt", b"same\n");
        index.add_or_replace(
            entry_from_stat(&wt.join("d/file.txt"), b"d/file.txt", tracked_oid, 0o100644).unwrap(),
        );
        const PADDING: usize = 1000;
        for i in 0..PADDING {
            let rel = format!("bulk/f{i:04}.txt");
            let oid = write_blob_file(wt, &rel, b"z\n");
            index.add_or_replace(
                entry_from_stat(&wt.join(&rel), rel.as_bytes(), oid, 0o100644).unwrap(),
            );
        }
        fs::remove_dir_all(wt.join("d")).unwrap();
        let target = wt.join("target");
        fs::create_dir_all(&target).unwrap();
        fs::write(target.join("file.txt"), b"same\n").unwrap();
        std::os::unix::fs::symlink(&target, wt.join("d")).unwrap();

        let odb = Odb::new(&wt.join(".git/objects"));
        let (diffs, _) = diff_index_to_worktree_with_options(
            &odb,
            &mut index,
            wt,
            DiffIndexToWorktreeOptions {
                thread_policy: Some(ThreadPolicy::default()),
                ..DiffIndexToWorktreeOptions::default()
            },
        )
        .expect("diff");
        assert_eq!(
            diffs.len(),
            1,
            "unstaged parallel diff must report deletion at indexed path"
        );
        assert_eq!(diffs[0].status, DiffStatus::Deleted);
        assert_eq!(diffs[0].old_path.as_deref(), Some("d/file.txt"));
    }

    #[cfg(unix)]
    #[test]
    fn directory_scan_symlink_to_unreadable_target_reports_missing() {
        use crate::diff::{
            diff_index_to_worktree_with_options, DiffIndexToWorktreeOptions, DiffStatus,
        };
        use std::os::unix::fs::PermissionsExt;

        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let oid = write_blob_file(wt, "dir/file.txt", b"tracked\n");
        let mut index = Index::new();
        index.add_or_replace(
            entry_from_stat(&wt.join("dir/file.txt"), b"dir/file.txt", oid, 0o100644).unwrap(),
        );

        fs::remove_dir_all(wt.join("dir")).unwrap();
        let secret = wt.join("secret");
        fs::create_dir(&secret).unwrap();
        fs::write(secret.join("file.txt"), b"other\n").unwrap();
        fs::set_permissions(&secret, fs::Permissions::from_mode(0)).unwrap();
        std::os::unix::fs::symlink(&secret, wt.join("dir")).unwrap();

        let odb = Odb::new(&wt.join(".git/objects"));
        let (diffs, _) = diff_index_to_worktree_with_options(
            &odb,
            &mut index,
            wt,
            DiffIndexToWorktreeOptions {
                index_mtime: None,
                ignore_submodule_untracked: false,
                simplify_gitlinks: false,
                error_on_broken_gitlinks: false,
                ..DiffIndexToWorktreeOptions::default()
            },
        )
        .expect("status diff must not traverse unreadable symlink target");
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].status, DiffStatus::Deleted);
        assert_eq!(diffs[0].old_path.as_deref(), Some("dir/file.txt"));
    }

    #[cfg(windows)]
    #[test]
    fn align_lookup_keeps_dir_scan_stat_when_path_probe_fails() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let rel = "probe-fail.txt";
        let oid = write_blob_file(wt, rel, b"payload\n");
        let mut index = Index::new();
        index
            .add_or_replace(entry_from_stat(&wt.join(rel), rel.as_bytes(), oid, 0o100644).unwrap());
        let dir_stat = BlobDiskStat::from_metadata(
            &fs::symlink_metadata(wt.join(rel)).expect("worktree metadata"),
        );
        let policy = crate::index::StatMatchPolicy::default();
        let lookup = align_lookup_stat_with_index_path_windows(
            &index,
            0,
            BlobDiskLookup::Present(dir_stat),
            || PathBuf::from("Z:\\no-such-path-for-align-probe\\missing.txt"),
            policy,
        );
        assert!(
            matches!(lookup, BlobDiskLookup::Present(s)),
            "path probe failure must not discard a successful directory scan"
        );
        if let BlobDiskLookup::Present(s) = lookup {
            assert_eq!(s.fields.index_size(), dir_stat.fields.index_size());
        }
    }

    #[cfg(windows)]
    const NSEC_POLICY: crate::index::StatMatchPolicy = crate::index::StatMatchPolicy {
        trust_ctime: true,
        check_stat: true,
        use_nanosec: true,
    };

    #[cfg(windows)]
    #[test]
    fn align_lookup_skips_path_probe_when_scan_fractions_match() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let rel = "clean.txt";
        let oid = write_blob_file(wt, rel, b"payload\n");
        let mut index = Index::new();
        let mut ie = entry_from_stat(&wt.join(rel), rel.as_bytes(), oid, 0o100644).unwrap();
        ie.ctime_sec += 1;
        index.add_or_replace(ie);
        let scan = BlobDiskStat::from_metadata(&fs::symlink_metadata(wt.join(rel)).unwrap());
        let lookup = align_lookup_stat_with_index_path_windows(
            &index,
            0,
            BlobDiskLookup::Present(scan),
            || panic!("whole-second ctime disagreement alone must not probe the path"),
            NSEC_POLICY,
        );
        assert!(matches!(lookup, BlobDiskLookup::Present(_)));
    }

    #[cfg(windows)]
    #[test]
    fn align_lookup_probes_path_when_scan_mtime_fraction_disagrees() {
        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let oid = write_blob_file(wt, "indexed.txt", b"payload\n");
        write_blob_file(wt, "scanned.txt", b"payload\n");
        let pinned = filetime::FileTime::from_unix_time(1_700_000_000, 123_456_700);
        let later = filetime::FileTime::from_unix_time(1_700_000_000, 123_456_800);
        filetime::set_file_mtime(wt.join("indexed.txt"), pinned).unwrap();
        filetime::set_file_mtime(wt.join("scanned.txt"), later).unwrap();
        let mut index = Index::new();
        index.add_or_replace(
            entry_from_stat(&wt.join("indexed.txt"), b"indexed.txt", oid, 0o100644).unwrap(),
        );
        let policy = crate::index::StatMatchPolicy {
            trust_ctime: false,
            ..NSEC_POLICY
        };
        let scan =
            BlobDiskStat::from_metadata(&fs::symlink_metadata(wt.join("scanned.txt")).unwrap());
        assert!(!crate::diff::stat_matches_blob_stat(
            &index.entries[0],
            &scan,
            policy
        ));

        let lookup = align_lookup_stat_with_index_path_windows(
            &index,
            0,
            BlobDiskLookup::Present(scan),
            || wt.join("indexed.txt"),
            policy,
        );
        let BlobDiskLookup::Present(stat) = lookup else {
            panic!("expected a present lookup");
        };
        assert!(
            crate::diff::stat_matches_blob_stat(&index.entries[0], &stat, policy),
            "a matching path probe must replace the scan stat"
        );
    }

    /// A clean checkout must status clean without opening tracked files, even one another
    /// process holds open with no sharing (issue #2175).
    #[cfg(windows)]
    #[test]
    fn status_after_checkout_is_clean_with_an_exclusively_locked_file() {
        use std::os::windows::fs::OpenOptionsExt;

        let tmp = TempDir::new().unwrap();
        let wt = tmp.path();
        fs::create_dir_all(wt.join(".git/objects")).unwrap();
        let mut index = Index::new();
        for rel in ["a.txt", "dir/b.txt", "locked.txt"] {
            let oid = write_blob_file(wt, rel, format!("{rel}\n").as_bytes());
            index.add_or_replace(
                entry_from_stat(&wt.join(rel), rel.as_bytes(), oid, 0o100644).unwrap(),
            );
        }
        index.sort();
        let index_path = wt.join(".git/index");
        index.write(&index_path).unwrap();
        let (sec, nsec) = crate::index::stage0_mtime_max(&index.entries);
        filetime::set_file_mtime(
            &index_path,
            filetime::FileTime::from_unix_time(i64::from(sec), nsec),
        )
        .unwrap();
        crate::index::advance_index_file_mtime_past_entries(&index, &index_path, None).unwrap();

        let _lock = fs::OpenOptions::new()
            .read(true)
            .share_mode(0)
            .open(wt.join("locked.txt"))
            .unwrap();
        assert!(
            fs::read(wt.join("locked.txt")).is_err(),
            "file must be unreadable"
        );

        let odb = Odb::new(&wt.join(".git/objects"));
        let (diffs, _) = crate::diff::diff_index_to_worktree_with_options(
            &odb,
            &mut index,
            wt,
            crate::diff::DiffIndexToWorktreeOptions {
                index_mtime: crate::index::index_file_mtime(&index_path),
                ..crate::diff::DiffIndexToWorktreeOptions::default()
            },
        )
        .expect("stat-only status must not read the locked file");
        assert!(diffs.is_empty(), "{diffs:?}");
    }
}