Skip to content

Commit afdaa41

Browse files
committed
stash: allow "git stash -p <pathspec>" to assume push again
Historically "git stash [<options>]" was assumed to mean "git stash save [<options>]". Since 1ada502 (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 9e14090 (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 8c3713c (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(). The support for assuming "push" when "-p" is given introduced in 9e14090 is very narrow, neither "git stash -m <message> -p <pathspec>" nor "git stash --patch <pathspec>" imply "push" and die instead. Fix the regression introduced by 8c3713c and relax the behavior introduced in 9e14090 by passing PARSE_OPT_STOP_AT_NON_OPTION when push is being assumed and then setting "force_assume" if "--patch" was present. This means "git stash <pathspec> -p" still dies so do assume the user meant "push" if they mistype a subcommand name but "git stash -m <message> -p <pathspec>" will now succeed. Tests are added to prevent future regressions. Signed-off-by: Phillip Wood <[email protected]>
1 parent 1a8a497 commit afdaa41

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

builtin/stash.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,11 +1789,15 @@ static int push_stash(int argc, const char **argv, const char *prefix,
17891789
int ret;
17901790

17911791
if (argc) {
1792-
force_assume = !strcmp(argv[0], "-p");
1792+
int flags = PARSE_OPT_KEEP_DASHDASH;
1793+
1794+
if (push_assumed)
1795+
flags |= PARSE_OPT_STOP_AT_NON_OPTION;
1796+
17931797
argc = parse_options(argc, argv, prefix, options,
17941798
push_assumed ? git_stash_usage :
1795-
git_stash_push_usage,
1796-
PARSE_OPT_KEEP_DASHDASH);
1799+
git_stash_push_usage, flags);
1800+
force_assume |= patch_mode;
17971801
}
17981802

17991803
if (argc) {

t/t3903-stash.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,25 @@ test_expect_success 'stash -- <pathspec> stashes and restores the file' '
11771177
test_path_is_file bar
11781178
'
11791179

1180+
test_expect_success 'stash --patch <pathspec> stash and restores the file' '
1181+
cat file >expect-file &&
1182+
echo changed-file >file &&
1183+
echo changed-other-file >other-file &&
1184+
echo a | git stash -m "stash bar" --patch file &&
1185+
test_cmp expect-file file &&
1186+
echo changed-other-file >expect &&
1187+
test_cmp expect other-file &&
1188+
git stash pop &&
1189+
test_cmp expect other-file &&
1190+
echo changed-file >expect &&
1191+
test_cmp expect file
1192+
'
1193+
1194+
test_expect_success 'stash <pathspec> -p is rejected' '
1195+
test_must_fail git stash file -p 2>err &&
1196+
test_grep "subcommand wasn${SQ}t specified; ${SQ}push${SQ} can${SQ}t be assumed due to unexpected token ${SQ}file${SQ}" err
1197+
'
1198+
11801199
test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
11811200
mkdir sub &&
11821201
>foo &&

0 commit comments

Comments
 (0)