Commit b3c0d43cead12805800b20e84ab0129187a646d6

Tyrie Vella 2018-01-22T14:44:31

merge: virtual commit should be last argument to merge-base Our virtual commit must be the last argument to merge-base: since our algorithm pushes _both_ parents of the virtual commit, it needs to be the last argument, since merge-base: > Given three commits A, B and C, git merge-base A B C will compute the > merge base between A and a hypothetical commit M We want to calculate the merge base between the actual commit ("two") and the virtual commit ("one") - since one actually pushes its parents to the merge-base calculation, we need to calculate the merge base of "two" and the parents of one.

diff --git a/src/merge.c b/src/merge.c
index 476a38a..a85b0a6 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2149,8 +2149,14 @@ static int compute_base(
 	if (given_opts)
 		memcpy(&opts, given_opts, sizeof(git_merge_options));
 
-	if ((error = insert_head_ids(&head_ids, one)) < 0 ||
-		(error = insert_head_ids(&head_ids, two)) < 0 ||
+	/* With more than two commits, merge_bases_many finds the base of
+	 * the first commit and a hypothetical merge of the others. Since
+	 * "one" may itself be a virtual commit, which insert_head_ids
+	 * substitutes multiple ancestors for, it needs to be added
+	 * after "two" which is always a single real commit.
+	 */
+	if ((error = insert_head_ids(&head_ids, two)) < 0 ||
+		(error = insert_head_ids(&head_ids, one)) < 0 ||
 		(error = git_merge_bases_many(&bases, repo,
 			head_ids.size, head_ids.ptr)) < 0)
 		goto done;
diff --git a/tests/merge/trees/recursive.c b/tests/merge/trees/recursive.c
index c845565..d4defb8 100644
--- a/tests/merge/trees/recursive.c
+++ b/tests/merge/trees/recursive.c
@@ -422,7 +422,7 @@ void test_merge_trees_recursive__merge_base_for_virtual_commit(void)
 
 	struct merge_index_entry merge_index_entries[] = {
 		{ 0100644, "1bde1883de4977ea3e664b315da951d1f614c3b1", 0, "targetfile.txt" },
-		{ 0100644, "b7de2b52ba055688061355fad1599a5d214ce8f8", 1, "version.txt" },
+		{ 0100644, "f66647f02d6779e119bd435fe2908867c64c265f", 1, "version.txt" },
 		{ 0100644, "358efd6f589384fa8baf92234db9c7899a53916e", 2, "version.txt" },
 		{ 0100644, "a664873b1c0b9a1ed300f8644dde536fdaa3a34f", 3, "version.txt" },
 	};
@@ -445,7 +445,7 @@ void test_merge_trees_recursive__merge_base_for_virtual_commit_2(void)
 
 	struct merge_index_entry merge_index_entries[] = {
 		{ 0100644, "4a06b258fed8a4d15967ec4253ae7366b70f727d", 0, "targetfile.txt" },
-		{ 0100644, "b6bd0f9952f396e757d3f91e08c59a7e91707201", 1, "version.txt" },
+		{ 0100644, "16c5a7810777e1181709833cfe6931949259f802", 1, "version.txt" },
 		{ 0100644, "f0856993e005c0d8ed2dc7cdc222cc1d89fb3c77", 2, "version.txt" },
 		{ 0100644, "2cba583804a4a6fad1baf97c959be447238d1489", 3, "version.txt" },
 	};