Skip to contentSkip to conversation

git stash: Give friendlier error when there is nothing to apply

PATCH In review Ori Avtalion 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git stash: Give friendlier error when there is nothing to apply

The old message was scary and included a 'fatal' error from rev-parse:
fatal: Needed a single revision
: no valid stashed state found

The new message is identical to the one given by 'git stash branch', and given *before* the check for a dirty working tree.

Previously, the command prompted the user to clean a dirty working tree when there is nothing to apply.

---
 git-stash.sh |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
git-stash.sh +2 -0
index 03e589f..aa84144 100755
@@ -162,6 +162,8 @@ show_stash () {
 }
 
 apply_stash () {
+	have_stash || die 'Nothing to apply'
+
 	git update-index -q --refresh &&
 	git diff-files --quiet --ignore-submodules ||
 		die 'Cannot apply to a dirty working tree, please stage your changes'
Signature · 1 line
1.6.4.73.gc144.dirty
#1 lore raw

git-stash.sh show_stash () { · line 169

Quoted 7 lines · Ori Avtalion

apply_stash () {

git-stash.sh +2 -0
+	have_stash || die 'Nothing to apply'
+
 	git update-index -q --refresh &&
 	git diff-files --quiet --ignore-submodules ||
 		die 'Cannot apply to a dirty working tree, please stage your changes'

This needs a guard against the case where the user says

git stash apply $some_sha1

but his refs/stash is empty. This could be the case, e.g., after mistakenly blowing away the reflog with 'git stash clear' and then going on a recovery hunt through the unreferenced commits.

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#2 lore raw

PATCH 1/1 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.

---

Thomas, I added handling for the 'git stash apply <stash>' case based on your reminder, and also changed the error messages related to it.

All of the stash tests pass, as before.

 git-stash.sh |   27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)
git-stash.sh +16 -11
index 03e589f..d61c9d0 100755
@@ -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
@@ -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"
Signature · 1 line
1.6.4.115.g82cf7
Acked-by on v1 1/1
#3 lore raw

Re: [PATCH] git stash: Give friendlier errors when there is nothing to apply

Quoted 14 lines · Ori Avtalion

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.

Quoted 7 lines

---

Thomas, I added handling for the 'git stash apply <stash>' case based on your reminder, and also changed the error messages related to it.

All of the stash tests pass, as before.

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#4 lore raw

Re: [PATCH] git stash: Give friendlier errors when there is nothing to apply

In reply to Ori Avtalion
Quoted 8 lines · Ori Avtalion

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

Quoted 8 lines

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.

I do not see anything that might break existing usage of the command. Comments?

A tangent; we might want an analogue to "shortlog -s -n" but based on "blame".

#5 lore raw

Re: [PATCH] git stash: Give friendlier errors when there is nothing to apply

Quoting Junio C Hamano <[email protected]>

Quoted 13 lines
Quoted 11 lines · Ori Avtalion

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'.

Quoted 8 lines
Quoted 5 lines

If a specific stash is specified, the 'Needed a single revision' errors from rev-parse are suppressed.

I do not see anything that might break existing usage of the command. Comments?

The patch looks good to me. I think it was my fault but you can avoid adding two extra --quiet by inspecting i_tree (in other words, "$s^2:") before w_tree and b_tree. Because a commit that is a stash exactly has two parents, it may be good to also make sure 'git rev-parse "$s^3"' fails.

	s=$(git rev-parse --verify --default $ref_stash "$@") &&
	! git rev-parse --quiet --verify "$s^3" >/dev/null &&
	i_tree=$(git rev-parse --quiet --verify "$s^2:") &&
	b_tree=$(git rev-parse --verify "$s^1:") &&
	w_tree=$(git rev-parse --verify "$s:") ||
		die "$*: no valid stashed state found"
Quoted 2 lines

A tangent; we might want an analogue to "shortlog -s -n" but based on "blame".

I'm sorry, I don't understand what you mean.

Signature · 2 lines
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/
#6 lore raw

Re: [PATCH] git stash: Give friendlier errors when there is nothing to apply

Quoted 6 lines · Nanako Shiraishi

Quoting Junio C Hamano <[email protected]>
...

Quoted 2 lines

A tangent; we might want an analogue to "shortlog -s -n" but based on "blame".

I'm sorry, I don't understand what you mean.

I often use shortlog to find whom to CC to, but sometimes blame output gives us a much better picture.

$ git shortlog -s -n git-stash.sh | head -n 6
    22	Junio C Hamano
     4	Brandon Casey
     2	Jeff King
     2	Johannes Schindelin
     2	Nanako Shiraishi
     2	SZEDER Gábor
$ git blame -w git-stash.sh |
  sed -e 's/^[0-9a-f]* (\(.*\) *200[7-9]-..-.. .*/\1/' |
  sort | uniq -c | sort -n -r | head -n 6
    110 Nanako Shiraishi    
     86 Junio C Hamano      
     55 Stephen Boyd        
     33 Brandon Casey       
     20 Abhijit Menon-Sen   
     16 Johannes Schindelin