Allows stashes to be referenced by index only. Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n". Signed-off-by: Aaron M Watson <watsona4@xxxxxxxxx> --- Documentation/git-stash.txt | 11 ++++--- git-stash.sh | 10 +++++- t/t3907-stash-index.sh | 77 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100755 t/t3907-stash-index.sh diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt index 92df596..af11cff 100644 --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@ -35,11 +35,12 @@ A stash is by default listed as "WIP on 'branchname' ...", but you can give a more descriptive message on the command line when you create one. -The latest stash you created is stored in `refs/stash`; older -stashes are found in the reflog of this reference and can be named using -the usual reflog syntax (e.g. `stash@{0}` is the most recently -created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}` -is also possible). +The latest stash you created is stored in `refs/stash`; older stashes +are found in the reflog of this reference and can be named using the +usual reflog syntax (e.g. `stash@{0}` is the most recently created +stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}` is also +possible). Stashes may also be references by specifying just the stash +index (e.g. the integer `n` is equivalent to `stash@{n}`). OPTIONS ------- diff --git a/git-stash.sh b/git-stash.sh index 826af18..a0c7f12 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Copyright (c) 2007, Nanako Shiraishi dashless=$(basename "$0" | sed -e 's/-/ /') @@ -371,6 +371,14 @@ parse_flags_and_rev() test "$PARSE_CACHE" = "$*" && return 0 # optimisation PARSE_CACHE="$*" + args=() + for arg + do + [[ $arg =~ ^[0-9]+$ ]] && arg="stash@{$arg}" + args+=("$arg") + done + set -- "${args[@]}" + IS_STASH_LIKE= IS_STASH_REF= INDEX_OPTION= diff --git a/t/t3907-stash-index.sh b/t/t3907-stash-index.sh new file mode 100755 index 0000000..72a1838 --- /dev/null +++ b/t/t3907-stash-index.sh @@ -0,0 +1,77 @@ +#!/bin/sh +# +# Copyright (c) 2016 Aaron M. Watson +# + +test_description='Test git stash with index ref specification' + +. ./test-lib.sh + +test_expect_success 'stash some dirty working directory' ' + echo 1 > file && + git add file && + echo unrelated >other-file && + git add other-file && + test_tick && + git commit -m initial && + echo 2 > file && + git add file && + echo 3 > file && + test_tick && + git stash && + git diff-files --quiet && + git diff-index --cached --quiet HEAD +' + +cat > expect << EOF +diff --git a/file b/file +index 0cfbf08..00750ed 100644 +--- a/file ++++ b/file +@@ -1 +1 @@ +-2 ++3 +EOF + +test_expect_success 'applying bogus stash does nothing' ' + test_must_fail git stash apply 1 && + echo 1 >expect && + test_cmp expect file +' + +test_expect_success 'drop middle stash' ' + git reset --hard && + echo 8 > file && + git stash && + echo 9 > file && + git stash && + git stash drop 1 && + test 2 = $(git stash list | wc -l) && + git stash apply && + test 9 = $(cat file) && + test 1 = $(git show :file) && + test 1 = $(git show HEAD:file) && + git reset --hard && + git stash drop && + git stash apply && + test 3 = $(cat file) && + test 1 = $(git show :file) && + test 1 = $(git show HEAD:file) +' + +test_expect_success 'invalid ref of the form "n", where n >= N' ' + git stash clear && + test_must_fail git stash drop 0 && + echo bar5 > file && + echo bar6 > file2 && + git add file2 && + git stash && + test_must_fail git stash drop 1 && + test_must_fail git stash pop 1 && + test_must_fail git stash apply 1 && + test_must_fail git stash show 1 && + test_must_fail git stash branch tmp 1 && + git stash drop +' + +test_done -- 2.7.4