Skip to content

Commit

Permalink
git stash: Give friendlier errors when there is nothing to apply
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Ori Avtalion authored and Junio C Hamano committed Aug 15, 2009
1 parent 6ffd781 commit 5fd448f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions git-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ show_stash () {
}

apply_stash () {
git update-index -q --refresh &&
git diff-files --quiet --ignore-submodules ||
die 'Cannot apply to a dirty working tree, please stage your changes'

unstash_index=

while test $# != 0
Expand All @@ -184,18 +180,27 @@ apply_stash () {
shift
done

# current index state
c_tree=$(git write-tree) ||
die 'Cannot apply a stash in the middle of a merge'
if test $# = 0
then
have_stash || die 'Nothing to apply'
fi

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

git update-index -q --refresh &&
git diff-files --quiet --ignore-submodules ||
die 'Cannot apply to a dirty working tree, please stage your changes'

# current index state
c_tree=$(git write-tree) ||
die 'Cannot apply a stash in the middle of a merge'

unstashed_index_tree=
if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
test "$c_tree" != "$i_tree"
Expand Down

0 comments on commit 5fd448f

Please sign in to comment.