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

Re: [PATCH v3 1/7] stash: use an enum for create status

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 21, 2026, 06:07 UTC
Message-ID
<xmqq5wyvsi2j.fsf@gitster.g>
In-Reply-To
<9e27bcd676ee763ed499fb337b1497f3fe2405b2.1792509606.git.phillip.wood@dunelm.org.uk>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 16 quoted lines
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> Change the return type of do_create_stash() to an enum in preparation
> for adding a separate value for "there are no changes to stash" in a
> future commit. There is a small change to behavior here which fixes
> an anomaly with the exit status of create_stash(). That function calls
> check_changes() without refreshing the index and exits 0 if there are
> no changes. However, because it does not refresh the index, that check
> is unreliable which means do_create_stash() can return 1 when there
> aren't any changes, and then create_stash() exits 1 rather than 0.
> This commit fixes that anomaly.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
>  builtin/stash.c | 83 ++++++++++++++++++++++++++++---------------------
>  1 file changed, 47 insertions(+), 36 deletions(-)
The patch makes it look more scary larger than it actually is.
Show 6 quoted lines
> +enum create_result {
> +	CREATE_ERROR = -1,
> +	CREATE_SUCCESS,
> +	/* No changes were stashed */
> +	CREATE_EMPTY,
> +};

SUCCESS immediatly following ERROR that is explicitly defined to be (-1) may qualify as explicitly defined to be 0, but personally I probably prefer to be extra explicit here, like

	CREATE_ERROR = -1,
	CREATE_SUCCESS = 0,
even though this is not worth rerolling the series for.
Show 5 quoted lines
> +static enum create_result stash_staged(struct stash_info *info,
> +				  struct strbuf *out_patch, int quiet)
>  {
> -	int ret = 0;
> +	enum create_result ret = 0;

Not CREATE_SUCCESS? This can be returned if various checks in the function assigns different values to this variable, right?

Show 6 quoted lines
> +static enum create_result stash_patch(struct stash_info *info,
> +		const struct pathspec *ps, struct strbuf *out_patch, int quiet,
> +		struct interactive_options *interactive_opts)
>  {
> -	int ret = 0;
> +	enum create_result ret = 0;
CREATE_SUCCESS?
Show 8 quoted lines
> +static enum create_result do_create_stash(const struct pathspec *ps,
> +		struct strbuf *stash_msg_buf, int include_untracked,
> +		int patch_mode, struct interactive_options *interactive_opts,
> +		int only_staged, struct stash_info *info, struct strbuf *patch,
> +		int quiet)
>  {
> -	int ret = 0;
> +	enum create_result ret = 0;
CREATE_SUCCESS?
Show 12 quoted lines
>  	if (patch_mode) {
>  		ret = stash_patch(info, ps, patch, quiet, interactive_opts);
> -		if (ret < 0) {
> +		if (ret == CREATE_ERROR) {
>  			if (!quiet)
>  				fprintf_ln(stderr, _("Cannot save the current "
>  						     "worktree state"));
>  			goto done;
> -		} else if (ret > 0) {
> +		} else if (ret != CREATE_SUCCESS) {
>  			goto done;
>  		}

I wonder if switch/case is easier to read for these users of these enum return values. Some check explicitly against CREATE_ERROR (like the one above), some treat anything other than CREATE_SUCCESS as undesirable, etc.

Show 13 quoted lines
>  	} else if (only_staged) {
>  		ret = stash_staged(info, patch, quiet);
> -		if (ret < 0) {
> +		if (ret == CREATE_ERROR) {
>  			if (!quiet)
>  				fprintf_ln(stderr, _("Cannot save the current "
>  						     "staged state"));
>  			goto done;
> -		} else if (ret > 0) {
> +		} else if (ret != CREATE_SUCCESS) {
>  			goto done;
>  		}
>  	} else {
Ditto.
Show 14 quoted lines
> @@ -1660,12 +1667,13 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
>  
>  	ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
>  			      NULL, 0);
> -	if (!ret)
> +	if (ret == CREATE_SUCCESS)
>  		printf_ln("%s", oid_to_hex(&info.w_commit));
>  
>  	free_stash_info(&info);
>  	strbuf_release(&stash_msg_buf);
> -	return ret;
> +	/* Do not return an error if there are no changes */
> +	return ret == CREATE_ERROR ? -1 : 0;
>  }
Ditto.
Previous: Phillip WoodNext: Phillip Wood
Message 24 of 47 in “stash: stop checking for changes twice”
  1. 0/2 stash: stop checking for changes twicePhillip Wood, Oct 5, 2026
  2. 1/2 stash create: remove duplicate changes detectionPhillip Wood, Oct 5, 2026
  3. Junio C HamanoOct 6, 2026
  4. Phillip WoodOct 7, 2026
  5. 2/2 stash push: remove duplicate changes detectionPhillip Wood, Oct 5, 2026
  6. Junio C HamanoOct 6, 2026
  7. Phillip WoodOct 7, 2026
  8. Junio C HamanoOct 7, 2026
  9. Phillip WoodOct 8, 2026
  10. 0/7 stash creation: avoid unecessary index loads and refreshesPhillip Wood, Oct 14, 2026
  11. 1/7 stash: use an enum for create statusPhillip Wood, Oct 14, 2026
  12. 2/7 stash create: remove duplicate changes detectionPhillip Wood, Oct 14, 2026
  13. 3/7 stash push: remove duplicate changes detectionPhillip Wood, Oct 14, 2026
  14. 4/7 stash push: avoid duplicate calls to preload_index()Phillip Wood, Oct 14, 2026
  15. Junio C HamanoOct 14, 2026
  16. 5/7 stash create: rework temporary index creationPhillip Wood, Oct 14, 2026
  17. Junio C HamanoOct 14, 2026
  18. Phillip WoodOct 20, 2026
  19. 6/7 stash create: remove unnecessary index load and refreshPhillip Wood, Oct 14, 2026
  20. Junio C HamanoOct 14, 2026
  21. 7/7 stash: simplify reset_tree() function signaturePhillip Wood, Oct 14, 2026
  22. 0/7 stash creation: avoid unecessary index loads and refreshesPhillip Wood, Oct 20, 2026
  23. 1/7 stash: use an enum for create statusPhillip Wood, Oct 20, 2026
  24. Junio C HamanoOct 21, 2026
  25. Phillip WoodOct 22, 2026
  26. Junio C HamanoOct 22, 2026
  27. Phillip WoodOct 22, 2026
  28. 2/7 stash create: remove duplicate changes detectionPhillip Wood, Oct 20, 2026
  29. 3/7 stash push: remove duplicate changes detectionPhillip Wood, Oct 20, 2026
  30. 4/7 stash push: avoid duplicate calls to preload_index()Phillip Wood, Oct 20, 2026
  31. 5/7 stash create: rework temporary index creationPhillip Wood, Oct 20, 2026
  32. Junio C HamanoOct 20, 2026
  33. 6/7 stash create: remove unnecessary index load and refreshPhillip Wood, Oct 20, 2026
  34. 7/7 stash: simplify reset_tree() function signaturePhillip Wood, Oct 20, 2026
  35. Junio C HamanoOct 21, 2026
  36. 0/7 stash creation: avoid unecessary index loads and refreshesPhillip Wood, Oct 23, 2026
  37. 1/7 stash: use an enum for create statusPhillip Wood, Oct 23, 2026
  38. 2/7 stash create: remove duplicate changes detectionPhillip Wood, Oct 23, 2026
  39. 3/7 stash push: remove duplicate changes detectionPhillip Wood, Oct 23, 2026
  40. 4/7 stash push: avoid duplicate calls to preload_index()Phillip Wood, Oct 23, 2026
  41. 5/7 stash create: rework temporary index creationPhillip Wood, Oct 23, 2026
  42. Junio C HamanoNov 5, 2026
  43. 6/7 stash create: remove unnecessary index load and refreshPhillip Wood, Oct 23, 2026
  44. Junio C HamanoNov 6, 2026
  45. 7/7 stash: simplify reset_tree() function signaturePhillip Wood, Oct 23, 2026
  46. Junio C HamanoNov 6, 2026
  47. Junio C HamanoNov 5, 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.