File: cmds.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (3172 lines) | stat: -rw-r--r-- 96,315 bytes parent folder | download
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
"""Editor commands"""
import os
import re
import sys
from fnmatch import fnmatch
from io import StringIO

try:
    from send2trash import send2trash
except ImportError:
    send2trash = None

from . import compat
from . import core
from . import gitcmds
from . import icons
from . import resources
from . import textwrap
from . import utils
from . import version
from .cmd import ContextCommand
from .git import STDOUT
from .git import MISSING_BLOB_OID
from .i18n import N_
from .interaction import Interaction
from .models import main
from .models import prefs


class UsageError(Exception):
    """Exception class for usage errors."""

    def __init__(self, title, message):
        Exception.__init__(self, message)
        self.title = title
        self.msg = message


class EditModel(ContextCommand):
    """Commands that mutate the main model diff data"""

    UNDOABLE = True

    def __init__(self, context):
        """Common edit operations on the main model"""
        super().__init__(context)

        self.old_diff_text = self.model.diff_text
        self.old_filename = self.model.filename
        self.old_mode = self.model.mode
        self.old_diff_type = self.model.diff_type
        self.old_file_type = self.model.file_type

        self.new_diff_text = self.old_diff_text
        self.new_filename = self.old_filename
        self.new_mode = self.old_mode
        self.new_diff_type = self.old_diff_type
        self.new_file_type = self.old_file_type

    def do(self):
        """Perform the operation."""
        self.model.filename = self.new_filename
        self.model.set_mode(self.new_mode)
        self.model.set_diff_text(self.new_diff_text)
        self.model.set_diff_type(self.new_diff_type)
        self.model.set_file_type(self.new_file_type)

    def undo(self):
        """Undo the operation."""
        self.model.filename = self.old_filename
        self.model.set_mode(self.old_mode)
        self.model.set_diff_text(self.old_diff_text)
        self.model.set_diff_type(self.old_diff_type)
        self.model.set_file_type(self.old_file_type)


class ConfirmAction(ContextCommand):
    """Confirm an action before running it"""

    def ok_to_run(self):
        """Return True when the command is okay to run"""
        return True

    def confirm(self):
        """Prompt for confirmation"""
        return True

    def action(self):
        """Run the command and return (status, out, err)"""
        return (-1, '', '')

    def success(self):
        """Callback run on success"""
        return

    def command(self):
        """Command name, for error messages"""
        return 'git'

    def error_message(self):
        """Command error message"""
        return ''

    def do(self):
        """Prompt for confirmation before running a command"""
        status = -1
        out = err = ''
        ok = self.ok_to_run() and self.confirm()
        if ok:
            status, out, err = self.action()
            if status == 0:
                self.success()
            title = self.error_message()
            cmd = self.command()
            Interaction.command(title, cmd, status, out, err)

        return ok, status, out, err


class AbortApplyPatch(ConfirmAction):
    """Reset an in-progress "git am" patch application"""

    def confirm(self):
        title = N_('Abort Applying Patch...')
        question = N_('Aborting applying the current patch?')
        info = N_(
            'Aborting a patch can cause uncommitted changes to be lost.\n'
            'Recovering uncommitted changes is not possible.'
        )
        ok_txt = N_('Abort Applying Patch')
        return Interaction.confirm(
            title, question, info, ok_txt, default=False, icon=icons.undo()
        )

    def action(self):
        status, out, err = gitcmds.abort_apply_patch(self.context)
        self.model.update_file_merge_status()
        return status, out, err

    def success(self):
        self.model.set_commitmsg('')

    def error_message(self):
        return N_('Error')

    def command(self):
        return 'git am --abort'


class AbortCherryPick(ConfirmAction):
    """Reset an in-progress cherry-pick"""

    def confirm(self):
        title = N_('Abort Cherry-Pick...')
        question = N_('Aborting the current cherry-pick?')
        info = N_(
            'Aborting a cherry-pick can cause uncommitted changes to be lost.\n'
            'Recovering uncommitted changes is not possible.'
        )
        ok_txt = N_('Abort Cherry-Pick')
        return Interaction.confirm(
            title, question, info, ok_txt, default=False, icon=icons.undo()
        )

    def action(self):
        status, out, err = gitcmds.abort_cherry_pick(self.context)
        self.model.update_file_merge_status()
        return status, out, err

    def success(self):
        self.model.set_commitmsg('')

    def error_message(self):
        return N_('Error')

    def command(self):
        return 'git cherry-pick --abort'


class AbortMerge(ConfirmAction):
    """Reset an in-progress merge back to HEAD"""

    def confirm(self):
        title = N_('Abort Merge...')
        question = N_('Aborting the current merge?')
        info = N_(
            'Aborting the current merge will cause '
            '*ALL* uncommitted changes to be lost.\n'
            'Recovering uncommitted changes is not possible.'
        )
        ok_txt = N_('Abort Merge')
        return Interaction.confirm(
            title, question, info, ok_txt, default=False, icon=icons.undo()
        )

    def action(self):
        status, out, err = gitcmds.abort_merge(self.context)
        self.model.update_file_merge_status()
        return status, out, err

    def success(self):
        self.model.set_commitmsg('')

    def error_message(self):
        return N_('Error')

    def command(self):
        return 'git merge'


class AmendMode(EditModel):
    """Try to amend a commit."""

    UNDOABLE = True
    LAST_MESSAGE = None

    @staticmethod
    def name():
        return N_('Amend')

    def __init__(self, context, amend=True):
        super().__init__(context)
        self.skip = False
        self.amending = amend
        self.old_commitmsg = self.model.commitmsg
        self.old_mode = self.model.mode

        if self.amending:
            self.new_mode = self.model.mode_amend
            self.new_commitmsg = gitcmds.prev_commitmsg(context)
            AmendMode.LAST_MESSAGE = self.model.commitmsg
            return
        # else, amend unchecked, regular commit
        self.new_mode = self.model.mode_none
        self.new_diff_text = ''
        self.new_commitmsg = self.model.commitmsg
        # If we're going back into new-commit-mode then search the
        # undo stack for a previous amend-commit-mode and grab the
        # commit message at that point in time.
        if AmendMode.LAST_MESSAGE is not None:
            self.new_commitmsg = AmendMode.LAST_MESSAGE
            AmendMode.LAST_MESSAGE = None

    def do(self):
        """Leave/enter amend mode."""
        # Attempt to enter amend mode.  Do not allow this when merging.
        if self.amending:
            if self.model.is_merging:
                self.skip = True
                self.model.set_mode(self.old_mode)
                Interaction.information(
                    N_('Cannot Amend'),
                    N_(
                        'You are in the middle of a merge.\n'
                        'Cannot amend while merging.'
                    ),
                )
                return
        self.skip = False
        super().do()
        self.model.set_commitmsg(self.new_commitmsg)
        self.model.update_file_status()
        self.context.selection.reset(emit=True)

    def undo(self):
        if self.skip:
            return
        self.model.set_commitmsg(self.old_commitmsg)
        super().undo()
        self.model.update_file_status()
        self.context.selection.reset(emit=True)


class AnnexAdd(ContextCommand):
    """Add to Git Annex"""

    def __init__(self, context):
        super().__init__(context)
        self.filename = self.selection.filename()

    def do(self):
        status, out, err = self.git.annex('add', self.filename)
        Interaction.command(N_('Error'), 'git annex add', status, out, err)
        self.model.update_status()


class AnnexInit(ContextCommand):
    """Initialize Git Annex"""

    def do(self):
        status, out, err = self.git.annex('init')
        Interaction.command(N_('Error'), 'git annex init', status, out, err)
        self.model.cfg.reset()
        self.model.emit_updated()


class LFSTrack(ContextCommand):
    """Add a file to git lfs"""

    def __init__(self, context):
        super().__init__(context)
        self.filename = self.selection.filename()
        self.stage_cmd = Stage(context, [self.filename])

    def do(self):
        status, out, err = self.git.lfs('track', self.filename)
        Interaction.command(N_('Error'), 'git lfs track', status, out, err)
        if status == 0:
            self.stage_cmd.do()


class LFSInstall(ContextCommand):
    """Initialize git lfs"""

    def do(self):
        status, out, err = self.git.lfs('install')
        Interaction.command(N_('Error'), 'git lfs install', status, out, err)
        self.model.update_config(reset=True, emit=True)


class ApplyPatch(ContextCommand):
    """Apply the specified patch to the worktree or index"""

    def __init__(
        self,
        context,
        patch,
        encoding,
        apply_to_worktree,
    ):
        super().__init__(context)
        self.patch = patch
        self.encoding = encoding
        self.apply_to_worktree = apply_to_worktree

    def do(self):
        context = self.context

        tmp_file = utils.tmp_filename('apply', suffix='.patch')
        try:
            core.write(tmp_file, self.patch.as_text(), encoding=self.encoding)
            if self.apply_to_worktree:
                status, out, err = gitcmds.apply_diff_to_worktree(context, tmp_file)
            else:
                status, out, err = gitcmds.apply_diff(context, tmp_file)
        finally:
            core.unlink(tmp_file)

        Interaction.log_status(status, out, err)
        self.model.update_file_status(update_index=True)


class ApplyPatches(ContextCommand):
    """Apply patches using the "git am" command"""

    def __init__(self, context, patches):
        super().__init__(context)
        self.patches = patches

    def do(self):
        status, output, err = self.git.am('-3', *self.patches)
        out = f'# git am -3 {core.list2cmdline(self.patches)}\n\n{output}'
        Interaction.command(N_('Patch failed to apply'), 'git am -3', status, out, err)
        # Display a diffstat
        self.model.update_file_status()

        patch_basenames = [os.path.basename(p) for p in self.patches]
        if len(patch_basenames) > 25:
            patch_basenames = patch_basenames[:25]
            patch_basenames.append('...')

        basenames = '\n'.join(patch_basenames)
        if status == 0:
            Interaction.information(
                N_('Patch(es) Applied'),
                (N_('%d patch(es) applied.') + '\n\n%s')
                % (len(self.patches), basenames),
            )


class ApplyPatchesContinue(ContextCommand):
    """Run "git am --continue" to continue on the next patch in a "git am" session"""

    def do(self):
        status, out, err = self.git.am('--continue')
        Interaction.command(
            N_('Failed to commit and continue applying patches'),
            'git am --continue',
            status,
            out,
            err,
        )
        self.model.update_status()
        return status, out, err


class ApplyPatchesSkip(ContextCommand):
    """Run "git am --skip" to continue on the next patch in a "git am" session"""

    def do(self):
        status, out, err = self.git.am(skip=True)
        Interaction.command(
            N_('Failed to continue applying patches after skipping the current patch'),
            'git am --skip',
            status,
            out,
            err,
        )
        self.model.update_status()
        return status, out, err


class Archive(ContextCommand):
    """ "Export archives using the "git archive" command"""

    def __init__(self, context, ref, fmt, prefix, filename):
        super().__init__(context)
        self.ref = ref
        self.fmt = fmt
        self.prefix = prefix
        self.filename = filename

    def do(self):
        fp = core.xopen(self.filename, 'wb')
        cmd = ['git', 'archive', '--format=' + self.fmt]
        if self.fmt in ('tgz', 'tar.gz'):
            cmd.append('-9')
        if self.prefix:
            cmd.append('--prefix=' + self.prefix)
        cmd.append(self.ref)
        proc = core.start_command(cmd, stdout=fp)
        out, err = proc.communicate()
        fp.close()
        status = proc.returncode
        Interaction.log_status(status, out or '', err or '')


class Checkout(EditModel):
    """A command object for git-checkout.

    The argv list is forwarded directly to git.
    """

    def __init__(self, context, argv, checkout_branch=False):
        super().__init__(context)
        self.argv = argv
        self.checkout_branch = checkout_branch
        self.new_diff_text = ''
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT

    def do(self):
        super().do()
        status, out, err = self.git.checkout(*self.argv)
        if self.checkout_branch:
            self.model.update_status()
        else:
            self.model.update_file_status()
        Interaction.command(N_('Error'), 'git checkout', status, out, err)
        return status, out, err


class CheckoutTheirs(ConfirmAction):
    """Checkout "their" version of a file when performing a merge"""

    @staticmethod
    def name():
        return N_('Checkout files from their branch (MERGE_HEAD)')

    def confirm(self):
        title = self.name()
        question = N_('Checkout files from their branch?')
        info = N_(
            'This operation will replace the selected unmerged files with content '
            'from the branch being merged using "git checkout --theirs".\n'
            '*ALL* uncommitted changes will be lost.\n'
            'Recovering uncommitted changes is not possible.'
        )
        ok_txt = N_('Checkout Files')
        return Interaction.confirm(
            title, question, info, ok_txt, default=True, icon=icons.merge()
        )

    def action(self):
        selection = self.selection.selection()
        paths = selection.unmerged
        if not paths:
            return 0, '', ''

        argv = ['--theirs', '--'] + paths
        cmd = Checkout(self.context, argv)
        return cmd.do()

    def error_message(self):
        return N_('Error')

    def command(self):
        return 'git checkout --theirs'


class CheckoutOurs(ConfirmAction):
    """Checkout "our" version of a file when performing a merge"""

    @staticmethod
    def name():
        return N_('Checkout files from our branch (HEAD)')

    def confirm(self):
        title = self.name()
        question = N_('Checkout files from our branch?')
        info = N_(
            'This operation will replace the selected unmerged files with content '
            'from your current branch using "git checkout --ours".\n'
            '*ALL* uncommitted changes will be lost.\n'
            'Recovering uncommitted changes is not possible.'
        )
        ok_txt = N_('Checkout Files')
        return Interaction.confirm(
            title, question, info, ok_txt, default=True, icon=icons.merge()
        )

    def action(self):
        selection = self.selection.selection()
        paths = selection.unmerged
        if not paths:
            return 0, '', ''

        argv = ['--ours', '--'] + paths
        cmd = Checkout(self.context, argv)
        return cmd.do()

    def error_message(self):
        return N_('Error')

    def command(self):
        return 'git checkout --ours'


class BlamePaths(ContextCommand):
    """Blame view for paths."""

    @staticmethod
    def name():
        return N_('Blame...')

    def __init__(self, context, paths=None):
        super().__init__(context)
        if not paths:
            paths = context.selection.union()
        viewer = utils.shell_split(prefs.blame_viewer(context))
        self.argv = viewer + list(paths)

    def do(self):
        try:
            core.fork(self.argv)
        except OSError as e:
            _, details = utils.format_exception(e)
            title = N_('Error Launching Blame Viewer')
            msg = N_('Cannot exec "%s": please configure a blame viewer') % ' '.join(
                self.argv
            )
            Interaction.critical(title, message=msg, details=details)


class CheckoutBranch(Checkout):
    """Checkout a branch."""

    def __init__(self, context, branch):
        args = [branch]
        super().__init__(context, args, checkout_branch=True)


class CherryPick(ContextCommand):
    """Cherry pick commits into the current branch."""

    def __init__(self, context, commits):
        super().__init__(context)
        self.commits = commits

    def do(self):
        status, out, err = gitcmds.cherry_pick(self.context, self.commits)
        self.model.update_file_merge_status()
        title = N_('Cherry-pick failed')
        Interaction.command(title, 'git cherry-pick', status, out, err)


class Revert(ContextCommand):
    """Revert a commit"""

    def __init__(self, context, oid):
        super().__init__(context)
        self.oid = oid

    def do(self):
        status, out, err = self.git.revert(self.oid, no_edit=True)
        self.model.update_file_status()
        title = N_('Revert failed')
        out = '# git revert %s\n\n' % self.oid
        Interaction.command(title, 'git revert', status, out, err)


class ResetMode(EditModel):
    """Reset the mode and clear the model's diff text."""

    def __init__(self, context):
        super().__init__(context)
        self.new_mode = self.model.mode_none
        self.new_diff_text = ''
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT
        self.new_filename = ''

    def do(self):
        super().do()
        self.model.update_file_status()
        self.context.selection.reset(emit=True)


class ResetCommand(ConfirmAction):
    """Reset state using the "git reset" command"""

    def __init__(self, context, ref):
        super().__init__(context)
        self.ref = ref

    def action(self):
        return self.reset()

    def command(self):
        return 'git reset'

    def error_message(self):
        return N_('Error')

    def success(self):
        self.model.update_file_status()

    def confirm(self):
        raise NotImplementedError('confirm() must be overridden')

    def reset(self):
        raise NotImplementedError('reset() must be overridden')


class ResetMixed(ResetCommand):
    @staticmethod
    def tooltip(ref):
        tooltip = N_('The branch will be reset using "git reset --mixed %s"')
        return tooltip % ref

    def confirm(self):
        title = N_('Reset Branch and Stage (Mixed)')
        question = N_('Point the current branch head to a new commit?')
        info = self.tooltip(self.ref)
        ok_text = N_('Reset Branch')
        return Interaction.confirm(title, question, info, ok_text)

    def reset(self):
        return self.git.reset(self.ref, '--', mixed=True)


class ResetKeep(ResetCommand):
    @staticmethod
    def tooltip(ref):
        tooltip = N_('The repository will be reset using "git reset --keep %s"')
        return tooltip % ref

    def confirm(self):
        title = N_('Restore Worktree and Reset All (Keep Unstaged Changes)')
        question = N_('Restore worktree, reset, and preserve unstaged edits?')
        info = self.tooltip(self.ref)
        ok_text = N_('Reset and Restore')
        return Interaction.confirm(title, question, info, ok_text)

    def reset(self):
        return self.git.reset(self.ref, '--', keep=True)


class ResetMerge(ResetCommand):
    @staticmethod
    def tooltip(ref):
        tooltip = N_('The repository will be reset using "git reset --merge %s"')
        return tooltip % ref

    def confirm(self):
        title = N_('Restore Worktree and Reset All (Merge)')
        question = N_('Reset Worktree and Reset All?')
        info = self.tooltip(self.ref)
        ok_text = N_('Reset and Restore')
        return Interaction.confirm(title, question, info, ok_text)

    def reset(self):
        return self.git.reset(self.ref, '--', merge=True)


class ResetSoft(ResetCommand):
    @staticmethod
    def tooltip(ref):
        tooltip = N_('The branch will be reset using "git reset --soft %s"')
        return tooltip % ref

    def confirm(self):
        title = N_('Reset Branch (Soft)')
        question = N_('Reset branch?')
        info = self.tooltip(self.ref)
        ok_text = N_('Reset Branch')
        return Interaction.confirm(title, question, info, ok_text)

    def reset(self):
        return self.git.reset(self.ref, '--', soft=True)


class ResetHard(ResetCommand):
    @staticmethod
    def tooltip(ref):
        tooltip = N_('The repository will be reset using "git reset --hard %s"')
        return tooltip % ref

    def confirm(self):
        title = N_('Restore Worktree and Reset All (Hard)')
        question = N_('Restore Worktree and Reset All?')
        info = self.tooltip(self.ref)
        ok_text = N_('Reset and Restore')
        return Interaction.confirm(title, question, info, ok_text)

    def reset(self):
        return self.git.reset(self.ref, '--', hard=True)


class RestoreWorktree(ConfirmAction):
    """Reset the worktree using the "git read-tree" command"""

    @staticmethod
    def tooltip(ref):
        tooltip = N_(
            'The worktree will be restored using "git read-tree --reset -u %s"'
        )
        return tooltip % ref

    def __init__(self, context, ref):
        super().__init__(context)
        self.ref = ref

    def action(self):
        return self.git.read_tree(self.ref, reset=True, u=True)

    def command(self):
        return 'git read-tree --reset -u %s' % self.ref

    def error_message(self):
        return N_('Error')

    def success(self):
        self.model.update_file_status()

    def confirm(self):
        title = N_('Restore Worktree')
        question = N_('Restore Worktree to %s?') % self.ref
        info = self.tooltip(self.ref)
        ok_text = N_('Restore Worktree')
        return Interaction.confirm(title, question, info, ok_text)


class UndoLastCommit(ResetCommand):
    """Undo the last commit"""

    # NOTE: this is the similar to ResetSoft() with an additional check for
    # published commits and different messages.
    def __init__(self, context):
        super().__init__(context, 'HEAD^')

    def confirm(self):
        check_published = prefs.check_published_commits(self.context)
        if check_published and self.model.is_commit_published():
            return Interaction.confirm(
                N_('Rewrite Published Commit?'),
                N_(
                    'This commit has already been published.\n'
                    'This operation will rewrite published history.\n'
                    "You probably don't want to do this."
                ),
                N_('Undo the published commit?'),
                N_('Undo Last Commit'),
                default=False,
                icon=icons.save(),
            )

        title = N_('Undo Last Commit')
        question = N_('Undo last commit?')
        info = N_('The branch will be reset using "git reset --soft %s"')
        ok_text = N_('Undo Last Commit')
        info_text = info % self.ref
        return Interaction.confirm(title, question, info_text, ok_text)

    def reset(self):
        return self.git.reset('HEAD^', '--', soft=True)


class Commit(ResetMode):
    """Attempt to create a new commit."""

    def __init__(
        self, context, amend, msg, sign, no_verify=False, author=None, date=None
    ):
        super().__init__(context)
        self.amend = amend
        self.msg = msg
        self.sign = sign
        self.no_verify = no_verify
        self.old_commitmsg = self.model.commitmsg
        self.new_commitmsg = ''
        self.author = author
        self.date = date

    def do(self):
        # Create the commit message file
        context = self.context
        msg = self.msg
        tmp_file = utils.tmp_filename('commit-message')
        add_env = {
            'NO_COLOR': '1',
            'TERM': 'dumb',
        }
        add_env.update(main.autodetect_proxy_environ())
        kwargs = {}
        # Override the commit date.
        if self.date:
            add_env['GIT_AUTHOR_DATE'] = self.date
            add_env['GIT_COMMITTER_DATE'] = self.date
            kwargs['date'] = self.date
        # Override the commit author.
        if self.author:
            kwargs['author'] = self.author
        try:
            core.write(tmp_file, msg)
            # Run 'git commit'
            status, out, err = self.git.commit(
                _add_env=add_env,
                F=tmp_file,
                v=True,
                gpg_sign=self.sign,
                amend=self.amend,
                no_verify=self.no_verify,
                **kwargs,
            )
        finally:
            core.unlink(tmp_file)
        if status == 0:
            super().do()
            if context.cfg.get(prefs.AUTOTEMPLATE):
                template_loader = LoadCommitMessageFromTemplate(context)
                template_loader.do()
            else:
                self.model.set_commitmsg(self.new_commitmsg)

        return status, out, err

    @staticmethod
    def strip_comments(msg, comment_char='#'):
        # Strip off comments
        message_lines = [
            line for line in msg.split('\n') if not line.startswith(comment_char)
        ]
        msg = '\n'.join(message_lines)
        if not msg.endswith('\n'):
            msg += '\n'

        return msg


class CycleReferenceSort(ContextCommand):
    """Choose the next reference sort type"""

    def do(self):
        self.model.cycle_ref_sort()


class Ignore(ContextCommand):
    """Add files to an exclusion file"""

    def __init__(self, context, filenames, local=False):
        super().__init__(context)
        self.filenames = list(filenames)
        self.local = local

    def do(self):
        if not self.filenames:
            return
        new_additions = '\n'.join(self.filenames) + '\n'
        for_status = new_additions
        if self.local:
            filename = self.git.git_path('info', 'exclude')
        else:
            filename = '.gitignore'
        if core.exists(filename):
            current_list = core.read(filename)
            new_additions = current_list.rstrip() + '\n' + new_additions
        core.write(filename, new_additions)
        Interaction.log_status(0, f'Added to {filename}:\n{for_status}', '')
        self.model.update_file_status()


def file_summary(files):
    txt = core.list2cmdline(files)
    if len(txt) > 768:
        txt = txt[:768].rstrip() + '...'
    wrap = textwrap.TextWrapper()
    return '\n'.join(wrap.wrap(txt))


class RemoteCommand(ConfirmAction):
    def __init__(self, context, remote):
        super().__init__(context)
        self.remote = remote

    def success(self):
        self.cfg.reset()
        self.model.update_remotes()


class RemoteAdd(RemoteCommand):
    def __init__(self, context, remote, url):
        super().__init__(context, remote)
        self.url = url

    def action(self):
        return self.git.remote('add', self.remote, self.url)

    def error_message(self):
        return N_('Error creating remote "%s"') % self.remote

    def command(self):
        return f'git remote add "{self.remote}" "{self.url}"'


class RemoteRemove(RemoteCommand):
    def confirm(self):
        title = N_('Delete Remote')
        question = N_('Delete remote?')
        info = N_('Delete remote "%s"') % self.remote
        ok_text = N_('Delete')
        return Interaction.confirm(title, question, info, ok_text)

    def action(self):
        return self.git.remote('rm', self.remote)

    def error_message(self):
        return N_('Error deleting remote "%s"') % self.remote

    def command(self):
        return 'git remote rm "%s"' % self.remote


class RemoteRename(RemoteCommand):
    def __init__(self, context, remote, new_name):
        super().__init__(context, remote)
        self.new_name = new_name

    def confirm(self):
        title = N_('Rename Remote')
        text = N_('Rename remote "%(current)s" to "%(new)s"?') % {
            'current': self.remote,
            'new': self.new_name,
        }
        info_text = ''
        ok_text = title
        return Interaction.confirm(title, text, info_text, ok_text)

    def action(self):
        return self.git.remote('rename', self.remote, self.new_name)

    def error_message(self):
        return N_('Error renaming "%(name)s" to "%(new_name)s"') % {
            'name': self.remote,
            'new_name': self.new_name,
        }

    def command(self):
        return f'git remote rename "{self.remote}" "{self.new_name}"'


class RemoteSetURL(RemoteCommand):
    def __init__(self, context, remote, url):
        super().__init__(context, remote)
        self.url = url

    def action(self):
        return self.git.remote('set-url', self.remote, self.url)

    def error_message(self):
        return N_('Unable to set URL for "%(name)s" to "%(url)s"') % {
            'name': self.remote,
            'url': self.url,
        }

    def command(self):
        return f'git remote set-url "{self.remote}" "{self.url}"'


class RemoteEdit(ContextCommand):
    """Combine RemoteRename and RemoteSetURL"""

    def __init__(self, context, old_name, remote, url):
        super().__init__(context)
        self.rename = RemoteRename(context, old_name, remote)
        self.set_url = RemoteSetURL(context, remote, url)

    def do(self):
        result = self.rename.do()
        name_ok = result[0]
        url_ok = False
        if name_ok:
            result = self.set_url.do()
            url_ok = result[0]
        return name_ok, url_ok


class RemoveFromSettings(ConfirmAction):
    def __init__(self, context, repo, entry, icon=None):
        super().__init__(context)
        self.context = context
        self.repo = repo
        self.entry = entry
        self.icon = icon

    def success(self):
        self.context.settings.save()


class RemoveBookmark(RemoveFromSettings):
    def confirm(self):
        entry = self.entry
        title = msg = N_('Delete Bookmark?')
        info = N_('%s will be removed from your bookmarks.') % entry
        ok_text = N_('Delete Bookmark')
        return Interaction.confirm(title, msg, info, ok_text, icon=self.icon)

    def action(self):
        self.context.settings.remove_bookmark(self.repo, self.entry)
        return (0, '', '')


class RemoveRecent(RemoveFromSettings):
    def confirm(self):
        repo = self.repo
        title = msg = N_('Remove %s from the recent list?') % repo
        info = N_('%s will be removed from your recent repositories.') % repo
        ok_text = N_('Remove')
        return Interaction.confirm(title, msg, info, ok_text, icon=self.icon)

    def action(self):
        self.context.settings.remove_recent(self.repo)
        return (0, '', '')


class RemoveFiles(ContextCommand):
    """Removes files"""

    def __init__(self, context, remover, filenames):
        super().__init__(context)
        if remover is None:
            remover = os.remove
        self.remover = remover
        self.filenames = filenames
        # We could git-hash-object stuff and provide undo-ability
        # as an option.  Heh.

    def do(self):
        files = self.filenames
        if not files:
            return

        rescan = False
        bad_filenames = []
        remove = self.remover
        for filename in files:
            if filename:
                try:
                    remove(filename)
                    rescan = True
                except OSError:
                    bad_filenames.append(filename)

        if bad_filenames:
            Interaction.information(
                N_('Error'), N_('Deleting "%s" failed') % file_summary(bad_filenames)
            )

        if rescan:
            self.model.update_file_status()


class Delete(RemoveFiles):
    """Delete files."""

    def __init__(self, context, filenames):
        super().__init__(context, os.remove, filenames)

    def do(self):
        files = self.filenames
        if not files:
            return

        title = N_('Delete Files?')
        msg = N_('The following files will be deleted:') + '\n\n'
        msg += file_summary(files)
        info_txt = N_('Delete %d file(s)?') % len(files)
        ok_txt = N_('Delete Files')

        if Interaction.confirm(
            title, msg, info_txt, ok_txt, default=True, icon=icons.remove()
        ):
            super().do()


class MoveToTrash(RemoveFiles):
    """Move files to the trash using send2trash"""

    AVAILABLE = send2trash is not None

    def __init__(self, context, filenames):
        super().__init__(context, send2trash, filenames)


class DeleteBranch(ConfirmAction):
    """Delete a git branch."""

    def __init__(self, context, branch):
        super().__init__(context)
        self.branch = branch

    def confirm(self):
        title = N_('Delete Branch')
        question = N_('Delete branch "%s"?') % self.branch
        info = N_('The branch will be no longer available.')
        ok_txt = N_('Delete Branch')
        return Interaction.confirm(
            title, question, info, ok_txt, default=True, icon=icons.discard()
        )

    def action(self):
        return self.model.delete_branch(self.branch)

    def error_message(self):
        return N_('Error deleting branch "%s"' % self.branch)

    def command(self):
        command = 'git branch -D %s'
        return command % self.branch


class Rename(ContextCommand):
    """Rename a set of paths."""

    def __init__(self, context, paths):
        super().__init__(context)
        self.paths = paths

    def do(self):
        msg = N_('Untracking: %s') % (', '.join(self.paths))
        Interaction.log(msg)

        for path in self.paths:
            ok = self.rename(path)
            if not ok:
                return

        self.model.update_status()

    def rename(self, path):
        git = self.git
        title = N_('Rename "%s"') % path

        if os.path.isdir(path):
            base_path = os.path.dirname(path)
        else:
            base_path = path
        new_path = Interaction.save_as(base_path, title)
        if not new_path:
            return False

        status, out, err = git.mv(path, new_path, force=True, verbose=True)
        Interaction.command(N_('Error'), 'git mv', status, out, err)
        return status == 0


class RenameBranch(ContextCommand):
    """Rename a git branch."""

    def __init__(self, context, branch, new_branch):
        super().__init__(context)
        self.branch = branch
        self.new_branch = new_branch

    def do(self):
        branch = self.branch
        new_branch = self.new_branch
        status, out, err = self.model.rename_branch(branch, new_branch)
        Interaction.log_status(status, out, err)


class DeleteRemoteBranch(DeleteBranch):
    """Delete a remote git branch."""

    def __init__(self, context, remote, branch):
        super().__init__(context, branch)
        self.remote = remote

    def action(self):
        kwargs = {}
        main.autodetect_proxy(self.context, kwargs)
        main.no_color(kwargs)
        return self.git.push(self.remote, self.branch, delete=True, **kwargs)

    def success(self):
        self.model.update_status()
        Interaction.information(
            N_('Remote Branch Deleted'),
            N_('"%(branch)s" has been deleted from "%(remote)s".')
            % {
                'branch': self.branch,
                'remote': self.remote,
            },
        )

    def error_message(self):
        return N_('Error Deleting Remote Branch')

    def command(self):
        command = 'git push --delete %s %s'
        return command % (self.remote, self.branch)


def get_mode(context, filename, staged, modified, unmerged, untracked):
    model = context.model
    if staged:
        mode = model.mode_index
    elif modified or unmerged:
        mode = model.mode_worktree
    elif untracked:
        if gitcmds.is_binary(context, filename):
            mode = model.mode_untracked
        else:
            mode = model.mode_untracked_diff
    else:
        mode = model.mode
    return mode


class DiffAgainstCommitMode(ContextCommand):
    """Diff against arbitrary commits"""

    def __init__(self, context, oid):
        super().__init__(context)
        self.oid = oid

    def do(self):
        self.model.set_mode(self.model.mode_diff, head=self.oid)
        self.model.update_file_status()


class DiffText(EditModel):
    """Set the diff type to text"""

    def __init__(self, context):
        super().__init__(context)
        self.new_file_type = main.Types.TEXT
        self.new_diff_type = main.Types.TEXT


class ToggleDiffType(ContextCommand):
    """Toggle the diff type between image and text"""

    def __init__(self, context):
        super().__init__(context)
        if self.model.diff_type == main.Types.IMAGE:
            self.new_diff_type = main.Types.TEXT
            self.new_value = False
        else:
            self.new_diff_type = main.Types.IMAGE
            self.new_value = True

    def do(self):
        diff_type = self.new_diff_type
        value = self.new_value

        self.model.set_diff_type(diff_type)

        filename = self.model.filename
        _, ext = os.path.splitext(filename)
        if ext.startswith('.'):
            cfg = 'cola.imagediff' + ext
            self.cfg.set_repo(cfg, value)


class DiffImage(EditModel):
    def __init__(
        self, context, filename, deleted, staged, modified, unmerged, untracked
    ):
        super().__init__(context)

        self.new_filename = filename
        self.new_diff_type = self.get_diff_type(filename)
        self.new_file_type = main.Types.IMAGE
        self.new_mode = get_mode(
            context, filename, staged, modified, unmerged, untracked
        )
        self.staged = staged
        self.modified = modified
        self.unmerged = unmerged
        self.untracked = untracked
        self.deleted = deleted
        self.annex = self.cfg.is_annex()

    def get_diff_type(self, filename):
        """Query the diff type to use based on cola.imagediff.<extension>"""
        _, ext = os.path.splitext(filename)
        if ext.startswith('.'):
            # Check e.g. "cola.imagediff.svg" to see if we should imagediff.
            cfg = 'cola.imagediff' + ext
            if self.cfg.get(cfg, True):
                result = main.Types.IMAGE
            else:
                result = main.Types.TEXT
        else:
            result = main.Types.IMAGE
        return result

    def do(self):
        filename = self.new_filename

        if self.staged:
            images = self.staged_images()
        elif self.modified:
            images = self.modified_images()
        elif self.unmerged:
            images = self.unmerged_images()
        elif self.untracked:
            images = [(filename, False)]
        else:
            images = []

        self.model.set_images(images)
        super().do()

    def staged_images(self):
        context = self.context
        git = self.git
        head = self.model.head
        filename = self.new_filename
        annex = self.annex

        images = []
        index = git.diff_index(head, '--', filename, cached=True)[STDOUT]
        if index:
            # Example:
            #  :100644 100644 fabadb8... 4866510... M      describe.c
            parts = index.split(' ')
            if len(parts) > 3:
                old_oid = parts[2]
                new_oid = parts[3]

            if old_oid != MISSING_BLOB_OID:
                # First, check if we can get a pre-image from git-annex
                annex_image = None
                if annex:
                    annex_image = gitcmds.annex_path(context, head, filename)
                if annex_image:
                    images.append((annex_image, False))  # git annex HEAD
                else:
                    image = gitcmds.write_blob_path(context, head, old_oid, filename)
                    if image:
                        images.append((image, True))

            if new_oid != MISSING_BLOB_OID:
                found_in_annex = False
                if annex and core.islink(filename):
                    status, out, _ = git.annex('status', '--', filename)
                    if status == 0:
                        details = out.split(' ')
                        if details and details[0] == 'A':  # newly added file
                            images.append((filename, False))
                            found_in_annex = True

                if not found_in_annex:
                    image = gitcmds.write_blob(context, new_oid, filename)
                    if image:
                        images.append((image, True))

        return images

    def unmerged_images(self):
        context = self.context
        git = self.git
        head = self.model.head
        filename = self.new_filename
        annex = self.annex

        candidate_merge_heads = ('HEAD', 'CHERRY_HEAD', 'MERGE_HEAD')
        merge_heads = [
            merge_head
            for merge_head in candidate_merge_heads
            if core.exists(git.git_path(merge_head))
        ]

        if annex:  # Attempt to find files in git-annex
            annex_images = []
            for merge_head in merge_heads:
                image = gitcmds.annex_path(context, merge_head, filename)
                if image:
                    annex_images.append((image, False))
            if annex_images:
                annex_images.append((filename, False))
                return annex_images

        # DIFF FORMAT FOR MERGES
        # "git-diff-tree", "git-diff-files" and "git-diff --raw"
        # can take -c or --cc option to generate diff output also
        # for merge commits. The output differs from the format
        # described above in the following way:
        #
        #  1. there is a colon for each parent
        #  2. there are more "src" modes and "src" sha1
        #  3. status is concatenated status characters for each parent
        #  4. no optional "score" number
        #  5. single path, only for "dst"
        # Example:
        #  ::100644 100644 100644 fabadb8... cc95eb0... 4866510... \
        #  MM      describe.c
        images = []
        index = git.diff_index(head, '--', filename, cached=True, cc=True)[STDOUT]
        if index:
            parts = index.split(' ')
            if len(parts) > 3:
                first_mode = parts[0]
                num_parents = first_mode.count(':')
                # colon for each parent, but for the index, the "parents"
                # are really entries in stages 1,2,3 (head, base, remote)
                # remote, base, head
                for i in range(num_parents):
                    offset = num_parents + i + 1
                    oid = parts[offset]
                    try:
                        merge_head = merge_heads[i]
                    except IndexError:
                        merge_head = 'HEAD'
                    if oid != MISSING_BLOB_OID:
                        image = gitcmds.write_blob_path(
                            context, merge_head, oid, filename
                        )
                        if image:
                            images.append((image, True))

        images.append((filename, False))
        return images

    def modified_images(self):
        context = self.context
        git = self.git
        head = self.model.head
        filename = self.new_filename
        annex = self.annex

        images = []
        annex_image = None
        if annex:  # Check for a pre-image from git-annex
            annex_image = gitcmds.annex_path(context, head, filename)
        if annex_image:
            images.append((annex_image, False))  # git annex HEAD
        else:
            worktree = git.diff_files('--', filename)[STDOUT]
            parts = worktree.split(' ')
            if len(parts) > 3:
                oid = parts[2]
                if oid != MISSING_BLOB_OID:
                    image = gitcmds.write_blob_path(context, head, oid, filename)
                    if image:
                        images.append((image, True))  # HEAD

        images.append((filename, False))  # worktree
        return images


class Diff(EditModel):
    """Perform a diff and set the model's current text."""

    def __init__(self, context, filename, cached=False, deleted=False):
        super().__init__(context)
        opts = {}
        if cached and gitcmds.is_valid_ref(context, self.model.head):
            opts['ref'] = self.model.head
        self.new_filename = filename
        self.new_mode = self.model.mode_worktree
        self.new_diff_text = gitcmds.diff_helper(
            self.context, filename=filename, cached=cached, deleted=deleted, **opts
        )


class Diffstat(EditModel):
    """Perform a diffstat and set the model's diff text."""

    def __init__(self, context):
        super().__init__(context)
        cfg = self.cfg
        diff_context = cfg.get('diff.context', 3)
        diff = self.git.diff(
            self.model.head,
            unified=diff_context,
            no_ext_diff=True,
            no_color=True,
            M=True,
            stat=True,
        )[STDOUT]
        self.new_diff_text = diff
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT
        self.new_mode = self.model.mode_diffstat


class DiffStaged(Diff):
    """Perform a staged diff on a file."""

    def __init__(self, context, filename, deleted=None):
        super().__init__(context, filename, cached=True, deleted=deleted)
        self.new_mode = self.model.mode_index


class DiffStagedSummary(EditModel):
    def __init__(self, context):
        super().__init__(context)
        diff = self.git.diff(
            self.model.head,
            cached=True,
            no_color=True,
            no_ext_diff=True,
            patch_with_stat=True,
            M=True,
        )[STDOUT]
        self.new_diff_text = diff
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT
        self.new_mode = self.model.mode_index


class Edit(ContextCommand):
    """Edit a file using the configured gui.editor."""

    @staticmethod
    def name():
        return N_('Launch Editor')

    def __init__(
        self,
        context,
        filenames,
        selected_filename=None,
        line_number=None,
        background_editor=False,
    ):
        super().__init__(context)
        if filenames and not selected_filename:
            selected_filename = filenames[0]
        self.filenames = filenames
        self.selected_filename = selected_filename
        self.line_number = line_number
        self.background_editor = background_editor

    def do(self):
        context = self.context
        if not self.filenames:
            return

        if self.background_editor:
            editor = prefs.background_editor(context)
        else:
            editor = prefs.editor(context)
        if self.line_number is None or self.selected_filename is None:
            args = self.filenames
        else:
            args = []
            # grep and diff are able to open files at specific line numbers.
            # We only know the line number for the first file.
            # Some editors can only apply the '+<line-number>' argument to one file.
            filename = self.selected_filename
            editor_opts = {
                '*vim*': ['+%s' % self.line_number, filename],
                '*emacs*': ['+%s' % self.line_number, filename],
                '*textpad*': [f'{filename}({self.line_number},0)'],
                '*notepad++*': ['-n%s' % self.line_number, filename],
                '*subl*': [f'{filename}:{self.line_number}'],
            }

            use_line_numbers = False
            for pattern, opts in editor_opts.items():
                if fnmatch(editor, pattern):
                    args.extend(opts)
                    use_line_numbers = True
                    break
            if use_line_numbers:
                args.extend(fname for fname in self.filenames if fname != filename)
            else:
                args = self.filenames

        try:
            core.fork(utils.shell_split(editor) + args)
        except (OSError, ValueError) as err:
            message = N_('Cannot exec "%s": please configure your editor') % editor
            _, details = utils.format_exception(err)
            Interaction.critical(N_('Error Editing File'), message, details)


class FormatPatch(ContextCommand):
    """Output a patch series given all revisions and a selected subset."""

    def __init__(self, context, to_export, revs, output='patches'):
        super().__init__(context)
        self.to_export = list(to_export)
        self.revs = list(revs)
        self.output = output

    def do(self):
        context = self.context
        status, out, err = gitcmds.format_patchsets(
            context, self.to_export, self.revs, self.output
        )
        Interaction.log_status(status, out, err)


class LaunchTerminal(ContextCommand):
    @staticmethod
    def name():
        return N_('Launch Terminal')

    @staticmethod
    def is_available(context):
        return context.cfg.terminal() is not None

    def __init__(self, context, path):
        super().__init__(context)
        self.path = path

    def do(self):
        cmd = self.context.cfg.terminal()
        if cmd is None:
            return
        if utils.is_win32():
            argv = ['start', '', cmd, '--login']
            shell = True
        else:
            argv = utils.shell_split(cmd)
            command = '/bin/sh'
            shells = ('zsh', 'fish', 'bash', 'sh')
            for basename in shells:
                executable = core.find_executable(basename)
                if executable:
                    command = executable
                    break
            argv.append(os.getenv('SHELL', command))
            shell = False

        core.fork(argv, cwd=self.path, shell=shell)


class LaunchEditor(Edit):
    @staticmethod
    def name():
        return N_('Launch Editor')

    def __init__(self, context):
        s = context.selection.selection()
        filenames = s.staged + s.unmerged + s.modified + s.untracked
        super().__init__(context, filenames, background_editor=True)


class LaunchEditorAtLine(LaunchEditor):
    """Launch an editor at the specified line"""

    def __init__(self, context):
        super().__init__(context)
        self.line_number = context.selection.line_number
        # Ensure that the model's filename is present in self.filenames otherwise we
        # will open a file that the user never requested. This constraint also ensures
        # that the line number corresponds to the selected filename.
        if context.model.filename in self.filenames:
            self.selected_filename = context.model.filename


class LoadCommitMessageFromFile(ContextCommand):
    """Loads a commit message from a path."""

    UNDOABLE = True

    def __init__(self, context, path):
        super().__init__(context)
        self.path = path
        self.old_commitmsg = self.model.commitmsg
        self.old_directory = self.model.directory

    def do(self):
        path = os.path.expanduser(self.path)
        if not path or not core.isfile(path):
            Interaction.log(N_('Error: Cannot find commit template'))
            Interaction.log(N_('%s: No such file or directory.') % path)
            return
        self.model.set_directory(os.path.dirname(path))
        self.model.set_commitmsg(core.read(path))

    def undo(self):
        self.model.set_commitmsg(self.old_commitmsg)
        self.model.set_directory(self.old_directory)


class LoadCommitMessageFromTemplate(LoadCommitMessageFromFile):
    """Loads the commit message template specified by commit.template."""

    def __init__(self, context):
        cfg = context.cfg
        template = cfg.get('commit.template')
        super().__init__(context, template)

    def do(self):
        if self.path is None:
            Interaction.log(N_('Error: Unconfigured commit template'))
            Interaction.log(
                N_(
                    'A commit template has not been configured.\n'
                    'Use "git config" to define "commit.template"\n'
                    'so that it points to a commit template.'
                )
            )
            return
        return LoadCommitMessageFromFile.do(self)


class LoadCommitMessageFromOID(ContextCommand):
    """Load a previous commit message"""

    UNDOABLE = True

    def __init__(self, context, oid, prefix=''):
        super().__init__(context)
        self.oid = oid
        self.old_commitmsg = self.model.commitmsg
        self.new_commitmsg = prefix + gitcmds.prev_commitmsg(context, oid)

    def do(self):
        self.model.set_commitmsg(self.new_commitmsg)

    def undo(self):
        self.model.set_commitmsg(self.old_commitmsg)


class PrepareCommitMessageHook(ContextCommand):
    """Use the cola-prepare-commit-msg hook to prepare the commit message"""

    UNDOABLE = True

    def __init__(self, context):
        super().__init__(context)
        self.old_commitmsg = self.model.commitmsg

    def get_message(self):
        title = N_('Error running prepare-commitmsg hook')
        hook = gitcmds.prepare_commit_message_hook(self.context)

        if os.path.exists(hook):
            filename = self.model.save_commitmsg()
            status, out, err = core.run_command([hook, filename])

            if status == 0:
                result = core.read(filename)
            else:
                result = self.old_commitmsg
                Interaction.command_error(title, hook, status, out, err)
        else:
            message = N_('A hook must be provided at "%s"') % hook
            Interaction.critical(title, message=message)
            result = self.old_commitmsg

        return result

    def do(self):
        msg = self.get_message()
        self.model.set_commitmsg(msg)

    def undo(self):
        self.model.set_commitmsg(self.old_commitmsg)


class LoadFixupMessage(LoadCommitMessageFromOID):
    """Load a fixup message"""

    def __init__(self, context, oid):
        super().__init__(context, oid, prefix='fixup! ')
        if self.new_commitmsg:
            self.new_commitmsg = self.new_commitmsg.splitlines()[0]


class Merge(ContextCommand):
    """Merge commits"""

    def __init__(self, context, revision, no_commit, squash, no_ff, sign):
        super().__init__(context)
        self.revision = revision
        self.no_ff = no_ff
        self.no_commit = no_commit
        self.squash = squash
        self.sign = sign

    def do(self):
        squash = self.squash
        revision = self.revision
        no_ff = self.no_ff
        no_commit = self.no_commit
        sign = self.sign

        status, out, err = self.git.merge(
            revision, gpg_sign=sign, no_ff=no_ff, no_commit=no_commit, squash=squash
        )
        self.model.update_status()
        title = N_('Merge failed.  Conflict resolution is required.')
        Interaction.command(title, 'git merge', status, out, err)

        return status, out, err


class OpenDefaultApp(ContextCommand):
    """Open a file using the OS default."""

    @staticmethod
    def name():
        return N_('Open Using Default Application')

    def __init__(self, context, filenames):
        super().__init__(context)
        self.filenames = filenames

    def do(self):
        if not self.filenames:
            return
        utils.launch_default_app(self.filenames)


class OpenDir(OpenDefaultApp):
    """Open directories using the OS default."""

    @staticmethod
    def name():
        return N_('Open Directory')

    @property
    def _dirnames(self):
        return self.filenames

    def do(self):
        dirnames = self._dirnames
        if not dirnames:
            return
        # An empty dirname defaults to to the current directory.
        dirs = [(dirname or core.getcwd()) for dirname in dirnames]
        utils.launch_default_app(dirs)


class OpenParentDir(OpenDir):
    """Open parent directories using the OS default."""

    @staticmethod
    def name():
        return N_('Open Parent Directory')

    @property
    def _dirnames(self):
        dirnames = list({os.path.dirname(x) for x in self.filenames})
        return dirnames


class OpenWorktree(OpenDir):
    """Open worktree directory using the OS default."""

    @staticmethod
    def name():
        return N_('Open Worktree')

    # The _unused parameter is needed by worktree_dir_action() -> common.cmd_action().
    def __init__(self, context, _unused=None):
        dirnames = [context.git.worktree()]
        super().__init__(context, dirnames)


class OpenNewRepo(ContextCommand):
    """Launches git-cola on a repo."""

    def __init__(self, context, repo_path):
        super().__init__(context)
        self.repo_path = repo_path

    def do(self):
        self.model.set_directory(self.repo_path)
        core.fork([sys.executable, sys.argv[0], '--repo', self.repo_path])


class OpenRepo(EditModel):
    def __init__(self, context, repo_path):
        super().__init__(context)
        self.repo_path = repo_path
        self.new_mode = self.model.mode_none
        self.new_diff_text = ''
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT
        self.new_commitmsg = ''
        self.new_filename = ''

    def do(self):
        old_repo = self.git.getcwd()
        if self.model.set_worktree(self.repo_path):
            self.fsmonitor.stop()
            self.fsmonitor.start()
            self.model.update_status(reset=True)
            # Check if template should be loaded
            if self.context.cfg.get(prefs.AUTOTEMPLATE):
                template_loader = LoadCommitMessageFromTemplate(self.context)
                template_loader.do()
            else:
                self.model.set_commitmsg(self.new_commitmsg)
            settings = self.context.settings
            settings.load()
            settings.add_recent(self.repo_path, prefs.maxrecent(self.context))
            settings.save()
            super().do()
        else:
            self.model.set_worktree(old_repo)


class OpenParentRepo(OpenRepo):
    def __init__(self, context):
        path = ''
        if version.check_git(context, 'show-superproject-working-tree'):
            status, out, _ = context.git.rev_parse(show_superproject_working_tree=True)
            if status == 0:
                path = out
        if not path:
            path = os.path.dirname(core.getcwd())
        super().__init__(context, path)


class Clone(ContextCommand):
    """Clones a repository and optionally spawns a new cola session."""

    def __init__(
        self, context, url, new_directory, submodules=False, shallow=False, spawn=True
    ):
        super().__init__(context)
        self.url = url
        self.new_directory = new_directory
        self.submodules = submodules
        self.shallow = shallow
        self.spawn = spawn
        self.status = -1
        self.out = ''
        self.err = ''

    def do(self):
        kwargs = {}
        if self.shallow:
            kwargs['depth'] = 1
        recurse_submodules = self.submodules
        shallow_submodules = self.submodules and self.shallow

        status, out, err = self.git.clone(
            self.url,
            self.new_directory,
            recurse_submodules=recurse_submodules,
            shallow_submodules=shallow_submodules,
            **kwargs,
        )

        self.status = status
        self.out = out
        self.err = err
        if status == 0 and self.spawn:
            executable = sys.executable
            core.fork([executable, sys.argv[0], '--repo', self.new_directory])
        return self


class NewBareRepo(ContextCommand):
    """Create a new shared bare repository"""

    def __init__(self, context, path):
        super().__init__(context)
        self.path = path

    def do(self):
        path = self.path
        status, out, err = self.git.init(path, bare=True, shared=True)
        Interaction.command(
            N_('Error'), 'git init --bare --shared "%s"' % path, status, out, err
        )
        return status == 0


def unix_path(path, is_win32=utils.is_win32):
    """Git for Windows requires Unix paths, so force them here"""
    if is_win32():
        path = path.replace('\\', '/')
        first = path[0]
        second = path[1]
        if second == ':':  # sanity check, this better be a Windows-style path
            path = '/' + first + path[2:]

    return path


def sequence_editor():
    """Set GIT_SEQUENCE_EDITOR for running git-cola-sequence-editor"""
    xbase = unix_path(resources.command('git-cola-sequence-editor'))
    if utils.is_win32():
        editor = core.list2cmdline([unix_path(sys.executable), xbase])
    else:
        editor = core.list2cmdline([xbase])
    return editor


class SequenceEditorEnvironment:
    """Set environment variables to enable git-cola-sequence-editor"""

    def __init__(self, context, **kwargs):
        self.env = {
            'GIT_EDITOR': prefs.editor(context),
            'GIT_SEQUENCE_EDITOR': sequence_editor(),
        }
        self.env.update(kwargs)

    def __enter__(self):
        for var, value in self.env.items():
            compat.setenv(var, value)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        for var in self.env:
            compat.unsetenv(var)


class Rebase(ContextCommand):
    def __init__(self, context, upstream=None, branch=None, **kwargs):
        """Start an interactive rebase session

        :param upstream: upstream branch
        :param branch: optional branch to checkout
        :param kwargs: forwarded directly to `git.rebase()`

        """
        super().__init__(context)

        self.upstream = upstream
        self.branch = branch
        self.kwargs = kwargs

    def prepare_arguments(self, upstream):
        args = []
        kwargs = {}

        # Rebase actions must be the only option specified
        for action in ('continue', 'abort', 'skip', 'edit_todo'):
            if self.kwargs.get(action, False):
                kwargs[action] = self.kwargs[action]
                return args, kwargs

        kwargs['interactive'] = True
        kwargs['autosquash'] = self.kwargs.get('autosquash', True)
        kwargs.update(self.kwargs)

        # Prompt to determine whether or not to use "git rebase --update-refs".
        update_refs = self.context.cfg.get(prefs.REBASE_UPDATE_REFS)
        has_update_refs = version.check_git(self.context, 'rebase-update-refs')
        if (
            has_update_refs
            and update_refs is None
            and not kwargs.get('update_refs', False)
        ):
            title = N_('Update stacked branches when rebasing?')
            text = N_(
                '"git rebase --update-refs" automatically force-updates any\n'
                'branches that point to commits that are being rebased.\n\n'
                'Any branches that are checked out in a worktree are not updated.\n\n'
                'Using this feature is helpful for "stacked" branch workflows.'
            )
            info = N_('Update stacked branches when rebasing?')
            ok_text = N_('Update stacked branches')
            cancel_text = N_('Do not update stacked branches')
            update_refs = Interaction.confirm(
                title,
                text,
                info,
                ok_text,
                default=True,
                cancel_text=cancel_text,
            )
            if update_refs:
                kwargs['update_refs'] = True

        if upstream:
            args.append(upstream)
        if self.branch:
            args.append(self.branch)

        return args, kwargs

    def do(self):
        (status, out, err) = (1, '', '')
        context = self.context
        cfg = self.cfg
        model = self.model

        if not cfg.get('rebase.autostash', False):
            if model.staged or model.unmerged or model.modified:
                Interaction.information(
                    N_('Unable to rebase'),
                    N_('You cannot rebase with uncommitted changes.'),
                )
                return status, out, err

        upstream = self.upstream or Interaction.choose_ref(
            context,
            N_('Select New Upstream'),
            N_('Interactive Rebase'),
            default='@{upstream}',
        )
        if not upstream:
            return status, out, err

        self.model.is_rebasing = True
        self.model.emit_updated()

        args, kwargs = self.prepare_arguments(upstream)
        upstream_title = upstream or '@{upstream}'
        with SequenceEditorEnvironment(
            self.context,
            GIT_COLA_SEQ_EDITOR_TITLE=N_('Rebase onto %s') % upstream_title,
            GIT_COLA_SEQ_EDITOR_ACTION=N_('Rebase'),
        ):
            # This blocks the user interface window for the duration
            # of git-cola-sequence-editor. We would need to run the command
            # in a QRunnable task to avoid blocking the main thread.
            # Alternatively, we can hide the main window while rebasing,
            # which doesn't require as much effort.
            status, out, err = self.git.rebase(
                *args, _no_win32_startupinfo=True, **kwargs
            )
        self.model.update_status()
        if err.strip() != 'Nothing to do':
            title = N_('Rebase stopped')
            Interaction.command(title, 'git rebase', status, out, err)
        return status, out, err


class RebaseEditTodo(ContextCommand):
    def do(self):
        (status, out, err) = (1, '', '')
        with SequenceEditorEnvironment(
            self.context,
            GIT_COLA_SEQ_EDITOR_TITLE=N_('Edit Rebase'),
            GIT_COLA_SEQ_EDITOR_ACTION=N_('Save'),
        ):
            status, out, err = self.git.rebase(edit_todo=True)
        Interaction.log_status(status, out, err)
        self.model.update_status()
        return status, out, err


class RebaseContinue(ContextCommand):
    def do(self):
        (status, out, err) = (1, '', '')
        with SequenceEditorEnvironment(
            self.context,
            GIT_COLA_SEQ_EDITOR_TITLE=N_('Rebase'),
            GIT_COLA_SEQ_EDITOR_ACTION=N_('Rebase'),
        ):
            status, out, err = self.git.rebase('--continue')
        Interaction.log_status(status, out, err)
        self.model.update_status()
        return status, out, err


class RebaseSkip(ContextCommand):
    def do(self):
        (status, out, err) = (1, '', '')
        with SequenceEditorEnvironment(
            self.context,
            GIT_COLA_SEQ_EDITOR_TITLE=N_('Rebase'),
            GIT_COLA_SEQ_EDITOR_ACTION=N_('Rebase'),
        ):
            status, out, err = self.git.rebase(skip=True)
        Interaction.log_status(status, out, err)
        self.model.update_status()
        return status, out, err


class RebaseAbort(ContextCommand):
    def do(self):
        status, out, err = self.git.rebase(abort=True)
        Interaction.log_status(status, out, err)
        self.model.update_status()


class Rescan(ContextCommand):
    """Rescan for changes"""

    def do(self):
        self.model.update_status()


class Refresh(ContextCommand):
    """Update refs, refresh the index, and update config"""

    @staticmethod
    def name():
        return N_('Refresh')

    def do(self):
        self.model.update_status(update_index=True)
        self.cfg.update()
        self.fsmonitor.refresh()
        self.selection.selection_changed.emit()


class RefreshConfig(ContextCommand):
    """Refresh the git config cache"""

    def do(self):
        self.cfg.update()


class RevertEditsCommand(ConfirmAction):
    def __init__(self, context):
        super().__init__(context)
        self.icon = icons.undo()

    def ok_to_run(self):
        return self.model.is_undoable()

    def checkout_from_head(self):
        return False

    def checkout_args(self):
        args = []
        s = self.selection.selection()
        if self.checkout_from_head():
            args.append(self.model.head)
        args.append('--')

        if s.staged:
            items = s.staged
        else:
            items = s.modified
        args.extend(items)

        return args

    def action(self):
        checkout_args = self.checkout_args()
        return self.git.checkout(*checkout_args)

    def success(self):
        self.model.set_diff_type(main.Types.TEXT)
        self.model.update_file_status()


class RevertUnstagedEdits(RevertEditsCommand):
    @staticmethod
    def name():
        return N_('Revert Unstaged Edits...')

    def checkout_from_head(self):
        # Being in amend mode should not affect the behavior of this command.
        # The only sensible thing to do is to checkout from the index.
        return False

    def confirm(self):
        title = N_('Revert Unstaged Changes?')
        text = N_(
            'This operation removes unstaged edits from selected files.\n'
            'These changes cannot be recovered.'
        )
        info = N_('Revert the unstaged changes?')
        ok_text = N_('Revert Unstaged Changes')
        return Interaction.confirm(
            title, text, info, ok_text, default=True, icon=self.icon
        )


class RevertUncommittedEdits(RevertEditsCommand):
    @staticmethod
    def name():
        return N_('Revert Uncommitted Edits...')

    def checkout_from_head(self):
        return True

    def confirm(self):
        """Prompt for reverting changes"""
        title = N_('Revert Uncommitted Changes?')
        text = N_(
            'This operation removes uncommitted edits from selected files.\n'
            'These changes cannot be recovered.'
        )
        info = N_('Revert the uncommitted changes?')
        ok_text = N_('Revert Uncommitted Changes')
        return Interaction.confirm(
            title, text, info, ok_text, default=True, icon=self.icon
        )


class RunConfigAction(ContextCommand):
    """Run a user-configured action, typically from the "Tools" menu"""

    def __init__(self, context, action_name):
        super().__init__(context)
        self.action_name = action_name

    def do(self):
        """Run the user-configured action"""
        for env in ('ARGS', 'DIRNAME', 'FILENAME', 'REVISION'):
            try:
                compat.unsetenv(env)
            except KeyError:
                pass
        rev = None
        args = None
        context = self.context
        cfg = self.cfg
        opts = cfg.get_guitool_opts(self.action_name)
        cmd = opts.get('cmd')
        if 'title' not in opts:
            opts['title'] = cmd

        if 'prompt' not in opts or opts.get('prompt') is True:
            prompt = N_('Run "%s"?') % cmd
            opts['prompt'] = prompt

        if opts.get('needsfile'):
            filename = self.selection.filename()
            if not filename:
                Interaction.information(
                    N_('Please select a file'),
                    N_('"%s" requires a selected file.') % cmd,
                )
                return False
            dirname = utils.dirname(filename, current_dir='.')
            compat.setenv('FILENAME', filename)
            compat.setenv('DIRNAME', dirname)

        if opts.get('revprompt') or opts.get('argprompt'):
            while True:
                ok = Interaction.confirm_config_action(context, cmd, opts)
                if not ok:
                    return False
                rev = opts.get('revision')
                args = opts.get('args')
                if opts.get('revprompt') and not rev:
                    title = N_('Invalid Revision')
                    msg = N_('The revision expression cannot be empty.')
                    Interaction.critical(title, msg)
                    continue
                break

        elif opts.get('confirm'):
            title = os.path.expandvars(opts.get('title'))
            prompt = os.path.expandvars(opts.get('prompt'))
            if not Interaction.question(title, prompt):
                return False
        if rev:
            compat.setenv('REVISION', rev)
        if args:
            compat.setenv('ARGS', args)
        title = os.path.expandvars(cmd)
        Interaction.log(N_('Running command: %s') % title)
        cmd = ['sh', '-c', cmd]

        if opts.get('background'):
            core.fork(cmd)
            status, out, err = (0, '', '')
        elif opts.get('noconsole'):
            status, out, err = core.run_command(cmd)
        else:
            status, out, err = Interaction.run_command(title, cmd)

        if not opts.get('background') and not opts.get('norescan'):
            self.model.update_status()

        title = N_('Error')
        Interaction.command(title, cmd, status, out, err)

        return status == 0


class SetDefaultRepo(ContextCommand):
    """Set the default repository"""

    def __init__(self, context, repo):
        super().__init__(context)
        self.repo = repo

    def do(self):
        self.cfg.set_user('cola.defaultrepo', self.repo)


class SetDiffText(EditModel):
    """Set the diff text"""

    UNDOABLE = True

    def __init__(self, context, text):
        super().__init__(context)
        self.new_diff_text = text
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT


class SetUpstreamBranch(ContextCommand):
    """Set the upstream branch"""

    def __init__(self, context, branch, remote, remote_branch):
        super().__init__(context)
        self.branch = branch
        self.remote = remote
        self.remote_branch = remote_branch

    def do(self):
        cfg = self.cfg
        remote = self.remote
        branch = self.branch
        remote_branch = self.remote_branch
        cfg.set_repo('branch.%s.remote' % branch, remote)
        cfg.set_repo('branch.%s.merge' % branch, 'refs/heads/' + remote_branch)


def format_hex(data):
    """Translate binary data into a hex dump"""
    hexdigits = '0123456789ABCDEF'
    result = ''
    offset = 0
    byte_offset_to_int = compat.byte_offset_to_int_converter()
    while offset < len(data):
        result += '%04u |' % offset
        textpart = ''
        for i in range(0, 16):
            if i > 0 and i % 4 == 0:
                result += ' '
            if offset < len(data):
                v = byte_offset_to_int(data[offset])
                result += ' ' + hexdigits[v >> 4] + hexdigits[v & 0xF]
                textpart += chr(v) if 32 <= v < 127 else '.'
                offset += 1
            else:
                result += '   '
                textpart += ' '
        result += ' | ' + textpart + ' |\n'

    return result


class ShowUntracked(EditModel):
    """Show an untracked file."""

    def __init__(self, context, filename):
        super().__init__(context)
        self.new_filename = filename
        if gitcmds.is_binary(context, filename):
            self.new_mode = self.model.mode_untracked
            self.new_diff_text = self.read(filename)
        else:
            self.new_mode = self.model.mode_untracked_diff
            self.new_diff_text = gitcmds.diff_helper(
                self.context, filename=filename, cached=False, untracked=True
            )
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT

    def read(self, filename):
        """Read file contents"""
        cfg = self.cfg
        size = cfg.get('cola.readsize', 2048)
        try:
            result = core.read(filename, size=size, encoding='bytes')
        except OSError:
            result = ''

        truncated = len(result) == size

        encoding = cfg.file_encoding(filename) or core.ENCODING
        try:
            text_result = core.decode_maybe(result, encoding)
        except UnicodeError:
            text_result = format_hex(result)

        if truncated:
            text_result += '...'
        return text_result


class SignOff(ContextCommand):
    """Append a sign-off to the commit message"""

    UNDOABLE = True

    @staticmethod
    def name():
        return N_('Sign Off')

    def __init__(self, context):
        super().__init__(context)
        self.old_commitmsg = self.model.commitmsg

    def do(self):
        """Add a sign-off to the commit message"""
        signoff = self.signoff()
        if signoff in self.model.commitmsg:
            return
        msg = self.model.commitmsg.rstrip()
        self.model.set_commitmsg(msg + '\n' + signoff)

    def undo(self):
        """Restore the commit message"""
        self.model.set_commitmsg(self.old_commitmsg)

    def signoff(self):
        """Generate the sign-off string"""
        name, email = self.cfg.get_author()
        return f'\nSigned-off-by: {name} <{email}>'


def check_conflicts(context, unmerged):
    """Check paths for conflicts

    Conflicting files can be filtered out one-by-one.

    """
    if prefs.check_conflicts(context):
        unmerged = [path for path in unmerged if is_conflict_free(path)]
    return unmerged


def is_conflict_free(path):
    """Return True if `path` contains no conflict markers"""
    rgx = re.compile(r'^(<<<<<<<|\|\|\|\|\|\|\||>>>>>>>) ')
    try:
        with core.xopen(path, 'rb') as f:
            for line in f:
                line = core.decode(line, errors='ignore')
                if rgx.match(line):
                    return should_stage_conflicts(path)
    except OSError:
        # We can't read this file ~ we may be staging a removal
        pass
    return True


def should_stage_conflicts(path):
    """Inform the user that a file contains merge conflicts

    Return `True` if we should stage the path nonetheless.

    """
    title = msg = N_('Stage conflicts?')
    info = (
        N_(
            '%s appears to contain merge conflicts.\n\n'
            'You should probably skip this file.\n'
            'Stage it anyways?'
        )
        % path
    )
    ok_text = N_('Stage conflicts')
    cancel_text = N_('Skip')
    return Interaction.confirm(
        title, msg, info, ok_text, default=False, cancel_text=cancel_text
    )


class Stage(ContextCommand):
    """Stage a set of paths."""

    @staticmethod
    def name():
        return N_('Stage')

    def __init__(self, context, paths):
        super().__init__(context)
        self.paths = paths

    def do(self):
        msg = N_('Staging: %s') % (', '.join(self.paths))
        Interaction.log(msg)
        return self.stage_paths()

    def stage_paths(self):
        """Stages add/removals to git."""
        context = self.context
        paths = self.paths
        if not paths:
            if self.model.cfg.get('cola.safemode', False):
                return (0, '', '')
            return self.stage_all()

        add = []
        remove = []
        status = 0
        out = ''
        err = ''

        for path in set(paths):
            if core.exists(path) or core.islink(path):
                if path.endswith('/'):
                    path = path.rstrip('/')
                add.append(path)
            else:
                remove.append(path)

        self.model.emit_about_to_update()

        # `git add -u` doesn't work on untracked files
        if add:
            status, out, err = gitcmds.add(context, add)
            Interaction.command(N_('Error'), 'git add', status, out, err)

        # If a path doesn't exist then that means it should be removed
        # from the index.   We use `git add -u` for that.
        if remove:
            status, out, err = gitcmds.add(context, remove, u=True)
            Interaction.command(N_('Error'), 'git add -u', status, out, err)

        self.model.update_files(emit=True)
        return status, out, err

    def stage_all(self):
        """Stage all files"""
        status, out, err = self.git.add(v=True, u=True)
        Interaction.command(N_('Error'), 'git add -u', status, out, err)
        self.model.update_file_status()
        return (status, out, err)


class StageCarefully(Stage):
    """Only stage when the path list is non-empty

    We use "git add -u -- <pathspec>" to stage, and it stages everything by
    default when no pathspec is specified, so this class ensures that paths
    are specified before calling git.

    When no paths are specified, the command does nothing.

    """

    def __init__(self, context):
        super().__init__(context, None)
        self.init_paths()

    def init_paths(self):
        """Initialize path data"""
        return

    def ok_to_run(self):
        """Prevent catch-all "git add -u" from adding unmerged files"""
        return self.paths or not self.model.unmerged

    def do(self):
        """Stage files when ok_to_run() return True"""
        if self.ok_to_run():
            return super().do()
        return (0, '', '')


class StageModified(StageCarefully):
    """Stage all modified files."""

    @staticmethod
    def name():
        return N_('Stage Modified')

    def init_paths(self):
        self.paths = self.model.modified


class StageUnmerged(StageCarefully):
    """Stage unmerged files."""

    @staticmethod
    def name():
        return N_('Stage Unmerged')

    def init_paths(self):
        self.paths = check_conflicts(self.context, self.model.unmerged)


class StageUntracked(StageCarefully):
    """Stage all untracked files."""

    @staticmethod
    def name():
        return N_('Stage Untracked')

    def init_paths(self):
        self.paths = self.model.untracked

    def stage_all(self):
        """Disable the stage_all() behavior for untracked files"""
        return (0, '', '')


class StageModifiedAndUntracked(StageCarefully):
    """Stage all untracked files."""

    @staticmethod
    def name():
        return N_('Stage Modified and Untracked')

    def init_paths(self):
        self.paths = self.model.modified + self.model.untracked


class StageOrUnstageAll(ContextCommand):
    """If the selection is staged, unstage it, otherwise stage"""

    @staticmethod
    def name():
        return N_('Stage / Unstage All')

    def do(self):
        if self.model.staged:
            do(Unstage, self.context, self.model.staged)
        else:
            if self.cfg.get('cola.safemode', False):
                unstaged = self.model.modified
            else:
                unstaged = self.model.modified + self.model.untracked
            do(Stage, self.context, unstaged)


class StageOrUnstage(ContextCommand):
    """If the selection is staged, unstage it, otherwise stage"""

    @staticmethod
    def name():
        return N_('Stage / Unstage')

    def do(self):
        s = self.selection.selection()
        if s.staged:
            do(Unstage, self.context, s.staged)

        unstaged = []
        unmerged = check_conflicts(self.context, s.unmerged)
        if unmerged:
            unstaged.extend(unmerged)
        if s.modified:
            unstaged.extend(s.modified)
        if s.untracked:
            unstaged.extend(s.untracked)
        if unstaged:
            do(Stage, self.context, unstaged)


class Tag(ContextCommand):
    """Create a tag object."""

    def __init__(self, context, name, revision, sign=False, message=''):
        super().__init__(context)
        self._name = name
        self._message = message
        self._revision = revision
        self._sign = sign

    def do(self):
        result = False
        git = self.git
        revision = self._revision
        tag_name = self._name
        tag_message = self._message

        if not revision:
            Interaction.critical(
                N_('Missing Revision'), N_('Please specify a revision to tag.')
            )
            return result

        if not tag_name:
            Interaction.critical(
                N_('Missing Name'), N_('Please specify a name for the new tag.')
            )
            return result

        title = N_('Missing Tag Message')
        message = N_('Tag-signing was requested but the tag message is empty.')
        info = N_(
            'An unsigned, lightweight tag will be created instead.\n'
            'Create an unsigned tag?'
        )
        ok_text = N_('Create Unsigned Tag')
        sign = self._sign
        if sign and not tag_message:
            # We require a message in order to sign the tag, so if they
            # choose to create an unsigned tag we have to clear the sign flag.
            if not Interaction.confirm(
                title, message, info, ok_text, default=False, icon=icons.save()
            ):
                return result
            sign = False

        opts = {}
        tmp_file = None
        try:
            if tag_message:
                tmp_file = utils.tmp_filename('tag-message')
                opts['file'] = tmp_file
                core.write(tmp_file, tag_message)

            if sign:
                opts['sign'] = True
            if tag_message:
                opts['annotate'] = True
            status, out, err = git.tag(tag_name, revision, **opts)
        finally:
            if tmp_file:
                core.unlink(tmp_file)

        title = N_('Error: could not create tag "%s"') % tag_name
        Interaction.command(title, 'git tag', status, out, err)

        if status == 0:
            result = True
            self.model.update_status()
            Interaction.information(
                N_('Tag Created'),
                N_('Created a new tag named "%s"') % tag_name,
                details=tag_message or None,
            )

        return result


class Unstage(ContextCommand):
    """Unstage a set of paths."""

    @staticmethod
    def name():
        return N_('Unstage')

    def __init__(self, context, paths):
        super().__init__(context)
        self.paths = paths

    def do(self):
        """Unstage paths"""
        context = self.context
        head = self.model.head
        paths = self.paths

        msg = N_('Unstaging: %s') % (', '.join(paths))
        Interaction.log(msg)
        if not paths:
            return unstage_all(context)
        status, out, err = gitcmds.unstage_paths(context, paths, head=head)
        Interaction.command(N_('Error'), 'git reset', status, out, err)
        self.model.update_file_status()
        return (status, out, err)


class UnstageAll(ContextCommand):
    """Unstage all files; resets the index."""

    def do(self):
        return unstage_all(self.context)


def unstage_all(context):
    """Unstage all files, even while amending"""
    model = context.model
    git = context.git
    head = model.head
    status, out, err = git.reset(head, '--', '.')
    Interaction.command(N_('Error'), 'git reset', status, out, err)
    model.update_file_status()
    return (status, out, err)


class StageSelected(ContextCommand):
    """Stage selected files, or all files if no selection exists."""

    def do(self):
        context = self.context
        paths = self.selection.unstaged
        if paths:
            do(Stage, context, paths)
        elif self.cfg.get('cola.safemode', False):
            do(StageModified, context)


class UnstageSelected(Unstage):
    """Unstage selected files."""

    def __init__(self, context):
        staged = context.selection.staged
        super().__init__(context, staged)


class Untrack(ContextCommand):
    """Unstage a set of paths."""

    def __init__(self, context, paths):
        super().__init__(context)
        self.paths = paths

    def do(self):
        msg = N_('Untracking: %s') % (', '.join(self.paths))
        Interaction.log(msg)
        status, out, err = self.model.untrack_paths(self.paths)
        Interaction.log_status(status, out, err)


class UnmergedSummary(EditModel):
    """List unmerged files in the diff text."""

    def __init__(self, context):
        super().__init__(context)
        unmerged = self.model.unmerged
        io = StringIO()
        io.write('# %s unmerged  file(s)\n' % len(unmerged))
        if unmerged:
            io.write('\n'.join(unmerged) + '\n')
        self.new_diff_text = io.getvalue()
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT
        self.new_mode = self.model.mode_display


class UntrackedSummary(EditModel):
    """List possible .gitignore rules as the diff text."""

    def __init__(self, context):
        super().__init__(context)
        untracked = self.model.untracked
        io = StringIO()
        io.write('# %s untracked file(s)\n' % len(untracked))
        if untracked:
            io.write('# Add these lines to ".gitignore" to ignore these files:\n')
            io.write('\n'.join('/' + filename for filename in untracked) + '\n')
        self.new_diff_text = io.getvalue()
        self.new_diff_type = main.Types.TEXT
        self.new_file_type = main.Types.TEXT
        self.new_mode = self.model.mode_display


class VisualizeAll(ContextCommand):
    """Visualize all branches."""

    def do(self):
        context = self.context
        browser = utils.shell_split(prefs.history_browser(context))
        launch_history_browser(browser + ['--all'])


class VisualizeCurrent(ContextCommand):
    """Visualize all branches."""

    def do(self):
        context = self.context
        browser = utils.shell_split(prefs.history_browser(context))
        launch_history_browser(browser + [self.model.currentbranch] + ['--'])


class VisualizePaths(ContextCommand):
    """Path-limited visualization."""

    def __init__(self, context, paths):
        super().__init__(context)
        context = self.context
        browser = utils.shell_split(prefs.history_browser(context))
        if paths:
            self.argv = browser + ['--'] + list(paths)
        else:
            self.argv = browser

    def do(self):
        launch_history_browser(self.argv)


class VisualizeRevision(ContextCommand):
    """Visualize a specific revision."""

    def __init__(self, context, revision, paths=None):
        super().__init__(context)
        self.revision = revision
        self.paths = paths

    def do(self):
        context = self.context
        argv = utils.shell_split(prefs.history_browser(context))
        if self.revision:
            argv.append(self.revision)
        if self.paths:
            argv.append('--')
            argv.extend(self.paths)
        launch_history_browser(argv)


class SubmoduleAdd(ConfirmAction):
    """Add specified submodules"""

    def __init__(self, context, url, path, branch, depth, reference):
        super().__init__(context)
        self.url = url
        self.path = path
        self.branch = branch
        self.depth = depth
        self.reference = reference

    def confirm(self):
        title = N_('Add Submodule...')
        question = N_('Add this submodule?')
        info = N_('The submodule will be added using\n' '"%s"' % self.command())
        ok_txt = N_('Add Submodule')
        return Interaction.confirm(title, question, info, ok_txt, icon=icons.ok())

    def action(self):
        context = self.context
        args = self.get_args()
        return context.git.submodule('add', *args)

    def success(self):
        self.model.update_file_status()
        self.model.update_submodules_list()

    def error_message(self):
        return N_('Error updating submodule %s' % self.path)

    def command(self):
        cmd = ['git', 'submodule', 'add']
        cmd.extend(self.get_args())
        return core.list2cmdline(cmd)

    def get_args(self):
        args = []
        if self.branch:
            args.extend(['--branch', self.branch])
        if self.reference:
            args.extend(['--reference', self.reference])
        if self.depth:
            args.extend(['--depth', '%d' % self.depth])
        args.extend(['--', self.url])
        if self.path:
            args.append(self.path)
        return args


class SubmoduleUpdate(ConfirmAction):
    """Update specified submodule"""

    def __init__(self, context, path):
        super().__init__(context)
        self.path = path

    def confirm(self):
        title = N_('Update Submodule...')
        question = N_('Update this submodule?')
        info = N_('The submodule will be updated using\n' '"%s"' % self.command())
        ok_txt = N_('Update Submodule')
        return Interaction.confirm(
            title, question, info, ok_txt, default=False, icon=icons.pull()
        )

    def action(self):
        context = self.context
        args = self.get_args()
        return context.git.submodule(*args)

    def success(self):
        self.model.update_file_status()

    def error_message(self):
        return N_('Error updating submodule %s' % self.path)

    def command(self):
        cmd = ['git', 'submodule']
        cmd.extend(self.get_args())
        return core.list2cmdline(cmd)

    def get_args(self):
        cmd = ['update']
        if version.check_git(self.context, 'submodule-update-recursive'):
            cmd.append('--recursive')
        cmd.extend(['--', self.path])
        return cmd


class SubmodulesUpdate(ConfirmAction):
    """Update all submodules"""

    def confirm(self):
        title = N_('Update submodules...')
        question = N_('Update all submodules?')
        info = N_('All submodules will be updated using\n' '"%s"' % self.command())
        ok_txt = N_('Update Submodules')
        return Interaction.confirm(
            title, question, info, ok_txt, default=False, icon=icons.pull()
        )

    def action(self):
        context = self.context
        args = self.get_args()
        return context.git.submodule(*args)

    def success(self):
        self.model.update_file_status()

    def error_message(self):
        return N_('Error updating submodules')

    def command(self):
        cmd = ['git', 'submodule']
        cmd.extend(self.get_args())
        return core.list2cmdline(cmd)

    def get_args(self):
        cmd = ['update']
        if version.check_git(self.context, 'submodule-update-recursive'):
            cmd.append('--recursive')
        return cmd


def launch_history_browser(argv):
    """Launch the configured history browser"""
    try:
        core.fork(argv)
    except OSError as e:
        _, details = utils.format_exception(e)
        title = N_('Error Launching History Browser')
        msg = N_('Cannot exec "%s": please configure a history browser') % ' '.join(
            argv
        )
        Interaction.critical(title, message=msg, details=details)


def run(cls, *args, **opts):
    """
    Returns a callback that runs a command

    If the caller of run() provides args or opts then those are
    used instead of the ones provided by the invoker of the callback.

    """

    def runner(*local_args, **local_opts):
        """Closure return by run() which runs the command"""
        if args or opts:
            return do(cls, *args, **opts)
        return do(cls, *local_args, **local_opts)

    return runner


def do(cls, *args, **opts):
    """Run a command in-place"""
    try:
        cmd = cls(*args, **opts)
        return cmd.do()
    except Exception as e:
        msg, details = utils.format_exception(e)
        if hasattr(cls, '__name__'):
            msg = f'{cls.__name__} exception:\n{msg}'
        Interaction.critical(N_('Error'), message=msg, details=details)
    return None