git-loom 0.25.0

A Git CLI tool that weaves together multiple feature branches into integration branches
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
//! Interactive status TUI (`loom tui`): the status tree on the left, the diff
//! of the item under the cursor on the right.
//!
//! Actions (commit, fold, move, split, branch, drop, absorb, reword) run the regular loom
//! command on a worker thread while the TUI stays up: the command's prompts
//! become popups and its messages a log (`core::ui`), and only an editor takes
//! the terminal over. Fold, move and commit pick their target in a second step
//! inside the tree; `C`, `F` and `S` pick their hunks first, in the hunk selector, which
//! runs before any action, as a nested shell on the same terminal.

use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::thread::JoinHandle;
use std::time::Duration;

use anyhow::Result;
use crossterm::event::{KeyCode, KeyModifiers, MouseButton, MouseEventKind};
use ratatui::{
    Frame,
    layout::{Position, Rect},
    style::Modifier,
    text::{Line, Span},
    widgets::ListItem,
};

use crate::core::graph::{self, Section};
use crate::core::hunk_select::{self, HunkArgs};
use crate::core::repo::{self, BranchInfo, CommitInfo, FileChange, RemoteStatus, RepoInfo};
use crate::core::shortid::IdAllocator;
use crate::core::staging;
use crate::core::transaction;
use crate::core::ui::{self, Answer, Cancelled, Level, Request};
use crate::git;
use crate::tui::hunk_selector::{FileEntry, run_hunk_selector_nested};
use crate::tui::shell::{KeyResult, PaneId, Shell, ShellApp, ShellConfig, Tick};
use crate::tui::status_tree::{
    self, LOCAL_CHANGES_KEY, PENDING_COMMIT_OID, Row, RowKind, RowMark, SelectionClass, branch_key,
    commit_file_key, working_file_key,
};
use crate::tui::theme::TuiTheme;
use crate::tui::widgets::common::{colorize_diff, pane_block};
use crate::tui::widgets::diff_pane::DiffPane;
use crate::tui::widgets::list_pane::ListPane;
use crate::tui::widgets::popup::{self, LogEntry, Notice, Prompt, PromptOutcome, TextField};
use crate::{absorb, branch, commit, drop, fold, reword, split};

// ── Data model ───────────────────────────────────────────────────────────

/// Everything gathered from the repo for one TUI round.
struct Snapshot {
    workdir: PathBuf,
    git_dir: PathBuf,
    cwd_prefix: String,
    /// Kept as gathered (hidden branches removed) so the graph can be rebuilt
    /// with a branch that does not exist yet.
    info: RepoInfo,
    ids: IdAllocator,
}

/// Placeholder name of a branch being created. Git rejects a ref component
/// starting with a dot, so it clashes with no real branch; keeping it
/// non-empty keeps it distinct from "no branch here" sentinels in `graph`.
const NEW_BRANCH_NAME: &str = ".new";

/// Where `c` puts its commit: loose on the integration line (`-i`) or at the
/// tip of a woven branch (`-b`).
#[derive(Debug, Clone, PartialEq, Eq)]
enum CommitDest {
    Integration,
    Branch(String),
}

/// A row that exists only on screen, or one drawn where it is not: a branch
/// being named, a commit being placed, or a commit being moved.
enum Preview {
    Branch(BranchInfo),
    Commit { dest: CommitDest, file_count: usize },
    Move { oid: git2::Oid, slot: MoveSlot },
}

/// Where `m` puts its commit, as `loom fold` spells it (Spec 007).
#[derive(Debug, Clone, PartialEq, Eq)]
enum MoveSlot {
    /// Where it already is: the walk starts here, and nothing runs.
    Stay,
    /// `--above <commit>`.
    Above(git2::Oid),
    /// `--below <commit>`.
    Below(git2::Oid),
    /// `fold <commit> <branch>`: the branch's tip, advancing only that name.
    /// Never shown as a branch pick: it is the one way to reach an empty
    /// branch, or to split a co-located group.
    Branch(String),
}

impl Snapshot {
    /// The graph sections with `preview` faked in — a branch at its tip, or a
    /// commit at its destination — so the tree shows where it will land:
    /// split ownership, co-located names, stacking and all.
    ///
    /// A commit being placed or moved keeps every branch where the real tree
    /// draws it, so one it fills or empties does not jump between the empty
    /// and the owning groups under `↑`/`↓`; the tree regroups once it runs.
    fn sections(&self, preview: Option<Preview>) -> Vec<Section> {
        let mut info = self.info.clone();
        let pinned = match preview {
            Some(Preview::Branch(pending)) => {
                info.branches.push(pending);
                false
            }
            Some(Preview::Commit { dest, file_count }) => {
                place_pending_commit(&mut info, &dest, file_count);
                true
            }
            Some(Preview::Move { oid, slot }) => {
                place_moved_commit(&mut info, oid, &slot);
                true
            }
            None => false,
        };
        let sections = graph::build_sections(info);
        if pinned {
            keep_branch_places(&graph::build_sections(self.info.clone()), sections)
        } else {
            sections
        }
    }

    /// Whether `files` (short IDs) is the `zz` that stands for every change.
    fn is_all_changes(&self, files: &[String]) -> bool {
        files.iter().any(|f| f == self.ids.get_unstaged())
    }

    /// How many files the index holds a change for — what a `C` commit takes.
    fn staged_count(&self) -> usize {
        self.info
            .working_changes
            .iter()
            .filter(|c| is_staged(c))
            .count()
    }
}

/// Reorder the branch sections of `preview` into the order `base` draws them
/// in. Each is ranked by the first of its names `base` places, one it lacks
/// by the section before it; the sort is stable, so a group or stack a
/// preview splits keeps its own order. An empty group is split by place.
fn keep_branch_places(base: &[Section], mut preview: Vec<Section>) -> Vec<Section> {
    let place: HashMap<&str, usize> = base
        .iter()
        .enumerate()
        .flat_map(|(i, s)| match s {
            Section::Branch { names, .. } => names.iter().map(|(n, _)| (n.as_str(), i)).collect(),
            _ => Vec::new(),
        })
        .collect();
    let is_branch = |s: &Section| matches!(s, Section::Branch { .. });
    let Some(start) = preview.iter().position(is_branch) else {
        return preview;
    };
    let end = start + preview[start..].iter().take_while(|s| is_branch(s)).count();
    let taken: Vec<Section> = preview.drain(start..end).collect();
    let rank_of = |name: &str| place.get(name).copied();
    let mut rank = 0;
    let mut run: Vec<(usize, Section)> = Vec::new();
    for section in taken {
        let Section::Branch { names, commits } = section else {
            unreachable!("a run of branch sections");
        };
        if !commits.is_empty() {
            rank = names
                .iter()
                .filter_map(|(n, _)| rank_of(n))
                .min()
                .unwrap_or(rank);
            run.push((rank, Section::Branch { names, commits }));
            continue;
        }
        // A branch the preview empties is co-located with those at the base,
        // but keeps its own place: nothing is drawn under the group anyway.
        for (name, remote) in names {
            rank = rank_of(&name).unwrap_or(rank);
            match run.last_mut() {
                Some((r, Section::Branch { names, commits }))
                    if *r == rank && commits.is_empty() =>
                {
                    names.push((name, remote))
                }
                _ => run.push((
                    rank,
                    Section::Branch {
                        names: vec![(name, remote)],
                        commits: Vec::new(),
                    },
                )),
            }
        }
    }
    run.sort_by_key(|(rank, _)| *rank);
    preview.splice(start..start, run.into_iter().map(|(_, s)| s));
    preview
}

/// Insert the pending commit into `info` where `loom commit` would put it:
/// newest on the integration line, or at the branch's tip with the branch
/// advanced onto it and whatever was stacked on that tip re-parented, so the
/// ownership walk in `graph` draws the stack as the relocation will leave it.
fn place_pending_commit(info: &mut RepoInfo, dest: &CommitDest, file_count: usize) {
    let oid = PENDING_COMMIT_OID;
    let mut pending = CommitInfo {
        oid,
        short_id: String::new(),
        message: format!(
            "new commit ({} file{})",
            file_count,
            if file_count == 1 { "" } else { "s" }
        ),
        parent_oid: None,
        change_id: None,
        files: Vec::new(),
    };
    match dest {
        CommitDest::Integration => {
            pending.parent_oid = Some(
                info.commits
                    .first()
                    .map_or(info.upstream.merge_base_oid, |c| c.oid),
            );
            info.commits.insert(0, pending);
        }
        // Every destination was read off a drawn branch row of this same
        // snapshot, so the branch is there.
        CommitDest::Branch(name) => push_onto_branch(info, pending, name),
    }
}

/// Put `commit` on top of branch `name`, advancing that name alone, with
/// whatever was stacked on the old tip re-parented onto it. A no-op when the
/// branch is not there.
fn push_onto_branch(info: &mut RepoInfo, mut commit: CommitInfo, name: &str) {
    let Some(branch) = info.branches.iter_mut().find(|b| b.name == name) else {
        return;
    };
    let oid = commit.oid;
    let tip = branch.tip_oid;
    branch.tip_oid = oid;
    commit.parent_oid = Some(tip);
    // A tip outside the range is the base: the branches forking from it stay
    // parallel, only a stack on an in-range tip follows.
    let at = info.commits.iter().position(|c| c.oid == tip);
    if at.is_some() {
        reparent_children(info, tip, oid);
    }
    info.commits
        .insert(at.unwrap_or(info.commits.len()), commit);
}

fn reparent_children(info: &mut RepoInfo, from: git2::Oid, to: git2::Oid) {
    for commit in &mut info.commits {
        if commit.parent_oid == Some(from) {
            commit.parent_oid = Some(to);
        }
    }
}

/// Move commit `oid` of `info` to `slot`, as `loom fold` would (Spec 007):
/// taken out, its children and the branches ending at it fall back onto its
/// parent; `--above` hands it the target's branches, `--below` leaves them.
fn place_moved_commit(info: &mut RepoInfo, oid: git2::Oid, slot: &MoveSlot) {
    let anchor_known = |x: &git2::Oid| *x != oid && info.commits.iter().any(|c| c.oid == *x);
    let known = match slot {
        MoveSlot::Stay => false,
        MoveSlot::Above(x) | MoveSlot::Below(x) => anchor_known(x),
        MoveSlot::Branch(name) => info.branches.iter().any(|b| b.name == *name),
    };
    let Some(from) = info
        .commits
        .iter()
        .position(|c| c.oid == oid)
        .filter(|_| known)
    else {
        return;
    };
    let mut moved = info.commits.remove(from);
    if let Some(parent) = moved.parent_oid {
        reparent_children(info, oid, parent);
        for branch in &mut info.branches {
            if branch.tip_oid == oid {
                branch.tip_oid = parent;
            }
        }
    }
    let position = |info: &RepoInfo, x: git2::Oid| {
        info.commits
            .iter()
            .position(|c| c.oid == x)
            .expect("checked above")
    };
    match slot {
        MoveSlot::Stay => {}
        MoveSlot::Above(x) => {
            let at = position(info, *x);
            reparent_children(info, *x, oid);
            for branch in &mut info.branches {
                if branch.tip_oid == *x {
                    branch.tip_oid = oid;
                }
            }
            moved.parent_oid = Some(*x);
            info.commits.insert(at, moved);
        }
        MoveSlot::Below(x) => {
            let at = position(info, *x);
            moved.parent_oid = info.commits[at].parent_oid;
            info.commits[at].parent_oid = Some(oid);
            info.commits.insert(at + 1, moved);
        }
        MoveSlot::Branch(name) => push_onto_branch(info, moved, name),
    }
}

/// What a commit being placed takes with it.
#[derive(Debug, PartialEq, Eq)]
enum CommitSource {
    /// Working files, passed as arguments (`c`): the index decides nothing.
    Files(Vec<String>),
    /// Whatever the hunk selector staged (`C`), committed as it stands.
    Index,
}

/// What folding the sources into a target does, tagged on the tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FoldEffect {
    /// Working changes or a fixup go into the target commit.
    Amend,
    /// The target is a source, or the commit a source file already sits in.
    Noop,
    /// The source commit or commit file goes back to the working tree.
    Uncommit,
    /// A commit file, or a commit's picked hunks, move into the target commit.
    MoveFile,
}

impl FoldEffect {
    fn tag(self) -> &'static str {
        match self {
            FoldEffect::Amend => "[AMEND]",
            FoldEffect::Noop => "[NOOP]",
            FoldEffect::Uncommit => "[UNCOMMIT]",
            FoldEffect::MoveFile => "[MOVE]",
        }
    }
}

/// A row a pending fold can land on.
#[derive(Debug, Clone)]
struct FoldTarget {
    key: String,
    /// The row's command argument.
    arg: String,
    effect: FoldEffect,
}

/// A loom command to run on the worker thread.
#[derive(Debug, PartialEq, Eq)]
enum Action {
    /// `loom commit -i|-b <branch> [files...]`. `c` always names files, so
    /// the index decides nothing; only `C`, which just staged what it picked,
    /// commits the index (no file arguments).
    Commit {
        source: CommitSource,
        dest: CommitDest,
    },
    /// `loom fold [-p] <sources...> <target> [--hunks <id>... --hunks-from
    /// <fingerprint>]`: the hunks, when some, are picked out of the one
    /// source commit.
    Fold {
        sources: Vec<String>,
        target: String,
        hunks: Option<HunkArgs>,
    },
    /// `loom fold -p <files...> <target>` with the working-tree hunks already
    /// picked; `sources` are the files or `zz` the picker was opened on,
    /// `stamp` the pick's `staging::binary_stamp`.
    FoldHunks {
        sources: Vec<String>,
        picked: Vec<FileEntry>,
        stamp: Stamp,
        target: String,
    },
    /// `loom fold <commit> --above|--below <commit>`, or `loom fold <commit>
    /// <branch>`; never `MoveSlot::Stay`.
    Move { commit: git2::Oid, slot: MoveSlot },
    /// `loom split <commit> [files...]`, or `loom split -p <commit> --hunks
    /// <id>... --hunks-from <fingerprint>` with the hunks `S` picked. `files`
    /// are cwd-relative, as the CLI takes them.
    Split {
        commit: String,
        files: Vec<String>,
        hunks: Option<HunkArgs>,
    },
    /// `loom branch new <name> [-t target]`.
    NewBranch {
        name: String,
        target: Option<String>,
    },
    /// `loom drop <targets...>`: one commit, branch, or `zz`, or several files.
    Drop { targets: Vec<String> },
    /// `loom absorb [files...]`; no files is every local change.
    Absorb { files: Vec<String> },
    /// `loom reword <target>`; `name` is the new branch name typed in the
    /// tree, `None` for a commit (the editor asks for the message).
    Reword {
        target: String,
        name: Option<String>,
    },
}

/// Why the event loop returned.
enum Outcome {
    Quit,
}

/// Input mode: normal, picking the target of a pending fold, placing a
/// pending commit, or typing a branch name over its row (an existing
/// branch's, or a placeholder row for a branch about to be created).
enum Mode {
    Normal,
    /// `↑`/`↓` move the cursor through `targets`, read off the tree `f` was
    /// pressed on; the tree is rebuilt for `targets[index]`.
    FoldTarget {
        sources: Vec<String>,
        source_rows: Sources,
        targets: Vec<FoldTarget>,
        index: usize,
        /// Key of the row `f` or `F` was pressed on, to go back to on cancel.
        origin: String,
        /// What `F` picked; `None` for `f`, which folds its sources whole.
        hunks: Option<FoldHunks>,
    },
    /// `↑`/`↓` move the placeholder commit through `dests`; the tree is
    /// rebuilt with it at `dests[index]`.
    CommitTarget {
        source: CommitSource,
        source_rows: Sources,
        dests: Vec<CommitDest>,
        index: usize,
        /// Key of the row `c` was pressed on, to go back to on cancel.
        origin: String,
    },
    /// `↑`/`↓` move `commit` through `slots`, each a distinct tree; the tree
    /// is rebuilt with it at `slots[index]`.
    MoveTarget {
        commit: git2::Oid,
        slots: Vec<MoveSlot>,
        index: usize,
    },
    RenameBranch {
        branch: String,
        field: TextField,
    },
    NewBranch {
        target: Option<String>,
        /// Commit the branch will point at; the fake branch is drawn there.
        tip: git2::Oid,
        /// Key of the row `b` was pressed on, to go back to on cancel.
        origin: String,
        field: TextField,
    },
}

/// The rows a pending fold or commit takes its content from, marked in the
/// gutter for as long as its target is being picked. Row keys rather than
/// command arguments: keys survive the rebuilds `↑`/`↓` trigger, and a row
/// subsumed by `zz` or by the index carries no argument of its own.
#[derive(Default)]
struct Sources {
    /// Rows the command names.
    named: HashSet<String>,
    /// Rows a named source subsumes: the files under a `zz` header, or the
    /// staged files `C` commits through the index.
    covered: HashSet<String>,
}

/// Whether the index holds something of `change` for a `C` commit to take.
/// The one spelling of it: the placeholder's file count and the rows marked
/// as covered must not be able to disagree.
fn is_staged(change: &FileChange) -> bool {
    matches!(change.index, 'A' | 'M' | 'D' | 'R')
}

/// Where a pick stands after one poll.
enum PickTick {
    Nothing,
    Redraw,
    /// `first` on the poll that read it, so a held pick redraws once rather
    /// than every tick with nothing animating.
    Ready {
        first: bool,
    },
}

/// What a pick's selection goes to.
enum PickFor {
    /// `C`: staged at once, then committed.
    Commit,
    /// `F`: folded into one of `targets`, read off the tree at the press so a
    /// pick with nowhere to go never opens. `commit` is the source commit when
    /// the hunks are its own rather than the working tree's.
    Fold {
        sources: Vec<String>,
        targets: Vec<FoldTarget>,
        commit: Option<git2::Oid>,
    },
    /// `S`: the hunks of `commit` that make the first of its two halves.
    Split { commit: git2::Oid },
}

impl PickFor {
    /// The command the pick is for, as notices and the status bar name it.
    fn label(&self) -> &'static str {
        match self {
            PickFor::Commit => "commit",
            PickFor::Fold { .. } => "fold",
            PickFor::Split { .. } => "split",
        }
    }
}

/// The two halves of a `C` or `F` pick. Reading the hunks is a git per changed
/// file, so it waits on a worker like any other slow work; only the selector
/// that follows needs the terminal, and only that half runs on the loop.
enum Pick {
    Collecting {
        handle: JoinHandle<Result<(Vec<FileEntry>, Stamp)>>,
        /// Key of the row `C` or `F` was pressed on, to go back to on cancel.
        origin: String,
        purpose: PickFor,
        /// Ticks so far, for the status-bar animation.
        ticks: usize,
        /// `Esc` was pressed: drop the result when it lands. The pick is held
        /// until then rather than abandoned, so no action can start beside a
        /// worker still running `git` over the same repository.
        cancelled: bool,
    },
    /// Read and waiting for the shell to hand the terminal over.
    Ready {
        entries: Vec<FileEntry>,
        stamp: Stamp,
        origin: String,
        purpose: PickFor,
    },
}

/// `staging::binary_stamp` of a pick's entries, taken as they are read; empty
/// for a pick that stages at once or reads a commit.
type Stamp = Vec<Option<git2::Oid>>;

/// The hunks `F` picked, folded instead of its source rows whole.
enum FoldHunks {
    /// Working-tree hunks, staged only once the target is confirmed, so a
    /// cancelled fold leaves the index as it was; `stamp` lets the fold
    /// refuse a pick the tree has moved away from by then.
    Worktree {
        picked: Vec<FileEntry>,
        stamp: Stamp,
    },
    /// Hunks of the source commit, handed to `loom fold -p` by id (Spec 019).
    Commit(Vec<FileEntry>),
}

/// The action currently running on its worker thread.
struct Running {
    handle: JoinHandle<Result<()>>,
    command: String,
    /// Text of the command's active spinner, if any.
    spinner: Option<String>,
    /// Ticks so far, for the status-bar animation.
    ticks: usize,
}

/// What is drawn over the panes and owns the keyboard.
enum Popup {
    /// A prompt from the running command; the answer goes back on `reply`.
    Prompt {
        prompt: Prompt,
        reply: Sender<Option<Answer>>,
    },
    /// A message to dismiss; `then` runs on dismissal.
    Notice {
        notice: Notice,
        then: AfterNotice,
    },
    Log {
        scroll: DiffPane,
    },
    Help {
        scroll: DiffPane,
    },
}

/// The `?` popup: everything the status bar leaves out.
const HELP: &[popup::HelpSection] = &[
    (
        "Navigation",
        &[
            (
                "↑/k  ↓/j",
                "move the cursor; scroll the diff when it has focus",
            ),
            (
                "←/h  →/l",
                "close / open a row; ← on a file walks up to its commit",
            ),
            ("Enter", "toggle a row open or closed"),
            ("PgUp/PgDn", "scroll the diff by a page"),
            ("Tab", "switch pane"),
            ("Ctrl-←/→", "narrow / widen the left pane"),
            ("+/-", "one more / one fewer context commit before the base"),
            ("Space", "select / unselect a row"),
            ("Esc", "cancel a pick or a mode, else clear the selection"),
        ],
    ),
    (
        "Actions (on the selection, else the cursor row)",
        &[
            ("c / C", "commit files / commit picked hunks"),
            ("f / F", "fold into a commit / fold picked hunks"),
            ("m", "move a commit above or below another"),
            ("s / S", "split a commit by file / by hunk"),
            ("b", "new branch at the cursor"),
            ("d", "drop"),
            ("a", "absorb into the commits that touched the same lines"),
            ("r", "reword a commit, or rename a branch"),
        ],
    ),
    (
        "Other",
        &[
            ("L", "action log"),
            ("R / F5", "reload the tree"),
            ("?", "this help"),
            ("q / Ctrl-C", "quit"),
        ],
    ),
];

enum AfterNotice {
    Nothing,
    /// The action failed: reload the tree to show whatever it left.
    Reload,
    /// The action paused on conflicts: nothing else can run, so leave.
    Quit,
}

const SPINNER_FRAMES: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];

// ── Entry point ──────────────────────────────────────────────────────────

/// Run the interactive status TUI.
pub fn run(theme: graph::Theme) -> Result<()> {
    // Backstop for agent mode — the primary guard rejects `tui` at dispatch
    // time, but any future call path must not open a full-screen TUI either.
    if crate::core::agent_mode::enabled() {
        anyhow::bail!(
            "the TUI is interactive and unavailable in agent mode\n\
             Use `loom status` instead"
        );
    }

    let tui_theme = TuiTheme::from_graph_theme(&theme);
    let context = crate::status::resolve_context(&repo::open_repo()?, None);
    let snapshot = load_snapshot(context)?;
    let git_dir = snapshot.git_dir.clone();

    // Local changes start expanded.
    let mut expanded: HashSet<String> = HashSet::new();
    expanded.insert(LOCAL_CHANGES_KEY.to_string());

    let app = App::new(snapshot, &tui_theme, theme, expanded, context);
    let (_, Outcome::Quit) = Shell::new(app).run()?;

    // The TUI leaves on a conflict pause; repeat the popup's guidance where
    // it stays readable.
    if let Ok(Some(state)) = transaction::load(&git_dir) {
        println!("{}", paused_message(&state.command));
    }
    Ok(())
}

fn paused_message(command: &str) -> String {
    format!(
        "A `loom {}` is paused due to conflicts.\n\
         Resolve them, then run `loom continue` to resume, or `loom abort` to cancel.",
        command
    )
}

/// Gather repo info, exactly like `loom status` with files enabled.
fn load_snapshot(context: usize) -> Result<Snapshot> {
    let repo = repo::open_repo()?;
    let workdir = repo::require_workdir(&repo, "display status")?.to_path_buf();
    let git_dir = repo.path().to_path_buf();
    let cwd_prefix = repo::cwd_relative_to_repo(&repo).unwrap_or_default();

    let mut info = repo::gather_repo_info(&repo, true, context)?;
    // Collect entities before filtering so short IDs stay stable.
    let ids = IdAllocator::new(info.collect_entities());
    crate::status::apply_hidden_branches(&repo, &mut info);

    Ok(Snapshot {
        workdir,
        git_dir,
        cwd_prefix,
        info,
        ids,
    })
}

/// Run the loom command for `action`. Runs on the worker thread: the trace
/// logger is thread-local, so each action gets its own log file, named after
/// the command line the log shows.
fn execute_action(
    action: Action,
    command: &str,
    git_dir: &std::path::Path,
    theme: &graph::Theme,
) -> Result<()> {
    crate::trace::init(git_dir, &format!("loom tui: {}", command));
    let result = match action {
        Action::Commit { source, dest } => {
            let (branch, integration) = match dest {
                CommitDest::Integration => (None, true),
                CommitDest::Branch(name) => (Some(name), false),
            };
            let files = match source {
                CommitSource::Files(files) => files,
                CommitSource::Index => vec![],
            };
            commit::run(branch, integration, None, None, files, vec![], theme)
        }
        Action::Fold {
            sources,
            target,
            hunks,
        } => {
            let mut args = sources;
            args.push(target);
            let patch = hunks.is_some();
            let hunks = hunks.unwrap_or_default();
            fold::run(false, patch, None, hunks, args, vec![], theme)
        }
        Action::FoldHunks {
            picked,
            stamp,
            target,
            ..
        } => fold::run_picked(&picked, &stamp, &target),
        Action::Move { commit, slot } => {
            let (anchor, mut args) = match slot {
                MoveSlot::Above(x) => (Some(fold::Anchor::Above(x.to_string())), vec![]),
                MoveSlot::Below(x) => (Some(fold::Anchor::Below(x.to_string())), vec![]),
                MoveSlot::Branch(name) => (None, vec![name]),
                MoveSlot::Stay => anyhow::bail!("Nothing to move"),
            };
            args.insert(0, commit.to_string());
            fold::run(
                false,
                false,
                anchor,
                HunkArgs::default(),
                args,
                vec![],
                theme,
            )
        }
        Action::Split {
            commit,
            files,
            hunks,
        } => {
            let patch = hunks.is_some();
            let hunks = hunks.unwrap_or_default();
            split::run(commit, None, patch, hunks, files, theme)
        }
        Action::NewBranch { name, target } => branch::new::run(Some(name), target),
        Action::Drop { targets } => drop::run(targets, false),
        Action::Absorb { files } => absorb::run(false, files),
        Action::Reword { target, name } => reword::run(target, name),
    };
    crate::trace::finalize();
    result
}

// ── App state ────────────────────────────────────────────────────────────

struct App<'a> {
    snapshot: Snapshot,
    theme: &'a TuiTheme,
    /// Handed to each action's worker (the hunk pickers take it).
    graph_theme: graph::Theme,
    rows: Vec<Row>,
    /// Tree pane cursor and view state.
    tree: ListPane,
    /// Keys of the multi-selected rows.
    selected: HashSet<String>,
    /// Class every selected row belongs to; `None` when nothing is selected.
    selected_class: Option<SelectionClass>,
    expanded: HashSet<String>,
    /// Context depth the tree is loaded with, as `loom status <N>` takes it.
    context: usize,
    mode: Mode,
    /// Right-pane scroll state.
    diff: DiffPane,
    /// Diff lines cached per row key.
    diff_cache: HashMap<String, Vec<Line<'static>>>,
    /// Row key to put the cursor on after the next reload, for an action that
    /// renames the row it acts on.
    next_cursor: Option<String>,
    /// Row key to fall back to when the action fails and the row it ran from
    /// was only a preview (`b`, `c`), so no longer exists after the reload.
    fallback_cursor: Option<String>,
    /// The `C` pick in flight, from the key press to the selector.
    pick: Option<Pick>,
    /// `q`/`Ctrl-C` during a pick: leave once its worker lands.
    quit_after_pick: bool,
    /// Transient message shown in the status bar until the next key.
    notice: Option<String>,
    /// Exit value set by deep handlers, drained after each key.
    outcome: Option<Outcome>,
    /// Requests from the running action (`core::ui`); `request_tx` is what
    /// each worker gets a clone of.
    requests: Receiver<Request>,
    request_tx: Sender<Request>,
    running: Option<Running>,
    popup: Option<Popup>,
    log: Vec<LogEntry>,
}

impl<'a> App<'a> {
    fn new(
        snapshot: Snapshot,
        theme: &'a TuiTheme,
        graph_theme: graph::Theme,
        expanded: HashSet<String>,
        context: usize,
    ) -> Self {
        let (request_tx, requests) = channel();
        let mut app = App {
            snapshot,
            theme,
            graph_theme,
            rows: Vec::new(),
            tree: ListPane::new(0),
            selected: HashSet::new(),
            selected_class: None,
            expanded,
            context,
            mode: Mode::Normal,
            diff: DiffPane::new(),
            diff_cache: HashMap::new(),
            next_cursor: None,
            fallback_cursor: None,
            pick: None,
            quit_after_pick: false,
            notice: None,
            outcome: None,
            requests,
            request_tx,
            running: None,
            popup: None,
            log: Vec::new(),
        };
        app.rows = app.build_rows();
        let cursor = app.rows.iter().position(|r| r.focusable).unwrap_or(0);
        app.tree.set_cursor(cursor);
        // Prime the diff for the initial cursor row so the first render
        // doesn't have to.
        app.ensure_diff_cached();
        app
    }

    /// The tree rows for the current snapshot, expansion state, and — while
    /// a new branch is being named or a commit placed — its placeholder row.
    fn build_rows(&self) -> Vec<Row> {
        let preview = match &self.mode {
            Mode::NewBranch { tip, .. } => Some(Preview::Branch(BranchInfo {
                name: NEW_BRANCH_NAME.to_string(),
                tip_oid: *tip,
                remote: None,
            })),
            Mode::CommitTarget {
                source,
                dests,
                index,
                ..
            } => Some(Preview::Commit {
                dest: dests[*index].clone(),
                file_count: match source {
                    CommitSource::Files(files) => self.commit_file_count(files),
                    CommitSource::Index => self.snapshot.staged_count(),
                },
            }),
            Mode::MoveTarget {
                commit,
                slots,
                index,
            } => Some(Preview::Move {
                oid: *commit,
                slot: slots[*index].clone(),
            }),
            _ => None,
        };
        let sections = self.snapshot.sections(preview);
        status_tree::build_rows(&sections, &self.snapshot.ids, &self.expanded)
    }

    /// How many working files `files` (short IDs, or `zz`) stand for.
    fn commit_file_count(&self, files: &[String]) -> usize {
        if self.snapshot.is_all_changes(files) {
            self.snapshot.info.working_changes.len()
        } else {
            files.len()
        }
    }

    fn current_row(&self) -> Option<&Row> {
        self.rows.get(self.tree.cursor())
    }

    /// Replace the repo state, keeping the cursor on the same row when it
    /// still exists. Selection and the diff cache are tied to the old rows.
    fn apply_snapshot(&mut self, snapshot: Snapshot) {
        let key = self
            .next_cursor
            .take()
            .or_else(|| self.current_row().map(|r| r.key.clone()));
        let previous = self.tree.cursor();
        self.snapshot = snapshot;
        self.clear_selection();
        self.diff_cache.clear();
        self.rows = self.build_rows();
        let cursor = key
            .and_then(|key| self.rows.iter().position(|r| r.focusable && r.key == key))
            .or_else(|| nearest_focusable(&self.rows, previous))
            .unwrap_or(0);
        self.tree.set_cursor(cursor);
        self.diff.reset();
        self.ensure_diff_cached();
    }

    /// Reload the tree from the repo; a failure is shown, the old tree kept.
    /// Reports whether the new snapshot was applied.
    fn reload(&mut self) -> bool {
        match load_snapshot(self.context) {
            Ok(snapshot) => {
                self.apply_snapshot(snapshot);
                true
            }
            Err(e) => {
                self.next_cursor = None;
                self.show_error(&e.to_string(), AfterNotice::Nothing);
                false
            }
        }
    }

    /// Show `delta` more (or fewer) context commits before the base and
    /// reload. Depth 1 is the floor: the base alone, as `loom status` starts.
    fn change_context(&mut self, delta: isize) {
        let depth = self.context.saturating_add_signed(delta).max(1);
        if depth == self.context {
            self.notice = Some(format!("context: {}", depth));
            return;
        }
        let previous = self.context;
        self.context = depth;
        if self.reload() {
            // The walk stops at the root commit, so asking for more than
            // history holds must not run the depth away from the tree: `-`
            // would then take as many presses to show anything.
            self.context = depth.min(self.snapshot.info.context_commits.len() + 1);
            self.notice = Some(format!("context: {}", self.context));
        } else {
            self.context = previous;
        }
    }

    fn show_error(&mut self, text: &str, then: AfterNotice) {
        self.popup = Some(Popup::Notice {
            notice: Notice::new("Error", Level::Error, text),
            then,
        });
    }

    // -- running an action ------------------------------------------------------

    /// The short ID the tree shows for `target`, or `target` itself when no
    /// row carries one.
    fn sid_of(&self, target: &str) -> String {
        self.rows
            .iter()
            .find(|r| r.target.as_deref() == Some(target) && !r.sid.is_empty())
            .map(|r| r.sid.clone())
            .unwrap_or_else(|| target.to_string())
    }

    /// `title` built from the sources' short IDs, or from a count when that
    /// would not fit the pane: the title is what still names the sources
    /// once they scroll off the tree.
    fn title_sources(
        &self,
        targets: &[String],
        width: u16,
        title: impl Fn(&str) -> String,
    ) -> String {
        let joined = targets
            .iter()
            .map(|t| self.sid_of(t))
            .collect::<Vec<_>>()
            .join(" ");
        let full = title(&joined);
        // Two columns for the corners the title sits between.
        if full.chars().count() + 2 <= width as usize {
            full
        } else {
            title(&format!("{} item(s)", targets.len()))
        }
    }

    /// The CLI line equivalent to `action`, with the short IDs the tree
    /// shows, for the log.
    fn command_line(&self, action: &Action) -> String {
        let sid = |target: &str| self.sid_of(target);
        let mut words = vec!["loom".to_string()];
        match action {
            Action::Commit { source, dest } => {
                words.push("commit".into());
                match dest {
                    CommitDest::Integration => words.push("-i".into()),
                    CommitDest::Branch(name) => words.extend(["-b".into(), sid(name)]),
                }
                // `C` staged what it picked, so this half names no files.
                if let CommitSource::Files(files) = source {
                    words.extend(files.iter().map(|f| sid(f)));
                }
            }
            Action::Fold {
                sources,
                target,
                hunks,
            } => {
                words.push("fold".into());
                if hunks.is_some() {
                    words.push("-p".into());
                }
                words.extend(sources.iter().map(|s| sid(s)));
                words.push(sid(target));
                if let Some(hunks) = hunks {
                    for id in &hunks.ids {
                        words.extend(["--hunks".into(), hunk_select::quoted(id)]);
                    }
                    if let Some(from) = &hunks.from {
                        words.extend(["--hunks-from".into(), from.clone()]);
                    }
                }
            }
            Action::FoldHunks {
                sources, target, ..
            } => {
                words.extend(["fold".into(), "-p".into()]);
                words.extend(sources.iter().map(|s| sid(s)));
                words.push(sid(target));
            }
            Action::Move { commit, slot } => {
                words.extend(["fold".into(), sid(&commit.to_string())]);
                match slot {
                    MoveSlot::Above(x) => words.extend(["--above".into(), sid(&x.to_string())]),
                    MoveSlot::Below(x) => words.extend(["--below".into(), sid(&x.to_string())]),
                    MoveSlot::Branch(name) => words.push(sid(name)),
                    MoveSlot::Stay => {}
                }
            }
            Action::Split {
                commit,
                files,
                hunks,
            } => {
                words.push("split".into());
                if hunks.is_some() {
                    words.push("-p".into());
                }
                words.push(sid(commit));
                words.extend(files.iter().map(|f| hunk_select::quoted(f)));
                if let Some(hunks) = hunks {
                    for id in &hunks.ids {
                        words.extend(["--hunks".into(), hunk_select::quoted(id)]);
                    }
                    if let Some(from) = &hunks.from {
                        words.extend(["--hunks-from".into(), from.clone()]);
                    }
                }
            }
            Action::NewBranch { name, target } => {
                words.extend(["branch".into(), "new".into(), name.clone()]);
                if let Some(target) = target {
                    words.extend(["-t".into(), sid(target)]);
                }
            }
            Action::Drop { targets } => {
                words.push("drop".into());
                words.extend(targets.iter().map(|t| sid(t)));
            }
            Action::Absorb { files } => {
                words.push("absorb".into());
                words.extend(files.iter().map(|f| sid(f)));
            }
            Action::Reword { target, name } => {
                words.extend(["reword".into(), sid(target)]);
                if let Some(name) = name {
                    words.extend(["-m".into(), name.clone()]);
                }
            }
        }
        words.join(" ")
    }

    /// Run `action` on a worker thread; the TUI keeps going and answers its
    /// prompts.
    fn start_action(&mut self, action: Action) {
        let command = self.command_line(&action);
        self.log.push(LogEntry {
            command: command.clone(),
            lines: Vec::new(),
        });
        self.clear_selection();
        self.mode = Mode::Normal;
        self.notice = None;

        let tx = self.request_tx.clone();
        let git_dir = self.snapshot.git_dir.clone();
        let theme = self.graph_theme.clone();
        let trace_name = command.clone();
        let handle = std::thread::spawn(move || {
            ui::install(tx);
            let result = execute_action(action, &trace_name, &git_dir, &theme);
            ui::uninstall();
            result
        });
        self.running = Some(Running {
            handle,
            command,
            spinner: None,
            ticks: 0,
        });
    }

    /// One request from the running action. `Suspend` is the shell's to
    /// handle and never reaches here.
    fn handle_request(&mut self, request: Request) {
        match request {
            Request::Prompt {
                kind,
                prompt,
                error,
                reply,
            } => {
                let command = self.running.as_ref().map_or("", |r| r.command.as_str());
                self.popup = Some(Popup::Prompt {
                    prompt: Prompt::new(kind, prompt, error, command),
                    reply,
                });
            }
            Request::Message { level, text } => {
                if let Some(entry) = self.log.last_mut() {
                    entry.lines.push((level, text));
                }
            }
            Request::Spinner(text) => {
                if let Some(running) = &mut self.running {
                    running.spinner = text;
                }
            }
            Request::Suspend(_) | Request::Resume => {}
        }
    }

    /// The worker is done: report in the status bar (success), a popup
    /// (failure), or nothing (cancelled prompt). Returns whether the repo may
    /// have changed, so the caller runs [`App::after_action`].
    fn finish_action(&mut self, result: Result<()>) -> bool {
        if result.is_err() {
            // Nothing was renamed or created: aim at the preview's origin row,
            // or at nothing, since the old key is still the live one.
            self.next_cursor = self.fallback_cursor.take();
        }
        self.fallback_cursor = None;
        match result {
            Ok(()) => {
                // A multi-target command (`drop a b`) prints one ✓ per target;
                // the bar shows the last and says how many it stands for.
                let successes: Vec<&str> = self.log.last().map_or_else(Vec::new, |entry| {
                    entry
                        .lines
                        .iter()
                        .filter(|(level, _)| *level == Level::Success)
                        .filter_map(|(_, text)| text.lines().next())
                        .collect()
                });
                self.notice = Some(match successes.as_slice() {
                    [] => "✓ done".to_string(),
                    [one] => format!("✓ {}", one),
                    [.., last] => {
                        format!("✓ {} (+{} more, L: log)", last, successes.len() - 1)
                    }
                });
                true
            }
            Err(e) if e.downcast_ref::<Cancelled>().is_some() => {
                self.log_line(Level::Warn, "Cancelled");
                self.notice = Some("cancelled".to_string());
                false
            }
            Err(e) => {
                let text = e.to_string();
                self.log_line(Level::Error, &text);
                // An open log already shows the error line, so reload behind
                // it instead of replacing what the reader is looking at.
                if matches!(self.popup, Some(Popup::Log { .. })) {
                    return true;
                }
                self.show_error(&text, AfterNotice::Reload);
                false
            }
        }
    }

    fn log_line(&mut self, level: Level, text: &str) {
        if let Some(entry) = self.log.last_mut() {
            entry.lines.push((level, text.to_string()));
        }
    }

    /// After an action changed the repo: a conflict pause ends the session
    /// (every other command is blocked until it's resolved), otherwise the
    /// tree reloads.
    fn after_action(&mut self) {
        match transaction::load(&self.snapshot.git_dir) {
            Ok(Some(state)) => {
                self.popup = Some(Popup::Notice {
                    notice: Notice::new("Paused", Level::Warn, &paused_message(&state.command)),
                    then: AfterNotice::Quit,
                });
            }
            _ => {
                self.reload();
            }
        }
    }

    fn dismiss_notice(&mut self, then: AfterNotice) {
        match then {
            AfterNotice::Nothing => {}
            AfterNotice::Reload => self.after_action(),
            AfterNotice::Quit => self.outcome = Some(Outcome::Quit),
        }
    }

    fn handle_popup_key(&mut self, code: KeyCode, modifiers: KeyModifiers) {
        let Some(popup) = self.popup.take() else {
            return;
        };
        match popup {
            Popup::Prompt { mut prompt, reply } => match prompt.handle_key(code, modifiers) {
                PromptOutcome::Pending => self.popup = Some(Popup::Prompt { prompt, reply }),
                PromptOutcome::Answer(answer) => {
                    let _ = reply.send(Some(answer));
                }
                PromptOutcome::Cancel => {
                    let _ = reply.send(None);
                }
            },
            Popup::Notice { notice, then } => {
                if Notice::dismisses(code) {
                    self.dismiss_notice(then);
                } else {
                    self.popup = Some(Popup::Notice { notice, then });
                }
            }
            Popup::Log { mut scroll } => match code {
                KeyCode::Esc | KeyCode::Char('q' | 'L') => {}
                _ => {
                    scroll_popup(&mut scroll, code);
                    self.popup = Some(Popup::Log { scroll });
                }
            },
            Popup::Help { mut scroll } => match code {
                KeyCode::Esc | KeyCode::Char('q' | '?') => {}
                _ => {
                    scroll_popup(&mut scroll, code);
                    self.popup = Some(Popup::Help { scroll });
                }
            },
        }
    }

    fn open_help(&mut self) {
        self.popup = Some(Popup::Help {
            scroll: DiffPane::new(),
        });
    }

    fn open_log(&mut self) {
        let mut scroll = DiffPane::new();
        // Start at the newest entry; the render clamps to the content.
        scroll.set_scroll(u16::MAX);
        self.popup = Some(Popup::Log { scroll });
    }

    /// Rebuild rows after an expansion change, keeping the cursor on `key`.
    /// Rows that went away take their selection with them: an off-screen row
    /// would keep vetoing every other class with no marker in sight.
    fn rebuild_rows(&mut self, key: &str) {
        self.rows = self.build_rows();
        let visible: HashSet<&str> = self.rows.iter().map(|r| r.key.as_str()).collect();
        self.selected.retain(|k| visible.contains(k.as_str()));
        if self.selected.is_empty() {
            self.selected_class = None;
        }
        if let Some(pos) = self.rows.iter().position(|r| r.key == key) {
            self.tree.set_cursor(pos);
        } else if self.tree.cursor() >= self.rows.len() {
            self.tree.set_cursor(self.rows.len().saturating_sub(1));
        }
    }

    // -- keyboard handling ----------------------------------------------------

    /// A key with no popup open and no action running.
    fn handle_tree_key(&mut self, focused: PaneId, code: KeyCode) {
        let action = match code {
            KeyCode::Esc => {
                self.handle_escape();
                None
            }
            KeyCode::Up | KeyCode::Char('k') => {
                match focused {
                    PaneId::Left => self.move_cursor(-1),
                    PaneId::Right => self.diff.scroll_by(-1),
                }
                None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                match focused {
                    PaneId::Left => self.move_cursor(1),
                    PaneId::Right => self.diff.scroll_by(1),
                }
                None
            }
            KeyCode::PageUp => {
                self.diff.scroll_page(-1);
                None
            }
            KeyCode::PageDown => {
                self.diff.scroll_page(1);
                None
            }
            // Folding is how the cursor leaves a row: `←` on a child walks up
            // to its parent. The cursor must stay on the placeholder or the
            // fold target, and a closed commit would hide a commit-file source.
            KeyCode::Left | KeyCode::Right | KeyCode::Char('h' | 'l')
                if self.placing().is_some() =>
            {
                let what = self.placing().unwrap_or_default();
                self.notice = Some(format!("{}: Enter to confirm, Esc to cancel", what));
                None
            }
            KeyCode::Right | KeyCode::Char('l') => {
                self.expand_current();
                None
            }
            KeyCode::Left | KeyCode::Char('h') => {
                self.collapse_current();
                None
            }
            KeyCode::Enter => match &self.mode {
                Mode::FoldTarget { .. } => self.confirm_fold_target(),
                Mode::CommitTarget { .. } => self.confirm_commit_target(),
                Mode::MoveTarget { .. } => self.confirm_move_target(),
                // Rename mode never gets here: it is handled above.
                _ => {
                    self.toggle_current();
                    None
                }
            },
            KeyCode::Char('L') => {
                self.open_log();
                None
            }
            KeyCode::Char('?') => {
                self.open_help();
                None
            }
            // While picking a fold target or placing a commit only navigation,
            // Enter, and Esc apply — action keys must not fire and discard
            // the pending operation.
            KeyCode::Char(
                ' ' | 'c' | 'C' | 'f' | 'F' | 'm' | 's' | 'S' | 'b' | 'd' | 'a' | 'r' | 'R' | '+'
                | '=' | '-',
            )
            | KeyCode::F(5)
                if self.placing().is_some() =>
            {
                let what = self.placing().unwrap_or_default();
                self.notice = Some(format!("{}: Enter to confirm, Esc to cancel", what));
                None
            }
            KeyCode::Char(' ') => {
                self.toggle_selection();
                None
            }
            KeyCode::Char('c') => {
                self.action_commit_start();
                None
            }
            KeyCode::Char('C') => {
                self.action_commit_patch_start();
                None
            }
            KeyCode::Char('f') => {
                self.action_fold_start();
                None
            }
            KeyCode::Char('F') => {
                self.action_fold_patch_start();
                None
            }
            KeyCode::Char('m') => {
                self.action_move_start();
                None
            }
            KeyCode::Char('s') => self.action_split(),
            KeyCode::Char('S') => {
                self.action_split_patch_start();
                None
            }
            KeyCode::Char('b') => {
                self.action_new_branch();
                None
            }
            KeyCode::Char('d') => self.action_drop(),
            KeyCode::Char('a') => self.action_absorb(),
            KeyCode::Char('r') => self.action_reword(),
            KeyCode::Char('R') | KeyCode::F(5) => {
                self.reload();
                None
            }
            // `=` is `+` without Shift on most layouts, next to `-`.
            KeyCode::Char('+' | '=') => {
                self.change_context(1);
                None
            }
            KeyCode::Char('-') => {
                self.change_context(-1);
                None
            }
            _ => None,
        };
        if let Some(action) = action {
            self.start_action(action);
        }
    }

    /// The command whose target the tree is walking, if any: only navigation,
    /// Enter and Esc apply then.
    fn placing(&self) -> Option<&'static str> {
        match self.mode {
            Mode::CommitTarget { .. } => Some("commit"),
            Mode::FoldTarget { .. } => Some("fold"),
            Mode::MoveTarget { .. } => Some("move"),
            _ => None,
        }
    }

    /// Esc: cancel fold-target, commit or move mode, else clear the selection.
    /// Never quits: it reads as cancel, and `q` is the way out.
    fn handle_escape(&mut self) {
        if matches!(self.mode, Mode::FoldTarget { .. }) {
            self.cancel_fold_target();
        } else if matches!(self.mode, Mode::CommitTarget { .. }) {
            self.cancel_commit_target();
        } else if matches!(self.mode, Mode::MoveTarget { .. }) {
            self.cancel_move_target();
        } else if !self.selected.is_empty() {
            self.clear_selection();
        }
    }

    fn move_cursor(&mut self, dir: isize) {
        match self.mode {
            Mode::CommitTarget { .. } => return self.move_commit_dest(dir),
            Mode::FoldTarget { .. } => return self.move_fold_target(dir),
            Mode::MoveTarget { .. } => return self.move_move_target(dir),
            _ => {}
        }
        if self
            .tree
            .move_cursor(dir, self.rows.len(), |i| self.rows[i].focusable)
        {
            self.diff.reset();
        }
    }

    fn expand_current(&mut self) {
        let Some(row) = self.current_row() else {
            return;
        };
        if row.expandable && !row.expanded {
            let key = row.key.clone();
            self.expanded.insert(key.clone());
            self.rebuild_rows(&key);
        }
    }

    /// Collapse the current row, or jump to (and collapse) its parent when the
    /// cursor is on a child file row.
    fn collapse_current(&mut self) {
        let Some(row) = self.current_row() else {
            return;
        };
        if row.expandable && row.expanded {
            let key = row.key.clone();
            self.expanded.remove(&key);
            self.rebuild_rows(&key);
            return;
        }
        if matches!(
            row.kind,
            RowKind::WorkingFile { .. } | RowKind::CommitFile { .. }
        ) {
            // Walk up to the expandable parent.
            let mut pos = self.tree.cursor();
            while pos > 0 {
                pos -= 1;
                if self.rows[pos].expandable {
                    let key = self.rows[pos].key.clone();
                    self.expanded.remove(&key);
                    self.rebuild_rows(&key);
                    return;
                }
            }
        }
    }

    fn toggle_current(&mut self) {
        let Some(row) = self.current_row() else {
            return;
        };
        if !row.expandable {
            return;
        }
        if row.expanded {
            self.collapse_current();
        } else {
            self.expand_current();
        }
    }

    /// Toggle the cursor row in or out of the selection. A selection holds one
    /// class of row: toggling in a different one is refused rather than
    /// replacing what is already selected, since no action could use the mix.
    fn toggle_selection(&mut self) {
        let Some(row) = self.current_row() else {
            return;
        };
        let Some(class) = row.selection_class() else {
            return;
        };
        let key = row.key.clone();
        if self.selected.remove(&key) {
            if self.selected.is_empty() {
                self.selected_class = None;
            }
        } else {
            if let Some(current) = self.selected_class
                && current != class
            {
                self.notice = Some(format!(
                    "selection holds {}; Esc clears it",
                    current.label()
                ));
                return;
            }
            self.selected.insert(key);
            self.selected_class = Some(class);
        }
        self.move_cursor(1);
    }

    fn clear_selection(&mut self) {
        self.selected.clear();
        self.selected_class = None;
    }

    // -- actions ----------------------------------------------------------------

    /// The selected rows, in tree order, else the cursor row.
    fn picked_rows(&self) -> Vec<&Row> {
        if self.selected.is_empty() {
            self.current_row().into_iter().collect()
        } else {
            self.rows
                .iter()
                .filter(|r| self.selected.contains(&r.key))
                .collect()
        }
    }

    /// The gutter marks for `rows` as the sources of a pending command: the
    /// rows themselves, plus the working files a `zz` header among them
    /// subsumes.
    fn mark_sources(&self, rows: &[&Row]) -> Sources {
        let covers_all = rows
            .iter()
            .any(|r| matches!(r.kind, RowKind::LocalChanges { .. }));
        Sources {
            named: rows.iter().map(|r| r.key.clone()).collect(),
            covered: if covers_all {
                self.working_file_keys(|_| true)
            } else {
                HashSet::new()
            },
        }
    }

    /// Keys of the working-file rows matching `keep`, read off the snapshot
    /// rather than the drawn rows: `[local changes]` may be closed, and the
    /// files it hides are covered all the same.
    fn working_file_keys(&self, keep: impl Fn(&FileChange) -> bool) -> HashSet<String> {
        self.snapshot
            .info
            .working_changes
            .iter()
            .filter(|c| keep(c))
            .map(|c| working_file_key(&c.path))
            .collect()
    }

    /// The gutter marks for a `C` commit: the index is the source, so the
    /// staged files are all there is to point at — no row names it.
    fn index_sources(&self) -> Sources {
        let mut covered = self.working_file_keys(is_staged);
        // The one row that is drawn whether `[local changes]` is open or
        // closed, so a `C` placement is never left with no mark at all.
        covered.insert(LOCAL_CHANGES_KEY.to_string());
        Sources {
            named: HashSet::new(),
            covered,
        }
    }

    /// The gutter mark for `row`. A pending command's sources outrank the
    /// selection, which `Space` cannot change while a target is being picked.
    fn row_mark(&self, row: &Row) -> RowMark {
        let source_rows = match &self.mode {
            Mode::CommitTarget { source_rows, .. } | Mode::FoldTarget { source_rows, .. } => {
                Some(source_rows)
            }
            _ => None,
        };
        if let Some(sources) = source_rows {
            if sources.named.contains(&row.key) {
                return RowMark::Source;
            }
            if sources.covered.contains(&row.key) {
                return RowMark::Covered;
            }
        }
        if self.selected.contains(&row.key) {
            return RowMark::Selected;
        }
        RowMark::None
    }

    /// `c`: the selected working files or `[local changes]` header (else the
    /// cursor's) are the commit; the index plays no part. The tree is then
    /// redrawn with the commit at its destination, and nothing runs until
    /// that is confirmed.
    fn action_commit_start(&mut self) {
        let Some((files, source_rows, origin)) = self.commit_sources() else {
            return;
        };
        self.enter_commit_target(CommitSource::Files(files), source_rows, origin);
    }

    /// `C`: every local change, whatever the cursor or selection — a picker
    /// showing one file would hide the rest of the index it is about to
    /// rewrite. The press starts the read on a worker; the selector that
    /// follows is what asks the shell for the terminal.
    fn action_commit_patch_start(&mut self) {
        if self.snapshot.info.working_changes.is_empty() {
            self.notice = Some("commit: no local changes".to_string());
            return;
        }
        let Some(origin) = self.current_row().map(|r| r.key.clone()) else {
            return;
        };
        let files = vec![self.snapshot.ids.get_unstaged().to_string()];
        let command = format!("loom add -p {}", files.join(" "));
        self.start_pick(command, origin, PickFor::Commit, move |workdir| {
            let repo = git2::Repository::open(workdir)?;
            let filter = staging::filter_paths(&repo, &files)?;
            let entries = staging::collect_file_entries(&repo, workdir, filter.as_deref())?;
            Ok((entries, Vec::new()))
        });
    }

    /// `F`: `f` with the hunks picked first — out of one commit's own diff,
    /// or, on working files or local changes, out of every local change, as
    /// `C` shows them whatever the cursor or selection. The press starts the
    /// read on a worker, as `C` does.
    fn action_fold_patch_start(&mut self) {
        let rows = self.picked_rows();
        let Some(origin) = self.current_row().map(|r| r.key.clone()) else {
            return;
        };
        let mut sources: Vec<String> = rows.iter().filter_map(|r| r.target.clone()).collect();
        if sources.is_empty() || sources.len() != rows.len() {
            self.notice = Some("fold: select source rows first".to_string());
            return;
        }
        let commit = match rows[0].kind {
            RowKind::Commit { oid, .. } => Some(oid),
            RowKind::LocalChanges { .. } | RowKind::WorkingFile { .. } => None,
            _ => {
                self.notice = Some("fold: move to a file, local changes, or a commit".to_string());
                return;
            }
        };
        let targets = match self.fold_targets(&rows, true) {
            Ok(targets) => targets,
            Err(notice) => {
                self.notice = Some(format!("fold: {}", notice));
                return;
            }
        };
        if commit.is_none() {
            sources = vec![self.snapshot.ids.get_unstaged().to_string()];
        }
        let shown: Vec<String> = sources.iter().map(|s| self.sid_of(s)).collect();
        let command = format!("loom fold -p {}", shown.join(" "));
        let purpose = PickFor::Fold {
            sources: sources.clone(),
            targets,
            commit,
        };
        match commit {
            Some(oid) => self.start_pick(command, origin, purpose, move |workdir| {
                let entries = staging::collect_commit_hunks(workdir, &oid.to_string(), &[])?;
                Ok((entries, Vec::new()))
            }),
            None => self.start_pick(command, origin, purpose, move |workdir| {
                let repo = git2::Repository::open(workdir)?;
                let entries = staging::collect_file_entries(&repo, workdir, None)?;
                let stamp = staging::binary_stamp(workdir, &entries);
                Ok((entries, stamp))
            }),
        }
    }

    /// Open `command`'s log entry and start `read` on a worker, over the
    /// snapshot's working tree.
    fn start_pick(
        &mut self,
        command: String,
        origin: String,
        purpose: PickFor,
        read: impl FnOnce(&std::path::Path) -> Result<(Vec<FileEntry>, Stamp)> + Send + 'static,
    ) {
        // Pushed before the worker starts, not when the staging happens: the
        // read is part of this command and its messages have to land here.
        self.log.push(LogEntry {
            command: command.clone(),
            lines: Vec::new(),
        });
        // The repository is opened here rather than from the process cwd: this
        // runs off the loop, and the tree it must agree with is the snapshot's.
        let workdir = self.snapshot.workdir.clone();
        let git_dir = self.snapshot.git_dir.clone();
        let tx = self.request_tx.clone();
        let handle = std::thread::spawn(move || {
            // Without the sink `msg` prints, and this thread would write onto
            // the screen the loop is drawing at the same moment.
            ui::install(tx);
            crate::trace::init(&git_dir, &format!("loom tui: {command}"));
            let result = read(&workdir);
            crate::trace::finalize();
            ui::uninstall();
            result
        });
        self.pick = Some(Pick::Collecting {
            handle,
            origin,
            purpose,
            ticks: 0,
            cancelled: false,
        });
    }

    /// The working files `c` stands for, their gutter marks, and the key of
    /// the row it came from. `None` when there is nothing to commit, after a
    /// notice saying which of the reasons it was — or silently, when there is
    /// no row to stand on.
    fn commit_sources(&mut self) -> Option<(Vec<String>, Sources, String)> {
        let rows = self.picked_rows();
        if rows.is_empty()
            || !rows.iter().all(|r| {
                matches!(
                    r.kind,
                    RowKind::WorkingFile { .. } | RowKind::LocalChanges { .. }
                )
            })
        {
            self.notice = Some("commit: move to local changes or select files".to_string());
            return None;
        }
        if rows
            .iter()
            .any(|r| matches!(r.kind, RowKind::LocalChanges { count: 0 }))
        {
            self.notice = Some("commit: no local changes".to_string());
            return None;
        }
        // A row of these kinds always carries a target; refuse rather than
        // fall through to an argument-less `loom commit`, which would commit
        // the index — the one thing `c` must never do. An unnamed row is the
        // same refusal: `loom commit -i ""` is not an improvement.
        let Some(files) = rows
            .iter()
            .map(|r| r.target.clone().filter(|t| !t.is_empty()))
            .collect::<Option<Vec<String>>>()
        else {
            self.notice = Some("commit: this row has nothing to commit".to_string());
            return None;
        };
        let source_rows = self.mark_sources(&rows);
        let origin = self.current_row().map(|r| r.key.clone())?;
        Some((files, source_rows, origin))
    }

    /// Redraw the tree with the placeholder commit and wait for a destination.
    fn enter_commit_target(&mut self, source: CommitSource, source_rows: Sources, origin: String) {
        // The destinations are read off the drawn tree, so their order is the
        // order `↑`/`↓` walk them in.
        let mut dests = vec![CommitDest::Integration];
        dests.extend(self.rows.iter().filter_map(|r| match &r.kind {
            RowKind::BranchName { name, .. } => Some(CommitDest::Branch(name.clone())),
            _ => None,
        }));
        self.mode = Mode::CommitTarget {
            source,
            source_rows,
            dests,
            index: 0,
            origin,
        };
        // Every placement shares one row key but shows the files this press
        // picked, so an earlier press's entry has to go. Moving the commit
        // afterwards never changes it: only the destination moves.
        self.diff_cache.remove(&PENDING_COMMIT_OID.to_string());
        self.diff.reset();
        self.show_pending_commit();
    }

    /// End the pick as soon as its read lands, and leave afterwards if asked.
    /// The worker is waited for either way: it reads with `git`, and one
    /// outliving its pick would run beside whatever comes next.
    fn cancel_pick(&mut self, then_quit: bool) {
        if let Some(Pick::Collecting { cancelled, .. }) = &mut self.pick {
            *cancelled = true;
            self.quit_after_pick |= then_quit;
        }
    }

    /// Move a pick along one tick. A read that fails, or one `Esc` marked,
    /// ends here too: the pick goes, and the loop only redraws.
    ///
    /// The worker is joined here rather than in [`App::take_over`], so the
    /// loop keeps drawing — a spinner, the tree, a resize — while it reads.
    fn advance_pick(&mut self) -> PickTick {
        match self.pick.take() {
            None => PickTick::Nothing,
            Some(ready @ Pick::Ready { .. }) => {
                self.pick = Some(ready);
                PickTick::Ready { first: false }
            }
            Some(Pick::Collecting {
                handle,
                origin,
                purpose,
                ticks,
                cancelled,
            }) if !handle.is_finished() => {
                self.pick = Some(Pick::Collecting {
                    handle,
                    origin,
                    purpose,
                    ticks: ticks + 1,
                    cancelled,
                });
                PickTick::Redraw
            }
            Some(Pick::Collecting {
                handle,
                origin,
                purpose,
                cancelled,
                ..
            }) => {
                let read = handle.join().unwrap_or_else(|_| {
                    Err(anyhow::anyhow!(
                        "reading the hunks crashed — run `loom trace` for what it did"
                    ))
                });
                if cancelled {
                    // Waited for on purpose: the result is dropped, but not the
                    // worker, so nothing of it outlives the pick.
                    self.log_line(Level::Warn, "Cancelled");
                    self.notice = Some(format!("{} cancelled", purpose.label()));
                    if self.quit_after_pick {
                        self.outcome = Some(Outcome::Quit);
                    }
                    return PickTick::Redraw;
                }
                match read {
                    Ok((entries, stamp)) => {
                        self.pick = Some(Pick::Ready {
                            entries,
                            stamp,
                            origin,
                            purpose,
                        });
                        PickTick::Ready { first: true }
                    }
                    Err(e) => {
                        // As a failed action reports: into the entry the press
                        // opened, and without taking an open log off the reader
                        // who is already seeing the line.
                        let text = e.to_string();
                        self.log_line(Level::Error, &text);
                        if !matches!(self.popup, Some(Popup::Log { .. })) {
                            self.show_error(&text, AfterNotice::Nothing);
                        }
                        PickTick::Redraw
                    }
                }
            }
        }
    }

    /// What a pick leaves behind once the selector is done with it: the
    /// placement, or a line saying why there is none. Split from the selector
    /// half, which cannot run without a terminal.
    fn finish_pick(&mut self, staged: Result<bool>, origin: String) {
        match staged {
            Ok(true) => {
                if self.reload() {
                    let source_rows = self.index_sources();
                    self.enter_commit_target(CommitSource::Index, source_rows, origin);
                }
            }
            // Every way out of a pick leaves a line: the entry the press
            // opened is the only record of what `C` did, and one that ends
            // mute cannot be told from one that ran and said nothing.
            Ok(false) => {
                self.log_line(Level::Warn, "Cancelled");
                self.notice = Some("commit cancelled".to_string());
            }
            Err(e) => {
                let text = e.to_string();
                self.log_line(Level::Error, &text);
                // `apply_selections` writes the index in several steps, so a
                // failure can leave it part-way: reload before reporting, or
                // the tree describes an index that is no longer there.
                self.reload();
                self.show_error(&text, AfterNotice::Nothing);
            }
        }
    }

    /// Offer `entries` in the selector on the tree's own terminal and stage
    /// what it keeps — the `loom add -p` half of `C`. `Ok(false)` if it was
    /// cancelled or kept nothing, which leaves the index as it was.
    fn select_and_stage(
        &mut self,
        entries: Vec<FileEntry>,
        terminal: &mut ratatui::DefaultTerminal,
    ) -> Result<bool> {
        let picked = run_hunk_selector_nested(
            entries,
            TuiTheme::from_graph_theme(&self.graph_theme),
            "COMMIT",
            terminal,
        )?;
        let Some(picked) = picked else {
            return Ok(false);
        };
        self.apply_pick(picked)
    }

    /// Stage what the selector kept, the half of `C` that writes the index —
    /// split from the selector, which is the only part needing a terminal.
    /// `Ok(false)` when nothing was kept, which leaves the index as it was.
    fn apply_pick(&mut self, picked: Vec<FileEntry>) -> Result<bool> {
        if !picked
            .iter()
            .any(|file| file.hunks.iter().any(|hunk| hunk.selected))
        {
            return Ok(false);
        }
        // Into the entry the press opened, so both halves of `loom add -p`
        // report under it. Nothing under `apply_selections` may prompt: the
        // thread that would answer is the one sitting here.
        ui::install(self.request_tx.clone());
        let applied =
            staging::apply_selections(&self.snapshot.workdir, &picked, staging::LeftOut::Unstaged);
        ui::uninstall();
        // `C` then commits the index, so an untick unstages, as in `add -p`.
        applied.map(|_| true)
    }

    /// Redraw the tree with the placeholder at the current destination and
    /// put the cursor back on it.
    fn show_pending_commit(&mut self) {
        self.rebuild_rows(&PENDING_COMMIT_OID.to_string());
    }

    /// `↑`/`↓` while placing a commit: the next destination down or up the
    /// tree, stopping at the ends.
    fn move_commit_dest(&mut self, dir: isize) {
        let Mode::CommitTarget { dests, index, .. } = &mut self.mode else {
            return;
        };
        let next = index.saturating_add_signed(dir).min(dests.len() - 1);
        if next == *index {
            return;
        }
        *index = next;
        self.show_pending_commit();
    }

    fn confirm_commit_target(&mut self) -> Option<Action> {
        let Mode::CommitTarget {
            source,
            dests,
            index,
            origin,
            ..
        } = std::mem::replace(&mut self.mode, Mode::Normal)
        else {
            return None;
        };
        // The placeholder row stays while the command runs; a failure has no
        // new commit to land on, so go back to where `c` was pressed.
        self.fallback_cursor = Some(origin);
        Some(Action::Commit {
            source,
            dest: dests[index].clone(),
        })
    }

    fn cancel_commit_target(&mut self) {
        let Mode::CommitTarget { origin, .. } = std::mem::replace(&mut self.mode, Mode::Normal)
        else {
            return;
        };
        self.rebuild_rows(&origin);
        self.diff.reset();
        self.notice = Some("commit cancelled".to_string());
    }

    /// `f`: the selection (else the cursor row) is what folds; `↑`/`↓` then
    /// walk the rows it can fold into, each tagged with what the fold does
    /// there, and nothing runs until one is confirmed.
    fn action_fold_start(&mut self) {
        let rows = self.picked_rows();
        let Some(origin) = self.current_row().map(|r| r.key.clone()) else {
            return;
        };
        let sources: Vec<String> = rows.iter().filter_map(|r| r.target.clone()).collect();
        if sources.is_empty() || sources.len() != rows.len() {
            self.notice = Some("fold: select source rows first".to_string());
            return;
        }
        let source_rows = self.mark_sources(&rows);
        let targets = match self.fold_targets(&rows, false) {
            Ok(targets) => targets,
            Err(notice) => {
                self.notice = Some(format!("fold: {}", notice));
                return;
            }
        };
        self.enter_fold_target(sources, source_rows, targets, origin, None);
    }

    fn enter_fold_target(
        &mut self,
        sources: Vec<String>,
        source_rows: Sources,
        targets: Vec<FoldTarget>,
        origin: String,
        hunks: Option<FoldHunks>,
    ) {
        let index = nearest_target(&self.rows, &targets, self.tree.cursor());
        self.mode = Mode::FoldTarget {
            sources,
            source_rows,
            targets,
            index,
            origin,
            hunks,
        };
        self.show_fold_target();
    }

    /// What an `F` pick leaves behind once the selector is done with it: the
    /// target walk, or a line in the entry the press opened saying why there
    /// is none.
    fn finish_fold_pick(
        &mut self,
        picked: Result<Option<Vec<FileEntry>>>,
        stamp: Stamp,
        sources: Vec<String>,
        targets: Vec<FoldTarget>,
        commit: Option<git2::Oid>,
        origin: String,
    ) {
        let picked = match picked {
            Ok(Some(picked)) if picked.iter().any(|f| f.hunks.iter().any(|h| h.selected)) => picked,
            Ok(_) => {
                self.log_line(Level::Warn, "Cancelled");
                self.notice = Some("fold cancelled".to_string());
                return;
            }
            Err(e) => {
                let text = e.to_string();
                self.log_line(Level::Error, &text);
                self.show_error(&text, AfterNotice::Nothing);
                return;
            }
        };
        let source_rows = self.hunk_sources(commit, &picked);
        let hunks = match commit {
            Some(_) => FoldHunks::Commit(picked),
            None => FoldHunks::Worktree { picked, stamp },
        };
        self.enter_fold_target(sources, source_rows, targets, origin, Some(hunks));
    }

    /// The gutter marks for picked hunks: the files they come from — the
    /// working files, or the source commit with its files covered.
    fn hunk_sources(&self, commit: Option<git2::Oid>, picked: &[FileEntry]) -> Sources {
        let paths: HashSet<&str> = picked
            .iter()
            .filter(|f| f.hunks.iter().any(|h| h.selected))
            .map(|f| f.path.as_str())
            .collect();
        let Some(oid) = commit else {
            return Sources {
                named: paths.iter().map(|p| working_file_key(p)).collect(),
                covered: HashSet::new(),
            };
        };
        let files = self
            .snapshot
            .info
            .commits
            .iter()
            .find(|c| c.oid == oid)
            .map_or(&[][..], |c| &c.files);
        Sources {
            named: HashSet::from([oid.to_string()]),
            covered: files
                .iter()
                .enumerate()
                .filter(|(_, f)| paths.contains(f.path.as_str()))
                .map(|(i, _)| commit_file_key(oid, i))
                .collect(),
        }
    }

    /// The rows `rows` can fold into, in tree order, as `loom fold` accepts
    /// them (Spec 007); the error is the notice for sources with none. Moving
    /// commits to a branch is not a fold here: the tree keeps fold to one
    /// thing going into another.
    ///
    /// With `hunks`, a commit's picked hunks move out of it rather than the
    /// whole commit folding: `[MOVE]` where `f` would say `[AMEND]`.
    fn fold_targets(&self, rows: &[&Row], hunks: bool) -> Result<Vec<FoldTarget>, &'static str> {
        let target = |row: &Row, effect| {
            row.target.clone().map(|arg| FoldTarget {
                key: row.key.clone(),
                arg,
                effect,
            })
        };
        let targets: Vec<FoldTarget> = match rows[0].kind {
            RowKind::LocalChanges { count: 0 } => return Err("no local changes"),
            RowKind::LocalChanges { .. } | RowKind::WorkingFile { .. } => self
                .rows
                .iter()
                .filter(|r| matches!(r.kind, RowKind::Commit { .. }))
                .filter_map(|r| target(r, FoldEffect::Amend))
                .collect(),
            RowKind::Commit { oid: source, .. } => {
                if rows.len() > 1 {
                    return Err("one commit at a time");
                }
                // Opened once for the fixup check; a repo that cannot answer
                // leaves the check to the command.
                let repo = git2::Repository::open(&self.snapshot.workdir).ok();
                let older = |oid: git2::Oid| {
                    repo.as_ref()
                        .is_none_or(|repo| repo.graph_descendant_of(source, oid).unwrap_or(true))
                };
                self.rows
                    .iter()
                    .filter_map(|r| match r.kind {
                        RowKind::LocalChanges { .. } => target(r, FoldEffect::Uncommit),
                        RowKind::Commit { oid, .. } if oid == source => target(r, FoldEffect::Noop),
                        // A fixup goes into a commit the source descends from.
                        RowKind::Commit { oid, .. } if older(oid) => target(
                            r,
                            if hunks {
                                FoldEffect::MoveFile
                            } else {
                                FoldEffect::Amend
                            },
                        ),
                        _ => None,
                    })
                    .collect()
            }
            RowKind::CommitFile { oid: owner, .. } => {
                if rows.len() > 1 {
                    return Err("one commit file at a time");
                }
                self.rows
                    .iter()
                    .filter_map(|r| match r.kind {
                        RowKind::LocalChanges { .. } => target(r, FoldEffect::Uncommit),
                        RowKind::Commit { oid, .. } if oid == owner => target(r, FoldEffect::Noop),
                        RowKind::Commit { .. } => target(r, FoldEffect::MoveFile),
                        _ => None,
                    })
                    .collect()
            }
            RowKind::BranchName { .. } => return Err("a branch cannot be folded"),
            _ => return Err("move to a file, commit, or commit file"),
        };
        if targets.iter().all(|t| t.effect == FoldEffect::Noop) {
            return Err("nothing to fold into");
        }
        Ok(targets)
    }

    /// Redraw the tree for the current fold target and put the cursor on it.
    fn show_fold_target(&mut self) {
        let Mode::FoldTarget { targets, index, .. } = &self.mode else {
            return;
        };
        let key = targets[*index].key.clone();
        self.rebuild_rows(&key);
        self.diff.reset();
    }

    /// `↑`/`↓` while picking a fold target: the next target down or up the
    /// tree, stopping at the ends.
    fn move_fold_target(&mut self, dir: isize) {
        let Mode::FoldTarget { targets, index, .. } = &mut self.mode else {
            return;
        };
        let next = index.saturating_add_signed(dir).min(targets.len() - 1);
        if next == *index {
            return;
        }
        *index = next;
        self.show_fold_target();
    }

    fn confirm_fold_target(&mut self) -> Option<Action> {
        let Mode::FoldTarget { targets, index, .. } = &self.mode else {
            return None;
        };
        let target = targets[*index].clone();
        if target.effect == FoldEffect::Noop {
            self.notice = Some("fold: nothing to do here, pick another target".to_string());
            return None;
        }
        let Mode::FoldTarget { sources, hunks, .. } =
            std::mem::replace(&mut self.mode, Mode::Normal)
        else {
            return None;
        };
        Some(match hunks {
            None => Action::Fold {
                sources,
                target: target.arg,
                hunks: None,
            },
            Some(FoldHunks::Worktree { picked, stamp }) => Action::FoldHunks {
                sources,
                picked,
                stamp,
                target: target.arg,
            },
            Some(FoldHunks::Commit(picked)) => {
                // Numbered against the commit and where its hunks land, as
                // `fold -p` re-reads them; `zz` names no commit.
                let into = (target.effect != FoldEffect::Uncommit).then_some(target.arg.as_str());
                let from = hunk_select::fingerprint(&sources[0], into, &picked);
                let ids = hunk_select::picked_ids(&picked);
                Action::Fold {
                    sources,
                    target: target.arg,
                    hunks: Some(HunkArgs::new(ids, Some(from))),
                }
            }
        })
    }

    fn cancel_fold_target(&mut self) {
        let Mode::FoldTarget { origin, .. } = std::mem::replace(&mut self.mode, Mode::Normal)
        else {
            return;
        };
        self.rebuild_rows(&origin);
        self.diff.reset();
        self.notice = Some("fold cancelled".to_string());
    }

    /// The tag the pending fold puts on `row`, its target.
    fn row_tag(&self, row: &Row) -> Option<&'static str> {
        match &self.mode {
            Mode::FoldTarget { targets, index, .. } => {
                let target = &targets[*index];
                (row.key == target.key).then(|| target.effect.tag())
            }
            Mode::MoveTarget { commit, .. } => (row.key == commit.to_string()).then_some("[MOVE]"),
            _ => None,
        }
    }

    /// `m`: the selected commit (else the cursor's) is moved; `↑`/`↓` then
    /// walk it through every place it can go, the tree redrawn with it there,
    /// and nothing runs until one is confirmed.
    fn action_move_start(&mut self) {
        let rows = self.picked_rows();
        let commit = match rows.as_slice() {
            [row] => match row.kind {
                RowKind::Commit { oid, .. } if row.target.is_some() => oid,
                _ => {
                    self.notice = Some("move: move to a commit".to_string());
                    return;
                }
            },
            [] => return,
            _ => {
                self.notice = Some("move: one commit at a time".to_string());
                return;
            }
        };
        let slots = self.move_slots(commit);
        if slots.len() < 2 {
            self.notice = Some("move: nowhere else to put it".to_string());
            return;
        }
        let index = slots
            .iter()
            .position(|s| *s == MoveSlot::Stay)
            .expect("always offered");
        self.clear_selection();
        self.mode = Mode::MoveTarget {
            commit,
            slots,
            index,
        };
        self.show_move_target();
    }

    /// Every distinct place `commit` can be moved to, in tree order, `Stay`
    /// among them. The candidates are `--above`/`--below` every other commit
    /// and every branch's tip, ranked by the row they sit next to; candidates
    /// drawing the same tree are one place, spelled the first way.
    fn move_slots(&self, commit: git2::Oid) -> Vec<MoveSlot> {
        let info = &self.snapshot.info;
        let parent_of = |oid: git2::Oid| {
            info.commits
                .iter()
                .find(|c| c.oid == oid)
                .and_then(|c| c.parent_oid)
        };
        let parent = parent_of(commit);
        // Three ranks per row: above it, the row itself, below it. A branch
        // tip ranks with the row after its name, behind an `--above` there.
        let mut candidates: Vec<(usize, MoveSlot)> = Vec::new();
        for (i, row) in self.rows.iter().enumerate() {
            match &row.kind {
                RowKind::Commit { oid, .. } if *oid == commit => {
                    candidates.push((3 * i + 1, MoveSlot::Stay));
                }
                RowKind::Commit { oid, .. } if row.target.is_some() => {
                    // `fold --above/--below` refuses the commit's own place,
                    // even where the move would carry a branch name along.
                    if parent != Some(*oid) {
                        candidates.push((3 * i, MoveSlot::Above(*oid)));
                    }
                    if parent_of(*oid) != Some(commit) {
                        candidates.push((3 * i + 2, MoveSlot::Below(*oid)));
                    }
                }
                RowKind::BranchName { name, .. }
                    if !info
                        .branches
                        .iter()
                        .any(|b| b.name == *name && b.tip_oid == commit) =>
                {
                    candidates.push((3 * i + 3, MoveSlot::Branch(name.clone())));
                }
                _ => {}
            }
        }
        candidates.sort_by_key(|(rank, slot)| (*rank, matches!(slot, MoveSlot::Branch(_))));

        let mut seen = HashSet::from([self.move_signature(commit, &MoveSlot::Stay)]);
        candidates
            .into_iter()
            .map(|(_, slot)| slot)
            .filter(|slot| {
                *slot == MoveSlot::Stay || seen.insert(self.move_signature(commit, slot))
            })
            .collect()
    }

    /// The tree `commit` at `slot` draws, as row keys and connectors: two
    /// slots with the same one are the same move as far as the tree shows.
    fn move_signature(&self, commit: git2::Oid, slot: &MoveSlot) -> Vec<String> {
        let sections = self.snapshot.sections(Some(Preview::Move {
            oid: commit,
            slot: slot.clone(),
        }));
        status_tree::build_rows(&sections, &self.snapshot.ids, &self.expanded)
            .into_iter()
            .map(|r| match r.kind {
                RowKind::Spacer(text) => text.to_string(),
                _ => r.key,
            })
            .collect()
    }

    /// Redraw the tree with the commit at the current slot and put the cursor
    /// on it.
    fn show_move_target(&mut self) {
        let Mode::MoveTarget { commit, .. } = &self.mode else {
            return;
        };
        let key = commit.to_string();
        self.rebuild_rows(&key);
        self.diff.reset();
    }

    /// `↑`/`↓` while moving a commit: the next place up or down the tree,
    /// stopping at the ends.
    fn move_move_target(&mut self, dir: isize) {
        let Mode::MoveTarget { slots, index, .. } = &mut self.mode else {
            return;
        };
        let next = index.saturating_add_signed(dir).min(slots.len() - 1);
        if next == *index {
            return;
        }
        *index = next;
        self.show_move_target();
    }

    fn confirm_move_target(&mut self) -> Option<Action> {
        let Mode::MoveTarget { slots, index, .. } = &self.mode else {
            return None;
        };
        if slots[*index] == MoveSlot::Stay {
            self.notice = Some("move: it is already here, pick another place".to_string());
            return None;
        }
        let Mode::MoveTarget {
            commit,
            mut slots,
            index,
        } = std::mem::replace(&mut self.mode, Mode::Normal)
        else {
            return None;
        };
        // The rewrite renames the commit, so there is no key to follow: the
        // cursor stays on the row the preview drew it at.
        Some(Action::Move {
            commit,
            slot: slots.swap_remove(index),
        })
    }

    fn cancel_move_target(&mut self) {
        let Mode::MoveTarget { commit, .. } = std::mem::replace(&mut self.mode, Mode::Normal)
        else {
            return;
        };
        self.rebuild_rows(&commit.to_string());
        self.diff.reset();
        self.notice = Some("move cancelled".to_string());
    }

    /// `b`: start naming a new branch, drawn in the tree as if it already
    /// existed at the cursor commit or branch tip (its `-t` target), else at
    /// the base. Nothing runs until the name is confirmed.
    fn action_new_branch(&mut self) {
        let Some(row) = self.current_row() else {
            return;
        };
        let origin = row.key.clone();
        let info = &self.snapshot.info;
        let (target, tip) = match &row.kind {
            RowKind::Commit { oid, .. } => (row.target.clone(), *oid),
            RowKind::BranchName { name, .. } => {
                let tip = info
                    .branches
                    .iter()
                    .find(|b| b.name == *name)
                    .map_or(info.upstream.merge_base_oid, |b| b.tip_oid);
                (row.target.clone(), tip)
            }
            _ => (None, info.upstream.merge_base_oid),
        };
        self.mode = Mode::NewBranch {
            target,
            tip,
            origin,
            field: TextField::new(""),
        };
        self.rebuild_rows(&branch_key(NEW_BRANCH_NAME));
        self.diff.reset();
    }

    /// Whether a branch name is being typed over a row in the tree.
    fn editing_name(&self) -> bool {
        matches!(
            self.mode,
            Mode::RenameBranch { .. } | Mode::NewBranch { .. }
        )
    }

    /// Route a key to whichever branch-name field is open.
    fn handle_name_key(&mut self, code: KeyCode, modifiers: KeyModifiers) -> Option<Action> {
        match self.mode {
            Mode::RenameBranch { .. } => self.handle_rename_key(code, modifiers),
            Mode::NewBranch { .. } => self.handle_new_branch_key(code, modifiers),
            _ => None,
        }
    }

    /// A key while a new branch's name is being typed on its placeholder row.
    /// Enter creates it; Esc, Ctrl-C, or an empty name drop the row again.
    fn handle_new_branch_key(&mut self, code: KeyCode, modifiers: KeyModifiers) -> Option<Action> {
        let Mode::NewBranch {
            target,
            origin,
            field,
            ..
        } = &mut self.mode
        else {
            return None;
        };
        let cancelled = code == KeyCode::Esc
            || (code == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL));
        if !cancelled && code != KeyCode::Enter {
            field.handle_key(code, modifiers);
            return None;
        }
        let name = field.value().trim().to_string();
        if cancelled || name.is_empty() {
            let origin = origin.clone();
            self.mode = Mode::Normal;
            self.rebuild_rows(&origin);
            self.diff.reset();
            self.notice = Some("branch cancelled".to_string());
            return None;
        }
        let target = target.clone();
        self.fallback_cursor = Some(origin.clone());
        self.mode = Mode::Normal;
        // Keep the row readable while the command runs, then follow the
        // branch to its real row once the tree reloads.
        let key = branch_key(NEW_BRANCH_NAME);
        if let Some(row) = self.rows.iter_mut().find(|r| r.key == key)
            && let RowKind::BranchName { name: shown, .. } = &mut row.kind
        {
            *shown = name.clone();
        }
        self.next_cursor = Some(branch_key(&name));
        Some(Action::NewBranch { name, target })
    }

    /// The one commit `s` or `S` splits: the commit the rows are, or the one
    /// the commit files among them belong to.
    fn split_source(&self, rows: &[&Row]) -> Result<git2::Oid, &'static str> {
        let mut commits = HashSet::new();
        for row in rows {
            match row.kind {
                RowKind::Commit { oid, .. } | RowKind::CommitFile { oid, .. }
                    if row.target.is_some() =>
                {
                    commits.insert(oid);
                }
                _ => return Err("move to a commit or commit file"),
            }
        }
        match commits.into_iter().collect::<Vec<_>>()[..] {
            [commit] => Ok(commit),
            [] => Err("move to a commit or commit file"),
            _ => Err("one commit at a time"),
        }
    }

    /// `s`: the selected commit files (else the cursor's) become the first
    /// commit, the rest of their commit the second; a commit row is `S`.
    fn action_split(&mut self) -> Option<Action> {
        let rows = self.picked_rows();
        let commit = match self.split_source(&rows) {
            Ok(commit) => commit,
            Err(notice) => {
                self.notice = Some(format!("split: {}", notice));
                return None;
            }
        };
        let files: Vec<String> = rows
            .iter()
            .filter_map(|r| match &r.kind {
                RowKind::CommitFile { path, .. } => {
                    Some(repo::cwd_relative_path(path, &self.snapshot.cwd_prefix))
                }
                _ => None,
            })
            .collect();
        // A commit row names no files to take out: pick its hunks, as `S` does.
        if files.is_empty() {
            self.action_split_patch_start();
            return None;
        }
        let total = self
            .snapshot
            .info
            .commits
            .iter()
            .find(|c| c.oid == commit)
            .map_or(0, |c| c.files.len());
        if total < 2 {
            self.notice = Some("split: one file only, split its hunks with S".to_string());
            return None;
        }
        if files.len() >= total {
            self.notice = Some("split: leave at least one file for the second commit".to_string());
            return None;
        }
        Some(Action::Split {
            commit: commit.to_string(),
            files,
            hunks: None,
        })
    }

    /// `S`: `s` with the hunks picked first, over the commit's whole diff
    /// whatever the cursor or selection, as `C` shows every local change. The
    /// press starts the read on a worker, as `C` does.
    fn action_split_patch_start(&mut self) {
        let rows = self.picked_rows();
        let Some(origin) = self.current_row().map(|r| r.key.clone()) else {
            return;
        };
        let commit = match self.split_source(&rows) {
            Ok(commit) => commit,
            Err(notice) => {
                self.notice = Some(format!("split: {}", notice));
                return;
            }
        };
        let command = format!("loom split -p {}", self.sid_of(&commit.to_string()));
        self.start_pick(command, origin, PickFor::Split { commit }, move |workdir| {
            let entries = staging::collect_commit_hunks(workdir, &commit.to_string(), &[])?;
            Ok((entries, Vec::new()))
        });
    }

    /// Whether an `S` read leaves nothing to split, after saying so: with fewer
    /// than two entries no pick can keep something on each side.
    fn refuse_split_pick(&mut self, entries: &[FileEntry]) -> bool {
        let (line, notice) = match entries.iter().map(|f| f.hunks.len()).sum::<usize>() {
            0 => ("No changes to pick", "split: no hunks to pick"),
            1 => ("Only one hunk", "split: one hunk only, nothing to split"),
            _ => return false,
        };
        self.log_line(Level::Warn, line);
        self.notice = Some(notice.to_string());
        true
    }

    /// The split an `S` pick confirms, handed to `loom split -p` by id (Spec
    /// 019), or `None` after a line in the entry the press opened saying why
    /// there is none. Both halves must keep something, as `split -p` checks.
    fn finish_split_pick(
        &mut self,
        picked: Result<Option<Vec<FileEntry>>>,
        commit: git2::Oid,
    ) -> Option<Action> {
        let picked = match picked {
            Ok(Some(picked)) if picked.iter().any(|f| f.hunks.iter().any(|h| h.selected)) => picked,
            Ok(_) => {
                self.log_line(Level::Warn, "Cancelled");
                self.notice = Some("split cancelled".to_string());
                return None;
            }
            Err(e) => {
                let text = e.to_string();
                self.log_line(Level::Error, &text);
                self.show_error(&text, AfterNotice::Nothing);
                return None;
            }
        };
        if picked.iter().all(|f| f.hunks.iter().all(|h| h.selected)) {
            self.log_line(Level::Warn, "Nothing left for the second commit");
            self.notice = Some("split: leave at least one hunk for the second commit".to_string());
            return None;
        }
        let commit = commit.to_string();
        let from = hunk_select::fingerprint(&commit, None, &picked);
        let ids = hunk_select::picked_ids(&picked);
        Some(Action::Split {
            commit,
            files: Vec::new(),
            hunks: Some(HunkArgs::new(ids, Some(from))),
        })
    }

    /// `d`: drop the selected working files together, else the cursor row (a
    /// commit, branch, working file, or the `[local changes]` header for
    /// `drop zz`). Only files can go together, as in the CLI.
    fn action_drop(&mut self) -> Option<Action> {
        let rows = self.picked_rows();
        let droppable = |row: &Row| {
            matches!(
                row.kind,
                RowKind::Commit { .. }
                    | RowKind::BranchName { .. }
                    | RowKind::WorkingFile { .. }
                    | RowKind::LocalChanges { .. }
            ) && row.target.is_some()
        };
        if rows.is_empty() || !rows.iter().all(|r| droppable(r)) {
            self.notice =
                Some("drop: move to a commit, branch, file, or local changes".to_string());
            return None;
        }
        if rows.len() > 1
            && !rows
                .iter()
                .all(|r| matches!(r.kind, RowKind::WorkingFile { .. }))
        {
            self.notice = Some("drop: only files can be dropped together".to_string());
            return None;
        }
        let targets = rows.iter().filter_map(|r| r.target.clone()).collect();
        Some(Action::Drop { targets })
    }

    /// `a`: absorb the selected working files, else the cursor's, else every
    /// local change. The command itself shows its plan to confirm.
    fn action_absorb(&mut self) -> Option<Action> {
        if self.snapshot.info.working_changes.is_empty() {
            self.notice = Some("absorb: no local changes".to_string());
            return None;
        }
        let rows = self.picked_rows();
        let is_file = |r: &&Row| matches!(r.kind, RowKind::WorkingFile { .. });
        if !self.selected.is_empty()
            && !rows
                .iter()
                .all(|r| is_file(r) || matches!(r.kind, RowKind::LocalChanges { .. }))
        {
            self.notice = Some("absorb: select files or local changes".to_string());
            return None;
        }
        let files = if rows.iter().all(is_file) {
            rows.iter().filter_map(|r| r.target.clone()).collect()
        } else {
            Vec::new()
        };
        Some(Action::Absorb { files })
    }

    /// `r`: reword the commit under the cursor, or start editing the branch
    /// name in place.
    fn action_reword(&mut self) -> Option<Action> {
        let row = self.current_row()?;
        let target = row.target.clone();
        match (&row.kind, target) {
            (RowKind::Commit { .. }, Some(target)) => Some(Action::Reword { target, name: None }),
            (RowKind::BranchName { name, .. }, Some(target)) => {
                let field = TextField::new(name);
                self.mode = Mode::RenameBranch {
                    branch: target,
                    field,
                };
                None
            }
            _ => {
                self.notice = Some("reword: move to a commit or branch".to_string());
                None
            }
        }
    }

    /// A key while the branch name is being edited in the tree. Enter runs
    /// the rename, Esc cancels, everything else edits the field.
    fn handle_rename_key(&mut self, code: KeyCode, modifiers: KeyModifiers) -> Option<Action> {
        let Mode::RenameBranch { branch, field } = &mut self.mode else {
            return None;
        };
        if code == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL) {
            self.mode = Mode::Normal;
            self.notice = Some("rename cancelled".to_string());
            return None;
        }
        match code {
            KeyCode::Enter => {
                let name = field.value().trim().to_string();
                let branch = branch.clone();
                if name.is_empty() {
                    self.notice = Some("rename: the name cannot be empty".to_string());
                    return None;
                }
                self.mode = Mode::Normal;
                if name == branch {
                    self.notice = Some("rename: name unchanged".to_string());
                    return None;
                }
                // Follow the branch to its new row rather than reloading to
                // the top of the tree.
                self.next_cursor = Some(branch_key(&name));
                Some(Action::Reword {
                    target: branch,
                    name: Some(name),
                })
            }
            KeyCode::Esc => {
                self.mode = Mode::Normal;
                self.notice = Some("rename cancelled".to_string());
                None
            }
            _ => {
                field.handle_key(code, modifiers);
                None
            }
        }
    }

    // -- rendering ----------------------------------------------------------------

    fn render_tree(&mut self, frame: &mut Frame, area: Rect, focused: bool) {
        let cursor = self.tree.cursor();
        let editing = match &self.mode {
            Mode::RenameBranch { branch, field } => Some((branch_key(branch), field)),
            Mode::NewBranch { field, .. } => Some((branch_key(NEW_BRANCH_NAME), field)),
            _ => None,
        };
        let items: Vec<ListItem> = self
            .rows
            .iter()
            .enumerate()
            .map(|(i, row)| {
                ListItem::new(row_line(
                    row,
                    self.theme,
                    &self.snapshot.cwd_prefix,
                    self.row_mark(row),
                    self.row_tag(row),
                    i == cursor,
                    editing
                        .as_ref()
                        .filter(|(key, _)| *key == row.key)
                        .map(|(_, f)| *f),
                ))
            })
            .collect();

        let title = match &self.mode {
            Mode::Normal => " Status ".to_string(),
            Mode::FoldTarget { sources, hunks, .. } => {
                let of = if hunks.is_some() { "hunks of " } else { "" };
                self.title_sources(sources, area.width, |what| {
                    format!(" Fold {}{} into... ", of, what)
                })
            }
            Mode::CommitTarget {
                source,
                dests,
                index,
                ..
            } => {
                let dest = match &dests[*index] {
                    CommitDest::Integration => &self.snapshot.info.branch_name,
                    CommitDest::Branch(name) => name,
                };
                let title = |what: &str| format!(" Commit {} → [{}] ", what, dest);
                match source {
                    CommitSource::Files(files) => self.title_sources(files, area.width, title),
                    CommitSource::Index => title("the index"),
                }
            }
            Mode::MoveTarget { commit, .. } => {
                format!(" Move {} ", self.sid_of(&commit.to_string()))
            }
            Mode::RenameBranch { .. } => " Rename branch ".to_string(),
            Mode::NewBranch { .. } => " New branch ".to_string(),
        };
        let block = pane_block(&title, self.theme, focused);
        self.tree
            .render(frame, area, items, block, self.theme.file_selected);
    }

    fn render_diff(&mut self, frame: &mut Frame, area: Rect, focused: bool) {
        let key = self.ensure_diff_cached();
        let lines = borrowed_lines(&self.diff_cache[&key]);
        let block = pane_block(" Diff ", self.theme, focused);
        self.diff.render(frame, area, lines, block);
    }

    /// Compute and cache the diff for the cursor row (a git call on first
    /// visit); returns the cache key. Called from the event path so rendering
    /// never shells out.
    fn ensure_diff_cached(&mut self) -> String {
        let key = match self.rows.get(self.tree.cursor()) {
            Some(row) => row.key.clone(),
            None => String::new(),
        };
        if !self.diff_cache.contains_key(&key) {
            let lines = match self.rows.get(self.tree.cursor()) {
                Some(row) => {
                    let text = match &self.mode {
                        Mode::CommitTarget { source, .. }
                            if row.key == PENDING_COMMIT_OID.to_string() =>
                        {
                            match source {
                                CommitSource::Files(files) => {
                                    pending_commit_diff(&self.snapshot, files)
                                }
                                CommitSource::Index => staged_commit_diff(&self.snapshot),
                            }
                        }
                        _ => diff_text(&self.snapshot, row),
                    };
                    colorize_diff(&text, self.theme)
                }
                None => vec![Line::from("")],
            };
            self.diff_cache.insert(key.clone(), lines);
        }
        key
    }
}

/// Borrow cached lines for rendering without copying their strings.
fn borrowed_lines<'a>(lines: &'a [Line<'static>]) -> Vec<Line<'a>> {
    lines
        .iter()
        .map(|line| {
            Line::from(
                line.spans
                    .iter()
                    .map(|s| Span::styled(s.content.as_ref(), s.style))
                    .collect::<Vec<Span<'a>>>(),
            )
            .style(line.style)
        })
        .collect()
}

// ── Shell integration ────────────────────────────────────────────────────

impl ShellApp for App<'_> {
    type Exit = Outcome;

    fn config(&self) -> ShellConfig {
        ShellConfig { split: (45, 55) }
    }

    fn theme(&self) -> &TuiTheme {
        self.theme
    }

    fn quit_exit(&mut self) -> Outcome {
        Outcome::Quit
    }

    fn handle_key(
        &mut self,
        focused: PaneId,
        code: KeyCode,
        modifiers: KeyModifiers,
    ) -> KeyResult<Outcome> {
        if self.popup.is_some() {
            self.handle_popup_key(code, modifiers);
        } else if self.editing_name() {
            if let Some(action) = self.handle_name_key(code, modifiers) {
                self.start_action(action);
            }
        } else if self.running.is_some() {
            // The log and help are read-only; everything else waits.
            match code {
                KeyCode::Char('L') => self.open_log(),
                KeyCode::Char('?') => self.open_help(),
                _ => self.notice = Some("an action is running…".to_string()),
            }
        } else if self.pick.is_some() {
            // The selector is about to own the screen, so nothing may start
            // here — including a second `C`. Only a read still running reaches
            // this: one that is done has taken the terminal, since the loop
            // polls before it reads a key. Covering the whole pick regardless
            // keeps that reasoning off the list of things to know.
            match code {
                KeyCode::Char('L') => self.open_log(),
                KeyCode::Char('?') => self.open_help(),
                // Marked, not dropped: the worker reads with `git`, so
                // letting it outlive the pick would put it beside whatever
                // action the freed keyboard starts next, over the same index.
                // The result is thrown away when it lands instead.
                KeyCode::Esc => self.cancel_pick(false),
                // Queued like the cancel above rather than answered now: the
                // worker is what the pick waits for, and leaving on top of it
                // is what the waiting exists to prevent.
                KeyCode::Char('q') => self.cancel_pick(true),
                KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => {
                    self.cancel_pick(true)
                }
                // Deliberately silent: a notice outranks the mode hint, so
                // saying "still reading" here would replace the spinner that
                // says it, and stay there until the next key.
                _ => {}
            }
        } else {
            self.handle_tree_key(focused, code);
        }
        // Deep handlers (Esc, notice dismissal) report exits through the
        // outcome field.
        match self.outcome.take() {
            Some(outcome) => KeyResult::Exit(outcome),
            None => {
                self.ensure_diff_cached();
                KeyResult::Handled
            }
        }
    }

    fn handle_mouse(&mut self, pane: PaneId, kind: MouseEventKind, pos: Position, area: Rect) {
        match pane {
            PaneId::Left => match kind {
                // A click while a commit is being placed would take the cursor
                // off the placeholder, and one picking a fold target may only
                // land on a target. The wheel is safe: it goes through
                // `move_cursor`, which moves the destination in those modes.
                MouseEventKind::Down(MouseButton::Left)
                    if !matches!(
                        self.mode,
                        Mode::CommitTarget { .. } | Mode::MoveTarget { .. }
                    ) =>
                {
                    let Some(clicked) = self.tree.hit_test(area, pos.y) else {
                        return;
                    };
                    let Some(key) = self
                        .rows
                        .get(clicked)
                        .filter(|r| r.focusable)
                        .map(|r| r.key.clone())
                    else {
                        return;
                    };
                    if let Mode::FoldTarget { targets, index, .. } = &mut self.mode {
                        if let Some(i) = targets.iter().position(|t| t.key == key) {
                            *index = i;
                            self.show_fold_target();
                        }
                    } else {
                        self.tree.set_cursor(clicked);
                        self.diff.reset();
                    }
                }
                MouseEventKind::ScrollUp => self.move_cursor(-1),
                MouseEventKind::ScrollDown => self.move_cursor(1),
                _ => {}
            },
            PaneId::Right => match kind {
                MouseEventKind::ScrollUp => self.diff.scroll_by(-3),
                MouseEventKind::ScrollDown => self.diff.scroll_by(3),
                _ => {}
            },
        }
        self.ensure_diff_cached();
    }

    fn render_pane(&mut self, frame: &mut Frame, pane: PaneId, area: Rect, focused: bool) {
        match pane {
            PaneId::Left => self.render_tree(frame, area, focused),
            PaneId::Right => self.render_diff(frame, area, focused),
        }
    }

    fn render_overlay(&mut self, frame: &mut Frame, area: Rect) {
        match &mut self.popup {
            Some(Popup::Prompt { prompt, .. }) => prompt.render(frame, area, self.theme),
            Some(Popup::Notice { notice, .. }) => notice.render(frame, area, self.theme),
            Some(Popup::Log { scroll }) => {
                popup::render_log(frame, area, &self.log, scroll, self.theme)
            }
            Some(Popup::Help { scroll }) => {
                popup::render_help(frame, area, HELP, scroll, self.theme)
            }
            None => {}
        }
    }

    fn modal_active(&self) -> bool {
        // A name field owns the keyboard too: `q` and Tab must reach it.
        self.popup.is_some() || self.running.is_some() || self.editing_name() || self.pick.is_some()
    }

    fn poll_background(&mut self) -> Tick<Outcome> {
        let mut changed = false;
        loop {
            match self.requests.try_recv() {
                Ok(Request::Suspend(ack)) => return Tick::Suspend(ack),
                Ok(request) => {
                    self.handle_request(request);
                    changed = true;
                }
                Err(_) => break,
            }
        }
        // After the drain, never before: what the pick's own worker reported
        // reaches its log entry before the selector covers the screen. A line
        // the action that just ended left queued lands in that entry too —
        // accepted, since it is misfiled rather than lost. No action can still
        // be running — `C` is a tree key — which the early return counts on: it
        // skips the action's own tick and join.
        match self.advance_pick() {
            // The shell hands the live terminal over without tearing it down,
            // so the selector draws straight onto this frame — which must be
            // nobody else's. A popup owns the screen and the keyboard, and
            // would still own them once the selector returned.
            PickTick::Ready { .. } if self.popup.is_none() => return Tick::TakeOver,
            // Held under a popup: the frame still carries the spinner of a read
            // that has finished — offering an `Esc` the popup now takes. One
            // draw clears it; there is nothing to animate after that.
            PickTick::Ready { first } => changed |= first,
            PickTick::Redraw => changed = true,
            PickTick::Nothing => {}
        }
        if let Some(running) = &mut self.running {
            running.ticks += 1;
            if running.handle.is_finished() {
                let running = self.running.take().expect("checked above");
                let result = running.handle.join().unwrap_or_else(|_| {
                    Err(anyhow::anyhow!(
                        "the action crashed — run `loom trace` for what it did"
                    ))
                });
                if self.finish_action(result) {
                    self.after_action();
                }
            }
            changed = true;
        }
        if let Some(outcome) = self.outcome.take() {
            return Tick::Exit(outcome);
        }
        if changed { Tick::Redraw } else { Tick::Idle }
    }

    /// The hunk selector for `C` or `F`, drawn on the tree's own terminal.
    /// What `C` keeps is staged at once, as `loom add -p` would; the commit
    /// that follows takes the index, so the tree reloads first to show it.
    /// What `F` keeps waits for its target, staging nothing.
    fn take_over(&mut self, terminal: &mut ratatui::DefaultTerminal) {
        // Only a read that is done is this to consume. Nothing else asks for
        // the terminal, so the other arm is a belt: it puts back rather than
        // drops, since dropping a running read would strand its worker.
        let (entries, stamp, origin, purpose) = match self.pick.take() {
            Some(Pick::Ready {
                entries,
                stamp,
                origin,
                purpose,
            }) => (entries, stamp, origin, purpose),
            other => {
                self.pick = other;
                return;
            }
        };
        match purpose {
            PickFor::Commit => {
                let staged = self.select_and_stage(entries, terminal);
                self.finish_pick(staged, origin);
            }
            PickFor::Split { commit } => {
                if self.refuse_split_pick(&entries) {
                    return;
                }
                let picked = run_hunk_selector_nested(
                    entries,
                    TuiTheme::from_graph_theme(&self.graph_theme),
                    "SPLIT",
                    terminal,
                );
                if let Some(action) = self.finish_split_pick(picked, commit) {
                    self.start_action(action);
                }
            }
            PickFor::Fold {
                sources,
                targets,
                commit,
            } => {
                if entries.is_empty() {
                    self.log_line(Level::Warn, "No changes to pick");
                    self.notice = Some("fold: no hunks to pick".to_string());
                    return;
                }
                let picked = run_hunk_selector_nested(
                    entries,
                    TuiTheme::from_graph_theme(&self.graph_theme),
                    "FOLD",
                    terminal,
                );
                self.finish_fold_pick(picked, stamp, sources, targets, commit, origin);
            }
        }
    }

    /// The worker holds the terminal (an editor); its `Resume` ends the wait.
    /// A worker that dies without one ends it too.
    fn wait_for_resume(&mut self) {
        loop {
            match self.requests.recv_timeout(Duration::from_millis(100)) {
                Ok(Request::Resume) | Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
                    return;
                }
                Ok(request) => self.handle_request(request),
                Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
                    if self.running.as_ref().is_none_or(|r| r.handle.is_finished()) {
                        return;
                    }
                }
            }
        }
    }

    fn notice(&self) -> Option<&str> {
        self.notice.as_deref()
    }

    fn clear_notice(&mut self) {
        self.notice = None;
    }

    fn mode_hint(&self) -> Option<String> {
        if let Some(running) = &self.running {
            let frame = SPINNER_FRAMES[(running.ticks / 2) % SPINNER_FRAMES.len()];
            let detail = running
                .spinner
                .as_deref()
                .map(|s| format!(" — {}", s))
                .unwrap_or_default();
            return Some(format!(" {} {}{}", frame, running.command, detail));
        }
        if let Some(Pick::Collecting {
            ticks,
            cancelled,
            purpose,
            ..
        }) = &self.pick
        {
            let frame = SPINNER_FRAMES[(ticks / 2) % SPINNER_FRAMES.len()];
            let what = match purpose {
                _ if *cancelled => "cancelling: waiting for the read to end",
                PickFor::Fold {
                    commit: Some(_), ..
                }
                | PickFor::Split { .. } => "reading the commit — Esc to cancel",
                _ => "reading the working tree — Esc to cancel",
            };
            return Some(format!(" {} {}: {}", frame, purpose.label(), what));
        }
        match &self.mode {
            Mode::FoldTarget { .. } => {
                Some(" fold: ↑/↓ choose the target, Enter to fold, Esc to cancel".to_string())
            }
            Mode::CommitTarget { .. } => Some(
                " commit: ↑/↓ choose the destination, Enter to commit, Esc to cancel".to_string(),
            ),
            Mode::MoveTarget { .. } => {
                Some(" move: ↑/↓ choose the place, Enter to move, Esc to cancel".to_string())
            }
            Mode::RenameBranch { .. } => Some(
                " rename: type the new branch name, Enter to confirm, Esc to cancel".to_string(),
            ),
            Mode::NewBranch { .. } => Some(
                " branch: type the new branch name, Enter to create, Esc to cancel".to_string(),
            ),
            Mode::Normal => None,
        }
    }

    fn status_hints(&self, _focused: PaneId) -> Vec<Cow<'static, str>> {
        vec![
            "Commit: c/C".into(),
            "Fold: f/F".into(),
            "Branch: b".into(),
            "Drop: d".into(),
            "Help: ?".into(),
            "Quit: q".into(),
        ]
    }
}

/// Keys the log and help popups scroll with.
fn scroll_popup(scroll: &mut DiffPane, code: KeyCode) {
    match code {
        KeyCode::Up | KeyCode::Char('k') => scroll.scroll_by(-1),
        KeyCode::Down | KeyCode::Char('j') => scroll.scroll_by(1),
        KeyCode::PageUp => scroll.scroll_page(-1),
        KeyCode::PageDown => scroll.scroll_page(1),
        _ => {}
    }
}

/// The focusable row at or above `index`, else the first focusable one.
fn nearest_focusable(rows: &[Row], index: usize) -> Option<usize> {
    if rows.is_empty() {
        return None;
    }
    let start = index.min(rows.len() - 1);
    rows[..=start]
        .iter()
        .rposition(|r| r.focusable)
        .or_else(|| rows.iter().position(|r| r.focusable))
}

/// Index of the target a fold starts on: the nearest `[NOOP]` one, so commit
/// sources start on themselves and a commit file on its commit, else the one
/// nearest `cursor`, the lower on a tie.
fn nearest_target(rows: &[Row], targets: &[FoldTarget], cursor: usize) -> usize {
    (0..targets.len())
        .min_by_key(|&i| {
            let at = rows
                .iter()
                .position(|r| r.key == targets[i].key)
                .unwrap_or(usize::MAX);
            (
                targets[i].effect != FoldEffect::Noop,
                at.abs_diff(cursor),
                at < cursor,
            )
        })
        .unwrap_or(0)
}

// ── Row rendering ────────────────────────────────────────────────────────

/// Render one tree row as a styled line; `editing` replaces the branch name
/// with the field being typed. The first span is the selection/source gutter.
fn row_line(
    row: &Row,
    theme: &TuiTheme,
    cwd_prefix: &str,
    mark: RowMark,
    tag: Option<&'static str>,
    is_cursor: bool,
    editing: Option<&TextField>,
) -> Line<'static> {
    // On the cursor row the selection background swallows regular dim text.
    let dim = if is_cursor {
        theme.dim_selected
    } else {
        theme.dim
    };
    let mut spans: Vec<Span<'static>> = vec![match mark {
        RowMark::Selected => Span::styled("✓ ", theme.selection),
        RowMark::Source => Span::styled("▸ ", theme.source),
        // Covered, not named: the same glyph, without the source's weight.
        RowMark::Covered => Span::styled("▸ ", dim),
        RowMark::None => Span::raw("  "),
    }];

    let display = |path: &str| crate::core::repo::cwd_relative_path(path, cwd_prefix);

    match &row.kind {
        RowKind::LocalChanges { count } => {
            spans.push(Span::styled("╭─ ", theme.graph));
            spans.push(Span::styled(row.sid.clone(), theme.shortid));
            if let Some(tag) = tag {
                spans.push(Span::raw(" "));
                spans.push(Span::styled(tag, theme.pending_tag));
            }
            spans.push(Span::styled(" [", dim));
            spans.push(Span::styled("local changes", theme.label));
            spans.push(Span::styled("]", dim));
            if *count == 0 {
                spans.push(Span::styled(" no changes", dim));
            } else if !row.expanded {
                spans.push(Span::styled(format!(" ({} files)", count), dim));
            }
        }
        RowKind::WorkingFile {
            path,
            index,
            worktree,
        } => {
            spans.push(Span::styled("│   ", theme.graph));
            spans.push(Span::styled(row.sid.clone(), theme.shortid));
            let group = graph::file_group(*index, *worktree);
            if group == graph::FileGroup::Untracked {
                spans.push(Span::styled(" ⁕ ", theme.untracked));
            } else if group == graph::FileGroup::Conflicted {
                spans.push(Span::styled(
                    " !! ",
                    theme.unstaged_status.add_modifier(Modifier::BOLD),
                ));
            } else {
                spans.push(Span::raw(" "));
                spans.push(Span::styled(index.to_string(), theme.staged_status));
                spans.push(Span::styled(worktree.to_string(), theme.unstaged_status));
                spans.push(Span::raw(" "));
            }
            spans.push(Span::raw(display(path)));
        }
        RowKind::BranchName {
            name,
            remote,
            connector,
            ..
        } => {
            spans.push(Span::styled(format!("{} ", connector), theme.graph));
            if row.key == branch_key(NEW_BRANCH_NAME) {
                // A branch being created has no short ID; say what the row is
                // instead of faking the column.
                spans.push(Span::styled("[CREATE BRANCH]", theme.pending_tag));
            } else {
                spans.push(Span::styled(row.sid.clone(), theme.shortid));
            }
            spans.push(Span::styled(" [", dim));
            match editing {
                Some(field) => spans.extend(field.spans(theme)),
                None => spans.push(Span::styled(name.clone(), theme.branch)),
            }
            spans.push(Span::styled("]", dim));
            match remote {
                Some(RemoteStatus::Synced) => spans.push(Span::styled(" ✓", theme.remote_synced)),
                Some(RemoteStatus::Different) => spans.push(Span::styled(" ↑", theme.remote_ahead)),
                Some(RemoteStatus::Gone) => spans.push(Span::styled(" ✗", theme.remote_gone)),
                None => {}
            }
        }
        RowKind::Commit {
            oid,
            message,
            dot_color,
            file_count,
        } => {
            match dot_color {
                Some(idx) => {
                    spans.push(Span::styled("│", theme.graph));
                    spans.push(Span::styled(
                        "●",
                        theme.branch_dots[idx % theme.branch_dots.len()],
                    ));
                    spans.push(Span::raw("  "));
                }
                None => {
                    spans.push(Span::styled("●", theme.graph));
                    spans.push(Span::raw("   "));
                }
            }
            if *oid == PENDING_COMMIT_OID {
                // A commit being placed has no short ID; say what the row is
                // instead of faking the column.
                spans.push(Span::styled("[CREATE COMMIT]", theme.pending_tag));
                spans.push(Span::raw(" "));
                spans.push(Span::styled(message.clone(), dim));
                return Line::from(spans);
            }
            spans.push(Span::styled(row.sid.clone(), theme.shortid));
            spans.push(Span::raw(graph::id_pad(&row.sid)));
            if let Some(tag) = tag {
                spans.push(Span::styled(tag, theme.pending_tag));
                spans.push(Span::raw(" "));
            }
            spans.push(Span::styled(message.clone(), theme.message));
            if row.expandable && !row.expanded {
                spans.push(Span::styled(format!(" ({} files)", file_count), dim));
            }
        }
        RowKind::CommitFile {
            path,
            index,
            worktree,
            on_branch,
            ..
        } => {
            let prefix = if *on_branch { "│┊    " } else { "┊     " };
            spans.push(Span::styled(prefix, theme.graph));
            spans.push(Span::styled(row.sid.clone(), theme.shortid));
            spans.push(Span::raw(" "));
            spans.push(Span::styled(index.to_string(), theme.staged_status));
            spans.push(Span::styled(worktree.to_string(), theme.unstaged_status));
            spans.push(Span::raw(" "));
            spans.push(Span::raw(display(path)));
        }
        RowKind::Upstream {
            label,
            base_short_id,
            base_message,
            commits_ahead,
        } => {
            spans.push(Span::styled("●  ", theme.graph));
            spans.push(Span::styled(format!("{} ", base_short_id), dim));
            spans.push(Span::styled("(upstream) ", theme.label));
            spans.push(Span::styled("[", dim));
            spans.push(Span::styled(label.clone(), theme.branch));
            spans.push(Span::styled("] ", dim));
            if *commits_ahead > 0 {
                spans.push(Span::styled(
                    format!(
                        "⏫ {} new commit{} — ",
                        commits_ahead,
                        if *commits_ahead == 1 { "" } else { "s" }
                    ),
                    theme.message,
                ));
            }
            spans.push(Span::styled(base_message.clone(), theme.message));
        }
        RowKind::Context {
            short_hash,
            date,
            message,
        } => {
            spans.push(Span::styled("· ", dim));
            spans.push(Span::styled(format!("{} {} ", short_hash, date), dim));
            spans.push(Span::styled(message.clone(), theme.message));
        }
        RowKind::Spacer(text) => {
            spans.push(Span::styled(*text, theme.graph));
        }
    }

    Line::from(spans)
}

// ── Diff pane content ────────────────────────────────────────────────────

/// Produce the raw diff text for a row by shelling out to git.
fn diff_text(snapshot: &Snapshot, row: &Row) -> String {
    let workdir = &snapshot.workdir;
    // The branch being named owns commits only on paper until it is created.
    if row.key == branch_key(NEW_BRANCH_NAME) {
        return "branch not created yet".to_string();
    }
    let result = match &row.kind {
        RowKind::LocalChanges { count } => {
            if *count == 0 {
                return "no changes".to_string();
            }
            git::diff_head_display(workdir)
        }
        RowKind::WorkingFile {
            path,
            index,
            worktree,
        } => {
            if graph::file_group(*index, *worktree) == graph::FileGroup::Untracked {
                return untracked_file_text(workdir, path);
            }
            git::diff_head_file_display(workdir, path)
        }
        RowKind::BranchName { range, .. } => match range {
            Some((base, tip)) => git::diff_range(workdir, base, tip),
            None => return "branch has no commits of its own".to_string(),
        },
        RowKind::Commit { oid, .. } if *oid == PENDING_COMMIT_OID => {
            return "commit not created yet".to_string();
        }
        RowKind::Commit { oid, .. } => git::show_commit_patch(workdir, &oid.to_string()),
        RowKind::CommitFile { oid, path, .. } => {
            git::show_commit_file(workdir, &oid.to_string(), path)
        }
        RowKind::Upstream {
            label,
            base_short_id,
            base_message,
            commits_ahead,
        } => {
            return format!(
                "upstream: {}\ncommon base: {} {}\n{} new commit(s) on the remote",
                label, base_short_id, base_message, commits_ahead
            );
        }
        RowKind::Context { short_hash, .. } => git::show_commit_patch(workdir, short_hash),
        RowKind::Spacer(_) => return String::new(),
    };
    match result {
        Ok(text) if text.trim().is_empty() => "no changes".to_string(),
        Ok(text) => text,
        Err(e) => format!("error: {}", e),
    }
}

/// What a `C` commit will contain: the index the selector just staged.
fn staged_commit_diff(snapshot: &Snapshot) -> String {
    match git::diff_cached_display(&snapshot.workdir) {
        Ok(text) if text.trim().is_empty() => "no changes".to_string(),
        Ok(text) => text,
        Err(e) => format!("error: {}", e),
    }
}

/// What the commit being placed will contain: the working-tree changes of
/// `files` (short IDs, or `zz` for all of them), as their rows show them.
///
/// `zz` commits through `git add -A`, so the untracked files go in too and
/// the preview must list them — `git diff HEAD` alone would hide exactly the
/// files the row counts.
fn pending_commit_diff(snapshot: &Snapshot, files: &[String]) -> String {
    let workdir = &snapshot.workdir;
    let ids = &snapshot.ids;
    let all = snapshot.is_all_changes(files);
    let or_empty = |result: Result<String>| match result {
        Ok(text) if text.trim().is_empty() => String::new(),
        Ok(text) => text,
        Err(e) => format!("error: {}", e),
    };
    // A yes/no question about one file, not the three-way grouping the tree
    // and the JSON share, so this stays local rather than going through
    // `graph::file_group`.
    let untracked = |c: &repo::FileChange| c.index == '?' && c.worktree == '?';
    let wanted = |c: &repo::FileChange| all || files.iter().any(|f| f == ids.get_file(&c.path));
    let mut out = String::new();

    // One `git` for every tracked file at once: `c` runs this on the event
    // path, and a spawn per selected file freezes the UI for the selection.
    if all {
        out.push_str(&or_empty(git::diff_head_display(workdir)));
    } else {
        let paths: Vec<&str> = snapshot
            .info
            .working_changes
            .iter()
            .filter(|c| wanted(c) && !untracked(c))
            .map(|c| c.path.as_str())
            .collect();
        out.push_str(&or_empty(git::diff_head_files_display(workdir, &paths)));
    }

    // Untracked files have no diff to ask for; `git add -A` commits them, so
    // the preview reads them itself.
    for change in snapshot
        .info
        .working_changes
        .iter()
        .filter(|c| wanted(c) && untracked(c))
    {
        // Before, not after: the tracked diff above may end mid-line (an
        // error message does), and its text must not run into this header.
        if !out.is_empty() && !out.ends_with('\n') {
            out.push('\n');
        }
        out.push_str(&untracked_file_text(workdir, &change.path));
    }
    if out.trim().is_empty() {
        return "no changes".to_string();
    }
    out
}

/// Render an untracked file's content as added lines.
///
/// Only a bounded prefix is read so a large artifact never spikes memory;
/// the binary (NUL byte) check runs on that prefix, like git's own heuristic.
fn untracked_file_text(workdir: &std::path::Path, path: &str) -> String {
    const MAX_LINES: usize = 2000;
    const MAX_BYTES: u64 = 256 * 1024;
    use std::io::Read;

    let full = workdir.join(path);
    let Ok(file) = std::fs::File::open(&full) else {
        return "(unreadable file)".to_string();
    };
    let mut bytes = Vec::new();
    if file.take(MAX_BYTES + 1).read_to_end(&mut bytes).is_err() {
        return "(unreadable file)".to_string();
    }
    let mut truncated = bytes.len() as u64 > MAX_BYTES;
    bytes.truncate(MAX_BYTES as usize);
    if bytes.contains(&0) {
        return "(binary file)".to_string();
    }
    let content = String::from_utf8_lossy(&bytes);
    let mut out = format!("untracked file: {}\n", path);
    for (i, line) in content.lines().enumerate() {
        if i >= MAX_LINES {
            truncated = true;
            break;
        }
        out.push('+');
        out.push_str(line);
        out.push('\n');
    }
    if truncated {
        out.push_str("...\n");
    }
    out
}

#[cfg(test)]
#[path = "app_test.rs"]
mod tests;