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 <artag...@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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to