Skip to content

Commit

Permalink
conflicts: extract header output into separate functions
Browse files Browse the repository at this point in the history
This will make it easier to add separate logic for "snapshot" style
conflict markers in the next commit.
  • Loading branch information
scott2000 committed Nov 22, 2024
1 parent 47a2bad commit 1d39636
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions lib/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ fn materialize_conflict_hunks(
let Some(right1) = hunk.get_add(add_index) else {
// If we have no more positive terms, emit the remaining negative
// terms as snapshots.
output.write_all(CONFLICT_MINUS_LINE)?;
output.write_all(format!(" Contents of {base_str}\n").as_bytes())?;
output.write_all(left)?;
write_base(&base_str, left, output)?;
continue;
};
let diff1 = Diff::by_line([&left, &right1]).hunks().collect_vec();
Expand All @@ -316,35 +314,20 @@ fn materialize_conflict_hunks(
// If the next positive term is a better match, emit
// the current positive term as a snapshot and the next
// positive term as a diff.
output.write_all(CONFLICT_PLUS_LINE)?;
output.write_all(
format!(" Contents of side #{}\n", add_index + 1).as_bytes(),
)?;
output.write_all(right1)?;
output.write_all(CONFLICT_DIFF_LINE)?;
output.write_all(
format!(" Changes from {base_str} to side #{}\n", add_index + 2)
.as_bytes(),
)?;
write_diff_hunks(&diff2, output)?;
write_side(add_index, right1, output)?;
write_diff(&base_str, add_index + 1, &diff2, output)?;
add_index += 2;
continue;
}
}

output.write_all(CONFLICT_DIFF_LINE)?;
output.write_all(
format!(" Changes from {base_str} to side #{}\n", add_index + 1).as_bytes(),
)?;
write_diff_hunks(&diff1, output)?;
write_diff(&base_str, add_index, &diff1, output)?;
add_index += 1;
}

// Emit the remaining positive terms as snapshots.
// Emit the remaining positive terms as snapshots.
for (add_index, slice) in hunk.adds().enumerate().skip(add_index) {
output.write_all(CONFLICT_PLUS_LINE)?;
output.write_all(format!(" Contents of side #{}\n", add_index + 1).as_bytes())?;
output.write_all(slice)?;
write_side(add_index, slice, output)?;
}
output.write_all(CONFLICT_END_LINE)?;
output.write_all(
Expand All @@ -355,6 +338,33 @@ fn materialize_conflict_hunks(
Ok(())
}

/// Write a positive snapshot (side) of a conflict
fn write_side(add_index: usize, data: &[u8], output: &mut dyn Write) -> std::io::Result<()> {
output.write_all(CONFLICT_PLUS_LINE)?;
output.write_all(format!(" Contents of side #{}\n", add_index + 1).as_bytes())?;
output.write_all(data)
}

/// Write a negative snapshot (base) of a conflict
fn write_base(base_str: &str, data: &[u8], output: &mut dyn Write) -> std::io::Result<()> {
output.write_all(CONFLICT_MINUS_LINE)?;
output.write_all(format!(" Contents of {base_str}\n").as_bytes())?;
output.write_all(data)
}

/// Write a diff from a negative term to a positive term
fn write_diff(
base_str: &str,
add_index: usize,
diff: &[DiffHunk],
output: &mut dyn Write,
) -> std::io::Result<()> {
output.write_all(CONFLICT_DIFF_LINE)?;
output
.write_all(format!(" Changes from {base_str} to side #{}\n", add_index + 1).as_bytes())?;
write_diff_hunks(diff, output)
}

fn diff_size(hunks: &[DiffHunk]) -> usize {
hunks
.iter()
Expand Down

0 comments on commit 1d39636

Please sign in to comment.