ok/jj
1
0
Fork 0
forked from mirrors/jj

files: relax requirement of merge() inputs

Most callers have Merge<ContentHunk> or Merge<Vec<u8>>.
This commit is contained in:
Yuya Nishihara 2024-07-17 20:54:38 +09:00
parent e5b49c7d52
commit 1a387489d9
4 changed files with 13 additions and 10 deletions

View file

@ -601,8 +601,7 @@ pub fn edit_merge_builtin(
path: &RepoPath, path: &RepoPath,
content: Merge<ContentHunk>, content: Merge<ContentHunk>,
) -> Result<MergedTreeId, BuiltinToolError> { ) -> Result<MergedTreeId, BuiltinToolError> {
let slices = content.map(|ContentHunk(v)| v.as_slice()); let merge_result = files::merge(&content);
let merge_result = files::merge(&slices);
let sections = make_merge_sections(merge_result)?; let sections = make_merge_sections(merge_result)?;
let mut input = scm_record::helpers::CrosstermInput; let mut input = scm_record::helpers::CrosstermInput;
let recorder = scm_record::Recorder::new( let recorder = scm_record::Recorder::new(
@ -1014,8 +1013,7 @@ mod tests {
let content = extract_as_single_hunk(&merge, store, path) let content = extract_as_single_hunk(&merge, store, path)
.block_on() .block_on()
.unwrap(); .unwrap();
let slices = content.map(|ContentHunk(buf)| buf.as_slice()); let merge_result = files::merge(&content);
let merge_result = files::merge(&slices);
let sections = make_merge_sections(merge_result).unwrap(); let sections = make_merge_sections(merge_result).unwrap();
insta::assert_debug_snapshot!(sections, @r###" insta::assert_debug_snapshot!(sections, @r###"
[ [

View file

@ -219,8 +219,7 @@ pub fn materialize_merge_result(
single_hunk: &Merge<ContentHunk>, single_hunk: &Merge<ContentHunk>,
output: &mut dyn Write, output: &mut dyn Write,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let slices = single_hunk.map(|content| content.0.as_slice()); let merge_result = files::merge(single_hunk);
let merge_result = files::merge(&slices);
match merge_result { match merge_result {
MergeResult::Resolved(content) => { MergeResult::Resolved(content) => {
output.write_all(&content.0)?; output.write_all(&content.0)?;

View file

@ -141,9 +141,16 @@ impl<'a> Iterator for DiffLineIterator<'a> {
} }
} }
// TODO: Maybe ContentHunk can be replaced with BString?
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct ContentHunk(pub Vec<u8>); pub struct ContentHunk(pub Vec<u8>);
impl AsRef<[u8]> for ContentHunk {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl Debug for ContentHunk { impl Debug for ContentHunk {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
String::from_utf8_lossy(&self.0).fmt(f) String::from_utf8_lossy(&self.0).fmt(f)
@ -156,7 +163,7 @@ pub enum MergeResult {
Conflict(Vec<Merge<ContentHunk>>), Conflict(Vec<Merge<ContentHunk>>),
} }
pub fn merge(slices: &Merge<&[u8]>) -> MergeResult { pub fn merge<T: AsRef<[u8]>>(slices: &Merge<T>) -> MergeResult {
// TODO: Using the first remove as base (first in the inputs) is how it's // TODO: Using the first remove as base (first in the inputs) is how it's
// usually done for 3-way conflicts. Are there better heuristics when there are // usually done for 3-way conflicts. Are there better heuristics when there are
// more than 3 parts? // more than 3 parts?
@ -213,7 +220,7 @@ mod tests {
} }
fn merge(removes: &[&[u8]], adds: &[&[u8]]) -> MergeResult { fn merge(removes: &[&[u8]], adds: &[&[u8]]) -> MergeResult {
super::merge(&Merge::from_removes_adds(removes.to_vec(), adds.to_vec())) super::merge(&Merge::from_removes_adds(removes, adds))
} }
#[test] #[test]

View file

@ -454,8 +454,7 @@ pub fn try_resolve_file_conflict(
})?; })?;
Ok(content) Ok(content)
})?; })?;
let slices = contents.map(|content| content.as_slice()); let merge_result = files::merge(&contents);
let merge_result = files::merge(&slices);
match merge_result { match merge_result {
MergeResult::Resolved(merged_content) => { MergeResult::Resolved(merged_content) => {
let id = store.write_file(filename, &mut merged_content.0.as_slice())?; let id = store.write_file(filename, &mut merged_content.0.as_slice())?;