File: test_cli.py

package info (click to toggle)
dulwich 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,388 kB
  • sloc: python: 99,991; makefile: 163; sh: 67
file content (4433 lines) | stat: -rw-r--r-- 167,260 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
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
#!/usr/bin/env python
# test_cli.py -- tests for dulwich.cli
# vim: expandtab
#
# Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.

"""Tests for dulwich.cli."""

import io
import logging
import os
import shutil
import sys
import tempfile
import unittest
from unittest import skipIf
from unittest.mock import MagicMock, patch

from dulwich import cli
from dulwich.cli import (
    AutoFlushBinaryIOWrapper,
    AutoFlushTextIOWrapper,
    _should_auto_flush,
    detect_terminal_width,
    format_bytes,
    launch_editor,
    write_columns,
)
from dulwich.repo import Repo
from dulwich.tests.utils import (
    build_commit_graph,
)

from .. import TestCase


class DulwichCliTestCase(TestCase):
    """Base class for CLI tests."""

    def setUp(self) -> None:
        super().setUp()
        # Suppress expected error logging during CLI tests
        cli_logger = logging.getLogger("dulwich.cli")
        original_cli_level = cli_logger.level
        cli_logger.setLevel(logging.CRITICAL)
        self.addCleanup(cli_logger.setLevel, original_cli_level)

        root_logger = logging.getLogger()
        original_root_level = root_logger.level
        root_logger.setLevel(logging.CRITICAL)
        self.addCleanup(root_logger.setLevel, original_root_level)

        self.test_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.test_dir)
        self.repo_path = os.path.join(self.test_dir, "repo")
        os.mkdir(self.repo_path)
        self.repo = Repo.init(self.repo_path)
        self.addCleanup(self.repo.close)

    def _run_cli(self, *args, stdout_stream=None):
        """Run CLI command and capture output."""

        class MockStream:
            def __init__(self):
                self._buffer = io.BytesIO()
                self.buffer = self._buffer

            def write(self, data):
                if isinstance(data, bytes):
                    self._buffer.write(data)
                else:
                    self._buffer.write(data.encode("utf-8"))

            def getvalue(self):
                value = self._buffer.getvalue()
                try:
                    return value.decode("utf-8")
                except UnicodeDecodeError:
                    return value

            def __getattr__(self, name):
                return getattr(self._buffer, name)

        old_stdout = sys.stdout
        old_stderr = sys.stderr
        old_cwd = os.getcwd()

        try:
            # Use custom stdout_stream if provided, otherwise use MockStream
            if stdout_stream:
                sys.stdout = stdout_stream
                if not hasattr(sys.stdout, "buffer"):
                    sys.stdout.buffer = sys.stdout
            else:
                sys.stdout = MockStream()

            sys.stderr = MockStream()

            os.chdir(self.repo_path)
            result = cli.main(list(args))
            return result, sys.stdout.getvalue(), sys.stderr.getvalue()
        finally:
            sys.stdout = old_stdout
            sys.stderr = old_stderr
            os.chdir(old_cwd)


class InitCommandTest(DulwichCliTestCase):
    """Tests for init command."""

    def test_init_basic(self):
        # Create a new directory for init
        new_repo_path = os.path.join(self.test_dir, "new_repo")
        _result, _stdout, _stderr = self._run_cli("init", new_repo_path)
        self.assertTrue(os.path.exists(os.path.join(new_repo_path, ".git")))

    def test_init_bare(self):
        # Create a new directory for bare repo
        bare_repo_path = os.path.join(self.test_dir, "bare_repo")
        _result, _stdout, _stderr = self._run_cli("init", "--bare", bare_repo_path)
        self.assertTrue(os.path.exists(os.path.join(bare_repo_path, "HEAD")))
        self.assertFalse(os.path.exists(os.path.join(bare_repo_path, ".git")))

    def test_init_objectformat_sha256(self) -> None:
        # Create a new directory for init with SHA-256
        new_repo_path = os.path.join(self.test_dir, "sha256_repo")
        _result, _stdout, _stderr = self._run_cli(
            "init", "--objectformat=sha256", new_repo_path
        )
        self.assertTrue(os.path.exists(os.path.join(new_repo_path, ".git")))
        # Verify the object format
        repo = Repo(new_repo_path)
        self.addCleanup(repo.close)
        config = repo.get_config()
        self.assertEqual(b"sha256", config.get((b"extensions",), b"objectformat"))

    def test_init_objectformat_sha1(self) -> None:
        # Create a new directory for init with SHA-1
        new_repo_path = os.path.join(self.test_dir, "sha1_repo")
        _result, _stdout, _stderr = self._run_cli(
            "init", "--objectformat=sha1", new_repo_path
        )
        self.assertTrue(os.path.exists(os.path.join(new_repo_path, ".git")))
        # SHA-1 is the default, so objectformat should not be set
        repo = Repo(new_repo_path)
        self.addCleanup(repo.close)
        config = repo.get_config()
        # The extensions section may not exist at all for SHA-1
        if config.has_section((b"extensions",)):
            object_format = config.get((b"extensions",), b"objectformat")
            self.assertNotEqual(b"sha256", object_format)
        # If the section doesn't exist, that's also fine (SHA-1 is default)

    def test_init_bare_objectformat_sha256(self) -> None:
        # Create a bare repo with SHA-256
        bare_repo_path = os.path.join(self.test_dir, "bare_sha256_repo")
        _result, _stdout, _stderr = self._run_cli(
            "init", "--bare", "--objectformat=sha256", bare_repo_path
        )
        self.assertTrue(os.path.exists(os.path.join(bare_repo_path, "HEAD")))
        self.assertFalse(os.path.exists(os.path.join(bare_repo_path, ".git")))
        # Verify the object format
        repo = Repo(bare_repo_path)
        self.addCleanup(repo.close)
        config = repo.get_config()
        self.assertEqual(b"sha256", config.get((b"extensions",), b"objectformat"))


class HelperFunctionsTest(TestCase):
    """Tests for CLI helper functions."""

    def test_format_bytes(self):
        self.assertEqual("0.0 B", format_bytes(0))
        self.assertEqual("100.0 B", format_bytes(100))
        self.assertEqual("1.0 KB", format_bytes(1024))
        self.assertEqual("1.5 KB", format_bytes(1536))
        self.assertEqual("1.0 MB", format_bytes(1024 * 1024))
        self.assertEqual("1.0 GB", format_bytes(1024 * 1024 * 1024))
        self.assertEqual("1.0 TB", format_bytes(1024 * 1024 * 1024 * 1024))

    def test_launch_editor_with_cat(self):
        """Test launch_editor by using cat as the editor."""
        self.overrideEnv("GIT_EDITOR", "cat")
        result = launch_editor(b"Test template content")
        self.assertEqual(b"Test template content", result)

    def test_parse_time_to_timestamp(self):
        """Test parsing time specifications to Unix timestamps."""
        import time

        from dulwich.cli import parse_time_to_timestamp

        # Test special values
        self.assertEqual(0, parse_time_to_timestamp("never"))
        future_time = parse_time_to_timestamp("all")
        self.assertGreater(future_time, int(time.time()))

        # Test Unix timestamp
        self.assertEqual(1234567890, parse_time_to_timestamp("1234567890"))

        # Test relative time
        now = int(time.time())
        result = parse_time_to_timestamp("1 day ago")
        expected = now - 86400
        # Allow 2 second tolerance for test execution time
        self.assertAlmostEqual(expected, result, delta=2)


class AddCommandTest(DulwichCliTestCase):
    """Tests for add command."""

    def test_add_single_file(self):
        # Create a file to add
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")

        _result, _stdout, _stderr = self._run_cli("add", "test.txt")
        # Check that file is in index
        self.assertIn(b"test.txt", self.repo.open_index())

    def test_add_multiple_files(self):
        # Create multiple files
        for i in range(3):
            test_file = os.path.join(self.repo_path, f"test{i}.txt")
            with open(test_file, "w") as f:
                f.write(f"content {i}")

        _result, _stdout, _stderr = self._run_cli(
            "add", "test0.txt", "test1.txt", "test2.txt"
        )
        index = self.repo.open_index()
        self.assertIn(b"test0.txt", index)
        self.assertIn(b"test1.txt", index)
        self.assertIn(b"test2.txt", index)


class RmCommandTest(DulwichCliTestCase):
    """Tests for rm command."""

    def test_rm_file(self):
        # Create, add and commit a file first
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Add test file")

        # Now remove it from index and working directory
        _result, _stdout, _stderr = self._run_cli("rm", "test.txt")
        # Check that file is not in index
        self.assertNotIn(b"test.txt", self.repo.open_index())


class CommitCommandTest(DulwichCliTestCase):
    """Tests for commit command."""

    def test_commit_basic(self):
        # Create and add a file
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")

        # Commit
        _result, _stdout, _stderr = self._run_cli("commit", "--message=Initial commit")
        # Check that HEAD points to a commit
        self.assertIsNotNone(self.repo.head())

    def test_commit_all_flag(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Modify the file (don't stage it)
        with open(test_file, "w") as f:
            f.write("modified content")

        # Create another file and don't add it (untracked)
        untracked_file = os.path.join(self.repo_path, "untracked.txt")
        with open(untracked_file, "w") as f:
            f.write("untracked content")

        # Commit with -a flag should stage and commit the modified file,
        # but not the untracked file
        _result, _stdout, _stderr = self._run_cli(
            "commit", "-a", "--message=Modified commit"
        )
        self.assertIsNotNone(self.repo.head())

        # Check that the modification was committed
        with open(test_file) as f:
            content = f.read()
        self.assertEqual(content, "modified content")

        # Check that untracked file is still untracked
        self.assertTrue(os.path.exists(untracked_file))

    def test_commit_all_flag_no_changes(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Try to commit with -a when there are no changes
        # This should still work (git allows this)
        _result, _stdout, _stderr = self._run_cli(
            "commit", "-a", "--message=No changes commit"
        )
        self.assertIsNotNone(self.repo.head())

    def test_commit_all_flag_multiple_files(self):
        # Create initial commit with multiple files
        file1 = os.path.join(self.repo_path, "file1.txt")
        file2 = os.path.join(self.repo_path, "file2.txt")

        with open(file1, "w") as f:
            f.write("content1")
        with open(file2, "w") as f:
            f.write("content2")

        self._run_cli("add", "file1.txt", "file2.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Modify both files
        with open(file1, "w") as f:
            f.write("modified content1")
        with open(file2, "w") as f:
            f.write("modified content2")

        # Create an untracked file
        untracked_file = os.path.join(self.repo_path, "untracked.txt")
        with open(untracked_file, "w") as f:
            f.write("untracked content")

        # Commit with -a should stage both modified files but not untracked
        _result, _stdout, _stderr = self._run_cli(
            "commit", "-a", "--message=Modified both files"
        )
        self.assertIsNotNone(self.repo.head())

        # Verify modifications were committed
        with open(file1) as f:
            self.assertEqual(f.read(), "modified content1")
        with open(file2) as f:
            self.assertEqual(f.read(), "modified content2")

        # Verify untracked file still exists
        self.assertTrue(os.path.exists(untracked_file))

    @patch("dulwich.cli.launch_editor")
    def test_commit_editor_success(self, mock_editor):
        """Test commit with editor when user provides a message."""
        # Create and add a file
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")

        # Mock editor to return a commit message
        mock_editor.return_value = b"My commit message\n\n# This is a comment\n"

        # Commit without --message flag
        _result, _stdout, _stderr = self._run_cli("commit")

        # Check that HEAD points to a commit
        commit = self.repo[self.repo.head()]
        self.assertEqual(commit.message, b"My commit message")

        # Verify editor was called
        mock_editor.assert_called_once()

    @patch("dulwich.cli.launch_editor")
    def test_commit_editor_empty_message(self, mock_editor):
        """Test commit with editor when user provides empty message."""
        # Create and add a file
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")

        # Mock editor to return only comments
        mock_editor.return_value = b"# All lines are comments\n# No actual message\n"

        # Commit without --message flag should fail with exit code 1
        result, _stdout, _stderr = self._run_cli("commit")
        self.assertEqual(result, 1)

    @patch("dulwich.cli.launch_editor")
    def test_commit_editor_unchanged_template(self, mock_editor):
        """Test commit with editor when user doesn't change the template."""
        # Create and add a file
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")

        # Mock editor to return the exact template that was passed to it
        def return_unchanged_template(template):
            return template

        mock_editor.side_effect = return_unchanged_template

        # Commit without --message flag should fail with exit code 1
        result, _stdout, _stderr = self._run_cli("commit")
        self.assertEqual(result, 1)


class LogCommandTest(DulwichCliTestCase):
    """Tests for log command."""

    def test_log_empty_repo(self):
        _result, _stdout, _stderr = self._run_cli("log")
        # Empty repo should not crash

    def test_log_with_commits(self):
        # Create some commits
        _c1, _c2, c3 = build_commit_graph(
            self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
        )
        self.repo.refs[b"HEAD"] = c3.id

        _result, stdout, _stderr = self._run_cli("log")
        self.assertIn("Commit 3", stdout)
        self.assertIn("Commit 2", stdout)
        self.assertIn("Commit 1", stdout)

    def test_log_reverse(self):
        # Create some commits
        _c1, _c2, c3 = build_commit_graph(
            self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
        )
        self.repo.refs[b"HEAD"] = c3.id

        _result, stdout, _stderr = self._run_cli("log", "--reverse")
        # Check order - commit 1 should appear before commit 3
        pos1 = stdout.index("Commit 1")
        pos3 = stdout.index("Commit 3")
        self.assertLess(pos1, pos3)


class StatusCommandTest(DulwichCliTestCase):
    """Tests for status command."""

    def test_status_empty(self):
        _result, _stdout, _stderr = self._run_cli("status")
        # Should not crash on empty repo

    def test_status_with_untracked(self):
        # Create an untracked file
        test_file = os.path.join(self.repo_path, "untracked.txt")
        with open(test_file, "w") as f:
            f.write("untracked content")

        _result, stdout, _stderr = self._run_cli("status")
        self.assertIn("Untracked files:", stdout)
        self.assertIn("untracked.txt", stdout)

    def test_status_with_column(self):
        # Create multiple untracked files
        for i in range(5):
            test_file = os.path.join(self.repo_path, f"file{i}.txt")
            with open(test_file, "w") as f:
                f.write(f"content {i}")

        _result, stdout, _stderr = self._run_cli("status", "--column")
        self.assertIn("Untracked files:", stdout)
        # Check that files are present in output
        self.assertIn("file0.txt", stdout)
        self.assertIn("file1.txt", stdout)
        # With column format, multiple files should appear on same line
        # (at least for 5 short filenames)
        lines = stdout.split("\n")
        untracked_section = False
        for line in lines:
            if "Untracked files:" in line:
                untracked_section = True
            if untracked_section and "file" in line:
                # At least one line should contain multiple files
                if line.count("file") > 1:
                    return  # Test passes
        # If we get here and have multiple files, column formatting worked
        # (even if each is on its own line due to terminal width)


class BranchCommandTest(DulwichCliTestCase):
    """Tests for branch command."""

    def test_branch_create(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create branch
        _result, _stdout, _stderr = self._run_cli("branch", "test-branch")
        self.assertIn(b"refs/heads/test-branch", self.repo.refs.keys())

    def test_branch_delete(self):
        # Create initial commit and branch
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")
        self._run_cli("branch", "test-branch")

        # Delete branch
        _result, _stdout, _stderr = self._run_cli("branch", "-d", "test-branch")
        self.assertNotIn(b"refs/heads/test-branch", self.repo.refs.keys())

    def test_branch_list_all(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create local test branches
        self._run_cli("branch", "feature-1")
        self._run_cli("branch", "feature-2")

        # Setup a remote and create remote branches
        self.repo.refs[b"refs/remotes/origin/master"] = self.repo.refs[
            b"refs/heads/master"
        ]
        self.repo.refs[b"refs/remotes/origin/feature-remote"] = self.repo.refs[
            b"refs/heads/master"
        ]

        # Test --all listing
        result, stdout, _stderr = self._run_cli("branch", "--all")
        self.assertEqual(result, 0)

        expected_branches = {
            "feature-1",  # local branch
            "feature-2",  # local branch
            "master",  # local branch
            "origin/master",  # remote branch
            "origin/feature-remote",  # remote branch
        }
        lines = [line.strip() for line in stdout.splitlines()]

        # All branches from stdout
        all_branches = set(line for line in lines)
        self.assertEqual(all_branches, expected_branches)

    def test_branch_list_merged(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        master_sha = self.repo.refs[b"refs/heads/master"]

        # Create a merged branch (points to same commit as master)
        self.repo.refs[b"refs/heads/merged-branch"] = master_sha

        # Create a new branch with different content (not merged)
        test_file2 = os.path.join(self.repo_path, "test2.txt")
        with open(test_file2, "w") as f:
            f.write("test2")
        self._run_cli("add", "test2.txt")
        self._run_cli("commit", "--message=New branch commit")

        new_branch_sha = self.repo.refs[b"HEAD"]

        # Switch back to master
        self.repo.refs[b"HEAD"] = master_sha

        # Create a non-merged branch that points to the new branch commit
        self.repo.refs[b"refs/heads/non-merged-branch"] = new_branch_sha

        # Test --merged listing
        result, stdout, _stderr = self._run_cli("branch", "--merged")
        self.assertEqual(result, 0)

        branches = [line.strip() for line in stdout.splitlines()]
        expected_branches = {"master", "merged-branch"}
        self.assertEqual(set(branches), expected_branches)

    def test_branch_list_no_merged(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        master_sha = self.repo.refs[b"refs/heads/master"]

        # Create a merged branch (points to same commit as master)
        self.repo.refs[b"refs/heads/merged-branch"] = master_sha

        # Create a new branch with different content (not merged)
        test_file2 = os.path.join(self.repo_path, "test2.txt")
        with open(test_file2, "w") as f:
            f.write("test2")
        self._run_cli("add", "test2.txt")
        self._run_cli("commit", "--message=New branch commit")
        new_branch_sha = self.repo.refs[b"HEAD"]

        # Switch back to master
        self.repo.refs[b"HEAD"] = master_sha

        # Create a non-merged branch that points to the new branch commit
        self.repo.refs[b"refs/heads/non-merged-branch"] = new_branch_sha

        # Test --no-merged listing
        result, stdout, _stderr = self._run_cli("branch", "--no-merged")
        self.assertEqual(result, 0)

        branches = [line.strip() for line in stdout.splitlines()]
        expected_branches = {"non-merged-branch"}

        self.assertEqual(set(branches), expected_branches)

    def test_branch_list_remotes(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Setup a remote and create remote branches
        self.repo.refs[b"refs/remotes/origin/master"] = self.repo.refs[
            b"refs/heads/master"
        ]
        self.repo.refs[b"refs/remotes/origin/feature-remote-1"] = self.repo.refs[
            b"refs/heads/master"
        ]
        self.repo.refs[b"refs/remotes/origin/feature-remote-2"] = self.repo.refs[
            b"refs/heads/master"
        ]

        # Test --remotes listing
        result, stdout, _stderr = self._run_cli("branch", "--remotes")
        self.assertEqual(result, 0)

        branches = [line.strip() for line in stdout.splitlines()]
        expected_branches = [
            "origin/feature-remote-1",
            "origin/feature-remote-2",
            "origin/master",
        ]

        self.assertEqual(branches, expected_branches)

    def test_branch_list_contains(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        initial_commit_sha = self.repo.refs[b"HEAD"]

        # Create first branch from initial commit
        self._run_cli("branch", "branch-1")

        # Make a new commit on master
        test_file2 = os.path.join(self.repo_path, "test2.txt")
        with open(test_file2, "w") as f:
            f.write("test2")
        self._run_cli("add", "test2.txt")
        self._run_cli("commit", "--message=Second commit")

        second_commit_sha = self.repo.refs[b"HEAD"]

        # Create second branch from current master (contains both commits)
        self._run_cli("branch", "branch-2")

        # Create third branch that doesn't contain the second commit
        # Switch to initial commit and create branch from there
        self.repo.refs[b"HEAD"] = initial_commit_sha
        self._run_cli("branch", "branch-3")

        # Switch back to master
        self.repo.refs[b"HEAD"] = second_commit_sha

        # Test --contains with second commit (should include master and branch-2)
        result, stdout, stderr = self._run_cli(
            "branch", "--contains", second_commit_sha.decode()
        )
        self.assertEqual(result, 0)

        branches = [line.strip() for line in stdout.splitlines()]
        expected_branches = {"master", "branch-2"}
        self.assertEqual(set(branches), expected_branches)

        # Test --contains with initial commit (should include all branches)
        result, stdout, stderr = self._run_cli(
            "branch", "--contains", initial_commit_sha.decode()
        )
        self.assertEqual(result, 0)

        branches = [line.strip() for line in stdout.splitlines()]
        expected_branches = {"master", "branch-1", "branch-2", "branch-3"}
        self.assertEqual(set(branches), expected_branches)

        # Test --contains without argument (uses HEAD, which is second commit)
        result, stdout, stderr = self._run_cli("branch", "--contains")
        self.assertEqual(result, 0)

        branches = [line.strip() for line in stdout.splitlines()]
        expected_branches = {"master", "branch-2"}
        self.assertEqual(set(branches), expected_branches)

        # Test with invalid commit hash
        result, stdout, stderr = self._run_cli("branch", "--contains", "invalid123")
        self.assertNotEqual(result, 0)
        self.assertIn("error: object name invalid123 not found", stderr)

    def test_branch_list_column(self):
        """Test branch --column formatting"""
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        self._run_cli("branch", "feature-1")
        self._run_cli("branch", "feature-2")
        self._run_cli("branch", "feature-3")

        # Run branch --column
        result, stdout, _stderr = self._run_cli("branch", "--all", "--column")
        self.assertEqual(result, 0)

        expected = ["feature-1", "feature-2", "feature-3"]

        for branch in expected:
            self.assertIn(branch, stdout)

        multiple_columns = any(
            sum(branch in line for branch in expected) > 1
            for line in stdout.strip().split("\n")
        )
        self.assertTrue(multiple_columns)

    def test_branch_list_flag(self):
        # Create an initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create local branches
        self._run_cli("branch", "feature-1")
        self._run_cli("branch", "feature-2")
        self._run_cli("branch", "branch-1")

        # Run `branch --list` with a pattern "feature-*"
        result, stdout, _stderr = self._run_cli(
            "branch", "--all", "--list", "feature-*"
        )
        self.assertEqual(result, 0)

        # Collect branches from the output
        branches = [line.strip() for line in stdout.splitlines()]

        # Expected branches — exactly those matching the pattern
        expected_branches = ["feature-1", "feature-2"]

        self.assertEqual(branches, expected_branches)


class TestTerminalWidth(TestCase):
    @patch("os.get_terminal_size")
    def test_terminal_size(self, mock_get_terminal_size):
        """Test os.get_terminal_size mocking."""
        mock_get_terminal_size.return_value.columns = 100
        width = detect_terminal_width()
        self.assertEqual(width, 100)

    @patch("os.get_terminal_size")
    def test_terminal_size_os_error(self, mock_get_terminal_size):
        """Test os.get_terminal_size raising OSError."""
        mock_get_terminal_size.side_effect = OSError("No terminal")
        width = detect_terminal_width()
        self.assertEqual(width, 80)


class TestWriteColumns(TestCase):
    """Tests for write_columns function"""

    def test_basic_functionality(self):
        """Test basic functionality with default terminal width."""
        out = io.StringIO()
        items = [b"main", b"dev", b"feature/branch-1"]
        write_columns(items, out, width=80)

        output_text = out.getvalue()
        self.assertEqual(output_text, "main  dev  feature/branch-1\n")

    def test_narrow_terminal_single_column(self):
        """Test with narrow terminal forcing single column."""
        out = io.StringIO()

        items = [b"main", b"dev", b"feature/branch-1"]
        write_columns(items, out, 20)

        self.assertEqual(out.getvalue(), "main\ndev\nfeature/branch-1\n")

    def test_wide_terminal_multiple_columns(self):
        """Test with wide terminal allowing multiple columns."""
        out = io.StringIO()
        items = [
            b"main",
            b"dev",
            b"feature/branch-1",
            b"feature/branch-2",
            b"feature/branch-3",
        ]
        write_columns(items, out, 120)

        output_text = out.getvalue()
        self.assertEqual(
            output_text,
            "main  dev  feature/branch-1  feature/branch-2  feature/branch-3\n",
        )

    def test_single_item(self):
        """Test with single item."""
        out = io.StringIO()
        write_columns([b"single"], out, 80)

        output_text = out.getvalue()
        self.assertEqual("single\n", output_text)
        self.assertTrue(output_text.endswith("\n"))

    def test_os_error_fallback(self):
        """Test fallback behavior when os.get_terminal_size raises OSError."""
        with patch("os.get_terminal_size", side_effect=OSError("No terminal")):
            out = io.StringIO()
            items = [b"main", b"dev"]
            write_columns(items, out)

            output_text = out.getvalue()
            # With default width (80), should display in columns
            self.assertEqual(output_text, "main  dev\n")

    def test_iterator_input(self):
        """Test with iterator input instead of list."""
        out = io.StringIO()
        items = [b"main", b"dev", b"feature/branch-1"]
        items_iterator = iter(items)
        write_columns(items_iterator, out, 80)

        output_text = out.getvalue()
        self.assertEqual(output_text, "main  dev  feature/branch-1\n")

    def test_column_alignment(self):
        """Test that columns are properly aligned."""
        out = io.StringIO()
        items = [b"short", b"medium_length", b"very_long______name"]
        write_columns(items, out, 50)

        output_text = out.getvalue()
        self.assertEqual(output_text, "short  medium_length  very_long______name\n")

    def test_columns_formatting(self):
        """Test that items are formatted in columns within single line."""
        out = io.StringIO()
        items = [b"branch-1", b"branch-2", b"branch-3", b"branch-4", b"branch-5"]
        write_columns(items, out, 80)

        output_text = out.getvalue()

        self.assertEqual(output_text.count("\n"), 1)
        self.assertTrue(output_text.endswith("\n"))

        line = output_text.strip()
        for item in items:
            self.assertIn(item.decode(), line)

    def test_column_alignment_multiple_lines(self):
        """Test that columns are properly aligned across multiple lines."""
        items = [
            b"short",
            b"medium_length",
            b"very_long_branch_name",
            b"another",
            b"more",
            b"even_longer_branch_name_here",
        ]

        out = io.StringIO()

        write_columns(items, out, width=60)

        output_text = out.getvalue()
        lines = output_text.strip().split("\n")

        self.assertGreater(len(lines), 1)

        line_lengths = [len(line) for line in lines if line.strip()]

        for length in line_lengths:
            self.assertLessEqual(length, 60)

        all_output = " ".join(lines)
        for item in items:
            self.assertIn(item.decode(), all_output)


class CheckoutCommandTest(DulwichCliTestCase):
    """Tests for checkout command."""

    def test_checkout_branch(self):
        # Create initial commit and branch
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")
        self._run_cli("branch", "test-branch")

        # Checkout branch
        _result, _stdout, _stderr = self._run_cli("checkout", "test-branch")
        self.assertEqual(
            self.repo.refs.read_ref(b"HEAD"), b"ref: refs/heads/test-branch"
        )


class TagCommandTest(DulwichCliTestCase):
    """Tests for tag command."""

    def test_tag_create(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create tag
        _result, _stdout, _stderr = self._run_cli("tag", "v1.0")
        self.assertIn(b"refs/tags/v1.0", self.repo.refs.keys())


class VerifyCommitCommandTest(DulwichCliTestCase):
    """Tests for verify-commit command."""

    def test_verify_commit_basic(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Mock the porcelain.verify_commit function since we don't have GPG setup
        with patch("dulwich.cli.porcelain.verify_commit") as mock_verify:
            _result, stdout, _stderr = self._run_cli("verify-commit", "HEAD")
            mock_verify.assert_called_once_with(".", "HEAD")
            self.assertIn("Good signature", stdout)

    def test_verify_commit_multiple(self):
        # Create multiple commits
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test1")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=First")

        with open(test_file, "w") as f:
            f.write("test2")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Second")

        # Mock the porcelain.verify_commit function
        with patch("dulwich.cli.porcelain.verify_commit") as mock_verify:
            _result, stdout, _stderr = self._run_cli("verify-commit", "HEAD", "HEAD~1")
            self.assertEqual(mock_verify.call_count, 2)
            self.assertIn("HEAD", stdout)
            self.assertIn("HEAD~1", stdout)

    def test_verify_commit_default_head(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Mock the porcelain.verify_commit function
        with patch("dulwich.cli.porcelain.verify_commit") as mock_verify:
            # Test that verify-commit without arguments defaults to HEAD
            _result, stdout, _stderr = self._run_cli("verify-commit")
            mock_verify.assert_called_once_with(".", "HEAD")
            self.assertIn("Good signature", stdout)


class VerifyTagCommandTest(DulwichCliTestCase):
    """Tests for verify-tag command."""

    def test_verify_tag_basic(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create an annotated tag
        self._run_cli("tag", "--annotated", "v1.0")

        # Mock the porcelain.verify_tag function since we don't have GPG setup
        with patch("dulwich.cli.porcelain.verify_tag") as mock_verify:
            _result, stdout, _stderr = self._run_cli("verify-tag", "v1.0")
            mock_verify.assert_called_once_with(".", "v1.0")
            self.assertIn("Good signature", stdout)

    def test_verify_tag_multiple(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create multiple annotated tags
        self._run_cli("tag", "--annotated", "v1.0")
        self._run_cli("tag", "--annotated", "v2.0")

        # Mock the porcelain.verify_tag function
        with patch("dulwich.cli.porcelain.verify_tag") as mock_verify:
            _result, stdout, _stderr = self._run_cli("verify-tag", "v1.0", "v2.0")
            self.assertEqual(mock_verify.call_count, 2)
            self.assertIn("v1.0", stdout)
            self.assertIn("v2.0", stdout)


class DiffCommandTest(DulwichCliTestCase):
    """Tests for diff command."""

    def test_diff_working_tree(self):
        # Create and commit a file
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Modify the file
        with open(test_file, "w") as f:
            f.write("initial content\nmodified\n")

        # Test unstaged diff
        _result, stdout, _stderr = self._run_cli("diff")
        self.assertIn("+modified", stdout)

    def test_diff_staged(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Modify and stage the file
        with open(test_file, "w") as f:
            f.write("initial content\nnew file\n")
        self._run_cli("add", "test.txt")

        # Test staged diff
        _result, stdout, _stderr = self._run_cli("diff", "--staged")
        self.assertIn("+new file", stdout)

    def test_diff_cached(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Modify and stage the file
        with open(test_file, "w") as f:
            f.write("initial content\nnew file\n")
        self._run_cli("add", "test.txt")

        # Test cached diff (alias for staged)
        _result, stdout, _stderr = self._run_cli("diff", "--cached")
        self.assertIn("+new file", stdout)

    def test_diff_commit(self):
        # Create two commits
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("first version\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=First")

        with open(test_file, "w") as f:
            f.write("first version\nsecond line\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Second")

        # Add working tree changes
        with open(test_file, "a") as f:
            f.write("working tree change\n")

        # Test single commit diff (should show working tree vs HEAD)
        _result, stdout, _stderr = self._run_cli("diff", "HEAD")
        self.assertIn("+working tree change", stdout)

    def test_diff_two_commits(self):
        # Create two commits
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("first version\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=First")

        # Get first commit SHA
        first_commit = self.repo.refs[b"HEAD"].decode()

        with open(test_file, "w") as f:
            f.write("first version\nsecond line\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Second")

        # Get second commit SHA
        second_commit = self.repo.refs[b"HEAD"].decode()

        # Test diff between two commits
        _result, stdout, _stderr = self._run_cli("diff", first_commit, second_commit)
        self.assertIn("+second line", stdout)

    def test_diff_commit_vs_working_tree(self):
        # Test that diff <commit> shows working tree vs commit (not commit vs parent)
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("first version\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=First")

        first_commit = self.repo.refs[b"HEAD"].decode()

        with open(test_file, "w") as f:
            f.write("first version\nsecond line\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Second")

        # Add changes to working tree
        with open(test_file, "w") as f:
            f.write("completely different\n")

        # diff <first_commit> should show working tree vs first commit
        _result, stdout, _stderr = self._run_cli("diff", first_commit)
        self.assertIn("-first version", stdout)
        self.assertIn("+completely different", stdout)

    def test_diff_with_paths(self):
        # Test path filtering
        # Create multiple files
        file1 = os.path.join(self.repo_path, "file1.txt")
        file2 = os.path.join(self.repo_path, "file2.txt")
        subdir = os.path.join(self.repo_path, "subdir")
        os.makedirs(subdir)
        file3 = os.path.join(subdir, "file3.txt")

        with open(file1, "w") as f:
            f.write("content1\n")
        with open(file2, "w") as f:
            f.write("content2\n")
        with open(file3, "w") as f:
            f.write("content3\n")

        self._run_cli("add", ".")
        self._run_cli("commit", "--message=Initial")

        # Modify all files
        with open(file1, "w") as f:
            f.write("modified1\n")
        with open(file2, "w") as f:
            f.write("modified2\n")
        with open(file3, "w") as f:
            f.write("modified3\n")

        # Test diff with specific file
        _result, stdout, _stderr = self._run_cli("diff", "--", "file1.txt")
        self.assertIn("file1.txt", stdout)
        self.assertNotIn("file2.txt", stdout)
        self.assertNotIn("file3.txt", stdout)

        # Test diff with directory
        _result, stdout, _stderr = self._run_cli("diff", "--", "subdir")
        self.assertNotIn("file1.txt", stdout)
        self.assertNotIn("file2.txt", stdout)
        self.assertIn("file3.txt", stdout)

        # Test staged diff with paths
        self._run_cli("add", "file1.txt")
        _result, stdout, _stderr = self._run_cli("diff", "--staged", "--", "file1.txt")
        self.assertIn("file1.txt", stdout)
        self.assertIn("+modified1", stdout)

        # Test diff with multiple paths (file2 and file3 are still unstaged)
        _result, stdout, _stderr = self._run_cli(
            "diff", "--", "file2.txt", "subdir/file3.txt"
        )
        self.assertIn("file2.txt", stdout)
        self.assertIn("file3.txt", stdout)
        self.assertNotIn("file1.txt", stdout)

        # Test diff with commit and paths
        first_commit = self.repo.refs[b"HEAD"].decode()
        with open(file1, "w") as f:
            f.write("newer1\n")
        _result, stdout, _stderr = self._run_cli(
            "diff", first_commit, "--", "file1.txt"
        )
        self.assertIn("file1.txt", stdout)
        self.assertIn("-content1", stdout)
        self.assertIn("+newer1", stdout)
        self.assertNotIn("file2.txt", stdout)

    def test_diff_stat(self):
        # Create and commit a file
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content\n")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Modify the file
        with open(test_file, "w") as f:
            f.write("initial content\nmodified\n")

        # Test --stat output
        _result, stdout, _stderr = self._run_cli("diff", "--stat")
        self.assertEqual(
            stdout,
            " test.txt | 1 +\n 1 files changed, 1 insertions(+), 0 deletions(-)\n",
        )


class FilterBranchCommandTest(DulwichCliTestCase):
    """Tests for filter-branch command."""

    def setUp(self):
        super().setUp()
        # Create a more complex repository structure for testing
        # Create some files in subdirectories
        os.makedirs(os.path.join(self.repo_path, "subdir"))
        os.makedirs(os.path.join(self.repo_path, "other"))

        # Create files
        files = {
            "README.md": "# Test Repo",
            "subdir/file1.txt": "File in subdir",
            "subdir/file2.txt": "Another file in subdir",
            "other/file3.txt": "File in other dir",
            "root.txt": "File at root",
        }

        for path, content in files.items():
            file_path = os.path.join(self.repo_path, path)
            with open(file_path, "w") as f:
                f.write(content)

        # Add all files and create initial commit
        self._run_cli("add", ".")
        self._run_cli("commit", "--message=Initial commit")

        # Create a second commit modifying subdir
        with open(os.path.join(self.repo_path, "subdir/file1.txt"), "a") as f:
            f.write("\nModified content")
        self._run_cli("add", "subdir/file1.txt")
        self._run_cli("commit", "--message=Modify subdir file")

        # Create a third commit in other dir
        with open(os.path.join(self.repo_path, "other/file3.txt"), "a") as f:
            f.write("\nMore content")
        self._run_cli("add", "other/file3.txt")
        self._run_cli("commit", "--message=Modify other file")

        # Create a branch
        self._run_cli("branch", "test-branch")

        # Create a tag
        self._run_cli("tag", "v1.0")

    def test_filter_branch_subdirectory_filter(self):
        """Test filter-branch with subdirectory filter."""
        # Run filter-branch to extract only the subdir
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "filter-branch", "--subdirectory-filter", "subdir"
            )

            # Check that the operation succeeded
            self.assertEqual(result, 0)
            log_output = "\n".join(cm.output)
            self.assertIn("Rewrite HEAD", log_output)

        # filter-branch rewrites history but doesn't update working tree
        # We need to check the commit contents, not the working tree
        # Reset to the rewritten HEAD to update working tree
        self._run_cli("reset", "--hard", "HEAD")

        # Now check that only files from subdir remain at root level
        self.assertTrue(os.path.exists(os.path.join(self.repo_path, "file1.txt")))
        self.assertTrue(os.path.exists(os.path.join(self.repo_path, "file2.txt")))
        self.assertFalse(os.path.exists(os.path.join(self.repo_path, "README.md")))
        self.assertFalse(os.path.exists(os.path.join(self.repo_path, "root.txt")))
        self.assertFalse(os.path.exists(os.path.join(self.repo_path, "other")))
        self.assertFalse(os.path.exists(os.path.join(self.repo_path, "subdir")))

        # Check that original refs were backed up
        original_refs = [
            ref for ref in self.repo.refs.keys() if ref.startswith(b"refs/original/")
        ]
        self.assertTrue(
            len(original_refs) > 0, "No original refs found after filter-branch"
        )

    @skipIf(sys.platform == "win32", "sed command not available on Windows")
    def test_filter_branch_msg_filter(self):
        """Test filter-branch with message filter."""
        # Run filter-branch to prepend [FILTERED] to commit messages
        result, stdout, _stderr = self._run_cli(
            "filter-branch", "--msg-filter", "sed 's/^/[FILTERED] /'"
        )

        self.assertEqual(result, 0)

        # Check that commit messages were modified
        result, stdout, _stderr = self._run_cli("log")
        self.assertIn("[FILTERED] Modify other file", stdout)
        self.assertIn("[FILTERED] Modify subdir file", stdout)
        self.assertIn("[FILTERED] Initial commit", stdout)

    def test_filter_branch_env_filter(self):
        """Test filter-branch with environment filter."""
        # Run filter-branch to change author email
        env_filter = """
        if [ "$GIT_AUTHOR_EMAIL" = "test@example.com" ]; then
            export GIT_AUTHOR_EMAIL="filtered@example.com"
        fi
        """
        result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--env-filter", env_filter
        )

        self.assertEqual(result, 0)

    def test_filter_branch_prune_empty(self):
        """Test filter-branch with prune-empty option."""
        # Create a commit that only touches files outside subdir
        with open(os.path.join(self.repo_path, "root.txt"), "a") as f:
            f.write("\nNew line")
        self._run_cli("add", "root.txt")
        self._run_cli("commit", "--message=Modify root file only")

        # Run filter-branch to extract subdir with prune-empty
        result, stdout, _stderr = self._run_cli(
            "filter-branch", "--subdirectory-filter", "subdir", "--prune-empty"
        )

        self.assertEqual(result, 0)

        # The last commit should have been pruned
        result, stdout, _stderr = self._run_cli("log")
        self.assertNotIn("Modify root file only", stdout)

    @skipIf(sys.platform == "win32", "sed command not available on Windows")
    def test_filter_branch_force(self):
        """Test filter-branch with force option."""
        # Run filter-branch once with a filter that actually changes something
        result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--msg-filter", "sed 's/^/[TEST] /'"
        )
        self.assertEqual(result, 0)

        # Check that backup refs were created
        # The implementation backs up refs under refs/original/
        original_refs = [
            ref for ref in self.repo.refs.keys() if ref.startswith(b"refs/original/")
        ]
        self.assertTrue(len(original_refs) > 0, "No original refs found")

        # Run again without force - should fail
        with self.assertLogs("dulwich.cli", level="ERROR") as cm:
            result, _stdout, _stderr = self._run_cli(
                "filter-branch", "--msg-filter", "sed 's/^/[TEST2] /'"
            )
            self.assertEqual(result, 1)
            log_output = "\n".join(cm.output)
            self.assertIn("Cannot create a new backup", log_output)
            self.assertIn("refs/original", log_output)

        # Run with force - should succeed
        result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--force", "--msg-filter", "sed 's/^/[TEST3] /'"
        )
        self.assertEqual(result, 0)

    @skipIf(sys.platform == "win32", "sed command not available on Windows")
    def test_filter_branch_specific_branch(self):
        """Test filter-branch on a specific branch."""
        # Switch to test-branch and add a commit
        self._run_cli("checkout", "test-branch")
        with open(os.path.join(self.repo_path, "branch-file.txt"), "w") as f:
            f.write("Branch specific file")
        self._run_cli("add", "branch-file.txt")
        self._run_cli("commit", "--message=Branch commit")

        # Run filter-branch on the test-branch
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, stdout, _stderr = self._run_cli(
                "filter-branch", "--msg-filter", "sed 's/^/[BRANCH] /'", "test-branch"
            )

            self.assertEqual(result, 0)
            log_output = "\n".join(cm.output)
            self.assertIn("Ref 'refs/heads/test-branch' was rewritten", log_output)

        # Check that only test-branch was modified
        result, stdout, _stderr = self._run_cli("log")
        self.assertIn("[BRANCH] Branch commit", stdout)

        # Switch to master and check it wasn't modified
        self._run_cli("checkout", "master")
        result, stdout, _stderr = self._run_cli("log")
        self.assertNotIn("[BRANCH]", stdout)

    def test_filter_branch_tree_filter(self):
        """Test filter-branch with tree filter."""
        # Use a tree filter to remove a specific file
        tree_filter = "rm -f root.txt"
        result, stdout, _stderr = self._run_cli(
            "filter-branch", "--tree-filter", tree_filter
        )

        self.assertEqual(result, 0)

        # Check that the file was removed from the latest commit
        # We need to check the commit tree, not the working directory
        result, stdout, _stderr = self._run_cli("ls-tree", "HEAD")
        self.assertNotIn("root.txt", stdout)

    def test_filter_branch_index_filter(self):
        """Test filter-branch with index filter."""
        # Use an index filter to remove a file from the index
        index_filter = "git rm --cached --ignore-unmatch root.txt"
        result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--index-filter", index_filter
        )

        self.assertEqual(result, 0)

    def test_filter_branch_parent_filter(self):
        """Test filter-branch with parent filter."""
        # Create a merge commit first
        self._run_cli("checkout", "HEAD", "-b", "feature")
        with open(os.path.join(self.repo_path, "feature.txt"), "w") as f:
            f.write("Feature")
        self._run_cli("add", "feature.txt")
        self._run_cli("commit", "--message=Feature commit")

        self._run_cli("checkout", "master")
        self._run_cli("merge", "feature", "--message=Merge feature")

        # Use parent filter to linearize history (remove second parent)
        parent_filter = "cut -d' ' -f1"
        result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--parent-filter", parent_filter
        )

        self.assertEqual(result, 0)

    def test_filter_branch_commit_filter(self):
        """Test filter-branch with commit filter."""
        # Use commit filter to skip commits with certain messages
        commit_filter = """
        if grep -q "Modify other" <<< "$GIT_COMMIT_MESSAGE"; then
            skip_commit "$@"
        else
            git commit-tree "$@"
        fi
        """
        _result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--commit-filter", commit_filter
        )

        # Note: This test may fail because the commit filter syntax is simplified
        # In real Git, skip_commit is a function, but our implementation may differ

    def test_filter_branch_tag_name_filter(self):
        """Test filter-branch with tag name filter."""
        # Run filter-branch with tag name filter to rename tags
        result, _stdout, _stderr = self._run_cli(
            "filter-branch",
            "--tag-name-filter",
            "sed 's/^v/version-/'",
            "--msg-filter",
            "cat",
        )

        self.assertEqual(result, 0)

        # Check that tag was renamed
        self.assertIn(b"refs/tags/version-1.0", self.repo.refs.keys())

    def test_filter_branch_errors(self):
        """Test filter-branch error handling."""
        # Test with invalid subdirectory
        result, _stdout, _stderr = self._run_cli(
            "filter-branch", "--subdirectory-filter", "nonexistent"
        )
        # Should still succeed but produce empty history
        self.assertEqual(result, 0)

    def test_filter_branch_no_args(self):
        """Test filter-branch with no arguments."""
        # Should work as no-op
        result, _stdout, _stderr = self._run_cli("filter-branch")
        self.assertEqual(result, 0)


class ShowCommandTest(DulwichCliTestCase):
    """Tests for show command."""

    def test_show_commit(self):
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        _result, stdout, _stderr = self._run_cli("show", "HEAD")
        self.assertIn("Test commit", stdout)


class ShowRefCommandTest(DulwichCliTestCase):
    """Tests for show-ref command."""

    def test_show_ref_basic(self):
        """Test basic show-ref functionality."""
        # Create a commit to have a HEAD ref
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Create a branch
        self._run_cli("branch", "test-branch")

        # Get the exact SHAs
        master_sha = self.repo.refs[b"refs/heads/master"].decode()
        test_branch_sha = self.repo.refs[b"refs/heads/test-branch"].decode()

        # Run show-ref
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-ref")
            output = "\n".join([record.message for record in cm.records])

        expected = (
            f"{master_sha} refs/heads/master\n{test_branch_sha} refs/heads/test-branch"
        )
        self.assertEqual(output, expected)

    def test_show_ref_with_head(self):
        """Test show-ref with --head option."""
        # Create a commit to have a HEAD ref
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Get the exact SHAs
        head_sha = self.repo.refs[b"HEAD"].decode()
        master_sha = self.repo.refs[b"refs/heads/master"].decode()

        # Run show-ref with --head
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-ref", "--head")
            output = "\n".join([record.message for record in cm.records])

        expected = f"{head_sha} HEAD\n{master_sha} refs/heads/master"
        self.assertEqual(output, expected)

    def test_show_ref_with_pattern(self):
        """Test show-ref with pattern matching."""
        # Create commits and branches
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")
        self._run_cli("branch", "feature-1")
        self._run_cli("branch", "feature-2")
        self._run_cli("branch", "bugfix-1")

        # Get the exact SHA for master
        master_sha = self.repo.refs[b"refs/heads/master"].decode()

        # Test pattern matching for "master"
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-ref", "master")
            output = "\n".join([record.message for record in cm.records])

        expected = f"{master_sha} refs/heads/master"
        self.assertEqual(output, expected)

    def test_show_ref_branches_only(self):
        """Test show-ref with --branches option."""
        # Create commits and a tag
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")
        self._run_cli("tag", "v1.0")

        # Get the exact SHA for master
        master_sha = self.repo.refs[b"refs/heads/master"].decode()

        # Run show-ref with --branches
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-ref", "--branches")
            output = "\n".join([record.message for record in cm.records])

        expected = f"{master_sha} refs/heads/master"
        self.assertEqual(output, expected)

    def test_show_ref_tags_only(self):
        """Test show-ref with --tags option."""
        # Create commits and tags
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")
        self._run_cli("tag", "v1.0")
        self._run_cli("tag", "v2.0")

        # Get the exact SHAs for tags
        v1_sha = self.repo.refs[b"refs/tags/v1.0"].decode()
        v2_sha = self.repo.refs[b"refs/tags/v2.0"].decode()

        # Run show-ref with --tags
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-ref", "--tags")
            output = "\n".join([record.message for record in cm.records])

        expected = f"{v1_sha} refs/tags/v1.0\n{v2_sha} refs/tags/v2.0"
        self.assertEqual(output, expected)

    def test_show_ref_hash_only(self):
        """Test show-ref with --hash option to show only OID."""
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Get the exact SHA for master
        master_sha = self.repo.refs[b"refs/heads/master"].decode()

        # Run show-ref with --hash
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli(
                "show-ref", "--hash", "--", "master"
            )
            output = "\n".join([record.message for record in cm.records])

        expected = f"{master_sha}"
        self.assertEqual(output, expected)

    def test_show_ref_verify(self):
        """Test show-ref with --verify option for exact matching."""
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Get the exact SHA for master
        master_sha = self.repo.refs[b"refs/heads/master"].decode()

        # Verify with exact ref path should succeed
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "show-ref", "--verify", "refs/heads/master"
            )
            self.assertEqual(result, 0)
            output = "\n".join([record.message for record in cm.records])

        expected = f"{master_sha} refs/heads/master"
        self.assertEqual(output, expected)

        # Verify with partial name should fail
        result, _stdout, _stderr = self._run_cli("show-ref", "--verify", "master")
        self.assertEqual(result, 1)

    def test_show_ref_exists(self):
        """Test show-ref with --exists option."""
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Check if existing ref exists
        result, _stdout, _stderr = self._run_cli(
            "show-ref", "--exists", "refs/heads/master"
        )
        self.assertEqual(result, 0)

        # Check if non-existing ref exists
        result, _stdout, _stderr = self._run_cli(
            "show-ref", "--exists", "refs/heads/nonexistent"
        )
        self.assertEqual(result, 2)

    def test_show_ref_quiet(self):
        """Test show-ref with --quiet option."""
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Run show-ref with --quiet - should not log anything
        result, _stdout, _stderr = self._run_cli("show-ref", "--quiet")
        self.assertEqual(result, 0)

    def test_show_ref_abbrev(self):
        """Test show-ref with --abbrev option."""
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Get the exact SHA for master
        master_sha = self.repo.refs[b"refs/heads/master"].decode()

        # Run show-ref with --abbrev=7
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-ref", "--abbrev=7")
            output = "\n".join([record.message for record in cm.records])

        expected = f"{master_sha[:7]} refs/heads/master"
        self.assertEqual(output, expected)

    def test_show_ref_no_matches(self):
        """Test show-ref returns error when no matches found."""
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Test commit")

        # Search for non-existent pattern
        result, _stdout, _stderr = self._run_cli("show-ref", "nonexistent")
        self.assertEqual(result, 1)


class ShowBranchCommandTest(DulwichCliTestCase):
    """Tests for show-branch command."""

    def test_show_branch_basic(self):
        """Test basic show-branch functionality."""
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Create a branch and add a commit
        self._run_cli("branch", "branch1")
        self._run_cli("checkout", "branch1")
        with open(test_file, "a") as f:
            f.write("\nbranch1 content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Branch1 commit")

        # Switch back to master
        self._run_cli("checkout", "master")

        # Run show-branch
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli(
                "show-branch", "master", "branch1"
            )
            output = "\n".join([record.message for record in cm.records])

        # Check exact output
        expected = (
            "! [branch1] Branch1 commit\n"
            " ![master] Initial commit\n"
            "----\n"
            "*  [Branch1 commit]\n"
            "+* [Initial commit]"
        )
        self.assertEqual(expected, output)

    def test_show_branch_list(self):
        """Test show-branch with --list option."""
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Create branches
        self._run_cli("branch", "branch1")
        self._run_cli("branch", "branch2")

        # Run show-branch --list
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("show-branch", "--list")
            output = "\n".join([record.message for record in cm.records])

        # Check exact output (only branch headers, no separator)
        expected = (
            "!  [branch1] Initial commit\n"
            " ! [branch2] Initial commit\n"
            "  ![master] Initial commit"
        )
        self.assertEqual(expected, output)

    def test_show_branch_independent(self):
        """Test show-branch with --independent option."""
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Create a branch and add a commit
        self._run_cli("branch", "branch1")
        self._run_cli("checkout", "branch1")
        with open(test_file, "a") as f:
            f.write("\nbranch1 content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Branch1 commit")

        # Run show-branch --independent
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli(
                "show-branch", "--independent", "master", "branch1"
            )
            output = "\n".join([record.message for record in cm.records])

        # Only branch1 should be shown (it's not reachable from master)
        expected = "branch1"
        self.assertEqual(expected, output)

    def test_show_branch_merge_base(self):
        """Test show-branch with --merge-base option."""
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Get the initial commit SHA
        initial_sha = self.repo.refs[b"HEAD"]

        # Create a branch and add a commit
        self._run_cli("branch", "branch1")
        self._run_cli("checkout", "branch1")
        with open(test_file, "a") as f:
            f.write("\nbranch1 content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Branch1 commit")

        # Switch back to master and add a different commit
        self._run_cli("checkout", "master")
        with open(test_file, "a") as f:
            f.write("\nmaster content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Master commit")

        # Run show-branch --merge-base
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli(
                "show-branch", "--merge-base", "master", "branch1"
            )
            output = "\n".join([record.message for record in cm.records])

        # The merge base should be the initial commit SHA
        expected = initial_sha.decode("ascii")
        self.assertEqual(expected, output)


class FormatPatchCommandTest(DulwichCliTestCase):
    """Tests for format-patch command."""

    def test_format_patch_single_commit(self):
        # Create a commit with actual content
        from dulwich.objects import Blob, Tree

        # Initial commit
        tree1 = Tree()
        self.repo.object_store.add_object(tree1)
        self.repo.get_worktree().commit(
            message=b"Initial commit",
            tree=tree1.id,
        )

        # Second commit with a file
        blob = Blob.from_string(b"Hello, World!\n")
        self.repo.object_store.add_object(blob)
        tree2 = Tree()
        tree2.add(b"hello.txt", 0o100644, blob.id)
        self.repo.object_store.add_object(tree2)
        self.repo.get_worktree().commit(
            message=b"Add hello.txt",
            tree=tree2.id,
        )

        # Test format-patch for last commit
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("format-patch", "-n", "1")
            self.assertEqual(result, None)
            log_output = "\n".join(cm.output)
            self.assertIn("0001-Add-hello.txt.patch", log_output)

        # Check patch contents
        patch_file = os.path.join(self.repo_path, "0001-Add-hello.txt.patch")
        with open(patch_file, "rb") as f:
            content = f.read()
            # Check header
            self.assertIn(b"Subject: [PATCH 1/1] Add hello.txt", content)
            self.assertIn(b"From:", content)
            self.assertIn(b"Date:", content)
            # Check diff content
            self.assertIn(b"diff --git a/hello.txt b/hello.txt", content)
            self.assertIn(b"new file mode", content)
            self.assertIn(b"+Hello, World!", content)
            # Check footer
            self.assertIn(b"-- \nDulwich", content)

        # Clean up
        os.remove(patch_file)

    def test_format_patch_multiple_commits(self):
        from dulwich.objects import Blob, Tree

        # Initial commit
        tree1 = Tree()
        self.repo.object_store.add_object(tree1)
        self.repo.get_worktree().commit(
            message=b"Initial commit",
            tree=tree1.id,
        )

        # Second commit
        blob1 = Blob.from_string(b"File 1 content\n")
        self.repo.object_store.add_object(blob1)
        tree2 = Tree()
        tree2.add(b"file1.txt", 0o100644, blob1.id)
        self.repo.object_store.add_object(tree2)
        self.repo.get_worktree().commit(
            message=b"Add file1.txt",
            tree=tree2.id,
        )

        # Third commit
        blob2 = Blob.from_string(b"File 2 content\n")
        self.repo.object_store.add_object(blob2)
        tree3 = Tree()
        tree3.add(b"file1.txt", 0o100644, blob1.id)
        tree3.add(b"file2.txt", 0o100644, blob2.id)
        self.repo.object_store.add_object(tree3)
        self.repo.get_worktree().commit(
            message=b"Add file2.txt",
            tree=tree3.id,
        )

        # Test format-patch for last 2 commits
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("format-patch", "-n", "2")
            self.assertEqual(result, None)
            log_output = "\n".join(cm.output)
            self.assertIn("0001-Add-file1.txt.patch", log_output)
            self.assertIn("0002-Add-file2.txt.patch", log_output)

        # Check first patch
        with open(os.path.join(self.repo_path, "0001-Add-file1.txt.patch"), "rb") as f:
            content = f.read()
            self.assertIn(b"Subject: [PATCH 1/2] Add file1.txt", content)
            self.assertIn(b"+File 1 content", content)

        # Check second patch
        with open(os.path.join(self.repo_path, "0002-Add-file2.txt.patch"), "rb") as f:
            content = f.read()
            self.assertIn(b"Subject: [PATCH 2/2] Add file2.txt", content)
            self.assertIn(b"+File 2 content", content)

        # Clean up
        os.remove(os.path.join(self.repo_path, "0001-Add-file1.txt.patch"))
        os.remove(os.path.join(self.repo_path, "0002-Add-file2.txt.patch"))

    def test_format_patch_output_directory(self):
        from dulwich.objects import Blob, Tree

        # Create a commit
        blob = Blob.from_string(b"Test content\n")
        self.repo.object_store.add_object(blob)
        tree = Tree()
        tree.add(b"test.txt", 0o100644, blob.id)
        self.repo.object_store.add_object(tree)
        self.repo.get_worktree().commit(
            message=b"Test commit",
            tree=tree.id,
        )

        # Create output directory
        output_dir = os.path.join(self.test_dir, "patches")
        os.makedirs(output_dir)

        # Test format-patch with output directory
        result, _stdout, _stderr = self._run_cli(
            "format-patch", "-o", output_dir, "-n", "1"
        )
        self.assertEqual(result, None)

        # Check that file was created in output directory with correct content
        patch_file = os.path.join(output_dir, "0001-Test-commit.patch")
        self.assertTrue(os.path.exists(patch_file))
        with open(patch_file, "rb") as f:
            content = f.read()
            self.assertIn(b"Subject: [PATCH 1/1] Test commit", content)
            self.assertIn(b"+Test content", content)

    def test_format_patch_commit_range(self):
        from dulwich.objects import Blob, Tree

        # Create commits with actual file changes
        commits = []
        trees = []

        # Initial empty commit
        tree0 = Tree()
        self.repo.object_store.add_object(tree0)
        trees.append(tree0)
        c0 = self.repo.get_worktree().commit(
            message=b"Initial commit",
            tree=tree0.id,
        )
        commits.append(c0)

        # Add three files in separate commits
        for i in range(1, 4):
            blob = Blob.from_string(f"Content {i}\n".encode())
            self.repo.object_store.add_object(blob)
            tree = Tree()
            # Copy previous files
            for j in range(1, i):
                prev_blob_id = trees[j][f"file{j}.txt".encode()][1]
                tree.add(f"file{j}.txt".encode(), 0o100644, prev_blob_id)
            # Add new file
            tree.add(f"file{i}.txt".encode(), 0o100644, blob.id)
            self.repo.object_store.add_object(tree)
            trees.append(tree)

            c = self.repo.get_worktree().commit(
                message=f"Add file{i}.txt".encode(),
                tree=tree.id,
            )
            commits.append(c)

        # Test format-patch with commit range (should get commits 2 and 3)
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "format-patch", f"{commits[1].decode()}..{commits[3].decode()}"
            )
            self.assertEqual(result, None)

            # Should create patches for commits 2 and 3
            log_output = "\n".join(cm.output)
            self.assertIn("0001-Add-file2.txt.patch", log_output)
            self.assertIn("0002-Add-file3.txt.patch", log_output)

        # Verify patch contents
        with open(os.path.join(self.repo_path, "0001-Add-file2.txt.patch"), "rb") as f:
            content = f.read()
            self.assertIn(b"Subject: [PATCH 1/2] Add file2.txt", content)
            self.assertIn(b"+Content 2", content)
            self.assertNotIn(b"file3.txt", content)  # Should not include file3

        with open(os.path.join(self.repo_path, "0002-Add-file3.txt.patch"), "rb") as f:
            content = f.read()
            self.assertIn(b"Subject: [PATCH 2/2] Add file3.txt", content)
            self.assertIn(b"+Content 3", content)
            self.assertNotIn(b"file2.txt", content)  # Should not modify file2

        # Clean up
        os.remove(os.path.join(self.repo_path, "0001-Add-file2.txt.patch"))
        os.remove(os.path.join(self.repo_path, "0002-Add-file3.txt.patch"))

    def test_format_patch_stdout(self):
        from dulwich.objects import Blob, Tree

        # Create a commit with modified file
        tree1 = Tree()
        blob1 = Blob.from_string(b"Original content\n")
        self.repo.object_store.add_object(blob1)
        tree1.add(b"file.txt", 0o100644, blob1.id)
        self.repo.object_store.add_object(tree1)
        self.repo.get_worktree().commit(
            message=b"Initial commit",
            tree=tree1.id,
        )

        tree2 = Tree()
        blob2 = Blob.from_string(b"Modified content\n")
        self.repo.object_store.add_object(blob2)
        tree2.add(b"file.txt", 0o100644, blob2.id)
        self.repo.object_store.add_object(tree2)
        self.repo.get_worktree().commit(
            message=b"Modify file.txt",
            tree=tree2.id,
        )

        # Mock stdout as a BytesIO for binary output
        stdout_stream = io.BytesIO()
        stdout_stream.buffer = stdout_stream

        # Run command with --stdout
        old_stdout = sys.stdout
        old_stderr = sys.stderr
        old_cwd = os.getcwd()
        try:
            sys.stdout = stdout_stream
            sys.stderr = io.StringIO()
            os.chdir(self.repo_path)
            cli.main(["format-patch", "--stdout", "-n", "1"])
        finally:
            sys.stdout = old_stdout
            sys.stderr = old_stderr
            os.chdir(old_cwd)

        # Check output
        stdout_stream.seek(0)
        output = stdout_stream.read()
        self.assertIn(b"Subject: [PATCH 1/1] Modify file.txt", output)
        self.assertIn(b"diff --git a/file.txt b/file.txt", output)
        self.assertIn(b"-Original content", output)
        self.assertIn(b"+Modified content", output)
        self.assertIn(b"-- \nDulwich", output)

    def test_format_patch_empty_repo(self):
        # Test with empty repository
        result, stdout, _stderr = self._run_cli("format-patch", "-n", "5")
        self.assertEqual(result, None)
        # Should produce no output for empty repo
        self.assertEqual(stdout.strip(), "")


class FetchPackCommandTest(DulwichCliTestCase):
    """Tests for fetch-pack command."""

    @patch("dulwich.cli.get_transport_and_path")
    def test_fetch_pack_basic(self, mock_transport):
        # Mock the transport
        mock_client = MagicMock()
        mock_transport.return_value = (mock_client, "/path/to/repo")
        mock_client.fetch.return_value = None

        _result, _stdout, _stderr = self._run_cli(
            "fetch-pack", "git://example.com/repo.git"
        )
        mock_client.fetch.assert_called_once()


class LsRemoteCommandTest(DulwichCliTestCase):
    """Tests for ls-remote command."""

    def test_ls_remote_basic(self):
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Test basic ls-remote
        _result, stdout, _stderr = self._run_cli("ls-remote", self.repo_path)
        lines = stdout.strip().split("\n")
        self.assertTrue(any("HEAD" in line for line in lines))
        self.assertTrue(any("refs/heads/master" in line for line in lines))

    def test_ls_remote_symref(self):
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Test ls-remote with --symref option
        _result, stdout, _stderr = self._run_cli(
            "ls-remote", "--symref", self.repo_path
        )
        lines = stdout.strip().split("\n")
        # Should show symref for HEAD in exact format: "ref: refs/heads/master\tHEAD"
        expected_line = "ref: refs/heads/master\tHEAD"
        self.assertIn(
            expected_line,
            lines,
            f"Expected line '{expected_line}' not found in output: {lines}",
        )


class PullCommandTest(DulwichCliTestCase):
    """Tests for pull command."""

    @patch("dulwich.porcelain.pull")
    def test_pull_basic(self, mock_pull):
        _result, _stdout, _stderr = self._run_cli("pull", "origin")
        mock_pull.assert_called_once()

    @patch("dulwich.porcelain.pull")
    def test_pull_with_refspec(self, mock_pull):
        _result, _stdout, _stderr = self._run_cli("pull", "origin", "master")
        mock_pull.assert_called_once()


class PushCommandTest(DulwichCliTestCase):
    """Tests for push command."""

    @patch("dulwich.porcelain.push")
    def test_push_basic(self, mock_push):
        _result, _stdout, _stderr = self._run_cli("push", "origin")
        mock_push.assert_called_once()

    @patch("dulwich.porcelain.push")
    def test_push_force(self, mock_push):
        _result, _stdout, _stderr = self._run_cli("push", "-f", "origin")
        mock_push.assert_called_with(".", "origin", None, force=True)


class ArchiveCommandTest(DulwichCliTestCase):
    """Tests for archive command."""

    def test_archive_basic(self):
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test content")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Archive produces binary output, so use BytesIO
        _result, stdout, _stderr = self._run_cli(
            "archive", "HEAD", stdout_stream=io.BytesIO()
        )
        # Should complete without error and produce some binary output
        self.assertIsInstance(stdout, bytes)
        self.assertGreater(len(stdout), 0)


class ForEachRefCommandTest(DulwichCliTestCase):
    """Tests for for-each-ref command."""

    def test_for_each_ref(self):
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("for-each-ref")
            log_output = "\n".join(cm.output)
            # Just check that we have some refs output and it contains refs/heads
            self.assertTrue(len(cm.output) > 0, "Expected some ref output")
            self.assertIn("refs/heads/", log_output)


class PackRefsCommandTest(DulwichCliTestCase):
    """Tests for pack-refs command."""

    def test_pack_refs(self):
        # Create some refs
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")
        self._run_cli("branch", "test-branch")

        _result, _stdout, _stderr = self._run_cli("pack-refs", "--all")
        # Check that packed-refs file exists
        self.assertTrue(
            os.path.exists(os.path.join(self.repo_path, ".git", "packed-refs"))
        )


class SubmoduleCommandTest(DulwichCliTestCase):
    """Tests for submodule commands."""

    def test_submodule_list(self):
        # Create an initial commit so repo has a HEAD
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        _result, _stdout, _stderr = self._run_cli("submodule")
        # Should not crash on repo without submodules

    def test_submodule_init(self):
        # Create .gitmodules file for init to work
        gitmodules = os.path.join(self.repo_path, ".gitmodules")
        with open(gitmodules, "w") as f:
            f.write("")  # Empty .gitmodules file

        _result, _stdout, _stderr = self._run_cli("submodule", "init")
        # Should not crash


class StashCommandTest(DulwichCliTestCase):
    """Tests for stash commands."""

    def test_stash_list_empty(self):
        _result, _stdout, _stderr = self._run_cli("stash", "list")
        # Should not crash on empty stash

    def test_stash_push_pop(self):
        # Create a file and modify it
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Modify file
        with open(test_file, "w") as f:
            f.write("modified")

        # Stash changes
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("stash", "push")
            self.assertIn("Saved working directory", cm.output[0])

        # Note: Dulwich stash doesn't currently update the working tree
        # so the file remains modified after stash push

        # Note: stash pop is not fully implemented in Dulwich yet
        # so we only test stash push here


class MergeCommandTest(DulwichCliTestCase):
    """Tests for merge command."""

    def test_merge_basic(self):
        # Create initial commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("initial")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        # Create and checkout new branch
        self._run_cli("branch", "feature")
        self._run_cli("checkout", "feature")

        # Make changes in feature branch
        with open(test_file, "w") as f:
            f.write("feature changes")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Feature commit")

        # Go back to main
        self._run_cli("checkout", "master")

        # Merge feature branch
        _result, _stdout, _stderr = self._run_cli("merge", "feature")


class HelpCommandTest(DulwichCliTestCase):
    """Tests for help command."""

    def test_help_basic(self):
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("help")
            log_output = "\n".join(cm.output)
            self.assertIn("dulwich command line tool", log_output)

    def test_help_all(self):
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("help", "-a")
            log_output = "\n".join(cm.output)
            self.assertIn("Available commands:", log_output)
            self.assertIn("add", log_output)
            self.assertIn("commit", log_output)


class RemoteCommandTest(DulwichCliTestCase):
    """Tests for remote commands."""

    def test_remote_add(self):
        _result, _stdout, _stderr = self._run_cli(
            "remote", "add", "origin", "https://github.com/example/repo.git"
        )
        # Check remote was added to config
        config = self.repo.get_config()
        self.assertEqual(
            config.get((b"remote", b"origin"), b"url"),
            b"https://github.com/example/repo.git",
        )


class CheckIgnoreCommandTest(DulwichCliTestCase):
    """Tests for check-ignore command."""

    def test_check_ignore(self):
        # Create .gitignore
        gitignore = os.path.join(self.repo_path, ".gitignore")
        with open(gitignore, "w") as f:
            f.write("*.log\n")

        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli(
                "check-ignore", "test.log", "test.txt"
            )
            log_output = "\n".join(cm.output)
            self.assertIn("test.log", log_output)
            self.assertNotIn("test.txt", log_output)


class LsFilesCommandTest(DulwichCliTestCase):
    """Tests for ls-files command."""

    def test_ls_files(self):
        # Add some files
        for name in ["a.txt", "b.txt", "c.txt"]:
            path = os.path.join(self.repo_path, name)
            with open(path, "w") as f:
                f.write(f"content of {name}")
        self._run_cli("add", "a.txt", "b.txt", "c.txt")

        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("ls-files")
            log_output = "\n".join(cm.output)
            self.assertIn("a.txt", log_output)
            self.assertIn("b.txt", log_output)
            self.assertIn("c.txt", log_output)


class LsTreeCommandTest(DulwichCliTestCase):
    """Tests for ls-tree command."""

    def test_ls_tree(self):
        # Create a directory structure
        os.mkdir(os.path.join(self.repo_path, "subdir"))
        with open(os.path.join(self.repo_path, "file.txt"), "w") as f:
            f.write("file content")
        with open(os.path.join(self.repo_path, "subdir", "nested.txt"), "w") as f:
            f.write("nested content")

        self._run_cli("add", ".")
        self._run_cli("commit", "--message=Initial")

        _result, stdout, _stderr = self._run_cli("ls-tree", "HEAD")
        self.assertIn("file.txt", stdout)
        self.assertIn("subdir", stdout)

    def test_ls_tree_recursive(self):
        # Create nested structure
        os.mkdir(os.path.join(self.repo_path, "subdir"))
        with open(os.path.join(self.repo_path, "subdir", "nested.txt"), "w") as f:
            f.write("nested")

        self._run_cli("add", ".")
        self._run_cli("commit", "--message=Initial")

        _result, stdout, _stderr = self._run_cli("ls-tree", "-r", "HEAD")
        self.assertIn("subdir/nested.txt", stdout)


class DescribeCommandTest(DulwichCliTestCase):
    """Tests for describe command."""

    def test_describe(self):
        # Create tagged commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")
        self._run_cli("tag", "v1.0")

        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            _result, _stdout, _stderr = self._run_cli("describe")
            self.assertIn("v1.0", cm.output[0])


class FsckCommandTest(DulwichCliTestCase):
    """Tests for fsck command."""

    def test_fsck(self):
        # Create a commit
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")

        _result, _stdout, _stderr = self._run_cli("fsck")
        # Should complete without errors


class GrepCommandTest(DulwichCliTestCase):
    """Tests for grep command."""

    def test_grep_basic(self):
        # Create test files
        with open(os.path.join(self.repo_path, "file1.txt"), "w") as f:
            f.write("hello world\n")
        with open(os.path.join(self.repo_path, "file2.txt"), "w") as f:
            f.write("foo bar\n")

        self._run_cli("add", "file1.txt", "file2.txt")
        self._run_cli("commit", "--message=Add files")

        _result, stdout, _stderr = self._run_cli("grep", "world")
        self.assertEqual("file1.txt:hello world\n", stdout.replace("\r\n", "\n"))

    def test_grep_line_numbers(self):
        with open(os.path.join(self.repo_path, "test.txt"), "w") as f:
            f.write("line1\nline2\nline3\n")

        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Add test")

        _result, stdout, _stderr = self._run_cli("grep", "-n", "line")
        self.assertEqual(
            "test.txt:1:line1\ntest.txt:2:line2\ntest.txt:3:line3\n",
            stdout.replace("\r\n", "\n"),
        )

    def test_grep_case_insensitive(self):
        with open(os.path.join(self.repo_path, "case.txt"), "w") as f:
            f.write("Hello World\n")

        self._run_cli("add", "case.txt")
        self._run_cli("commit", "--message=Add case")

        _result, stdout, _stderr = self._run_cli("grep", "-i", "hello")
        self.assertEqual("case.txt:Hello World\n", stdout.replace("\r\n", "\n"))

    def test_grep_no_matches(self):
        with open(os.path.join(self.repo_path, "empty.txt"), "w") as f:
            f.write("nothing here\n")

        self._run_cli("add", "empty.txt")
        self._run_cli("commit", "--message=Add empty")

        _result, stdout, _stderr = self._run_cli("grep", "nonexistent")
        self.assertEqual("", stdout)


class RepackCommandTest(DulwichCliTestCase):
    """Tests for repack command."""

    def test_repack(self):
        # Create some objects
        for i in range(5):
            test_file = os.path.join(self.repo_path, f"test{i}.txt")
            with open(test_file, "w") as f:
                f.write(f"content {i}")
            self._run_cli("add", f"test{i}.txt")
            self._run_cli("commit", f"--message=Commit {i}")

        _result, _stdout, _stderr = self._run_cli("repack")
        # Should create pack files
        pack_dir = os.path.join(self.repo_path, ".git", "objects", "pack")
        self.assertTrue(any(f.endswith(".pack") for f in os.listdir(pack_dir)))

    def test_repack_write_bitmap_index(self):
        """Test repack with --write-bitmap-index flag."""
        # Create some objects
        for i in range(5):
            test_file = os.path.join(self.repo_path, f"test{i}.txt")
            with open(test_file, "w") as f:
                f.write(f"content {i}")
            self._run_cli("add", f"test{i}.txt")
            self._run_cli("commit", f"--message=Commit {i}")

        _result, _stdout, _stderr = self._run_cli("repack", "--write-bitmap-index")
        # Should create pack and bitmap files
        pack_dir = os.path.join(self.repo_path, ".git", "objects", "pack")
        self.assertTrue(any(f.endswith(".pack") for f in os.listdir(pack_dir)))
        self.assertTrue(any(f.endswith(".bitmap") for f in os.listdir(pack_dir)))


class ResetCommandTest(DulwichCliTestCase):
    """Tests for reset command."""

    def test_reset_soft(self):
        # Create commits
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("first")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=First")
        first_commit = self.repo.head()

        with open(test_file, "w") as f:
            f.write("second")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Second")

        # Reset soft
        _result, _stdout, _stderr = self._run_cli(
            "reset", "--soft", first_commit.decode()
        )
        # HEAD should be at first commit
        self.assertEqual(self.repo.head(), first_commit)


class WriteTreeCommandTest(DulwichCliTestCase):
    """Tests for write-tree command."""

    def test_write_tree(self):
        # Create and add files
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")

        _result, stdout, _stderr = self._run_cli("write-tree")
        # Should output tree SHA
        self.assertEqual(len(stdout.strip()), 40)


class UpdateServerInfoCommandTest(DulwichCliTestCase):
    """Tests for update-server-info command."""

    def test_update_server_info(self):
        _result, _stdout, _stderr = self._run_cli("update-server-info")
        # Should create info/refs file
        info_refs = os.path.join(self.repo_path, ".git", "info", "refs")
        self.assertTrue(os.path.exists(info_refs))


class SymbolicRefCommandTest(DulwichCliTestCase):
    """Tests for symbolic-ref command."""

    def test_symbolic_ref(self):
        # Create a branch
        test_file = os.path.join(self.repo_path, "test.txt")
        with open(test_file, "w") as f:
            f.write("test")
        self._run_cli("add", "test.txt")
        self._run_cli("commit", "--message=Initial")
        self._run_cli("branch", "test-branch")

        _result, _stdout, _stderr = self._run_cli(
            "symbolic-ref", "HEAD", "refs/heads/test-branch"
        )
        # HEAD should now point to test-branch
        self.assertEqual(
            self.repo.refs.read_ref(b"HEAD"), b"ref: refs/heads/test-branch"
        )


class BundleCommandTest(DulwichCliTestCase):
    """Tests for bundle commands."""

    def setUp(self):
        super().setUp()
        # Create a basic repository with some commits for bundle testing
        # Create initial commit
        test_file = os.path.join(self.repo_path, "file1.txt")
        with open(test_file, "w") as f:
            f.write("Content of file1\n")
        self._run_cli("add", "file1.txt")
        self._run_cli("commit", "--message=Initial commit")

        # Create second commit
        test_file2 = os.path.join(self.repo_path, "file2.txt")
        with open(test_file2, "w") as f:
            f.write("Content of file2\n")
        self._run_cli("add", "file2.txt")
        self._run_cli("commit", "--message=Add file2")

        # Create a branch and tag for testing
        self._run_cli("branch", "feature")
        self._run_cli("tag", "v1.0")

    def test_bundle_create_basic(self):
        """Test basic bundle creation."""
        bundle_file = os.path.join(self.test_dir, "test.bundle")

        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))
        self.assertGreater(os.path.getsize(bundle_file), 0)

    def test_bundle_create_all_refs(self):
        """Test bundle creation with --all flag."""
        bundle_file = os.path.join(self.test_dir, "all.bundle")

        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", "--all", bundle_file
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

    def test_bundle_create_specific_refs(self):
        """Test bundle creation with specific refs."""
        bundle_file = os.path.join(self.test_dir, "refs.bundle")

        # Only use HEAD since feature branch may not exist
        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

    def test_bundle_create_with_range(self):
        """Test bundle creation with commit range."""
        # Get the first commit SHA by looking at the log
        result, stdout, _stderr = self._run_cli("log", "--reverse")
        lines = stdout.strip().split("\n")
        # Find first commit line that contains a SHA
        first_commit = None
        for line in lines:
            if line.startswith("commit "):
                first_commit = line.split()[1][:8]  # Get short SHA
                break

        if first_commit:
            bundle_file = os.path.join(self.test_dir, "range.bundle")

            result, stdout, _stderr = self._run_cli(
                "bundle", "create", bundle_file, f"{first_commit}..HEAD"
            )
            self.assertEqual(result, 0)
            self.assertTrue(os.path.exists(bundle_file))
        else:
            self.skipTest("Could not determine first commit SHA")

    def test_bundle_create_to_stdout(self):
        """Test bundle creation to stdout."""
        result, stdout, _stderr = self._run_cli("bundle", "create", "-", "HEAD")
        self.assertEqual(result, 0)
        self.assertGreater(len(stdout), 0)
        # Bundle output is binary, so check it's not empty
        self.assertIsInstance(stdout, (str, bytes))

    def test_bundle_create_no_refs(self):
        """Test bundle creation with no refs specified."""
        bundle_file = os.path.join(self.test_dir, "noref.bundle")

        with self.assertLogs("dulwich.cli", level="ERROR") as cm:
            result, _stdout, _stderr = self._run_cli("bundle", "create", bundle_file)
            self.assertEqual(result, 1)
            self.assertIn("No refs specified", cm.output[0])

    def test_bundle_create_empty_bundle_refused(self):
        """Test that empty bundles are refused."""
        bundle_file = os.path.join(self.test_dir, "empty.bundle")

        # Try to create bundle with non-existent ref - this should fail with KeyError
        with self.assertRaises(KeyError):
            _result, _stdout, _stderr = self._run_cli(
                "bundle", "create", bundle_file, "nonexistent-ref"
            )

    def test_bundle_verify_valid(self):
        """Test bundle verification of valid bundle."""
        bundle_file = os.path.join(self.test_dir, "valid.bundle")

        # First create a bundle
        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)

        # Now verify it
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("bundle", "verify", bundle_file)
            self.assertEqual(result, 0)
            self.assertIn("valid and can be applied", cm.output[0])

    def test_bundle_verify_quiet(self):
        """Test bundle verification with quiet flag."""
        bundle_file = os.path.join(self.test_dir, "quiet.bundle")

        # Create bundle
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Verify quietly
        result, stdout, _stderr = self._run_cli(
            "bundle", "verify", "--quiet", bundle_file
        )
        self.assertEqual(result, 0)
        self.assertEqual(stdout.strip(), "")

    def test_bundle_verify_from_stdin(self):
        """Test bundle verification from stdin."""
        bundle_file = os.path.join(self.test_dir, "stdin.bundle")

        # Create bundle
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Read bundle content
        with open(bundle_file, "rb") as f:
            bundle_content = f.read()

        # Mock stdin with bundle content
        old_stdin = sys.stdin
        try:
            sys.stdin = io.BytesIO(bundle_content)
            sys.stdin.buffer = sys.stdin
            result, _stdout, _stderr = self._run_cli("bundle", "verify", "-")
            self.assertEqual(result, 0)
        finally:
            sys.stdin = old_stdin

    def test_bundle_list_heads(self):
        """Test listing bundle heads."""
        bundle_file = os.path.join(self.test_dir, "heads.bundle")

        # Create bundle with HEAD only
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # List heads
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "bundle", "list-heads", bundle_file
            )
            self.assertEqual(result, 0)
            # Should contain at least the HEAD reference
            self.assertTrue(len(cm.output) > 0)

    def test_bundle_list_heads_specific_refs(self):
        """Test listing specific bundle heads."""
        bundle_file = os.path.join(self.test_dir, "specific.bundle")

        # Create bundle with HEAD
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # List heads without filtering
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "bundle", "list-heads", bundle_file
            )
            self.assertEqual(result, 0)
            # Should contain some reference
            self.assertTrue(len(cm.output) > 0)

    def test_bundle_list_heads_from_stdin(self):
        """Test listing bundle heads from stdin."""
        bundle_file = os.path.join(self.test_dir, "stdin-heads.bundle")

        # Create bundle
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Read bundle content
        with open(bundle_file, "rb") as f:
            bundle_content = f.read()

        # Mock stdin
        old_stdin = sys.stdin
        try:
            sys.stdin = io.BytesIO(bundle_content)
            sys.stdin.buffer = sys.stdin
            result, _stdout, _stderr = self._run_cli("bundle", "list-heads", "-")
            self.assertEqual(result, 0)
        finally:
            sys.stdin = old_stdin

    def test_bundle_unbundle(self):
        """Test bundle unbundling."""
        bundle_file = os.path.join(self.test_dir, "unbundle.bundle")

        # Create bundle
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Unbundle
        result, _stdout, _stderr = self._run_cli("bundle", "unbundle", bundle_file)
        self.assertEqual(result, 0)

    def test_bundle_unbundle_specific_refs(self):
        """Test unbundling specific refs."""
        bundle_file = os.path.join(self.test_dir, "unbundle-specific.bundle")

        # Create bundle with HEAD
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Unbundle only HEAD
        result, _stdout, _stderr = self._run_cli(
            "bundle", "unbundle", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)

    def test_bundle_unbundle_from_stdin(self):
        """Test unbundling from stdin."""
        bundle_file = os.path.join(self.test_dir, "stdin-unbundle.bundle")

        # Create bundle
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Read bundle content to simulate stdin
        with open(bundle_file, "rb") as f:
            bundle_content = f.read()

        # Mock stdin with bundle content
        old_stdin = sys.stdin
        try:
            # Create a BytesIO object with buffer attribute
            mock_stdin = io.BytesIO(bundle_content)
            mock_stdin.buffer = mock_stdin
            sys.stdin = mock_stdin

            result, _stdout, _stderr = self._run_cli("bundle", "unbundle", "-")
            self.assertEqual(result, 0)
        finally:
            sys.stdin = old_stdin

    def test_bundle_unbundle_with_progress(self):
        """Test unbundling with progress output."""
        bundle_file = os.path.join(self.test_dir, "progress.bundle")

        # Create bundle
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Unbundle with progress
        result, _stdout, _stderr = self._run_cli(
            "bundle", "unbundle", "--progress", bundle_file
        )
        self.assertEqual(result, 0)

    def test_bundle_create_with_progress(self):
        """Test bundle creation with progress output."""
        bundle_file = os.path.join(self.test_dir, "create-progress.bundle")

        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", "--progress", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

    def test_bundle_create_with_quiet(self):
        """Test bundle creation with quiet flag."""
        bundle_file = os.path.join(self.test_dir, "quiet-create.bundle")

        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", "--quiet", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

    def test_bundle_create_version_2(self):
        """Test bundle creation with specific version."""
        bundle_file = os.path.join(self.test_dir, "v2.bundle")

        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", "--version", "2", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

    def test_bundle_create_version_3(self):
        """Test bundle creation with version 3."""
        bundle_file = os.path.join(self.test_dir, "v3.bundle")

        result, _stdout, _stderr = self._run_cli(
            "bundle", "create", "--version", "3", bundle_file, "HEAD"
        )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

    def test_bundle_invalid_subcommand(self):
        """Test invalid bundle subcommand."""
        with self.assertLogs("dulwich.cli", level="ERROR") as cm:
            result, _stdout, _stderr = self._run_cli("bundle", "invalid-command")
            self.assertEqual(result, 1)
            self.assertIn("Unknown bundle subcommand", cm.output[0])

    def test_bundle_no_subcommand(self):
        """Test bundle command with no subcommand."""
        with self.assertLogs("dulwich.cli", level="ERROR") as cm:
            result, _stdout, _stderr = self._run_cli("bundle")
            self.assertEqual(result, 1)
            self.assertIn("Usage: bundle", cm.output[0])

    def test_bundle_create_with_stdin_refs(self):
        """Test bundle creation reading refs from stdin."""
        bundle_file = os.path.join(self.test_dir, "stdin-refs.bundle")

        # Mock stdin with refs
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("master\nfeature\n")
            result, _stdout, _stderr = self._run_cli(
                "bundle", "create", "--stdin", bundle_file
            )
            self.assertEqual(result, 0)
            self.assertTrue(os.path.exists(bundle_file))
        finally:
            sys.stdin = old_stdin

    def test_bundle_verify_missing_prerequisites(self):
        """Test bundle verification with missing prerequisites."""
        # Create a simple bundle first
        bundle_file = os.path.join(self.test_dir, "prereq.bundle")
        self._run_cli("bundle", "create", bundle_file, "HEAD")

        # Create a new repo to simulate missing objects
        new_repo_path = os.path.join(self.test_dir, "new_repo")
        os.mkdir(new_repo_path)
        new_repo = Repo.init(new_repo_path)
        new_repo.close()

        # Try to verify in new repo
        old_cwd = os.getcwd()
        try:
            os.chdir(new_repo_path)
            result, _stdout, _stderr = self._run_cli("bundle", "verify", bundle_file)
            # Just check that verification runs - result depends on bundle content
            self.assertIn(result, [0, 1])
        finally:
            os.chdir(old_cwd)

    def test_bundle_create_with_committish_range(self):
        """Test bundle creation with commit range using parse_committish_range."""
        # Create additional commits for range testing
        test_file3 = os.path.join(self.repo_path, "file3.txt")
        with open(test_file3, "w") as f:
            f.write("Content of file3\n")
        self._run_cli("add", "file3.txt")
        self._run_cli("commit", "--message=Add file3")

        # Get commit SHAs
        result, stdout, _stderr = self._run_cli("log")
        lines = stdout.strip().split("\n")
        # Extract SHAs from commit lines
        commits = []
        for line in lines:
            if line.startswith("commit:"):
                sha = line.split()[1]
                commits.append(sha[:8])  # Get short SHA

        # We should have exactly 3 commits: Add file3, Add file2, Initial commit
        self.assertEqual(len(commits), 3)

        bundle_file = os.path.join(self.test_dir, "range-test.bundle")

        # Test with commit range using .. syntax
        # Create a bundle containing commits reachable from commits[0] but not from commits[2]
        result, stdout, stderr = self._run_cli(
            "bundle", "create", bundle_file, f"{commits[2]}..HEAD"
        )
        if result != 0:
            self.fail(
                f"Bundle create failed with exit code {result}. stdout: {stdout!r}, stderr: {stderr!r}"
            )
        self.assertEqual(result, 0)
        self.assertTrue(os.path.exists(bundle_file))

        # Verify the bundle was created
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, stdout, stderr = self._run_cli("bundle", "verify", bundle_file)
            self.assertEqual(result, 0)
            self.assertIn("valid and can be applied", cm.output[0])


class FormatBytesTestCase(TestCase):
    """Tests for format_bytes function."""

    def test_bytes(self):
        """Test formatting bytes."""
        self.assertEqual("0.0 B", format_bytes(0))
        self.assertEqual("1.0 B", format_bytes(1))
        self.assertEqual("512.0 B", format_bytes(512))
        self.assertEqual("1023.0 B", format_bytes(1023))

    def test_kilobytes(self):
        """Test formatting kilobytes."""
        self.assertEqual("1.0 KB", format_bytes(1024))
        self.assertEqual("1.5 KB", format_bytes(1536))
        self.assertEqual("2.0 KB", format_bytes(2048))
        self.assertEqual("1023.0 KB", format_bytes(1024 * 1023))

    def test_megabytes(self):
        """Test formatting megabytes."""
        self.assertEqual("1.0 MB", format_bytes(1024 * 1024))
        self.assertEqual("1.5 MB", format_bytes(1024 * 1024 * 1.5))
        self.assertEqual("10.0 MB", format_bytes(1024 * 1024 * 10))
        self.assertEqual("1023.0 MB", format_bytes(1024 * 1024 * 1023))

    def test_gigabytes(self):
        """Test formatting gigabytes."""
        self.assertEqual("1.0 GB", format_bytes(1024 * 1024 * 1024))
        self.assertEqual("2.5 GB", format_bytes(1024 * 1024 * 1024 * 2.5))
        self.assertEqual("1023.0 GB", format_bytes(1024 * 1024 * 1024 * 1023))

    def test_terabytes(self):
        """Test formatting terabytes."""
        self.assertEqual("1.0 TB", format_bytes(1024 * 1024 * 1024 * 1024))
        self.assertEqual("5.0 TB", format_bytes(1024 * 1024 * 1024 * 1024 * 5))
        self.assertEqual("1000.0 TB", format_bytes(1024 * 1024 * 1024 * 1024 * 1000))


class GetPagerTest(TestCase):
    """Tests for get_pager function."""

    def setUp(self):
        super().setUp()
        # Save original environment
        self.original_env = os.environ.copy()
        # Clear pager-related environment variables
        for var in ["DULWICH_PAGER", "GIT_PAGER", "PAGER"]:
            os.environ.pop(var, None)
        # Reset the global pager disable flag
        cli.get_pager._disabled = False

    def tearDown(self):
        super().tearDown()
        # Restore original environment
        os.environ.clear()
        os.environ.update(self.original_env)
        # Reset the global pager disable flag
        cli.get_pager._disabled = False

    def test_pager_disabled_globally(self):
        """Test that globally disabled pager returns stdout wrapper."""
        cli.disable_pager()
        pager = cli.get_pager()
        self.assertIsInstance(pager, cli._StreamContextAdapter)
        self.assertEqual(pager.stream, sys.stdout)

    def test_pager_not_tty(self):
        """Test that pager is disabled when stdout is not a TTY."""
        with patch("sys.stdout.isatty", return_value=False):
            pager = cli.get_pager()
            self.assertIsInstance(pager, cli._StreamContextAdapter)

    def test_pager_env_dulwich_pager(self):
        """Test DULWICH_PAGER environment variable."""
        os.environ["DULWICH_PAGER"] = "custom_pager"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager()
            self.assertIsInstance(pager, cli.Pager)
            self.assertEqual(pager.pager_cmd, "custom_pager")

    def test_pager_env_dulwich_pager_false(self):
        """Test DULWICH_PAGER=false disables pager."""
        os.environ["DULWICH_PAGER"] = "false"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager()
            self.assertIsInstance(pager, cli._StreamContextAdapter)

    def test_pager_env_git_pager(self):
        """Test GIT_PAGER environment variable."""
        os.environ["GIT_PAGER"] = "git_custom_pager"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager()
            self.assertIsInstance(pager, cli.Pager)
            self.assertEqual(pager.pager_cmd, "git_custom_pager")

    def test_pager_env_pager(self):
        """Test PAGER environment variable."""
        os.environ["PAGER"] = "my_pager"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager()
            self.assertIsInstance(pager, cli.Pager)
            self.assertEqual(pager.pager_cmd, "my_pager")

    def test_pager_env_priority(self):
        """Test environment variable priority order."""
        os.environ["PAGER"] = "pager_low"
        os.environ["GIT_PAGER"] = "pager_medium"
        os.environ["DULWICH_PAGER"] = "pager_high"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager()
            self.assertEqual(pager.pager_cmd, "pager_high")

    def test_pager_config_core_pager(self):
        """Test core.pager configuration."""
        config = MagicMock()
        config.get.return_value = b"config_pager"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager(config=config)
            self.assertIsInstance(pager, cli.Pager)
            self.assertEqual(pager.pager_cmd, "config_pager")
            config.get.assert_called_with(("core",), b"pager")

    def test_pager_config_core_pager_false(self):
        """Test core.pager=false disables pager."""
        config = MagicMock()
        config.get.return_value = b"false"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager(config=config)
            self.assertIsInstance(pager, cli._StreamContextAdapter)

    def test_pager_config_core_pager_empty(self):
        """Test core.pager="" disables pager."""
        config = MagicMock()
        config.get.return_value = b""
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager(config=config)
            self.assertIsInstance(pager, cli._StreamContextAdapter)

    def test_pager_config_per_command(self):
        """Test per-command pager configuration."""
        config = MagicMock()
        config.get.side_effect = lambda section, key: {
            (("pager",), b"log"): b"log_pager",
        }.get((section, key), KeyError())

        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager(config=config, cmd_name="log")
            self.assertIsInstance(pager, cli.Pager)
            self.assertEqual(pager.pager_cmd, "log_pager")

    def test_pager_config_per_command_false(self):
        """Test per-command pager=false disables pager."""
        config = MagicMock()
        config.get.return_value = b"false"
        with patch("sys.stdout.isatty", return_value=True):
            pager = cli.get_pager(config=config, cmd_name="log")
            self.assertIsInstance(pager, cli._StreamContextAdapter)

    def test_pager_config_per_command_true(self):
        """Test per-command pager=true uses default pager."""
        config = MagicMock()

        def get_side_effect(section, key):
            if section == ("pager",) and key == b"log":
                return b"true"
            raise KeyError

        config.get.side_effect = get_side_effect

        with patch("sys.stdout.isatty", return_value=True):
            with patch("shutil.which", side_effect=lambda cmd: cmd == "less"):
                pager = cli.get_pager(config=config, cmd_name="log")
                self.assertIsInstance(pager, cli.Pager)
                self.assertEqual(pager.pager_cmd, "less -FRX")

    def test_pager_priority_order(self):
        """Test complete priority order."""
        # Set up all possible configurations
        os.environ["PAGER"] = "env_pager"
        os.environ["GIT_PAGER"] = "env_git_pager"

        config = MagicMock()

        def get_side_effect(section, key):
            if section == ("pager",) and key == b"log":
                return b"cmd_pager"
            elif section == ("core",) and key == b"pager":
                return b"core_pager"
            raise KeyError

        config.get.side_effect = get_side_effect

        with patch("sys.stdout.isatty", return_value=True):
            # Per-command config should win
            pager = cli.get_pager(config=config, cmd_name="log")
            self.assertEqual(pager.pager_cmd, "cmd_pager")

    def test_pager_fallback_less(self):
        """Test fallback to less with proper flags."""
        with patch("sys.stdout.isatty", return_value=True):
            with patch("shutil.which", side_effect=lambda cmd: cmd == "less"):
                pager = cli.get_pager()
                self.assertIsInstance(pager, cli.Pager)
                self.assertEqual(pager.pager_cmd, "less -FRX")

    def test_pager_fallback_more(self):
        """Test fallback to more when less is not available."""
        with patch("sys.stdout.isatty", return_value=True):
            with patch("shutil.which", side_effect=lambda cmd: cmd == "more"):
                pager = cli.get_pager()
                self.assertIsInstance(pager, cli.Pager)
                self.assertEqual(pager.pager_cmd, "more")

    def test_pager_fallback_cat(self):
        """Test ultimate fallback to cat."""
        with patch("sys.stdout.isatty", return_value=True):
            with patch("shutil.which", return_value=None):
                pager = cli.get_pager()
                self.assertIsInstance(pager, cli.Pager)
                self.assertEqual(pager.pager_cmd, "cat")

    def test_pager_context_manager(self):
        """Test that pager works as a context manager."""
        with patch("sys.stdout.isatty", return_value=True):
            with cli.get_pager() as pager:
                self.assertTrue(hasattr(pager, "write"))
                self.assertTrue(hasattr(pager, "flush"))


class WorktreeCliTests(DulwichCliTestCase):
    """Tests for worktree CLI commands."""

    def setUp(self):
        super().setUp()
        # Base class already creates and initializes the repo
        # Just create initial commit
        with open(os.path.join(self.repo_path, "test.txt"), "w") as f:
            f.write("test content")
        from dulwich import porcelain

        porcelain.add(self.repo_path, ["test.txt"])
        porcelain.commit(self.repo_path, message=b"Initial commit")

    def test_worktree_list(self):
        """Test worktree list command."""
        # Change to repo directory
        old_cwd = os.getcwd()
        os.chdir(self.repo_path)
        try:
            io.StringIO()
            cmd = cli.cmd_worktree()
            result = cmd.run(["list"])

            # Should list the main worktree
            self.assertEqual(result, 0)
        finally:
            os.chdir(old_cwd)

    def test_worktree_add(self):
        """Test worktree add command."""
        wt_path = os.path.join(self.test_dir, "worktree1")

        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "worktree", "add", wt_path, "feature"
            )
            self.assertEqual(result, 0)
            self.assertTrue(os.path.exists(wt_path))
            log_output = "\n".join(cm.output)
            self.assertIn("Worktree added:", log_output)

    def test_worktree_add_detached(self):
        """Test worktree add with detached HEAD."""
        wt_path = os.path.join(self.test_dir, "detached-wt")

        # Change to repo directory
        old_cwd = os.getcwd()
        os.chdir(self.repo_path)
        try:
            cmd = cli.cmd_worktree()
            with patch("sys.stdout", new_callable=io.StringIO):
                result = cmd.run(["add", "--detach", wt_path])

            self.assertEqual(result, 0)
            self.assertTrue(os.path.exists(wt_path))
        finally:
            os.chdir(old_cwd)

    def test_worktree_remove(self):
        """Test worktree remove command."""
        # First add a worktree
        wt_path = os.path.join(self.test_dir, "to-remove")
        result, _stdout, _stderr = self._run_cli("worktree", "add", wt_path)
        self.assertEqual(result, 0)

        # Then remove it
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("worktree", "remove", wt_path)
            self.assertEqual(result, 0)
            self.assertFalse(os.path.exists(wt_path))
            log_output = "\n".join(cm.output)
            self.assertIn("Worktree removed:", log_output)

    def test_worktree_prune(self):
        """Test worktree prune command."""
        # Add a worktree and manually remove it
        wt_path = os.path.join(self.test_dir, "to-prune")
        result, _stdout, _stderr = self._run_cli("worktree", "add", wt_path)
        self.assertEqual(result, 0)
        shutil.rmtree(wt_path)

        # Prune
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("worktree", "prune", "-v")
            self.assertEqual(result, 0)
            log_output = "\n".join(cm.output)
            self.assertIn("to-prune", log_output)

    def test_worktree_lock_unlock(self):
        """Test worktree lock and unlock commands."""
        # Add a worktree
        wt_path = os.path.join(self.test_dir, "lockable")
        result, _stdout, _stderr = self._run_cli("worktree", "add", wt_path)
        self.assertEqual(result, 0)

        # Lock it
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "worktree", "lock", wt_path, "--reason", "Testing"
            )
            self.assertEqual(result, 0)
            log_output = "\n".join(cm.output)
            self.assertIn("Worktree locked:", log_output)

        # Unlock it
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("worktree", "unlock", wt_path)
            self.assertEqual(result, 0)
            log_output = "\n".join(cm.output)
            self.assertIn("Worktree unlocked:", log_output)

    def test_worktree_move(self):
        """Test worktree move command."""
        # Add a worktree
        old_path = os.path.join(self.test_dir, "old-location")
        new_path = os.path.join(self.test_dir, "new-location")
        result, _stdout, _stderr = self._run_cli("worktree", "add", old_path)
        self.assertEqual(result, 0)

        # Move it
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli(
                "worktree", "move", old_path, new_path
            )
            self.assertEqual(result, 0)
            self.assertFalse(os.path.exists(old_path))
            self.assertTrue(os.path.exists(new_path))
            log_output = "\n".join(cm.output)
            self.assertIn("Worktree moved:", log_output)

    def test_worktree_invalid_command(self):
        """Test invalid worktree subcommand."""
        cmd = cli.cmd_worktree()
        with patch("sys.stderr", new_callable=io.StringIO):
            with self.assertRaises(SystemExit):
                cmd.run(["invalid"])


class MergeBaseCommandTest(DulwichCliTestCase):
    """Tests for merge-base command."""

    def _create_commits(self):
        """Helper to create a commit history for testing."""
        # Create three commits in linear history
        for i in range(1, 4):
            test_file = os.path.join(self.repo_path, f"file{i}.txt")
            with open(test_file, "w") as f:
                f.write(f"content{i}")
            self._run_cli("add", f"file{i}.txt")
            self._run_cli("commit", f"--message=Commit {i}")

    def test_merge_base_linear_history(self):
        """Test merge-base with linear history."""
        self._create_commits()

        result, stdout, _stderr = self._run_cli("merge-base", "HEAD", "HEAD~1")
        self.assertEqual(result, 0)

        # Should return HEAD~1 as the merge base
        output = stdout.strip()
        # Verify it's a valid commit ID (40 hex chars)
        self.assertEqual(len(output), 40)
        self.assertTrue(all(c in "0123456789abcdef" for c in output))

    def test_merge_base_is_ancestor_true(self):
        """Test merge-base --is-ancestor when true."""
        self._create_commits()

        result, _stdout, _stderr = self._run_cli(
            "merge-base", "--is-ancestor", "HEAD~1", "HEAD"
        )
        self.assertEqual(result, 0)  # Exit code 0 means true

    def test_merge_base_is_ancestor_false(self):
        """Test merge-base --is-ancestor when false."""
        self._create_commits()

        result, _stdout, _stderr = self._run_cli(
            "merge-base", "--is-ancestor", "HEAD", "HEAD~1"
        )
        self.assertEqual(result, 1)  # Exit code 1 means false

    def test_merge_base_independent(self):
        """Test merge-base --independent."""
        self._create_commits()

        # All three commits in linear history - only HEAD should be independent
        head = self.repo.refs[b"HEAD"]
        head_1 = self.repo[head].parents[0]
        head_2 = self.repo[head_1].parents[0]

        result, stdout, _stderr = self._run_cli(
            "merge-base",
            "--independent",
            head.decode(),
            head_1.decode(),
            head_2.decode(),
        )
        self.assertEqual(result, 0)

        # Only HEAD should be in output (as it's the only independent commit)
        lines = stdout.strip().split("\n")
        self.assertEqual(len(lines), 1)
        self.assertEqual(lines[0], head.decode())

    def test_merge_base_requires_two_commits(self):
        """Test merge-base requires at least two commits."""
        self._create_commits()

        result, _stdout, _stderr = self._run_cli("merge-base", "HEAD")
        self.assertEqual(result, 1)

    def test_merge_base_is_ancestor_requires_two_commits(self):
        """Test merge-base --is-ancestor requires exactly two commits."""
        self._create_commits()

        result, _stdout, _stderr = self._run_cli("merge-base", "--is-ancestor", "HEAD")
        self.assertEqual(result, 1)


class ConfigCommandTest(DulwichCliTestCase):
    """Tests for config command."""

    def test_config_set_and_get(self):
        """Test setting and getting a config value."""
        # Set a config value
        result, stdout, _stderr = self._run_cli("config", "user.name", "Test User")
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "")

        # Get the value back
        result, stdout, _stderr = self._run_cli("config", "user.name")
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "Test User\n")

    def test_config_set_and_get_subsection(self):
        """Test setting and getting a config value with subsection."""
        # Set a config value with subsection (e.g., remote.origin.url)
        result, stdout, _stderr = self._run_cli(
            "config", "remote.origin.url", "https://example.com/repo.git"
        )
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "")

        # Get the value back
        result, stdout, _stderr = self._run_cli("config", "remote.origin.url")
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "https://example.com/repo.git\n")

    def test_config_list(self):
        """Test listing all config values."""
        # Set some config values
        self._run_cli("config", "user.name", "Test User")
        self._run_cli("config", "user.email", "test@example.com")

        # Get the actual config values that may vary by platform
        config = self.repo.get_config()
        filemode = config.get((b"core",), b"filemode")
        try:
            symlinks = config.get((b"core",), b"symlinks")
        except KeyError:
            symlinks = None

        # List all values
        result, stdout, _stderr = self._run_cli("config", "--list")
        self.assertEqual(result, 0)

        # Build expected output with platform-specific values
        expected = "core.repositoryformatversion=0\n"
        expected += f"core.filemode={filemode.decode('utf-8')}\n"
        if symlinks is not None:
            expected += f"core.symlinks={symlinks.decode('utf-8')}\n"
        expected += (
            "core.bare=false\n"
            "core.logallrefupdates=true\n"
            "user.name=Test User\n"
            "user.email=test@example.com\n"
        )

        self.assertEqual(stdout, expected)

    def test_config_unset(self):
        """Test unsetting a config value."""
        # Set a config value
        self._run_cli("config", "user.name", "Test User")

        # Verify it's set
        result, stdout, _stderr = self._run_cli("config", "user.name")
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "Test User\n")

        # Unset it
        result, stdout, _stderr = self._run_cli("config", "--unset", "user.name")
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "")

        # Verify it's gone
        result, stdout, _stderr = self._run_cli("config", "user.name")
        self.assertEqual(result, 1)
        self.assertEqual(stdout, "")

    def test_config_get_nonexistent(self):
        """Test getting a nonexistent config value."""
        result, stdout, _stderr = self._run_cli("config", "nonexistent.key")
        self.assertEqual(result, 1)
        self.assertEqual(stdout, "")

    def test_config_unset_nonexistent(self):
        """Test unsetting a nonexistent config value."""
        result, _stdout, _stderr = self._run_cli("config", "--unset", "nonexistent.key")
        self.assertEqual(result, 1)

    def test_config_invalid_key_format(self):
        """Test config with invalid key format."""
        result, stdout, _stderr = self._run_cli("config", "invalidkey")
        self.assertEqual(result, 1)
        self.assertEqual(stdout, "")

    def test_config_get_all(self):
        """Test getting all values for a multivar."""
        # Set multiple values for the same key
        config = self.repo.get_config()
        config.set(("test",), "multivar", "value1")
        config.add(("test",), "multivar", "value2")
        config.add(("test",), "multivar", "value3")
        config.write_to_path()

        # Get all values
        result, stdout, _stderr = self._run_cli("config", "--get-all", "test.multivar")
        self.assertEqual(result, 0)
        self.assertEqual(stdout, "value1\nvalue2\nvalue3\n")


class GitFlushTest(TestCase):
    """Tests for GIT_FLUSH environment variable support."""

    def test_should_auto_flush_with_git_flush_1(self):
        """Test that GIT_FLUSH=1 enables auto-flushing."""

        mock_stream = MagicMock()
        mock_stream.isatty.return_value = True

        self.assertTrue(_should_auto_flush(mock_stream, env={"GIT_FLUSH": "1"}))

    def test_should_auto_flush_with_git_flush_0(self):
        """Test that GIT_FLUSH=0 disables auto-flushing."""
        mock_stream = MagicMock()
        mock_stream.isatty.return_value = True

        self.assertFalse(_should_auto_flush(mock_stream, env={"GIT_FLUSH": "0"}))

    def test_should_auto_flush_auto_detect_tty(self):
        """Test that auto-detect returns False for TTY (no flush needed)."""
        mock_stream = MagicMock()
        mock_stream.isatty.return_value = True

        self.assertFalse(_should_auto_flush(mock_stream, env={}))

    def test_should_auto_flush_auto_detect_pipe(self):
        """Test that auto-detect returns True for pipes (flush needed)."""
        mock_stream = MagicMock()
        mock_stream.isatty.return_value = False

        self.assertTrue(_should_auto_flush(mock_stream, env={}))

    def test_text_wrapper_flushes_on_write(self):
        """Test that AutoFlushTextIOWrapper flushes after write."""
        mock_stream = MagicMock()
        wrapper = AutoFlushTextIOWrapper(mock_stream)

        wrapper.write("test")
        mock_stream.write.assert_called_once_with("test")
        mock_stream.flush.assert_called_once()

    def test_text_wrapper_flushes_on_writelines(self):
        """Test that AutoFlushTextIOWrapper flushes after writelines."""
        from dulwich.cli import AutoFlushTextIOWrapper

        mock_stream = MagicMock()
        wrapper = AutoFlushTextIOWrapper(mock_stream)

        wrapper.writelines(["line1\n", "line2\n"])
        mock_stream.writelines.assert_called_once()
        mock_stream.flush.assert_called_once()

    def test_binary_wrapper_flushes_on_write(self):
        """Test that AutoFlushBinaryIOWrapper flushes after write."""
        mock_stream = MagicMock()
        wrapper = AutoFlushBinaryIOWrapper(mock_stream)

        wrapper.write(b"test")
        mock_stream.write.assert_called_once_with(b"test")
        mock_stream.flush.assert_called_once()

    def test_text_wrapper_env_classmethod(self):
        """Test that AutoFlushTextIOWrapper.env() respects GIT_FLUSH."""
        mock_stream = MagicMock()
        mock_stream.isatty.return_value = False

        wrapper = AutoFlushTextIOWrapper.env(mock_stream, env={"GIT_FLUSH": "1"})
        self.assertIsInstance(wrapper, AutoFlushTextIOWrapper)

        wrapper = AutoFlushTextIOWrapper.env(mock_stream, env={"GIT_FLUSH": "0"})
        self.assertIs(mock_stream, wrapper)

    def test_binary_wrapper_env_classmethod(self):
        """Test that AutoFlushBinaryIOWrapper.env() respects GIT_FLUSH."""
        mock_stream = MagicMock()
        mock_stream.isatty.return_value = False

        wrapper = AutoFlushBinaryIOWrapper.env(mock_stream, env={"GIT_FLUSH": "1"})
        self.assertIsInstance(wrapper, AutoFlushBinaryIOWrapper)

        wrapper = AutoFlushBinaryIOWrapper.env(mock_stream, env={"GIT_FLUSH": "0"})
        self.assertIs(wrapper, mock_stream)

    def test_wrapper_delegates_attributes(self):
        """Test that wrapper delegates unknown attributes to stream."""
        mock_stream = MagicMock()
        mock_stream.encoding = "utf-8"
        wrapper = AutoFlushTextIOWrapper(mock_stream)

        self.assertEqual(wrapper.encoding, "utf-8")

    def test_wrapper_context_manager(self):
        """Test that wrapper supports context manager protocol."""
        mock_stream = MagicMock()
        wrapper = AutoFlushTextIOWrapper(mock_stream)

        with wrapper as w:
            self.assertIs(w, wrapper)


class MaintenanceCommandTest(DulwichCliTestCase):
    """Tests for maintenance command."""

    def setUp(self):
        super().setUp()
        # Set up a temporary HOME for testing global config
        self.temp_home = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.temp_home)
        self.overrideEnv("HOME", self.temp_home)

    def test_maintenance_run_default(self):
        """Test maintenance run with default tasks."""
        result, _stdout, _stderr = self._run_cli("maintenance", "run")
        self.assertIsNone(result)

    def test_maintenance_run_specific_task(self):
        """Test maintenance run with a specific task."""
        result, _stdout, _stderr = self._run_cli(
            "maintenance", "run", "--task", "pack-refs"
        )
        self.assertIsNone(result)

    def test_maintenance_run_multiple_tasks(self):
        """Test maintenance run with multiple specific tasks."""
        result, _stdout, _stderr = self._run_cli(
            "maintenance", "run", "--task", "pack-refs", "--task", "gc"
        )
        self.assertIsNone(result)

    def test_maintenance_run_quiet(self):
        """Test maintenance run with quiet flag."""
        result, _stdout, _stderr = self._run_cli("maintenance", "run", "--quiet")
        self.assertIsNone(result)

    def test_maintenance_run_auto(self):
        """Test maintenance run with auto flag."""
        result, _stdout, _stderr = self._run_cli("maintenance", "run", "--auto")
        self.assertIsNone(result)

    def test_maintenance_no_subcommand(self):
        """Test maintenance command without subcommand shows help."""
        result, _stdout, _stderr = self._run_cli("maintenance")
        self.assertEqual(result, 1)

    def test_maintenance_register(self):
        """Test maintenance register subcommand."""
        result, _stdout, _stderr = self._run_cli("maintenance", "register")
        self.assertIsNone(result)

    def test_maintenance_unregister(self):
        """Test maintenance unregister subcommand."""
        # First register
        _result, _stdout, _stderr = self._run_cli("maintenance", "register")

        # Then unregister
        result, _stdout, _stderr = self._run_cli("maintenance", "unregister")
        self.assertIsNone(result)

    def test_maintenance_unregister_not_registered(self):
        """Test unregistering a repository that is not registered."""
        result, _stdout, _stderr = self._run_cli("maintenance", "unregister")
        self.assertEqual(result, 1)

    def test_maintenance_unregister_force(self):
        """Test unregistering with --force flag."""
        result, _stdout, _stderr = self._run_cli("maintenance", "unregister", "--force")
        self.assertIsNone(result)

    def test_maintenance_unimplemented_subcommand(self):
        """Test unimplemented maintenance subcommands."""
        for subcommand in ["start", "stop"]:
            result, _stdout, _stderr = self._run_cli("maintenance", subcommand)
            self.assertEqual(result, 1)


class InterpretTrailersCommandTest(DulwichCliTestCase):
    """Tests for interpret-trailers command."""

    def test_parse_trailers_from_file(self):
        """Test parsing trailers from a file."""
        # Create a message file with trailers
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody\n\nSigned-off-by: Alice <alice@example.com>\n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers", "--only-trailers", msg_file
        )
        self.assertIsNone(result)
        self.assertIn("Signed-off-by: Alice <alice@example.com>", stdout)

    def test_add_trailer_to_message(self):
        """Test adding a trailer to a message."""
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody text\n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers",
            "--trailer",
            "Signed-off-by:Alice <alice@example.com>",
            msg_file,
        )
        self.assertIsNone(result)
        self.assertIn("Signed-off-by: Alice <alice@example.com>", stdout)
        self.assertIn("Subject", stdout)
        self.assertIn("Body text", stdout)

    def test_add_multiple_trailers(self):
        """Test adding multiple trailers."""
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody\n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers",
            "--trailer",
            "Signed-off-by:Alice",
            "--trailer",
            "Reviewed-by:Bob",
            msg_file,
        )
        self.assertIsNone(result)
        self.assertIn("Signed-off-by: Alice", stdout)
        self.assertIn("Reviewed-by: Bob", stdout)

    def test_parse_shorthand(self):
        """Test --parse shorthand option."""
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody\n\nSigned-off-by: Alice\n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers", "--parse", msg_file
        )
        self.assertIsNone(result)
        # --parse is shorthand for --only-trailers --only-input --unfold
        self.assertEqual(stdout, "Signed-off-by: Alice\n")

    def test_trim_empty(self):
        """Test --trim-empty option."""
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody\n\nSigned-off-by: Alice\nReviewed-by: \n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers", "--trim-empty", "--only-trailers", msg_file
        )
        self.assertIsNone(result)
        self.assertIn("Signed-off-by: Alice", stdout)
        self.assertNotIn("Reviewed-by:", stdout)

    def test_if_exists_replace(self):
        """Test --if-exists replace option."""
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody\n\nSigned-off-by: Alice\n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers",
            "--if-exists",
            "replace",
            "--trailer",
            "Signed-off-by:Bob",
            msg_file,
        )
        self.assertIsNone(result)
        self.assertIn("Signed-off-by: Bob", stdout)
        self.assertNotIn("Alice", stdout)

    def test_trailer_with_equals(self):
        """Test trailer with equals separator."""
        msg_file = os.path.join(self.test_dir, "message.txt")
        with open(msg_file, "wb") as f:
            f.write(b"Subject\n\nBody\n")

        result, stdout, _stderr = self._run_cli(
            "interpret-trailers", "--trailer", "Bug=12345", msg_file
        )
        self.assertIsNone(result)
        self.assertIn("Bug: 12345", stdout)


class ReplaceCommandTest(DulwichCliTestCase):
    """Tests for replace command."""

    def test_replace_create(self):
        """Test creating a replacement ref."""
        # Create two commits
        [c1, c2] = build_commit_graph(self.repo.object_store, [[1], [2]])
        self.repo[b"HEAD"] = c1.id

        # Create replacement using the create form (decode to string for CLI)
        c1_str = c1.id.decode("ascii")
        c2_str = c2.id.decode("ascii")
        _result, _stdout, _stderr = self._run_cli("replace", c1_str, c2_str)

        # Verify the replacement ref was created
        replace_ref = b"refs/replace/" + c1.id
        self.assertIn(replace_ref, self.repo.refs.keys())
        self.assertEqual(c2.id, self.repo.refs[replace_ref])

    def test_replace_list_empty(self):
        """Test listing replacements when there are none."""
        _result, stdout, _stderr = self._run_cli("replace", "list")
        self.assertEqual("", stdout)

    def test_replace_list(self):
        """Test listing replacement refs."""
        # Create two commits
        [c1, c2] = build_commit_graph(self.repo.object_store, [[1], [2]])
        self.repo[b"HEAD"] = c1.id

        # Create replacement
        c1_str = c1.id.decode("ascii")
        c2_str = c2.id.decode("ascii")
        self._run_cli("replace", c1_str, c2_str)

        # List replacements
        _result, stdout, _stderr = self._run_cli("replace", "list")
        self.assertIn(c1_str, stdout)
        self.assertIn(c2_str, stdout)

    def test_replace_default_list(self):
        """Test that replace without subcommand defaults to list."""
        # Create two commits
        [c1, c2] = build_commit_graph(self.repo.object_store, [[1], [2]])
        self.repo[b"HEAD"] = c1.id

        # Create replacement
        c1_str = c1.id.decode("ascii")
        c2_str = c2.id.decode("ascii")
        self._run_cli("replace", c1_str, c2_str)

        # Call replace without subcommand (should list)
        _result, stdout, _stderr = self._run_cli("replace")
        self.assertIn(c1_str, stdout)
        self.assertIn(c2_str, stdout)

    def test_replace_delete(self):
        """Test deleting a replacement ref."""
        # Create two commits
        [c1, c2] = build_commit_graph(self.repo.object_store, [[1], [2]])
        self.repo[b"HEAD"] = c1.id

        # Create replacement
        c1_str = c1.id.decode("ascii")
        c2_str = c2.id.decode("ascii")
        self._run_cli("replace", c1_str, c2_str)

        # Verify it exists
        replace_ref = b"refs/replace/" + c1.id
        self.assertIn(replace_ref, self.repo.refs.keys())

        # Delete the replacement
        _result, _stdout, _stderr = self._run_cli("replace", "delete", c1_str)

        # Verify it's gone
        self.assertNotIn(replace_ref, self.repo.refs.keys())

    def test_replace_delete_nonexistent(self):
        """Test deleting a nonexistent replacement ref fails."""
        # Create a commit
        [c1] = build_commit_graph(self.repo.object_store, [[1]])
        self.repo[b"HEAD"] = c1.id

        # Try to delete a non-existent replacement
        c1_str = c1.id.decode("ascii")
        result, _stdout, _stderr = self._run_cli("replace", "delete", c1_str)
        self.assertEqual(result, 1)


class StripspaceCommandTest(DulwichCliTestCase):
    """Tests for stripspace command."""

    def test_stripspace_from_file(self):
        """Test stripspace from a file."""
        # Create a text file with whitespace issues
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"  hello  \n\n\n\n  world  \n\n")

        result, stdout, _stderr = self._run_cli("stripspace", text_file)
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\n\nworld\n")

    def test_stripspace_simple(self):
        """Test basic stripspace functionality."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"hello\nworld\n")

        result, stdout, _stderr = self._run_cli("stripspace", text_file)
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\nworld\n")

    def test_stripspace_trailing_whitespace(self):
        """Test that trailing whitespace is removed."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"hello  \nworld\t\n")

        result, stdout, _stderr = self._run_cli("stripspace", text_file)
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\nworld\n")

    def test_stripspace_strip_comments(self):
        """Test stripping comments."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"# comment\nhello\n# another comment\nworld\n")

        result, stdout, _stderr = self._run_cli(
            "stripspace", "--strip-comments", text_file
        )
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\nworld\n")

    def test_stripspace_comment_lines(self):
        """Test prepending comment character."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"hello\nworld\n")

        result, stdout, _stderr = self._run_cli(
            "stripspace", "--comment-lines", text_file
        )
        self.assertIsNone(result)
        self.assertEqual(stdout, "# hello\n# world\n")

    def test_stripspace_custom_comment_char(self):
        """Test using custom comment character."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"; comment\nhello\n; another comment\nworld\n")

        result, stdout, _stderr = self._run_cli(
            "stripspace", "--strip-comments", "--comment-char", ";", text_file
        )
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\nworld\n")

    def test_stripspace_leading_trailing_blanks(self):
        """Test removing leading and trailing blank lines."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"\n\nhello\nworld\n\n\n")

        result, stdout, _stderr = self._run_cli("stripspace", text_file)
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\nworld\n")

    def test_stripspace_collapse_blank_lines(self):
        """Test collapsing multiple blank lines."""
        text_file = os.path.join(self.test_dir, "message.txt")
        with open(text_file, "wb") as f:
            f.write(b"hello\n\n\n\nworld\n")

        result, stdout, _stderr = self._run_cli("stripspace", text_file)
        self.assertIsNone(result)
        self.assertEqual(stdout, "hello\n\nworld\n")


class ColumnCommandTest(DulwichCliTestCase):
    """Tests for column command."""

    def test_column_mode_default(self):
        """Test column mode (default) - fills columns first."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n")
            result, stdout, _stderr = self._run_cli("column", "--width", "40")
            self.assertIsNone(result)
            # In column mode, items go down then across
            # With 12 items and width 40, should fit in multiple columns
            lines = stdout.strip().split("\n")
            # First line should start with "1"
            self.assertTrue(lines[0].startswith("1"))
        finally:
            sys.stdin = old_stdin

    def test_column_mode_row(self):
        """Test row mode - fills rows first."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("1\n2\n3\n4\n5\n6\n")
            result, stdout, _stderr = self._run_cli(
                "column", "--mode", "row", "--width", "40"
            )
            self.assertIsNone(result)
            # In row mode, items go across then down
            # Should have items 1, 2, 3... on first line
            lines = stdout.strip().split("\n")
            self.assertTrue("1" in lines[0])
            self.assertTrue("2" in lines[0])
        finally:
            sys.stdin = old_stdin

    def test_column_mode_plain(self):
        """Test plain mode - one item per line."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("apple\nbanana\ncherry\n")
            result, stdout, _stderr = self._run_cli("column", "--mode", "plain")
            self.assertIsNone(result)
            self.assertEqual(stdout, "apple\nbanana\ncherry\n")
        finally:
            sys.stdin = old_stdin

    def test_column_padding(self):
        """Test custom padding between columns."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("a\nb\nc\nd\ne\nf\n")
            result, stdout, _stderr = self._run_cli(
                "column", "--mode", "row", "--padding", "5", "--width", "80"
            )
            self.assertIsNone(result)
            # With padding=5, should have 5 spaces between items
            self.assertIn("     ", stdout)
        finally:
            sys.stdin = old_stdin

    def test_column_indent(self):
        """Test indent prepended to each line."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("apple\nbanana\n")
            result, stdout, _stderr = self._run_cli(
                "column", "--mode", "plain", "--indent", "  "
            )
            self.assertIsNone(result)
            lines = stdout.split("\n")
            self.assertTrue(lines[0].startswith("  apple"))
            self.assertTrue(lines[1].startswith("  banana"))
        finally:
            sys.stdin = old_stdin

    def test_column_empty_input(self):
        """Test with empty input."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("")
            result, stdout, _stderr = self._run_cli("column")
            self.assertIsNone(result)
            self.assertEqual(stdout, "")
        finally:
            sys.stdin = old_stdin

    def test_column_single_item(self):
        """Test with single item."""
        old_stdin = sys.stdin
        try:
            sys.stdin = io.StringIO("single\n")
            result, stdout, _stderr = self._run_cli("column")
            self.assertIsNone(result)
            self.assertEqual(stdout, "single\n")
        finally:
            sys.stdin = old_stdin


class MailinfoCommandTests(DulwichCliTestCase):
    """Tests for the mailinfo command."""

    def test_mailinfo_basic(self):
        """Test basic mailinfo command."""
        email_content = b"""From: Test User <test@example.com>
Subject: [PATCH] Add feature
Date: Mon, 1 Jan 2024 12:00:00 +0000

This is the commit message.

---
diff --git a/file.txt b/file.txt
"""
        email_file = os.path.join(self.test_dir, "email.txt")
        with open(email_file, "wb") as f:
            f.write(email_content)

        msg_file = os.path.join(self.test_dir, "msg")
        patch_file = os.path.join(self.test_dir, "patch")

        result, stdout, _stderr = self._run_cli(
            "mailinfo", msg_file, patch_file, email_file
        )
        self.assertIsNone(result)

        # Check stdout contains author info
        self.assertIn("Author: Test User", stdout)
        self.assertIn("Email: test@example.com", stdout)
        self.assertIn("Subject: Add feature", stdout)

        # Check files were written
        self.assertTrue(os.path.exists(msg_file))
        self.assertTrue(os.path.exists(patch_file))

        # Check file contents
        with open(msg_file) as f:
            msg_content = f.read()
            self.assertIn("This is the commit message.", msg_content)

        with open(patch_file) as f:
            patch_content = f.read()
            self.assertIn("diff --git", patch_content)

    def test_mailinfo_keep_subject(self):
        """Test mailinfo with -k flag."""
        email_content = b"""From: Test <test@example.com>
Subject: [PATCH 1/2] Feature

Body
"""
        email_file = os.path.join(self.test_dir, "email.txt")
        with open(email_file, "wb") as f:
            f.write(email_content)

        msg_file = os.path.join(self.test_dir, "msg")
        patch_file = os.path.join(self.test_dir, "patch")

        result, stdout, _stderr = self._run_cli(
            "mailinfo", "-k", msg_file, patch_file, email_file
        )
        self.assertIsNone(result)
        self.assertIn("Subject: [PATCH 1/2] Feature", stdout)

    def test_mailinfo_keep_non_patch(self):
        """Test mailinfo with -b flag."""
        email_content = b"""From: Test <test@example.com>
Subject: [RFC][PATCH] Feature

Body
"""
        email_file = os.path.join(self.test_dir, "email.txt")
        with open(email_file, "wb") as f:
            f.write(email_content)

        msg_file = os.path.join(self.test_dir, "msg")
        patch_file = os.path.join(self.test_dir, "patch")

        result, stdout, _stderr = self._run_cli(
            "mailinfo", "-b", msg_file, patch_file, email_file
        )
        self.assertIsNone(result)
        self.assertIn("Subject: [RFC] Feature", stdout)

    def test_mailinfo_scissors(self):
        """Test mailinfo with --scissors flag."""
        email_content = b"""From: Test <test@example.com>
Subject: Test

Ignore this part

-- >8 --

Keep this part
"""
        email_file = os.path.join(self.test_dir, "email.txt")
        with open(email_file, "wb") as f:
            f.write(email_content)

        msg_file = os.path.join(self.test_dir, "msg")
        patch_file = os.path.join(self.test_dir, "patch")

        result, _stdout, _stderr = self._run_cli(
            "mailinfo", "--scissors", msg_file, patch_file, email_file
        )
        self.assertIsNone(result)

        # Check message file
        with open(msg_file) as f:
            msg_content = f.read()
            self.assertIn("Keep this part", msg_content)
            self.assertNotIn("Ignore this part", msg_content)

    def test_mailinfo_message_id(self):
        """Test mailinfo with -m flag."""
        email_content = b"""From: Test <test@example.com>
Subject: Test
Message-ID: <test123@example.com>

Body
"""
        email_file = os.path.join(self.test_dir, "email.txt")
        with open(email_file, "wb") as f:
            f.write(email_content)

        msg_file = os.path.join(self.test_dir, "msg")
        patch_file = os.path.join(self.test_dir, "patch")

        result, _stdout, _stderr = self._run_cli(
            "mailinfo", "-m", msg_file, patch_file, email_file
        )
        self.assertIsNone(result)

        # Check message file contains Message-ID
        with open(msg_file) as f:
            msg_content = f.read()
            self.assertIn("Message-ID:", msg_content)

    def test_mailinfo_encoding(self):
        """Test mailinfo with --encoding flag."""
        email_content = (
            b"From: Test <test@example.com>\n"
            b"Subject: Test\n"
            b"Content-Type: text/plain; charset=utf-8\n"
            b"\n"
            b"Body with UTF-8: " + "naïve".encode() + b"\n"
        )
        email_file = os.path.join(self.test_dir, "email.txt")
        with open(email_file, "wb") as f:
            f.write(email_content)

        msg_file = os.path.join(self.test_dir, "msg")
        patch_file = os.path.join(self.test_dir, "patch")

        result, _stdout, _stderr = self._run_cli(
            "mailinfo", "--encoding", "utf-8", msg_file, patch_file, email_file
        )
        self.assertIsNone(result)

        # Just verify the command runs successfully
        with open(msg_file) as f:
            msg_content = f.read()
            self.assertIn("Body", msg_content)


class DiagnoseCommandTest(DulwichCliTestCase):
    """Tests for diagnose command."""

    def test_diagnose(self):
        """Test the diagnose command."""
        with self.assertLogs("dulwich.cli", level="INFO") as cm:
            result, _stdout, _stderr = self._run_cli("diagnose")
            self.assertIsNone(result)

            # Check that key information is present in log output
            log_output = "\n".join(cm.output)
            self.assertIn("Python version:", log_output)
            self.assertIn("Python executable:", log_output)
            self.assertIn("PYTHONPATH:", log_output)
            self.assertIn("sys.path:", log_output)
            self.assertIn("Dulwich version:", log_output)
            self.assertIn("Installed dependencies:", log_output)

            # Check that at least core dependencies are listed
            self.assertIn("urllib3:", log_output)


if __name__ == "__main__":
    unittest.main()