stash: allow "git stash -p <pathspec>" to assume push again

Historically "git stash [<options>]" was assumed to mean "git stash save [<options>]". Since 1ada5020b38 (stash: use stash_push for no verb form, 2017-02-28) it is assumed to mean "git stash push [<options>]". As the push subcommand supports pathspecs, 9e140909f61 (stash: allow pathspecs in the no verb form, 2017-02-28) allowed "git stash -p <pathspec>" to mean "git stash push -p <pathspec>". This was broken in 8c3713cede7 (stash: eliminate crude option parsing, 2020-02-17) which failed to account for "push" being added to the start of argv in cmd_stash() before it calls push_stash() and kept looking in argv[0] for "-p" after moving the code to push_stash(). Fix this by regression by checking argv[1] instead of argv[0] and add a couple of tests to prevent future regressions. Helped-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jun 7, 2025 at 10:45 UTC e6659b77dfed6f3778e5c6870927be3da082d4f5
2 files changed +23 -1
builtin/stash.c
+1 -1
@@ -1789,7 +1789,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1789 int ret;
1790
1791 if (argc) {
1792 - force_assume = !strcmp(argv[0], "-p");
1792 + force_assume = argc > 1 && !strcmp(argv[1], "-p");
1793 argc = parse_options(argc, argv, prefix, options,
1794 push_assumed ? git_stash_usage :
1795 git_stash_push_usage,
t/t3903-stash.sh
+22
@@ -1177,6 +1177,28 @@ test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1177 test_path_is_file bar
1178 '
1179
1180 +test_expect_success 'stash -p <pathspec> stash and restores the file' '
1181 + test_write_lines b c >file &&
1182 + git commit -m "add a few lines" file &&
1183 + test_write_lines a b c d >file &&
1184 + test_write_lines b c d >expect-file &&
1185 + echo changed-other-file >other-file &&
1186 + test_write_lines s y n | git stash -p file &&
1187 + test_cmp expect-file file &&
1188 + echo changed-other-file >expect &&
1189 + test_cmp expect other-file &&
1190 + git checkout HEAD -- file &&
1191 + git stash pop &&
1192 + test_cmp expect other-file &&
1193 + test_write_lines a b c >expect &&
1194 + test_cmp expect file
1195 +'
1196 +
1197 +test_expect_success 'stash <pathspec> -p is rejected' '
1198 + test_must_fail git stash file -p 2>err &&
1199 + test_grep "subcommand wasn${SQ}t specified; ${SQ}push${SQ} can${SQ}t be assumed due to unexpected token ${SQ}file${SQ}" err
1200 +'
1201 +
1202 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1203 mkdir sub &&
1204 >foo &&