conflicts: extract inner block of materialize_merge_result()

I'm going to make materialize_merge_result() accept reference type. This patch
extracts large non-generic part to a separate function.
This commit is contained in:
Yuya Nishihara 2024-09-18 19:33:31 +09:00
parent 5cc0bd0950
commit 2aa913b035

View file

@ -14,6 +14,7 @@
#![allow(missing_docs)]
use std::io;
use std::io::Read;
use std::io::Write;
use std::iter::zip;
@ -234,13 +235,15 @@ async fn materialize_tree_value_no_access_denied(
pub fn materialize_merge_result(
single_hunk: &Merge<BString>,
output: &mut dyn Write,
) -> std::io::Result<()> {
) -> io::Result<()> {
let merge_result = files::merge(single_hunk);
match merge_result {
MergeResult::Resolved(content) => {
output.write_all(&content)?;
match &merge_result {
MergeResult::Resolved(content) => output.write_all(content),
MergeResult::Conflict(hunks) => materialize_conflict_hunks(hunks, output),
}
MergeResult::Conflict(hunks) => {
}
fn materialize_conflict_hunks(hunks: &[Merge<BString>], output: &mut dyn Write) -> io::Result<()> {
let num_conflicts = hunks
.iter()
.filter(|hunk| hunk.as_resolved().is_none())
@ -252,9 +255,8 @@ pub fn materialize_merge_result(
} else {
conflict_index += 1;
output.write_all(CONFLICT_START_LINE)?;
output.write_all(
format!(" Conflict {conflict_index} of {num_conflicts}\n").as_bytes(),
)?;
output
.write_all(format!(" Conflict {conflict_index} of {num_conflicts}\n").as_bytes())?;
let mut add_index = 0;
for (base_index, left) in hunk.removes().enumerate() {
// The vast majority of conflicts one actually tries to
@ -292,10 +294,7 @@ pub fn materialize_merge_result(
output.write_all(right1)?;
output.write_all(CONFLICT_DIFF_LINE)?;
output.write_all(
format!(
" Changes from {base_str} to side #{}\n",
add_index + 2
)
format!(" Changes from {base_str} to side #{}\n", add_index + 2)
.as_bytes(),
)?;
write_diff_hunks(&diff2, output)?;
@ -306,8 +305,7 @@ pub fn materialize_merge_result(
output.write_all(CONFLICT_DIFF_LINE)?;
output.write_all(
format!(" Changes from {base_str} to side #{}\n", add_index + 1)
.as_bytes(),
format!(" Changes from {base_str} to side #{}\n", add_index + 1).as_bytes(),
)?;
write_diff_hunks(&diff1, output)?;
add_index += 1;
@ -316,9 +314,7 @@ pub fn materialize_merge_result(
// 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(format!(" Contents of side #{}\n", add_index + 1).as_bytes())?;
output.write_all(slice)?;
}
output.write_all(CONFLICT_END_LINE)?;
@ -327,8 +323,6 @@ pub fn materialize_merge_result(
)?;
}
}
}
}
Ok(())
}