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

Re: [PATCH 4/5] stash: introduce 'git stash store'

From
Phil Hord <phil.hord@gmail.com>
Date
Jun 14, 2013, 12:39 UTC
Message-ID
<CABURp0rsP-Z7uT46WtSpVV7j4OGdcApcpDswsHt79V9WYbfCoQ@mail.gmail.com>
In-Reply-To
<1371205924-8982-5-git-send-email-artagnon@gmail.com>

On Fri, Jun 14, 2013 at 6:32 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:

Show 41 quoted lines
> 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 |  7 +++++++
>  git-stash.sh                | 48 +++++++++++++++++++++++++++++++++++++++------
>  t/t3903-stash.sh            | 19 ++++++++++++++++++
>  3 files changed, 68 insertions(+), 6 deletions(-)
>
> diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
> index 08cdf2f..ec3a9a3 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>]
> +'git stash' store [-m|--message <message>] [-e|--error <message>] <commit>
>
>  DESCRIPTION
>  -----------
> @@ -153,6 +154,12 @@ create::
>         object name, without storing it anywhere in the ref namespace.
>         This is probably not what 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 probably not what you want to use; see
> +       "save" above.

Here, again, I think you should explain more about this command. Your commit message explained it pretty well.

  + Like create, store is intended to be useful for scripts.
Show 62 quoted lines
>
>  DISCUSSION
>  ----------
> diff --git a/git-stash.sh b/git-stash.sh
> index 64800b8..7985473 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -156,6 +156,43 @@ create_stash () {
>         die "$(gettext "Cannot record working tree state")"
>  }
>
> +store_stash () {
> +       while test $# != 0
> +       do
> +               case "$1" in
> +               -m|--message)
> +                       shift
> +                       stash_msg="$1"
> +                       ;;
> +               -e|--error)
> +                       shift
> +                       error_msg="$1"
> +                       ;;
> +               *)
> +                       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
> +       if test -z "$error_msg"
> +       then
> +               error_msg="Cannot update $ref_stash with $w_commit."
> +       fi
> +
> +       # Make sure the reflog for stash is kept.
> +       : >>"$GIT_DIR/logs/$ref_stash"
> +       git update-ref -m "$stash_msg" $ref_stash $w_commit ||
> +       die "$(eval_gettext "$error_msg")"
> +}
> +
>  save_stash () {
>         keep_index=
>         patch_mode=
> @@ -227,12 +264,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 -m "$stash_msg" -e "Cannot save the current status." $w_commit

nit: this adds a period to the end of the message which it did not have previously.

Show 44 quoted lines
>         say Saved working directory and index state "$stash_msg"
>
>         if test -z "$patch_mode"
> @@ -549,6 +581,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..75189ec 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -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
> --
> 1.8.3.1.383.g0d5ad6b
Looks clean to me, but I didn't test it.
Phil
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 8 of 13 in “Write a good 'git stash store' for autostash”
  1. 0/5 Write a good 'git stash store' for autostashRamkumar Ramachandra, Jun 14, 2013
  2. 1/5 stash doc: add a warning about using createRamkumar Ramachandra, Jun 14, 2013
  3. Phil HordJun 14, 2013
  4. Junio C HamanoJun 14, 2013
  5. 2/5 stash doc: document short form -p in synopsisRamkumar Ramachandra, Jun 14, 2013
  6. 3/5 stash: simplify option parser for createRamkumar Ramachandra, Jun 14, 2013
  7. 4/5 stash: introduce 'git stash store'Ramkumar Ramachandra, Jun 14, 2013
  8. Phil HordJun 14, 2013
  9. 5/5 rebase: use 'git stash store' to simplify logicRamkumar Ramachandra, Jun 14, 2013
  10. Phil HordJun 14, 2013
  11. Ramkumar RamachandraJun 14, 2013
  12. Junio C HamanoJun 14, 2013
  13. Ramkumar RamachandraJun 14, 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.