built-in rebase: set ORIG_HEAD just once, before the rebase

Technically, the scripted version set ORIG_HEAD only in two spots (which really could have been one, because it called `git checkout $onto^0` to start the rebase and also if it could take a shortcut, and in both cases it called `git update-ref $orig_head`). Practically, it *implicitly* reset ORIG_HEAD whenever `git reset --hard` was called. However, what we really want is that it is set exactly once, at the beginning of the rebase. So let's do that. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Mar 3, 2019 at 09:11 UTC cbd29ead92d8555f2b918c7f9d53913f26f7a872
2 files changed +19 -14
builtin/rebase.c
+18 -13
@@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
369 #define RESET_HEAD_DETACH (1<<0)
370 #define RESET_HEAD_HARD (1<<1)
371 #define RESET_HEAD_REFS_ONLY (1<<2)
372 +#define RESET_ORIG_HEAD (1<<3)
373
374 static int reset_head(struct object_id *oid, const char *action,
375 const char *switch_to_branch, unsigned flags,
@@ -377,6 +378,7 @@ static int reset_head(struct object_id *oid, const char *action,
378 unsigned detach_head = flags & RESET_HEAD_DETACH;
379 unsigned reset_hard = flags & RESET_HEAD_HARD;
380 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
381 + unsigned update_orig_head = flags & RESET_ORIG_HEAD;
382 struct object_id head_oid;
383 struct tree_desc desc[2] = { { NULL }, { NULL } };
384 struct lock_file lock = LOCK_INIT;
@@ -453,18 +455,21 @@ reset_head_refs:
455 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
456 prefix_len = msg.len;
457
456 - if (!get_oid("ORIG_HEAD", &oid_old_orig))
457 - old_orig = &oid_old_orig;
458 - if (!get_oid("HEAD", &oid_orig)) {
459 - orig = &oid_orig;
460 - if (!reflog_orig_head) {
461 - strbuf_addstr(&msg, "updating ORIG_HEAD");
462 - reflog_orig_head = msg.buf;
463 - }
464 - update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
465 - UPDATE_REFS_MSG_ON_ERR);
466 - } else if (old_orig)
467 - delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
458 + if (update_orig_head) {
459 + if (!get_oid("ORIG_HEAD", &oid_old_orig))
460 + old_orig = &oid_old_orig;
461 + if (!get_oid("HEAD", &oid_orig)) {
462 + orig = &oid_orig;
463 + if (!reflog_orig_head) {
464 + strbuf_addstr(&msg, "updating ORIG_HEAD");
465 + reflog_orig_head = msg.buf;
466 + }
467 + update_ref(reflog_orig_head, "ORIG_HEAD", orig,
468 + old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
469 + } else if (old_orig)
470 + delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
471 + }
472 +
473 if (!reflog_head) {
474 strbuf_setlen(&msg, prefix_len);
475 strbuf_addstr(&msg, "updating HEAD");
@@ -1725,7 +1730,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1730 strbuf_addf(&msg, "%s: checkout %s",
1731 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1732 if (reset_head(&options.onto->object.oid, "checkout", NULL,
1728 - RESET_HEAD_DETACH, NULL, msg.buf))
1733 + RESET_HEAD_DETACH | RESET_ORIG_HEAD, NULL, msg.buf))
1734 die(_("Could not detach HEAD"));
1735 strbuf_release(&msg);
1736
t/t3400-rebase.sh
+1 -1
@@ -59,7 +59,7 @@ test_expect_success 'rebase against master' '
59 git rebase master
60 '
61
62 -test_expect_failure 'rebase sets ORIG_HEAD to pre-rebase state' '
62 +test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
63 git checkout -b orig-head topic &&
64 pre="$(git rev-parse --verify HEAD)" &&
65 git rebase master &&