mise 2026.9.14

Dev tools, env vars, and tasks in one CLI
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
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
//! `[dotfiles]` — declarative config files (dotfiles) applied by
//! `mise dot apply` or `mise bootstrap`, and removed by
//! `mise dot unapply`.
//!
//! Entries are keyed by target path and point at a source file or directory,
//! resolved relative to the config file that declares them:
//!
//! ```toml
//! [dotfiles]
//! "~/.config/mise/config.toml" = { source = "config.toml", mode = "symlink" }
//! "~/.zshrc" = { mode = "symlink" }                      # implied source
//! "~/.gitconfig" = "dotfiles/gitconfig"                  # explicit source
//! "~/.config/foo.toml" = { mode = "copy" }               # implied source
//! "~/.ssh/config" = { source = "ssh.tmpl", mode = "template" }
//! "~/.config/nvim" = "dotfiles/nvim"                     # symlink the dir itself
//! "~/.local/bin" = { source = "bin", mode = "symlink-each" }
//! ```
//!
//! Like `[bootstrap.packages]`, entries merge across the config hierarchy
//! (global -> local, local overrides by target key) and are only ever
//! applied by an explicit command, never implicitly.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::Command;

use eyre::{Result, WrapErr, bail};
use indexmap::IndexMap;
use itertools::Itertools;
use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::config::{Config, ConfigMap, Settings};
use crate::dirs;
use crate::file;
use crate::hash::{hash_sha256_to_str, hash_to_str};
use crate::path::PathExt;
use crate::system::history::journal::{self, Capture};
use crate::system::resources::ResourceOrigin;
use crate::system::secrets::SecretValues;
use crate::ui::prompt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FileMode {
    /// symlink the target to the source — a file or the directory itself
    Symlink,
    /// source is a directory: recreate its directory structure under the
    /// target and symlink each file individually, so the target directory
    /// can also hold files mise doesn't manage
    SymlinkEach,
    /// copy the source file (or directory, recursively)
    Copy,
    /// render the source through the mise template engine and write the
    /// result (permissions are taken from the source file unless the entry
    /// sets `permissions`)
    Template,
    /// write literal content declared directly in mise.toml
    Content,
    /// the live file stays where it is; history protects (and shares) it
    Track,
    /// remove the target: a regular file or symlink is deleted, a directory
    /// is refused and never removed recursively
    Absent,
    /// set the permissions of an existing target without managing its
    /// content: an entry with `permissions` and no source, content, or mode
    Permissions,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FileManifest {
    Git,
}

impl FileManifest {
    fn parse(s: &str) -> Option<Self> {
        match s {
            "git" => Some(Self::Git),
            _ => None,
        }
    }
}

impl FileMode {
    pub(crate) fn parse(s: &str) -> Option<Self> {
        match s {
            "symlink" => Some(Self::Symlink),
            "symlink-each" => Some(Self::SymlinkEach),
            "copy" => Some(Self::Copy),
            "template" => Some(Self::Template),
            "track" => Some(Self::Track),
            "absent" => Some(Self::Absent),
            _ => None,
        }
    }

    pub(crate) fn name(self) -> &'static str {
        match self {
            Self::Symlink => "symlink",
            Self::SymlinkEach => "symlink-each",
            Self::Copy => "copy",
            Self::Template => "template",
            Self::Content => "content",
            Self::Track => "track",
            Self::Permissions => "permissions",
            Self::Absent => "absent",
        }
    }

    /// Whether requests in this mode read a source path. Inline content,
    /// permissions-only entries, and absent targets have none.
    pub(crate) fn has_source(self) -> bool {
        !matches!(self, Self::Content | Self::Permissions | Self::Absent)
    }
}

/// Parse a `permissions` value: an octal string such as `"0600"`, with the
/// same syntax `[bootstrap.files]` accepts for `mode`.
fn parse_permissions(value: &str) -> Result<u32> {
    crate::system::managed_files::parse_mode(Some(value), 0).map_err(|_| {
        eyre::eyre!(
            "permissions must be an octal string between \"0000\" and \"7777\", got {value:?}"
        )
    })
}

/// Why `permissions` cannot be combined with an entry's other keys, if it
/// cannot. `mode` is the declared mode, or `None` for a permissions-only entry.
fn permissions_conflict(
    mode: Option<FileMode>,
    exclude: bool,
    manifest: bool,
    encrypt: bool,
) -> Option<&'static str> {
    match mode {
        Some(FileMode::Track) => Some(
            "permissions is not supported with mode = \"track\"; history records a tracked file's mode itself",
        ),
        Some(FileMode::Symlink | FileMode::SymlinkEach) => Some(
            "permissions requires mode copy or template, or inline content; a symlink has no permissions of its own",
        ),
        Some(_) if manifest => Some("permissions is not supported with a manifest directory copy"),
        Some(_) => None,
        None if exclude || manifest || encrypt => {
            Some("a permissions-only entry takes no exclude, manifest, or encrypt")
        }
        None => None,
    }
}

/// Windows has no Unix permission bits; entries there ignore `permissions`.
#[cfg(not(unix))]
fn warn_permissions_ignored() {
    static WARNED: std::sync::Once = std::sync::Once::new();
    WARNED.call_once(|| warn!("[dotfiles]: permissions is ignored on this platform"));
}

/// How history treats a destination: whether edits are saved automatically,
/// whether the file's saved version is shared with other machines, and
/// whether it enters remote backups.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct FilePolicy {
    pub autosave: bool,
    pub encrypt: bool,
    /// Which fields the declaration wrote, so a later layer repeating it
    /// overrides only what it says and inherits the rest.
    pub explicit: ExplicitFields,
}

/// The fields a `[dotfiles]` declaration wrote explicitly.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct ExplicitFields {
    pub autosave: bool,
    pub encrypt: bool,
    pub variants: bool,
    pub enabled: bool,
    pub exclude: bool,
    pub include: bool,
}

impl FilePolicy {
    /// Policy defaults do not enroll a deployment. Only explicit Track
    /// declarations observe files, regardless of how those files are deployed.
    pub(crate) fn for_mode(_mode: FileMode) -> Self {
        Self {
            autosave: true,
            encrypt: false,
            explicit: ExplicitFields::default(),
        }
    }
}

/// A `[dotfiles]` declaration history could not honour, reported instead
/// of silently ignored so failed enrollment is never mistaken for
/// protection.
#[derive(Debug, Clone, serde::Serialize)]
pub(crate) struct InvalidDeclaration {
    pub target: String,
    pub config: PathBuf,
    pub reason: String,
    /// Why the declaration is not in force.
    pub cause: Ignored,
}

/// **Two reasons to ignore a declaration, and only one of them is a
/// problem with the declaration.**
///
/// A rewrite has to tell them apart: replacing something mise could not
/// read would discard configuration nobody can see, while replacing
/// nothing — a `mode = "track"` entry in project configuration, which is
/// ignored by policy and always was — loses nothing at all.
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum Ignored {
    /// mise could not read it: a pattern that is not a glob, a mode it
    /// does not know, an encryption declaration that cannot hold.
    Unreadable,
    /// It reads fine and does not apply here.
    ByPolicy,
}

static INVALID_DECLARATIONS: std::sync::Mutex<Vec<InvalidDeclaration>> =
    std::sync::Mutex::new(Vec::new());

fn record_invalid(target: &str, config: &Path, reason: impl Into<String>) {
    record_ignored(target, config, reason, Ignored::Unreadable);
}

fn record_ignored(target: &str, config: &Path, reason: impl Into<String>, cause: Ignored) {
    let reason = reason.into();
    warn!("[dotfiles].\"{target}\": {reason}, ignoring entry");
    let mut invalid = INVALID_DECLARATIONS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    if !invalid
        .iter()
        .any(|existing| existing.target == target && existing.config == config)
    {
        invalid.push(InvalidDeclaration {
            target: target.to_string(),
            config: config.to_path_buf(),
            reason,
            cause,
        });
    }
}

/// The declarations ignored while loading `[dotfiles]` in this process.
pub(crate) fn invalid_declarations() -> Vec<InvalidDeclaration> {
    INVALID_DECLARATIONS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .clone()
}

/// A configuration reload must not retain failures from a previous version.
pub(crate) fn clear_invalid_declarations() {
    INVALID_DECLARATIONS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .clear();
}

/// Deployment variants reuse tracking selectors without changing history streams.
#[derive(Debug, Clone)]
pub(crate) struct FileVariant {
    target: Option<String>,
    selector: crate::system::history::select::Variant,
}

impl<'de> Deserialize<'de> for FileVariant {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        // Flattening Variant would bypass its deny_unknown_fields check. Remove
        // our one additional field, then use the original strict selector parser.
        let mut table = toml::Table::deserialize(deserializer)?;
        let target = table
            .remove("target")
            .map(toml::Value::try_into)
            .transpose()
            .map_err(serde::de::Error::custom)?;
        let selector = toml::Value::Table(table)
            .try_into()
            .map_err(serde::de::Error::custom)?;
        Ok(Self { target, selector })
    }
}

/// Validate selector combinations and destination syntax before selecting a variant.
/// Returns a `dotfiles.root`-relative implied source for a logical entry key.
/// A permissions-only entry has no source, so none is implied for it.
fn validate_file_variants(
    target: &str,
    source: Option<&str>,
    content: Option<&str>,
    mode: Option<&str>,
    permissions_only: bool,
    variants: &[FileVariant],
) -> Result<Option<PathBuf>> {
    let selectors: Vec<_> = variants.iter().map(|v| v.selector.clone()).collect();
    crate::system::history::select::validate(&selectors)?;
    let has_target_override = variants.iter().any(|v| v.target.is_some());
    if has_target_override && mode == Some("track") {
        bail!("target overrides are not supported with mode = \"track\"");
    }
    if has_target_override && content.is_some() {
        bail!("destination variants with inline content are not supported");
    }
    // an absent entry removes its destination and reads no source
    let implied_source = if has_target_override
        && source.is_none()
        && !permissions_only
        && mode != Some("absent")
    {
        if !variants.iter().all(|v| v.target.is_some()) {
            bail!(
                "destination variants require an explicit source when any variant uses the entry key as its target"
            );
        }
        Some(logical_source_path(target)?)
    } else {
        None
    };
    if variants.is_empty() && resolve_target_arg(target).is_relative() {
        bail!("target must be absolute or start with ~/");
    }
    for variant in variants {
        let destination = variant.target.as_deref().unwrap_or(target);
        if !variant_target_is_absolute(destination) {
            bail!("variant target must be absolute or start with ~/");
        }
    }
    Ok(implied_source)
}

fn logical_source_path(key: &str) -> Result<PathBuf> {
    let path = file::replace_path(key);
    if !path.is_relative() {
        bail!(
            "destination variants require an explicit source unless the entry key is a relative source path"
        );
    }
    let mut relative = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::Normal(component) => relative.push(component),
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                bail!("an implied source entry key must not contain '..'");
            }
            std::path::Component::RootDir | std::path::Component::Prefix(_) => {
                bail!("an implied source entry key must be relative");
            }
        }
    }
    if relative.as_os_str().is_empty() {
        bail!("an implied source entry key must not be empty");
    }
    Ok(relative)
}

/// Inactive variants can contain another platform's absolute path syntax.
/// The selected destination is still checked with native path rules before use.
fn variant_target_is_absolute(target: &str) -> bool {
    let bytes = target.as_bytes();
    resolve_target_arg(target).is_absolute()
        || target.starts_with('/')
        || target.starts_with("\\\\")
        || (bytes.len() >= 3
            && bytes[0].is_ascii_alphabetic()
            && bytes[1] == b':'
            && matches!(bytes[2], b'/' | b'\\'))
}

/// One `[dotfiles]` whole-file entry as written in mise.toml.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub(crate) enum FileTomlEntry {
    /// `"~/.gitconfig" = "dotfiles/gitconfig"`
    Source(String),
    /// `"~/.gitconfig" = { source = "...", mode = "..." }` — every field is
    /// optional so `{}` can mean implied source/default mode. Mode stays a
    /// string here so configs using modes from newer mise versions still
    /// parse (they warn and are skipped, like unknown package managers)
    Table {
        #[serde(default)]
        source: Option<String>,
        #[serde(default)]
        content: Option<String>,
        #[serde(default)]
        mode: Option<String>,
        #[serde(default)]
        exclude: Option<Vec<String>>,
        /// history: capture only these paths of a tracked directory
        #[serde(default)]
        include: Option<Vec<String>>,
        #[serde(default)]
        manifest: Option<String>,
        /// octal permissions for the target, e.g. `"0600"`; on its own it
        /// manages only the permissions of an existing target
        #[serde(default)]
        permissions: Option<String>,
        /// history: save edits automatically (default true)
        #[serde(default)]
        autosave: Option<bool>,
        #[serde(default)]
        encrypt: Option<bool>,
        /// Platform / profile selectors, with optional deployment destinations
        #[serde(default)]
        variants: Option<Vec<FileVariant>>,
        /// `false` disables an inherited declaration on this machine
        #[serde(default)]
        enabled: Option<bool>,
        /// template only: remove the target when the template renders empty
        #[serde(default)]
        remove_empty: Option<bool>,
        /// directory-walking modes only: deploy a source name like
        /// `dot-bashrc` as `.bashrc`
        #[serde(default)]
        dot_prefix: Option<bool>,
        /// symlink modes only: link with a relative target, overriding
        /// `dotfiles.relative_symlinks`
        #[serde(default)]
        relative: Option<bool>,
    },
}

impl FileRequest {
    /// Takes from `later`, a repeat of this declaration from a later layer
    /// or file, only what it wrote explicitly (`enabled`, the policies, the
    /// variants); what it left unsaid stays inherited.
    fn override_from(&mut self, later: FileRequest) {
        let explicit = later.policy.explicit;
        if explicit.enabled {
            self.enabled = later.enabled;
        }
        if explicit.autosave {
            self.policy.autosave = later.policy.autosave;
        }
        if explicit.encrypt {
            self.policy.encrypt = later.policy.encrypt;
        }
        if explicit.variants {
            self.variants = later.variants;
        }
        if explicit.exclude {
            self.exclude = later.exclude;
        }
        if explicit.include {
            self.include = later.include;
        }
        // the later file is the effective declaration
        self.origin = later.origin;
        let mine = self.policy.explicit;
        self.policy.explicit = ExplicitFields {
            autosave: mine.autosave || explicit.autosave,
            encrypt: mine.encrypt || explicit.encrypt,
            variants: mine.variants || explicit.variants,
            enabled: mine.enabled || explicit.enabled,
            exclude: mine.exclude || explicit.exclude,
            include: mine.include || explicit.include,
        };
    }
}

/// one file entry, resolved against the config file that declared it
#[derive(Debug, Clone)]
pub(crate) struct FileRequest {
    /// target path as written in config (display/merge key)
    pub target_raw: String,
    /// absolute, lexically normalized target path (`~` expanded)
    pub target: PathBuf,
    /// absolute source path (relative sources resolve against the config
    /// file's directory; omitted sources resolve under dotfiles.root)
    pub source: PathBuf,
    /// literal whole-file content; present only for inline content entries
    pub content: Option<String>,
    pub mode: FileMode,
    /// glob patterns, matched against source-relative paths, for files a
    /// directory-walking mode should skip (see [`is_excluded`])
    pub exclude: Vec<glob::Pattern>,
    /// history: the only paths of a tracked directory that are captured,
    /// relative to it and matched like `exclude` (see [`is_excluded`]).
    ///
    /// `None` means no list was declared and the whole tree is captured.
    /// `Some` means one was, and only what it names is — including
    /// `Some([])`, which selects nothing. A declared list that happens to
    /// be empty must not be read as no list at all.
    pub include: Option<Vec<glob::Pattern>>,
    /// optional source manifest limiting which directory entries are managed
    pub manifest: Option<FileManifest>,
    /// permission bits the target must have, overriding what the mode would
    /// otherwise give it (Unix only)
    pub permissions: Option<u32>,
    /// directory of the declaring config file — base dir for template
    /// functions like `exec` and `read_file`
    pub base: PathBuf,
    pub origin: ResourceOrigin,
    /// how history treats the destination
    pub policy: FilePolicy,
    /// platform / profile streams of a tracked file
    pub variants: Vec<crate::system::history::select::Variant>,
    /// `false` when a later layer disabled the declaration
    pub enabled: bool,
    /// template only: an empty (whitespace-only) render removes the target
    /// instead of writing an empty file
    pub remove_empty: bool,
    /// directory-walking modes only: each source path component named
    /// `dot-<name>` is deployed as `.<name>`, like GNU Stow's `--dotfiles`
    pub dot_prefix: bool,
    /// symlink modes only: links point at the source by a path relative to
    /// the link's directory (see [`relative_link_path`])
    pub relative: bool,
}

const SYMLINK_EACH_STATE_VERSION: u8 = 1;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct ManagedLink {
    source: PathBuf,
    target: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct SymlinkEachState {
    version: u8,
    source: PathBuf,
    target: PathBuf,
    links: Vec<ManagedLink>,
}

#[derive(Debug)]
struct SymlinkEachReconciliation {
    stale_links: Vec<ManagedLink>,
    targets: Vec<PathBuf>,
}

enum LoadedSymlinkEachState {
    Missing,
    Invalid,
    Present(SymlinkEachState),
}

const TARGET_STATE_VERSION: u8 = 1;

/// What mise knows about a single-file target it wrote, keyed by the target
/// path and kept under `$MISE_STATE_DIR/dotfiles/targets/`. It is the
/// ownership evidence for removing a target mise no longer wants: a file is
/// only removed without `--force` when it still holds what mise last wrote.
/// New fields must default, so a record written by an older mise still
/// loads; bump the version only for an incompatible change.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
struct TargetState {
    version: u8,
    target: PathBuf,
    /// sha256 of the content mise last wrote to the target
    content_digest: Option<String>,
    /// directories mise created to hold the target. Only these are removed
    /// with the target, and only once they are empty; a record without them
    /// (or from an older mise) removes none.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    created_dirs: Vec<PathBuf>,
}

/// What an empty template render means for the target currently on disk.
#[derive(Debug, PartialEq, Eq)]
enum EmptyRenderTarget {
    /// nothing there: already converged
    Absent,
    /// an empty file, or the content mise last wrote: safe to remove
    Owned,
    /// anything else needs `--force`; the reason is human-readable
    Conflict(&'static str),
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) enum FileState {
    Applied,
    Missing,
    /// target exists but doesn't match — the reason is human-readable
    Differs(String),
    SourceMissing,
    /// a tracked file: nothing to apply, history protects it where it is
    Tracked,
}

/// Aggregate whole-file `[dotfiles]` entries across all loaded config files.
/// Keys union global -> local; a more local config overrides an entry for the
/// same target. Malformed entries and unknown modes warn and are skipped.
pub(crate) fn files_from_config(config: &Config) -> Result<Vec<FileRequest>> {
    Ok(composed_files_from_config(config)?
        .into_iter()
        .filter(|request| request.enabled)
        .collect())
}

/// Keep disabled declarations so explicit tracking removal remains observable
/// when the prior enrollment exists in Git rather than local configuration.
pub(crate) fn composed_files_from_config(config: &Config) -> Result<Vec<FileRequest>> {
    let mut composed: IndexMap<PathBuf, Vec<FileRequest>> = IndexMap::new();
    let trusted_roots = global_composed_roots(config);
    for config_files in config.bootstrap_config_maps() {
        for request in
            files_from_config_files_with_tracking_roots(config_files, Some(&trusted_roots))
        {
            let siblings = composed.entry(request.target.clone()).or_default();
            if let Some(existing) = siblings
                .iter_mut()
                .find(|existing| file_requests_match(config, existing, &request))
            {
                // the same declaration from a later layer: what it says
                // about `enabled`, the policies, and the variants wins, so a
                // local `enabled = false` overrides what
                // it inherited instead of being dropped as a duplicate; what
                // it leaves unsaid stays inherited
                existing.override_from(request);
                continue;
            }
            if let Some(existing) = siblings.iter().find(|existing| {
                existing.mode != FileMode::Track
                    && request.mode != FileMode::Track
                    && (existing.mode != FileMode::SymlinkEach
                        || request.mode != FileMode::SymlinkEach)
            }) {
                bail!(
                    "conflicting dotfile declarations for {}\n\n  first:\n    {}\n\n  second:\n    {}",
                    request.target.display(),
                    existing.origin.conflict_description(),
                    request.origin.conflict_description(),
                );
            }
            siblings.push(request);
        }
    }
    Ok(composed.into_values().flatten().collect())
}

/// Whether a declaration comes from the system or global layers (or a root
/// they compose): the only layers history enrolls files from.
pub(crate) fn declaration_is_global(config: &Config, req: &FileRequest) -> bool {
    track_layer_allowed(&req.origin, &global_composed_roots(config))
}

/// Invalid project declarations must not block personal history either.
pub(crate) fn tracking_config_is_global(config: &Config, path: &Path) -> bool {
    crate::config::is_global_config(path)
        || global_composed_roots(config)
            .iter()
            .any(|root| path.starts_with(root))
}

/// Whether a track declaration comes from a layer allowed to enroll files.
fn track_layer_allowed(origin: &ResourceOrigin, trusted_roots: &[PathBuf]) -> bool {
    crate::config::is_global_config(&origin.config)
        || trusted_roots
            .iter()
            .any(|root| origin.config_root == *root || origin.config.starts_with(root))
}

/// The bootstrap config roots composed by system and global configuration.
fn global_composed_roots(config: &Config) -> Vec<PathBuf> {
    let mut roots = vec![];
    for (root, config_files) in config.selected_bootstrap_config_maps() {
        let declared_globally = config_files
            .keys()
            .any(|path| crate::config::is_global_config(path) && !path.starts_with(root));
        if declared_globally {
            roots.push(root.to_path_buf());
        }
    }
    roots
}

/// Validate the complete paths claimed by composed `[dotfiles]` entries.
/// Directory copies and `symlink-each` entries may share directories, but no
/// two entries may own the same leaf or require a directory where another
/// entry places a leaf.
pub(crate) fn validate_composed_file_footprints(requests: &[FileRequest]) -> Result<()> {
    let mut leaves: IndexMap<PathBuf, &FileRequest> = IndexMap::new();
    let mut directories: IndexMap<PathBuf, &FileRequest> = IndexMap::new();
    let mut symlink_each_identities: HashMap<(&Path, &Path), &FileRequest> = HashMap::new();
    let mut permissions_only = vec![];

    for request in requests {
        // Tracking observes native files; it does not own an apply leaf.
        // A tracked parent may contain independently managed destinations.
        if request.mode == FileMode::Track {
            continue;
        }
        // A permissions-only entry creates nothing, so a directory it
        // chmods may hold other entries' files. It still must not fight
        // another entry over the permissions of a file that entry writes.
        if request.mode == FileMode::Permissions {
            permissions_only.push(request);
            continue;
        }
        if request.permissions.is_some() && request.source.is_dir() {
            bail!(
                "[dotfiles].\"{}\": permissions requires a file source, not a directory: {}",
                request.target_raw,
                request.source.display_user()
            );
        }
        if request.manifest.is_some() && request.source.exists() && !request.source.is_dir() {
            bail!(
                "[dotfiles].\"{}\": manifest requires the source to be a directory: {}",
                request.target_raw,
                request.source.display_user()
            );
        }
        if request.dot_prefix && request.source.exists() && !request.source.is_dir() {
            return Err(dot_prefix_file_source(request));
        }
        if request.mode == FileMode::SymlinkEach
            && let Some(existing) = symlink_each_identities.insert(
                (request.source.as_path(), request.target.as_path()),
                request,
            )
        {
            return Err(composed_file_footprint_conflict(
                &request.target,
                existing,
                request,
            ));
        }
        // A missing source has an unknown eventual shape, but it still claims
        // its target. Whole-resource modes reserve a leaf; symlink-each has a
        // known directory-shaped target even before its children are known.
        // An absent entry has no source and claims its target as a leaf, so
        // declaring the same path present elsewhere (or a file beneath it)
        // conflicts.
        let source_unavailable = request.mode.has_source()
            && (!request.source.exists()
                || request.mode == FileMode::SymlinkEach && !request.source.is_dir());
        let directory_walker = !source_unavailable
            && matches!(request.mode, FileMode::Copy | FileMode::SymlinkEach)
            && request.source.is_dir();
        let unresolved_directory = source_unavailable && request.mode == FileMode::SymlinkEach;
        let request_leaves = if unresolved_directory {
            vec![]
        } else if directory_walker {
            walk_source_files(request)?
                .into_iter()
                .map(|(_, target)| target)
                .collect::<Vec<_>>()
        } else {
            vec![request.target.clone()]
        };
        let mut request_directories = indexmap::IndexSet::new();
        for leaf in &request_leaves {
            request_directories.extend(leaf.ancestors().skip(1).map(Path::to_path_buf));
        }
        if directory_walker || unresolved_directory {
            request_directories.insert(request.target.clone());
            request_directories.extend(request.target.ancestors().skip(1).map(Path::to_path_buf));
        }

        for leaf in &request_leaves {
            if let Some(existing) = leaves.get(leaf).or_else(|| directories.get(leaf)) {
                return Err(composed_file_footprint_conflict(leaf, existing, request));
            }
        }
        for directory in &request_directories {
            if let Some(existing) = leaves.get(directory) {
                return Err(composed_file_footprint_conflict(
                    directory, existing, request,
                ));
            }
        }
        for leaf in request_leaves {
            leaves.insert(leaf, request);
        }
        for directory in request_directories {
            directories.entry(directory).or_insert(request);
        }
    }
    for request in permissions_only {
        if let Some(existing) = leaves.get(&request.target) {
            return Err(composed_file_footprint_conflict(
                &request.target,
                existing,
                request,
            ));
        }
    }
    Ok(())
}

/// Describe a footprint collision while preserving the established
/// `symlink-each` diagnostic for two contributors in that mode.
fn composed_file_footprint_conflict(
    path: &Path,
    first: &FileRequest,
    second: &FileRequest,
) -> eyre::Report {
    let kind = if first.mode == FileMode::SymlinkEach && second.mode == FileMode::SymlinkEach {
        "symlink-each"
    } else {
        "dotfile"
    };
    eyre::eyre!(
        "conflicting {kind} declarations for {}\n\n  first:\n    {}\n\n  second:\n    {}",
        path.display(),
        first.origin.conflict_description(),
        second.origin.conflict_description(),
    )
}

/// Returns whether sibling declarations produce the same whole-file resource.
fn file_requests_match(config: &Config, first: &FileRequest, second: &FileRequest) -> bool {
    first.target == second.target
        && first.source == second.source
        && first.content == second.content
        && first.mode == second.mode
        && first.manifest == second.manifest
        && first.remove_empty == second.remove_empty
        && first.dot_prefix == second.dot_prefix
        && first.relative == second.relative
        && first.permissions == second.permissions
        // a track entry's list is a policy a later layer may change, like
        // autosave; a deployment entry's list is part of what it deploys
        && (first.mode == FileMode::Track
            || first
                .exclude
                .iter()
                .map(glob::Pattern::as_str)
                .eq(second.exclude.iter().map(glob::Pattern::as_str)))
        && (first.mode != FileMode::Template
            || first.base == second.base
                && config.bootstrap_tera_ctx(&first.origin.config)
                    == config.bootstrap_tera_ctx(&second.origin.config))
}

/// Incoming configuration must fail preflight rather than silently dropping
/// malformed declarations and applying the rest of the setup.
pub(crate) fn validate_incoming_files(config_files: &ConfigMap) -> Result<()> {
    for (path, config) in config_files {
        let Some(dotfiles) = config.dotfiles_config() else {
            continue;
        };
        for (target, value) in dotfiles.0 {
            if value.as_table().is_some_and(|t| {
                t.contains_key("encrypt")
                    && t.get("encrypt").and_then(toml::Value::as_bool).is_none()
            }) {
                bail!("dotfile {target}: encrypt must be a boolean");
            }
            if value.as_table().is_some_and(|t| {
                t.get("encrypt").and_then(toml::Value::as_bool) == Some(true)
                    && ["content", "block", "line", "template"]
                        .iter()
                        .any(|key| t.contains_key(*key))
            }) {
                bail!(
                    "encrypted dotfile {target} requires an external source, not inline content or edits"
                );
            }
            let Some(entry) = file_entry_from_toml(&target, value.clone()) else {
                // Managed line/block edits are handled by the edit engine,
                // not by this whole-file declaration parser.
                if let Some(table) = value.as_table().filter(|table| {
                    ["block", "line", "template", "comment", "position"]
                        .iter()
                        .any(|key| table.contains_key(*key))
                }) {
                    for key in ["permissions", "relative", "dot_prefix"] {
                        if table.contains_key(key) {
                            bail!(
                                "dotfile {target}: {key} applies to whole-file entries, not block or line edits"
                            );
                        }
                    }
                    continue;
                }
                bail!("invalid dotfile declaration {target} in {}", path.display());
            };
            if let Some(table) = value.as_table() {
                for key in table.keys() {
                    if !matches!(
                        key.as_str(),
                        "source"
                            | "content"
                            | "mode"
                            | "exclude"
                            | "include"
                            | "manifest"
                            | "permissions"
                            | "autosave"
                            | "encrypt"
                            | "variants"
                            | "enabled"
                            | "remove_empty"
                            | "dot_prefix"
                            | "relative"
                    ) {
                        bail!(
                            "unknown dotfile key {key:?} for {target} in {}",
                            path.display()
                        );
                    }
                }
            }
            if let FileTomlEntry::Source(_) = &entry {
                validate_file_variants(&target, None, None, None, false, &[])?;
            }
            if let FileTomlEntry::Table {
                source,
                content,
                mode,
                manifest,
                exclude,
                encrypt,
                include,
                permissions,
                variants,
                remove_empty,
                dot_prefix,
                relative,
                ..
            } = entry
            {
                let permissions_only = permissions.is_some()
                    && source.is_none()
                    && content.is_none()
                    && mode.is_none();
                let implied_variant_source = validate_file_variants(
                    &target,
                    source.as_deref(),
                    content.as_deref(),
                    mode.as_deref(),
                    permissions_only,
                    variants.as_deref().unwrap_or_default(),
                )?;
                if content.is_some() && (mode.is_some() || exclude.is_some() || manifest.is_some())
                {
                    bail!(
                        "dotfile {target}: inline content does not support mode, exclude, or manifest"
                    );
                }
                let mode = match mode.as_deref() {
                    Some(value) => FileMode::parse(value).ok_or_else(|| {
                        eyre::eyre!("unknown dotfile mode {value:?} for {target}")
                    })?,
                    None => default_mode(),
                };
                if mode == FileMode::Track
                    && (source.is_some() || content.is_some() || manifest.is_some())
                {
                    bail!("tracked file {target} cannot declare source, content, or manifest");
                }
                if mode == FileMode::Absent
                    && (source.is_some()
                        || manifest.is_some()
                        || exclude.is_some()
                        || permissions.is_some()
                        || encrypt == Some(true))
                {
                    bail!(
                        "dotfile {target} with mode = \"absent\" cannot declare source, content, manifest, exclude, permissions, or encrypt"
                    );
                }
                // composition checks whichever destination a variant selects,
                // so every one of them must be a single path
                if mode == FileMode::Absent
                    && std::iter::once(target.as_str())
                        .chain(
                            variants
                                .iter()
                                .flatten()
                                .filter_map(|variant| variant.target.as_deref()),
                        )
                        .any(|path| is_glob_pattern(&resolve_target_arg(path)))
                {
                    bail!("dotfile {target}: an absent target cannot use wildcards");
                }
                if source.is_some() && content.is_some() {
                    bail!("dotfile {target} cannot declare both source and content");
                }
                if let Some(permissions) = &permissions {
                    parse_permissions(permissions)
                        .map_err(|err| eyre::eyre!("dotfile {target}: {err}"))?;
                    let declared = if permissions_only {
                        None
                    } else if content.is_some() {
                        Some(FileMode::Content)
                    } else {
                        Some(mode)
                    };
                    if let Some(reason) = permissions_conflict(
                        declared,
                        exclude.is_some(),
                        manifest.is_some(),
                        encrypt.is_some(),
                    ) {
                        bail!("dotfile {target}: {reason}");
                    }
                    if permissions_only && is_glob_pattern(&resolve_target_arg(&target)) {
                        bail!("dotfile {target}: a permissions-only target cannot use wildcards");
                    }
                }
                if remove_empty == Some(true)
                    && (content.is_some() || permissions_only || mode != FileMode::Template)
                {
                    bail!("dotfile {target}: remove_empty requires mode = \"template\"");
                }
                if dot_prefix == Some(true)
                    && (content.is_some()
                        || permissions_only
                        || !matches!(mode, FileMode::Copy | FileMode::SymlinkEach))
                {
                    bail!("dotfile {target}: dot_prefix requires mode copy or symlink-each");
                }
                if relative == Some(true)
                    && (content.is_some()
                        || permissions_only
                        || !matches!(mode, FileMode::Symlink | FileMode::SymlinkEach))
                {
                    bail!(
                        "dotfile {target}: relative requires mode = \"symlink\" or \"symlink-each\""
                    );
                }
                if !matches!(mode, FileMode::Track | FileMode::Absent)
                    && !permissions_only
                    && source.is_none()
                    && content.is_none()
                    && implied_variant_source.is_none()
                {
                    implied_source(&resolve_target_arg(&target))?;
                }
                if let Some(manifest) = manifest
                    && (FileManifest::parse(&manifest).is_none()
                        || !matches!(mode, FileMode::Copy | FileMode::SymlinkEach))
                {
                    bail!("invalid manifest {manifest:?} for dotfile {target}");
                }
                // **Preflight has to reject what composition would
                // drop, not just what it cannot parse.** An `include` on
                // a deployment entry is refused when the configuration is
                // composed, so accepting it here let a pull report
                // success while quietly leaving that entry out. Two
                // readers of one declaration must not disagree about
                // whether it is usable.
                if include.is_some() && mode != FileMode::Track {
                    bail!("dotfile {target}: include applies only to mode = \"track\"");
                }
                if include.is_some()
                    && std::fs::symlink_metadata(resolve_target_arg(&target))
                        .is_ok_and(|meta| !meta.is_dir())
                {
                    bail!(
                        "dotfile {target}: include selects paths inside a tracked directory; remove it from this file or track its parent directory"
                    );
                }
                // Both selection lists, not just one: an incoming
                // `include` this mise cannot compile must fail preflight
                // rather than be dropped, or the entry silently selects
                // the whole tree on the machine that receives it.
                for pattern in exclude
                    .into_iter()
                    .flatten()
                    .chain(include.into_iter().flatten())
                {
                    glob::Pattern::new(&pattern)?;
                }
            }
        }
    }
    Ok(())
}

/// An edit on a path an `absent` entry removes would recreate the file on
/// every apply, so the two contradict each other.
pub(crate) fn validate_absent_edit_targets(
    files: &[FileRequest],
    edits: &[crate::system::edits::EditRequest],
) -> Result<()> {
    for edit in edits {
        // absent targets are lexically normalized; compare the edit's path
        // the same way so a `..` spelling cannot slip past
        let path = lexical_normalize(&edit.path);
        if let Some(file) = files
            .iter()
            .find(|file| file.mode == FileMode::Absent && file.target == path)
        {
            bail!(
                "conflicting dotfile declarations for {}: mode = \"absent\" removes the file that the edit {} changes\n\n  absent:\n    {}\n\n  edit:\n    {}",
                edit.path.display_user(),
                edit.describe_op(),
                file.origin.conflict_description(),
                edit.origin.conflict_description(),
            );
        }
    }
    Ok(())
}

/// Aggregate `[dotfiles]` across a specific set of config files. This is
/// used by OCI builds, which intentionally scope config to project files by
/// default instead of blindly inheriting global dotfiles.
pub(crate) fn files_from_config_files(config_files: &ConfigMap) -> Vec<FileRequest> {
    files_from_config_files_with_tracking_roots(config_files, None)
}

/// Resolve deployment overrides while limiting history enrollment to trusted roots.
fn files_from_config_files_with_tracking_roots(
    config_files: &ConfigMap,
    tracking_roots: Option<&[PathBuf]>,
) -> Vec<FileRequest> {
    // keyed by the *expanded* target so "~/.gitconfig" in one config and
    // its absolute spelling in another are one entry, not two
    let mut merged: IndexMap<(PathBuf, bool), FileRequest> = IndexMap::new();
    // A logical declaration must be overridden before selecting its destination:
    // otherwise a local override that changes the target would deploy both paths.
    let mut destination_declarations = IndexMap::new();
    let mut resolved_declarations = HashMap::new();
    for (path, cf) in config_files {
        let base = path.parent().unwrap_or(Path::new("."));
        let origin = ResourceOrigin {
            config: path.clone(),
            config_root: cf.config_root(),
            environment: crate::config::environments_for_config_path(path),
            source: None,
        };
        if let Some(dotfiles) = cf.dotfiles_config() {
            for (key, value) in dotfiles.0 {
                if value.get("mode").and_then(toml::Value::as_str) == Some("track") {
                    continue;
                }
                let mut requests = IndexMap::new();
                if let Some(entry) = parse_file_entry(&key, value, path) {
                    let overrides_target = matches!(&entry,
                        FileTomlEntry::Table { variants: Some(vs), .. }
                            if vs.iter().any(|v| v.target.is_some()));
                    merge_file_entry(key.clone(), entry, base, &origin, &mut requests);
                    // An invalid or inactive declaration cannot suppress an
                    // inherited request. Cache resolution so sources are walked once.
                    if !requests.is_empty() {
                        let (_, has_override) = destination_declarations
                            .entry(resolve_target_arg(&key))
                            .or_insert((path, false));
                        *has_override |= overrides_target;
                    }
                }
                resolved_declarations.insert((path, key), requests);
            }
        }
    }
    // config_files is ordered local -> global; reverse for global -> local
    for (path, cf) in config_files.iter().rev() {
        let base = path.parent().unwrap_or(Path::new(".")).to_path_buf();
        let origin = ResourceOrigin {
            config: path.clone(),
            config_root: cf.config_root(),
            environment: crate::config::environments_for_config_path(path),
            source: None,
        };
        let Some(dotfiles) = cf.dotfiles_config() else {
            continue;
        };
        for (target_raw, value) in dotfiles.0 {
            if value.get("mode").and_then(toml::Value::as_str) != Some("track")
                && destination_declarations
                    .get(&resolve_target_arg(&target_raw))
                    .is_some_and(|(winner, has_override)| *has_override && *winner != path)
            {
                continue;
            }
            if tracking_roots.is_some_and(|roots| !track_layer_allowed(&origin, roots))
                && value.get("mode").and_then(toml::Value::as_str) == Some("track")
            {
                record_ignored(
                    &target_raw,
                    &origin.config,
                    "tracking is enrolled from the global configuration only (ignored: project config)",
                    Ignored::ByPolicy,
                );
                continue;
            }
            if let Some(requests) = resolved_declarations.remove(&(path, target_raw.clone())) {
                merged.extend(requests);
            } else if let Some(entry) = parse_file_entry(&target_raw, value, path) {
                merge_file_entry(target_raw, entry, &base, &origin, &mut merged);
            }
        }
    }
    merged.into_values().collect()
}

/// Parse a whole-file declaration and reject unsupported encryption combinations.
fn parse_file_entry(target: &str, value: toml::Value, config: &Path) -> Option<FileTomlEntry> {
    if value.as_table().is_some_and(|t| {
        t.get("encrypt").and_then(toml::Value::as_bool) == Some(true)
            && ["content", "block", "line", "template"]
                .iter()
                .any(|key| t.contains_key(*key))
    }) {
        record_invalid(
            target,
            config,
            "encrypted dotfiles require an external source, not inline content or edits",
        );
        return None;
    }
    // Deserializing a whole-file entry drops the edit keys, so an edit that
    // also says `mode = "absent"` would silently become a removal of the
    // file it meant to edit.
    if value.as_table().is_some_and(|t| {
        t.get("mode").and_then(toml::Value::as_str) == Some("absent")
            && ["block", "line", "template", "comment", "position"]
                .iter()
                .any(|key| t.contains_key(*key))
    }) {
        record_invalid(
            target,
            config,
            "mode = \"absent\" removes the whole file and cannot be combined with block or line edits",
        );
        return None;
    }
    let encryption_declared = value
        .as_table()
        .is_some_and(|table| table.contains_key("encrypt"));
    let entry = file_entry_from_toml(target, value);
    if entry.is_none() && encryption_declared {
        record_invalid(
            target,
            config,
            "invalid encryption declaration; encrypt must be a boolean on a whole-file entry",
        );
    }
    entry
}

fn file_entry_from_toml(target_raw: &str, value: toml::Value) -> Option<FileTomlEntry> {
    match &value {
        toml::Value::String(_) => {}
        toml::Value::Table(table)
            if table.is_empty()
                || table.contains_key("mode")
                || table.contains_key("exclude")
                || table.contains_key("include")
                || table.contains_key("manifest")
                || table.contains_key("autosave")
                || table.contains_key("encrypt")
                || table.contains_key("variants")
                || table.contains_key("enabled")
                || table.contains_key("remove_empty")
                || ((table.contains_key("source")
                    || table.contains_key("content")
                    || table.contains_key("permissions")
                    || table.contains_key("relative")
                    || table.contains_key("dot_prefix"))
                    && !table.contains_key("block")
                    && !table.contains_key("line")
                    && !table.contains_key("template")
                    && !table.contains_key("comment")) => {}
        toml::Value::Table(_) => return None,
        _ => {
            warn!("[dotfiles].\"{target_raw}\": expected string or table entry, ignoring entry");
            return None;
        }
    }
    match value.try_into() {
        Ok(entry) => Some(entry),
        Err(err) => {
            warn!("[dotfiles].\"{target_raw}\": invalid file entry: {err}");
            None
        }
    }
}

/// Resolve one declaration, merging explicit tracking policies into earlier layers.
fn merge_file_entry(
    target_raw: String,
    entry: FileTomlEntry,
    base: &Path,
    origin: &ResourceOrigin,
    merged: &mut IndexMap<(PathBuf, bool), FileRequest>,
) {
    let (
        source,
        content,
        mode,
        exclude,
        include,
        manifest,
        permissions,
        autosave,
        encrypt,
        variants,
        enabled,
        remove_empty,
        dot_prefix,
        relative,
    ) = match entry {
        FileTomlEntry::Source(source) => (
            Some(source),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        ),
        FileTomlEntry::Table {
            source,
            content,
            mode,
            exclude,
            include,
            manifest,
            permissions,
            autosave,
            encrypt,
            variants,
            enabled,
            remove_empty,
            dot_prefix,
            relative,
        } => (
            source,
            content,
            mode,
            exclude,
            include,
            manifest,
            permissions,
            autosave,
            encrypt,
            variants,
            enabled,
            remove_empty,
            dot_prefix,
            relative,
        ),
    };
    // `{ permissions = "0600" }` alone manages only an existing target's
    // permissions; it never implies a source under dotfiles.root
    let permissions_only =
        permissions.is_some() && source.is_none() && content.is_none() && mode.is_none();
    let permissions = match permissions.as_deref().map(parse_permissions).transpose() {
        Ok(permissions) => permissions,
        Err(err) => {
            warn!("[dotfiles].\"{target_raw}\": {err}, ignoring entry");
            return;
        }
    };
    let remove_empty = remove_empty.unwrap_or(false);
    let dot_prefix = dot_prefix.unwrap_or(false);
    if encrypt == Some(true) && content.is_some() {
        record_invalid(
            &target_raw,
            &origin.config,
            "encrypted dotfiles require an external source; inline content is shared in configuration",
        );
        return;
    }
    let explicit = ExplicitFields {
        autosave: autosave.is_some(),
        encrypt: encrypt.is_some(),
        variants: variants.is_some(),
        enabled: enabled.is_some(),
        exclude: exclude.is_some(),
        include: include.is_some(),
    };
    let enabled = enabled.unwrap_or(true);
    let variants = variants.unwrap_or_default();
    let implied_variant_source = match validate_file_variants(
        &target_raw,
        source.as_deref(),
        content.as_deref(),
        mode.as_deref(),
        permissions_only,
        &variants,
    ) {
        Ok(Some(relative)) => Some(dotfiles_root().join(relative)),
        Ok(None) => None,
        Err(err) => {
            record_invalid(&target_raw, &origin.config, err.to_string());
            return;
        }
    };
    let selectors: Vec<_> = variants.iter().map(|v| v.selector.clone()).collect();
    let policy_for = |mode: FileMode| {
        let defaults = FilePolicy::for_mode(mode);
        FilePolicy {
            autosave: autosave.unwrap_or(defaults.autosave),
            encrypt: encrypt.unwrap_or(false),
            explicit,
        }
    };
    if mode.as_deref() != Some("track") && include.is_some() {
        record_invalid(
            &target_raw,
            &origin.config,
            "include selects what a tracked directory saves and applies only to mode = \"track\"",
        );
        return;
    }
    if mode.as_deref() == Some("track") {
        if source.is_some()
            || content.is_some()
            || manifest.is_some()
            || remove_empty
            || relative == Some(true)
            || dot_prefix
        {
            record_invalid(
                &target_raw,
                &origin.config,
                "mode = \"track\" leaves the file where it is and takes no source, content, manifest, remove_empty, relative, or dot_prefix",
            );
            return;
        }
        if permissions.is_some()
            && let Some(reason) = permissions_conflict(Some(FileMode::Track), false, false, false)
        {
            record_invalid(&target_raw, &origin.config, reason);
            return;
        }
        let target = resolve_target_arg(&target_raw);
        if target.is_relative() {
            record_invalid(
                &target_raw,
                &origin.config,
                "target must be absolute or start with ~/",
            );
            return;
        }
        // **An `include` list selects paths inside a tracked directory,
        // so a list on an entry that is a file can never select
        // anything.** `"~/.aws/credentials" = { include = ["credentials"]
        // }` is the natural mistake next to the documented directory
        // example, and it would otherwise be a declaration that captures
        // nothing at all. Said at the point it is written, with the fix,
        // rather than left to be worked out from an omission line. A
        // target that does not exist yet is not judged: it is captured
        // once it appears, and what it will be is not knowable here.
        if include.is_some() && std::fs::symlink_metadata(&target).is_ok_and(|meta| !meta.is_dir())
        {
            record_invalid(
                &target_raw,
                &origin.config,
                "include selects paths inside a tracked directory and does nothing on a file: remove it, or track the parent directory and name this file in its include list",
            );
            return;
        }
        let compiled = (
            compile_patterns("exclude", exclude),
            compile_patterns("include", include),
        );
        let (exclude, include) = match compiled {
            (Ok(exclude), Ok(include)) => (exclude.unwrap_or_default(), include),
            // both lists are reported when both are wrong: naming one and
            // dropping the other sends the user back for a second round
            // over a mistake mise had already seen
            (exclude, include) => {
                let reasons = [exclude.err(), include.err()]
                    .into_iter()
                    .flatten()
                    .collect::<Vec<_>>()
                    .join("; ");
                record_invalid(&target_raw, &origin.config, &reasons);
                return;
            }
        };
        let request = FileRequest {
            target_raw,
            target: target.clone(),
            source: PathBuf::new(),
            content: None,
            mode: FileMode::Track,
            exclude,
            include,
            manifest: None,
            permissions: None,
            base: base.to_path_buf(),
            origin: origin.clone(),
            policy: policy_for(FileMode::Track),
            variants: selectors,
            enabled,
            remove_empty: false,
            dot_prefix: false,
            relative: false,
        };
        // a later file of the same directory (`config.local.toml` after
        // `config.toml`) repeating a track declaration overrides only what
        // it says
        match merged.get_mut(&(target.clone(), true)) {
            Some(existing) if existing.mode == FileMode::Track => existing.override_from(request),
            _ => {
                merged.insert((target, true), request);
            }
        }
        return;
    }
    use crate::system::history::select::{self, Selection};
    let target_raw = match select::select(&selectors, &select::active_environments()) {
        Selection::Single => target_raw,
        Selection::Variant(selected) => variants
            .iter()
            .find(|v| v.selector == selected)
            .and_then(|v| v.target.clone())
            .unwrap_or(target_raw),
        Selection::NoMatch => return,
        Selection::Ambiguous(_) => {
            record_invalid(&target_raw, &origin.config, "ambiguous dotfile variants");
            return;
        }
    };
    if source.is_some() && content.is_some() {
        warn!(
            "[dotfiles].\"{target_raw}\": source and content are mutually exclusive, ignoring entry"
        );
        return;
    }
    if content.is_some() && (mode.is_some() || exclude.is_some() || manifest.is_some()) {
        warn!(
            "[dotfiles].\"{target_raw}\": inline content does not support mode, exclude, or manifest, ignoring entry"
        );
        return;
    }
    if mode.as_deref() == Some("absent") {
        if source.is_some()
            || exclude.is_some()
            || manifest.is_some()
            || permissions.is_some()
            || encrypt == Some(true)
        {
            warn!(
                "[dotfiles].\"{target_raw}\": mode = \"absent\" removes the target and takes no source, content, exclude, manifest, permissions, or encrypt, ignoring entry"
            );
            return;
        }
        if remove_empty || relative == Some(true) {
            warn!(
                "[dotfiles].\"{target_raw}\": mode = \"absent\" takes no remove_empty or relative, ignoring entry"
            );
            return;
        }
        if dot_prefix {
            warn!(
                "[dotfiles].\"{target_raw}\": dot_prefix requires mode copy or symlink-each, ignoring entry"
            );
            return;
        }
        let target = resolve_target_arg(&target_raw);
        if target.is_relative() {
            warn!(
                "[dotfiles].\"{target_raw}\": target must be absolute or start with ~/, ignoring entry"
            );
            return;
        }
        // a pattern would be checked as a literal path and remove nothing
        if is_glob_pattern(&target) {
            warn!(
                "[dotfiles].\"{target_raw}\": an absent target cannot use wildcards, ignoring entry"
            );
            return;
        }
        merged.insert(
            (target.clone(), false),
            FileRequest {
                target_raw,
                target,
                source: PathBuf::new(),
                content: None,
                mode: FileMode::Absent,
                exclude: vec![],
                include: None,
                manifest: None,
                permissions: None,
                base: base.to_path_buf(),
                origin: origin.clone(),
                policy: policy_for(FileMode::Absent),
                variants: vec![],
                enabled,
                remove_empty: false,
                dot_prefix: false,
                relative: false,
            },
        );
        return;
    }
    // compile once here so a typo is reported against the entry that wrote
    // it, not on every walk of the source.
    //
    // **A deployment entry keeps working with the rest of its list**, as
    // it always has: what an unreadable pattern costs here is a file
    // copied or linked that the user meant to leave behind, which they
    // can see. A tracked entry is refused instead (see the `track`
    // branch above), because what it costs there is a file captured into
    // history and pushed to a remote, which they cannot take back.
    let exclude = exclude
        .unwrap_or_default()
        .into_iter()
        .filter_map(|pattern| match glob::Pattern::new(&pattern) {
            Ok(pattern) => Some(pattern),
            Err(err) => {
                warn!("[dotfiles].\"{target_raw}\": invalid exclude pattern '{pattern}': {err}");
                None
            }
        })
        .collect::<Vec<_>>();
    let mode = match mode.as_deref() {
        None => default_mode(),
        Some(m) => match FileMode::parse(m) {
            Some(m) => m,
            None => {
                warn!("[dotfiles].\"{target_raw}\": unknown mode '{m}', ignoring entry");
                return;
            }
        },
    };
    let manifest = match manifest.as_deref() {
        None => None,
        Some(value) => match FileManifest::parse(value) {
            Some(manifest) => Some(manifest),
            None => {
                warn!("[dotfiles].\"{target_raw}\": unknown manifest '{value}', ignoring entry");
                return;
            }
        },
    };
    if manifest.is_some() && !matches!(mode, FileMode::Copy | FileMode::SymlinkEach) {
        warn!(
            "[dotfiles].\"{target_raw}\": manifest requires mode copy or symlink-each, ignoring entry"
        );
        return;
    }
    if permissions.is_some() {
        let declared = if permissions_only {
            None
        } else if content.is_some() {
            Some(FileMode::Content)
        } else {
            Some(mode)
        };
        if let Some(reason) = permissions_conflict(
            declared,
            !exclude.is_empty(),
            manifest.is_some(),
            encrypt.is_some(),
        ) {
            warn!("[dotfiles].\"{target_raw}\": {reason}, ignoring entry");
            return;
        }
    }
    #[cfg(not(unix))]
    let permissions = {
        if permissions.is_some() {
            warn_permissions_ignored();
            if permissions_only {
                return;
            }
        }
        None::<u32>
    };
    if remove_empty && (content.is_some() || permissions_only || mode != FileMode::Template) {
        warn!(
            "[dotfiles].\"{target_raw}\": remove_empty requires mode = \"template\", ignoring entry"
        );
        return;
    }
    if dot_prefix
        && (content.is_some()
            || permissions_only
            || !matches!(mode, FileMode::Copy | FileMode::SymlinkEach))
    {
        warn!(
            "[dotfiles].\"{target_raw}\": dot_prefix requires mode copy or symlink-each, ignoring entry"
        );
        return;
    }
    if relative == Some(true)
        && (content.is_some()
            || permissions_only
            || !matches!(mode, FileMode::Symlink | FileMode::SymlinkEach))
    {
        warn!(
            "[dotfiles].\"{target_raw}\": relative requires mode = \"symlink\" or \"symlink-each\", ignoring entry"
        );
        return;
    }
    let target = resolve_target_arg(&target_raw);
    if target.is_relative() {
        warn!(
            "[dotfiles].\"{target_raw}\": target must be absolute or start with ~/, ignoring entry"
        );
        return;
    }
    if permissions_only {
        if is_glob_pattern(&target) {
            warn!(
                "[dotfiles].\"{target_raw}\": a permissions-only target cannot use wildcards, ignoring entry"
            );
            return;
        }
        merged.insert(
            (target.clone(), false),
            FileRequest {
                target_raw,
                target,
                source: PathBuf::new(),
                content: None,
                mode: FileMode::Permissions,
                exclude: vec![],
                include: None,
                manifest: None,
                permissions,
                base: base.to_path_buf(),
                origin: origin.clone(),
                policy: policy_for(FileMode::Permissions),
                variants: vec![],
                enabled,
                remove_empty: false,
                dot_prefix: false,
                relative: false,
            },
        );
        return;
    }
    if let Some(content) = content {
        merged.insert(
            (target.clone(), false),
            FileRequest {
                target_raw,
                target,
                source: PathBuf::new(),
                content: Some(content),
                mode: FileMode::Content,
                exclude: vec![],
                include: None,
                manifest: None,
                permissions,
                base: base.to_path_buf(),
                origin: origin.clone(),
                policy: policy_for(FileMode::Content),
                variants: vec![],
                enabled,
                remove_empty: false,
                dot_prefix: false,
                relative: false,
            },
        );
        return;
    }
    let source = match source {
        Some(source) => {
            let source = file::replace_path(&source);
            if source.is_relative() {
                base.join(source)
            } else {
                source
            }
        }
        None => match implied_variant_source
            .map(Ok)
            .unwrap_or_else(|| implied_source(&target))
        {
            Ok(source) => source,
            Err(err) => {
                warn!("[dotfiles].\"{target_raw}\": {err}, ignoring entry");
                return;
            }
        },
    };
    let mut origin = origin.clone();
    origin.source = Some(source.clone());
    for req in expand_request(FileRequest {
        target_raw,
        target,
        source,
        content: None,
        mode,
        exclude,
        // `include` applies only to `mode = "track"`, which returned above
        include: None,
        manifest,
        permissions,
        base: base.to_path_buf(),
        origin,
        policy: policy_for(mode),
        variants: vec![],
        enabled,
        remove_empty,
        dot_prefix,
        relative: relative_symlinks(mode, relative),
    }) {
        merged.insert((req.target.clone(), false), req);
    }
}

/// Whether an entry in `mode` links by relative path: its own `relative` key
/// when set, else `dotfiles.relative_symlinks`. Only symlink modes link, and
/// never on Windows, where a directory link is a junction and has no
/// relative form.
pub(crate) fn relative_symlinks(mode: FileMode, declared: Option<bool>) -> bool {
    cfg!(unix)
        && matches!(mode, FileMode::Symlink | FileMode::SymlinkEach)
        && declared.unwrap_or_else(|| Settings::get().dotfiles.relative_symlinks)
}

/// Resolve the default deployment mode, warning and using symlinks for unsupported values.
pub(crate) fn default_mode() -> FileMode {
    let settings = Settings::get();
    let mode = settings.dotfiles.default_mode.as_str();
    match FileMode::parse(mode) {
        Some(
            mode
            @ (FileMode::Symlink | FileMode::SymlinkEach | FileMode::Copy | FileMode::Template),
        ) => mode,
        _ => {
            warn!("dotfiles.default_mode: unsupported mode '{mode}', using symlink");
            FileMode::Symlink
        }
    }
}

pub(crate) fn dotfiles_root() -> PathBuf {
    file::replace_path(&Settings::get().dotfiles.root)
}

pub(crate) fn implied_source(target: &Path) -> Result<PathBuf> {
    let home: &Path = &dirs::HOME;
    let rel = target.strip_prefix(home).map_err(|_| {
        eyre::eyre!(
            "source is required for targets outside $HOME: {}",
            target.display_user()
        )
    })?;
    if rel.as_os_str().is_empty() {
        bail!("source is required for the home directory itself");
    }
    Ok(dotfiles_root().join(rel))
}

pub(crate) fn source_is_implied(req: &FileRequest) -> bool {
    if !req.mode.has_source() {
        return false;
    }
    match implied_source(&req.target) {
        Ok(source) => source == req.source,
        Err(_) => false,
    }
}

pub(crate) fn resolve_target_arg(target: &str) -> PathBuf {
    lexical_normalize(&file::replace_path(target))
}

fn lexical_normalize(path: &Path) -> PathBuf {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                normalized.pop();
            }
            component => normalized.push(component.as_os_str()),
        }
    }
    normalized
}

pub(crate) fn matches_target(req_target: &Path, req_raw: &str, filters: &[String]) -> bool {
    filters.is_empty()
        || filters.iter().any(|filter| {
            filter == req_raw || {
                let resolved = resolve_target_arg(filter);
                resolved == req_target
            }
        })
}

pub(crate) fn copy_path(source: &Path, target: &Path) -> Result<()> {
    if let Some(parent) = target.parent() {
        file::create_dir_all(parent)?;
    }
    if source.is_dir() {
        if target.exists() || target.is_symlink() {
            remove_existing(target)?;
        }
        file::create_dir_all(target)?;
        file::copy_dir_all_preserve_symlinks(source, target)?;
    } else {
        if target.is_symlink() {
            file::remove_file(target)?;
        }
        file::copy(source, target)?;
    }
    Ok(())
}

fn expand_request(req: FileRequest) -> Vec<FileRequest> {
    let FileRequest {
        target_raw,
        target,
        source,
        mode,
        exclude,
        include,
        manifest,
        permissions,
        base,
        origin,
        policy,
        enabled,
        remove_empty,
        dot_prefix,
        relative,
        ..
    } = req;
    if !is_glob_pattern(&source) {
        return vec![FileRequest {
            target_raw,
            target,
            source,
            content: None,
            mode,
            exclude,
            include,
            manifest,
            permissions,
            base,
            origin,
            policy,
            variants: vec![],
            enabled,
            remove_empty,
            dot_prefix,
            relative,
        }];
    }

    let source_pattern = source.to_string_lossy().to_string();
    let matches = match glob::glob(&source_pattern) {
        Ok(paths) => paths
            .filter_map(|path| match path {
                Ok(path) => Some(path),
                Err(err) => {
                    warn!(
                        "[dotfiles].\"{target_raw}\": error reading source pattern {source_pattern}: {err}"
                    );
                    None
                }
            })
            .sorted()
            .collect_vec(),
        Err(err) => {
            warn!("[dotfiles].\"{target_raw}\": invalid source pattern: {err}");
            return vec![];
        }
    };
    if matches.is_empty() {
        warn!("[dotfiles].\"{target_raw}\": source pattern matched no files, ignoring entry");
        return vec![];
    }

    let target_pattern = target.to_string_lossy().to_string();
    if !is_glob_pattern(&target) {
        if matches.len() > 1 {
            warn!(
                "[dotfiles].\"{target_raw}\": source pattern matched multiple paths but target has no wildcard, ignoring entry"
            );
            return vec![];
        }
        return vec![FileRequest {
            target_raw,
            target,
            source: matches[0].clone(),
            content: None,
            mode,
            exclude,
            include,
            manifest,
            permissions,
            base,
            origin: ResourceOrigin {
                source: Some(matches[0].clone()),
                ..origin
            },
            policy,
            variants: vec![],
            enabled,
            remove_empty,
            dot_prefix,
            relative,
        }];
    }

    matches
        .into_iter()
        .filter_map(|matched_source| {
            let captures = match wildcard_captures(&source_pattern, &matched_source) {
                Ok(captures) => captures,
                Err(err) => {
                    warn!("[dotfiles].\"{target_raw}\": {err}");
                    return None;
                }
            };
            let Some(target_path) = expand_target_pattern(&target_pattern, &captures) else {
                warn!(
                    "[dotfiles].\"{target_raw}\": target wildcard count does not match source pattern, ignoring {}",
                    matched_source.display_user()
                );
                return None;
            };
            Some(FileRequest {
                target_raw: target_path.display_user().to_string(),
                target: target_path,
                source: matched_source.clone(),
                content: None,
                mode,
                exclude: exclude.clone(),
                include: None,
                manifest,
                permissions,
                base: base.clone(),
                origin: ResourceOrigin {
                    source: Some(matched_source.clone()),
                    ..origin.clone()
                },
                policy,
                variants: vec![],
                enabled,
                remove_empty,
                dot_prefix,
                relative,
            })
        })
        .collect()
}

fn is_glob_pattern(path: &Path) -> bool {
    path.to_string_lossy()
        .chars()
        .any(|c| matches!(c, '*' | '?' | '['))
}

fn wildcard_captures(pattern: &str, path: &Path) -> Result<Vec<String>> {
    let path = normalize_path_separators(&path.to_string_lossy());
    let re = wildcard_regex(pattern)?;
    let Some(captures) = re.captures(&path) else {
        bail!("source pattern did not match {path}");
    };
    Ok((1..captures.len())
        .map(|i| {
            captures
                .get(i)
                .map(|m| m.as_str().to_string())
                .unwrap_or_default()
        })
        .collect())
}

fn wildcard_regex(pattern: &str) -> Result<Regex> {
    let mut re = String::from("^");
    let pattern = normalize_path_separators(pattern);
    let mut chars = pattern.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '*' if chars.peek() == Some(&'*') => {
                chars.next();
                if chars.peek() == Some(&'/') {
                    chars.next();
                    // Glob `**/` can match zero directories. Capture the
                    // directory body without the trailing slash so target
                    // expansion can omit the slash when the capture is empty.
                    re.push_str("(?:(.*)/)?");
                } else {
                    re.push_str("(.*)");
                }
            }
            '*' => re.push_str("([^/]*)"),
            '?' => re.push_str("([^/])"),
            '[' => {
                let Some(class) = read_glob_class(&mut chars) else {
                    re.push_str("\\[");
                    continue;
                };
                re.push('(');
                re.push_str(&class);
                re.push(')');
            }
            _ => re.push_str(&regex::escape(&ch.to_string())),
        }
    }
    re.push('$');
    Ok(Regex::new(&re)?)
}

fn expand_target_pattern(pattern: &str, captures: &[String]) -> Option<PathBuf> {
    let mut out = String::new();
    let pattern = normalize_path_separators(pattern);
    let mut chars = pattern.chars().peekable();
    let mut captures = captures.iter();
    while let Some(ch) = chars.next() {
        match ch {
            '*' if chars.peek() == Some(&'*') => {
                chars.next();
                let capture = normalize_path_separators(captures.next()?);
                if chars.peek() == Some(&'/') {
                    chars.next();
                    if !capture.is_empty() {
                        out.push_str(&capture);
                        out.push('/');
                    }
                } else {
                    out.push_str(&capture);
                }
            }
            '*' => out.push_str(&normalize_path_separators(captures.next()?)),
            '?' => out.push_str(&normalize_path_separators(captures.next()?)),
            '[' => {
                read_glob_class(&mut chars)?;
                out.push_str(&normalize_path_separators(captures.next()?));
            }
            _ => out.push(ch),
        }
    }
    if captures.next().is_some() {
        return None;
    }
    Some(PathBuf::from(native_path_separators(&out)))
}

fn normalize_path_separators(path: &str) -> String {
    path.replace('\\', "/")
}

fn native_path_separators(path: &str) -> String {
    if std::path::MAIN_SEPARATOR == '/' {
        path.to_string()
    } else {
        path.replace('/', std::path::MAIN_SEPARATOR_STR)
    }
}

fn read_glob_class<I>(chars: &mut std::iter::Peekable<I>) -> Option<String>
where
    I: Iterator<Item = char>,
{
    let mut class = String::from("[");
    if chars.peek() == Some(&'!') {
        chars.next();
        class.push('^');
    }
    for ch in chars.by_ref() {
        class.push(ch);
        if ch == ']' {
            return Some(class);
        }
    }
    None
}

/// Current state of one entry on this machine.
///
/// Note: computing a template entry's state requires rendering it, so this
/// runs the template engine — including `exec()` — from
/// `mise dot status`. That's the same trust model as `[env]`
/// templates (which run on
/// every command in a trusted config); only `--dry-run` promises to execute
/// nothing and therefore skips template checks entirely.
pub(crate) fn check(
    config: &Config,
    req: &FileRequest,
    secrets: &SecretValues,
) -> Result<FileState> {
    if req.mode == FileMode::Track {
        return Ok(FileState::Tracked);
    }
    if req.mode.has_source() && !req.source.exists() {
        return Ok(FileState::SourceMissing);
    }
    // render at most once per call — templates may use exec()
    let rendered = match req.mode {
        FileMode::Template => Some(render_template(config, req, secrets)?),
        _ => None,
    };
    check_rendered(req, rendered.as_deref())
}

/// [`check`] with template output already rendered, so callers that go on to
/// write the file render only once (templates may use `exec()`, which must
/// not run more often than necessary)
fn check_rendered(req: &FileRequest, rendered: Option<&str>) -> Result<FileState> {
    match req.mode {
        FileMode::Track => Ok(FileState::Tracked),
        FileMode::Absent => check_absent(&req.target),
        FileMode::Symlink => check_symlink(&req.source, &req.target, req.relative),
        FileMode::SymlinkEach => check_symlink_each(req),
        FileMode::Copy if req.source.is_dir() => {
            if req.permissions.is_some() {
                bail!("permissions requires a file source, not a directory");
            }
            check_copy_dir(req)
        }
        FileMode::Copy => {
            let expected = file::read(&req.source)?;
            check_permissions(req, check_content(&req.target, &expected))
        }
        FileMode::Content => check_permissions(
            req,
            check_content(
                &req.target,
                req.content.as_deref().expect("inline content").as_bytes(),
            ),
        ),
        FileMode::Template if removes_target(req, rendered) => {
            Ok(match empty_render_target(req)? {
                EmptyRenderTarget::Absent => FileState::Applied,
                EmptyRenderTarget::Owned => {
                    FileState::Differs("template renders empty, target will be removed".into())
                }
                EmptyRenderTarget::Conflict(reason) => FileState::Differs(format!(
                    "template renders empty, but {reason}; use --force to remove it"
                )),
            })
        }
        FileMode::Template => check_permissions(
            req,
            check_content(
                &req.target,
                rendered.expect("rendered template content").as_bytes(),
            ),
        ),
        FileMode::Permissions => check_permissions_only(req),
    }
}

/// The permission bits apply must leave on a written target, when the entry
/// promises any: an explicit `permissions`, or a template's source mode.
/// Copies and inline content without `permissions` keep their historical
/// behaviour and are not checked for permission drift.
#[cfg(unix)]
fn desired_permissions(req: &FileRequest) -> Result<Option<u32>> {
    if req.permissions.is_some() {
        return Ok(req.permissions);
    }
    if req.mode == FileMode::Template {
        return Ok(Some(permission_bits(&req.source.metadata()?)));
    }
    Ok(None)
}

#[cfg(unix)]
fn permission_bits(metadata: &std::fs::Metadata) -> u32 {
    use std::os::unix::fs::PermissionsExt;
    metadata.permissions().mode() & 0o7777
}

/// Combine a content check with the target's permissions: an otherwise
/// applied target whose permissions drifted (e.g. a later chmod) is
/// `Differs`, so apply repairs them too. The mode is read without opening the
/// file, so a declared mode that denies its owner read access (`0200`,
/// `0000`) is still compared; such a target is applied once its mode matches,
/// because its content cannot be read back.
fn check_permissions(req: &FileRequest, content: Result<FileState>) -> Result<FileState> {
    #[cfg(unix)]
    if let Some(desired) = desired_permissions(req)? {
        let mode_differs = match std::fs::symlink_metadata(&req.target) {
            Ok(metadata) if metadata.file_type().is_file() => permission_bits(&metadata) != desired,
            _ => false,
        };
        let permissions_differ = || FileState::Differs("permissions differ".into());
        return match content {
            Ok(FileState::Applied) if mode_differs => Ok(permissions_differ()),
            // an unreadable target either drifted to a mode that denies
            // its owner read access (apply rewrites it) or was declared so
            Err(err) if (mode_differs || desired & 0o400 == 0) && is_permission_denied(&err) => {
                Ok(if mode_differs {
                    permissions_differ()
                } else {
                    FileState::Applied
                })
            }
            other => other,
        };
    }
    #[cfg(not(unix))]
    let _ = req;
    content
}

fn is_permission_denied(err: &eyre::Report) -> bool {
    err.chain().any(|cause| {
        cause
            .downcast_ref::<std::io::Error>()
            .is_some_and(|err| err.kind() == std::io::ErrorKind::PermissionDenied)
    })
}

/// A permissions-only entry never creates or rewrites its target and never
/// follows a symlink there: it only compares the bits of what exists. A
/// target that does not exist has nothing to adjust, so it counts as
/// satisfied (see [`permissions_target_absent`]).
fn check_permissions_only(req: &FileRequest) -> Result<FileState> {
    let metadata = match std::fs::symlink_metadata(&req.target) {
        Ok(metadata) => metadata,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(FileState::Applied),
        Err(err) => return Err(err.into()),
    };
    if metadata.file_type().is_symlink() {
        return Ok(FileState::Differs(PERMISSIONS_THROUGH_LINK.into()));
    }
    #[cfg(unix)]
    if let Some(desired) = req.permissions
        && permission_bits(&metadata) != desired
    {
        return Ok(FileState::Differs("permissions differ".into()));
    }
    Ok(FileState::Applied)
}

/// Why an applied permissions-only entry changed nothing: its target does not
/// exist. Status shows this next to `applied`.
pub(crate) fn permissions_target_absent(req: &FileRequest) -> Option<&'static str> {
    (req.mode == FileMode::Permissions
        && std::fs::symlink_metadata(&req.target)
            .is_err_and(|err| err.kind() == std::io::ErrorKind::NotFound))
    .then_some("target absent; permissions not applied")
}

const PERMISSIONS_THROUGH_LINK: &str =
    "exists but is a symlink; permissions are not set through links";

/// Why a permissions-only entry cannot act on its target right now, if it
/// cannot: the target is missing, or it is a symlink mise must not follow.
fn permissions_target_unavailable(req: &FileRequest) -> Result<Option<String>> {
    match std::fs::symlink_metadata(&req.target) {
        Ok(metadata) if metadata.file_type().is_symlink() => Ok(Some(format!(
            "{} is a symlink, which is never followed",
            req.target.display_user()
        ))),
        Ok(_) => Ok(None),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(Some(format!(
            "{} does not exist",
            req.target.display_user()
        ))),
        Err(err) => Err(err.into()),
    }
}

/// An absent target is converged once nothing is there. A regular file or a
/// symlink (to anything, even a directory) is removed without comparing its
/// content: the declaration itself says it must not exist. A real directory
/// is an error rather than a `--force`-able conflict, because absent entries
/// never delete recursively.
fn check_absent(target: &Path) -> Result<FileState> {
    // links first: a Windows directory symlink or junction is a directory
    // carrying a reparse point, and is removed as a link, not refused
    if file::is_symlink_or_junction(target) {
        return Ok(FileState::Differs("present (symlink)".into()));
    }
    match std::fs::symlink_metadata(target) {
        // a parent that is a file means the target cannot exist either
        Err(err)
            if matches!(
                err.kind(),
                std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory
            ) =>
        {
            Ok(FileState::Applied)
        }
        Err(err) => Err(err.into()),
        Ok(meta) if meta.is_dir() => bail!(
            "{} is a directory; mode = \"absent\" only removes files and symlinks",
            target.display_user()
        ),
        Ok(meta) if meta.is_file() => Ok(FileState::Differs("present".into())),
        // a FIFO, socket, or device node is not a configuration file
        Ok(_) => bail!(
            "{} is not a regular file or symlink; mode = \"absent\" only removes files and symlinks",
            target.display_user()
        ),
    }
}

/// With `relative`, a link that reaches the source by an absolute path is not
/// applied: it is re-pointed, so turning the option on converts links an
/// earlier apply made. Without it, any link reaching the source is accepted,
/// relative or not, as it always was.
fn check_symlink(source: &Path, target: &Path, relative: bool) -> Result<FileState> {
    // On Windows a file link is a real symlink when the privilege was available and a copy
    // otherwise (see `link_path`), so which one is on disk decides how to read it. Only fall
    // through to the copy comparison when it is not a symlink.
    if cfg!(windows) && source.is_file() && !target.is_symlink() {
        return check_copy(source, target);
    }
    if target.is_symlink() {
        let dest = std::fs::read_link(target)?;
        if !link_points_to(source, target) {
            Ok(FileState::Differs(format!(
                "symlink points to {}",
                dest.display_user()
            )))
        } else if relative && dest.is_absolute() {
            Ok(FileState::Differs(format!(
                "symlink points to {} by an absolute path; relative requested",
                dest.display_user()
            )))
        } else {
            Ok(FileState::Applied)
        }
    } else if target.exists() {
        Ok(FileState::Differs("exists but is not a symlink".into()))
    } else {
        Ok(FileState::Missing)
    }
}

fn points_at_same_file(target: &Path, source: &Path) -> bool {
    match (target.canonicalize(), source.canonicalize()) {
        (Ok(a), Ok(b)) => a == b,
        _ => false,
    }
}

fn check_symlink_each(req: &FileRequest) -> Result<FileState> {
    if !req.source.is_dir() {
        // callers add the entry's context (status row / apply error list)
        bail!(
            "mode symlink-each requires the source to be a directory: {}",
            req.source.display_user()
        );
    }
    let stale = stale_links(req)?;
    let stale_reason = || format!("{} stale link(s)", stale.len());
    let files = walk_source_files(req)?;
    // with no files to link the desired state is just the target directory —
    // a blocking non-directory must still surface (and be --force-able)
    if files.is_empty() {
        return if req.target.is_dir() {
            if stale.is_empty() {
                Ok(FileState::Applied)
            } else {
                Ok(FileState::Differs(stale_reason()))
            }
        } else if req.target.exists() || req.target.is_symlink() {
            Ok(FileState::Differs("exists but is not a directory".into()))
        } else {
            Ok(FileState::Missing)
        };
    }
    let mut applied = 0;
    let mut missing = 0;
    let mut differs: Option<String> = None;
    for (source, target) in files {
        match check_symlink(&source, &target, req.relative)? {
            FileState::Applied => applied += 1,
            FileState::Missing => missing += 1,
            FileState::Differs(reason) => {
                differs.get_or_insert(format!("{}: {reason}", target.display_user()));
            }
            FileState::SourceMissing | FileState::Tracked => {
                unreachable!("walked from source")
            }
        }
    }
    if let Some(reason) = differs {
        Ok(FileState::Differs(reason))
    } else if missing > 0 && applied > 0 {
        Ok(FileState::Differs(format!(
            "{applied} file(s) linked, {missing} missing"
        )))
    } else if missing > 0 {
        Ok(FileState::Missing)
    } else if !stale.is_empty() {
        Ok(FileState::Differs(stale_reason()))
    } else {
        Ok(FileState::Applied)
    }
}

fn check_copy(source: &Path, target: &Path) -> Result<FileState> {
    check_content(target, &file::read(source)?)
}

/// walks through [`walk_source_files`] rather than the source tree directly
/// so `exclude` applies to a directory copy the same way it does to
/// symlink-each
fn check_copy_dir(req: &FileRequest) -> Result<FileState> {
    if !req.target.exists() {
        return Ok(FileState::Missing);
    }
    if !req.target.is_dir() {
        return Ok(FileState::Differs("exists but is not a directory".into()));
    }
    for (source, target) in walk_source_files(req)? {
        match check_content(&target, &file::read(&source)?)? {
            FileState::Applied => {}
            _ => {
                let rel = target.strip_prefix(&req.target).unwrap_or(&target);
                return Ok(FileState::Differs(format!("{} differs", rel.display())));
            }
        }
    }
    Ok(FileState::Applied)
}

fn check_content(target: &Path, expected: &[u8]) -> Result<FileState> {
    // copy/template targets must be real files; a symlink — dangling or
    // live — gets replaced (re-pointing/replacing symlinks needs no --force)
    if target.is_symlink() {
        return Ok(FileState::Differs("exists but is a symlink".into()));
    }
    if !target.exists() {
        return Ok(FileState::Missing);
    }
    if target.is_dir() {
        return Ok(FileState::Differs("exists but is a directory".into()));
    }
    if file::read(target)? == expected {
        Ok(FileState::Applied)
    } else {
        Ok(FileState::Differs("content differs".into()))
    }
}

pub(crate) fn render_template(
    config: &Config,
    req: &FileRequest,
    secrets: &SecretValues,
) -> Result<String> {
    let raw = file::read_to_string(&req.source)?;
    let rendered = secrets
        .render_dotfile(config, &raw, &req.base, &req.origin.config)
        .map_err(|err| {
            eyre::eyre!(
                "[dotfiles].\"{}\": failed to render template {}: {err}",
                req.target_raw,
                req.source.display_user()
            )
        })?;
    Ok(rendered)
}

pub(crate) fn render_template_for_oci(config: &Config, req: &FileRequest) -> Result<String> {
    let raw = file::read_to_string(&req.source)?;
    let rendered =
        SecretValues::render_dotfile_for_oci(config, &raw, &req.base, &req.origin.config).map_err(
            |err| {
                eyre::eyre!(
                    "[dotfiles].\"{}\": failed to render template {}: {err}",
                    req.target_raw,
                    req.source.display_user()
                )
            },
        )?;
    Ok(rendered)
}

/// Render every configured dotfile template before a full bootstrap can
/// mutate anything. Secret values are cached, but templates are rendered again
/// when applied so hooks can update dynamic inputs such as files or commands.
pub(crate) fn preflight_templates(
    config: &Config,
    requests: &[FileRequest],
    secrets: &SecretValues,
) -> Result<()> {
    validate_composed_file_footprints(requests)?;
    let broken = requests
        .iter()
        .filter(|req| req.mode == FileMode::Template)
        .filter_map(|req| render_template(config, req, secrets).err())
        .map(|err| format!("  {err}"))
        .collect::<Vec<_>>();
    if !broken.is_empty() {
        bail!("files: entries with errors:\n{}", broken.join("\n"));
    }
    Ok(())
}

/// directories a symlink-each entry needs: the target itself plus every
/// intermediate directory for nested source files
fn needed_dirs(req: &FileRequest) -> Result<Vec<PathBuf>> {
    let mut out = indexmap::IndexSet::new();
    out.insert(req.target.clone());
    for (_, target) in walk_source_files(req)? {
        let mut dir = target.parent();
        while let Some(d) = dir {
            if d == req.target {
                break;
            }
            out.insert(d.to_path_buf());
            dir = d.parent();
        }
    }
    Ok(out.into_iter().collect())
}

fn symlink_each_state_path(req: &FileRequest) -> PathBuf {
    let source = lexical_normalize(&req.source);
    let target = lexical_normalize(&req.target);
    dirs::STATE.join("dotfiles").join(format!(
        "{}.toml",
        hash_to_str(&(source.as_path(), target.as_path()))
    ))
}

fn desired_symlink_each_state(req: &FileRequest) -> Result<SymlinkEachState> {
    Ok(SymlinkEachState {
        version: SYMLINK_EACH_STATE_VERSION,
        source: lexical_normalize(&req.source),
        target: lexical_normalize(&req.target),
        links: walk_source_files(req)?
            .into_iter()
            .map(|(source, target)| ManagedLink {
                source: lexical_normalize(&source),
                target: lexical_normalize(&target),
            })
            .collect(),
    })
}

fn active_symlink_each_state(req: &FileRequest) -> Result<SymlinkEachState> {
    if req.source.is_dir() {
        desired_symlink_each_state(req)
    } else {
        Ok(SymlinkEachState {
            version: SYMLINK_EACH_STATE_VERSION,
            source: lexical_normalize(&req.source),
            target: lexical_normalize(&req.target),
            links: vec![],
        })
    }
}

fn load_symlink_each_state(req: &FileRequest) -> LoadedSymlinkEachState {
    let path = symlink_each_state_path(req);
    if !path.exists() {
        return LoadedSymlinkEachState::Missing;
    }
    let state = match read_symlink_each_state(&path) {
        Ok(state) => state,
        Err(err) => {
            warn!(
                "files: failed to read dotfiles state {}: {err}",
                path.display_user()
            );
            return LoadedSymlinkEachState::Invalid;
        }
    };
    let valid = state.version == SYMLINK_EACH_STATE_VERSION
        && state.source == lexical_normalize(&req.source)
        && state.target == lexical_normalize(&req.target)
        && state
            .links
            .iter()
            .all(|link| link.target != req.target && link.target.starts_with(&req.target));
    if valid {
        LoadedSymlinkEachState::Present(state)
    } else {
        warn!(
            "files: ignoring invalid dotfiles state {}",
            path.display_user()
        );
        LoadedSymlinkEachState::Invalid
    }
}

fn read_symlink_each_state(path: &Path) -> Result<SymlinkEachState> {
    file::read_to_string(path)
        .and_then(|contents| toml::from_str::<SymlinkEachState>(&contents).map_err(Into::into))
        .map(normalize_symlink_each_state)
}

fn normalize_symlink_each_state(mut state: SymlinkEachState) -> SymlinkEachState {
    state.source = lexical_normalize(&state.source);
    state.target = lexical_normalize(&state.target);
    for link in &mut state.links {
        link.source = lexical_normalize(&link.source);
        link.target = lexical_normalize(&link.target);
    }
    state
}

fn valid_symlink_each_state(state: &SymlinkEachState) -> bool {
    state.version == SYMLINK_EACH_STATE_VERSION
        && state
            .links
            .iter()
            .all(|link| link.target != state.target && link.target.starts_with(&state.target))
}

fn plan_symlink_each_reconciliation(
    active_requests: &[FileRequest],
    selected_requests: &[FileRequest],
) -> Result<SymlinkEachReconciliation> {
    let selected_targets = selected_requests
        .iter()
        .map(|req| lexical_normalize(&req.target))
        .collect::<std::collections::HashSet<_>>();
    let active = active_requests
        .iter()
        .filter(|req| {
            req.mode == FileMode::SymlinkEach
                && selected_targets.contains(&lexical_normalize(&req.target))
        })
        .map(active_symlink_each_state)
        .collect::<Result<Vec<_>>>()?;
    let state_dir = dirs::STATE.join("dotfiles");
    let stored = if state_dir.is_dir() {
        state_dir
            .read_dir()?
            .filter_map(|entry| entry.ok())
            .filter_map(|entry| read_symlink_each_state(&entry.path()).ok())
            .collect()
    } else {
        vec![]
    };
    Ok(reconcile_symlink_each_states(
        &active,
        &selected_targets,
        &stored,
    ))
}

fn reconcile_symlink_each_states(
    active: &[SymlinkEachState],
    selected_targets: &std::collections::HashSet<PathBuf>,
    stored: &[SymlinkEachState],
) -> SymlinkEachReconciliation {
    let active_keys = active
        .iter()
        .map(|state| (&state.source, &state.target))
        .collect::<std::collections::HashSet<_>>();
    let desired_links = active
        .iter()
        .flat_map(|state| state.links.iter())
        .map(|link| (&link.target, &link.source))
        .collect::<std::collections::HashMap<_, _>>();
    if selected_targets.is_empty() {
        return SymlinkEachReconciliation {
            stale_links: vec![],
            targets: vec![],
        };
    }
    let mut stale_links = IndexMap::<PathBuf, ManagedLink>::new();
    let mut targets = indexmap::IndexSet::new();
    for state in stored {
        if !valid_symlink_each_state(state)
            || !selected_targets.contains(&state.target)
            || active_keys.contains(&(&state.source, &state.target))
        {
            continue;
        }
        let stale_before = stale_links.len();
        for link in &state.links {
            if desired_links
                .get(&link.target)
                .is_none_or(|source| **source != link.source)
                && link_points_to(&link.source, &link.target)
            {
                stale_links.insert(link.target.clone(), link.clone());
            }
        }
        if stale_links.len() != stale_before {
            targets.insert(state.target.clone());
        }
    }
    SymlinkEachReconciliation {
        stale_links: stale_links.into_values().collect(),
        targets: targets.into_iter().collect(),
    }
}

fn save_symlink_each_state(req: &FileRequest) {
    if cfg!(windows) {
        return;
    }
    let path = symlink_each_state_path(req);
    let result = (|| -> Result<()> {
        file::create_dir_all(path.parent().expect("dotfiles state parent"))?;
        file::write(
            &path,
            toml::to_string_pretty(&desired_symlink_each_state(req)?)?,
        )
    })();
    if let Err(err) = result {
        warn!(
            "files: failed to write dotfiles state {}: {err}",
            path.display_user()
        );
    }
}

fn remove_symlink_each_state(req: &FileRequest) -> Result<()> {
    let path = symlink_each_state_path(req);
    if path.exists() {
        file::remove_file(path)?;
    }
    Ok(())
}

fn target_state_path(req: &FileRequest) -> PathBuf {
    let target = lexical_normalize(&req.target);
    dirs::STATE
        .join("dotfiles")
        .join("targets")
        .join(format!("{}.toml", hash_to_str(&target.as_path())))
}

/// The record for `req`'s target. A missing, unreadable, or foreign record
/// proves nothing, so it reads as no record at all.
fn load_target_state(req: &FileRequest) -> Option<TargetState> {
    let path = target_state_path(req);
    if !path.exists() {
        return None;
    }
    let state = match file::read_to_string(&path)
        .and_then(|contents| toml::from_str::<TargetState>(&contents).map_err(Into::into))
    {
        Ok(state) => state,
        Err(err) => {
            warn!(
                "files: failed to read dotfiles state {}: {err}",
                path.display_user()
            );
            return None;
        }
    };
    if state.version == TARGET_STATE_VERSION
        && lexical_normalize(&state.target) == lexical_normalize(&req.target)
    {
        Some(state)
    } else {
        warn!(
            "files: ignoring invalid dotfiles state {}",
            path.display_user()
        );
        None
    }
}

/// Record `content` as what mise last wrote to `req`'s target. A record that
/// cannot be written only costs a later `--force`, so it warns.
fn save_target_state(req: &FileRequest, content: &str) {
    update_target_state(req, |state| {
        state.content_digest = Some(content_digest(content));
    });
}

/// Add `created` to the directories recorded as mise's for `req`'s target,
/// keeping those recorded by earlier applies. A record that cannot be written
/// only leaves the directories in place later, so it warns.
fn record_created_dirs(req: &FileRequest, created: &[PathBuf]) {
    if created.is_empty() {
        return;
    }
    update_target_state(req, |state| {
        for dir in created {
            if !state.created_dirs.contains(dir) {
                state.created_dirs.push(dir.clone());
            }
        }
    });
}

fn update_target_state(req: &FileRequest, update: impl FnOnce(&mut TargetState)) {
    let path = target_state_path(req);
    let mut state = load_target_state(req).unwrap_or_default();
    state.version = TARGET_STATE_VERSION;
    state.target = lexical_normalize(&req.target);
    update(&mut state);
    let result = (|| -> Result<()> {
        file::create_dir_all(path.parent().expect("dotfiles state parent"))?;
        file::write_atomic(&path, toml::to_string_pretty(&state)?)
    })();
    if let Err(err) = result {
        warn!(
            "files: failed to write dotfiles state {}: {err}",
            path.display_user()
        );
    }
}

fn remove_target_state(req: &FileRequest) -> Result<()> {
    let path = target_state_path(req);
    if path.exists() {
        file::remove_file(path)?;
    }
    Ok(())
}

/// Whether mise records the directories it creates for `req`: entries whose
/// target is a single file or link. Entries that walk a source directory
/// share their target with unmanaged files, permissions-only entries never
/// create or remove theirs, and absent entries create nothing (they prune
/// from a record an earlier entry for the same target left; see
/// [`removal_created_dirs`]).
fn records_created_dirs(req: &FileRequest) -> bool {
    match req.mode {
        FileMode::Symlink | FileMode::Template | FileMode::Content => true,
        FileMode::Copy => !req.source.is_dir(),
        FileMode::SymlinkEach | FileMode::Track | FileMode::Permissions | FileMode::Absent => false,
    }
}

/// The directories removing `target` may take with it: the `created` ones in
/// an unbroken run upward from its parent, deepest first. A directory that is
/// already gone (an earlier removal took it) holds nothing, so the run passes
/// over it. Only directories strictly inside `home` qualify: `home` itself,
/// everything above it, and everything outside it (such as `/opt/app` for a
/// target `/opt/app/file`) may be shared with other software, so they stay
/// even when mise created them.
fn created_dirs_to_prune(target: &Path, created: &[PathBuf], home: &Path) -> Vec<PathBuf> {
    let mut chain = vec![];
    for dir in target.ancestors().skip(1) {
        if dir == home || !dir.starts_with(home) {
            break;
        }
        if created.iter().any(|c| c == dir) {
            chain.push(dir.to_path_buf());
        } else if dir.exists() || dir.is_symlink() {
            break;
        }
    }
    chain
}

/// Remove the empty directories of `chain` (deepest first), stopping at the
/// first one that holds anything, is not a directory, or is `claimed` by
/// another entry. One already gone, taken by an earlier walk, is passed
/// over. The chain is checked against `home` by path only, so a symlinked
/// ancestor (`~/.config -> /opt/config`) can put a directory physically
/// outside home: the walk stops there and gives the directory up, since
/// mise will never remove it. Returns what was removed or given up, which
/// the records drop.
fn remove_created_dirs(chain: &[PathBuf], claimed: &HashSet<PathBuf>, home: &Path) -> Vec<PathBuf> {
    use std::io::ErrorKind;
    // Best effort: the targets are already gone, so a directory that cannot
    // be removed only stays behind. It stays in the record too, so a later
    // removal retries it.
    let give_up = |dir: &Path, err: std::io::Error| {
        if matches!(
            err.kind(),
            ErrorKind::NotFound | ErrorKind::DirectoryNotEmpty
        ) {
            // something else removed it or wrote into it meanwhile
            debug!("files: keeping {}: {err}", dir.display_user());
        } else {
            warn!(
                "files: cannot remove empty directory {}: {err}",
                dir.display_user()
            );
        }
    };
    let physical_home = std::fs::canonicalize(home).unwrap_or_else(|_| home.to_path_buf());
    let mut settled = vec![];
    for dir in chain {
        match std::fs::symlink_metadata(dir) {
            Err(err) if err.kind() == ErrorKind::NotFound => continue,
            Err(err) => {
                give_up(dir, err);
                break;
            }
            Ok(metadata) if !metadata.is_dir() => break,
            Ok(_) => {}
        }
        let physical = match std::fs::canonicalize(dir) {
            Ok(physical) => physical,
            Err(err) => {
                give_up(dir, err);
                break;
            }
        };
        if physical == physical_home || !physical.starts_with(&physical_home) {
            debug!(
                "files: keeping {}: it is {}, outside the home directory",
                dir.display_user(),
                physical.display()
            );
            settled.push(dir.clone());
            break;
        }
        // Check and remove the resolved path that passed the home check, so
        // an ancestor swapped for a symlink afterwards cannot redirect them.
        // The journal and records keep the path as the user sees it.
        // `remove_dir` only removes an empty directory, so a remaining race
        // can at worst remove an empty directory that is inside home.
        if claimed.contains(dir) {
            break;
        }
        match physical.read_dir() {
            Ok(mut entries) => {
                if entries.next().is_some() {
                    break;
                }
            }
            Err(err) => {
                give_up(dir, err);
                break;
            }
        }
        debug!("files: removing empty directory {}", dir.display_user());
        if let Err(err) = std::fs::remove_dir(&physical) {
            give_up(dir, err);
            break;
        }
        settled.push(dir.clone());
    }
    settled
}

/// The directories recorded as created for `req`'s target, if its kind
/// records them.
fn recorded_created_dirs(req: &FileRequest) -> Vec<PathBuf> {
    if !records_created_dirs(req) {
        return vec![];
    }
    load_target_state(req)
        .map(|state| state.created_dirs)
        .unwrap_or_default()
}

/// The recorded directories an apply that removes `req`'s target may prune.
/// Records are keyed by target, so an absent entry finds the one a copy,
/// symlink, template, or content entry left for the same path.
fn removal_created_dirs(req: &FileRequest) -> Vec<PathBuf> {
    if req.mode == FileMode::Absent {
        load_target_state(req)
            .map(|state| state.created_dirs)
            .unwrap_or_default()
    } else {
        recorded_created_dirs(req)
    }
}

/// Whether the record for `req`'s already-removed target still lists a
/// directory mise created that is there to prune.
fn has_leftover_created_dirs(req: &FileRequest) -> bool {
    created_dirs_to_prune(&req.target, &removal_created_dirs(req), &dirs::HOME)
        .iter()
        .any(|dir| dir.is_dir())
}

/// The pruning pass for a run: the targets it removed, and the converged
/// ones whose leftover directories it retries, each with its record.
fn prune_after_apply<'a>(removed: impl IntoIterator<Item = &'a FileRequest>, plan: &ApplyPlan<'a>) {
    let removals = removed
        .into_iter()
        .chain(plan.prune_leftovers.iter().copied())
        .map(|req| {
            if plan.prune_leftovers.iter().any(|r| std::ptr::eq(*r, req)) {
                debug!(
                    "files: retrying the directories created for {}",
                    req.target.display_user()
                );
            }
            (req, removal_created_dirs(req))
        })
        .collect::<Vec<_>>();
    prune_created_dirs(&removals, &plan.claimed_dirs, &dirs::HOME, true);
}

/// Whether this apply of `req` removes its target.
fn apply_removes_target(req: &FileRequest, rendered: Option<&str>) -> bool {
    req.mode == FileMode::Absent || removes_target(req, rendered)
}

/// Remove the directories mise created for targets it has just removed,
/// each paired with its recorded directories. This runs once every target
/// of the batch is gone, and each walk counts the directories recorded by
/// any of them: only the first entry written into a new directory records
/// it, so a directory shared by sibling targets still goes, whatever order
/// they were removed in. Each walk is journaled on its own. With
/// `update_records`, the removed directories are dropped from the records
/// that held them, which stay for the ownership evidence they hold.
///
/// This never fails: the targets are already removed, and an error here
/// would skip the steps after it while the next apply saw the targets as
/// converged. A directory that cannot be removed is warned about and stays
/// in its record, so a later removal retries it.
fn prune_created_dirs(
    removals: &[(&FileRequest, Vec<PathBuf>)],
    claimed: &HashSet<PathBuf>,
    home: &Path,
    update_records: bool,
) {
    let created = removals
        .iter()
        .flat_map(|(_, dirs)| dirs.iter().cloned())
        .unique()
        .collect::<Vec<_>>();
    if created.is_empty() {
        return;
    }
    for (req, _) in removals
        .iter()
        .sorted_by_key(|(req, _)| std::cmp::Reverse(req.target.components().count()))
    {
        let chain = created_dirs_to_prune(&req.target, &created, home);
        if !chain.iter().any(|dir| dir.is_dir()) {
            continue;
        }
        let holders = removals
            .iter()
            .filter(|(_, dirs)| update_records && dirs.iter().any(|dir| chain.contains(dir)))
            .map(|(holder, _)| *holder)
            .collect::<Vec<_>>();
        // the directories and the records that list them change together
        let paths = chain
            .iter()
            .map(|dir| (dir.clone(), Capture::Shallow))
            .chain(
                holders
                    .iter()
                    .map(|holder| (target_state_path(holder), Capture::Full)),
            )
            .collect::<Vec<_>>();
        // nothing is removed without its write-ahead record
        let pending = match journal::begin_changes_with(DOTFILES_PART, &req.target_raw, paths) {
            Ok(pending) => pending,
            Err(err) => {
                warn!(
                    "files: keeping the directories created for {}: {err:#}",
                    req.target.display_user()
                );
                continue;
            }
        };
        let settled = remove_created_dirs(&chain, claimed, home);
        if !settled.is_empty() {
            for holder in &holders {
                update_target_state(holder, |state| {
                    state.created_dirs.retain(|dir| !settled.contains(dir));
                });
            }
        }
        journal::commit_changes(pending);
    }
}

/// The recorded directories a removal of `req`'s target may take with it,
/// deepest first, for dry runs.
fn prunable_created_dirs(req: &FileRequest) -> Vec<PathBuf> {
    created_dirs_to_prune(&req.target, &recorded_created_dirs(req), &dirs::HOME)
}

/// Directories other entries need to stay: their targets, and for entries
/// that walk a source directory every directory their files go in. An absent
/// entry needs nothing.
fn claimed_dirs<'a>(
    requests: impl IntoIterator<Item = &'a FileRequest>,
) -> Result<HashSet<PathBuf>> {
    let mut out = HashSet::new();
    for req in requests {
        if req.mode == FileMode::Absent {
            continue;
        }
        if matches!(req.mode, FileMode::Copy | FileMode::SymlinkEach) && req.source.is_dir() {
            out.extend(needed_dirs(req)?);
        }
        out.insert(req.target.clone());
    }
    Ok(out)
}

fn content_digest(content: &str) -> String {
    hash_sha256_to_str(content)
}

/// Whether the record already holds `rendered` as the target's content.
fn target_state_matches(req: &FileRequest, rendered: &str) -> bool {
    load_target_state(req)
        .and_then(|state| state.content_digest)
        .is_some_and(|digest| digest == content_digest(rendered))
}

/// Empty means nothing but whitespace, so a template that leaves a stray
/// newline between `{% if %}` blocks still counts.
fn renders_empty(rendered: &str) -> bool {
    rendered.trim().is_empty()
}

/// Whether applying `req` with this render removes its target instead of
/// writing it.
pub(crate) fn removes_target(req: &FileRequest, rendered: Option<&str>) -> bool {
    req.mode == FileMode::Template && req.remove_empty && rendered.is_some_and(renders_empty)
}

/// Classify the target of a template that rendered empty. Only an empty file
/// or one still holding exactly what mise last wrote is mise's to remove;
/// anything else may hold a user's edits.
fn empty_render_target(req: &FileRequest) -> Result<EmptyRenderTarget> {
    let target = &req.target;
    if target.is_symlink() {
        return Ok(EmptyRenderTarget::Conflict("it is a symlink"));
    }
    if !target.exists() {
        return Ok(EmptyRenderTarget::Absent);
    }
    if target.is_dir() {
        return Ok(EmptyRenderTarget::Conflict("it is a directory"));
    }
    // ownership cannot be proven without reading the content (a template
    // written with `permissions = "0200"`, say), so only --force removes it
    let Ok(current) = file::read(target) else {
        return Ok(EmptyRenderTarget::Conflict(
            "it cannot be read to confirm mise wrote it",
        ));
    };
    let owned = match str::from_utf8(&current) {
        Ok(current) => renders_empty(current) || target_state_matches(req, current),
        // mise only writes rendered text, so non-UTF-8 content is not its own
        Err(_) => false,
    };
    Ok(if owned {
        EmptyRenderTarget::Owned
    } else {
        EmptyRenderTarget::Conflict("it changed since mise last wrote it")
    })
}

fn link_points_to(source: &Path, target: &Path) -> bool {
    if !target.is_symlink() {
        return false;
    }
    std::fs::read_link(target).is_ok_and(|dest| {
        dest == source
            || points_at_same_file(target, source)
            || if dest.is_absolute() {
                lexical_normalize(&dest) == lexical_normalize(source)
            } else {
                // the kernel resolves a relative link from the directory the
                // link physically sits in, so its text is read from there too:
                // read from the configured spelling, a link in a symlinked
                // directory could seem to reach a source it does not
                resolve_relative_link(target, &dest)
                    .is_some_and(|resolved| resolved == physical_path(source))
            }
    })
}

/// Where a link at `target` holding the relative `dest` physically leads,
/// stepping through `dest` from the link's canonical directory as the
/// kernel does (see [`walk_physical`]).
fn resolve_relative_link(target: &Path, dest: &Path) -> Option<PathBuf> {
    walk_physical(target.parent()?.canonicalize().ok()?, dest)
}

/// Where `path` physically is, without requiring it to exist (so a deleted
/// file or a dangling link still has a location). Its own last component is
/// not followed: a source that is a symlink is the link, not what it names.
fn physical_path(path: &Path) -> PathBuf {
    walk_physical(PathBuf::new(), path).unwrap_or_else(|| lexical_normalize(path))
}

/// Append `path` to `base` a component at a time, resolving every directory
/// on the way before a later `..` steps out of it: `alias/../x` climbs out
/// of wherever `alias` really leads, so collapsing it lexically first could
/// name a different file. A component that does not exist cannot be a
/// symlink and is kept as written; a dangling one in the middle has no
/// location at all.
fn walk_physical(mut cur: PathBuf, path: &Path) -> Option<PathBuf> {
    use std::path::Component;
    let components = path.components().collect::<Vec<_>>();
    for (i, component) in components.iter().enumerate() {
        match component {
            Component::Prefix(_) | Component::RootDir => cur.push(component.as_os_str()),
            Component::CurDir => {}
            Component::ParentDir => {
                cur.pop();
            }
            Component::Normal(name) => {
                cur.push(name);
                if i + 1 < components.len() {
                    match cur.canonicalize() {
                        Ok(canonical) => cur = canonical,
                        Err(_) if std::fs::symlink_metadata(&cur).is_err() => {}
                        Err(_) => return None,
                    }
                }
            }
        }
    }
    Some(cur)
}

fn tracked_stale_links(state: &SymlinkEachState, desired: &SymlinkEachState) -> Vec<PathBuf> {
    let desired = desired
        .links
        .iter()
        .map(|link| (&link.target, &link.source))
        .collect::<std::collections::HashMap<_, _>>();
    state
        .links
        .iter()
        .filter(|link| {
            desired
                .get(&link.target)
                .is_none_or(|source| **source != link.source)
                && link_points_to(&link.source, &link.target)
        })
        .map(|link| link.target.clone())
        .collect()
}

/// The source-relative path a symlink found at `rel` under an entry's target
/// would have been deployed from. Without `dot_prefix` the two are the same
/// path. With it, `.bashrc` could come from `dot-bashrc` or `.bashrc`, so the
/// link's own destination decides, and a link into neither is not the entry's.
fn linked_source_rel(req: &FileRequest, link: &Path, rel: &Path, dest: &Path) -> Option<PathBuf> {
    if !req.dot_prefix {
        return Some(rel.to_path_buf());
    }
    // a relative destination is read from where the link physically sits,
    // as the kernel reads it (see `link_points_to`)
    let (dest, source) = if dest.is_absolute() {
        (lexical_normalize(dest), lexical_normalize(&req.source))
    } else {
        // the source directory itself may be a symlink, which a link
        // resolved physically has already stepped through
        let source = req
            .source
            .canonicalize()
            .unwrap_or_else(|_| physical_path(&req.source));
        (resolve_relative_link(link, dest)?, source)
    };
    let source_rel = dest.strip_prefix(source).ok()?.to_path_buf();
    (target_rel(req, &source_rel) == rel).then_some(source_rel)
}

/// Whether a link's destination names `expected`, even when `expected` no
/// longer exists. Compared as paths, so the `.` in a source like
/// `/dotfiles/.` doesn't have to match character for character; a relative
/// destination (`relative` or `dotfiles.relative_symlinks`) is resolved from
/// where the link physically sits, like [`link_points_to`] does.
fn link_points_at(link: &Path, dest: &Path, expected: &Path) -> bool {
    dest == expected
        || if dest.is_absolute() {
            lexical_normalize(dest) == lexical_normalize(expected)
        } else {
            resolve_relative_link(link, dest)
                .is_some_and(|resolved| resolved == physical_path(expected))
        }
}

/// Legacy ownership discovery for installations that predate persistent
/// symlink-each state. A successful apply records the exact links it owns, so
/// this unbounded target walk happens at most once per target.
fn legacy_stale_links(req: &FileRequest) -> Result<Vec<PathBuf>> {
    if cfg!(windows) || !req.target.is_dir() || req.target.is_symlink() {
        return Ok(vec![]);
    }
    let mut out = vec![];
    for entry in walkdir::WalkDir::new(&req.target).sort_by_file_name() {
        // a path we can't read shouldn't fail the whole entry — the links we
        // can see are still worth pruning
        let entry = match entry {
            Ok(entry) => entry,
            Err(err) => {
                debug!("files: walking {}: {err}", req.target.display_user());
                continue;
            }
        };
        if !entry.file_type().is_symlink() {
            continue;
        }
        let dest = match std::fs::read_link(entry.path()) {
            Ok(dest) => dest,
            Err(err) => {
                debug!("files: reading {}: {err}", entry.path().display_user());
                continue;
            }
        };
        let Ok(rel) = entry.path().strip_prefix(&req.target) else {
            continue;
        };
        let Some(source_rel) = linked_source_rel(req, entry.path(), rel, &dest) else {
            continue;
        };
        let expected = req.source.join(&source_rel);
        if link_points_at(entry.path(), &dest, &expected)
            && (!expected.exists() || is_excluded(&source_rel, &req.exclude))
        {
            out.push(entry.path().to_path_buf());
        }
    }
    Ok(out)
}

/// Links recorded for this target that are no longer desired. Persistent
/// ownership state keeps this proportional to the managed source tree instead
/// of recursively walking a shared target such as the user's home directory.
fn stale_links(req: &FileRequest) -> Result<Vec<PathBuf>> {
    match load_symlink_each_state(req) {
        LoadedSymlinkEachState::Present(state) => Ok(tracked_stale_links(
            &state,
            &desired_symlink_each_state(req)?,
        )),
        LoadedSymlinkEachState::Missing | LoadedSymlinkEachState::Invalid => {
            legacy_stale_links(req)
        }
    }
}

fn symlink_each_state_needs_update(req: &FileRequest) -> Result<bool> {
    if cfg!(windows) {
        return Ok(false);
    }
    let desired = desired_symlink_each_state(req)?;
    Ok(!matches!(
        load_symlink_each_state(req),
        LoadedSymlinkEachState::Present(state) if state == desired
    ))
}

/// Compiled once here so a typo is reported against the entry that wrote
/// it, not on every walk of the source (or of a tracked directory).
/// Compiles a per-entry `exclude` or `include` list, or names the first
/// pattern that will not parse.
///
/// **A list that does not compile is an error, never a shorter list.**
/// Dropping a bad pattern fails open in both directions: a shorter
/// `exclude` captures files the user asked to leave out, and a shorter
/// `include` — or an empty one — captures the whole tree the user asked
/// to narrow. Neither is something to warn about and carry on from.
fn compile_patterns(
    key: &str,
    patterns: Option<Vec<String>>,
) -> std::result::Result<Option<Vec<glob::Pattern>>, String> {
    let Some(patterns) = patterns else {
        return Ok(None);
    };
    let mut compiled = vec![];
    for pattern in patterns {
        match glob::Pattern::new(&pattern) {
            Ok(pattern) => compiled.push(pattern),
            Err(err) => return Err(format!("invalid {key} pattern '{pattern}': {err}")),
        }
    }
    Ok(Some(compiled))
}

/// Whether a source-relative (or entry-relative) path is dropped by the
/// entry's `exclude` patterns. A pattern without `/` matches any single
/// path component, so `exclude = ["mise.toml"]` drops that file wherever
/// it sits in the tree and `["*.md"]` drops every markdown file; a pattern
/// containing `/` is anchored to the source root. Either kind matching a
/// directory takes everything under it, which is why ancestors are tested
/// too. Track entries use the same rules relative to the tracked path.
pub(crate) fn is_excluded(rel: &Path, patterns: &[glob::Pattern]) -> bool {
    patterns.iter().any(|pattern| {
        if pattern.as_str().contains('/') {
            rel.ancestors().any(|a| pattern.matches_path(a))
        } else {
            rel.components()
                .any(|c| pattern.matches(&c.as_os_str().to_string_lossy()))
        }
    })
}

/// every (source file, target path) pair of a directory-walking entry —
/// `symlink-each`, and `copy` with a directory source
fn walk_source_files(req: &FileRequest) -> Result<Vec<(PathBuf, PathBuf)>> {
    let files = walk_source_files_unchecked(req)?;
    if req.dot_prefix {
        check_dot_prefix_collisions(req, &files)?;
    }
    Ok(files)
}

/// Every (source file, target path) pair of a directory-walking entry, for
/// builds that skip apply's footprint validation: a `dot_prefix` source must
/// be a directory, and no two of its paths may deploy to the same place.
pub(crate) fn directory_source_files(req: &FileRequest) -> Result<Vec<(PathBuf, PathBuf)>> {
    if req.dot_prefix && !req.source.is_dir() {
        return Err(dot_prefix_file_source(req));
    }
    walk_source_files(req)
}

/// `dot_prefix` renames paths inside a directory; a single file keeps the
/// target its entry names, so the option would silently do nothing.
fn dot_prefix_file_source(req: &FileRequest) -> eyre::Report {
    eyre::eyre!(
        "[dotfiles].\"{}\": dot_prefix requires the source to be a directory: {}",
        req.target_raw,
        req.source.display_user()
    )
}

/// The path under an entry's target that a source-relative path deploys to.
pub(crate) fn target_rel(req: &FileRequest, rel: &Path) -> PathBuf {
    if !req.dot_prefix {
        return rel.to_path_buf();
    }
    rel.components()
        .map(|component| match component {
            std::path::Component::Normal(name) => {
                match name.to_str().and_then(|name| name.strip_prefix("dot-")) {
                    // `dot-` and `dot-.` would name `.` and `..`
                    Some(rest) if !rest.is_empty() && rest != "." => {
                        std::ffi::OsString::from(format!(".{rest}"))
                    }
                    _ => name.to_os_string(),
                }
            }
            other => other.as_os_str().to_os_string(),
        })
        .collect()
}

/// With `dot_prefix`, `dot-bashrc` and `.bashrc` in one source both deploy
/// to `.bashrc`, and a `dot-config/` directory beside a `.config` file puts
/// a directory where a file goes. Neither has a right answer to pick.
fn check_dot_prefix_collisions(req: &FileRequest, files: &[(PathBuf, PathBuf)]) -> Result<()> {
    let by_target: HashMap<&Path, &Path> = files
        .iter()
        .map(|(source, target)| (target.as_path(), source.as_path()))
        .collect();
    for (source, target) in files {
        if let Some(other) = by_target.get(target.as_path())
            && *other != source.as_path()
        {
            bail!(
                "[dotfiles].\"{}\": {} and {} both deploy to {} with dot_prefix",
                req.target_raw,
                source.display_user(),
                other.display_user(),
                target.display_user()
            );
        }
        for ancestor in target.ancestors().skip(1) {
            if !ancestor.starts_with(&req.target) {
                break;
            }
            if let Some(other) = by_target.get(ancestor) {
                bail!(
                    "[dotfiles].\"{}\": {} deploys to {}, but {} needs it to be a directory with dot_prefix",
                    req.target_raw,
                    other.display_user(),
                    ancestor.display_user(),
                    source.display_user()
                );
            }
        }
    }
    Ok(())
}

fn walk_source_files_unchecked(req: &FileRequest) -> Result<Vec<(PathBuf, PathBuf)>> {
    if req.manifest == Some(FileManifest::Git) {
        return git_tracked_paths(&req.source)?.into_iter().try_fold(
            vec![],
            |mut out, entry| -> Result<_> {
                if entry.is_gitlink || is_excluded(&entry.path, &req.exclude) {
                    return Ok(out);
                }
                let source = req.source.join(&entry.path);
                match std::fs::symlink_metadata(&source) {
                    Ok(metadata) if !metadata.file_type().is_dir() => {
                        out.push((source, req.target.join(target_rel(req, &entry.path))));
                    }
                    Ok(_) => {}
                    Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
                    Err(err) => return Err(err.into()),
                }
                Ok(out)
            },
        );
    }
    let mut out = vec![];
    let mut walk = walkdir::WalkDir::new(&req.source)
        .sort_by_file_name()
        .into_iter();
    while let Some(entry) = walk.next() {
        let entry = entry?;
        let rel = entry.path().strip_prefix(&req.source)?;
        if !rel.as_os_str().is_empty() && is_excluded(rel, &req.exclude) {
            // don't descend into an excluded directory — its children are
            // excluded by the same pattern, just more slowly
            if entry.file_type().is_dir() {
                walk.skip_current_dir();
            }
            continue;
        }
        if entry.file_type().is_dir() {
            continue;
        }
        out.push((
            entry.path().to_path_buf(),
            req.target.join(target_rel(req, rel)),
        ));
    }
    Ok(out)
}

struct GitTrackedPath {
    path: PathBuf,
    is_gitlink: bool,
    is_symlink: bool,
}

fn git_tracked_paths(source: &Path) -> Result<Vec<GitTrackedPath>> {
    let mut root_command = Command::new("git");
    root_command
        .arg("-C")
        .arg(source)
        .args(["-c", "safe.directory=*", "rev-parse", "--show-toplevel"])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());
    crate::git::sanitize_git_command(&mut root_command);
    let root_output = root_command.output().wrap_err_with(|| {
        format!(
            "failed to locate Git repository for {}",
            source.display_user()
        )
    })?;
    if !root_output.status.success() {
        let stderr = String::from_utf8_lossy(&root_output.stderr)
            .trim()
            .to_string();
        bail!(
            "failed to locate Git repository for {}: {}",
            source.display_user(),
            if stderr.is_empty() {
                root_output.status.to_string()
            } else {
                stderr
            }
        );
    }
    let root = root_output
        .stdout
        .strip_suffix(b"\n")
        .unwrap_or(&root_output.stdout);
    let root = root.strip_suffix(b"\r").unwrap_or(root);
    let safe = format!("safe.directory={}", path_buf_from_git_bytes(root).display());
    let mut command = Command::new("git");
    command
        .arg("-C")
        .arg(source)
        .arg("-c")
        .arg(safe)
        .arg("-c")
        .arg("core.autocrlf=false")
        .args(["ls-files", "-z", "--cached", "--stage", "--", "."])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());
    crate::git::sanitize_git_command(&mut command);
    let output = command.output().wrap_err_with(|| {
        format!(
            "failed to list Git-tracked files in {}",
            source.display_user()
        )
    })?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        bail!(
            "failed to list Git-tracked files in {}: {}",
            source.display_user(),
            if stderr.is_empty() {
                output.status.to_string()
            } else {
                stderr
            }
        );
    }
    output
        .stdout
        .split(|byte| *byte == 0)
        .filter(|record| !record.is_empty())
        .map(|record| {
            let tab = record
                .iter()
                .position(|byte| *byte == b'\t')
                .ok_or_else(|| {
                    eyre::eyre!(
                        "unexpected git ls-files output for {}",
                        source.display_user()
                    )
                })?;
            let metadata = &record[..tab];
            let path = path_buf_from_git_bytes(&record[tab + 1..]);
            if !metadata.ends_with(b" 0") {
                bail!(
                    "unresolved Git index entry {} in {}",
                    path.display(),
                    source.display_user()
                );
            }
            Ok(GitTrackedPath {
                is_gitlink: metadata.starts_with(b"160000 "),
                is_symlink: metadata.starts_with(b"120000 "),
                path,
            })
        })
        .collect()
}

/// Capture only the files selected by a Git manifest, preserving the source
/// repository and any untracked files around them.
pub(crate) fn capture_git_manifest(req: &FileRequest) -> Result<()> {
    for entry in git_tracked_paths(&req.source)? {
        if entry.is_gitlink || entry.is_symlink || is_excluded(&entry.path, &req.exclude) {
            continue;
        }
        let from = req.target.join(target_rel(req, &entry.path));
        let to = req.source.join(entry.path);
        if from.exists() || from.is_symlink() {
            if !file::same_file(&from, &to) {
                copy_path(&from, &to)?;
            }
        } else if to.exists() || to.is_symlink() {
            remove_existing(&to)?;
        }
    }
    Ok(())
}

#[cfg(unix)]
fn path_buf_from_git_bytes(path: &[u8]) -> PathBuf {
    use std::os::unix::ffi::OsStringExt;
    std::ffi::OsString::from_vec(path.to_vec()).into()
}

#[cfg(not(unix))]
fn path_buf_from_git_bytes(path: &[u8]) -> PathBuf {
    String::from_utf8_lossy(path).into_owned().into()
}

pub(crate) struct ApplyOpts {
    pub dry_run: bool,
    pub verbose: bool,
    /// replace conflicting targets (existing real files where a symlink
    /// should go, or type mismatches) instead of erroring
    pub force: bool,
    pub force_hint: &'static str,
    pub yes: bool,
}

pub(crate) struct ApplyPlan<'a> {
    todo: Vec<(&'a FileRequest, Option<String>)>,
    record_symlink_each: Vec<&'a FileRequest>,
    /// converged templates whose ownership record is missing or stale, with
    /// the content to record
    record_templates: Vec<(&'a FileRequest, String)>,
    /// converged entries whose target is already gone but whose record
    /// still lists directories mise created that are there: an earlier
    /// prune could not remove them, so this run retries. Not a file change,
    /// so never shown as one.
    prune_leftovers: Vec<&'a FileRequest>,
    /// directories active entries need, which a removed target never takes
    /// along; only computed when a removal has directories it could prune
    claimed_dirs: HashSet<PathBuf>,
    reconciliation: SymlinkEachReconciliation,
}

/// Apply all entries that aren't already in the desired state. Conflicting
/// targets (a real file where a symlink should go, a directory where a file
/// should go) are an error unless `force` is set — content updates for
/// copy/template entries are not conflicts, overwriting is their job. Returns
/// `false` when the user declines the confirmation prompt. The target paths
/// written or removed are appended to `written` as each entry is applied,
/// so a caller still sees what changed when a later entry fails; nothing is
/// appended on a dry run.
pub(crate) fn apply(
    config: &Config,
    requests: &[FileRequest],
    opts: &ApplyOpts,
    secrets: &SecretValues,
    written: &mut Vec<PathBuf>,
) -> Result<bool> {
    execute_apply(
        config,
        plan_apply(config, requests, opts, secrets)?,
        opts,
        written,
    )
}

pub(crate) fn execute_apply(
    config: &Config,
    plan: ApplyPlan<'_>,
    opts: &ApplyOpts,
    written: &mut Vec<PathBuf>,
) -> Result<bool> {
    let has_reconciliation = !plan.reconciliation.stale_links.is_empty();
    if plan.todo.is_empty() && !has_reconciliation {
        if !opts.dry_run {
            prune_after_apply([], &plan);
            for req in plan.record_symlink_each {
                let pending = journal::begin_changes(
                    DOTFILES_PART,
                    &req.target_raw,
                    [symlink_each_state_path(req)],
                )?;
                save_symlink_each_state(req);
                journal::commit_changes(pending);
            }
            record_template_states(&plan.record_templates)?;
        }
        info!("files: all files are applied");
        return Ok(true);
    }
    if opts.dry_run {
        for link in &plan.reconciliation.stale_links {
            miseprintln!("rm {}", link.target.display_user());
        }
        for (req, rendered) in &plan.todo {
            // template state wasn't computed (no rendering on dry runs), so
            // the entry may already be converged
            let conditional = req.mode == FileMode::Template && rendered.is_none();
            let suffix = if conditional { " (if changed)" } else { "" };
            miseprintln!("{}{suffix}", describe(req)?);
            if opts.verbose && !conditional {
                print_diff(config, req, rendered.as_deref())?;
            }
        }
        return Ok(true);
    }
    if !opts.yes && console::user_attended_stderr() {
        let list = plan
            .todo
            .iter()
            .map(|(r, _)| r.target_raw.clone())
            .chain(
                plan.reconciliation
                    .targets
                    .iter()
                    .map(|target| target.display_user()),
            )
            .unique()
            .collect::<Vec<_>>()
            .join(", ");
        if !prompt::confirm(format!("files: apply {list}?"))?.is_yes() {
            info!("files: skipped");
            return Ok(false);
        }
    }
    for link in &plan.reconciliation.stale_links {
        if link_points_to(&link.source, &link.target) {
            let item = link.target.display_user().to_string();
            // parents up to the entry target may be pruned once empty
            let root = plan
                .reconciliation
                .targets
                .iter()
                .find(|target| link.target.starts_with(target));
            let mut paths = vec![(link.target.clone(), Capture::Full)];
            if let Some(root) = root {
                paths.extend(
                    dirs_between(&link.target, root)
                        .into_iter()
                        .map(|dir| (dir, Capture::Shallow)),
                );
            }
            let pending = journal::begin_changes_with(DOTFILES_PART, &item, paths)?;
            file::remove_file(&link.target)?;
            written.push(link.target.clone());
            journal::commit_changes(pending);
        }
    }
    let mut removals = vec![];
    for (req, rendered) in &plan.todo {
        recheck_removal(req, rendered.as_deref(), opts.force)?;
        let pending =
            journal::begin_changes_with(DOTFILES_PART, &req.target_raw, touched_paths(req)?)?;
        apply_one(req, rendered.as_deref(), written)?;
        if apply_removes_target(req, rendered.as_deref()) {
            removals.push(*req);
        }
        if req.mode == FileMode::SymlinkEach {
            save_symlink_each_state(req);
        }
        journal::commit_changes(pending);
        if removes_target(req, rendered.as_deref()) {
            info!(
                "files: removed {} (template rendered empty)",
                req.target.display_user()
            );
        } else {
            info!("files: {}", describe_applied(req)?);
        }
    }
    prune_after_apply(removals, &plan);
    record_template_states(&plan.record_templates)?;
    for req in plan.record_symlink_each {
        if !plan.todo.iter().any(|(todo, _)| std::ptr::eq(*todo, req)) {
            let pending = journal::begin_changes(
                DOTFILES_PART,
                &req.target_raw,
                [symlink_each_state_path(req)],
            )?;
            save_symlink_each_state(req);
            journal::commit_changes(pending);
        }
    }
    cleanup_reconciled_directories(&plan.reconciliation)?;
    let applied = plan
        .todo
        .iter()
        .map(|(r, _)| r.target_raw.clone())
        .chain(
            plan.reconciliation
                .targets
                .iter()
                .map(|target| target.display_user()),
        )
        .unique()
        .collect::<Vec<_>>();
    info!("files: applied {}", applied.join(", "));
    Ok(true)
}

/// Plan and validate an apply without changing targets. Templates are rendered
/// here so execution writes exactly the content that was validated.
pub(crate) fn plan_apply<'a>(
    config: &Config,
    requests: &'a [FileRequest],
    opts: &ApplyOpts,
    secrets: &SecretValues,
) -> Result<ApplyPlan<'a>> {
    let active_requests = files_from_config(config)?;
    plan_apply_with_active(config, requests, &active_requests, opts, secrets)
}

/// Plan an apply against the requests that will be active when it executes.
/// This is used by transactional config updates that apply before saving the
/// prospective configuration.
pub(crate) fn plan_apply_with_active<'a>(
    config: &Config,
    requests: &'a [FileRequest],
    active_requests: &[FileRequest],
    opts: &ApplyOpts,
    secrets: &SecretValues,
) -> Result<ApplyPlan<'a>> {
    validate_composed_file_footprints(requests)?;
    // pre-rendered template output rides along so it's written as compared,
    // and exec() in templates runs once per apply
    let mut todo: Vec<(&FileRequest, Option<String>)> = vec![];
    let mut missing_sources = vec![];
    let mut broken = vec![];
    let mut conflicts = vec![];
    let mut record_symlink_each = vec![];
    let mut record_templates = vec![];
    let mut prune_leftovers = vec![];
    let mut missing_permission_targets = vec![];
    for req in requests {
        // a tracked file is never written: history captures it as it is
        if req.mode == FileMode::Track {
            continue;
        }
        // report every problem in one pass instead of fix-and-retry — a
        // render or check failure on one entry must not hide the rest
        if req.mode.has_source() && !req.source.exists() {
            missing_sources.push(format!(
                "  [dotfiles].\"{}\": {}",
                req.target_raw,
                req.source.display_user()
            ));
            continue;
        }
        // a permissions-only entry never creates its target and never
        // follows a link there: nothing to do is not an error. A missing
        // directory another entry of this apply creates is decided once
        // every entry is planned.
        if req.mode == FileMode::Permissions
            && let Some(reason) = permissions_target_unavailable(req)?
        {
            if std::fs::symlink_metadata(&req.target).is_err() {
                missing_permission_targets.push((req, reason));
            } else {
                warn!(
                    "[dotfiles].\"{}\": {reason}; permissions not set",
                    req.target_raw
                );
            }
            continue;
        }
        // rendering can run exec() — a dry run must not execute anything,
        // so list template entries without computing their current state
        if opts.dry_run && req.mode == FileMode::Template {
            conflicts.extend(find_conflicts(req)?);
            todo.push((req, None));
            continue;
        }
        let rendered = match req.mode {
            FileMode::Template => match render_template(config, req, secrets) {
                Ok(rendered) => Some(rendered),
                // already carries the entry's context
                Err(err) => {
                    broken.push(format!("  {err}"));
                    continue;
                }
            },
            _ => None,
        };
        match check_rendered(req, rendered.as_deref()) {
            Ok(FileState::Applied) => {
                // a target already gone can still leave directories an
                // earlier prune could not remove; retry them after the run
                if apply_removes_target(req, rendered.as_deref()) && has_leftover_created_dirs(req)
                {
                    prune_leftovers.push(req);
                }
                if req.mode == FileMode::SymlinkEach && symlink_each_state_needs_update(req)? {
                    record_symlink_each.push(req);
                }
                if let Some(update) = template_state_update(req, rendered) {
                    record_templates.push((req, update));
                }
                continue;
            }
            Ok(_) => {}
            Err(err) => {
                broken.push(format!("  [dotfiles].\"{}\": {err}", req.target_raw));
                continue;
            }
        }
        if removes_target(req, rendered.as_deref()) {
            if matches!(empty_render_target(req)?, EmptyRenderTarget::Conflict(_)) {
                conflicts.push(req.target.clone());
            }
        } else {
            conflicts.extend(find_conflicts(req)?);
        }
        todo.push((req, rendered));
    }
    let mut problems = vec![];
    if !missing_sources.is_empty() {
        problems.push(format!(
            "sources do not exist:\n{}",
            missing_sources.join("\n")
        ));
    }
    if !broken.is_empty() {
        problems.push(format!("entries with errors:\n{}", broken.join("\n")));
    }
    if !conflicts.is_empty() && !opts.force {
        problems.push(format!(
            "refusing to overwrite existing files ({}):\n{}",
            opts.force_hint,
            conflicts
                .iter()
                .map(|p| format!("  {}", p.display_user()))
                .collect::<Vec<_>>()
                .join("\n")
        ));
    }
    if !problems.is_empty() {
        bail!("files: {}", problems.join("\nfiles: "));
    }
    // entries run in order, so one that creates a directory a
    // permissions-only entry names has made it by the time the chmod runs
    let mut deferred = vec![];
    for (req, reason) in missing_permission_targets {
        if todo.iter().any(|(other, _)| {
            // an absent entry removes rather than creates
            !matches!(other.mode, FileMode::Permissions | FileMode::Absent)
                && other.target.starts_with(&req.target)
        }) {
            deferred.push((req, None));
        } else {
            warn!(
                "[dotfiles].\"{}\": {reason}; permissions not set",
                req.target_raw
            );
        }
    }
    todo.extend(deferred);
    let claimed_dirs = if !prune_leftovers.is_empty()
        || todo.iter().any(|(req, rendered)| {
            apply_removes_target(req, rendered.as_deref()) && !removal_created_dirs(req).is_empty()
        }) {
        claimed_dirs(active_requests)?
    } else {
        HashSet::new()
    };
    Ok(ApplyPlan {
        todo,
        record_symlink_each,
        record_templates,
        prune_leftovers,
        claimed_dirs,
        reconciliation: plan_symlink_each_reconciliation(active_requests, requests)?,
    })
}

/// The plan classified a removal's target before the confirmation prompt; an
/// edit made since then must not be removed without `--force`.
fn recheck_removal(req: &FileRequest, rendered: Option<&str>, force: bool) -> Result<()> {
    if !force
        && removes_target(req, rendered)
        && let EmptyRenderTarget::Conflict(reason) = empty_render_target(req)?
    {
        bail!(
            "files: {} changed during apply: {reason}; use --force to remove it",
            req.target.display_user()
        );
    }
    Ok(())
}

/// The ownership-record change a converged template needs, if any. Recording
/// every template, not just `remove_empty` ones, means turning `remove_empty`
/// on later still finds the evidence that the current file is mise's.
fn template_state_update(req: &FileRequest, rendered: Option<String>) -> Option<String> {
    if req.mode != FileMode::Template {
        return None;
    }
    let rendered = rendered?;
    // a converged removal wrote nothing; the record keeps the last write
    if removes_target(req, Some(&rendered)) || target_state_matches(req, &rendered) {
        None
    } else {
        Some(rendered)
    }
}

/// Journal and write the records of converged templates.
fn record_template_states(updates: &[(&FileRequest, String)]) -> Result<()> {
    for (req, content) in updates {
        let pending =
            journal::begin_changes(DOTFILES_PART, &req.target_raw, [target_state_path(req)])?;
        save_target_state(req, content);
        journal::commit_changes(pending);
    }
    Ok(())
}

fn cleanup_reconciled_directories(reconciliation: &SymlinkEachReconciliation) -> Result<()> {
    for target in &reconciliation.targets {
        remove_empty_dirs_upward(
            reconciliation
                .stale_links
                .iter()
                .filter(|link| link.target.starts_with(target))
                .filter_map(|link| link.target.parent()),
            |dir| dir != target && dir.starts_with(target),
        )?;
    }
    Ok(())
}

pub(crate) struct UnapplyOpts {
    pub dry_run: bool,
    pub verbose: bool,
    /// remove targets whose ownership cannot be verified from their current
    /// state (for example a modified copy)
    pub force: bool,
    pub yes: bool,
}

#[derive(Debug)]
pub(crate) struct UnapplyPlan<'a> {
    req: &'a FileRequest,
    paths: Vec<PathBuf>,
    /// directory-walking modes share their target with unmanaged files, so
    /// only remove directories after their managed children are gone and only
    /// while they are empty
    cleanup_empty_dirs: bool,
    /// template dry-runs do not render, so the removal is conditional
    conditional: bool,
    /// remove persistent symlink-each ownership after successful cleanup
    clear_symlink_each_state: bool,
}

/// Remove configured whole-file entries without recursively deleting
/// directories that may contain unmanaged files. Symlinks carry their own
/// ownership evidence. Copies and templates must still match their source
/// unless `--force` was given.
pub(crate) fn plan_unapply<'a>(
    requests: &'a [FileRequest],
    opts: &UnapplyOpts,
) -> Result<Vec<UnapplyPlan<'a>>> {
    let mut todo = vec![];
    let mut problems = vec![];
    for req in requests {
        match plan_unapply_one(req, opts) {
            Ok(Some(plan)) => todo.push(plan),
            Ok(None) => {}
            Err(err) => problems.push(format!("  [dotfiles].\"{}\": {err}", req.target_raw)),
        }
    }
    if !problems.is_empty() {
        bail!(
            "files: cannot unapply these entries:\n{}",
            problems.join("\n")
        );
    }
    Ok(todo)
}

/// Resolve checks that may execute user-authored template functions. This runs
/// only after interactive confirmation, but still before any mutation.
pub(crate) fn resolve_unapply(
    config: &Config,
    plans: &mut Vec<UnapplyPlan<'_>>,
    opts: &UnapplyOpts,
    secrets: &SecretValues,
) -> Result<()> {
    if opts.dry_run {
        return Ok(());
    }
    let mut problems = vec![];
    let rendered = plans
        .iter()
        .map(|plan| {
            if plan.conditional {
                match render_template(config, plan.req, secrets) {
                    Ok(rendered) => Some(rendered),
                    Err(err) => {
                        problems.push(format!("  [dotfiles].\"{}\": {err}", plan.req.target_raw));
                        None
                    }
                }
            } else {
                None
            }
        })
        .collect::<Vec<_>>();
    if !problems.is_empty() {
        bail!(
            "files: cannot unapply these entries:\n{}",
            problems.join("\n")
        );
    }

    // Template functions can change any selected target. Refresh every plan
    // only after all templates have rendered so no stale validation is used.
    let mut refreshed = Vec::with_capacity(plans.len());
    for (plan, rendered) in std::mem::take(plans).into_iter().zip(rendered) {
        let result = if let Some(rendered) = rendered {
            let mut paths = IndexMap::new();
            plan_expected_content(rendered.as_bytes(), &plan.req.target, false, &mut paths).map(
                |_| {
                    Some(UnapplyPlan {
                        req: plan.req,
                        paths: paths.into_keys().collect(),
                        cleanup_empty_dirs: false,
                        conditional: false,
                        clear_symlink_each_state: false,
                    })
                },
            )
        } else {
            plan_unapply_one(plan.req, opts)
        };
        match result {
            Ok(Some(plan)) => refreshed.push(plan),
            Ok(None) => {}
            Err(err) => problems.push(format!("  [dotfiles].\"{}\": {err}", plan.req.target_raw)),
        }
    }
    if !problems.is_empty() {
        bail!(
            "files: cannot unapply these entries:\n{}",
            problems.join("\n")
        );
    }
    *plans = refreshed;
    Ok(())
}

pub(crate) fn execute_unapply(
    config: &Config,
    plans: &[UnapplyPlan<'_>],
    opts: &UnapplyOpts,
) -> Result<()> {
    let todo = plans;
    if todo.is_empty() {
        info!("files: all files are unapplied");
        return Ok(());
    }
    if opts.dry_run {
        for plan in todo {
            for path in &plan.paths {
                let suffix = if plan.conditional {
                    " (if unchanged)"
                } else {
                    ""
                };
                miseprintln!("rm {}{suffix}", path.display_user());
            }
            for dir in prunable_created_dirs(plan.req) {
                miseprintln!("rmdir {} (if empty)", dir.display_user());
            }
            if plan.cleanup_empty_dirs {
                miseprintln!("rmdir {} (if empty)", plan.req.target.display_user());
            }
            if opts.verbose && plan.cleanup_empty_dirs {
                miseprintln!(
                    "  preserve unmanaged files under {}",
                    plan.req.target.display_user()
                );
            }
        }
        return Ok(());
    }
    if !opts.yes && console::user_attended_stderr() {
        let list = todo
            .iter()
            .map(|plan| plan.req.target_raw.clone())
            .join(", ");
        if !prompt::confirm(format!("files: unapply {list}?"))?.is_yes() {
            info!("files: skipped");
            return Ok(());
        }
    }
    // The records go with their targets, so they are read up front. A plan
    // with no paths only clears the record of a target that is already gone;
    // the directories mise created for it may still be there and empty.
    let removals = todo
        .iter()
        .map(|plan| (plan.req, recorded_created_dirs(plan.req)))
        .filter(|(_, dirs)| !dirs.is_empty())
        .collect::<Vec<_>>();
    // Directories mise created go only when no entry that stays needs them.
    // Loaded before anything changes, so a config that cannot be read fails
    // the unapply rather than leaving it half done.
    let claimed = if !removals.is_empty() {
        let unapplied = todo
            .iter()
            .map(|plan| lexical_normalize(&plan.req.target))
            .collect::<HashSet<_>>();
        claimed_dirs(
            files_from_config(config)?
                .iter()
                .filter(|req| !unapplied.contains(&lexical_normalize(&req.target))),
        )?
    } else {
        HashSet::new()
    };
    for plan in todo {
        let mut paths: Vec<(PathBuf, Capture)> = plan
            .paths
            .iter()
            .map(|path| (path.clone(), Capture::Full))
            .collect();
        if plan.clear_symlink_each_state {
            paths.push((symlink_each_state_path(plan.req), Capture::Full));
        }
        let clear_target_state = target_state_path(plan.req).exists();
        if clear_target_state {
            paths.push((target_state_path(plan.req), Capture::Full));
        }
        if plan.cleanup_empty_dirs {
            // the upward walk removes directories that end up empty
            for path in &plan.paths {
                paths.extend(
                    dirs_between(path, &plan.req.target)
                        .into_iter()
                        .map(|dir| (dir, Capture::Shallow)),
                );
            }
            paths.push((plan.req.target.clone(), Capture::Shallow));
        }
        let pending = journal::begin_changes_with(DOTFILES_PART, &plan.req.target_raw, paths)?;
        unapply_one(plan)?;
        if plan.clear_symlink_each_state {
            remove_symlink_each_state(plan.req)?;
        }
        if clear_target_state {
            remove_target_state(plan.req)?;
        }
        journal::commit_changes(pending);
    }
    // once every target is gone, so siblings no longer hold their parents
    prune_created_dirs(&removals, &claimed, &dirs::HOME, false);
    info!(
        "files: unapplied {}",
        todo.iter()
            .map(|plan| plan.req.target_raw.clone())
            .join(", ")
    );
    Ok(())
}

fn plan_unapply_one<'a>(
    req: &'a FileRequest,
    opts: &UnapplyOpts,
) -> Result<Option<UnapplyPlan<'a>>> {
    let mut paths = IndexMap::<PathBuf, ()>::new();
    let mut cleanup_empty_dirs = false;
    let mut conditional = false;
    let mut clear_symlink_each_state = false;
    if records_created_dirs(req) && !req.target.exists() && !req.target.is_symlink() {
        // Nothing to remove, but a record left by an earlier removal (a
        // `remove_empty` apply, or the user deleting the file) still claims
        // the path and the directories mise created for it; execute clears
        // the record and prunes those directories with the (empty) plan.
        return Ok(target_state_path(req).exists().then_some(UnapplyPlan {
            req,
            paths: vec![],
            cleanup_empty_dirs: false,
            conditional: false,
            clear_symlink_each_state: false,
        }));
    }
    match req.mode {
        // A Windows file link that came out as a copy is planned by content further down; one
        // that is a real symlink belongs here, where the link target is what identifies it as
        // ours. `plan_expected_content` requires a non-symlink, so routing a symlink there
        // would make unapply demand `--force`.
        FileMode::Symlink
            if !(cfg!(windows) && req.source.is_file() && !req.target.is_symlink()) =>
        {
            if req.target.is_symlink() {
                let dest = std::fs::read_link(&req.target)?;
                // `link_points_to` also resolves a relative link whose source
                // is gone, which `canonicalize` cannot
                if opts.force || link_points_to(&req.source, &req.target) {
                    paths.insert(req.target.clone(), ());
                } else {
                    bail!(
                        "target symlink points to {}, use --force to remove it",
                        dest.display_user()
                    );
                }
            } else if req.target.exists() {
                if opts.force {
                    paths.insert(req.target.clone(), ());
                } else {
                    bail!("target is not the managed symlink, use --force to remove it");
                }
            }
        }
        FileMode::SymlinkEach if !cfg!(windows) => {
            clear_symlink_each_state = symlink_each_state_path(req).exists();
            if req.target.is_symlink() || (req.target.exists() && !req.target.is_dir()) {
                if opts.force {
                    paths.insert(req.target.clone(), ());
                } else {
                    bail!("target is not the managed directory, use --force to remove it");
                }
            } else if req.target.is_dir() {
                for path in owned_links(req)? {
                    paths.insert(path, ());
                }
                cleanup_empty_dirs = true;
            }
        }
        FileMode::Copy | FileMode::SymlinkEach if req.source.is_dir() => {
            if req.target.is_symlink() || (req.target.exists() && !req.target.is_dir()) {
                if opts.force {
                    paths.insert(req.target.clone(), ());
                } else {
                    bail!("target is not the managed directory, use --force to remove it");
                }
            } else if req.target.is_dir() {
                for (source, target) in walk_source_files(req)? {
                    plan_regular_file(&source, &target, opts.force, &mut paths)?;
                }
                cleanup_empty_dirs = true;
            }
        }
        FileMode::Copy => {
            if req.target.is_dir() && !req.source.exists() {
                bail!("source directory is missing, so managed children cannot be identified");
            }
            plan_single_file(req, opts, &mut paths)?;
        }
        // a tracked file was never written by mise: stop tracking it with
        // `mise dot untrack`, the file itself stays
        FileMode::Track => {
            info!(
                "files: {} is tracked in place; nothing to remove (use `mise dot untrack` to stop tracking it)",
                req.target.display_user()
            );
            return Ok(None);
        }
        // undoing an absence would mean recreating a file mise never wrote
        FileMode::Absent => {
            debug!(
                "files: {} is declared absent; nothing to unapply",
                req.target.display_user()
            );
            return Ok(None);
        }
        // mise only changed the permissions of a file it does not own, so
        // unapplying never removes it, not even with --force
        FileMode::Permissions => {
            debug!(
                "files: {} has only its permissions managed; nothing to remove",
                req.target.display_user()
            );
            return Ok(None);
        }
        // an absent target was handled above
        FileMode::Content => {
            if opts.force {
                paths.insert(req.target.clone(), ());
            } else {
                plan_inline_file(req, &mut paths)?;
            }
        }
        FileMode::Symlink => {
            plan_single_file(req, opts, &mut paths)?;
        }
        FileMode::SymlinkEach => {
            bail!("mode symlink-each requires the source to be a directory");
        }
        FileMode::Template => {
            if opts.force {
                paths.insert(req.target.clone(), ());
            } else if opts.dry_run {
                // Rendering may execute commands. Keep dry-run inert, matching
                // apply/status policy, and describe the removal as conditional.
                paths.insert(req.target.clone(), ());
                conditional = true;
            } else {
                if !req.source.exists() {
                    bail!("source is missing; use --force to remove the target");
                }
                // Rendering may execute user-authored commands. Defer it until
                // after the complete unapply plan has been confirmed.
                conditional = true;
            }
        }
    }
    if paths.is_empty() && !cleanup_empty_dirs && !conditional && !clear_symlink_each_state {
        Ok(None)
    } else {
        Ok(Some(UnapplyPlan {
            req,
            paths: paths.into_keys().collect(),
            cleanup_empty_dirs,
            conditional,
            clear_symlink_each_state,
        }))
    }
}

fn plan_inline_file(req: &FileRequest, paths: &mut IndexMap<PathBuf, ()>) -> Result<()> {
    if !req.target.is_symlink()
        && req.target.is_file()
        && file::read(&req.target)? == req.content.as_deref().expect("inline content").as_bytes()
    {
        paths.insert(req.target.clone(), ());
    } else if req.target.exists() || req.target.is_symlink() {
        bail!("target differs from the managed content, use --force to remove it");
    }
    Ok(())
}

fn plan_single_file(
    req: &FileRequest,
    opts: &UnapplyOpts,
    paths: &mut IndexMap<PathBuf, ()>,
) -> Result<()> {
    if !req.source.exists() && req.target.exists() && !opts.force {
        bail!("source is missing; use --force to remove the target");
    }
    if opts.force && (req.target.exists() || req.target.is_symlink()) {
        paths.insert(req.target.clone(), ());
    } else if req.source.exists() {
        plan_regular_file(&req.source, &req.target, false, paths)?;
    }
    Ok(())
}

fn plan_regular_file(
    source: &Path,
    target: &Path,
    force: bool,
    paths: &mut IndexMap<PathBuf, ()>,
) -> Result<()> {
    if !target.exists() && !target.is_symlink() {
        return Ok(());
    }
    if source.is_file() {
        plan_expected_content(&file::read(source)?, target, force, paths)
    } else if force {
        paths.insert(target.to_path_buf(), ());
        Ok(())
    } else {
        bail!(
            "cannot verify {}; use --force to remove it",
            target.display_user()
        )
    }
}

fn plan_expected_content(
    expected: &[u8],
    target: &Path,
    force: bool,
    paths: &mut IndexMap<PathBuf, ()>,
) -> Result<()> {
    if force || (target.is_file() && !target.is_symlink() && file::read(target)? == expected) {
        paths.insert(target.to_path_buf(), ());
        Ok(())
    } else {
        bail!(
            "{} differs from its managed source; use --force to remove it",
            target.display_user()
        )
    }
}

/// All symlinks under a symlink-each target that point exactly where this
/// entry maps that relative path. Unlike stale-link pruning this includes
/// both current and deleted source files because unapply removes the entry's
/// complete observable footprint.
fn legacy_owned_links(req: &FileRequest) -> Result<Vec<PathBuf>> {
    if !req.target.is_dir() || req.target.is_symlink() {
        return Ok(vec![]);
    }
    let mut out = vec![];
    for entry in walkdir::WalkDir::new(&req.target).sort_by_file_name() {
        let entry = match entry {
            Ok(entry) => entry,
            Err(err) => {
                debug!("files: walking {}: {err}", req.target.display_user());
                continue;
            }
        };
        if !entry.file_type().is_symlink() {
            continue;
        }
        let Ok(rel) = entry.path().strip_prefix(&req.target) else {
            continue;
        };
        let dest = match std::fs::read_link(entry.path()) {
            Ok(dest) => dest,
            Err(err) => {
                debug!("files: reading {}: {err}", entry.path().display_user());
                continue;
            }
        };
        let Some(source_rel) = linked_source_rel(req, entry.path(), rel, &dest) else {
            continue;
        };
        // Excluded paths are outside this entry's managed footprint. Even an
        // exact source-shaped link there may have been created by the user.
        if is_excluded(&source_rel, &req.exclude) {
            continue;
        }
        let expected = req.source.join(&source_rel);
        if link_points_at(entry.path(), &dest, &expected)
            || points_at_same_file(entry.path(), &expected)
        {
            out.push(entry.path().to_path_buf());
        }
    }
    Ok(out)
}

fn owned_links(req: &FileRequest) -> Result<Vec<PathBuf>> {
    let state = match load_symlink_each_state(req) {
        LoadedSymlinkEachState::Present(state) => state,
        LoadedSymlinkEachState::Missing | LoadedSymlinkEachState::Invalid => {
            return legacy_owned_links(req);
        }
    };
    let mut out = IndexMap::<PathBuf, ()>::new();
    for link in state.links {
        if link_points_to(&link.source, &link.target) {
            out.insert(link.target, ());
        }
    }
    Ok(out.into_keys().collect())
}

fn unapply_one(plan: &UnapplyPlan<'_>) -> Result<()> {
    let mut parents = vec![];
    for path in &plan.paths {
        debug!("files: removing {}", path.display_user());
        if path.is_symlink() || path.is_file() {
            file::remove_file(path)?;
        } else if path.is_dir() {
            // Only reached for a forced type conflict. The user explicitly
            // asked to remove the declared whole target.
            file::remove_all(path)?;
        }
        if let Some(parent) = path.parent() {
            parents.push(parent.to_path_buf());
        }
    }
    if plan.cleanup_empty_dirs && plan.req.target.is_dir() {
        parents.push(plan.req.target.clone());
        // the walk ends at the target: its parent does not start with it
        remove_empty_dirs_upward(parents.iter().map(PathBuf::as_path), |dir| {
            dir.starts_with(&plan.req.target)
        })?;
    }
    Ok(())
}

/// existing paths this entry would have to delete or replace — not counting
/// content overwrites by copy/template (those are the declared intent) or
/// re-pointing symlinks (always mise-owned territory)
fn find_conflicts(req: &FileRequest) -> Result<Vec<PathBuf>> {
    // A symlink at the target is one mise made, so it is re-pointable. Anything else that is
    // there — a regular file or a directory — belongs to someone else and needs `--force`.
    //
    // Windows used to exempt regular files here, on the grounds that a file link becomes a copy
    // on that platform so an existing file is only a content update. That reasoning holds for
    // `copy` mode, which is declared as overwriting; for a `symlink` entry it meant the copy
    // silently destroyed a file the user wrote, with no `--force` and no message — while unix
    // refused the same apply.
    let file_link_conflicts =
        |target: &Path| -> Result<bool> { Ok(target.exists() && !target.is_symlink()) };
    let mut out = vec![];
    match req.mode {
        FileMode::Symlink => {
            if file_link_conflicts(&req.target)? {
                out.push(req.target.clone());
            }
        }
        FileMode::SymlinkEach => {
            // a regular file where the target directory (or a nested one)
            // should go blocks the whole entry
            for dir in needed_dirs(req)? {
                if dir.exists() && !dir.is_dir() {
                    out.push(dir);
                }
            }
            for (_source, target) in walk_source_files(req)? {
                if file_link_conflicts(&target)? {
                    out.push(target);
                }
            }
        }
        FileMode::Copy | FileMode::Template => {
            // a dir where a file should go (or vice versa) must be removed;
            // file-over-file is an ordinary overwrite
            if req.target.exists() && req.target.is_dir() != req.source.is_dir() {
                out.push(req.target.clone());
            }
        }
        FileMode::Content => {
            if req.target.is_dir() {
                out.push(req.target.clone());
            }
        }
        // removal is the declared intent; a directory is refused by
        // `check_absent` instead, even with --force
        FileMode::Track | FileMode::Absent | FileMode::Permissions => {}
    }
    Ok(out)
}

fn describe(req: &FileRequest) -> Result<String> {
    let src = req.source.display_user();
    let tgt = req.target.display_user();
    // `ln -r` (GNU) is the familiar spelling of a relative link
    let ln = if req.relative { "ln -sfr" } else { "ln -sf" };
    Ok(match req.mode {
        FileMode::Track => format!("track {tgt} in place"),
        FileMode::Absent => format!("rm {tgt}"),
        FileMode::Symlink => format!("{ln} {src} {tgt}"),
        FileMode::SymlinkEach => {
            let stale = stale_links(req)?.len();
            let removals = match stale {
                0 => String::new(),
                n => format!(", rm {n} stale link(s)"),
            };
            format!(
                "{ln} {src}/* into {tgt}/ ({} files){removals}",
                walk_source_files(req)?.len()
            )
        }
        FileMode::Copy if req.source.is_dir() => format!("cp -r {src} {tgt}"),
        FileMode::Copy => format!("cp {src} {tgt}"),
        FileMode::Template => format!("render {src} -> {tgt}"),
        FileMode::Content => format!("write inline content to {tgt}"),
        FileMode::Permissions => format!("chmod {:04o} {tgt}", req.permissions.unwrap_or_default()),
    })
}

fn describe_applied(req: &FileRequest) -> Result<String> {
    let src = req.source.display_user();
    let tgt = req.target.display_user();
    Ok(match req.mode {
        FileMode::Track => format!("tracked {tgt} in place"),
        FileMode::Absent => format!("removed {tgt}"),
        FileMode::Symlink => format!("created symlink {tgt} -> {src}"),
        FileMode::SymlinkEach => format!(
            "created {} symlink(s) from {src} in {tgt}",
            walk_source_files(req)?.len()
        ),
        FileMode::Copy => format!("copied {src} to {tgt}"),
        FileMode::Template => format!("rendered {src} to {tgt}"),
        FileMode::Content => format!("wrote inline content to {tgt}"),
        FileMode::Permissions => format!(
            "set permissions of {tgt} to {:04o}",
            req.permissions.unwrap_or_default()
        ),
    })
}

fn print_diff(config: &Config, req: &FileRequest, rendered: Option<&str>) -> Result<()> {
    if removes_target(req, rendered) {
        match empty_render_target(req)? {
            EmptyRenderTarget::Absent => {}
            EmptyRenderTarget::Owned => {
                miseprintln!(
                    "  template renders empty: remove {}",
                    req.target.display_user()
                );
                if let Some(current) = current_regular_file_for_diff(req)?
                    && !current.is_empty()
                {
                    print_content_diff(config, req, &current, &[])?;
                }
            }
            // apply refuses these without --force, so they are not removals
            EmptyRenderTarget::Conflict(reason) => miseprintln!(
                "  template renders empty, but {reason}: {} is kept unless applied with --force",
                req.target.display_user()
            ),
        }
        return Ok(());
    }
    match req.mode {
        FileMode::Track => {}
        FileMode::Absent => {
            if req.target.is_symlink() {
                let dest = std::fs::read_link(&req.target)?;
                miseprintln!(
                    "  current symlink: {} -> {}",
                    req.target.display_user(),
                    dest.display_user()
                );
            } else {
                miseprintln!("  current: {} exists", req.target.display_user());
            }
            miseprintln!("  desired: {} absent", req.target.display_user());
        }
        FileMode::Symlink => {
            if req.target.is_symlink() {
                let dest = std::fs::read_link(&req.target)?;
                miseprintln!(
                    "  current symlink: {} -> {}",
                    req.target.display_user(),
                    dest.display_user()
                );
            } else if req.target.exists() {
                miseprintln!("  current: {} exists", req.target.display_user());
            } else {
                miseprintln!("  current: {} missing", req.target.display_user());
            }
            miseprintln!(
                "  desired symlink: {} -> {}",
                req.target.display_user(),
                req.source.display_user()
            );
        }
        FileMode::SymlinkEach => {
            miseprintln!(
                "  desired symlink-each: {} files from {}",
                walk_source_files(req)?.len(),
                req.source.display_user()
            );
            for path in stale_links(req)? {
                miseprintln!("  stale link to remove: {}", path.display_user());
            }
        }
        FileMode::Copy | FileMode::Template if req.source.is_file() => {
            let desired = match req.mode {
                FileMode::Template => rendered.unwrap_or_default().as_bytes().to_vec(),
                _ => file::read(&req.source)?,
            };
            if let Some(current) = current_regular_file_for_diff(req)?
                && current != desired
            {
                print_content_diff(config, req, &current, &desired)?;
            }
            print_permissions_diff(req)?;
        }
        FileMode::Copy | FileMode::Template => {
            miseprintln!(
                "  desired directory contents: {} -> {}",
                req.source.display_user(),
                req.target.display_user()
            );
        }
        FileMode::Content => {
            let desired = req.content.as_deref().expect("inline content").as_bytes();
            if let Some(current) = current_regular_file_for_diff(req)?
                && current != desired
            {
                print_content_diff(config, req, &current, desired)?;
            }
            print_permissions_diff(req)?;
        }
        FileMode::Permissions => match permissions_target_unavailable(req)? {
            Some(reason) => miseprintln!("  current: {reason}"),
            None => print_permissions_diff(req)?,
        },
    }
    Ok(())
}

/// Print the permission change apply would make to an existing regular file
/// or directory (never through a symlink).
fn print_permissions_diff(req: &FileRequest) -> Result<()> {
    #[cfg(unix)]
    if !req.target.is_symlink()
        && (req.target.is_file() || req.mode == FileMode::Permissions && req.target.exists())
        && let Some(desired) = desired_permissions(req)?
    {
        let current = permission_bits(&std::fs::symlink_metadata(&req.target)?);
        if current != desired {
            miseprintln!(
                "  permissions differ: {current:04o} (current) -> {desired:04o} (desired)"
            );
        }
    }
    #[cfg(not(unix))]
    let _ = req;
    Ok(())
}

/// Read a regular target without following a symlink. Non-file targets are
/// described structurally and return no bytes to compare.
fn current_regular_file_for_diff(req: &FileRequest) -> Result<Option<Vec<u8>>> {
    if req.target.is_symlink() {
        let dest = std::fs::read_link(&req.target)?;
        miseprintln!(
            "  file type differs: current symlink {} -> {}; desired regular file",
            req.target.display_user(),
            dest.display_user()
        );
        return Ok(None);
    }
    if req.target.is_file() {
        return match file::read(&req.target) {
            Ok(current) => Ok(Some(current)),
            // a mode such as 0200 can deny even the owner read access
            Err(err) if is_permission_denied(&err) => {
                miseprintln!("  current: {} is not readable", req.target.display_user());
                Ok(None)
            }
            Err(err) => Err(err),
        };
    }
    if req.target.exists() {
        miseprintln!(
            "  file type differs: current directory {}; desired regular file",
            req.target.display_user()
        );
        return Ok(None);
    }
    miseprintln!("  current: {} missing", req.target.display_user());
    Ok(Some(vec![]))
}

fn print_content_diff(
    config: &Config,
    req: &FileRequest,
    current: &[u8],
    desired: &[u8],
) -> Result<()> {
    let source = match req.mode {
        FileMode::Content => "inline".to_string(),
        _ => req.source.display_user(),
    };
    miseprintln!(
        "  content differs: {} -> {}",
        source,
        req.target.display_user()
    );
    let mut opts = diffy::DiffOptions::new();
    opts.set_original_filename(format!("{} (current)", req.target.display_user()))
        .set_modified_filename(match req.mode {
            FileMode::Content => format!("{} (desired)", req.target.display_user()),
            _ => format!("{} (desired)", req.source.display_user()),
        });
    match (str::from_utf8(current), str::from_utf8(desired)) {
        (Ok(current), Ok(desired)) => {
            let current = config.redact(current);
            let desired = config.redact(desired);
            let patch = opts.create_patch(&current, &desired);
            miseprint!("{}", diffy::PatchFormatter::new().fmt_patch(&patch))?;
        }
        _ => miseprintln!("  binary content differs"),
    }
    Ok(())
}

/// Print the changes required to converge whole-file dotfile entries.
/// Templates are rendered because a meaningful diff requires their desired
/// content, matching the trust and execution semantics of dotfiles status.
pub(crate) fn print_diffs(
    config: &Config,
    requests: &[FileRequest],
    secrets: &SecretValues,
) -> Result<()> {
    let mut changed = false;
    let mut problems = vec![];
    for req in requests {
        if req.mode == FileMode::Track {
            continue;
        }
        if req.mode.has_source() && !req.source.exists() {
            miseprintln!("{}: source missing", req.target_raw);
            changed = true;
            continue;
        }
        let rendered = match req.mode {
            FileMode::Template => match render_template(config, req, secrets) {
                Ok(rendered) => Some(rendered),
                Err(err) => {
                    problems.push(format!("  \"{}\": {err}", req.target_raw));
                    continue;
                }
            },
            _ => None,
        };
        match check_rendered(req, rendered.as_deref()) {
            Ok(FileState::Applied) => continue,
            Ok(_) => {}
            Err(err) => {
                problems.push(format!("  \"{}\": {err}", req.target_raw));
                continue;
            }
        }
        changed = true;
        miseprintln!("dotfile differs: {}", req.target.display_user());
        if let Err(err) = print_diff(config, req, rendered.as_deref()) {
            problems.push(format!("  \"{}\": {err}", req.target_raw));
        }
    }
    if !problems.is_empty() {
        bail!(
            "files: cannot diff these entries, fix them manually:\n{}",
            problems.join("\n")
        );
    }
    if !changed {
        info!("files: all files are applied");
    }
    Ok(())
}

const DOTFILES_PART: &str = "dotfiles";

/// Every path `apply_one` may create, replace, or remove for `req`, with how
/// deeply to capture it first: a path that gets replaced is captured whole,
/// a directory that stays a directory only by existence.
fn touched_paths(req: &FileRequest) -> Result<Vec<(PathBuf, Capture)>> {
    let mut paths: IndexMap<PathBuf, Capture> = IndexMap::new();
    for dir in missing_ancestors(&req.target) {
        paths.insert(dir, Capture::Shallow);
    }
    // a directory that will keep being a directory is never walked; a file
    // or link in the way of one is replaced and captured whole
    let dir_capture = |dir: &Path| {
        if dir.is_dir() {
            Capture::Shallow
        } else {
            Capture::Full
        }
    };
    match req.mode {
        FileMode::Track => {}
        // only the mode changes; a directory's contents stay untouched
        FileMode::Permissions => {
            paths.insert(req.target.clone(), dir_capture(&req.target));
        }
        // captured whole, so rollback restores a removed file or link
        FileMode::Absent | FileMode::Symlink | FileMode::Content => {
            paths.insert(req.target.clone(), Capture::Full);
        }
        FileMode::Template => {
            paths.insert(req.target.clone(), Capture::Full);
            // the ownership record changes with the target, so a rollback
            // restores the two together
            paths.insert(target_state_path(req), Capture::Full);
        }
        FileMode::Copy => {
            if req.source.is_dir() {
                paths.insert(req.target.clone(), dir_capture(&req.target));
                for (_, target) in walk_source_files(req)? {
                    // intermediate directories the copy creates on the way
                    // down; an existing one keeps being a directory
                    for dir in missing_ancestors(&target) {
                        paths.entry(dir).or_insert(Capture::Shallow);
                    }
                    paths.insert(target, Capture::Full);
                }
            } else {
                paths.insert(req.target.clone(), Capture::Full);
            }
        }
        FileMode::SymlinkEach => {
            for dir in needed_dirs(req)? {
                let capture = dir_capture(&dir);
                paths.insert(dir, capture);
            }
            for (_, target) in walk_source_files(req)? {
                paths.insert(target, Capture::Full);
            }
            let stale = stale_links(req)?;
            for path in &stale {
                paths.insert(path.clone(), Capture::Full);
            }
            // Journal removed parents after their children, so crash recovery
            // recreates directories (including their modes) before the links.
            for dir in stale
                .iter()
                .flat_map(|path| dirs_between(path, &req.target))
                .sorted_by_key(|path| std::cmp::Reverse(path.components().count()))
                .unique()
            {
                paths.entry(dir).or_insert(Capture::Shallow);
            }
            paths.insert(symlink_each_state_path(req), Capture::Full);
        }
    }
    // Directories a removal empties are journaled by `prune_created_dirs`,
    // which runs after the whole batch.
    if records_created_dirs(req) {
        // the record gains the directories created on the way: the only
        // shallow paths of a single-file entry are the missing ancestors
        if paths.values().any(|capture| *capture == Capture::Shallow) {
            paths.entry(target_state_path(req)).or_insert(Capture::Full);
        }
    }
    Ok(paths.into_iter().collect())
}

/// Directories strictly between `path` and `root`, deepest first: the ones an
/// upward cleanup may remove once they are empty.
fn dirs_between(path: &Path, root: &Path) -> Vec<PathBuf> {
    let mut dirs = vec![];
    let mut dir = path.parent();
    while let Some(d) = dir {
        if d == root || !d.starts_with(root) {
            break;
        }
        dirs.push(d.to_path_buf());
        dir = d.parent();
    }
    dirs
}

/// Remove each of `starts` and then its parents while `may_remove` allows it
/// and the directory is empty, stopping each walk at the first directory that
/// stays. Deepest starts go first, so emptying a nested directory can empty
/// its parent too. Returns the removed directories.
fn remove_empty_dirs_upward<'a>(
    starts: impl IntoIterator<Item = &'a Path>,
    may_remove: impl Fn(&Path) -> bool,
) -> Result<Vec<PathBuf>> {
    let mut removed = vec![];
    for start in starts
        .into_iter()
        .sorted_by_key(|dir| std::cmp::Reverse(dir.components().count()))
        .unique()
    {
        let mut dir = Some(start);
        while let Some(d) = dir {
            if !may_remove(d) || !d.is_dir() || d.read_dir()?.next().is_some() {
                break;
            }
            debug!("files: removing empty directory {}", d.display_user());
            file::remove_dir(d)?;
            removed.push(d.to_path_buf());
            dir = d.parent();
        }
    }
    Ok(removed)
}

/// Ancestors of `path` that do not exist yet, outermost first.
pub(crate) fn missing_ancestors(path: &Path) -> Vec<PathBuf> {
    let mut missing = vec![];
    let mut dir = path.parent();
    while let Some(d) = dir {
        if d.exists() || d.as_os_str().is_empty() {
            break;
        }
        missing.push(d.to_path_buf());
        dir = d.parent();
    }
    missing.reverse();
    missing
}

/// Write one entry. Each path is appended to `written` at the point it is
/// first mutated — after the removal of what was there, or else after its
/// own write lands — so a caller sees exactly the files that changed when a
/// later write fails: the target of a whole-file entry, each file a
/// directory copy or symlink-each places, anything cleared to make room, and
/// each stale link symlink-each prunes. A symlink to a directory also lists
/// the files it exposes, so a `[history.reload]` glob under the target
/// matches. Directories created on the way are not listed.
fn apply_one(req: &FileRequest, rendered: Option<&str>, written: &mut Vec<PathBuf>) -> Result<()> {
    if removes_target(req, rendered) {
        // conflicts were vetted (or --force given) when the plan was made
        debug!(
            "files: rm {} (template rendered empty)",
            req.target.display_user()
        );
        if remove_existing(&req.target)? {
            written.push(req.target.clone());
        }
        // The record keeps what mise last wrote, so a file brought back by
        // `mise dot undo` is still recognised as mise's own.
        return Ok(());
    }
    debug!("files: {}", describe(req)?);
    if req.mode == FileMode::Absent {
        // checked again here, not only when planning: a directory that
        // appeared since must still never be removed
        if check_absent(&req.target)? != FileState::Applied {
            if file::is_symlink_or_junction(&req.target) {
                // removes the link itself by handle; a Windows directory
                // link needs this (`remove_file` refuses it), and the
                // directory it points to is never entered
                file::remove_symlink_or_junction(&req.target)?;
            } else {
                file::remove_file(&req.target)?;
            }
            written.push(req.target.clone());
        }
        return Ok(());
    }
    // what is missing now is what mise creates, and so may remove again
    let created_dirs = if records_created_dirs(req) {
        missing_ancestors(&req.target)
    } else {
        vec![]
    };
    if req.mode != FileMode::Permissions
        && let Some(parent) = req.target.parent()
    {
        file::create_dir_all(parent)?;
    }
    match req.mode {
        FileMode::Symlink => {
            replace_recorded(&req.target, written, || {
                link_path(&req.source, &req.target, req.relative, true)
            })?;
            // the link is in place; listing what it exposes only feeds
            // reload matching, so a walk that fails must not fail the apply
            if req.source.is_dir() {
                match walk_source_files(req) {
                    Ok(files) => written.extend(files.into_iter().map(|(_, target)| target)),
                    Err(err) => warn!(
                        "files: cannot list {} for reload matching: {err:#}",
                        req.source.display_user()
                    ),
                }
            }
        }
        FileMode::SymlinkEach => {
            // conflicts were vetted (or --force given): clear anything
            // blocking a directory we need
            for dir in needed_dirs(req)? {
                if dir.exists() && !dir.is_dir() && remove_existing(&dir)? {
                    written.push(dir);
                }
            }
            // even an empty source dir must produce the target dir, or the
            // entry would never converge
            file::create_dir_all(&req.target)?;
            for (source, target) in walk_source_files(req)? {
                if check_symlink(&source, &target, req.relative)? == FileState::Applied {
                    continue;
                }
                if let Some(parent) = target.parent() {
                    file::create_dir_all(parent)?;
                }
                replace_recorded(&target, written, || {
                    link_path(&source, &target, req.relative, false)
                })?;
            }
            prune_stale_links(req, written)?;
        }
        FileMode::Copy => {
            if req.source.is_dir() {
                // additive: overwrite matching files, leave files mise
                // doesn't manage in place — only a type mismatch (vetted
                // as a conflict) removes the target
                if req.target.exists() && !req.target.is_dir() && remove_existing(&req.target)? {
                    written.push(req.target.clone());
                }
                // even an empty source dir must produce the target dir,
                // or the entry would never converge
                file::create_dir_all(&req.target)?;
                // per-file instead of a directory copy so a symlink at a
                // destination is replaced, not written through
                for (source, target) in walk_source_files(req)? {
                    if let Some(parent) = target.parent() {
                        file::create_dir_all(parent)?;
                    }
                    if target.is_symlink() {
                        // a link is replaced: recorded once it is gone. The
                        // source is opened and checked first and the copy
                        // reads from that handle, so one that cannot be
                        // copied leaves the link alone and one that changes
                        // meanwhile cannot leave the target missing
                        let mut from = CopySource::open(&source, &target)?;
                        file::remove_file(&target)?;
                        written.push(target.clone());
                        let to = std::fs::File::create(&target)
                            .wrap_err_with(copy_failure(&source, &target))?;
                        from.copy_into(to)?;
                    } else if target.is_file() {
                        overwrite_recorded(&source, &target, written)?;
                    } else {
                        match std::fs::symlink_metadata(&target) {
                            // absent: recorded once it exists, even if the
                            // copy that created it then failed
                            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                                create_recorded(&target, written, || file::copy(&source, &target))?;
                            }
                            // something else is there (a directory, say):
                            // a copy that fails leaves it as it was, so it
                            // is recorded only once the copy succeeded
                            Ok(_) => {
                                file::copy(&source, &target)?;
                                written.push(target.clone());
                            }
                            Err(err) => return Err(err.into()),
                        }
                    }
                }
            } else {
                replace_recorded(&req.target, written, || {
                    file::copy(&req.source, &req.target)
                })?;
                // the copy took the source's permissions; an explicit
                // `permissions` overrides them
                #[cfg(unix)]
                if let Some(permissions) = req.permissions {
                    set_mode(&req.target, permissions)?;
                }
            }
        }
        FileMode::Template => {
            let rendered = rendered.expect("rendered template content");
            replace_recorded(&req.target, written, || file::write(&req.target, rendered))?;
            #[cfg(unix)]
            match req.permissions {
                Some(permissions) => set_mode(&req.target, permissions)?,
                None => {
                    std::fs::set_permissions(&req.target, req.source.metadata()?.permissions())?
                }
            }
            save_target_state(req, rendered);
        }
        FileMode::Track => unreachable!("tracked files are never written"),
        FileMode::Absent => unreachable!("absent targets are removed above"),
        FileMode::Content => {
            replace_recorded(&req.target, written, || {
                file::write(&req.target, req.content.as_deref().expect("inline content"))
            })?;
            #[cfg(unix)]
            set_mode(&req.target, req.permissions.unwrap_or(0o600))?;
        }
        FileMode::Permissions => {
            // planning skipped a missing target or a symlink; this check only
            // gives the clearer message, the chmod itself never follows a
            // link that appeared since
            if let Some(reason) = permissions_target_unavailable(req)? {
                bail!("[dotfiles].\"{}\": {reason}", req.target_raw);
            }
            #[cfg(unix)]
            if let Some(permissions) = req.permissions {
                chmod_no_follow(&req.target, permissions)
                    .wrap_err_with(|| format!("[dotfiles].\"{}\"", req.target_raw))?;
                written.push(req.target.clone());
            }
        }
    }
    record_created_dirs(req, &created_dirs);
    Ok(())
}

#[cfg(unix)]
fn set_mode(path: &Path, mode: u32) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))?;
    Ok(())
}

/// Set the mode of a file mise does not own without ever following a symlink
/// at `path`: the check and the chmod act on one descriptor, so a link
/// swapped in after planning is refused instead of redirecting the change.
#[cfg(unix)]
fn chmod_no_follow(path: &Path, mode: u32) -> Result<()> {
    use nix::errno::Errno;
    use nix::fcntl::{OFlag, open};
    use nix::sys::stat::{Mode, fchmod};

    let mode = Mode::from_bits_truncate(mode as nix::libc::mode_t);
    let flags = OFlag::O_NOFOLLOW | OFlag::O_NONBLOCK | OFlag::O_CLOEXEC;
    // a write-only file cannot be opened for reading, nor a directory for
    // writing; either descriptor is enough for fchmod
    for access in [OFlag::O_RDONLY, OFlag::O_WRONLY] {
        match open(path, flags | access, Mode::empty()) {
            Ok(fd) => {
                fchmod(&fd, mode).wrap_err_with(|| {
                    format!("failed to set permissions of {}", path.display_user())
                })?;
                return Ok(());
            }
            Err(Errno::EACCES | Errno::EISDIR) => continue,
            Err(Errno::ELOOP) => bail!(
                "{} is a symlink, which is never followed",
                path.display_user()
            ),
            Err(err) => {
                return Err(err)
                    .wrap_err_with(|| format!("failed to open {}", path.display_user()));
            }
        }
    }
    // a target its owner can neither read nor write (mode 0000) cannot be
    // opened for either, but its owner may still change its mode
    chmod_unopenable_no_follow(path, mode)
}

/// Linux: an `O_PATH` descriptor needs no read or write permission, and with
/// `O_NOFOLLOW` it refers to a final symlink itself, which the type check
/// then refuses. Linux has no `fchmod` for such a descriptor, so the change
/// goes through its `/proc/self/fd` entry, which resolves to the opened inode
/// rather than to the path again. (`fchmodat` with `AT_SYMLINK_NOFOLLOW`
/// fails with `ENOTSUP` on older kernels and C libraries.)
#[cfg(target_os = "linux")]
fn chmod_unopenable_no_follow(path: &Path, mode: nix::sys::stat::Mode) -> Result<()> {
    use nix::fcntl::{AT_FDCWD, OFlag, open};
    use nix::sys::stat::{FchmodatFlags, Mode, SFlag, fchmodat, fstat};
    use std::os::fd::AsRawFd;

    let fd = open(
        path,
        OFlag::O_PATH | OFlag::O_NOFOLLOW | OFlag::O_CLOEXEC,
        Mode::empty(),
    )
    .wrap_err_with(|| format!("failed to open {}", path.display_user()))?;
    let kind = SFlag::from_bits_truncate(fstat(&fd)?.st_mode) & SFlag::S_IFMT;
    if kind == SFlag::S_IFLNK {
        bail!(
            "{} is a symlink, which is never followed",
            path.display_user()
        );
    }
    if kind != SFlag::S_IFREG && kind != SFlag::S_IFDIR {
        bail!("{} is not a file or directory", path.display_user());
    }
    let descriptor = PathBuf::from(format!("/proc/self/fd/{}", fd.as_raw_fd()));
    fchmodat(AT_FDCWD, &descriptor, mode, FchmodatFlags::FollowSymlink)
        .wrap_err_with(|| format!("failed to set permissions of {}", path.display_user()))
}

/// Other Unix systems (macOS, the BSDs) implement `fchmodat` with
/// `AT_SYMLINK_NOFOLLOW` directly. There it changes a symlink's own mode
/// rather than failing, so a link is refused first; one swapped in after that
/// check only has its own mode changed, never its target's.
#[cfg(all(unix, not(target_os = "linux")))]
fn chmod_unopenable_no_follow(path: &Path, mode: nix::sys::stat::Mode) -> Result<()> {
    use nix::errno::Errno;
    use nix::fcntl::AT_FDCWD;
    use nix::sys::stat::{FchmodatFlags, fchmodat};

    let file_type = std::fs::symlink_metadata(path)
        .wrap_err_with(|| format!("failed to inspect {}", path.display_user()))?
        .file_type();
    if file_type.is_symlink() {
        bail!(
            "{} is a symlink, which is never followed",
            path.display_user()
        );
    }
    if !file_type.is_file() && !file_type.is_dir() {
        bail!("{} is not a file or directory", path.display_user());
    }
    match fchmodat(AT_FDCWD, path, mode, FchmodatFlags::NoFollowSymlink) {
        Ok(()) => Ok(()),
        // one errno on some systems, two on others
        Err(err) if err == Errno::ENOTSUP || err == Errno::EOPNOTSUPP => bail!(
            "cannot set permissions of {} without following symlinks on this system; make it readable or writable by its owner first",
            path.display_user()
        ),
        Err(err) => Err(err)
            .wrap_err_with(|| format!("failed to set permissions of {}", path.display_user())),
    }
}

/// delete this entry's leftover links (see [`stale_links`]) and any directory
/// they emptied out. A directory only goes when the links we just removed were
/// all that was in it and the entry has no source file left that needs it, so
/// user content — and the target directory itself — always survives.
fn prune_stale_links(req: &FileRequest, written: &mut Vec<PathBuf>) -> Result<()> {
    let stale = stale_links(req)?;
    if stale.is_empty() {
        return Ok(());
    }
    for path in &stale {
        debug!("files: removing stale link {}", path.display_user());
        file::remove_file(path)?;
        written.push(path.clone());
    }
    let needed: HashSet<PathBuf> = needed_dirs(req)?.into_iter().collect();
    remove_empty_dirs_upward(stale.iter().filter_map(|p| p.parent()), |dir| {
        dir != req.target && dir.starts_with(&req.target) && !needed.contains(dir)
    })?;
    Ok(())
}

/// remove whatever sits at `path` so it can be replaced — conflicts have
/// already been vetted (or --force given) by the time this runs
fn remove_existing(path: &Path) -> Result<bool> {
    if path.is_symlink() || path.is_file() {
        file::remove_file(path)?;
    } else if path.is_dir() {
        file::remove_all(path)?;
    } else {
        return Ok(false);
    }
    Ok(true)
}

/// A copy source held open, so the destination is touched only once the
/// source has been opened and checked, and the copy reads from that same
/// handle — a source replaced or removed meanwhile cannot leave the
/// destination cleared with nothing to put in its place.
struct CopySource<'a> {
    file: std::fs::File,
    metadata: std::fs::Metadata,
    source: &'a Path,
    target: &'a Path,
}

impl<'a> CopySource<'a> {
    /// Open `source` for copying to `target`, with the check `fs::copy`
    /// performs before it touches its destination: the source must be a
    /// regular file (or a link to one).
    fn open(source: &'a Path, target: &'a Path) -> Result<Self> {
        let failed = copy_failure(source, target);
        let file = std::fs::File::open(source).wrap_err_with(&failed)?;
        let metadata = file.metadata().wrap_err_with(&failed)?;
        if !metadata.is_file() {
            bail!(
                "{}: the source path is neither a regular file nor a symlink to a regular file",
                failed()
            );
        }
        Ok(Self {
            file,
            metadata,
            source,
            target,
        })
    }

    /// Write the source's content and permission bits into the open `to`,
    /// as `fs::copy` would.
    fn copy_into(&mut self, mut to: std::fs::File) -> Result<()> {
        let failed = copy_failure(self.source, self.target);
        std::io::copy(&mut self.file, &mut to).wrap_err_with(&failed)?;
        to.set_permissions(self.metadata.permissions())
            .wrap_err_with(&failed)?;
        Ok(())
    }
}

fn copy_failure<'a>(source: &'a Path, target: &'a Path) -> impl Fn() -> String + 'a {
    move || {
        format!(
            "failed copy: {} -> {}",
            source.display_user(),
            target.display_user()
        )
    }
}

/// Overwrite the existing regular file at `target` with `source` in place,
/// as `file::copy` would (content and permission bits), recording the target
/// in `written` once it has been opened for truncation — the first mutation.
/// The source is opened and checked first, so a source that is not a regular
/// file (a directory behind a link, say) fails before the target is touched.
/// An open that fails (a read-only target file or filesystem) changes
/// nothing and records nothing.
fn overwrite_recorded(source: &Path, target: &Path, written: &mut Vec<PathBuf>) -> Result<()> {
    let mut from = CopySource::open(source, target)?;
    let to = std::fs::OpenOptions::new()
        .write(true)
        .truncate(true)
        .open(target)
        .wrap_err_with(copy_failure(source, target))?;
    written.push(target.to_path_buf());
    from.copy_into(to)
}

/// Run `write` against a `target` that does not exist yet, recording the
/// target in `written` if it exists afterwards: after a successful write,
/// and after one that failed only once it had created the file (its on-disk
/// state changed either way). A write that failed before creating anything
/// records nothing.
pub(crate) fn create_recorded(
    target: &Path,
    written: &mut Vec<PathBuf>,
    write: impl FnOnce() -> Result<()>,
) -> Result<()> {
    let result = write();
    if result.is_ok() || std::fs::symlink_metadata(target).is_ok() {
        written.push(target.to_path_buf());
    }
    result
}

/// Clear `target` and run `write` in its place, recording the target in
/// `written` at its first mutation: right after the removal when something
/// was there (a write that then fails still leaves the old content gone),
/// otherwise as [`create_recorded`] does.
fn replace_recorded(
    target: &Path,
    written: &mut Vec<PathBuf>,
    write: impl FnOnce() -> Result<()>,
) -> Result<()> {
    if remove_existing(target)? {
        written.push(target.to_path_buf());
        return write();
    }
    create_recorded(target, written, write)
}

/// `allow_windows_symlink` is false for `symlink-each`, which stays on the Windows copy path:
/// its unapply planner is `#[cfg(not(windows))]`-guarded and falls through to the content
/// comparison, which rejects a symlink — creating one there would make unapply demand `--force`.
fn link_path(
    source: &Path,
    target: &Path,
    relative: bool,
    allow_windows_symlink: bool,
) -> Result<()> {
    #[cfg(windows)]
    if source.is_file() {
        // Windows grants SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE when Developer Mode is
        // on, so a file symlink is often creatable without elevation -- `windows_shim_mode`
        // already relies on that. Try it rather than assume it fails. Junctions only cover
        // directories, so a copy stays the fallback: it is what this did unconditionally
        // before, and it keeps working where the privilege really is absent.
        if allow_windows_symlink && std::os::windows::fs::symlink_file(source, target).is_ok() {
            return Ok(());
        }
        file::copy(source, target)?;
        return Ok(());
    }
    #[cfg(not(windows))]
    let _ = allow_windows_symlink;
    if relative {
        file::make_symlink(&relative_link_path(source, target), target)?;
    } else {
        file::make_symlink(source, target)?;
    }
    Ok(())
}

/// The path a link at `target` should hold to reach `source` relatively.
///
/// The kernel resolves `..` in a link against the directory the link
/// physically sits in, so a path worked out from the configured spelling is
/// only used when it resolves to the source from there (it does not when
/// the link's directory is itself reached through a symlink). Otherwise the
/// path runs between the canonical locations, and if even that cannot be
/// worked out the link stays absolute.
fn relative_link_path(source: &Path, target: &Path) -> PathBuf {
    let Some(parent) = target.parent() else {
        return source.to_path_buf();
    };
    // `physical_path` rather than `canonicalize`: a `symlink-each` source may
    // be a dangling link, which must still get a relative link or the entry
    // would never converge
    let physical_source = physical_path(source);
    let source = lexical_normalize(source);
    let resolves =
        |rel: &Path| resolve_relative_link(target, rel).is_some_and(|p| p == physical_source);
    if let Some(rel) = pathdiff::diff_paths(&source, lexical_normalize(parent))
        && resolves(&rel)
    {
        return rel;
    }
    if let Ok(canonical_parent) = parent.canonicalize()
        && let Some(rel) = pathdiff::diff_paths(&physical_source, canonical_parent)
        && resolves(&rel)
    {
        return rel;
    }
    source
}

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

    #[test]
    fn destination_variants_survive_incoming_config_preflight() -> Result<()> {
        use crate::config::config_file::mise_toml::MiseToml;
        use std::sync::Arc;

        let path = dirs::HOME.join(".config/mise/config.toml");
        let body = r#"
[dotfiles."vscode/settings.json"]
mode = "copy"
variants = [
    { os = "macos", target = "~/Library/Application Support/Code/User/settings.json" },
    { os = "linux", profile = "work", target = "/etc/example/settings.json" },
    { os = "windows", target = 'C:\Users\example\settings.json' },
]
"#;
        let mut configs = ConfigMap::new();
        configs.insert(
            path.clone(),
            Arc::new(MiseToml::for_history_preflight(body, &path)?),
        );
        validate_incoming_files(&configs)?;

        // Validate inactive destinations too, before accepting shared config.
        let invalid = body.replace(
            "~/Library/Application Support/Code/User/settings.json",
            "relative/settings.json",
        );
        configs.insert(
            path.clone(),
            Arc::new(MiseToml::for_history_preflight(&invalid, &path)?),
        );
        assert!(validate_incoming_files(&configs).is_err());
        Ok(())
    }

    #[test]
    fn destination_variants_infer_safe_root_relative_sources() -> Result<()> {
        let entry: FileTomlEntry = toml::from_str(
            r#"
mode = "copy"
variants = [
    { os = "macos", target = "~/Library/Application Support/Code/User/settings.json" },
    { os = "linux", target = "~/.config/Code/User/settings.json" },
]
"#,
        )?;
        let FileTomlEntry::Table {
            source,
            content,
            mode,
            variants: Some(variants),
            ..
        } = entry
        else {
            bail!("expected a table entry with variants");
        };
        assert_eq!(
            validate_file_variants(
                "vscode/settings.json",
                source.as_deref(),
                content.as_deref(),
                mode.as_deref(),
                false,
                &variants,
            )?,
            Some(PathBuf::from("vscode/settings.json"))
        );
        for key in [
            "",
            ".",
            "../settings.json",
            "vscode/../settings.json",
            "~/.settings.json",
        ] {
            assert!(logical_source_path(key).is_err(), "{key}");
        }
        Ok(())
    }

    #[test]
    fn destination_variants_reject_unknown_fields_in_all_modes() {
        for mode in ["copy", "track"] {
            for field in ["oss", "profle", "targte"] {
                let input = format!(
                    r#"mode = "{mode}"
variants = [{{ {field} = "linux" }}]"#
                );
                assert!(toml::from_str::<FileTomlEntry>(&input).is_err(), "{input}");
            }
        }
    }

    #[test]
    fn destination_variant_paths_accept_foreign_absolute_syntax() {
        for target in [
            "~/settings.json",
            "/etc/example/settings.json",
            "C:/Users/example/settings.json",
            r"C:\Users\example\settings.json",
            r"\\server\share\settings.json",
        ] {
            assert!(variant_target_is_absolute(target), "{target}");
        }
        for target in ["settings.json", "./settings.json", "C:settings.json", ""] {
            assert!(!variant_target_is_absolute(target), "{target}");
        }
    }

    #[cfg(unix)]
    #[test]
    fn stale_link_parent_directories_have_recovery_preimages() -> Result<()> {
        use crate::system::history::journal::{JournalEntry, PathSnapshot, PathState};
        use std::os::unix::fs::PermissionsExt;
        let temp = tempfile::tempdir()?;
        let root = temp.path().canonicalize()?;
        let source = root.join("source");
        let target = root.join("target");
        std::fs::create_dir_all(&source)?;
        let nested = target.join("nested");
        std::fs::create_dir_all(&nested)?;
        std::fs::set_permissions(&nested, std::fs::Permissions::from_mode(0o700))?;
        for name in ["a", "b"] {
            std::os::unix::fs::symlink(source.join("nested").join(name), nested.join(name))?;
        }
        let req = link_req(&source, &target, FileMode::SymlinkEach);
        let state = temp.path().join("recovery");
        let mut journal = touched_paths(&req)?
            .into_iter()
            .map(|(path, capture)| {
                let prior = PathSnapshot::capture_with(&state, &path, capture);
                JournalEntry::PathChanged {
                    part: "dotfiles".into(),
                    item: "test".into(),
                    path,
                    prior,
                }
            })
            .collect::<Vec<_>>();
        let mut written = vec![];
        prune_stale_links(&req, &mut written)?;
        assert!(!nested.exists());
        // the removed links are listed, the pruned directory is not
        assert_eq!(written, vec![nested.join("a"), nested.join("b")]);
        let committed = journal
            .iter()
            .enumerate()
            .filter_map(|(seq, entry)| {
                let JournalEntry::PathChanged { path, .. } = entry else {
                    return None;
                };
                Some(JournalEntry::Committed {
                    seq: seq as u32,
                    after: PathState::observe(path),
                })
            })
            .collect::<Vec<_>>();
        journal.extend(committed);
        crate::system::history::recovery::recover(&state, &journal)?;
        assert_eq!(
            std::fs::metadata(&nested)?.permissions().mode() & 0o777,
            0o700
        );
        for name in ["a", "b"] {
            assert_eq!(
                std::fs::read_link(nested.join(name))?,
                source.join("nested").join(name)
            );
        }
        Ok(())
    }

    #[test]
    fn configuration_reload_discards_old_declaration_diagnostics() {
        let target = "~/.mise-diagnostic-reset-test";
        record_invalid(target, Path::new("diagnostic-reset.toml"), "invalid mode");
        assert!(
            invalid_declarations()
                .iter()
                .any(|item| item.target == target)
        );
        clear_invalid_declarations();
        assert!(
            !invalid_declarations()
                .iter()
                .any(|item| item.target == target)
        );
    }

    #[test]
    fn test_file_mode_parse() {
        assert_eq!(FileMode::parse("symlink"), Some(FileMode::Symlink));
        assert_eq!(FileMode::parse("symlink-each"), Some(FileMode::SymlinkEach));
        assert_eq!(FileMode::parse("copy"), Some(FileMode::Copy));
        assert_eq!(FileMode::parse("template"), Some(FileMode::Template));
        assert_eq!(FileMode::parse("absent"), Some(FileMode::Absent));
        assert_eq!(FileMode::Absent.name(), "absent");
        assert!(!FileMode::Absent.has_source());
        assert_eq!(FileMode::parse("hardlink"), None);
    }

    fn absent_req(target: &Path) -> FileRequest {
        FileRequest {
            target_raw: target.to_string_lossy().to_string(),
            target: target.to_path_buf(),
            source: PathBuf::new(),
            content: None,
            mode: FileMode::Absent,
            exclude: vec![],
            include: None,
            manifest: None,
            permissions: None,
            base: PathBuf::from("/"),
            origin: ResourceOrigin {
                config: PathBuf::from("/mise.toml"),
                config_root: PathBuf::from("/"),
                environment: vec![],
                source: None,
            },
            policy: FilePolicy::for_mode(FileMode::Absent),
            variants: vec![],
            enabled: true,
            remove_empty: false,
            dot_prefix: false,
            relative: false,
        }
    }

    fn validate_incoming_body(body: &str) -> Result<()> {
        use crate::config::config_file::mise_toml::MiseToml;
        use std::sync::Arc;

        let path = dirs::HOME.join(".config/mise/config.toml");
        let mut configs = ConfigMap::new();
        configs.insert(
            path.clone(),
            Arc::new(MiseToml::for_history_preflight(body, &path)?),
        );
        validate_incoming_files(&configs)
    }

    #[test]
    fn absent_entries_take_no_source() -> Result<()> {
        validate_incoming_body(
            r#"
[dotfiles]
"~/.oldrc" = { mode = "absent" }
"#,
        )?;
        for extra in [
            r#"source = "oldrc""#,
            r#"content = "x""#,
            r#"manifest = "git""#,
            r#"exclude = ["*.bak"]"#,
            r#"permissions = "0600""#,
            "encrypt = true",
        ] {
            let body = format!(
                r#"
[dotfiles."~/.oldrc"]
mode = "absent"
{extra}
"#
            );
            assert!(validate_incoming_body(&body).is_err(), "{extra}");
        }
        Ok(())
    }

    /// A destination override needs no source when the entry removes it.
    #[test]
    fn absent_entries_accept_destination_variants() -> Result<()> {
        validate_incoming_body(
            r#"
[dotfiles."~/.oldrc"]
mode = "absent"
variants = [
    { os = "windows", target = 'C:\Users\example\oldrc' },
    { os = ["linux", "macos"] },
]
"#,
        )
    }

    #[test]
    fn merged_absent_entries_have_no_source() -> Result<()> {
        let origin = absent_req(Path::new("/unused")).origin;
        let mut merged = IndexMap::new();
        let entry: FileTomlEntry = toml::from_str(r#"mode = "absent""#)?;
        merge_file_entry(
            "~/.oldrc".into(),
            entry,
            Path::new("/"),
            &origin,
            &mut merged,
        );
        let [request] = merged.values().collect::<Vec<_>>()[..] else {
            bail!("expected one absent request");
        };
        assert_eq!(request.mode, FileMode::Absent);
        assert_eq!(request.target, dirs::HOME.join(".oldrc"));
        assert_eq!(request.source, PathBuf::new());

        // a pattern is rejected rather than checked as a literal path
        assert!(
            validate_incoming_body(
                r#"
[dotfiles]
"~/.old*" = { mode = "absent" }
"#
            )
            .is_err()
        );
        // so is one a variant selects as its destination
        let err = validate_incoming_body(
            r#"
[dotfiles."~/.oldrc"]
mode = "absent"
variants = [{ os = "windows", target = "~/.old[0-9]" }, { default = true }]
"#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("cannot use wildcards"), "{err}");
        let mut merged = IndexMap::new();
        let entry: FileTomlEntry = toml::from_str(r#"mode = "absent""#)?;
        merge_file_entry(
            "~/.old*".into(),
            entry,
            Path::new("/"),
            &origin,
            &mut merged,
        );
        assert!(merged.is_empty());

        let mut merged = IndexMap::new();
        let entry: FileTomlEntry = toml::from_str(
            r#"mode = "absent"
source = "oldrc""#,
        )?;
        merge_file_entry(
            "~/.oldrc".into(),
            entry,
            Path::new("/"),
            &origin,
            &mut merged,
        );
        assert!(merged.is_empty());
        Ok(())
    }

    #[test]
    fn absent_claims_its_target_in_the_footprint() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        file::write(&source, "content")?;

        let err = validate_composed_file_footprints(&[
            absent_req(&target),
            link_req(&source, &target, FileMode::Copy),
        ])
        .unwrap_err();
        assert!(err.to_string().contains("conflicting dotfile declarations"));
        // a permissions-only entry cannot chmod a file an absent entry removes
        let mut permissions_only = absent_req(&target);
        permissions_only.mode = FileMode::Permissions;
        permissions_only.permissions = Some(0o600);
        let err = validate_composed_file_footprints(&[absent_req(&target), permissions_only])
            .unwrap_err();
        assert!(err.to_string().contains("conflicting dotfile declarations"));
        // a file beneath an absent target would need it as a directory
        let err = validate_composed_file_footprints(&[
            absent_req(&target),
            link_req(&source, &target.join("nested"), FileMode::Copy),
        ])
        .unwrap_err();
        assert!(err.to_string().contains("conflicting dotfile declarations"));
        Ok(())
    }

    #[test]
    fn absent_removes_a_file_without_a_content_check() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let target = dir.path().join("oldrc");
        let req = absent_req(&target);
        assert_eq!(check_rendered(&req, None)?, FileState::Applied);

        file::write(&target, "anything")?;
        assert_eq!(
            check_rendered(&req, None)?,
            FileState::Differs("present".into())
        );
        assert!(find_conflicts(&req)?.is_empty());
        assert_eq!(touched_paths(&req)?, vec![(target.clone(), Capture::Full)]);

        let mut written = vec![];
        apply_one(&req, None, &mut written)?;
        assert!(!target.exists());
        assert_eq!(written, vec![target.clone()]);
        assert_eq!(check_rendered(&req, None)?, FileState::Applied);

        // converged: nothing is removed or recorded
        let mut written = vec![];
        apply_one(&req, None, &mut written)?;
        assert!(written.is_empty());
        Ok(())
    }

    /// Not `#[cfg(unix)]`: `make_symlink` writes a junction on Windows, a
    /// directory link that must be removed as a link, not refused as a
    /// directory, and never entered.
    #[test]
    fn absent_removes_a_symlink_but_not_what_it_points_at() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let pointee = dir.path().join("pointee");
        let target = dir.path().join("link");
        file::create_dir_all(&pointee)?;
        file::write(pointee.join("keep"), "keep")?;
        file::make_symlink(&pointee, &target)?;
        let req = absent_req(&target);
        assert!(matches!(check_rendered(&req, None)?, FileState::Differs(_)));

        // the journal captures the link itself (std reports a junction as a
        // symlink), never walking into the directory it points to; undo
        // restores it with `make_symlink`, which writes a junction again
        let state = tempfile::tempdir()?;
        for (path, capture) in touched_paths(&req)? {
            let snapshot = journal::PathSnapshot::capture_with(state.path(), &path, capture);
            assert!(
                matches!(snapshot, journal::PathSnapshot::Symlink { .. }),
                "{}",
                snapshot.describe()
            );
        }

        let mut written = vec![];
        apply_one(&req, None, &mut written)?;
        assert!(!target.is_symlink());
        assert!(pointee.join("keep").is_file());
        Ok(())
    }

    #[test]
    fn absent_under_a_file_is_already_applied() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let parent = dir.path().join("oldrc");
        file::write(&parent, "a file, not a directory")?;
        let req = absent_req(&parent.join("x"));
        assert_eq!(check_rendered(&req, None)?, FileState::Applied);
        let mut written = vec![];
        apply_one(&req, None, &mut written)?;
        assert!(written.is_empty());
        assert!(parent.is_file());
        Ok(())
    }

    #[test]
    fn absent_refuses_a_directory() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let target = dir.path().join("olddir");
        file::create_dir_all(&target)?;
        file::write(target.join("keep"), "keep")?;
        let req = absent_req(&target);

        let err = check_rendered(&req, None).unwrap_err();
        assert!(err.to_string().contains("is a directory"), "{err}");
        let mut written = vec![];
        assert!(apply_one(&req, None, &mut written).is_err());
        assert!(written.is_empty());
        assert!(target.join("keep").is_file());
        Ok(())
    }

    /// Deserializing a whole-file entry would drop the edit keys, turning an
    /// edit into a removal of the file it meant to edit.
    #[test]
    fn absent_entries_reject_edit_keys() {
        for extra in [
            r#"block = "x""#,
            r#"line = "x""#,
            r#"template = "tera""#,
            r#"comment = ";""#,
            r#"position = "prepend""#,
        ] {
            let value: toml::Value =
                toml::from_str(&format!("mode = \"absent\"\n{extra}")).expect("toml");
            assert!(
                parse_file_entry("~/.oldrc", value, Path::new("/mise.toml")).is_none(),
                "{extra}"
            );
        }
    }

    /// Only regular files and symlinks are removed: a socket (like a FIFO or
    /// device node) may belong to a running service.
    #[cfg(unix)]
    #[test]
    fn absent_refuses_special_files() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let target = dir.path().join("socket");
        let _listener = std::os::unix::net::UnixListener::bind(&target)?;
        let req = absent_req(&target);

        let err = check_rendered(&req, None).unwrap_err();
        assert!(
            err.to_string().contains("not a regular file or symlink"),
            "{err}"
        );
        let mut written = vec![];
        assert!(apply_one(&req, None, &mut written).is_err());
        assert!(written.is_empty());
        assert!(std::fs::symlink_metadata(&target).is_ok());
        Ok(())
    }

    #[test]
    fn absent_rejects_an_edit_on_the_same_file() -> Result<()> {
        use crate::system::edits::{EditOp, EditRequest, LinePosition};
        let target = dirs::HOME.join(".oldrc");
        let origin = absent_req(&target).origin;
        let edit = |path: &Path| EditRequest {
            path_raw: path.display().to_string(),
            path: path.to_path_buf(),
            id: "x".into(),
            op: EditOp::Line {
                line: "x".into(),
                position: LinePosition::Append,
            },
            base: PathBuf::from("/"),
            config_path: PathBuf::from("/mise.toml"),
            origin: origin.clone(),
        };
        let err =
            validate_absent_edit_targets(&[absent_req(&target)], &[edit(&target)]).unwrap_err();
        assert!(err.to_string().contains("conflicting dotfile declarations"));
        // another spelling of the same path
        let dotted = dirs::HOME.join(".dir").join("..").join(".oldrc");
        assert!(validate_absent_edit_targets(&[absent_req(&target)], &[edit(&dotted)]).is_err());
        validate_absent_edit_targets(&[absent_req(&target)], &[edit(&target.with_extension("x"))])?;
        // a whole-file entry of another mode may still be edited
        let copy = link_req(&target, &target, FileMode::Copy);
        validate_absent_edit_targets(&[copy], &[edit(&target)])?;
        Ok(())
    }

    #[test]
    fn absent_has_nothing_to_unapply() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let target = dir.path().join("oldrc");
        file::write(&target, "put back by hand")?;
        let req = absent_req(&target);
        let opts = UnapplyOpts {
            dry_run: false,
            verbose: false,
            force: true,
            yes: true,
        };
        assert!(plan_unapply_one(&req, &opts)?.is_none());
        Ok(())
    }

    fn patterns(patterns: &[&str]) -> Vec<glob::Pattern> {
        patterns
            .iter()
            .map(|p| glob::Pattern::new(p).unwrap())
            .collect()
    }

    /// A list mise cannot read in full is an error naming the entry and
    /// the pattern, never a shorter list. A shorter `exclude` captures
    /// files the user asked to leave out; a shorter — or empty —
    /// `include` captures the whole tree they asked to narrow.
    #[test]
    fn an_unparsable_pattern_list_is_an_error_naming_the_pattern() {
        for key in ["exclude", "include"] {
            let error = compile_patterns(key, Some(vec!["fine/**".into(), "[".into()]))
                .expect_err("an unparsable pattern is an error");
            assert!(error.contains(key), "{error}");
            assert!(error.contains('['), "{error}");
        }
        // a list that reads in full is kept exactly, empty or not
        assert_eq!(
            compile_patterns("include", Some(vec![]))
                .unwrap()
                .map(|patterns| patterns.len()),
            Some(0),
            "a declared empty list stays a declared empty list"
        );
        assert!(compile_patterns("include", None).unwrap().is_none());
    }

    #[test]
    fn test_exclude_bare_pattern_matches_any_component() {
        let pats = patterns(&["mise.toml"]);
        assert!(is_excluded(Path::new("mise.toml"), &pats));
        assert!(is_excluded(Path::new("nested/mise.toml"), &pats));
        assert!(!is_excluded(Path::new("mise.toml.bak"), &pats));
        assert!(!is_excluded(Path::new("other.toml"), &pats));
    }

    #[test]
    fn test_exclude_wildcard_matches_by_component() {
        let pats = patterns(&["*.md"]);
        assert!(is_excluded(Path::new("README.md"), &pats));
        assert!(is_excluded(Path::new("docs/guide.md"), &pats));
        assert!(!is_excluded(Path::new("README.txt"), &pats));
    }

    #[test]
    fn test_exclude_slash_pattern_is_anchored() {
        let pats = patterns(&["nvim/spell"]);
        assert!(is_excluded(Path::new("nvim/spell"), &pats));
        // matching a directory takes everything under it
        assert!(is_excluded(Path::new("nvim/spell/en.add"), &pats));
        // but the same name elsewhere in the tree is untouched
        assert!(!is_excluded(Path::new("config/nvim/spell"), &pats));
        assert!(!is_excluded(Path::new("spell"), &pats));
    }

    #[test]
    fn test_exclude_directory_component_takes_children() {
        let pats = patterns(&[".git"]);
        assert!(is_excluded(Path::new(".git"), &pats));
        assert!(is_excluded(Path::new(".git/config"), &pats));
        assert!(!is_excluded(Path::new("git/config"), &pats));
    }

    #[test]
    fn test_exclude_empty_matches_nothing() {
        assert!(!is_excluded(Path::new("mise.toml"), &[]));
    }

    #[test]
    fn a_later_layer_overrides_a_track_entry_exclude_list() {
        let request = |exclude: Vec<&str>, explicit: bool| FileRequest {
            target_raw: "~/.codex".into(),
            target: PathBuf::from("/home/test/.codex"),
            source: PathBuf::new(),
            content: None,
            mode: FileMode::Track,
            exclude: exclude
                .into_iter()
                .map(|p| glob::Pattern::new(p).unwrap())
                .collect(),
            include: None,
            manifest: None,
            permissions: None,
            base: PathBuf::from("/home/test"),
            origin: crate::system::resources::ResourceOrigin {
                config: PathBuf::from("/home/test/.config/mise/config.toml"),
                config_root: PathBuf::from("/home/test/.config/mise"),
                environment: vec![],
                source: None,
            },
            policy: FilePolicy {
                explicit: ExplicitFields {
                    exclude: explicit,
                    ..Default::default()
                },
                ..FilePolicy::for_mode(FileMode::Track)
            },
            variants: vec![],
            enabled: true,
            remove_empty: false,
            dot_prefix: false,
            relative: false,
        };
        let mut first = request(vec!["sessions"], true);
        first.override_from(request(vec!["cache"], true));
        assert_eq!(first.exclude[0].as_str(), "cache");
        assert!(first.policy.explicit.exclude);
        let mut first = request(vec!["sessions"], true);
        first.override_from(request(vec![], false));
        assert_eq!(first.exclude[0].as_str(), "sessions");
    }

    #[test]
    fn test_wildcard_target_expansion() {
        let captures = wildcard_captures(
            "/repo/dotfiles/config/*.toml",
            Path::new("/repo/dotfiles/config/starship.toml"),
        )
        .unwrap();
        let target = expand_target_pattern("/home/me/.config/*.toml", &captures).unwrap();
        assert_eq!(target, PathBuf::from("/home/me/.config/starship.toml"));
    }

    #[test]
    fn test_recursive_wildcard_target_expansion() {
        let captures = wildcard_captures(
            "/repo/dotfiles/config/**/*.toml",
            Path::new("/repo/dotfiles/config/a/b/tool.toml"),
        )
        .unwrap();
        let target = expand_target_pattern("/home/me/.config/**/*.toml", &captures).unwrap();
        assert_eq!(target, PathBuf::from("/home/me/.config/a/b/tool.toml"));
    }

    #[test]
    fn test_recursive_wildcard_matches_zero_directories() {
        let captures = wildcard_captures(
            "/repo/dotfiles/config/**/*.toml",
            Path::new("/repo/dotfiles/config/tool.toml"),
        )
        .unwrap();
        let target = expand_target_pattern("/home/me/.config/**/*.toml", &captures).unwrap();
        assert_eq!(target, PathBuf::from("/home/me/.config/tool.toml"));
    }

    #[test]
    fn test_question_mark_target_expansion() {
        let captures = wildcard_captures(
            "/repo/dotfiles/config/app?.toml",
            Path::new("/repo/dotfiles/config/app1.toml"),
        )
        .unwrap();
        let target = expand_target_pattern("/home/me/.config/app?.toml", &captures).unwrap();
        assert_eq!(target, PathBuf::from("/home/me/.config/app1.toml"));
    }

    #[test]
    fn test_character_class_target_expansion() {
        let captures = wildcard_captures(
            "/repo/dotfiles/config/theme-[ab].toml",
            Path::new("/repo/dotfiles/config/theme-a.toml"),
        )
        .unwrap();
        let target = expand_target_pattern("/home/me/.config/theme-[ab].toml", &captures).unwrap();
        assert_eq!(target, PathBuf::from("/home/me/.config/theme-a.toml"));
    }

    #[test]
    fn test_windows_separator_wildcard_expansion() {
        let captures = wildcard_captures(
            r"C:\repo\dotfiles\config\*.toml",
            Path::new(r"C:\repo\dotfiles\config\starship.toml"),
        )
        .unwrap();
        let target = expand_target_pattern(r"C:\Users\me\.config\*.toml", &captures).unwrap();
        assert_eq!(
            target,
            PathBuf::from(native_path_separators("C:/Users/me/.config/starship.toml"))
        );
    }

    #[test]
    fn test_windows_separator_recursive_wildcard_expansion() {
        let captures = wildcard_captures(
            r"C:\repo\dotfiles\config\**\*.toml",
            Path::new(r"C:\repo\dotfiles\config\tool.toml"),
        )
        .unwrap();
        let target = expand_target_pattern(r"C:\Users\me\.config\**\*.toml", &captures).unwrap();
        assert_eq!(
            target,
            PathBuf::from(native_path_separators("C:/Users/me/.config/tool.toml"))
        );
    }

    fn link_req(source: &Path, target: &Path, mode: FileMode) -> FileRequest {
        FileRequest {
            target_raw: target.to_string_lossy().to_string(),
            target: target.to_path_buf(),
            source: source.to_path_buf(),
            // These tests are about link and copy modes, which read the file at `source`. Inline
            // content is the other kind of entry and has nothing to do with what they assert.
            content: None,
            mode,
            exclude: vec![],
            include: None,
            manifest: None,
            permissions: None,
            base: source.parent().expect("source parent").to_path_buf(),
            origin: ResourceOrigin {
                config: PathBuf::from("/mise.toml"),
                config_root: PathBuf::from("/"),
                environment: vec![],
                source: Some(source.to_path_buf()),
            },
            policy: FilePolicy::for_mode(mode),
            variants: vec![],
            enabled: true,
            remove_empty: false,
            dot_prefix: false,
            relative: false,
        }
    }

    fn symlink_req(source: &Path, target: &Path) -> FileRequest {
        link_req(source, target, FileMode::Symlink)
    }

    #[test]
    fn apply_one_lists_only_the_files_it_wrote() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::create_dir_all(source.join("sub"))?;
        file::write(source.join("a.toml"), "a")?;
        file::write(source.join("sub/b.toml"), "b")?;
        let file_source = dir.path().join("file");
        file::write(&file_source, "file")?;

        // a single-file copy lists its target once it is written, not the
        // directories created on the way
        let target = dir.path().join("one/settings.toml");
        let mut written = vec![];
        apply_one(
            &link_req(&file_source, &target, FileMode::Copy),
            None,
            &mut written,
        )?;
        assert_eq!(written, vec![target]);

        // a directory copy lists each file as it lands
        let target = dir.path().join("all");
        let mut written = vec![];
        apply_one(
            &link_req(&source, &target, FileMode::Copy),
            None,
            &mut written,
        )?;
        written.sort();
        assert_eq!(
            written,
            vec![target.join("a.toml"), target.join("sub/b.toml")]
        );

        // a directory copy that fails part-way lists the files written
        // before the failure and nothing after it: a file where `sub` must
        // become a directory stops the walk after `a.toml`
        let target = dir.path().join("partial");
        file::create_dir_all(&target)?;
        file::write(target.join("sub"), "in the way")?;
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert_eq!(written, vec![target.join("a.toml")]);

        // a write that fails before touching its target lists nothing
        let target = dir.path().join("partial/sub/settings.toml");
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&file_source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert!(written.is_empty());

        // an existing target that was cleared before the write failed is
        // mutated (its old content is gone), so it is listed
        let target = dir.path().join("replaced");
        file::write(&target, "old")?;
        let missing_source = dir.path().join("missing");
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&missing_source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert!(!target.exists());
        assert_eq!(written, vec![target]);
        // the same failed write against an absent target lists nothing
        let target = dir.path().join("never");
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&missing_source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert!(written.is_empty());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn directory_copy_leaves_an_existing_file_alone_when_the_source_is_not_a_file() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::create_dir_all(source.join("real"))?;
        // a link to a directory walks as a file-like entry but is not one
        std::os::unix::fs::symlink(source.join("real"), source.join("entry"))?;
        let target = dir.path().join("target");
        file::create_dir_all(&target)?;
        file::write(target.join("entry"), "keep me")?;

        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert_eq!(file::read_to_string(target.join("entry"))?, "keep me");
        assert!(written.is_empty());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn directory_copy_leaves_an_existing_link_alone_when_the_source_is_not_a_file() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::create_dir_all(source.join("real"))?;
        std::os::unix::fs::symlink(source.join("real"), source.join("entry"))?;
        let elsewhere = dir.path().join("elsewhere");
        file::write(&elsewhere, "keep me")?;
        let target = dir.path().join("target");
        file::create_dir_all(&target)?;
        std::os::unix::fs::symlink(&elsewhere, target.join("entry"))?;

        // the source cannot be copied, so the link it would replace stays
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert_eq!(std::fs::read_link(target.join("entry"))?, elsewhere);
        assert!(written.is_empty());
        Ok(())
    }

    #[test]
    fn directory_copy_does_not_record_an_existing_directory_the_copy_left_alone() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::create_dir_all(&source)?;
        file::write(source.join("entry"), "file")?;
        // a directory where the copy wants a file: the copy fails without
        // touching it, so nothing changed and nothing is recorded
        let target = dir.path().join("target");
        file::create_dir_all(target.join("entry"))?;
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert!(target.join("entry").is_dir());
        assert!(written.is_empty());
        Ok(())
    }

    #[test]
    fn create_recorded_lists_a_target_the_failed_write_created() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let target = dir.path().join("created");
        // the write created the file before failing: its state changed
        let mut written = vec![];
        let result = create_recorded(&target, &mut written, || {
            file::write(&target, "partial")?;
            bail!("disk full")
        });
        assert!(result.is_err());
        assert_eq!(written, vec![target.clone()]);
        // the write failed before creating anything: nothing to reload
        let target = dir.path().join("never");
        let mut written = vec![];
        let result = create_recorded(&target, &mut written, || bail!("permission denied"));
        assert!(result.is_err());
        assert!(written.is_empty());
        // and a successful write is recorded as before
        let mut written = vec![];
        create_recorded(&target, &mut written, || file::write(&target, "ok"))?;
        assert_eq!(written, vec![target]);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn directory_copy_records_an_existing_file_only_once_it_is_opened_for_writing() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::create_dir_all(&source)?;
        file::write(source.join("a.toml"), "new")?;
        let target = dir.path().join("target");
        file::create_dir_all(&target)?;
        file::write(target.join("a.toml"), "old")?;

        // an existing writable file is overwritten in place and recorded
        let mut written = vec![];
        apply_one(
            &link_req(&source, &target, FileMode::Copy),
            None,
            &mut written,
        )?;
        assert_eq!(written, vec![target.join("a.toml")]);
        assert_eq!(file::read_to_string(target.join("a.toml"))?, "new");

        // a read-only existing file cannot be opened for truncation: nothing
        // changes and nothing is recorded (root can open it regardless, so
        // the case is skipped there)
        file::write(target.join("a.toml"), "old")?;
        std::fs::set_permissions(
            target.join("a.toml"),
            std::fs::Permissions::from_mode(0o444),
        )?;
        if std::fs::OpenOptions::new()
            .write(true)
            .open(target.join("a.toml"))
            .is_ok()
        {
            return Ok(());
        }
        let mut written = vec![];
        assert!(
            apply_one(
                &link_req(&source, &target, FileMode::Copy),
                None,
                &mut written
            )
            .is_err()
        );
        assert!(written.is_empty());
        assert_eq!(file::read_to_string(target.join("a.toml"))?, "old");
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn directory_symlink_listing_is_best_effort() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::create_dir_all(source.join("sub"))?;
        file::write(source.join("sub/hidden.toml"), "x")?;
        // an unreadable subdirectory makes the walk fail (unless running as
        // root, where the walk simply succeeds); the link still lands and is
        // recorded either way
        std::fs::set_permissions(source.join("sub"), std::fs::Permissions::from_mode(0o000))?;
        let target = dir.path().join("target");
        let mut written = vec![];
        let result = apply_one(&symlink_req(&source, &target), None, &mut written);
        std::fs::set_permissions(source.join("sub"), std::fs::Permissions::from_mode(0o755))?;
        result?;
        assert!(target.is_symlink());
        assert_eq!(written.first(), Some(&target));
        Ok(())
    }

    #[test]
    fn composed_symlink_each_allows_disjoint_leaves() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_a = dir.path().join("a");
        let source_b = dir.path().join("b");
        let target = dir.path().join("target");
        file::create_dir_all(source_a.join("conf.d"))?;
        file::create_dir_all(source_b.join("conf.d"))?;
        file::write(source_a.join("conf.d/a.toml"), "a")?;
        file::write(source_b.join("conf.d/b.toml"), "b")?;

        validate_composed_file_footprints(&[
            link_req(&source_a, &target, FileMode::SymlinkEach),
            link_req(&source_b, &target, FileMode::SymlinkEach),
        ])?;
        Ok(())
    }

    #[test]
    fn reconciliation_preserves_every_active_contributor_for_selected_target() {
        let target = PathBuf::from("/home/example");
        let state = |source: &str, leaf: &str| SymlinkEachState {
            version: SYMLINK_EACH_STATE_VERSION,
            source: PathBuf::from(source),
            target: target.clone(),
            links: vec![ManagedLink {
                source: PathBuf::from(source).join(leaf),
                target: target.join(leaf),
            }],
        };
        let home = state("/repo/home", ".zshrc");
        let work = state("/repo/work", ".gitconfig");
        let selected_targets = [target].into_iter().collect();

        let reconciliation =
            reconcile_symlink_each_states(&[home, work.clone()], &selected_targets, &[work]);

        assert!(reconciliation.stale_links.is_empty());
        assert!(reconciliation.targets.is_empty());
    }

    #[test]
    fn reconciliation_preserves_ownership_for_missing_active_source() {
        let target = PathBuf::from("/home/example");
        let stored = SymlinkEachState {
            version: SYMLINK_EACH_STATE_VERSION,
            source: PathBuf::from("/repo/home"),
            target: target.clone(),
            links: vec![ManagedLink {
                source: PathBuf::from("/repo/home/.zshrc"),
                target: target.join(".zshrc"),
            }],
        };
        let active = SymlinkEachState {
            links: vec![],
            ..stored.clone()
        };
        let selected_targets = [target].into_iter().collect();

        let reconciliation = reconcile_symlink_each_states(&[active], &selected_targets, &[stored]);

        assert!(reconciliation.stale_links.is_empty());
        assert!(reconciliation.targets.is_empty());
    }

    #[cfg(unix)]
    #[test]
    fn normalized_source_matches_dangling_managed_link() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_dir = dir.path().join("profile");
        let source = source_dir.join("../profile/.zshrc");
        let target = dir.path().join("target");
        file::create_dir_all(&source_dir)?;
        file::write(&source, "managed")?;
        std::os::unix::fs::symlink(&source, &target)?;
        file::remove_file(&source)?;

        assert!(link_points_to(&lexical_normalize(&source), &target));
        Ok(())
    }

    #[test]
    fn legacy_symlink_each_state_paths_are_normalized() {
        let state = SymlinkEachState {
            version: SYMLINK_EACH_STATE_VERSION,
            source: PathBuf::from("/repo/./home"),
            target: PathBuf::from("/home/example/../example"),
            links: vec![ManagedLink {
                source: PathBuf::from("/repo/home/config/../.zshrc"),
                target: PathBuf::from("/home/example/./.zshrc"),
            }],
        };

        assert_eq!(
            normalize_symlink_each_state(state),
            SymlinkEachState {
                version: SYMLINK_EACH_STATE_VERSION,
                source: PathBuf::from("/repo/home"),
                target: PathBuf::from("/home/example"),
                links: vec![ManagedLink {
                    source: PathBuf::from("/repo/home/.zshrc"),
                    target: PathBuf::from("/home/example/.zshrc"),
                }],
            }
        );
    }

    #[test]
    fn composed_symlink_each_rejects_duplicate_state_identity() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        file::create_dir_all(&source)?;
        let ordinary = link_req(&source, &target, FileMode::SymlinkEach);
        let mut git_manifest = ordinary.clone();
        git_manifest.manifest = Some(FileManifest::Git);

        let err = validate_composed_file_footprints(&[ordinary, git_manifest]).unwrap_err();
        assert!(err.to_string().contains("conflicting symlink-each"));
        Ok(())
    }

    #[test]
    fn composed_symlink_each_rejects_duplicate_leaves() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_a = dir.path().join("a");
        let source_b = dir.path().join("b");
        let target = dir.path().join("target");
        file::create_dir_all(&source_a)?;
        file::create_dir_all(&source_b)?;
        file::write(source_a.join("shared"), "a")?;
        file::write(source_b.join("shared"), "b")?;

        let err = validate_composed_file_footprints(&[
            link_req(&source_a, &target, FileMode::SymlinkEach),
            link_req(&source_b, &target, FileMode::SymlinkEach),
        ])
        .unwrap_err();
        assert!(
            err.to_string()
                .contains(&target.join("shared").to_string_lossy().to_string())
        );
        Ok(())
    }

    #[test]
    fn composed_symlink_each_rejects_file_directory_collisions() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_a = dir.path().join("a");
        let source_b = dir.path().join("b");
        let target = dir.path().join("target");
        file::create_dir_all(&source_a)?;
        file::create_dir_all(source_b.join("shared"))?;
        file::write(source_a.join("shared"), "a")?;
        file::write(source_b.join("shared/nested"), "b")?;

        let err = validate_composed_file_footprints(&[
            link_req(&source_a, &target, FileMode::SymlinkEach),
            link_req(&source_b, &target, FileMode::SymlinkEach),
        ])
        .unwrap_err();
        assert!(
            err.to_string()
                .contains(&target.join("shared").to_string_lossy().to_string())
        );
        Ok(())
    }

    /// Nested declarations may share directories when their concrete leaves
    /// remain disjoint.
    #[test]
    fn tracking_does_not_claim_an_apply_footprint() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        file::write(&source, "managed")?;
        for requests in [
            vec![
                link_req(&source, &target, FileMode::Track),
                link_req(&source, &target.join("nested"), FileMode::Copy),
            ],
            vec![
                link_req(&source, &target.join("nested"), FileMode::Copy),
                link_req(&source, &target, FileMode::Track),
            ],
        ] {
            validate_composed_file_footprints(&requests)?;
        }
        Ok(())
    }

    #[test]
    fn managed_directory_can_contain_explicitly_tracked_children() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        file::create_dir_all(&source)?;
        file::write(source.join("native"), "managed")?;
        for mode in [FileMode::Copy, FileMode::SymlinkEach] {
            let requests = vec![
                link_req(&source, &target, mode),
                link_req(&source, &target.join("native"), FileMode::Track),
            ];
            validate_composed_file_footprints(&requests)?;
            let reversed = requests.into_iter().rev().collect::<Vec<_>>();
            validate_composed_file_footprints(&reversed)?;
        }
        Ok(())
    }

    #[test]
    fn composed_file_footprints_allow_disjoint_nested_leaves() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_tree = dir.path().join("tree");
        let source_file = dir.path().join("nested");
        let target = dir.path().join("target");
        file::create_dir_all(&source_tree)?;
        file::write(source_tree.join("owned-by-tree"), "tree")?;
        file::write(&source_file, "nested")?;

        validate_composed_file_footprints(&[
            link_req(&source_tree, &target, FileMode::Copy),
            link_req(
                &source_file,
                &target.join("owned-separately"),
                FileMode::Copy,
            ),
        ])?;
        Ok(())
    }

    /// A nested whole-file declaration cannot also be owned by a directory
    /// copy, regardless of declaration order.
    #[test]
    fn composed_file_footprints_reject_directory_copy_nested_leaf() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_tree = dir.path().join("tree");
        let source_file = dir.path().join("nested");
        let target = dir.path().join("target");
        file::create_dir_all(&source_tree)?;
        file::write(source_tree.join("shared"), "tree")?;
        file::write(&source_file, "nested")?;
        let tree = link_req(&source_tree, &target, FileMode::Copy);
        let nested = link_req(&source_file, &target.join("shared"), FileMode::Copy);

        for requests in [
            [tree.clone(), nested.clone()],
            [nested.clone(), tree.clone()],
        ] {
            let err = validate_composed_file_footprints(&requests).unwrap_err();
            assert!(err.to_string().contains("conflicting dotfile declarations"));
            assert!(
                err.to_string()
                    .contains(&target.join("shared").to_string_lossy().to_string())
            );
        }
        Ok(())
    }

    /// A whole-resource leaf cannot occupy a path another declaration needs
    /// as a directory.
    #[test]
    fn composed_file_footprints_reject_leaf_required_as_directory() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_tree = dir.path().join("tree");
        let source_file = dir.path().join("nested");
        let target = dir.path().join("target");
        file::create_dir_all(&source_tree)?;
        file::write(&source_file, "nested")?;

        let err = validate_composed_file_footprints(&[
            link_req(&source_tree, &target, FileMode::Symlink),
            link_req(&source_file, &target.join("nested"), FileMode::Copy),
        ])
        .unwrap_err();
        assert!(err.to_string().contains("conflicting dotfile declarations"));
        assert!(
            err.to_string()
                .contains(&target.to_string_lossy().to_string())
        );
        Ok(())
    }

    /// A declaration whose source is unavailable still reserves its target,
    /// preventing a filtered apply from writing a nested declaration there.
    #[test]
    fn composed_file_footprints_reserve_missing_source_target() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let missing = dir.path().join("missing");
        let source_file = dir.path().join("nested");
        let target = dir.path().join("target");
        file::write(&source_file, "nested")?;

        let err = validate_composed_file_footprints(&[
            link_req(&missing, &target, FileMode::Copy),
            link_req(&source_file, &target.join("nested"), FileMode::Copy),
        ])
        .unwrap_err();
        assert!(err.to_string().contains("conflicting dotfile declarations"));
        assert!(
            err.to_string()
                .contains(&target.to_string_lossy().to_string())
        );
        Ok(())
    }

    /// An unavailable symlink-each contributor reserves the shared target as
    /// a directory, allowing a sibling's known leaves to remain composable.
    #[test]
    fn composed_file_footprints_keep_missing_symlink_each_directory_shaped() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let missing = dir.path().join("missing");
        let source_tree = dir.path().join("tree");
        let target = dir.path().join("target");
        file::create_dir_all(&source_tree)?;
        file::write(source_tree.join("known"), "known")?;

        validate_composed_file_footprints(&[
            link_req(&missing, &target, FileMode::SymlinkEach),
            link_req(&source_tree, &target, FileMode::SymlinkEach),
        ])?;
        Ok(())
    }

    /// The fix: a file the user wrote is not mise's to replace. This used to pass on Windows,
    /// where the entry applied and overwrote it with no `--force` and no message.
    #[test]
    fn an_unmanaged_file_at_the_target_is_a_conflict() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        file::write(&source, "managed")?;
        file::write(&target, "the user's own file")?;

        assert_eq!(
            find_conflicts(&symlink_req(&source, &target))?,
            vec![target]
        );
        Ok(())
    }

    /// The control for the test above: the two cases that must *not* start blocking. A symlink
    /// is one mise made, so re-pointing it is the normal path and would be a regression to
    /// refuse; a missing target has nothing to protect.
    #[test]
    fn a_managed_symlink_and_a_missing_target_are_not_conflicts() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "managed")?;

        let absent = dir.path().join("absent");
        assert!(find_conflicts(&symlink_req(&source, &absent))?.is_empty());

        // Created directly rather than through `link_path`, which copies on Windows: the point
        // here is what `find_conflicts` does when a symlink *is* present.
        let link = dir.path().join("link");
        #[cfg(unix)]
        std::os::unix::fs::symlink(&source, &link)?;
        #[cfg(windows)]
        let created = std::os::windows::fs::symlink_file(&source, &link).is_ok();
        #[cfg(unix)]
        let created = true;
        if created {
            assert!(link.is_symlink(), "precondition: target must be a symlink");
            assert!(find_conflicts(&symlink_req(&source, &link))?.is_empty());
        }
        Ok(())
    }

    /// `symlink-each` shares the predicate, so it gains the same protection: a file the user
    /// wrote inside the target directory is not mise's to replace either. Asserted separately
    /// because it reaches `file_link_conflicts` through `walk_source_files` rather than
    /// directly, and the mode is the half of this change most likely to be overlooked.
    #[test]
    fn symlink_each_also_treats_an_unmanaged_file_as_a_conflict() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        // `file::write` does not create parents, and the target directory must already exist or
        // `needed_dirs` would report it as its own conflict and drown the assertion.
        file::create_dir_all(&source)?;
        file::create_dir_all(&target)?;
        file::write(source.join("one"), "managed")?;
        file::write(source.join("two"), "managed")?;
        // Only `one` is squatted on; `two` has nothing in its way.
        file::write(target.join("one"), "the user's own file")?;

        assert_eq!(
            find_conflicts(&link_req(&source, &target, FileMode::SymlinkEach))?,
            vec![target.join("one")]
        );
        Ok(())
    }

    /// A directory where a file belongs blocked before this change and still does — the
    /// type-mismatch case is untouched.
    #[test]
    fn a_directory_at_the_target_is_still_a_conflict() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        let target = dir.path().join("target");
        file::write(&source, "managed")?;
        file::create_dir_all(&target)?;

        assert_eq!(
            find_conflicts(&symlink_req(&source, &target))?,
            vec![target]
        );
        Ok(())
    }

    /// `link_path` produces either a symlink or a copy on Windows depending on a privilege the
    /// test runner may or may not have, so these assert the property that has to hold for both:
    /// whichever form lands, `check_symlink` reads it as Applied. Branching on what actually
    /// happened rather than on an assumed privilege keeps this meaningful on a runner without
    /// Developer Mode, where only the copy path is exercised.
    #[test]
    fn link_path_result_is_recognised_whichever_form_it_takes() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dotfile");
        let target = dir.path().join("linked");
        file::write(&source, "contents")?;

        link_path(&source, &target, false, true)?;

        assert!(
            target.exists() || target.is_symlink(),
            "link_path produced nothing"
        );
        assert_eq!(check_symlink(&source, &target, false)?, FileState::Applied);
        Ok(())
    }

    /// `symlink-each` opts out of the Windows symlink attempt, so it keeps producing a copy
    /// there. Pinned because the two modes share `link_path`: making it symlink for everyone
    /// would route `symlink-each` unapply through a planner that rejects symlinks.
    #[cfg(windows)]
    #[test]
    fn symlink_each_still_copies_on_windows() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dotfile");
        let target = dir.path().join("copied");
        file::write(&source, "contents")?;

        link_path(&source, &target, false, false)?;

        assert!(
            !target.is_symlink(),
            "symlink-each must not create a symlink"
        );
        assert_eq!(file::read_to_string(&target)?, "contents");
        Ok(())
    }

    /// The control for the test above: a target that is neither a copy of the source nor a link
    /// to it must not read as Applied, or the assertion there would hold for the wrong reason.
    #[test]
    fn check_symlink_rejects_an_unrelated_file() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dotfile");
        let target = dir.path().join("unrelated");
        file::write(&source, "contents")?;
        file::write(&target, "something else")?;

        assert!(!matches!(
            check_symlink(&source, &target, false)?,
            FileState::Applied
        ));
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn relative_link_reaches_the_source_and_survives_a_move() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let source = home.join("dotfiles/foo");
        let target = home.join(".config/foo");
        file::create_dir_all(source.parent().unwrap())?;
        file::write(&source, "contents")?;
        file::create_dir_all(target.parent().unwrap())?;

        link_path(&source, &target, true, true)?;

        assert_eq!(
            std::fs::read_link(&target)?,
            PathBuf::from("../dotfiles/foo")
        );
        assert_eq!(check_symlink(&source, &target, true)?, FileState::Applied);
        // either setting accepts a relative link that reaches the source
        assert_eq!(check_symlink(&source, &target, false)?, FileState::Applied);

        let moved = dir.path().join("moved");
        std::fs::rename(&home, &moved)?;
        assert_eq!(file::read_to_string(moved.join(".config/foo"))?, "contents");
        Ok(())
    }

    /// Turning `relative` on must convert a link an earlier apply made.
    #[cfg(unix)]
    #[test]
    fn relative_rejects_an_absolute_link_to_the_source() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dotfile");
        let target = dir.path().join("linked");
        file::write(&source, "contents")?;
        link_path(&source, &target, false, true)?;

        assert!(matches!(
            check_symlink(&source, &target, true)?,
            FileState::Differs(_)
        ));
        Ok(())
    }

    /// `..` in a link resolves against the directory the link physically
    /// sits in, so a link directory reached through a symlink must not get
    /// a path computed from its configured spelling.
    #[cfg(unix)]
    #[test]
    fn relative_link_resolves_from_a_symlinked_link_directory() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("home/dotfiles/foo");
        let real_config = dir.path().join("elsewhere/config");
        let config = dir.path().join("home/.config");
        file::create_dir_all(source.parent().unwrap())?;
        file::write(&source, "contents")?;
        file::create_dir_all(&real_config)?;
        file::make_symlink(&real_config, &config)?;
        let target = config.join("foo");

        link_path(&source, &target, true, true)?;

        assert!(std::fs::read_link(&target)?.is_relative());
        assert_eq!(file::read_to_string(&target)?, "contents");
        assert_eq!(check_symlink(&source, &target, true)?, FileState::Applied);
        Ok(())
    }

    /// Builds `home/dotfiles/foo` and a `home/.config` that is a symlink to
    /// `elsewhere/config`, returning (source, link directory, root).
    #[cfg(unix)]
    fn symlinked_link_dir() -> Result<(tempfile::TempDir, PathBuf, PathBuf)> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("home/dotfiles/foo");
        let real_config = dir.path().join("elsewhere/config");
        let config = dir.path().join("home/.config");
        file::create_dir_all(source.parent().unwrap())?;
        file::write(&source, "contents")?;
        file::create_dir_all(&real_config)?;
        file::make_symlink(&real_config, &config)?;
        Ok((dir, source, config))
    }

    /// Unapply and `symlink-each` pruning must still own a link written from
    /// the canonical link directory once its source file is deleted.
    #[cfg(unix)]
    #[test]
    fn relative_link_in_a_symlinked_directory_is_owned_after_its_source_goes() -> Result<()> {
        let (_dir, source, config) = symlinked_link_dir()?;
        let target = config.join("foo");
        link_path(&source, &target, true, true)?;
        std::fs::remove_file(&source)?;

        assert!(link_points_to(&source, &target));
        Ok(())
    }

    /// Read from the configured spelling, `../dotfiles/foo` in `~/.config`
    /// names `~/dotfiles/foo`, but physically it leads somewhere else. That
    /// link is not mise's to remove.
    #[cfg(unix)]
    #[test]
    fn relative_link_matching_only_as_text_is_not_owned() -> Result<()> {
        let (dir, source, config) = symlinked_link_dir()?;
        let other = dir.path().join("elsewhere/dotfiles/foo");
        file::create_dir_all(other.parent().unwrap())?;
        file::write(&other, "someone else's")?;
        let target = config.join("foo");
        file::make_symlink(Path::new("../dotfiles/foo"), &target)?;

        assert!(!link_points_to(&source, &target));
        assert!(!matches!(
            check_symlink(&source, &target, true)?,
            FileState::Applied
        ));
        Ok(())
    }

    /// A dangling symlink in a `symlink-each` source still gets a relative
    /// link, or the entry would be rewritten on every apply.
    #[cfg(unix)]
    #[test]
    fn relative_link_to_a_dangling_source_converges() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dotfiles/dangling");
        let target = dir.path().join("home/dangling");
        file::create_dir_all(source.parent().unwrap())?;
        file::create_dir_all(target.parent().unwrap())?;
        file::make_symlink(&dir.path().join("nowhere"), &source)?;

        link_path(&source, &target, true, false)?;

        assert_eq!(
            std::fs::read_link(&target)?,
            PathBuf::from("../dotfiles/dangling")
        );
        assert_eq!(check_symlink(&source, &target, true)?, FileState::Applied);
        Ok(())
    }

    /// `alias/../dotfile` leaves wherever `alias` really leads, not the
    /// directory the link sits in, so it names a different file here.
    #[cfg(unix)]
    #[test]
    fn relative_link_through_a_symlink_then_parent_is_not_owned() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dotfile");
        let other = dir.path().join("real/dotfile");
        file::write(&source, "mine")?;
        file::create_dir_all(dir.path().join("real/sub"))?;
        file::write(&other, "someone else's")?;
        file::make_symlink(&dir.path().join("real/sub"), &dir.path().join("alias"))?;
        let target = dir.path().join("link");
        file::make_symlink(Path::new("alias/../dotfile"), &target)?;
        assert_eq!(file::read_to_string(&target)?, "someone else's");

        assert!(!link_points_to(&source, &target));
        std::fs::remove_file(&other)?;
        assert!(!link_points_to(&source, &target));
        Ok(())
    }

    fn permissions_req(target: &Path, permissions: u32) -> FileRequest {
        FileRequest {
            source: PathBuf::new(),
            mode: FileMode::Permissions,
            permissions: Some(permissions),
            policy: FilePolicy::for_mode(FileMode::Permissions),
            ..link_req(Path::new("/unused"), target, FileMode::Copy)
        }
    }

    fn incoming(body: &str) -> Result<()> {
        use crate::config::config_file::mise_toml::MiseToml;
        use std::sync::Arc;

        let path = dirs::HOME.join(".config/mise/config.toml");
        let mut configs = ConfigMap::new();
        configs.insert(
            path.clone(),
            Arc::new(MiseToml::for_history_preflight(body, &path)?),
        );
        validate_incoming_files(&configs)
    }

    #[test]
    fn permissions_parse_as_octal_strings() {
        assert_eq!(parse_permissions("0600").unwrap(), 0o600);
        assert_eq!(parse_permissions("0o750").unwrap(), 0o750);
        assert_eq!(parse_permissions("600").unwrap(), 0o600);
        for invalid in ["", "0800", "rw-------", "17777"] {
            assert!(parse_permissions(invalid).is_err(), "{invalid:?}");
        }
    }

    /// A table holding only `permissions` is a whole-file entry, not an
    /// edit, and it infers no source.
    #[test]
    fn a_permissions_only_table_is_a_whole_file_entry() {
        let value: toml::Value = toml::from_str(r#"permissions = "0600""#).unwrap();
        let Some(FileTomlEntry::Table {
            source,
            permissions,
            ..
        }) = file_entry_from_toml("~/.ssh/config", value)
        else {
            panic!("expected a whole-file table entry");
        };
        assert_eq!(source, None);
        assert_eq!(permissions.as_deref(), Some("0600"));

        // an edit key keeps the table an edit entry
        let value: toml::Value = toml::from_str("block = \"x\"\npermissions = \"0600\"").unwrap();
        assert!(file_entry_from_toml("~/.bashrc/id", value).is_none());
    }

    #[test]
    fn incoming_permissions_accept_file_writing_entries() -> Result<()> {
        incoming(
            r#"
[dotfiles]
"~/.ssh/config" = { permissions = "0600" }
"~/.netrc" = { source = "netrc.tera", mode = "template", permissions = "0600" }
"~/.config/app.toml" = { source = "app.toml", mode = "copy", permissions = "0640" }
"~/.config/token" = { content = "secret\n", permissions = "0400" }
"#,
        )
    }

    #[test]
    fn incoming_permissions_reject_unsupported_combinations() {
        for (entry, expected) in [
            (r#"{ permissions = "0600", mode = "track" }"#, "track"),
            (
                r#"{ source = "x", mode = "symlink", permissions = "0600" }"#,
                "mode copy or template",
            ),
            (
                r#"{ source = "x", mode = "symlink-each", permissions = "0600" }"#,
                "mode copy or template",
            ),
            (r#"{ permissions = "0999" }"#, "octal"),
            (
                r#"{ permissions = "0600", exclude = ["*.bak"] }"#,
                "permissions-only",
            ),
            (
                r#"{ permissions = "0600", encrypt = false }"#,
                "permissions-only",
            ),
            (
                r#"{ source = "x", mode = "copy", manifest = "git", permissions = "0600" }"#,
                "manifest",
            ),
            (r#"{ permissions = "0600" }"#, "wildcards"),
        ] {
            let key = if expected == "wildcards" {
                "~/.ssh/id_*"
            } else {
                "~/.ssh/config"
            };
            let err = incoming(&format!("[dotfiles]\n\"{key}\" = {entry}\n"))
                .expect_err(entry)
                .to_string();
            assert!(err.contains(expected), "{entry}: {err}");
        }
    }

    #[cfg(unix)]
    #[test]
    fn a_permissions_only_entry_changes_only_the_mode() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let target = dir.path().join("config");
        file::write(&target, "user content")?;
        std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o644))?;
        let req = permissions_req(&target, 0o600);

        assert_eq!(
            check_rendered(&req, None)?,
            FileState::Differs("permissions differ".into())
        );
        assert!(find_conflicts(&req)?.is_empty());
        let mut written = vec![];
        apply_one(&req, None, &mut written)?;
        assert_eq!(written, vec![target.clone()]);
        assert_eq!(
            std::fs::metadata(&target)?.permissions().mode() & 0o7777,
            0o600
        );
        assert_eq!(file::read_to_string(&target)?, "user content");
        assert_eq!(check_rendered(&req, None)?, FileState::Applied);

        // unapply never removes a file mise only chmods, even with --force
        let opts = UnapplyOpts {
            dry_run: false,
            verbose: false,
            force: true,
            yes: true,
        };
        assert!(plan_unapply_one(&req, &opts)?.is_none());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn a_permissions_only_entry_skips_missing_targets_and_links() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;

        let missing = dir.path().join("missing/config");
        let req = permissions_req(&missing, 0o600);
        // nothing to adjust counts as satisfied; status names the reason
        assert_eq!(check_rendered(&req, None)?, FileState::Applied);
        assert!(permissions_target_absent(&req).is_some());
        assert!(permissions_target_unavailable(&req)?.is_some());
        // applying is refused rather than creating the file or its parent
        assert!(apply_one(&req, None, &mut vec![]).is_err());
        assert!(!missing.parent().unwrap().exists());

        let real = dir.path().join("real");
        file::write(&real, "linked")?;
        std::fs::set_permissions(&real, std::fs::Permissions::from_mode(0o644))?;
        let link = dir.path().join("link");
        std::os::unix::fs::symlink(&real, &link)?;
        let req = permissions_req(&link, 0o600);
        assert!(matches!(
            check_rendered(&req, None)?,
            FileState::Differs(reason) if reason.contains("symlink")
        ));
        assert!(apply_one(&req, None, &mut vec![]).is_err());
        assert_eq!(
            std::fs::metadata(&real)?.permissions().mode() & 0o7777,
            0o644,
            "the link must not be followed"
        );
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn permissions_override_what_copies_and_templates_would_set() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "managed")?;
        std::fs::set_permissions(&source, std::fs::Permissions::from_mode(0o644))?;
        let mode_of = |path: &Path| -> Result<u32> {
            Ok(std::fs::metadata(path)?.permissions().mode() & 0o7777)
        };

        for mode in [FileMode::Copy, FileMode::Template, FileMode::Content] {
            let target = dir.path().join(mode.name());
            let mut req = link_req(&source, &target, mode);
            req.permissions = Some(0o600);
            if mode == FileMode::Content {
                req.content = Some("managed".into());
            }
            let rendered = (mode == FileMode::Template).then_some("managed");
            apply_one(&req, rendered, &mut vec![])?;
            assert_eq!(mode_of(&target)?, 0o600, "{}", mode.name());
            assert_eq!(check_rendered(&req, rendered)?, FileState::Applied);

            // drift is reported, and applying again repairs it
            std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o644))?;
            assert_eq!(
                check_rendered(&req, rendered)?,
                FileState::Differs("permissions differ".into()),
                "{}",
                mode.name()
            );
            apply_one(&req, rendered, &mut vec![])?;
            assert_eq!(mode_of(&target)?, 0o600, "{}", mode.name());
        }
        Ok(())
    }

    #[test]
    fn composed_file_footprints_scope_permissions_only_entries() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source_file = dir.path().join("file");
        let source_tree = dir.path().join("tree");
        file::write(&source_file, "file")?;
        file::create_dir_all(&source_tree)?;
        file::write(source_tree.join("config"), "tree")?;
        let target = dir.path().join("target");

        // chmodding a directory another entry writes into claims nothing
        let copy = link_req(&source_file, &target.join("config"), FileMode::Copy);
        let directory = permissions_req(&target, 0o700);
        validate_composed_file_footprints(&[directory.clone(), copy.clone()])?;
        validate_composed_file_footprints(&[copy, directory])?;

        // but it must not fight another entry over a file that entry writes
        let tree = link_req(&source_tree, &target, FileMode::Copy);
        let leaf = permissions_req(&target.join("config"), 0o600);
        for requests in [[tree.clone(), leaf.clone()], [leaf, tree.clone()]] {
            let err = validate_composed_file_footprints(&requests).unwrap_err();
            assert!(err.to_string().contains("conflicting dotfile declarations"));
        }

        // permissions on a directory copy apply to no single file
        let mut tree = tree;
        tree.permissions = Some(0o600);
        let err = validate_composed_file_footprints(&[tree]).unwrap_err();
        assert!(err.to_string().contains("requires a file source"));
        Ok(())
    }

    #[test]
    fn incoming_permissions_reject_edit_entries() {
        let err =
            incoming("[dotfiles]\n\"~/.bashrc/id\" = { block = \"x\", permissions = \"0600\" }\n")
                .unwrap_err()
                .to_string();
        assert!(err.contains("whole-file entries"), "{err}");
    }

    /// The chmod acts on a descriptor opened without following a link, so a
    /// symlink swapped in after planning is refused, and a target its owner
    /// cannot read or write is still reachable.
    #[cfg(unix)]
    #[test]
    fn chmod_no_follow_refuses_links_and_reaches_unreadable_targets() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let mode_of =
            |path: &Path| -> Result<u32> { Ok(permission_bits(&std::fs::symlink_metadata(path)?)) };

        let real = dir.path().join("real");
        file::write(&real, "real")?;
        std::fs::set_permissions(&real, std::fs::Permissions::from_mode(0o644))?;
        let link = dir.path().join("link");
        std::os::unix::fs::symlink(&real, &link)?;
        let err = chmod_no_follow(&link, 0o600).unwrap_err().to_string();
        assert!(err.contains("symlink"), "{err}");
        assert_eq!(mode_of(&real)?, 0o644);

        for from in [0o000, 0o200, 0o400] {
            let target = dir.path().join(format!("file{from:o}"));
            file::write(&target, "content")?;
            std::fs::set_permissions(&target, std::fs::Permissions::from_mode(from))?;
            chmod_no_follow(&target, 0o600)?;
            assert_eq!(mode_of(&target)?, 0o600, "from {from:o}");
        }

        let directory = dir.path().join("directory");
        file::create_dir_all(&directory)?;
        chmod_no_follow(&directory, 0o700)?;
        assert_eq!(mode_of(&directory)?, 0o700);
        Ok(())
    }

    /// A declared mode that denies the owner read access leaves content that
    /// cannot be compared; the mode is still checked instead of failing.
    #[cfg(unix)]
    #[test]
    fn a_write_only_copy_is_checked_by_its_mode() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "managed")?;
        let target = dir.path().join("target");
        let mut req = link_req(&source, &target, FileMode::Copy);
        req.permissions = Some(0o200);

        apply_one(&req, None, &mut vec![])?;
        assert_eq!(permission_bits(&std::fs::metadata(&target)?), 0o200);
        assert_eq!(check_rendered(&req, None)?, FileState::Applied);

        std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o000))?;
        assert_eq!(
            check_rendered(&req, None)?,
            FileState::Differs("permissions differ".into())
        );
        apply_one(&req, None, &mut vec![])?;
        assert_eq!(permission_bits(&std::fs::metadata(&target)?), 0o200);
        Ok(())
    }

    /// Drift to a mode that denies the owner read access makes the content
    /// unreadable; it must read as a permission difference apply repairs, not
    /// as a broken entry. Root reads any file, so the case needs a non-root
    /// user to mean anything.
    #[cfg(unix)]
    #[test]
    fn drift_to_an_unreadable_mode_is_repaired() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        if nix::unistd::geteuid().is_root() {
            return Ok(());
        }
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "managed")?;
        for mode in [FileMode::Copy, FileMode::Template, FileMode::Content] {
            let target = dir.path().join(mode.name());
            let mut req = link_req(&source, &target, mode);
            req.permissions = Some(0o600);
            if mode == FileMode::Content {
                req.content = Some("managed".into());
            }
            let rendered = (mode == FileMode::Template).then_some("managed");
            apply_one(&req, rendered, &mut vec![])?;

            for drifted in [0o000, 0o200] {
                std::fs::set_permissions(&target, std::fs::Permissions::from_mode(drifted))?;
                assert_eq!(
                    check_rendered(&req, rendered)?,
                    FileState::Differs("permissions differ".into()),
                    "{} at {drifted:o}",
                    mode.name()
                );
                apply_one(&req, rendered, &mut vec![])?;
                assert_eq!(permission_bits(&std::fs::metadata(&target)?), 0o600);
                assert_eq!(file::read_to_string(&target)?, "managed");
                assert_eq!(check_rendered(&req, rendered)?, FileState::Applied);
            }
        }
        Ok(())
    }

    /// The fallback for a target that cannot be opened for reading or
    /// writing: it works on files and directories and never follows a link.
    #[cfg(unix)]
    #[test]
    fn chmod_of_an_unopenable_target_never_follows_links() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let mode = |bits| nix::sys::stat::Mode::from_bits_truncate(bits);

        let target = dir.path().join("file");
        file::write(&target, "content")?;
        std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o000))?;
        chmod_unopenable_no_follow(&target, mode(0o600))?;
        assert_eq!(permission_bits(&std::fs::symlink_metadata(&target)?), 0o600);

        let directory = dir.path().join("directory");
        file::create_dir_all(&directory)?;
        std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o000))?;
        chmod_unopenable_no_follow(&directory, mode(0o700))?;
        assert_eq!(
            permission_bits(&std::fs::symlink_metadata(&directory)?),
            0o700
        );

        let link = dir.path().join("link");
        std::os::unix::fs::symlink(&target, &link)?;
        assert!(chmod_unopenable_no_follow(&link, mode(0o644)).is_err());
        assert_eq!(permission_bits(&std::fs::symlink_metadata(&target)?), 0o600);
        Ok(())
    }

    fn template_req(dir: &Path, remove_empty: bool) -> Result<FileRequest> {
        let source = dir.join("source.tera");
        file::write(&source, "")?;
        let mut req = link_req(&source, &dir.join("target"), FileMode::Template);
        req.remove_empty = remove_empty;
        Ok(req)
    }

    #[test]
    fn relative_is_rejected_outside_symlink_modes() -> Result<()> {
        use crate::config::config_file::mise_toml::MiseToml;
        use std::sync::Arc;

        let path = dirs::HOME.join(".config/mise/config.toml");
        let validate = |entry: &str| -> Result<()> {
            let body = format!("[dotfiles]\n\"~/.relative-test\" = {entry}\n");
            let mut configs = ConfigMap::new();
            configs.insert(
                path.clone(),
                Arc::new(MiseToml::for_history_preflight(&body, &path)?),
            );
            validate_incoming_files(&configs)
        };
        validate(r#"{ source = "a", mode = "symlink", relative = true }"#)?;
        validate(r#"{ source = "a", mode = "symlink-each", relative = true }"#)?;
        validate(r#"{ source = "a", mode = "copy", relative = false }"#)?;
        for entry in [
            r#"{ source = "a", mode = "copy", relative = true }"#,
            r#"{ source = "a.tera", mode = "template", relative = true }"#,
            r#"{ content = "x", relative = true }"#,
            r#"{ permissions = "0600", relative = true }"#,
            r#"{ mode = "absent", relative = true }"#,
            r#"{ mode = "track", relative = true }"#,
            r#"{ block = "x", relative = true }"#,
            r#"{ line = "x", relative = false }"#,
        ] {
            assert!(validate(entry).is_err(), "{entry} should be rejected");
        }
        Ok(())
    }

    #[test]
    fn remove_empty_is_rejected_outside_template_mode() -> Result<()> {
        use crate::config::config_file::mise_toml::MiseToml;
        use std::sync::Arc;

        let path = dirs::HOME.join(".config/mise/config.toml");
        let validate = |entry: &str| -> Result<()> {
            let body = format!("[dotfiles]\n\"~/.remove-empty-test\" = {entry}\n");
            let mut configs = ConfigMap::new();
            configs.insert(
                path.clone(),
                Arc::new(MiseToml::for_history_preflight(&body, &path)?),
            );
            validate_incoming_files(&configs)
        };
        validate(r#"{ source = "a.tera", mode = "template", remove_empty = true }"#)?;
        validate(
            r#"{ source = "a.tera", mode = "template", permissions = "0600", remove_empty = true }"#,
        )?;
        for entry in [
            r#"{ permissions = "0600", remove_empty = true }"#,
            r#"{ mode = "absent", remove_empty = true }"#,
            r#"{ source = "a", mode = "copy", remove_empty = true }"#,
            r#"{ source = "a", mode = "symlink", remove_empty = true }"#,
            r#"{ content = "x", remove_empty = true }"#,
            r#"{ mode = "track", remove_empty = true }"#,
        ] {
            assert!(validate(entry).is_err(), "{entry} should be rejected");
        }

        let origin = ResourceOrigin {
            config: PathBuf::from("/mise.toml"),
            config_root: PathBuf::from("/"),
            environment: vec![],
            source: None,
        };
        let merge = |entry: &str| {
            let entry: FileTomlEntry = toml::from_str(entry).unwrap();
            let mut merged = IndexMap::new();
            merge_file_entry(
                "~/.remove-empty-test".into(),
                entry,
                Path::new("/"),
                &origin,
                &mut merged,
            );
            merged.into_values().collect::<Vec<_>>()
        };
        let accepted = merge("source = \"a.tera\"\nmode = \"template\"\nremove_empty = true");
        assert!(accepted[0].remove_empty);
        assert!(merge("source = \"a\"\nmode = \"copy\"\nremove_empty = true").is_empty());
        assert!(merge("content = \"x\"\nremove_empty = true").is_empty());
        assert!(merge("permissions = \"0600\"\nremove_empty = true").is_empty());
        assert!(merge("mode = \"absent\"\nremove_empty = true").is_empty());
        Ok(())
    }

    #[test]
    fn dot_prefix_is_rejected_outside_directory_walking_modes() -> Result<()> {
        use crate::config::config_file::mise_toml::MiseToml;
        use std::sync::Arc;

        let path = dirs::HOME.join(".config/mise/config.toml");
        let validate = |entry: &str| -> Result<()> {
            let body = format!("[dotfiles]\n\"~/.dot-prefix-test\" = {entry}\n");
            let mut configs = ConfigMap::new();
            configs.insert(
                path.clone(),
                Arc::new(MiseToml::for_history_preflight(&body, &path)?),
            );
            validate_incoming_files(&configs)
        };
        validate(r#"{ source = "a", mode = "symlink-each", dot_prefix = true }"#)?;
        validate(r#"{ source = "a", mode = "copy", dot_prefix = true }"#)?;
        validate(r#"{ source = "a", mode = "symlink", dot_prefix = false }"#)?;
        for entry in [
            r#"{ source = "a", mode = "symlink", dot_prefix = true }"#,
            r#"{ source = "a", mode = "template", dot_prefix = true }"#,
            r#"{ content = "x", dot_prefix = true }"#,
            r#"{ permissions = "0600", dot_prefix = true }"#,
            r#"{ mode = "absent", dot_prefix = true }"#,
            r#"{ mode = "track", dot_prefix = true }"#,
        ] {
            assert!(validate(entry).is_err(), "{entry} should be rejected");
        }
        // an edit that also says dot_prefix is not a whole-file entry whose
        // block would be dropped, and preflight names the stray key
        let err = validate(r#"{ block = "x", dot_prefix = true }"#)
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("dot_prefix applies to whole-file entries"),
            "{err}"
        );
        assert!(
            file_entry_from_toml(
                "~/.dot-prefix-test/edit",
                toml::from_str("block = \"x\"\ndot_prefix = true")?,
            )
            .is_none()
        );

        let origin = ResourceOrigin {
            config: PathBuf::from("/mise.toml"),
            config_root: PathBuf::from("/"),
            environment: vec![],
            source: None,
        };
        let merge = |entry: &str| {
            let entry: FileTomlEntry = toml::from_str(entry).unwrap();
            let mut merged = IndexMap::new();
            merge_file_entry(
                "~/.dot-prefix-test".into(),
                entry,
                Path::new("/"),
                &origin,
                &mut merged,
            );
            merged.into_values().collect::<Vec<_>>()
        };
        let accepted = merge("source = \"a\"\nmode = \"symlink-each\"\ndot_prefix = true");
        assert!(accepted[0].dot_prefix);
        assert!(merge("source = \"a\"\nmode = \"symlink\"\ndot_prefix = true").is_empty());
        assert!(merge("content = \"x\"\ndot_prefix = true").is_empty());
        assert!(merge("mode = \"absent\"\ndot_prefix = true").is_empty());
        assert!(merge("mode = \"track\"\ndot_prefix = true").is_empty());
        Ok(())
    }

    #[test]
    fn dot_prefix_maps_each_dot_component() {
        let mut req = link_req(Path::new("/src"), Path::new("/home"), FileMode::SymlinkEach);
        let rel = Path::new("dot-config/foo/dot-rc");
        assert_eq!(target_rel(&req, rel), rel);
        req.dot_prefix = true;
        for (source, target) in [
            ("dot-bashrc", ".bashrc"),
            ("dot-config/foo/config.toml", ".config/foo/config.toml"),
            ("dot-config/foo/dot-rc", ".config/foo/.rc"),
            (".already", ".already"),
            ("plain/dot-", "plain/dot-"),
            ("dot-.", "dot-."),
            ("my-dot-file", "my-dot-file"),
        ] {
            assert_eq!(target_rel(&req, Path::new(source)), Path::new(target));
        }
    }

    #[test]
    fn dot_prefix_walks_and_rejects_colliding_names() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("src");
        let target = dir.path().join("home");
        file::create_dir_all(source.join("dot-config/app"))?;
        file::write(source.join("dot-bashrc"), "")?;
        file::write(source.join("dot-config/app/config.toml"), "")?;
        file::write(source.join("dot-git.md"), "")?;
        let mut req = link_req(&source, &target, FileMode::SymlinkEach);
        req.dot_prefix = true;
        // exclude still matches source names
        req.exclude = vec![glob::Pattern::new("dot-git.md")?];
        assert_eq!(
            walk_source_files(&req)?,
            vec![
                (source.join("dot-bashrc"), target.join(".bashrc")),
                (
                    source.join("dot-config/app/config.toml"),
                    target.join(".config/app/config.toml")
                ),
            ]
        );

        file::write(source.join(".bashrc"), "")?;
        let err = walk_source_files(&req).unwrap_err().to_string();
        assert!(err.contains("both deploy to"), "{err}");
        std::fs::remove_file(source.join(".bashrc"))?;

        file::write(source.join(".config"), "")?;
        let err = walk_source_files(&req).unwrap_err().to_string();
        assert!(err.contains("to be a directory"), "{err}");
        // builds that skip apply's footprint validation still check it
        assert!(directory_source_files(&req).is_err());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn legacy_cleanup_recognizes_relative_links() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("src");
        let target = dir.path().join("home");
        file::create_dir_all(source.join("dot-config"))?;
        file::create_dir_all(target.join(".config"))?;
        file::write(source.join("kept"), "")?;
        let plain = target.join("gone");
        let dotted = target.join(".config/gone");
        let kept = target.join("kept");
        // dangling: their sources were removed before any state was recorded
        file::make_symlink(&relative_link_path(&source.join("gone"), &plain), &plain)?;
        file::make_symlink(
            &relative_link_path(&source.join("dot-config/gone"), &dotted),
            &dotted,
        )?;
        file::make_symlink(&relative_link_path(&source.join("kept"), &kept), &kept)?;
        assert!(std::fs::read_link(&plain)?.is_relative());

        let mut req = link_req(&source, &target, FileMode::SymlinkEach);
        req.relative = true;
        assert_eq!(legacy_stale_links(&req)?, vec![plain.clone()]);
        assert_eq!(legacy_owned_links(&req)?, vec![plain.clone(), kept.clone()]);

        req.dot_prefix = true;
        assert_eq!(
            legacy_stale_links(&req)?,
            vec![dotted.clone(), plain.clone()]
        );
        assert_eq!(legacy_owned_links(&req)?, vec![dotted, plain, kept]);

        // a source directory reached through a symlink: links resolve to
        // where it really is, so the source root is compared canonically
        let alias = dir.path().join("alias");
        file::make_symlink(&source, &alias)?;
        let aliased = target.join(".config/aliased");
        file::make_symlink(
            &relative_link_path(&alias.join("dot-config/aliased"), &aliased),
            &aliased,
        )?;
        req.source = alias;
        assert!(legacy_stale_links(&req)?.contains(&aliased));
        assert!(legacy_owned_links(&req)?.contains(&aliased));
        Ok(())
    }

    #[test]
    fn dot_prefix_requires_a_directory_source() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("dot-bashrc");
        file::write(&source, "")?;
        let mut req = link_req(&source, &dir.path().join(".bashrc"), FileMode::Copy);
        req.dot_prefix = true;
        let err = validate_composed_file_footprints(std::slice::from_ref(&req))
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("dot_prefix requires the source to be a directory"),
            "{err}"
        );
        assert!(directory_source_files(&req).is_err());
        Ok(())
    }

    #[test]
    fn an_empty_render_classifies_the_target_by_ownership() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), true)?;
        assert_eq!(empty_render_target(&req)?, EmptyRenderTarget::Absent);
        assert_eq!(check_rendered(&req, Some(" \n"))?, FileState::Applied);

        // an empty or whitespace-only file holds nothing to lose
        file::write(&req.target, " \n\t")?;
        assert_eq!(empty_render_target(&req)?, EmptyRenderTarget::Owned);

        // content with no record is someone else's
        file::write(&req.target, "work = true\n")?;
        assert!(matches!(
            empty_render_target(&req)?,
            EmptyRenderTarget::Conflict(_)
        ));
        assert!(matches!(
            check_rendered(&req, Some(""))?,
            FileState::Differs(reason) if reason.contains("--force")
        ));

        // what mise last wrote is its own to remove
        save_target_state(&req, "work = true\n");
        assert_eq!(empty_render_target(&req)?, EmptyRenderTarget::Owned);
        assert!(matches!(
            check_rendered(&req, Some("\n"))?,
            FileState::Differs(reason) if reason.contains("will be removed")
        ));

        // a later edit takes it back
        file::write(&req.target, "work = true\nedited = 1\n")?;
        assert!(matches!(
            empty_render_target(&req)?,
            EmptyRenderTarget::Conflict(_)
        ));

        file::remove_file(&req.target)?;
        file::create_dir_all(&req.target)?;
        assert_eq!(
            empty_render_target(&req)?,
            EmptyRenderTarget::Conflict("it is a directory")
        );
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn an_empty_render_without_remove_empty_still_writes_the_file() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), false)?;
        assert!(!removes_target(&req, Some("")));
        assert_eq!(check_rendered(&req, Some(""))?, FileState::Missing);
        Ok(())
    }

    #[test]
    fn apply_one_records_what_it_wrote_and_removes_it_when_it_renders_empty() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), true)?;
        let mut written = vec![];
        apply_one(&req, Some("work = true\n"), &mut written)?;
        assert_eq!(file::read_to_string(&req.target)?, "work = true\n");
        assert!(target_state_matches(&req, "work = true\n"));
        // converged with a current record: nothing to record
        assert_eq!(
            template_state_update(&req, Some("work = true\n".into())),
            None
        );

        written.clear();
        apply_one(&req, Some("  \n"), &mut written)?;
        assert!(!req.target.exists());
        assert_eq!(written, vec![req.target.clone()]);
        // removed: converged, and the record still holds the last write so
        // a file brought back by undo is recognised
        assert_eq!(check_rendered(&req, Some(""))?, FileState::Applied);
        assert_eq!(template_state_update(&req, Some(String::new())), None);
        assert!(target_state_matches(&req, "work = true\n"));
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn a_converged_template_without_a_record_gets_one() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), false)?;
        file::write(&req.target, "work = true\n")?;
        assert_eq!(
            template_state_update(&req, Some("work = true\n".into())),
            Some("work = true\n".into())
        );
        save_target_state(&req, "work = true\n");
        assert!(target_state_matches(&req, "work = true\n"));
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn target_state_ignores_unknown_fields_and_defaults_missing_ones() -> Result<()> {
        let state: TargetState = toml::from_str("version = 1\ntarget = \"/x\"\nfuture = [1]\n")?;
        assert_eq!(
            state,
            TargetState {
                version: 1,
                target: PathBuf::from("/x"),
                content_digest: None,
                created_dirs: vec![],
            }
        );
        Ok(())
    }

    /// `home/.config` exists; `newapp/sub` under it is what mise created.
    fn created_dirs_fixture(home: &Path) -> Result<(PathBuf, Vec<PathBuf>)> {
        let newapp = home.join(".config/newapp");
        let sub = newapp.join("sub");
        file::create_dir_all(&sub)?;
        Ok((sub.join("work.toml"), vec![newapp, sub]))
    }

    /// Prune after the targets of `removals` are gone, each paired with the
    /// directories its record lists.
    fn prune_after(
        removals: &[(&Path, Vec<PathBuf>)],
        claimed: &HashSet<PathBuf>,
        home: &Path,
    ) -> Result<()> {
        let reqs = removals
            .iter()
            .map(|(target, _)| link_req(Path::new("/source"), target, FileMode::Copy))
            .collect::<Vec<_>>();
        let pairs = reqs
            .iter()
            .zip(removals)
            .map(|(req, (_, dirs))| (req, dirs.clone()))
            .collect::<Vec<_>>();
        prune_created_dirs(&pairs, claimed, home, false);
        Ok(())
    }

    #[test]
    fn removing_created_dirs_keeps_pre_existing_ones() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let (target, created) = created_dirs_fixture(&home)?;
        prune_after(&[(&target, created.clone())], &HashSet::new(), &home)?;
        assert!(!created[0].exists());
        assert!(home.join(".config").is_dir());
        Ok(())
    }

    #[test]
    fn removing_created_dirs_stops_at_one_holding_anything() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let (target, created) = created_dirs_fixture(&home)?;
        file::write(created[0].join("mine.toml"), "")?;
        prune_after(&[(&target, created.clone())], &HashSet::new(), &home)?;
        assert!(!created[1].exists());
        assert!(created[0].join("mine.toml").exists());

        // a directory still holding the target is not touched at all
        file::create_dir_all(&created[1])?;
        file::write(&target, "")?;
        prune_after(&[(&target, created.clone())], &HashSet::new(), &home)?;
        assert!(target.exists());
        Ok(())
    }

    #[test]
    fn removing_created_dirs_stops_at_one_not_recorded() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let (target, created) = created_dirs_fixture(&home)?;
        // only the outer one recorded: the walk never reaches it
        prune_after(&[(&target, created[..1].to_vec())], &HashSet::new(), &home)?;
        assert!(created[1].is_dir());
        // an old record with none removes nothing
        prune_after(&[(&target, vec![])], &HashSet::new(), &home)?;
        assert!(created[1].is_dir());
        Ok(())
    }

    #[test]
    fn removing_created_dirs_passes_over_one_already_gone() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let (target, created) = created_dirs_fixture(&home)?;
        // an earlier removal took `sub` and dropped it from the record, and
        // the user's file kept `newapp`; now that file is gone too
        file::remove_dir(&created[1])?;
        prune_after(&[(&target, created[..1].to_vec())], &HashSet::new(), &home)?;
        assert!(!created[0].exists());
        assert!(home.join(".config").is_dir());
        Ok(())
    }

    #[test]
    fn a_created_dir_that_cannot_be_checked_stays_recorded_without_failing() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let (target, created) = created_dirs_fixture(&home)?;
        // `sub` became a file, so looking inside it fails (ENOTDIR, even as
        // root) for a recorded directory beneath it
        file::remove_dir(&created[1])?;
        file::write(&created[1], "")?;
        let below = created[1].join("inner");
        let target_below = below.join("file");
        let mut recorded = created.clone();
        recorded.push(below.clone());
        let chain = created_dirs_to_prune(&target_below, &recorded, &home);
        assert_eq!(chain.first(), Some(&below));
        assert!(remove_created_dirs(&chain, &HashSet::new(), &home).is_empty());

        // the whole prune is best effort: nothing fails, the record keeps
        // what could not be removed, and the file in the way stays
        let source = dir.path().join("source");
        file::write(&source, "")?;
        let req = link_req(&source, &target_below, FileMode::Copy);
        record_created_dirs(&req, &recorded);
        prune_created_dirs(&[(&req, recorded.clone())], &HashSet::new(), &home, true);
        assert_eq!(recorded_created_dirs(&req), recorded);
        assert!(created[1].is_file());
        assert!(!target.exists());
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn removing_created_dirs_never_removes_home_or_above() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        file::create_dir_all(&home)?;
        let created = vec![dir.path().to_path_buf(), home.clone()];
        let target = home.join(".rc");
        assert!(created_dirs_to_prune(&target, &created, &home).is_empty());
        prune_after(&[(&target, created)], &HashSet::new(), &home)?;
        assert!(home.is_dir());
        Ok(())
    }

    #[test]
    fn removing_created_dirs_keeps_directories_outside_home() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        file::create_dir_all(&home)?;
        // an absolute target outside home, such as /opt/newapp/file
        let newapp = dir.path().join("opt/newapp");
        file::create_dir_all(&newapp)?;
        let target = newapp.join("file");
        let created = vec![dir.path().join("opt"), newapp.clone()];
        assert!(created_dirs_to_prune(&target, &created, &home).is_empty());
        prune_after(&[(&target, created)], &HashSet::new(), &home)?;
        assert!(newapp.is_dir());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn removing_created_dirs_keeps_one_a_symlinked_ancestor_puts_outside_home() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        file::create_dir_all(&home)?;
        // ~/.config -> <tmp>/opt/config: ~/.config/app is really outside home
        let outside = dir.path().join("opt/config");
        file::create_dir_all(outside.join("app"))?;
        std::os::unix::fs::symlink(&outside, home.join(".config"))?;
        let app = home.join(".config/app");
        let target = app.join("file");
        prune_after(&[(&target, vec![app.clone()])], &HashSet::new(), &home)?;
        assert!(outside.join("app").is_dir());

        // a symlinked ancestor that resolves inside home: the directory is
        // checked and removed at its resolved location
        let real = home.join("real-config");
        file::create_dir_all(real.join("app"))?;
        std::os::unix::fs::symlink(&real, home.join(".linked"))?;
        let linked = home.join(".linked/app");
        prune_after(
            &[(&linked.join("file"), vec![linked.clone()])],
            &HashSet::new(),
            &home,
        )?;
        assert!(!real.join("app").exists());
        assert!(real.is_dir());

        // the same layout with a real ~/.config inside home goes
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let app = home.join(".config/app");
        file::create_dir_all(&app)?;
        prune_after(
            &[(&app.join("file"), vec![app.clone()])],
            &HashSet::new(),
            &home,
        )?;
        assert!(!app.exists());
        Ok(())
    }

    #[test]
    fn removing_sibling_targets_prunes_their_shared_parent_in_any_order() -> Result<()> {
        for owner_first in [true, false] {
            let dir = tempfile::tempdir()?;
            let home = dir.path().join("home");
            let newapp = home.join(".config/newapp");
            file::create_dir_all(&newapp)?;
            // only the first entry written into newapp recorded it
            let owner = (newapp.join("a.toml"), vec![newapp.clone()]);
            let sibling = (newapp.join("b.toml"), vec![]);
            let mut removals = vec![
                (owner.0.as_path(), owner.1),
                (sibling.0.as_path(), sibling.1),
            ];
            if !owner_first {
                removals.reverse();
            }
            prune_after(&removals, &HashSet::new(), &home)?;
            assert!(!newapp.exists(), "owner first: {owner_first}");
            assert!(home.join(".config").is_dir());
        }

        // a sibling in a deeper directory it created itself, and a shallower
        // one whose parent another record lists
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let newapp = home.join(".config/newapp");
        let deep = newapp.join("p/q");
        file::create_dir_all(&deep)?;
        file::create_dir_all(newapp.join("t"))?;
        let s = deep.join("s");
        let t = newapp.join("t/u");
        let removals = [
            (
                s.as_path(),
                vec![newapp.clone(), newapp.join("p"), deep.clone()],
            ),
            (t.as_path(), vec![newapp.join("t")]),
        ];
        prune_after(&removals, &HashSet::new(), &home)?;
        assert!(!newapp.exists());
        Ok(())
    }

    #[test]
    fn removing_created_dirs_keeps_one_another_entry_needs() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let home = dir.path().join("home");
        let (target, created) = created_dirs_fixture(&home)?;
        let claimed = HashSet::from([created[0].clone()]);
        prune_after(&[(&target, created.clone())], &claimed, &home)?;
        assert!(!created[1].exists());
        assert!(created[0].is_dir());

        // a symlink-each entry claims its target and every directory below it
        let source = dir.path().join("links");
        file::create_dir_all(source.join("nested"))?;
        file::write(source.join("nested/file"), "")?;
        let links = link_req(&source, &created[0], FileMode::SymlinkEach);
        let claimed = claimed_dirs([&links])?;
        assert!(claimed.contains(&created[0]));
        assert!(claimed.contains(&created[0].join("nested")));

        // a permissions-only entry naming a directory claims it, and never
        // records directories of its own
        let perms = permissions_req(&created[0], 0o700);
        assert!(!records_created_dirs(&perms));
        let claimed = claimed_dirs([&perms])?;
        file::create_dir_all(&created[1])?;
        prune_after(&[(&target, created.clone())], &claimed, &home)?;
        assert!(!created[1].exists());
        assert!(created[0].is_dir());
        Ok(())
    }

    #[test]
    fn an_absent_entry_prunes_what_an_earlier_entry_for_its_target_created() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "copied")?;
        let newapp = dir.path().join("newapp");
        let target = newapp.join("sub/file");
        let copy = link_req(&source, &target, FileMode::Copy);
        apply_one(&copy, None, &mut vec![])?;

        // the entry is changed to mode = "absent": it records nothing, needs
        // nothing, and finds the copy's record under the same target
        let absent = absent_req(&target);
        assert!(!records_created_dirs(&absent));
        assert!(claimed_dirs([&absent])?.is_empty());
        assert!(apply_removes_target(&absent, None));
        let created = removal_created_dirs(&absent);
        assert_eq!(created, vec![newapp.clone(), newapp.join("sub")]);
        apply_one(&absent, None, &mut vec![])?;
        assert!(!target.exists());
        // the tempdir stands in for home
        prune_created_dirs(&[(&absent, created)], &HashSet::new(), dir.path(), true);
        assert!(!newapp.exists());
        assert!(removal_created_dirs(&absent).is_empty());
        remove_target_state(&copy)?;
        Ok(())
    }

    #[test]
    fn a_converged_removal_retries_leftover_created_dirs() -> Result<()> {
        // under the test home, since pruning never leaves it
        let dir = tempfile::Builder::new().tempdir_in(*dirs::HOME)?;
        let source = dir.path().join("source");
        file::write(&source, "copied")?;
        let newapp = dir.path().join("newapp");
        let target = newapp.join("sub/file");
        apply_one(
            &link_req(&source, &target, FileMode::Copy),
            None,
            &mut vec![],
        )?;
        // the target went but an earlier prune left the directories
        file::remove_file(&target)?;
        let absent = absent_req(&target);
        assert_eq!(check_rendered(&absent, None)?, FileState::Applied);
        assert!(apply_removes_target(&absent, None));
        assert!(has_leftover_created_dirs(&absent));

        let plan = ApplyPlan {
            todo: vec![],
            record_symlink_each: vec![],
            record_templates: vec![],
            prune_leftovers: vec![&absent],
            claimed_dirs: HashSet::new(),
            reconciliation: SymlinkEachReconciliation {
                stale_links: vec![],
                targets: vec![],
            },
        };
        prune_after_apply([], &plan);
        assert!(!newapp.exists());
        assert!(removal_created_dirs(&absent).is_empty());
        assert!(!has_leftover_created_dirs(&absent));
        remove_target_state(&absent)?;
        Ok(())
    }

    #[test]
    fn apply_one_records_the_directories_it_creates() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "copied")?;
        let newapp = dir.path().join("newapp");
        let req = link_req(&source, &newapp.join("sub/file"), FileMode::Copy);
        apply_one(&req, None, &mut vec![])?;
        let recorded = load_target_state(&req).expect("record").created_dirs;
        assert_eq!(recorded, vec![newapp.clone(), newapp.join("sub")]);

        // a second apply creates nothing and keeps what was recorded
        apply_one(&req, None, &mut vec![])?;
        assert_eq!(
            load_target_state(&req).expect("record").created_dirs,
            recorded
        );
        assert!(
            touched_paths(&req)?
                .iter()
                .all(|(path, _)| path != &target_state_path(&req))
        );

        // a symlink-each entry shares its target with unmanaged files
        let links = dir.path().join("links");
        file::create_dir_all(&links)?;
        let each = link_req(
            &links,
            &dir.path().join("each/target"),
            FileMode::SymlinkEach,
        );
        apply_one(&each, None, &mut vec![])?;
        assert!(load_target_state(&each).is_none());

        // pruning drops what it removed from the record; the tempdir stands
        // in for home
        file::remove_file(&req.target)?;
        prune_created_dirs(
            &[(&req, recorded_created_dirs(&req))],
            &HashSet::new(),
            dir.path(),
            true,
        );
        assert!(!newapp.exists());
        assert!(
            load_target_state(&req)
                .expect("record")
                .created_dirs
                .is_empty()
        );
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn a_removal_rechecks_ownership_right_before_it_runs() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), true)?;
        let mut written = vec![];
        apply_one(&req, Some("work = true\n"), &mut written)?;
        // planned as owned, then edited while the prompt waited
        assert_eq!(empty_render_target(&req)?, EmptyRenderTarget::Owned);
        file::write(&req.target, "work = true\nmine = 1\n")?;
        assert!(recheck_removal(&req, Some(""), false).is_err());
        recheck_removal(&req, Some(""), true)?;
        // a write, or an unchanged owned target, needs no recheck
        recheck_removal(&req, Some("work = true\n"), false)?;
        file::write(&req.target, "work = true\n")?;
        recheck_removal(&req, Some(""), false)?;
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn creating_a_parent_journals_it_with_the_record() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source.tera");
        file::write(&source, "")?;
        let newapp = dir.path().join("newapp");
        let req = link_req(&source, &newapp.join("work.toml"), FileMode::Content);
        // the missing parent before the target, and the record it goes in
        let paths = touched_paths(&req)?;
        assert_eq!(paths[0], (newapp.clone(), Capture::Shallow));
        assert!(paths.contains(&(target_state_path(&req), Capture::Full)));
        Ok(())
    }

    #[test]
    fn unapply_clears_the_record_of_an_already_removed_target() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), true)?;
        let opts = UnapplyOpts {
            dry_run: false,
            verbose: false,
            force: false,
            yes: true,
        };
        assert!(plan_unapply_one(&req, &opts)?.is_none());
        save_target_state(&req, "work = true\n");
        let plan = plan_unapply_one(&req, &opts)?.expect("a plan that clears the record");
        assert!(plan.paths.is_empty());
        remove_target_state(&req)?;
        Ok(())
    }

    #[test]
    fn unapply_plans_every_single_file_kind_whose_target_is_gone_by_its_record() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let source = dir.path().join("source");
        file::write(&source, "copied")?;
        let links = dir.path().join("links");
        file::create_dir_all(&links)?;
        let opts = UnapplyOpts {
            dry_run: false,
            verbose: false,
            force: false,
            yes: true,
        };
        for mode in [FileMode::Copy, FileMode::Symlink, FileMode::Content] {
            let newapp = dir.path().join(format!("{}-app", mode.name()));
            let mut req = link_req(&source, &newapp.join("sub/file"), mode);
            if mode == FileMode::Content {
                req.content = Some("inline".into());
            }
            apply_one(&req, None, &mut vec![])?;
            assert!(!recorded_created_dirs(&req).is_empty(), "{mode:?}");
            // the user deletes the file by hand
            file::remove_file(&req.target)?;
            let plan = plan_unapply_one(&req, &opts)?.expect("a plan that clears the record");
            assert!(plan.paths.is_empty(), "{mode:?}");
            remove_target_state(&req)?;
            // without a record there is nothing to do
            assert!(plan_unapply_one(&req, &opts)?.is_none(), "{mode:?}");
        }
        // a directory copy keeps no record and plans as before
        let each = link_req(&links, &dir.path().join("each"), FileMode::Copy);
        assert!(plan_unapply_one(&each, &opts)?.is_none());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn an_unreadable_target_is_a_conflict_force_can_clear() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir()?;
        let req = template_req(dir.path(), true)?;
        file::write(&req.target, "work = true\n")?;
        save_target_state(&req, "work = true\n");
        std::fs::set_permissions(&req.target, std::fs::Permissions::from_mode(0o000))?;
        // root reads any file, so ownership is still provable there
        if std::fs::read(&req.target).is_err() {
            assert_eq!(
                empty_render_target(&req)?,
                EmptyRenderTarget::Conflict("it cannot be read to confirm mise wrote it")
            );
            assert!(matches!(
                check_rendered(&req, Some(""))?,
                FileState::Differs(reason) if reason.contains("cannot be read")
            ));
            assert!(recheck_removal(&req, Some(""), false).is_err());
            recheck_removal(&req, Some(""), true)?;
        }
        // --force removes it: removal needs only the directory to be writable
        let mut written = vec![];
        apply_one(&req, Some(""), &mut written)?;
        assert!(std::fs::symlink_metadata(&req.target).is_err());
        remove_target_state(&req)?;
        Ok(())
    }
}