Skip to contentSkip to conversation

Allow stashes to be referenced by index only

PATCH In review Aaron M Watson 4 participants 8 messages Sep 8, 2016 – Oct 26, 2016 1 message not in the archive Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Allow stashes to be referenced by index only

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index"). The syntax for the typical stash (stash@{n}) is slightly annoying and easy to forget, and sometimes difficult to escape properly in a script. Because of this the capability to do things with the stash by simply referencing the index is desirable.

This patch includes the superior implementation provided by Øsse Walle (thanks for that), with a slight change to fix a broken test in the test suite. I also merged the test scripts as suggested by Jeff King, and un-wrapped the documentation as suggested by Junio Hamano.

---
 Documentation/git-stash.txt |  3 ++-
 git-stash.sh                | 17 +++++++++++++++--
 t/t3903-stash.sh            | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 3 deletions(-)
Documentation/git-stash.txt +2 -1
index 92df596..2e9cef0 100644
@@ -39,7 +39,8 @@ The latest stash you created is stored in `refs/stash`; older
 stashes are found in the reflog of this reference and can be named using
 the usual reflog syntax (e.g. `stash@{0}` is the most recently
 created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
-is also possible).
+is also possible). Stashes may also be referenced by specifying just the
+stash index (e.g. the integer `n` is equivalent to `stash@{n}`).
 
 OPTIONS
 -------
git-stash.sh +15 -2
index 826af18..d8d3b8d 100755
@@ -384,9 +384,10 @@ parse_flags_and_rev()
 	i_tree=
 	u_tree=
 
-	REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
+	REV=$(git rev-parse --no-flags --symbolic --sq "$@" 2> /dev/null)
 
 	FLAGS=
+	ARGV=
 	for opt
 	do
 		case "$opt" in
@@ -404,10 +405,13 @@ parse_flags_and_rev()
 					die "$(eval_gettext "unknown option: \$opt")"
 				FLAGS="${FLAGS}${FLAGS:+ }$opt"
 			;;
+			*)
+				ARGV="${ARGV}${ARGV:+ }'$opt'"
+			;;
 		esac
 	done
 
-	eval set -- $REV
+	eval set -- $ARGV
 
 	case $# in
 		0)
@@ -422,6 +426,15 @@ parse_flags_and_rev()
 		;;
 	esac
 
+	case "$1" in
+		*[!0-9]*)
+			:
+		;;
+		*)
+			set -- "${ref_stash}@{$1}"
+		;;
+	esac
+
 	REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
 		reference="$1"
 		die "$(eval_gettext "\$reference is not a valid reference")"
t/t3903-stash.sh +35 -0
index 2142c1f..f82a8c4 100755
@@ -131,6 +131,26 @@ test_expect_success 'drop middle stash' '
 	test 1 = $(git show HEAD:file)
 '
 
+test_expect_success 'drop middle stash by index' '
+	git reset --hard &&
+	echo 8 > file &&
+	git stash &&
+	echo 9 > file &&
+	git stash &&
+	git stash drop 1 &&
+	test 2 = $(git stash list | wc -l) &&
+	git stash apply &&
+	test 9 = $(cat file) &&
+	test 1 = $(git show :file) &&
+	test 1 = $(git show HEAD:file) &&
+	git reset --hard &&
+	git stash drop &&
+	git stash apply &&
+	test 3 = $(cat file) &&
+	test 1 = $(git show :file) &&
+	test 1 = $(git show HEAD:file)
+'
+
 test_expect_success 'stash pop' '
 	git reset --hard &&
 	git stash pop &&
@@ -604,6 +624,21 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash drop
 '
 
+test_expect_success 'invalid ref of the form "n", n >= N' '
+	git stash clear &&
+	test_must_fail git stash drop 0 &&
+	echo bar5 > file &&
+	echo bar6 > file2 &&
+	git add file2 &&
+	git stash &&
+	test_must_fail git stash drop 1 &&
+	test_must_fail git stash pop 1 &&
+	test_must_fail git stash apply 1 &&
+	test_must_fail git stash show 1 &&
+	test_must_fail git stash branch tmp 1 &&
+	git stash drop
+'
+
 test_expect_success 'stash branch should not drop the stash if the branch exists' '
 	git stash clear &&
 	echo foo >file &&
Signature · 1 line
2.7.4
#1 lore raw
Quoted 8 lines · Aaron M Watson

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index"). The syntax for the typical stash (stash@{n}) is slightly annoying and easy to forget, and sometimes difficult to escape properly in a script. Because of this the capability to do things with the stash by simply referencing the index is desirable.

Quoted 4 lines

This patch includes the superior implementation provided by Øsse Walle (thanks for that), with a slight change to fix a broken test in the test suite. I also merged the test scripts as suggested by Jeff King, and un-wrapped the documentation as suggested by Junio Hamano.

Just cleaning out my list of leftover bits, and it looks like this patch fell through the cracks.

The last thing before this was generally people agreeing with the approach that Øsse showed:

http://public-inbox.org/git/CAFaJEqu-JUcwLjrQBk_huSa3DZfCf8O4eAZ=UgcXHzN=CLgtpw@mail.gmail.com/

http://public-inbox.org/git/[email protected]/

and we just needed to roll that up into a patch. Which this _mostly_ is, but there are a few funny things still:

git-stash.sh parse_flags_and_rev() · line 387

Quoted 10 lines
git-stash.sh +1 -1
index 826af18..d8d3b8d 100755
@@ -384,9 +384,10 @@ parse_flags_and_rev()
 	i_tree=
 	u_tree=
 
-	REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
+	REV=$(git rev-parse --no-flags --symbolic --sq "$@" 2> /dev/null)

Style: we don't put a space between ">" and the filename.

So here we assign REV, and we no longer exit (to handle something like "1" by itself, which is good).

Quoted 7 lines
git-stash.sh +1 -0
 	FLAGS=
+	ARGV=
 	for opt
 	do
 		case "$opt" in
@@ -404,10 +405,13 @@ parse_flags_and_rev()
 					die "$(eval_gettext "unknown option: \$opt")"

git-stash.sh parse_flags_and_rev() · line 414

Quoted 10 lines
git-stash.sh +4 -1
 				FLAGS="${FLAGS}${FLAGS:+ }$opt"
 			;;
+			*)
+				ARGV="${ARGV}${ARGV:+ }'$opt'"
+			;;
 		esac
 	done
 
-	eval set -- $REV
+	eval set -- $ARGV

But what's going on here? Why did we bother running rev-parse earlier if we don't actually use the value of REV?

You mentioned tweaking it to fix a broken test, and indeed, just using $REV here breaks a few tests in t3903.

Offhand, I do not see anything wrong with pulling the non-option values out in the loop. But in that case I think the assignment of REV can just go away completely.

t/t3903-stash.sh test_expect_success 'drop middle stash' ' · line 138

Quoted 13 lines
t/t3903-stash.sh +5 -0
index 2142c1f..f82a8c4 100755
@@ -131,6 +131,26 @@ test_expect_success 'drop middle stash' '
 	test 1 = $(git show HEAD:file)
 '
 
+test_expect_success 'drop middle stash by index' '
+	git reset --hard &&
+	echo 8 > file &&
+	git stash &&
+	echo 9 > file &&

Ditto on the ">" style here and elsewhere.

The tests otherwise look good to me.

-Peff

A message that is not in the archive (sent off-list or not yet received): CAB0+k9JrX7Ax26HfTEgoSyj02szFyHLayqTyW6KPuxVvXBOEOw@mail.gmail.com

#3 lore raw
Quoted 11 lines · Aaron and Ashley Watson
Quoted 10 lines

But what's going on here? Why did we bother running rev-parse earlier if we don't actually use the value of REV?

You mentioned tweaking it to fix a broken test, and indeed, just using $REV here breaks a few tests in t3903.

Offhand, I do not see anything wrong with pulling the non-option values out in the loop. But in that case I think the assignment of REV can just go away completely.

Quoted 5 lines

The only reason for REV to remain is to preserve the error message seen with the previous behavior. Perhaps it would be better to instead move the assignment
of REV to the only place it is still used: the error message when multiple arguments were detected.

Ah, thanks, I missed that use. We suppress stderr, so we're literally just getting the set of revs there. But that should match what we have in ARGV anyway (after all, $ARGV is where we decided we had too many revs, and what we'll feed to rev-parse to get the sha1).

So I wonder if:

Too many revisions specified: $ARGV

would be more appropriate (at which point you can probably just continue to call it $REV).

Quoted 3 lines

I'm not sure of the next steps in the process of submitting a patch. Should I submit a new patch by replying to this email, or is using git send-email to create a new mail thread better?

Generally re-rolls of a patch are done as replies to the original. Gmail is bad about corrupting whitespace, though, so you can't just reply and paste the patch in there. You can use ask git-send-email to continue the thread, though:

[email protected] git send-email --in-reply-to=$mid ...other options...

You might also want to cc the people involved in the earlier discussion. The public-inbox archive gives a customized full send-email command for each message to make it easy.

http://public-inbox.org/git/[email protected]/

-Peff

#4 lore raw

PATCH 1/1 Allow stashes to be referenced by index only

In reply to Aaron M Watson

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index"). The syntax for the typical stash (stash@{n}) is slightly annoying and easy to forget, and sometimes difficult to escape properly in a script. Because of this the capability to do things with the stash by simply referencing the index is desirable.

This patch includes the superior implementation provided by Øsse Walle (thanks for that), with a slight change to fix a broken test in the test suite. I also merged the test scripts as suggested by Jeff King, and un-wrapped the documentation as suggested by Junio Hamano.

---
 Documentation/git-stash.txt |  3 ++-
 git-stash.sh                | 15 +++++++++++++--
 t/t3903-stash.sh            | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 3 deletions(-)
Documentation/git-stash.txt +2 -1
index 92df596..2e9cef0 100644
@@ -39,7 +39,8 @@ The latest stash you created is stored in `refs/stash`; older
 stashes are found in the reflog of this reference and can be named using
 the usual reflog syntax (e.g. `stash@{0}` is the most recently
 created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
-is also possible).
+is also possible). Stashes may also be referenced by specifying just the
+stash index (e.g. the integer `n` is equivalent to `stash@{n}`).
 
 OPTIONS
 -------
git-stash.sh +13 -2
index 826af18..d7072c8 100755
@@ -384,9 +384,8 @@ parse_flags_and_rev()
 	i_tree=
 	u_tree=
 
-	REV=$(git rev-parse --no-flags --symbolic --sq "$@") || exit 1
-
 	FLAGS=
+	REV=
 	for opt
 	do
 		case "$opt" in
@@ -404,6 +403,9 @@ parse_flags_and_rev()
 					die "$(eval_gettext "unknown option: \$opt")"
 				FLAGS="${FLAGS}${FLAGS:+ }$opt"
 			;;
+			*)
+				REV="${REV}${REV:+ }'$opt'"
+			;;
 		esac
 	done
 
@@ -422,6 +424,15 @@ parse_flags_and_rev()
 		;;
 	esac
 
+	case "$1" in
+		*[!0-9]*)
+			:
+		;;
+		*)
+			set -- "${ref_stash}@{$1}"
+		;;
+	esac
+
 	REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
 		reference="$1"
 		die "$(eval_gettext "\$reference is not a valid reference")"
t/t3903-stash.sh +35 -0
index 2142c1f..f82a8c4 100755
@@ -131,6 +131,26 @@ test_expect_success 'drop middle stash' '
 	test 1 = $(git show HEAD:file)
 '
 
+test_expect_success 'drop middle stash by index' '
+	git reset --hard &&
+	echo 8 >file &&
+	git stash &&
+	echo 9 >file &&
+	git stash &&
+	git stash drop 1 &&
+	test 2 = $(git stash list | wc -l) &&
+	git stash apply &&
+	test 9 = $(cat file) &&
+	test 1 = $(git show :file) &&
+	test 1 = $(git show HEAD:file) &&
+	git reset --hard &&
+	git stash drop &&
+	git stash apply &&
+	test 3 = $(cat file) &&
+	test 1 = $(git show :file) &&
+	test 1 = $(git show HEAD:file)
+'
+
 test_expect_success 'stash pop' '
 	git reset --hard &&
 	git stash pop &&
@@ -604,7 +624,21 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash drop
 '
 
+test_expect_success 'invalid ref of the form "n", n >= N' '
+	git stash clear &&
+	test_must_fail git stash drop 0 &&
+	echo bar5 >file &&
+	echo bar6 >file2 &&
+	git add file2 &&
+	git stash &&
+	test_must_fail git stash drop 1 &&
+	test_must_fail git stash pop 1 &&
+	test_must_fail git stash apply 1 &&
+	test_must_fail git stash show 1 &&
+	test_must_fail git stash branch tmp 1 &&
+	git stash drop
+'
+
 test_expect_success 'stash branch should not drop the stash if the branch exists' '
 	git stash clear &&
 	echo foo >file &&
Signature · 1 line
2.7.4
#5 lore raw
Quoted 8 lines · Aaron M Watson

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index"). The syntax for the typical stash (stash@{n}) is slightly annoying and easy to forget, and sometimes difficult to escape properly in a script. Because of this the capability to do things with the stash by simply referencing the index is desirable.

Quoted 7 lines

This patch includes the superior implementation provided by Øsse Walle (thanks for that), with a slight change to fix a broken test in the test suite. I also merged the test scripts as suggested by Jeff King, and un-wrapped the documentation as suggested by Junio Hamano.

---

Thanks, this version looks good to me.

Oddly, it does not seem to apply. I get:

  $ git am -3 ~/patch
  Applying: Allow stashes to be referenced by index only
  Using index info to reconstruct a base tree...
  M       git-stash.sh
  error: patch failed: t/t3903-stash.sh:604
  error: t/t3903-stash.sh: patch does not apply
  error: Did you hand edit your patch?
  It does not apply to blobs recorded in its index.
  Patch failed at 0001 Allow stashes to be referenced by index only

The culprit seems to be the final hunk header:

Review test_expect_success 'invalid ref of the form stash@{n}, n >= N' '

Quoted 1 line
(quoted diff) +0 -0
@@ -604,7 +624,21 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '

This should be "604,6", as there are 6 context lines, and your patch does not remove any lines.

I suspect the maintainer can fix it up while applying, but for my
curiosity: did you hand-edit it, or is there a potential bug in git's diff code?

-Peff

#6 lore raw
Quoted 34 lines · Jeff King
Quoted 8 lines · Aaron M Watson

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index"). The syntax for the typical stash (stash@{n}) is slightly annoying and easy to forget, and sometimes difficult to escape properly in a script. Because of this the capability to do things with the stash by simply referencing the index is desirable.

Quoted 7 lines

This patch includes the superior implementation provided by Øsse Walle (thanks for that), with a slight change to fix a broken test in the test suite. I also merged the test scripts as suggested by Jeff King, and un-wrapped the documentation as suggested by Junio Hamano.

---

Thanks, this version looks good to me.

Oddly, it does not seem to apply. I get:

  $ git am -3 ~/patch
  Applying: Allow stashes to be referenced by index only
  Using index info to reconstruct a base tree...
  M       git-stash.sh
  error: patch failed: t/t3903-stash.sh:604
  error: t/t3903-stash.sh: patch does not apply
  error: Did you hand edit your patch?
  It does not apply to blobs recorded in its index.
  Patch failed at 0001 Allow stashes to be referenced by index only

The culprit seems to be the final hunk header:

Quoted 8 lines

Review test_expect_success 'invalid ref of the form stash@{n}, n >= N' '

Quoted 1 line
(quoted diff) +0 -0
@@ -604,7 +624,21 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '

This should be "604,6", as there are 6 context lines, and your patch does not remove any lines.

I suspect the maintainer can fix it up while applying, but for my
curiosity: did you hand-edit it, or is there a potential bug in git's diff code?

I did indeed edit the patch by hand (I forgot to remove the spaces after the > in the test file), but
the bug appears to be in emacs's diff-mode, not in git.

Quoted 2 lines

-Peff

Signature · 1 line
Aaron and Ashley Watson
#7 lore raw
In reply to Aaron M Watson

Aaron M Watson <[email protected]> writes:

Quoted 4 lines · Aaron M Watson

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n".
Most users only reference stashes by their position in the stash stask (what I refer to as the "index").

It is unclear if the first sentence is a statement of the fact, an expression of desire, or something else. With the current codebase, it cannot simply be referenced as "n", and you either "wish it were possible", or "make it possible to do so", or perhaps little bit of both.

This is why we tend to use imperative mood to give an order to the codebase to "be like so" to make it clear.

Perhaps

Instead of referencing "stash@{n}" explicitly, make it possible to simply reference as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index" here).

or something like that (which is what I tenatively rewritten this to while queuing).

Quoted 12 lines
git-stash.sh +3 -0
@@ -404,6 +403,9 @@ parse_flags_and_rev()
 					die "$(eval_gettext "unknown option: \$opt")"
 				FLAGS="${FLAGS}${FLAGS:+ }$opt"
 			;;
+			*)
+				REV="${REV}${REV:+ }'$opt'"
+			;;
 		esac
 	done
 
@@ -422,6 +424,15 @@ parse_flags_and_rev()
 		;;

git-stash.sh parse_flags_and_rev() · line 434

Quoted 10 lines
git-stash.sh +8 -0
 	esac
 
+	case "$1" in
+		*[!0-9]*)
+			:
+		;;
+		*)
+			set -- "${ref_stash}@{$1}"
+		;;
+	esac

I can see that you inherited the brokenness from an existing one in the earlier hunk, but case arms in these two case statements are indented one level too deep. It would be good to fix it in a follow-up patch (not a reroll of this patch).

Thanks. Will queue.

#8 lore raw
Quoted 15 lines · Junio C Hamano

Aaron M Watson <[email protected]> writes:

Quoted 4 lines · Aaron M Watson

Instead of referencing "stash@{n}" explicitly, it can simply be referenced as "n".
Most users only reference stashes by their position in the stash stask (what I refer to as the "index").

It is unclear if the first sentence is a statement of the fact, an expression of desire, or something else. With the current codebase, it cannot simply be referenced as "n", and you either "wish it were possible", or "make it possible to do so", or perhaps little bit of both.

Quoted 8 lines

This is why we tend to use imperative mood to give an order to the codebase to "be like so" to make it clear.

Perhaps

Instead of referencing "stash@{n}" explicitly, make it possible to simply reference as "n". Most users only reference stashes by their position in the stash stask (what I refer to as the "index" here).

s/stask/stack/

ATB,
Ramsay Jones