git stash create currently supports a positional argument for adding a
message.  This is not quite in line with how git commands usually take
comments (using a -m flag).

Add a new syntax for adding a message to git stash create using a -m
flag.  This is with the goal of deprecating the old style git stash
create with positional arguments.

This also adds a -u argument, for untracked files.  This is already used
internally as another positional argument, but can now be used from the
command line.

Signed-off-by: Thomas Gummerer <t.gumme...@gmail.com>
---
 Documentation/git-stash.txt |  1 +
 git-stash.sh                | 50 +++++++++++++++++++++++++++++++++++++++++----
 t/t3903-stash.sh            | 18 ++++++++++++++++
 3 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 0fc23c25ee..0bce33e3fc 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -17,6 +17,7 @@ SYNOPSIS
             [-u|--include-untracked] [-a|--all] [<message>]]
 'git stash' clear
 'git stash' create [<message>]
+'git stash' create [-m <message>] [-u|--include-untracked <untracked|all>]
 'git stash' store [-m|--message <message>] [-q|--quiet] <commit>
 
 DESCRIPTION
diff --git a/git-stash.sh b/git-stash.sh
index 8528708f61..5f08b43967 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -56,8 +56,50 @@ clear_stash () {
 }
 
 create_stash () {
-       stash_msg="$1"
-       untracked="$2"
+       stash_msg=
+       untracked=
+       new_style=
+       while test $# != 0
+       do
+               case "$1" in
+               -m|--message)
+                       shift
+                       stash_msg="$1"
+                       new_style=t
+                       ;;
+               -u|--include-untracked)
+                       shift
+                       untracked="$1"
+                       new_style=t
+                       ;;
+               *)
+                       if test -n "$new_style"
+                       then
+                               echo "invalid argument"
+                               option="$1"
+                               # TRANSLATORS: $option is an invalid option, 
like
+                               # `--blah-blah'. The 7 spaces at the beginning 
of the
+                               # second line correspond to "error: ". So you 
should line
+                               # up the second line with however many 
characters the
+                               # translation of "error: " takes in your 
language. E.g. in
+                               # English this is:
+                               #
+                               #    $ git stash save --blah-blah 2>&1 | head 
-n 2
+                               #    error: unknown option for 'stash save': 
--blah-blah
+                               #           To provide a message, use git stash 
save -- '--blah-blah'
+                               eval_gettextln "error: unknown option for 
'stash create': \$option"
+                               usage
+                       fi
+                       break
+                       ;;
+               esac
+               shift
+       done
+
+       if test -z "$new_style"
+       then
+               stash_msg="$*"
+       fi
 
        git update-index -q --refresh
        if no_changes
@@ -265,7 +307,7 @@ push_stash () {
        git reflog exists $ref_stash ||
                clear_stash || die "$(gettext "Cannot initialize stash")"
 
-       create_stash "$stash_msg" $untracked
+       create_stash -m "$stash_msg" -u "$untracked"
        store_stash -m "$stash_msg" -q $w_commit ||
        die "$(gettext "Cannot save the current status")"
        say "$(eval_gettext "Saved working directory and index state 
\$stash_msg")"
@@ -697,7 +739,7 @@ clear)
        ;;
 create)
        shift
-       create_stash "$*" && echo "$w_commit"
+       create_stash "$@" && echo "$w_commit"
        ;;
 store)
        shift
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 0171b824c9..34e9610bb6 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -784,4 +784,22 @@ test_expect_success 'push -m shows right message' '
        test_cmp expect actual
 '
 
+test_expect_success 'deprecated version of stash create stores correct 
message' '
+       >foo &&
+       git add foo &&
+       STASH_ID=$(git stash create "create test message") &&
+       echo "On master: create test message" >expect &&
+       git show --pretty=%s ${STASH_ID} | head -n1 >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'new style stash create stores correct message' '
+       >foo &&
+       git add foo &&
+       STASH_ID=$(git stash create -m "create test message new style") &&
+       echo "On master: create test message new style" >expect &&
+       git show --pretty=%s ${STASH_ID} | head -n1 >actual &&
+       test_cmp expect actual
+'
+
 test_done
-- 
2.11.0.297.g9a2118ac0b.dirty

Reply via email to