Commit 2e43a37bcb14d324ab09de4801cb37cf2f31b9bb

Carlos Martín Nieto 2016-05-04T19:05:38

Merge pull request #3769 from libgit2/ethomson/rebase_inmemory_no_base Rebase: rebase a branch with no merge base for in-memory

diff --git a/src/rebase.c b/src/rebase.c
index 93a9154..9f3b6ec 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -852,6 +852,7 @@ static int rebase_next_inmemory(
 	git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
 	git_rebase_operation *operation;
 	git_index *index = NULL;
+	unsigned int parent_count;
 	int error;
 
 	*out = NULL;
@@ -859,10 +860,20 @@ static int rebase_next_inmemory(
 	operation = git_array_get(rebase->operations, rebase->current);
 
 	if ((error = git_commit_lookup(&current_commit, rebase->repo, &operation->id)) < 0 ||
-		(error = git_commit_tree(&current_tree, current_commit)) < 0 ||
-		(error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 ||
-		(error = git_commit_tree(&parent_tree, parent_commit)) < 0 ||
-		(error = git_commit_tree(&head_tree, rebase->last_commit)) < 0 ||
+		(error = git_commit_tree(&current_tree, current_commit)) < 0)
+		goto done;
+
+	if ((parent_count = git_commit_parentcount(current_commit)) > 1) {
+		giterr_set(GITERR_REBASE, "Cannot rebase a merge commit");
+		error = -1;
+		goto done;
+	} else if (parent_count) {
+		if ((error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 ||
+			(error = git_commit_tree(&parent_tree, parent_commit)) < 0)
+			goto done;
+	}
+
+	if ((error = git_commit_tree(&head_tree, rebase->last_commit)) < 0 ||
 		(error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, &rebase->options.merge_options)) < 0)
 		goto done;
 
diff --git a/tests/rebase/inmemory.c b/tests/rebase/inmemory.c
index d5d89c7..7ce865b 100644
--- a/tests/rebase/inmemory.c
+++ b/tests/rebase/inmemory.c
@@ -5,15 +5,20 @@
 #include <fcntl.h>
 
 static git_repository *repo;
+static git_signature *signature;
 
 // Fixture setup and teardown
 void test_rebase_inmemory__initialize(void)
 {
 	repo = cl_git_sandbox_init("rebase");
+
+	cl_git_pass(git_signature_new(&signature,
+		"Rebaser", "rebaser@rebaser.rb", 1405694510, 0));
 }
 
 void test_rebase_inmemory__cleanup(void)
 {
+	git_signature_free(signature);
 	cl_git_sandbox_cleanup();
 }
 
@@ -53,14 +58,10 @@ void test_rebase_inmemory__can_resolve_conflicts(void)
 	git_rebase_operation *rebase_operation;
 	git_status_list *status_list;
 	git_oid pick_id, commit_id, expected_commit_id;
-	git_signature *signature;
 	git_index *rebase_index, *repo_index;
 	git_index_entry resolution = {{0}};
 	git_rebase_options opts = GIT_REBASE_OPTIONS_INIT;
 
-	cl_git_pass(git_signature_new(&signature,
-		"Rebaser", "rebaser@rebaser.rb", 1405694510, 0));
-
 	opts.inmemory = true;
 
 	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/asparagus"));
@@ -104,7 +105,6 @@ void test_rebase_inmemory__can_resolve_conflicts(void)
 	cl_git_pass(git_oid_fromstr(&expected_commit_id, "db7af47222181e548810da2ab5fec0e9357c5637"));
 	cl_assert_equal_oid(&commit_id, &expected_commit_id);
 
-	git_signature_free(signature);
 	git_status_list_free(status_list);
 	git_annotated_commit_free(branch_head);
 	git_annotated_commit_free(upstream_head);
@@ -114,3 +114,54 @@ void test_rebase_inmemory__can_resolve_conflicts(void)
 	git_index_free(rebase_index);
 	git_rebase_free(rebase);
 }
+
+void test_rebase_inmemory__no_common_ancestor(void)
+{
+	git_rebase *rebase;
+	git_reference *branch_ref, *upstream_ref;
+	git_annotated_commit *branch_head, *upstream_head;
+	git_rebase_operation *rebase_operation;
+	git_oid commit_id, expected_final_id;
+	git_rebase_options opts = GIT_REBASE_OPTIONS_INIT;
+
+	opts.inmemory = true;
+
+	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/barley"));
+	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));
+
+	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
+	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
+
+	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &opts));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_finish(rebase, signature));
+
+	git_oid_fromstr(&expected_final_id, "71e7ee8d4fe7d8bf0d107355197e0a953dfdb7f3");
+	cl_assert_equal_oid(&expected_final_id, &commit_id);
+
+	git_annotated_commit_free(branch_head);
+	git_annotated_commit_free(upstream_head);
+	git_reference_free(branch_ref);
+	git_reference_free(upstream_ref);
+	git_rebase_free(rebase);
+}
diff --git a/tests/rebase/merge.c b/tests/rebase/merge.c
index 2d6df36..74507e2 100644
--- a/tests/rebase/merge.c
+++ b/tests/rebase/merge.c
@@ -525,6 +525,54 @@ void test_rebase_merge__finish_with_ids(void)
 	git_rebase_free(rebase);
 }
 
+void test_rebase_merge__no_common_ancestor(void)
+{
+	git_rebase *rebase;
+	git_reference *branch_ref, *upstream_ref;
+	git_annotated_commit *branch_head, *upstream_head;
+	git_rebase_operation *rebase_operation;
+	git_oid commit_id, expected_final_id;
+
+	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/barley"));
+	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));
+
+	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
+	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
+
+	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
+	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
+		NULL, NULL));
+
+	cl_git_pass(git_rebase_finish(rebase, signature));
+
+	git_oid_fromstr(&expected_final_id, "71e7ee8d4fe7d8bf0d107355197e0a953dfdb7f3");
+	cl_assert_equal_oid(&expected_final_id, &commit_id);
+
+	git_annotated_commit_free(branch_head);
+	git_annotated_commit_free(upstream_head);
+	git_reference_free(branch_ref);
+	git_reference_free(upstream_ref);
+	git_rebase_free(rebase);
+}
+
 static void test_copy_note(
 	const git_rebase_options *opts,
 	bool should_exist)