Skip to content

Commit 8d9325f

Browse files
committed
Merge branch 'aw/numbered-stash' into HEAD
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
2 parents e56a8eb + a56c8f5 commit 8d9325f

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

Documentation/git-stash.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ The latest stash you created is stored in `refs/stash`; older
3939
stashes are found in the reflog of this reference and can be named using
4040
the usual reflog syntax (e.g. `stash@{0}` is the most recently
4141
created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
42-
is also possible).
42+
is also possible). Stashes may also be referenced by specifying just the
43+
stash index (e.g. the integer `n` is equivalent to `stash@{n}`).
4344

4445
OPTIONS
4546
-------

git-stash.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,8 @@ parse_flags_and_rev()
384384
i_tree=
385385
u_tree=
386386

387-
REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
388-
389387
FLAGS=
388+
REV=
390389
for opt
391390
do
392391
case "$opt" in
@@ -404,6 +403,9 @@ parse_flags_and_rev()
404403
die "$(eval_gettext "unknown option: \$opt")"
405404
FLAGS="${FLAGS}${FLAGS:+ }$opt"
406405
;;
406+
*)
407+
REV="${REV}${REV:+ }'$opt'"
408+
;;
407409
esac
408410
done
409411

@@ -422,6 +424,15 @@ parse_flags_and_rev()
422424
;;
423425
esac
424426

427+
case "$1" in
428+
*[!0-9]*)
429+
:
430+
;;
431+
*)
432+
set -- "${ref_stash}@{$1}"
433+
;;
434+
esac
435+
425436
REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
426437
reference="$1"
427438
die "$(eval_gettext "\$reference is not a valid reference")"

t/t3903-stash.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ test_expect_success 'drop middle stash' '
131131
test 1 = $(git show HEAD:file)
132132
'
133133

134+
test_expect_success 'drop middle stash by index' '
135+
git reset --hard &&
136+
echo 8 >file &&
137+
git stash &&
138+
echo 9 >file &&
139+
git stash &&
140+
git stash drop 1 &&
141+
test 2 = $(git stash list | wc -l) &&
142+
git stash apply &&
143+
test 9 = $(cat file) &&
144+
test 1 = $(git show :file) &&
145+
test 1 = $(git show HEAD:file) &&
146+
git reset --hard &&
147+
git stash drop &&
148+
git stash apply &&
149+
test 3 = $(cat file) &&
150+
test 1 = $(git show :file) &&
151+
test 1 = $(git show HEAD:file)
152+
'
153+
134154
test_expect_success 'stash pop' '
135155
git reset --hard &&
136156
git stash pop &&
@@ -604,6 +624,21 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
604624
git stash drop
605625
'
606626

627+
test_expect_success 'invalid ref of the form "n", n >= N' '
628+
git stash clear &&
629+
test_must_fail git stash drop 0 &&
630+
echo bar5 >file &&
631+
echo bar6 >file2 &&
632+
git add file2 &&
633+
git stash &&
634+
test_must_fail git stash drop 1 &&
635+
test_must_fail git stash pop 1 &&
636+
test_must_fail git stash apply 1 &&
637+
test_must_fail git stash show 1 &&
638+
test_must_fail git stash branch tmp 1 &&
639+
git stash drop
640+
'
641+
607642
test_expect_success 'stash branch should not drop the stash if the branch exists' '
608643
git stash clear &&
609644
echo foo >file &&

0 commit comments

Comments
 (0)