Skip to content

Commit

Permalink
conflicts: extract materialize_merge_result_to_bytes() helper
Browse files Browse the repository at this point in the history
We have many callers of materialize_merge_result() who just want in-memory
buffer.
  • Loading branch information
yuja committed Nov 20, 2024
1 parent 09d6052 commit b2ca171
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 38 deletions.
20 changes: 6 additions & 14 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 } => {
Expand Down
6 changes: 2 additions & 4 deletions cli/src/merge_tools/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions cli/src/merge_tools/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::new(vec![])
};
assert_eq!(content.num_sides(), 2);
let files: HashMap<&str, &[u8]> = maplit::hashmap! {
Expand Down
13 changes: 13 additions & 0 deletions lib/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions lib/src/default_index/revset_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 2 additions & 4 deletions lib/src/local_working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 } => {
Expand Down
6 changes: 2 additions & 4 deletions lib/tests/test_conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}

0 comments on commit b2ca171

Please sign in to comment.