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

[PATCH v3 1/2] stash: allow "git stash -p <pathspec>" to assume push again

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Jun 7, 2025, 09:45 UTC
Message-ID
<c147eaf2eaec6ed4e46f3f34bc864cbf8ecb8e45.1749289514.git.phillip.wood@dunelm.org.uk>
In-Reply-To
<cover.1749289514.git.phillip.wood@dunelm.org.uk>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

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>
---
 builtin/stash.c  |  2 +-
 t/t3903-stash.sh | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/builtin/stash.c b/builtin/stash.c
index cfbd92852a6..bc2c34fa048 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1789,7 +1789,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 	int ret;
 
 	if (argc) {
-		force_assume = !strcmp(argv[0], "-p");
+		force_assume = argc > 1 && !strcmp(argv[1], "-p");
 		argc = parse_options(argc, argv, prefix, options,
 				     push_assumed ? git_stash_usage :
 				     git_stash_push_usage,
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 74666ff3e4b..a99a746221e 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1177,6 +1177,28 @@ 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_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_cmp expect-file file &&
+	echo changed-other-file >expect &&
+	test_cmp expect other-file &&
+	git checkout HEAD -- file &&
+	git stash pop &&
+	test_cmp expect other-file &&
+	test_write_lines a b c >expect &&
+	test_cmp expect file
+'
+
+test_expect_success 'stash <pathspec> -p is rejected' '
+	test_must_fail git stash file -p 2>err &&
+	test_grep "subcommand wasn${SQ}t specified; ${SQ}push${SQ} can${SQ}t be assumed due to unexpected token ${SQ}file${SQ}" err
+'
+
 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
 	mkdir sub &&
 	>foo &&
-- 
2.49.0.897.gfad3eb7d210
Previous: Phillip WoodNext: Phillip Wood
Message 14 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.