Commit 49b8293c75d287f609eebae334519872784cd893

Edward Thomson 2015-02-13T11:20:32

rebase: allow `NULL` branch to indicate `HEAD` Don't require the branch to rebase, if given `NULL`, simply look up `HEAD`.

diff --git a/include/git2/rebase.h b/include/git2/rebase.h
index 1da3fb5..19f5cb8 100644
--- a/include/git2/rebase.h
+++ b/include/git2/rebase.h
@@ -133,7 +133,8 @@ GIT_EXTERN(int) git_rebase_init_options(
  *
  * @param out Pointer to store the rebase object
  * @param repo The repository to perform the rebase
- * @param branch The terminal commit to rebase
+ * @param branch The terminal commit to rebase, or NULL to rebase the
+ *               current branch
  * @param upstream The commit to begin rebasing from, or NULL to rebase all
  *                 reachable commits
  * @param onto The branch to rebase onto, or NULL to rebase onto the given
diff --git a/src/rebase.c b/src/rebase.c
index 229b18a..a1c72e8 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -608,11 +608,21 @@ static int rebase_init(
 	const git_annotated_commit *onto,
 	const git_rebase_options *opts)
 {
+	git_reference *head_ref = NULL;
+	git_annotated_commit *head_branch = NULL;
 	git_buf state_path = GIT_BUF_INIT;
 	int error;
 
 	if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0)
-		return error;
+		goto done;
+
+	if (!branch) {
+		if ((error = git_repository_head(&head_ref, repo)) < 0 ||
+			(error = git_annotated_commit_from_ref(&head_branch, repo, head_ref)) < 0)
+			goto done;
+
+		branch = head_branch;
+	}
 
 	rebase->repo = repo;
 	rebase->type = GIT_REBASE_TYPE_MERGE;
@@ -630,6 +640,10 @@ static int rebase_init(
 
 	git_buf_free(&state_path);
 
+done:
+	git_reference_free(head_ref);
+	git_annotated_commit_free(head_branch);
+
 	return error;
 }
 
@@ -649,7 +663,7 @@ int git_rebase_init(
 	git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
 	int error;
 
-	assert(repo && branch && (upstream || onto));
+	assert(repo && (upstream || onto));
 
 	*out = NULL;
 
diff --git a/tests/rebase/setup.c b/tests/rebase/setup.c
index c81ca12..f1eb6a4 100644
--- a/tests/rebase/setup.c
+++ b/tests/rebase/setup.c
@@ -293,6 +293,55 @@ void test_rebase_setup__orphan_branch(void)
 	git_rebase_free(rebase);
 }
 
+/* git checkout beef && git rebase --merge master */
+void test_rebase_setup__merge_null_branch_uses_HEAD(void)
+{
+	git_rebase *rebase;
+	git_reference *upstream_ref;
+	git_annotated_commit *upstream_head;
+	git_reference *head;
+	git_commit *head_commit;
+	git_oid head_id;
+	git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
+
+	checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+
+	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
+
+	cl_git_pass(git_repository_set_head(repo, "refs/heads/beef", NULL, NULL));
+	cl_git_pass(git_checkout_head(repo, &checkout_opts));
+
+	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));
+	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
+
+	cl_git_pass(git_rebase_init(&rebase, repo, NULL, upstream_head, NULL, signature, NULL));
+
+	cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
+
+	git_oid_fromstr(&head_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00");
+	cl_git_pass(git_repository_head(&head, repo));
+	cl_git_pass(git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT));
+	cl_assert_equal_oid(&head_id, git_commit_id(head_commit));
+
+	cl_assert_equal_file("b146bd7608eac53d9bf9e1a6963543588b555c64\n", 41, "rebase/.git/ORIG_HEAD");
+
+	cl_assert_equal_file("da9c51a23d02d931a486f45ad18cda05cf5d2b94\n", 41, "rebase/.git/rebase-merge/cmt.1");
+	cl_assert_equal_file("8d1f13f93c4995760ac07d129246ac1ff64c0be9\n", 41, "rebase/.git/rebase-merge/cmt.2");
+	cl_assert_equal_file("3069cc907e6294623e5917ef6de663928c1febfb\n", 41, "rebase/.git/rebase-merge/cmt.3");
+	cl_assert_equal_file("588e5d2f04d49707fe4aab865e1deacaf7ef6787\n", 41, "rebase/.git/rebase-merge/cmt.4");
+	cl_assert_equal_file("b146bd7608eac53d9bf9e1a6963543588b555c64\n", 41, "rebase/.git/rebase-merge/cmt.5");
+	cl_assert_equal_file("5\n", 2, "rebase/.git/rebase-merge/end");
+	cl_assert_equal_file("efad0b11c47cb2f0220cbd6f5b0f93bb99064b00\n", 41, "rebase/.git/rebase-merge/onto");
+	cl_assert_equal_file("master\n", 7, "rebase/.git/rebase-merge/onto_name");
+	cl_assert_equal_file("b146bd7608eac53d9bf9e1a6963543588b555c64\n", 41, "rebase/.git/rebase-merge/orig-head");
+
+	git_commit_free(head_commit);
+	git_reference_free(head);
+	git_annotated_commit_free(upstream_head);
+	git_reference_free(upstream_ref);
+	git_rebase_free(rebase);
+}
+
 static int rebase_is_blocked(void)
 {
 	git_rebase *rebase = NULL;