vcs-core 0.9.0

One async Rust API over Git and Jujutsu: repo auto-detection plus a unified, backend-agnostic VCS handle (status, branches, worktrees, snapshots).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
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
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(rustdoc::broken_intra_doc_links)]
//! `vcs-core` — write code against "the repository" without caring whether it's
//! git or jj.
//!
//! You hold one handle, [`Repo`], that auto-detects whether a directory is a git or
//! a jj checkout and runs whatever operations *both* tools support — handing back
//! plain result types ([`RepoSnapshot`], [`FileChange`], [`MergeProbe`], …) that
//! don't mention the backend (whether the repo is git or jj). Async, structured
//! errors, and every subprocess
//! inherits the underlying client's OS-**job** containment (an OS-level container
//! that kills the whole process tree if your program exits, via [`processkit`]) so
//! no `git`/`jj` tree is orphaned.
//!
//! # What you can do
//!
//! From one [`Repo`] handle: read the current branch and a batched status
//! [`snapshot`](Repo::snapshot) · list & diff changed files · commit paths · fetch
//! / push / checkout / rebase · probe a merge for conflicts
//! ([`try_merge`](Repo::try_merge)) · drive in-progress merge/rebase state · manage
//! worktrees. Open one and read a prompt line:
//!
//! ```no_run
//! use vcs_core::Repo;
//! # async fn demo() -> vcs_core::Result<()> {
//! let repo = Repo::discover(".")?;        // walks up, detects git vs jj
//! let s = repo.snapshot().await?;         // a few spawns, not a call per field
//! let branch = s.branch.as_deref().unwrap_or("(detached)");
//! println!("{branch} {}", if s.dirty { "*" } else { "" });
//! # Ok(()) }
//! ```
//!
//! **It's a thin common layer, not a god-object.** The shared surface carries only
//! what unifies *without lying*; the few operations the two tools model too
//! differently (a full `merge`, jj's `op restore`, range/revset queries) stay on
//! the raw `git`/`jj` handle rather than being faked (see
//! [below](#whats-deliberately-not-unified)). Reach for the unified handle when code
//! must work on both backends; drop to the raw client when you need power only one
//! of them offers.
//!
//! # Mental model (engineering reference)
//!
//! The surface is three layers, narrowing from "which tool is this?" to "do the
//! thing":
//!
//! - **[`discover`]** — walk up from a directory to the filesystem root for a
//!   `.git`/`.jj` repo (jj wins when colocated — it's the tool driving the working
//!   copy). Pure filesystem probing, no subprocess; yields a [`Located`]
//!   ([`BackendKind`] + worktree root).
//! - **[`Repo`]** — the cwd-bound facade handle, the thing you hold. Open one with
//!   [`Repo::discover`] (walks up to find the repo; real job-backed runner) or
//!   [`Repo::open`] (strict — exactly `dir`, no walking up), or build it over an
//!   explicit client with [`Repo::from_git`] / [`Repo::from_jj`] (the test seam).
//!   Re-anchor it to another directory cheaply with [`Repo::at`] — the backend is
//!   shared behind an `Arc`, so threading work across worktrees never re-detects
//!   or rebuilds the client. Inspect it with [`kind`](Repo::kind) /
//!   [`root`](Repo::root) / [`cwd`](Repo::cwd).
//! - **[`VcsRepo`]** — the same common surface as an object-safe trait, so a
//!   consumer can hold a `Box<dyn VcsRepo>` / `&dyn VcsRepo` without naming the
//!   [`ProcessRunner`] generic. Every method mirrors the like-named inherent method
//!   on [`Repo`]; it adds nothing but the abstraction boundary.
//!
//! ## The common operations
//!
//! All on [`Repo`] (and [`VcsRepo`]), dir-free, dispatched per backend:
//!
//! - **Refs** — [`current_branch`](Repo::current_branch),
//!   [`trunk`](Repo::trunk), [`local_branches`](Repo::local_branches),
//!   [`branch_exists`](Repo::branch_exists),
//!   [`create_branch`](Repo::create_branch),
//!   [`delete_branch`](Repo::delete_branch),
//!   [`rename_branch`](Repo::rename_branch) (branch on git, bookmark on jj).
//! - **Status** — [`changed_files`](Repo::changed_files),
//!   [`diff_stat`](Repo::diff_stat), [`diff`](Repo::diff),
//!   [`has_uncommitted_changes`](Repo::has_uncommitted_changes),
//!   [`has_tracked_changes`](Repo::has_tracked_changes),
//!   [`conflicted_files`](Repo::conflicted_files), and
//!   [`snapshot`](Repo::snapshot) — a **batched** prompt/status-bar read of the
//!   lot in one or two spawns.
//! - **Mutations** — [`commit_paths`](Repo::commit_paths) (partial commit),
//!   [`fetch`](Repo::fetch) / [`fetch_from`](Repo::fetch_from) /
//!   [`fetch_branch`](Repo::fetch_branch) /
//!   [`push`](Repo::push), [`checkout`](Repo::checkout),
//!   [`rebase`](Repo::rebase).
//! - **Merge & operation state** — [`try_merge`](Repo::try_merge) (a
//!   trace-free conflict probe → [`MergeProbe`]),
//!   [`in_progress_state`](Repo::in_progress_state) /
//!   [`abort_in_progress`](Repo::abort_in_progress) /
//!   [`continue_in_progress`](Repo::continue_in_progress) → [`OperationState`].
//! - **Worktrees / workspaces** — [`list_worktrees`](Repo::list_worktrees),
//!   [`create_worktree`](Repo::create_worktree),
//!   [`remove_worktree`](Repo::remove_worktree), and the **synchronous**
//!   [`cleanup_worktree_blocking`](Repo::cleanup_worktree_blocking) for a `Drop`
//!   guard that cannot `.await`.
//!
//! Because the backends genuinely diverge in places, several common methods carry
//! a documented asymmetry (e.g. `upstream`/`ahead`/`behind` are always `None` on
//! jj; [`diff_stat`](Repo::diff_stat) and [`diff`](Repo::diff) exclude untracked
//! files on git but not jj;
//! [`in_progress_state`](Repo::in_progress_state) never returns `Conflict` on git).
//! The method docs spell each one out — the facade unifies the *shape*, not away
//! the truth.
//!
//! ## The escape hatches
//!
//! Tool-specific work reaches the underlying typed clients without adding
//! `vcs-git`/`vcs-jj` as separate dependencies (both are re-exported):
//! [`git_at`](Repo::git_at) / [`jj_at`](Repo::jj_at) hand out a cwd-bound view
//! ([`GitAt`] / [`JjAt`], `dir` dropped); the raw
//! [`git`](Repo::git) / [`jj`](Repo::jj) hand out a borrow of the client itself.
//! Each returns `None` for the other backend.
//!
//! ## What's deliberately *not* unified
//!
//! Three families stay off the common surface because no honest single shape
//! exists — reach them through the bound handles:
//!
//! - **Full `merge`** — jj composes `new` + `squash` + bookmark moves; git runs a
//!   single command. Only the *conflict probe* unifies, as
//!   [`try_merge`](Repo::try_merge).
//! - **Operation rollback** — jj's `op restore` has no faithful git analogue; use
//!   [`Jj::transaction`](vcs_jj::Jj::transaction) on the jj client.
//! - **Range / revset queries** — commit counts and diff stats over a range: git's
//!   `a..b` and jj's revsets aren't interchangeable, so neither is forced onto a
//!   shared signature.
//!
//! # Recipes
//!
//! Probe a merge for conflicts (trace-free), or spin up a worktree:
//!
//! ```no_run
//! use std::path::Path;
//! use vcs_core::{MergeProbe, Repo, WorktreeCreate};
//! # async fn demo(repo: &Repo) -> vcs_core::Result<()> {
//! match repo.try_merge("feature").await? {
//!     MergeProbe::Clean            => println!("merges cleanly"),
//!     MergeProbe::Conflicts(paths) => println!("would conflict in {paths:?}"),
//!     _                            => {} // #[non_exhaustive]
//! }
//! let wt = repo
//!     .create_worktree(WorktreeCreate::new(Path::new("/tmp/feat"), "feature").base("main"))
//!     .await?;
//! # let _ = wt;
//! # Ok(()) }
//! ```
//!
//! # Testing
//!
//! There is **no mock feature** on the facade traits — the runner is the seam.
//! Build a [`Repo`] over a fake [`ProcessRunner`] with [`Repo::from_git`] /
//! [`Repo::from_jj`] (e.g. a [`ScriptedRunner`](processkit::testing::ScriptedRunner)
//! replying to canned argv), so the *real* per-backend dispatch, argv-building and
//! parsing run against canned output — exactly what a mocked `VcsRepo` would skip.
//! The cross-cutting patterns live in
//! [vcs-testkit's guide](https://docs.rs/vcs-testkit/latest/vcs_testkit/guide/testing/).
//!
//! ```no_run
//! use processkit::testing::{Reply, ScriptedRunner};
//! use vcs_core::{vcs_git::Git, Repo};
//! # async fn demo() -> vcs_core::Result<()> {
//! let runner = ScriptedRunner::new().on(["git", "status"], Reply::ok(" M a.rs\0"));
//! let repo = Repo::from_git("/repo", "/repo", Git::with_runner(runner));
//! assert!(repo.has_uncommitted_changes().await?);
//! # Ok(()) }
//! ```
//!
//! # In-depth guide
//!
//! Beyond this page, this crate ships a full how-to guide — rendered on docs.rs
//! from `docs/`. See the [`guide`] module, which walks every operation in depth
//! and hosts the cross-cutting sub-guides: a [`cookbook`](guide::cookbook) of
//! end-to-end flows, the [`process_model`](guide::process_model) (job containment,
//! errors, cancellation), [`positioning`](guide::positioning) (facade-vs-raw-client
//! and the three call shapes), and the [`stability`](guide::stability) contract.

use std::fmt::{self, Debug, Formatter};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use processkit::{JobRunner, ProcessRunner};
use vcs_git::{Git, GitAt};
use vcs_jj::{Jj, JjAt};

mod dto;
mod error;
mod git_backend;
mod jj_backend;

pub use dto::{
    AnnotationLine, BackendKind, BranchDelete, ChangeKind, Commit, CreateOutcome, DiffStat,
    FileChange, FileDiff, MergeProbe, OperationState, RepoSnapshot, UpstreamTracking,
    WorktreeCreate, WorktreeCreatePartial, WorktreeInfo, WorktreeRemove,
};
pub use error::{Error, Result};
// The shared output-budget knob (from the CLI-support plumbing, via `vcs-git`): a
// per-client default ([`Repo::from_git`]/[`from_jj`] over a client built with
// `default_output_budget`) or a per-call override
// ([`Repo::show_file_within`](Repo::show_file_within)) for the content read this
// facade exposes. `vcs-git` and `vcs-jj` re-export the same type.
pub use vcs_git::OutputBudget;

// Re-export the underlying typed clients so a consumer depending only on
// `vcs-core` can still reach raw, tool-specific operations — and their types
// (`GitApi`, `JjApi`, `WorktreeAdd`, `JjFileset`, …) — without adding `vcs-git`
// / `vcs-jj` as separate dependencies. [`Repo::git`] / [`Repo::jj`] hand out
// borrows of these clients; the consumer decides, per call, whether to go
// through the facade or straight to the tool.
pub use vcs_git;
pub use vcs_jj;
// Re-export `processkit` itself so a `vcs-core`-only consumer can name the
// wrapped error directly — `match err { Error::Vcs(vcs_core::processkit::Error::
// Timeout { .. }) => … }` — and reach `Outcome`/`CancellationToken`/… without
// adding `processkit` as a separate dependency. (`Error::Vcs` carries a
// `processkit::Error`; the classifiers below cover the common branches.)
pub use processkit;
// Also surfaced at the crate root so the token a `default_cancel_on` client takes
// (built via `Git`/`Jj`, then passed to `Repo::from_git`/`from_jj`) is one name
// away. (Cancellation is core in processkit 0.10 — always available, no feature.)
pub use processkit::CancellationToken;

/// The result of [`discover`]: which backend, and the repository root it was
/// found at.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Located {
    /// The detected backend.
    pub kind: BackendKind,
    /// The directory holding `.git`/`.jj` — the worktree root.
    pub root: PathBuf,
}

/// Walk up from `start` to the filesystem root looking for a repository. A `.jj`
/// directory wins over `.git` (colocated repos are driven through jj); `.git` may
/// be a directory or a gitlink file (a linked worktree/submodule). Pure
/// filesystem probing — no subprocess.
///
/// `start` is walked exactly as given via [`Path::parent`], so pass an **absolute**
/// path to search ancestors — a relative path like `"."` has no ancestor chain
/// and only its own directory is checked. ([`Repo::discover`] absolutises for
/// you.) See [`Repo::open`] for a strict, non-walking check of exactly one
/// directory.
pub fn discover(start: &Path) -> Option<Located> {
    let mut current = Some(start);
    while let Some(dir) = current {
        if is_jj_marker(&dir.join(".jj")) {
            return Some(Located {
                kind: BackendKind::Jj,
                root: dir.to_path_buf(),
            });
        }
        if is_git_marker(&dir.join(".git")) {
            return Some(Located {
                kind: BackendKind::Git,
                root: dir.to_path_buf(),
            });
        }
        current = dir.parent();
    }
    None
}

/// Whether `path` (a candidate `.jj`) is a real jj repository marker — a `.jj`
/// **directory** that contains a **`repo`** entry (the store: a *directory* in a
/// repo's main workspace / a colocated repo, a *file* pointer in a secondary
/// workspace). A stray/empty directory merely *named* `.jj` (e.g. a leftover
/// `mkdir .jj`) has no `repo` entry, so it can't shadow a healthy `.git` repo in the
/// same or a higher directory (M19). Symmetric with [`is_git_marker`]: both require a
/// *valid* marker, not mere existence.
fn is_jj_marker(path: &Path) -> bool {
    path.is_dir() && path.join("repo").exists()
}

/// Whether `path` (a candidate `.git`) is a real git repository marker — a `.git`
/// **directory**, or a **gitlink file** (a linked worktree / submodule) whose
/// content starts with `gitdir:`. A stray/garbage file merely *named* `.git` is
/// rejected, so it can't shadow a real repository higher up the tree, and a binary
/// or unreadable file is rejected too (the read fails → `false`). Symmetric with
/// [`is_jj_marker`]: both require a *valid* marker, not mere existence.
fn is_git_marker(path: &Path) -> bool {
    use std::io::Read;
    match std::fs::metadata(path) {
        Ok(meta) if meta.is_dir() => true,
        Ok(meta) if meta.is_file() => {
            // A gitlink file is tiny (`gitdir: <path>\n`), so read only a small
            // prefix: `discover` walks *up to the filesystem root*, so a huge/garbage
            // file merely named `.git` in an ancestor we don't own must not force an
            // unbounded read. `read_to_end` loops over short reads (unlike a single
            // `read`, which the `Read` contract lets return fewer bytes), and
            // `from_utf8_lossy` tolerates a binary file or a multibyte char split at
            // the cap — the `gitdir:` marker is ASCII and within the first bytes.
            let Ok(file) = std::fs::File::open(path) else {
                return false;
            };
            let mut buf = Vec::new();
            let _ = file.take(32).read_to_end(&mut buf);
            String::from_utf8_lossy(&buf)
                .trim_start()
                .starts_with("gitdir:")
        }
        _ => false,
    }
}

/// Whether `dir` (the candidate itself, not a `.git` beneath it) is a **bare**
/// git repository — created with `git init --bare` (or an equivalent bare
/// clone): `HEAD`/`config`/`objects`/`refs` sit directly in `dir`, with no
/// `.git` subdirectory. Requires all four markers together (`HEAD` a file,
/// `config` a file, `objects`/`refs` directories) so a directory that merely
/// happens to contain one or two similarly-named entries isn't misdetected —
/// symmetric with [`is_jj_marker`]/[`is_git_marker`]: a *valid* marker, not
/// mere partial name overlap. Used to give bare repositories their own
/// [`Error::BareRepository`](crate::Error::BareRepository) instead of the
/// generic [`Error::NotARepository`](crate::Error::NotARepository) (issue #6).
fn is_bare_git_repo_marker(dir: &Path) -> bool {
    dir.join("HEAD").is_file()
        && dir.join("config").is_file()
        && dir.join("objects").is_dir()
        && dir.join("refs").is_dir()
}

/// Walk up from `start` to the filesystem root looking for a **bare** git
/// repository marker (see [`is_bare_git_repo_marker`]). Only called after
/// [`discover`] has already walked the same chain and found no `.jj`/`.git`, so
/// any hit here is unambiguous — no real (non-bare) repository intervenes
/// between `start` and the bare repository root.
fn find_bare_git_repo(start: &Path) -> Option<PathBuf> {
    let mut current = Some(start);
    while let Some(dir) = current {
        if is_bare_git_repo_marker(dir) {
            return Some(dir.to_path_buf());
        }
        current = dir.parent();
    }
    None
}

/// The per-tool client behind a [`Repo`]. Shared via `Arc` so [`Repo::at`] can
/// re-anchor the cwd cheaply without rebuilding the client.
enum Backend<R: ProcessRunner> {
    Git(Arc<Git<R>>),
    Jj(Arc<Jj<R>>),
}

impl<R: ProcessRunner> Debug for Backend<R> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let variant_name = match self {
            Backend::Git(_) => "Git",
            Backend::Jj(_) => "Jj",
        };
        f.debug_tuple(variant_name).finish_non_exhaustive()
    }
}

impl<R: ProcessRunner> Backend<R> {
    fn shared(&self) -> Self {
        match self {
            Backend::Git(g) => Backend::Git(Arc::clone(g)),
            Backend::Jj(j) => Backend::Jj(Arc::clone(j)),
        }
    }
}

/// A cwd-bound, backend-agnostic VCS handle. Operations run against the bound
/// directory ([`cwd`](Repo::cwd)); use [`at`](Repo::at) to get a sibling handle
/// bound elsewhere.
pub struct Repo<R: ProcessRunner = JobRunner> {
    root: PathBuf,
    cwd: PathBuf,
    backend: Backend<R>,
}
// need a manual impl to avoid `R: Debug` bound.
impl<R: ProcessRunner> Debug for Repo<R> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let Repo { root, cwd, backend } = self;
        f.debug_struct("Repo")
            .field("root", root)
            .field("cwd", cwd)
            .field("backend", backend)
            .finish()
    }
}

impl Repo<JobRunner> {
    /// Discover the repository at or above `dir` and open a handle bound to
    /// `dir`, using the real job-backed runner. Walks up from `dir` toward the
    /// filesystem root — see [`discover`] — so it finds a repository whose root
    /// is `dir` itself or any ancestor. Errors with [`Error::NotARepository`]
    /// when no `.git`/`.jj` is found, or with [`Error::BareRepository`] when the
    /// walk instead reaches a **bare** git repository (`git init --bare`) before
    /// any `.jj`/`.git` — a bare repo has no working tree for this facade to
    /// drive (issue #6).
    ///
    /// For a strict check of exactly `dir` — no walking up — see [`Repo::open`].
    pub fn discover(dir: impl AsRef<Path>) -> Result<Self> {
        // Absolutise first: `discover` walks parents, and a relative path like "."
        // has no real ancestor chain (`Path::new(".").parent()` is `""`, then
        // `None`), so a relative input would never find a repo above the cwd.
        let dir = std::path::absolute(dir.as_ref())?;
        let located = match discover(&dir) {
            Some(located) => located,
            None => {
                // `discover` already walked the full chain and found nothing — a
                // second, cheap walk tells us whether the reason is "no
                // repository at all" or "a bare git repository sits in the
                // way", so the caller gets the more precise error.
                return Err(match find_bare_git_repo(&dir) {
                    Some(bare_root) => Error::BareRepository(bare_root),
                    None => Error::NotARepository(dir),
                });
            }
        };
        let backend = match located.kind {
            BackendKind::Git => Backend::Git(Arc::new(Git::new())),
            BackendKind::Jj => Backend::Jj(Arc::new(Jj::new())),
        };
        Ok(Repo {
            root: located.root,
            cwd: dir,
            backend,
        })
    }

    /// Open the repository at **exactly** `dir` — unlike [`Repo::discover`],
    /// this does **not** walk up through parent directories: `dir` itself must
    /// hold the `.jj`/`.git` marker (a `.jj` directory with a `repo` entry, or a
    /// `.git` directory / gitlink file — the same validated markers [`discover`]
    /// uses), or this errors with
    /// [`Error::NotARepository(dir)`](Error::NotARepository)
    /// even if a repository exists somewhere above `dir`. Mirrors the
    /// discover-vs-open split in gitoxide (`gix::discover` vs `gix::open`) and
    /// libgit2 (`git_repository_discover` vs `git_repository_open`) — see
    /// issue #8.
    ///
    /// If `dir` itself is a **bare** git repository (`git init --bare`: no
    /// `.git` subdirectory, just `HEAD`/`config`/`objects`/`refs` directly in
    /// `dir` — see `is_bare_git_repo_marker`), this errors with
    /// [`Error::BareRepository(dir)`](Error::BareRepository) instead of the
    /// generic `NotARepository`, matching what [`Repo::discover`] reports for
    /// the same directory (issue #6) — `open` still never walks up, so this
    /// only applies to `dir` itself, not an ancestor.
    pub fn open(dir: impl AsRef<Path>) -> Result<Self> {
        // Absolutise so the bound `cwd`/`root` are consistent with `discover`'s
        // and so a relative "." names the actual directory, not an empty path.
        let dir = std::path::absolute(dir.as_ref())?;
        let kind = if is_jj_marker(&dir.join(".jj")) {
            BackendKind::Jj
        } else if is_git_marker(&dir.join(".git")) {
            BackendKind::Git
        } else if is_bare_git_repo_marker(&dir) {
            return Err(Error::BareRepository(dir));
        } else {
            return Err(Error::NotARepository(dir));
        };
        let backend = match kind {
            BackendKind::Git => Backend::Git(Arc::new(Git::new())),
            BackendKind::Jj => Backend::Jj(Arc::new(Jj::new())),
        };
        Ok(Repo {
            root: dir.clone(),
            cwd: dir,
            backend,
        })
    }
}

impl<R: ProcessRunner> Repo<R> {
    /// Build a git-backed handle from an explicit client — for a custom runner
    /// (e.g. a test seam) or a pre-configured [`Git`].
    pub fn from_git(root: impl Into<PathBuf>, cwd: impl Into<PathBuf>, client: Git<R>) -> Self {
        Repo {
            root: root.into(),
            cwd: cwd.into(),
            backend: Backend::Git(Arc::new(client)),
        }
    }

    /// Build a jj-backed handle from an explicit client.
    pub fn from_jj(root: impl Into<PathBuf>, cwd: impl Into<PathBuf>, client: Jj<R>) -> Self {
        Repo {
            root: root.into(),
            cwd: cwd.into(),
            backend: Backend::Jj(Arc::new(client)),
        }
    }

    /// Discover the repository at or above `dir` — exactly as [`Repo::discover`] —
    /// but build the handle from a **caller-injected** client instead of the plain
    /// default one. `dir` is absolutised and walked toward the filesystem root (see
    /// [`discover`]), then the factory for the **detected** backend is invoked:
    /// `git` for a `.git` repository, `jj` for a `.jj` one. Only the matching
    /// closure runs — the other is never called, so neither client is built
    /// speculatively.
    ///
    /// This is the injected-client counterpart of [`Repo::discover`]: reach for it
    /// when the handle needs a pre-configured client — a hardened [`Git`], a
    /// per-command timeout, a custom [`ProcessRunner`] test seam — rather than the
    /// default `Git::new` / `Jj::new` that [`Repo::discover`] uses. It shares
    /// [`Repo::discover`]'s exact detection and error classification, so a consumer
    /// no longer has to re-implement the [`discover`] walk, match [`BackendKind`],
    /// and assemble [`from_git`](Repo::from_git) / [`from_jj`](Repo::from_jj) by
    /// hand just to inject clients.
    ///
    /// Because the match on [`BackendKind`] lives **here**, inside the crate that
    /// declares the enum `#[non_exhaustive]`, adding a future backend variant is a
    /// change to this one method — callers need no wildcard/catch-all arm of their
    /// own to keep in sync.
    ///
    /// # Errors
    /// The same as [`Repo::discover`]: [`Error::NotARepository`] when no `.git`/`.jj`
    /// marker is found from `dir` up to the filesystem root, or
    /// [`Error::BareRepository`] when the walk instead reaches a **bare** git
    /// repository (`git init --bare`) first. It reuses the very same private
    /// `find_bare_git_repo` diagnostic path as [`Repo::discover`], so the bare-repo
    /// distinction is reported identically no matter which entry point opened the
    /// repository.
    ///
    /// ```no_run
    /// # use std::time::Duration;
    /// # use vcs_core::{Repo, vcs_git::Git, vcs_jj::Jj};
    /// # fn f() -> vcs_core::Result<()> {
    /// // A hardened git client / timeout-bound jj client, injected lazily — only
    /// // the one matching the detected backend is ever built.
    /// let repo = Repo::discover_with(
    ///     ".",
    ///     || Git::hardened().default_timeout(Duration::from_secs(120)),
    ///     || Jj::new().default_timeout(Duration::from_secs(120)),
    /// )?;
    /// # let _ = repo;
    /// # Ok(()) }
    /// ```
    pub fn discover_with<G, J>(dir: impl AsRef<Path>, git: G, jj: J) -> Result<Self>
    where
        G: FnOnce() -> Git<R>,
        J: FnOnce() -> Jj<R>,
    {
        // Absolutise first, for the same reason `Repo::discover` does: `discover`
        // walks parents, and a relative path like "." has no real ancestor chain.
        let dir = std::path::absolute(dir.as_ref())?;
        let located = match discover(&dir) {
            Some(located) => located,
            None => {
                // Identical bare-vs-nothing diagnostic to `Repo::discover`: the
                // second, cheap walk distinguishes "no repository at all" from "a
                // bare git repository sits in the way", so the injected-client path
                // reports the precise error rather than a stringly-typed blob.
                return Err(match find_bare_git_repo(&dir) {
                    Some(bare_root) => Error::BareRepository(bare_root),
                    None => Error::NotARepository(dir),
                });
            }
        };
        // The match on `BackendKind` lives inside `vcs-core`, where the enum is
        // declared, so a new `#[non_exhaustive]` variant is handled here (a
        // compile error to add without a client) rather than by a caller's
        // catch-all arm.
        Ok(match located.kind {
            BackendKind::Git => Repo::from_git(located.root, dir, git()),
            BackendKind::Jj => Repo::from_jj(located.root, dir, jj()),
        })
    }

    /// Which backend drives this handle.
    pub fn kind(&self) -> BackendKind {
        match &self.backend {
            Backend::Git(_) => BackendKind::Git,
            Backend::Jj(_) => BackendKind::Jj,
        }
    }

    /// The repository root detected at open time.
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// The directory operations run against.
    pub fn cwd(&self) -> &Path {
        &self.cwd
    }

    /// A sibling handle bound to `dir`, sharing this handle's client and root.
    pub fn at(&self, dir: impl Into<PathBuf>) -> Self {
        Repo {
            root: self.root.clone(),
            cwd: dir.into(),
            backend: self.backend.shared(),
        }
    }

    /// The underlying [`Git`] client, or `None` when jj-backed — an escape hatch
    /// to git-only operations not on the common surface.
    pub fn git(&self) -> Option<&Git<R>> {
        match &self.backend {
            Backend::Git(g) => Some(g.as_ref()),
            Backend::Jj(_) => None,
        }
    }

    /// The underlying [`Jj`] client, or `None` when git-backed.
    pub fn jj(&self) -> Option<&Jj<R>> {
        match &self.backend {
            Backend::Jj(j) => Some(j.as_ref()),
            Backend::Git(_) => None,
        }
    }

    /// The git client bound to this handle's [`cwd`](Repo::cwd) — a [`GitAt`] whose
    /// methods omit the `dir` argument — or `None` when jj-backed. The dir-free
    /// counterpart of [`git`](Repo::git): `repo.git_at()?.merge_continue().await?`.
    ///
    /// The returned view borrows `self`. To work in another worktree, **bind the
    /// re-anchored handle first** (the view can't outlive a temporary
    /// [`at`](Repo::at)):
    ///
    /// ```no_run
    /// # async fn f(repo: vcs_core::Repo, wt: &std::path::Path) -> vcs_core::Result<()> {
    /// let wt = repo.at(wt);          // owns the re-anchored handle
    /// let git = wt.git_at().unwrap();
    /// git.fetch().await?;
    /// # Ok(()) }
    /// ```
    pub fn git_at(&self) -> Option<GitAt<'_, R>> {
        match &self.backend {
            Backend::Git(g) => Some(g.at(&self.cwd)),
            Backend::Jj(_) => None,
        }
    }

    /// The jj client bound to this handle's [`cwd`](Repo::cwd) — a [`JjAt`] whose
    /// methods omit the `dir` argument — or `None` when git-backed. The dir-free
    /// counterpart of [`jj`](Repo::jj). For another workspace, bind the re-anchored
    /// handle first (`let ws = repo.at(path); ws.jj_at()…`) — see [`git_at`](Repo::git_at).
    pub fn jj_at(&self) -> Option<JjAt<'_, R>> {
        match &self.backend {
            Backend::Jj(j) => Some(j.at(&self.cwd)),
            Backend::Git(_) => None,
        }
    }

    /// The current branch (git) or bookmark (jj). On jj this is the nearest
    /// bookmark reachable from the working copy (`heads(::@ & bookmarks())`),
    /// so it stays set across a `jj describe`/`jj new`/`jj commit` — which leave
    /// the bookmark on the described parent while the new change carries none —
    /// matching git's "still on my branch" reporting. When several bookmarks are
    /// equally near `@`, the lexicographically-smallest name is returned
    /// (deterministic). `None` only when detached / no bookmark on or above `@`.
    pub async fn current_branch(&self) -> Result<Option<String>> {
        match &self.backend {
            Backend::Git(g) => git_backend::current_branch(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::current_branch(j, &self.cwd).await,
        }
    }

    /// The trunk branch/bookmark. Resolution order: the backend's own notion
    /// (git's `origin/HEAD`, jj's `trunk()` revset), then a fallback to a local
    /// `main`, then `master`; `None` when none of those resolve.
    pub async fn trunk(&self) -> Result<Option<String>> {
        let native = match &self.backend {
            Backend::Git(g) => git_backend::trunk(g, &self.cwd).await?,
            Backend::Jj(j) => jj_backend::trunk(j, &self.cwd).await?,
        };
        if native.is_some() {
            return Ok(native);
        }
        for candidate in ["main", "master"] {
            if self.branch_exists(candidate).await? {
                return Ok(Some(candidate.to_string()));
            }
        }
        Ok(None)
    }

    /// Local branch (git) / bookmark (jj) names.
    ///
    /// Backend divergence: on **jj**, a bookmark deleted locally but still **tracked**
    /// on a remote renders as a *tombstone* row (jj keeps it internally so the
    /// deletion can be propagated) until the deletion is pushed. This tombstone is
    /// filtered out here — a name a `delete_branch` just removed does **not**
    /// reappear in the result — while a *conflicted* bookmark (present, but with no
    /// single normal target) is still reported as existing, since dropping it too
    /// would hide a real, live bookmark that merely has a conflict (T-041; was M21).
    pub async fn local_branches(&self) -> Result<Vec<String>> {
        match &self.backend {
            Backend::Git(g) => git_backend::local_branches(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::local_branches(j, &self.cwd).await,
        }
    }

    /// A **read-only** [`local_branches`](Repo::local_branches): the same result,
    /// but on **jj** it passes `--ignore-working-copy`, so listing the bookmarks
    /// records no jj operation and never moves `@`. On **git** it is exactly
    /// [`local_branches`](Repo::local_branches) — git's branch listing records no
    /// operation and moves no ref, so there is nothing to make read-only.
    ///
    /// Use it (with [`snapshot_readonly`](Repo::snapshot_readonly)) from an
    /// *observer* — a watcher or a prompt refresh — that must not perturb the
    /// state it reads. See [`snapshot_readonly`](Repo::snapshot_readonly) for the
    /// jj working-copy trade-off this shares.
    pub async fn local_branches_readonly(&self) -> Result<Vec<String>> {
        match &self.backend {
            Backend::Git(g) => git_backend::local_branches(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::local_branches_readonly(j, &self.cwd).await,
        }
    }

    /// Whether a local branch/bookmark named `name` exists. See
    /// [`local_branches`](Repo::local_branches) for the jj deleted-but-tracked
    /// *tombstone* divergence: on jj, a bookmark just removed by `delete_branch` but
    /// still tracked on a remote does **not** read as existing here (the tombstone is
    /// filtered), while a *conflicted* bookmark still does.
    pub async fn branch_exists(&self, name: &str) -> Result<bool> {
        match &self.backend {
            Backend::Git(g) => git_backend::branch_exists(g, &self.cwd, name).await,
            Backend::Jj(j) => jj_backend::branch_exists(j, &self.cwd, name).await,
        }
    }

    /// Whether the working copy has uncommitted changes (git: a non-empty
    /// `status`; jj: a non-empty working-copy change `@`).
    pub async fn has_uncommitted_changes(&self) -> Result<bool> {
        match &self.backend {
            Backend::Git(g) => git_backend::has_uncommitted_changes(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::has_uncommitted_changes(j, &self.cwd).await,
        }
    }

    /// Whether the working copy has uncommitted changes to *tracked* files.
    ///
    /// Backend nuance: git ignores untracked files here
    /// (`status --untracked-files=no`); jj auto-tracks new files, so there is no
    /// untracked concept and this equals
    /// [`has_uncommitted_changes`](Self::has_uncommitted_changes).
    pub async fn has_tracked_changes(&self) -> Result<bool> {
        match &self.backend {
            Backend::Git(g) => git_backend::has_tracked_changes(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::has_uncommitted_changes(j, &self.cwd).await,
        }
    }

    /// Paths with unresolved merge conflicts in the working copy, repo-relative
    /// with `/` separators (git `diff --diff-filter=U` / jj `resolve --list -r @`).
    /// Empty when there are none. Each path is a [`PathBuf`] carried losslessly from
    /// the backend, so a non-UTF-8 conflicted filename (legal on Unix) is not
    /// corrupted to `U+FFFD`.
    pub async fn conflicted_files(&self) -> Result<Vec<PathBuf>> {
        match &self.backend {
            Backend::Git(g) => git_backend::conflicted_files(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::conflicted_files(j, &self.cwd).await,
        }
    }

    /// Create a local branch (git) / bookmark (jj) at the current head, without
    /// switching the working copy (git `branch <name>`; jj `bookmark create <name>
    /// -r @`).
    pub async fn create_branch(&self, name: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::create_branch(g, &self.cwd, name).await,
            Backend::Jj(j) => jj_backend::create_branch(j, &self.cwd, name).await,
        }
    }

    /// Delete a local branch (git) / bookmark (jj). The [`BranchDelete`] spec's
    /// [`force`](BranchDelete::force) applies to git only (`branch -D` vs `-d`); jj
    /// has no force and ignores it.
    pub async fn delete_branch(&self, spec: BranchDelete) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => {
                git_backend::delete_branch(g, &self.cwd, &spec.name, spec.force).await
            }
            Backend::Jj(j) => jj_backend::delete_branch(j, &self.cwd, &spec.name).await,
        }
    }

    /// Rename a local branch (git) / bookmark (jj).
    pub async fn rename_branch(&self, old: &str, new: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::rename_branch(g, &self.cwd, old, new).await,
            Backend::Jj(j) => jj_backend::rename_branch(j, &self.cwd, old, new).await,
        }
    }

    /// The working-copy changes (git `status` / jj `diff -r @ --summary`).
    pub async fn changed_files(&self) -> Result<Vec<FileChange>> {
        match &self.backend {
            Backend::Git(g) => git_backend::changed_files(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::changed_files(j, &self.cwd).await,
        }
    }

    /// Aggregate insertion/deletion counts for the working copy.
    ///
    /// Backend nuance: git counts the working tree against `HEAD` (`git diff`,
    /// which **excludes untracked files**), while jj counts the `@` change against
    /// its parent (which **includes** newly-added files). So on git a brand-new
    /// file shows in [`changed_files`](Self::changed_files) but not here, whereas
    /// on jj it shows in both. On an unborn git repo (no commits yet) the count is
    /// taken against the empty tree, so a pre-first-commit working tree stats
    /// instead of erroring.
    ///
    /// jj snapshot caveat: like every other jj-backed read here (`status`,
    /// `changed_files`, `snapshot`, `log`, …), this runs a plain `jj diff` — jj's
    /// default mode, which first **snapshots the working copy** (imports any bare
    /// filesystem edit into a fresh `@`) and **records a new operation** in the op
    /// log. That is a bookkeeping side effect, not a content mutation (no tracked
    /// file/ref changes, and it is transparently undoable via `jj op undo`), but it
    /// is not a *no-op* read either. A genuinely non-recording read exists
    /// (`--ignore-working-copy`, wired up as `vcs_jj`'s `_ignoring_working_copy`
    /// client methods and this crate's [`snapshot_readonly`](Self::snapshot_readonly)
    /// / `local_branches_readonly`, built for [`vcs-watch`](https://docs.rs/vcs-watch)'s
    /// polling loop) but is deliberately **not** used here: it reports the state of
    /// the *last recorded* operation rather than the live working tree, so a bare
    /// edit no jj command has yet snapshotted would be silently invisible — wrong
    /// for a method whose whole purpose is reporting the *current* working-copy
    /// state.
    pub async fn diff_stat(&self) -> Result<DiffStat> {
        match &self.backend {
            Backend::Git(g) => git_backend::diff_stat(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::diff_stat(j, &self.cwd).await,
        }
    }

    /// The full parsed diff for the working copy — the same scope as
    /// [`diff_stat`](Self::diff_stat) (git: working tree vs `HEAD`, using the
    /// empty-tree oid on an unborn repo; jj: `@` vs its parent), but returning the
    /// per-file hunks/lines ([`FileDiff`]) rather than just the aggregate counts.
    /// Dispatches to the already-existing `GitApi::diff`/`JjApi::diff` with
    /// [`vcs_git::DiffSpec::WorkingTree`], so it inherits the backend client's
    /// [`OutputBudget`] — an over-budget diff errors with
    /// [`OutputTooLarge`](processkit::Error::OutputTooLarge) rather than
    /// buffering (or silently truncating) an unbounded diff.
    ///
    /// Backend nuance (same as `diff_stat`): git diffs the working tree against
    /// `HEAD`, which **excludes untracked files**; jj diffs `@` against its
    /// parent, which **includes** newly-added files. So a brand-new file shows in
    /// [`changed_files`](Self::changed_files) but *not* here on git, whereas on jj
    /// it shows in both — don't assume the two backends return the same file set.
    ///
    /// Cross-backend revision-range diffs are deliberately **not** exposed here
    /// (see the crate docs' "what's deliberately not unified" — range diffs stay
    /// on the raw [`git`](Self::git)/[`jj`](Self::jj) client, via `GitApi::diff`/
    /// `JjApi::diff` with `DiffSpec::Rev`).
    ///
    /// Same jj snapshot caveat as [`diff_stat`](Self::diff_stat): on jj this is a
    /// plain `jj diff -r @ --git`, jj's default working-copy-snapshotting mode
    /// (records an operation in the op log — a reversible bookkeeping side effect,
    /// not a tracked-content mutation), deliberately not the non-recording
    /// `--ignore-working-copy` mode, which would make this method blind to any not-
    /// yet-snapshotted edit.
    pub async fn diff(&self) -> Result<Vec<FileDiff>> {
        match &self.backend {
            Backend::Git(g) => git_backend::diff(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::diff(j, &self.cwd).await,
        }
    }

    /// Recent history: up to `max` commits reachable from `revspec_or_revset`
    /// (git revspec / jj revset), most-recent-first (git `log`'s default order /
    /// jj `log`'s topological order).
    ///
    /// Backend nuance: [`Commit::author`]/[`Commit::date`] are `Some` only on
    /// git — jj's typed log doesn't currently surface authorship or a
    /// timestamp, so they're `None` there rather than guessed (see the
    /// [`Commit`] type docs).
    pub async fn log(&self, revspec_or_revset: &str, max: usize) -> Result<Vec<Commit>> {
        match &self.backend {
            Backend::Git(g) => git_backend::log(g, &self.cwd, revspec_or_revset, max).await,
            Backend::Jj(j) => jj_backend::log(j, &self.cwd, revspec_or_revset, max).await,
        }
    }

    /// Per-line attribution for `path`, optionally at `rev` (a git revspec / jj
    /// revset). `None` reads the current git `HEAD` / jj `@`; a supplied revision is
    /// passed to the selected backend without facade-level interpretation.
    ///
    /// Backend nuance: [`AnnotationLine::author`]/[`AnnotationLine::date`] are
    /// `Some` only on git — jj's typed annotation exposes only the introducing
    /// change and line content, so the facade leaves them `None` rather than guessing
    /// (see [`AnnotationLine`]).
    pub async fn annotate(&self, path: &str, rev: Option<&str>) -> Result<Vec<AnnotationLine>> {
        match &self.backend {
            Backend::Git(g) => git_backend::annotate(g, &self.cwd, path, rev).await,
            Backend::Jj(j) => jj_backend::annotate(j, &self.cwd, path, rev).await,
        }
    }

    /// The content of `path` as it exists at `rev` (git revspec / jj revset), e.g.
    /// `HEAD:src/lib.rs` on git or `@-` + a fileset on jj — both normalise
    /// backslash path separators and return the file's bytes verbatim (including
    /// any trailing newline).
    pub async fn show_file(&self, rev: &str, path: &str) -> Result<String> {
        match &self.backend {
            Backend::Git(g) => git_backend::show_file(g, &self.cwd, rev, path).await,
            Backend::Jj(j) => jj_backend::show_file(j, &self.cwd, rev, path).await,
        }
    }

    /// [`show_file`](Repo::show_file) with an explicit per-call [`OutputBudget`],
    /// instead of the budget the backend client was built with
    /// ([`default_output_budget`](vcs_git::Git::default_output_budget), inherited
    /// through [`from_git`](Repo::from_git)/[`from_jj`](Repo::from_jj)). Reads the
    /// blob under `budget`: past the ceiling it errors with an
    /// [`OutputTooLarge`](processkit::Error::OutputTooLarge)-carrying
    /// [`Error::Vcs`] (actual and allowed sizes) rather than buffering an unbounded
    /// file — use it to read a legitimately large file
    /// ([`OutputBudget::unlimited`], or a higher cap) or to tighten the cap for one
    /// call. A truncated blob is never returned as if complete.
    pub async fn show_file_within(
        &self,
        rev: &str,
        path: &str,
        budget: OutputBudget,
    ) -> Result<String> {
        match &self.backend {
            Backend::Git(g) => git_backend::show_file_within(g, &self.cwd, rev, path, budget).await,
            Backend::Jj(j) => jj_backend::show_file_within(j, &self.cwd, rev, path, budget).await,
        }
    }

    /// A batched [`RepoSnapshot`] of the common repo state — branch, upstream,
    /// ahead/behind, dirtiness, change count, and operation state — in a **small
    /// fixed** number of spawns instead of a call per field (git: `status
    /// --porcelain=v2 --branch` + the in-progress probe; jj: a `log -r @`
    /// template for head/empty/conflict, a `reachable_bookmarks` query for
    /// `branch`, and a change count only when dirty). Built for prompt/status-bar/
    /// TUI refreshes. Note the asymmetry: [`tracking`](RepoSnapshot::tracking)
    /// (the upstream ref + ahead/behind) is always `None` on jj, which has no
    /// git-style upstream tracking.
    pub async fn snapshot(&self) -> Result<RepoSnapshot> {
        match &self.backend {
            Backend::Git(g) => git_backend::snapshot(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::snapshot(j, &self.cwd).await,
        }
    }

    /// A **read-only** [`snapshot`](Repo::snapshot): the same [`RepoSnapshot`],
    /// but on **jj** it never snapshots the working copy — every underlying query
    /// passes `--ignore-working-copy`, so the batched read records **no** jj
    /// operation and never moves `@`. On **git** it is exactly
    /// [`snapshot`](Repo::snapshot) (git's status query records no operation and
    /// moves no ref).
    ///
    /// Use it for an *observer* — a repository watcher, a prompt/status-bar
    /// refresh — that must not perturb the state it reports: an ordinary jj query
    /// snapshots the working copy as a side effect (taking the lock, recording an
    /// operation, possibly moving `@`), so the observer would otherwise *mutate*
    /// the repo it merely means to read.
    ///
    /// **jj trade-off:** because the working copy isn't snapshotted, a bare
    /// working-tree edit that no jj command has recorded yet is **not** reflected
    /// — [`dirty`](RepoSnapshot::dirty)/[`head`](RepoSnapshot::head) are as of the
    /// last recorded operation. To observe such unsnapshotted edits, accept the
    /// mutation and call [`snapshot`](Repo::snapshot).
    pub async fn snapshot_readonly(&self) -> Result<RepoSnapshot> {
        match &self.backend {
            Backend::Git(g) => git_backend::snapshot(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::snapshot_readonly(j, &self.cwd).await,
        }
    }

    /// Commit exactly `paths` with `message` (git `commit --only`, jj
    /// `commit <filesets>`). Paths are repo-relative. `paths` must be non-empty:
    /// an empty set is refused up front, because the backends would diverge
    /// dangerously — git errors out, while jj's `commit` with no filesets would
    /// silently commit the **entire** working copy.
    ///
    /// Takes [`PathBuf`]s so a path obtained from [`changed_files`](Self::changed_files)
    /// / [`conflicted_files`](Self::conflicted_files) round-trips **losslessly** — on
    /// git a non-UTF-8 path (legal on Unix) reaches the commit unchanged via the
    /// NUL-safe pathspec transport; on jj the fileset language is text, so jj's own
    /// (non-UTF-8-incapable) fileset handling applies.
    pub async fn commit_paths(&self, paths: &[PathBuf], message: &str) -> Result<()> {
        if paths.is_empty() {
            return Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "commit_paths requires at least one path: an empty set would error \
                 on git but commit the entire working copy on jj",
            )));
        }
        match &self.backend {
            Backend::Git(g) => git_backend::commit_paths(g, &self.cwd, paths, message).await,
            Backend::Jj(j) => jj_backend::commit_paths(j, &self.cwd, paths, message).await,
        }
    }

    /// Fetch from the default remote (git `fetch` / jj `git fetch`).
    pub async fn fetch(&self) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::fetch(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::fetch(j, &self.cwd).await,
        }
    }

    /// Fetch from a *named* remote (git `fetch <remote>` / jj
    /// `git fetch --remote <remote>`). Transient network failures are retried by
    /// the underlying client.
    pub async fn fetch_from(&self, remote: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::fetch_from(g, &self.cwd, remote).await,
            Backend::Jj(j) => jj_backend::fetch_from(j, &self.cwd, remote).await,
        }
    }

    /// Fetch a single branch/bookmark from `origin` into its remote-tracking ref
    /// (git `fetch_branch` / jj `git fetch -b`). Transient network failures
    /// are retried by the underlying client.
    pub async fn fetch_branch(&self, branch: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::fetch_branch(g, &self.cwd, branch).await,
            Backend::Jj(j) => jj_backend::fetch_branch(j, &self.cwd, branch).await,
        }
    }

    /// Push `branch` to `origin` (git `push -u origin <branch>` / jj
    /// `git push -b <branch>`).
    ///
    /// The branch (jj: bookmark) must already exist locally. The two backends
    /// honestly differ in what "push" means: git pushes the *ref* and records
    /// the upstream (`-u`; idempotent on repeat pushes), while jj pushes the
    /// *bookmark's state* — including deleting the remote branch if the
    /// bookmark was deleted locally. Renamed refspecs (`local:remote`) and
    /// non-`origin` remotes are git-only concepts; use the
    /// [`git()`](Repo::git) escape hatch ([`vcs_git::GitPush`]) for those.
    pub async fn push(&self, branch: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::push(g, &self.cwd, branch).await,
            Backend::Jj(j) => jj_backend::push(j, &self.cwd, branch).await,
        }
    }

    /// Switch the working copy to `reference` (git `checkout` / jj `edit`).
    ///
    /// ⚠ **Backend divergence — this is not "detach and build on top" on jj.** On
    /// **git**, a subsequent commit *appends* on top of `reference` (its tip is
    /// untouched). On **jj**, `checkout` maps to `jj edit`, which makes `reference`'s
    /// commit *itself* the working-copy change — so a following
    /// [`commit_paths`](Repo::commit_paths) (or any edit) **rewrites that commit in
    /// place** (a new change-id, a replaced
    /// description), silently amending a possibly-already-pushed commit rather than
    /// adding a new one.
    ///
    /// So backend-agnostic "start fresh work on top of `main`" code must **not** rely
    /// on `checkout` alone. If you want git-like append-on-top semantics on both
    /// backends, use [`new_child`](Repo::new_child), which maps to `jj new
    /// <reference>` on jj and to `checkout <reference>` on git.
    pub async fn checkout(&self, reference: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::checkout(g, &self.cwd, reference).await,
            Backend::Jj(j) => jj_backend::checkout(j, &self.cwd, reference).await,
        }
    }

    /// Start new work on top of `reference` without modifying it.
    ///
    /// On git this checks out `reference`; the next commit naturally appends on top.
    /// On jj this runs `jj new <reference>`, creating an undescribed child change.
    pub async fn new_child(&self, reference: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::new_child(g, &self.cwd, reference).await,
            Backend::Jj(j) => jj_backend::new_child(j, &self.cwd, reference).await,
        }
    }

    /// Rebase the current line onto `onto`. The two backends **diverge** on
    /// non-linear layouts, so this is a documented least-common-denominator:
    /// - **git** (`rebase <onto>` = `merge-base(HEAD,onto)..HEAD`) moves only
    ///   `HEAD`'s own ancestor line; commits stacked on `HEAD` stay put.
    /// - **jj** (`rebase -d <onto>` = the default `-b @` = `(onto..@)::`) moves
    ///   that line *and its whole descendant closure* — anything stacked on `@`,
    ///   and any sibling off an *intermediate* commit of the line, move too.
    ///
    /// They agree on a linear `HEAD`/`@`; on a **stacked or intermediate-fork**
    /// layout jj moves strictly more. A sibling that shares only the fork point is
    /// moved by neither. `onto` is a branch/bookmark name or revision the backend
    /// understands.
    pub async fn rebase(&self, onto: &str) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => git_backend::rebase(g, &self.cwd, onto).await,
            Backend::Jj(j) => jj_backend::rebase(j, &self.cwd, onto).await,
        }
    }

    /// Probe whether merging `source` into the current work would conflict,
    /// **without leaving any trace**: the probe is rolled back before returning
    /// (git: `merge --no-commit --no-ff` then `merge --abort`; jj: a merge
    /// change probed and undone via `op restore`).
    ///
    /// Preconditions/behaviour:
    /// - git: requires a clean-enough working tree — a dirty-tree refusal
    ///   propagates as a plain error, not as [`MergeProbe::Conflicts`].
    /// - A failing rollback **propagates as an error** rather than returning a
    ///   result that misdescribes the on-disk state.
    /// - **Cancellation-safe rollback:** on **both** backends the *whole* rollback
    ///   path — the decision of whether to roll back **and** the command that
    ///   performs it — runs on a fresh cancellation context with its own bounded
    ///   deadline (git: `Git::is_merge_in_progress_detached` + `merge --abort` via
    ///   `Git::merge_abort_detached`; jj: the op-log probe + `op restore` via
    ///   `Jj::rollback_to`), so a `default_cancel_on` token (the `cancellation`
    ///   feature) that fires during the probe no longer cancels the rollback too —
    ///   not even by cancelling the "is a trial merge still staged?" check before
    ///   the abort is reached. The trial merge is still undone rather than left
    ///   staged, closing the gap where a cancelled probe abandoned it on git. (A
    ///   rollback that fails for another reason still propagates per the bullet
    ///   above.)
    pub async fn try_merge(&self, source: &str) -> Result<MergeProbe> {
        match &self.backend {
            Backend::Git(g) => git_backend::try_merge(g, &self.cwd, source).await,
            Backend::Jj(j) => jj_backend::try_merge(j, &self.cwd, source).await,
        }
    }

    /// Abort the in-progress operation, if any (git: `merge --abort` /
    /// `rebase --abort`; jj: a no-op — there are no paused operations, roll back
    /// explicitly via `Jj::transaction` / `op_restore`). Returns the fresh
    /// *post-call* [`OperationState`]; `Clear` when nothing was (or remains) in
    /// progress.
    pub async fn abort_in_progress(&self) -> Result<OperationState> {
        match &self.backend {
            Backend::Git(g) => git_backend::abort_in_progress(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::abort_in_progress(j, &self.cwd).await,
        }
    }

    /// Continue the in-progress operation after conflict resolution (git:
    /// `commit --no-edit` for a merge, or the matching `--continue` for a rebase /
    /// `am` / cherry-pick / revert; jj: a no-op — resolving the files *is* the
    /// continuation). A `git bisect` has no such step and is refused with
    /// [`Error::Unsupported`] rather than silently reported still in progress.
    /// Returns the fresh *post-call* [`OperationState`]:
    /// - `Conflict` when unresolved paths still block continuing (also on git —
    ///   unlike [`in_progress_state`](Self::in_progress_state), this method
    ///   *does* report `Conflict` for git), or when a continued rebase stops on
    ///   the next patch's conflict.
    /// - `Clear` when the operation finished.
    pub async fn continue_in_progress(&self) -> Result<OperationState> {
        match &self.backend {
            Backend::Git(g) => git_backend::continue_in_progress(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::continue_in_progress(j, &self.cwd).await,
        }
    }

    /// Whether the working copy is mid-operation or conflicted — see
    /// [`OperationState`]. Lets a caller decide between abort/continue without
    /// knowing the backend's model. Note the asymmetry: *this method* reports
    /// `Merge`/`Rebase` (never `Conflict`) on git — a git conflict *is* that
    /// paused state, and the conflict itself surfaces on the failed op via
    /// [`Error::is_merge_conflict`] (or as `Conflict` from
    /// [`continue_in_progress`](Self::continue_in_progress)) — while jj has no
    /// paused op and reports `Conflict` directly.
    pub async fn in_progress_state(&self) -> Result<OperationState> {
        match &self.backend {
            Backend::Git(g) => git_backend::in_progress_state(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::in_progress_state(j, &self.cwd).await,
        }
    }

    /// List attached worktrees (git) / workspaces (jj).
    pub async fn list_worktrees(&self) -> Result<Vec<WorktreeInfo>> {
        match &self.backend {
            Backend::Git(g) => git_backend::list_worktrees(g, &self.cwd).await,
            Backend::Jj(j) => jj_backend::list_worktrees(j, &self.cwd).await,
        }
    }

    /// Create a worktree/workspace at `path` on a **new** `branch` based on
    /// `base`. Always [`CreateOutcome::Plain`]; a copy-on-write strategy stays in
    /// the consumer.
    ///
    /// `branch` must not already exist. The jj path is two steps (`workspace add`
    /// then `bookmark create`) and is not atomic, but a failed bookmark step
    /// **rolls back**: the workspace directory is removed only when `workspace add`
    /// created it (a pre-existing directory the caller already had is left intact),
    /// then the workspace is forgotten. Residue is no longer swallowed: if the
    /// rollback can't remove that directory or can't `forget` the workspace, the call
    /// fails with a composite [`Error::Io`] naming what still needs cleaning up (and
    /// is safe to re-run); a clean rollback instead surfaces the original
    /// bookmark-step error unchanged (its [`Error::Vcs`] classification) — so a failed
    /// call never silently leaks a half-made worktree.
    pub async fn create_worktree(&self, spec: WorktreeCreate) -> Result<CreateOutcome> {
        let WorktreeCreate { path, branch, base } = &spec;
        match &self.backend {
            Backend::Git(g) => git_backend::create_worktree(g, &self.cwd, path, branch, base).await,
            Backend::Jj(j) => jj_backend::create_worktree(j, &self.cwd, path, branch, base).await,
        }
    }

    /// Remove the worktree/workspace at `path`. For jj this resolves the
    /// workspace name by matching `path`, deletes the directory, then forgets it;
    /// a `path` that matches none of the **resolvable** jj workspaces returns
    /// [`Error::WorktreeNotFound`], but when some registered workspace can't be
    /// resolved via `jj workspace root --name` the path's absence is unprovable, so a
    /// distinct diagnosable [`Error::Io`] (naming the unresolved workspaces;
    /// [`is_resource_not_found`](Error::is_resource_not_found) stays `false`) is
    /// returned instead. A directory that can't be deleted is likewise surfaced (an
    /// [`Error::Io`] naming the still-registered workspace, with the `forget` left for
    /// the retry). (For the short-lived, blocking `Drop`-path variant, see
    /// [`cleanup_worktree_blocking`](Self::cleanup_worktree_blocking).)
    ///
    /// The [`WorktreeRemove`] spec's [`force`](WorktreeRemove::force) mirrors git's
    /// `worktree remove`: without it a worktree that still has **uncommitted changes**
    /// is refused (`Err`) rather than deleted, so a stray edit isn't silently lost —
    /// build `WorktreeRemove::new(path).force()` to remove it anyway. On **jj** the
    /// changes are snapshotted into the op log before the check, so a refusal keeps
    /// them recoverable; note that checking spawns a jj command in the target
    /// workspace, so a genuinely stale working copy can surface an error without
    /// `force` (use `.force()` there). The repository's **main** workspace is always
    /// refused (it can't be removed without destroying the repo), regardless of `force`.
    pub async fn remove_worktree(&self, spec: WorktreeRemove) -> Result<()> {
        match &self.backend {
            Backend::Git(g) => {
                git_backend::remove_worktree(g, &self.cwd, &spec.path, spec.force).await
            }
            Backend::Jj(j) => {
                jj_backend::remove_worktree(j, &self.cwd, &spec.path, spec.force).await
            }
        }
    }

    /// **Synchronous** worktree cleanup for a context that cannot `.await` —
    /// chiefly a `Drop` guard. Force-removes the worktree at `path` (git:
    /// `worktree remove --force`; jj: resolve the workspace name by `path`, delete
    /// the directory, then `workspace forget`). Short-lived and shells out directly
    /// (no job-containment), but not error-swallowing: a jj `path` that genuinely
    /// matches no workspace is an `Ok` no-op, yet a probe failure (the `workspace
    /// list`, or a registered workspace that won't resolve) and a `remove_dir_all`
    /// failure are surfaced as `Err` (the `forget` is skipped on a failed removal, so
    /// a surviving directory isn't orphaned). Like the async
    /// [`remove_worktree`](Self::remove_worktree), it **refuses the repository's
    /// main workspace** (whose directory is the main working copy) — deleting it
    /// would wipe the repo — even on this force-by-contract path.
    pub fn cleanup_worktree_blocking(&self, path: &Path) -> Result<()> {
        match &self.backend {
            Backend::Git(_) => vcs_git::blocking::worktree_remove(
                &self.cwd,
                vcs_git::WorktreeRemove::new(path).force(),
            )
            .map_err(Error::Io),
            Backend::Jj(_) => {
                // jj resolves a relative worktree path against the repo dir (its
                // cwd), so resolve it the same way here — the lookup and the dir
                // removal must target the location jj used, not one under the process
                // cwd (which may differ from `self.cwd`).
                let abs_path = self.cwd.join(path);
                // Tell a genuine "no such workspace" (`Ok(None)` → nothing to clean
                // up, a no-op) apart from a probe failure (`Err` → surfaced, not
                // silently treated as a no-op): the blocking resolver no longer folds
                // both into `None`.
                match vcs_jj::blocking::workspace_name_for_path(&self.cwd, &abs_path)
                    .map_err(Error::Io)?
                {
                    Some(name) => {
                        // Same main-workspace guard as the async `remove_worktree`
                        // (jj_backend.rs): never `remove_dir_all` the repository's
                        // main working copy — its directory owns the object store, so
                        // deleting it wipes the whole repo. The `default` name and the
                        // store-owning `.jj/repo` *directory* (a secondary's is a file
                        // pointer) both flag it, so a `jj workspace rename` can't
                        // bypass it. Force is implied on this Drop path, but this guard
                        // is unconditional — a repo-wipe is never the intent.
                        if name == "default" || abs_path.join(".jj").join("repo").is_dir() {
                            return Err(Error::Io(std::io::Error::new(
                                std::io::ErrorKind::InvalidInput,
                                "refusing to remove the repository's main workspace",
                            )));
                        }
                        // Delete the on-disk dir first (jj `forget` leaves it), then
                        // drop jj's record of the workspace. A removal failure is
                        // SURFACED (not swallowed with `let _ =`) and the forget is
                        // skipped: forgetting a workspace whose directory survived
                        // would orphan that dir — worse than a still-attached workspace
                        // — and the reported error names what is still registered so
                        // the cleanup can be safely re-run once the directory is free.
                        if abs_path.exists() {
                            std::fs::remove_dir_all(&abs_path).map_err(|e| {
                                Error::Io(std::io::Error::new(
                                    e.kind(),
                                    format!(
                                        "failed to remove the worktree directory {} ({e}); the jj \
                                         workspace `{name}` is still registered — free the \
                                         directory and retry the cleanup",
                                        abs_path.display()
                                    ),
                                ))
                            })?;
                        }
                        vcs_jj::blocking::workspace_forget(&self.cwd, &name).map_err(Error::Io)
                    }
                    None => Ok(()),
                }
            }
        }
    }
}

/// Generate a facade trait from one signature table: the `#[async_trait]` trait
/// declaration *and* the delegating `impl … for $Ty<R>`, so the two can never drift
/// out of sync (a hazard when each is hand-maintained). Every generated body is a
/// trivial delegation to the like-named inherent method — which method resolution
/// prefers, so this never recurses; the real backend-`match` dispatch stays
/// hand-written on the inherent `impl`. `async` methods doc-link to their inherent
/// twin; `sync` methods carry an explicit doc string (their docs aren't uniform).
///
/// `vcs-forge` used to carry a near-identical copy of this macro, kept
/// deliberately unshared (separate crates, ~40-line macro — duplication beats a
/// new dependency); it was removed there in v0.1.1 when new trait methods needed
/// default bodies the macro couldn't express, so `vcs-forge`'s facade trait and
/// impl are now hand-maintained (see the removal note in `vcs-forge`'s
/// `src/lib.rs`). This crate is still v0.x and doesn't need that, so the
/// original signature-table macro remains the right shape here.
///
/// Signatures only: each entry is a bare `&self` (or sync) method — no method-level
/// generics, no `&mut self`, no default bodies (a new method shaped that way needs a
/// grammar tweak, not just a table row).
///
/// No `mockall::automock`: a Wave-S spike proved it can't process a trait whose
/// signatures come from `macro_rules!`. Captured `$_:ty` fragments reach `automock`
/// as opaque nonterminal token groups; its `syn` parser rejects them ("unsupported
/// type in this position"), whereas `#[async_trait]` tolerates them. So the facade
/// traits stay test-seam-tested (build a handle over a fake runner — see the trait
/// docs), which is also what their docs already recommend over mocking.
macro_rules! facade_trait {
    (
        $(#[doc = $tdoc:expr])*
        trait $Trait:ident for $Ty:ident;
        sync {
            $( #[doc = $sdoc:expr] fn $sn:ident( $($sa:ident: $sat:ty),* $(,)? ) -> $sr:ty; )*
        }
        async {
            $( fn $an:ident( $($aa:ident: $aat:ty),* $(,)? ) -> $ar:ty; )*
        }
    ) => {
        $(#[doc = $tdoc])*
        #[async_trait::async_trait]
        pub trait $Trait: Send + Sync {
            $(
                #[doc = $sdoc]
                fn $sn(&self, $($sa: $sat),*) -> $sr;
            )*
            $(
                #[doc = concat!("See [`", stringify!($Ty), "::", stringify!($an), "`].")]
                async fn $an(&self, $($aa: $aat),*) -> $ar;
            )*
        }

        // Delegates to the inherent methods, which method resolution prefers — so
        // these bodies dispatch through the concrete type's real implementations,
        // not back into the trait.
        #[async_trait::async_trait]
        impl<R: ProcessRunner> $Trait for $Ty<R> {
            $(
                fn $sn(&self, $($sa: $sat),*) -> $sr {
                    self.$sn($($sa),*)
                }
            )*
            $(
                async fn $an(&self, $($aa: $aat),*) -> $ar {
                    self.$an($($aa),*).await
                }
            )*
        }
    };
}

facade_trait! {
    /// The backend-agnostic common surface of [`Repo`], as a trait — so a consumer can
    /// hold a `Box<dyn VcsRepo>` / `&dyn VcsRepo` and code against the operations
    /// without naming the [`ProcessRunner`] generic or wrapping `Repo` themselves.
    ///
    /// Every method mirrors the like-named inherent method on [`Repo`]; the trait adds
    /// nothing but the abstraction boundary. Tool-specific operations stay off it (see
    /// the crate docs) — reach those through the concrete [`Repo`] and its bound
    /// handles. For hermetic tests, build a `Repo` over a fake runner with
    /// [`Repo::from_git`] / [`Repo::from_jj`] rather than mocking this trait.
    trait VcsRepo for Repo;
    sync {
        #[doc = "Which backend drives this handle."]
        fn kind() -> BackendKind;
        #[doc = "The repository root detected at open time."]
        fn root() -> &Path;
        #[doc = "The directory operations run against."]
        fn cwd() -> &Path;
        #[doc = "See [`Repo::cleanup_worktree_blocking`]."]
        fn cleanup_worktree_blocking(path: &Path) -> Result<()>;
    }
    async {
        fn current_branch() -> Result<Option<String>>;
        fn trunk() -> Result<Option<String>>;
        fn local_branches() -> Result<Vec<String>>;
        fn local_branches_readonly() -> Result<Vec<String>>;
        fn branch_exists(name: &str) -> Result<bool>;
        fn has_uncommitted_changes() -> Result<bool>;
        fn has_tracked_changes() -> Result<bool>;
        fn conflicted_files() -> Result<Vec<PathBuf>>;
        fn create_branch(name: &str) -> Result<()>;
        fn delete_branch(spec: BranchDelete) -> Result<()>;
        fn rename_branch(old: &str, new: &str) -> Result<()>;
        fn changed_files() -> Result<Vec<FileChange>>;
        fn diff_stat() -> Result<DiffStat>;
        fn diff() -> Result<Vec<FileDiff>>;
        fn log(revspec_or_revset: &str, max: usize) -> Result<Vec<Commit>>;
        fn show_file(rev: &str, path: &str) -> Result<String>;
        fn annotate(path: &str, rev: Option<&str>) -> Result<Vec<AnnotationLine>>;
        fn show_file_within(rev: &str, path: &str, budget: OutputBudget) -> Result<String>;
        fn snapshot() -> Result<RepoSnapshot>;
        fn snapshot_readonly() -> Result<RepoSnapshot>;
        fn commit_paths(paths: &[PathBuf], message: &str) -> Result<()>;
        fn fetch() -> Result<()>;
        fn fetch_from(remote: &str) -> Result<()>;
        fn fetch_branch(branch: &str) -> Result<()>;
        fn push(branch: &str) -> Result<()>;
        fn checkout(reference: &str) -> Result<()>;
        fn new_child(reference: &str) -> Result<()>;
        fn rebase(onto: &str) -> Result<()>;
        fn try_merge(source: &str) -> Result<MergeProbe>;
        fn abort_in_progress() -> Result<OperationState>;
        fn continue_in_progress() -> Result<OperationState>;
        fn in_progress_state() -> Result<OperationState>;
        fn list_worktrees() -> Result<Vec<WorktreeInfo>>;
        fn create_worktree(spec: WorktreeCreate) -> Result<CreateOutcome>;
        fn remove_worktree(spec: WorktreeRemove) -> Result<()>;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use processkit::testing::{Reply, ScriptedRunner};
    // The shared sandbox fixture — a unique temp dir removed on drop. Using the
    // testkit's one impl instead of a private copy means the wrappers/facades
    // don't each carry a fixture that could drift.
    use vcs_testkit::TempDir;

    // --- discover ------------------------------------------------------------

    #[test]
    fn discover_finds_git_and_jj_and_prefers_jj() {
        let tmp = TempDir::new("discover");
        let root = tmp.path();

        // Plain git repo.
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let located = discover(root).expect("git detected");
        assert_eq!(located.kind, BackendKind::Git);
        assert_eq!(located.root, root);

        // Colocated: adding a *valid* .jj (with its `repo` store) makes jj win.
        std::fs::create_dir_all(root.join(".jj").join("repo")).unwrap();
        assert_eq!(discover(root).unwrap().kind, BackendKind::Jj);
    }

    // M19: a stray/empty `.jj` directory (no `repo` store — e.g. a leftover
    // `mkdir .jj`) is NOT a jj marker and must not shadow a healthy `.git` repo in the
    // same directory. A valid `.jj` (with `repo`, dir or file) still wins.
    #[test]
    fn discover_ignores_a_dotjj_without_a_repo_store() {
        let tmp = TempDir::new("stray-jj");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".git")).unwrap();
        std::fs::create_dir_all(root.join(".jj")).unwrap(); // empty — no `repo`
        assert_eq!(
            discover(root).expect("git still detected").kind,
            BackendKind::Git,
            "an empty .jj must not shadow a real .git"
        );

        // A secondary workspace's `.jj/repo` is a *file* pointer — still valid.
        let sec = TempDir::new("jj-secondary");
        std::fs::create_dir_all(sec.path().join(".jj")).unwrap();
        std::fs::write(sec.path().join(".jj").join("repo"), b"/path/to/store\n").unwrap();
        assert_eq!(discover(sec.path()).unwrap().kind, BackendKind::Jj);
    }

    #[test]
    fn discover_walks_up_to_ancestor() {
        let tmp = TempDir::new("walkup");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let nested = root.join("a").join("b");
        std::fs::create_dir_all(&nested).unwrap();
        let located = discover(&nested).expect("found via ancestor walk");
        assert_eq!(located.kind, BackendKind::Git);
        assert_eq!(located.root, root);
    }

    #[test]
    fn discover_returns_none_outside_repo() {
        let tmp = TempDir::new("norepo");
        assert!(discover(tmp.path()).is_none());
    }

    // A gitlink `.git` *file* (a linked worktree / submodule) is a valid git marker;
    // a stray file merely named `.git` is NOT — so it can't shadow a real repo above.
    #[test]
    fn discover_validates_dotgit_file_is_a_gitlink() {
        let tmp = TempDir::new("gitlink");
        let root = tmp.path();

        // A gitlink file → detected as a git repo at this dir.
        std::fs::write(root.join(".git"), "gitdir: /somewhere/.git/worktrees/wt\n").unwrap();
        assert_eq!(
            discover(root).expect("gitlink detected").kind,
            BackendKind::Git
        );

        // A garbage file named `.git` (not a gitlink) is rejected — and must NOT
        // shadow a real `.git` directory in the parent.
        let parent = TempDir::new("gitlink-parent");
        std::fs::create_dir_all(parent.path().join(".git")).unwrap();
        let child = parent.path().join("sub");
        std::fs::create_dir_all(&child).unwrap();
        std::fs::write(child.join(".git"), "not a gitlink, just noise\n").unwrap();
        let located = discover(&child).expect("walks up past the bogus .git file");
        assert_eq!(located.root, parent.path(), "the real repo is the parent");

        // An empty `.git` file is not a marker.
        let empty = TempDir::new("gitlink-empty");
        std::fs::write(empty.path().join(".git"), "").unwrap();
        assert!(discover(empty.path()).is_none(), "empty .git is not a repo");

        // Leading whitespace before `gitdir:` is tolerated (the `trim_start`).
        let spaced = TempDir::new("gitlink-spaced");
        std::fs::write(
            spaced.path().join(".git"),
            "  gitdir: /x/.git/worktrees/w\n",
        )
        .unwrap();
        assert_eq!(
            discover(spaced.path())
                .expect("spaced gitlink detected")
                .kind,
            BackendKind::Git
        );
    }

    // --- bare git repository (issue #6) -------------------------------------

    // The issue #6 repro: a `git init --bare` directory (no `.git` subdir, just
    // `HEAD`/`config`/`objects`/`refs` in the root) must open as
    // `Error::BareRepository`, not the generic `Error::NotARepository` — matched
    // by variant, not by message substring, so the distinction can't silently
    // regress into the old generic error.
    #[test]
    fn discover_reports_bare_repository_not_generic_not_a_repository() {
        let tmp = TempDir::new("bare-repo");
        let root = tmp.path();
        std::fs::write(root.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        std::fs::write(root.join("config"), "[core]\n\tbare = true\n").unwrap();
        std::fs::create_dir_all(root.join("objects")).unwrap();
        std::fs::create_dir_all(root.join("refs")).unwrap();

        match Repo::discover(root) {
            Err(Error::BareRepository(p)) => assert_eq!(p, root),
            other => panic!("expected Error::BareRepository, got {other:?}"),
        }

        // The strict, non-walking `open`, called directly on the bare repo's own
        // root, also special-cases it via `is_bare_git_repo_marker` — mirroring
        // `discover`'s classification for this same directory (issue #6/#8
        // symmetry), even though `open` itself never walks up.
        match Repo::open(root) {
            Err(Error::BareRepository(p)) => assert_eq!(p, root),
            other => panic!("expected Error::BareRepository, got {other:?}"),
        }
    }

    // A bare repository nested a few levels below `dir` is still found by
    // walking up — mirrors `discover_walks_up_to_ancestor` for the bare case.
    #[test]
    fn discover_finds_bare_repository_via_ancestor_walk() {
        let tmp = TempDir::new("bare-walkup");
        let root = tmp.path();
        std::fs::write(root.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        std::fs::write(root.join("config"), "[core]\n\tbare = true\n").unwrap();
        std::fs::create_dir_all(root.join("objects")).unwrap();
        std::fs::create_dir_all(root.join("refs")).unwrap();
        let nested = root.join("a").join("b");
        std::fs::create_dir_all(&nested).unwrap();

        match Repo::discover(&nested) {
            Err(Error::BareRepository(p)) => assert_eq!(p, root),
            other => panic!("expected Error::BareRepository, got {other:?}"),
        }

        // The strict `open` never walks up, so it reports `NotARepository` on
        // the nested dir regardless of what sits above it.
        match Repo::open(&nested) {
            Err(Error::NotARepository(p)) => assert_eq!(p, nested),
            other => panic!("expected Error::NotARepository, got {other:?}"),
        }
    }

    // A directory that merely happens to hold some, but not all four, of the
    // bare-repo marker entries must NOT be misdetected as a bare repository —
    // it's just an ordinary non-repository directory.
    #[test]
    fn discover_does_not_misdetect_partial_bare_markers_as_bare_repository() {
        let tmp = TempDir::new("bare-partial");
        let root = tmp.path();
        // Only `HEAD` and `config` — no `objects`/`refs` directories.
        std::fs::write(root.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        std::fs::write(root.join("config"), "[core]\n\tbare = true\n").unwrap();

        match Repo::discover(root) {
            Err(Error::NotARepository(p)) => assert_eq!(p, root),
            other => panic!("expected Error::NotARepository, got {other:?}"),
        }
    }

    // A real (non-bare) git repository — `.git` subdirectory present — must
    // keep opening as before, not get swept up by the new bare-detection path.
    #[test]
    fn open_still_opens_a_normal_git_repository() {
        let tmp = TempDir::new("normal-git");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".git")).unwrap();

        let repo = Repo::open(root).expect("normal git repo still opens");
        assert_eq!(repo.kind(), BackendKind::Git);
        assert_eq!(repo.root(), root);
    }

    // A real jj repository must also keep opening as before.
    #[test]
    fn open_still_opens_a_normal_jj_repository() {
        let tmp = TempDir::new("normal-jj");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".jj").join("repo")).unwrap();

        let repo = Repo::open(root).expect("normal jj repo still opens");
        assert_eq!(repo.kind(), BackendKind::Jj);
        assert_eq!(repo.root(), root);
    }

    // A directory that is neither a repo nor a bare repo still reports the
    // generic `NotARepository`.
    #[test]
    fn open_reports_not_a_repository_when_nothing_found() {
        let tmp = TempDir::new("norepo-open");
        match Repo::open(tmp.path()) {
            Err(Error::NotARepository(p)) => assert_eq!(p, tmp.path()),
            other => panic!("expected Error::NotARepository, got {other:?}"),
        }
    }

    // Unlike `discover`, the strict `open` never walks up — a repository at an
    // ancestor of `dir` must NOT make `open(dir)` succeed, even though
    // `discover(dir)` would find it.
    #[test]
    fn open_does_not_walk_up_even_though_discover_would() {
        let tmp = TempDir::new("open-no-walkup");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let nested = root.join("a").join("b");
        std::fs::create_dir_all(&nested).unwrap();

        match Repo::open(&nested) {
            Err(Error::NotARepository(p)) => assert_eq!(p, nested),
            other => panic!("expected Error::NotARepository, got {other:?}"),
        }
        // `discover` from the same nested dir finds the repo at `root`.
        assert_eq!(
            Repo::discover(&nested).expect("discover walks up").root(),
            root
        );
    }

    // --- discover_with (injected clients) -----------------------------------

    // `discover_with` runs the SAME detection as `Repo::discover`, then builds the
    // handle from the caller's client for the DETECTED backend only — the other
    // factory is never invoked (so no client is built speculatively). Both the git
    // and jj happy paths are covered here, over a hermetic `ScriptedRunner` client.
    #[test]
    fn discover_with_builds_only_the_detected_backends_client() {
        use std::cell::Cell;

        // git: a `.git` dir → the git factory runs, the jj factory does not; the
        // handle is git-backed and bound to the absolutised discovery dir.
        let tmp = TempDir::new("discover-with-git");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let git_built = Cell::new(false);
        let jj_built = Cell::new(false);
        let repo = Repo::discover_with(
            root,
            || {
                git_built.set(true);
                Git::with_runner(ScriptedRunner::new())
            },
            || {
                jj_built.set(true);
                Jj::with_runner(ScriptedRunner::new())
            },
        )
        .expect("git repo discovered");
        assert_eq!(repo.kind(), BackendKind::Git);
        assert_eq!(repo.root(), root);
        assert_eq!(repo.cwd(), root);
        assert!(git_built.get(), "the git factory must run for a .git repo");
        assert!(
            !jj_built.get(),
            "the jj factory must NOT run for a .git repo"
        );

        // jj: a valid `.jj` (with its `repo` store) → symmetric, only the jj factory
        // runs. `.jj` wins over `.git` exactly as in `discover`.
        let tmp = TempDir::new("discover-with-jj");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".jj").join("repo")).unwrap();
        let git_built = Cell::new(false);
        let jj_built = Cell::new(false);
        let repo = Repo::discover_with(
            root,
            || {
                git_built.set(true);
                Git::with_runner(ScriptedRunner::new())
            },
            || {
                jj_built.set(true);
                Jj::with_runner(ScriptedRunner::new())
            },
        )
        .expect("jj repo discovered");
        assert_eq!(repo.kind(), BackendKind::Jj);
        assert_eq!(repo.root(), root);
        assert!(jj_built.get(), "the jj factory must run for a .jj repo");
        assert!(
            !git_built.get(),
            "the git factory must NOT run for a .jj repo"
        );
    }

    // The injected client actually DRIVES the handle's operations (not merely
    // stored): a `Repo` opened via `discover_with` over a scripted git client
    // answers `current_branch` from that runner's scripted reply — proving the
    // caller-provided client, not a default one, is what backs the facade. The jj
    // factory panics if touched, pinning the "detected backend only" contract.
    #[tokio::test]
    async fn discover_with_injects_the_client_that_backs_operations() {
        let tmp = TempDir::new("discover-with-drives");
        let root = tmp.path();
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let repo = Repo::discover_with(
            root,
            || {
                Git::with_runner(ScriptedRunner::new().on(
                    ["git", "symbolic-ref", "--quiet", "--short", "HEAD"],
                    Reply::ok("feature/x"),
                ))
            },
            || -> Jj<ScriptedRunner> { panic!("jj factory must not run for a .git repo") },
        )
        .expect("git repo discovered");
        assert_eq!(
            repo.current_branch().await.unwrap().as_deref(),
            Some("feature/x"),
            "the scripted, injected client must answer the facade call"
        );
    }

    // The bare-repository diagnostic is shared with `Repo::discover`: opening a
    // `git init --bare` directory (no working tree) via the injected-client path
    // still yields `Error::BareRepository`, matched by variant — not the generic
    // `NotARepository`, and not a stringly-typed message. Neither client factory
    // runs, since discovery finds no working tree to back.
    #[test]
    fn discover_with_reports_bare_repository_on_injected_client() {
        use std::cell::Cell;
        let tmp = TempDir::new("discover-with-bare");
        let root = tmp.path();
        std::fs::write(root.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        std::fs::write(root.join("config"), "[core]\n\tbare = true\n").unwrap();
        std::fs::create_dir_all(root.join("objects")).unwrap();
        std::fs::create_dir_all(root.join("refs")).unwrap();

        let git_built = Cell::new(false);
        let jj_built = Cell::new(false);
        let outcome = Repo::discover_with(
            root,
            || {
                git_built.set(true);
                Git::with_runner(ScriptedRunner::new())
            },
            || {
                jj_built.set(true);
                Jj::with_runner(ScriptedRunner::new())
            },
        );
        match outcome {
            Err(Error::BareRepository(p)) => assert_eq!(p, root),
            other => panic!("expected Error::BareRepository, got {other:?}"),
        }
        assert!(
            !git_built.get() && !jj_built.get(),
            "no client is built when discovery finds no working tree"
        );

        // A directory that is neither a repo nor a bare repo still reports the
        // generic `NotARepository` through the same path.
        let empty = TempDir::new("discover-with-norepo");
        match Repo::discover_with(
            empty.path(),
            || Git::with_runner(ScriptedRunner::new()),
            || Jj::with_runner(ScriptedRunner::new()),
        ) {
            Err(Error::NotARepository(p)) => assert_eq!(p, empty.path()),
            other => panic!("expected Error::NotARepository, got {other:?}"),
        }
    }

    // --- dispatch (hermetic, ScriptedRunner-backed) ------------------------

    fn git_repo(runner: ScriptedRunner) -> Repo<ScriptedRunner> {
        Repo::from_git("/repo", "/repo", Git::with_runner(runner))
    }

    fn jj_repo(runner: ScriptedRunner) -> Repo<ScriptedRunner> {
        Repo::from_jj("/repo", "/repo", Jj::with_runner(runner))
    }

    // --- Debug -------------------------------------------------------------
    //
    // Regression tests for the `Repo`/`Backend` `Debug` impl (PR #7): formatting
    // the facade must show the elided shape (`Repo { .. }` with a `Git(..)`/
    // `Jj(..)` backend) but never expose the wrapped CLI client — and therefore
    // never a credential token that client might hold. These exist to catch a
    // future refactor that accidentally starts formatting the client (e.g.
    // deriving `Debug` on `Backend` directly, or dropping `finish_non_exhaustive`).

    // A git-backed `Repo` built over a `Git` client holding a token via
    // `with_token` must format to the expected elided shape and must NOT leak
    // the token (or any other inner-client internal) through `{:?}`.
    #[test]
    fn debug_output_shows_elided_git_backend_and_never_leaks_the_token() {
        let repo = Repo::from_git(
            "/repo",
            "/repo",
            Git::with_runner(ScriptedRunner::new()).with_token("ghp_super_secret_token"),
        );
        let out = format!("{repo:?}");
        assert!(out.contains("Repo {"), "{out}");
        assert!(out.contains("root"), "{out}");
        assert!(out.contains("cwd"), "{out}");
        assert!(out.contains("Git(.."), "{out}");
        assert!(
            !out.contains("ghp_super_secret_token"),
            "token must not leak through Debug: {out}"
        );
        // Nothing from the inner `Git`/`ManagedClient`/`CliClient` internals
        // (e.g. its env-var bookkeeping) should surface either — the backend
        // must render as a bare, elided discriminant.
        assert!(!out.contains("ManagedClient"), "{out}");
        assert!(!out.contains("CliClient"), "{out}");
    }

    // A jj-backed `Repo` (jj is ambient-auth-only — no `with_token`) must format
    // to the analogous elided shape, with the `Jj(..)` discriminant and no inner
    // client internals.
    #[test]
    fn debug_output_shows_elided_jj_backend() {
        let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(ScriptedRunner::new()));
        let out = format!("{repo:?}");
        assert!(out.contains("Repo {"), "{out}");
        assert!(out.contains("Jj(.."), "{out}");
        assert!(!out.contains("ManagedClient"), "{out}");
        assert!(!out.contains("CliClient"), "{out}");
    }

    // --- snapshot ----------------------------------------------------------

    // git: one porcelain-v2 call + a git-dir probe → a combined RepoSnapshot.
    #[tokio::test]
    async fn git_snapshot_combines_v2_status_and_op_state() {
        let v2 = concat!(
            "# branch.oid abc123\0",
            "# branch.head main\0",
            "# branch.upstream origin/main\0",
            "# branch.ab +2 -0\0",
            "1 .M N... 100644 100644 100644 1 2 a.rs\0",
            "? new.txt\0",
        );
        // An empty git dir → no MERGE_HEAD / rebase dir → Clear.
        let gitdir = TempDir::new("snap-git");
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
                .on(
                    ["git", "rev-parse", "--git-dir"],
                    Reply::ok(gitdir.path().to_str().unwrap()),
                ),
        );
        let s = repo.snapshot().await.unwrap();
        assert_eq!(s.branch.as_deref(), Some("main"));
        let tracking = s.tracking.as_ref().expect("upstream tracking");
        assert_eq!(tracking.branch, "origin/main");
        assert_eq!((tracking.ahead, tracking.behind), (Some(2), Some(0)));
        assert!(s.dirty);
        assert_eq!(s.change_count, 2, "1 tracked + 1 untracked");
        assert!(!s.conflicted);
        assert_eq!(s.operation, OperationState::Clear);
    }

    // M20 (whole-solution): `snapshot()` has its OWN operation probe (separate from
    // `in_progress_state`); it too must report a `git am` as `ApplyMailbox`, not
    // `Rebase` — otherwise the new variant is dead on the snapshot → watch → mcp path.
    #[tokio::test]
    async fn git_snapshot_reports_git_am_as_apply_mailbox() {
        let v2 = concat!("# branch.oid abc\0", "# branch.head main\0");
        let gitdir = TempDir::new("snap-git-am");
        // A `git am` in progress: `rebase-apply/` WITH the `applying` marker.
        let apply = gitdir.path().join("rebase-apply");
        std::fs::create_dir_all(&apply).unwrap();
        std::fs::write(apply.join("applying"), b"").unwrap();
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
                .on(
                    ["git", "rev-parse", "--git-dir"],
                    Reply::ok(gitdir.path().to_str().unwrap()),
                ),
        );
        let s = repo.snapshot().await.unwrap();
        assert_eq!(
            s.operation,
            OperationState::ApplyMailbox,
            "a git am must not read as Rebase in snapshot()"
        );
    }

    // git with NO upstream configured: porcelain v2 omits the `# branch.upstream`
    // and `# branch.ab` lines, so `tracking` is None (the all-or-nothing invariant —
    // git is the only backend that can produce either) — mirrors the jj None case.
    #[tokio::test]
    async fn git_snapshot_without_upstream_has_no_tracking() {
        let v2 = concat!("# branch.oid abc123\0", "# branch.head main\0");
        let gitdir = TempDir::new("snap-git-noup");
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
                .on(
                    ["git", "rev-parse", "--git-dir"],
                    Reply::ok(gitdir.path().to_str().unwrap()),
                ),
        );
        let s = repo.snapshot().await.unwrap();
        assert_eq!(s.branch.as_deref(), Some("main"));
        assert!(s.tracking.is_none(), "no upstream → no tracking");
    }

    // M17: an upstream that is SET but GONE (deleted on the remote, or not yet
    // fetched) — porcelain v2 emits `# branch.upstream` but OMITS `# branch.ab`, so the
    // counts are uncountable. `tracking` must be `Some { branch, ahead: None, behind:
    // None }` (tracking configured but uncountable), NOT a fabricated in-sync `0`/`0`.
    #[tokio::test]
    async fn git_snapshot_upstream_set_but_gone_is_uncountable() {
        let v2 = concat!(
            "# branch.oid abc123\0",
            "# branch.head main\0",
            "# branch.upstream origin/main\0", // upstream named…
                                               // …but no `# branch.ab` line — it doesn't resolve.
        );
        let gitdir = TempDir::new("snap-git-gone");
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
                .on(
                    ["git", "rev-parse", "--git-dir"],
                    Reply::ok(gitdir.path().to_str().unwrap()),
                ),
        );
        let s = repo.snapshot().await.unwrap();
        let tracking = s.tracking.as_ref().expect("upstream is set");
        assert_eq!(tracking.branch, "origin/main");
        assert_eq!(
            (tracking.ahead, tracking.behind),
            (None, None),
            "a gone upstream is uncountable, not in-sync 0/0"
        );
    }

    // jj: one template row + a status count; a conflicted @ maps to Conflict; no
    // git-style upstream/ahead/behind.
    #[tokio::test]
    async fn jj_snapshot_dirty_with_change_count() {
        let repo = jj_repo(
            ScriptedRunner::new()
                // snapshot template (`jj log -r @`): commit_id \t empty \t conflict
                .on(["jj", "log", "-r", "@"], Reply::ok("deadbeef\t0\t1\n")) // empty=0 dirty, conflict=1
                // `branch` via `current_branch` → `reachable_bookmarks`
                // (`jj log -r heads(::@ & bookmarks())`): bookmarks \t commit
                .on(
                    ["jj", "log", "-r", "heads(::@ & bookmarks())"],
                    Reply::ok("\"main\"\tdeadbeef\n"),
                )
                .on(["jj", "root"], Reply::ok("/repo\n"))
                .on(["jj", "diff"], Reply::ok("M a.rs\nA b.rs\n")), // status -r @ --summary → 2
        );
        let s = repo.snapshot().await.unwrap();
        assert_eq!(s.head.as_deref(), Some("deadbeef"));
        assert_eq!(s.branch.as_deref(), Some("main"));
        assert!(s.dirty);
        assert_eq!(s.change_count, 2);
        assert!(s.conflicted);
        assert_eq!(s.operation, OperationState::Conflict);
        assert!(s.tracking.is_none(), "jj has no upstream tracking");
    }

    // jj: a clean `@` (empty=1) skips the change-count spawn entirely — the test
    // scripts NO `diff` rule, so calling `status` would error.
    #[tokio::test]
    async fn jj_snapshot_clean_skips_change_count() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(["jj", "log", "-r", "@"], Reply::ok("c0ffee\t1\t0\n"))
                .on(
                    ["jj", "log", "-r", "heads(::@ & bookmarks())"],
                    Reply::ok(""),
                ),
        );
        let s = repo.snapshot().await.unwrap();
        assert_eq!(s.head.as_deref(), Some("c0ffee"));
        assert_eq!(s.branch, None, "no bookmark");
        assert!(!s.dirty);
        assert_eq!(s.change_count, 0);
        assert!(!s.conflicted);
        assert_eq!(s.operation, OperationState::Clear);
    }

    // jj: a conflicted `@` that jj marks `empty` (conflict but no net content change)
    // is still reported `dirty` — the conflict is uncommitted state needing
    // resolution — so the count runs and the snapshot is coherent (no
    // `conflicted: true` next to `dirty: false`), mirroring git's conflict handling.
    #[tokio::test]
    async fn jj_snapshot_conflicted_empty_change_is_dirty() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(["jj", "log", "-r", "@"], Reply::ok("c0ffee\t1\t1\n")) // empty=1, conflict=1
                .on(
                    ["jj", "log", "-r", "heads(::@ & bookmarks())"],
                    Reply::ok(""),
                ) // no bookmark
                .on(["jj", "root"], Reply::ok("/repo\n"))
                .on(["jj", "diff"], Reply::ok("M conflicted.rs\n")), // status → 1
        );
        let s = repo.snapshot().await.unwrap();
        assert!(s.conflicted);
        assert!(s.dirty, "a conflicted change is a dirty working copy");
        assert_eq!(s.change_count, 1);
        assert_eq!(s.operation, OperationState::Conflict);
    }

    // jj `list_worktrees` resolves each workspace's root via the batched
    // `workspace_roots` fan-out (one `workspace root --name <n>` per `workspace
    // list` row), then builds a `WorktreeInfo` per workspace. Hermetic: scripts the
    // template rows + the per-name root replies — the backend glue that the
    // `#[ignore]` integration tests otherwise cover only with a real `jj`.
    #[tokio::test]
    async fn jj_list_worktrees_batches_root_lookups() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(
                    ["jj", "workspace", "list"],
                    Reply::ok("\"default\"\tc0ffee\t\"main\"\n\"ws1\"\tdecaf0\t\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "default",
                    ],
                    Reply::ok("/repo\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "ws1",
                    ],
                    Reply::ok("/repo/ws1\n"),
                ),
        );
        let worktrees = repo.list_worktrees().await.expect("list_worktrees");
        assert_eq!(worktrees.len(), 2);
        assert_eq!(worktrees[0].path, Path::new("/repo"));
        assert_eq!(worktrees[0].branch.as_deref(), Some("main"));
        assert_eq!(worktrees[1].path, Path::new("/repo/ws1"));
        assert_eq!(worktrees[1].branch, None);
    }

    // A workspace whose `workspace root` lookup errors is skipped (no useful path),
    // mirroring the old sequential loop — the batch maps that slot to `Err`.
    #[tokio::test]
    async fn jj_list_worktrees_skips_unresolvable_root() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(
                    ["jj", "workspace", "list"],
                    Reply::ok("\"default\"\tc0ffee\t\"main\"\n\"gone\"\tdecaf0\t\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "default",
                    ],
                    Reply::ok("/repo\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "gone",
                    ],
                    Reply::fail(1, "Error: No such workspace"),
                ),
        );
        let worktrees = repo.list_worktrees().await.expect("list_worktrees");
        assert_eq!(worktrees.len(), 1, "the unresolvable workspace is skipped");
        assert_eq!(worktrees[0].path, Path::new("/repo"));
    }

    // remove_worktree surfaces a `workspace forget` failure rather than swallowing
    // it — name resolution already proved the workspace is registered, so a forget
    // error is a real dangling-registration the caller should see.
    #[tokio::test]
    async fn jj_remove_worktree_surfaces_forget_error() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(
                    ["jj", "workspace", "list"],
                    Reply::ok("\"ws1\"\tc0ffee\t\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "ws1",
                    ],
                    Reply::ok("/repo/ws1\n"),
                )
                .on(
                    ["jj", "workspace", "forget"],
                    Reply::fail(1, "Error: cannot forget workspace"),
                ),
        );
        // `/repo/ws1` does not exist on disk, so the dir-removal step is skipped and
        // the forget error is the sole outcome.
        let res = repo.remove_worktree(WorktreeRemove::new("/repo/ws1")).await;
        assert!(res.is_err(), "a forget failure is surfaced, not swallowed");
    }

    // Windows-like removal failure: `remove_worktree` surfaces a `remove_dir_all`
    // failure and names what remains (the still-registered workspace) rather than
    // swallowing it. A *file* sits where the workspace dir should be, so
    // `remove_dir_all` errors deterministically on every platform.
    #[tokio::test]
    async fn jj_remove_worktree_surfaces_dir_removal_failure() {
        let tmp = TempDir::new("rmw-rmdir-fail");
        let ws = tmp.path().join("ws1");
        std::fs::write(&ws, b"not a dir").expect("write file where the dir should be");
        let root = tmp.path().to_string_lossy().into_owned();
        let ws_str = ws.to_string_lossy().into_owned();
        let repo = Repo::from_jj(
            &root,
            &root,
            Jj::with_runner(
                ScriptedRunner::new()
                    .on(
                        ["jj", "workspace", "list"],
                        Reply::ok("\"ws1\"\tc0ffee\t\n"),
                    )
                    .on(
                        [
                            "jj",
                            "--ignore-working-copy",
                            "workspace",
                            "root",
                            "--name",
                            "ws1",
                        ],
                        Reply::ok(format!("{ws_str}\n")),
                    ),
            ),
        );
        // force skips the dirty check, so the removal step is reached directly.
        let err = repo
            .remove_worktree(WorktreeRemove::new(ws.clone()).force())
            .await
            .expect_err("a dir-removal failure must be surfaced");
        let msg = err.to_string();
        assert!(
            msg.contains("still registered") && msg.contains("ws1"),
            "the failure must name what remains to clean up: {msg}"
        );
        assert!(
            ws.exists(),
            "the undeletable path must survive the failed removal"
        );
    }

    // Compatible fallback / diagnosable error: when a registered workspace's root
    // can't be resolved via `workspace root --name`, a path matching none of the
    // resolvable ones is NOT reported as a clean `WorktreeNotFound` — absence can't be
    // proven, so a distinct diagnosable error naming the unresolved workspace is
    // raised instead (so a real-but-unresolvable workspace isn't misreported).
    #[tokio::test]
    async fn jj_remove_worktree_reports_unresolvable_workspaces() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(
                    ["jj", "workspace", "list"],
                    Reply::ok("\"ws1\"\tc0ffee\t\n\"gone\"\tdecaf0\t\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "ws1",
                    ],
                    Reply::ok("/repo/ws1\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "gone",
                    ],
                    Reply::fail(1, "Error: No such workspace"),
                ),
        );
        let err = repo
            .remove_worktree(WorktreeRemove::new("/repo/missing"))
            .await
            .expect_err("an unresolvable workspace must not be reported as a clean not-found");
        assert!(
            !err.is_resource_not_found(),
            "a partial resolution is not a clean WorktreeNotFound: {err}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("could not resolve") && msg.contains("gone"),
            "the diagnosable error must name the unresolved workspace: {msg}"
        );
    }

    // Repeated cleanup is idempotent: after a first pass removed the directory but its
    // `workspace forget` failed, a retry finds the dir already gone, re-resolves the
    // still-registered workspace by name, and completes the forget — no error.
    #[tokio::test]
    async fn jj_remove_worktree_retry_after_dir_gone_forgets_cleanly() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(
                    ["jj", "workspace", "list"],
                    Reply::ok("\"ws1\"\tc0ffee\t\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "ws1",
                    ],
                    Reply::ok("/repo/ws1\n"),
                )
                .on(["jj", "workspace", "forget"], Reply::ok("")),
        );
        // `/repo/ws1` does not exist on disk (a prior pass removed it), so the removal
        // step is skipped and the forget clears the dangling registration.
        repo.remove_worktree(WorktreeRemove::new("/repo/ws1"))
            .await
            .expect("a retry with the dir already gone completes the forget");
    }

    // C1: the default workspace resolves at the repo root; removing it would wipe
    // the whole repository, so it is refused even with force = true and WITHOUT
    // running `workspace forget` (no such cassette rule — a miss would also error,
    // so we assert the *refusal* message to prove the guard, not a fallthrough).
    #[tokio::test]
    async fn jj_remove_worktree_refuses_the_main_workspace() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(
                    ["jj", "workspace", "list"],
                    Reply::ok("\"default\"\tc0ffee\t\n"),
                )
                .on(
                    [
                        "jj",
                        "--ignore-working-copy",
                        "workspace",
                        "root",
                        "--name",
                        "default",
                    ],
                    Reply::ok("/repo\n"),
                ),
        );
        let err = repo
            .remove_worktree(WorktreeRemove::new("/repo").force())
            .await
            .expect_err("the main workspace must be refused");
        assert!(
            err.to_string().contains("main workspace"),
            "refusal message, not a cassette miss: {err}"
        );
    }

    // C1: a secondary workspace with un-snapshotted edits (`current_change` reports
    // non-empty) is refused under force = false, and its directory is NOT deleted.
    #[tokio::test]
    async fn jj_remove_worktree_refuses_dirty_workspace_without_force() {
        let tmp = TempDir::new("rmw-dirty");
        let root = tmp.path().to_string_lossy().into_owned();
        let repo = Repo::from_jj(
            &root,
            &root,
            Jj::with_runner(
                ScriptedRunner::new()
                    .on(
                        ["jj", "workspace", "list"],
                        Reply::ok("\"ws1\"\tc0ffee\t\n"),
                    )
                    .on(
                        [
                            "jj",
                            "--ignore-working-copy",
                            "workspace",
                            "root",
                            "--name",
                            "ws1",
                        ],
                        Reply::ok(format!("{root}\n")),
                    )
                    // `current_change` → 3rd field `false` = not empty = dirty.
                    .on(["jj", "log"], Reply::ok("aaa\tbbb\tfalse\t\"work\"\n")),
            ),
        );
        let err = repo
            .remove_worktree(WorktreeRemove::new(tmp.path()))
            .await
            .expect_err("a dirty workspace must be refused without force");
        assert!(
            err.to_string().contains("uncommitted changes"),
            "refusal message: {err}"
        );
        assert!(
            tmp.path().exists(),
            "the workspace directory must survive a refusal"
        );
    }

    // C1: force = true skips the dirty check and removes the directory (no
    // `current_change` rule is scripted, proving the check is bypassed).
    #[tokio::test]
    async fn jj_remove_worktree_with_force_removes_the_dir() {
        let tmp = TempDir::new("rmw-force");
        let ws = tmp.path().join("ws1");
        std::fs::create_dir_all(&ws).expect("mkdir ws");
        let root = tmp.path().to_string_lossy().into_owned();
        let ws_str = ws.to_string_lossy().into_owned();
        let repo = Repo::from_jj(
            &root,
            &root,
            Jj::with_runner(
                ScriptedRunner::new()
                    .on(
                        ["jj", "workspace", "list"],
                        Reply::ok("\"ws1\"\tc0ffee\t\n"),
                    )
                    .on(
                        [
                            "jj",
                            "--ignore-working-copy",
                            "workspace",
                            "root",
                            "--name",
                            "ws1",
                        ],
                        Reply::ok(format!("{ws_str}\n")),
                    )
                    .on(["jj", "workspace", "forget"], Reply::ok("")),
            ),
        );
        repo.remove_worktree(WorktreeRemove::new(ws.clone()).force())
            .await
            .expect("force removes a dirty worktree");
        assert!(!ws.exists(), "the worktree directory was removed");
    }

    // C1: the main-workspace guard's store-directory branch — a workspace whose
    // name was changed away from `default` (via `jj workspace rename`) still owns
    // the object store (`.jj/repo` is a *directory*, not a secondary's file
    // pointer), so removal is refused even with force = true, and the dir survives.
    // Exercises the `|| .jj/repo.is_dir()` half of the guard (the name is not
    // `default`), which the name-based test can't reach.
    #[tokio::test]
    async fn jj_remove_worktree_refuses_renamed_store_owning_workspace() {
        let tmp = TempDir::new("rmw-store");
        std::fs::create_dir_all(tmp.path().join(".jj").join("repo")).expect("mk .jj/repo dir");
        let root = tmp.path().to_string_lossy().into_owned();
        let repo = Repo::from_jj(
            &root,
            &root,
            Jj::with_runner(
                ScriptedRunner::new()
                    .on(
                        ["jj", "workspace", "list"],
                        Reply::ok("\"mainws\"\tc0ffee\t\n"),
                    )
                    .on(
                        [
                            "jj",
                            "--ignore-working-copy",
                            "workspace",
                            "root",
                            "--name",
                            "mainws",
                        ],
                        Reply::ok(format!("{root}\n")),
                    ),
            ),
        );
        let err = repo
            .remove_worktree(WorktreeRemove::new(tmp.path()).force())
            .await
            .expect_err("a renamed store-owning workspace is still refused");
        assert!(
            err.to_string().contains("main workspace"),
            "refusal message: {err}"
        );
        assert!(
            tmp.path().exists(),
            "the store-owning directory must not be deleted"
        );
    }

    #[tokio::test]
    async fn kind_and_escape_hatches_reflect_backend() {
        let repo = git_repo(ScriptedRunner::new());
        assert_eq!(repo.kind(), BackendKind::Git);
        assert!(repo.git().is_some());
        assert!(repo.jj().is_none());
    }

    // The cwd-bound views mirror the backend, and `at` re-binds them to another
    // directory without a separate client.
    #[tokio::test]
    async fn bound_views_reflect_backend_and_cwd() {
        let git = git_repo(ScriptedRunner::new());
        assert!(git.git_at().is_some());
        assert!(git.jj_at().is_none());
        // A sibling handle bound elsewhere yields a view rooted at that dir.
        assert_eq!(git.at("/repo/wt").cwd(), Path::new("/repo/wt"));

        let jj = jj_repo(ScriptedRunner::new());
        assert!(jj.jj_at().is_some());
        assert!(jj.git_at().is_none());
    }

    #[tokio::test]
    async fn current_branch_maps_detached_head_to_none() {
        // git's `current_branch` now runs `symbolic-ref --quiet --short HEAD`:
        // exit 0 → the branch name, exit 1 → detached HEAD → None.
        let named =
            git_repo(ScriptedRunner::new().on(["git", "symbolic-ref"], Reply::ok("main\n")));
        assert_eq!(
            named.current_branch().await.unwrap().as_deref(),
            Some("main")
        );
        let detached =
            git_repo(ScriptedRunner::new().on(["git", "symbolic-ref"], Reply::fail(1, "")));
        assert!(detached.current_branch().await.unwrap().is_none());
    }

    #[tokio::test]
    async fn changed_files_maps_git_status() {
        let repo = git_repo(ScriptedRunner::new().on(
            ["git", "status"],
            Reply::ok(" M a.rs\0?? b.rs\0R  new.rs\0old.rs\0"),
        ));
        let changes = repo.changed_files().await.unwrap();
        assert_eq!(changes.len(), 3);
        assert_eq!(changes[0].kind, ChangeKind::Modified);
        assert_eq!(changes[1].kind, ChangeKind::Added);
        assert_eq!(changes[2].kind, ChangeKind::Renamed);
        assert_eq!(changes[2].old_path.as_deref(), Some(Path::new("old.rs")));
    }

    #[tokio::test]
    async fn local_branches_maps_git_branch_output() {
        let repo =
            git_repo(ScriptedRunner::new().on(["git", "branch"], Reply::ok("* main\n  feat\n")));
        assert_eq!(repo.local_branches().await.unwrap(), ["main", "feat"]);
    }

    #[tokio::test]
    async fn branch_exists_reads_show_ref_exit() {
        let yes = git_repo(ScriptedRunner::new().on(["git", "show-ref"], Reply::ok("")));
        assert!(yes.branch_exists("main").await.unwrap());
        let no = git_repo(ScriptedRunner::new().on(["git", "show-ref"], Reply::fail(1, "")));
        assert!(!no.branch_exists("nope").await.unwrap());
    }

    #[tokio::test]
    async fn has_uncommitted_changes_reflects_status() {
        let dirty = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok(" M a.rs\0")));
        assert!(dirty.has_uncommitted_changes().await.unwrap());
        let clean = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok("")));
        assert!(!clean.has_uncommitted_changes().await.unwrap());
    }

    #[tokio::test]
    async fn at_rebinds_cwd_and_shares_backend() {
        let repo = git_repo(ScriptedRunner::new());
        let moved = repo.at("/repo/sub");
        assert_eq!(moved.cwd(), Path::new("/repo/sub"));
        assert_eq!(moved.root(), Path::new("/repo"));
        assert_eq!(moved.kind(), BackendKind::Git);
    }

    // --- dispatch: jj backend (hermetic) -----------------------------------

    #[tokio::test]
    async fn jj_kind_and_escape_hatches_reflect_backend() {
        let repo = jj_repo(ScriptedRunner::new());
        assert_eq!(repo.kind(), BackendKind::Jj);
        assert!(repo.jj().is_some() && repo.git().is_none());
    }

    #[tokio::test]
    async fn jj_current_branch_reads_bookmark() {
        // current_branch derives from `reachable_bookmarks`, whose template is
        // `<bookmarks space-joined>\t<commit>` — distinct from the strict
        // `current_bookmark(@)` comma-joined template.
        let repo =
            jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("\"main\"\t53e4e879\n")));
        assert_eq!(
            repo.current_branch().await.unwrap().as_deref(),
            Some("main")
        );
    }

    #[tokio::test]
    async fn jj_current_branch_persists_across_commit() {
        // After a jj commit the new working-copy change carries no bookmark, but
        // the described parent does. `reachable_bookmarks` resolves the nearest
        // bookmarked ancestor, so the facade still reports it — git-like "I'm
        // still on my branch". Under the old strict `current_bookmark(@)` rule
        // this returned `None`; feeding the reachable template (`feat\t…`,
        // unparseable as a comma-joined bookmark name) pins the new derivation.
        let repo =
            jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("\"feat\"\tc8d49332\n")));
        assert_eq!(
            repo.current_branch().await.unwrap().as_deref(),
            Some("feat")
        );
    }

    #[tokio::test]
    async fn jj_current_branch_tie_break_is_deterministic() {
        // `heads(::@ & bookmarks())` can yield several equally-near bookmarks —
        // a merge of two bookmarked lines (one row each) or one commit carrying
        // several (one row, space-joined). current_branch returns the
        // lexicographically-smallest name regardless of jj's row order, so the
        // result is stable. Here: rows `zeta` then `alpha beta` ⇒ `alpha`.
        let repo = jj_repo(ScriptedRunner::new().on(
            ["jj", "log"],
            Reply::ok("\"zeta\"\tabc1234\n\"alpha\" \"beta\"\tdef5678\n"),
        ));
        assert_eq!(
            repo.current_branch().await.unwrap().as_deref(),
            Some("alpha")
        );
    }

    #[tokio::test]
    async fn jj_local_branches_maps_bookmark_list() {
        // BOOKMARK_LIST_TEMPLATE rows: `<present>\t<remote>\t"<name>"\t<commit>`.
        let repo = jj_repo(ScriptedRunner::new().on(
            ["jj", "bookmark", "list"],
            Reply::ok("1\t\t\"main\"\tcmt\n1\t\t\"feat\"\tm2\n"),
        ));
        assert_eq!(repo.local_branches().await.unwrap(), ["main", "feat"]);
    }

    #[tokio::test]
    async fn jj_branch_exists_scans_bookmarks() {
        let repo = jj_repo(ScriptedRunner::new().on(
            ["jj", "bookmark", "list"],
            Reply::ok("1\t\t\"main\"\tcmt\n"),
        ));
        assert!(repo.branch_exists("main").await.unwrap());
        let repo2 = jj_repo(ScriptedRunner::new().on(
            ["jj", "bookmark", "list"],
            Reply::ok("1\t\t\"main\"\tcmt\n"),
        ));
        assert!(!repo2.branch_exists("missing").await.unwrap());
    }

    #[tokio::test]
    async fn jj_has_uncommitted_changes_reads_empty_flag() {
        // CHANGE_TEMPLATE row: change_id \t commit_id \t empty \t description
        let dirty =
            jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("kz\t38\tfalse\t\"wip\"\n")));
        assert!(dirty.has_uncommitted_changes().await.unwrap());
        let clean =
            jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("kz\t38\ttrue\t\"\"\n")));
        assert!(!clean.has_uncommitted_changes().await.unwrap());
    }

    // M18: a conflicted-but-**empty** `@` is uncommitted state (it needs resolution),
    // so `has_uncommitted_changes` returns true — agreeing with `snapshot().dirty`,
    // which already treats `conflict ⇒ dirty`. First `jj log` = current_change (empty),
    // second = is_conflicted (`"1"`).
    #[tokio::test]
    async fn jj_has_uncommitted_changes_true_when_conflicted_even_if_empty() {
        let repo = jj_repo(ScriptedRunner::new().on_sequence(
            ["jj", "log"],
            [
                Reply::ok("kz\t38\ttrue\t\"\"\n"), // current_change: empty = true
                Reply::ok("1\n"),                  // is_conflicted: conflicted
            ],
        ));
        assert!(
            repo.has_uncommitted_changes().await.unwrap(),
            "a conflicted empty @ is dirty"
        );
    }

    #[tokio::test]
    async fn jj_changed_files_maps_diff_summary() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(["jj", "root"], Reply::ok("/repo\n"))
                .on(["jj", "diff"], Reply::ok("M src/a.rs\nA b.rs\nD gone.rs\n")),
        );
        let changes = repo.changed_files().await.unwrap();
        assert_eq!(changes.len(), 3);
        assert_eq!(changes[0].kind, ChangeKind::Modified);
        assert_eq!(changes[1].kind, ChangeKind::Added);
        assert_eq!(changes[2].kind, ChangeKind::Deleted);
        assert!(changes.iter().all(|c| c.old_path.is_none()));
    }

    // jj DOES supply the rename's original path (its `{old => new}` summary
    // form) — `old_path` is populated on both backends, as the DTO documents.
    #[tokio::test]
    async fn jj_changed_files_populates_rename_old_path() {
        let repo = jj_repo(
            ScriptedRunner::new()
                .on(["jj", "root"], Reply::ok("/repo\n"))
                .on(["jj", "diff"], Reply::ok("R src/{old.rs => new.rs}\n")),
        );
        let changes = repo.changed_files().await.unwrap();
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].kind, ChangeKind::Renamed);
        assert_eq!(changes[0].path, Path::new("src/new.rs"));
        assert_eq!(
            changes[0].old_path.as_deref(),
            Some(Path::new("src/old.rs"))
        );
    }

    // `commit_paths(&[])` is refused up front on BOTH backends: the runners have
    // no rules, so reaching the CLI would error differently — the guard must trip
    // first (on jj an empty fileset would otherwise commit the whole working
    // copy; on git it would exit 128).
    #[tokio::test]
    async fn commit_paths_refuses_an_empty_path_set() {
        for repo in [
            git_repo(ScriptedRunner::new()),
            jj_repo(ScriptedRunner::new()),
        ] {
            let err = repo
                .commit_paths(&[], "msg")
                .await
                .expect_err("empty paths must be refused");
            assert!(
                err.to_string().contains("at least one path"),
                "unexpected error: {err}"
            );
        }
    }

    // `create_branch` dispatches to `git branch <name>` (no checkout) on git and to
    // `jj bookmark create <name> -r @` (anchored on the current head, not moving it)
    // on jj.
    #[tokio::test]
    async fn create_branch_dispatches_per_backend() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .create_branch("feat")
            .await
            .unwrap();
        assert_eq!(grec.only_call().args_str(), ["branch", "feat"]);

        let jrec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .create_branch("feat")
            .await
            .unwrap();
        assert_eq!(
            jrec.only_call().args_str(),
            ["bookmark", "create", "feat", "-r", "@", "--color", "never"]
        );
    }

    // The `RefName`/`BookmarkName` newtype guards reject a leading `-` (an
    // injectable flag-like name) and an empty name — before either backend spawns
    // anything (`ScriptedRunner::new()` has no rules, so a spawn attempt would
    // panic on an unmatched command instead of hitting this assertion).
    #[tokio::test]
    async fn create_branch_rejects_invalid_name_without_spawning() {
        for repo in [
            git_repo(ScriptedRunner::new()),
            jj_repo(ScriptedRunner::new()),
        ] {
            for bad in ["-evil", ""] {
                repo.create_branch(bad)
                    .await
                    .expect_err(&format!("{bad:?} must be refused before spawning"));
            }
        }
    }

    #[tokio::test]
    async fn jj_rename_branch_builds_bookmark_rename() {
        use processkit::testing::RecordingRunner;
        let rec = RecordingRunner::replying(Reply::ok(""));
        let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
        repo.rename_branch("old", "new").await.unwrap();
        assert_eq!(
            rec.only_call().args_str(),
            ["bookmark", "rename", "old", "new", "--color", "never"]
        );
    }

    // The widened common surface dispatches `checkout` to each backend's verb:
    // git `checkout`, jj `edit`.
    #[tokio::test]
    async fn checkout_dispatches_per_backend() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .checkout("feat")
            .await
            .unwrap();
        // Trailing `--` so a path-like ref can't fall into pathspec mode (C2).
        assert_eq!(grec.only_call().args_str(), ["checkout", "feat", "--"]);

        let jrec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .checkout("feat")
            .await
            .unwrap();
        assert_eq!(
            jrec.only_call().args_str(),
            ["edit", "feat", "--color", "never"]
        );
    }

    #[tokio::test]
    async fn new_child_dispatches_per_backend() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .new_child("feat")
            .await
            .unwrap();
        assert_eq!(grec.only_call().args_str(), ["checkout", "feat", "--"]);

        let jrec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .new_child("feat")
            .await
            .unwrap();
        assert_eq!(
            jrec.only_call().args_str(),
            ["new", "feat", "--color", "never"]
        );
    }

    // A1: `delete_branch` takes a `BranchDelete` spec; `.force()` threads through to
    // git's `-D` (vs `-d`), and jj ignores it (its `bookmark delete` has no force).
    #[tokio::test]
    async fn delete_branch_spec_threads_force_to_git_only() {
        use processkit::testing::RecordingRunner;
        let forced = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&forced))
            .delete_branch(BranchDelete::new("feat").force())
            .await
            .unwrap();
        assert!(
            forced.only_call().args_str().iter().any(|a| a == "-D"),
            "force → branch -D"
        );

        let unforced = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&unforced))
            .delete_branch(BranchDelete::new("feat"))
            .await
            .unwrap();
        assert!(
            unforced.only_call().args_str().iter().any(|a| a == "-d"),
            "no force → branch -d"
        );

        let jj = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jj))
            .delete_branch(BranchDelete::new("feat").force())
            .await
            .unwrap();
        assert!(
            !jj.only_call()
                .args_str()
                .iter()
                .any(|a| a == "-D" || a == "--force"),
            "jj bookmark delete has no force flag"
        );
    }

    #[tokio::test]
    async fn fetch_branch_dispatches_per_backend() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .fetch_branch("main")
            .await
            .unwrap();
        assert!(
            grec.only_call()
                .args_str()
                .starts_with(&["fetch".to_string()])
        );

        let jrec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .fetch_branch("main")
            .await
            .unwrap();
        let args = jrec.only_call().args_str();
        assert_eq!(&args[..2], &["git", "fetch"]);
    }

    // The facade push is the honest LCD: git pushes the ref with `-u origin`,
    // jj pushes the bookmark's state with `-b`. Argv pinned on both backends.
    #[tokio::test]
    async fn push_dispatches_per_backend() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .push("feature")
            .await
            .unwrap();
        assert_eq!(
            grec.only_call().args_str(),
            ["push", "-u", "origin", "feature"]
        );

        let jrec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .push("feature")
            .await
            .unwrap();
        let args = jrec.only_call().args_str();
        // `exact:` disables jj's glob matching so a `*` can't push every bookmark (H1).
        assert_eq!(&args[..4], &["git", "push", "-b", "exact:feature"]);
    }

    // A flag-like branch is now rejected the same way on BOTH backends: the
    // facade converts the branch string into the validated newtype at the
    // boundary (`vcs_git::RefName` / `vcs_jj::BookmarkName`), so `--force` is
    // refused with a classifiable input-validation error BEFORE any process
    // spawns — no longer a per-backend difference.
    #[tokio::test]
    async fn push_flag_like_branch_rejected_before_spawn_on_both_backends() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        let err = Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .push("--force")
            .await
            .unwrap_err();
        assert!(err.is_invalid_input(), "git: got {err:?}");
        assert_eq!(grec.calls().len(), 0, "git: no process must have spawned");

        let jrec = RecordingRunner::replying(Reply::ok(""));
        let err = Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .push("--force")
            .await
            .unwrap_err();
        assert!(err.is_invalid_input(), "jj: got {err:?}");
        assert_eq!(jrec.calls().len(), 0, "jj: no process must have spawned");
    }

    #[tokio::test]
    async fn fetch_from_names_the_remote_on_both_backends() {
        use processkit::testing::RecordingRunner;
        let grec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
            .fetch_from("upstream")
            .await
            .unwrap();
        assert_eq!(
            grec.only_call().args_str(),
            ["fetch", "--quiet", "upstream"]
        );

        let jrec = RecordingRunner::replying(Reply::ok(""));
        Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
            .fetch_from("upstream")
            .await
            .unwrap();
        let args = jrec.only_call().args_str();
        // `exact:` disables jj's glob matching on the remote name (H1).
        assert_eq!(&args[..4], &["git", "fetch", "--remote", "exact:upstream"]);
    }

    // git: untracked files count as uncommitted but not as *tracked* changes.
    #[tokio::test]
    async fn git_has_tracked_changes_ignores_untracked() {
        let dirty = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok(" M a.rs\0")));
        assert!(dirty.has_tracked_changes().await.unwrap());
        // `--untracked-files=no` means git itself omits `??` entries; an empty
        // reply is what a tracked-clean tree returns.
        let clean = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok("")));
        assert!(!clean.has_tracked_changes().await.unwrap());
    }

    // jj has no untracked concept — `has_tracked_changes` follows `@`'s emptiness.
    #[tokio::test]
    async fn jj_has_tracked_changes_follows_working_copy() {
        let dirty =
            jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("kz\t38\tfalse\t\"wip\"\n")));
        assert!(dirty.has_tracked_changes().await.unwrap());
    }

    #[tokio::test]
    async fn conflicted_files_dispatches_per_backend() {
        let git =
            git_repo(ScriptedRunner::new().on(["git", "diff"], Reply::ok("a.rs\0b dir/c.rs\0")));
        assert_eq!(
            git.conflicted_files().await.unwrap(),
            [PathBuf::from("a.rs"), PathBuf::from("b dir/c.rs")]
        );

        let jj = jj_repo(
            ScriptedRunner::new().on(["jj", "resolve"], Reply::ok("a.rs    2-sided conflict\n")),
        );
        assert_eq!(
            jj.conflicted_files().await.unwrap(),
            [PathBuf::from("a.rs")]
        );
        // The benign "no conflicts" non-zero exit still reads as an empty list.
        let clean = jj_repo(ScriptedRunner::new().on(
            ["jj", "resolve"],
            Reply::fail(2, "Error: No conflicts found at this revision"),
        ));
        assert!(clean.conflicted_files().await.unwrap().is_empty());
    }

    #[test]
    fn merge_probe_is_clean() {
        assert!(MergeProbe::Clean.is_clean());
        assert!(!MergeProbe::Conflicts(vec!["a.rs".into()]).is_clean());
    }

    // git try_merge, clean: probe merge, no MERGE_HEAD afterwards (the scripted
    // git-dir doesn't exist) → no abort, `Clean`.
    #[tokio::test]
    async fn git_try_merge_reports_clean_and_skips_needless_abort() {
        use processkit::testing::RecordingRunner;
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["git", "merge"], Reply::ok("Already up to date.\n"))
                .on(["git", "rev-parse"], Reply::ok("/vcs-core-no-such-git-dir")),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        assert_eq!(repo.try_merge("other").await.unwrap(), MergeProbe::Clean);
        assert!(
            rec.calls()
                .iter()
                .all(|c| !c.args_str().contains(&"--abort".to_string())),
            "no merge to abort"
        );
    }

    // git try_merge, conflict: conflicted paths are read BEFORE the abort (abort
    // clears the unmerged index), then the merge is aborted.
    #[tokio::test]
    async fn git_try_merge_collects_conflicts_then_aborts() {
        use processkit::testing::RecordingRunner;
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                // Order matters: ["merge","--abort"] must outrank the ["merge"] rule.
                .on(["git", "merge", "--abort"], Reply::ok(""))
                .on(
                    ["git", "merge"],
                    Reply::fail(1, "CONFLICT (content): Merge conflict in a.rs"),
                )
                .on(["git", "diff"], Reply::ok("a.rs\0")),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        assert_eq!(
            repo.try_merge("other").await.unwrap(),
            MergeProbe::Conflicts(vec![PathBuf::from("a.rs")])
        );
        let calls = rec.calls();
        let diff_pos = calls.iter().position(|c| c.args_str()[0] == "diff");
        let abort_pos = calls
            .iter()
            .position(|c| c.args_str().contains(&"--abort".to_string()));
        assert!(diff_pos.unwrap() < abort_pos.unwrap(), "{calls:?}");
    }

    // git try_merge: a failing rollback must propagate, not be reported as a
    // clean/conflicted probe.
    #[tokio::test]
    async fn git_try_merge_propagates_abort_failure() {
        let tmp = TempDir::new("probe-abort");
        std::fs::write(tmp.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
        let repo = git_repo(
            ScriptedRunner::new()
                .on(
                    ["git", "merge", "--abort"],
                    Reply::fail(128, "fatal: cannot abort"),
                )
                .on(["git", "merge"], Reply::ok(""))
                .on(
                    ["git", "rev-parse"],
                    Reply::ok(tmp.path().to_str().unwrap()),
                ),
        );
        assert!(repo.try_merge("other").await.is_err());
    }

    // A thin shim over the standard `RecordingRunner`/`ScriptedRunner` that fires a
    // cancellation token the instant a command whose argv satisfies `trip` is
    // dispatched — modelling the client's `default_cancel_on` firing at a *precise*
    // point during `try_merge`. Needed because a plain scripted reply cannot express
    // this: an already-fired token short-circuits the *first* command (so the later
    // stages are never reached), and the harness has no mid-sequence hook. Choosing
    // `trip` pins the exact moment — at the rollback abort, or earlier, at the
    // in-progress probe.
    struct CancelWhen<R: ProcessRunner> {
        inner: R,
        token: CancellationToken,
        trip: fn(&processkit::Command) -> bool,
    }

    #[async_trait::async_trait]
    impl<R: ProcessRunner> ProcessRunner for CancelWhen<R> {
        async fn output_string(
            &self,
            command: &processkit::Command,
        ) -> processkit::Result<processkit::ProcessResult<String>> {
            // Fire the client token as `trip` selects. A detached cleanup command
            // (fresh token) survives it; a token-inheriting one is cancelled. Firing
            // during command N's dispatch leaves the token fired for command N+1,
            // which `ScriptedRunner` short-circuits when the token is inherited.
            if (self.trip)(command) {
                self.token.cancel();
            }
            self.inner.output_string(command).await
        }
    }

    fn arg_present(command: &processkit::Command, needle: &str) -> bool {
        command
            .arguments()
            .iter()
            .any(|a| a.to_str() == Some(needle))
    }

    // The facade `try_merge`'s rollback must survive the client's cancellation
    // firing as it reaches cleanup — the git analogue of jj's unit test
    // `rollback_to_survives_fired_cancellation`. With the fix, the cleanup
    // `merge --abort` runs on a FRESH cancel token and completes (→ `Clean`); the
    // old token-inheriting abort would be cancelled and surface `Error::Cancelled`,
    // abandoning the staged trial merge — so this test fails on the pre-fix code.
    #[tokio::test]
    async fn git_try_merge_cleanup_survives_cancellation_fired_at_rollback() {
        use processkit::testing::RecordingRunner;
        let tmp = TempDir::new("probe-cancel");
        std::fs::write(tmp.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
        let token = CancellationToken::new();
        let runner = CancelWhen {
            inner: RecordingRunner::new(
                ScriptedRunner::new()
                    .on(["git", "merge", "--abort"], Reply::ok(""))
                    .on(["git", "merge"], Reply::ok(""))
                    .on(
                        ["git", "rev-parse"],
                        Reply::ok(tmp.path().to_str().unwrap()),
                    ),
            ),
            token: token.clone(),
            // Fire exactly as the rollback abort is issued.
            trip: |c| arg_present(c, "--abort"),
        };
        let repo = Repo::from_git(
            "/repo",
            "/repo",
            Git::with_runner(&runner).default_cancel_on(token),
        );
        // The probe merge is clean and MERGE_HEAD is present, so `try_merge` reaches
        // the cleanup abort — which runs on a fresh cancel token and completes
        // despite the client cancellation the shim fires at that exact moment.
        assert_eq!(repo.try_merge("other").await.unwrap(), MergeProbe::Clean);
        // ...and the detached abort really was issued, not skipped.
        assert!(
            runner
                .inner
                .calls()
                .iter()
                .any(|c| c.args_str().contains(&"--abort".to_string())),
            "the cleanup abort must have run: {:?}",
            runner.inner.calls()
        );
    }

    // The gap R-01 caught: the cleanup DECISION — `is_merge_in_progress`, whose
    // `rev-parse --git-dir` used to inherit the client token — must also survive a
    // cancellation that fires DURING the probe, before the abort is ever reached.
    // Here the token fires as that `rev-parse --git-dir` is dispatched (the Ok
    // branch: the `--no-ff` probe merge staged a real merge, then the deadline
    // hit). With the fix the probe runs detached (fresh token) → sees MERGE_HEAD →
    // the detached abort runs → `Clean`. On the pre-fix code the probe's `?`
    // propagated `Cancelled` and the abort was skipped, abandoning the staged trial
    // merge — so this test fails there. Firing at the probe, not at `--abort`, is
    // exactly what the older `..._fired_at_rollback` test could not cover.
    #[tokio::test]
    async fn git_try_merge_cleanup_survives_cancellation_fired_at_probe() {
        use processkit::testing::RecordingRunner;
        let tmp = TempDir::new("probe-cancel-at-probe");
        std::fs::write(tmp.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
        let token = CancellationToken::new();
        let runner = CancelWhen {
            inner: RecordingRunner::new(
                ScriptedRunner::new()
                    .on(["git", "merge", "--abort"], Reply::ok(""))
                    .on(["git", "merge"], Reply::ok(""))
                    .on(
                        ["git", "rev-parse"],
                        Reply::ok(tmp.path().to_str().unwrap()),
                    ),
            ),
            token: token.clone(),
            // Fire as the in-progress probe's `rev-parse --git-dir` is dispatched —
            // strictly BEFORE the abort, unlike `..._fired_at_rollback`.
            trip: |c| arg_present(c, "--git-dir"),
        };
        let repo = Repo::from_git(
            "/repo",
            "/repo",
            Git::with_runner(&runner).default_cancel_on(token),
        );
        // Decision + abort both run on fresh tokens, so the probe still reports the
        // staged merge and the abort still undoes it despite the fired client token.
        assert_eq!(repo.try_merge("other").await.unwrap(), MergeProbe::Clean);
        assert!(
            runner
                .inner
                .calls()
                .iter()
                .any(|c| c.args_str().contains(&"--abort".to_string())),
            "cleanup abort must run even when cancellation fires at the probe: {:?}",
            runner.inner.calls()
        );
    }

    // jj try_merge: op head captured first, probe runs, op restore always runs.
    #[tokio::test]
    async fn jj_try_merge_probes_and_restores() {
        use processkit::testing::RecordingRunner;
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["jj", "op", "log"], Reply::ok("op42\n"))
                .on(["jj", "op", "restore"], Reply::ok(""))
                .on(["jj", "new"], Reply::ok(""))
                .on(["jj", "log"], Reply::ok("1\n")) // is_conflicted → true
                .on(["jj", "resolve"], Reply::ok("a.rs    2-sided conflict\n")),
        );
        let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
        assert_eq!(
            repo.try_merge("feature").await.unwrap(),
            MergeProbe::Conflicts(vec![PathBuf::from("a.rs")])
        );
        let calls = rec.calls();
        assert_eq!(calls[0].args_str()[..2], ["op", "log"]);
        assert_eq!(calls[1].args_str()[0], "new");
        let last = calls.last().unwrap().args_str();
        assert_eq!(last[..3], ["op", "restore", "op42"]);
    }

    #[tokio::test]
    async fn jj_try_merge_clean_and_restore_failure() {
        // Conflict-free probe → Clean (no resolve call needed).
        let clean = jj_repo(
            ScriptedRunner::new()
                .on(["jj", "op", "log"], Reply::ok("op42\n"))
                .on(["jj", "op", "restore"], Reply::ok(""))
                .on(["jj", "new"], Reply::ok(""))
                .on(["jj", "log"], Reply::ok("0\n")),
        );
        assert_eq!(clean.try_merge("feature").await.unwrap(), MergeProbe::Clean);

        // A failing op restore breaks the rollback guarantee → error, not Clean.
        let broken = jj_repo(
            ScriptedRunner::new()
                .on(["jj", "op", "log"], Reply::ok("op42\n"))
                .on(["jj", "op", "restore"], Reply::fail(1, "op not found"))
                .on(["jj", "new"], Reply::ok(""))
                .on(["jj", "log"], Reply::ok("0\n")),
        );
        assert!(broken.try_merge("feature").await.is_err());
    }

    // jj try_merge shares `Jj::rollback_to`'s concurrency guard: if a concurrent jj
    // process advances the op log during the trial merge (jj records a `>= 2`-parent
    // "reconcile divergent operations" merge), the rollback is REFUSED rather than
    // clobbering that work — try_merge surfaces `Error::Rollback` instead of a stale,
    // untrustworthy `Clean`, and issues no `op restore`.
    #[tokio::test]
    async fn jj_try_merge_refuses_rollback_on_op_log_divergence() {
        use processkit::testing::RecordingRunner;
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on_sequence(
                    ["jj", "op", "log"],
                    [
                        Reply::ok("op42\n"),              // capture → pre
                        Reply::ok("merge\t2\nop42\t1\n"), // probe → foreign reconcile merge
                    ],
                )
                .on(["jj", "op", "restore"], Reply::ok(""))
                .on(["jj", "new"], Reply::ok(""))
                .on(["jj", "log"], Reply::ok("0\n")),
        );
        let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
        let err = repo
            .try_merge("feature")
            .await
            .expect_err("a divergence must error, not report a stale Clean");
        assert!(
            matches!(err, Error::Rollback(vcs_jj::Rollback::SkippedDiverged)),
            "expected Error::Rollback(SkippedDiverged), got {err:?}"
        );
        assert!(
            rec.calls()
                .iter()
                .all(|c| c.args_str()[..2] != ["op", "restore"]),
            "the concurrent op must not be clobbered by a restore: {:?}",
            rec.calls()
        );
    }

    // continue_in_progress with unresolved paths reports `Conflict` and must NOT
    // attempt the continue (git would hard-error).
    #[tokio::test]
    async fn git_continue_blocked_by_conflicts_does_not_act() {
        use processkit::testing::RecordingRunner;
        let rec =
            RecordingRunner::new(ScriptedRunner::new().on(["git", "diff"], Reply::ok("a.rs\0")));
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        assert_eq!(
            repo.continue_in_progress().await.unwrap(),
            OperationState::Conflict
        );
        assert!(
            rec.calls().iter().all(|c| c.args_str()[0] == "diff"),
            "only the conflict probe may run: {:?}",
            rec.calls()
        );
    }

    // A continued rebase that stops on the NEXT patch's conflict exits non-zero;
    // continue_in_progress must report that as `Conflict`, not as an error. The
    // first conflict probe must see a clean index (else continue is blocked), the
    // post-continue probe must see the new conflict — a stateful predicate
    // sequences the two `diff` replies.
    #[tokio::test]
    async fn git_continue_maps_rebase_re_conflict() {
        use std::sync::Arc as StdArc;
        use std::sync::atomic::{AtomicBool, Ordering};
        let tmp = TempDir::new("rebase-restop");
        std::fs::create_dir_all(tmp.path().join("rebase-merge")).unwrap();
        let seen_first_diff = StdArc::new(AtomicBool::new(false));
        let flag = StdArc::clone(&seen_first_diff);
        let repo = git_repo(
            ScriptedRunner::new()
                .when(
                    move |cmd| {
                        cmd.arguments().first().and_then(|a| a.to_str()) == Some("diff")
                            && flag.swap(true, Ordering::SeqCst)
                    },
                    Reply::ok("a.rs\0"),
                )
                .on(["git", "diff"], Reply::ok(""))
                .on(
                    ["git", "rev-parse"],
                    Reply::ok(tmp.path().to_str().unwrap()),
                )
                .on(
                    ["git", "rebase", "--continue"],
                    Reply::fail(1, "CONFLICT (content): Merge conflict in a.rs"),
                ),
        );
        assert_eq!(
            repo.continue_in_progress().await.unwrap(),
            OperationState::Conflict
        );
    }

    // abort_in_progress dispatches to `merge --abort` when MERGE_HEAD is present.
    #[tokio::test]
    async fn git_abort_dispatches_on_merge_in_progress() {
        use processkit::testing::RecordingRunner;
        let tmp = TempDir::new("abort");
        std::fs::write(tmp.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(
                    ["git", "rev-parse"],
                    Reply::ok(tmp.path().to_str().unwrap()),
                )
                .on(["git", "merge", "--abort"], Reply::ok("")),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        repo.abort_in_progress().await.unwrap();
        assert!(
            rec.calls()
                .iter()
                .any(|c| c.args_str() == ["merge", "--abort"]),
            "{:?}",
            rec.calls()
        );
    }

    // git surfaces an interrupted op as on-disk state: in_progress_state returns
    // Merge when MERGE_HEAD is present and Rebase when a rebase dir is — the
    // documented asymmetry (git's conflict IS that paused state, never `Conflict`
    // from this method).
    #[tokio::test]
    async fn git_in_progress_state_maps_merge_and_rebase() {
        let merging = TempDir::new("inprog-merge");
        std::fs::write(merging.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
        let merge_repo = Repo::from_git(
            "/repo",
            "/repo",
            Git::with_runner(ScriptedRunner::new().on(
                ["git", "rev-parse"],
                Reply::ok(merging.path().to_str().unwrap()),
            )),
        );
        assert_eq!(
            merge_repo.in_progress_state().await.unwrap(),
            OperationState::Merge
        );

        let rebasing = TempDir::new("inprog-rebase");
        std::fs::create_dir_all(rebasing.path().join("rebase-merge")).unwrap();
        let rebase_repo = Repo::from_git(
            "/repo",
            "/repo",
            Git::with_runner(ScriptedRunner::new().on(
                ["git", "rev-parse"],
                Reply::ok(rebasing.path().to_str().unwrap()),
            )),
        );
        assert_eq!(
            rebase_repo.in_progress_state().await.unwrap(),
            OperationState::Rebase
        );
    }

    // T-044: the sequencer states are read from their own git-dir markers and,
    // crucially, a cherry-pick/revert marker is NOT mistaken for a merge (which
    // would then dispatch `merge --abort`). `snapshot().operation` must agree with
    // `in_progress_state`, since the watcher diffs the snapshot.
    #[tokio::test]
    async fn git_in_progress_state_maps_cherry_pick_revert_and_bisect() {
        for (marker, expected) in [
            ("CHERRY_PICK_HEAD", OperationState::CherryPick),
            ("REVERT_HEAD", OperationState::Revert),
            ("BISECT_LOG", OperationState::Bisect),
        ] {
            let gd = TempDir::new("inprog-seq");
            std::fs::write(gd.path().join(marker), "deadbeef\n").unwrap();
            let repo = Repo::from_git(
                "/repo",
                "/repo",
                // `snapshot` also runs `status --porcelain=v2 --branch`; a clean reply
                // lets it reach the operation probe. Both methods resolve the git dir
                // via `rev-parse`.
                Git::with_runner(
                    ScriptedRunner::new()
                        .on(["git", "status"], Reply::ok(""))
                        .on(["git", "rev-parse"], Reply::ok(gd.path().to_str().unwrap())),
                ),
            );
            assert_eq!(
                repo.in_progress_state().await.unwrap(),
                expected,
                "{marker} must read as {expected:?}"
            );
            assert_eq!(
                repo.snapshot().await.unwrap().operation,
                expected,
                "snapshot().operation must agree for {marker}"
            );
        }
    }

    // T-044: abort dispatches the state's OWN git command — the whole point of
    // keeping the states distinct. A cherry-pick must abort with `cherry-pick
    // --abort`, never `merge --abort`.
    #[tokio::test]
    async fn git_abort_dispatches_each_sequencer_command() {
        use processkit::testing::RecordingRunner;
        for (marker, argv) in [
            ("CHERRY_PICK_HEAD", vec!["cherry-pick", "--abort"]),
            ("REVERT_HEAD", vec!["revert", "--abort"]),
            ("BISECT_LOG", vec!["bisect", "reset"]),
        ] {
            let gd = TempDir::new("abort-seq");
            let marker_path = gd.path().join(marker);
            std::fs::write(&marker_path, "x\n").unwrap();
            // The abort command's ScriptedRunner side-effect: remove the marker so the
            // *post-call* `in_progress_state` re-probe reads `Clear`.
            let mp = marker_path.clone();
            let rec = RecordingRunner::new(
                ScriptedRunner::new()
                    .on(["git", "rev-parse"], Reply::ok(gd.path().to_str().unwrap()))
                    .when(
                        move |cmd| {
                            let a0 = cmd.arguments().first().and_then(|a| a.to_str());
                            let is_abort = matches!(a0, Some("cherry-pick" | "revert" | "bisect"));
                            if is_abort {
                                let _ = std::fs::remove_file(&mp);
                            }
                            is_abort
                        },
                        Reply::ok(""),
                    ),
            );
            let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
            assert_eq!(
                repo.abort_in_progress().await.unwrap(),
                OperationState::Clear,
                "{marker} abort must leave the repo Clear"
            );
            assert!(
                rec.calls().iter().any(|c| c.args_str() == argv),
                "{marker} must dispatch {argv:?}, got {:?}",
                rec.calls()
            );
        }
    }

    // T-044: a bisect has no continue step — `continue_in_progress` must refuse it
    // with `Error::Unsupported`, not silently report it still in progress. And no
    // git mutation may run (only the conflict probe + git-dir resolution).
    #[tokio::test]
    async fn git_continue_on_bisect_is_unsupported_and_inert() {
        use processkit::testing::RecordingRunner;
        let gd = TempDir::new("continue-bisect");
        std::fs::write(gd.path().join("BISECT_LOG"), "x\n").unwrap();
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["git", "diff"], Reply::ok("")) // no conflicted paths
                .on(["git", "rev-parse"], Reply::ok(gd.path().to_str().unwrap())),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        let err = repo
            .continue_in_progress()
            .await
            .expect_err("bisect continue must be refused");
        assert!(err.is_unsupported(), "expected Unsupported, got {err:?}");
        assert!(
            rec.calls()
                .iter()
                .all(|c| matches!(c.args_str()[0].as_str(), "diff" | "rev-parse")),
            "no git mutation may run for an unsupported continue: {:?}",
            rec.calls()
        );
    }

    // T-044: a cherry-pick that continues cleanly commits and reports the post-call
    // state; the routing calls `cherry-pick --continue`, not a merge/rebase continue.
    #[tokio::test]
    async fn git_continue_dispatches_cherry_pick_continue() {
        use processkit::testing::RecordingRunner;
        let gd = TempDir::new("continue-cp");
        let marker = gd.path().join("CHERRY_PICK_HEAD");
        std::fs::write(&marker, "x\n").unwrap();
        let mp = marker.clone();
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["git", "diff"], Reply::ok("")) // nothing conflicted → not blocked
                .on(["git", "rev-parse"], Reply::ok(gd.path().to_str().unwrap()))
                .when(
                    move |cmd| {
                        let is_cont =
                            cmd.arguments().first().and_then(|a| a.to_str()) == Some("cherry-pick");
                        if is_cont {
                            let _ = std::fs::remove_file(&mp); // completes the pick
                        }
                        is_cont
                    },
                    Reply::ok(""),
                ),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        assert_eq!(
            repo.continue_in_progress().await.unwrap(),
            OperationState::Clear
        );
        assert!(
            rec.calls()
                .iter()
                .any(|c| c.args_str() == ["cherry-pick", "--continue"]),
            "must dispatch cherry-pick --continue: {:?}",
            rec.calls()
        );
    }

    // T-065: an interrupted `git am` is driven forward with `am --continue`, not left
    // as a silent no-op. A clean continue finishes the mailbox and reports the
    // post-call `Clear`; the routing must call `am --continue`, never a rebase/merge
    // continue (an am shares `rebase-apply/` but marks it `applying`, M20).
    #[tokio::test]
    async fn git_continue_dispatches_am_continue() {
        use processkit::testing::RecordingRunner;
        let gd = TempDir::new("continue-am");
        // `git am` marks its `rebase-apply/` dir with an `applying` file.
        let apply = gd.path().join("rebase-apply");
        std::fs::create_dir_all(&apply).unwrap();
        std::fs::write(apply.join("applying"), "x\n").unwrap();
        let apply_dir = apply.clone();
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["git", "diff"], Reply::ok("")) // nothing conflicted → not blocked
                .on(["git", "rev-parse"], Reply::ok(gd.path().to_str().unwrap()))
                .when(
                    move |cmd| {
                        let is_am = cmd.arguments().first().and_then(|a| a.to_str()) == Some("am");
                        if is_am {
                            // A completed `am --continue` clears the whole
                            // `rebase-apply/` dir, so the post-call probe reads `Clear`
                            // (removing only `applying` would leave it looking like a
                            // paused apply-backend rebase).
                            let _ = std::fs::remove_dir_all(&apply_dir);
                        }
                        is_am
                    },
                    Reply::ok(""),
                ),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        assert_eq!(
            repo.continue_in_progress().await.unwrap(),
            OperationState::Clear
        );
        assert!(
            rec.calls()
                .iter()
                .any(|c| c.args_str() == ["am", "--continue"]),
            "must dispatch am --continue: {:?}",
            rec.calls()
        );
    }

    // T-065: an `am --continue` that stops on the NEXT patch's conflict exits
    // non-zero; continue_in_progress must map that to `Conflict`, not an error — the
    // same re-stop handling the rebase/cherry-pick/revert paths get. The first
    // conflict probe must see a clean index (else continue is blocked), the
    // post-continue probe the new conflict, so a stateful predicate sequences the two
    // `diff` replies (as in the rebase re-conflict test).
    #[tokio::test]
    async fn git_continue_maps_am_re_conflict() {
        use std::sync::Arc as StdArc;
        use std::sync::atomic::{AtomicBool, Ordering};
        let gd = TempDir::new("am-restop");
        let apply = gd.path().join("rebase-apply");
        std::fs::create_dir_all(&apply).unwrap();
        std::fs::write(apply.join("applying"), "x\n").unwrap();
        let seen_first_diff = StdArc::new(AtomicBool::new(false));
        let flag = StdArc::clone(&seen_first_diff);
        let repo = git_repo(
            ScriptedRunner::new()
                .when(
                    move |cmd| {
                        cmd.arguments().first().and_then(|a| a.to_str()) == Some("diff")
                            && flag.swap(true, Ordering::SeqCst)
                    },
                    Reply::ok("a.rs\0"),
                )
                .on(["git", "diff"], Reply::ok(""))
                .on(["git", "rev-parse"], Reply::ok(gd.path().to_str().unwrap()))
                .on(
                    ["git", "am", "--continue"],
                    Reply::fail(1, "CONFLICT (content): Merge conflict in a.rs"),
                ),
        );
        assert_eq!(
            repo.continue_in_progress().await.unwrap(),
            OperationState::Conflict
        );
    }

    // On an unborn git repo (no commits) diff_stat probes is_unborn and stats
    // against the empty tree instead of the unresolvable HEAD, so a fresh working
    // tree reports its additions rather than erroring. The empty-tree id is
    // resolved from git (`hash-object`), so it tracks the repo's object format
    // rather than being a hard-coded SHA-1 value.
    #[tokio::test]
    async fn git_diff_stat_unborn_uses_empty_tree() {
        use processkit::testing::RecordingRunner;
        // A SHA-256 repo's empty-tree id (64 hex): the value `hash-object` returns,
        // which `diff_stat` must then target verbatim.
        let oid = "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321";
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["git", "rev-parse"], Reply::fail(1, "")) // HEAD unborn
                .on(["git", "hash-object"], Reply::ok(format!("{oid}\n")))
                .on(
                    ["git", "diff", "--shortstat"],
                    Reply::ok(" 1 file changed, 2 insertions(+)\n"),
                ),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        let stat = repo.diff_stat().await.unwrap();
        assert_eq!(stat.insertions, 2);
        assert!(
            rec.calls()
                .iter()
                .any(|c| c.args_str() == ["diff", "--shortstat", oid, "--"]),
            "diff_stat should target the resolved empty tree on an unborn repo: {:?}",
            rec.calls()
        );
    }

    // `Repo::diff` on git dispatches to `GitApi::diff(DiffSpec::WorkingTree)` — the
    // same argv shape `diff_text_builds_working_tree_args` pins in `vcs-git`
    // (`is_unborn` probe, then `diff HEAD --no-color --no-ext-diff -M
    // --src-prefix=a/ --dst-prefix=b/ --`) — and parses the git-format output into
    // `FileDiff`s.
    #[tokio::test]
    async fn git_diff_dispatches_working_tree_diff() {
        use processkit::testing::RecordingRunner;
        let out = "diff --git a/m b/m\n--- a/m\n+++ b/m\n@@ -1 +1 @@\n-a\n+b\n";
        let rec = RecordingRunner::new(
            ScriptedRunner::new()
                .on(["git", "rev-parse"], Reply::ok("deadbeef\n")) // HEAD resolves
                .on(["git", "diff"], Reply::ok(out)),
        );
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        let files = repo.diff().await.unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].path, Path::new("m"));
        assert_eq!(files[0].change, ChangeKind::Modified);
        assert!(
            rec.calls().iter().any(|c| c.args_str()
                == [
                    "diff",
                    "HEAD",
                    "--no-color",
                    "--no-ext-diff",
                    "-M",
                    "--src-prefix=a/",
                    "--dst-prefix=b/",
                    "--",
                ]),
            "diff should target HEAD (working tree, same scope as diff_stat): {:?}",
            rec.calls()
        );
    }

    // On an unborn git repo `Repo::diff` targets the resolved empty tree instead of
    // the unresolvable `HEAD` — same fallback `diff_stat` uses, exercised here
    // through `GitApi::diff`'s own internal `is_unborn` probe rather than the
    // manual one `git_backend::diff_stat` performs.
    #[tokio::test]
    async fn git_diff_unborn_uses_empty_tree() {
        let oid = "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321";
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "rev-parse"], Reply::fail(1, "")) // HEAD unborn
                .on(["git", "hash-object"], Reply::ok(format!("{oid}\n")))
                .on(["git", "diff", oid], Reply::ok("")),
        );
        let files = repo.diff().await.unwrap();
        assert!(files.is_empty());
    }

    // `Repo::diff` on jj dispatches to `JjApi::diff(DiffSpec::WorkingTree)`, which
    // targets `@` (vs its parent) — the same scope `diff_stat` targets via
    // `rev("@")` — and parses the `--git`-format output into `FileDiff`s.
    #[tokio::test]
    async fn jj_diff_dispatches_at_change() {
        use processkit::testing::RecordingRunner;
        let out = "diff --git a/m b/m\n--- a/m\n+++ b/m\n@@ -1 +1 @@\n-a\n+b\n";
        let rec = RecordingRunner::new(ScriptedRunner::new().on(["jj", "diff"], Reply::ok(out)));
        let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
        let files = repo.diff().await.unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].path, Path::new("m"));
        assert_eq!(files[0].change, ChangeKind::Modified);
        assert!(
            rec.calls()
                .iter()
                .any(|c| c.args_str() == ["diff", "-r", "@", "--git", "--color", "never"]),
            "diff should target @ vs its parent (same scope as diff_stat): {:?}",
            rec.calls()
        );
    }

    // R-01 (T-068 review): `Repo::diff()` must stay consistent with the already-
    // shipped `Repo::diff_stat()` precedent on jj's working-copy-snapshot
    // behaviour — neither passes `--ignore-working-copy`, so both let jj snapshot
    // the working copy (record an operation) exactly the same way. Pinned
    // hermetically so the two can't silently drift apart (e.g. one gaining the
    // flag while the other doesn't, which would make the "same scope as
    // diff_stat" doc claim false). See `Repo::diff`/`Repo::diff_stat`'s rustdoc
    // for why `--ignore-working-copy` is deliberately not used by either: it would
    // make the read blind to any not-yet-snapshotted working-tree edit.
    #[tokio::test]
    async fn jj_diff_and_diff_stat_snapshot_the_working_copy_consistently() {
        use processkit::testing::RecordingRunner;

        let diff_rec =
            RecordingRunner::new(ScriptedRunner::new().on(["jj", "diff"], Reply::ok("")));
        let diff_repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&diff_rec));
        diff_repo.diff().await.unwrap();

        let stat_rec = RecordingRunner::new(
            ScriptedRunner::new().on(["jj", "diff"], Reply::ok("0 files changed\n")),
        );
        let stat_repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&stat_rec));
        stat_repo.diff_stat().await.unwrap();

        for (name, rec) in [("diff", &diff_rec), ("diff_stat", &stat_rec)] {
            assert!(
                rec.calls()
                    .iter()
                    .all(|c| !c.args_str().iter().any(|a| a == "--ignore-working-copy")),
                "{name} must let jj snapshot the working copy (no --ignore-working-copy), \
                 consistent with its sibling: {:?}",
                rec.calls()
            );
        }
    }

    // `Repo::log` on git maps `GitApi::log`'s typed `Commit` (hash/author/date/
    // subject) onto the facade `Commit`, with author/date populated.
    #[tokio::test]
    async fn git_log_maps_commit_fields() {
        let repo = git_repo(ScriptedRunner::new().on(
            ["git", "log"],
            Reply::ok("deadbeef\u{1f}dead\u{1f}Jane\u{1f}2026-05-31T10:00:00+00:00\u{1f}Fix bug\0"),
        ));
        let commits = repo.log("HEAD", 10).await.unwrap();
        assert_eq!(commits.len(), 1);
        assert_eq!(commits[0].id, "deadbeef");
        assert_eq!(commits[0].description, "Fix bug");
        assert_eq!(commits[0].author.as_deref(), Some("Jane"));
        assert_eq!(
            commits[0].date.as_deref(),
            Some("2026-05-31T10:00:00+00:00")
        );
    }

    // `Repo::log` on jj maps `JjApi::log`'s typed `Change` (change-id/commit-id/
    // empty/description) onto the facade `Commit` — author/date stay `None`, since
    // jj's typed log doesn't surface them.
    #[tokio::test]
    async fn jj_log_maps_change_with_no_author_or_date() {
        let repo = jj_repo(ScriptedRunner::new().on(
            ["jj", "log"],
            Reply::ok("kztuxlro\t38e00654\tfalse\t\"wip\"\n"),
        ));
        let commits = repo.log("@", 10).await.unwrap();
        assert_eq!(commits.len(), 1);
        assert_eq!(commits[0].id, "38e00654");
        assert_eq!(commits[0].description, "wip");
        assert_eq!(commits[0].author, None);
        assert_eq!(commits[0].date, None);
    }

    // `Repo::annotate` maps git blame's richer per-line metadata into the common
    // DTO and forwards the optional revspec before the pathspec separator.
    #[tokio::test]
    async fn git_annotate_maps_blame_and_forwards_revspec() {
        use processkit::testing::RecordingRunner;

        let sha = "a".repeat(40);
        let rec = RecordingRunner::replying(Reply::ok(format!(
            "{sha} 3 7 1\nauthor Jane\nauthor-time 1717700000\nauthor-tz +0200\n\tlet x = 1;\n"
        )));
        let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
        let lines = repo.annotate("src/lib.rs", Some("HEAD~1")).await.unwrap();

        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0].id, sha);
        assert_eq!(lines[0].line, 7);
        assert_eq!(lines[0].content, "let x = 1;");
        assert_eq!(lines[0].author.as_deref(), Some("Jane"));
        assert_eq!(lines[0].date, Some(1_717_700_000));
        assert_eq!(
            rec.only_call().args_str(),
            ["blame", "--line-porcelain", "HEAD~1", "--", "src/lib.rs"]
        );
    }

    // jj's annotation has the common id/line/content fields but no author/date;
    // it takes a plain path after `--` and keeps jj's default snapshotting mode.
    #[tokio::test]
    async fn jj_annotate_maps_annotation_and_forwards_revset() {
        use processkit::testing::RecordingRunner;

        let rec = RecordingRunner::replying(Reply::ok("kz\tline one\n"));
        let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
        let lines = repo.annotate("src/lib.rs", Some("@-")).await.unwrap();

        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0].id, "kz");
        assert_eq!(lines[0].line, 1);
        assert_eq!(lines[0].content, "line one");
        assert_eq!(lines[0].author, None);
        assert_eq!(lines[0].date, None);
        let args = rec.only_call().args_str();
        assert_eq!(&args[..4], ["file", "annotate", "-r", "@-"]);
        assert_eq!(&args[args.len() - 2..], ["--", "src/lib.rs"]);
        assert!(
            !args.iter().any(|arg| arg == "--ignore-working-copy"),
            "annotate must use jj's live, snapshotting working-copy mode: {args:?}"
        );
    }

    // `Repo::show_file` on git dispatches to `GitApi::show_file` and forwards its
    // content verbatim.
    #[tokio::test]
    async fn git_show_file_dispatches_to_git_backend() {
        let repo = git_repo(ScriptedRunner::new().on(["git", "show"], Reply::ok("fn main() {}\n")));
        let content = repo.show_file("HEAD", "src/main.rs").await.unwrap();
        assert_eq!(content, "fn main() {}\n");
    }

    // `Repo::show_file` on jj dispatches to `JjApi::file_show` and forwards its
    // content verbatim.
    #[tokio::test]
    async fn jj_show_file_dispatches_to_jj_backend() {
        let repo =
            jj_repo(ScriptedRunner::new().on(["jj", "file", "show"], Reply::ok("fn main() {}\n")));
        let content = repo.show_file("@-", "src/main.rs").await.unwrap();
        assert_eq!(content, "fn main() {}\n");
    }

    // On jj, abort/continue are reporting no-ops (nothing is ever paused).
    #[tokio::test]
    async fn jj_abort_and_continue_are_reporting_noops() {
        let conflicted = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("1\n")));
        assert_eq!(
            conflicted.abort_in_progress().await.unwrap(),
            OperationState::Conflict
        );
        let clear = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("0\n")));
        assert_eq!(
            clear.continue_in_progress().await.unwrap(),
            OperationState::Clear
        );
    }

    // jj records conflicts on the change; the facade maps that to `Conflict`.
    #[tokio::test]
    async fn jj_in_progress_state_maps_conflict() {
        let conflicted = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("1\n")));
        assert_eq!(
            conflicted.in_progress_state().await.unwrap(),
            OperationState::Conflict
        );
        let clear = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("0\n")));
        assert_eq!(
            clear.in_progress_state().await.unwrap(),
            OperationState::Clear
        );
    }

    // `&dyn VcsRepo` must dispatch through the real inherent methods (a delegating
    // body that recursed would stack-overflow here instead of returning).
    #[tokio::test]
    async fn vcs_repo_trait_object_dispatches() {
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "symbolic-ref"], Reply::ok("main\n"))
                .on(["git", "show-ref"], Reply::ok("")),
        );
        let dynamic: &dyn VcsRepo = &repo;
        assert_eq!(dynamic.kind(), BackendKind::Git);
        assert_eq!(
            dynamic.current_branch().await.unwrap().as_deref(),
            Some("main")
        );
        // Exercise a reference-argument async method through `&dyn` — pins the
        // async_trait lifetime capture the macro relies on (no-arg calls don't).
        assert!(dynamic.branch_exists("main").await.unwrap());
    }

    // When the backend has no native trunk (git `origin/HEAD` unset), the facade
    // falls back to a local `main`, then `master`.
    #[tokio::test]
    async fn trunk_falls_back_to_main() {
        let repo = git_repo(
            ScriptedRunner::new()
                .on(["git", "symbolic-ref"], Reply::fail(1, "")) // origin/HEAD unset → None
                .on(["git", "show-ref"], Reply::ok("")), // branch_exists("main") → exit 0
        );
        assert_eq!(repo.trunk().await.unwrap().as_deref(), Some("main"));
    }

    #[test]
    fn error_classifiers_recognise_markers() {
        let conflict = Error::Vcs(processkit::Error::exit(
            "git",
            1,
            "CONFLICT (content): Merge conflict in a.rs",
            "",
        ));
        assert!(conflict.is_merge_conflict());
        assert!(!conflict.is_nothing_to_commit());
        // A non-Vcs error classifies as none of them.
        assert!(!Error::NotARepository("/x".into()).is_merge_conflict());
    }
}

// Long-form how-to guides, rendered from this crate's docs/*.md on docs.rs.
#[doc = include_str!("../docs/core.md")]
#[allow(rustdoc::broken_intra_doc_links)]
pub mod guide {
    #[doc = include_str!("../docs/cookbook.md")]
    #[allow(rustdoc::broken_intra_doc_links)]
    pub mod cookbook {}
    #[doc = include_str!("../docs/process-model.md")]
    #[allow(rustdoc::broken_intra_doc_links)]
    pub mod process_model {}
    #[doc = include_str!("../docs/positioning.md")]
    #[allow(rustdoc::broken_intra_doc_links)]
    pub mod positioning {}
    #[doc = include_str!("../docs/stability.md")]
    #[allow(rustdoc::broken_intra_doc_links)]
    pub mod stability {}
}