bazaar 3.5.4

Rust implementation of the Bazaar formats and protocols
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
//! Multi-parent diff representation.
//!
//! Port of the pure-logic pieces of `bzrformats/multiparent.py`: the
//! [`MultiParent`] container, its [`Hunk`] variants, and the patch
//! serialization format. Construction from line lists (which depends on
//! patiencediff) and the `VersionedFile` wrappers (which do I/O) remain in
//! Python for now.

/// One hunk of a multi-parent diff.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Hunk {
    /// Lines introduced by this text (not present in any parent).
    NewText(Vec<Vec<u8>>),
    /// A reference to a run of lines in one of the parent texts.
    ParentText {
        parent: usize,
        parent_pos: usize,
        child_pos: usize,
        num_lines: usize,
    },
}

/// A multi-parent diff: an ordered sequence of [`Hunk`]s.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MultiParent {
    pub hunks: Vec<Hunk>,
}

/// Error returned when [`MultiParent::from_patch`] fails to parse input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseError {
    /// A header line started with an unexpected byte.
    UnexpectedChar(u8),
    /// An `i N` or `c ...` header could not be parsed.
    BadHeader(Vec<u8>),
    /// A NewText header promised more lines than the input contained.
    Truncated,
    /// A `\n` continuation line appeared with no preceding NewText hunk.
    OrphanContinuation,
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseError::UnexpectedChar(c) => write!(f, "unexpected leading byte {:#x}", c),
            ParseError::BadHeader(h) => write!(f, "bad header line: {:?}", h),
            ParseError::Truncated => write!(f, "truncated patch"),
            ParseError::OrphanContinuation => write!(f, "continuation line with no NewText"),
        }
    }
}

impl std::error::Error for ParseError {}

/// Error returned when reconstructing a fulltext from a `MultiParent` diff fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReconstructError {
    /// A `ParentText` hunk references a parent slot that the version's parent
    /// list does not contain (typically because the caller fed the diff into a
    /// `MultiMemoryVersionedFile` with fewer parents than the diff was built
    /// against).
    ParentIndexOutOfRange {
        /// The parent slot the diff asked for.
        parent_index: usize,
        /// How many parents the version actually has.
        parent_count: usize,
    },
    /// Reconstruction was asked for a version that has no recorded diff.
    UnknownVersion,
}

impl std::fmt::Display for ReconstructError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ReconstructError::ParentIndexOutOfRange {
                parent_index,
                parent_count,
            } => write!(
                f,
                "parent index {} out of range (version has {} parents)",
                parent_index, parent_count
            ),
            ReconstructError::UnknownVersion => write!(f, "no diff recorded for requested version"),
        }
    }
}

impl std::error::Error for ReconstructError {}

impl MultiParent {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_hunks(hunks: Vec<Hunk>) -> Self {
        Self { hunks }
    }

    /// Build a [`MultiParent`] from `text` and per-parent matching blocks.
    ///
    /// Mirrors `MultiParent.from_lines` in `bzrformats/multiparent.py`. The
    /// caller computes each parent's `get_matching_blocks()` sequence
    /// (typically via patiencediff) and passes them here; this function owns
    /// the greedy longest-match selection loop.
    ///
    /// Each element of `parent_blocks` is the block list for parent `p`: a
    /// sequence of `(i, j, n)` triples where `i` is the offset in the parent,
    /// `j` the offset in `text`, and `n` the run length. The final sentinel
    /// block `(parent_len, text_len, 0)` may be present or absent — both
    /// shapes are accepted.
    pub fn from_lines_with_blocks(
        text: &[Vec<u8>],
        parent_blocks: &[Vec<(usize, usize, usize)>],
    ) -> Self {
        let mut hunks: Vec<Hunk> = Vec::new();
        let mut new_lines: Vec<Vec<u8>> = Vec::new();
        let mut iters: Vec<std::slice::Iter<'_, (usize, usize, usize)>> =
            parent_blocks.iter().map(|b| b.iter()).collect();
        // cur_block[p] tracks the next candidate block for parent p, or None
        // when the iterator is exhausted.
        let mut cur_block: Vec<Option<(usize, usize, usize)>> =
            iters.iter_mut().map(|it| it.next().copied()).collect();

        let mut cur_line = 0usize;
        while cur_line < text.len() {
            // Best match across parents: the longest ParentText we can anchor
            // at cur_line.
            let mut best: Option<(usize, usize, usize, usize)> = None; // (parent, parent_pos, child_pos, num_lines)
            for (p, slot) in cur_block.iter_mut().enumerate() {
                // Advance past blocks that end at or before cur_line.
                loop {
                    match *slot {
                        Some((_, j, n)) if j + n <= cur_line => {
                            *slot = iters[p].next().copied();
                        }
                        _ => break,
                    }
                }
                let Some((i, j, n)) = *slot else { continue };
                if j > cur_line {
                    continue;
                }
                let offset = cur_line - j;
                let i = i + offset;
                let j = cur_line;
                let n = n - offset;
                if n == 0 {
                    continue;
                }
                if best.is_none_or(|b| n > b.3) {
                    best = Some((p, i, j, n));
                }
            }
            match best {
                None => {
                    new_lines.push(text[cur_line].clone());
                    cur_line += 1;
                }
                Some((parent, parent_pos, child_pos, num_lines)) => {
                    if !new_lines.is_empty() {
                        hunks.push(Hunk::NewText(std::mem::take(&mut new_lines)));
                    }
                    hunks.push(Hunk::ParentText {
                        parent,
                        parent_pos,
                        child_pos,
                        num_lines,
                    });
                    cur_line += num_lines;
                }
            }
        }
        if !new_lines.is_empty() {
            hunks.push(Hunk::NewText(new_lines));
        }
        Self { hunks }
    }

    /// Build a [`MultiParent`] from `text` and its `parents`, computing each
    /// parent's matching-block sequence with patiencediff. `left_blocks` may
    /// be supplied to skip the diff against `parents[0]`.
    ///
    /// Mirrors `MultiParent.from_lines` in `bzrformats/multiparent.py`.
    pub fn from_lines(
        text: &[Vec<u8>],
        parents: &[&[Vec<u8>]],
        left_blocks: Option<Vec<(usize, usize, usize)>>,
    ) -> Self {
        if parents.is_empty() {
            return Self::from_lines_with_blocks(text, &[]);
        }
        let compare = |parent: &[Vec<u8>]| -> Vec<(usize, usize, usize)> {
            patiencediff::SequenceMatcher::new(parent, text)
                .get_matching_blocks()
                .to_vec()
        };
        let mut parent_blocks: Vec<Vec<(usize, usize, usize)>> = Vec::with_capacity(parents.len());
        parent_blocks.push(left_blocks.unwrap_or_else(|| compare(parents[0])));
        for p in &parents[1..] {
            parent_blocks.push(compare(p));
        }
        Self::from_lines_with_blocks(text, &parent_blocks)
    }

    /// Matching `(parent_pos, child_pos, num_lines)` triples between this
    /// diff and `parent` (its index into the parents list), plus a final
    /// sentinel `(parent_len, num_lines, 0)`.
    ///
    /// Mirrors `MultiParent.get_matching_blocks` — used by
    /// `VersionedFiles.add_mpdiffs` to pass the single-parent matching
    /// blocks straight into `add_lines` as a delta-compression hint.
    pub fn get_matching_blocks(
        &self,
        parent: usize,
        parent_len: usize,
    ) -> Vec<(usize, usize, usize)> {
        let mut out: Vec<(usize, usize, usize)> = Vec::new();
        for hunk in &self.hunks {
            if let Hunk::ParentText {
                parent: p,
                parent_pos,
                child_pos,
                num_lines,
            } = hunk
            {
                if *p == parent {
                    out.push((*parent_pos, *child_pos, *num_lines));
                }
            }
        }
        out.push((parent_len, self.num_lines(), 0));
        out
    }

    /// Total number of lines in the reconstructed text.
    ///
    /// Mirrors Python's `num_lines`: a trailing ParentText carries absolute
    /// positioning, so we scan from the end summing NewText lengths until we
    /// hit one.
    pub fn num_lines(&self) -> usize {
        let mut extra = 0usize;
        for hunk in self.hunks.iter().rev() {
            match hunk {
                Hunk::ParentText {
                    child_pos,
                    num_lines,
                    ..
                } => return child_pos + num_lines + extra,
                Hunk::NewText(lines) => extra += lines.len(),
            }
        }
        extra
    }

    /// True when this diff is effectively a fulltext (one NewText hunk).
    pub fn is_snapshot(&self) -> bool {
        matches!(self.hunks.as_slice(), [Hunk::NewText(_)])
    }

    /// The length in bytes of the gzip-compressed patch. Mirrors
    /// `MultiParent.zipped_patch_len`.
    pub fn zipped_patch_len(&self) -> usize {
        use std::io::Write;
        let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
        for chunk in self.to_patch() {
            // Writing to an in-memory Vec never fails.
            let _ = enc.write_all(&chunk);
        }
        enc.finish().map(|v| v.len()).unwrap_or(0)
    }

    /// Serialize to the patch wire format, yielding one byte chunk per line.
    pub fn to_patch(&self) -> Vec<Vec<u8>> {
        let mut out = Vec::new();
        for hunk in &self.hunks {
            match hunk {
                Hunk::NewText(lines) => {
                    out.push(format!("i {}\n", lines.len()).into_bytes());
                    for line in lines {
                        out.push(line.clone());
                    }
                    out.push(b"\n".to_vec());
                }
                Hunk::ParentText {
                    parent,
                    parent_pos,
                    child_pos,
                    num_lines,
                } => {
                    out.push(
                        format!("c {} {} {} {}\n", parent, parent_pos, child_pos, num_lines)
                            .into_bytes(),
                    );
                }
            }
        }
        out
    }

    /// Length in bytes of the serialized patch.
    pub fn patch_len(&self) -> usize {
        self.to_patch().iter().map(|l| l.len()).sum()
    }

    /// Parse a patch (as a single byte slice) back into a [`MultiParent`].
    pub fn from_patch(text: &[u8]) -> Result<Self, ParseError> {
        Self::from_patch_lines(split_lines(text))
    }

    fn from_patch_lines(lines: Vec<&[u8]>) -> Result<Self, ParseError> {
        let mut hunks: Vec<Hunk> = Vec::new();
        let mut i = 0;
        while i < lines.len() {
            let cur = lines[i];
            i += 1;
            let first = match cur.first().copied() {
                Some(c) => c,
                None => return Err(ParseError::BadHeader(cur.to_vec())),
            };
            match first {
                b'i' => {
                    let n = parse_usize_after_space(cur)?;
                    if i + n > lines.len() {
                        return Err(ParseError::Truncated);
                    }
                    let mut hunk_lines: Vec<Vec<u8>> =
                        lines[i..i + n].iter().map(|s| s.to_vec()).collect();
                    i += n;
                    // Python strips the trailing '\n' from the final inserted
                    // line; `to_patch` emits a bare '\n' separator afterwards,
                    // which round-trips back via the '\n' continuation branch.
                    if let Some(last) = hunk_lines.last_mut() {
                        if last.last() == Some(&b'\n') {
                            last.pop();
                        }
                    }
                    hunks.push(Hunk::NewText(hunk_lines));
                }
                b'\n' => match hunks.last_mut() {
                    Some(Hunk::NewText(lines)) => {
                        if let Some(last) = lines.last_mut() {
                            last.push(b'\n');
                        } else {
                            return Err(ParseError::OrphanContinuation);
                        }
                    }
                    _ => return Err(ParseError::OrphanContinuation),
                },
                b'c' => {
                    let (parent, parent_pos, child_pos, num_lines) = parse_c_header(cur)?;
                    hunks.push(Hunk::ParentText {
                        parent,
                        parent_pos,
                        child_pos,
                        num_lines,
                    });
                }
                other => return Err(ParseError::UnexpectedChar(other)),
            }
        }
        Ok(MultiParent { hunks })
    }

    /// Iterate the hunks alongside their `[start, end)` line ranges.
    ///
    /// Yields `(start, end, kind)` where kind is either the new lines or a
    /// reference tuple `(parent, parent_start, parent_end)`. Mirrors Python's
    /// `range_iterator`.
    pub fn range_iterator(&self) -> Vec<RangeItem<'_>> {
        let mut out = Vec::with_capacity(self.hunks.len());
        let mut start = 0usize;
        for hunk in &self.hunks {
            match hunk {
                Hunk::NewText(lines) => {
                    let end = start + lines.len();
                    out.push(RangeItem {
                        start,
                        end,
                        data: RangeData::New(lines),
                    });
                    start = end;
                }
                Hunk::ParentText {
                    parent,
                    parent_pos,
                    child_pos,
                    num_lines,
                } => {
                    let end = child_pos + num_lines;
                    out.push(RangeItem {
                        start: *child_pos,
                        end,
                        data: RangeData::Parent {
                            parent: *parent,
                            parent_start: *parent_pos,
                            parent_end: parent_pos + num_lines,
                        },
                    });
                    start = end;
                }
            }
        }
        out
    }

    /// Yield matching blocks for a specific parent, terminating with the
    /// conventional `(parent_len, child_len, 0)` sentinel.
    pub fn matching_blocks(&self, parent: usize, parent_len: usize) -> Vec<(usize, usize, usize)> {
        let mut out = Vec::new();
        for hunk in &self.hunks {
            if let Hunk::ParentText {
                parent: p,
                parent_pos,
                child_pos,
                num_lines,
            } = hunk
            {
                if *p == parent {
                    out.push((*parent_pos, *child_pos, *num_lines));
                }
            }
        }
        out.push((parent_len, self.num_lines(), 0));
        out
    }
}

/// Borrowed view of a single entry yielded by [`MultiParent::range_iterator`].
#[derive(Debug, PartialEq, Eq)]
pub struct RangeItem<'a> {
    pub start: usize,
    pub end: usize,
    pub data: RangeData<'a>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum RangeData<'a> {
    New(&'a [Vec<u8>]),
    Parent {
        parent: usize,
        parent_start: usize,
        parent_end: usize,
    },
}

/// Split bytes the same way Python's `BytesIO.readlines()` does: each line
/// keeps its trailing `\n`, except possibly the last.
fn split_lines(data: &[u8]) -> Vec<&[u8]> {
    let mut out = Vec::new();
    let mut start = 0;
    for (i, &b) in data.iter().enumerate() {
        if b == b'\n' {
            out.push(&data[start..=i]);
            start = i + 1;
        }
    }
    if start < data.len() {
        out.push(&data[start..]);
    }
    out
}

fn parse_usize_after_space(line: &[u8]) -> Result<usize, ParseError> {
    let rest = line
        .iter()
        .position(|&b| b == b' ')
        .map(|p| &line[p + 1..])
        .ok_or_else(|| ParseError::BadHeader(line.to_vec()))?;
    let end = rest
        .iter()
        .position(|&b| b == b' ' || b == b'\n')
        .unwrap_or(rest.len());
    std::str::from_utf8(&rest[..end])
        .ok()
        .and_then(|s| s.parse::<usize>().ok())
        .ok_or_else(|| ParseError::BadHeader(line.to_vec()))
}

fn parse_c_header(line: &[u8]) -> Result<(usize, usize, usize, usize), ParseError> {
    let trimmed = if line.last() == Some(&b'\n') {
        &line[..line.len() - 1]
    } else {
        line
    };
    let s = std::str::from_utf8(trimmed).map_err(|_| ParseError::BadHeader(line.to_vec()))?;
    let mut parts = s.split(' ');
    let tag = parts.next();
    if tag != Some("c") {
        return Err(ParseError::BadHeader(line.to_vec()));
    }
    let mut next_num = || -> Result<usize, ParseError> {
        parts
            .next()
            .and_then(|p| p.parse::<usize>().ok())
            .ok_or_else(|| ParseError::BadHeader(line.to_vec()))
    };
    let parent = next_num()?;
    let parent_pos = next_num()?;
    let child_pos = next_num()?;
    let num_lines = next_num()?;
    if parts.next().is_some() {
        return Err(ParseError::BadHeader(line.to_vec()));
    }
    Ok((parent, parent_pos, child_pos, num_lines))
}

/// Gzip-compress `lines` into a single gzip container. Mirrors
/// `multiparent.gzip_string`.
pub fn gzip_string<'a>(lines: impl IntoIterator<Item = &'a [u8]>) -> Vec<u8> {
    use std::io::Write;
    let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
    for line in lines {
        // Writing to an in-memory Vec never fails.
        let _ = enc.write_all(line);
    }
    enc.finish().unwrap_or_default()
}

/// Topologically sort `versions` given a `parents` mapping.
///
/// Port of `multiparent._topo_iter`. `parents[v]` is either `Some(parents)`
/// or `None` for a "parentless" sentinel (treated as having no parents).
/// Keys in `parents` not present in `versions` are ignored when counting
/// pending predecessors. Returns versions in an order where every version
/// appears after its parents that are also in the input set.
///
/// Input ordering of `versions` is used as a tiebreaker so the output is
/// deterministic. Duplicate entries in `versions` are emitted only once.
pub fn topo_iter<K>(
    parents: &std::collections::HashMap<K, Option<Vec<K>>>,
    versions: &[K],
) -> Vec<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    let mut version_order: Vec<K> = Vec::with_capacity(versions.len());
    let mut version_set: std::collections::HashSet<K> = std::collections::HashSet::new();
    for v in versions {
        if version_set.insert(v.clone()) {
            version_order.push(v.clone());
        }
    }

    let mut seen: std::collections::HashSet<K> = std::collections::HashSet::new();
    let mut descendants: std::collections::HashMap<K, Vec<K>> = std::collections::HashMap::new();

    let pending_count = |v: &K, seen: &std::collections::HashSet<K>| -> usize {
        match parents.get(v) {
            Some(Some(ps)) => ps
                .iter()
                .filter(|p| version_set.contains(*p) && !seen.contains(*p))
                .count(),
            _ => 0,
        }
    };

    for v in &version_order {
        if let Some(Some(ps)) = parents.get(v) {
            for p in ps {
                descendants.entry(p.clone()).or_default().push(v.clone());
            }
        }
    }

    let mut cur: Vec<K> = version_order
        .iter()
        .filter(|v| pending_count(v, &seen) == 0)
        .cloned()
        .collect();

    let mut out: Vec<K> = Vec::new();
    while !cur.is_empty() {
        let mut next: Vec<K> = Vec::new();
        for v in &cur {
            if seen.contains(v) {
                continue;
            }
            if pending_count(v, &seen) != 0 {
                continue;
            }
            if let Some(ds) = descendants.get(v) {
                next.extend(ds.iter().cloned());
            }
            out.push(v.clone());
            seen.insert(v.clone());
        }
        cur = next;
    }
    out
}

/// In-memory `BaseVersionedFile`/`MultiMemoryVersionedFile` analogue.
///
/// Holds an mpdiff per version together with its parent keys, and can
/// reconstruct any version's fulltext lines by walking the chain (cached
/// in `_lines`). Mirrors the subset of `BaseVersionedFile` /
/// `MultiMemoryVersionedFile` that `VersionedFiles.add_mpdiffs` exercises:
/// `add_diff`, `add_version`, `has_version`, `get_diff`, `get_line_list`.
///
/// Snapshot bookkeeping, size ranking, build ranking, import_versionedfile
/// and the other helpers from the Python `BaseVersionedFile` are not
/// ported — `add_mpdiffs` doesn't use them, and they'd nearly double the
/// pyo3 surface for no current caller.
pub struct MultiMemoryVersionedFile<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    diffs: std::collections::HashMap<K, MultiParent>,
    parents: std::collections::HashMap<K, Vec<K>>,
    lines_cache: std::collections::HashMap<K, Vec<Vec<u8>>>,
    snapshots: std::collections::HashSet<K>,
    snapshot_interval: Option<usize>,
    max_snapshots: Option<usize>,
    /// Preserves insertion order so `versions()` yields the same sequence
    /// as Python's `iter(self._parents)`, which dicts preserve insertion
    /// order for.
    insert_order: Vec<K>,
}

impl<K> Default for MultiMemoryVersionedFile<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    fn default() -> Self {
        Self::new(Some(25), None)
    }
}

impl<K> MultiMemoryVersionedFile<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    pub fn new(snapshot_interval: Option<usize>, max_snapshots: Option<usize>) -> Self {
        Self {
            diffs: std::collections::HashMap::new(),
            parents: std::collections::HashMap::new(),
            lines_cache: std::collections::HashMap::new(),
            snapshots: std::collections::HashSet::new(),
            snapshot_interval,
            max_snapshots,
            insert_order: Vec::new(),
        }
    }

    pub fn has_version(&self, version: &K) -> bool {
        self.parents.contains_key(version)
    }

    pub fn get_diff(&self, version: &K) -> Option<&MultiParent> {
        self.diffs.get(version)
    }

    pub fn get_parents(&self, version: &K) -> Option<&[K]> {
        self.parents.get(version).map(Vec::as_slice)
    }

    /// Park `diff` against `version_id`, with the given parent keys. No
    /// snapshot decision is made; the lines cache is not touched. Mirrors
    /// `MultiMemoryVersionedFile.add_diff`.
    pub fn add_diff(&mut self, diff: MultiParent, version_id: K, parent_ids: Vec<K>) {
        if !self.parents.contains_key(&version_id) {
            self.insert_order.push(version_id.clone());
        }
        self.diffs.insert(version_id.clone(), diff);
        self.parents.insert(version_id, parent_ids);
    }

    /// Add a version (with fulltext `lines`). Decides whether to record as
    /// a snapshot (`NewText`) or as a multiparent delta. Mirrors
    /// `BaseVersionedFile.add_version`; `force_snapshot=None` means use
    /// `do_snapshot`, `single_parent` controls whether to diff against
    /// only the first parent.
    pub fn add_version(
        &mut self,
        lines: Vec<Vec<u8>>,
        version_id: K,
        parent_ids: Vec<K>,
        force_snapshot: Option<bool>,
        single_parent: bool,
    ) -> Result<(), ReconstructError> {
        let take_snapshot =
            force_snapshot.unwrap_or_else(|| self.do_snapshot(&version_id, &parent_ids));
        let diff = if take_snapshot {
            self.snapshots.insert(version_id.clone());
            MultiParent::with_hunks(vec![Hunk::NewText(lines.clone())])
        } else {
            let parents_slice: &[K] = if single_parent {
                &parent_ids[..parent_ids.len().min(1)]
            } else {
                &parent_ids[..]
            };
            let parent_lines = self.get_line_list_owned(parents_slice)?;
            let parent_refs: Vec<&[Vec<u8>]> = parent_lines.iter().map(Vec::as_slice).collect();
            let d = MultiParent::from_lines(&lines, &parent_refs, None);
            if d.is_snapshot() {
                self.snapshots.insert(version_id.clone());
            }
            d
        };
        self.add_diff(diff, version_id.clone(), parent_ids);
        self.lines_cache.insert(version_id, lines);
        Ok(())
    }

    /// Mirror of `BaseVersionedFile.do_snapshot`: walk back
    /// `snapshot_interval` levels; if the chain reaches a snapshot in that
    /// many steps, no need to record this one.
    pub fn do_snapshot(&self, _version_id: &K, parent_ids: &[K]) -> bool {
        let Some(interval) = self.snapshot_interval else {
            return false;
        };
        if let Some(max) = self.max_snapshots {
            if self.snapshots.len() == max {
                return false;
            }
        }
        if parent_ids.is_empty() {
            return true;
        }
        let mut frontier: Vec<K> = parent_ids.to_vec();
        for _ in 0..interval {
            if frontier.is_empty() {
                return false;
            }
            let current = std::mem::take(&mut frontier);
            for v in current {
                if !self.snapshots.contains(&v) {
                    if let Some(ps) = self.parents.get(&v) {
                        frontier.extend(ps.iter().cloned());
                    }
                }
            }
        }
        true
    }

    /// Get the reconstructed lines for each version in `version_ids`,
    /// caching as we go. Mirrors `BaseVersionedFile.get_line_list`.
    pub fn get_line_list(
        &mut self,
        version_ids: &[K],
    ) -> Result<Vec<Vec<Vec<u8>>>, ReconstructError> {
        version_ids
            .iter()
            .map(|v| self.cache_version(v).map(<[Vec<u8>]>::to_vec))
            .collect()
    }

    fn get_line_list_owned(
        &mut self,
        version_ids: &[K],
    ) -> Result<Vec<Vec<Vec<u8>>>, ReconstructError> {
        self.get_line_list(version_ids)
    }

    /// Reconstruct a version's fulltext (caching the result) and return a
    /// reference into the cache. Returns [`ReconstructError`] if the diff
    /// references a parent index outside the version's parent list.
    pub fn cache_version(&mut self, version_id: &K) -> Result<&[Vec<u8>], ReconstructError> {
        if !self.lines_cache.contains_key(version_id) {
            let length = self
                .diffs
                .get(version_id)
                .map(MultiParent::num_lines)
                .unwrap_or(0);
            let mut lines: Vec<Vec<u8>> = Vec::with_capacity(length);
            self.reconstruct(&mut lines, version_id.clone(), 0, length)?;
            self.lines_cache.insert(version_id.clone(), lines);
        }
        Ok(self
            .lines_cache
            .get(version_id)
            .expect("just inserted above")
            .as_slice())
    }

    /// Append lines for `[req_start, req_end)` of `req_version_id` to `out`.
    ///
    /// Iterative port of `_Reconstructor._reconstruct`: walks the diff
    /// chain backward, splitting a range across hunk boundaries when
    /// necessary. Each ParentText hunk is rewritten as a fresh range
    /// request against the parent and pushed onto a pending stack.
    fn reconstruct(
        &mut self,
        out: &mut Vec<Vec<u8>>,
        req_version_id: K,
        req_start: usize,
        req_end: usize,
    ) -> Result<(), ReconstructError> {
        if req_start == req_end {
            return Ok(());
        }
        let mut pending: Vec<(K, usize, usize)> = vec![(req_version_id, req_start, req_end)];
        while let Some((version_id, req_start, req_end)) = pending.pop() {
            if let Some(cached) = self.lines_cache.get(&version_id) {
                out.extend_from_slice(&cached[req_start..req_end]);
                continue;
            }
            let diff = self
                .diffs
                .get(&version_id)
                .ok_or(ReconstructError::UnknownVersion)?;
            let ranges = diff.range_iterator();
            let mut idx = 0;
            while idx < ranges.len() && ranges[idx].end <= req_start {
                idx += 1;
            }
            if idx == ranges.len() {
                continue;
            }
            let hunk = &ranges[idx];
            let mut req_end = req_end;
            if req_end > hunk.end {
                pending.push((version_id.clone(), hunk.end, req_end));
                req_end = hunk.end;
            }
            match &hunk.data {
                RangeData::New(lines) => {
                    let local_start = req_start - hunk.start;
                    let local_end = req_end - hunk.start;
                    out.extend(lines[local_start..local_end].iter().cloned());
                }
                RangeData::Parent {
                    parent,
                    parent_start,
                    parent_end,
                } => {
                    let parents = self.parents.get(&version_id);
                    let parent_count = parents.map(Vec::len).unwrap_or(0);
                    let parent_key = parents
                        .and_then(|ps| ps.get(*parent))
                        .ok_or(ReconstructError::ParentIndexOutOfRange {
                            parent_index: *parent,
                            parent_count,
                        })?
                        .clone();
                    let new_start = parent_start + req_start - hunk.start;
                    let new_end = parent_end + req_end - hunk.end;
                    pending.push((parent_key, new_start, new_end));
                }
            }
        }
        Ok(())
    }

    pub fn versions(&self) -> impl Iterator<Item = &K> {
        self.insert_order.iter()
    }

    /// Read-only access to the parent map (version -> list of parent keys).
    pub fn parents_map(&self) -> &std::collections::HashMap<K, Vec<K>> {
        &self.parents
    }

    /// Read-only access to the lines cache (version -> reconstructed
    /// fulltext lines). A version only appears here after it has been
    /// reconstructed at least once, or seeded by `add_version`.
    pub fn lines_cache(&self) -> &std::collections::HashMap<K, Vec<Vec<u8>>> {
        &self.lines_cache
    }

    /// Snapshot set (versions stored as `NewText` instead of a delta).
    pub fn snapshots(&self) -> &std::collections::HashSet<K> {
        &self.snapshots
    }

    /// Whether `version` is a recorded snapshot.
    pub fn is_snapshot(&self, version: &K) -> bool {
        self.snapshots.contains(version)
    }

    pub fn clear_cache(&mut self) {
        self.lines_cache.clear();
    }

    /// Replace a version's existing diff with a fulltext snapshot of its
    /// reconstructed lines. Mirrors `BaseVersionedFile.make_snapshot`.
    pub fn make_snapshot(&mut self, version_id: K) -> Result<(), ReconstructError> {
        let lines = self.cache_version(&version_id)?.to_vec();
        let parents = self.parents.get(&version_id).cloned().unwrap_or_default();
        let snap = MultiParent::with_hunks(vec![Hunk::NewText(lines)]);
        self.add_diff(snap, version_id.clone(), parents);
        self.snapshots.insert(version_id);
        Ok(())
    }

    /// Like `BaseVersionedFile.import_diffs`: copy every version's diff +
    /// parent list from `other` into `self` (without recomputing).
    pub fn import_diffs(&mut self, other: &Self) {
        for v in other.versions() {
            if let (Some(d), Some(p)) = (other.get_diff(v), other.get_parents(v)) {
                self.add_diff(d.clone(), v.clone(), p.to_vec());
            }
        }
    }

    /// Versions ranked by `(snapshot_len - delta_len)` ascending — the
    /// negative end is the cheapest to snapshot. Mirrors
    /// `BaseVersionedFile.get_size_ranking`. Snapshot versions are
    /// skipped.
    pub fn get_size_ranking(&mut self) -> Result<Vec<(isize, K)>, ReconstructError> {
        let versions: Vec<K> = self.insert_order.clone();
        let mut out: Vec<(isize, K)> = Vec::new();
        for v in &versions {
            if self.snapshots.contains(v) {
                continue;
            }
            let diff_len = self
                .diffs
                .get(v)
                .map(|d| d.to_patch().iter().map(Vec::len).sum::<usize>())
                .unwrap_or(0);
            let lines = self.cache_version(v)?.to_vec();
            let snap = MultiParent::with_hunks(vec![Hunk::NewText(lines)]);
            let snap_len: usize = snap.to_patch().iter().map(Vec::len).sum();
            out.push((snap_len as isize - diff_len as isize, v.clone()));
        }
        out.sort_by(|a, b| a.0.cmp(&b.0));
        Ok(out)
    }

    /// Select new snapshots to drop the output size below `num` total
    /// snapshots. Returns the versions to snapshot. Mirrors
    /// `BaseVersionedFile.select_by_size` — picks the last `num` entries
    /// from the size ranking.
    pub fn select_by_size(&mut self, num: usize) -> Result<Vec<K>, ReconstructError> {
        let needed = num.saturating_sub(self.snapshots.len());
        let ranking = self.get_size_ranking()?;
        Ok(ranking
            .into_iter()
            .rev()
            .take(needed)
            .map(|(_, v)| v)
            .collect())
    }

    /// Select which versions to add as snapshots given the chain depth
    /// from each version to its nearest snapshot ancestor. Mirrors
    /// `BaseVersionedFile.select_snapshots`.
    pub fn select_snapshots(&self) -> std::collections::HashSet<K> {
        let interval = self.snapshot_interval.unwrap_or(usize::MAX);
        // Topo-walk via the existing topo_iter helper.
        let parents_map: std::collections::HashMap<K, Option<Vec<K>>> = self
            .parents
            .iter()
            .map(|(k, v)| (k.clone(), Some(v.clone())))
            .collect();
        let order: Vec<K> = topo_iter(&parents_map, &self.insert_order);
        let mut build_ancestors: std::collections::HashMap<K, std::collections::HashSet<K>> =
            std::collections::HashMap::new();
        let mut snapshots: std::collections::HashSet<K> = std::collections::HashSet::new();
        for version_id in &order {
            let parents = self.parents.get(version_id).cloned().unwrap_or_default();
            let mut potential: std::collections::HashSet<K> = parents.iter().cloned().collect();
            if parents.is_empty() {
                snapshots.insert(version_id.clone());
                build_ancestors.insert(version_id.clone(), std::collections::HashSet::new());
            } else {
                for p in &parents {
                    if let Some(set) = build_ancestors.get(p) {
                        potential.extend(set.iter().cloned());
                    }
                }
                if potential.len() > interval {
                    snapshots.insert(version_id.clone());
                    build_ancestors.insert(version_id.clone(), std::collections::HashSet::new());
                } else {
                    build_ancestors.insert(version_id.clone(), potential);
                }
            }
        }
        snapshots
    }

    /// Rank versions by how much their snapshot status reduces overall
    /// build complexity. Mirrors `BaseVersionedFile.get_build_ranking`.
    pub fn get_build_ranking(&self) -> Vec<K> {
        let mut could_avoid: std::collections::HashMap<K, std::collections::HashSet<K>> =
            std::collections::HashMap::new();
        let mut referenced_by: std::collections::HashMap<K, std::collections::HashSet<K>> =
            std::collections::HashMap::new();
        let parents_map: std::collections::HashMap<K, Option<Vec<K>>> = self
            .parents
            .iter()
            .map(|(k, v)| (k.clone(), Some(v.clone())))
            .collect();
        let order: Vec<K> = topo_iter(&parents_map, &self.insert_order);
        for v in &order {
            could_avoid.insert(v.clone(), std::collections::HashSet::new());
            if !self.snapshots.contains(v) {
                let parents = self.parents.get(v).cloned().unwrap_or_default();
                for p in &parents {
                    if let Some(set) = could_avoid.get(p).cloned() {
                        could_avoid.get_mut(v).unwrap().extend(set);
                    }
                }
                let all_known: Vec<K> = self.parents.keys().cloned().collect();
                could_avoid.get_mut(v).unwrap().extend(all_known);
                could_avoid.get_mut(v).unwrap().remove(v);
            }
            let avoid_set = could_avoid.get(v).cloned().unwrap_or_default();
            for avoid_id in avoid_set {
                referenced_by.entry(avoid_id).or_default().insert(v.clone());
            }
        }
        let mut available: Vec<K> = self.insert_order.clone();
        let mut ranking: Vec<K> = Vec::new();
        while !available.is_empty() {
            available.sort_by_key(|x| {
                could_avoid.get(x).map(|s| s.len()).unwrap_or(0)
                    * referenced_by.get(x).map(|s| s.len()).unwrap_or(0)
            });
            let selected = available.pop().expect("non-empty checked above");
            ranking.push(selected.clone());
            let selected_refs = referenced_by.get(&selected).cloned().unwrap_or_default();
            let selected_avoid = could_avoid.get(&selected).cloned().unwrap_or_default();
            for v in &selected_refs {
                if let Some(set) = could_avoid.get_mut(v) {
                    for r in &selected_avoid {
                        set.remove(r);
                    }
                }
            }
            for v in &selected_avoid {
                if let Some(set) = referenced_by.get_mut(v) {
                    for r in &selected_refs {
                        set.remove(r);
                    }
                }
            }
        }
        ranking
    }

    pub fn snapshot_interval(&self) -> Option<usize> {
        self.snapshot_interval
    }

    pub fn max_snapshots(&self) -> Option<usize> {
        self.max_snapshots
    }

    /// The set of version ids currently recorded as snapshots.
    pub fn snapshots_set(&self) -> &std::collections::HashSet<K> {
        &self.snapshots
    }

    /// Record `version_id` as a snapshot without recomputing its diff. Used
    /// when restoring state (e.g. loading a disk index).
    pub fn mark_snapshot(&mut self, version_id: K) {
        self.snapshots.insert(version_id);
    }
}

/// Error from a [`DiskMultiVersionedFile`] operation: either reconstruction
/// failed or the underlying disk I/O failed.
#[derive(Debug)]
pub enum DiskError {
    Reconstruct(ReconstructError),
    Io(std::io::Error),
}

impl std::fmt::Display for DiskError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DiskError::Reconstruct(e) => write!(f, "{}", e),
            DiskError::Io(e) => write!(f, "{}", e),
        }
    }
}

impl std::error::Error for DiskError {}

impl From<ReconstructError> for DiskError {
    fn from(e: ReconstructError) -> Self {
        DiskError::Reconstruct(e)
    }
}

impl From<std::io::Error> for DiskError {
    fn from(e: std::io::Error) -> Self {
        DiskError::Io(e)
    }
}

/// Disk-backed multi-parent versioned file, ported from
/// `bzrformats.multiparent.MultiVersionedFile`.
///
/// Diffs are appended to `<filename>.mpknit` as independent gzip members (each
/// prefixed with a `version <id>\n` line) and the parents/snapshots/offsets
/// index is bencoded to `<filename>.mpidx`. An in-memory
/// [`MultiMemoryVersionedFile`] holds the live diffs so reconstruction reuses
/// the shared engine; `load` repopulates it by reading every diff off disk.
pub struct DiskMultiVersionedFile {
    filename: String,
    mem: MultiMemoryVersionedFile<Vec<u8>>,
    /// version id -> (byte offset, byte length) of its gzip member in .mpknit
    diff_offset: std::collections::HashMap<Vec<u8>, (u64, u64)>,
}

impl DiskMultiVersionedFile {
    pub fn new(
        filename: String,
        snapshot_interval: Option<usize>,
        max_snapshots: Option<usize>,
    ) -> Self {
        Self {
            filename,
            mem: MultiMemoryVersionedFile::new(snapshot_interval, max_snapshots),
            diff_offset: std::collections::HashMap::new(),
        }
    }

    fn knit_path(&self) -> String {
        format!("{}.mpknit", self.filename)
    }

    fn idx_path(&self) -> String {
        format!("{}.mpidx", self.filename)
    }

    /// Append `diff` for `version_id` to the .mpknit file as a gzip member and
    /// record its offset. Mirrors `MultiVersionedFile.add_diff`.
    fn write_diff_to_disk(&mut self, diff: &MultiParent, version_id: &[u8]) -> std::io::Result<()> {
        use std::io::{Seek, SeekFrom, Write};
        let mut outfile = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(self.knit_path())?;
        let start = outfile.seek(SeekFrom::End(0))?;
        {
            let mut enc =
                flate2::write::GzEncoder::new(&mut outfile, flate2::Compression::default());
            enc.write_all(b"version ")?;
            enc.write_all(version_id)?;
            enc.write_all(b"\n")?;
            for chunk in diff.to_patch() {
                enc.write_all(&chunk)?;
            }
            enc.finish()?;
        }
        let end = outfile.seek(SeekFrom::End(0))?;
        self.diff_offset
            .insert(version_id.to_vec(), (start, end - start));
        Ok(())
    }

    /// Add a fulltext version: compute its diff against parents (deciding
    /// snapshots), store it in the in-memory VF and append it to disk.
    pub fn add_version(
        &mut self,
        lines: Vec<Vec<u8>>,
        version_id: Vec<u8>,
        parent_ids: Vec<Vec<u8>>,
        force_snapshot: Option<bool>,
        single_parent: bool,
    ) -> Result<(), DiskError> {
        self.mem.add_version(
            lines,
            version_id.clone(),
            parent_ids,
            force_snapshot,
            single_parent,
        )?;
        let diff = self.mem.get_diff(&version_id).expect("just added").clone();
        self.write_diff_to_disk(&diff, &version_id)?;
        Ok(())
    }

    /// Reconstruct the fulltext line lists for `version_ids`.
    pub fn get_line_list(
        &mut self,
        version_ids: &[Vec<u8>],
    ) -> Result<Vec<Vec<Vec<u8>>>, ReconstructError> {
        self.mem.get_line_list(version_ids)
    }

    /// Read a single diff back from the .mpknit file.
    pub fn read_diff_from_disk(&self, version_id: &[u8]) -> std::io::Result<MultiParent> {
        use std::io::{Read, Seek, SeekFrom};
        let (start, count) = *self
            .diff_offset
            .get(version_id)
            .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "unknown version"))?;
        let mut infile = std::fs::File::open(self.knit_path())?;
        infile.seek(SeekFrom::Start(start))?;
        let mut buf = vec![0u8; count as usize];
        infile.read_exact(&mut buf)?;
        let mut dec = flate2::read::GzDecoder::new(&buf[..]);
        let mut content = Vec::new();
        dec.read_to_end(&mut content)?;
        // Drop the leading `version <id>\n` header line.
        let body = match content.iter().position(|&b| b == b'\n') {
            Some(i) => &content[i + 1..],
            None => &content[..],
        };
        MultiParent::from_patch(body)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))
    }

    /// Persist the parents/snapshots/offsets index to the .mpidx file as a
    /// bencoded `(parents, snapshots, diff_offset)` tuple, matching the
    /// `fastbencode` layout the Python implementation wrote.
    pub fn save(&self) -> std::io::Result<()> {
        let data = self.encode_index();
        std::fs::write(self.idx_path(), data)
    }

    /// Load the index from .mpidx and repopulate the in-memory VF by reading
    /// every diff back off the .mpknit file.
    pub fn load(&mut self) -> std::io::Result<()> {
        let data = std::fs::read(self.idx_path())?;
        let (parents, snapshots, diff_offset) = Self::decode_index(&data)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
        self.diff_offset = diff_offset;
        let mut mem: MultiMemoryVersionedFile<Vec<u8>> =
            MultiMemoryVersionedFile::new(self.mem.snapshot_interval(), self.mem.max_snapshots());
        // Re-add each diff in the on-disk order so reconstruction has them.
        for (version_id, parent_ids) in &parents {
            let diff = self.read_diff_from_disk(version_id)?;
            mem.add_diff(diff, version_id.clone(), parent_ids.clone());
        }
        for snap in snapshots {
            mem.mark_snapshot(snap);
        }
        self.mem = mem;
        Ok(())
    }

    /// Remove the .mpknit and .mpidx files from disk.
    pub fn destroy(&self) -> std::io::Result<()> {
        for path in [self.knit_path(), self.idx_path()] {
            match std::fs::remove_file(&path) {
                Ok(()) => {}
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }

    /// Bencode the `(parents, snapshots, diff_offset)` index.
    ///
    /// `parents` is a dict version_id -> [parent_id, ...]; `snapshots` is a
    /// list of version_ids; `diff_offset` is a dict version_id ->
    /// [start, length]. Dict keys are emitted in sorted order, as bencode
    /// requires.
    fn encode_index(&self) -> Vec<u8> {
        use bendy::encoding::Encoder;
        let mut parents: Vec<(Vec<u8>, Vec<Vec<u8>>)> = self
            .mem
            .parents_map()
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        parents.sort_by(|a, b| a.0.cmp(&b.0));
        let mut offsets: Vec<(Vec<u8>, (u64, u64))> = self
            .diff_offset
            .iter()
            .map(|(k, v)| (k.clone(), *v))
            .collect();
        offsets.sort_by(|a, b| a.0.cmp(&b.0));
        let mut snapshots: Vec<Vec<u8>> = self.mem.snapshots_set().iter().cloned().collect();
        snapshots.sort();

        let mut e = Encoder::new();
        e.emit_list(|list| {
            // parents dict
            list.emit_dict(|mut d| {
                for (k, v) in &parents {
                    d.emit_pair_with(k, |e| {
                        e.emit_list(|l| {
                            for p in v {
                                l.emit_bytes(p)?;
                            }
                            Ok(())
                        })
                    })?;
                }
                Ok(())
            })?;
            // snapshots list
            list.emit_list(|l| {
                for s in &snapshots {
                    l.emit_bytes(s)?;
                }
                Ok(())
            })?;
            // diff_offset dict
            list.emit_dict(|mut d| {
                for (k, (start, len)) in &offsets {
                    d.emit_pair_with(k, |e| {
                        e.emit_list(|l| {
                            l.emit_int(*start)?;
                            l.emit_int(*len)?;
                            Ok(())
                        })
                    })?;
                }
                Ok(())
            })?;
            Ok(())
        })
        .expect("bencode index");
        e.get_output().expect("bencode index")
    }

    #[allow(clippy::type_complexity)]
    fn decode_index(
        data: &[u8],
    ) -> Result<
        (
            Vec<(Vec<u8>, Vec<Vec<u8>>)>,
            Vec<Vec<u8>>,
            std::collections::HashMap<Vec<u8>, (u64, u64)>,
        ),
        String,
    > {
        use bendy::decoding::{Decoder, Object};
        let mut decoder = Decoder::new(data);
        let mut top = match decoder.next_object().map_err(|e| e.to_string())? {
            Some(Object::List(l)) => l,
            _ => return Err("index is not a bencode list".to_string()),
        };
        // parents dict
        let mut parents: Vec<(Vec<u8>, Vec<Vec<u8>>)> = Vec::new();
        match top.next_object().map_err(|e| e.to_string())? {
            Some(Object::Dict(mut d)) => {
                while let Some((k, v)) = d.next_pair().map_err(|e| e.to_string())? {
                    let key = k.to_vec();
                    let mut ps = Vec::new();
                    if let Object::List(mut pl) = v {
                        while let Some(p) = pl.next_object().map_err(|e| e.to_string())? {
                            ps.push(bytes_of(p)?);
                        }
                    }
                    parents.push((key, ps));
                }
            }
            _ => return Err("expected parents dict".to_string()),
        }
        // snapshots list
        let mut snapshots = Vec::new();
        match top.next_object().map_err(|e| e.to_string())? {
            Some(Object::List(mut l)) => {
                while let Some(s) = l.next_object().map_err(|e| e.to_string())? {
                    snapshots.push(bytes_of(s)?);
                }
            }
            _ => return Err("expected snapshots list".to_string()),
        }
        // diff_offset dict
        let mut diff_offset = std::collections::HashMap::new();
        match top.next_object().map_err(|e| e.to_string())? {
            Some(Object::Dict(mut d)) => {
                while let Some((k, v)) = d.next_pair().map_err(|e| e.to_string())? {
                    let key = k.to_vec();
                    if let Object::List(mut pair) = v {
                        let start = int_of(pair.next_object().map_err(|e| e.to_string())?)?;
                        let len = int_of(pair.next_object().map_err(|e| e.to_string())?)?;
                        diff_offset.insert(key, (start, len));
                    }
                }
            }
            _ => return Err("expected diff_offset dict".to_string()),
        }
        Ok((parents, snapshots, diff_offset))
    }
}

fn bytes_of(obj: bendy::decoding::Object<'_, '_>) -> Result<Vec<u8>, String> {
    match obj {
        bendy::decoding::Object::Bytes(b) => Ok(b.to_vec()),
        _ => Err("expected bencode bytes".to_string()),
    }
}

fn int_of(obj: Option<bendy::decoding::Object<'_, '_>>) -> Result<u64, String> {
    match obj {
        Some(bendy::decoding::Object::Integer(s)) => s.parse::<u64>().map_err(|e| e.to_string()),
        _ => Err("expected bencode integer".to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn lines(s: &[&[u8]]) -> Vec<Vec<u8>> {
        s.iter().map(|l| l.to_vec()).collect()
    }

    #[test]
    fn disk_vf_save_load_roundtrip() {
        // Mirrors bzrformats test_multiparent.TestMultiVersionedFile.test_save_load.
        let dir = std::env::temp_dir().join(format!("mpvf-test-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let base = dir.join("foop").to_str().unwrap().to_string();

        let mut vf = DiskMultiVersionedFile::new(base.clone(), Some(25), None);
        vf.add_version(
            lines(&[b"a\n", b"b\n", b"c\n", b"d"]),
            b"a".to_vec(),
            vec![],
            None,
            false,
        )
        .unwrap();
        vf.add_version(
            lines(&[b"a\n", b"e\n", b"d\n"]),
            b"b".to_vec(),
            vec![b"a".to_vec()],
            None,
            false,
        )
        .unwrap();
        vf.save().unwrap();

        let mut newvf = DiskMultiVersionedFile::new(base, Some(25), None);
        newvf.load().unwrap();
        let a = newvf.get_line_list(&[b"a".to_vec()]).unwrap();
        assert_eq!(a[0].concat(), b"a\nb\nc\nd");
        let b = newvf.get_line_list(&[b"b".to_vec()]).unwrap();
        assert_eq!(b[0].concat(), b"a\ne\nd\n");
        newvf.destroy().unwrap();
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn new_text_to_patch() {
        let mp = MultiParent::with_hunks(vec![Hunk::NewText(lines(&[b"a\n"]))]);
        assert_eq!(
            mp.to_patch(),
            vec![b"i 1\n".to_vec(), b"a\n".to_vec(), b"\n".to_vec()]
        );
    }

    #[test]
    fn empty_new_text_to_patch() {
        // Mirrors test_multiparent.TestNewText.test_to_patch empty case.
        let mp = MultiParent::with_hunks(vec![Hunk::NewText(vec![])]);
        assert_eq!(mp.to_patch(), vec![b"i 0\n".to_vec(), b"\n".to_vec()]);
    }

    #[test]
    fn new_text_line_without_trailing_newline_to_patch() {
        // Mirrors test_multiparent.TestNewText.test_to_patch `[b"a"]` case —
        // `to_patch` must emit the bare `b"\n"` separator regardless of
        // whether the final payload line itself ends in `\n`.
        let mp = MultiParent::with_hunks(vec![Hunk::NewText(lines(&[b"a"]))]);
        assert_eq!(
            mp.to_patch(),
            vec![b"i 1\n".to_vec(), b"a".to_vec(), b"\n".to_vec()]
        );
    }

    #[test]
    fn mixed_to_patch() {
        let mp = MultiParent::with_hunks(vec![
            Hunk::NewText(lines(&[b"a\n"])),
            Hunk::ParentText {
                parent: 0,
                parent_pos: 1,
                child_pos: 2,
                num_lines: 3,
            },
        ]);
        assert_eq!(
            mp.to_patch(),
            vec![
                b"i 1\n".to_vec(),
                b"a\n".to_vec(),
                b"\n".to_vec(),
                b"c 0 1 2 3\n".to_vec(),
            ]
        );
    }

    #[test]
    fn from_patch_round_trip() {
        let mp = MultiParent::with_hunks(vec![
            Hunk::NewText(lines(&[b"a\n"])),
            Hunk::ParentText {
                parent: 0,
                parent_pos: 1,
                child_pos: 2,
                num_lines: 3,
            },
        ]);
        let parsed = MultiParent::from_patch(b"i 1\na\n\nc 0 1 2 3").unwrap();
        assert_eq!(parsed, mp);
    }

    #[test]
    fn from_patch_without_trailing_separator() {
        let parsed = MultiParent::from_patch(b"i 1\na\nc 0 1 2 3\n").unwrap();
        let expected = MultiParent::with_hunks(vec![
            Hunk::NewText(vec![b"a".to_vec()]),
            Hunk::ParentText {
                parent: 0,
                parent_pos: 1,
                child_pos: 2,
                num_lines: 3,
            },
        ]);
        assert_eq!(parsed, expected);
    }

    #[test]
    fn num_lines_matches_python() {
        let mut mp = MultiParent::with_hunks(vec![Hunk::NewText(lines(&[b"a\n"]))]);
        assert_eq!(mp.num_lines(), 1);
        mp.hunks.push(Hunk::NewText(lines(&[b"b\n", b"c\n"])));
        assert_eq!(mp.num_lines(), 3);
        mp.hunks.push(Hunk::ParentText {
            parent: 0,
            parent_pos: 0,
            child_pos: 3,
            num_lines: 2,
        });
        assert_eq!(mp.num_lines(), 5);
        mp.hunks.push(Hunk::NewText(lines(&[b"f\n", b"g\n"])));
        assert_eq!(mp.num_lines(), 7);
    }

    #[test]
    fn range_iterator_shape() {
        let mp = MultiParent::with_hunks(vec![
            Hunk::ParentText {
                parent: 1,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 4,
            },
            Hunk::ParentText {
                parent: 0,
                parent_pos: 3,
                child_pos: 4,
                num_lines: 1,
            },
            Hunk::NewText(lines(&[b"q\n"])),
        ]);
        let items = mp.range_iterator();
        assert_eq!(items.len(), 3);
        assert_eq!((items[0].start, items[0].end), (0, 4));
        assert_eq!(
            items[0].data,
            RangeData::Parent {
                parent: 1,
                parent_start: 0,
                parent_end: 4,
            }
        );
        assert_eq!((items[1].start, items[1].end), (4, 5));
        assert_eq!(
            items[1].data,
            RangeData::Parent {
                parent: 0,
                parent_start: 3,
                parent_end: 4,
            }
        );
        assert_eq!((items[2].start, items[2].end), (5, 6));
        match items[2].data {
            RangeData::New(ls) => assert_eq!(ls, &[b"q\n".to_vec()][..]),
            _ => panic!("expected New"),
        }
    }

    #[test]
    fn matching_blocks_emits_sentinel() {
        let mp = MultiParent::with_hunks(vec![
            Hunk::ParentText {
                parent: 0,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 1,
            },
            Hunk::NewText(lines(&[b"b\n"])),
            Hunk::ParentText {
                parent: 0,
                parent_pos: 1,
                child_pos: 2,
                num_lines: 3,
            },
        ]);
        assert_eq!(
            mp.matching_blocks(0, 4),
            vec![(0, 0, 1), (1, 2, 3), (4, 5, 0)]
        );
    }

    #[test]
    fn is_snapshot() {
        assert!(MultiParent::with_hunks(vec![Hunk::NewText(lines(&[b"a\n"]))]).is_snapshot());
        assert!(!MultiParent::new().is_snapshot());
        assert!(!MultiParent::with_hunks(vec![
            Hunk::NewText(lines(&[b"a\n"])),
            Hunk::NewText(lines(&[b"b\n"])),
        ])
        .is_snapshot());
        assert!(!MultiParent::with_hunks(vec![Hunk::ParentText {
            parent: 0,
            parent_pos: 0,
            child_pos: 0,
            num_lines: 1,
        }])
        .is_snapshot());
    }

    #[test]
    fn binary_content_round_trip() {
        // From test_binary_content: bytes containing \r, \xff, NUL.
        let lf_split: Vec<Vec<u8>> = vec![
            b"\x00\n".to_vec(),
            b"\x00\r\x01\n".to_vec(),
            b"\x02\r\xff".to_vec(),
        ];
        let mp = MultiParent::with_hunks(vec![Hunk::NewText(lf_split.clone())]);
        let patch: Vec<u8> = mp.to_patch().into_iter().flatten().collect();
        let parsed = MultiParent::from_patch(&patch).unwrap();
        assert_eq!(parsed, mp);
    }

    #[test]
    fn patch_len_matches_to_patch() {
        let mp = MultiParent::with_hunks(vec![
            Hunk::NewText(lines(&[b"hello\n", b"world\n"])),
            Hunk::ParentText {
                parent: 2,
                parent_pos: 10,
                child_pos: 20,
                num_lines: 5,
            },
        ]);
        let concatenated: usize = mp.to_patch().iter().map(|l| l.len()).sum();
        assert_eq!(mp.patch_len(), concatenated);
    }

    #[test]
    fn from_patch_rejects_unexpected_char() {
        assert_eq!(
            MultiParent::from_patch(b"x nonsense\n"),
            Err(ParseError::UnexpectedChar(b'x'))
        );
    }

    fn topo_parents(
        entries: &[(&str, Option<&[&str]>)],
    ) -> std::collections::HashMap<String, Option<Vec<String>>> {
        entries
            .iter()
            .map(|(k, ps)| {
                (
                    (*k).to_string(),
                    ps.map(|ps| ps.iter().map(|p| (*p).to_string()).collect()),
                )
            })
            .collect()
    }

    fn topo_versions(vs: &[&str]) -> Vec<String> {
        vs.iter().map(|v| (*v).to_string()).collect()
    }

    #[test]
    fn topo_iter_linear_chain() {
        // a <- b <- c <- d, fed in insertion order.
        let parents = topo_parents(&[
            ("a", Some(&[])),
            ("b", Some(&["a"])),
            ("c", Some(&["b"])),
            ("d", Some(&["c"])),
        ]);
        let versions = topo_versions(&["a", "b", "c", "d"]);
        assert_eq!(topo_iter(&parents, &versions), versions);
    }

    #[test]
    fn topo_iter_orders_parents_before_children_when_input_is_shuffled() {
        // Same diamond shape, shuffled input. Tiebreakers come from the
        // order in which descendants were registered while walking
        // `version_order`, so the exact sequence is deterministic and
        // matches the Python `_topo_iter` implementation.
        let parents = topo_parents(&[
            ("a", Some(&[])),
            ("b", Some(&["a"])),
            ("c", Some(&["a"])),
            ("d", Some(&["b", "c"])),
        ]);
        let got = topo_iter(&parents, &topo_versions(&["d", "c", "b", "a"]));
        assert_eq!(got, topo_versions(&["a", "c", "b", "d"]));
    }

    #[test]
    fn topo_iter_parentless_sentinel_is_treated_as_root() {
        // A `None` entry (parentless sentinel) is yielded without waiting
        // on anything, mirroring the Python special case.
        let parents = topo_parents(&[("a", None), ("b", Some(&["a"]))]);
        let got = topo_iter(&parents, &topo_versions(&["b", "a"]));
        assert_eq!(got, topo_versions(&["a", "b"]));
    }

    #[test]
    fn topo_iter_ignores_parents_outside_input_set() {
        // If a parent isn't in the version set, it doesn't count as
        // pending — the child can be yielded immediately.
        let parents = topo_parents(&[("x", Some(&["not-in-set"])), ("y", Some(&["x"]))]);
        let got = topo_iter(&parents, &topo_versions(&["x", "y"]));
        assert_eq!(got, topo_versions(&["x", "y"]));
    }

    #[test]
    fn topo_iter_empty_input() {
        let parents: std::collections::HashMap<String, Option<Vec<String>>> =
            std::collections::HashMap::new();
        let got = topo_iter(&parents, &[] as &[String]);
        assert!(got.is_empty());
    }

    #[test]
    fn topo_iter_deduplicates_input() {
        // Duplicate versions in the input list produce a single output
        // entry, matching the "seen" bookkeeping.
        let parents = topo_parents(&[("a", Some(&[])), ("b", Some(&["a"]))]);
        let got = topo_iter(&parents, &topo_versions(&["a", "b", "a", "b"]));
        assert_eq!(got, topo_versions(&["a", "b"]));
    }

    #[test]
    fn topo_iter_diamond() {
        // a -> b, a -> c, b+c -> d
        let parents = topo_parents(&[
            ("a", Some(&[])),
            ("b", Some(&["a"])),
            ("c", Some(&["a"])),
            ("d", Some(&["b", "c"])),
        ]);
        let got = topo_iter(&parents, &topo_versions(&["a", "b", "c", "d"]));
        assert_eq!(got, topo_versions(&["a", "b", "c", "d"]));
    }

    #[test]
    fn from_patch_rejects_truncated_new_text() {
        assert_eq!(
            MultiParent::from_patch(b"i 3\nonly\n"),
            Err(ParseError::Truncated)
        );
    }

    #[test]
    fn from_lines_no_parents_is_single_new_text() {
        let text = lines(&[b"a\n", b"b\n"]);
        let mp = MultiParent::from_lines_with_blocks(&text, &[]);
        assert_eq!(mp.hunks, vec![Hunk::NewText(lines(&[b"a\n", b"b\n"]))]);
    }

    #[test]
    fn from_lines_runs_patiencediff_for_each_parent() {
        // text = parent → single ParentText covering everything.
        let text = lines(&[b"a\n", b"b\n", b"c\n"]);
        let p0 = lines(&[b"a\n", b"b\n", b"c\n"]);
        let parents: Vec<&[Vec<u8>]> = vec![&p0];
        let mp = MultiParent::from_lines(&text, &parents, None);
        assert_eq!(
            mp.hunks,
            vec![Hunk::ParentText {
                parent: 0,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 3,
            }]
        );
    }

    #[test]
    fn from_lines_supplied_left_blocks_skip_left_diff() {
        // Supplied blocks claim a perfect match even though parent doesn't
        // contain text — proves from_lines used them instead of running
        // patiencediff.
        let text = lines(&[b"a\n", b"b\n"]);
        let p0 = lines(&[b"x\n", b"y\n"]);
        let parents: Vec<&[Vec<u8>]> = vec![&p0];
        let mp = MultiParent::from_lines(&text, &parents, Some(vec![(0, 0, 2), (2, 2, 0)]));
        assert_eq!(
            mp.hunks,
            vec![Hunk::ParentText {
                parent: 0,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 2,
            }]
        );
    }

    #[test]
    fn from_lines_single_parent_full_match() {
        // text == parent. One (0,0,2) block plus sentinel.
        let text = lines(&[b"a\n", b"b\n"]);
        let blocks = vec![vec![(0, 0, 2), (2, 2, 0)]];
        let mp = MultiParent::from_lines_with_blocks(&text, &blocks);
        assert_eq!(
            mp.hunks,
            vec![Hunk::ParentText {
                parent: 0,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 2,
            }]
        );
    }

    #[test]
    fn from_lines_prefers_longest_match_across_parents() {
        // text = [a b c d]
        // parent 0 matches [a b] at (0,0,2)
        // parent 1 matches [a b c d] at (0,0,4)
        // The longest match (parent 1) should win.
        let text = lines(&[b"a\n", b"b\n", b"c\n", b"d\n"]);
        let blocks = vec![vec![(0, 0, 2), (2, 4, 0)], vec![(0, 0, 4), (4, 4, 0)]];
        let mp = MultiParent::from_lines_with_blocks(&text, &blocks);
        assert_eq!(
            mp.hunks,
            vec![Hunk::ParentText {
                parent: 1,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 4,
            }]
        );
    }

    #[test]
    fn from_lines_mixes_new_text_and_parent_text() {
        // text = [x a b y]
        // parent 0 matches [a b] at (0,1,2)
        let text = lines(&[b"x\n", b"a\n", b"b\n", b"y\n"]);
        let blocks = vec![vec![(0, 1, 2), (2, 4, 0)]];
        let mp = MultiParent::from_lines_with_blocks(&text, &blocks);
        assert_eq!(
            mp.hunks,
            vec![
                Hunk::NewText(lines(&[b"x\n"])),
                Hunk::ParentText {
                    parent: 0,
                    parent_pos: 0,
                    child_pos: 1,
                    num_lines: 2,
                },
                Hunk::NewText(lines(&[b"y\n"])),
            ]
        );
    }

    #[test]
    fn from_lines_advances_block_offset_when_partial() {
        // text = [a b c]; parent provides (0,0,3) but cur_line might land
        // mid-block if a prior hunk consumed the start. Simulate this by
        // pretending a longer parent matched first.
        // text = [a b c d]
        // parent 0: single block (0,0,4)
        let text = lines(&[b"a\n", b"b\n", b"c\n", b"d\n"]);
        let blocks = vec![vec![(0, 0, 4), (4, 4, 0)]];
        let mp = MultiParent::from_lines_with_blocks(&text, &blocks);
        assert_eq!(
            mp.hunks,
            vec![Hunk::ParentText {
                parent: 0,
                parent_pos: 0,
                child_pos: 0,
                num_lines: 4,
            }]
        );
    }

    #[test]
    fn mpvf_fulltext_roundtrip_via_add_version() {
        // Add a single fulltext (no parents → snapshot), read it back.
        let mut mpvf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        let text = lines(&[b"a\n", b"b\n"]);
        mpvf.add_version(text.clone(), "v1", vec![], None, false)
            .unwrap();
        assert!(mpvf.has_version(&"v1"));
        mpvf.clear_cache();
        let got = mpvf.get_line_list(&["v1"]).unwrap();
        assert_eq!(got, vec![text]);
    }

    #[test]
    fn mpvf_delta_reconstructs_from_parent() {
        // v1 = [a b c], v2 = [a x c] (replace line 1 with x).
        let mut mpvf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        let v1 = lines(&[b"a\n", b"b\n", b"c\n"]);
        let v2 = lines(&[b"a\n", b"x\n", b"c\n"]);
        mpvf.add_version(v1.clone(), "v1", vec![], None, false)
            .unwrap();
        mpvf.add_version(v2.clone(), "v2", vec!["v1"], None, false)
            .unwrap();
        // Force reconstruction from chain only.
        mpvf.clear_cache();
        let got = mpvf.get_line_list(&["v2"]).unwrap();
        assert_eq!(got, vec![v2]);
    }

    #[test]
    fn mpvf_add_diff_then_reconstruct_via_get_line_list() {
        // Wire up the diff directly (the path add_mpdiffs uses) and verify
        // get_line_list walks the chain.
        let mut mpvf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        mpvf.add_version(lines(&[b"x\n", b"y\n"]), "base", vec![], None, false)
            .unwrap();
        // Manually craft a delta that replaces line 0 with "X".
        let diff = MultiParent::with_hunks(vec![
            Hunk::NewText(lines(&[b"X\n"])),
            Hunk::ParentText {
                parent: 0,
                parent_pos: 1,
                child_pos: 1,
                num_lines: 1,
            },
        ]);
        mpvf.add_diff(diff, "child", vec!["base"]);
        // Clear the cache so reconstruct must walk the diff chain.
        mpvf.clear_cache();
        let got = mpvf.get_line_list(&["child"]).unwrap();
        assert_eq!(got, vec![lines(&[b"X\n", b"y\n"])]);
    }

    /// Split a byte string into one `"x\n"` line per byte, mirroring the
    /// Python test helper `add_version`.
    fn char_lines(s: &[u8]) -> Vec<Vec<u8>> {
        s.iter().map(|b| vec![*b, b'\n']).collect()
    }

    /// The 3-version fixture from the Python TestMultiParent.make_vf:
    /// rev-a=abcd, rev-b=acde, rev-c=abef with parents [rev-a, rev-b].
    fn make_two_parent_vf() -> MultiMemoryVersionedFile<&'static str> {
        let mut vf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        vf.add_version(char_lines(b"abcd"), "rev-a", vec![], None, false)
            .unwrap();
        vf.add_version(char_lines(b"acde"), "rev-b", vec![], None, false)
            .unwrap();
        vf.add_version(
            char_lines(b"abef"),
            "rev-c",
            vec!["rev-a", "rev-b"],
            None,
            false,
        )
        .unwrap();
        vf
    }

    #[test]
    fn mpvf_reconstructs_version_with_two_parents() {
        // rev-c is a diff against both rev-a and rev-b; reconstructing it
        // exercises hunks that reference different parent slots.
        let mut vf = make_two_parent_vf();
        vf.clear_cache();
        let got = vf.get_line_list(&["rev-a", "rev-c"]).unwrap();
        assert_eq!(got[0], char_lines(b"abcd"));
        assert_eq!(got[1], char_lines(b"abef"));
    }

    #[test]
    fn mpvf_get_build_ranking_returns_all_versions() {
        let vf = make_two_parent_vf();
        let ranking: std::collections::HashSet<&str> = vf.get_build_ranking().into_iter().collect();
        let expected: std::collections::HashSet<&str> =
            vec!["rev-a", "rev-b", "rev-c"].into_iter().collect();
        assert_eq!(ranking, expected);
    }

    #[test]
    fn mpvf_get_build_ranking_single_version() {
        let mut vf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        vf.add_version(char_lines(b"a"), "rev-a", vec![], None, false)
            .unwrap();
        assert_eq!(vf.get_build_ranking(), vec!["rev-a"]);
    }

    #[test]
    fn mpvf_reordered_lines_from_distinct_parent_hunks() {
        // The corner case requiring a cursor restart during reconstruction:
        // rev-e draws one line each from two different hunks of rev-b, in the
        // opposite order to how they appear in rev-b.
        let mut vf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        vf.add_version(char_lines(b"c"), "rev-a", vec![], None, false)
            .unwrap();
        vf.add_version(char_lines(b"acb"), "rev-b", vec!["rev-a"], None, false)
            .unwrap();
        vf.add_version(char_lines(b"b"), "rev-c", vec!["rev-b"], None, false)
            .unwrap();
        vf.add_version(char_lines(b"a"), "rev-d", vec!["rev-b"], None, false)
            .unwrap();
        vf.add_version(
            char_lines(b"ba"),
            "rev-e",
            vec!["rev-c", "rev-d"],
            None,
            false,
        )
        .unwrap();
        vf.clear_cache();
        let got = vf.get_line_list(&["rev-e"]).unwrap();
        assert_eq!(got[0], char_lines(b"ba"));
    }

    #[test]
    fn mpvf_versions_preserves_insert_order() {
        let mut mpvf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        mpvf.add_version(vec![], "a", vec![], None, false).unwrap();
        mpvf.add_version(vec![], "b", vec![], None, false).unwrap();
        mpvf.add_version(vec![], "c", vec![], None, false).unwrap();
        let v: Vec<&str> = mpvf.versions().copied().collect();
        assert_eq!(v, vec!["a", "b", "c"]);
    }

    #[test]
    fn mpvf_make_snapshot_replaces_delta_with_fulltext() {
        let mut mpvf: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        let base = lines(&[b"a\n", b"b\n"]);
        let child = lines(&[b"X\n", b"b\n"]);
        mpvf.add_version(base, "base", vec![], None, false).unwrap();
        mpvf.add_version(child.clone(), "child", vec!["base"], None, false)
            .unwrap();
        assert!(!mpvf.is_snapshot(&"child"));
        mpvf.make_snapshot("child").unwrap();
        assert!(mpvf.is_snapshot(&"child"));
        // The stored diff is now a single NewText covering the full child.
        let d = mpvf.get_diff(&"child").unwrap();
        assert!(matches!(d.hunks.as_slice(), [Hunk::NewText(_)]));
    }

    #[test]
    fn mpvf_import_diffs_copies_each_version() {
        let mut src: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        src.add_version(lines(&[b"a\n"]), "v1", vec![], None, false)
            .unwrap();
        src.add_version(lines(&[b"b\n"]), "v2", vec!["v1"], None, false)
            .unwrap();
        let mut dst: MultiMemoryVersionedFile<&'static str> = MultiMemoryVersionedFile::default();
        dst.import_diffs(&src);
        assert!(dst.has_version(&"v1"));
        assert!(dst.has_version(&"v2"));
        // Parents are preserved.
        assert_eq!(dst.get_parents(&"v2"), Some(&["v1"][..]));
    }

    #[test]
    fn mpvf_select_snapshots_picks_chain_breaks() {
        // Build a long chain; with snapshot_interval=2 every third
        // version (counting the root) should be selected.
        let mut mpvf: MultiMemoryVersionedFile<&'static str> =
            MultiMemoryVersionedFile::new(Some(2), None);
        mpvf.add_version(lines(&[b"v1\n"]), "v1", vec![], None, false)
            .unwrap();
        mpvf.add_version(lines(&[b"v2\n"]), "v2", vec!["v1"], None, false)
            .unwrap();
        mpvf.add_version(lines(&[b"v3\n"]), "v3", vec!["v2"], None, false)
            .unwrap();
        mpvf.add_version(lines(&[b"v4\n"]), "v4", vec!["v3"], None, false)
            .unwrap();
        let chosen = mpvf.select_snapshots();
        // v1 has no parents → always a snapshot. After two steps we
        // exceed the interval, so v4 (3 ancestors) is also selected.
        assert!(chosen.contains(&"v1"));
        assert!(chosen.contains(&"v4"));
    }
}