Skip to content

Commit fa4bf9e

Browse files
committed
Merge branch 'rr/rebase-stash-store'
Finishing touches for the "git rebase --autostash" feature introduced earlier. * rr/rebase-stash-store: rebase: use 'git stash store' to simplify logic stash: introduce 'git stash store' stash: simplify option parser for create stash doc: document short form -p in synopsis stash doc: add a warning about using create
2 parents 85318f5 + 20351bb commit fa4bf9e

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

Documentation/git-stash.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ SYNOPSIS
1313
'git stash' drop [-q|--quiet] [<stash>]
1414
'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
1515
'git stash' branch <branchname> [<stash>]
16-
'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
16+
'git stash' [save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
1717
[-u|--include-untracked] [-a|--all] [<message>]]
1818
'git stash' clear
19-
'git stash' create
19+
'git stash' create [<message>]
20+
'git stash' store [-m|--message <message>] [-q|--quiet] <commit>
2021

2122
DESCRIPTION
2223
-----------
@@ -151,7 +152,15 @@ create::
151152

152153
Create a stash (which is a regular commit object) and return its
153154
object name, without storing it anywhere in the ref namespace.
155+
This is intended to be useful for scripts. It is probably not
156+
the command you want to use; see "save" above.
154157

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.
155164

156165
DISCUSSION
157166
----------

git-rebase.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,8 @@ finish_rebase () {
155155
then
156156
echo "$(gettext 'Applied autostash.')"
157157
else
158-
ref_stash=refs/stash &&
159-
>>"$GIT_DIR/logs/$ref_stash" &&
160-
git update-ref -m "autostash" $ref_stash $stash_sha1 ||
161-
die "$(eval_gettext 'Cannot store $stash_sha1')"
162-
158+
git stash store -m "autostash" -q $stash_sha1 ||
159+
die "$(eval_gettext "Cannot store \$stash_sha1")"
163160
gettext 'Applying autostash resulted in conflicts.
164161
Your changes are safe in the stash.
165162
You can run "git stash pop" or "git stash drop" it at any time.

git-stash.sh

Lines changed: 42 additions & 10 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"
@@ -546,12 +577,13 @@ clear)
546577
clear_stash "$@"
547578
;;
548579
create)
549-
if test $# -gt 0 && test "$1" = create
550-
then
551-
shift
552-
fi
580+
shift
553581
create_stash "$*" && echo "$w_commit"
554582
;;
583+
store)
584+
shift
585+
store_stash "$@"
586+
;;
555587
drop)
556588
shift
557589
drop_stash "$@"

t/t3903-stash.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,4 +654,23 @@ test_expect_success 'stash where working directory contains "HEAD" file' '
654654
test_cmp output expect
655655
'
656656

657+
test_expect_success 'store called with invalid commit' '
658+
test_must_fail git stash store foo
659+
'
660+
661+
test_expect_success 'store updates stash ref and reflog' '
662+
git stash clear &&
663+
git reset --hard &&
664+
echo quux >bazzy &&
665+
git add bazzy &&
666+
STASH_ID=$(git stash create) &&
667+
git reset --hard &&
668+
! grep quux bazzy &&
669+
git stash store -m quuxery $STASH_ID &&
670+
test $(cat .git/refs/stash) = $STASH_ID &&
671+
grep $STASH_ID .git/logs/refs/stash &&
672+
git stash pop &&
673+
grep quux bazzy
674+
'
675+
657676
test_done

0 commit comments

Comments
 (0)