Skip to content

Commit

Permalink
files: relax requirement of merge() inputs
Browse files Browse the repository at this point in the history
Most callers have Merge<ContentHunk> or Merge<Vec<u8>>.
  • Loading branch information
yuja committed Jul 17, 2024
1 parent a76b025 commit b8b2d5d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 2 additions & 4 deletions cli/src/merge_tools/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,7 @@ pub fn edit_merge_builtin(
path: &RepoPath,
content: Merge<ContentHunk>,
) -> Result<MergedTreeId, BuiltinToolError> {
let slices = content.map(|ContentHunk(v)| v.as_slice());
let merge_result = files::merge(&slices);
let merge_result = files::merge(&content);
let sections = make_merge_sections(merge_result)?;
let mut input = scm_record::helpers::CrosstermInput;
let recorder = scm_record::Recorder::new(
Expand Down Expand Up @@ -1014,8 +1013,7 @@ mod tests {
let content = extract_as_single_hunk(&merge, store, path)
.block_on()
.unwrap();
let slices = content.map(|ContentHunk(buf)| buf.as_slice());
let merge_result = files::merge(&slices);
let merge_result = files::merge(&content);
let sections = make_merge_sections(merge_result).unwrap();
insta::assert_debug_snapshot!(sections, @r###"
[
Expand Down
3 changes: 1 addition & 2 deletions lib/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ pub fn materialize_merge_result(
single_hunk: &Merge<ContentHunk>,
output: &mut dyn Write,
) -> std::io::Result<()> {
let slices = single_hunk.map(|content| content.0.as_slice());
let merge_result = files::merge(&slices);
let merge_result = files::merge(single_hunk);
match merge_result {
MergeResult::Resolved(content) => {
output.write_all(&content.0)?;
Expand Down
11 changes: 9 additions & 2 deletions lib/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,16 @@ impl<'a> Iterator for DiffLineIterator<'a> {
}
}

// TODO: Maybe ContentHunk can be replaced with BString?
#[derive(PartialEq, Eq, Clone)]
pub struct ContentHunk(pub Vec<u8>);

impl AsRef<[u8]> for ContentHunk {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl Debug for ContentHunk {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
String::from_utf8_lossy(&self.0).fmt(f)
Expand All @@ -156,7 +163,7 @@ pub enum MergeResult {
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
// usually done for 3-way conflicts. Are there better heuristics when there are
// more than 3 parts?
Expand Down Expand Up @@ -213,7 +220,7 @@ mod tests {
}

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]
Expand Down
3 changes: 1 addition & 2 deletions lib/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ pub fn try_resolve_file_conflict(
})?;
Ok(content)
})?;
let slices = contents.map(|content| content.as_slice());
let merge_result = files::merge(&slices);
let merge_result = files::merge(&contents);
match merge_result {
MergeResult::Resolved(merged_content) => {
let id = store.write_file(filename, &mut merged_content.0.as_slice())?;
Expand Down

0 comments on commit b8b2d5d

Please sign in to comment.