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

View file

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

View file

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

View file

@ -32,7 +32,7 @@ use pollster::FutureExt;
use crate::backend::BackendError;
use crate::backend::CommitId;
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::MaterializedTreeValue;
use crate::diff::Diff;
@ -358,16 +358,8 @@ fn get_file_contents(
})?;
Ok(file_contents)
}
MaterializedTreeValue::FileConflict { id, contents, .. } => {
let mut materialized_conflict_buffer = Vec::new();
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)
MaterializedTreeValue::FileConflict { contents, .. } => {
Ok(materialize_merge_result_to_bytes(&contents).into())
}
_ => 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<()> {
let num_conflicts = hunks
.iter()

View file

@ -42,7 +42,7 @@ use crate::backend::ChangeId;
use crate::backend::CommitId;
use crate::backend::MillisSinceEpoch;
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::MaterializedTreeValue;
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::GitSubmodule(_) => Ok(vec![]),
MaterializedTreeValue::FileConflict { contents, .. } => {
let mut content = vec![];
materialize_merge_result(&contents, &mut content)
.expect("Failed to materialize conflict to in-memory buffer");
Ok(content)
Ok(materialize_merge_result_to_bytes(&contents).into())
}
MaterializedTreeValue::OtherConflict { .. } => Ok(vec![]),
MaterializedTreeValue::Tree(id) => {

View file

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

View file

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