Commit

2ba77ea82891678dee18fe869b0aecaa1019298e

checkout: separate autostash conflict advice from branch-switch message

"git checkout -m" stashes the user's local changes when it cannot
perform the checkout, and then applies the stash.  When applying the
stash results in conflicts, the advice on how to deal with them is
printed directly on top of the branch-switch message ("Switched to
branch ..."), making the two hard to tell apart.  Print a blank line
in between so that the advice and the branch-switch message are
visually distinct.

apply_autostash_ref() reports whether applying the stash resulted in
conflicts via its enum stash_apply_result return value, so only print
the blank line in the conflicted case.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Harald Nordgren <haraldnordgren@gmail.com> · 2026-09-03 14:39 UTC · committed by Junio C Hamano

Browse files
2 files changed, +21 −10
modified builtin/checkout.c
@@ -1163,6 +1163,7 @@ static int switch_branches(const struct checkout_opts *opts,
1163 1163 int flag, writeout_error = 0;
1164 1164 int do_merge = 1;
1165 1165 int created_autostash = 0;
1166 + enum stash_apply_result autostash_res = STASH_APPLY_CLEAN;
1166 1167 struct strbuf old_commit_shortname = STRBUF_INIT;
1167 1168 struct strbuf autostash_msg = STRBUF_INIT;
1168 1169 const char *stash_label_base = NULL;
@@ -1234,12 +1235,12 @@ static int switch_branches(const struct checkout_opts *opts,
1234 1235 git_config_push_parameter(cfg.buf);
1235 1236 strbuf_release(&cfg);
1236 1237 }
1237 - apply_autostash_ref(the_repository,
1238 - "CHECKOUT_AUTOSTASH_HEAD",
1239 - new_branch_info->name,
1240 - "local",
1241 - stash_label_base,
1242 - autostash_msg.buf);
1238 + autostash_res = apply_autostash_ref(the_repository,
1239 + "CHECKOUT_AUTOSTASH_HEAD",
1240 + new_branch_info->name,
1241 + "local",
1242 + stash_label_base,
1243 + autostash_msg.buf);
1243 1244 }
1244 1245 if (ret) {
1245 1246 branch_info_release(&old_branch_info);
@@ -1252,6 +1253,8 @@ static int switch_branches(const struct checkout_opts *opts,
1252 1253 if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)
1253 1254 orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit);
1254 1255
1256 + if (autostash_res == STASH_APPLY_CONFLICT && !opts->quiet)
1257 + fputc('\n', stderr);
1255 1258 update_refs_for_switch(opts, &old_branch_info, new_branch_info);
1256 1259
1257 1260 if (created_autostash) {
modified t/t7201-co.sh
@@ -236,10 +236,18 @@ test_expect_success 'checkout -m creates a recoverable stash on conflict' '
236 236 test_must_fail git checkout side 2>stderr &&
237 237 test_grep "Your local changes" stderr &&
238 238 git checkout -m side >actual 2>&1 &&
239 - test_grep "resulted in conflicts" actual &&
240 - test_grep "git stash drop" actual &&
241 - test_grep "git stash pop" actual &&
242 - test_grep "The following paths have local changes" actual &&
239 + cat >expect <<-EOF &&
240 + Your local changes are stashed, however applying them
241 + resulted in conflicts. You can either resolve the conflicts
242 + and then discard the stash with "git stash drop", or, if you
243 + do not want to resolve them now, run "git reset --hard" and
244 + apply the local changes later by running "git stash pop".
245 +
246 + Switched to branch ${SQ}side${SQ}
247 + The following paths have local changes:
248 + M one
249 + EOF
250 + test_cmp expect actual &&
243 251 git log -p -1 --format="%gs%n%B" -g --diff-merges=1 refs/stash >actual &&
244 252 sed /^index/d actual >actual.trimmed &&
245 253 cat >expect <<-EOF &&