@matheusbsbs / matheus-bagliano / commits / 3c2eb80fe3
Fork 0

stash: simplify defaulting to "save" and reject unknown options

With the earlier DWIM patches, certain combination of options defaulted to the "save" command correctly while certain equally valid combination did not. For example, "git stash -k" were Ok but "git stash -q -k" did not work. This makes the logic of defaulting to "save" much simpler. If there are no non-flag arguments, it is clear that there is no command word, and we default to "save" subcommand. This rule prevents "git stash -q apply" from quietly creating a stash with "apply" as the message. This also teaches "git stash save" to reject an unknown option. This is to keep a mistyped "git stash save --quite" from creating a stash with a message "--quite", and this safety is more important with the new logic to default to "save" with any option-looking argument without an explicit comand word. [jc: this is based on Matthieu's 3-patch series, and a follow-up discussion, and he and Peff take all the credit; if I have introduced bugs while reworking, they are mine.] Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Aug 18, 2009 at 23:38 UTC 3c2eb80fe3f3c7efbb25e929df9f70d7c896a5ef
3 files changed +39 -8
Documentation/git-stash.txt
+5 -4
@@ -14,7 +14,6 @@ SYNOPSIS
14 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
15 'git stash' branch <branchname> [<stash>]
16 'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]
17 -'git stash' [-p|--patch|-k|--keep-index]
17 'git stash' clear
18 'git stash' create
19
@@ -46,9 +45,11 @@ OPTIONS
45 save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]::
46
47 Save your local modifications to a new 'stash', and run `git reset
49 - --hard` to revert them. This is the default action when no
50 - subcommand is given. The <message> part is optional and gives
51 - the description along with the stashed state.
48 + --hard` to revert them. The <message> part is optional and gives
49 + the description along with the stashed state. For quickly making
50 + a snapshot, you can omit _both_ "save" and <message>, but giving
51 + only <message> does not trigger this action to prevent a misspelled
52 + subcommand from making an unwanted stash.
53 +
54 If the `--keep-index` option is used, all changes already added to the
55 index are left intact.
git-stash.sh
+23 -4
@@ -8,7 +8,6 @@ USAGE="list [<options>]
8 or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
9 or: $dashless branch <branchname> [<stash>]
10 or: $dashless [save [-k|--keep-index] [-q|--quiet] [<message>]]
11 - or: $dashless [-k|--keep-index]
11 or: $dashless clear"
12
13 SUBDIRECTORY_OK=Yes
@@ -146,6 +145,14 @@ save_stash () {
145 -q|--quiet)
146 GIT_QUIET=t
147 ;;
148 + --)
149 + shift
150 + break
151 + ;;
152 + -*)
153 + echo "error: unknown option for 'stash save': $1"
154 + usage
155 + ;;
156 *)
157 break
158 ;;
@@ -355,6 +362,18 @@ apply_to_branch () {
362 drop_stash $stash
363 }
364
365 +# The default command is "save" if nothing but options are given
366 +seen_non_option=
367 +for opt
368 +do
369 + case "$opt" in
370 + -*) ;;
371 + *) seen_non_option=t; break ;;
372 + esac
373 +done
374 +
375 +test -n "$seen_non_option" || set "save" "$@"
376 +
377 # Main command set
378 case "$1" in
379 list)
@@ -406,9 +425,9 @@ branch)
425 apply_to_branch "$@"
426 ;;
427 *)
409 - case $#,"$1","$2" in
410 - 0,,|1,-k,|1,--keep-index,|1,-p,|1,--patch,|2,-p,--no-keep-index|2,--patch,--no-keep-index)
411 - save_stash "$@" &&
428 + case $# in
429 + 0)
430 + save_stash &&
431 say '(To restore them type "git stash apply")'
432 ;;
433 *)
t/t3903-stash.sh
+11
@@ -208,4 +208,15 @@ test_expect_success 'stash -k' '
208 test bar,bar4 = $(cat file),$(cat file2)
209 '
210
211 +test_expect_success 'stash --invalid-option' '
212 + echo bar5 > file &&
213 + echo bar6 > file2 &&
214 + git add file2 &&
215 + test_must_fail git stash --invalid-option &&
216 + test_must_fail git stash save --invalid-option &&
217 + test bar5,bar6 = $(cat file),$(cat file2) &&
218 + git stash -- -message-starting-with-dash &&
219 + test bar,bar2 = $(cat file),$(cat file2)
220 +'
221 +
222 test_done