sley-diff-merge 0.4.3

Native-Rust Git diff and three-way merge engine for the sley engine, including tree diffing and the textual renderer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
//! Three-way tree merge engine.

use sley_core::{BString, GitError, ObjectFormat, ObjectId, Result};
use sley_index::{is_gitlink, is_symlink_mode};
use sley_object::{Commit, EncodedObject, ObjectType, Tree, TreeEntries, TreeEntry};
use sley_odb::{FileObjectDatabase, ObjectReader, ObjectWriter};
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use std::sync::Arc;

use crate::blob_merge::{
    merge_blobs, ConflictStyle, MergeBlobOptions, MergeBlobResult, MergeFavor,
};
use crate::line_diff::WsIgnore;
use crate::name_status::{
    diff_name_status_maps_with_renames, flatten_tree, is_type_change, DiffNameStatusOptions,
    MergeEntryMap, NameStatus, NameStatusEntry, TrackedEntry, DEFAULT_RENAME_THRESHOLD,
};

// ===========================================================================
// Library tree-merge seam (`merge_trees`).
//
// This is the single 3-way tree-merge engine that every merge porcelain calls.
// Before it existed the logic was duplicated across the CLI: `merge-tree
// --write-tree` had its own copy and `git merge` / `cherry-pick` / `revert`
// had a second copy. Both copies implemented the identical per-path diff3
// resolution; the only differences were *rendering* (write-tree emits a tree +
// stage list + messages; the porcelains stage an index + materialize a
// worktree). This seam computes the merge once and returns a per-path result
// rich enough for both renderings, so the resolution lives in exactly one
// place.
//
// The result is byte-identical to the old per-command copies on every cell
// they already handled (clean merges, content / add-add / modify-delete
// conflicts, mode merges). On top of that it adds rename-aware resolution: a
// file renamed on one side and modified on the other follows the rename,
// gated by [`MergeTreesOptions::detect_renames`] (the classic merge-ort
// non-recursive rename case).
// ===========================================================================

/// Options controlling a [`merge_trees`] run.
pub struct MergeTreesOptions<'a> {
    /// Conflict-marker label for ours (e.g. a branch name or `HEAD`).
    pub ours_label: &'a str,
    /// Conflict-marker label for theirs.
    pub theirs_label: &'a str,
    /// Diff3 ancestor label (the `|||||||` side); merge porcelains use
    /// `"merged common ancestors"`.
    pub ancestor_label: &'a str,
    /// `-Xours` / `-Xtheirs` favouring for textual conflicts.
    pub favor: MergeFavor,
    /// Optional per-path favor, used only when [`Self::favor`] is
    /// [`MergeFavor::None`]. Merge porcelains use this for attributes such as
    /// `merge=union` without changing the command-line `-X` override.
    pub path_favor: Option<&'a dyn Fn(&[u8]) -> MergeFavor>,
    /// Optional per-path conflict marker length resolver for attributes such as
    /// `conflict-marker-size`.
    pub path_marker_size: Option<&'a dyn Fn(&[u8]) -> usize>,
    /// Enable rename-aware merging: a file renamed on one side and modified on
    /// the other follows the rename. When `false`, the merge is purely
    /// path-keyed (the historical behaviour).
    pub detect_renames: bool,
    /// Minimum similarity (`0..=100`) for inexact rename detection.
    pub rename_threshold: u8,
    /// Cap on the inexact rename matrix (`merge.renameLimit`/`diff.renameLimit`).
    /// `0` means unlimited; otherwise inexact detection is skipped when the
    /// candidate source × destination count exceeds `rename_limit²`.
    pub rename_limit: usize,
    /// Directory-rename detection mode. When [`DirectoryRenames::False`], a file
    /// added on one side under a directory that the *other* side renamed stays
    /// put. When enabled, such files are re-homed into the renamed directory,
    /// matching `merge.directoryRenames`. Requires `detect_renames` to have any
    /// effect (directory renames are inferred from the file renames it finds).
    pub directory_renames: DirectoryRenames,
    /// Conflict-marker style for textual conflicts (`merge.conflictStyle`).
    pub style: ConflictStyle,
    /// Whitespace-insensitivity for textual 3-way merges, mirroring
    /// `-Xignore-space-change`/`-Xignore-all-space`/`-Xignore-space-at-eol`.
    pub ws_ignore: WsIgnore,
}

/// How directory-rename detection behaves, mirroring git's
/// `merge.directoryRenames` configuration.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum DirectoryRenames {
    /// Disable directory-rename detection (`merge.directoryRenames=false`).
    #[default]
    False,
    /// Apply directory renames silently (`merge.directoryRenames=true`).
    True,
    /// Detect directory renames but treat each re-homed path as a conflict
    /// requiring confirmation (`merge.directoryRenames=conflict`). git's default.
    Conflict,
}

impl Default for MergeTreesOptions<'_> {
    fn default() -> Self {
        Self {
            ours_label: "ours",
            theirs_label: "theirs",
            ancestor_label: "merged common ancestors",
            favor: MergeFavor::None,
            path_favor: None,
            path_marker_size: None,
            detect_renames: false,
            rename_threshold: DEFAULT_RENAME_THRESHOLD,
            rename_limit: 0,
            directory_renames: DirectoryRenames::False,
            style: ConflictStyle::Merge,
            ws_ignore: WsIgnore::EMPTY,
        }
    }
}

fn merge_favor_for_path(options: &MergeTreesOptions<'_>, path: &[u8]) -> MergeFavor {
    if options.favor != MergeFavor::None {
        return options.favor;
    }
    options
        .path_favor
        .map(|resolver| resolver(path))
        .unwrap_or(MergeFavor::None)
}

fn merge_marker_size_for_path(options: &MergeTreesOptions<'_>, path: &[u8]) -> usize {
    options
        .path_marker_size
        .map(|resolver| resolver(path))
        .unwrap_or(7)
}

/// The kind of conflict recorded for a path, used to render the stable
/// conflict-type token and human message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MergeConflictKind {
    /// Both sides changed the file content differently (or both added it with
    /// differing content — an add/add).
    Content { add_add: bool },
    /// The file was deleted on one side and modified on the other.
    ModifyDelete {
        /// The side label that deleted the path.
        deleted_in: String,
        /// The side label that modified (and thus kept) the path.
        modified_in: String,
    },
    /// A file renamed on one side, with a content conflict against the other
    /// side's change at the destination.
    RenameContent {
        /// The original (pre-rename) path.
        old_path: Vec<u8>,
    },
    /// Two paths were renamed to the same destination, producing a
    /// rename/rename(2to1) conflict.
    RenameRenameTwoToOne {
        /// Ours' pre-destination path.
        ours_path: Vec<u8>,
        /// Theirs' pre-destination path.
        theirs_path: Vec<u8>,
    },
    /// One source path was renamed to different destinations on each side,
    /// producing a rename/rename(1to2) conflict.
    RenameRenameOneToTwo {
        /// The pre-rename source path.
        old_path: Vec<u8>,
        /// Ours' destination path.
        ours_path: Vec<u8>,
        /// Theirs' destination path.
        theirs_path: Vec<u8>,
        /// The label for our side.
        ours_label: String,
        /// The label for their side.
        theirs_label: String,
    },
    /// An auxiliary higher-stage entry for a rename/rename(1to2) conflict. The
    /// user-facing message is emitted by [`RenameRenameOneToTwo`].
    RenameRenameOneToTwoStage,
    /// A directory was split evenly across multiple destinations, so no
    /// directory rename could be applied for paths the other side left there.
    DirRenameSplit {
        /// The original directory with no unique destination.
        source_dir: Vec<u8>,
    },
    /// A file renamed on one side whose source was deleted on the other side.
    RenameDelete {
        /// The pre-rename source path.
        old_path: Vec<u8>,
        /// The side label that performed the rename.
        renamed_in: String,
        /// The side label that deleted the source.
        deleted_in: String,
    },
    /// A file collides with a directory at the same path in the merged result:
    /// the directory wins at the original path and the file is moved aside to
    /// `path~<branch>` (merge-ort's D/F conflict, `unique_path`). git emits
    /// `CONFLICT (file/directory): directory in the way of <old> from <branch>;
    /// moving it to <new> instead.`
    FileDirectory {
        /// The original (pre-move) path now occupied by the directory.
        original_path: Vec<u8>,
        /// The side label whose file was moved aside.
        moved_from: String,
    },
    /// A path was added/renamed under a directory the other side renamed, so the
    /// merge silently moved it into the renamed directory but, in
    /// `merge.directoryRenames=conflict` mode, flags it for the user to confirm.
    /// git emits `CONFLICT (file location): ... suggesting it should perhaps be
    /// moved to <new_path>.` The tree still contains the re-homed content.
    DirRenameLocation {
        /// The pre-re-home path (`old_path` in git's message): where the side
        /// placed the file before directory-rename detection moved it.
        old_path: Vec<u8>,
        /// `Some(source)` when the file was *renamed* into `old_path` by this
        /// side (git's "renamed to" wording, naming the original `source`);
        /// `None` when it was a fresh add (git's "added in" wording).
        renamed_from: Option<Vec<u8>>,
        /// The side label that added/renamed the file (`branch_with_new_path`).
        added_in: String,
        /// The side label that renamed the directory (`branch_with_dir_rename`).
        dir_renamed_in: String,
        /// True when the directory rename moved the file back onto its own base
        /// source path (rename-to-self) and the other side modified that path. The
        /// `CONFLICT (file location)` message is the same, but git records the
        /// path UNMERGED (stages 1/2/3) instead of staging the re-homed content
        /// cleanly: the index writers stage these 1/2/3, not at stage 0.
        back_to_self: bool,
    },
    /// A directory rename would have moved one or more paths onto this path, but
    /// it is already occupied (a file/dir in the way) or several sources map
    /// here. git emits `CONFLICT (implicit dir rename): Existing file/dir at
    /// <path> in the way of implicit directory rename(s) putting the following
    /// path(s) there: <sources>.` The path keeps its original content; the
    /// re-homed sources are left where they were.
    DirRenameImplicitCollision {
        /// The source path(s) the directory rename tried to move onto this path.
        sources: Vec<Vec<u8>>,
    },
    /// The two sides hold different object types at one path (regular↔symlink,
    /// regular↔gitlink, symlink↔gitlink). git's `process_entry` (merge-ort.c
    /// ~4220) renames each *regular-file* side to `path~<branch>` so each type
    /// can be recorded somewhere, ignoring `-Xours`/`-Xtheirs`, and emits a
    /// single `CONFLICT (distinct types)` line. (gitlink↔gitlink and
    /// symlink↔symlink share an `S_IFMT` and never reach this arm.) This kind is
    /// attached to the leaf that carries the message — the side left at
    /// `original_path` when only one side moved, else ours; the other renamed
    /// leaf carries [`DistinctTypesStage`].
    DistinctTypes {
        /// The original colliding path (git's message subject and sort key).
        original_path: Vec<u8>,
        /// `Some(p)` when ours was renamed aside to `p`; `None` when ours stayed
        /// at `original_path`.
        ours_renamed: Option<Vec<u8>>,
        /// `Some(p)` when theirs was renamed aside to `p`; `None` when theirs
        /// stayed at `original_path`.
        theirs_renamed: Option<Vec<u8>>,
    },
    /// The non-message-carrying leaf of a [`DistinctTypes`] conflict. The
    /// user-facing line is emitted once by the [`DistinctTypes`] leaf.
    DistinctTypesStage,
}

/// One resolved/conflicted path in the merged tree.
#[derive(Debug, Clone)]
pub struct MergedPath {
    /// Destination path in the merged tree.
    pub path: Vec<u8>,
    /// The per-stage (1=base, 2=ours, 3=theirs) entries when conflicted; all
    /// `None` for a clean resolution.
    pub stages: MergeStages,
    /// `Some((mode, oid))` is the final leaf written to the merged tree; `None`
    /// means the path is absent in the result (a clean delete).
    pub result: Option<(u32, ObjectId)>,
    /// When conflicted, the worktree bytes + mode to materialize (content with
    /// conflict markers, or the surviving side's bytes). `None` for a clean
    /// path.
    pub worktree: Option<(u32, Vec<u8>)>,
    /// `Some(..)` exactly when this path conflicted.
    pub conflict: Option<MergeConflictKind>,
    /// True when this path went through a textual 3-way content merge (both
    /// sides diverged and both were mergeable files). Drives the "Auto-merging
    /// <path>" informational message, which `git merge-tree` emits for every
    /// such path — clean or conflicted.
    pub auto_merged: bool,
}

impl MergedPath {
    /// True when this path resolved cleanly (no conflict recorded).
    pub fn is_clean(&self) -> bool {
        self.conflict.is_none()
    }
}

/// Per-stage higher-order index entries for a conflicted path.
#[derive(Debug, Clone, Default)]
pub struct MergeStages {
    pub base: Option<(u32, ObjectId)>,
    pub ours: Option<(u32, ObjectId)>,
    pub theirs: Option<(u32, ObjectId)>,
}

/// The outcome of a 3-way tree merge: the merged top-level tree plus per-path
/// detail and a clean/conflicted flag.
#[derive(Debug, Clone)]
pub struct MergeTreesResult {
    /// Object id of the merged top-level tree (always written, even on
    /// conflict — conflicted blobs go in with their marker content).
    pub tree: ObjectId,
    /// Per-path results, sorted by path.
    pub paths: Vec<MergedPath>,
    /// False if any path conflicted.
    pub clean: bool,
    /// Original paths removed by rename or directory-rename rewrites. These are
    /// cleanup-only paths for porcelains materializing a conflicted merge; they
    /// are absent from the merged tree.
    pub cleanup_paths: Vec<Vec<u8>>,
    /// Non-conflict informational messages produced while detecting renames.
    pub info_messages: Vec<MergeInfoMessage>,
}

impl MergeTreesResult {
    /// Iterate over the paths that conflicted, in path order.
    pub fn conflicts(&self) -> impl Iterator<Item = &MergedPath> {
        self.paths.iter().filter(|entry| entry.conflict.is_some())
    }
}

/// Non-conflict merge information that porcelain commands may print.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MergeInfoMessage {
    /// A directory rename was skipped because the suggested target directory was
    /// itself renamed away on this side.
    DirRenameSkippedDueToRerename {
        old_dir: Vec<u8>,
        path: Vec<u8>,
        new_dir: Vec<u8>,
    },
    /// A path was updated due to a directory rename in
    /// `merge.directoryRenames=true` mode.
    DirRenameApplied {
        old_path: Vec<u8>,
        new_path: Vec<u8>,
        renamed_from: Option<Vec<u8>>,
        added_in: String,
        dir_renamed_in: String,
    },
    /// A directory-rename location conflict that overlaps another conflict at
    /// the same final path, such as a content conflict. The path's primary
    /// conflict kind remains attached to the path; this carries git's extra
    /// `CONFLICT (file location)` line.
    DirRenameLocationConflict {
        old_path: Vec<u8>,
        new_path: Vec<u8>,
        renamed_from: Option<Vec<u8>>,
        added_in: String,
        dir_renamed_in: String,
    },
    /// A rename/delete conflict whose conflicted destination was later moved
    /// aside by directory/file conflict handling. The primary per-path conflict
    /// remains `FileDirectory`; this preserves git's extra rename/delete line.
    RenameDeleteConflict {
        old_path: Vec<u8>,
        new_path: Vec<u8>,
        renamed_in: String,
        deleted_in: String,
    },
}


/// True for a plain file blob (regular or executable) — i.e. a mode whose
/// content can be textually 3-way merged. Symlinks and gitlinks are excluded.
pub fn is_mergeable_file_mode(mode: u32) -> bool {
    mode == 0o100644 || mode == 0o100755
}

/// 3-way merge of three trees into a single merged tree.
///
/// `base` is the common-ancestor tree (`None` for unrelated histories — every
/// path is then treated as added on both sides). `ours`/`theirs` are the two
/// sides. Cleanly-merged blob content and the resulting (sub)trees are written
/// to `db`; the returned [`MergeTreesResult`] carries the merged top-level tree
/// oid plus per-path detail.
///
/// This is the shared engine behind `git merge-tree --write-tree`, `git merge`,
/// `git cherry-pick`, and `git revert`. It is behaviour-preserving relative to
/// the per-command copies it replaced, and additionally resolves renames when
/// [`MergeTreesOptions::detect_renames`] is set.
pub fn merge_trees(
    db: &FileObjectDatabase,
    format: ObjectFormat,
    base: Option<&ObjectId>,
    ours: &ObjectId,
    theirs: &ObjectId,
    options: &MergeTreesOptions<'_>,
) -> Result<MergeTreesResult> {
    let base_map = match base {
        Some(tree) => flatten_tree(db, format, tree)?,
        None => MergeEntryMap::new(),
    };
    let ours_map = flatten_tree(db, format, ours)?;
    let theirs_map = flatten_tree(db, format, theirs)?;
    merge_entry_maps(db, format, &base_map, &ours_map, &theirs_map, options)
}

/// [`merge_trees`] operating on already-flattened entry maps. The merge
/// porcelains often hold the flattened maps already (e.g. cherry-pick builds
/// `theirs` from a picked commit's tree), so this avoids re-reading them.
pub fn merge_entry_maps(
    db: &FileObjectDatabase,
    format: ObjectFormat,
    base_map: &MergeEntryMap,
    ours_map: &MergeEntryMap,
    theirs_map: &MergeEntryMap,
    options: &MergeTreesOptions<'_>,
) -> Result<MergeTreesResult> {
    // Rename-aware step: detect files renamed on exactly one side relative to
    // base, so a modification on the other side follows the rename. This is the
    // non-recursive merge-ort rename case. We compute a rewrite map that, for a
    // one-sided rename old->new, presents the *other* side's `old` content at
    // `new` (and drops `old`), letting the path-keyed core below do the 3-way
    // content merge at the destination.
    let (mut renames, side_renames) = if options.detect_renames {
        let (renames, ours_side, theirs_side) =
            detect_merge_renames(db, format, base_map, ours_map, theirs_map, options)?;
        (renames, Some((ours_side, theirs_side)))
    } else {
        (MergeRenames::default(), None)
    };

    // Build the effective per-side maps with file renames applied.
    let (mut eff_base, mut eff_ours, mut eff_theirs) =
        apply_merge_renames(base_map, ours_map, theirs_map, &renames);

    // Directory-rename detection: when one side renamed a whole directory and
    // the other side added a file under (or renamed a file into) the old
    // directory, re-home that path into the renamed directory — including
    // transitive renames (a file the other side renamed into a directory this
    // side renamed follows on into the final directory). This is the
    // merge.directoryRenames behaviour, applied as a rewrite of the rename/add
    // destination paths so every merged path consults directory renames.
    let mut dir_rename_dirty = false;
    let mut rehomed_paths: BTreeMap<Vec<u8>, RehomeSides> = BTreeMap::new();
    let mut dir_rename_two_to_one: Vec<DirRenameTwoToOne> = Vec::new();
    let mut dir_rename_collisions: Vec<DirRenameCollision> = Vec::new();
    let mut dir_rename_splits: BTreeSet<Vec<u8>> = BTreeSet::new();
    let mut dir_rename_back_to_self: BTreeSet<Vec<u8>> = BTreeSet::new();
    let mut info_messages = Vec::new();
    let mut cleanup_paths: BTreeSet<Vec<u8>> = renames
        .dest_to_source
        .values()
        .map(|rename| rename.source.clone())
        .collect();
    if options.directory_renames != DirectoryRenames::False
        && let Some((ours_side, theirs_side)) = &side_renames
    {
        let dir_renames = compute_directory_renames(ours_map, theirs_map, ours_side, theirs_side);
        let outcome = apply_directory_renames(
            base_map,
            &eff_base,
            &eff_ours,
            &eff_theirs,
            ours_side,
            theirs_side,
            &dir_renames,
            &renames.dest_to_source,
        );
        eff_base = outcome.base;
        eff_ours = outcome.ours;
        eff_theirs = outcome.theirs;
        rehomed_paths = outcome.rehomed;
        dir_rename_collisions = outcome.collisions;
        dir_rename_splits = outcome.splits;
        dir_rename_back_to_self = outcome.back_to_self;
        info_messages = outcome.info_messages;
        dir_rename_dirty = outcome.dirty;
        remap_rename_destinations(&mut renames, &rehomed_paths);
        drop_collapsed_rename_rename_conflicts(&mut renames);
        dir_rename_two_to_one = collect_dir_rename_two_to_one(&renames, &rehomed_paths);
    }
    for info in rehomed_paths
        .values()
        .flat_map(|sides| [&sides.ours, &sides.theirs])
        .flatten()
    {
        cleanup_paths.insert(info.old_path.clone());
    }
    if options.directory_renames == DirectoryRenames::True {
        for (dest, sides) in &rehomed_paths {
            for info in [&sides.ours, &sides.theirs].into_iter().flatten() {
                let (added_in, dir_renamed_in) = if info.added_on_ours {
                    (
                        options.ours_label.to_string(),
                        options.theirs_label.to_string(),
                    )
                } else {
                    (
                        options.theirs_label.to_string(),
                        options.ours_label.to_string(),
                    )
                };
                info_messages.push(MergeInfoMessage::DirRenameApplied {
                    old_path: info.old_path.clone(),
                    new_path: dest.clone(),
                    renamed_from: info.renamed_from.clone(),
                    added_in,
                    dir_renamed_in,
                });
            }
        }
    }
    // In =conflict mode, every re-homed path is reported as a location conflict
    // (the tree still gets the re-homed content, but the merge is marked dirty).
    let dir_rename_conflict_paths: BTreeMap<Vec<u8>, RehomeSides> =
        if options.directory_renames == DirectoryRenames::Conflict {
            rehomed_paths.clone()
        } else {
            BTreeMap::new()
        };

    let mut all_paths = BTreeSet::new();
    all_paths.extend(eff_base.keys().cloned());
    all_paths.extend(eff_ours.keys().cloned());
    all_paths.extend(eff_theirs.keys().cloned());

    let mut paths: Vec<MergedPath> = Vec::new();
    let mut leaves: MergeEntryMap = BTreeMap::new();
    let mut clean = true;

    for path in all_paths {
        let base = eff_base.get(&path).cloned();
        let ours = eff_ours.get(&path).cloned();
        let theirs = eff_theirs.get(&path).cloned();
        let rename = renames.dest_to_source.get(&path);
        let old_path = rename.map(|r| r.source.clone());
        let favor = merge_favor_for_path(options, &path);

        // Trivial resolutions (identical to the historical per-command logic).
        if ours == theirs {
            if let Some(entry) = ours {
                leaves.insert(path.clone(), entry);
            }
            paths.push(clean_path(path, ours));
            continue;
        }
        if ours == base {
            if let Some(entry) = &theirs {
                leaves.insert(path.clone(), *entry);
            }
            paths.push(clean_path(path, theirs));
            continue;
        }
        if theirs == base {
            if let Some(entry) = &ours {
                leaves.insert(path.clone(), *entry);
            }
            paths.push(clean_path(path, ours));
            continue;
        }

        // Both sides diverged. Decide how to combine.
        let content_mergeable = matches!(&ours, Some((mode, _)) if is_mergeable_file_mode(*mode))
            && matches!(&theirs, Some((mode, _)) if is_mergeable_file_mode(*mode))
            && match &base {
                Some((mode, _)) => is_mergeable_file_mode(*mode),
                None => true,
            };

        if let (true, Some((ours_mode, ours_oid)), Some((theirs_mode, theirs_oid))) =
            (content_mergeable, &ours, &theirs)
        {
            let add_add = base.is_none();
            let base_bytes = match &base {
                Some((_, oid)) => merge_blob_bytes(db, oid)?,
                None => Vec::new(),
            };
            let ours_bytes = merge_blob_bytes(db, ours_oid)?;
            let theirs_bytes = merge_blob_bytes(db, theirs_oid)?;
            // When this destination came from a one-sided rename, git qualifies
            // the conflict-marker labels with the per-side path (the renaming
            // side shows the new path, the other side the old path), e.g.
            // `<<<<<<< HEAD:old.txt` / `>>>>>>> feature:new.txt`.
            let rehome = rehomed_paths.get(&path);
            // git's `merge_3way` qualifies all three labels with their per-side
            // path (`<name>:<path>`) whenever the three paths are not identical —
            // pathnames[0] is the base/ancestor path (the rename source). When
            // they are identical (no rename), it uses the bare names.
            let (base_label_owned, ours_label, theirs_label) = match rename {
                Some(MergeRename { source, side }) => {
                    let (ours_path, theirs_path) = match side {
                        // theirs renamed -> ours kept the source path.
                        RenameSide::Theirs => (source.as_slice(), path.as_slice()),
                        // ours renamed -> theirs kept the source path.
                        RenameSide::Ours => (path.as_slice(), source.as_slice()),
                    };
                    (
                        qualify_label(options.ancestor_label, source.as_slice()),
                        qualify_label(options.ours_label, ours_path),
                        qualify_label(options.theirs_label, theirs_path),
                    )
                }
                None => {
                    let ours_path = rehome
                        .and_then(|info| info.ours.as_ref())
                        .map_or(path.as_slice(), |info| info.old_path.as_slice());
                    let theirs_path = rehome
                        .and_then(|info| info.theirs.as_ref())
                        .map_or(path.as_slice(), |info| info.old_path.as_slice());
                    if ours_path != path.as_slice() || theirs_path != path.as_slice() {
                        (
                            qualify_label(options.ancestor_label, path.as_slice()),
                            qualify_label(options.ours_label, ours_path),
                            qualify_label(options.theirs_label, theirs_path),
                        )
                    } else {
                        (
                            options.ancestor_label.to_string(),
                            options.ours_label.to_string(),
                            options.theirs_label.to_string(),
                        )
                    }
                }
            };
            let result = merge_blobs(
                &base_bytes,
                &ours_bytes,
                &theirs_bytes,
                &MergeBlobOptions {
                    ours_label: &ours_label,
                    theirs_label: &theirs_label,
                    base_label: &base_label_owned,
                    style: options.style,
                    favor,
                    ws_ignore: options.ws_ignore,
                    marker_size: merge_marker_size_for_path(options, &path),
                },
            );

            let base_mode = base.as_ref().map(|(mode, _)| *mode);
            let (resolved_mode, mode_conflict) =
                merge_file_modes(base_mode, *ours_mode, *theirs_mode);

            if !result.conflicted && !mode_conflict {
                let oid = db.write_object(EncodedObject::new(ObjectType::Blob, result.content))?;
                leaves.insert(path.clone(), (resolved_mode, oid));
                paths.push(clean_path_auto(path, Some((resolved_mode, oid)), true));
            } else if favor != MergeFavor::None && !mode_conflict {
                let chosen = if favor == MergeFavor::Ours {
                    ours
                } else {
                    theirs
                };
                if let Some(entry) = chosen {
                    leaves.insert(path.clone(), entry);
                }
                paths.push(clean_path_auto(path, chosen, true));
            } else {
                clean = false;
                let oid =
                    db.write_object(EncodedObject::new(ObjectType::Blob, result.content.clone()))?;
                leaves.insert(path.clone(), (resolved_mode, oid));
                let worktree_mode = if *ours_mode == *theirs_mode {
                    *ours_mode
                } else {
                    0o100644
                };
                let conflict = if let Some(old) = &old_path {
                    MergeConflictKind::RenameContent {
                        old_path: old.clone(),
                    }
                } else if add_add {
                    match rehome.and_then(|info| Some((info.ours.as_ref()?, info.theirs.as_ref()?)))
                    {
                        Some((ours_info, theirs_info)) => MergeConflictKind::RenameRenameTwoToOne {
                            ours_path: ours_info.old_path.clone(),
                            theirs_path: theirs_info.old_path.clone(),
                        },
                        None => MergeConflictKind::Content { add_add },
                    }
                } else {
                    MergeConflictKind::Content { add_add }
                };
                paths.push(MergedPath {
                    path: path.clone(),
                    stages: stages_for(&base, &ours, &theirs),
                    result: Some((resolved_mode, oid)),
                    worktree: Some((worktree_mode, result.content)),
                    conflict: Some(conflict),
                    auto_merged: true,
                });
            }
        } else if base.is_some() && (ours.is_none() || theirs.is_none()) {
            // modify/delete.
            clean = false;
            let (deleted_in, modified_in, surviving) = if ours.is_none() {
                (
                    options.ours_label.to_string(),
                    options.theirs_label.to_string(),
                    theirs,
                )
            } else {
                (
                    options.theirs_label.to_string(),
                    options.ours_label.to_string(),
                    ours,
                )
            };
            let worktree = match &surviving {
                Some((mode, oid)) => Some((*mode, merge_worktree_bytes(db, *mode, oid)?)),
                None => None,
            };
            if let Some(entry) = surviving {
                leaves.insert(path.clone(), entry);
            }
            paths.push(MergedPath {
                path: path.clone(),
                stages: stages_for(&base, &ours, &theirs),
                result: surviving,
                worktree,
                conflict: Some(MergeConflictKind::ModifyDelete {
                    deleted_in,
                    modified_in,
                }),
                auto_merged: false,
            });
        } else if let (Some(&(ours_mode, ours_oid)), Some(&(theirs_mode, theirs_oid))) =
            (ours.as_ref(), theirs.as_ref())
            && sley_index::is_symlink_mode(ours_mode)
            && sley_index::is_symlink_mode(theirs_mode)
        {
            // Both sides are symlinks that diverged from the base and from each
            // other (the trivial oid resolutions above already took the agreeing
            // cases). A symlink is never textually merged; git's
            // `handle_content_merge` symlink arm (merge-ort.c) resolves CLEAN to
            // a side under `-Xours`/`-Xtheirs`, and otherwise records a CONFLICT
            // carrying ours' target.
            match favor {
                MergeFavor::Ours => {
                    leaves.insert(path.clone(), (ours_mode, ours_oid));
                    paths.push(clean_path_auto(
                        path.clone(),
                        Some((ours_mode, ours_oid)),
                        false,
                    ));
                }
                MergeFavor::Theirs => {
                    leaves.insert(path.clone(), (theirs_mode, theirs_oid));
                    paths.push(clean_path_auto(
                        path.clone(),
                        Some((theirs_mode, theirs_oid)),
                        false,
                    ));
                }
                MergeFavor::None | MergeFavor::Union => {
                    clean = false;
                    leaves.insert(path.clone(), (ours_mode, ours_oid));
                    let worktree =
                        Some((ours_mode, merge_worktree_bytes(db, ours_mode, &ours_oid)?));
                    paths.push(MergedPath {
                        path: path.clone(),
                        stages: stages_for(&base, &ours, &theirs),
                        result: Some((ours_mode, ours_oid)),
                        worktree,
                        conflict: Some(MergeConflictKind::Content {
                            add_add: base.is_none(),
                        }),
                        auto_merged: false,
                    });
                }
            }
        } else if let (Some((ours_mode, ours_oid)), Some((theirs_mode, theirs_oid))) =
            (ours, theirs)
            && is_type_change(ours_mode, theirs_mode)
        {
            // Distinct types at one path: both sides present with different
            // `S_IFMT` (regular↔symlink, regular↔gitlink, symlink↔gitlink).
            // Mirror merge-ort's `process_entry`: rename each regular-file side
            // to `path~<branch>` so each type is recorded somewhere; ignore
            // `-Xours`/`-Xtheirs`. gitlink↔gitlink and symlink↔symlink share an
            // `S_IFMT` and are handled by the arms above.
            clean = false;
            // git renames the regular-file side(s): only the regular side when
            // exactly one is regular, both when neither is (symlink↔gitlink).
            let (rename_ours, rename_theirs) = if is_mergeable_file_mode(ours_mode) {
                (true, false)
            } else if is_mergeable_file_mode(theirs_mode) {
                (false, true)
            } else {
                (true, true)
            };
            // git keeps the base stage (index stage 1) for a side only when that
            // side shares the base's file type.
            let ours_base = base.filter(|(mode, _)| !is_type_change(*mode, ours_mode));
            let theirs_base = base.filter(|(mode, _)| !is_type_change(*mode, theirs_mode));
            // Name and reserve ours' aside-path first so the two renamed paths
            // can never collide (`unique_df_path` consults `leaves`/`paths`).
            let ours_path = if rename_ours {
                unique_df_path(&path, options.ours_label, &leaves, &paths)
            } else {
                path.clone()
            };
            leaves.insert(ours_path.clone(), (ours_mode, ours_oid));
            let theirs_path = if rename_theirs {
                unique_df_path(&path, options.theirs_label, &leaves, &paths)
            } else {
                path.clone()
            };
            leaves.insert(theirs_path.clone(), (theirs_mode, theirs_oid));

            // The message is emitted once, by the leaf left at `original_path`
            // when only one side moved (matching git's keying), else by ours.
            let ours_carries_message = !rename_ours || rename_theirs;
            let distinct = MergeConflictKind::DistinctTypes {
                original_path: path.clone(),
                ours_renamed: rename_ours.then(|| ours_path.clone()),
                theirs_renamed: rename_theirs.then(|| theirs_path.clone()),
            };
            let ours_worktree = Some((ours_mode, merge_worktree_bytes(db, ours_mode, &ours_oid)?));
            paths.push(MergedPath {
                path: ours_path,
                stages: MergeStages {
                    base: ours_base,
                    ours: Some((ours_mode, ours_oid)),
                    theirs: None,
                },
                result: Some((ours_mode, ours_oid)),
                worktree: ours_worktree,
                conflict: Some(if ours_carries_message {
                    distinct.clone()
                } else {
                    MergeConflictKind::DistinctTypesStage
                }),
                auto_merged: false,
            });
            let theirs_worktree = Some((
                theirs_mode,
                merge_worktree_bytes(db, theirs_mode, &theirs_oid)?,
            ));
            paths.push(MergedPath {
                path: theirs_path,
                stages: MergeStages {
                    base: theirs_base,
                    ours: None,
                    theirs: Some((theirs_mode, theirs_oid)),
                },
                result: Some((theirs_mode, theirs_oid)),
                worktree: theirs_worktree,
                conflict: Some(if ours_carries_message {
                    MergeConflictKind::DistinctTypesStage
                } else {
                    distinct
                }),
                auto_merged: false,
            });
        } else {
            // add/add of non-files, mode changes on same-type entries, etc. Keep
            // the surviving side's content and record a generic content conflict.
            clean = false;
            let add_add = base.is_none();
            let surviving = ours.or(theirs);
            let worktree = match &surviving {
                Some((mode, oid)) => Some((*mode, merge_worktree_bytes(db, *mode, oid)?)),
                None => None,
            };
            if let Some(entry) = surviving {
                leaves.insert(path.clone(), entry);
            }
            paths.push(MergedPath {
                path: path.clone(),
                stages: stages_for(&base, &ours, &theirs),
                result: surviving,
                worktree,
                conflict: Some(MergeConflictKind::Content { add_add }),
                auto_merged: false,
            });
        }
    }

    if !renames.rename_rename_one_to_two.is_empty() {
        apply_rename_rename_one_to_two_conflicts(
            db,
            base_map,
            &eff_ours,
            &eff_theirs,
            &renames.rename_rename_one_to_two,
            &mut paths,
            &mut leaves,
            options,
        )?;
        clean = false;
    }

    if !dir_rename_two_to_one.is_empty() {
        apply_dir_rename_two_to_one_conflicts(
            db,
            &eff_ours,
            &eff_theirs,
            &dir_rename_two_to_one,
            &mut paths,
            &mut leaves,
            options,
        )?;
        clean = false;
    }

    // Rename/rename(2to1) and rename/add: two distinct contents collide on one
    // destination (and the rename source(s) are consumed). Detected from the full
    // per-side rename sets, applied here so the destination carries both sides'
    // content-merged stages instead of the path-keyed core's raw add/add.
    if !renames.rename_rename_two_to_one.is_empty() || !renames.rename_adds.is_empty() {
        apply_rename_two_to_one_and_add_conflicts(
            db,
            base_map,
            ours_map,
            theirs_map,
            &renames,
            &mut paths,
            &mut leaves,
            options,
        )?;
        clean = false;
    }

    // Rename/delete conflicts: a file renamed on one side whose source the other
    // side deleted. The merge core resolved the destination cleanly (only the
    // renaming side has it), but git flags this as a conflict — keep the renamed
    // content in the tree, record higher-order stages, and mark the merge dirty.
    if !renames.rename_deletes.is_empty() {
        for (dest, rd) in &renames.rename_deletes {
            // Skip if another conflict already claimed this destination.
            let Some(slot) = paths.iter_mut().find(|p| &p.path == dest) else {
                continue;
            };
            if slot.conflict.is_some() {
                continue;
            }
            let base_entry = base_map.get(&rd.source).copied();
            let renamed_entry = slot.result;
            // The renamed content sits on the renaming side; the deleting side
            // contributes no stage at the destination.
            let (ours_stage, theirs_stage) = match rd.side {
                RenameSide::Ours => (renamed_entry, None),
                RenameSide::Theirs => (None, renamed_entry),
            };
            let (renamed_in, deleted_in) = match rd.side {
                RenameSide::Ours => (
                    options.ours_label.to_string(),
                    options.theirs_label.to_string(),
                ),
                RenameSide::Theirs => (
                    options.theirs_label.to_string(),
                    options.ours_label.to_string(),
                ),
            };
            let worktree = match &renamed_entry {
                Some((mode, oid)) => Some((*mode, merge_worktree_bytes(db, *mode, oid)?)),
                None => None,
            };
            slot.stages = MergeStages {
                base: base_entry,
                ours: ours_stage,
                theirs: theirs_stage,
            };
            slot.worktree = worktree;
            slot.conflict = Some(MergeConflictKind::RenameDelete {
                old_path: rd.source.clone(),
                renamed_in,
                deleted_in,
            });
            clean = false;
        }
    }

    // Directory-rename outcomes that make the merge dirty. A collision/split
    // detected while re-homing (two paths onto one destination, an ambiguous
    // split source, or a file in the way) marks the merge unclean regardless of
    // mode. In =conflict mode, every silently re-homed path is *also* reported
    // as a location conflict: the tree keeps the re-homed content but git wants
    // the user to confirm the suggested move.
    if dir_rename_dirty {
        clean = false;
    }
    // Implicit-directory-rename collisions (a directory rename would put a path
    // onto an existing file/dir, or N paths onto one destination). git emits
    // `CONFLICT (implicit dir rename): Existing file/dir at <dest> in the way ...`
    // regardless of mode, and the merge is unclean. Attach the conflict to the
    // blocked destination path (which keeps its original content).
    for collision in &dir_rename_collisions {
        clean = false;
        if let Some(slot) = paths.iter_mut().find(|p| p.path == collision.dest)
            && slot.conflict.is_none()
        {
            slot.conflict = Some(MergeConflictKind::DirRenameImplicitCollision {
                sources: collision.sources.clone(),
            });
        } else if !paths.iter().any(|p| p.path == collision.dest) {
            paths.push(MergedPath {
                path: collision.dest.clone(),
                stages: MergeStages::default(),
                result: None,
                worktree: None,
                conflict: Some(MergeConflictKind::DirRenameImplicitCollision {
                    sources: collision.sources.clone(),
                }),
                auto_merged: false,
            });
        }
    }
    for source_dir in &dir_rename_splits {
        clean = false;
        paths.push(MergedPath {
            path: source_dir.clone(),
            stages: MergeStages::default(),
            result: None,
            worktree: None,
            conflict: Some(MergeConflictKind::DirRenameSplit {
                source_dir: source_dir.clone(),
            }),
            auto_merged: false,
        });
    }
    if !dir_rename_conflict_paths.is_empty() {
        clean = false;
        for (dest, infos) in &dir_rename_conflict_paths {
            for info in [&infos.ours, &infos.theirs].into_iter().flatten() {
                let (added_in, dir_renamed_in) = if info.added_on_ours {
                    // The path was added/renamed by ours, into a dir theirs renamed.
                    (
                        options.ours_label.to_string(),
                        options.theirs_label.to_string(),
                    )
                } else {
                    (
                        options.theirs_label.to_string(),
                        options.ours_label.to_string(),
                    )
                };
                // Rename-to-self via a directory rename (merge-ort 12i2): the
                // re-home landed the file back on its own base source path where
                // the other side modified it. git records this UNMERGED (UU) even
                // though the trivial 3-way at the destination resolves cleanly
                // (the renamed side's content equals base). Stage the three
                // versions so the index carries the conflict.
                let back_to_self = dir_rename_back_to_self.contains(dest);
                if let Some(slot) = paths.iter_mut().find(|p| &p.path == dest)
                    && slot.conflict.is_none()
                {
                    if back_to_self {
                        slot.stages = MergeStages {
                            base: eff_base.get(dest).copied(),
                            ours: eff_ours.get(dest).copied(),
                            theirs: eff_theirs.get(dest).copied(),
                        };
                        slot.worktree = match &slot.result {
                            Some((mode, oid)) => {
                                Some((*mode, merge_worktree_bytes(db, *mode, oid)?))
                            }
                            None => slot.worktree.clone(),
                        };
                    }
                    slot.conflict = Some(MergeConflictKind::DirRenameLocation {
                        old_path: info.old_path.clone(),
                        renamed_from: info.renamed_from.clone(),
                        added_in,
                        dir_renamed_in,
                        back_to_self,
                    });
                } else {
                    info_messages.push(MergeInfoMessage::DirRenameLocationConflict {
                        old_path: info.old_path.clone(),
                        new_path: dest.clone(),
                        renamed_from: info.renamed_from.clone(),
                        added_in,
                        dir_renamed_in,
                    });
                }
            }
        }
    }

    // Directory/file (D/F) conflict resolution (merge-ort `process_entry`): a
    // path that ends up as a *file* in the merged result while another result
    // path lives *under* it (so the path is simultaneously a directory) cannot
    // coexist. git keeps the directory at the original path and moves the file
    // aside to `path~<branch>` via `unique_path`, where `<branch>` is the side
    // that contributed the file. We resolve this on the flattened `leaves` after
    // every per-path decision is made, so renames/dir-renames have settled first.
    resolve_directory_file_conflicts(
        db,
        &mut paths,
        &mut leaves,
        &mut clean,
        &eff_ours,
        &eff_theirs,
        options,
        &mut info_messages,
    )?;

    let tree = write_merged_tree(db, &leaves)?;

    cleanup_paths.retain(|path| !leaves.contains_key(path));

    Ok(MergeTreesResult {
        tree,
        paths,
        clean,
        cleanup_paths: cleanup_paths.into_iter().collect(),
        info_messages,
    })
}

/// Flatten a branch label the way git's `add_flattened_path` does for
/// `unique_path`: any `/` in the branch name becomes `_` so the synthesized
/// `path~branch` stays a single path component family.
fn flatten_branch_label(branch: &str) -> String {
    branch.replace('/', "_")
}

/// Pick a `path~<branch>` name not already present in `leaves` (or claimed by an
/// existing `paths` entry), mirroring merge-ort's `unique_path`: start from
/// `path~branch`, then append `_0`, `_1`, … on collision.
fn unique_df_path(
    path: &[u8],
    branch: &str,
    leaves: &MergeEntryMap,
    paths: &[MergedPath],
) -> Vec<u8> {
    let mut base = path.to_vec();
    base.push(b'~');
    base.extend_from_slice(flatten_branch_label(branch).as_bytes());
    let taken = |candidate: &[u8]| {
        leaves.contains_key(candidate) || paths.iter().any(|p| p.path == candidate)
    };
    if !taken(&base) {
        return base;
    }
    let mut suffix = 0usize;
    loop {
        let mut candidate = base.clone();
        candidate.push(b'_');
        candidate.extend_from_slice(suffix.to_string().as_bytes());
        if !taken(&candidate) {
            return candidate;
        }
        suffix += 1;
    }
}

/// Resolve directory/file collisions in the merged leaf set. For every file leaf
/// whose path is also a directory (some other leaf lives under `path/`), move the
/// file to `path~<branch>` and record a [`MergeConflictKind::FileDirectory`].
#[allow(clippy::too_many_arguments)]
fn resolve_directory_file_conflicts(
    db: &FileObjectDatabase,
    paths: &mut Vec<MergedPath>,
    leaves: &mut MergeEntryMap,
    clean: &mut bool,
    eff_ours: &MergeEntryMap,
    eff_theirs: &MergeEntryMap,
    options: &MergeTreesOptions<'_>,
    info_messages: &mut Vec<MergeInfoMessage>,
) -> Result<()> {
    // A path is a "directory" in the result iff some leaf key has it as a strict
    // `path/` prefix. Collect every such directory prefix once.
    let mut directory_prefixes: BTreeSet<Vec<u8>> = BTreeSet::new();
    for key in leaves.keys() {
        let mut idx = 0;
        while let Some(pos) = key[idx..].iter().position(|b| *b == b'/') {
            let end = idx + pos;
            directory_prefixes.insert(key[..end].to_vec());
            idx = end + 1;
        }
    }
    if directory_prefixes.is_empty() {
        return Ok(());
    }

    // File leaves that collide with a directory of the same name.
    let colliding: Vec<Vec<u8>> = leaves
        .keys()
        .filter(|key| directory_prefixes.contains(*key))
        .cloned()
        .collect();

    for original in colliding {
        let Some(entry) = leaves.remove(&original) else {
            continue;
        };
        // The moved-aside file must be materialized in the worktree at its new
        // path; read its blob bytes once so the porcelain has worktree content.
        let moved_bytes = merge_worktree_bytes(db, entry.0, &entry.1)?;
        // Which side contributed the file? git keys off `dirmask`: the file lives
        // on the side that is NOT the directory. We read it off the effective side
        // maps — whichever side has this path as a plain file. When only theirs has
        // it, use the theirs label; otherwise (ours has it, or both do) ours wins,
        // matching git's index-1 bias for the moved-aside name.
        let ours_has_file = eff_ours.contains_key(&original);
        let theirs_has_file = eff_theirs.contains_key(&original);
        let from_ours = ours_has_file || !theirs_has_file;
        let branch = if from_ours {
            options.ours_label
        } else {
            options.theirs_label
        };
        let new_path = unique_df_path(&original, branch, leaves, paths);
        leaves.insert(new_path.clone(), entry);
        *clean = false;

        // Relocate the path's MergedPath: update its destination and stamp the D/F
        // conflict. If the path had no MergedPath (defensive), synthesize one.
        if let Some(slot) = paths.iter_mut().find(|p| p.path == original) {
            if let Some(MergeConflictKind::RenameDelete {
                old_path,
                renamed_in,
                deleted_in,
            }) = &slot.conflict
            {
                info_messages.push(MergeInfoMessage::RenameDeleteConflict {
                    old_path: old_path.clone(),
                    new_path: original.clone(),
                    renamed_in: renamed_in.clone(),
                    deleted_in: deleted_in.clone(),
                });
            }
            slot.path = new_path.clone();
            slot.result = Some(entry);
            // Preserve any pre-existing higher-order stages; a clean file leaf has
            // none, so seed ours/theirs from the effective maps for `ls-files -u`.
            if slot.stages.base.is_none()
                && slot.stages.ours.is_none()
                && slot.stages.theirs.is_none()
            {
                slot.stages = MergeStages {
                    base: None,
                    ours: if from_ours { Some(entry) } else { None },
                    theirs: if from_ours { None } else { Some(entry) },
                };
            }
            // Keep the slot's existing `auto_merged`: git only emits
            // `Auto-merging <new_path>` for the moved file when a real content
            // merge ran (a rename or both-sides change drives filemask>=6 through
            // handle_content_merge). A plain one-sided add (filemask 2/4) is moved
            // aside silently, so we must NOT force the flag on here.
            slot.worktree = Some((entry.0, moved_bytes));
            slot.conflict = Some(MergeConflictKind::FileDirectory {
                original_path: original.clone(),
                moved_from: branch.to_string(),
            });
        } else {
            paths.push(MergedPath {
                path: new_path.clone(),
                stages: MergeStages {
                    base: None,
                    ours: if from_ours { Some(entry) } else { None },
                    theirs: if from_ours { None } else { Some(entry) },
                },
                result: Some(entry),
                worktree: Some((entry.0, moved_bytes)),
                conflict: Some(MergeConflictKind::FileDirectory {
                    original_path: original.clone(),
                    moved_from: branch.to_string(),
                }),
                auto_merged: false,
            });
        }
    }

    // Keep `paths` sorted by destination path (callers and tests assume order).
    paths.sort_by(|a, b| a.path.cmp(&b.path));
    Ok(())
}

/// Construct a clean (non-conflicted) [`MergedPath`].
fn clean_path(path: Vec<u8>, result: Option<(u32, ObjectId)>) -> MergedPath {
    clean_path_auto(path, result, false)
}

/// Like [`clean_path`] but records whether the path went through a textual
/// 3-way content merge (for the "Auto-merging" message).
fn clean_path_auto(
    path: Vec<u8>,
    result: Option<(u32, ObjectId)>,
    auto_merged: bool,
) -> MergedPath {
    MergedPath {
        path,
        stages: MergeStages::default(),
        result,
        worktree: None,
        conflict: None,
        auto_merged,
    }
}

/// Snapshot the present stages for a conflicted path.
fn stages_for(
    base: &Option<(u32, ObjectId)>,
    ours: &Option<(u32, ObjectId)>,
    theirs: &Option<(u32, ObjectId)>,
) -> MergeStages {
    MergeStages {
        base: *base,
        ours: *ours,
        theirs: *theirs,
    }
}

/// Read a blob's raw bytes, requiring it to be a blob object.
fn merge_blob_bytes(reader: &impl ObjectReader, oid: &ObjectId) -> Result<Vec<u8>> {
    let object = reader.read_object(oid)?;
    if object.object_type != ObjectType::Blob {
        return Err(GitError::InvalidObject(format!(
            "expected blob {}, found {}",
            oid,
            object.object_type.as_str()
        )));
    }
    Ok(object.body.clone())
}

fn merge_worktree_bytes(reader: &impl ObjectReader, mode: u32, oid: &ObjectId) -> Result<Vec<u8>> {
    if sley_index::is_gitlink(mode) {
        Ok(Vec::new())
    } else {
        merge_blob_bytes(reader, oid)
    }
}

/// 3-way merge of a file mode. Returns the resolved mode and whether the modes
/// conflict (both sides changed it to different non-base values).
fn merge_file_modes(base: Option<u32>, ours: u32, theirs: u32) -> (u32, bool) {
    if ours == theirs {
        return (ours, false);
    }
    match base {
        Some(base) if ours == base => (theirs, false),
        Some(base) if theirs == base => (ours, false),
        _ => (ours, true),
    }
}

/// Build a top-level tree object from a flat map of `path -> (mode, oid)`
/// leaves, writing every (sub)tree object to `db`.
fn write_merged_tree(db: &FileObjectDatabase, leaves: &MergeEntryMap) -> Result<ObjectId> {
    let mut root = MergeTreeNode::default();
    for (path, (mode, oid)) in leaves {
        root.insert(path, *mode, *oid);
    }
    root.write(db)
}

#[derive(Default)]
struct MergeTreeNode {
    blobs: BTreeMap<Vec<u8>, (u32, ObjectId)>,
    subtrees: BTreeMap<Vec<u8>, MergeTreeNode>,
}

impl MergeTreeNode {
    fn insert(&mut self, path: &[u8], mode: u32, oid: ObjectId) {
        match path.iter().position(|byte| *byte == b'/') {
            Some(slash) => {
                let component = path[..slash].to_vec();
                let rest = &path[slash + 1..];
                self.subtrees
                    .entry(component)
                    .or_default()
                    .insert(rest, mode, oid);
            }
            None => {
                self.blobs.insert(path.to_vec(), (mode, oid));
            }
        }
    }

    fn write(&self, db: &FileObjectDatabase) -> Result<ObjectId> {
        let mut entries: Vec<TreeEntry> = Vec::new();
        for (name, (mode, oid)) in &self.blobs {
            entries.push(TreeEntry {
                mode: *mode,
                name: BString::from(name.clone()),
                oid: *oid,
            });
        }
        for (name, subtree) in &self.subtrees {
            let oid = subtree.write(db)?;
            entries.push(TreeEntry {
                mode: 0o040000,
                name: BString::from(name.clone()),
                oid,
            });
        }
        entries.sort_by_key(merge_tree_sort_key);
        let tree = Tree { entries };
        db.write_object(EncodedObject::new(ObjectType::Tree, tree.write()))
    }
}

fn merge_tree_sort_key(entry: &TreeEntry) -> Vec<u8> {
    let mut key = entry.name.as_bytes().to_vec();
    if entry.mode == 0o040000 {
        key.push(b'/');
    }
    key
}

// --- Rename-aware non-recursive merge -------------------------------------

/// Which side of the merge performed a rename.
#[derive(Clone, Copy, PartialEq, Eq)]
enum RenameSide {
    Ours,
    Theirs,
}

/// One detected one-sided rename: its source path and which side renamed it.
#[derive(Clone)]
struct MergeRename {
    source: Vec<u8>,
    side: RenameSide,
}

/// A file renamed on one side whose source was *deleted* on the other side — a
/// rename/delete conflict. git keeps the renamed content at the destination but
/// flags the merge as conflicted.
#[derive(Clone)]
struct RenameDelete {
    /// The pre-rename source path (deleted on the other side).
    source: Vec<u8>,
    /// Which side performed the rename (the other side deleted the source).
    side: RenameSide,
}

/// The rename pairings discovered for one merge: which destination paths came
/// from which source path, and which side renamed (so the other side's change
/// can follow the rename and conflict labels can be path-qualified like git).
#[derive(Default)]
struct MergeRenames {
    /// One-sided renames keyed by *destination* path. Only renames where the
    /// OTHER side kept/modified the source in place are recorded (the case
    /// where the modification must follow the rename).
    dest_to_source: BTreeMap<Vec<u8>, MergeRename>,
    /// Rename/delete conflicts: a file renamed on one side whose source the
    /// other side deleted. Keyed by destination path.
    rename_deletes: BTreeMap<Vec<u8>, RenameDelete>,
    /// Rename/rename(1to2) conflicts keyed by source path.
    rename_rename_one_to_two: BTreeMap<Vec<u8>, RenameRenameOneToTwo>,
    /// Rename/rename(2to1) conflicts keyed by the shared *destination* path:
    /// ours renamed `ours_source`->dest and theirs renamed `theirs_source`->dest.
    rename_rename_two_to_one: BTreeMap<Vec<u8>, RenameRenameTwoToOne>,
    /// Rename/add conflicts keyed by *destination*: one side renamed a file to
    /// `dest` while the other side added a different file at the same `dest`.
    rename_adds: BTreeMap<Vec<u8>, RenameAdd>,
}

#[derive(Clone)]
struct RenameRenameOneToTwo {
    ours_dest: Vec<u8>,
    theirs_dest: Vec<u8>,
}

/// A rename/rename(2to1): two distinct sources renamed onto one destination, one
/// rename per side. Each side's content at the destination is the 3-way merge of
/// its rename (the other side's change to that source follows the rename).
#[derive(Clone)]
struct RenameRenameTwoToOne {
    /// The source ours renamed onto the destination.
    ours_source: Vec<u8>,
    /// The source theirs renamed onto the destination.
    theirs_source: Vec<u8>,
}

/// A rename/add: one side renamed a file onto `dest`, the other side added an
/// unrelated file at `dest`. The renaming side's content is the 3-way merge of
/// its rename; the adding side contributes its added blob verbatim.
#[derive(Clone)]
struct RenameAdd {
    /// The pre-rename source path on the renaming side.
    source: Vec<u8>,
    /// Which side performed the rename (the other side added at `dest`).
    side: RenameSide,
}

/// Every file rename observed on one side (base->side), as `(old, new)` pairs.
/// Unlike [`MergeRenames`] this is the *complete* rename set on a side — it is
/// the input to directory-rename inference, which needs to see all the per-file
/// moves between directories, not just the ones the other side kept in place.
struct SideRenames {
    pairs: Vec<(Vec<u8>, Vec<u8>)>,
}

/// Detect one-sided renames usable for a non-recursive merge: a path present in
/// `base`, deleted on one side and present (renamed) at a new path on that same
/// side, while the OTHER side still has the original path (modified or
/// unchanged). Such a rename lets the other side's change move to the
/// destination.
///
/// Also returns the complete per-side rename set so the caller can infer
/// directory renames (which need every file move, not just the merge-relevant
/// ones).
fn detect_merge_renames(
    db: &FileObjectDatabase,
    format: ObjectFormat,
    base_map: &MergeEntryMap,
    ours_map: &MergeEntryMap,
    theirs_map: &MergeEntryMap,
    options: &MergeTreesOptions<'_>,
) -> Result<(MergeRenames, SideRenames, SideRenames)> {
    let mut renames = MergeRenames::default();

    // Renames on ours: the other side that must carry its change is theirs.
    let ours_side = collect_side_renames(
        db,
        format,
        base_map,
        ours_map,
        theirs_map,
        RenameSide::Ours,
        options.rename_threshold,
        options.rename_limit,
        &mut renames,
    )?;
    // Renames on theirs: the other side that carries its change is ours.
    let theirs_side = collect_side_renames(
        db,
        format,
        base_map,
        theirs_map,
        ours_map,
        RenameSide::Theirs,
        options.rename_threshold,
        options.rename_limit,
        &mut renames,
    )?;

    collect_rename_rename_one_to_two(&mut renames, &ours_side, &theirs_side);
    collect_rename_rename_two_to_one_and_adds(
        &mut renames,
        &ours_side,
        &theirs_side,
        base_map,
        ours_map,
        theirs_map,
    );

    Ok((renames, ours_side, theirs_side))
}

/// Detect rename/rename(2to1) and rename/add conflicts from the complete per-side
/// rename sets. Both arise when a one-sided rename's destination is *occupied* on
/// the other side (so [`collect_side_renames`] left it out of `dest_to_source`):
///
/// * 2to1 — both sides renamed (distinct sources) onto the same destination.
/// * rename/add — one side renamed onto a path the other side *added* fresh
///   (the destination is new to the other side, not a base path it kept and not
///   itself a rename destination on that side).
fn collect_rename_rename_two_to_one_and_adds(
    renames: &mut MergeRenames,
    ours_side: &SideRenames,
    theirs_side: &SideRenames,
    base_map: &MergeEntryMap,
    ours_map: &MergeEntryMap,
    theirs_map: &MergeEntryMap,
) {
    let ours_by_dest: BTreeMap<&[u8], &[u8]> = ours_side
        .pairs
        .iter()
        .map(|(old, new)| (new.as_slice(), old.as_slice()))
        .collect();
    let theirs_by_dest: BTreeMap<&[u8], &[u8]> = theirs_side
        .pairs
        .iter()
        .map(|(old, new)| (new.as_slice(), old.as_slice()))
        .collect();

    // 2to1: a destination that is a rename target on BOTH sides from different
    // sources. (Same source on both sides is a rename/rename(1to1), handled by
    // the path-keyed core; same source to two dests is the 1to2 case above.)
    for (dest, ours_src) in &ours_by_dest {
        let Some(theirs_src) = theirs_by_dest.get(dest) else {
            continue;
        };
        if ours_src == theirs_src {
            continue;
        }
        // Don't disturb a destination the 1to2 pass already claimed.
        if renames.rename_rename_one_to_two.contains_key(*dest) {
            continue;
        }
        renames.rename_rename_two_to_one.insert(
            dest.to_vec(),
            RenameRenameTwoToOne {
                ours_source: ours_src.to_vec(),
                theirs_source: theirs_src.to_vec(),
            },
        );
    }

    // rename/add on ours: ours renamed onto `dest`, which theirs added (present
    // on theirs, absent from base, and not a theirs rename target).
    for (dest, ours_src) in &ours_by_dest {
        if renames.rename_rename_two_to_one.contains_key(*dest)
            || renames.rename_rename_one_to_two.contains_key(*dest)
        {
            continue;
        }
        if theirs_map.contains_key(*dest)
            && !base_map.contains_key(*dest)
            && !theirs_by_dest.contains_key(dest)
        {
            renames.rename_adds.insert(
                dest.to_vec(),
                RenameAdd {
                    source: ours_src.to_vec(),
                    side: RenameSide::Ours,
                },
            );
        }
    }
    // rename/add on theirs: symmetric.
    for (dest, theirs_src) in &theirs_by_dest {
        if renames.rename_rename_two_to_one.contains_key(*dest)
            || renames.rename_rename_one_to_two.contains_key(*dest)
            || renames.rename_adds.contains_key(*dest)
        {
            continue;
        }
        if ours_map.contains_key(*dest)
            && !base_map.contains_key(*dest)
            && !ours_by_dest.contains_key(dest)
        {
            renames.rename_adds.insert(
                dest.to_vec(),
                RenameAdd {
                    source: theirs_src.to_vec(),
                    side: RenameSide::Theirs,
                },
            );
        }
    }
}

fn collect_rename_rename_one_to_two(
    renames: &mut MergeRenames,
    ours_side: &SideRenames,
    theirs_side: &SideRenames,
) {
    let ours_by_source: BTreeMap<&[u8], &[u8]> = ours_side
        .pairs
        .iter()
        .map(|(old, new)| (old.as_slice(), new.as_slice()))
        .collect();
    for (old, theirs_new) in &theirs_side.pairs {
        let Some(ours_new) = ours_by_source.get(old.as_slice()) else {
            continue;
        };
        if *ours_new == theirs_new.as_slice() {
            continue;
        }
        renames.rename_deletes.remove(*ours_new);
        renames.rename_deletes.remove(theirs_new);
        renames.dest_to_source.remove(*ours_new);
        renames.dest_to_source.remove(theirs_new);
        renames.rename_rename_one_to_two.insert(
            old.clone(),
            RenameRenameOneToTwo {
                ours_dest: (*ours_new).to_vec(),
                theirs_dest: theirs_new.clone(),
            },
        );
    }
}

/// Collect renames that occurred on `side` (relative to `base`). Records the
/// merge-relevant subset (renames the `other` side still references) into
/// `renames`, and returns the *complete* per-side rename set for directory-rename
/// inference. `db`/`format` resolve blob bytes for similarity scoring.
#[allow(clippy::too_many_arguments)]
fn collect_side_renames(
    db: &FileObjectDatabase,
    format: ObjectFormat,
    base_map: &MergeEntryMap,
    side_map: &MergeEntryMap,
    other_map: &MergeEntryMap,
    side: RenameSide,
    threshold: u8,
    rename_limit: usize,
    renames: &mut MergeRenames,
) -> Result<SideRenames> {
    // Diff base->side with inexact rename detection; the resulting `Renamed`
    // entries name (old_path -> new_path) pairs on this side.
    let base_tree = entry_map_as_tracked(base_map);
    let side_tree = entry_map_as_tracked(side_map);
    let options = DiffNameStatusOptions {
        detect_renames: true,
        detect_copies: false,
        find_copies_harder: false,
        rename_empty: false,
        detect_inexact: true,
        rename_threshold: threshold,
        copy_threshold: threshold,
        rename_limit,
        ..Default::default()
    };
    let changes = diff_name_status_maps_with_renames(
        &base_tree,
        &side_tree,
        base_tree.keys().chain(side_tree.keys()),
        options,
        |oid| merge_blob_bytes(db, oid).ok().map(|b| Arc::from(b.into_boxed_slice())),
    )?;

    let mut pairs = Vec::new();
    for change in changes {
        let NameStatus::Renamed(_) = change.status else {
            continue;
        };
        let Some(old_path) = change.old_path.as_ref() else {
            continue;
        };
        let old = old_path.as_bytes().to_vec();
        let new = change.path.as_bytes().to_vec();
        // Complete rename set, fed to directory-rename inference.
        pairs.push((old.clone(), new.clone()));

        // Only act when the destination is genuinely new (not already present
        // in either side from a different origin) and the OTHER side still
        // references the source path — i.e. the other side modified/kept `old`,
        // and its change should follow the rename to `new`.
        if !other_map.contains_key(&old) {
            // The source path is gone on the other side. If it existed in base
            // (so the other side *deleted* it) and the other side did not also
            // produce `new`, this is a rename/delete conflict: this side renamed
            // the file, the other side deleted its source.
            if base_map.contains_key(&old) && !other_map.contains_key(&new) {
                renames
                    .rename_deletes
                    .entry(new.clone())
                    .or_insert(RenameDelete {
                        source: old.clone(),
                        side,
                    });
            }
            continue;
        }
        // If the other side ALSO renamed/created `new`, that is a rename/rename
        // or rename/add corner case we leave to the path-keyed core (stage-b).
        if other_map.contains_key(&new) {
            continue;
        }
        // Skip if both sides renamed the same source to the same dest (already
        // recorded) or to anything (first writer wins; the path-keyed core then
        // sees identical dest entries and resolves trivially).
        renames
            .dest_to_source
            .entry(new)
            .or_insert(MergeRename { source: old, side });
    }

    let _ = format;
    Ok(SideRenames { pairs })
}

/// Rewrite the three side maps so that each detected one-sided rename old->new
/// presents the OTHER side's `old` entry at `new`, and removes `old` from
/// every side. The path-keyed merge core then performs the 3-way content merge
/// at `new` with base=base[old], one side = the renaming side's new content,
/// the other side = the modifying side's old content.
fn apply_merge_renames(
    base_map: &MergeEntryMap,
    ours_map: &MergeEntryMap,
    theirs_map: &MergeEntryMap,
    renames: &MergeRenames,
) -> (MergeEntryMap, MergeEntryMap, MergeEntryMap) {
    if renames.dest_to_source.is_empty() {
        return (base_map.clone(), ours_map.clone(), theirs_map.clone());
    }
    let mut base = base_map.clone();
    let mut ours = ours_map.clone();
    let mut theirs = theirs_map.clone();

    for (new, rename) in &renames.dest_to_source {
        let old = &rename.source;
        // Move base[old] to base[new] so the destination has a proper ancestor.
        if let Some(entry) = base.remove(old) {
            base.entry(new.clone()).or_insert(entry);
        }
        // For each side, if it still has `old`, move that entry to `new`.
        for side in [&mut ours, &mut theirs] {
            if let Some(entry) = side.remove(old) {
                side.entry(new.clone()).or_insert(entry);
            }
        }
    }
    (base, ours, theirs)
}

// --- Directory-rename detection -------------------------------------------

/// The parent directory of `path`, or `None` for a top-level path.
fn parent_dir(path: &[u8]) -> Option<&[u8]> {
    path.iter().rposition(|b| *b == b'/').map(|i| &path[..i])
}

/// Apply a directory rename `old_dir -> new_dir` to `path` (which must live
/// under `old_dir`). E.g. `old_dir=z`, `new_dir=y`, `path=z/d` -> `y/d`; an
/// empty `new_dir` (rename into the repo root) drops the directory prefix.
fn apply_dir_rename(old_dir: &[u8], new_dir: &[u8], path: &[u8]) -> Vec<u8> {
    // The portion of `path` after `old_dir/` (handle root-target by stepping
    // past the separator, exactly as git's apply_dir_rename does).
    let rest_start = if new_dir.is_empty() {
        old_dir.len() + 1
    } else {
        old_dir.len()
    };
    let mut out = new_dir.to_vec();
    out.extend_from_slice(&path[rest_start..]);
    out
}

/// Find the longest renamed ancestor directory of `path`: walk parent dirs from
/// the deepest up and return the first one present in `dir_renames`. Mirrors
/// merge-ort's `check_dir_renamed`.
fn check_dir_renamed<'a>(
    path: &[u8],
    dir_renames: &'a BTreeMap<Vec<u8>, Vec<u8>>,
) -> Option<(&'a [u8], &'a [u8])> {
    let mut cur = parent_dir(path);
    while let Some(dir) = cur {
        if let Some((old_dir, new_dir)) = dir_renames.get_key_value(dir) {
            return Some((old_dir.as_slice(), new_dir.as_slice()));
        }
        cur = parent_dir(dir);
    }
    None
}

/// The provisional directory renames computed for both sides, plus the source
/// directories whose rename was ambiguous (a "split").
struct DirectoryRenameMaps {
    /// `old_dir -> new_dir` directory renames detected on ours' side. A path
    /// added/renamed by theirs under `old_dir` re-homes into `new_dir`.
    ours: BTreeMap<Vec<u8>, Vec<u8>>,
    /// Directory renames detected on theirs' side.
    theirs: BTreeMap<Vec<u8>, Vec<u8>>,
    /// Source directories whose split was unclear on ours' side (no unique
    /// majority target); paths on theirs that would need to follow such a rename
    /// are a conflict, not silent.
    ours_split: BTreeSet<Vec<u8>>,
    /// Source directories whose split was unclear on theirs' side.
    theirs_split: BTreeSet<Vec<u8>>,
}

/// Infer directory renames from the complete per-side file-rename sets, mirroring
/// merge-ort's `get_provisional_directory_renames` + `handle_directory_level_conflicts`.
/// For every file moved `.../old_dir/x -> .../new_dir/x`, the ancestor pairs are
/// tallied (`dir_rename_count`) and collapsed to `old_dir -> best_new_dir` where
/// `best` is the unique highest count. A tie marks the source directory as a
/// "split". A rename is only kept if the source directory was *entirely removed*
/// on that side (the `dirs_removed` gate). A directory renamed on BOTH sides is
/// dropped from both maps (ambiguous).
fn compute_directory_renames(
    ours_map: &MergeEntryMap,
    theirs_map: &MergeEntryMap,
    ours_side: &SideRenames,
    theirs_side: &SideRenames,
) -> DirectoryRenameMaps {
    let ours = compute_side_dir_renames(&ours_side.pairs, ours_map);
    let theirs = compute_side_dir_renames(&theirs_side.pairs, theirs_map);

    // A directory renamed on BOTH sides (to whatever target) is ambiguous;
    // git's handle_directory_level_conflicts drops it from both maps so neither
    // side's directory rename is applied.
    let mut ours_map_out = ours.renames;
    let mut theirs_map_out = theirs.renames;
    let dup: Vec<Vec<u8>> = ours_map_out
        .keys()
        .filter(|k| theirs_map_out.contains_key(*k))
        .cloned()
        .collect();
    for k in dup {
        ours_map_out.remove(&k);
        theirs_map_out.remove(&k);
    }

    DirectoryRenameMaps {
        ours: ours_map_out,
        theirs: theirs_map_out,
        ours_split: ours.split,
        theirs_split: theirs.split,
    }
}

/// Per-side directory-rename computation result.
struct SideDirRenames {
    renames: BTreeMap<Vec<u8>, Vec<u8>>,
    split: BTreeSet<Vec<u8>>,
}

/// Compute one side's `old_dir -> new_dir` map from its file renames, gated on
/// the source directory being fully removed on that side.
fn compute_side_dir_renames(
    pairs: &[(Vec<u8>, Vec<u8>)],
    side_map: &MergeEntryMap,
) -> SideDirRenames {
    // dir_rename_count: count[old_dir][new_dir]. Built by walking every rename's
    // ancestor directories while the *trailing* path components match, exactly
    // as merge-ort's update_dir_rename_counts does. For
    //   a/b/c/d/e/foo.c -> a/b/some/thing/else/e/foo.c
    // this records both
    //   a/b/c/d/e => a/b/some/thing/else/e   AND   a/b/c/d => a/b/some/thing/else
    // but stops once the trailing components diverge.
    let mut counts: BTreeMap<Vec<u8>, BTreeMap<Vec<u8>, usize>> = BTreeMap::new();
    for (old, new) in pairs {
        update_dir_rename_counts(&mut counts, old, new);
    }

    let mut renames = BTreeMap::new();
    let mut split = BTreeSet::new();
    for (old_dir, targets) in counts {
        let mut max = 0usize;
        let mut bad_max = 0usize;
        let mut best: Option<Vec<u8>> = None;
        for (target, count) in &targets {
            if *count == max {
                bad_max = max;
            } else if *count > max {
                max = *count;
                best = Some(target.clone());
            }
        }
        if max == 0 {
            continue;
        }
        if bad_max == max {
            split.insert(old_dir);
            continue;
        }
        // dirs_removed gate: the source directory must be entirely gone on this
        // side. New files that recreate the old directory count too; otherwise
        // cases like "both sides renamed z/ -> y/, but one side added z/d"
        // incorrectly look like both sides performed a whole-directory rename.
        if let Some(best) = best
            && directory_fully_removed(&old_dir, side_map)
        {
            renames.insert(old_dir, best);
        }
    }

    SideDirRenames { renames, split }
}

/// Tally the ancestor directory-rename pairs implied by a single file rename
/// `old -> new`, mirroring merge-ort's `update_dir_rename_counts`. Starting from
/// the immediate parent dirs, we strip one trailing component at a time and
/// record `old_ancestor -> new_ancestor` as long as the *remaining* trailing
/// suffix still matches between the two paths.
fn update_dir_rename_counts(
    counts: &mut BTreeMap<Vec<u8>, BTreeMap<Vec<u8>, usize>>,
    old: &[u8],
    new: &[u8],
) {
    // Work on owned copies we progressively truncate at each '/'.
    let mut old_dir = old.to_vec();
    let mut new_dir = new.to_vec();
    let mut first = true;
    loop {
        // Strip the trailing component (basename on the first pass, then a dir
        // each pass) to ascend one level.
        let old_has = dir_munge(&mut old_dir);
        let new_has = dir_munge(&mut new_dir);

        // On the first pass we only stripped the basename; the dirs need not
        // match. On later passes the *trailing* components must agree, otherwise
        // the rename no longer implies this ancestor pairing.
        if !first {
            let old_sub = trailing_component(old, &old_dir);
            let new_sub = trailing_component(new, &new_dir);
            if old_sub != new_sub {
                break;
            }
        }

        if old_dir == new_dir {
            // Same directory at this level — no rename implied, and no deeper
            // ancestor can differ usefully either.
            break;
        }
        *counts
            .entry(old_dir.clone())
            .or_default()
            .entry(new_dir.clone())
            .or_default() += 1;

        first = false;
        // Hitting the toplevel ("") on either side ends the ascent.
        if old_dir.is_empty() || new_dir.is_empty() {
            break;
        }
        // If the two ancestors are identical from here up, stop (git stops once
        // the suffix-equal walk reaches a common prefix).
        if !old_has || !new_has {
            break;
        }
    }
}

/// Truncate `buf` at its last '/', leaving the parent directory (or empty for a
/// toplevel name). Returns whether a '/' was present (i.e. there is a deeper
/// ancestor to ascend into).
fn dir_munge(buf: &mut Vec<u8>) -> bool {
    match buf.iter().rposition(|b| *b == b'/') {
        Some(i) => {
            buf.truncate(i);
            true
        }
        None => {
            buf.clear();
            false
        }
    }
}

/// The trailing path component that was stripped from `full` to reach `dir`
/// (i.e. the suffix of `full` after `dir/`). Used to compare whether the two
/// sides of a rename share the same trailing directory chain.
fn trailing_component<'a>(full: &'a [u8], dir: &[u8]) -> &'a [u8] {
    if dir.is_empty() {
        full
    } else {
        // full = dir + "/" + suffix
        &full[dir.len() + 1..]
    }
}

/// True when no path under `dir/` exists on `side` (the directory was entirely
/// removed there). Mirrors merge-ort's `dirs_removed` precondition.
fn directory_fully_removed(dir: &[u8], side_map: &MergeEntryMap) -> bool {
    let mut prefix = dir.to_vec();
    prefix.push(b'/');
    for path in side_map.keys() {
        if path.starts_with(&prefix) {
            return false;
        }
    }
    true
}

/// A path on one side whose location is rewritten by a directory rename the
/// *other* side performed. The rewrite applies equally to a freshly added file
/// and to a file the side itself renamed (a transitive rename).
struct DirRenameMove {
    /// The path as it currently sits in the side's effective map (the side's own
    /// rename, if any, already applied).
    from: Vec<u8>,
    /// The re-homed destination, after applying the other side's directory rename.
    to: Vec<u8>,
    /// `Some(source)` when `from` is a rename destination produced by this side
    /// (transitive rename); `None` for a fresh add. Drives git's
    /// "renamed to"/"added in" message wording.
    renamed_from: Option<Vec<u8>>,
}

struct DirRenameTwoToOne {
    dest: Vec<u8>,
    ours_source: Vec<u8>,
    theirs_source: Vec<u8>,
    ours_label_path: Vec<u8>,
    theirs_label_path: Vec<u8>,
}

/// Provenance of a re-homed path, for `=conflict`-mode `CONFLICT (file location)`
/// reporting.
#[derive(Clone)]
struct RehomeInfo {
    /// The pre-re-home path on the adding/renaming side.
    old_path: Vec<u8>,
    /// `Some(source)` for a transitive rename, `None` for a fresh add.
    renamed_from: Option<Vec<u8>>,
    /// Whether the *adding/renaming* side was ours (true) or theirs (false). The
    /// caller resolves this to a branch label.
    added_on_ours: bool,
}

/// Per-side provenance for a destination created by directory-rename rehoming.
#[derive(Clone, Default)]
struct RehomeSides {
    ours: Option<RehomeInfo>,
    theirs: Option<RehomeInfo>,
}

/// An implicit-directory-rename collision: one or more paths a directory rename
/// would re-home onto `dest`, which is blocked because `dest` is already
/// occupied (a file in the way) or because multiple sources map to it. git emits
/// `CONFLICT (implicit dir rename): Existing file/dir at <dest> in the way ...`.
struct DirRenameCollision {
    /// The blocked destination path (the file/dir already there).
    dest: Vec<u8>,
    /// The source path(s) the directory rename tried to move onto `dest`.
    sources: Vec<Vec<u8>>,
}

/// Outcome of applying directory renames to all three effective maps.
struct DirRenameOutcome {
    /// Rewritten base/ours/theirs maps with re-homed paths moved to their
    /// destinations. `base` moves too so a re-homed content-merge keeps its
    /// ancestor at the new location.
    base: MergeEntryMap,
    ours: MergeEntryMap,
    theirs: MergeEntryMap,
    /// Re-homed destination path -> provenance (for `=conflict`-mode reporting).
    rehomed: BTreeMap<Vec<u8>, RehomeSides>,
    /// Implicit-dir-rename collisions (file in the way / N-to-1), for the
    /// `CONFLICT (implicit dir rename)` message; always conflicts regardless of
    /// mode.
    collisions: Vec<DirRenameCollision>,
    /// Split source dirs that were relevant to a path on the other side.
    splits: BTreeSet<Vec<u8>>,
    /// Destinations where a directory rename moved a file back onto its own base
    /// source path (rename-to-self) and the other side modified that path. git
    /// records these as an unmerged file-location conflict (`UU`) rather than a
    /// clean auto-resolution; the trivial 3-way at the destination would
    /// otherwise resolve cleanly because the renamed side's content equals base.
    back_to_self: BTreeSet<Vec<u8>>,
    /// True if a directory-level collision or split made the merge dirty even in
    /// `=true` mode (e.g. two paths re-homed onto one destination).
    dirty: bool,
    info_messages: Vec<MergeInfoMessage>,
}

/// Apply directory renames to both sides' effective maps.
///
/// This mirrors merge-ort's `collect_renames` + `check_for_directory_rename` +
/// `apply_directory_rename_modifications`: every path a side *added* or *renamed*
/// that lives under a directory the OTHER side renamed has its destination
/// rewritten to follow that rename — making the directory rename a property of
/// the rename-detection pass that every path consults, not a per-file special
/// case. Handles:
///   - transitive renames (a file the side renamed into a dir the other side
///     renamed follows on into the final directory),
///   - `dir_rename_exclusions` (never re-home into a directory THIS side itself
///     renamed — that would create a spurious rename/rename(1to2)),
///   - collisions (N paths mapping to one destination -> conflict),
///   - splits (a source dir with no majority target -> conflict, leave in place).
#[allow(clippy::too_many_arguments)]
fn apply_directory_renames(
    base_map: &MergeEntryMap,
    eff_base: &MergeEntryMap,
    eff_ours: &MergeEntryMap,
    eff_theirs: &MergeEntryMap,
    ours_side: &SideRenames,
    theirs_side: &SideRenames,
    dir_renames: &DirectoryRenameMaps,
    file_rename_dests: &BTreeMap<Vec<u8>, MergeRename>,
) -> DirRenameOutcome {
    let mut base = eff_base.clone();
    let mut ours = eff_ours.clone();
    let mut theirs = eff_theirs.clone();
    let mut rehomed = BTreeMap::new();
    let mut collisions = Vec::new();
    let mut splits = BTreeSet::new();
    let mut back_to_self = BTreeSet::new();
    let mut info_messages = Vec::new();
    let mut dirty = false;

    // Ours' paths follow THEIRS' directory renames; the exclusions are OURS' own
    // renamed-into dirs (never re-home a path into a directory this same side
    // renamed). Symmetrically for theirs.
    let ours_excl = exclusion_dirs(&dir_renames.ours);
    let theirs_excl = exclusion_dirs(&dir_renames.theirs);

    // Plan ours' moves (following theirs' dir-renames) and theirs' moves
    // (following ours' dir-renames). Planning before applying lets us detect
    // collisions (N paths onto one destination) across the whole side.
    let ours_moves = plan_rehome(
        base_map,
        &ours,
        ours_side,
        &dir_renames.theirs,
        &ours_excl,
        &dir_renames.theirs_split,
        &mut collisions,
        &mut splits,
        &mut info_messages,
        &mut dirty,
    );
    let theirs_moves = plan_rehome(
        base_map,
        &theirs,
        theirs_side,
        &dir_renames.ours,
        &theirs_excl,
        &dir_renames.ours_split,
        &mut collisions,
        &mut splits,
        &mut info_messages,
        &mut dirty,
    );

    apply_rehome_moves(
        base_map,
        file_rename_dests,
        &mut base,
        &mut ours,
        &mut theirs,
        ours_moves,
        true,
        &mut rehomed,
        &mut collisions,
        &mut back_to_self,
        &mut dirty,
    );
    apply_rehome_moves(
        base_map,
        file_rename_dests,
        &mut base,
        &mut ours,
        &mut theirs,
        theirs_moves,
        false,
        &mut rehomed,
        &mut collisions,
        &mut back_to_self,
        &mut dirty,
    );

    DirRenameOutcome {
        base,
        ours,
        theirs,
        rehomed,
        collisions,
        splits,
        back_to_self,
        dirty,
        info_messages,
    }
}

/// The set of *source* directories a side renamed away from. A directory rename
/// the other side wants to apply into one of these dirs is skipped (it would
/// produce a spurious rename/rename(1to2)); git's `dir_rename_exclusions`.
fn exclusion_dirs(side_dir_renames: &BTreeMap<Vec<u8>, Vec<u8>>) -> BTreeSet<Vec<u8>> {
    side_dir_renames.keys().cloned().collect()
}

/// Re-home `target`'s added/renamed paths that fall under a directory the other
/// side renamed (`renamer_dirs`: `old_dir -> new_dir`).
///
/// Candidates are paths present on this side and absent in base — i.e. both
/// Plan the directory-rename moves for one side: which of its added/renamed
/// paths re-home where, following `renamer_dirs` (the OTHER side's dir-renames).
///
/// Candidates are paths present on this side and absent in base — both freshly
/// added files AND this side's own rename destinations (the latter give the
/// transitive-rename behaviour). A candidate whose target directory is in
/// `exclusions` (a dir this side itself renamed) is skipped. Splits mark the
/// merge dirty; N-to-1 collisions (multiple sources onto one destination) record
/// a `DirRenameCollision` and yield no move. Returns the surviving single moves
/// (one per destination).
#[allow(clippy::too_many_arguments)]
fn plan_rehome(
    base_map: &MergeEntryMap,
    side: &MergeEntryMap,
    side_renames: &SideRenames,
    renamer_dirs: &BTreeMap<Vec<u8>, Vec<u8>>,
    exclusions: &BTreeSet<Vec<u8>>,
    split_dirs: &BTreeSet<Vec<u8>>,
    collisions: &mut Vec<DirRenameCollision>,
    splits: &mut BTreeSet<Vec<u8>>,
    info_messages: &mut Vec<MergeInfoMessage>,
    dirty: &mut bool,
) -> Vec<DirRenameMove> {
    if renamer_dirs.is_empty() && split_dirs.is_empty() {
        return Vec::new();
    }

    // This side's rename destinations -> sources; eligible for a transitive
    // rewrite and carry the original source for message wording.
    let side_rename_src: BTreeMap<&[u8], &[u8]> = side_renames
        .pairs
        .iter()
        .map(|(o, n)| (n.as_slice(), o.as_slice()))
        .collect();

    let candidates: Vec<Vec<u8>> = side
        .keys()
        .filter(|p| !base_map.contains_key(*p) || side_rename_src.contains_key(p.as_slice()))
        .cloned()
        .collect();

    // dest -> the moves wanting to land there (collision detection).
    let mut planned: BTreeMap<Vec<u8>, Vec<DirRenameMove>> = BTreeMap::new();
    for path in candidates {
        if let Some(split_dir) = check_dir_split(&path, split_dirs) {
            splits.insert(split_dir.to_vec());
            *dirty = true;
            continue;
        }
        let Some((old_dir, new_dir)) = check_dir_renamed(&path, renamer_dirs) else {
            continue;
        };
        // dir_rename_exclusions: don't apply a rename INTO a directory this side
        // itself renamed; that would cause a spurious rename/rename(1to2). The
        // file instead follows this side's own rename, so leave it.
        let new_dir_is_exclusion = exclusions.contains(new_dir);
        let new_dir_inside_exclusion = exclusions
            .iter()
            .any(|dir| directory_contains_proper(dir, new_dir));
        if new_dir_is_exclusion
            || (new_dir_inside_exclusion
                && !side_has_pure_add_under_dir(side, base_map, &side_rename_src, old_dir))
        {
            info_messages.push(MergeInfoMessage::DirRenameSkippedDueToRerename {
                old_dir: old_dir.to_vec(),
                path: path.clone(),
                new_dir: new_dir.to_vec(),
            });
            continue;
        }
        let dest = apply_dir_rename(old_dir, new_dir, &path);
        if dest == path {
            // Directory rename causes a rename-to-self: already in place.
            continue;
        }
        let renamed_from = side_rename_src.get(path.as_slice()).map(|s| s.to_vec());
        planned
            .entry(dest.clone())
            .or_default()
            .push(DirRenameMove {
                from: path,
                to: dest,
                renamed_from,
            });
    }

    let mut moves = Vec::new();
    for (dest, group) in planned {
        if group.len() > 1 {
            // Multiple paths map to one destination: an implicit-dir-rename
            // collision. git leaves all of them in place and conflicts.
            *dirty = true;
            collisions.push(DirRenameCollision {
                dest,
                sources: group.into_iter().map(|m| m.from).collect(),
            });
            continue;
        }
        moves.push(group.into_iter().next().expect("non-empty"));
    }
    moves
}

fn check_dir_split<'a>(path: &[u8], split_dirs: &'a BTreeSet<Vec<u8>>) -> Option<&'a [u8]> {
    let mut dir = parent_dir(path)?;
    loop {
        if let Some(split_dir) = split_dirs.get(dir) {
            return Some(split_dir);
        }
        dir = parent_dir(dir)?;
    }
}

fn directory_contains_proper(parent: &[u8], child: &[u8]) -> bool {
    !parent.is_empty()
        && child.len() > parent.len()
        && child.starts_with(parent)
        && child[parent.len()] == b'/'
}

fn side_has_pure_add_under_dir(
    side: &MergeEntryMap,
    base_map: &MergeEntryMap,
    side_rename_src: &BTreeMap<&[u8], &[u8]>,
    dir: &[u8],
) -> bool {
    side.keys().any(|path| {
        path_is_under_dir(path, dir)
            && !base_map.contains_key(path)
            && !side_rename_src.contains_key(path.as_slice())
    })
}

fn path_is_under_dir(path: &[u8], dir: &[u8]) -> bool {
    !dir.is_empty() && path.len() > dir.len() && path.starts_with(dir) && path[dir.len()] == b'/'
}

/// Apply a side's planned re-home moves to all three effective maps.
///
/// `side_is_ours` says whether the moves originate from ours' (true) or theirs'
/// (false) paths — used both for `=conflict`-mode provenance and to decide which
/// side's entry the move primarily belongs to. A move whose source is a
/// content-merge path (present on the other side and in base too) re-homes
/// across `base`/`ours`/`theirs` together, so the 3-way merge follows it to the
/// new location; a pure add re-homes only its own side.
#[allow(clippy::too_many_arguments)]
fn apply_rehome_moves(
    original_base: &MergeEntryMap,
    file_rename_dests: &BTreeMap<Vec<u8>, MergeRename>,
    base: &mut MergeEntryMap,
    ours: &mut MergeEntryMap,
    theirs: &mut MergeEntryMap,
    moves: Vec<DirRenameMove>,
    side_is_ours: bool,
    rehomed: &mut BTreeMap<Vec<u8>, RehomeSides>,
    collisions: &mut Vec<DirRenameCollision>,
    back_to_self: &mut BTreeSet<Vec<u8>>,
    dirty: &mut bool,
) {
    for mv in moves {
        // A file in the way at the destination is only a blocker when it is
        // present on this same side (or in base). If the other side already
        // occupies the destination, applying this move produces the normal
        // two-sided conflict at that path (e.g. t6423 1d's rename/rename(2to1)).
        let occupied_on_this_side = if side_is_ours {
            ours.contains_key(&mv.to) || map_has_directory_at(ours, &mv.to)
        } else {
            theirs.contains_key(&mv.to) || map_has_directory_at(theirs, &mv.to)
        };
        let occupied_by_cross_rename =
            file_rename_dests
                .get(&mv.to)
                .is_some_and(|rename| match (side_is_ours, rename.side) {
                    (true, RenameSide::Theirs) | (false, RenameSide::Ours) => true,
                    (true, RenameSide::Ours) | (false, RenameSide::Theirs) => false,
                });
        let base_entry_at_dest = original_base.get(&mv.to).copied();
        let base_entry_at_source = original_base.get(&mv.from).copied();
        let other_side_entry_at_dest = if side_is_ours {
            theirs.get(&mv.to).copied()
        } else {
            ours.get(&mv.to).copied()
        };
        let other_side_entry_at_source = if side_is_ours {
            theirs.get(&mv.from).copied()
        } else {
            ours.get(&mv.from).copied()
        };
        let base_entry_for_shifted_source = base_entry_at_source.or(base_entry_at_dest);
        let rename_back_to_modified_source = mv
            .renamed_from
            .as_ref()
            .is_some_and(|source| source == &mv.to)
            && base_entry_at_dest.is_some()
            && (other_side_entry_at_dest.is_some_and(|entry| Some(entry) != base_entry_at_dest)
                || other_side_entry_at_source
                    .is_some_and(|entry| Some(entry) != base_entry_for_shifted_source));
        if ((base_entry_at_dest.is_some() && !rename_back_to_modified_source)
            || (occupied_on_this_side && !occupied_by_cross_rename))
            && mv.to != mv.from
        {
            *dirty = true;
            collisions.push(DirRenameCollision {
                dest: mv.to.clone(),
                sources: vec![mv.from.clone()],
            });
            continue;
        }
        let mut moved = false;
        if occupied_by_cross_rename {
            base.remove(&mv.from);
            if side_is_ours {
                if let Some(entry) = ours.remove(&mv.from) {
                    ours.insert(mv.to.clone(), entry);
                    moved = true;
                }
                theirs.remove(&mv.from);
            } else {
                ours.remove(&mv.from);
                if let Some(entry) = theirs.remove(&mv.from) {
                    theirs.insert(mv.to.clone(), entry);
                    moved = true;
                }
            }
        } else {
            // Move the path on every map that holds it (base for the ancestor,
            // and whichever sides carry content at the path). This keeps a
            // content-merge keyed consistently at the re-homed destination.
            for m in [&mut *base, &mut *ours, &mut *theirs] {
                if let Some(entry) = m.remove(&mv.from) {
                    m.insert(mv.to.clone(), entry);
                    moved = true;
                }
            }
        }
        if moved {
            if rename_back_to_modified_source {
                back_to_self.insert(mv.to.clone());
            }
            let info = RehomeInfo {
                old_path: mv.from.clone(),
                renamed_from: mv.renamed_from.clone(),
                added_on_ours: side_is_ours,
            };
            let entry = rehomed.entry(mv.to.clone()).or_default();
            if side_is_ours {
                entry.ours = Some(info);
            } else {
                entry.theirs = Some(info);
            }
        }
    }
}

fn collect_dir_rename_two_to_one(
    renames: &MergeRenames,
    rehomed: &BTreeMap<Vec<u8>, RehomeSides>,
) -> Vec<DirRenameTwoToOne> {
    let mut conflicts = Vec::new();
    for (dest, sides) in rehomed {
        let Some(file_rename) = renames.dest_to_source.get(dest) else {
            continue;
        };
        match file_rename.side {
            RenameSide::Ours => {
                let Some(info) = sides.theirs.as_ref() else {
                    continue;
                };
                let Some(theirs_source) = info.renamed_from.as_ref() else {
                    continue;
                };
                conflicts.push(DirRenameTwoToOne {
                    dest: dest.clone(),
                    ours_source: file_rename.source.clone(),
                    theirs_source: theirs_source.clone(),
                    ours_label_path: dest.clone(),
                    theirs_label_path: info.old_path.clone(),
                });
            }
            RenameSide::Theirs => {
                let Some(info) = sides.ours.as_ref() else {
                    continue;
                };
                let Some(ours_source) = info.renamed_from.as_ref() else {
                    continue;
                };
                conflicts.push(DirRenameTwoToOne {
                    dest: dest.clone(),
                    ours_source: ours_source.clone(),
                    theirs_source: file_rename.source.clone(),
                    ours_label_path: info.old_path.clone(),
                    theirs_label_path: dest.clone(),
                });
            }
        }
    }
    conflicts
}

fn map_has_directory_at(map: &MergeEntryMap, path: &[u8]) -> bool {
    let mut prefix = path.to_vec();
    prefix.push(b'/');
    map.keys().any(|candidate| candidate.starts_with(&prefix))
}

fn remap_rename_destinations(renames: &mut MergeRenames, rehomed: &BTreeMap<Vec<u8>, RehomeSides>) {
    if rehomed.is_empty() {
        return;
    }
    let mut remapped_deletes = BTreeMap::new();
    for (dest, rd) in std::mem::take(&mut renames.rename_deletes) {
        let new_dest = rehomed
            .iter()
            .find_map(|(new_dest, sides)| {
                let moved = sides
                    .ours
                    .as_ref()
                    .is_some_and(|info| info.old_path == dest)
                    || sides
                        .theirs
                        .as_ref()
                        .is_some_and(|info| info.old_path == dest);
                moved.then(|| new_dest.clone())
            })
            .unwrap_or(dest);
        remapped_deletes.insert(new_dest, rd);
    }
    renames.rename_deletes = remapped_deletes;

    for rename in renames.rename_rename_one_to_two.values_mut() {
        for (dest, sides) in rehomed {
            if sides
                .ours
                .as_ref()
                .is_some_and(|info| info.old_path == rename.ours_dest)
            {
                rename.ours_dest = dest.clone();
            }
            if sides
                .theirs
                .as_ref()
                .is_some_and(|info| info.old_path == rename.theirs_dest)
            {
                rename.theirs_dest = dest.clone();
            }
        }
    }
}

fn drop_collapsed_rename_rename_conflicts(renames: &mut MergeRenames) {
    renames
        .rename_rename_one_to_two
        .retain(|_, rename| rename.ours_dest != rename.theirs_dest);
}

fn apply_dir_rename_two_to_one_conflicts(
    db: &FileObjectDatabase,
    eff_ours: &MergeEntryMap,
    eff_theirs: &MergeEntryMap,
    conflicts: &[DirRenameTwoToOne],
    paths: &mut [MergedPath],
    leaves: &mut MergeEntryMap,
    options: &MergeTreesOptions<'_>,
) -> Result<()> {
    for conflict in conflicts {
        let Some(slot) = paths.iter_mut().find(|path| path.path == conflict.dest) else {
            continue;
        };
        let ours_entry = eff_ours.get(&conflict.dest).copied();
        let theirs_entry = eff_theirs.get(&conflict.dest).copied();
        let (Some((ours_mode, ours_oid)), Some((theirs_mode, theirs_oid))) =
            (ours_entry, theirs_entry)
        else {
            continue;
        };
        let ours_bytes = merge_blob_bytes(db, &ours_oid)?;
        let theirs_bytes = merge_blob_bytes(db, &theirs_oid)?;
        let (resolved_mode, mode_conflict) = merge_file_modes(None, ours_mode, theirs_mode);
        let favor = merge_favor_for_path(options, &conflict.dest);
        let result = if is_mergeable_file_mode(ours_mode) && is_mergeable_file_mode(theirs_mode) {
            merge_blobs(
                &[],
                &ours_bytes,
                &theirs_bytes,
                &MergeBlobOptions {
                    ours_label: &qualify_label(options.ours_label, &conflict.ours_label_path),
                    theirs_label: &qualify_label(options.theirs_label, &conflict.theirs_label_path),
                    base_label: options.ancestor_label,
                    style: options.style,
                    favor,
                    ws_ignore: options.ws_ignore,
                    marker_size: merge_marker_size_for_path(options, &conflict.dest),
                },
            )
        } else {
            MergeBlobResult {
                content: ours_bytes.clone(),
                conflicted: true,
            }
        };
        let oid = db.write_object(EncodedObject::new(ObjectType::Blob, result.content.clone()))?;
        leaves.insert(conflict.dest.clone(), (resolved_mode, oid));
        slot.stages = MergeStages {
            base: None,
            ours: ours_entry,
            theirs: theirs_entry,
        };
        slot.result = Some((resolved_mode, oid));
        slot.worktree = Some((
            if ours_mode == theirs_mode {
                ours_mode
            } else {
                0o100644
            },
            result.content,
        ));
        slot.conflict = Some(MergeConflictKind::RenameRenameTwoToOne {
            ours_path: conflict.ours_source.clone(),
            theirs_path: conflict.theirs_source.clone(),
        });
        slot.auto_merged = !mode_conflict;
    }
    Ok(())
}

/// 3-way merge one rename's content into a single leaf entry: `base` is the
/// source's ancestor blob, `ours`/`theirs` the two sides' content (one of which
/// is the renamed file, the other the other side's change to the source). Both
/// present and differing → a real content merge; otherwise the surviving side's
/// entry is carried as-is.
fn rename_merged_leaf(
    db: &FileObjectDatabase,
    base: Option<(u32, ObjectId)>,
    ours: Option<(u32, ObjectId)>,
    theirs: Option<(u32, ObjectId)>,
    path: &[u8],
    options: &MergeTreesOptions<'_>,
) -> Result<Option<(u32, ObjectId)>> {
    match (ours, theirs) {
        (None, None) => Ok(None),
        (Some(entry), None) | (None, Some(entry)) => Ok(Some(entry)),
        (Some((ours_mode, ours_oid)), Some((theirs_mode, theirs_oid))) => {
            if (ours_mode, ours_oid) == (theirs_mode, theirs_oid) {
                return Ok(Some((ours_mode, ours_oid)));
            }
            if !is_mergeable_file_mode(ours_mode) || !is_mergeable_file_mode(theirs_mode) {
                return Ok(Some((ours_mode, ours_oid)));
            }
            let base_bytes = match base {
                Some((_, oid)) => merge_blob_bytes(db, &oid)?,
                None => Vec::new(),
            };
            let favor = merge_favor_for_path(options, path);
            let result = merge_blobs(
                &base_bytes,
                &merge_blob_bytes(db, &ours_oid)?,
                &merge_blob_bytes(db, &theirs_oid)?,
                &MergeBlobOptions {
                    ours_label: options.ours_label,
                    theirs_label: options.theirs_label,
                    base_label: options.ancestor_label,
                    style: options.style,
                    favor,
                    ws_ignore: options.ws_ignore,
                    marker_size: merge_marker_size_for_path(options, path),
                },
            );
            let (mode, _) = merge_file_modes(base.map(|(mode, _)| mode), ours_mode, theirs_mode);
            let oid = db.write_object(EncodedObject::new(ObjectType::Blob, result.content))?;
            Ok(Some((mode, oid)))
        }
    }
}

/// Apply rename/rename(2to1) and rename/add conflicts: two distinct contents
/// land on one destination path. Each side's content at the destination is the
/// 3-way merge of its own rename (so the other side's change to the renamed
/// source follows the rename); the two results become stages 2 and 3 with no
/// common ancestor, and the worktree holds their two-way merge. The rename
/// source paths are consumed (removed from the path set) so they don't surface as
/// a spurious modify/delete.
#[allow(clippy::too_many_arguments)]
fn apply_rename_two_to_one_and_add_conflicts(
    db: &FileObjectDatabase,
    base_map: &MergeEntryMap,
    ours_map: &MergeEntryMap,
    theirs_map: &MergeEntryMap,
    renames: &MergeRenames,
    paths: &mut Vec<MergedPath>,
    leaves: &mut MergeEntryMap,
    options: &MergeTreesOptions<'_>,
) -> Result<()> {
    let mut consumed_sources: Vec<Vec<u8>> = Vec::new();

    for (dest, conflict) in &renames.rename_rename_two_to_one {
        // Ours renamed `ours_source`->dest; theirs' change to `ours_source`
        // follows the rename. Symmetric for theirs.
        let ours_leaf = rename_merged_leaf(
            db,
            base_map.get(&conflict.ours_source).copied(),
            ours_map.get(dest).copied(),
            theirs_map.get(&conflict.ours_source).copied(),
            dest,
            options,
        )?;
        let theirs_leaf = rename_merged_leaf(
            db,
            base_map.get(&conflict.theirs_source).copied(),
            ours_map.get(&conflict.theirs_source).copied(),
            theirs_map.get(dest).copied(),
            dest,
            options,
        )?;
        write_two_sided_dest_conflict(
            db,
            dest,
            ours_leaf,
            theirs_leaf,
            MergeConflictKind::RenameRenameTwoToOne {
                ours_path: conflict.ours_source.clone(),
                theirs_path: conflict.theirs_source.clone(),
            },
            options,
            paths,
            leaves,
        )?;
        consumed_sources.push(conflict.ours_source.clone());
        consumed_sources.push(conflict.theirs_source.clone());
    }

    for (dest, add) in &renames.rename_adds {
        let (ours_leaf, theirs_leaf) = match add.side {
            RenameSide::Ours => (
                rename_merged_leaf(
                    db,
                    base_map.get(&add.source).copied(),
                    ours_map.get(dest).copied(),
                    theirs_map.get(&add.source).copied(),
                    dest,
                    options,
                )?,
                theirs_map.get(dest).copied(),
            ),
            RenameSide::Theirs => (
                ours_map.get(dest).copied(),
                rename_merged_leaf(
                    db,
                    base_map.get(&add.source).copied(),
                    ours_map.get(&add.source).copied(),
                    theirs_map.get(dest).copied(),
                    dest,
                    options,
                )?,
            ),
        };
        write_two_sided_dest_conflict(
            db,
            dest,
            ours_leaf,
            theirs_leaf,
            MergeConflictKind::Content { add_add: true },
            options,
            paths,
            leaves,
        )?;
        consumed_sources.push(add.source.clone());
    }

    // The rename source paths are consumed by the rename: the other side's
    // change to them followed the rename to the destination, so they resolve to
    // a clean deletion (not the path-keyed core's modify/delete). Marking them
    // `Resolved(None)` lets the worktree writer remove the now-stale source file
    // rather than leaving it as a stray untracked file.
    for source in &consumed_sources {
        leaves.remove(source);
        if let Some(slot) = paths.iter_mut().find(|path| &path.path == source) {
            slot.stages = MergeStages::default();
            slot.result = None;
            slot.worktree = None;
            slot.conflict = None;
            slot.auto_merged = false;
        } else {
            paths.push(MergedPath {
                path: source.clone(),
                stages: MergeStages::default(),
                result: None,
                worktree: None,
                conflict: None,
                auto_merged: false,
            });
        }
    }
    Ok(())
}

/// Record a destination path that holds two unmerged contents (rename/rename
/// 2to1 or rename/add): stage 2 = `ours_leaf`, stage 3 = `theirs_leaf`, no
/// common ancestor, worktree = their two-way merge. Replaces any existing slot
/// (the path-keyed core's add/add result) for the destination.
#[allow(clippy::too_many_arguments)]
fn write_two_sided_dest_conflict(
    db: &FileObjectDatabase,
    dest: &[u8],
    ours_leaf: Option<(u32, ObjectId)>,
    theirs_leaf: Option<(u32, ObjectId)>,
    kind: MergeConflictKind,
    options: &MergeTreesOptions<'_>,
    paths: &mut Vec<MergedPath>,
    leaves: &mut MergeEntryMap,
) -> Result<()> {
    let ours_bytes = match ours_leaf {
        Some((mode, oid)) => Some((mode, merge_worktree_bytes(db, mode, &oid)?)),
        None => None,
    };
    let theirs_bytes = match theirs_leaf {
        Some((mode, oid)) => Some((mode, merge_worktree_bytes(db, mode, &oid)?)),
        None => None,
    };
    let (worktree_mode, worktree_content, result_leaf) = match (&ours_bytes, &theirs_bytes) {
        (Some((ours_mode, ours_content)), Some((theirs_mode, theirs_content))) => {
            let favor = merge_favor_for_path(options, dest);
            let merged = merge_blobs(
                &[],
                ours_content,
                theirs_content,
                &MergeBlobOptions {
                    ours_label: options.ours_label,
                    theirs_label: options.theirs_label,
                    base_label: options.ancestor_label,
                    style: options.style,
                    favor,
                    ws_ignore: options.ws_ignore,
                    marker_size: merge_marker_size_for_path(options, dest),
                },
            );
            let mode = if ours_mode == theirs_mode {
                *ours_mode
            } else {
                0o100644
            };
            let oid =
                db.write_object(EncodedObject::new(ObjectType::Blob, merged.content.clone()))?;
            (mode, merged.content, Some((mode, oid)))
        }
        (Some((mode, content)), None) | (None, Some((mode, content))) => {
            (*mode, content.clone(), ours_leaf.or(theirs_leaf))
        }
        (None, None) => (0o100644, Vec::new(), None),
    };

    let slot = MergedPath {
        path: dest.to_vec(),
        stages: MergeStages {
            base: None,
            ours: ours_leaf,
            theirs: theirs_leaf,
        },
        result: result_leaf,
        worktree: Some((worktree_mode, worktree_content)),
        conflict: Some(kind),
        auto_merged: true,
    };
    if let Some(existing) = paths.iter_mut().find(|path| path.path == dest) {
        *existing = slot;
    } else {
        paths.push(slot);
    }
    if let Some(leaf) = result_leaf {
        leaves.insert(dest.to_vec(), leaf);
    } else {
        leaves.remove(dest);
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn apply_rename_rename_one_to_two_conflicts(
    db: &FileObjectDatabase,
    base_map: &MergeEntryMap,
    eff_ours: &MergeEntryMap,
    eff_theirs: &MergeEntryMap,
    conflicts: &BTreeMap<Vec<u8>, RenameRenameOneToTwo>,
    paths: &mut Vec<MergedPath>,
    leaves: &mut MergeEntryMap,
    options: &MergeTreesOptions<'_>,
) -> Result<()> {
    for (old_path, conflict) in conflicts {
        let base_entry = base_map.get(old_path).copied();
        let ours_entry = eff_ours.get(&conflict.ours_dest).copied();
        let theirs_entry = eff_theirs.get(&conflict.theirs_dest).copied();
        let theirs_add_at_ours_dest = eff_theirs.get(&conflict.ours_dest).copied();
        let ours_add_at_theirs_dest = eff_ours.get(&conflict.theirs_dest).copied();

        leaves.remove(old_path);
        leaves.remove(&conflict.ours_dest);
        leaves.remove(&conflict.theirs_dest);
        paths.retain(|path| {
            path.path != *old_path
                && path.path != conflict.ours_dest
                && path.path != conflict.theirs_dest
        });

        paths.push(MergedPath {
            path: old_path.clone(),
            stages: MergeStages {
                base: base_entry,
                ours: None,
                theirs: None,
            },
            result: None,
            worktree: None,
            conflict: Some(MergeConflictKind::RenameRenameOneToTwo {
                old_path: old_path.clone(),
                ours_path: conflict.ours_dest.clone(),
                theirs_path: conflict.theirs_dest.clone(),
                ours_label: options.ours_label.to_string(),
                theirs_label: options.theirs_label.to_string(),
            }),
            auto_merged: false,
        });

        let ours_worktree = match ours_entry {
            Some((mode, oid)) => Some((mode, merge_worktree_bytes(db, mode, &oid)?)),
            None => None,
        };
        paths.push(MergedPath {
            path: conflict.ours_dest.clone(),
            stages: MergeStages {
                base: None,
                ours: ours_entry,
                theirs: theirs_add_at_ours_dest,
            },
            result: None,
            worktree: ours_worktree,
            conflict: Some(MergeConflictKind::RenameRenameOneToTwoStage),
            auto_merged: false,
        });

        let theirs_worktree = match theirs_entry {
            Some((mode, oid)) => Some((mode, merge_worktree_bytes(db, mode, &oid)?)),
            None => None,
        };
        paths.push(MergedPath {
            path: conflict.theirs_dest.clone(),
            stages: MergeStages {
                base: None,
                ours: ours_add_at_theirs_dest,
                theirs: theirs_entry,
            },
            result: None,
            worktree: theirs_worktree,
            conflict: Some(MergeConflictKind::RenameRenameOneToTwoStage),
            auto_merged: false,
        });
    }
    Ok(())
}

/// Build a path-qualified conflict-marker label `"<label>:<path>"`, as git does
/// for renamed files (so the two sides of a conflict name their distinct paths).
fn qualify_label(label: &str, path: &[u8]) -> String {
    format!("{label}:{}", String::from_utf8_lossy(path))
}

/// Adapt a flat `path -> (mode, oid)` map into the `TrackedEntry` map the
/// name-status diff core consumes.
fn entry_map_as_tracked(map: &MergeEntryMap) -> BTreeMap<Vec<u8>, TrackedEntry> {
    map.iter()
        .map(|(path, (mode, oid))| {
            (
                path.clone(),
                TrackedEntry {
                    mode: *mode,
                    oid: *oid,
                },
            )
        })
        .collect()
}

/// Build the flattened entry map of the *virtual ancestor* for a 3-way merge,
/// recursively merging the merge bases together (merge-recursive's criss-cross
/// construction). Conflicts in the virtual merge are folded into the tree rather
/// than surfaced — the outer merge owns conflict reporting.
///
/// `merge_bases` supplies the pairwise merge-base list for two commits (typically
/// [`sley_rev::merge_bases`] at the call site; kept injectable to avoid a
/// `sley-diff-merge` ↔ `sley-rev` dependency cycle).
pub fn virtual_ancestor_entry_map(
    db: &FileObjectDatabase,
    format: ObjectFormat,
    bases: &[ObjectId],
    merge_bases: impl Fn(&ObjectId, &ObjectId) -> Result<Vec<ObjectId>>,
) -> Result<MergeEntryMap> {
    let first = bases
        .first()
        .ok_or_else(|| GitError::Command("virtual ancestor needs at least one base".into()))?;
    let acc_tree = commit_tree_oid(db, format, first)?;
    let mut acc_map = flatten_tree(db, format, &acc_tree)?;
    let mut acc_commits = vec![*first];

    for base in &bases[1..] {
        let other_tree = commit_tree_oid(db, format, base)?;
        let other_map = flatten_tree(db, format, &other_tree)?;
        let sub_bases = merge_bases(&acc_commits[0], base)?;
        let sub_base_map = match sub_bases.first() {
            Some(sb) => {
                let sb_tree = commit_tree_oid(db, format, sb)?;
                flatten_tree(db, format, &sb_tree)?
            }
            None => MergeEntryMap::new(),
        };
        let merge = merge_entry_maps(
            db,
            format,
            &sub_base_map,
            &acc_map,
            &other_map,
            &MergeTreesOptions {
                ours_label: "Temporary merge branch 1",
                theirs_label: "Temporary merge branch 2",
                detect_renames: true,
                rename_threshold: DEFAULT_RENAME_THRESHOLD,
                ..Default::default()
            },
        )?;
        let mut next = MergeEntryMap::new();
        for entry in merge.paths {
            if let Some(leaf) = fold_virtual_ancestor_path(db, &entry)? {
                next.insert(entry.path, leaf);
            }
        }
        acc_map = next;
        acc_commits = vec![*base];
    }
    Ok(acc_map)
}

fn commit_tree_oid(
    db: &FileObjectDatabase,
    format: ObjectFormat,
    commit_oid: &ObjectId,
) -> Result<ObjectId> {
    let object = db.read_object(commit_oid)?;
    if object.object_type != ObjectType::Commit {
        return Err(GitError::InvalidObject(format!(
            "expected commit {commit_oid}, found {}",
            object.object_type.as_str()
        )));
    }
    Ok(Commit::parse_ref(format, &object.body)?.tree)
}

fn fold_virtual_ancestor_path(
    db: &FileObjectDatabase,
    entry: &MergedPath,
) -> Result<Option<(u32, ObjectId)>> {
    if entry.conflict.is_none() {
        return Ok(entry.result);
    }
    let base = entry.stages.base;
    let ours = entry.stages.ours;
    let theirs = entry.stages.theirs;
    if let Some((mode, _)) = entry.worktree.as_ref() {
        if is_symlink_mode(*mode) {
            return Ok(base);
        }
        if is_gitlink(*mode) {
            return Ok(theirs.or(ours));
        }
        if let Some((mode, bytes)) = &entry.worktree {
            let oid = db.write_object(EncodedObject::new(ObjectType::Blob, bytes.clone()))?;
            return Ok(Some((*mode, oid)));
        }
    }
    Ok(ours.or(theirs))
}