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

[PATCH 3/3] stash: reject stash name starting with a dash.

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Aug 18, 2009, 21:38 UTC
Message-ID
<1250631523-10524-4-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<1250631523-10524-3-git-send-email-Matthieu.Moy@imag.fr>

This avoids typos like 'git stash save --invalid-option', particularly nasty since one can omit the 'save' subcommand. The syntax 'git stash save -- "-name starting with dash" still allows such stash name.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
Jeff King <peff@peff.net> writes:
Show 19 quoted lines
>> But I may have missed its drawbacks ;-)
>
> The only I can think of is that bogus input will provoke 'save'. So
> something like:
>
>   git stash --apply
>
> will invoke "git stash save --apply", which doesn't even complain. It
> just tries to make a stash with message --apply. Now of course this
> input is obviously bogus, but probably the right thing to do is
> complain.
>
> OTOH, I think it is the fault of "git stash save --apply" for not doing
> the complaining, so your patch really isn't making it worse. Probably it
> should barf on anything unrecognized starting with a '-', and allow '--'
> to separate the message from the rest of the options (in the rare case
> that you want a message starting with '-').
>
> -Peff
So, here it is!
 Documentation/git-stash.txt |    2 +-
 git-stash.sh                |   10 +++++++++-
 t/t3903-stash.sh            |   10 ++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 7e515ce..ded62e0 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -13,7 +13,7 @@ SYNOPSIS
 'git stash' drop [-q|--quiet] [<stash>]
 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
 'git stash' branch <branchname> [<stash>]
-'git stash' [save] [--patch] [--[no-]keep-index|-k] [-q|--quiet] [<message>]
+'git stash' [save] [--patch] [--[no-]keep-index|-k] [-q|--quiet] [--] [<message>]
 'git stash' clear
 'git stash' create
 
diff --git a/git-stash.sh b/git-stash.sh
index bb36bc7..642e265 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -7,7 +7,7 @@ USAGE="list [<options>]
    or: $dashless drop [-q|--quiet] [<stash>]
    or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
    or: $dashless branch <branchname> [<stash>]
-   or: $dashless [save] [-k|--keep-index] [-q|--quiet] [<message>]
+   or: $dashless [save] [-k|--keep-index] [-q|--quiet] [--] [<message>]
    or: $dashless clear"
 
 SUBDIRECTORY_OK=Yes
@@ -145,6 +145,14 @@ save_stash () {
 		-q|--quiet)
 			GIT_QUIET=t
 			;;
+		--)
+			shift
+			break
+			;;
+		-*)
+			echo "error: unknown option for 'stash save': $1"
+			usage
+			;;
 		*)
 			break
 			;;
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 0e831e0..87e5a14 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -208,4 +208,14 @@ test_expect_success 'stash -k' '
        test bar,bar4 = $(cat file),$(cat file2)
 '
 
+test_expect_success 'stash --invalid-option' '
+       echo bar5 > file &&
+       echo bar6 > file2 &&
+       git add file2 &&
+       ! git stash --invalid-option &&
+       test bar5,bar6 = $(cat file),$(cat file2) &&
+       git stash -- -message-starting-with-dash &&
+       test bar,bar2 = $(cat file),$(cat file2)
+'
+
 test_done
-- 
1.6.4.rc2.31.g2d7d7
Previous: Matthieu MoyNext: Jeff King
Message 8 of 22 in “stash: accept options also when subcommand 'save' is omitted”
  1. stash: accept options also when subcommand 'save' is omittedMatthieu Moy, Aug 18, 2009
  2. Matthieu MoyAug 18, 2009
  3. Jeff KingAug 18, 2009
  4. Junio C HamanoAug 18, 2009
  5. 0/3 short syntaxes for 'git stash'Matthieu Moy, Aug 18, 2009
  6. 1/3 stash: accept -k as a shortcut for --keep-indexMatthieu Moy, Aug 18, 2009
  7. 2/3 stash: accept options also when subcommand 'save' is omittedMatthieu Moy, Aug 18, 2009
  8. 3/3 stash: reject stash name starting with a dash.Matthieu Moy, Aug 18, 2009
  9. Jeff KingAug 18, 2009
  10. Junio C HamanoAug 18, 2009
  11. Jeff KingAug 18, 2009
  12. Junio C HamanoAug 18, 2009
  13. Matthieu MoyAug 19, 2009
  14. Jeff KingAug 19, 2009
  15. Jeff KingAug 18, 2009
  16. Johannes SchindelinAug 18, 2009
  17. Matthieu MoyAug 18, 2009
  18. Johannes SchindelinAug 18, 2009
  19. Matthieu MoyAug 19, 2009
  20. Jeff KingAug 18, 2009
  21. Johannes SchindelinAug 18, 2009
  22. Jeff KingAug 18, 2009

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.