conflicts: extract materialize_merge_result_to_bytes() helper

We have many callers of materialize_merge_result() who just want in-memory
buffer.
This commit is contained in:
Yuya Nishihara 2024-08-19 13:45:10 +09:00
parent 4cdead34b4
commit 59a79fdcc0
8 changed files with 34 additions and 49 deletions

View file

@ -32,7 +32,7 @@ use jj_lib::backend::CommitId;
use jj_lib::backend::CopyRecord; use jj_lib::backend::CopyRecord;
use jj_lib::backend::TreeValue; use jj_lib::backend::TreeValue;
use jj_lib::commit::Commit; use jj_lib::commit::Commit;
use jj_lib::conflicts::materialize_merge_result; use jj_lib::conflicts::materialize_merge_result_to_bytes;
use jj_lib::conflicts::materialized_diff_stream; use jj_lib::conflicts::materialized_diff_stream;
use jj_lib::conflicts::MaterializedTreeDiffEntry; use jj_lib::conflicts::MaterializedTreeDiffEntry;
use jj_lib::conflicts::MaterializedTreeValue; use jj_lib::conflicts::MaterializedTreeValue;
@ -863,15 +863,10 @@ fn diff_content(path: &RepoPath, value: MaterializedTreeValue) -> io::Result<Fil
id: _, id: _,
contents, contents,
executable: _, executable: _,
} => { } => Ok(FileContent {
let mut data = vec![];
materialize_merge_result(&contents, &mut data)
.expect("Failed to materialize conflict to in-memory buffer");
Ok(FileContent {
is_binary: false, is_binary: false,
contents: data, contents: materialize_merge_result_to_bytes(&contents).into(),
}) }),
}
MaterializedTreeValue::OtherConflict { id } => Ok(FileContent { MaterializedTreeValue::OtherConflict { id } => Ok(FileContent {
is_binary: false, is_binary: false,
contents: id.describe().into_bytes(), contents: id.describe().into_bytes(),
@ -1181,12 +1176,9 @@ fn git_diff_part(
} => { } => {
mode = if executable { "100755" } else { "100644" }; mode = if executable { "100755" } else { "100644" };
hash = DUMMY_HASH.to_owned(); hash = DUMMY_HASH.to_owned();
let mut data = vec![];
materialize_merge_result(&contents, &mut data)
.expect("Failed to materialize conflict to in-memory buffer");
content = FileContent { content = FileContent {
is_binary: false, // TODO: are we sure this is never binary? is_binary: false, // TODO: are we sure this is never binary?
contents: data, contents: materialize_merge_result_to_bytes(&contents).into(),
}; };
} }
MaterializedTreeValue::OtherConflict { id } => { MaterializedTreeValue::OtherConflict { id } => {

View file

@ -12,7 +12,7 @@ use jj_lib::backend::BackendResult;
use jj_lib::backend::FileId; use jj_lib::backend::FileId;
use jj_lib::backend::MergedTreeId; use jj_lib::backend::MergedTreeId;
use jj_lib::backend::TreeValue; use jj_lib::backend::TreeValue;
use jj_lib::conflicts::materialize_merge_result; use jj_lib::conflicts::materialize_merge_result_to_bytes;
use jj_lib::conflicts::materialize_tree_value; use jj_lib::conflicts::materialize_tree_value;
use jj_lib::conflicts::MaterializedTreeValue; use jj_lib::conflicts::MaterializedTreeValue;
use jj_lib::diff::Diff; use jj_lib::diff::Diff;
@ -211,9 +211,7 @@ fn read_file_contents(
contents, contents,
executable: _, executable: _,
} => { } => {
let mut buf = Vec::new(); let buf = materialize_merge_result_to_bytes(&contents).into();
materialize_merge_result(&contents, &mut buf)
.expect("Failed to materialize conflict to in-memory buffer");
// TODO: Render the ID somehow? // TODO: Render the ID somehow?
let contents = buf_to_file_contents(None, buf); let contents = buf_to_file_contents(None, buf);
Ok(FileInfo { Ok(FileInfo {

View file

@ -12,7 +12,7 @@ use jj_lib::backend::FileId;
use jj_lib::backend::MergedTreeId; use jj_lib::backend::MergedTreeId;
use jj_lib::backend::TreeValue; use jj_lib::backend::TreeValue;
use jj_lib::conflicts; use jj_lib::conflicts;
use jj_lib::conflicts::materialize_merge_result; use jj_lib::conflicts::materialize_merge_result_to_bytes;
use jj_lib::gitignore::GitIgnoreFile; use jj_lib::gitignore::GitIgnoreFile;
use jj_lib::matchers::Matcher; use jj_lib::matchers::Matcher;
use jj_lib::merge::Merge; use jj_lib::merge::Merge;
@ -157,13 +157,10 @@ pub fn run_mergetool_external(
conflict: MergedTreeValue, conflict: MergedTreeValue,
tree: &MergedTree, tree: &MergedTree,
) -> Result<MergedTreeId, ConflictResolveError> { ) -> Result<MergedTreeId, ConflictResolveError> {
let initial_output_content: Vec<u8> = if editor.merge_tool_edits_conflict_markers { let initial_output_content = if editor.merge_tool_edits_conflict_markers {
let mut materialized_conflict = vec![]; materialize_merge_result_to_bytes(&content)
materialize_merge_result(&content, &mut materialized_conflict)
.expect("Writing to an in-memory buffer should never fail");
materialized_conflict
} else { } else {
vec![] BString::default()
}; };
assert_eq!(content.num_sides(), 2); assert_eq!(content.num_sides(), 2);
let files: HashMap<&str, &[u8]> = maplit::hashmap! { let files: HashMap<&str, &[u8]> = maplit::hashmap! {

View file

@ -32,7 +32,7 @@ use pollster::FutureExt;
use crate::backend::BackendError; use crate::backend::BackendError;
use crate::backend::CommitId; use crate::backend::CommitId;
use crate::commit::Commit; use crate::commit::Commit;
use crate::conflicts::materialize_merge_result; use crate::conflicts::materialize_merge_result_to_bytes;
use crate::conflicts::materialize_tree_value; use crate::conflicts::materialize_tree_value;
use crate::conflicts::MaterializedTreeValue; use crate::conflicts::MaterializedTreeValue;
use crate::diff::Diff; use crate::diff::Diff;
@ -358,16 +358,8 @@ fn get_file_contents(
})?; })?;
Ok(file_contents) Ok(file_contents)
} }
MaterializedTreeValue::FileConflict { id, contents, .. } => { MaterializedTreeValue::FileConflict { contents, .. } => {
let mut materialized_conflict_buffer = Vec::new(); Ok(materialize_merge_result_to_bytes(&contents).into())
materialize_merge_result(&contents, &mut materialized_conflict_buffer).map_err(
|io_err| BackendError::ReadFile {
path: path.to_owned(),
source: Box::new(io_err),
id: id.first().clone().unwrap(),
},
)?;
Ok(materialized_conflict_buffer)
} }
_ => Ok(Vec::new()), _ => Ok(Vec::new()),
} }

View file

@ -243,6 +243,19 @@ pub fn materialize_merge_result<T: AsRef<[u8]>>(
} }
} }
pub fn materialize_merge_result_to_bytes<T: AsRef<[u8]>>(single_hunk: &Merge<T>) -> BString {
let merge_result = files::merge(single_hunk);
match merge_result {
MergeResult::Resolved(content) => content,
MergeResult::Conflict(hunks) => {
let mut output = Vec::new();
materialize_conflict_hunks(&hunks, &mut output)
.expect("writing to an in-memory buffer should never fail");
output.into()
}
}
}
fn materialize_conflict_hunks(hunks: &[Merge<BString>], output: &mut dyn Write) -> io::Result<()> { fn materialize_conflict_hunks(hunks: &[Merge<BString>], output: &mut dyn Write) -> io::Result<()> {
let num_conflicts = hunks let num_conflicts = hunks
.iter() .iter()

View file

@ -42,7 +42,7 @@ use crate::backend::ChangeId;
use crate::backend::CommitId; use crate::backend::CommitId;
use crate::backend::MillisSinceEpoch; use crate::backend::MillisSinceEpoch;
use crate::commit::Commit; use crate::commit::Commit;
use crate::conflicts::materialize_merge_result; use crate::conflicts::materialize_merge_result_to_bytes;
use crate::conflicts::materialize_tree_value; use crate::conflicts::materialize_tree_value;
use crate::conflicts::MaterializedTreeValue; use crate::conflicts::MaterializedTreeValue;
use crate::default_index::AsCompositeIndex; use crate::default_index::AsCompositeIndex;
@ -1346,10 +1346,7 @@ fn to_file_content(path: &RepoPath, value: MaterializedTreeValue) -> BackendResu
MaterializedTreeValue::Symlink { id: _, target } => Ok(target.into_bytes()), MaterializedTreeValue::Symlink { id: _, target } => Ok(target.into_bytes()),
MaterializedTreeValue::GitSubmodule(_) => Ok(vec![]), MaterializedTreeValue::GitSubmodule(_) => Ok(vec![]),
MaterializedTreeValue::FileConflict { contents, .. } => { MaterializedTreeValue::FileConflict { contents, .. } => {
let mut content = vec![]; Ok(materialize_merge_result_to_bytes(&contents).into())
materialize_merge_result(&contents, &mut content)
.expect("Failed to materialize conflict to in-memory buffer");
Ok(content)
} }
MaterializedTreeValue::OtherConflict { .. } => Ok(vec![]), MaterializedTreeValue::OtherConflict { .. } => Ok(vec![]),
MaterializedTreeValue::Tree(id) => { MaterializedTreeValue::Tree(id) => {

View file

@ -61,7 +61,7 @@ use crate::backend::TreeId;
use crate::backend::TreeValue; use crate::backend::TreeValue;
use crate::commit::Commit; use crate::commit::Commit;
use crate::conflicts; use crate::conflicts;
use crate::conflicts::materialize_merge_result; use crate::conflicts::materialize_merge_result_to_bytes;
use crate::conflicts::materialize_tree_value; use crate::conflicts::materialize_tree_value;
use crate::conflicts::MaterializedTreeValue; use crate::conflicts::MaterializedTreeValue;
use crate::file_util::check_symlink_support; use crate::file_util::check_symlink_support;
@ -1597,9 +1597,7 @@ impl TreeState {
contents, contents,
executable, executable,
} => { } => {
let mut data = vec![]; let data = materialize_merge_result_to_bytes(&contents).into();
materialize_merge_result(&contents, &mut data)
.expect("Failed to materialize conflict to in-memory buffer");
self.write_conflict(&disk_path, data, executable)? self.write_conflict(&disk_path, data, executable)?
} }
MaterializedTreeValue::OtherConflict { id } => { MaterializedTreeValue::OtherConflict { id } => {

View file

@ -15,7 +15,7 @@
use indoc::indoc; use indoc::indoc;
use jj_lib::backend::FileId; use jj_lib::backend::FileId;
use jj_lib::conflicts::extract_as_single_hunk; use jj_lib::conflicts::extract_as_single_hunk;
use jj_lib::conflicts::materialize_merge_result; use jj_lib::conflicts::materialize_merge_result_to_bytes;
use jj_lib::conflicts::parse_conflict; use jj_lib::conflicts::parse_conflict;
use jj_lib::conflicts::update_from_content; use jj_lib::conflicts::update_from_content;
use jj_lib::merge::Merge; use jj_lib::merge::Merge;
@ -1063,10 +1063,8 @@ fn materialize_conflict_string(
path: &RepoPath, path: &RepoPath,
conflict: &Merge<Option<FileId>>, conflict: &Merge<Option<FileId>>,
) -> String { ) -> String {
let mut result: Vec<u8> = vec![];
let contents = extract_as_single_hunk(conflict, store, path) let contents = extract_as_single_hunk(conflict, store, path)
.block_on() .block_on()
.unwrap(); .unwrap();
materialize_merge_result(&contents, &mut result).unwrap(); String::from_utf8(materialize_merge_result_to_bytes(&contents).into()).unwrap()
String::from_utf8(result).unwrap()
} }