Skip to content

Commit

Permalink
stash: introduce 'git stash store'
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Ramkumar Ramachandra authored and Junio C Hamano committed Jun 17, 2013
1 parent 0719f30 commit bd514ca
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Documentation/git-stash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SYNOPSIS
[-u|--include-untracked] [-a|--all] [<message>]]
'git stash' clear
'git stash' create [<message>]
'git stash' store [-m|--message <message>] [-q|--quiet] <commit>

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

store::

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

DISCUSSION
----------
Expand Down
47 changes: 41 additions & 6 deletions git-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,41 @@ create_stash () {
die "$(gettext "Cannot record working tree state")"
}

store_stash () {
while test $# != 0
do
case "$1" in
-m|--message)
shift
stash_msg="$1"
;;
-q|--quiet)
quiet=t
;;
*)
break
;;
esac
shift
done
test $# = 1 ||
die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"

w_commit="$1"
if test -z "$stash_msg"
then
stash_msg="Created via \"git stash store\"."
fi

# Make sure the reflog for stash is kept.
: >>"$GIT_DIR/logs/$ref_stash"
git update-ref -m "$stash_msg" $ref_stash $w_commit
ret=$?
test $ret != 0 && test -z $quiet &&
die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
return $ret
}

save_stash () {
keep_index=
patch_mode=
Expand Down Expand Up @@ -227,12 +262,8 @@ 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 -m "$stash_msg" -q $w_commit ||
die "$(gettext "Cannot save the current status")"
say Saved working directory and index state "$stash_msg"

if test -z "$patch_mode"
Expand Down Expand Up @@ -549,6 +580,10 @@ create)
shift
create_stash "$*" && echo "$w_commit"
;;
store)
shift
store_stash "$@"
;;
drop)
shift
drop_stash "$@"
Expand Down
19 changes: 19 additions & 0 deletions t/t3903-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,23 @@ test_expect_success 'stash where working directory contains "HEAD" file' '
test_cmp output expect
'

test_expect_success 'store called with invalid commit' '
test_must_fail git stash store foo
'

test_expect_success 'store updates stash ref and reflog' '
git stash clear &&
git reset --hard &&
echo quux >bazzy &&
git add bazzy &&
STASH_ID=$(git stash create) &&
git reset --hard &&
! grep quux bazzy &&
git stash store -m quuxery $STASH_ID &&
test $(cat .git/refs/stash) = $STASH_ID &&
grep $STASH_ID .git/logs/refs/stash &&
git stash pop &&
grep quux bazzy
'

test_done

0 comments on commit bd514ca

Please sign in to comment.