Skip to contentSkip to conversation

git-stash: add new 'drop' subcommand

PATCH In review Jeff King 6 participants 9 messages Jun 15, 2016 1 message not in the archive Documentation

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 git-stash: add new 'drop' subcommand

This allows a single stash entry to be deleted. It takes an optional argument which is a stash reflog entry. If no arguments are supplied, stash@{0} is used.

---

Thus far I haven't been a big user of git stash, but I plan to use it more and I expect to use 'drop' more often than 'clear'. I expect in the common case there will be a single stash, and 'drop' will be sufficient. For the case where there are many stashes and I want to remove one, 'drop' is required. 'git stash clear' will become a command that I give special attention to just like 'rm -f *'.

I'm not sure if there is a proper way to get 'stash@{0}' from 'refs/stash' so I kept my usage of that former string outside of the drop_stash() function.

Comments welcome, especially if there is a more appropriate way to do this.

-brandon

 Documentation/git-stash.txt |    7 ++++++-
 git-stash.sh                |   29 ++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)
Documentation/git-stash.txt +6 -1
index c0147b9..b89eadb 100644
@@ -8,7 +8,7 @@ git-stash - Stash the changes in a dirty working directory away
 SYNOPSIS
 --------
 [verse]
-'git-stash' (list | show [<stash>] | apply [<stash>] | clear)
+'git-stash' (list | show [<stash>] | apply [<stash>] | clear | drop [<stash>])
 'git-stash' [save] [message...]
 
 DESCRIPTION
@@ -81,6 +81,11 @@ clear::
 	Remove all the stashed states. Note that those states will then
 	be subject to pruning, and may be difficult or impossible to recover.
 
+drop [<stash>]::
+
+	Remove a single stashed state from the stash list. When no `<stash>`
+	is given, it removes the latest one. i.e. `stash@\{0}`
+
 
 DISCUSSION
 ----------
git-stash.sh +28 -1
index 06cb177..a789a53 100755
@@ -1,7 +1,7 @@
 #!/bin/sh
 # Copyright (c) 2007, Nanako Shiraishi
 
-USAGE='[  | save | list | show | apply | clear | create ]'
+USAGE='[  | save | list | show | apply | clear | create | drop ]'
 
 SUBDIRECTORY_OK=Yes
 OPTIONS_SPEC=
@@ -192,6 +192,24 @@ apply_stash () {
 	fi
 }
 
+drop_stash () {
+	if ! have_stash
+	then
+		echo >&2 'No stash entries to drop'
+		exit 0
+	fi
+
+	# Verify supplied argument looks like a stash entry
+	s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
+	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
+	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
+	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
+		die "$*: not a valid stashed state"
+
+	git reflog delete "$@" && echo "Dropped $@ ($s)" ||
+		die "$*: Could not drop stash entry"
+}
+
 # Main command set
 case "$1" in
 list)
@@ -225,6 +243,15 @@ create)
 	fi
 	create_stash "$*" && echo "$w_commit"
 	;;
+drop)
+	shift
+	if test $# = 0
+	then
+		set -- "stash@{0}"
+	fi
+	drop_stash "$@" &&
+	(git rev-parse --verify "stash@{0}" > /dev/null 2>&1 || clear_stash)
+	;;
 *)
 	if test $# -eq 0
 	then
Signature · 1 line
1.5.4.rc2.1119.g6fdf-dirty
author
#2 lore raw
Quoted 3 lines · Brandon Casey

Thus far I haven't been a big user of git stash, but I plan to use it more and I expect to use 'drop' more often than 'clear'. I expect in the common case there will be a single

There was some discussion of a sensible name, but I don't recall seeing a resolution on this: why not "clear stash@{0}" to clear one, and "clear" to clear all? Otherwise, I foresee "git stash clear stash@{0}" followed by "oops, I just deleted all of my stashes."

I guess you get "git stash drop" as a synonym for "git stash drop stash@{0}" this way, but it just seems mean to users to make them remember which of "drop" and "clear" does what they want.

-Peff

#3 lore raw
Quoted 10 lines · Jeff King
Quoted 3 lines · Brandon Casey

Thus far I haven't been a big user of git stash, but I plan to use it more and I expect to use 'drop' more often than 'clear'. I expect in the common case there will be a single

There was some discussion of a sensible name, but I don't recall seeing a resolution on this: why not "clear stash@{0}" to clear one, and "clear" to clear all? Otherwise, I foresee "git stash clear stash@{0}" followed by "oops, I just deleted all of my stashes."

I actually got hit by this. I didn't know that stash clear affected all stashes and lost quite a bit of work that way (I use stash to store various test database configs for a tree I work with, and so lost all of them when trying to remove one particular stash).

Quoted 3 lines

I guess you get "git stash drop" as a synonym for "git stash drop stash@{0}" this way, but it just seems mean to users to make them remember which of "drop" and "clear" does what they want.

I have to agree with this.

Signature · 6 lines
JM Ibanez
Software Architect
Orange & Bronze Software Labs, Ltd. Co.

[email protected]
http://software.orangeandbronze.com/
#4 lore raw

PATCH 1/1 git-stash clear: refuse to work with extra parameter for now

Because it is so tempting to expect "git stash clear stash@{4}" to remove the fourth element in the stash while leaving other elements intact, we should not blindly throw away everything.

This may change when we start using "git reflog delete" to selectively nuke a single (or multiple, for that matter) stash entries when such a command is given.

---

Quoted 11 lines · JM Ibanez

Jeff King <[email protected]> writes:
...

Quoted 4 lines

There was some discussion of a sensible name, but I don't recall seeing a resolution on this: why not "clear stash@{0}" to clear one, and "clear" to clear all? Otherwise, I foresee "git stash clear stash@{0}" followed by "oops, I just deleted all of my stashes."

I actually got hit by this. I didn't know that stash clear affected all stashes and lost quite a bit of work that way (I use stash to store various test database configs for a tree I work with, and so lost all of them when trying to remove one particular stash).

I think something along this line may be necessary to futureproof our users.

 git-stash.sh |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
git-stash.sh +5 -1
index 06cb177..80036ef 100755
@@ -20,6 +20,10 @@ no_changes () {
 }
 
 clear_stash () {
+	if test $# != 0
+	then
+		die "git stash clear with parameters unimplemented $@"
+	fi
 	if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
 	then
 		git update-ref -d $ref_stash $current
@@ -216,7 +220,7 @@ apply)
 	apply_stash "$@"
 	;;
 clear)
-	clear_stash
+	clear_stash "$@"
 	;;
 create)
 	if test $# -gt 0 && test "$1" = create
author Acked-by on v1 1/1
#5 lore raw

Re: [PATCH] git-stash clear: refuse to work with extra parameter for now

Quoted 2 lines · Junio C Hamano

I think something along this line may be necessary to futureproof our users.

I think it not only futureproofs, but it helps those who misunderstand (or don't read) the documentation from accidentally losing work.

-Peff

#6 lore raw
In reply to Brandon Casey
Quoted 3 lines · Brandon Casey

I'm not sure if there is a proper way to get 'stash@{0}' from 'refs/stash' so I kept my usage of that former string outside of the drop_stash() function.

Doesn't "$refs_stash@{0}" (which would give refs/stash@{0} not stash@{0}) work for you?

git-stash.sh line 4

Quoted 10 lines
git-stash.sh +1 -1
index 06cb177..a789a53 100755
@@ -1,7 +1,7 @@
 #!/bin/sh
 # Copyright (c) 2007, Nanako Shiraishi
 
-USAGE='[  | save | list | show | apply | clear | create ]'
+USAGE='[  | save | list | show | apply | clear | create | drop ]'

Might want to put drop next to clear, but that is minor.

Quoted 5 lines
git-stash.sh +5 -0
+drop_stash () {
+	if ! have_stash
+	then
+		echo >&2 'No stash entries to drop'
+		exit 0

git-stash.sh apply_stash () { · line 209

Quoted 10 lines
git-stash.sh +10 -0
+	fi
+
+	# Verify supplied argument looks like a stash entry
+	s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
+	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
+	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
+	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
+		die "$*: not a valid stashed state"
+
+	git reflog delete "$@" && echo "Dropped $@ ($s)" ||

The second $@ is inconsistent with the next line's use of $*; intentional?

git-stash.sh create) · line 253

Quoted 11 lines
git-stash.sh +10 -0
+		die "$*: Could not drop stash entry"
+}
 
+drop)
+	shift
+	if test $# = 0
+	then
+		set -- "stash@{0}"
+	fi
+	drop_stash "$@" &&
+	(git rev-parse --verify "stash@{0}" > /dev/null 2>&1 || clear_stash)

Curious.

(1) Why not do the clearing inside drop_stash?

(2) Why is clearning necessary in the first place (iow, shouldn't "reflog delete" take care of that)?

Other than that, nicely done.

#7 lore raw
Quoted 9 lines · Junio C Hamano <gitster <at> pobox.com> writes:
Quoted 3 lines · Brandon Casey <casey <at> nrlssc.navy.mil> writes:

I'm not sure if there is a proper way to get 'stash@{0}' from 'refs/stash' so I kept my usage of that former string outside of the drop_stash() function.

Doesn't "$refs_stash@{0}" (which would give refs/stash@{0} not stash@{0}) work for you?

yep that works. much nicer.

Quoted 5 lines

git-stash.sh line 4

Quoted 3 lines
git-stash.sh +1 -1
-USAGE='[  | save | list | show | apply | clear | create ]'
+USAGE='[  | save | list | show | apply | clear | create | drop ]'

Might want to put drop next to clear, but that is minor.

no problem.

Quoted 3 lines

git-stash.sh apply_stash () { · line 209

Quoted 1 line
git-stash.sh +1 -0
+	git reflog delete "$@" && echo "Dropped $@ ($s)" ||

The second $@ is inconsistent with the next line's use of $*; intentional?

not intentional.

Quoted 8 lines

git-stash.sh create) · line 253

Quoted 4 lines
git-stash.sh +4 -0
+		set -- "stash@{0}"
+	fi
+	drop_stash "$@" &&
+	(git rev-parse --verify "stash@{0}" > /dev/null 2>&1 || clear_stash)

Curious.

(1) Why not do the clearing inside drop_stash?

only because I didn't like stash@{0} notation and didn't want it buried inside a function.

Quoted 3 lines

(2) Why is clearning necessary in the first place (iow, shouldn't "reflog delete" take care of that)?

clear_stash additionally deletes refs/stash and logs/refs/stash at least.

-brandon

#8 lore raw

PATCH 1/1 git-stash: add new 'drop' subcommand

This allows a single stash entry to be deleted. It takes an optional argument which is a stash reflog entry. If no arguments are supplied, it drops the most recent stash entry.

---

This implements the suggested changes on top of latest next. We can leave the discussion of naming and any other issues until after 1.5.4 so not to cause a distraction unless people feel otherwise.

-brandon

 Documentation/git-stash.txt |    7 ++++++-
 git-stash.sh                |   25 ++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
Documentation/git-stash.txt +6 -1
index 9889806..7afc8d3 100644
@@ -8,7 +8,7 @@ git-stash - Stash the changes in a dirty working directory away
 SYNOPSIS
 --------
 [verse]
-'git-stash' (list | show [<stash>] | apply [<stash>] | clear)
+'git-stash' (list | show [<stash>] | apply [<stash>] | clear | drop [<stash>])
 'git-stash' [save] [message...]
 
 DESCRIPTION
@@ -81,6 +81,11 @@ clear::
 	Remove all the stashed states. Note that those states will then
 	be subject to pruning, and may be difficult or impossible to recover.
 
+drop [<stash>]::
+
+	Remove a single stashed state from the stash list. When no `<stash>`
+	is given, it removes the latest one. i.e. `stash@\{0}`
+
 
 DISCUSSION
 ----------
git-stash.sh +24 -1
index b00f888..d6d77fa 100755
@@ -1,7 +1,7 @@
 #!/bin/sh
 # Copyright (c) 2007, Nanako Shiraishi
 
-USAGE='[  | save | list | show | apply | clear | create ]'
+USAGE='[  | save | list | show | apply | clear | drop | create ]'
 
 SUBDIRECTORY_OK=Yes
 OPTIONS_SPEC=
@@ -196,6 +196,25 @@ apply_stash () {
 	fi
 }
 
+drop_stash () {
+	have_stash || die 'No stash entries to drop'
+
+	test $# = 0 && set -- "$ref_stash@{0}"
+
+	# Verify supplied argument looks like a stash entry
+	s=$(git rev-parse --revs-only --no-flags "$@") &&
+	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
+	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
+	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
+		die "$*: not a valid stashed state"
+
+	git reflog delete "$@" && echo "Dropped $* ($s)" ||
+		die "$*: Could not drop stash entry"
+
+	# clear_stash if we just dropped the last stash entry
+	git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
+}
+
 # Main command set
 case "$1" in
 list)
@@ -230,6 +249,10 @@ create)
 	fi
 	create_stash "$*" && echo "$w_commit"
 	;;
+drop)
+	shift
+	drop_stash "$@"
+	;;
 *)
 	if test $# -eq 0
 	then
Signature · 1 line
1.5.3.6
#9 lore raw
In reply to Brandon Casey

git-stash.sh apply_stash () { · line 200

Quoted 8 lines · Brandon Casey
git-stash.sh +6 -0
+drop_stash () {
+       if ! have_stash
+       then
+               echo >&2 'No stash entries to drop'
+               exit 0
+       fi

Please, or

Review

Quoted 2 lines
(quoted diff) +2 -0
+               echo >&2 'No stash entries to drop'
+               exit 1

or

Review

Quoted 2 lines
(quoted diff) +2 -0
+               echo 'No stash entries to drop'
+               exit 0

I would prefer the latter.

Thanks
Marco