git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Aaron M Watson <watsona4@gmail.com>
To: git@vger.kernel.org
Cc: "Jon Seymour" <jon.seymour@gmail.com>,
	"David Caldwell" <david@porkrind.org>,
	"Øystein Walle" <oystwa@gmail.com>, "Jeff King" <peff@peff.net>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"David Aguilar" <davvid@gmail.com>,
	"Alex Henrie" <alexhenrie24@gmail.com>
Subject: [PATCH] stash: allow ref of a stash by index
Date: Sat, 03 Sep 2016 19:21:18 -0400	[thread overview]
Message-ID: <1472944878.19860.4.camel@gmail.com> (raw)

Allows stashes to be referenced by index only. Instead of referencing
"stash@{n}" explicitly, it can simply be referenced as "n".

Signed-off-by: Aaron M Watson <watsona4@gmail.com>
---
 Documentation/git-stash.txt | 11 ++++---
 git-stash.sh                | 10 +++++-
 t/t3907-stash-index.sh      | 77
+++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+), 6 deletions(-)
 create mode 100755 t/t3907-stash-index.sh

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 92df596..af11cff 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -35,11 +35,12 @@ A stash is by default listed as "WIP on
'branchname' ...", but
 you can give a more descriptive message on the command line when
 you create one.
 
-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).
+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). Stashes may also be references by specifying just the stash
+index (e.g. the integer `n` is equivalent to `stash@{n}`).
 
 OPTIONS
 -------
diff --git a/git-stash.sh b/git-stash.sh
index 826af18..a0c7f12 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # Copyright (c) 2007, Nanako Shiraishi
 
 dashless=$(basename "$0" | sed -e 's/-/ /')
@@ -371,6 +371,14 @@ parse_flags_and_rev()
 	test "$PARSE_CACHE" = "$*" && return 0 # optimisation
 	PARSE_CACHE="$*"
 
+	args=()
+	for arg
+	do
+		[[ $arg =~ ^[0-9]+$ ]] && arg="stash@{$arg}"
+		args+=("$arg")
+	done
+	set -- "${args[@]}"
+
 	IS_STASH_LIKE=
 	IS_STASH_REF=
 	INDEX_OPTION=
diff --git a/t/t3907-stash-index.sh b/t/t3907-stash-index.sh
new file mode 100755
index 0000000..72a1838
--- /dev/null
+++ b/t/t3907-stash-index.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+#
+# Copyright (c) 2016 Aaron M. Watson
+#
+
+test_description='Test git stash with index ref specification'
+
+. ./test-lib.sh
+
+test_expect_success 'stash some dirty working directory' '
+	echo 1 > file &&
+	git add file &&
+	echo unrelated >other-file &&
+	git add other-file &&
+	test_tick &&
+	git commit -m initial &&
+	echo 2 > file &&
+	git add file &&
+	echo 3 > file &&
+	test_tick &&
+	git stash &&
+	git diff-files --quiet &&
+	git diff-index --cached --quiet HEAD
+'
+
+cat > expect << EOF
+diff --git a/file b/file
+index 0cfbf08..00750ed 100644
+--- a/file
++++ b/file
+@@ -1 +1 @@
+-2
++3
+EOF
+
+test_expect_success 'applying bogus stash does nothing' '
+	test_must_fail git stash apply 1 &&
+	echo 1 >expect &&
+	test_cmp expect file
+'
+
+test_expect_success 'drop middle stash' '
+	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 'invalid ref of the form "n", where 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_done
-- 
2.7.4

             reply	other threads:[~2016-09-03 23:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-03 23:21 Aaron M Watson [this message]
2016-09-04  1:52 ` [PATCH] stash: allow ref of a stash by index Jeff King
2016-09-04  7:01   ` Junio C Hamano
2016-09-04  7:21   ` Johannes Schindelin
2016-09-04 10:57     ` Philip Oakley
2016-09-05 21:46   ` Øystein Walle
2016-09-05 21:58     ` Øystein Walle
2016-09-05 23:52     ` Jeff King
2016-09-07 17:50     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1472944878.19860.4.camel@gmail.com \
    --to=watsona4@gmail.com \
    --cc=alexhenrie24@gmail.com \
    --cc=avarab@gmail.com \
    --cc=david@porkrind.org \
    --cc=davvid@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jon.seymour@gmail.com \
    --cc=oystwa@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).