grit-lib 0.5.21

Core library for the grit Git implementation
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
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
//! `git checkout` worktree-apply primitives.
//!
//! `checkout` is a large worktree mutator. Its CLI shell
//! (`grit/src/commands/checkout.rs`) still owns argv/clap parsing,
//! branch-switch messaging, hook dispatch, progress-to-stderr, and the
//! detached-HEAD advice text. This module holds the **pure worktree-apply
//! primitives** that shell calls into: writing a blob's bytes to a working-tree
//! path (handling symlinks, executable bits, and parent-directory
//! preparation), removing now-empty parent directories, and the simple glob
//! matcher used to resolve interactive-patch path filters.
//!
//! These functions compute and apply worktree changes from index/object data
//! and make no presentation decisions — no colour, pager, tty, or stdout.

use std::borrow::Cow;
use std::collections::{BTreeSet, HashSet};
use std::fs::{self, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;

use crate::config::ConfigSet;
use crate::crlf::{self, ConversionConfig};
use crate::diff::{
    diff_tree_path_change_count, diff_trees, format_mode, zero_oid, DiffEntry, DiffStatus,
};
use crate::error::{Error, Result};
use crate::filter_process::{DelayedCheckoutError, DelayedProcessCheckout, FilterSmudgeMeta};
use crate::index::{
    entry_from_metadata, entry_from_stat, Index, IndexEntry, MODE_EXECUTABLE, MODE_GITLINK,
    MODE_REGULAR, MODE_SYMLINK, MODE_TREE,
};
use crate::objects::{Object, ObjectId, TreeEntry};
use crate::odb::{BoundedRead, Odb};
use crate::parallel::{try_par_map, ParallelHashError, ParallelOp, ParallelOptions};
use crate::path_icase::paths_equal;
use crate::path_population::{validate_tree_walk_entry, validate_worktree_path};
use crate::path_validate::ProtectOptions;
use crate::progress::{
    Cancel, CancelProgress, NeverCancel, NullProgress, ProgressPhase, ProgressSink, ProgressUnit,
};
use crate::repo::Repository;
use crate::sparse_checkout::SparseCheckout;
use crate::worktree::{io_with_path, WorktreePathOperation};
use crate::worktree_rules::WorktreeRules;

#[path = "checkout_parallel.rs"]
mod checkout_parallel;
pub(crate) use checkout_parallel::is_eligible_for_parallel_checkout;
use checkout_parallel::{
    execute_write_jobs, paths_needing_serial_checkout, CheckoutPlanSlot, CheckoutWriteJob,
    WriteJobOutcome,
};

const PARALLEL_PER_FILE_DELETE_MIN: usize = 32;
/// Chunk size for parallel checkout deletes so cancellation can stop before an entire tree is removed.
const PARALLEL_DELETE_CANCEL_CHUNK: usize = 8;
const POPULATED_TREE_CHECKOUT_CHUNK: usize = 2048;

fn remove_checkout_files_parallel_cancellable(
    work_tree: &Path,
    paths: &[String],
    options: ParallelOptions,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<(HashSet<String>, Vec<PathBuf>)> {
    let mut parallel_deleted_paths = HashSet::new();
    let mut deleted_abs_paths = Vec::new();
    for chunk in paths.chunks(PARALLEL_DELETE_CANCEL_CHUNK) {
        crate::progress::poll_cancel(progress, cancel)?;
        let abs_paths: Vec<PathBuf> = chunk
            .iter()
            .map(|p| checkout_stored_worktree_abs(work_tree, p))
            .collect();
        try_par_map(&abs_paths, options, 0, |abs| {
            let _ = fs::remove_file(abs);
            Ok(())
        })
        .map_err(|e: ParallelHashError<()>| match e {
            ParallelHashError::Task(()) => {
                Error::PathError("parallel checkout delete failed".into())
            }
        })?;
        for (path, abs) in chunk.iter().zip(abs_paths) {
            parallel_deleted_paths.insert(path.clone());
            deleted_abs_paths.push(abs);
            progress.inc(1);
            crate::progress::poll_cancel(progress, cancel)?;
        }
    }
    Ok((parallel_deleted_paths, deleted_abs_paths))
}

fn remove_wholesale_dirs_parallel_cancellable(
    work_tree: &Path,
    dirs: &[String],
    options: ParallelOptions,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<Vec<(String, PathBuf)>> {
    let mut removed = Vec::new();
    if dirs.is_empty() {
        return Ok(removed);
    }
    if dirs.len() < 2 {
        for dir in dirs {
            crate::progress::poll_cancel(progress, cancel)?;
            let abs = work_tree.join(dir);
            if remove_wholesale_dir(&abs) {
                removed.push((dir.clone(), abs));
            }
        }
        return Ok(removed);
    }
    let wt = work_tree.to_path_buf();
    for chunk in dirs.chunks(PARALLEL_DELETE_CANCEL_CHUNK) {
        crate::progress::poll_cancel(progress, cancel)?;
        let removed_chunk = try_par_map(chunk, options, 0, |dir| {
            let abs = wt.join(dir);
            Ok(remove_wholesale_dir(&abs).then_some((dir.clone(), abs)))
        })
        .map_err(|e: ParallelHashError<()>| match e {
            ParallelHashError::Task(()) => {
                Error::PathError("parallel wholesale directory removal failed".into())
            }
        })?;
        removed.extend(removed_chunk.into_iter().flatten());
    }
    Ok(removed)
}

/// One blob entry while walking a tree for populated checkout (no full diff list).
struct PopulatedTreeBlob {
    path: String,
    mode: u32,
    oid: ObjectId,
}

struct PopulatedTreeWalkFrame {
    prefix: String,
    entries: Vec<TreeEntry>,
    index: usize,
}

struct PopulatedTreeWalk<'a> {
    odb: &'a Odb,
    stack: Vec<PopulatedTreeWalkFrame>,
    protect: ProtectOptions,
}

impl<'a> PopulatedTreeWalk<'a> {
    fn new(odb: &'a Odb, tree_oid: &ObjectId, protect: ProtectOptions) -> Result<Self> {
        let entries = (*odb.cached_tree_entries(tree_oid)?).clone();
        Ok(Self {
            odb,
            stack: vec![PopulatedTreeWalkFrame {
                prefix: String::new(),
                entries,
                index: 0,
            }],
            protect,
        })
    }

    fn next_blob(&mut self) -> Result<Option<PopulatedTreeBlob>> {
        while let Some(frame) = self.stack.last_mut() {
            if frame.index >= frame.entries.len() {
                self.stack.pop();
                continue;
            }
            let entry = frame.entries[frame.index].clone();
            frame.index += 1;
            let path = crate::repo_relpath::format_path_stored(&frame.prefix, &entry.name);
            validate_tree_walk_entry(
                frame.prefix.as_bytes(),
                &entry.name,
                entry.mode,
                self.protect,
            )?;
            if entry.mode == MODE_TREE {
                let entries = (*self.odb.cached_tree_entries(&entry.oid)?).clone();
                self.stack.push(PopulatedTreeWalkFrame {
                    prefix: path,
                    entries,
                    index: 0,
                });
                continue;
            }
            return Ok(Some(PopulatedTreeBlob {
                path,
                mode: entry.mode,
                oid: entry.oid,
            }));
        }
        Ok(None)
    }

    fn fill_chunk(&mut self, chunk: &mut Vec<PopulatedTreeBlob>) -> Result<bool> {
        chunk.clear();
        while chunk.len() < POPULATED_TREE_CHECKOUT_CHUNK {
            match self.next_blob()? {
                Some(blob) => chunk.push(blob),
                None => break,
            }
        }
        Ok(!chunk.is_empty())
    }
}

fn tree_contains_dot_gitattributes(
    odb: &Odb,
    tree_oid: &ObjectId,
    prefix: &str,
    protect: ProtectOptions,
) -> Result<bool> {
    let entries = (*odb.cached_tree_entries(tree_oid)?).clone();
    for entry in entries {
        if entry.name == b".gitattributes" && entry.mode != MODE_TREE {
            return Ok(true);
        }
        if entry.mode == MODE_TREE {
            validate_tree_walk_entry(prefix.as_bytes(), &entry.name, entry.mode, protect)?;
            let path = crate::repo_relpath::format_path_stored(prefix, &entry.name);
            if tree_contains_dot_gitattributes(odb, &entry.oid, &path, protect)? {
                return Ok(true);
            }
        }
    }
    Ok(false)
}

/// Forwards `inc`/`set` to the inner sink but ignores `start`/`finish` (populated-tree chunks).
struct ChunkProgressSink<'a>(&'a mut dyn ProgressSink);

impl ProgressSink for ChunkProgressSink<'_> {
    fn start(&mut self, _phase: ProgressPhase, _total: Option<u64>) {}

    fn start_with_unit(&mut self, _phase: ProgressPhase, _unit: ProgressUnit, _total: Option<u64>) {
    }

    fn inc(&mut self, units: u64) {
        self.0.inc(units);
    }

    fn set(&mut self, current: u64) {
        self.0.set(current);
    }

    fn finish(&mut self) {}
}

fn populated_blob_to_added_diff(blob: &PopulatedTreeBlob) -> DiffEntry {
    DiffEntry {
        status: DiffStatus::Added,
        old_path: None,
        new_path: Some(blob.path.clone()),
        old_mode: "000000".to_owned(),
        new_mode: format_mode(blob.mode),
        old_oid: zero_oid(),
        new_oid: blob.oid,
        score: None,
    }
}

/// Populate a work tree and index from `tree_oid` (empty index or wholesale replace).
fn checkout_populated_tree(
    repo: &Repository,
    tree_oid: &ObjectId,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<()> {
    let work_tree = repo
        .work_tree()
        .ok_or_else(|| Error::PathError("cannot update a bare repository's working tree".into()))?;

    let config = repo.config()?;
    let protect = ProtectOptions::from_config(config.as_ref());
    let ignorecase = config
        .get_bool("core.ignorecase")
        .and_then(|r| r.ok())
        .unwrap_or(false);
    let precompose = repo.pathspec_precompose_enabled();

    let total = diff_tree_path_change_count(repo.odb(), None, Some(tree_oid), "")?;
    progress.start(
        ProgressPhase::CheckingOutFiles,
        Some(u64::try_from(total).unwrap_or(u64::MAX)),
    );

    let mut loaded_index = repo.load_index()?;
    let index_mut = &mut loaded_index;

    let rules = Rc::new(
        if tree_contains_dot_gitattributes(repo.odb(), tree_oid, "", protect)? {
            let mut prospective = Index::new();
            for entry in crate::porcelain::merge::tree_to_index_entries(repo, tree_oid, "")? {
                prospective.add_or_replace(entry);
            }
            prospective.sort();
            WorktreeRules::from_checkout_prospective_index(repo, &prospective, config.clone())?
        } else {
            checkout_worktree_rules(repo, index_mut, &[], config.clone())?
        },
    );
    let conv = rules.conversion().clone();

    let mut walk = PopulatedTreeWalk::new(repo.odb(), tree_oid, protect)?;
    let mut chunk = Vec::with_capacity(POPULATED_TREE_CHECKOUT_CHUNK);
    // Chunks only update the in-memory index. Building the cache tree walks the whole tree and
    // writing the index serializes every entry, so both happen once, after the last chunk.
    let mut apply = CheckoutApplyOptions {
        parallel_prepare_blobs: false,
        checkout_result_tree: None,
        defer_index_disk_write: true,
        precomputed_worktree_rules: Some(Rc::clone(&rules)),
        sparse_paths_applied: true,
        ..CheckoutApplyOptions::default()
    };
    let sparse = SparseCheckout::load(repo)?;
    let mut bulk_index_rewrite = false;

    while walk.fill_chunk(&mut chunk)? {
        crate::progress::poll_cancel(progress, cancel)?;
        let chunk_changes: Vec<DiffEntry> =
            chunk.iter().map(populated_blob_to_added_diff).collect();
        let changes = apply_sparse_tree_changes_to_index(
            sparse.as_ref(),
            work_tree,
            index_mut,
            &chunk_changes,
            &mut apply,
        );
        prefetch_promisor_blobs_for_checkout(repo, &changes)?;
        let mut chunk_progress = ChunkProgressSink(progress);
        let bulk_write = bulk_write_checkout_eligible(&changes, rules.as_ref())
            && tree_changes_are_all_additions(&changes);
        bulk_index_rewrite = chunk.len() == POPULATED_TREE_CHECKOUT_CHUNK
            || bulk_write
            || changes.len() >= BULK_INDEX_REWRITE_MIN;
        if bulk_write {
            checkout_bulk_added_tree_changes(
                repo,
                work_tree,
                &changes,
                index_mut,
                rules.as_ref(),
                &config,
                &conv,
                apply.clone(),
                precompose,
                ignorecase,
                &mut chunk_progress,
                cancel,
            )?;
        } else {
            apply.skip_bulk_checkout_fast_path = true;
            checkout_tree_changes_with_progress(
                repo,
                &changes,
                &mut chunk_progress,
                cancel,
                Some(index_mut),
                apply.clone(),
                None,
            )?;
            apply.skip_bulk_checkout_fast_path = false;
        }
    }

    attach_checkout_result_cache_tree(repo, index_mut, tree_oid)?;
    write_checkout_index(repo, index_mut, bulk_index_rewrite, None)?;

    progress.finish();
    crate::progress::poll_cancel(progress, cancel)
}

fn checkout_between_trees_slow(
    repo: &Repository,
    from: Option<&ObjectId>,
    to: Option<&ObjectId>,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<()> {
    let changes = diff_trees(repo.odb(), from, to, "")?;
    let mut apply = CheckoutApplyOptions::default();
    if let Some(tree) = to {
        apply.checkout_result_tree = Some(*tree);
        if from.is_none() {
            apply.parallel_prepare_blobs = true;
        }
    }
    checkout_tree_changes_with_progress(repo, &changes, progress, cancel, None, apply, None)
}

/// Apply the change from tree `from` to tree `to` onto the working tree and
/// index, then write the index.
///
/// This is the reusable core of a clean branch switch / fast-forward: it writes,
/// updates, and deletes only the paths that differ between the two trees, and
/// rebuilds the matching index entries (with fresh stat data so a subsequent
/// status reports a clean tree). Unchanged paths are left untouched.
///
/// The caller is responsible for policy: this function assumes the tracked
/// working tree is **clean** relative to `from` (no staged or unstaged changes
/// that would be silently overwritten) and that no untracked file sits where an
/// added path needs to land. It performs the mechanical apply only.
pub fn checkout_between_trees(
    repo: &Repository,
    from: Option<&ObjectId>,
    to: &ObjectId,
) -> Result<()> {
    checkout_between_trees_with_progress(repo, from, to, &mut NullProgress, &NeverCancel)
}

/// Like [`checkout_between_trees`], reporting checkout progress to `progress`.
///
/// Populated-tree checkouts emit [`ProgressPhase::CheckingOutFiles`] with the
/// exact blob count from the destination tree; tree-to-tree diffs report the
/// number of path changes.
pub fn checkout_between_trees_with_progress(
    repo: &Repository,
    from: Option<&ObjectId>,
    to: &ObjectId,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<()> {
    if from.is_none() {
        return checkout_populated_tree(repo, to, progress, cancel);
    }
    checkout_between_trees_slow(repo, from, Some(to), progress, cancel)
}

/// Replace the index and tracked worktree so they match `target_tree` exactly.
///
/// Used by hard reset. When `head_tree` differs from `target_tree`, only paths that
/// changed between the two trees are written first (same as branch checkout); paths
/// that stayed the same are left on disk when already up to date. When both trees
/// are equal (reset to the current commit), only dirty or mismatched paths are
/// rewritten. Tracked paths absent from `target_tree` are removed and the index is
/// rebuilt without unmerged stages.
///
/// # Errors
///
/// Returns an error when the repository has no work tree or checkout I/O fails.
pub fn checkout_hard_to_tree_from_head_tree(
    repo: &Repository,
    head_tree: &ObjectId,
    target_tree: &ObjectId,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<()> {
    use crate::porcelain::index_from_tree::index_from_tree_reusing_cache_tree;

    let work_tree = repo
        .work_tree()
        .ok_or_else(|| Error::PathError("cannot update a bare repository's working tree".into()))?;

    let pre_reset_index = repo.load_index()?;
    let mut target = index_from_tree_reusing_cache_tree(repo, target_tree, &pre_reset_index)?;
    let target_cache_tree = target.cache_tree.take();
    let target_entries = std::mem::take(&mut target.entries);
    let trees_differ = head_tree != target_tree;
    let changes = if trees_differ {
        diff_trees(repo.odb(), Some(head_tree), Some(target_tree), "")?
    } else {
        Vec::new()
    };
    // Raciness is judged against the index as it was before the reset: the tree-diff checkout
    // below rewrites the index, which would make racily clean entries look settled.
    let pre_reset_index_mtime = crate::diff::index_mtime_for_diff(&pre_reset_index, None);
    let deleted_abs_paths = remove_index_paths_outside_tree_diff(
        &pre_reset_index,
        work_tree,
        &target_entries,
        &changes,
    );
    drop(pre_reset_index);

    let mut written_by_tree_diff: BTreeSet<Vec<u8>> = BTreeSet::new();
    if !changes.is_empty() {
        checkout_tree_changes_with_progress(
            repo,
            &changes,
            progress,
            cancel,
            None,
            CheckoutApplyOptions::default(),
            None,
        )?;
        for change in &changes {
            if change.status == DiffStatus::Deleted {
                continue;
            }
            if let Some(path) = &change.new_path {
                written_by_tree_diff.insert(crate::repo_relpath::storage_bytes(path).into_owned());
            }
        }
    }

    let mut index = repo.load_index()?;
    index.source_mtime = pre_reset_index_mtime;
    let config = repo.config()?;
    let rules = WorktreeRules::from_parts(repo, &index, config.clone())?;
    let sparse = SparseCheckout::load(repo)?;
    let mut delayed = DelayedProcessCheckout::default();
    let mut pending_delayed: Vec<DelayedCheckoutTarget> = Vec::new();
    let mut new_entries: Vec<IndexEntry> = Vec::with_capacity(target_entries.len());
    let protect = ProtectOptions::from_config(config.as_ref());
    let mut dir_cache = LeadingDirCache::with_protect(protect);

    let worktree_stats = scan_index_blob_stats(repo, work_tree, &index, config.as_ref())?;
    let stat_policy = crate::index::StatMatchPolicy::from_config(Some(config.as_ref()));
    let trust_filemode = config
        .get_bool("core.filemode")
        .and_then(|r| r.ok())
        .unwrap_or(true);
    let mut actions: Vec<HardResetEntryAction> = target_entries
        .iter()
        .map(|te| {
            if sparse.as_ref().is_some_and(|sparse| {
                sparse.checkout_skips(&index, &crate::repo_relpath::storage_string(&te.path))
            }) {
                return HardResetEntryAction::SkipWorktree;
            }
            if te.mode == MODE_GITLINK {
                return HardResetEntryAction::Gitlink;
            }
            let pos = index.find_entry_pos(&te.path, 0);
            let existing = pos.map(|pos| (pos, &index.entries[pos]));
            let matches_target = existing.filter(|(_, ie)| ie.oid == te.oid && ie.mode == te.mode);
            if trees_differ && written_by_tree_diff.contains(&te.path) {
                return HardResetEntryAction::Written(
                    matches_target
                        .filter(|(_, ie)| !ie.stat_cache_unset())
                        .map(|(pos, _)| pos),
                );
            }
            let Some((pos, existing)) = matches_target else {
                return HardResetEntryAction::Write;
            };
            match worktree_stats[pos] {
                Some(stat)
                    if hard_reset_entry_stat_clean(
                        existing,
                        &stat,
                        stat_policy,
                        trust_filemode,
                        pre_reset_index_mtime,
                    ) =>
                {
                    HardResetEntryAction::Keep(pos)
                }
                _ => HardResetEntryAction::Verify,
            }
        })
        .collect();
    verify_hard_reset_candidates(
        repo,
        work_tree,
        &index,
        &target_entries,
        &rules,
        &mut actions,
    )?;

    progress.start(
        ProgressPhase::CheckingOutFiles,
        Some(u64::try_from(target_entries.len()).unwrap_or(u64::MAX)),
    );
    for (mut te, action) in target_entries.into_iter().zip(actions) {
        crate::progress::poll_cancel(progress, cancel)?;
        let abs = crate::index::worktree_path_from_index_rel(work_tree, &te.path);
        let mode = te.mode;
        match action {
            HardResetEntryAction::SkipWorktree => {
                te.set_skip_worktree(true);
                new_entries.push(te);
                continue;
            }
            HardResetEntryAction::Keep(pos) | HardResetEntryAction::Written(Some(pos)) => {
                new_entries.push(index_entry_with_recorded_stat(&index.entries[pos]));
                continue;
            }
            HardResetEntryAction::Written(None) | HardResetEntryAction::VerifiedUpToDate => {
                let meta = fs::symlink_metadata(&abs).map_err(Error::Io)?;
                new_entries.push(entry_from_metadata(&meta, te.path.as_slice(), te.oid, mode));
                continue;
            }
            HardResetEntryAction::Gitlink
            | HardResetEntryAction::Write
            | HardResetEntryAction::Verify => {}
        }
        if mode == MODE_GITLINK {
            if trees_differ && written_by_tree_diff.contains(&te.path) {
                let abs = abs.clone();
                let mut entry = entry_from_stat(&abs, te.path.as_slice(), te.oid, MODE_GITLINK)?;
                entry.size = 0;
                new_entries.push(entry);
                continue;
            }
            if let Some(existing) = index.get(te.path.as_slice(), 0) {
                if existing.oid == te.oid
                    && existing.mode == MODE_GITLINK
                    && gitlink_worktree_matches_index(&abs)
                {
                    let abs = abs.clone();
                    let meta = fs::symlink_metadata(&abs).map_err(Error::Io)?;
                    let mut entry =
                        entry_from_metadata(&meta, te.path.as_slice(), te.oid, MODE_GITLINK);
                    entry.size = 0;
                    new_entries.push(entry);
                    continue;
                }
            }
            let path = crate::repo_relpath::storage_string(&te.path);
            materialize_gitlink_worktree(work_tree, &path, &mut dir_cache)?;
            let abs = abs.clone();
            let mut entry = entry_from_stat(&abs, te.path.as_slice(), te.oid, MODE_GITLINK)?;
            entry.size = 0;
            new_entries.push(entry);
            continue;
        }

        let path = crate::repo_relpath::storage_string(&te.path);
        let change = diff_entry_for_worktree_blob_refresh(work_tree, &path, te.oid, mode);
        let Some(object) = read_blob_unless_streamed(
            repo,
            rules.conversion(),
            &rules.file_attrs(&path, false),
            mode,
            &te.oid,
        )?
        else {
            new_entries.push(stream_checkout_entry(
                repo,
                work_tree,
                &path,
                mode,
                &change,
                &mut dir_cache,
                te.oid,
            )?);
            continue;
        };
        let worktree_bytes = worktree_bytes_from_index_blob(
            repo,
            &rules,
            &path,
            mode,
            &object.data,
            &te.oid,
            None,
            Some(&mut delayed),
        )?;
        let Some(worktree_bytes) = worktree_bytes else {
            pending_delayed.push(DelayedCheckoutTarget {
                path: path.clone(),
                mode,
                oid: te.oid,
                change: change.clone(),
            });
            continue;
        };
        let entry = write_checkout_entry(
            work_tree,
            &path,
            WorktreeFileContent::Bytes(&worktree_bytes),
            mode,
            &change,
            &mut dir_cache,
            te.oid,
        )?;
        new_entries.push(entry);
        progress.inc(1);
    }

    finish_delayed_checkouts(
        repo,
        &rules,
        work_tree,
        &mut delayed,
        &pending_delayed,
        &mut dir_cache,
        &mut new_entries,
    )?;

    remove_empty_parent_dirs_batch(work_tree, &deleted_abs_paths);
    progress.finish();

    let mut new_index = Index::new();
    new_index.entries = new_entries;
    new_index.sort();
    if let Some(cache_tree) = target_cache_tree
        .filter(|ct| usize::try_from(ct.entry_count).ok() == Some(new_index.entries.len()))
    {
        new_index.set_cache_tree(cache_tree);
    }
    repo.write_index(&mut new_index)?;
    Ok(())
}

/// What a hard reset does with one target-tree entry.
enum HardResetEntryAction {
    /// Outside the sparse checkout: record the index row with `skip-worktree`, no work-tree I/O.
    SkipWorktree,
    /// Gitlink: handled by the submodule placeholder logic.
    Gitlink,
    /// Written by the tree-diff checkout; reuse the index row it recorded (position in the
    /// reloaded index) when that row has stat data, else stat the path.
    Written(Option<usize>),
    /// Unchanged and stat-clean: keep the worktree file and its index row (position in the
    /// reloaded index).
    Keep(usize),
    /// Unchanged but stat-dirty, racy or unscanned: compare content before deciding.
    Verify,
    /// Content check passed: keep the worktree file.
    VerifiedUpToDate,
    /// Write the blob to the worktree.
    Write,
}

/// Stage-0 copy of an index row whose stat data was recorded by a path stat (Git's own
/// write, or this checkout), so the rebuilt index never stores directory-scan stat fields.
fn index_entry_with_recorded_stat(entry: &IndexEntry) -> IndexEntry {
    let mut entry = entry.clone();
    entry.flags = entry.path.len().min(0xFFF) as u16;
    entry.flags_extended = None;
    entry.base_index_pos = 0;
    entry
}

/// Whether a directory-scan stat proves the worktree file still matches `existing` without
/// reading it: a regular file (a directory can share size and mtime with the file it
/// replaced), same stat data and (with `core.filemode`) executable bit, and the entry is not
/// racily clean.
fn hard_reset_entry_stat_clean(
    existing: &IndexEntry,
    stat: &crate::worktree_scan::BlobDiskStat,
    stat_policy: crate::index::StatMatchPolicy,
    trust_filemode: bool,
    index_mtime: Option<(u32, u32)>,
) -> bool {
    is_regular_git_mode(existing.mode)
        && !stat.is_dir
        && is_regular_git_mode(stat.git_mode)
        && (!trust_filemode || stat.git_mode == existing.mode)
        && crate::diff::stat_matches_blob_stat(existing, stat, stat_policy)
        && !crate::diff::entry_is_racy_with_policy(existing, index_mtime, stat_policy)
}

/// Stat every scan-eligible stage-0 blob in `index` with one directory-grouped pass.
///
/// Returns one slot per index entry: `Some` when the path exists on disk, `None` when it is
/// missing, unreadable, or not part of the scan (gitlinks, skip-worktree, unmerged).
fn scan_index_blob_stats(
    repo: &Repository,
    work_tree: &Path,
    index: &Index,
    config: &ConfigSet,
) -> Result<Vec<Option<crate::worktree_scan::BlobDiskStat>>> {
    use crate::worktree_scan::{
        collect_blob_lookup_batches, group_blob_entries_by_dir, BlobDiskLookup, BlobScanSparseMode,
        WorktreeBlobScanOptions,
    };

    let mut stats = vec![None; index.entries.len()];
    let plan = group_blob_entries_by_dir(index, false);
    if plan.is_empty() {
        return Ok(stats);
    }
    let preload_index = config
        .get_bool("core.preloadIndex")
        .and_then(|r| r.ok())
        .unwrap_or(true);
    let dir_root = work_tree.to_path_buf();
    let file_root = work_tree.to_path_buf();
    let batches = collect_blob_lookup_batches(
        index,
        &plan,
        WorktreeBlobScanOptions {
            preload_index,
            stat_parallel_threads: None,
        },
        &repo.thread_policy(),
        config,
        move |dir: &[u8]| crate::index::worktree_path_from_index_rel(&dir_root, dir),
        move |rel: &[u8]| crate::index::worktree_path_from_index_rel(&file_root, rel),
        |_| false,
        BlobScanSparseMode::Dense,
    )?;
    for (entry_index, lookup) in batches.into_iter().flatten() {
        if let BlobDiskLookup::Present(stat) = lookup {
            stats[entry_index] = Some(stat);
        }
    }
    Ok(stats)
}

/// Resolve [`HardResetEntryAction::Verify`] entries by comparing worktree content with the
/// index blob, in parallel (racy files are typically a whole batch just written by a
/// previous checkout, and each check opens and reads the file).
fn verify_hard_reset_candidates(
    repo: &Repository,
    work_tree: &Path,
    index: &Index,
    target_entries: &[IndexEntry],
    rules: &WorktreeRules,
    actions: &mut [HardResetEntryAction],
) -> Result<()> {
    let candidates: Vec<usize> = actions
        .iter()
        .enumerate()
        .filter(|(_, action)| matches!(action, HardResetEntryAction::Verify))
        .map(|(i, _)| i)
        .collect();
    if candidates.is_empty() {
        return Ok(());
    }
    let config = repo.config()?;
    let mut options = repo
        .thread_policy()
        .resolve(ParallelOp::IndexRefresh, config.as_ref());
    options.min_total_bytes = None;
    let filter_process = rules.filter_process();
    let odb = repo.odb();
    let git_dir = repo.git_dir();
    let up_to_date = try_par_map(&candidates, options, 0, |&i| {
        let te = &target_entries[i];
        let Some(existing) = index.get(&te.path, 0) else {
            return Ok(false);
        };
        crate::diff::path_checkout_skip_blob_write_when_up_to_date_with_config(
            odb,
            git_dir,
            work_tree,
            index,
            existing,
            &te.oid,
            te.mode,
            &crate::repo_relpath::storage_string(&te.path),
            Some(filter_process),
            Some(config.as_ref()),
        )
    })
    .map_err(|ParallelHashError::Task(e)| e)?;
    for (i, keep) in candidates.into_iter().zip(up_to_date) {
        actions[i] = if keep {
            HardResetEntryAction::VerifiedUpToDate
        } else {
            HardResetEntryAction::Write
        };
    }
    Ok(())
}

/// Remove indexed paths that leave the index but are not tree-diff deletions.
///
/// Tree-diff deletions of plain blobs are left to the tree-diff checkout, which removes whole
/// directories at once and cleans up empty parents in one batch. Paths that only exist in the
/// index (staged additions), gitlinks and unmerged paths are removed here. Returns the
/// removed paths for empty-parent cleanup.
fn remove_index_paths_outside_tree_diff(
    index: &Index,
    work_tree: &Path,
    target_entries: &[IndexEntry],
    changes: &[DiffEntry],
) -> Vec<PathBuf> {
    let target_paths: HashSet<&[u8]> = target_entries.iter().map(|e| e.path.as_slice()).collect();
    let tree_deleted: HashSet<Vec<u8>> = changes
        .iter()
        .filter(|change| change.status == DiffStatus::Deleted)
        .filter_map(|change| change.old_path.as_deref())
        .map(checkout_stored_index_path_bytes)
        .collect();
    let mut removed = Vec::new();
    let mut previous: Option<&[u8]> = None;
    for entry in &index.entries {
        let path = entry.path.as_slice();
        if previous == Some(path) || target_paths.contains(path) {
            continue;
        }
        previous = Some(path);
        let stage0 = index.get(path, 0);
        if tree_deleted.contains(path) && stage0.is_some_and(|e| e.mode != MODE_GITLINK) {
            continue;
        }
        let abs = crate::index::worktree_path_from_index_rel(work_tree, path);
        if stage0.is_some_and(|e| e.mode == MODE_GITLINK) {
            remove_uninitialized_gitlink_worktree(&abs);
        } else {
            let _ = fs::remove_file(&abs);
        }
        removed.push(abs);
    }
    removed
}

/// Apply a precomputed tree-to-tree diff to the working tree and index.
///
/// Callers such as `grit switch` can reuse one [`diff_trees`] result for both
/// untracked guards and the mechanical apply.
pub fn checkout_tree_changes(repo: &Repository, changes: &[DiffEntry]) -> Result<()> {
    checkout_tree_changes_with_progress(
        repo,
        changes,
        &mut NullProgress,
        &NeverCancel,
        None,
        CheckoutApplyOptions::default(),
        None,
    )
}

/// Options for [`checkout_tree_changes_with_index_opts`].
#[derive(Clone, Default)]
pub(crate) struct CheckoutApplyOptions {
    /// Read blob ODB objects in parallel when preparing write jobs.
    ///
    /// Only set when every OID is known to exist (e.g. tree-to-tree switch); general checkout
    /// must keep serial prepare so missing blobs flush earlier writes before failing.
    pub parallel_prepare_blobs: bool,
    /// Override `checkout.thresholdForParallelism` for this apply (`None` = repository config).
    pub checkout_parallel_threshold: Option<i64>,
    /// When true, wholesale directory removal skips the on-disk safety walk (switch already
    /// verified no untracked paths under those directories).
    pub trust_wholesale_dir_removal: bool,
    /// Wholesale delete directories that passed the on-disk safety walk during switch prepare.
    pub preverified_wholesale_dirs: BTreeSet<String>,
    /// When true, [`preverified_wholesale_dirs`] is authoritative and checkout skips safety walks.
    pub wholesale_safety_precomputed: bool,
    /// Skip bulk add/delete fast paths (used when bulk checkout hits a delayed filter).
    pub skip_bulk_checkout_fast_path: bool,
    /// When set, the index should match this tree after checkout (updates cache-tree root).
    pub checkout_result_tree: Option<crate::objects::ObjectId>,
    /// Apply checkout to the worktree but defer index disk writes until the caller finishes.
    pub defer_index_disk_write: bool,
    /// Pre-built attribute/conversion context (populated-tree checkout with `.gitattributes`).
    pub precomputed_worktree_rules: Option<Rc<WorktreeRules>>,
    /// The caller already split off sparse-checkout skipped paths; every change left is
    /// applied to the work tree.
    pub sparse_paths_applied: bool,
    /// Index rows (with `skip-worktree` set) for sparse-checkout skipped paths, merged into the
    /// index together with the rows checkout writes.
    pub skip_worktree_rows: Vec<IndexEntry>,
}

/// Like [`checkout_tree_changes`], but updates `index` in place instead of loading from disk.
///
/// Callers such as `grit switch` use this after a tree-diff guard pass that already loaded the index.
pub fn checkout_tree_changes_with_index(
    repo: &Repository,
    changes: &[DiffEntry],
    index: &mut Index,
) -> Result<()> {
    checkout_tree_changes_with_index_opts(
        repo,
        changes,
        index,
        &mut NullProgress,
        &NeverCancel,
        CheckoutApplyOptions::default(),
        None,
    )
}

/// Like [`checkout_tree_changes_with_index`], with apply-time tuning for trusted callers.
pub(crate) fn checkout_tree_changes_with_index_opts(
    repo: &Repository,
    changes: &[DiffEntry],
    index: &mut Index,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
    apply: CheckoutApplyOptions,
    attribute_changes: Option<&[DiffEntry]>,
) -> Result<()> {
    checkout_tree_changes_with_progress(
        repo,
        changes,
        progress,
        cancel,
        Some(index),
        apply,
        attribute_changes,
    )
}

/// Top-level directories whose tracked files are entirely removed by `delete_paths`.
pub(crate) fn wholesale_deleted_directories(
    index: &Index,
    delete_paths: &[String],
) -> BTreeSet<String> {
    use std::collections::BTreeMap;
    if delete_paths.len() < 8 {
        return BTreeSet::new();
    }
    let mut deleted_under: BTreeMap<String, usize> = BTreeMap::new();
    for path in delete_paths {
        let Some((dir, _rest)) = path.split_once('/') else {
            continue;
        };
        *deleted_under.entry(dir.to_owned()).or_default() += 1;
    }
    if deleted_under.is_empty() {
        return BTreeSet::new();
    }
    let delete_set: HashSet<String> = delete_paths.iter().cloned().collect();
    let mut wholesale = BTreeSet::new();
    if index.entries_sorted {
        for (dir, del_count) in &deleted_under {
            if *del_count == 0 {
                continue;
            }
            let prefix = format!("{dir}/");
            let prefix_bytes = prefix.as_bytes();
            let start = index
                .entries
                .partition_point(|e| e.path.as_slice() < prefix_bytes);
            let mut indexed_count = 0usize;
            let mut all_scheduled = true;
            for entry in index.entries[start..].iter() {
                if !entry.path.starts_with(prefix_bytes) {
                    break;
                }
                if entry.stage() != 0 {
                    continue;
                }
                indexed_count += 1;
                let Ok(path) = std::str::from_utf8(&entry.path) else {
                    all_scheduled = false;
                    break;
                };
                if !delete_path_is_scheduled(path, &delete_set, false) {
                    all_scheduled = false;
                    break;
                }
            }
            if all_scheduled && indexed_count == *del_count {
                wholesale.insert(dir.clone());
            }
        }
        return wholesale;
    }
    let mut indexed_under: BTreeMap<String, usize> = BTreeMap::new();
    let mut blocked: BTreeSet<String> = BTreeSet::new();
    for entry in &index.entries {
        if entry.stage() != 0 {
            continue;
        }
        let Ok(path) = std::str::from_utf8(&entry.path) else {
            continue;
        };
        let Some((dir, _rest)) = path.split_once('/') else {
            continue;
        };
        if !deleted_under.contains_key(dir) {
            continue;
        }
        *indexed_under.entry(dir.to_owned()).or_default() += 1;
        if !delete_path_is_scheduled(path, &delete_set, false) {
            blocked.insert(dir.to_owned());
        }
    }
    for (dir, del_count) in deleted_under {
        if del_count == 0 || blocked.contains(&dir) {
            continue;
        }
        if indexed_under.get(&dir).copied() == Some(del_count) {
            wholesale.insert(dir);
        }
    }
    wholesale
}

/// True when every file under `work_tree`/`dir` is listed in `delete_paths` (Git keeps untracked/ignored extras).
fn delete_path_is_scheduled(rel: &str, delete_paths: &HashSet<String>, ignorecase: bool) -> bool {
    if ignorecase {
        delete_paths
            .iter()
            .any(|del| paths_equal(rel.as_bytes(), del.as_bytes(), true))
    } else {
        delete_paths.contains(rel)
    }
}

/// Untracked and ignored paths collected under wholesale delete trees before switch checkout.
pub(crate) struct WholesaleWorktreeScan<'a> {
    pub untracked: &'a [String],
    pub ignored: &'a [String],
    pub ignorecase: bool,
}

fn wholesale_dir_has_extra_paths(dir: &str, scan: &WholesaleWorktreeScan<'_>) -> bool {
    scan.untracked
        .iter()
        .chain(scan.ignored.iter())
        .any(|path| path_is_under_dir(path, dir, scan.ignorecase))
}

fn path_is_under_dir(path: &str, dir: &str, ignorecase: bool) -> bool {
    let prefix = format!("{dir}/");
    if ignorecase {
        path.eq_ignore_ascii_case(dir)
            || path
                .get(..prefix.len())
                .is_some_and(|head| head.eq_ignore_ascii_case(&prefix))
    } else {
        path == dir || path.starts_with(&prefix)
    }
}

/// Wholesale directories that passed switch prepare safety checks.
#[derive(Debug, Default)]
pub(crate) struct WholesalePrecomputeResult {
    pub safe_directories: BTreeSet<String>,
}

pub(crate) fn wholesale_directory_safe_to_remove(
    work_tree: &Path,
    dir: &str,
    delete_paths: &HashSet<String>,
    ignorecase: bool,
) -> bool {
    let abs_dir = work_tree.join(dir);
    let mut stack = vec![abs_dir];
    while let Some(current) = stack.pop() {
        let read_dir = match fs::read_dir(&current) {
            Ok(rd) => rd,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => {
                let _ = e;
                return false;
            }
        };
        for entry in read_dir.flatten() {
            let path = entry.path();
            let file_type = match entry.file_type() {
                Ok(t) => t,
                Err(_) => return false,
            };
            if file_type.is_dir() {
                stack.push(path);
                continue;
            }
            if !file_type.is_file() && !file_type.is_symlink() {
                return false;
            }
            let Ok(rel) = path.strip_prefix(work_tree) else {
                return false;
            };
            let rel = rel.to_string_lossy();
            if !delete_path_is_scheduled(rel.as_ref(), delete_paths, ignorecase) {
                return false;
            }
        }
    }
    true
}

/// On-disk safety walk for wholesale directory removal, parallelized for switch prepare.
pub(crate) fn precompute_wholesale_safe_directories(
    repo: &Repository,
    work_tree: &Path,
    index: &Index,
    delete_paths: &[String],
    ignorecase: bool,
    worktree_scan: Option<&WholesaleWorktreeScan<'_>>,
    gitlink_deletes: &HashSet<String>,
) -> WholesalePrecomputeResult {
    let candidates = wholesale_deleted_directories(index, delete_paths);
    if candidates.is_empty() {
        return WholesalePrecomputeResult::default();
    }
    if let Some(scan) = worktree_scan {
        let filtered: BTreeSet<String> = candidates
            .into_iter()
            .filter(|dir| !wholesale_dir_has_extra_paths(dir, scan))
            .collect();
        return WholesalePrecomputeResult {
            safe_directories: filter_wholesale_dirs_with_populated_gitlinks(
                work_tree,
                filtered,
                gitlink_deletes,
            ),
        };
    }
    let delete_set: HashSet<String> = delete_paths.iter().cloned().collect();
    let needs_walk: Vec<String> = candidates.into_iter().collect();
    let default_cfg = ConfigSet::new();
    let config = repo.config().ok();
    let cfg = config.as_deref().unwrap_or(&default_cfg);
    let options = repo.thread_policy().resolve(ParallelOp::Checkout, cfg);
    let safe: BTreeSet<String> = if needs_walk.len() >= 2 && options.threads.get() > 1 {
        let wt = work_tree.to_path_buf();
        let delete_set = delete_set.clone();
        let safe_flags = try_par_map(&needs_walk, options, 0, |dir| {
            Ok(wholesale_directory_safe_to_remove(
                &wt,
                dir,
                &delete_set,
                ignorecase,
            ))
        })
        .unwrap_or_else(|_: ParallelHashError<()>| {
            needs_walk
                .iter()
                .map(|dir| {
                    wholesale_directory_safe_to_remove(work_tree, dir, &delete_set, ignorecase)
                })
                .collect()
        });
        needs_walk
            .into_iter()
            .zip(safe_flags)
            .filter_map(|(dir, safe)| safe.then_some(dir))
            .collect()
    } else {
        needs_walk
            .into_iter()
            .filter(|dir| {
                wholesale_directory_safe_to_remove(work_tree, dir, &delete_set, ignorecase)
            })
            .collect()
    };
    WholesalePrecomputeResult {
        safe_directories: filter_wholesale_dirs_with_populated_gitlinks(
            work_tree,
            safe,
            gitlink_deletes,
        ),
    }
}

fn wholesale_deleted_directories_on_disk(
    repo: &Repository,
    work_tree: &Path,
    index: &Index,
    delete_paths: &[String],
    ignorecase: bool,
    apply: &CheckoutApplyOptions,
    gitlink_deletes: &HashSet<String>,
) -> BTreeSet<String> {
    let candidates = wholesale_deleted_directories(index, delete_paths);
    if apply.trust_wholesale_dir_removal {
        return filter_wholesale_dirs_with_populated_gitlinks(
            work_tree,
            candidates,
            gitlink_deletes,
        );
    }
    if apply.wholesale_safety_precomputed {
        let filtered = candidates
            .into_iter()
            .filter(|dir| apply.preverified_wholesale_dirs.contains(dir))
            .collect();
        return filter_wholesale_dirs_with_populated_gitlinks(work_tree, filtered, gitlink_deletes);
    }
    let delete_set: HashSet<String> = delete_paths.iter().cloned().collect();
    let needs_walk: Vec<String> = candidates.into_iter().collect();
    let mut trusted = BTreeSet::new();
    if needs_walk.is_empty() {
        return trusted;
    }
    let config = repo.config().ok();
    let default_cfg = ConfigSet::new();
    let cfg = config.as_deref().unwrap_or(&default_cfg);
    let options = repo.thread_policy().resolve(ParallelOp::Checkout, cfg);
    if needs_walk.len() >= 4 && options.threads.get() > 1 {
        let wt = work_tree.to_path_buf();
        let delete_set = delete_set.clone();
        let safe_flags = try_par_map(&needs_walk, options, 0, |dir| {
            Ok(wholesale_directory_safe_to_remove(
                &wt,
                dir,
                &delete_set,
                ignorecase,
            ))
        })
        .unwrap_or_else(|_: ParallelHashError<()>| {
            needs_walk
                .iter()
                .map(|dir| {
                    wholesale_directory_safe_to_remove(work_tree, dir, &delete_set, ignorecase)
                })
                .collect()
        });
        for (dir, safe) in needs_walk.into_iter().zip(safe_flags) {
            if safe {
                trusted.insert(dir);
            }
        }
    } else {
        for dir in needs_walk {
            if wholesale_directory_safe_to_remove(work_tree, &dir, &delete_set, ignorecase) {
                trusted.insert(dir);
            }
        }
    }
    filter_wholesale_dirs_with_populated_gitlinks(work_tree, trusted, gitlink_deletes)
}

pub(crate) fn path_under_wholesale_directory(
    path: &str,
    wholesale_dirs: &BTreeSet<String>,
) -> bool {
    wholesale_dirs.iter().any(|dir| {
        path == dir.as_str()
            || path
                .strip_prefix(dir.as_str())
                .is_some_and(|rest| rest.starts_with('/'))
    })
}

fn checkout_stored_worktree_abs(work_tree: &Path, stored_path: &str) -> PathBuf {
    crate::repo_relpath::worktree_abs(work_tree, stored_path)
}

fn checkout_stored_index_path_bytes(stored_path: &str) -> Vec<u8> {
    crate::repo_relpath::storage_bytes(stored_path).into_owned()
}

const BULK_ADD_CHECKOUT_MIN: usize = 32;
/// Change count from which a checkout's index write skips the pre-write housekeeping.
const BULK_INDEX_REWRITE_MIN: usize = 64;
const BULK_DELETE_CHECKOUT_MIN: usize = 32;

/// Remove a wholesale-deleted directory; true once it no longer exists. A directory that only
/// held skip-worktree paths is usually absent already.
fn remove_wholesale_dir(abs: &Path) -> bool {
    match fs::remove_dir_all(abs) {
        Ok(()) => true,
        Err(e) => e.kind() == std::io::ErrorKind::NotFound,
    }
}

fn remove_wholesale_directories_on_disk(
    repo: &Repository,
    work_tree: &Path,
    wholesale_index_prefixes: &BTreeSet<String>,
    config: &std::sync::Arc<ConfigSet>,
) -> Result<BTreeSet<String>> {
    let mut wholesale_dirs_removed = BTreeSet::new();
    if wholesale_index_prefixes.is_empty() {
        return Ok(wholesale_dirs_removed);
    }
    if wholesale_index_prefixes.len() >= 2 {
        let policy = repo.thread_policy();
        let options = policy.resolve(ParallelOp::Checkout, config.as_ref());
        let wt = work_tree.to_path_buf();
        let dirs: Vec<String> = wholesale_index_prefixes.iter().cloned().collect();
        let removed = try_par_map(&dirs, options, 0, |dir| {
            let abs = wt.join(dir);
            Ok(remove_wholesale_dir(&abs).then_some(dir.clone()))
        })
        .map_err(|e: ParallelHashError<()>| match e {
            ParallelHashError::Task(()) => {
                Error::PathError("parallel wholesale directory removal failed".into())
            }
        })?;
        wholesale_dirs_removed.extend(removed.into_iter().flatten());
    } else {
        for dir in wholesale_index_prefixes {
            let abs = work_tree.join(dir);
            if remove_wholesale_dir(&abs) {
                wholesale_dirs_removed.insert(dir.clone());
            }
        }
    }
    Ok(wholesale_dirs_removed)
}

#[allow(clippy::too_many_arguments)]
fn checkout_commit_index_update(
    repo: &Repository,
    index_mut: &mut Index,
    wholesale_dirs_removed: &BTreeSet<String>,
    paths_to_remove: &[Vec<u8>],
    mut new_entries: Vec<IndexEntry>,
    skip_worktree_rows: &[IndexEntry],
    bulk_index_rewrite: bool,
    checkout_result_tree: Option<crate::objects::ObjectId>,
    defer_index_disk_write: bool,
) -> Result<()> {
    new_entries.extend_from_slice(skip_worktree_rows);
    let change_count = paths_to_remove.len() + new_entries.len();
    let pre_mtime_max = crate::index::stage0_mtime_max(&index_mut.entries);
    let new_mtime_max = crate::index::stage0_mtime_max(&new_entries);
    let index_mtime_hint = Some(crate::index::max_stage0_mtime_pair(
        pre_mtime_max,
        new_mtime_max,
    ));
    index_mut.defer_untracked_cache_invalidation =
        bulk_index_rewrite || !wholesale_dirs_removed.is_empty();
    if bulk_index_rewrite || !wholesale_dirs_removed.is_empty() {
        if index_mut.defer_untracked_cache_invalidation {
            const CACHE_TREE_PARTIAL_INVALIDATE_MAX: usize = 4096;
            if !wholesale_dirs_removed.is_empty() {
                index_mut.cache_tree = None;
                index_mut.cache_tree_root = None;
            } else if change_count <= CACHE_TREE_PARTIAL_INVALIDATE_MAX {
                index_mut.invalidate_cache_tree_for_bulk_checkout(
                    wholesale_dirs_removed,
                    paths_to_remove.iter().map(|p| p.as_slice()),
                    &new_entries,
                );
            } else {
                index_mut.cache_tree = None;
                index_mut.cache_tree_root = None;
            }
        } else {
            index_mut.drop_derived_index_extensions();
        }
    }
    index_mut.remove_prefixes_paths_and_insert(
        wholesale_dirs_removed,
        paths_to_remove.iter().map(|p| p.as_slice()),
        new_entries,
    );
    index_mut.ensure_canonical_order();
    if let Some(tree) = checkout_result_tree {
        attach_checkout_result_cache_tree(repo, index_mut, &tree)?;
    }
    index_mut.defer_untracked_cache_invalidation = false;
    if defer_index_disk_write {
        return Ok(());
    }
    write_checkout_index(repo, index_mut, bulk_index_rewrite, index_mtime_hint)
}

/// Record that `index_mut` now matches `tree`: keep a still-valid cache tree and point its
/// root at `tree`, or build one from `tree` when the entry counts agree.
fn attach_checkout_result_cache_tree(
    repo: &Repository,
    index_mut: &mut Index,
    tree: &ObjectId,
) -> Result<()> {
    if index_mut.cache_tree.is_some() {
        index_mut.cache_tree_root = Some(*tree);
        return Ok(());
    }
    let cache_tree = crate::write_tree::build_cache_tree_from_tree(repo.odb(), tree)?;
    if usize::try_from(cache_tree.entry_count).ok() == Some(index_mut.entries.len()) {
        index_mut.set_cache_tree(cache_tree);
    }
    Ok(())
}

/// Write the index after a checkout, with fresh stat data trusted (no racy smudging), then
/// move the index file mtime past the newest entry so later status checks stay fast.
fn write_checkout_index(
    repo: &Repository,
    index_mut: &mut Index,
    bulk_index_rewrite: bool,
    index_mtime_hint: Option<(u32, u32)>,
) -> Result<()> {
    index_mut.suppress_pre_write_index_housekeeping = bulk_index_rewrite;
    index_mut.skip_racy_smudge_on_write = true;
    let result = repo.write_index_with_post_index_change(index_mut, true, false);
    index_mut.skip_racy_smudge_on_write = false;
    index_mut.suppress_pre_write_index_housekeeping = false;
    result?;
    let index_path = repo.index_path_for_env()?;
    crate::index::advance_index_file_mtime_past_entries(index_mut, &index_path, index_mtime_hint)?;
    index_mut.source_mtime = crate::index::index_file_mtime(&index_path);
    Ok(())
}

/// Split off the changes that sparse checkout keeps out of the work tree.
///
/// Those paths get no work-tree I/O: deleted ones lose their index row here, and the others get
/// a row in [`CheckoutApplyOptions::skip_worktree_rows`], which the index commit merges after any
/// wholesale directory removal. The row has `skip-worktree` set unless the path is new to the
/// index and a file already sits there: that file is left untouched and tracked, like Git.
/// Returns the changes that still have to be applied to the work tree; when sparse checkout is
/// off this is `changes` itself.
fn apply_sparse_tree_changes_to_index<'a>(
    sparse: Option<&SparseCheckout>,
    work_tree: &Path,
    index: &mut Index,
    changes: &'a [DiffEntry],
    apply: &mut CheckoutApplyOptions,
) -> Cow<'a, [DiffEntry]> {
    let Some(sparse) = sparse else {
        return Cow::Borrowed(changes);
    };
    apply.skip_worktree_rows.clear();
    let skipped_path = |change: &'a DiffEntry| -> Option<&'a str> {
        let path = match change.status {
            DiffStatus::Deleted => change.old_path.as_deref(),
            DiffStatus::Added | DiffStatus::Modified | DiffStatus::TypeChanged => {
                change.new_path.as_deref()
            }
            _ => None,
        }?;
        sparse.checkout_skips(index, path).then_some(path)
    };
    let skipped: Vec<Option<&str>> = changes.iter().map(skipped_path).collect();
    if skipped.iter().all(Option::is_none) {
        return Cow::Borrowed(changes);
    }
    let mut worktree = Vec::with_capacity(changes.len());
    let mut removed: Vec<Vec<u8>> = Vec::new();
    for (change, path) in changes.iter().zip(skipped) {
        let Some(path) = path else {
            worktree.push(change.clone());
            continue;
        };
        if change.status == DiffStatus::Deleted {
            removed.push(checkout_stored_index_path_bytes(path));
            continue;
        }
        let mut entry =
            prospective_index_entry(path, change.new_oid, parse_git_mode(&change.new_mode));
        entry.path = checkout_stored_index_path_bytes(path);
        let untracked_file_present = index.get(&entry.path, 0).is_none()
            && fs::symlink_metadata(checkout_stored_worktree_abs(work_tree, path)).is_ok();
        entry.set_skip_worktree(!untracked_file_present);
        apply.skip_worktree_rows.push(entry);
    }
    if !removed.is_empty() {
        index.remove_prefixes_paths_and_insert(
            &BTreeSet::new(),
            removed.iter().map(Vec::as_slice),
            Vec::new(),
        );
    }
    Cow::Owned(worktree)
}

fn tree_changes_are_all_additions(changes: &[DiffEntry]) -> bool {
    !changes.is_empty() && changes.iter().all(|c| c.status == DiffStatus::Added)
}

fn tree_changes_are_all_modifications(changes: &[DiffEntry]) -> bool {
    !changes.is_empty() && changes.iter().all(|c| c.status == DiffStatus::Modified)
}

fn tree_changes_are_all_deletions(changes: &[DiffEntry]) -> bool {
    !changes.is_empty() && changes.iter().all(|c| c.status == DiffStatus::Deleted)
}

fn tree_changes_are_bulk_mod_add_delete(changes: &[DiffEntry]) -> bool {
    !changes.is_empty()
        && changes.iter().all(|c| {
            matches!(
                c.status,
                DiffStatus::Modified | DiffStatus::Deleted | DiffStatus::Added
            )
        })
}

fn bulk_write_checkout_eligible(changes: &[DiffEntry], rules: &WorktreeRules) -> bool {
    changes.len() >= BULK_ADD_CHECKOUT_MIN
        && changes
            .iter()
            .all(|c| matches!(c.status, DiffStatus::Added | DiffStatus::Modified))
        && changes.iter().all(|change| {
            let Some(path) = change.new_path.as_deref() else {
                return false;
            };
            let mode = parse_git_mode(&change.new_mode);
            mode != MODE_GITLINK
                && is_eligible_for_parallel_checkout(mode, &rules.file_attrs(path, false))
        })
}

fn bulk_mod_delete_checkout_eligible(changes: &[DiffEntry], rules: &WorktreeRules) -> bool {
    if changes.len() < BULK_ADD_CHECKOUT_MIN
        || !tree_changes_are_bulk_mod_add_delete(changes)
        || tree_changes_touch_gitattributes(changes)
    {
        return false;
    }
    let mut saw_write = false;
    for change in changes {
        if !matches!(change.status, DiffStatus::Modified | DiffStatus::Added) {
            continue;
        }
        saw_write = true;
        let Some(path) = change.new_path.as_deref() else {
            return false;
        };
        let mode = parse_git_mode(&change.new_mode);
        if mode == MODE_GITLINK
            || !is_eligible_for_parallel_checkout(mode, &rules.file_attrs(path, false))
        {
            return false;
        }
    }
    saw_write
}

#[allow(clippy::too_many_arguments)]
fn checkout_bulk_added_tree_changes(
    repo: &Repository,
    work_tree: &Path,
    changes: &[DiffEntry],
    index_mut: &mut Index,
    rules: &WorktreeRules,
    config: &std::sync::Arc<ConfigSet>,
    conv: &crlf::ConversionConfig,
    apply: CheckoutApplyOptions,
    precompose: bool,
    ignorecase: bool,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<()> {
    let checkout_smudge_ctx = crate::filter_process::checkout_smudge_context(repo);
    let mut dir_cache = LeadingDirCache::with_protect(ProtectOptions::from_config(config.as_ref()));
    let mut write_jobs: Vec<CheckoutWriteJob> = Vec::with_capacity(changes.len());
    for change in changes {
        crate::progress::poll_cancel(progress, cancel)?;
        let Some(path) = change.new_path.as_deref() else {
            continue;
        };
        let mode = parse_git_mode(&change.new_mode);
        let file_attrs = rules.file_attrs(path, false);
        write_jobs.push(CheckoutWriteJob {
            path: path.to_owned(),
            mode,
            oid: change.new_oid,
            change: change.clone(),
            file_attrs,
            smudge_meta: crate::filter_process::smudge_meta_for_checkout_blob(
                &checkout_smudge_ctx,
                &change.new_oid.to_hex(),
            ),
        });
    }
    let write_paths: Vec<String> = write_jobs.iter().map(|j| j.path.clone()).collect();
    let serial_paths = paths_needing_serial_checkout(&write_paths, ignorecase, precompose);
    let parallel_indices: HashSet<usize> = write_jobs
        .iter()
        .enumerate()
        .filter(|(_, job)| {
            is_eligible_for_parallel_checkout(job.mode, &job.file_attrs)
                && !serial_paths.contains(&job.path)
        })
        .map(|(i, _)| i)
        .collect();
    if write_jobs.len() >= BULK_ADD_CHECKOUT_MIN {
        precreate_checkout_parent_dirs(work_tree, &write_jobs, &mut dir_cache);
    } else {
        for job in &write_jobs {
            dir_cache.ensure_parents(work_tree, &job.path)?;
        }
    }
    let mut delayed = DelayedProcessCheckout::default();
    let write_outcomes = execute_write_jobs(
        repo,
        rules,
        config,
        conv,
        work_tree,
        &mut dir_cache,
        &mut delayed,
        &write_jobs,
        &parallel_indices,
        apply.parallel_prepare_blobs,
        apply.checkout_parallel_threshold,
        progress,
        cancel,
    )?;
    let new_entries = finish_bulk_write_jobs(
        repo,
        rules,
        work_tree,
        &mut dir_cache,
        &mut delayed,
        write_outcomes,
    )?;
    if apply.wholesale_safety_precomputed {
        let _ = remove_wholesale_directories_on_disk(
            repo,
            work_tree,
            &apply.preverified_wholesale_dirs,
            config,
        )?;
    }
    checkout_commit_index_update(
        repo,
        index_mut,
        &apply.preverified_wholesale_dirs,
        &[],
        new_entries,
        &apply.skip_worktree_rows,
        true,
        apply.checkout_result_tree,
        apply.defer_index_disk_write,
    )?;
    progress.finish();
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn checkout_bulk_mod_delete_tree_changes(
    repo: &Repository,
    work_tree: &Path,
    changes: &[DiffEntry],
    index_mut: &mut Index,
    rules: &WorktreeRules,
    config: &std::sync::Arc<ConfigSet>,
    conv: &crlf::ConversionConfig,
    apply: CheckoutApplyOptions,
    precompose: bool,
    ignorecase: bool,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
) -> Result<()> {
    let index: &Index = index_mut;
    let delete_changes: Vec<&DiffEntry> = changes
        .iter()
        .filter(|c| c.status == DiffStatus::Deleted)
        .collect();
    let write_changes: Vec<DiffEntry> = changes
        .iter()
        .filter(|c| matches!(c.status, DiffStatus::Modified | DiffStatus::Added))
        .cloned()
        .collect();
    let delete_paths: Vec<String> = delete_changes
        .iter()
        .filter_map(|c| c.old_path.clone())
        .collect();
    let gitlink_deletes: HashSet<String> = delete_changes
        .iter()
        .filter_map(|c| {
            let path = c.old_path.as_ref()?;
            (parse_git_mode(&c.old_mode) == MODE_GITLINK).then(|| path.clone())
        })
        .collect();
    let owned_wholesale_candidates;
    let wholesale_index_prefixes: &BTreeSet<String> = if apply.wholesale_safety_precomputed {
        &apply.preverified_wholesale_dirs
    } else {
        owned_wholesale_candidates = wholesale_deleted_directories_on_disk(
            repo,
            work_tree,
            index,
            &delete_paths,
            ignorecase,
            &apply,
            &gitlink_deletes,
        );
        &owned_wholesale_candidates
    };

    let checkout_smudge_ctx = crate::filter_process::checkout_smudge_context(repo);
    let mut dir_cache = LeadingDirCache::with_protect(ProtectOptions::from_config(config.as_ref()));
    let mut write_jobs: Vec<CheckoutWriteJob> = Vec::with_capacity(write_changes.len());
    for change in &write_changes {
        let Some(path) = change.new_path.as_deref() else {
            continue;
        };
        let mode = parse_git_mode(&change.new_mode);
        let file_attrs = rules.file_attrs(path, false);
        write_jobs.push(CheckoutWriteJob {
            path: path.to_owned(),
            mode,
            oid: change.new_oid,
            change: change.clone(),
            file_attrs,
            smudge_meta: crate::filter_process::smudge_meta_for_checkout_blob(
                &checkout_smudge_ctx,
                &change.new_oid.to_hex(),
            ),
        });
    }

    let wholesale_dirs_removed =
        remove_wholesale_directories_on_disk(repo, work_tree, wholesale_index_prefixes, config)?;
    let mut deleted_abs_paths: Vec<PathBuf> = wholesale_dirs_removed
        .iter()
        .map(|dir| work_tree.join(dir))
        .collect();
    let mut per_file_deletes: Vec<String> = Vec::new();
    for change in &delete_changes {
        let Some(path) = &change.old_path else {
            continue;
        };
        if parse_git_mode(&change.old_mode) == MODE_GITLINK
            || path_under_wholesale_directory(path, wholesale_index_prefixes)
        {
            continue;
        }
        per_file_deletes.push(path.clone());
    }
    let mut parallel_deleted_paths: HashSet<String> = HashSet::new();
    if per_file_deletes.len() >= PARALLEL_PER_FILE_DELETE_MIN {
        let policy = repo.thread_policy();
        let options = policy.resolve(ParallelOp::Checkout, config.as_ref());
        let (parallel_set, abs_paths) = remove_checkout_files_parallel_cancellable(
            work_tree,
            &per_file_deletes,
            options,
            progress,
            cancel,
        )?;
        deleted_abs_paths.extend(abs_paths);
        parallel_deleted_paths.extend(parallel_set);
    }
    let mut paths_to_remove: Vec<Vec<u8>> = Vec::with_capacity(delete_changes.len());
    for change in &delete_changes {
        crate::progress::poll_cancel(progress, cancel)?;
        let Some(path) = &change.old_path else {
            continue;
        };
        let old_mode = parse_git_mode(&change.old_mode);
        if old_mode == MODE_GITLINK
            || !path_under_wholesale_directory(path, &wholesale_dirs_removed)
        {
            if old_mode == MODE_GITLINK {
                let abs = checkout_stored_worktree_abs(work_tree, path);
                remove_uninitialized_gitlink_worktree(&abs);
                deleted_abs_paths.push(abs);
            } else if !parallel_deleted_paths.contains(path) {
                let abs = checkout_stored_worktree_abs(work_tree, path);
                let _ = fs::remove_file(&abs);
                deleted_abs_paths.push(abs);
            }
        }
        if !path_under_wholesale_directory(path, wholesale_index_prefixes) {
            paths_to_remove.push(checkout_stored_index_path_bytes(path));
        }
        if !parallel_deleted_paths.contains(path) {
            progress.inc(1);
            crate::progress::poll_cancel(progress, cancel)?;
        }
    }
    remove_empty_parent_dirs_batch(work_tree, &deleted_abs_paths);

    let write_paths: Vec<String> = write_jobs.iter().map(|j| j.path.clone()).collect();
    let serial_paths = paths_needing_serial_checkout(&write_paths, ignorecase, precompose);
    let parallel_indices: HashSet<usize> = write_jobs
        .iter()
        .enumerate()
        .filter(|(_, job)| {
            is_eligible_for_parallel_checkout(job.mode, &job.file_attrs)
                && !serial_paths.contains(&job.path)
        })
        .map(|(i, _)| i)
        .collect();
    if write_jobs.len() >= BULK_ADD_CHECKOUT_MIN {
        precreate_checkout_parent_dirs(work_tree, &write_jobs, &mut dir_cache);
    } else {
        for job in &write_jobs {
            dir_cache.ensure_parents(work_tree, &job.path)?;
        }
    }
    let mut delayed = DelayedProcessCheckout::default();
    let write_outcomes = execute_write_jobs(
        repo,
        rules,
        config,
        conv,
        work_tree,
        &mut dir_cache,
        &mut delayed,
        &write_jobs,
        &parallel_indices,
        apply.parallel_prepare_blobs,
        apply.checkout_parallel_threshold,
        progress,
        cancel,
    )?;
    let new_entries = finish_bulk_write_jobs(
        repo,
        rules,
        work_tree,
        &mut dir_cache,
        &mut delayed,
        write_outcomes,
    )?;

    checkout_commit_index_update(
        repo,
        index_mut,
        wholesale_index_prefixes,
        &paths_to_remove,
        new_entries,
        &apply.skip_worktree_rows,
        true,
        apply.checkout_result_tree,
        apply.defer_index_disk_write,
    )?;
    progress.finish();
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn checkout_bulk_deleted_tree_changes(
    repo: &Repository,
    work_tree: &Path,
    changes: &[DiffEntry],
    index_mut: &mut Index,
    wholesale_index_prefixes: &BTreeSet<String>,
    wholesale_dirs_removed: &BTreeSet<String>,
    deleted_abs_paths: &mut Vec<PathBuf>,
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
    apply: &CheckoutApplyOptions,
) -> Result<()> {
    let mut parallel_deleted_paths: HashSet<String> = HashSet::new();
    let mut per_file_deletes: Vec<String> = Vec::new();
    for change in changes {
        crate::progress::poll_cancel(progress, cancel)?;
        if change.status != DiffStatus::Deleted {
            continue;
        }
        let Some(path) = &change.old_path else {
            continue;
        };
        let old_mode = parse_git_mode(&change.old_mode);
        if old_mode == MODE_GITLINK || path_under_wholesale_directory(path, wholesale_dirs_removed)
        {
            continue;
        }
        per_file_deletes.push(path.clone());
    }
    if per_file_deletes.len() >= PARALLEL_PER_FILE_DELETE_MIN {
        let config = repo.config()?;
        let policy = repo.thread_policy();
        let options = policy.resolve(ParallelOp::Checkout, config.as_ref());
        let (parallel_set, abs_paths) = remove_checkout_files_parallel_cancellable(
            work_tree,
            &per_file_deletes,
            options,
            progress,
            cancel,
        )?;
        deleted_abs_paths.extend(abs_paths);
        parallel_deleted_paths.extend(parallel_set);
    }
    let mut paths_to_remove: Vec<Vec<u8>> = Vec::with_capacity(changes.len());
    for change in changes {
        crate::progress::poll_cancel(progress, cancel)?;
        if change.status != DiffStatus::Deleted {
            continue;
        }
        let Some(path) = &change.old_path else {
            continue;
        };
        let old_mode = parse_git_mode(&change.old_mode);
        if old_mode == MODE_GITLINK || !path_under_wholesale_directory(path, wholesale_dirs_removed)
        {
            if old_mode == MODE_GITLINK {
                let abs = checkout_stored_worktree_abs(work_tree, path);
                remove_uninitialized_gitlink_worktree(&abs);
                deleted_abs_paths.push(abs);
            } else if !parallel_deleted_paths.contains(path) {
                let abs = checkout_stored_worktree_abs(work_tree, path);
                let _ = fs::remove_file(&abs);
                deleted_abs_paths.push(abs);
            }
        }
        if !path_under_wholesale_directory(path, wholesale_index_prefixes) {
            paths_to_remove.push(checkout_stored_index_path_bytes(path));
        }
        if !parallel_deleted_paths.contains(path) {
            progress.inc(1);
            crate::progress::poll_cancel(progress, cancel)?;
        }
    }
    remove_empty_parent_dirs_batch(work_tree, deleted_abs_paths);
    checkout_commit_index_update(
        repo,
        index_mut,
        wholesale_index_prefixes,
        &paths_to_remove,
        Vec::new(),
        &apply.skip_worktree_rows,
        true,
        apply.checkout_result_tree,
        apply.defer_index_disk_write,
    )?;
    progress.finish();
    Ok(())
}

#[cfg(test)]
thread_local! {
    static PREFETCH_PROMISOR_FOR_CHECKOUT_CALLS: std::cell::RefCell<usize> =
        const { std::cell::RefCell::new(0) };
}

fn prefetch_promisor_blobs_for_checkout(repo: &Repository, changes: &[DiffEntry]) -> Result<()> {
    #[cfg(test)]
    PREFETCH_PROMISOR_FOR_CHECKOUT_CALLS.with(|c| *c.borrow_mut() += 1);
    let config = repo.config()?;
    if !crate::promisor::repo_treats_promisor_packs(repo.git_dir(), config.as_ref()) {
        return Ok(());
    }
    if !crate::promisor_fetch::promisor_lazy_fetch_enabled(config.as_ref(), repo.environment()) {
        return Ok(());
    }
    if repo.caches().promisor_fetcher().is_none() {
        return Ok(());
    }
    let odb = repo.odb();
    let mut oids = Vec::new();
    for change in changes {
        if change.status == DiffStatus::Deleted {
            continue;
        }
        let mode = parse_git_mode(&change.new_mode);
        if mode != MODE_REGULAR && mode != MODE_EXECUTABLE && mode != MODE_SYMLINK {
            continue;
        }
        if change.new_oid.is_zero() || odb.try_exists_stored(&change.new_oid)? {
            continue;
        }
        oids.push(change.new_oid);
    }
    if oids.is_empty() {
        return Ok(());
    }
    repo.prefetch_promised(&oids)
}

fn checkout_tree_changes_with_progress(
    repo: &Repository,
    changes: &[DiffEntry],
    progress: &mut dyn ProgressSink,
    cancel: &dyn Cancel,
    reuse_index: Option<&mut Index>,
    apply: CheckoutApplyOptions,
    attribute_changes: Option<&[DiffEntry]>,
) -> Result<()> {
    let mut progress = CancelProgress::new(progress, cancel);
    let work_tree = repo
        .work_tree()
        .ok_or_else(|| Error::PathError("cannot update a bare repository's working tree".into()))?;

    let mut loaded_index;
    let index_mut: &mut Index = if let Some(index) = reuse_index {
        index
    } else {
        loaded_index = repo.load_index()?;
        &mut loaded_index
    };
    let config = repo.config()?;
    let protect = ProtectOptions::from_config(config.as_ref());
    validate_diff_changes_for_worktree(changes, protect)?;
    let mut apply = apply;
    let sparse = if apply.sparse_paths_applied {
        None
    } else {
        SparseCheckout::load(repo)?
    };
    let worktree_changes = apply_sparse_tree_changes_to_index(
        sparse.as_ref(),
        work_tree,
        index_mut,
        changes,
        &mut apply,
    );
    let changes: &[DiffEntry] = &worktree_changes;

    prefetch_promisor_blobs_for_checkout(repo, changes)?;
    if let Some(extra) = attribute_changes {
        validate_diff_changes_for_worktree(extra, protect)?;
    }
    let ignorecase = config
        .get_bool("core.ignorecase")
        .and_then(|r| r.ok())
        .unwrap_or(false);
    let precompose = repo.pathspec_precompose_enabled();
    let attr_changes = attribute_changes.unwrap_or(changes);

    progress.start(
        ProgressPhase::CheckingOutFiles,
        Some(u64::try_from(changes.len()).unwrap_or(u64::MAX)),
    );

    if !apply.skip_bulk_checkout_fast_path {
        let precomputed_rules = apply.precomputed_worktree_rules.clone();
        let owned_bulk_rules;
        let bulk_rules: &WorktreeRules = if let Some(ref precomputed) = precomputed_rules {
            precomputed.as_ref()
        } else {
            owned_bulk_rules =
                checkout_worktree_rules(repo, index_mut, attr_changes, config.clone())?;
            &owned_bulk_rules
        };
        let conv = bulk_rules.conversion().clone();

        if bulk_mod_delete_checkout_eligible(changes, bulk_rules) {
            checkout_bulk_mod_delete_tree_changes(
                repo,
                work_tree,
                changes,
                index_mut,
                bulk_rules,
                &config,
                &conv,
                apply,
                precompose,
                ignorecase,
                &mut progress,
                cancel,
            )?;
            return progress.finish_poll();
        }
        if bulk_write_checkout_eligible(changes, bulk_rules)
            && (tree_changes_are_all_additions(changes)
                || tree_changes_are_all_modifications(changes))
        {
            checkout_bulk_added_tree_changes(
                repo,
                work_tree,
                changes,
                index_mut,
                bulk_rules,
                &config,
                &conv,
                apply,
                precompose,
                ignorecase,
                &mut progress,
                cancel,
            )?;
            return progress.finish_poll();
        }
        if changes.len() >= BULK_DELETE_CHECKOUT_MIN && tree_changes_are_all_deletions(changes) {
            let index: &Index = index_mut;
            let delete_paths: Vec<String> = changes
                .iter()
                .filter(|c| c.status == DiffStatus::Deleted)
                .filter_map(|c| c.old_path.clone())
                .collect();
            let gitlink_deletes: HashSet<String> = changes
                .iter()
                .filter(|c| c.status == DiffStatus::Deleted)
                .filter_map(|c| {
                    let path = c.old_path.as_ref()?;
                    (parse_git_mode(&c.old_mode) == MODE_GITLINK).then(|| path.clone())
                })
                .collect();
            let wholesale_candidates = if apply.wholesale_safety_precomputed {
                apply.preverified_wholesale_dirs.clone()
            } else {
                wholesale_deleted_directories_on_disk(
                    repo,
                    work_tree,
                    index,
                    &delete_paths,
                    ignorecase,
                    &apply,
                    &gitlink_deletes,
                )
            };
            let wholesale_index_prefixes = wholesale_candidates;
            let mut wholesale_dirs_removed = BTreeSet::new();
            let mut deleted_abs_paths: Vec<PathBuf> = Vec::new();
            if !wholesale_index_prefixes.is_empty() {
                let policy = repo.thread_policy();
                let options = policy.resolve(ParallelOp::Checkout, config.as_ref());
                let dirs: Vec<String> = wholesale_index_prefixes.iter().cloned().collect();
                for (dir, abs) in remove_wholesale_dirs_parallel_cancellable(
                    work_tree,
                    &dirs,
                    options,
                    &mut progress,
                    cancel,
                )? {
                    wholesale_dirs_removed.insert(dir);
                    deleted_abs_paths.push(abs);
                }
            }
            checkout_bulk_deleted_tree_changes(
                repo,
                work_tree,
                changes,
                index_mut,
                &wholesale_index_prefixes,
                &wholesale_dirs_removed,
                &mut deleted_abs_paths,
                &mut progress,
                cancel,
                &apply,
            )?;
            return progress.finish_poll();
        }
    }

    let index: &Index = index_mut;
    let checkout_smudge_ctx = crate::filter_process::checkout_smudge_context(repo);
    let mut paths_to_remove: Vec<Vec<u8>> = Vec::with_capacity(changes.len() / 8);
    let mut deleted_abs_paths: Vec<PathBuf> = Vec::with_capacity(changes.len() / 8);
    let mut dir_cache = LeadingDirCache::with_protect(protect);
    let mut plan: Vec<CheckoutPlanSlot> = Vec::with_capacity(changes.len());
    let mut write_jobs: Vec<CheckoutWriteJob> = Vec::new();
    let mut rules: Option<Rc<WorktreeRules>> =
        apply.precomputed_worktree_rules.as_ref().map(Rc::clone);
    let mut conv: Option<ConversionConfig> = None;
    let mut delayed = DelayedProcessCheckout::default();
    let mut pending_delayed: Vec<DelayedCheckoutTarget> = Vec::new();
    let attr_changes = attribute_changes.unwrap_or(changes);
    let precompose = repo.pathspec_precompose_enabled();

    let delete_paths: Vec<String> = changes
        .iter()
        .filter(|c| c.status == DiffStatus::Deleted)
        .filter_map(|c| c.old_path.clone())
        .collect();
    let gitlink_deletes: HashSet<String> = changes
        .iter()
        .filter(|c| c.status == DiffStatus::Deleted)
        .filter_map(|c| {
            let path = c.old_path.as_ref()?;
            (parse_git_mode(&c.old_mode) == MODE_GITLINK).then(|| path.clone())
        })
        .collect();
    let wholesale_candidates = if apply.wholesale_safety_precomputed {
        apply.preverified_wholesale_dirs.clone()
    } else {
        wholesale_deleted_directories_on_disk(
            repo,
            work_tree,
            index,
            &delete_paths,
            ignorecase,
            &apply,
            &gitlink_deletes,
        )
    };
    let wholesale_index_prefixes = wholesale_candidates;
    let mut wholesale_dirs_removed = BTreeSet::new();
    if !wholesale_index_prefixes.is_empty() {
        let policy = repo.thread_policy();
        let options = policy.resolve(ParallelOp::Checkout, config.as_ref());
        let dirs: Vec<String> = wholesale_index_prefixes.iter().cloned().collect();
        for (dir, abs) in remove_wholesale_dirs_parallel_cancellable(
            work_tree,
            &dirs,
            options,
            &mut progress,
            cancel,
        )? {
            wholesale_dirs_removed.insert(dir);
            deleted_abs_paths.push(abs);
        }
    }

    let mut parallel_deleted_paths: HashSet<String> = HashSet::new();
    let mut per_file_deletes: Vec<String> = Vec::new();
    for change in changes {
        if change.status != DiffStatus::Deleted {
            continue;
        }
        let Some(path) = &change.old_path else {
            continue;
        };
        let old_mode = parse_git_mode(&change.old_mode);
        if old_mode == MODE_GITLINK || path_under_wholesale_directory(path, &wholesale_dirs_removed)
        {
            continue;
        }
        per_file_deletes.push(path.clone());
    }
    if per_file_deletes.len() >= PARALLEL_PER_FILE_DELETE_MIN {
        let policy = repo.thread_policy();
        let options = policy.resolve(ParallelOp::Checkout, config.as_ref());
        let (parallel_set, abs_paths) = remove_checkout_files_parallel_cancellable(
            work_tree,
            &per_file_deletes,
            options,
            &mut progress,
            cancel,
        )?;
        deleted_abs_paths.extend(abs_paths);
        parallel_deleted_paths.extend(parallel_set);
    }

    {
        crate::trace::trace_span!("grit.checkout.plan");
        for change in changes {
            if change.status == DiffStatus::Deleted {
                crate::progress::poll_cancel(&mut progress, cancel)?;
                if let Some(path) = &change.old_path {
                    let old_mode = parse_git_mode(&change.old_mode);
                    if old_mode == MODE_GITLINK
                        || !path_under_wholesale_directory(path, &wholesale_dirs_removed)
                    {
                        if old_mode == MODE_GITLINK {
                            let abs = checkout_stored_worktree_abs(work_tree, path);
                            remove_uninitialized_gitlink_worktree(&abs);
                            deleted_abs_paths.push(abs);
                        } else if !parallel_deleted_paths.contains(path) {
                            let abs = checkout_stored_worktree_abs(work_tree, path);
                            let _ = fs::remove_file(&abs);
                            deleted_abs_paths.push(abs);
                        }
                    }
                    if !path_under_wholesale_directory(path, &wholesale_dirs_removed) {
                        paths_to_remove.push(checkout_stored_index_path_bytes(path));
                    }
                }
                if change
                    .old_path
                    .as_ref()
                    .is_none_or(|path| !parallel_deleted_paths.contains(path))
                {
                    progress.inc(1);
                    crate::progress::poll_cancel(&mut progress, cancel)?;
                }
                continue;
            }

            let Some(path) = &change.new_path else {
                progress.inc(1);
                continue;
            };
            if rules.is_none() {
                rules = Some(Rc::new(checkout_worktree_rules(
                    repo,
                    index,
                    attr_changes,
                    config.clone(),
                )?));
            }
            let rules = rules.as_deref().ok_or_else(|| {
                Error::PathError("internal error: missing checkout rules for write".into())
            })?;
            if conv.is_none() {
                conv = Some(rules.conversion().clone());
            }
            let mode = parse_git_mode(&change.new_mode);
            if mode == MODE_GITLINK {
                let index_path = checkout_stored_index_path_bytes(path);
                let abs = checkout_stored_worktree_abs(work_tree, path);
                if let Some(existing) = index.get(&index_path, 0) {
                    if existing.oid == change.new_oid
                        && existing.mode == MODE_GITLINK
                        && gitlink_worktree_matches_index(&abs)
                    {
                        let meta = fs::symlink_metadata(&abs).map_err(Error::Io)?;
                        let mut entry =
                            entry_from_metadata(&meta, &index_path, change.new_oid, MODE_GITLINK);
                        entry.size = 0;
                        plan.push(CheckoutPlanSlot::Ready(entry));
                        progress.inc(1);
                        continue;
                    }
                }
                materialize_gitlink_worktree(work_tree, path, &mut dir_cache)?;
                let mut entry = entry_from_stat(&abs, &index_path, change.new_oid, MODE_GITLINK)?;
                entry.size = 0;
                plan.push(CheckoutPlanSlot::Ready(entry));
                progress.inc(1);
                continue;
            }
            let index_path = checkout_stored_index_path_bytes(path);
            let abs = checkout_stored_worktree_abs(work_tree, path);
            if let Some(existing) = index.get(&index_path, 0) {
                if crate::diff::path_checkout_skip_blob_write_when_up_to_date_with_config(
                    repo.odb(),
                    repo.git_dir(),
                    work_tree,
                    index,
                    existing,
                    &change.new_oid,
                    mode,
                    path,
                    Some(rules.filter_process()),
                    Some(config.as_ref()),
                )? {
                    let meta = fs::symlink_metadata(&abs).map_err(Error::Io)?;
                    plan.push(CheckoutPlanSlot::Ready(entry_from_metadata(
                        &meta,
                        &index_path,
                        change.new_oid,
                        mode,
                    )));
                    progress.inc(1);
                    continue;
                }
            }
            let file_attrs = rules.file_attrs(path, false);
            let smudge_meta = crate::filter_process::smudge_meta_for_checkout_blob(
                &checkout_smudge_ctx,
                &change.new_oid.to_hex(),
            );
            write_jobs.push(CheckoutWriteJob {
                path: path.clone(),
                mode,
                oid: change.new_oid,
                change: change.clone(),
                file_attrs,
                smudge_meta,
            });
            plan.push(CheckoutPlanSlot::Write);
        }
    }

    let write_paths: Vec<String> = write_jobs.iter().map(|j| j.path.clone()).collect();
    let serial_paths = paths_needing_serial_checkout(&write_paths, ignorecase, precompose);
    let parallel_indices: HashSet<usize> = write_jobs
        .iter()
        .enumerate()
        .filter(|(_, job)| {
            is_eligible_for_parallel_checkout(job.mode, &job.file_attrs)
                && !serial_paths.contains(&job.path)
        })
        .map(|(i, _)| i)
        .collect();

    if write_jobs.len() >= 64 {
        precreate_checkout_parent_dirs(work_tree, &write_jobs, &mut dir_cache);
    } else {
        for job in &write_jobs {
            dir_cache.ensure_parents(work_tree, &job.path)?;
        }
    }

    let write_outcomes = if write_jobs.is_empty() {
        Vec::new()
    } else {
        let rules = rules.as_ref().ok_or_else(|| {
            Error::PathError("internal error: missing checkout rules for write jobs".into())
        })?;
        let conv = conv.as_ref().ok_or_else(|| {
            Error::PathError("internal error: missing conversion config for write jobs".into())
        })?;
        crate::trace::trace_span!("grit.checkout.write");
        execute_write_jobs(
            repo,
            rules,
            &config,
            conv,
            work_tree,
            &mut dir_cache,
            &mut delayed,
            &write_jobs,
            &parallel_indices,
            apply.parallel_prepare_blobs,
            apply.checkout_parallel_threshold,
            &mut progress,
            cancel,
        )?
    };

    let mut new_entries: Vec<IndexEntry> = Vec::with_capacity(plan.len());
    let mut write_iter = write_outcomes.into_iter();
    for slot in plan {
        match slot {
            CheckoutPlanSlot::Ready(entry) => new_entries.push(entry),
            CheckoutPlanSlot::Write => {
                match write_iter.next().ok_or_else(|| {
                    Error::PathError("internal error: missing parallel checkout outcome".into())
                })? {
                    WriteJobOutcome::Index(entry) => {
                        new_entries.push(entry);
                    }
                    WriteJobOutcome::Delayed(target) => {
                        pending_delayed.push(target);
                    }
                }
            }
        }
    }

    if !pending_delayed.is_empty() {
        let rules = rules.as_ref().ok_or_else(|| {
            Error::PathError("internal error: missing checkout rules for delayed writes".into())
        })?;
        finish_delayed_checkouts(
            repo,
            rules,
            work_tree,
            &mut delayed,
            &pending_delayed,
            &mut dir_cache,
            &mut new_entries,
        )?;
    }

    remove_empty_parent_dirs_batch(work_tree, &deleted_abs_paths);
    drop(rules);
    let bulk_index_rewrite = changes.len() >= BULK_INDEX_REWRITE_MIN;
    checkout_commit_index_update(
        repo,
        index_mut,
        &wholesale_dirs_removed,
        &paths_to_remove,
        new_entries,
        &apply.skip_worktree_rows,
        bulk_index_rewrite,
        apply.checkout_result_tree,
        apply.defer_index_disk_write,
    )?;
    progress.finish();
    progress.finish_poll()
}

#[derive(Clone)]
struct DelayedCheckoutTarget {
    path: String,
    mode: u32,
    oid: ObjectId,
    change: DiffEntry,
}

/// Attribute/conversion context for checkout smudge.
fn checkout_worktree_rules(
    repo: &Repository,
    index: &Index,
    attr_changes: &[DiffEntry],
    config: std::sync::Arc<ConfigSet>,
) -> Result<WorktreeRules> {
    if tree_changes_touch_gitattributes(attr_changes) {
        let prospective_index = prospective_index_for_checkout(index, attr_changes);
        WorktreeRules::from_checkout_prospective_index(repo, &prospective_index, config)
    } else {
        WorktreeRules::from_parts_index_attributes_only(repo, index, config)
    }
}

pub(crate) fn tree_changes_touch_gitattributes(changes: &[DiffEntry]) -> bool {
    use std::ffi::OsStr;
    use std::path::Path;
    changes.iter().any(|change| {
        [change.old_path.as_deref(), change.new_path.as_deref()]
            .into_iter()
            .flatten()
            .any(|path| Path::new(path).file_name() == Some(OsStr::new(".gitattributes")))
    })
}

/// Index after applying `changes`, used to resolve destination `.gitattributes` before writes land.
fn prospective_index_for_checkout(index: &Index, changes: &[DiffEntry]) -> Index {
    let mut idx = index.clone();
    for change in changes {
        if change.status == DiffStatus::Deleted {
            if let Some(path) = &change.old_path {
                idx.remove(path.as_bytes());
            }
            continue;
        }
        let Some(path) = &change.new_path else {
            continue;
        };
        if let Some(old) = &change.old_path {
            if old != path {
                idx.remove(old.as_bytes());
            }
        }
        let mode = parse_git_mode(&change.new_mode);
        idx.add_or_replace(prospective_index_entry(path, change.new_oid, mode));
    }
    idx.sort();
    idx
}

fn prospective_index_entry(path: &str, oid: ObjectId, mode: u32) -> IndexEntry {
    IndexEntry {
        ctime_sec: 0,
        ctime_nsec: 0,
        mtime_sec: 0,
        mtime_nsec: 0,
        dev: 0,
        ino: 0,
        mode,
        uid: 0,
        gid: 0,
        size: 0,
        oid,
        flags: path.len().min(0xFFF) as u16,
        flags_extended: None,
        path: path.as_bytes().to_vec(),
        base_index_pos: 0,
    }
}

/// Index blob bytes after checkout smudge (EOL, encoding, ident, filters).
///
/// Symlink targets are returned unchanged. When a process filter returns `status=delayed`,
/// returns `Ok(None)` and records the path in `delayed_checkout`; the checkout driver completes
/// those paths via [`DelayedProcessCheckout::finish`].
/// Smudge index-format blob bytes for a worktree path (EOL, filters, ident).
///
/// Returns symlink targets unchanged. Used when writing merge/stash conflict markers
/// and other blobs that are stored LF-normalized in the object database.
pub(crate) fn smudge_bytes_for_worktree_path(
    repo: &Repository,
    rules: &WorktreeRules,
    rel_path: &str,
    mode: u32,
    blob: &[u8],
    oid: &ObjectId,
) -> Result<Vec<u8>> {
    if mode == MODE_SYMLINK {
        return Ok(blob.to_vec());
    }
    match worktree_bytes_from_index_blob(repo, rules, rel_path, mode, blob, oid, None, None)? {
        Some(bytes) => Ok(bytes),
        None => Err(Error::PathError(format!(
            "delayed filter checkout is not supported for '{rel_path}'"
        ))),
    }
}

/// Write a blob from the object database to the worktree with checkout smudge rules.
pub(crate) fn write_smudged_blob_to_worktree(
    repo: &Repository,
    work_tree: &Path,
    rules: &WorktreeRules,
    rel_path: &str,
    oid: &ObjectId,
    mode: u32,
) -> Result<()> {
    let written = write_blob_to_worktree(
        repo,
        rules,
        work_tree,
        rel_path,
        oid,
        mode,
        &mut LeadingDirCache::new(),
    )?;
    if written.is_none() {
        return Err(Error::PathError(format!(
            "delayed filter checkout is not supported for '{rel_path}'"
        )));
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn worktree_bytes_from_index_blob(
    repo: &Repository,
    rules: &WorktreeRules,
    rel_path: &str,
    mode: u32,
    blob: &[u8],
    oid: &ObjectId,
    smudge_meta: Option<&crate::filter_process::FilterSmudgeMeta>,
    delayed_checkout: Option<&mut DelayedProcessCheckout>,
) -> Result<Option<Vec<u8>>> {
    if mode == MODE_SYMLINK {
        return Ok(Some(blob.to_vec()));
    }
    let oid_hex = oid.to_string();
    let smudge_meta_owned;
    let smudge_meta = if let Some(meta) = smudge_meta {
        meta
    } else {
        smudge_meta_owned = crate::filter_process::smudge_meta_for_checkout(repo, &oid_hex);
        &smudge_meta_owned
    };
    let conv = rules.conversion();
    let file_attrs = rules.file_attrs(rel_path, false);
    crlf::convert_to_worktree(
        blob,
        rel_path,
        conv,
        &file_attrs,
        Some(&oid_hex),
        Some(smudge_meta),
        delayed_checkout,
        Some(rules.filter_process()),
    )
    .map_err(Error::from)
}

fn finish_bulk_write_jobs(
    repo: &Repository,
    rules: &WorktreeRules,
    work_tree: &Path,
    dir_cache: &mut LeadingDirCache,
    delayed: &mut DelayedProcessCheckout,
    write_outcomes: Vec<WriteJobOutcome>,
) -> Result<Vec<IndexEntry>> {
    let mut new_entries = Vec::with_capacity(write_outcomes.len());
    let mut pending_delayed = Vec::new();
    for outcome in write_outcomes {
        match outcome {
            WriteJobOutcome::Index(entry) => {
                new_entries.push(entry);
            }
            WriteJobOutcome::Delayed(target) => {
                pending_delayed.push(target);
            }
        }
    }
    finish_delayed_checkouts(
        repo,
        rules,
        work_tree,
        delayed,
        &pending_delayed,
        dir_cache,
        &mut new_entries,
    )?;
    Ok(new_entries)
}

#[allow(clippy::too_many_arguments)]
fn finish_delayed_checkouts(
    repo: &Repository,
    rules: &WorktreeRules,
    work_tree: &Path,
    delayed: &mut DelayedProcessCheckout,
    pending: &[DelayedCheckoutTarget],
    dir_cache: &mut LeadingDirCache,
    new_entries: &mut Vec<IndexEntry>,
) -> Result<()> {
    if delayed.entries.is_empty() {
        return Ok(());
    }
    let finish_result = delayed.finish(
        rules.filter_process(),
        |path, meta| smudge_retry_after_delay(repo, rules, path, meta),
        |path, data| {
            let target = pending.iter().find(|t| t.path == path).ok_or_else(|| {
                format!("internal error: delayed checkout for unknown path '{path}'")
            })?;
            let entry = write_checkout_entry(
                work_tree,
                path,
                WorktreeFileContent::Bytes(data),
                target.mode,
                &target.change,
                dir_cache,
                target.oid,
            )
            .map_err(|e| e.to_string())?;
            new_entries.push(entry);
            Ok(())
        },
    );
    match finish_result {
        Ok(()) => Ok(()),
        Err(DelayedCheckoutError::Reported(problems)) => {
            let msg = problems
                .iter()
                .map(|p| match p {
                    crate::filter_process::DelayedCheckoutProblem::FilterSignaledAvailable {
                        filter_cmd,
                        path,
                    } => format!(
                        "external filter '{filter_cmd}' signaled that '{path}' is now available although it has not been delayed earlier"
                    ),
                    crate::filter_process::DelayedCheckoutProblem::PathNotFilteredProperly {
                        path,
                    } => format!("'{path}' was not filtered properly"),
                })
                .collect::<Vec<_>>()
                .join("; ");
            Err(Error::PathError(msg))
        }
        Err(DelayedCheckoutError::Transport(msg)) => Err(Error::PathError(msg)),
    }
}

fn smudge_retry_after_delay(
    repo: &Repository,
    rules: &WorktreeRules,
    rel_path: &str,
    meta: &FilterSmudgeMeta,
) -> std::result::Result<Vec<u8>, String> {
    let blob_hex = meta
        .blob_hex
        .as_deref()
        .ok_or_else(|| format!("missing blob id for delayed checkout of '{rel_path}'"))?;
    let oid = ObjectId::from_hex(blob_hex)
        .map_err(|e| format!("invalid blob id for delayed checkout of '{rel_path}': {e}"))?;
    let obj = repo
        .odb()
        .read(&oid)
        .map_err(|e| format!("reading blob for delayed checkout of '{rel_path}': {e}"))?;
    // Git `CE_RETRY`: run smudge again without allowing delay (filter serves cached output).
    crlf::convert_to_worktree_eager(
        &obj.data,
        rel_path,
        rules.conversion(),
        &rules.file_attrs(rel_path, false),
        Some(blob_hex),
        Some(meta),
        Some(rules.filter_process()),
    )
    .map_err(|e| e.to_string())
}

/// Build a modified diff for rewriting an unchanged blob, using on-disk mode as `old_mode`.
fn diff_entry_for_worktree_blob_refresh(
    work_tree: &Path,
    rel_path: &str,
    oid: ObjectId,
    target_mode: u32,
) -> DiffEntry {
    let abs = work_tree.join(rel_path);
    let old_mode = fs::symlink_metadata(&abs)
        .ok()
        .filter(|meta| meta.is_file())
        .map(|meta| crate::diff::mode_from_metadata(&meta))
        .unwrap_or(target_mode);
    DiffEntry {
        status: DiffStatus::Modified,
        old_path: Some(rel_path.to_owned()),
        new_path: Some(rel_path.to_owned()),
        old_mode: format_mode(old_mode),
        new_mode: format_mode(target_mode),
        old_oid: oid,
        new_oid: oid,
        score: None,
    }
}

fn parse_git_mode(mode: &str) -> u32 {
    u32::from_str_radix(mode, 8).unwrap_or(MODE_REGULAR)
}

fn validate_diff_changes_for_worktree(
    changes: &[DiffEntry],
    protect: ProtectOptions,
) -> Result<()> {
    for change in changes {
        let Some(path) = &change.new_path else {
            continue;
        };
        let mode = parse_git_mode(&change.new_mode);
        validate_worktree_path(path.as_bytes(), mode, protect)?;
    }
    Ok(())
}

/// True when `path` is a gitlink directory with an initialized submodule (`.git` file or dir).
fn gitlink_worktree_is_populated(path: &Path) -> bool {
    if !path.is_dir() {
        return false;
    }
    let git_meta = path.join(".git");
    git_meta.is_file() || git_meta.is_dir()
}

/// Drop wholesale-removal candidates when a deleted gitlink under `dir` still has a worktree.
/// Index-derived wholesale directory candidates when switch prepare already trusts removal.
pub(crate) fn trusted_wholesale_directories_for_switch(
    work_tree: &Path,
    index: &Index,
    delete_paths: &[String],
    gitlink_deletes: &HashSet<String>,
) -> BTreeSet<String> {
    let candidates = wholesale_deleted_directories(index, delete_paths);
    filter_wholesale_dirs_with_populated_gitlinks(work_tree, candidates, gitlink_deletes)
}

fn filter_wholesale_dirs_with_populated_gitlinks(
    work_tree: &Path,
    dir_candidates: BTreeSet<String>,
    gitlink_deletes: &HashSet<String>,
) -> BTreeSet<String> {
    if dir_candidates.is_empty() || gitlink_deletes.is_empty() {
        return dir_candidates;
    }
    dir_candidates
        .into_iter()
        .filter(|dir| {
            let prefix = format!("{dir}/");
            !gitlink_deletes.iter().any(|path| {
                (path == dir || path.starts_with(&prefix))
                    && gitlink_worktree_is_populated(&work_tree.join(path))
            })
        })
        .collect()
}

/// True when `path` is an uninitialized submodule checkout (directory, no nested `.git`).
fn gitlink_worktree_matches_index(path: &Path) -> bool {
    if !path.is_dir() {
        return false;
    }
    !gitlink_worktree_is_populated(path)
}

/// Remove a gitlink path from the worktree unless it looks like an initialized submodule.
fn remove_uninitialized_gitlink_worktree(abs: &Path) {
    if abs.is_dir() {
        let git_meta = abs.join(".git");
        if git_meta.is_file() || git_meta.is_dir() {
            return;
        }
        let _ = fs::remove_dir_all(abs);
    } else {
        let _ = fs::remove_file(abs);
    }
}

/// Create an empty directory for an uninitialized gitlink, replacing conflicting paths.
fn materialize_gitlink_worktree(
    work_tree: &Path,
    rel_path: &str,
    dir_cache: &mut LeadingDirCache,
) -> Result<()> {
    dir_cache.ensure_parents(work_tree, rel_path)?;
    let file_path = crate::repo_relpath::worktree_abs(work_tree, rel_path);
    if let Ok(meta) = fs::symlink_metadata(&file_path) {
        if meta.file_type().is_symlink() || meta.is_file() {
            fs::remove_file(&file_path)?;
        } else if meta.is_dir() {
            let git_meta = file_path.join(".git");
            if !(git_meta.is_file() || git_meta.is_dir()) {
                fs::remove_dir_all(&file_path)?;
            }
        }
    }
    fs::create_dir_all(&file_path)
        .map_err(|e| Error::PathError(format!("creating gitlink directory '{rel_path}': {e}")))?;
    Ok(())
}

fn is_regular_git_mode(mode: u32) -> bool {
    mode == MODE_REGULAR || mode == MODE_EXECUTABLE
}

fn can_overwrite_regular_in_place(change: &DiffEntry) -> bool {
    change.status == DiffStatus::Modified
        && is_regular_git_mode(parse_git_mode(&change.old_mode))
        && is_regular_git_mode(parse_git_mode(&change.new_mode))
}

/// Blobs larger than this are streamed from the object database into the work tree when
/// checkout copies them verbatim, so checkout memory stays flat however large a file is.
const STREAMED_CHECKOUT_LIMIT: u64 = 1024 * 1024;

/// Buffer size for copying a streamed blob into a work-tree file.
const STREAMED_CHECKOUT_COPY_BUF: usize = 256 * 1024;

/// Contents of one work-tree file being checked out.
pub(crate) enum WorktreeFileContent<'a> {
    /// Bytes already in memory, after any checkout conversion.
    Bytes(&'a [u8]),
    /// A blob payload copied verbatim from an object-database stream (regular files only).
    ///
    /// The stream is spooled to a temporary file next to the destination, which is renamed
    /// into place only once the reader has reached its validated end: a corrupt or truncated
    /// object fails the checkout without leaving a partial file or disturbing the old one.
    Stream(&'a mut dyn Read),
}

/// Copy `reader` into a new temporary file in `dir`, created with the checkout mode for `mode`.
///
/// The temporary file is removed again when reading or writing fails, including when the
/// reader reports a corrupt or truncated object at its end.
fn spool_stream_to_temp(
    dir: &Path,
    rel_path: &str,
    reader: &mut dyn Read,
    mode: u32,
) -> Result<tempfile::NamedTempFile> {
    let write_error = |e: std::io::Error| {
        crate::worktree::map_worktree_io(rel_path, WorktreePathOperation::Replace, e)
    };
    let mut builder = tempfile::Builder::new();
    builder.prefix(".grit-checkout-");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        builder.permissions(fs::Permissions::from_mode(git_create_mode_for_index_mode(
            mode,
        )));
    }
    #[cfg(not(unix))]
    let _ = mode;
    let mut tmp = builder.tempfile_in(dir).map_err(write_error)?;
    let mut buf = vec![0u8; STREAMED_CHECKOUT_COPY_BUF];
    loop {
        let n = match reader.read(&mut buf) {
            Ok(0) => return Ok(tmp),
            Ok(n) => n,
            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(e) => return Err(Error::Io(e)),
        };
        tmp.as_file_mut()
            .write_all(&buf[..n])
            .map_err(write_error)?;
    }
}

/// Read blob `oid` for checkout, or return `None` when it should be streamed instead.
///
/// A regular file whose checkout conversion is the identity
/// ([`crlf::worktree_conversion_is_identity`]) and whose blob is larger than
/// [`STREAMED_CHECKOUT_LIMIT`] is left for [`stream_checkout_entry`]; everything else is read
/// whole. Objects within the limit are read in that one lookup; a streamed object is opened
/// again by [`stream_checkout_entry`].
pub(crate) fn read_blob_unless_streamed(
    repo: &Repository,
    conv: &crlf::ConversionConfig,
    attrs: &crlf::FileAttrs,
    mode: u32,
    oid: &ObjectId,
) -> Result<Option<Object>> {
    if !is_regular_git_mode(mode) || !crlf::worktree_conversion_is_identity(conv, attrs) {
        return repo.odb().read(oid).map(Some);
    }
    match repo
        .odb()
        .read_unless_larger(oid, STREAMED_CHECKOUT_LIMIT)?
    {
        BoundedRead::Object(object) => Ok(Some(object)),
        BoundedRead::Larger => Ok(None),
    }
}

/// Check out blob `oid` at `rel_path` by streaming it straight into the work tree.
///
/// For blobs [`read_blob_unless_streamed`] left unread; the content is written verbatim.
///
/// # Errors
///
/// [`Error::ObjectNotFound`] when the object is missing, [`Error::Io`] when its stream is
/// corrupt, and the usual work-tree write errors.
pub(crate) fn stream_checkout_entry(
    repo: &Repository,
    work_tree: &Path,
    rel_path: &str,
    mode: u32,
    change: &DiffEntry,
    dir_cache: &mut LeadingDirCache,
    oid: ObjectId,
) -> Result<IndexEntry> {
    repo.odb()
        .with_stream(&oid, |stream| {
            let mut reader = stream.reader;
            write_checkout_entry(
                work_tree,
                rel_path,
                WorktreeFileContent::Stream(&mut *reader),
                mode,
                change,
                dir_cache,
                oid,
            )
        })?
        .ok_or_else(|| Error::ObjectNotFound(oid.to_hex()))
}

/// Write blob `oid` to `rel_path` with checkout conversion, replacing whatever is there.
///
/// Large blobs copied verbatim are streamed from the object database (see
/// [`read_blob_unless_streamed`]); everything else is read, converted and written whole.
/// Returns the written path's metadata, or `None` when a process filter delayed the path.
pub(crate) fn write_blob_to_worktree(
    repo: &Repository,
    rules: &WorktreeRules,
    work_tree: &Path,
    rel_path: &str,
    oid: &ObjectId,
    mode: u32,
    dir_cache: &mut LeadingDirCache,
) -> Result<Option<fs::Metadata>> {
    let attrs = rules.file_attrs(rel_path, false);
    let Some(blob) = read_blob_unless_streamed(repo, rules.conversion(), &attrs, mode, oid)? else {
        return repo
            .odb()
            .with_stream(oid, |stream| {
                let mut reader = stream.reader;
                write_worktree_content(
                    work_tree,
                    rel_path,
                    WorktreeFileContent::Stream(&mut *reader),
                    mode,
                    dir_cache,
                )
            })?
            .ok_or_else(|| Error::ObjectNotFound(oid.to_hex()))
            .map(Some);
    };
    let Some(bytes) =
        worktree_bytes_from_index_blob(repo, rules, rel_path, mode, &blob.data, oid, None, None)?
    else {
        return Ok(None);
    };
    write_to_worktree_cached(work_tree, rel_path, &bytes, mode, dir_cache).map(Some)
}

fn write_checkout_entry(
    work_tree: &Path,
    rel_path: &str,
    content: WorktreeFileContent<'_>,
    mode: u32,
    change: &DiffEntry,
    dir_cache: &mut LeadingDirCache,
    oid: ObjectId,
) -> Result<IndexEntry> {
    let index_path = crate::repo_relpath::storage_bytes(rel_path).into_owned();
    let in_place_bytes = match content {
        WorktreeFileContent::Bytes(data)
            if mode != MODE_SYMLINK && can_overwrite_regular_in_place(change) =>
        {
            Some(data)
        }
        _ => None,
    };
    if let Some(data) = in_place_bytes {
        dir_cache.ensure_parents(work_tree, rel_path)?;
        let abs_path = crate::repo_relpath::worktree_abs(work_tree, rel_path);
        if let Ok(mut file) = OpenOptions::new()
            .write(true)
            .truncate(true)
            .open(&abs_path)
        {
            file.write_all(data).map_err(|e| {
                Error::PathError(format!("writing '{rel_path}' during checkout: {e}"))
            })?;
            drop(file);
            let old_mode = parse_git_mode(&change.old_mode);
            apply_in_place_git_mode_transition_at_path(&abs_path, old_mode, mode)?;
            let meta = written_file_symlink_metadata(&abs_path)?;
            return Ok(entry_from_metadata(&meta, &index_path, oid, mode));
        }
    }

    let meta = write_worktree_content(work_tree, rel_path, content, mode, dir_cache)?;
    Ok(entry_from_metadata(&meta, &index_path, oid, mode))
}

/// Adjust only the executable bit for an in-place overwrite, preserving umask-driven permissions.
#[cfg(unix)]
fn apply_in_place_git_mode_transition_at_path(
    abs_path: &Path,
    old_mode: u32,
    new_mode: u32,
) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let old_exec = old_mode == MODE_EXECUTABLE;
    let new_exec = new_mode == MODE_EXECUTABLE;
    if old_exec == new_exec {
        return Ok(());
    }
    let mut perms = fs::metadata(abs_path)?.permissions();
    let mut bits = perms.mode();
    const ALL_EXEC: u32 = 0o111;
    if new_exec {
        // Match Git: grant execute wherever read is already present (0644→0755 under umask 022,
        // 0600→0700 under umask 077).
        bits = grant_execute_from_read_bits(bits);
    } else {
        bits &= !ALL_EXEC;
    }
    perms.set_mode(bits);
    fs::set_permissions(abs_path, perms).map_err(Error::Io)
}

#[cfg(not(unix))]
fn apply_in_place_git_mode_transition_at_path(
    _abs_path: &Path,
    _old_mode: u32,
    _new_mode: u32,
) -> Result<()> {
    Ok(())
}

/// Tracks leading directories already verified or created during one checkout pass.
pub(crate) struct LeadingDirCache {
    ready: HashSet<PathBuf>,
    protect: ProtectOptions,
}

fn precreate_checkout_parent_dirs(
    work_tree: &Path,
    jobs: &[CheckoutWriteJob],
    cache: &mut LeadingDirCache,
) {
    let mut dirs = BTreeSet::new();
    for job in jobs {
        let Some(parent) = Path::new(&job.path).parent() else {
            continue;
        };
        if parent.as_os_str().is_empty() {
            continue;
        }
        dirs.insert(work_tree.join(parent));
    }
    for dir in dirs {
        if cache.ready.contains(&dir) {
            continue;
        }
        let _ = fs::create_dir_all(&dir);
        cache.ready.insert(dir);
    }
}

impl LeadingDirCache {
    pub(crate) fn new() -> Self {
        Self::with_protect(ProtectOptions::default())
    }

    pub(crate) fn with_protect(protect: ProtectOptions) -> Self {
        Self {
            ready: HashSet::new(),
            protect,
        }
    }

    /// Share parent-directory state from planning so workers do not re-stat prepared paths.
    pub(crate) fn with_ready_from(source: &Self) -> Self {
        Self {
            ready: source.ready.clone(),
            protect: source.protect,
        }
    }

    fn ensure_parents(&mut self, work_tree: &Path, rel_path: &str) -> Result<()> {
        let rel_bytes = crate::repo_relpath::storage_bytes(rel_path);
        let parent = crate::repo_relpath::parent_dir(&rel_bytes);
        if parent.is_empty() {
            return Ok(());
        }
        #[cfg(unix)]
        {
            use std::ffi::OsStr;
            use std::os::unix::ffi::OsStrExt;
            let mut cur = work_tree.to_path_buf();
            for comp in parent.split(|&b| b == b'/') {
                if comp.is_empty() {
                    continue;
                }
                cur.push(OsStr::from_bytes(comp));
                if self.ready.contains(&cur) {
                    continue;
                }
                if let Ok(meta) = leading_dir_metadata(&cur) {
                    if meta.file_type().is_symlink() || !meta.is_dir() {
                        fs::remove_file(&cur)?;
                    }
                }
                if fs::create_dir(&cur).is_err() {
                    // Exists as a directory already.
                }
                self.ready.insert(cur.clone());
            }
        }
        #[cfg(not(unix))]
        {
            use std::path::Component;
            let parent_str = std::str::from_utf8(&parent)
                .map_err(|_| Error::Message("non-UTF-8 parent path during checkout".into()))?;
            let path = Path::new(parent_str);
            let mut cur = work_tree.to_path_buf();
            for comp in path.components() {
                if let Component::Normal(name) = comp {
                    cur.push(name);
                    if self.ready.contains(&cur) {
                        continue;
                    }
                    if let Ok(meta) = leading_dir_metadata(&cur) {
                        if meta.file_type().is_symlink() || !meta.is_dir() {
                            fs::remove_file(&cur)?;
                        }
                    }
                    if fs::create_dir(&cur).is_err() {
                        // Exists as a directory already.
                    }
                    self.ready.insert(cur.clone());
                }
            }
        }
        Ok(())
    }
}

fn leading_dir_metadata(path: &Path) -> Result<fs::Metadata> {
    #[cfg(test)]
    syscall_probe::note_metadata(path, syscall_probe::MetadataKind::Directory);
    fs::symlink_metadata(path).map_err(Error::Io)
}

fn written_file_symlink_metadata(path: &Path) -> Result<fs::Metadata> {
    #[cfg(test)]
    syscall_probe::note_metadata(path, syscall_probe::MetadataKind::WrittenFile);
    fs::symlink_metadata(path).map_err(Error::Io)
}

/// Stat a path just written through `file`, after close (no durability fsync).
///
/// Status and refresh paths probe the worktree with `symlink_metadata` on the path. On Windows,
/// `File::metadata()` on an open handle can disagree with that probe (Git `mingw_stat`), which
/// forces a full re-hash on the first status after clone checkout. Closing the handle is enough
/// for path stat to match status; per-file `sync_all` is not required and regresses clone checkout.
fn written_file_metadata(file: std::fs::File, path: &Path) -> Result<fs::Metadata> {
    drop(file);
    written_file_symlink_metadata(path)
}

/// Grant execute bits wherever read is already present (Git index executable semantics).
#[cfg(unix)]
fn grant_execute_from_read_bits(bits: u32) -> u32 {
    bits | ((bits & 0o444) >> 2)
}

/// Mode passed to `open(2)` when creating a fresh worktree file (before the process umask).
#[cfg(unix)]
fn git_create_mode_for_index_mode(mode: u32) -> u32 {
    if mode == MODE_EXECUTABLE {
        0o777
    } else {
        0o666
    }
}

/// Create a new regular file and write `data`, letting the kernel apply umask to the create mode.
fn write_fresh_worktree_blob(
    abs_path: &Path,
    rel_path: &str,
    data: &[u8],
    mode: u32,
) -> Result<fs::Metadata> {
    let mut options = OpenOptions::new();
    options.write(true).create(true).truncate(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(git_create_mode_for_index_mode(mode));
    }
    #[cfg(not(unix))]
    let _ = mode;
    let mut file = options.open(abs_path).map_err(|e| {
        crate::worktree::map_worktree_io(rel_path, WorktreePathOperation::Replace, e)
    })?;
    io_with_path(
        rel_path,
        WorktreePathOperation::Replace,
        file.write_all(data),
    )?;
    written_file_metadata(file, abs_path)
}

/// Adjust an existing file to an executable index mode (in-place checkout only).
///
/// Fresh files are created with mode `0777` so umask can mask
/// execute independently of read; this helper propagates execute from read when the prior mode
/// is already fixed on disk.
pub fn apply_index_file_mode(abs_path: &Path, mode: u32) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if mode != MODE_EXECUTABLE {
            return Ok(());
        }
        let mut perms = fs::metadata(abs_path)?.permissions();
        let bits = grant_execute_from_read_bits(perms.mode());
        perms.set_mode(bits);
        fs::set_permissions(abs_path, perms)?;
    }
    // Windows has no POSIX mode bits; the executable bit is not represented in
    // the filesystem, so this is a no-op there.
    #[cfg(not(unix))]
    let _ = (abs_path, mode);
    Ok(())
}

/// Write data to a working tree file, handling symlinks and executable bits.
pub(crate) fn write_to_worktree_cached(
    work_tree: &Path,
    rel_path: &str,
    data: &[u8],
    mode: u32,
    dir_cache: &mut LeadingDirCache,
) -> Result<fs::Metadata> {
    write_worktree_content(
        work_tree,
        rel_path,
        WorktreeFileContent::Bytes(data),
        mode,
        dir_cache,
    )
}

/// Write `content` to a fresh working tree file, replacing whatever is at the path.
fn write_worktree_content(
    work_tree: &Path,
    rel_path: &str,
    content: WorktreeFileContent<'_>,
    mode: u32,
    dir_cache: &mut LeadingDirCache,
) -> Result<fs::Metadata> {
    let rel_bytes = crate::repo_relpath::storage_bytes(rel_path);
    validate_worktree_path(&rel_bytes, mode, dir_cache.protect)?;
    let abs_path = crate::index::worktree_path_from_index_rel(work_tree, &rel_bytes);

    dir_cache.ensure_parents(work_tree, rel_path)?;
    if let Some(parent) = abs_path.parent() {
        if !dir_cache.ready.contains(parent) {
            io_with_path(
                rel_path,
                WorktreePathOperation::Create,
                fs::create_dir_all(parent),
            )?;
            dir_cache.ready.insert(parent.to_path_buf());
        }
    }

    enum Ready<'a> {
        Bytes(&'a [u8]),
        Spooled(tempfile::NamedTempFile),
    }
    let ready = match content {
        WorktreeFileContent::Bytes(data) => Ready::Bytes(data),
        WorktreeFileContent::Stream(_) if mode == MODE_SYMLINK => {
            return Err(Error::PathError(format!(
                "symlink target for '{rel_path}' must be in memory"
            )));
        }
        WorktreeFileContent::Stream(reader) => {
            let dir = abs_path.parent().unwrap_or(work_tree);
            Ready::Spooled(spool_stream_to_temp(dir, rel_path, reader, mode)?)
        }
    };

    // Remove existing file/dir/symlink at target path. Use symlink_metadata + is_symlink so we
    // replace symlinked paths (e.g. `D` → `untracked`) before creating a real directory tree.
    if let Ok(meta) = fs::symlink_metadata(&abs_path) {
        if meta.file_type().is_symlink() {
            io_with_path(
                rel_path,
                WorktreePathOperation::Remove,
                fs::remove_file(&abs_path),
            )?;
        } else if meta.is_dir() {
            io_with_path(
                rel_path,
                WorktreePathOperation::Remove,
                fs::remove_dir_all(&abs_path),
            )?;
        } else {
            io_with_path(
                rel_path,
                WorktreePathOperation::Remove,
                fs::remove_file(&abs_path),
            )?;
        }
    }

    let data = match ready {
        Ready::Spooled(tmp) => {
            let file = tmp.persist(&abs_path).map_err(|e| {
                crate::worktree::map_worktree_io(rel_path, WorktreePathOperation::Replace, e.error)
            })?;
            return written_file_metadata(file, &abs_path);
        }
        Ready::Bytes(data) => data,
    };
    if mode == MODE_SYMLINK {
        let target = std::str::from_utf8(data).map_err(|_| {
            Error::PathError(format!("symlink target for '{rel_path}' is not UTF-8"))
        })?;
        #[cfg(unix)]
        io_with_path(
            rel_path,
            WorktreePathOperation::Create,
            std::os::unix::fs::symlink(target, &abs_path),
        )?;
        // Windows lacks unprivileged symlink creation; materialise the link as a
        // regular file containing the target path so the worktree stays populated.
        #[cfg(not(unix))]
        {
            let mut file = OpenOptions::new()
                .write(true)
                .create(true)
                .truncate(true)
                .open(&abs_path)
                .map_err(|e| {
                    crate::worktree::map_worktree_io(rel_path, WorktreePathOperation::Replace, e)
                })?;
            io_with_path(
                rel_path,
                WorktreePathOperation::Replace,
                file.write_all(target.as_bytes()),
            )?;
            return written_file_metadata(file, &abs_path);
        }
        #[cfg(unix)]
        return written_file_symlink_metadata(&abs_path);
    }
    write_fresh_worktree_blob(&abs_path, rel_path, data, mode)
}

/// Remove empty parent directories up to (but not including) `work_tree`.
pub fn remove_empty_parent_dirs(work_tree: &Path, path: &Path) {
    remove_empty_parent_dirs_batch(work_tree, &[path.to_path_buf()]);
}

fn remove_empty_parent_dirs_batch(work_tree: &Path, deleted_paths: &[PathBuf]) {
    let cwd = Some(crate::environment::Environment::empty().cwd.clone());
    let mut dirs: BTreeSet<PathBuf> = BTreeSet::new();
    for path in deleted_paths {
        let mut current = path.parent();
        while let Some(dir) = current {
            if dir == work_tree {
                break;
            }
            dirs.insert(dir.to_path_buf());
            current = dir.parent();
        }
    }
    let mut ordered: Vec<PathBuf> = dirs.into_iter().collect();
    ordered.sort_by_key(|p| std::cmp::Reverse(p.components().count()));
    for dir in ordered {
        if cwd
            .as_ref()
            .is_some_and(|cwd| cwd == &dir || cwd.starts_with(&dir))
        {
            continue;
        }
        let _ = fs::remove_dir(&dir);
    }
}

#[cfg(test)]
mod syscall_probe {
    use std::cell::RefCell;
    use std::collections::HashMap;
    use std::path::{Path, PathBuf};

    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    pub enum MetadataKind {
        Directory,
        WrittenFile,
    }

    thread_local! {
        static COUNTS: RefCell<HashMap<(PathBuf, MetadataKind), u32>> =
            RefCell::new(HashMap::new());
    }

    pub fn reset() {
        COUNTS.with(|c| c.borrow_mut().clear());
    }

    pub fn note_metadata(path: &Path, kind: MetadataKind) {
        COUNTS.with(|c| {
            let mut map = c.borrow_mut();
            *map.entry((path.to_path_buf(), kind)).or_insert(0) += 1;
        });
    }

    pub fn max_per_directory() -> u32 {
        COUNTS.with(|c| {
            c.borrow()
                .iter()
                .filter(|((_, k), _)| *k == MetadataKind::Directory)
                .map(|(_, v)| *v)
                .max()
                .unwrap_or(0)
        })
    }

    pub fn max_per_written_file() -> u32 {
        COUNTS.with(|c| {
            c.borrow()
                .iter()
                .filter(|((_, k), _)| *k == MetadataKind::WrittenFile)
                .map(|(_, v)| *v)
                .max()
                .unwrap_or(0)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::entry_from_stat;
    use crate::objects::ObjectKind;
    use crate::progress::{ProgressPhase, ProgressSink};
    use crate::repo::init_repository;
    use crate::write_tree::{write_tree_update_index, WriteTreeFlags};
    use tempfile::TempDir;

    #[derive(Default)]
    struct CheckoutProgressRecorder {
        started: Vec<(ProgressPhase, Option<u64>)>,
        file_units: u64,
        finished: u32,
    }

    impl ProgressSink for CheckoutProgressRecorder {
        fn start(&mut self, phase: ProgressPhase, total: Option<u64>) {
            self.started.push((phase, total));
        }
        fn inc(&mut self, units: u64) {
            self.file_units += units;
        }
        fn finish(&mut self) {
            self.finished += 1;
        }
    }

    fn commit_all(repo: &Repository, message: &str) -> ObjectId {
        use crate::objects::{serialize_commit, CommitData};
        use crate::refs;
        let work_tree = repo.work_tree().expect("work tree");
        let mut index = repo.load_index().expect("index");
        for entry in std::fs::read_dir(work_tree).expect("read_dir") {
            let entry = entry.expect("entry");
            let path = entry.path();
            if path.is_dir() {
                continue;
            }
            let rel = path.strip_prefix(work_tree).expect("rel").to_string_lossy();
            let data = fs::read(&path).expect("read");
            let oid = repo.odb().write(ObjectKind::Blob, &data).expect("blob");
            let e = entry_from_stat(&path, rel.as_bytes(), oid, MODE_REGULAR).expect("entry");
            index.add_or_replace(e);
        }
        index.sort();
        let tree = write_tree_update_index(repo.odb(), &mut index, "", WriteTreeFlags::silent())
            .expect("tree");
        repo.write_index(&mut index).expect("write index");
        let parent = refs::resolve_ref(repo.git_dir(), "HEAD").ok();
        let commit_data = CommitData {
            tree,
            parents: parent.into_iter().collect(),
            author: "Test <t@example.com>".to_owned(),
            committer: "Test <t@example.com>".to_owned(),
            author_raw: Vec::new(),
            committer_raw: Vec::new(),
            encoding: None,
            message: format!("{message}\n"),
            raw_message: None,
            extra_headers: Vec::new(),
        };
        let bytes = serialize_commit(&commit_data);
        let commit_oid = repo
            .odb()
            .write(ObjectKind::Commit, &bytes)
            .expect("commit");
        refs::write_ref(repo.git_dir(), "HEAD", &commit_oid).expect("head");
        tree
    }

    #[test]
    fn wholesale_deleted_directories_skips_dir_with_remaining_gitlink() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        std::fs::create_dir_all(wt.join("d005")).expect("mkdir");
        let mut index = repo.load_index().expect("index");
        let mut delete_paths = Vec::new();
        for i in 0..50 {
            let rel = format!("d005/f{i:02}.txt");
            let abs = wt.join(&rel);
            std::fs::write(&abs, format!("v{i}\n")).expect("write");
            let data = fs::read(&abs).expect("read");
            let oid = repo.odb().write(ObjectKind::Blob, &data).expect("blob");
            index.add_or_replace(
                entry_from_stat(&abs, rel.as_bytes(), oid, MODE_REGULAR).expect("entry"),
            );
            delete_paths.push(rel);
        }
        let sub_rel = "d005/submodule";
        std::fs::create_dir_all(wt.join(sub_rel)).expect("gitlink dir");
        let sub_oid = repo.odb().write(ObjectKind::Blob, b"sub\n").expect("blob");
        index.add_or_replace(
            entry_from_stat(&wt.join(sub_rel), sub_rel.as_bytes(), sub_oid, MODE_GITLINK)
                .expect("gitlink entry"),
        );
        index.sort();
        repo.write_index(&mut index).expect("write index");
        let wholesale = wholesale_deleted_directories(&index, &delete_paths);
        assert!(
            wholesale.is_empty(),
            "gitlink under dir must block wholesale removal"
        );
    }

    #[test]
    fn bulk_mod_delete_checkout_eligible_accepts_mixed_mod_and_delete() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let config = repo.config().expect("config");
        let index = repo.load_index().expect("index");
        let rules = WorktreeRules::from_parts_index_attributes_only(&repo, &index, config.clone())
            .expect("rules");
        let mut changes = Vec::new();
        for i in 0..40 {
            changes.push(DiffEntry {
                status: DiffStatus::Modified,
                old_path: Some(format!("f{i:03}.txt")),
                new_path: Some(format!("f{i:03}.txt")),
                old_mode: "100644".into(),
                new_mode: "100644".into(),
                old_oid: ObjectId::zero(),
                new_oid: ObjectId::zero(),
                score: None,
            });
        }
        for i in 0..40 {
            changes.push(DiffEntry {
                status: DiffStatus::Deleted,
                old_path: Some(format!("gone{i:03}.txt")),
                new_path: None,
                old_mode: "100644".into(),
                new_mode: String::new(),
                old_oid: ObjectId::zero(),
                new_oid: ObjectId::zero(),
                score: None,
            });
        }
        assert!(bulk_mod_delete_checkout_eligible(&changes, &rules));
    }

    #[test]
    fn bulk_mod_delete_checkout_eligible_accepts_mixed_add_mod_and_delete() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let config = repo.config().expect("config");
        let index = repo.load_index().expect("index");
        let rules = WorktreeRules::from_parts_index_attributes_only(&repo, &index, config.clone())
            .expect("rules");
        let mut changes = Vec::new();
        for i in 0..20 {
            changes.push(DiffEntry {
                status: DiffStatus::Added,
                old_path: None,
                new_path: Some(format!("new{i:03}.txt")),
                old_mode: String::new(),
                new_mode: "100644".into(),
                old_oid: ObjectId::zero(),
                new_oid: ObjectId::zero(),
                score: None,
            });
        }
        for i in 0..20 {
            changes.push(DiffEntry {
                status: DiffStatus::Modified,
                old_path: Some(format!("f{i:03}.txt")),
                new_path: Some(format!("f{i:03}.txt")),
                old_mode: "100644".into(),
                new_mode: "100644".into(),
                old_oid: ObjectId::zero(),
                new_oid: ObjectId::zero(),
                score: None,
            });
        }
        for i in 0..20 {
            changes.push(DiffEntry {
                status: DiffStatus::Deleted,
                old_path: Some(format!("gone{i:03}.txt")),
                new_path: None,
                old_mode: "100644".into(),
                new_mode: String::new(),
                old_oid: ObjectId::zero(),
                new_oid: ObjectId::zero(),
                score: None,
            });
        }
        assert!(bulk_mod_delete_checkout_eligible(&changes, &rules));
    }

    #[test]
    fn wholesale_deleted_directories_detects_full_top_level_dir() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        std::fs::create_dir_all(wt.join("d005")).expect("mkdir");
        let mut index = repo.load_index().expect("index");
        let mut delete_paths = Vec::new();
        for i in 0..50 {
            let rel = format!("d005/f{i:02}.txt");
            let abs = wt.join(&rel);
            std::fs::write(&abs, format!("v{i}\n")).expect("write");
            let data = fs::read(&abs).expect("read");
            let oid = repo.odb().write(ObjectKind::Blob, &data).expect("blob");
            index.add_or_replace(
                entry_from_stat(&abs, rel.as_bytes(), oid, MODE_REGULAR).expect("entry"),
            );
            delete_paths.push(rel);
        }
        index.sort();
        repo.write_index(&mut index).expect("write index");
        assert_eq!(delete_paths.len(), 50);
        let wholesale = wholesale_deleted_directories(&index, &delete_paths);
        assert_eq!(wholesale, BTreeSet::from(["d005".to_owned()]));
    }

    #[test]
    fn wholesale_directory_removal_skips_when_untracked_present() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        std::fs::create_dir_all(wt.join("d")).expect("mkdir");
        let mut index = repo.load_index().expect("index");
        let mut delete_paths = Vec::new();
        for i in 0..8 {
            let rel = format!("d/f{i}.txt");
            let abs = wt.join(&rel);
            std::fs::write(&abs, format!("v{i}\n")).expect("write");
            let data = fs::read(&abs).expect("read");
            let oid = repo.odb().write(ObjectKind::Blob, &data).expect("blob");
            index.add_or_replace(
                entry_from_stat(&abs, rel.as_bytes(), oid, MODE_REGULAR).expect("entry"),
            );
            delete_paths.push(rel);
        }
        std::fs::write(wt.join("d/untracked.txt"), b"keep me\n").expect("untracked");
        index.sort();
        let delete_set: HashSet<String> = delete_paths.iter().cloned().collect();
        assert!(!super::wholesale_directory_safe_to_remove(
            wt,
            "d",
            &delete_set,
            false
        ));
        let wholesale = super::wholesale_deleted_directories_on_disk(
            &repo,
            wt,
            &index,
            &delete_paths,
            false,
            &CheckoutApplyOptions::default(),
            &HashSet::new(),
        );
        assert!(wholesale.is_empty());
    }

    #[test]
    fn checkout_deletes_gitlink_directory() {
        let tmp = TempDir::new().expect("tempdir");
        let wt = tmp.path().join("wt");
        fs::create_dir_all(&wt).expect("mkdir");
        let repo = init_repository(&wt, false, "main", None, crate::RefStorageFormat::Files)
            .expect("init");
        let gitlink: ObjectId = "855827c583bc30645ba427885caa40c5b81764d2"
            .parse()
            .expect("oid");
        fs::write(wt.join("a"), b"a\n").expect("a");
        let a_oid = repo.odb().write(ObjectKind::Blob, b"a\n").expect("blob");
        let mut index = repo.load_index().expect("index");
        index.add_or_replace(entry_from_stat(&wt.join("a"), b"a", a_oid, MODE_REGULAR).expect("e"));
        index.add_or_replace(prospective_index_entry("sub", gitlink, MODE_GITLINK));
        index.sort();
        let with_sub =
            write_tree_update_index(repo.odb(), &mut index, "", WriteTreeFlags::silent())
                .expect("tree with sub");
        fs::write(wt.join("b"), b"b\n").expect("b");
        let b_oid = repo.odb().write(ObjectKind::Blob, b"b\n").expect("blob");
        index.remove(b"sub");
        index.add_or_replace(entry_from_stat(&wt.join("b"), b"b", b_oid, MODE_REGULAR).expect("e"));
        index.sort();
        let no_sub = write_tree_update_index(repo.odb(), &mut index, "", WriteTreeFlags::silent())
            .expect("tree no sub");

        checkout_between_trees(&repo, None, &with_sub).expect("checkout gitlink");
        assert!(wt.join("sub").is_dir());
        checkout_between_trees(&repo, Some(&with_sub), &no_sub).expect("remove gitlink");
        assert!(
            !wt.join("sub").exists(),
            "gitlink directory should be removed on delete"
        );
    }

    #[test]
    fn populated_checkout_reports_file_count() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        for i in 0..300 {
            fs::write(wt.join(format!("f{i}.txt")), format!("body {i}\n")).expect("write");
        }
        let tree = commit_all(&repo, "many files");
        let mut index = Index::new();
        repo.write_index(&mut index).expect("empty index");

        let mut rec = CheckoutProgressRecorder::default();
        checkout_between_trees_with_progress(&repo, None, &tree, &mut rec, &NeverCancel)
            .expect("checkout");

        let index = repo.load_index().expect("index after");
        assert_eq!(
            rec.started,
            vec![(ProgressPhase::CheckingOutFiles, Some(300))]
        );
        assert_eq!(rec.finished, 1);
        assert_eq!(
            rec.file_units,
            index.entries.len() as u64,
            "progress file count should match index entries (one unit per checked-out path)"
        );
        assert_eq!(index.entries.len(), 300);
    }

    #[test]
    fn slow_checkout_reports_total() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        for d in 0..3 {
            fs::write(wt.join(format!("a{d}.txt")), format!("v0 {d}\n")).expect("write");
        }
        let head = commit_all(&repo, "initial");
        for d in 0..3 {
            fs::write(wt.join(format!("a{d}.txt")), format!("v1 {d}\n")).expect("write");
        }
        let to_tree = commit_all(&repo, "updated");
        let changes = diff_trees(repo.odb(), Some(&head), Some(&to_tree), "").expect("diff");

        let mut rec = CheckoutProgressRecorder::default();
        checkout_between_trees_with_progress(&repo, Some(&head), &to_tree, &mut rec, &NeverCancel)
            .expect("checkout");

        assert_eq!(rec.started.len(), 1);
        assert_eq!(rec.started[0].0, ProgressPhase::CheckingOutFiles);
        assert_eq!(rec.started[0].1, Some(changes.len() as u64));
        assert_eq!(rec.file_units, changes.len() as u64);
        assert_eq!(rec.finished, 1);
    }

    #[test]
    fn checkout_syscall_counts_one_metadata_per_dir_and_file() {
        syscall_probe::reset();
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        for d in 0..5 {
            let dir = wt.join(format!("dir{d}"));
            fs::create_dir_all(&dir).expect("mkdir");
            for f in 0..20 {
                fs::write(dir.join(format!("f{f}.txt")), format!("body {d}-{f}\n")).expect("w");
            }
        }
        let head = commit_all(&repo, "initial");
        for d in 0..5 {
            let dir = wt.join(format!("dir{d}"));
            for f in 0..20 {
                fs::write(dir.join(format!("f{f}.txt")), format!("new {d}-{f}\n")).expect("w");
            }
        }
        let mut index = repo.load_index().expect("index");
        for d in 0..5 {
            for f in 0..20 {
                let rel = format!("dir{d}/f{f}.txt");
                let abs = wt.join(&rel);
                let data = fs::read(&abs).expect("read");
                let oid = repo.odb().write(ObjectKind::Blob, &data).expect("blob");
                let entry = entry_from_stat(&abs, rel.as_bytes(), oid, MODE_REGULAR).expect("e");
                index.add_or_replace(entry);
            }
        }
        index.sort();
        let to_tree = write_tree_update_index(repo.odb(), &mut index, "", WriteTreeFlags::silent())
            .expect("t");

        syscall_probe::reset();
        checkout_between_trees(&repo, Some(&head), &to_tree).expect("checkout");

        assert!(
            syscall_probe::max_per_directory() <= 1,
            "each directory should incur at most one tracked metadata call"
        );
        assert!(
            syscall_probe::max_per_written_file() <= 1,
            "each written file should incur at most one post-write metadata call"
        );
    }

    #[test]
    fn bulk_added_checkout_applies_preverified_wholesale_removals() {
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        std::fs::create_dir_all(wt.join("old")).expect("mkdir old");
        std::fs::create_dir_all(wt.join("new")).expect("mkdir new");
        let mut index = repo.load_index().expect("index");
        let mut changes = Vec::new();
        for i in 0..40 {
            let old_rel = format!("old/f{i:02}.txt");
            let new_rel = format!("new/f{i:02}.txt");
            let old_abs = wt.join(&old_rel);
            let new_abs = wt.join(&new_rel);
            std::fs::write(&old_abs, format!("old {i}\n")).expect("write old");
            let old_data = fs::read(&old_abs).expect("read old");
            let old_oid = repo.odb().write(ObjectKind::Blob, &old_data).expect("blob");
            index.add_or_replace(
                entry_from_stat(&old_abs, old_rel.as_bytes(), old_oid, MODE_REGULAR)
                    .expect("old entry"),
            );
            std::fs::write(&new_abs, format!("new {i}\n")).expect("write new");
            let new_data = fs::read(&new_abs).expect("read new");
            let new_oid = repo.odb().write(ObjectKind::Blob, &new_data).expect("blob");
            changes.push(DiffEntry {
                status: DiffStatus::Added,
                old_path: None,
                new_path: Some(new_rel),
                old_mode: String::new(),
                new_mode: "100644".into(),
                old_oid: ObjectId::zero(),
                new_oid,
                score: None,
            });
        }
        index.sort();
        let mut preverified = BTreeSet::new();
        preverified.insert("old".into());
        checkout_tree_changes_with_index_opts(
            &repo,
            &changes,
            &mut index,
            &mut NullProgress,
            &NeverCancel,
            CheckoutApplyOptions {
                parallel_prepare_blobs: true,
                checkout_parallel_threshold: Some(0),
                trust_wholesale_dir_removal: true,
                preverified_wholesale_dirs: preverified,
                wholesale_safety_precomputed: true,
                skip_bulk_checkout_fast_path: false,
                checkout_result_tree: None,
                defer_index_disk_write: false,
                ..CheckoutApplyOptions::default()
            },
            None,
        )
        .expect("checkout");
        assert!(
            !wt.join("old").exists(),
            "wholesale directory should be removed during bulk-add checkout"
        );
        for i in 0..40 {
            assert!(wt.join(format!("new/f{i:02}.txt")).is_file());
        }
    }

    #[cfg(unix)]
    #[test]
    fn bulk_added_checkout_drops_index_when_wholesale_removal_fails() {
        use std::os::unix::fs::PermissionsExt;

        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        std::fs::create_dir_all(wt.join("old")).expect("mkdir old");
        std::fs::create_dir_all(wt.join("new")).expect("mkdir new");
        let mut index = repo.load_index().expect("index");
        let mut changes = Vec::new();
        for i in 0..40 {
            let old_rel = format!("old/f{i:02}.txt");
            let new_rel = format!("new/f{i:02}.txt");
            let old_abs = wt.join(&old_rel);
            let new_abs = wt.join(&new_rel);
            std::fs::write(&old_abs, format!("old {i}\n")).expect("write old");
            let old_data = fs::read(&old_abs).expect("read old");
            let old_oid = repo.odb().write(ObjectKind::Blob, &old_data).expect("blob");
            index.add_or_replace(
                entry_from_stat(&old_abs, old_rel.as_bytes(), old_oid, MODE_REGULAR)
                    .expect("old entry"),
            );
            std::fs::write(&new_abs, format!("new {i}\n")).expect("write new");
            let new_data = fs::read(&new_abs).expect("read new");
            let new_oid = repo.odb().write(ObjectKind::Blob, &new_data).expect("blob");
            changes.push(DiffEntry {
                status: DiffStatus::Added,
                old_path: None,
                new_path: Some(new_rel),
                old_mode: String::new(),
                new_mode: "100644".into(),
                old_oid: ObjectId::zero(),
                new_oid,
                score: None,
            });
        }
        index.sort();
        let mut perms = std::fs::metadata(wt.join("old"))
            .expect("meta")
            .permissions();
        perms.set_mode(0o555);
        std::fs::set_permissions(wt.join("old"), perms).expect("chmod old");

        let mut preverified = BTreeSet::new();
        preverified.insert("old".into());
        checkout_tree_changes_with_index_opts(
            &repo,
            &changes,
            &mut index,
            &mut NullProgress,
            &NeverCancel,
            CheckoutApplyOptions {
                parallel_prepare_blobs: true,
                checkout_parallel_threshold: Some(0),
                trust_wholesale_dir_removal: true,
                preverified_wholesale_dirs: preverified,
                wholesale_safety_precomputed: true,
                skip_bulk_checkout_fast_path: false,
                checkout_result_tree: None,
                defer_index_disk_write: false,
                ..CheckoutApplyOptions::default()
            },
            None,
        )
        .expect("checkout");
        assert!(
            index.get(b"old/f00.txt", 0).is_none(),
            "index must drop wholesale-deleted paths even when directory removal fails"
        );
        for i in 0..40 {
            assert!(index
                .get(format!("new/f{i:02}.txt").as_bytes(), 0)
                .is_some());
        }
    }

    #[test]
    fn populated_tree_prefetches_promisor_blobs_once_per_chunk() {
        PREFETCH_PROMISOR_FOR_CHECKOUT_CALLS.with(|c| *c.borrow_mut() = 0);
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        for i in 0..=POPULATED_TREE_CHECKOUT_CHUNK {
            let rel = format!("f{i:05}.txt");
            std::fs::write(wt.join(&rel), format!("body {i}\n")).expect("write");
        }
        let tree = commit_all(&repo, "many");
        let mut index = crate::index::Index::new();
        repo.index_handle().write(&mut index).expect("clear index");
        for i in 0..=POPULATED_TREE_CHECKOUT_CHUNK {
            std::fs::remove_file(wt.join(format!("f{i:05}.txt"))).expect("remove");
        }
        checkout_between_trees(&repo, None, &tree).expect("populate");
        let calls = PREFETCH_PROMISOR_FOR_CHECKOUT_CALLS.with(|c| *c.borrow());
        assert!(
            calls >= 2,
            "expected promisor prefetch at least once per populated-tree chunk, got {calls}"
        );
    }

    #[test]
    fn populated_tree_builds_cache_tree_once_across_chunks() {
        // Two full chunks take the bulk path; the short tail takes the per-change path.
        let count = 2 * POPULATED_TREE_CHECKOUT_CHUNK + 5;
        let tmp = TempDir::new().expect("tempdir");
        let repo = init_repository(
            tmp.path(),
            false,
            "main",
            None,
            crate::RefStorageFormat::Files,
        )
        .expect("init");
        let wt = repo.work_tree().expect("wt");
        for i in 0..count {
            fs::write(wt.join(format!("f{i:05}.txt")), format!("body {i}\n")).expect("write");
        }
        let tree = commit_all(&repo, "many");
        let mut index = Index::new();
        repo.index_handle().write(&mut index).expect("clear index");
        for i in 0..count {
            fs::remove_file(wt.join(format!("f{i:05}.txt"))).expect("remove");
        }

        let builds_before = repo.odb().hot_path_metrics().cache_tree_builds();
        checkout_between_trees(&repo, None, &tree).expect("populate");
        let builds = repo.odb().hot_path_metrics().cache_tree_builds() - builds_before;

        assert_eq!(
            builds, 1,
            "populated checkout should build the cache tree once, not once per chunk"
        );
        let index = repo.load_index().expect("index after");
        assert_eq!(index.entries.len(), count);
        assert_eq!(index.cache_tree_root, Some(tree));
        assert_eq!(
            index.cache_tree.as_ref().map(|t| t.entry_count),
            Some(i32::try_from(count).expect("count fits"))
        );
        assert!(wt.join(format!("f{:05}.txt", count - 1)).is_file());
    }
}