conflicts: remove redundant num_removes argument from parse_conflict()

Merges always have exactly one more "adds" than "removes" these days.
This commit is contained in:
Martin von Zweigbergk 2023-08-12 09:39:08 -07:00 committed by Martin von Zweigbergk
parent 72271c0d1f
commit 0b3b62a777
2 changed files with 7 additions and 18 deletions

View file

@ -186,16 +186,12 @@ fn diff_size(hunks: &[DiffHunk]) -> usize {
}
/// Parses conflict markers from a slice. Returns None if there were no valid
/// conflict markers. The caller has to provide the expected number of removed
/// and added inputs to the conflicts. Conflict markers that are otherwise valid
/// will be considered invalid if they don't have the expected arity.
/// conflict markers. The caller has to provide the expected number of merge
/// sides (adds). Conflict markers that are otherwise valid will be considered
/// invalid if they don't have the expected arity.
// TODO: "parse" is not usually the opposite of "materialize", so maybe we
// should rename them to "serialize" and "deserialize"?
pub fn parse_conflict(
input: &[u8],
num_removes: usize,
num_adds: usize,
) -> Option<Vec<Merge<ContentHunk>>> {
pub fn parse_conflict(input: &[u8], num_sides: usize) -> Option<Vec<Merge<ContentHunk>>> {
if input.is_empty() {
return None;
}
@ -209,7 +205,7 @@ pub fn parse_conflict(
} else if conflict_start.is_some() && line == CONFLICT_END_LINE {
let conflict_body = &input[conflict_start.unwrap() + CONFLICT_START_LINE.len()..pos];
let hunk = parse_conflict_hunk(conflict_body);
if hunk.removes().len() == num_removes && hunk.adds().len() == num_adds {
if hunk.removes().len() + 1 == num_sides && hunk.adds().len() == num_sides {
let resolved_slice = &input[resolved_start..conflict_start.unwrap()];
if !resolved_slice.is_empty() {
hunks.push(Merge::resolved(ContentHunk(resolved_slice.to_vec())));
@ -317,8 +313,7 @@ pub fn update_from_content(
let mut removed_content = vec![vec![]; file_ids.removes().len()];
let mut added_content = vec![vec![]; file_ids.adds().len()];
let Some(hunks) = parse_conflict(content, file_ids.removes().len(), file_ids.adds().len())
else {
let Some(hunks) = parse_conflict(content, file_ids.adds().len()) else {
// Either there are no self markers of they don't have the expected arity
let file_id = store.write_file(path, &mut &content[..])?;
return Ok(Merge::normal(file_id));

View file

@ -299,7 +299,7 @@ line 5 right
// The first add should always be from the left side
insta::assert_debug_snapshot!(
parse_conflict(materialized.as_bytes(), conflict.removes().len(), conflict.adds().len()),
parse_conflict(materialized.as_bytes(), conflict.adds().len()),
@r###"
Some(
[
@ -436,7 +436,6 @@ line 3
line 4
line 5
",
1,
2
),
None
@ -459,7 +458,6 @@ right
>>>>>>>
line 5
",
1,
2
),
@r###"
@ -513,7 +511,6 @@ right
>>>>>>>
line 5
",
2,
3
),
@r###"
@ -564,7 +561,6 @@ right
>>>>>>>
line 5
",
2,
3
),
None
@ -587,7 +583,6 @@ right
>>>>>>>
line 5
",
1,
2
),
None
@ -611,7 +606,6 @@ right
>>>>>>>
line 5
",
1,
2
),
None