diff options
author | Edward Thomson <ethomson@github.com> | 2016-04-21 18:16:37 -0400 |
---|---|---|
committer | Edward Thomson <ethomson@github.com> | 2016-04-21 18:19:05 -0400 |
commit | 1f84caf0c0e1bb1c1b4b228cec618d4f3ab3e408 (patch) | |
tree | 2f44c05b84333a543cc8694e670d945939b59862 | |
parent | badc72838fdc387eaf96e01bd414a47369b95e8b (diff) | |
download | libgit2-1f84caf0c0e1bb1c1b4b228cec618d4f3ab3e408.tar.gz |
rebase: correctly finish rebasing detached headsethomson/rebase_detached
When rebasing with IDs, we do not return to the `branch`,
we remain in a detached HEAD state.
-rw-r--r-- | src/rebase.c | 59 | ||||
-rw-r--r-- | tests/rebase/merge.c | 50 |
2 files changed, 85 insertions, 24 deletions
diff --git a/src/rebase.c b/src/rebase.c index a2123ca69..93a91545d 100644 --- a/src/rebase.c +++ b/src/rebase.c @@ -1262,42 +1262,33 @@ done: return error; } -int git_rebase_finish( - git_rebase *rebase, - const git_signature *signature) +static int return_to_orig_head(git_rebase *rebase) { git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL; git_commit *terminal_commit = NULL; git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT; char onto[GIT_OID_HEXSZ]; - int error; - - assert(rebase); - - if (rebase->inmemory) - return 0; + int error = 0; git_oid_fmt(onto, &rebase->onto_id); - if ((error = git_buf_printf(&branch_msg, "rebase finished: %s onto %.*s", - rebase->orig_head_name, GIT_OID_HEXSZ, onto)) < 0 || - (error = git_buf_printf(&head_msg, "rebase finished: returning to %s", - rebase->orig_head_name)) < 0 || - (error = git_repository_head(&terminal_ref, rebase->repo)) < 0 || + if ((error = git_buf_printf(&branch_msg, + "rebase finished: %s onto %.*s", + rebase->orig_head_name, GIT_OID_HEXSZ, onto)) == 0 && + (error = git_buf_printf(&head_msg, + "rebase finished: returning to %s", + rebase->orig_head_name)) == 0 && + (error = git_repository_head(&terminal_ref, rebase->repo)) == 0 && (error = git_reference_peel((git_object **)&terminal_commit, - terminal_ref, GIT_OBJ_COMMIT)) < 0 || + terminal_ref, GIT_OBJ_COMMIT)) == 0 && (error = git_reference_create_matching(&branch_ref, - rebase->repo, rebase->orig_head_name, git_commit_id(terminal_commit), 1, - &rebase->orig_head_id, branch_msg.ptr)) < 0 || - (error = git_reference_symbolic_create(&head_ref, + rebase->repo, rebase->orig_head_name, + git_commit_id(terminal_commit), 1, + &rebase->orig_head_id, branch_msg.ptr)) == 0) + error = git_reference_symbolic_create(&head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, - head_msg.ptr)) < 0 || - (error = rebase_copy_notes(rebase, signature)) < 0) - goto done; - - error = rebase_cleanup(rebase); + head_msg.ptr); -done: git_buf_free(&head_msg); git_buf_free(&branch_msg); git_commit_free(terminal_commit); @@ -1308,6 +1299,26 @@ done: return error; } +int git_rebase_finish( + git_rebase *rebase, + const git_signature *signature) +{ + int error = 0; + + assert(rebase); + + if (rebase->inmemory) + return 0; + + if (!rebase->head_detached) + error = return_to_orig_head(rebase); + + if (error == 0 && (error = rebase_copy_notes(rebase, signature)) == 0) + error = rebase_cleanup(rebase); + + return error; +} + size_t git_rebase_operation_entrycount(git_rebase *rebase) { assert(rebase); diff --git a/tests/rebase/merge.c b/tests/rebase/merge.c index d090e02e8..2d6df369e 100644 --- a/tests/rebase/merge.c +++ b/tests/rebase/merge.c @@ -475,6 +475,56 @@ void test_rebase_merge__finish(void) git_rebase_free(rebase); } +void test_rebase_merge__finish_with_ids(void) +{ + git_rebase *rebase; + git_reference *head_ref; + git_oid branch_id, upstream_id; + git_annotated_commit *branch_head, *upstream_head; + git_rebase_operation *rebase_operation; + git_oid commit_id; + git_reflog *reflog; + const git_reflog_entry *reflog_entry; + int error; + + cl_git_pass(git_oid_fromstr(&branch_id, "d616d97082eb7bb2dc6f180a7cca940993b7a56f")); + cl_git_pass(git_oid_fromstr(&upstream_id, "f87d14a4a236582a0278a916340a793714256864")); + + cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id)); + cl_git_pass(git_annotated_commit_lookup(&upstream_head, repo, &upstream_id)); + + 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_fail(error = git_rebase_next(&rebase_operation, rebase)); + cl_assert_equal_i(GIT_ITEROVER, error); + + cl_git_pass(git_rebase_finish(rebase, signature)); + + cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo)); + + cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD")); + cl_assert_equal_i(GIT_REF_OID, git_reference_type(head_ref)); + cl_assert_equal_oid(&commit_id, git_reference_target(head_ref)); + + /* reflogs are not updated as if we were operating on proper + * branches. check that the last reflog entry is the rebase. + */ + cl_git_pass(git_reflog_read(&reflog, repo, "HEAD")); + cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0)); + cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry)); + cl_assert_equal_s("rebase: Modification 3 to gravy", git_reflog_entry_message(reflog_entry)); + git_reflog_free(reflog); + + git_annotated_commit_free(branch_head); + git_annotated_commit_free(upstream_head); + git_reference_free(head_ref); + git_rebase_free(rebase); +} + static void test_copy_note( const git_rebase_options *opts, bool should_exist) |