From 9946e52fdfa5b2f553bf2789018b307889677411 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 21 Sep 2023 11:39:41 -0700 Subject: [PATCH] tree: leverage `Merge::try_map()` when reading file contents to merge --- lib/src/tree.rs | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 73ced72a0..a977bd8ba 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -433,9 +433,7 @@ pub fn try_resolve_file_conflict( executable, })); } - let mut removed_contents = vec![]; - let mut added_contents = vec![]; - for &file_id in file_id_conflict.removes() { + let contents = file_id_conflict.try_map(|&file_id| -> Result, TreeMergeError> { let mut content = vec![]; store .read_file(filename, file_id)? @@ -444,23 +442,11 @@ pub fn try_resolve_file_conflict( source: err, file_id: file_id.clone(), })?; - removed_contents.push(content); - } - for &file_id in file_id_conflict.adds() { - let mut content = vec![]; - store - .read_file(filename, file_id)? - .read_to_end(&mut content) - .map_err(|err| TreeMergeError::ReadError { - source: err, - file_id: file_id.clone(), - })?; - added_contents.push(content); - } - let merge_result = files::merge( - &removed_contents.iter().map(Vec::as_slice).collect_vec(), - &added_contents.iter().map(Vec::as_slice).collect_vec(), - ); + Ok(content) + })?; + let removed_contents = contents.removes().iter().map(Vec::as_slice).collect_vec(); + let added_contents = contents.adds().iter().map(Vec::as_slice).collect_vec(); + let merge_result = files::merge(&removed_contents, &added_contents); match merge_result { MergeResult::Resolved(merged_content) => { let id = store.write_file(filename, &mut merged_content.0.as_slice())?;