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 <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pratik Karki committed
Aug 8, 2018 at 21:21 UTC
fa443d40b115acf15716a0cd81b84794cfbbf5af
1 file changed
+21
-12
builtin/rebase.c
+21
-12
@@ -431,7 +431,8 @@ static int run_specific_rebase(struct rebase_options *opts)
431
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
432
433
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)
436
{
437
struct object_id head_oid;
438
struct tree_desc desc;
@@ -506,20 +507,26 @@ static int reset_head(struct object_id *oid, const char *action,
507
old_orig = &oid_old_orig;
508
if (!get_oid("HEAD", &oid_orig)) {
509
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,
515
UPDATE_REFS_MSG_ON_ERR);
516
} else if (old_orig)
517
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
+ }
523
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,
525
UPDATE_REFS_MSG_ON_ERR);
526
else {
527
ret = create_symref("HEAD", switch_to_branch, msg.buf);
528
if (!ret)
522
- ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
529
+ ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
530
UPDATE_REFS_MSG_ON_ERR);
531
}
532
@@ -900,7 +907,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
907
rerere_clear(&merge_rr);
908
string_list_clear(&merge_rr, 1);
909
903
- if (reset_head(NULL, "reset", NULL, 0) < 0)
910
+ if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
911
die(_("could not discard worktree changes"));
912
if (read_basic_state(&options))
913
exit(1);
@@ -916,7 +923,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
923
if (read_basic_state(&options))
924
exit(1);
925
if (reset_head(&options.orig_head, "reset",
919
- options.head_name, 0) < 0)
926
+ options.head_name, 0, NULL, NULL) < 0)
927
die(_("could not move back to %s"),
928
oid_to_hex(&options.orig_head));
929
ret = finish_rebase(&options);
@@ -1236,7 +1243,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1243
write_file(autostash, "%s", buf.buf);
1244
printf(_("Created autostash: %s\n"), buf.buf);
1245
if (reset_head(&head->object.oid, "reset --hard",
1239
- NULL, 0) < 0)
1246
+ NULL, 0, NULL, NULL) < 0)
1247
die(_("could not reset --hard"));
1248
printf(_("HEAD is now at %s"),
1249
find_unique_abbrev(&head->object.oid,
@@ -1290,7 +1297,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1297
strbuf_addf(&buf, "rebase: checkout %s",
1298
options.switch_to);
1299
if (reset_head(&oid, "checkout",
1293
- options.head_name, 0) < 0) {
1300
+ options.head_name, 0,
1301
+ NULL, NULL) < 0) {
1302
ret = !!error(_("could not switch to "
1303
"%s"),
1304
options.switch_to);
@@ -1355,7 +1363,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1363
"it...\n"));
1364
1365
strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1358
- if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1366
+ if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1367
+ NULL, msg.buf))
1368
die(_("Could not detach HEAD"));
1369
strbuf_release(&msg);
1370