[PATCH] stash: infer "push" when push-specific options are given
- From
Deveshi Dwivedi <deveshigurgaon@gmail.com>
- Date
- Apr 4, 2026, 14:36 UTC
- Message-ID
- <20260404143640.6679-1-deveshigurgaon@gmail.com>
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 unambiguously a "push" option, and the user's intent is clear. The same applies to other push-specific options like "--staged", "--keep-index", "--include-untracked", and "--pathspec-from-file".
Expand the set of options that force the "push" assumption to include all push-specific options, so that pathspec arguments are accepted without requiring "--" or the explicit "push" subcommand when the command line already contains a push-only option.
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> --- builtin/stash.c | 4 +++- t/t3903-stash.sh | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/builtin/stash.c b/builtin/stash.c index 95c5005b0b..197814241c 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -1877,7 +1877,9 @@ static int push_stash(int argc, const char **argv, const char *prefix, argc = parse_options(argc, argv, prefix, options, push_assumed ? git_stash_usage : git_stash_push_usage, flags); - force_assume |= patch_mode; + force_assume |= patch_mode || stash_msg || + keep_index != -1 || only_staged || + include_untracked || pathspec_from_file; } if (argc) { diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 70879941c2..9812b64989 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -414,6 +414,27 @@ test_expect_success 'dont assume push with non-option args' ' test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err ' +test_expect_success 'assume push when options imply push' ' + git reset --hard && + 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 +' + test_expect_success 'stash --invalid-option' ' echo bar5 >file && echo bar6 >file2 && base-commit: 2855562ca6a9c6b0e7bc780b050c1e83c9fcfbd0
-- 2.52.0.230.gd8af7cadaa