rebase --abort: improve reflog message
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>
Phillip Wood committed
Oct 12, 2022 at 09:35 UTC
6159e7add493cf1d62b4817eb86ac8890e768c17
2 files changed
+59
-20
builtin/rebase.c
+8
-20
@@ -1001,23 +1001,6 @@ static void NORETURN error_on_missing_default_upstream(void)
1001
exit(1);
1002
}
1003
1004
-static void set_reflog_action(struct rebase_options *options)
1005
-{
1006
- const char *env;
1007
- struct strbuf buf = STRBUF_INIT;
1008
-
1009
- if (!is_merge(options))
1010
- return;
1011
-
1012
- env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
1013
- if (env && strcmp("rebase", env))
1014
- return; /* only override it if it is "rebase" */
1015
-
1016
- strbuf_addf(&buf, "rebase (%s)", options->action);
1017
- setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
1018
- strbuf_release(&buf);
1019
-}
1020
-
1004
static int check_exec_cmd(const char *cmd)
1005
{
1006
if (strchr(cmd, '\n'))
@@ -1311,18 +1294,23 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1294
}
1295
case ACTION_ABORT: {
1296
struct string_list merge_rr = STRING_LIST_INIT_DUP;
1314
- options.action = "abort";
1315
- set_reflog_action(&options);
1297
+ struct strbuf head_msg = STRBUF_INIT;
1298
1299
+ options.action = "abort";
1300
rerere_clear(the_repository, &merge_rr);
1301
string_list_clear(&merge_rr, 1);
1302
1303
if (read_basic_state(&options))
1304
exit(1);
1305
+
1306
+ strbuf_addf(&head_msg, "%s (abort): returning to %s",
1307
+ getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1308
+ options.head_name ? options.head_name
1309
+ : oid_to_hex(&options.orig_head->object.oid));
1310
ropts.oid = &options.orig_head->object.oid;
1311
+ ropts.head_msg = head_msg.buf;
1312
ropts.branch = options.head_name;
1313
ropts.flags = RESET_HEAD_HARD;
1325
- ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
1314
if (reset_head(the_repository, &ropts) < 0)
1315
die(_("could not move back to %s"),
1316
oid_to_hex(&options.orig_head->object.oid));
t/t3406-rebase-message.sh
+51
@@ -187,6 +187,57 @@ test_reflog () {
187
EOF
188
test_cmp expect actual
189
'
190
+
191
+ test_expect_success "rebase $mode --abort reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
192
+ git checkout conflicts &&
193
+ test_when_finished "git reset --hard Q" &&
194
+
195
+ git log -g -1 conflicts >branch-expect &&
196
+ (
197
+ if test -n "$reflog_action"
198
+ then
199
+ GIT_REFLOG_ACTION="$reflog_action" &&
200
+ export GIT_REFLOG_ACTION
201
+ fi &&
202
+ test_must_fail git rebase $mode main &&
203
+ git rebase --abort
204
+ ) &&
205
+
206
+ git log -g --format=%gs -3 >actual &&
207
+ write_reflog_expect <<-EOF &&
208
+ ${reflog_action:-rebase} (abort): returning to refs/heads/conflicts
209
+ ${reflog_action:-rebase} (pick): P
210
+ ${reflog_action:-rebase} (start): checkout main
211
+ EOF
212
+ test_cmp expect actual &&
213
+
214
+ # check branch reflog is unchanged
215
+ git log -g -1 conflicts >branch-actual &&
216
+ test_cmp branch-expect branch-actual
217
+ '
218
+
219
+ test_expect_success "rebase $mode --abort detached HEAD reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
220
+ git checkout Q &&
221
+ test_when_finished "git reset --hard Q" &&
222
+
223
+ (
224
+ if test -n "$reflog_action"
225
+ then
226
+ GIT_REFLOG_ACTION="$reflog_action" &&
227
+ export GIT_REFLOG_ACTION
228
+ fi &&
229
+ test_must_fail git rebase $mode main &&
230
+ git rebase --abort
231
+ ) &&
232
+
233
+ git log -g --format=%gs -3 >actual &&
234
+ write_reflog_expect <<-EOF &&
235
+ ${reflog_action:-rebase} (abort): returning to $(git rev-parse Q)
236
+ ${reflog_action:-rebase} (pick): P
237
+ ${reflog_action:-rebase} (start): checkout main
238
+ EOF
239
+ test_cmp expect actual
240
+ '
241
}
242
243
test_reflog --merge