vcs-jj 0.11.0

Automate Jujutsu (jj) from Rust: a typed, async wrapper that drives the real jj CLI with its exact behavior.
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
//! Typed model of jj's **materialized** conflict markers — parse a conflicted
//! file's content into structured regions and write a chosen resolution back.
//! Pure functions (no subprocess), so everything here is hermetic.
//!
//! Covers jj's native styles (`ui.conflict-marker-style`): **`diff`** (the
//! 0.38 default — one side rendered as a unified diff against the base) and
//! **`snapshot`** (every side and the base rendered verbatim). Files
//! materialized with the `git` style use git's grammar — parse those with
//! `vcs_git::conflict` instead (documented asymmetry, not an oversight).
//!
//! A region looks like:
//!
//! ```text
//! <<<<<<< conflict 1 of 1
//! %%%%%%% diff from: <change> <commit> "base"
//! \\\\\\\        to: <change> <commit> "side-a"
//! -line 2
//! +main line 2
//! +++++++ <change> <commit> "side-b"
//! feature line 2
//! >>>>>>> conflict 1 of 1 ends
//! ```
//!
//! Lines are kept verbatim (including `\r\n` and a missing trailing newline),
//! so [`render`] is a byte-exact roundtrip.

use processkit::{Error, Result};

use crate::BINARY;

/// One section inside a jj conflict region.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum JjConflictSection {
    /// A `%%%%%%%` section: one side expressed as a unified diff from the
    /// base (`-`/`+`/` `-prefixed lines). The side's content is the diff's
    /// *new* text; the base is its *old* text.
    Diff {
        /// The `diff from:` label (the base's ids/description).
        from_label: String,
        /// The `to:` label (this side's ids/description).
        to_label: String,
        /// Raw diff lines, verbatim.
        lines: Vec<String>,
    },
    /// A `+++++++` section: one side's content, verbatim.
    Snapshot {
        /// The side's ids/description.
        label: String,
        /// The side's lines, verbatim.
        lines: Vec<String>,
    },
    /// A `-------` section (snapshot style): the base's content, verbatim.
    Base {
        /// The base's ids/description.
        label: String,
        /// The base's lines, verbatim.
        lines: Vec<String>,
    },
}

/// One materialized jj conflict region (`<<<<<<< conflict N of M` …
/// `>>>>>>> conflict N of M ends`).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct JjConflictRegion {
    /// This region's number within the file (the `N` of `conflict N of M`).
    pub number: u32,
    /// The file's total conflict count (the `M`).
    pub total: u32,
    /// The region's sections, in file order.
    pub sections: Vec<JjConflictSection>,
    // Verbatim marker lines for byte-exact rendering.
    marker_start: String,
    marker_end: String,
    section_markers: Vec<String>,
}

impl JjConflictRegion {
    /// The materialized content of each *side*, in file order (a diff section
    /// contributes its new text; base sections are not sides). Each side is a
    /// list of newline-terminated lines whose concatenation is the side's exact
    /// bytes — including a **missing** terminating newline, which jj encodes and
    /// this reconstruction honors.
    pub fn sides(&self) -> Vec<Vec<String>> {
        let mixed = self.mixed_eol();
        self.sections
            .iter()
            .filter_map(|section| match section {
                JjConflictSection::Diff {
                    to_label, lines, ..
                } => Some(join_sublines(
                    &apply_diff(lines, false),
                    labeled_no_eol(to_label),
                    mixed,
                )),
                JjConflictSection::Snapshot { label, lines } => {
                    Some(join_sublines(lines, labeled_no_eol(label), mixed))
                }
                JjConflictSection::Base { .. } => None,
            })
            .collect()
    }

    /// The base content, when the region records one (a diff section's old
    /// text, or a snapshot-style `-------` section). Honors a missing
    /// terminating newline the same way [`sides`](Self::sides) does.
    pub fn base(&self) -> Option<Vec<String>> {
        let mixed = self.mixed_eol();
        self.sections.iter().find_map(|section| match section {
            JjConflictSection::Diff {
                from_label, lines, ..
            } => Some(join_sublines(
                &apply_diff(lines, true),
                labeled_no_eol(from_label),
                mixed,
            )),
            JjConflictSection::Base { label, lines } => {
                Some(join_sublines(lines, labeled_no_eol(label), mixed))
            }
            JjConflictSection::Snapshot { .. } => None,
        })
    }

    /// Whether jj rendered this region in its explicit trailing-newline mode.
    ///
    /// When every side (and the base) ends in a newline, jj renders each side's
    /// lines verbatim. But as soon as **any** side lacks a terminating newline,
    /// jj switches representation for the *whole* region: the newline-less side
    /// gets a `(no terminating newline)` label marker and its content with no
    /// trailing line, while every newline-terminated side gets an extra trailing
    /// empty line (a bare `\n`, or a ` `/`+`/`-` context line inside a diff) to
    /// mark "this one *does* end in a newline". Detecting the mode lets
    /// [`join_sublines`] undo that extra line so the materialized bytes are exact
    /// (verified against jj 0.42 across all four newline permutations).
    fn mixed_eol(&self) -> bool {
        self.sections.iter().any(|section| match section {
            JjConflictSection::Snapshot { label, .. } | JjConflictSection::Base { label, .. } => {
                labeled_no_eol(label)
            }
            JjConflictSection::Diff {
                from_label,
                to_label,
                ..
            } => labeled_no_eol(from_label) || labeled_no_eol(to_label),
        })
    }
}

/// jj's marker for a side whose content does not end in a newline. It is
/// appended to the section's label, e.g. `… "side-a" (no terminating newline)`.
const NO_EOL_MARKER: &str = "(no terminating newline)";

/// Whether a section label carries jj's [`NO_EOL_MARKER`].
fn labeled_no_eol(label: &str) -> bool {
    label.trim_end().ends_with(NO_EOL_MARKER)
}

/// Re-join a section's rendered sub-lines (already prefix-stripped for a diff)
/// into the side/base's true bytes, undoing jj's trailing-newline encoding.
///
/// - `no_eol` — this role's label carries [`NO_EOL_MARKER`]: the last sub-line's
///   `\n` is jj's display artifact for a side that has no terminating newline;
///   drop it, so the content ends without a newline.
/// - `mixed` — the region is in the explicit trailing-newline mode
///   ([`JjConflictRegion::mixed_eol`]): a newline-terminated side carries one
///   extra trailing empty sub-line marking "has newline"; drop that one `\n`.
///
/// The two are mutually exclusive per section (a no-newline side is annotated,
/// not padded), so at most one correction applies. The result is re-split into
/// newline-terminated lines so callers and [`resolve`] reproduce the bytes by
/// plain concatenation.
fn join_sublines(sublines: &[String], no_eol: bool, mixed: bool) -> Vec<String> {
    let mut content: String = sublines.concat();
    if no_eol || mixed {
        // Remove the trailing line terminator jj added — its display newline
        // (no_eol) or the trailing-empty pad line (mixed). jj emits it with the
        // file's own ending, so strip a full `\r\n` on a CRLF file (else a bare
        // `\n` would leave a stray `\r` and silently corrupt the resolved bytes).
        if content.ends_with("\r\n") {
            content.truncate(content.len() - 2);
        } else if content.ends_with('\n') {
            content.pop();
        }
    }
    content.split_inclusive('\n').map(str::to_string).collect()
}

/// A conflicted file as a sequence of plain-text runs and conflict regions.
///
/// Intentionally **exhaustive** (like `vcs_git::conflict::ConflictSegment`): a
/// file is text-or-conflict and consumers match every segment; field evolution
/// rides [`JjConflictRegion`], which *is* `#[non_exhaustive]`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JjConflictSegment {
    /// Lines outside any conflict (verbatim).
    Text(Vec<String>),
    /// One materialized conflict region (boxed — much larger than a text run).
    Conflict(Box<JjConflictRegion>),
}

/// What [`resolve`] keeps in place of each conflict region.
///
/// Intentionally **exhaustive** (no `#[non_exhaustive]`): the domain is closed —
/// `Side(usize)` already spans any number of sides and `Base` is the only other
/// choice — so `#[non_exhaustive]` would buy no future variant while forcing a
/// wildcard arm on any caller that matches this (callers usually *construct* it to
/// pass to [`resolve`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JjResolution {
    /// The N-th side (0-based, file order) — `Side(0)` is the first side.
    Side(usize),
    /// The recorded base.
    Base,
}

/// Materialize a diff section: `old = true` keeps `-`/` ` lines (the base),
/// `old = false` keeps `+`/` ` lines (the side), stripping the prefix char
/// but preserving the line ending.
fn apply_diff(lines: &[String], old: bool) -> Vec<String> {
    let keep = if old { ['-', ' '] } else { ['+', ' '] };
    lines
        .iter()
        .filter_map(|line| {
            let mut chars = line.chars();
            let first = chars.next()?;
            keep.contains(&first).then(|| chars.as_str().to_string())
        })
        .collect()
}

/// The marker run length when `line` starts with a run of `ch` (≥ 7) followed
/// by a space or line end.
fn marker_run(line: &str, ch: char) -> Option<usize> {
    let trimmed = line.trim_end_matches(['\r', '\n']);
    let n = trimmed.chars().take_while(|&c| c == ch).count();
    let rest = &trimmed[n..];
    (n >= 7 && (rest.is_empty() || rest.starts_with(' '))).then_some(n)
}

fn marker_label(line: &str, n: usize) -> String {
    line.trim_end_matches(['\r', '\n'])[n..]
        .trim_start()
        .to_string()
}

fn parse_error(message: String) -> Error {
    Error::parse(BINARY, message)
}

/// Parse `conflict N of M` / `conflict N of M ends` headers.
fn parse_counter(label: &str) -> Option<(u32, u32)> {
    let rest = label.strip_prefix("conflict ")?;
    let mut parts = rest.split_whitespace();
    let n = parts.next()?.parse().ok()?;
    let of = parts.next()?;
    let m = parts.next()?.parse().ok()?;
    (of == "of").then_some((n, m))
}

/// Does `content` contain a jj conflict-start marker? Cheap pre-check.
pub fn has_conflict_markers(content: &str) -> bool {
    content.split_inclusive('\n').any(|line| {
        marker_run(line, '<').is_some_and(|n| parse_counter(&marker_label(line, n)).is_some())
    })
}

/// Whether `content` carries git's `<<<`/`===`/`>>>` conflict triad — used only to
/// steer a caller who passed a **git-style** file (jj's `git` marker style, or a
/// plain git conflict) to `vcs_git::conflict`. Requires all three marker runs, so a
/// jj file that merely contains a marker-*like* content line (a single `<<<` run,
/// no `===`/`>>>` structure) is not mistaken for one.
fn looks_git_style(content: &str) -> bool {
    let has_run = |ch: char| {
        content
            .split_inclusive('\n')
            .any(|l| marker_run(l, ch).is_some())
    };
    has_run('<') && has_run('=') && has_run('>')
}

/// Parse a jj-materialized conflicted file (native `diff`/`snapshot` styles)
/// into text/conflict segments. Errors with [`Error::Parse`] on malformed input
/// (unterminated region, content before the first section marker). A **git-style**
/// file (the `<<<`/`===`/`>>>` triad with no jj `conflict N of M` header) is
/// redirected to `vcs_git::conflict`. A jj file whose *content* merely contains a
/// `<<<<<<<`-like line (jj lengthens its own markers past such content) parses
/// fine — the marker-like line is kept as text.
pub fn parse_conflicts(content: &str) -> Result<Vec<JjConflictSegment>> {
    // Only steer to the git parser when the file is genuinely git-style (the full
    // triad) AND has no jj header — never for a jj file that just happens to carry a
    // marker-like content line alongside its real (longer) markers.
    if !has_conflict_markers(content) && looks_git_style(content) {
        return Err(parse_error(
            "git-style conflict markers — parse this file with vcs_git::conflict \
             (jj's `git` marker style uses git's grammar)"
                .to_string(),
        ));
    }

    let mut segments = Vec::new();
    let mut text: Vec<String> = Vec::new();
    let mut lines = content.split_inclusive('\n');

    while let Some(line) = lines.next() {
        let counter = marker_run(line, '<')
            .map(|n| (n, marker_label(line, n)))
            .and_then(|(n, label)| parse_counter(&label).map(|c| (n, c)));
        let Some((n, (number, total))) = counter else {
            // A `<<<` run that isn't a `conflict N of M` header is content, not an
            // error: jj lengthens its real markers past any marker-like content, so
            // a bare `<<<<<<<` line here is part of the file's text.
            text.push(line.to_string());
            continue;
        };
        if !text.is_empty() {
            segments.push(JjConflictSegment::Text(std::mem::take(&mut text)));
        }

        let marker_start = line.to_string();
        let mut sections: Vec<JjConflictSection> = Vec::new();
        let mut section_markers: Vec<String> = Vec::new();
        let marker_end = loop {
            let Some(line) = lines.next() else {
                return Err(parse_error(format!(
                    "unterminated jj conflict {number} of {total}"
                )));
            };
            // Section/end markers must match the region's opening run length —
            // jj lengthens ALL of a file's markers together when the content
            // itself contains marker-like runs, so a shorter run is content.
            if marker_run(line, '>') == Some(n) {
                let label = marker_label(line, n);
                // jj's end marker is `conflict N of M ends`; `parse_counter` matches
                // it after trimming the trailing ` ends` (and also a hypothetical
                // `conflict N of M` with no ` ends`). We rely *solely* on that
                // structural check — no loose `ends_with("ends")` fallback, which
                // could wrongly terminate the region on content that happens to be a
                // run of exactly `n` `>` followed by a word ending in "ends".
                if parse_counter(label.trim_end_matches(" ends").trim_end()).is_some() {
                    break line.to_string();
                }
            }
            if let Some(m) = marker_run(line, '%').filter(|&m| m == n) {
                // `%%%%%%% diff from: …` then a `\\\\\\\        to: …` line.
                let from_label = marker_label(line, m)
                    .trim_start_matches("diff from:")
                    .trim()
                    .to_string();
                let Some(to_line) = lines.next() else {
                    return Err(parse_error("diff section missing its `to:` line".into()));
                };
                // The `\\\` run must match the `%%%` run length `m` (== the
                // region's `n`) — jj lengthens every marker in a file together,
                // so a differing run is malformed input, not a valid `to:` line.
                if marker_run(to_line, '\\') != Some(m) {
                    return Err(parse_error(format!(
                        "diff section: expected a {m}-long `\\` `to:` line, got {:?}",
                        to_line.trim_end()
                    )));
                }
                let to_label = marker_label(to_line, m)
                    .trim_start_matches("to:")
                    .trim()
                    .to_string();
                section_markers.push(format!("{line}{to_line}"));
                sections.push(JjConflictSection::Diff {
                    from_label,
                    to_label,
                    lines: Vec::new(),
                });
                continue;
            }
            if let Some(m) = marker_run(line, '+').filter(|&m| m == n) {
                section_markers.push(line.to_string());
                sections.push(JjConflictSection::Snapshot {
                    label: marker_label(line, m),
                    lines: Vec::new(),
                });
                continue;
            }
            if let Some(m) = marker_run(line, '-').filter(|&m| m == n) {
                section_markers.push(line.to_string());
                sections.push(JjConflictSection::Base {
                    label: marker_label(line, m),
                    lines: Vec::new(),
                });
                continue;
            }
            // Content line for the current section.
            match sections.last_mut() {
                Some(
                    JjConflictSection::Diff { lines, .. }
                    | JjConflictSection::Snapshot { lines, .. }
                    | JjConflictSection::Base { lines, .. },
                ) => lines.push(line.to_string()),
                None => {
                    return Err(parse_error(format!(
                        "content before the first section marker in conflict \
                         {number}: {:?}",
                        line.trim_end()
                    )));
                }
            }
        };

        segments.push(JjConflictSegment::Conflict(Box::new(JjConflictRegion {
            number,
            total,
            sections,
            marker_start,
            marker_end,
            section_markers,
        })));
    }
    if !text.is_empty() {
        segments.push(JjConflictSegment::Text(text));
    }
    Ok(segments)
}

/// Re-render segments verbatim — the byte-exact inverse of
/// [`parse_conflicts`].
pub fn render(segments: &[JjConflictSegment]) -> String {
    let mut out = String::new();
    for segment in segments {
        match segment {
            JjConflictSegment::Text(lines) => lines.iter().for_each(|l| out.push_str(l)),
            JjConflictSegment::Conflict(region) => {
                out.push_str(&region.marker_start);
                for (section, marker) in region.sections.iter().zip(&region.section_markers) {
                    out.push_str(marker);
                    let (JjConflictSection::Diff { lines, .. }
                    | JjConflictSection::Snapshot { lines, .. }
                    | JjConflictSection::Base { lines, .. }) = section;
                    lines.iter().for_each(|l| out.push_str(l));
                }
                out.push_str(&region.marker_end);
            }
        }
    }
    out
}

/// Produce the file content with every conflict resolved to `resolution`.
///
/// Errors with a clear message when a region has no such side/base.
pub fn resolve(segments: &[JjConflictSegment], resolution: JjResolution) -> Result<String> {
    let refuse = |what: String| {
        Error::spawn(
            BINARY,
            std::io::Error::new(std::io::ErrorKind::InvalidInput, what),
        )
    };
    let mut out = String::new();
    for segment in segments {
        match segment {
            JjConflictSegment::Text(lines) => lines.iter().for_each(|l| out.push_str(l)),
            JjConflictSegment::Conflict(region) => {
                let chosen = match resolution {
                    JjResolution::Side(i) => {
                        let sides = region.sides();
                        sides.get(i).cloned().ok_or_else(|| {
                            refuse(format!(
                                "conflict {} has {} side(s); Side({i}) does not exist",
                                region.number,
                                sides.len()
                            ))
                        })?
                    }
                    JjResolution::Base => region.base().ok_or_else(|| {
                        refuse(format!("conflict {} records no base", region.number))
                    })?,
                };
                chosen.iter().for_each(|l| out.push_str(l));
            }
        }
    }
    Ok(out)
}

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

    // Captured verbatim from jj 0.38 (default `diff` style).
    const DIFF_STYLE: &str = "line 1\n<<<<<<< conflict 1 of 1\n%%%%%%% diff from: rnxsupvw 638ae425 \"base\"\n\\\\\\\\\\\\\\        to: ozvltnxm 92f2b14f \"side-a\"\n-line 2\n+main line 2\n+++++++ xyrusolp ad268d1f \"side-b\"\nfeature line 2\n>>>>>>> conflict 1 of 1 ends\nline 3\n";

    // Captured verbatim from jj 0.38 (`snapshot` style).
    const SNAPSHOT_STYLE: &str = "line 1\n<<<<<<< conflict 1 of 1\n+++++++ kttusupp 7eedad44 \"side-a\"\nmain line 2\n------- rzkutuko 4fe1246f \"base\"\nline 2\n+++++++ ukuqwwlw 38f5069b \"side-b\"\nfeature line 2\n>>>>>>> conflict 1 of 1 ends\nline 3\n";

    #[test]
    fn parses_diff_style_and_materializes_sides() {
        let segments = parse_conflicts(DIFF_STYLE).expect("parse");
        assert_eq!(segments.len(), 3);
        let JjConflictSegment::Conflict(region) = &segments[1] else {
            panic!("expected a conflict, got {segments:?}");
        };
        assert_eq!((region.number, region.total), (1, 1));
        assert_eq!(region.sections.len(), 2);
        let sides = region.sides();
        assert_eq!(sides.len(), 2);
        assert_eq!(sides[0], ["main line 2\n"], "diff side = applied new text");
        assert_eq!(sides[1], ["feature line 2\n"], "snapshot side verbatim");
        assert_eq!(region.base().unwrap(), ["line 2\n"], "diff old text = base");
    }

    #[test]
    fn parses_snapshot_style() {
        let segments = parse_conflicts(SNAPSHOT_STYLE).expect("parse");
        let JjConflictSegment::Conflict(region) = &segments[1] else {
            panic!("expected a conflict");
        };
        assert_eq!(region.sections.len(), 3);
        let sides = region.sides();
        assert_eq!(sides[0], ["main line 2\n"]);
        assert_eq!(sides[1], ["feature line 2\n"]);
        assert_eq!(region.base().unwrap(), ["line 2\n"]);
        assert!(
            matches!(&region.sections[1], JjConflictSection::Base { label, .. }
                if label.contains("\"base\"")),
        );
    }

    // A content line that is a run of exactly `n` `>` followed by a word ending in
    // "ends" must NOT be mistaken for the region terminator — only `conflict N of M
    // ends` ends the region. (The removed loose `ends_with("ends")` fallback would
    // have wrongly terminated here.) This is malformed input jj wouldn't emit (it
    // lengthens markers past content runs), but the parser must not be fooled by it.
    #[test]
    fn content_run_ending_in_ends_is_not_the_terminator() {
        let input = concat!(
            "line 1\n",
            "<<<<<<< conflict 1 of 1\n",
            "+++++++ side-a\n",
            ">>>>>>> recommends\n", // 7 `>`, ends in "ends" — content, NOT the end marker
            "------- base\n",
            "line 2\n",
            "+++++++ side-b\n",
            "feature line 2\n",
            ">>>>>>> conflict 1 of 1 ends\n", // the real terminator
            "line 3\n",
        );
        let segments = parse_conflicts(input).expect("parse");
        // Three segments — the region did not end early at the `recommends` line.
        assert_eq!(segments.len(), 3);
        let JjConflictSegment::Conflict(region) = &segments[1] else {
            panic!("expected a conflict, got {segments:?}");
        };
        assert_eq!((region.number, region.total), (1, 1));
        assert!(
            region.sides()[0].iter().any(|l| l.contains("recommends")),
            "the `>>>…recommends` content line is part of side-a, not the terminator"
        );
        // And it round-trips byte-for-byte (the content line is stored verbatim).
        assert_eq!(render(&segments), input);
    }

    // The `\\\ to:` run must match the region's marker length. A `%%%%%%%` (7)
    // diff header followed by an 8-long `\` run is malformed (jj lengthens all
    // markers together) and must be rejected, not silently accepted.
    #[test]
    fn diff_section_rejects_mismatched_to_marker_length() {
        let input = concat!(
            "<<<<<<< conflict 1 of 1\n",
            "%%%%%%% diff from: ab cd \"base\"\n",
            "\\\\\\\\\\\\\\\\        to: ef gh \"side\"\n", // 8 backslashes ≠ 7
            "-line\n",
            "+new\n",
            ">>>>>>> conflict 1 of 1 ends\n",
        );
        let err = parse_conflicts(input).unwrap_err();
        assert!(matches!(err, Error::Parse { .. }), "structured parse error");
        assert!(
            err.to_string().contains("to:"),
            "error should point at the malformed `to:` line: {err}"
        );
    }

    // H6: a line that *starts* with a `<<<<<<<` run but is not a `conflict N of M`
    // header is content, not a "git-style file" error — jj lengthens its own markers
    // past marker-like content, so such a line is text.
    #[test]
    fn marker_like_content_line_is_not_rejected() {
        // No real conflict at all → parses as all text (was wrongly an error).
        let plain = "<<<<<<< a line documenting git markers\nmore text\n";
        let segs = parse_conflicts(plain).expect("marker-like content is text, not an error");
        assert!(segs.iter().all(|s| matches!(s, JjConflictSegment::Text(_))));
        assert_eq!(render(&segs), plain, "round-trips");

        // A real jj conflict preceded by a marker-like content line: the content
        // line is text, the real region parses.
        let mixed = concat!(
            "<<<<<<< documentation, not a header\n",
            "<<<<<<< conflict 1 of 1\n",
            "+++++++ aaa 111 \"side-a\"\n",
            "X\n",
            ">>>>>>> conflict 1 of 1 ends\n",
        );
        let segs = parse_conflicts(mixed).expect("parse");
        assert_eq!(render(&segs), mixed, "round-trips");
        assert!(
            segs.iter()
                .any(|s| matches!(s, JjConflictSegment::Conflict(_))),
            "the real region still parses: {segs:?}"
        );
    }

    #[test]
    fn render_roundtrips_exactly() {
        for sample in [DIFF_STYLE, SNAPSHOT_STYLE] {
            let segments = parse_conflicts(sample).expect("parse");
            assert_eq!(render(&segments), sample, "roundtrip");
        }
        // Conflict at EOF without trailing newline still roundtrips.
        let eof = DIFF_STYLE.trim_end_matches("line 3\n");
        let eof = &eof[..eof.len() - 1]; // drop the final newline of the end marker
        let segments = parse_conflicts(eof).expect("parse");
        assert_eq!(render(&segments), eof);
    }

    #[test]
    fn resolve_picks_sides_and_base() {
        let segments = parse_conflicts(DIFF_STYLE).expect("parse");
        assert_eq!(
            resolve(&segments, JjResolution::Side(0)).unwrap(),
            "line 1\nmain line 2\nline 3\n"
        );
        assert_eq!(
            resolve(&segments, JjResolution::Side(1)).unwrap(),
            "line 1\nfeature line 2\nline 3\n"
        );
        assert_eq!(
            resolve(&segments, JjResolution::Base).unwrap(),
            "line 1\nline 2\nline 3\n"
        );
        assert!(resolve(&segments, JjResolution::Side(2)).is_err());
    }

    // jj switches to an explicit trailing-newline representation the moment ANY
    // side lacks a terminating newline (structures captured verbatim from jj
    // 0.42 across all four permutations). `resolve` must reproduce each side's
    // and the base's exact bytes — including a *missing* final newline — and
    // `render` must still round-trip. Regression for the silent corruption where
    // the phantom trailing/context line became a spurious extra blank line.
    fn check_eol(input: &str, side0: &str, side1: &str, base: &str) {
        let segments = parse_conflicts(input).expect("parse");
        assert_eq!(
            render(&segments),
            input,
            "render must round-trip byte-exact"
        );
        assert_eq!(
            resolve(&segments, JjResolution::Side(0)).unwrap(),
            side0,
            "side 0"
        );
        assert_eq!(
            resolve(&segments, JjResolution::Side(1)).unwrap(),
            side1,
            "side 1"
        );
        assert_eq!(
            resolve(&segments, JjResolution::Base).unwrap(),
            base,
            "base"
        );
    }

    #[test]
    fn resolve_honors_missing_terminating_newline() {
        // side-a (snapshot) lacks the terminating newline; side-b via diff.
        check_eol(
            concat!(
                "line 1\n",
                "<<<<<<< conflict 1 of 1\n",
                "+++++++ aaa 111 \"side-a\" (no terminating newline)\n",
                "main 2\n",
                "%%%%%%% diff from: bbb 222 \"base\"\n",
                "\\\\\\\\\\\\\\        to: ccc 333 \"side-b\"\n",
                "-line 2\n",
                "+feat 2\n",
                " \n",
                ">>>>>>> conflict 1 of 1 ends",
            ),
            "line 1\nmain 2", // side-a: no trailing newline (was corrupted to `main 2\n`)
            "line 1\nfeat 2\n", // side-b: keeps its newline, no phantom blank line
            "line 1\nline 2\n", // base
        );
        // side-b (via diff) lacks the terminating newline; side-a via diff.
        check_eol(
            concat!(
                "line 1\n",
                "<<<<<<< conflict 1 of 1\n",
                "%%%%%%% diff from: bbb 222 \"base\"\n",
                "\\\\\\\\\\\\\\        to: aaa 111 \"side-a\"\n",
                "-line 2\n",
                "+main 2\n",
                " \n",
                "+++++++ ccc 333 \"side-b\" (no terminating newline)\n",
                "feat 2\n",
                ">>>>>>> conflict 1 of 1 ends",
            ),
            "line 1\nmain 2\n",
            "line 1\nfeat 2",
            "line 1\nline 2\n",
        );
        // base lacks the terminating newline (both sides keep theirs).
        check_eol(
            concat!(
                "line 1\n",
                "<<<<<<< conflict 1 of 1\n",
                "%%%%%%% diff from: bbb 222 \"base\" (no terminating newline)\n",
                "\\\\\\\\\\\\\\        to: aaa 111 \"side-a\"\n",
                "-line 2\n",
                "+main 2\n",
                "+\n",
                "+++++++ ccc 333 \"side-b\"\n",
                "feat 2\n",
                "\n",
                ">>>>>>> conflict 1 of 1 ends",
            ),
            "line 1\nmain 2\n",
            "line 1\nfeat 2\n",
            "line 1\nline 2", // base: no trailing newline
        );
        // both sides lack a terminating newline (base keeps one).
        check_eol(
            concat!(
                "line 1\n",
                "<<<<<<< conflict 1 of 1\n",
                "%%%%%%% diff from: bbb 222 \"base\"\n",
                "\\\\\\\\\\\\\\        to: aaa 111 \"side-a\" (no terminating newline)\n",
                "-line 2\n",
                "-\n",
                "+main 2\n",
                "+++++++ ccc 333 \"side-b\" (no terminating newline)\n",
                "feat 2\n",
                ">>>>>>> conflict 1 of 1 ends",
            ),
            "line 1\nmain 2",
            "line 1\nfeat 2",
            "line 1\nline 2\n",
        );
    }

    // CRLF variant (captured byte-for-byte from jj 0.42): every marker and pad
    // line ends in `\r\n`. side-a lacks the terminating newline. The materializer
    // must strip a full `\r\n` terminator — a bare-`\n` pop would leave a stray
    // `\r` and corrupt every resolution on Windows files.
    #[test]
    fn resolve_honors_missing_newline_with_crlf() {
        check_eol(
            concat!(
                "line 1\r\n",
                "<<<<<<< conflict 1 of 1\r\n",
                "+++++++ aaa 111 \"side-a\" (no terminating newline)\r\n",
                "main 2\r\n",
                "%%%%%%% diff from: bbb 222 \"base\"\r\n",
                "\\\\\\\\\\\\\\        to: ccc 333 \"side-b\"\r\n",
                "-line 2\r\n",
                "+feat 2\r\n",
                " \r\n",
                ">>>>>>> conflict 1 of 1 ends",
            ),
            "line 1\r\nmain 2",     // side-a: no trailing newline, no stray \r
            "line 1\r\nfeat 2\r\n", // side-b: full CRLF terminator preserved
            "line 1\r\nline 2\r\n", // base
        );
    }

    // A 3-sided (octopus) conflict with one no-terminating-newline side, captured
    // from jj 0.42: jj pads EVERY newline-terminated side uniformly (the ` \n`
    // context lines) and annotates only the no-eol one — so the region-wide
    // `mixed_eol` + per-section strip generalizes past two sides.
    #[test]
    fn resolve_handles_three_sided_conflict_with_missing_newline() {
        let input = concat!(
            "line 1\n",
            "<<<<<<< conflict 1 of 1\n",
            "%%%%%%% diff from: aaa 111 \"base\"\n",
            "\\\\\\\\\\\\\\        to: bbb 222 \"sa\"\n",
            "-line 2\n",
            "+AAA\n",
            " \n",
            "%%%%%%% diff from: aaa 111 \"base\"\n",
            "\\\\\\\\\\\\\\        to: ccc 333 \"sb\"\n",
            "-line 2\n",
            "+BBB\n",
            " \n",
            "+++++++ ddd 444 \"sc\" (no terminating newline)\n",
            "CCC\n",
            ">>>>>>> conflict 1 of 1 ends",
        );
        let segs = parse_conflicts(input).expect("parse");
        assert_eq!(render(&segs), input, "render round-trips");
        assert_eq!(
            resolve(&segs, JjResolution::Side(0)).unwrap(),
            "line 1\nAAA\n"
        );
        assert_eq!(
            resolve(&segs, JjResolution::Side(1)).unwrap(),
            "line 1\nBBB\n"
        );
        assert_eq!(
            resolve(&segs, JjResolution::Side(2)).unwrap(),
            "line 1\nCCC", // the no-eol side, no trailing newline
        );
        assert_eq!(
            resolve(&segs, JjResolution::Base).unwrap(),
            "line 1\nline 2\n"
        );
    }

    // Snapshot-style (`ui.conflict-marker-style = snapshot`) with an explicit
    // `------- base` section and a no-eol side, captured from jj 0.42 — exercises
    // the Base-section role and mixed-mode padding on `-------`/`+++++++` sections.
    #[test]
    fn resolve_handles_snapshot_style_with_missing_newline_and_base() {
        check_eol(
            concat!(
                "line 1\n",
                "<<<<<<< conflict 1 of 1\n",
                "+++++++ aaa 111 \"sa\" (no terminating newline)\n",
                "AAA\n",
                "------- bbb 222 \"base\"\n",
                "line 2\n",
                "\n",
                "+++++++ ccc 333 \"sb\"\n",
                "BBB\n",
                "\n",
                ">>>>>>> conflict 1 of 1 ends",
            ),
            "line 1\nAAA",      // sa: no trailing newline
            "line 1\nBBB\n",    // sb
            "line 1\nline 2\n", // base (from the `-------` section)
        );
    }

    #[test]
    fn multi_region_counters_parse() {
        let two = format!(
            "{}middle\n{}",
            DIFF_STYLE,
            DIFF_STYLE
                .replace("conflict 1 of 1", "conflict 2 of 2")
                .replace("line 1\n", "")
                .replace("line 3\n", "")
        );
        let segments = parse_conflicts(&two).expect("parse");
        let counters: Vec<(u32, u32)> = segments
            .iter()
            .filter_map(|s| match s {
                JjConflictSegment::Conflict(r) => Some((r.number, r.total)),
                _ => None,
            })
            .collect();
        assert_eq!(counters, [(1, 1), (2, 2)]);
    }

    #[test]
    fn git_style_and_malformed_are_rejected() {
        let git_style = "<<<<<<< abc 123 \"side-a\"\nx\n||||||| base\ny\n=======\nz\n>>>>>>> def\n";
        let err = parse_conflicts(git_style).unwrap_err();
        assert!(matches!(err, Error::Parse { .. }), "structured parse error");
        // The message must actively steer the caller to the right parser, not just
        // fail — that redirect is the point of rejecting a git-style file here.
        assert!(
            err.to_string().contains("vcs_git::conflict"),
            "git-style error should redirect to vcs_git::conflict: {err}"
        );
        assert!(parse_conflicts("<<<<<<< conflict 1 of 1\nstray content\n").is_err());
        assert!(has_conflict_markers(DIFF_STYLE));
        assert!(!has_conflict_markers(git_style), "git markers aren't jj's");
    }
}

// Property-based fuzzing of the jj conflict grammar: marker-run slicing, the
// `conflict N of M` counters, the `%%%`/`\\\` diff-section continuation, and
// `apply_diff` must never panic on hostile input, and `render(parse(x)?) == x`
// must hold byte-for-byte.
#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    /// Lines from jj's materialized-conflict vocabulary (diff + snapshot
    /// styles) with variable counters/marker lengths and multibyte content.
    fn conflict_line() -> impl Strategy<Value = String> {
        prop_oneof![
            (1u32..3, 1u32..3).prop_map(|(n, m)| format!("<<<<<<< conflict {n} of {m}\n")),
            (1u32..3, 1u32..3).prop_map(|(n, m)| format!(">>>>>>> conflict {n} of {m} ends\n")),
            Just("%%%%%%% diff from: ab cd \"basé\"\n".to_string()),
            Just("\\\\\\\\\\\\\\        to: ef gh \"side\"\n".to_string()),
            Just("+++++++ ij kl \"side-b\"\n".to_string()),
            Just("------- mn op \"base\"\n".to_string()),
            "[-+ ]?[a-zé]{0,10}\n", // diff/content line incl. multibyte
        ]
    }

    fn conflict_doc() -> impl Strategy<Value = String> {
        prop::collection::vec(conflict_line(), 0..30).prop_map(|lines| lines.concat())
    }

    proptest! {
        #[test]
        fn parse_never_panics_on_arbitrary_text(s in any::<String>()) {
            let _ = has_conflict_markers(&s);
            if let Ok(segments) = parse_conflicts(&s) {
                // Whatever arbitrary text parses must round-trip byte-exact — the
                // load-bearing invariant, asserted on this generator too.
                prop_assert_eq!(render(&segments), s.clone());
                // Exercise the materializers on whatever parsed.
                for seg in &segments {
                    if let JjConflictSegment::Conflict(r) = seg {
                        let _ = r.sides();
                        let _ = r.base();
                    }
                }
            }
        }

        #[test]
        fn parse_never_panics_on_structured_text(s in conflict_doc()) {
            let _ = parse_conflicts(&s);
        }

        #[test]
        fn render_roundtrips_whatever_parses(s in conflict_doc()) {
            if let Ok(segments) = parse_conflicts(&s) {
                prop_assert_eq!(render(&segments), s);
            }
        }
    }
}