Skip to content

Commit badc728

Browse files
author
Edward Thomson
committed
rebase: handle detached HEADs in init
When `init`ing a rebase from a detached HEAD, be sure to remember that we were in a detached HEAD state so that we can correctly `abort` the object that we just created.
1 parent 320f53c commit badc728

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/rebase.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ static int rebase_setupfiles_merge(git_rebase *rebase)
472472
static int rebase_setupfiles(git_rebase *rebase)
473473
{
474474
char onto[GIT_OID_HEXSZ], orig_head[GIT_OID_HEXSZ];
475+
const char *orig_head_name;
475476

476477
git_oid_fmt(onto, &rebase->onto_id);
477478
git_oid_fmt(orig_head, &rebase->orig_head_id);
@@ -481,8 +482,11 @@ static int rebase_setupfiles(git_rebase *rebase)
481482
return -1;
482483
}
483484

485+
orig_head_name = rebase->head_detached ? ORIG_DETACHED_HEAD :
486+
rebase->orig_head_name;
487+
484488
if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 ||
485-
rebase_setupfile(rebase, HEAD_NAME_FILE, -1, "%s\n", rebase->orig_head_name) < 0 ||
489+
rebase_setupfile(rebase, HEAD_NAME_FILE, -1, "%s\n", orig_head_name) < 0 ||
486490
rebase_setupfile(rebase, ONTO_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, onto) < 0 ||
487491
rebase_setupfile(rebase, ORIG_HEAD_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, orig_head) < 0 ||
488492
rebase_setupfile(rebase, QUIET_FILE, -1, rebase->quiet ? "t\n" : "\n") < 0)
@@ -626,8 +630,12 @@ static int rebase_init_merge(
626630
rebase->state_path = git_buf_detach(&state_path);
627631
GITERR_CHECK_ALLOC(rebase->state_path);
628632

629-
rebase->orig_head_name = git__strdup(branch->ref_name ? branch->ref_name : ORIG_DETACHED_HEAD);
630-
GITERR_CHECK_ALLOC(rebase->orig_head_name);
633+
if (branch->ref_name) {
634+
rebase->orig_head_name = git__strdup(branch->ref_name);
635+
GITERR_CHECK_ALLOC(rebase->orig_head_name);
636+
} else {
637+
rebase->head_detached = 1;
638+
}
631639

632640
rebase->onto_name = git__strdup(rebase_onto_name(onto));
633641
GITERR_CHECK_ALLOC(rebase->onto_name);

0 commit comments

Comments
 (0)