Skip to content

Commit 1f84caf

Browse files
author
Edward Thomson
committed
rebase: correctly finish rebasing detached heads
When rebasing with IDs, we do not return to the `branch`, we remain in a detached HEAD state.
1 parent badc728 commit 1f84caf

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

src/rebase.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,42 +1262,33 @@ static int rebase_copy_notes(
12621262
return error;
12631263
}
12641264

1265-
int git_rebase_finish(
1266-
git_rebase *rebase,
1267-
const git_signature *signature)
1265+
static int return_to_orig_head(git_rebase *rebase)
12681266
{
12691267
git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL;
12701268
git_commit *terminal_commit = NULL;
12711269
git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT;
12721270
char onto[GIT_OID_HEXSZ];
1273-
int error;
1274-
1275-
assert(rebase);
1276-
1277-
if (rebase->inmemory)
1278-
return 0;
1271+
int error = 0;
12791272

12801273
git_oid_fmt(onto, &rebase->onto_id);
12811274

1282-
if ((error = git_buf_printf(&branch_msg, "rebase finished: %s onto %.*s",
1283-
rebase->orig_head_name, GIT_OID_HEXSZ, onto)) < 0 ||
1284-
(error = git_buf_printf(&head_msg, "rebase finished: returning to %s",
1285-
rebase->orig_head_name)) < 0 ||
1286-
(error = git_repository_head(&terminal_ref, rebase->repo)) < 0 ||
1275+
if ((error = git_buf_printf(&branch_msg,
1276+
"rebase finished: %s onto %.*s",
1277+
rebase->orig_head_name, GIT_OID_HEXSZ, onto)) == 0 &&
1278+
(error = git_buf_printf(&head_msg,
1279+
"rebase finished: returning to %s",
1280+
rebase->orig_head_name)) == 0 &&
1281+
(error = git_repository_head(&terminal_ref, rebase->repo)) == 0 &&
12871282
(error = git_reference_peel((git_object **)&terminal_commit,
1288-
terminal_ref, GIT_OBJ_COMMIT)) < 0 ||
1283+
terminal_ref, GIT_OBJ_COMMIT)) == 0 &&
12891284
(error = git_reference_create_matching(&branch_ref,
1290-
rebase->repo, rebase->orig_head_name, git_commit_id(terminal_commit), 1,
1291-
&rebase->orig_head_id, branch_msg.ptr)) < 0 ||
1292-
(error = git_reference_symbolic_create(&head_ref,
1285+
rebase->repo, rebase->orig_head_name,
1286+
git_commit_id(terminal_commit), 1,
1287+
&rebase->orig_head_id, branch_msg.ptr)) == 0)
1288+
error = git_reference_symbolic_create(&head_ref,
12931289
rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1,
1294-
head_msg.ptr)) < 0 ||
1295-
(error = rebase_copy_notes(rebase, signature)) < 0)
1296-
goto done;
1297-
1298-
error = rebase_cleanup(rebase);
1290+
head_msg.ptr);
12991291

1300-
done:
13011292
git_buf_free(&head_msg);
13021293
git_buf_free(&branch_msg);
13031294
git_commit_free(terminal_commit);
@@ -1308,6 +1299,26 @@ int git_rebase_finish(
13081299
return error;
13091300
}
13101301

1302+
int git_rebase_finish(
1303+
git_rebase *rebase,
1304+
const git_signature *signature)
1305+
{
1306+
int error = 0;
1307+
1308+
assert(rebase);
1309+
1310+
if (rebase->inmemory)
1311+
return 0;
1312+
1313+
if (!rebase->head_detached)
1314+
error = return_to_orig_head(rebase);
1315+
1316+
if (error == 0 && (error = rebase_copy_notes(rebase, signature)) == 0)
1317+
error = rebase_cleanup(rebase);
1318+
1319+
return error;
1320+
}
1321+
13111322
size_t git_rebase_operation_entrycount(git_rebase *rebase)
13121323
{
13131324
assert(rebase);

tests/rebase/merge.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,56 @@ void test_rebase_merge__finish(void)
475475
git_rebase_free(rebase);
476476
}
477477

478+
void test_rebase_merge__finish_with_ids(void)
479+
{
480+
git_rebase *rebase;
481+
git_reference *head_ref;
482+
git_oid branch_id, upstream_id;
483+
git_annotated_commit *branch_head, *upstream_head;
484+
git_rebase_operation *rebase_operation;
485+
git_oid commit_id;
486+
git_reflog *reflog;
487+
const git_reflog_entry *reflog_entry;
488+
int error;
489+
490+
cl_git_pass(git_oid_fromstr(&branch_id, "d616d97082eb7bb2dc6f180a7cca940993b7a56f"));
491+
cl_git_pass(git_oid_fromstr(&upstream_id, "f87d14a4a236582a0278a916340a793714256864"));
492+
493+
cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
494+
cl_git_pass(git_annotated_commit_lookup(&upstream_head, repo, &upstream_id));
495+
496+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
497+
498+
cl_git_pass(git_rebase_next(&rebase_operation, rebase));
499+
cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
500+
NULL, NULL));
501+
502+
cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
503+
cl_assert_equal_i(GIT_ITEROVER, error);
504+
505+
cl_git_pass(git_rebase_finish(rebase, signature));
506+
507+
cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
508+
509+
cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
510+
cl_assert_equal_i(GIT_REF_OID, git_reference_type(head_ref));
511+
cl_assert_equal_oid(&commit_id, git_reference_target(head_ref));
512+
513+
/* reflogs are not updated as if we were operating on proper
514+
* branches. check that the last reflog entry is the rebase.
515+
*/
516+
cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));
517+
cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
518+
cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));
519+
cl_assert_equal_s("rebase: Modification 3 to gravy", git_reflog_entry_message(reflog_entry));
520+
git_reflog_free(reflog);
521+
522+
git_annotated_commit_free(branch_head);
523+
git_annotated_commit_free(upstream_head);
524+
git_reference_free(head_ref);
525+
git_rebase_free(rebase);
526+
}
527+
478528
static void test_copy_note(
479529
const git_rebase_options *opts,
480530
bool should_exist)

0 commit comments

Comments
 (0)