use std::ops::Range;
use similar::{Algorithm, DiffTag, capture_diff_slices};
const MAX_EXTRA_PASSES: usize = 8;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Change {
pub clean: bool,
pub line: usize,
pub yours: String,
pub default: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Merge {
pub text: String,
pub base: String,
pub changes: Vec<Change>,
pub kept: usize,
pub base_lines: usize,
}
impl Merge {
pub fn applied(&self) -> impl Iterator<Item = &Change> {
self.changes.iter().filter(|c| c.clean)
}
pub fn conflicts(&self) -> impl Iterator<Item = &Change> {
self.changes.iter().filter(|c| !c.clean)
}
}
struct Hunk {
base: Range<usize>,
side: Range<usize>,
}
struct Chunk<'h> {
base: Range<usize>,
yours: Vec<&'h Hunk>,
new: Vec<&'h Hunk>,
}
pub fn merge(base: &str, yours: &str, new: &str) -> Merge {
let mut merged = merge_once(base, yours, new);
for _ in 0..MAX_EXTRA_PASSES {
let next = merge_once(&merged.base, &merged.text, new);
if next.applied().next().is_none() {
break;
}
let earlier = merged.changes.into_iter().filter(|c| c.clean);
merged = Merge {
changes: earlier.chain(next.changes).collect(),
text: next.text,
base: next.base,
..merged
};
}
merged
}
fn merge_once(base: &str, yours: &str, new: &str) -> Merge {
let (b, y, n) = (blocks(base), blocks(yours), blocks(new));
if [&b, &y, &n].into_iter().any(|s| repeats_a_key(s)) {
return merge_lines(base, yours, new);
}
let mut order: Vec<&Key> = n.iter().map(|(k, _)| k).collect();
for (i, (key, _)) in y.iter().enumerate() {
if !order.contains(&key) {
let after = y[..i]
.iter()
.rev()
.find_map(|(prev, _)| order.iter().position(|o| *o == prev));
order.insert(after.map_or(0, |p| p + 1), key);
}
}
let mut merged = Merge {
text: String::new(),
base: String::new(),
changes: Vec::new(),
kept: 0,
base_lines: 0,
};
let mut starts = Vec::with_capacity(y.len());
let mut line_count = 0;
for (key, text) in &y {
starts.push((key, line_count));
line_count += text.split_inclusive('\n').count();
}
let mut after_yours = 0;
for key in order {
let yours = text_of(&y, key);
let start = starts
.iter()
.find(|(k, _)| *k == key)
.map_or(after_yours, |(_, s)| *s);
after_yours = start + yours.split_inclusive('\n').count();
let one = merge_lines(text_of(&b, key), yours, text_of(&n, key));
merged
.changes
.extend(one.changes.into_iter().map(|c| Change {
line: c.line + start,
..c
}));
merged.text.push_str(&one.text);
merged.base.push_str(&one.base);
merged.kept += one.kept;
merged.base_lines += one.base_lines;
}
merged
}
type Key = (String, String);
pub(super) fn blocks(text: &str) -> Vec<(Key, &str)> {
let mut out = Vec::new();
let (mut section, mut key) = (String::new(), (String::new(), String::new()));
let (mut start, mut at) = (0, 0);
for line in text.split_inclusive('\n') {
let next = if let Some(top) = top_level_key(line) {
top.clone_into(&mut section);
Some((section.clone(), String::new()))
} else {
rule_id(line).map(|id| (section.clone(), id.to_owned()))
};
if let Some(next) = next {
if at > start {
out.push((key, &text[start..at]));
}
(key, start) = (next, at);
}
at += line.len();
}
if at > start {
out.push((key, &text[start..]));
}
out
}
fn top_level_key(line: &str) -> Option<&str> {
let first = line.chars().next()?;
if first.is_whitespace() || matches!(first, '#' | '-') {
return None;
}
let (key, _) = line.split_once(':')?;
let plain = key
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'));
(!key.is_empty() && plain).then_some(key)
}
fn rule_id(line: &str) -> Option<&str> {
let id = line.strip_prefix(" - id:")?.trim();
(!id.is_empty()).then_some(id)
}
fn text_of<'t>(blocks: &[(Key, &'t str)], key: &Key) -> &'t str {
blocks
.iter()
.find(|(k, _)| k == key)
.map_or("", |(_, t)| *t)
}
fn repeats_a_key(blocks: &[(Key, &str)]) -> bool {
blocks
.iter()
.enumerate()
.any(|(i, (key, _))| blocks[..i].iter().any(|(k, _)| k == key))
}
fn merge_lines(base: &str, yours: &str, new: &str) -> Merge {
let base_lines: Vec<&str> = base.split_inclusive('\n').collect();
let your_lines: Vec<&str> = yours.split_inclusive('\n').collect();
let new_lines: Vec<&str> = new.split_inclusive('\n').collect();
let your_hunks = hunks(&base_lines, &your_lines);
let new_hunks = hunks(&base_lines, &new_lines);
let changed: usize = your_hunks.iter().map(|h| h.base.len()).sum();
let mut merged = Merge {
text: String::new(),
base: String::new(),
changes: Vec::new(),
kept: base_lines.len() - changed,
base_lines: base_lines.len(),
};
let mut at = 0;
let mut line = 1;
for chunk in chunks(&your_hunks, &new_hunks) {
let same = base_lines[at..chunk.base.start].concat();
merged.text.push_str(&same);
merged.base.push_str(&same);
line += chunk.base.start - at;
let original = base_lines[chunk.base.clone()].concat();
let (mine, mine_count) = side(&base_lines, &your_lines, &chunk.yours, &chunk.base);
let (theirs, _) = side(&base_lines, &new_lines, &chunk.new, &chunk.base);
let clean = chunk.yours.is_empty() && !chunk.new.is_empty();
if chunk.new.is_empty() {
merged.base.push_str(&original);
} else if mine == theirs {
merged.base.push_str(&theirs);
} else {
merged
.base
.push_str(if clean { &theirs } else { &original });
merged.changes.push(Change {
clean,
line,
yours: mine.clone(),
default: theirs.clone(),
});
}
merged.text.push_str(if clean { &theirs } else { &mine });
line += mine_count;
at = chunk.base.end;
}
let rest = base_lines[at..].concat();
merged.text.push_str(&rest);
merged.base.push_str(&rest);
merged
}
fn hunks(base: &[&str], side: &[&str]) -> Vec<Hunk> {
capture_diff_slices(Algorithm::Myers, base, side)
.iter()
.filter_map(|op| {
let (tag, base, side) = op.as_tag_tuple();
(tag != DiffTag::Equal).then_some(Hunk { base, side })
})
.collect()
}
fn chunks<'h>(yours: &'h [Hunk], new: &'h [Hunk]) -> Vec<Chunk<'h>> {
let mut all: Vec<(bool, &Hunk)> = yours
.iter()
.map(|h| (true, h))
.chain(new.iter().map(|h| (false, h)))
.collect();
all.sort_by_key(|(_, h)| (h.base.start, h.base.end));
let mut chunks: Vec<Chunk<'h>> = Vec::new();
for (mine, hunk) in all {
let joins = chunks
.last()
.is_some_and(|last| hunk.base.start <= last.base.end);
if !joins {
chunks.push(Chunk {
base: hunk.base.clone(),
yours: Vec::new(),
new: Vec::new(),
});
}
let Some(chunk) = chunks.last_mut() else {
continue;
};
chunk.base.end = chunk.base.end.max(hunk.base.end);
if mine {
chunk.yours.push(hunk);
} else {
chunk.new.push(hunk);
}
}
chunks
}
fn side(base: &[&str], side: &[&str], hunks: &[&Hunk], range: &Range<usize>) -> (String, usize) {
let mut text = String::new();
let mut count = 0;
let mut at = range.start;
for hunk in hunks {
text.push_str(&base[at..hunk.base.start].concat());
text.push_str(&side[hunk.side.clone()].concat());
count += hunk.base.start - at + hunk.side.len();
at = hunk.base.end;
}
text.push_str(&base[at..range.end].concat());
(text, count + range.end - at)
}