Skip to content

Commit bd514ca

Browse files
artagnongitster
authored andcommitted
stash: introduce 'git stash store'
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0719f30 commit bd514ca

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

Documentation/git-stash.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SYNOPSIS
1717
[-u|--include-untracked] [-a|--all] [<message>]]
1818
'git stash' clear
1919
'git stash' create [<message>]
20+
'git stash' store [-m|--message <message>] [-q|--quiet] <commit>
2021

2122
DESCRIPTION
2223
-----------
@@ -154,6 +155,12 @@ create::
154155
This is intended to be useful for scripts. It is probably not
155156
the command you want to use; see "save" above.
156157

158+
store::
159+
160+
Store a given stash created via 'git stash create' (which is a
161+
dangling merge commit) in the stash ref, updating the stash
162+
reflog. This is intended to be useful for scripts. It is
163+
probably not the command you want to use; see "save" above.
157164

158165
DISCUSSION
159166
----------

git-stash.sh

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,41 @@ create_stash () {
156156
die "$(gettext "Cannot record working tree state")"
157157
}
158158

159+
store_stash () {
160+
while test $# != 0
161+
do
162+
case "$1" in
163+
-m|--message)
164+
shift
165+
stash_msg="$1"
166+
;;
167+
-q|--quiet)
168+
quiet=t
169+
;;
170+
*)
171+
break
172+
;;
173+
esac
174+
shift
175+
done
176+
test $# = 1 ||
177+
die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
178+
179+
w_commit="$1"
180+
if test -z "$stash_msg"
181+
then
182+
stash_msg="Created via \"git stash store\"."
183+
fi
184+
185+
# Make sure the reflog for stash is kept.
186+
: >>"$GIT_DIR/logs/$ref_stash"
187+
git update-ref -m "$stash_msg" $ref_stash $w_commit
188+
ret=$?
189+
test $ret != 0 && test -z $quiet &&
190+
die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
191+
return $ret
192+
}
193+
159194
save_stash () {
160195
keep_index=
161196
patch_mode=
@@ -227,12 +262,8 @@ save_stash () {
227262
clear_stash || die "$(gettext "Cannot initialize stash")"
228263

229264
create_stash "$stash_msg" $untracked
230-
231-
# Make sure the reflog for stash is kept.
232-
: >>"$GIT_DIR/logs/$ref_stash"
233-
234-
git update-ref -m "$stash_msg" $ref_stash $w_commit ||
235-
die "$(gettext "Cannot save the current status")"
265+
store_stash -m "$stash_msg" -q $w_commit ||
266+
die "$(gettext "Cannot save the current status")"
236267
say Saved working directory and index state "$stash_msg"
237268

238269
if test -z "$patch_mode"
@@ -549,6 +580,10 @@ create)
549580
shift
550581
create_stash "$*" && echo "$w_commit"
551582
;;
583+
store)
584+
shift
585+
store_stash "$@"
586+
;;
552587
drop)
553588
shift
554589
drop_stash "$@"

t/t3903-stash.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,23 @@ test_expect_success 'stash where working directory contains "HEAD" file' '
637637
test_cmp output expect
638638
'
639639

640+
test_expect_success 'store called with invalid commit' '
641+
test_must_fail git stash store foo
642+
'
643+
644+
test_expect_success 'store updates stash ref and reflog' '
645+
git stash clear &&
646+
git reset --hard &&
647+
echo quux >bazzy &&
648+
git add bazzy &&
649+
STASH_ID=$(git stash create) &&
650+
git reset --hard &&
651+
! grep quux bazzy &&
652+
git stash store -m quuxery $STASH_ID &&
653+
test $(cat .git/refs/stash) = $STASH_ID &&
654+
grep $STASH_ID .git/logs/refs/stash &&
655+
git stash pop &&
656+
grep quux bazzy
657+
'
658+
640659
test_done

0 commit comments

Comments
 (0)