From: Junio C Hamano <gitster@pobox.com>
To: Phillip Wood <phillip.wood123@gmail.com>
Cc: git@vger.kernel.org, 重田一聖 <kazumasa.shigeta@kanamei.com>
Subject: Re: [PATCH v3 1/7] stash: use an enum for create status
Date: Tue, 20 Oct 2026 23:07:00 -0700 [thread overview]
Message-ID: <xmqq5wyvsi2j.fsf@gitster.g> (raw)
In-Reply-To: <9e27bcd676ee763ed499fb337b1497f3fe2405b2.1792509606.git.phillip.wood@dunelm.org.uk> (Phillip Wood's message of "Tue, 20 Oct 2026 16:20:36 +0100")
Phillip Wood <phillip.wood123@gmail.com> writes:
> 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.
> +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.
> +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?
> +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?
> +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?
> 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.
> } 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.
> @@ -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.
next prev parent reply other threads:[~2026-10-21 6:07 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-05 16:35 [PATCH 0/2] stash: stop checking for changes twice Phillip Wood
2026-10-05 16:35 ` [PATCH 1/2] stash create: remove duplicate changes detection Phillip Wood
2026-10-06 12:44 ` Junio C Hamano
2026-10-07 13:49 ` Phillip Wood
2026-10-05 16:35 ` [PATCH 2/2] stash push: " Phillip Wood
2026-10-06 14:27 ` Junio C Hamano
2026-10-07 13:46 ` Phillip Wood
2026-10-07 17:15 ` Junio C Hamano
2026-10-08 13:35 ` Phillip Wood
2026-10-14 15:40 ` [PATCH v2 0/7] stash creation: avoid unecessary index loads and refreshes Phillip Wood
2026-10-14 15:40 ` [PATCH v2 1/7] stash: use an enum for create status Phillip Wood
2026-10-14 15:40 ` [PATCH v2 2/7] stash create: remove duplicate changes detection Phillip Wood
2026-10-14 15:40 ` [PATCH v2 3/7] stash push: " Phillip Wood
2026-10-14 15:40 ` [PATCH v2 4/7] stash push: avoid duplicate calls to preload_index() Phillip Wood
2026-10-14 18:09 ` Junio C Hamano
2026-10-14 15:40 ` [PATCH v2 5/7] stash create: rework temporary index creation Phillip Wood
2026-10-14 18:16 ` Junio C Hamano
2026-10-20 14:54 ` Phillip Wood
2026-10-14 15:40 ` [PATCH v2 6/7] stash create: remove unnecessary index load and refresh Phillip Wood
2026-10-14 18:21 ` Junio C Hamano
2026-10-14 15:40 ` [PATCH v2 7/7] stash: simplify reset_tree() function signature Phillip Wood
2026-10-20 15:20 ` [PATCH v3 0/7] stash creation: avoid unecessary index loads and refreshes Phillip Wood
2026-10-20 15:20 ` [PATCH v3 1/7] stash: use an enum for create status Phillip Wood
2026-10-21 6:07 ` Junio C Hamano [this message]
2026-10-20 15:20 ` [PATCH v3 2/7] stash create: remove duplicate changes detection Phillip Wood
2026-10-20 15:20 ` [PATCH v3 3/7] stash push: " Phillip Wood
2026-10-20 15:20 ` [PATCH v3 4/7] stash push: avoid duplicate calls to preload_index() Phillip Wood
2026-10-20 15:20 ` [PATCH v3 5/7] stash create: rework temporary index creation Phillip Wood
2026-10-20 22:07 ` Junio C Hamano
2026-10-20 15:20 ` [PATCH v3 6/7] stash create: remove unnecessary index load and refresh Phillip Wood
2026-10-20 15:20 ` [PATCH v3 7/7] stash: simplify reset_tree() function signature Phillip Wood
2026-10-21 0:07 ` [PATCH v3 0/7] stash creation: avoid unecessary index loads and refreshes Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqq5wyvsi2j.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=kazumasa.shigeta@kanamei.com \
--cc=phillip.wood123@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox