git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] checkout: print blank line after autostash conflict advice

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 31, 2026, 17:19 UTC
Message-ID
<xmqq4igaxl5t.fsf@gitster.g>
In-Reply-To
<pull.2364.v2.git.git.1788177601572.gitgitgadget@gmail.com>
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 5 quoted lines
> To learn whether the stash apply resulted in conflicts, make the
> autostash apply functions return 1 when the stash could not be applied
> due to conflicts (and was stored back), while success and error remain
> 0 and -1 respectively.  Checkout can then use the return value to decide
> whether to print the blank line.
Show 40 quoted lines
> @@ -1237,12 +1238,12 @@ static int switch_branches(const struct checkout_opts *opts,
>  				git_config_push_parameter(cfg.buf);
>  				strbuf_release(&cfg);
>  			}
> -			apply_autostash_ref(the_repository,
> -					    "CHECKOUT_AUTOSTASH_HEAD",
> -					    new_branch_info->name,
> -					    "local",
> -					    stash_label_base,
> -					    autostash_msg.buf);
> +			autostash_res = apply_autostash_ref(the_repository,
> +				    "CHECKOUT_AUTOSTASH_HEAD",
> +				    new_branch_info->name,
> +				    "local",
> +				    stash_label_base,
> +				    autostash_msg.buf);
>  		}
>  		if (ret) {
>  			branch_info_release(&old_branch_info);
> @@ -1255,6 +1256,8 @@ static int switch_branches(const struct checkout_opts *opts,
>  	if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)
>  		orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit);
>  
> +	if (autostash_res == 1 && !opts->quiet)
> +		fputc('\n', stderr);
>  	update_refs_for_switch(opts, &old_branch_info, new_branch_info);
>  
>  	if (created_autostash) {
> diff --git a/sequencer.c b/sequencer.c
> index 65afd100d9..5ed9ae86c4 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -4815,7 +4815,8 @@ static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply,
>  		if (label_base)
>  			strvec_pushf(&child.args, "--label-base=%s", label_base);
>  		strvec_push(&child.args, stash_oid);
> -		ret = run_command(&child);
> +		if (run_command(&child))
> +			ret = 1;
>  	}

This does not look like the right way to have the function return 1 if the objective is to do so only when the spawned "git stash apply <oid>" process fails due to conflicts.

The reasons for a non-zero return value from run_command() range from failing to find the command to run, to the command crashing, to the command voluntarily exiting with a fixed exit status to signal how exactly it exited to the spawning process.

Skimming bultin/stash.c::do_apply_stash(), I find this bit:
	clean = merge_ort_nonrecursive(&o, head, merge, merge_base);
	/*
	 * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the
	 * merge was clean, and nonzero if the merge was unclean or encountered
	 * an error.
	 */
	ret = clean >= 0 ? !clean : clean;

If 'clean' is 0 (i.e., unclean merge), we assign 1 (== !clean) to ret. If 'clean' is 1 or more (presumably a clean merge? Check what merge_ort_nonrecursive() does for details---I didn't), return 0. If merge-ort gave an error (i.e., negative), use it as-is. And this 'ret' is eventually returned to the caller apply_stash() , which returns that value as-is.

The value is given back to builtin/stash.c::cmd_stash() and the more detailed exit value unfortunately discarded ehre:

	if (fn)
		return !!fn(argc, argv, prefix, repo);
but if one wants to implement 
    > To learn whether the stash apply resulted in conflicts, make the
    > autostash apply functions return 1 when the stash could not be applied
    > due to conflicts (and was stored back),...
correctly, I think this patch needs a bit more work.  
 (1) Find the values returned by foo_stash() implementations of all
     'git stash foo' subcommands.  Do they follow the 0 == success,
     negative == failure, positive == something else convention?
 (2) Update the code above to something like
	if (fn) {
		int ret = fn(argc, argv, prefix, repo);
		if (ret < 0)
			return 1; /* as before */
		if (!ret)
			return 0; /* as before */
		return ret + 1;
	}
      or with whatever necessary tweak to allow the positive "I have
      something to tell the spawning process" case from the bog
      standard and boring 'return error("message");' cases.
 (3) document what each of 'git stash foo' signals with its exit
     status.

For expediency, it may be OK to assume any and all failures from "git stash apply <oid>" come from a conflicted stash application in your first version. If that is what your reviewer recommended, I would agree. But let's help users and future developers (who do not necessarily have to be you) by leaving a note that this code is not doing what it claims to do and needs more work in the code.

Thanks.
Previous: Harald Nordgren via GitGitGadgetNext: Phillip Wood
Message 12 of 37 in “checkout -m: refine autostash fallback”
  1. 0/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Jul 25, 2026
  2. 1/2 sequencer: teach autostash apply to report conflictsHarald Nordgren via GitGitGadget, Jul 25, 2026
  3. Phillip WoodAug 27, 2026
  4. Harald NordgrenAug 31, 2026
  5. phillip.wood123@gmail.comAug 31, 2026
  6. 2/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Jul 25, 2026
  7. Junio C HamanoJul 28, 2026
  8. Phillip WoodAug 27, 2026
  9. Junio C HamanoAug 26, 2026
  10. Phillip WoodAug 27, 2026
  11. checkout: print blank line after autostash conflict adviceHarald Nordgren via GitGitGadget, Aug 31, 2026
  12. Junio C HamanoAug 31, 2026
  13. Phillip WoodSep 1, 2026
  14. Junio C HamanoSep 1, 2026
  15. checkout: separate autostash conflict advice from branch-switch messageHarald Nordgren via GitGitGadget, Sep 1, 2026
  16. Phillip WoodSep 1, 2026
  17. Junio C HamanoSep 1, 2026
  18. 0/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Sep 2, 2026
  19. 1/2 stash: reserve exit status 1 for conflictsHarald Nordgren via GitGitGadget, Sep 2, 2026
  20. Junio C HamanoSep 2, 2026
  21. Junio C HamanoSep 2, 2026
  22. Phillip WoodSep 3, 2026
  23. Harald NordgrenSep 3, 2026
  24. Junio C HamanoSep 3, 2026
  25. Harald NordgrenSep 3, 2026
  26. Junio C HamanoSep 3, 2026
  27. Harald NordgrenSep 4, 2026
  28. Phillip WoodSep 4, 2026
  29. Harald NordgrenSep 4, 2026
  30. Junio C HamanoSep 4, 2026
  31. 2/2 checkout: separate autostash conflict advice from branch-switch messageHarald Nordgren via GitGitGadget, Sep 2, 2026
  32. Junio C HamanoSep 2, 2026
  33. Phillip WoodSep 3, 2026
  34. 0/2 checkout -m: refine autostash fallbackHarald Nordgren via GitGitGadget, Sep 3, 2026
  35. 1/2 stash: reserve exit status 1 for conflictsHarald Nordgren via GitGitGadget, Sep 3, 2026
  36. 2/2 checkout: separate autostash conflict advice from branch-switch messageHarald Nordgren via GitGitGadget, Sep 3, 2026
  37. Junio C HamanoSep 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.