Skip to content

Commit 5fd448f

Browse files
salty-horsegitster
authored andcommitted
git stash: Give friendlier errors when there is nothing to apply
The change makes sure a stash (given or default) exists before checking if the working tree is dirty. If the default stash is requested, the old message was scary and included a 'fatal' error from rev-parse: fatal: Needed a single revision : no valid stashed state found It is replaced with a friendlier 'Nothing to apply' error, similar to 'git stash branch'. If a specific stash is specified, the 'Needed a single revision' errors from rev-parse are suppressed. Signed-off-by: Ori Avtalion <[email protected]> Acked-by: Thomas Rast <[email protected]> Acked-by: Nanako Shiraishi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6ffd781 commit 5fd448f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

git-stash.sh

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ show_stash () {
162162
}
163163

164164
apply_stash () {
165-
git update-index -q --refresh &&
166-
git diff-files --quiet --ignore-submodules ||
167-
die 'Cannot apply to a dirty working tree, please stage your changes'
168-
169165
unstash_index=
170166

171167
while test $# != 0
@@ -184,18 +180,27 @@ apply_stash () {
184180
shift
185181
done
186182

187-
# current index state
188-
c_tree=$(git write-tree) ||
189-
die 'Cannot apply a stash in the middle of a merge'
183+
if test $# = 0
184+
then
185+
have_stash || die 'Nothing to apply'
186+
fi
190187

191188
# stash records the work tree, and is a merge between the
192189
# base commit (first parent) and the index tree (second parent).
193-
s=$(git rev-parse --verify --default $ref_stash "$@") &&
194-
w_tree=$(git rev-parse --verify "$s:") &&
195-
b_tree=$(git rev-parse --verify "$s^1:") &&
196-
i_tree=$(git rev-parse --verify "$s^2:") ||
190+
s=$(git rev-parse --quiet --verify --default $ref_stash "$@") &&
191+
w_tree=$(git rev-parse --quiet --verify "$s:") &&
192+
b_tree=$(git rev-parse --quiet --verify "$s^1:") &&
193+
i_tree=$(git rev-parse --quiet --verify "$s^2:") ||
197194
die "$*: no valid stashed state found"
198195

196+
git update-index -q --refresh &&
197+
git diff-files --quiet --ignore-submodules ||
198+
die 'Cannot apply to a dirty working tree, please stage your changes'
199+
200+
# current index state
201+
c_tree=$(git write-tree) ||
202+
die 'Cannot apply a stash in the middle of a merge'
203+
199204
unstashed_index_tree=
200205
if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
201206
test "$c_tree" != "$i_tree"

0 commit comments

Comments
 (0)