Merge branch 'aw/numbered-stash'

The user always has to say "stash@{$N}" when naming a single
element in the default location of the stash, i.e. reflogs in
refs/stash.  The "git stash" command learned to accept "git stash
apply 4" as a short-hand for "git stash apply stash@{4}".

* aw/numbered-stash:
  stash: allow stashes to be referenced by index only
This commit is contained in:
Junio C Hamano 2016-10-31 13:15:22 -07:00
commit 9fa1f902bf
3 changed files with 50 additions and 3 deletions

View File

@ -39,7 +39,8 @@ 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 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 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}` created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
is also possible). is also possible). Stashes may also be referenced by specifying just the
stash index (e.g. the integer `n` is equivalent to `stash@{n}`).
OPTIONS OPTIONS
------- -------

View File

@ -384,9 +384,8 @@ parse_flags_and_rev()
i_tree= i_tree=
u_tree= u_tree=
REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
FLAGS= FLAGS=
REV=
for opt for opt
do do
case "$opt" in case "$opt" in
@ -404,6 +403,9 @@ parse_flags_and_rev()
die "$(eval_gettext "unknown option: \$opt")" die "$(eval_gettext "unknown option: \$opt")"
FLAGS="${FLAGS}${FLAGS:+ }$opt" FLAGS="${FLAGS}${FLAGS:+ }$opt"
;; ;;
*)
REV="${REV}${REV:+ }'$opt'"
;;
esac esac
done done
@ -422,6 +424,15 @@ parse_flags_and_rev()
;; ;;
esac esac
case "$1" in
*[!0-9]*)
:
;;
*)
set -- "${ref_stash}@{$1}"
;;
esac
REV=$(git rev-parse --symbolic --verify --quiet "$1") || { REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
reference="$1" reference="$1"
die "$(eval_gettext "\$reference is not a valid reference")" die "$(eval_gettext "\$reference is not a valid reference")"

View File

@ -131,6 +131,26 @@ test_expect_success 'drop middle stash' '
test 1 = $(git show HEAD:file) test 1 = $(git show HEAD:file)
' '
test_expect_success 'drop middle stash by index' '
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 'stash pop' ' test_expect_success 'stash pop' '
git reset --hard && git reset --hard &&
git stash pop && git stash pop &&
@ -604,6 +624,21 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
git stash drop git stash drop
' '
test_expect_success 'invalid ref of the form "n", 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_expect_success 'stash branch should not drop the stash if the branch exists' ' test_expect_success 'stash branch should not drop the stash if the branch exists' '
git stash clear && git stash clear &&
echo foo >file && echo foo >file &&