Git development
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Deveshi Dwivedi <deveshigurgaon@gmail.com>, git@vger.kernel.org
Cc: ben.knoble@gmail.com, mroik@delayed.space,
	quentin.bernet@bluewin.ch, gitster@pobox.com
Subject: Re: [PATCH v4] stash: infer "push" when command line starts with an option
Date: Mon, 13 Apr 2026 10:08:13 +0100	[thread overview]
Message-ID: <00b92c0b-8602-45e4-bec3-c7c538bd288b@gmail.com> (raw)
In-Reply-To: <20260412195204.4636-1-deveshigurgaon@gmail.com>

On 12/04/2026 20:52, Deveshi Dwivedi wrote:
> When "git stash" is run without the "push" subcommand, the command
> tries to assume "push" but rejects any non-option arguments (i.e.,
> pathspecs without "--") to avoid treating a misspelled subcommand
> name as a pathspec.  The only exception is "-p", which sets
> force_assume and allows pathspecs to follow.
> 
> This means "git stash -m foo file" is rejected even though "-m" is
> clearly an option and not a subcommand name, and the user's intent
> is clear.  The same applies to any command line that begins with an
> option.
> 
> A command line that begins with an option cannot be naming a "git
> stash" subcommand, so unconditionally assume "push" in that case and
> allow pathspec arguments to follow without requiring "--".  This is
> simpler and more robust than checking a specific list of options,
> and remains correct even if push or other subcommands gain new
> options in the future.
> 
> Note that this does not check for negated options, so "git stash
> --no-staged [<pathspec>]" is still rejected.  Handling negated
> options would require teaching the inference logic about them
> explicitly.

That was true of the implementation in V3 which checked to see if any of 
the option variables were non-zero. Looking below, it now checks if the 
first argument begins with "-" which means that force_assume will be 
true when "--no-stage" is given.

The implementation looks good, I've left a couple of comments below.

> This was marked as #leftoverbits in [1].
> 
> [1] https://lore.kernel.org/git/xmqqtsu1jipp.fsf@gitster.g/
>  
> Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com>
> ---
> 
> Changes since v3:
>    - Rewrote the approach per Junio and Phillip's suggestion: instead of
>      checking a specific list of push-only options, unconditionally
>      assume "push" whenever the command line begins with any option.
>      This is simpler and robust against future option additions, and
>      sidesteps the fact that -m and --include-untracked are not unique
>      to "push".
>    - Updated the test to reflect the new rule and switched cleanup to
>      test_when_finished per Junio's suggestion.
>    - Updated documentation accordingly.
> 
>   Documentation/git-stash.adoc |  7 ++++---
>   builtin/stash.c              |  6 ++++--
>   t/t3903-stash.sh             | 26 ++++++++++++++++++++++++--
>   3 files changed, 32 insertions(+), 7 deletions(-)
> 
> diff --git a/Documentation/git-stash.adoc b/Documentation/git-stash.adoc
> index 235d57ddd8..135719611a 100644
> --- a/Documentation/git-stash.adoc
> +++ b/Documentation/git-stash.adoc
> @@ -61,9 +61,10 @@ COMMANDS
>   +
>   For quickly making a snapshot, you can omit "push".  In this mode,
>   non-option arguments are not allowed to prevent a misspelled
> -subcommand from making an unwanted stash entry.  The two exceptions to this
> -are `stash -p` which acts as alias for `stash push -p` and pathspec elements,
> -which are allowed after a double hyphen `--` for disambiguation.
> +subcommand from making an unwanted stash entry.  Pathspec elements
> +are allowed after a double hyphen `--` for disambiguation.  When
> +the command line begins with an option, "push" is inferred and

"assumed" might be easier to understand than "inferred"

> +pathspec arguments are also accepted without `--`.
>   
>   `save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-u | --include-untracked] [-a | --all] [-q | --quiet] [<message>]`::
>   
> diff --git a/builtin/stash.c b/builtin/stash.c
> index 95c5005b0b..be96338d35 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -1871,13 +1871,15 @@ static int push_stash(int argc, const char **argv, const char *prefix,
>   	if (argc) {
>   		int flags = PARSE_OPT_KEEP_DASHDASH;
>   
> -		if (push_assumed)
> +		if (push_assumed) {
>   			flags |= PARSE_OPT_STOP_AT_NON_OPTION;
> +			if (argc > 1 && argv[1][0] == '-')
> +				force_assume = 1;

We assume push was given if the first argument starts with '-'. That 
makes sense. If we get on invalid option we'll display the push usage as 
we did before.

> +		}
>   
>   		argc = parse_options(argc, argv, prefix, options,
>   				     push_assumed ? git_stash_usage :
>   				     git_stash_push_usage, flags);
> -		force_assume |= patch_mode;
>   	}
>   
>   	if (argc) {
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 70879941c2..88f2b3c86b 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -410,8 +410,30 @@ test_expect_success 'stash --staged with binary file' '
>   '
>   
>   test_expect_success 'dont assume push with non-option args' '
> -	test_must_fail git stash -q drop 2>err &&
> -	test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
> +	test_must_fail git stash someunknown 2>err &&
> +	test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''someunknown'\''" err

This is based on the existing test, but there is no need for "-e" and we 
normally match a single quote as "${SQ}" (which is defined by the test 
suite) or simply "."

> +'
> +
> +test_expect_success 'assume push when command line starts with option' '
> +	test_when_finished "git reset --hard" &&
> +	test_when_finished "rm -f untracked-file" &&
> +	echo changed >file &&
> +	git add file &&
> +	git stash -m "implied push" file &&
> +	git stash pop &&
> +
> +	git add file &&
> +	git stash --staged file &&
> +	git stash pop &&
> +
> +	git add file &&
> +	git stash --keep-index file &&
> +	git stash pop &&
> +
> +	echo untracked >untracked-file &&
> +	git stash --include-untracked untracked-file &&
> +	test_path_is_missing untracked-file &&
> +	git stash pop
>   '

This test looks good

Thanks

Phillip

>   test_expect_success 'stash --invalid-option' '
> 
> base-commit: 2855562ca6a9c6b0e7bc780b050c1e83c9fcfbd0


  reply	other threads:[~2026-04-13  9:08 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-04 14:36 [PATCH] stash: infer "push" when push-specific options are given Deveshi Dwivedi
2026-04-04 15:19 ` Mirko Faina
2026-04-04 16:03 ` [PATCH v2] " Deveshi Dwivedi
2026-04-04 23:40   ` Mirko Faina
2026-04-05  7:02     ` Deveshi Dwivedi
2026-04-05 11:09 ` [PATCH v3] " Deveshi Dwivedi
2026-04-06 18:15   ` Mirko Faina
2026-04-07  9:36     ` Phillip Wood
2026-04-09 19:22       ` Deveshi Dwivedi
2026-04-09 19:37         ` Mirko Faina
2026-04-09 20:31       ` Junio C Hamano
2026-04-09 20:22   ` Junio C Hamano
2026-04-12 19:52 ` [PATCH v4] stash: infer "push" when command line starts with an option Deveshi Dwivedi
2026-04-13  9:08   ` Phillip Wood [this message]
2026-04-13 15:09   ` Junio C Hamano
2026-04-19 16:54 ` [PATCH v5] stash: assume " Deveshi Dwivedi
2026-04-21 15:28   ` Phillip Wood

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=00b92c0b-8602-45e4-bec3-c7c538bd288b@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=ben.knoble@gmail.com \
    --cc=deveshigurgaon@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mroik@delayed.space \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=quentin.bernet@bluewin.ch \
    /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