From 3c2eb80fe3f3c7efbb25e929df9f70d7c896a5ef Mon Sep 17 00:00:00 2001 From: Matthieu Moy Date: Tue, 18 Aug 2009 23:38:40 +0200 Subject: 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 Signed-off-by: Junio C Hamano --- git-stash.sh | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'git-stash.sh') diff --git a/git-stash.sh b/git-stash.sh index 9fd72894c4..f24337613b 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -8,7 +8,6 @@ USAGE="list [] or: $dashless ( pop | apply ) [--index] [-q|--quiet] [] or: $dashless branch [] or: $dashless [save [-k|--keep-index] [-q|--quiet] []] - or: $dashless [-k|--keep-index] or: $dashless clear" SUBDIRECTORY_OK=Yes @@ -146,6 +145,14 @@ save_stash () { -q|--quiet) GIT_QUIET=t ;; + --) + shift + break + ;; + -*) + echo "error: unknown option for 'stash save': $1" + usage + ;; *) break ;; @@ -355,6 +362,18 @@ apply_to_branch () { drop_stash $stash } +# The default command is "save" if nothing but options are given +seen_non_option= +for opt +do + case "$opt" in + -*) ;; + *) seen_non_option=t; break ;; + esac +done + +test -n "$seen_non_option" || set "save" "$@" + # Main command set case "$1" in list) @@ -406,9 +425,9 @@ branch) apply_to_branch "$@" ;; *) - case $#,"$1","$2" in - 0,,|1,-k,|1,--keep-index,|1,-p,|1,--patch,|2,-p,--no-keep-index|2,--patch,--no-keep-index) - save_stash "$@" && + case $# in + 0) + save_stash && say '(To restore them type "git stash apply")' ;; *) -- cgit v1.2.3