Skip to content

Commit 3c2eb80

Browse files
moygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 14c674e commit 3c2eb80

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

Documentation/git-stash.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ SYNOPSIS
1414
'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
1515
'git stash' branch <branchname> [<stash>]
1616
'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]
17-
'git stash' [-p|--patch|-k|--keep-index]
1817
'git stash' clear
1918
'git stash' create
2019

@@ -46,9 +45,11 @@ OPTIONS
4645
save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]::
4746

4847
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.
5253
+
5354
If the `--keep-index` option is used, all changes already added to the
5455
index are left intact.

git-stash.sh

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ USAGE="list [<options>]
88
or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
99
or: $dashless branch <branchname> [<stash>]
1010
or: $dashless [save [-k|--keep-index] [-q|--quiet] [<message>]]
11-
or: $dashless [-k|--keep-index]
1211
or: $dashless clear"
1312

1413
SUBDIRECTORY_OK=Yes
@@ -146,6 +145,14 @@ save_stash () {
146145
-q|--quiet)
147146
GIT_QUIET=t
148147
;;
148+
--)
149+
shift
150+
break
151+
;;
152+
-*)
153+
echo "error: unknown option for 'stash save': $1"
154+
usage
155+
;;
149156
*)
150157
break
151158
;;
@@ -355,6 +362,18 @@ apply_to_branch () {
355362
drop_stash $stash
356363
}
357364

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+
358377
# Main command set
359378
case "$1" in
360379
list)
@@ -406,9 +425,9 @@ branch)
406425
apply_to_branch "$@"
407426
;;
408427
*)
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 &&
412431
say '(To restore them type "git stash apply")'
413432
;;
414433
*)

t/t3903-stash.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,15 @@ test_expect_success 'stash -k' '
208208
test bar,bar4 = $(cat file),$(cat file2)
209209
'
210210

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+
211222
test_done

0 commit comments

Comments
 (0)