]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rebase --abort: improve reflog message
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Wed, 12 Oct 2022 09:35:11 +0000 (09:35 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Oct 2022 19:55:03 +0000 (12:55 -0700)
When aborting a rebase the reflog message looks like

rebase (abort): updating HEAD

which is not very informative. Improve the message by mentioning the
branch that we are returning to as we do at the end of a successful
rebase so it looks like.

rebase (abort): returning to refs/heads/topic

If GIT_REFLOG_ACTION is set in the environment we no longer omit
"(abort)" from the reflog message. We don't omit "(start)" and
"(finish)" when starting and finishing a rebase in that case so we
shouldn't omit "(abort)".

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase.c
t/t3406-rebase-message.sh

index 5253ba668614565641f9f2a25c7475f66bebd961..d63d77757e824aa5964f656c0f4d8d7259489fbb 100644 (file)
@@ -1001,23 +1001,6 @@ static void NORETURN error_on_missing_default_upstream(void)
        exit(1);
 }
 
-static void set_reflog_action(struct rebase_options *options)
-{
-       const char *env;
-       struct strbuf buf = STRBUF_INIT;
-
-       if (!is_merge(options))
-               return;
-
-       env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
-       if (env && strcmp("rebase", env))
-               return; /* only override it if it is "rebase" */
-
-       strbuf_addf(&buf, "rebase (%s)", options->action);
-       setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
-       strbuf_release(&buf);
-}
-
 static int check_exec_cmd(const char *cmd)
 {
        if (strchr(cmd, '\n'))
@@ -1311,18 +1294,23 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
        }
        case ACTION_ABORT: {
                struct string_list merge_rr = STRING_LIST_INIT_DUP;
-               options.action = "abort";
-               set_reflog_action(&options);
+               struct strbuf head_msg = STRBUF_INIT;
 
+               options.action = "abort";
                rerere_clear(the_repository, &merge_rr);
                string_list_clear(&merge_rr, 1);
 
                if (read_basic_state(&options))
                        exit(1);
+
+               strbuf_addf(&head_msg, "%s (abort): returning to %s",
+                           getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
+                           options.head_name ? options.head_name
+                                             : oid_to_hex(&options.orig_head->object.oid));
                ropts.oid = &options.orig_head->object.oid;
+               ropts.head_msg = head_msg.buf;
                ropts.branch = options.head_name;
                ropts.flags = RESET_HEAD_HARD;
-               ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
                if (reset_head(the_repository, &ropts) < 0)
                        die(_("could not move back to %s"),
                            oid_to_hex(&options.orig_head->object.oid));
index 5c6cd9af3bc406d7f75b60a33aa597d67b642255..ceca160005339168982d702e7d4e4e4897d494d3 100755 (executable)
@@ -187,6 +187,57 @@ test_reflog () {
        EOF
        test_cmp expect actual
        '
+
+       test_expect_success "rebase $mode --abort reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+       git checkout conflicts &&
+       test_when_finished "git reset --hard Q" &&
+
+       git log -g -1 conflicts >branch-expect &&
+       (
+               if test -n "$reflog_action"
+               then
+                       GIT_REFLOG_ACTION="$reflog_action" &&
+                       export GIT_REFLOG_ACTION
+               fi &&
+               test_must_fail git rebase $mode main &&
+               git rebase --abort
+       ) &&
+
+       git log -g --format=%gs -3 >actual &&
+       write_reflog_expect <<-EOF &&
+       ${reflog_action:-rebase} (abort): returning to refs/heads/conflicts
+       ${reflog_action:-rebase} (pick): P
+       ${reflog_action:-rebase} (start): checkout main
+       EOF
+       test_cmp expect actual &&
+
+       # check branch reflog is unchanged
+       git log -g -1 conflicts >branch-actual &&
+       test_cmp branch-expect branch-actual
+       '
+
+       test_expect_success "rebase $mode --abort detached HEAD reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+       git checkout Q &&
+       test_when_finished "git reset --hard Q" &&
+
+       (
+               if test -n "$reflog_action"
+               then
+                       GIT_REFLOG_ACTION="$reflog_action" &&
+                       export GIT_REFLOG_ACTION
+               fi &&
+               test_must_fail git rebase $mode main &&
+               git rebase --abort
+       ) &&
+
+       git log -g --format=%gs -3 >actual &&
+       write_reflog_expect <<-EOF &&
+       ${reflog_action:-rebase} (abort): returning to $(git rev-parse Q)
+       ${reflog_action:-rebase} (pick): P
+       ${reflog_action:-rebase} (start): checkout main
+       EOF
+       test_cmp expect actual
+       '
 }
 
 test_reflog --merge