Code

wrap git's main usage string.
[git.git] / git-stash.sh
index 567aa5d725e38750b860b01a43ba01914e180ea1..4febbbfa5d13b241e9bfcc479c6767a90bd12919 100755 (executable)
@@ -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 [--keep-index] [-q|--quiet] [<message>]]
+   or: $dashless [save [-k|--keep-index] [-q|--quiet] [<message>]]
    or: $dashless clear"
 
 SUBDIRECTORY_OK=Yes
@@ -132,7 +132,7 @@ save_stash () {
        while test $# != 0
        do
                case "$1" in
-               --keep-index)
+               -k|--keep-index)
                        keep_index=t
                        ;;
                --no-keep-index)
@@ -145,6 +145,14 @@ save_stash () {
                -q|--quiet)
                        GIT_QUIET=t
                        ;;
+               --)
+                       shift
+                       break
+                       ;;
+               -*)
+                       echo "error: unknown option for 'stash save': $1"
+                       usage
+                       ;;
                *)
                        break
                        ;;
@@ -214,10 +222,6 @@ show_stash () {
 }
 
 apply_stash () {
-       git update-index -q --refresh &&
-       git diff-files --quiet --ignore-submodules ||
-               die 'Cannot apply to a dirty working tree, please stage your changes'
-
        unstash_index=
 
        while test $# != 0
@@ -236,18 +240,27 @@ apply_stash () {
                shift
        done
 
-       # current index state
-       c_tree=$(git write-tree) ||
-               die 'Cannot apply a stash in the middle of a merge'
+       if test $# = 0
+       then
+               have_stash || die 'Nothing to apply'
+       fi
 
        # stash records the work tree, and is a merge between the
        # base commit (first parent) and the index tree (second parent).
-       s=$(git rev-parse --verify --default $ref_stash "$@") &&
-       w_tree=$(git rev-parse --verify "$s:") &&
-       b_tree=$(git rev-parse --verify "$s^1:") &&
-       i_tree=$(git rev-parse --verify "$s^2:") ||
+       s=$(git rev-parse --quiet --verify --default $ref_stash "$@") &&
+       w_tree=$(git rev-parse --quiet --verify "$s:") &&
+       b_tree=$(git rev-parse --quiet --verify "$s^1:") &&
+       i_tree=$(git rev-parse --quiet --verify "$s^2:") ||
                die "$*: no valid stashed state found"
 
+       git update-index -q --refresh &&
+       git diff-files --quiet --ignore-submodules ||
+               die 'Cannot apply to a dirty working tree, please stage your changes'
+
+       # current index state
+       c_tree=$(git write-tree) ||
+               die 'Cannot apply a stash in the middle of a merge'
+
        unstashed_index_tree=
        if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
                        test "$c_tree" != "$i_tree"
@@ -354,6 +367,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)
@@ -405,12 +430,13 @@ branch)
        apply_to_branch "$@"
        ;;
 *)
-       if test $# -eq 0
-       then
+       case $# in
+       0)
                save_stash &&
                say '(To restore them type "git stash apply")'
-       else
+               ;;
+       *)
                usage
-       fi
+       esac
        ;;
 esac