]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: allow "git stash [<options>] --patch <pathspec>" to assume push
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Sat, 7 Jun 2025 09:45:26 +0000 (10:45 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 7 Jun 2025 17:37:17 +0000 (10:37 -0700)
The support for assuming "push" when "-p" is given introduced in
9e140909f61 (stash: allow pathspecs in the no verb form, 2017-02-28) is
very narrow, neither "git stash -m <message> -p <pathspec>" nor "git
stash --patch <pathspec>" imply "push" and die instead. Relax this 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 that it does not assume the user meant
"push" if they mistype a subcommand name but "git stash -m <message> -p
<pathspec>" will now succeed. The test added in the last commit is
adjusted to check that push is still assumed when "--patch" comes after
other options on the command-line.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index bc2c34fa048b9f7b3ac08edd3fc65214582af82d..b12fd6c40f100bfc3c55134f8b7a421cead07fed 100644 (file)
@@ -1789,11 +1789,15 @@ static int push_stash(int argc, const char **argv, const char *prefix,
        int ret;
 
        if (argc) {
-               force_assume = argc > 1 && !strcmp(argv[1], "-p");
+               int flags = PARSE_OPT_KEEP_DASHDASH;
+
+               if (push_assumed)
+                       flags |= PARSE_OPT_STOP_AT_NON_OPTION;
+
                argc = parse_options(argc, argv, prefix, options,
                                     push_assumed ? git_stash_usage :
-                                    git_stash_push_usage,
-                                    PARSE_OPT_KEEP_DASHDASH);
+                                    git_stash_push_usage, flags);
+               force_assume |= patch_mode;
        }
 
        if (argc) {
index a99a746221e5004bb91d724aacbc09794ad1545b..2bba3baa10f979f400e85c9b24db46196c23d863 100755 (executable)
@@ -1177,13 +1177,13 @@ test_expect_success 'stash -- <pathspec> stashes and restores the file' '
        test_path_is_file bar
 '
 
-test_expect_success 'stash -p <pathspec> stash and restores the file' '
+test_expect_success 'stash --patch <pathspec> stash and restores the file' '
        test_write_lines b c >file &&
        git commit -m "add a few lines" file &&
        test_write_lines a b c d >file &&
        test_write_lines b c d >expect-file &&
        echo changed-other-file >other-file &&
-       test_write_lines s y n | git stash -p file &&
+       test_write_lines s y n | git stash -m "stash bar" --patch file &&
        test_cmp expect-file file &&
        echo changed-other-file >expect &&
        test_cmp expect other-file &&