git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/2] stash: allow "git stash [<options>] --patch <pathspec>" to assume push

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Jun 6, 2025, 11:32 UTC
Message-ID
<CAN0heSq56q5nQnrd0YBOWEvj7uXEFkWG3DH6Ms6JVkFNnuUmBA@mail.gmail.com>
In-Reply-To
<98ad3de977090a793408b25ca880b65f058ea44e.1747733203.git.phillip.wood@dunelm.org.uk>
On Tue, 20 May 2025 at 11:27, Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 12 quoted lines
>
> 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.
All makes sense to me.
>         if (argc) {
> -               force_assume = argc > 1 && !strcmp(argv[1], "-p");
This is where we drop the very specific approach of "let's look for -p".
> +               int flags = PARSE_OPT_KEEP_DASHDASH;
This is the flag we've always been using.
> +               if (push_assumed)
> +                       flags |= PARSE_OPT_STOP_AT_NON_OPTION;

Now we use this, too, if we've assumed "push". Makes sense even without the specific context of this patch: we've assumed an implicit "push", so let's be a bit less aggressive in parsing the remainder.

Show 6 quoted lines
>                 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;

Rather than looking for "-p" in a fixed place, we see if option parsing spotted it. Makes perfect sense. Although, why `|=` here? We initialize `force_assume` to 0 at the top and this is the only other time we write to it. Why not just `force_assume = patch_mode`? Future-proofing?

Show 10 quoted lines
> -test_expect_success 'stash -p <pathspec> stash and restores the file' '
> +test_expect_success 'stash --patch <pathspec> stash and restores the file' '
>         cat file >expect-file &&
>         echo changed-file >file &&
>         echo changed-other-file >other-file &&
> -       echo a | git stash -p file &&
> +       echo a | git stash -m "stash bar" --patch file &&
>         test_cmp expect-file file &&
>         echo changed-other-file >expect &&
>         test_cmp expect other-file &&

We lose the test of `-p` that we just added. Ok. We should be able to trust our option parsing machinery to get this right. This s/-p/--patch/ demonstrates that your patch works, and as for running this as a regression test in the future, we'll be using one of the equivalent ways of spelling this option. Ok.

Martin
Previous: Phillip WoodNext: Junio C Hamano
Message 9 of 18 in “stash: allow "git stash -p <pathspec>" to assume push again”
  1. stash: allow "git stash -p <pathspec>" to assume push againPhillip Wood, May 16, 2025
  2. Junio C HamanoMay 16, 2025
  3. Phillip WoodMay 20, 2025
  4. 0/2 stash: fix and improve "git stash -p <pathspec>"Phillip Wood, May 20, 2025
  5. 1/2 stash: allow "git stash -p <pathspec>" to assume push againPhillip Wood, May 20, 2025
  6. Martin ÅgrenJun 6, 2025
  7. Phillip WoodJun 6, 2025
  8. 2/2 stash: allow "git stash [<options>] --patch <pathspec>" to assume pushPhillip Wood, May 20, 2025
  9. Martin ÅgrenJun 6, 2025
  10. Junio C HamanoMay 21, 2025
  11. Junio C HamanoJun 3, 2025
  12. Martin ÅgrenJun 6, 2025
  13. 0/2 stash: fix and improve "git stash -p <pathspec>"Phillip Wood, Jun 7, 2025
  14. 1/2 stash: allow "git stash -p <pathspec>" to assume push againPhillip Wood, Jun 7, 2025
  15. 2/2 stash: allow "git stash [<options>] --patch <pathspec>" to assume pushPhillip Wood, Jun 7, 2025
  16. Martin ÅgrenJun 7, 2025
  17. Phillip WoodJun 9, 2025
  18. Martin ÅgrenJun 10, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.