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 <ori@avtalion.name> Acked-by: Thomas Rast <trast@student.ethz.ch> Acked-by: Nanako Shiraishi <nanako3@lavabit.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ori Avtalion committed
Aug 11, 2009 at 14:12 UTC
5fd448f1142c61da31b28c74d7d340938ab0e01d
1 file changed
+16
-11
git-stash.sh
+16
-11
@@ -162,10 +162,6 @@ show_stash () {
162
}
163
164
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
-
165
unstash_index=
166
167
while test $# != 0
@@ -184,18 +180,27 @@ apply_stash () {
180
shift
181
done
182
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
187
188
# stash records the work tree, and is a merge between the
189
# 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:") ||
194
die "$*: no valid stashed state found"
195
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
+
204
unstashed_index_tree=
205
if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
206
test "$c_tree" != "$i_tree"