merge: add Merge::num_sides()

An alternative name for it would be `arity()`, but `num_sides()`
probably more clearly says that it's not about the number of removes
or the total number of terms.
This commit is contained in:
Martin von Zweigbergk 2023-08-23 18:41:48 -07:00 committed by Martin von Zweigbergk
parent f0efdf116e
commit f877610792
5 changed files with 11 additions and 6 deletions

View file

@ -2787,7 +2787,7 @@ fn print_conflicted_paths(
.map(|p| format!("{:width$}", p, width = max_path_len.min(32) + 3));
for ((_, conflict), formatted_path) in std::iter::zip(conflicts.iter(), formatted_paths) {
let sides = conflict.adds().len();
let sides = conflict.num_sides();
let n_adds = conflict.adds().iter().flatten().count();
let deletions = sides - n_adds;

View file

@ -98,10 +98,10 @@ pub fn run_mergetool(
)
})?;
// We only support conflicts with 2 sides (3-way conflicts)
if file_merge.adds().len() > 2 {
if file_merge.num_sides() > 2 {
return Err(ConflictResolveError::ConflictTooComplicated {
path: repo_path.clone(),
sides: file_merge.adds().len(),
sides: file_merge.num_sides(),
});
};
let content = extract_as_single_hunk(&file_merge, tree.store(), repo_path);

View file

@ -206,7 +206,7 @@ pub fn parse_conflict(input: &[u8], num_sides: usize) -> Option<Vec<Merge<Conten
} 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.adds().len() == num_sides {
if hunk.num_sides() == 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())));
@ -314,7 +314,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.adds().len()) else {
let Some(hunks) = parse_conflict(content, file_ids.num_sides()) 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

@ -154,6 +154,11 @@ impl<T> Merge<T> {
&self.adds
}
/// The number of positive terms in the conflict.
pub fn num_sides(&self) -> usize {
self.adds.len()
}
/// Whether this merge is resolved. Does not resolve trivial merges.
pub fn is_resolved(&self) -> bool {
self.removes.is_empty()

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.adds().len()),
parse_conflict(materialized.as_bytes(), conflict.num_sides()),
@r###"
Some(
[