Skip to content

Commit

Permalink
merge: correctly identify conflicts
Browse files Browse the repository at this point in the history
Fix a bug and correctly identify merge conflicts between inserts and
deletes.
  • Loading branch information
bmwill committed Jun 14, 2024
1 parent aafb635 commit a1bef7b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/merge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,14 @@ fn create_merge_range<'ancestor, 'ours, 'theirs, T: ?Sized + SliceLike>(
(None, Some(ours), None) => Some(MergeRange::Ours(ours)),
(None, None, Some(theirs)) => Some(MergeRange::Theirs(theirs)),

(Some(_), Some(_), None)
| (Some(_), None, Some(_))
| (Some(_), None, None)
| (None, None, None) => None,
(Some(ancestor), None, Some(theirs)) => {
Some(MergeRange::Conflict(ancestor, Range::empty(), theirs))
}
(Some(ancestor), Some(ours), None) => {
Some(MergeRange::Conflict(ancestor, ours, Range::empty()))
}

(Some(_), None, None) | (None, None, None) => None,
}
}

Expand Down
61 changes: 61 additions & 0 deletions src/merge/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,64 @@ class Call(models.Model):

assert_merge!(base, ours, theirs, Ok(expected), "MergeRange::Both case");
}

#[test]
fn delete_and_insert_conflict() {
let base = r#"
{
int a = 2;
}
"#;

let ours = r#"
{
}
"#;

let theirs = r#"
{
int a = 2;
int b = 3;
}
"#;

let expected = r#"
{
<<<<<<< ours
||||||| original
int a = 2;
=======
int a = 2;
int b = 3;
>>>>>>> theirs
}
"#;

assert_merge!(
base,
ours,
theirs,
Err(expected),
"MergeRange (Ours::delete, Theirs::insert) conflict"
);

let expected = r#"
{
<<<<<<< ours
int a = 2;
int b = 3;
||||||| original
int a = 2;
=======
>>>>>>> theirs
}
"#;

assert_merge!(
base,
theirs,
ours,
Err(expected),
"MergeRange (Theirs::delete, Ours::insert) conflict"
);
}

0 comments on commit a1bef7b

Please sign in to comment.