From 5eab7b7ede5e9c128f3d57bf5bccb7090ec22614 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Mon, 19 Aug 2024 20:07:46 -0300 Subject: [PATCH 1/4] fix: Incorrect merge algorithm --- .../incorrect_merge_conflict/base.java | 15 + .../incorrect_merge_conflict/left.java | 16 + .../incorrect_merge_conflict/merge.java | 7 + .../incorrect_merge_conflict/right.java | 9 + merge/src/ordered_merge.rs | 315 +++++++++--------- 5 files changed, 205 insertions(+), 157 deletions(-) create mode 100644 bin/tests/scenarios/incorrect_merge_conflict/base.java create mode 100644 bin/tests/scenarios/incorrect_merge_conflict/left.java create mode 100644 bin/tests/scenarios/incorrect_merge_conflict/merge.java create mode 100644 bin/tests/scenarios/incorrect_merge_conflict/right.java diff --git a/bin/tests/scenarios/incorrect_merge_conflict/base.java b/bin/tests/scenarios/incorrect_merge_conflict/base.java new file mode 100644 index 0000000..0aa1066 --- /dev/null +++ b/bin/tests/scenarios/incorrect_merge_conflict/base.java @@ -0,0 +1,15 @@ +public class Main { + public static void main() { + return new PagedIterable() { + public PagedIterator _iterator(int pageSize) { + return new PagedIterator(retrieve().with("since",since).asIterator("/repositories", GHRepository[].class, pageSize)) { + @Override + protected void wrapUp(GHRepository[] page) { + for (GHRepository c : page) + c.wrap(GitHub.this); + } + }; + } + }; + } +} \ No newline at end of file diff --git a/bin/tests/scenarios/incorrect_merge_conflict/left.java b/bin/tests/scenarios/incorrect_merge_conflict/left.java new file mode 100644 index 0000000..e1b5516 --- /dev/null +++ b/bin/tests/scenarios/incorrect_merge_conflict/left.java @@ -0,0 +1,16 @@ +public class Main { + public static void main() { + return new PagedIterable() { + @Override + public PagedIterator _iterator(int pageSize) { + return new PagedIterator(retrieve().with("since",since).asIterator("/repositories", GHRepository[].class, pageSize)) { + @Override + protected void wrapUp(GHRepository[] page) { + for (GHRepository c : page) + c.wrap(GitHub.this); + } + }; + } + }; + } +} \ No newline at end of file diff --git a/bin/tests/scenarios/incorrect_merge_conflict/merge.java b/bin/tests/scenarios/incorrect_merge_conflict/merge.java new file mode 100644 index 0000000..a18e334 --- /dev/null +++ b/bin/tests/scenarios/incorrect_merge_conflict/merge.java @@ -0,0 +1,7 @@ + public class Main { public static void main ( ) { return +<<<<<<< + new PagedIterable < GHRepository > ( ) { @ Override public PagedIterator < GHRepository > _iterator ( int pageSize ) { return new PagedIterator < GHRepository > ( retrieve ( ) . with ( "since" , since ) . asIterator ( "/repositories" , GHRepository [ ] . class , pageSize ) ) { @ Override protected void wrapUp ( GHRepository [ ] page ) { for ( GHRepository c : page ) c . wrap ( GitHub . this ) ; } } ; } } +======= + retrieve ( ) . with ( "since" , since ) . asPagedIterable ( "/repositories" , GHRepository [ ] . class , item -> item . wrap ( GitHub . this ) ) +>>>>>>> + ; } } \ No newline at end of file diff --git a/bin/tests/scenarios/incorrect_merge_conflict/right.java b/bin/tests/scenarios/incorrect_merge_conflict/right.java new file mode 100644 index 0000000..b9793a4 --- /dev/null +++ b/bin/tests/scenarios/incorrect_merge_conflict/right.java @@ -0,0 +1,9 @@ +public class Main { + public static void main() { + return retrieve().with("since",since) + .asPagedIterable( + "/repositories", + GHRepository[].class, + item -> item.wrap(GitHub.this) ); + } +} \ No newline at end of file diff --git a/merge/src/ordered_merge.rs b/merge/src/ordered_merge.rs index 30a8606..e531acb 100644 --- a/merge/src/ordered_merge.rs +++ b/merge/src/ordered_merge.rs @@ -139,13 +139,13 @@ pub fn ordered_merge<'a>( cur_right_option = children_right_it.next(); } (false, None, Some(matching_base_left), None, None) => { - result_children.push(cur_right.into()); - if !matching_base_left.is_perfect_match { result_children.push(MergedCSTNode::Conflict { left: Some(Box::new(cur_left.into())), - right: None, - }) + right: Some(Box::new(cur_right.into())), + }); + } else { + result_children.push(cur_right.into()); } cur_left_option = children_left_it.next(); @@ -160,12 +160,13 @@ pub fn ordered_merge<'a>( cur_left_option = children_left_it.next(); } (false, None, None, None, Some(matching_base_right)) => { - result_children.push(cur_left.into()); if !matching_base_right.is_perfect_match { result_children.push(MergedCSTNode::Conflict { - left: None, + left: Some(Box::new(cur_left.into())), right: Some(Box::new(cur_right.into())), }) + } else { + result_children.push(cur_left.into()); } cur_left_option = children_left_it.next(); @@ -574,157 +575,157 @@ mod tests { ) } - #[test] - fn it_merges_when_one_parent_adds_a_node_and_removes_from_another_that_was_changed( - ) -> Result<(), MergeError> { - let base = CSTNode::NonTerminal(NonTerminal { - id: uuid::Uuid::new_v4(), - kind: "kind", - are_children_unordered: false, - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - children: vec![CSTNode::NonTerminal(NonTerminal { - id: uuid::Uuid::new_v4(), - kind: "subtree", - are_children_unordered: false, - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - children: vec![CSTNode::Terminal(Terminal { - id: uuid::Uuid::new_v4(), - kind: "kind_a", - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - value: "value_a", - is_block_end_delimiter: false, - })], - ..Default::default() - })], - ..Default::default() - }); - - let parent_a = CSTNode::NonTerminal(NonTerminal { - id: uuid::Uuid::new_v4(), - kind: "kind", - are_children_unordered: false, - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - children: vec![CSTNode::NonTerminal(NonTerminal { - id: uuid::Uuid::new_v4(), - kind: "another_subtree", - are_children_unordered: false, - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - children: vec![CSTNode::Terminal(Terminal { - id: uuid::Uuid::new_v4(), - kind: "kind_b", - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - value: "value_b", - is_block_end_delimiter: false, - })], - ..Default::default() - })], - ..Default::default() - }); - - let parent_b = CSTNode::NonTerminal(NonTerminal { - id: uuid::Uuid::new_v4(), - kind: "kind", - are_children_unordered: false, - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - children: vec![CSTNode::NonTerminal(NonTerminal { - id: uuid::Uuid::new_v4(), - kind: "subtree", - are_children_unordered: false, - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - children: vec![CSTNode::Terminal(Terminal { - id: uuid::Uuid::new_v4(), - kind: "kind_c", - start_position: Point { row: 0, column: 0 }, - end_position: Point { row: 0, column: 7 }, - value: "value_c", - is_block_end_delimiter: false, - })], - ..Default::default() - })], - ..Default::default() - }); - - let matchings_base_parent_a = ordered::calculate_matchings(&base, &parent_a); - let matchings_base_parent_b = ordered::calculate_matchings(&base, &parent_b); - let matchings_parents = ordered::calculate_matchings(&parent_a, &parent_b); - - let merged_tree = ordered_merge( - (&parent_a).try_into().unwrap(), - (&parent_b).try_into().unwrap(), - &matchings_base_parent_a, - &matchings_base_parent_b, - &matchings_parents, - )?; - let merged_tree_swap = ordered_merge( - (&parent_b).try_into().unwrap(), - (&parent_a).try_into().unwrap(), - &matchings_base_parent_b, - &matchings_base_parent_a, - &matchings_parents, - )?; - - assert_eq!( - MergedCSTNode::NonTerminal { - kind: "kind", - children: vec![ - MergedCSTNode::NonTerminal { - kind: "another_subtree", - children: vec![MergedCSTNode::Terminal { - kind: "kind_b", - value: Cow::Borrowed("value_b"), - }], - }, - MergedCSTNode::Conflict { - left: None, - right: Some(Box::new(MergedCSTNode::NonTerminal { - kind: "subtree", - children: vec![MergedCSTNode::Terminal { - kind: "kind_c", - value: Cow::Borrowed("value_c"), - }], - })), - }, - ], - }, - merged_tree - ); - - assert_eq!( - MergedCSTNode::NonTerminal { - kind: "kind", - children: vec![ - MergedCSTNode::NonTerminal { - kind: "another_subtree", - children: vec![MergedCSTNode::Terminal { - kind: "kind_b", - value: Cow::Borrowed("value_b"), - }], - }, - MergedCSTNode::Conflict { - left: Some(Box::new(MergedCSTNode::NonTerminal { - kind: "subtree", - children: vec![MergedCSTNode::Terminal { - kind: "kind_c", - value: Cow::Borrowed("value_c"), - }], - })), - right: None, - }, - ], - }, - merged_tree_swap - ); - - Ok(()) - } + // #[test] + // fn it_merges_when_one_parent_adds_a_node_and_removes_from_another_that_was_changed( + // ) -> Result<(), MergeError> { + // let base = CSTNode::NonTerminal(NonTerminal { + // id: uuid::Uuid::new_v4(), + // kind: "kind", + // are_children_unordered: false, + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // children: vec![CSTNode::NonTerminal(NonTerminal { + // id: uuid::Uuid::new_v4(), + // kind: "subtree", + // are_children_unordered: false, + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // children: vec![CSTNode::Terminal(Terminal { + // id: uuid::Uuid::new_v4(), + // kind: "kind_a", + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // value: "value_a", + // is_block_end_delimiter: false, + // })], + // ..Default::default() + // })], + // ..Default::default() + // }); + + // let parent_a = CSTNode::NonTerminal(NonTerminal { + // id: uuid::Uuid::new_v4(), + // kind: "kind", + // are_children_unordered: false, + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // children: vec![CSTNode::NonTerminal(NonTerminal { + // id: uuid::Uuid::new_v4(), + // kind: "another_subtree", + // are_children_unordered: false, + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // children: vec![CSTNode::Terminal(Terminal { + // id: uuid::Uuid::new_v4(), + // kind: "kind_b", + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // value: "value_b", + // is_block_end_delimiter: false, + // })], + // ..Default::default() + // })], + // ..Default::default() + // }); + + // let parent_b = CSTNode::NonTerminal(NonTerminal { + // id: uuid::Uuid::new_v4(), + // kind: "kind", + // are_children_unordered: false, + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // children: vec![CSTNode::NonTerminal(NonTerminal { + // id: uuid::Uuid::new_v4(), + // kind: "subtree", + // are_children_unordered: false, + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // children: vec![CSTNode::Terminal(Terminal { + // id: uuid::Uuid::new_v4(), + // kind: "kind_c", + // start_position: Point { row: 0, column: 0 }, + // end_position: Point { row: 0, column: 7 }, + // value: "value_c", + // is_block_end_delimiter: false, + // })], + // ..Default::default() + // })], + // ..Default::default() + // }); + + // let matchings_base_parent_a = ordered::calculate_matchings(&base, &parent_a); + // let matchings_base_parent_b = ordered::calculate_matchings(&base, &parent_b); + // let matchings_parents = ordered::calculate_matchings(&parent_a, &parent_b); + + // let merged_tree = ordered_merge( + // (&parent_a).try_into().unwrap(), + // (&parent_b).try_into().unwrap(), + // &matchings_base_parent_a, + // &matchings_base_parent_b, + // &matchings_parents, + // )?; + // let merged_tree_swap = ordered_merge( + // (&parent_b).try_into().unwrap(), + // (&parent_a).try_into().unwrap(), + // &matchings_base_parent_b, + // &matchings_base_parent_a, + // &matchings_parents, + // )?; + + // assert_eq!( + // MergedCSTNode::NonTerminal { + // kind: "kind", + // children: vec![ + // MergedCSTNode::NonTerminal { + // kind: "another_subtree", + // children: vec![MergedCSTNode::Terminal { + // kind: "kind_b", + // value: Cow::Borrowed("value_b"), + // }], + // }, + // MergedCSTNode::Conflict { + // left: None, + // right: Some(Box::new(MergedCSTNode::NonTerminal { + // kind: "subtree", + // children: vec![MergedCSTNode::Terminal { + // kind: "kind_c", + // value: Cow::Borrowed("value_c"), + // }], + // })), + // }, + // ], + // }, + // merged_tree + // ); + + // assert_eq!( + // MergedCSTNode::NonTerminal { + // kind: "kind", + // children: vec![ + // MergedCSTNode::NonTerminal { + // kind: "another_subtree", + // children: vec![MergedCSTNode::Terminal { + // kind: "kind_b", + // value: Cow::Borrowed("value_b"), + // }], + // }, + // MergedCSTNode::Conflict { + // left: Some(Box::new(MergedCSTNode::NonTerminal { + // kind: "subtree", + // children: vec![MergedCSTNode::Terminal { + // kind: "kind_c", + // value: Cow::Borrowed("value_c"), + // }], + // })), + // right: None, + // }, + // ], + // }, + // merged_tree_swap + // ); + + // Ok(()) + // } #[test] fn if_both_parents_add_different_nodes_then_we_have_a_conflict() -> Result<(), MergeError> { From f7349dab181e86851642be6b7288f3666b73a376 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Wed, 21 Aug 2024 12:13:03 -0300 Subject: [PATCH 2/4] test: adjust test --- merge/src/ordered_merge.rs | 296 ++++++++++++++++++------------------- 1 file changed, 145 insertions(+), 151 deletions(-) diff --git a/merge/src/ordered_merge.rs b/merge/src/ordered_merge.rs index e531acb..befa450 100644 --- a/merge/src/ordered_merge.rs +++ b/merge/src/ordered_merge.rs @@ -575,157 +575,151 @@ mod tests { ) } - // #[test] - // fn it_merges_when_one_parent_adds_a_node_and_removes_from_another_that_was_changed( - // ) -> Result<(), MergeError> { - // let base = CSTNode::NonTerminal(NonTerminal { - // id: uuid::Uuid::new_v4(), - // kind: "kind", - // are_children_unordered: false, - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // children: vec![CSTNode::NonTerminal(NonTerminal { - // id: uuid::Uuid::new_v4(), - // kind: "subtree", - // are_children_unordered: false, - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // children: vec![CSTNode::Terminal(Terminal { - // id: uuid::Uuid::new_v4(), - // kind: "kind_a", - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // value: "value_a", - // is_block_end_delimiter: false, - // })], - // ..Default::default() - // })], - // ..Default::default() - // }); - - // let parent_a = CSTNode::NonTerminal(NonTerminal { - // id: uuid::Uuid::new_v4(), - // kind: "kind", - // are_children_unordered: false, - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // children: vec![CSTNode::NonTerminal(NonTerminal { - // id: uuid::Uuid::new_v4(), - // kind: "another_subtree", - // are_children_unordered: false, - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // children: vec![CSTNode::Terminal(Terminal { - // id: uuid::Uuid::new_v4(), - // kind: "kind_b", - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // value: "value_b", - // is_block_end_delimiter: false, - // })], - // ..Default::default() - // })], - // ..Default::default() - // }); - - // let parent_b = CSTNode::NonTerminal(NonTerminal { - // id: uuid::Uuid::new_v4(), - // kind: "kind", - // are_children_unordered: false, - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // children: vec![CSTNode::NonTerminal(NonTerminal { - // id: uuid::Uuid::new_v4(), - // kind: "subtree", - // are_children_unordered: false, - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // children: vec![CSTNode::Terminal(Terminal { - // id: uuid::Uuid::new_v4(), - // kind: "kind_c", - // start_position: Point { row: 0, column: 0 }, - // end_position: Point { row: 0, column: 7 }, - // value: "value_c", - // is_block_end_delimiter: false, - // })], - // ..Default::default() - // })], - // ..Default::default() - // }); - - // let matchings_base_parent_a = ordered::calculate_matchings(&base, &parent_a); - // let matchings_base_parent_b = ordered::calculate_matchings(&base, &parent_b); - // let matchings_parents = ordered::calculate_matchings(&parent_a, &parent_b); - - // let merged_tree = ordered_merge( - // (&parent_a).try_into().unwrap(), - // (&parent_b).try_into().unwrap(), - // &matchings_base_parent_a, - // &matchings_base_parent_b, - // &matchings_parents, - // )?; - // let merged_tree_swap = ordered_merge( - // (&parent_b).try_into().unwrap(), - // (&parent_a).try_into().unwrap(), - // &matchings_base_parent_b, - // &matchings_base_parent_a, - // &matchings_parents, - // )?; - - // assert_eq!( - // MergedCSTNode::NonTerminal { - // kind: "kind", - // children: vec![ - // MergedCSTNode::NonTerminal { - // kind: "another_subtree", - // children: vec![MergedCSTNode::Terminal { - // kind: "kind_b", - // value: Cow::Borrowed("value_b"), - // }], - // }, - // MergedCSTNode::Conflict { - // left: None, - // right: Some(Box::new(MergedCSTNode::NonTerminal { - // kind: "subtree", - // children: vec![MergedCSTNode::Terminal { - // kind: "kind_c", - // value: Cow::Borrowed("value_c"), - // }], - // })), - // }, - // ], - // }, - // merged_tree - // ); - - // assert_eq!( - // MergedCSTNode::NonTerminal { - // kind: "kind", - // children: vec![ - // MergedCSTNode::NonTerminal { - // kind: "another_subtree", - // children: vec![MergedCSTNode::Terminal { - // kind: "kind_b", - // value: Cow::Borrowed("value_b"), - // }], - // }, - // MergedCSTNode::Conflict { - // left: Some(Box::new(MergedCSTNode::NonTerminal { - // kind: "subtree", - // children: vec![MergedCSTNode::Terminal { - // kind: "kind_c", - // value: Cow::Borrowed("value_c"), - // }], - // })), - // right: None, - // }, - // ], - // }, - // merged_tree_swap - // ); - - // Ok(()) - // } + #[test] + fn it_merges_when_one_parent_adds_a_node_and_removes_from_another_that_was_changed( + ) -> Result<(), MergeError> { + let base = CSTNode::NonTerminal(NonTerminal { + id: uuid::Uuid::new_v4(), + kind: "kind", + are_children_unordered: false, + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + children: vec![CSTNode::NonTerminal(NonTerminal { + id: uuid::Uuid::new_v4(), + kind: "subtree", + are_children_unordered: false, + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + children: vec![CSTNode::Terminal(Terminal { + id: uuid::Uuid::new_v4(), + kind: "kind_a", + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + value: "value_a", + is_block_end_delimiter: false, + })], + ..Default::default() + })], + ..Default::default() + }); + + let parent_a = CSTNode::NonTerminal(NonTerminal { + id: uuid::Uuid::new_v4(), + kind: "kind", + are_children_unordered: false, + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + children: vec![CSTNode::NonTerminal(NonTerminal { + id: uuid::Uuid::new_v4(), + kind: "another_subtree", + are_children_unordered: false, + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + children: vec![CSTNode::Terminal(Terminal { + id: uuid::Uuid::new_v4(), + kind: "kind_b", + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + value: "value_b", + is_block_end_delimiter: false, + })], + ..Default::default() + })], + ..Default::default() + }); + + let parent_b = CSTNode::NonTerminal(NonTerminal { + id: uuid::Uuid::new_v4(), + kind: "kind", + are_children_unordered: false, + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + children: vec![CSTNode::NonTerminal(NonTerminal { + id: uuid::Uuid::new_v4(), + kind: "subtree", + are_children_unordered: false, + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + children: vec![CSTNode::Terminal(Terminal { + id: uuid::Uuid::new_v4(), + kind: "kind_c", + start_position: Point { row: 0, column: 0 }, + end_position: Point { row: 0, column: 7 }, + value: "value_c", + is_block_end_delimiter: false, + })], + ..Default::default() + })], + ..Default::default() + }); + + let matchings_base_parent_a = ordered::calculate_matchings(&base, &parent_a); + let matchings_base_parent_b = ordered::calculate_matchings(&base, &parent_b); + let matchings_parents = ordered::calculate_matchings(&parent_a, &parent_b); + + let merged_tree = ordered_merge( + (&parent_a).try_into().unwrap(), + (&parent_b).try_into().unwrap(), + &matchings_base_parent_a, + &matchings_base_parent_b, + &matchings_parents, + )?; + let merged_tree_swap = ordered_merge( + (&parent_b).try_into().unwrap(), + (&parent_a).try_into().unwrap(), + &matchings_base_parent_b, + &matchings_base_parent_a, + &matchings_parents, + )?; + + assert_eq!( + MergedCSTNode::NonTerminal { + kind: "kind", + children: vec![MergedCSTNode::Conflict { + left: Some(Box::new(MergedCSTNode::NonTerminal { + kind: "another_subtree", + children: vec![MergedCSTNode::Terminal { + kind: "kind_b", + value: Cow::Borrowed("value_b"), + }], + })), + right: Some(Box::new(MergedCSTNode::NonTerminal { + kind: "subtree", + children: vec![MergedCSTNode::Terminal { + kind: "kind_c", + value: Cow::Borrowed("value_c"), + }], + })), + },], + }, + merged_tree + ); + + assert_eq!( + MergedCSTNode::NonTerminal { + kind: "kind", + children: vec![MergedCSTNode::Conflict { + left: Some(Box::new(MergedCSTNode::NonTerminal { + kind: "subtree", + children: vec![MergedCSTNode::Terminal { + kind: "kind_c", + value: Cow::Borrowed("value_c"), + }], + })), + right: Some(Box::new(MergedCSTNode::NonTerminal { + kind: "another_subtree", + children: vec![MergedCSTNode::Terminal { + kind: "kind_b", + value: Cow::Borrowed("value_b"), + }], + })), + },], + }, + merged_tree_swap + ); + + Ok(()) + } #[test] fn if_both_parents_add_different_nodes_then_we_have_a_conflict() -> Result<(), MergeError> { From 075824c9055957fbd78476ddc1f4ddfcb090849c Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Wed, 21 Aug 2024 12:14:35 -0300 Subject: [PATCH 3/4] style: adjust trailing space --- bin/tests/scenarios/incorrect_merge_conflict/base.java | 2 +- bin/tests/scenarios/incorrect_merge_conflict/left.java | 2 +- bin/tests/scenarios/incorrect_merge_conflict/merge.java | 2 +- bin/tests/scenarios/incorrect_merge_conflict/right.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/tests/scenarios/incorrect_merge_conflict/base.java b/bin/tests/scenarios/incorrect_merge_conflict/base.java index 0aa1066..fd94228 100644 --- a/bin/tests/scenarios/incorrect_merge_conflict/base.java +++ b/bin/tests/scenarios/incorrect_merge_conflict/base.java @@ -12,4 +12,4 @@ protected void wrapUp(GHRepository[] page) { } }; } -} \ No newline at end of file +} diff --git a/bin/tests/scenarios/incorrect_merge_conflict/left.java b/bin/tests/scenarios/incorrect_merge_conflict/left.java index e1b5516..2535508 100644 --- a/bin/tests/scenarios/incorrect_merge_conflict/left.java +++ b/bin/tests/scenarios/incorrect_merge_conflict/left.java @@ -13,4 +13,4 @@ protected void wrapUp(GHRepository[] page) { } }; } -} \ No newline at end of file +} diff --git a/bin/tests/scenarios/incorrect_merge_conflict/merge.java b/bin/tests/scenarios/incorrect_merge_conflict/merge.java index a18e334..ad6b7ca 100644 --- a/bin/tests/scenarios/incorrect_merge_conflict/merge.java +++ b/bin/tests/scenarios/incorrect_merge_conflict/merge.java @@ -4,4 +4,4 @@ public class Main { public static void main ( ) { return ======= retrieve ( ) . with ( "since" , since ) . asPagedIterable ( "/repositories" , GHRepository [ ] . class , item -> item . wrap ( GitHub . this ) ) >>>>>>> - ; } } \ No newline at end of file + ; } } diff --git a/bin/tests/scenarios/incorrect_merge_conflict/right.java b/bin/tests/scenarios/incorrect_merge_conflict/right.java index b9793a4..3895e71 100644 --- a/bin/tests/scenarios/incorrect_merge_conflict/right.java +++ b/bin/tests/scenarios/incorrect_merge_conflict/right.java @@ -6,4 +6,4 @@ public static void main() { GHRepository[].class, item -> item.wrap(GitHub.this) ); } -} \ No newline at end of file +} From 2d2c7e55dad3110791b0bd7d7cccb3836f165aa5 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Thu, 22 Aug 2024 21:54:38 -0300 Subject: [PATCH 4/4] fix: fixing some panic scenarios dues to invalid matching --- bin/src/control.rs | 2 + .../unordered_with_non_labelled/merge.java | 14 ++--- merge/src/ordered_merge.rs | 59 +++++++++++++++++-- parsing/src/tree_sitter_parser.rs | 8 +++ 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/bin/src/control.rs b/bin/src/control.rs index 036e855..3ce6d5f 100644 --- a/bin/src/control.rs +++ b/bin/src/control.rs @@ -50,10 +50,12 @@ pub fn run_tool_on_merge_scenario( right: &str, ) -> Result { if base == left { + log::info!("Early returning because base equals left"); return Ok(ExecutionResult::WithoutConflicts(right.to_string())); } if base == right { + log::info!("Early returning because base equals right"); return Ok(ExecutionResult::WithoutConflicts(left.to_string())); } diff --git a/bin/tests/scenarios/unordered_with_non_labelled/merge.java b/bin/tests/scenarios/unordered_with_non_labelled/merge.java index 1fb9389..ce9067a 100644 --- a/bin/tests/scenarios/unordered_with_non_labelled/merge.java +++ b/bin/tests/scenarios/unordered_with_non_labelled/merge.java @@ -1,13 +1,7 @@ - public class Main { static { int + public class Main { static { int <<<<<<< -x + x = 0 ======= -y + y = 2 >>>>>>> - = -<<<<<<< -0 -======= -2 ->>>>>>> - ; } static { System . out . println ( "I'm a static block" ) ; } public Main ( ) { System . out . println ( "I'm a constructor" ) ; int y = 3 ; } static { System . out . println ( "I don't know what's going on" ) ; } } \ No newline at end of file + ; } static { System . out . println ( "I'm a static block" ) ; } public Main ( ) { System . out . println ( "I'm a constructor" ) ; int y = 3 ; } static { System . out . println ( "I don't know what's going on" ) ; } } diff --git a/merge/src/ordered_merge.rs b/merge/src/ordered_merge.rs index befa450..5314bde 100644 --- a/merge/src/ordered_merge.rs +++ b/merge/src/ordered_merge.rs @@ -42,9 +42,9 @@ pub fn ordered_merge<'a>( right_matching_in_left, matching_base_right, ) { - (true, Some(_), Some(_), Some(_), Some(_)) => { + (true, Some(_), Some(matching_base_left), Some(_), Some(_)) => { result_children.push(crate::merge( - cur_left, + matching_base_left.matching_node, cur_left, cur_right, base_left_matchings, @@ -68,6 +68,32 @@ pub fn ordered_merge<'a>( cur_left_option = children_left_it.next(); cur_right_option = children_right_it.next(); } + (true, Some(_), Some(matching_base_left), Some(_), None) => { + result_children.push(crate::merge( + matching_base_left.matching_node, + cur_left, + cur_right, + base_left_matchings, + base_right_matchings, + left_right_matchings, + )?); + + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); + } + (true, Some(_), None, Some(_), Some(matching_base_right)) => { + result_children.push(crate::merge( + matching_base_right.matching_node, + cur_left, + cur_right, + base_left_matchings, + base_right_matchings, + left_right_matchings, + )?); + + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); + } (false, Some(_), Some(_), None, Some(matching_base_right)) => { if !matching_base_right.is_perfect_match { result_children.push(MergedCSTNode::Conflict { @@ -182,13 +208,36 @@ pub fn ordered_merge<'a>( cur_right_option = children_right_it.next(); } (a, b, c, d, e) => { - return Err(MergeError::InvalidMatchingConfiguration( + log::warn!( + "Reached an Invalid Matching Configuration. {} {} {} {} {}", a, b.is_some(), c.is_some(), d.is_some(), - e.is_some(), - )); + e.is_some() + ); + log::debug!( + "Involved nodes {} AND {}", + cur_left.contents(), + cur_right.contents() + ); + log::debug!( + "Involved nodes parents {} AND {}", + left.contents(), + right.contents() + ); + + if cur_left.contents() == cur_right.contents() { + result_children.push(cur_left.into()) + } else { + result_children.push(MergedCSTNode::Conflict { + left: Some(Box::new(cur_left.into())), + right: Some(Box::new(cur_right.into())), + }) + } + + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } } } diff --git a/parsing/src/tree_sitter_parser.rs b/parsing/src/tree_sitter_parser.rs index 9f697a9..c3b1945 100644 --- a/parsing/src/tree_sitter_parser.rs +++ b/parsing/src/tree_sitter_parser.rs @@ -82,6 +82,14 @@ impl From for ParserConfiguration { tree_sitter_java::language(), )), ); + + map.insert( + "variable_declarator", + Box::new(TreeSitterQuery::new( + r#"(variable_declarator (identifier) @name)"#, + tree_sitter_java::language(), + )), + ); map }, },