Skip to content

Commit 5b7b27a

Browse files
prertikgitster
authored andcommitted
builtin rebase: optionally pass custom reflogs to reset_head()
In the next patch, we will make use of that in the code that fast-forwards to `onto` whenever possible. Signed-off-by: Pratik Karki <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbb2811 commit 5b7b27a

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

builtin/rebase.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ static int run_specific_rebase(struct rebase_options *opts)
431431
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
432432

433433
static int reset_head(struct object_id *oid, const char *action,
434-
const char *switch_to_branch, int detach_head)
434+
const char *switch_to_branch, int detach_head,
435+
const char *reflog_orig_head, const char *reflog_head)
435436
{
436437
struct object_id head_oid;
437438
struct tree_desc desc;
@@ -506,20 +507,26 @@ static int reset_head(struct object_id *oid, const char *action,
506507
old_orig = &oid_old_orig;
507508
if (!get_oid("HEAD", &oid_orig)) {
508509
orig = &oid_orig;
509-
strbuf_addstr(&msg, "updating ORIG_HEAD");
510-
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
510+
if (!reflog_orig_head) {
511+
strbuf_addstr(&msg, "updating ORIG_HEAD");
512+
reflog_orig_head = msg.buf;
513+
}
514+
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
511515
UPDATE_REFS_MSG_ON_ERR);
512516
} else if (old_orig)
513517
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
514-
strbuf_setlen(&msg, prefix_len);
515-
strbuf_addstr(&msg, "updating HEAD");
518+
if (!reflog_head) {
519+
strbuf_setlen(&msg, prefix_len);
520+
strbuf_addstr(&msg, "updating HEAD");
521+
reflog_head = msg.buf;
522+
}
516523
if (!switch_to_branch)
517-
ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
524+
ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF,
518525
UPDATE_REFS_MSG_ON_ERR);
519526
else {
520527
ret = create_symref("HEAD", switch_to_branch, msg.buf);
521528
if (!ret)
522-
ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
529+
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
523530
UPDATE_REFS_MSG_ON_ERR);
524531
}
525532

@@ -899,7 +906,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
899906
rerere_clear(&merge_rr);
900907
string_list_clear(&merge_rr, 1);
901908

902-
if (reset_head(NULL, "reset", NULL, 0) < 0)
909+
if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
903910
die(_("could not discard worktree changes"));
904911
if (read_basic_state(&options))
905912
exit(1);
@@ -915,7 +922,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
915922
if (read_basic_state(&options))
916923
exit(1);
917924
if (reset_head(&options.orig_head, "reset",
918-
options.head_name, 0) < 0)
925+
options.head_name, 0, NULL, NULL) < 0)
919926
die(_("could not move back to %s"),
920927
oid_to_hex(&options.orig_head));
921928
ret = finish_rebase(&options);
@@ -1235,7 +1242,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12351242
write_file(autostash, "%s", buf.buf);
12361243
printf(_("Created autostash: %s\n"), buf.buf);
12371244
if (reset_head(&head->object.oid, "reset --hard",
1238-
NULL, 0) < 0)
1245+
NULL, 0, NULL, NULL) < 0)
12391246
die(_("could not reset --hard"));
12401247
printf(_("HEAD is now at %s"),
12411248
find_unique_abbrev(&head->object.oid,
@@ -1289,7 +1296,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12891296
strbuf_addf(&buf, "rebase: checkout %s",
12901297
options.switch_to);
12911298
if (reset_head(&oid, "checkout",
1292-
options.head_name, 0) < 0) {
1299+
options.head_name, 0,
1300+
NULL, NULL) < 0) {
12931301
ret = !!error(_("could not switch to "
12941302
"%s"),
12951303
options.switch_to);
@@ -1354,7 +1362,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13541362
"it...\n"));
13551363

13561364
strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1357-
if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1365+
if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1366+
NULL, msg.buf))
13581367
die(_("Could not detach HEAD"));
13591368
strbuf_release(&msg);
13601369

0 commit comments

Comments
 (0)