| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Conflict markers, in the style git writes them: |
| 5 | //! |
| 6 | //! ```text |
| 7 | //! <<<<<<< ours label |
| 8 | //! the lines of ours |
| 9 | //! ======= |
| 10 | //! the lines of theirs |
| 11 | //! >>>>>>> theirs label |
| 12 | //! ``` |
| 13 | //! |
| 14 | //! The regions come from the engine's three-way merge, so they are exactly |
| 15 | //! the hunks a conflict reports. The parts between them are merged again on |
| 16 | //! their own, which gives the clean result the whole merge would have had. |
| 17 | |
| 18 | use braid_engine::{ConflictHunk, MergeResult, merge3}; |
| 19 | |
| 20 | /// The first line of a conflict region starts with this. |
| 21 | const OURS: &[u8] = b"<<<<<<<"; |
| 22 | /// The line between the two sides. |
| 23 | const SEPARATOR: &[u8] = b"======="; |
| 24 | /// The last line of a conflict region starts with this. |
| 25 | const THEIRS: &[u8] = b">>>>>>>"; |
| 26 | |
| 27 | /// `ours` and `theirs` merged against `base`, with each conflicting region |
| 28 | /// written between markers, or `None` when the content cannot be merged line |
| 29 | /// by line (binary content) or merges cleanly. |
| 30 | pub(crate) fn render( |
| 31 | base: &[u8], |
| 32 | ours: &[u8], |
| 33 | theirs: &[u8], |
| 34 | ours_label: &str, |
| 35 | theirs_label: &str, |
| 36 | ) -> Option<Vec<u8>> { |
| 37 | let MergeResult::Conflict { hunks } = merge3(base, ours, theirs) else { |
| 38 | return None; |
| 39 | }; |
| 40 | if hunks.is_empty() { |
| 41 | return None; |
| 42 | } |
| 43 | let base_lines = lines(base); |
| 44 | let ours_lines = lines(ours); |
| 45 | let theirs_lines = lines(theirs); |
| 46 | let mut out = Vec::with_capacity(ours.len() + theirs.len()); |
| 47 | let (mut b, mut o, mut t) = (0, 0, 0); |
| 48 | for hunk in &hunks { |
| 49 | let ConflictHunk { |
| 50 | base: hb, |
| 51 | ours: ho, |
| 52 | theirs: ht, |
| 53 | } = hunk; |
| 54 | merge_between( |
| 55 | &mut out, |
| 56 | &base_lines[b..hb.lines.start], |
| 57 | &ours_lines[o..ho.lines.start], |
| 58 | &theirs_lines[t..ht.lines.start], |
| 59 | ours_label, |
| 60 | theirs_label, |
| 61 | ); |
| 62 | region(&mut out, &ho.text, &ht.text, ours_label, theirs_label); |
| 63 | (b, o, t) = (hb.lines.end, ho.lines.end, ht.lines.end); |
| 64 | } |
| 65 | merge_between( |
| 66 | &mut out, |
| 67 | &base_lines[b..], |
| 68 | &ours_lines[o..], |
| 69 | &theirs_lines[t..], |
| 70 | ours_label, |
| 71 | theirs_label, |
| 72 | ); |
| 73 | Some(out) |
| 74 | } |
| 75 | |
| 76 | /// Whether `content` still holds a line that starts a conflict region, |
| 77 | /// separates its sides or ends it. |
| 78 | pub(crate) fn has_markers(content: &[u8]) -> bool { |
| 79 | lines(content).iter().any(|line| { |
| 80 | let line = line.strip_suffix(b"\n").unwrap_or(line); |
| 81 | let line = line.strip_suffix(b"\r").unwrap_or(line); |
| 82 | line == SEPARATOR |
| 83 | || [OURS, THEIRS].iter().any(|marker| { |
| 84 | line.starts_with(marker) |
| 85 | && (line.len() == marker.len() || line[marker.len()] == b' ') |
| 86 | }) |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | /// Lines, each with its `\n`; a last line without one is a line too. |
| 91 | fn lines(content: &[u8]) -> Vec<&[u8]> { |
| 92 | content.split_inclusive(|byte| *byte == b'\n').collect() |
| 93 | } |
| 94 | |
| 95 | /// The lines between two conflict regions, which merged cleanly as part of |
| 96 | /// the whole; merged again on their own. Should that disagree, the stretch |
| 97 | /// is shown as a conflict too, rather than guessed. |
| 98 | fn merge_between( |
| 99 | out: &mut Vec<u8>, |
| 100 | base: &[&[u8]], |
| 101 | ours: &[&[u8]], |
| 102 | theirs: &[&[u8]], |
| 103 | ours_label: &str, |
| 104 | theirs_label: &str, |
| 105 | ) { |
| 106 | let (base, ours, theirs) = (base.concat(), ours.concat(), theirs.concat()); |
| 107 | match merge3(&base, &ours, &theirs) { |
| 108 | MergeResult::Clean(merged) => out.extend_from_slice(&merged), |
| 109 | MergeResult::Conflict { .. } => region(out, &ours, &theirs, ours_label, theirs_label), |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn region(out: &mut Vec<u8>, ours: &[u8], theirs: &[u8], ours_label: &str, theirs_label: &str) { |
| 114 | marker_line(out, OURS, ours_label); |
| 115 | side(out, ours); |
| 116 | out.extend_from_slice(SEPARATOR); |
| 117 | out.push(b'\n'); |
| 118 | side(out, theirs); |
| 119 | marker_line(out, THEIRS, theirs_label); |
| 120 | } |
| 121 | |
| 122 | fn marker_line(out: &mut Vec<u8>, marker: &[u8], label: &str) { |
| 123 | out.extend_from_slice(marker); |
| 124 | if !label.is_empty() { |
| 125 | out.push(b' '); |
| 126 | out.extend_from_slice(label.replace('\n', " ").as_bytes()); |
| 127 | } |
| 128 | out.push(b'\n'); |
| 129 | } |
| 130 | |
| 131 | /// One side of a region, ending with a newline so the next marker starts a |
| 132 | /// line. |
| 133 | fn side(out: &mut Vec<u8>, text: &[u8]) { |
| 134 | out.extend_from_slice(text); |
| 135 | if !text.is_empty() && !text.ends_with(b"\n") { |
| 136 | out.push(b'\n'); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[cfg(test)] |
| 141 | mod tests { |
| 142 | use super::*; |
| 143 | |
| 144 | fn text(bytes: Option<Vec<u8>>) -> String { |
| 145 | String::from_utf8(bytes.expect("a conflict")).unwrap() |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn one_region_between_clean_parts() { |
| 150 | let base = "1\n2\n3\n4\n5\n6\n7\n"; |
| 151 | let ours = "ONE\n2\n3\nours\n5\n6\n7\n"; |
| 152 | let theirs = "1\n2\n3\ntheirs\n5\n6\nSEVEN\n"; |
| 153 | let merged = text(render( |
| 154 | base.as_bytes(), |
| 155 | ours.as_bytes(), |
| 156 | theirs.as_bytes(), |
| 157 | "main", |
| 158 | "mine", |
| 159 | )); |
| 160 | assert_eq!( |
| 161 | merged, |
| 162 | "ONE\n2\n3\n<<<<<<< main\nours\n=======\ntheirs\n>>>>>>> mine\n5\n6\nSEVEN\n" |
| 163 | ); |
| 164 | assert!(has_markers(merged.as_bytes())); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn several_regions_and_a_last_line_without_newline() { |
| 169 | let base = "a\nb\nc\nd\ne\nf"; |
| 170 | let ours = "A\nb\nc\nd\ne\nF"; |
| 171 | let theirs = "a2\nb\nc\nd\ne\nf2"; |
| 172 | let merged = text(render( |
| 173 | base.as_bytes(), |
| 174 | ours.as_bytes(), |
| 175 | theirs.as_bytes(), |
| 176 | "o", |
| 177 | "t", |
| 178 | )); |
| 179 | assert_eq!( |
| 180 | merged, |
| 181 | "<<<<<<< o\nA\n=======\na2\n>>>>>>> t\nb\nc\nd\ne\n<<<<<<< o\nF\n=======\nf2\n>>>>>>> t\n" |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn nothing_to_mark() { |
| 187 | assert_eq!(render(b"a\n", b"b\n", b"a\n", "o", "t"), None); |
| 188 | assert_eq!(render(b"\0a", b"\0b", b"\0c", "o", "t"), None); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn marker_detection() { |
| 193 | assert!(!has_markers(b"plain\ntext\n")); |
| 194 | assert!(!has_markers(b"<<<<<<<< eight\n========\n")); |
| 195 | assert!(!has_markers(b"a <<<<<<< b\n")); |
| 196 | assert!(has_markers(b"x\n=======\r\ny\n")); |
| 197 | assert!(has_markers(b">>>>>>> theirs")); |
| 198 | assert!(has_markers(b"<<<<<<<\n")); |
| 199 | } |
| 200 | } |