Skip to content

Commit 4db1fc7

Browse files
committed
git_rebase_init: correctly handle detached HEAD
git_rebase_finish relies on head_detached being set, but rebase_init_merge was only setting it when branch->ref_name was unset. But branch->ref_name would be set to "HEAD" in the case of detached HEAD being either implicitly (NULL) or explicitly passed to git_rebase_init.
1 parent ae5838f commit 4db1fc7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ static int rebase_init_merge(
630630
rebase->state_path = git_buf_detach(&state_path);
631631
GITERR_CHECK_ALLOC(rebase->state_path);
632632

633-
if (branch->ref_name) {
633+
if (branch->ref_name && strcmp(branch->ref_name, "HEAD")) {
634634
rebase->orig_head_name = git__strdup(branch->ref_name);
635635
GITERR_CHECK_ALLOC(rebase->orig_head_name);
636636
} else {

tests/rebase/merge.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "clar_libgit2.h"
2+
#include "git2/checkout.h"
23
#include "git2/rebase.h"
34
#include "posix.h"
45
#include "signature.h"
@@ -475,6 +476,58 @@ void test_rebase_merge__finish(void)
475476
git_rebase_free(rebase);
476477
}
477478

479+
void test_rebase_merge__detached_finish(void)
480+
{
481+
git_rebase *rebase;
482+
git_reference *branch_ref, *upstream_ref, *head_ref;
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+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
489+
int error;
490+
491+
cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
492+
cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
493+
494+
cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
495+
cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
496+
497+
cl_git_pass(git_repository_set_head_detached_from_annotated(repo, branch_head));
498+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
499+
git_checkout_head(repo, &opts);
500+
501+
cl_git_pass(git_rebase_init(&rebase, repo, NULL, upstream_head, NULL, NULL));
502+
503+
cl_git_pass(git_rebase_next(&rebase_operation, rebase));
504+
cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
505+
NULL, NULL));
506+
507+
cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
508+
cl_assert_equal_i(GIT_ITEROVER, error);
509+
510+
cl_git_pass(git_rebase_finish(rebase, signature));
511+
512+
cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
513+
514+
cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
515+
cl_assert_equal_i(GIT_REF_OID, git_reference_type(head_ref));
516+
517+
/* Make sure the reflogs are updated appropriately */
518+
cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));
519+
cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
520+
cl_assert_equal_oid(git_annotated_commit_id(upstream_head), git_reflog_entry_id_old(reflog_entry));
521+
cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));
522+
523+
git_reflog_free(reflog);
524+
git_annotated_commit_free(branch_head);
525+
git_annotated_commit_free(upstream_head);
526+
git_reference_free(branch_ref);
527+
git_reference_free(upstream_ref);
528+
git_rebase_free(rebase);
529+
}
530+
478531
void test_rebase_merge__finish_with_ids(void)
479532
{
480533
git_rebase *rebase;

0 commit comments

Comments
 (0)