X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=git-stash.sh;h=e15c12abc31c1e4d22bb3943d70a65ddf33abb53;hb=9ca8f6079cdb199851636c719900472a9745885f;hp=c2b68205a2b69eb91b2403e11238093fc65c04ef;hpb=998b912927281a871e8d379e2b3a4385c775c4fe;p=git.git diff --git a/git-stash.sh b/git-stash.sh index c2b68205a..e15c12abc 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -1,7 +1,13 @@ #!/bin/sh # Copyright (c) 2007, Nanako Shiraishi -USAGE='[ | save | list | show | apply | clear | drop | pop | create ]' +dashless=$(basename "$0" | sed -e 's/-/ /') +USAGE="list [] + or: $dashless (show | drop | pop ) [] + or: $dashless apply [--index] [] + or: $dashless branch [] + or: $dashless [save [--keep-index] []] + or: $dashless clear" SUBDIRECTORY_OK=Yes OPTIONS_SPEC= @@ -15,8 +21,8 @@ trap 'rm -f "$TMP-*"' 0 ref_stash=refs/stash no_changes () { - git diff-index --quiet --cached HEAD -- && - git diff-files --quiet + git diff-index --quiet --cached HEAD --ignore-submodules -- && + git diff-files --quiet --ignore-submodules } clear_stash () { @@ -86,7 +92,14 @@ create_stash () { } save_stash () { - stash_msg="$1" + keep_index= + case "$1" in + --keep-index) + keep_index=t + shift + esac + + stash_msg="$*" if no_changes then @@ -104,6 +117,13 @@ save_stash () { git update-ref -m "$stash_msg" $ref_stash $w_commit || die "Cannot save the current status" printf 'Saved working directory and index state "%s"\n' "$stash_msg" + + git reset --hard + + if test -n "$keep_index" && test -n $i_tree + then + git read-tree --reset -u $i_tree + fi } have_stash () { @@ -130,7 +150,7 @@ show_stash () { } apply_stash () { - git diff-files --quiet || + git diff-files --quiet --ignore-submodules || die 'Cannot restore on top of a dirty state' unstash_index= @@ -153,7 +173,8 @@ apply_stash () { die "$*: no valid stashed state found" unstashed_index_tree= - if test -n "$unstash_index" && test "$b_tree" != "$i_tree" + if test -n "$unstash_index" && test "$b_tree" != "$i_tree" && + test "$c_tree" != "$i_tree" then git diff-tree --binary $s^2^..$s^2 | git apply --cached test $? -ne 0 && @@ -218,6 +239,23 @@ drop_stash () { git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash } +apply_to_branch () { + have_stash || die 'Nothing to apply' + + test -n "$1" || die 'No branch name specified' + branch=$1 + + if test -z "$2" + then + set x "$ref_stash@{0}" + fi + stash=$2 + + git-checkout -b $branch $stash^ && + apply_stash --index $stash && + drop_stash $stash +} + # Main command set case "$1" in list) @@ -235,7 +273,7 @@ show) ;; save) shift - save_stash "$*" && git-reset --hard + save_stash "$@" ;; apply) shift @@ -264,12 +302,15 @@ pop) drop_stash "$@" fi ;; +branch) + shift + apply_to_branch "$@" + ;; *) if test $# -eq 0 then save_stash && - echo '(To restore them type "git stash apply")' && - git-reset --hard + echo '(To restore them type "git stash apply")' else usage fi