git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/6] stash: introduce 'git stash store'

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
May 13, 2013, 12:45 UTC
Message-ID
<1368449154-21882-5-git-send-email-artagnon@gmail.com>
In-Reply-To
<1368449154-21882-1-git-send-email-artagnon@gmail.com>

save_stash() contains the logic for doing two potentially independent operations; the first is preparing the stash merge commit, and the second is updating the stash ref/ reflog accordingly. While the first operation is abstracted out into a create_stash() for callers to access via 'git stash create', the second one is not. Fix this by factoring out the logic for storing the stash into a store_stash() that callers can access via 'git stash store'.

Like create, store is not intended for end user interactive use, but for callers in other scripts. We can simplify the logic in the rebase.autostash feature using this new subcommand.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 Documentation/git-stash.txt |  6 ++++++
 git-stash.sh                | 25 +++++++++++++++++++------
 t/t3903-stash.sh            | 20 ++++++++++++++++++++
 3 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 05e462b..e58ab74 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -17,6 +17,7 @@ SYNOPSIS
 	     [-u|--include-untracked] [-a|--all] [<message>]]
 'git stash' clear
 'git stash' create [<message> [<include-untracked-p>]]
+'git stash' store <commit> <message>
 
 DESCRIPTION
 -----------
@@ -152,6 +153,11 @@ create::
 	Create a stash (which is a regular commit object) and return its
 	object name, without storing it anywhere in the ref namespace.
 
+store::
+
+	Store a given stash created via 'git stash create' (which is a
+	dangling merge commit) in the stash ref, updating the stash
+	reflog.
 
 DISCUSSION
 ----------
diff --git a/git-stash.sh b/git-stash.sh
index 0ede313..1d483f5 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -156,6 +156,20 @@ create_stash () {
 	die "$(gettext "Cannot record working tree state")"
 }
 
+store_stash () {
+	if test $# != 2
+	then
+		die "$(gettext "git stash store requires two arguments")"
+	fi
+	w_commit="$1"
+	stash_msg="$2"
+
+	# Make sure the reflog for stash is kept.
+	: >>"$GIT_DIR/logs/$ref_stash"
+	git update-ref -m "$stash_msg" $ref_stash $w_commit ||
+	die "$(gettext "Cannot save the current status")"
+}
+
 save_stash () {
 	keep_index=
 	patch_mode=
@@ -227,12 +241,7 @@ save_stash () {
 		clear_stash || die "$(gettext "Cannot initialize stash")"
 
 	create_stash "$stash_msg" $untracked
-
-	# Make sure the reflog for stash is kept.
-	: >>"$GIT_DIR/logs/$ref_stash"
-
-	git update-ref -m "$stash_msg" $ref_stash $w_commit ||
-		die "$(gettext "Cannot save the current status")"
+	store_stash $w_commit "$stash_msg"
 	say Saved working directory and index state "$stash_msg"
 
 	if test -z "$patch_mode"
@@ -549,6 +558,10 @@ create)
 	shift
 	create_stash "$@" && echo "$w_commit"
 	;;
+store)
+	shift
+	store_stash "$@"
+	;;
 drop)
 	shift
 	drop_stash "$@"
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 5dfbda7..2ff3afd 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -637,4 +637,24 @@ test_expect_success 'stash where working directory contains "HEAD" file' '
 	test_cmp output expect
 '
 
+test_expect_success 'store - argument 1 not a commit' '
+	test_must_fail git stash store foo bar
+'
+
+test_expect_success 'store - updates stash ref and reflog' '
+	git stash clear &&
+	git reset --hard &&
+	echo quux >file &&
+	git add file &&
+	STASH_ID=$(git stash create) &&
+	git reset --hard &&
+	! grep quux file &&
+	git stash store ${STASH_ID} quuxery &&
+	test $(cat .git/refs/stash) = ${STASH_ID} &&
+	grep ${STASH_ID} .git/logs/refs/stash &&
+	git stash pop &&
+	grep quux file &&
+	false
+'
+
 test_done
-- 
1.8.3.rc1.57.g4ac1522
Previous: Ramkumar RamachandraNext: Junio C Hamano
Message 7 of 21 in “Get stash to help rebase.autostash”
  1. 0/6 Get stash to help rebase.autostashRamkumar Ramachandra, May 13, 2013
  2. 1/6 Documentation/stash: correct synopsis for createRamkumar Ramachandra, May 13, 2013
  3. 2/6 Documentation/stash: document short form -p in synopsisRamkumar Ramachandra, May 13, 2013
  4. 3/6 stash: simplify option parser for createRamkumar Ramachandra, May 13, 2013
  5. Junio C HamanoMay 13, 2013
  6. Ramkumar RamachandraMay 13, 2013
  7. 4/6 stash: introduce 'git stash store'Ramkumar Ramachandra, May 13, 2013
  8. Junio C HamanoMay 13, 2013
  9. Ramkumar RamachandraMay 13, 2013
  10. Junio C HamanoMay 13, 2013
  11. Ramkumar RamachandraMay 13, 2013
  12. Junio C HamanoMay 13, 2013
  13. Ramkumar RamachandraMay 13, 2013
  14. Junio C HamanoMay 13, 2013
  15. Ramkumar RamachandraMay 13, 2013
  16. Junio C HamanoMay 13, 2013
  17. Ramkumar RamachandraMay 13, 2013
  18. Ramkumar RamachandraMay 13, 2013
  19. 5/6 stash: tweak error message in store_stash ()Ramkumar Ramachandra, May 13, 2013
  20. Junio C HamanoMay 13, 2013
  21. 6/6 rebase: use 'git stash store' to simplify logicRamkumar Ramachandra, May 13, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.