]> asedeno.scripts.mit.edu Git - git.git/blobdiff - git-stash.sh
"git-merge": allow fast-forwarding in a stat-dirty tree
[git.git] / git-stash.sh
index 06cb177ec6c10d21f128390edc5bfedfa05db9a9..e15c12abc31c1e4d22bb3943d70a65ddf33abb53 100755 (executable)
@@ -1,7 +1,13 @@
 #!/bin/sh
 # Copyright (c) 2007, Nanako Shiraishi
 
-USAGE='[  | save | list | show | apply | clear | create ]'
+dashless=$(basename "$0" | sed -e 's/-/ /')
+USAGE="list [<options>]
+   or: $dashless (show | drop | pop ) [<stash>]
+   or: $dashless apply [--index] [<stash>]
+   or: $dashless branch <branchname> [<stash>]
+   or: $dashless [save [--keep-index] [<message>]]
+   or: $dashless clear"
 
 SUBDIRECTORY_OK=Yes
 OPTIONS_SPEC=
@@ -15,11 +21,15 @@ 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 () {
+       if test $# != 0
+       then
+               die "git stash clear with parameters is unimplemented"
+       fi
        if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
        then
                git update-ref -d $ref_stash $current
@@ -82,11 +92,18 @@ 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
-               echo >&2 'No local changes to save'
+               echo 'No local changes to save'
                exit 0
        fi
        test -f "$GIT_DIR/logs/$ref_stash" ||
@@ -99,7 +116,14 @@ save_stash () {
 
        git update-ref -m "$stash_msg" $ref_stash $w_commit ||
                die "Cannot save the current status"
-       printf >&2 'Saved working directory and index state "%s"\n' "$stash_msg"
+       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 () {
@@ -126,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=
@@ -149,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 &&
@@ -192,6 +217,45 @@ apply_stash () {
        fi
 }
 
+drop_stash () {
+       have_stash || die 'No stash entries to drop'
+
+       if test $# = 0
+       then
+               set x "$ref_stash@{0}"
+               shift
+       fi
+       # Verify supplied argument looks like a stash entry
+       s=$(git rev-parse --revs-only --no-flags "$@") &&
+       git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
+       git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
+       git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
+               die "$*: not a valid stashed state"
+
+       git reflog delete --updateref --rewrite "$@" &&
+               echo "Dropped $* ($s)" || die "$*: Could not drop stash entry"
+
+       # clear_stash if we just dropped the last stash entry
+       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)
@@ -209,14 +273,15 @@ show)
        ;;
 save)
        shift
-       save_stash "$*" && git-reset --hard
+       save_stash "$@"
        ;;
 apply)
        shift
        apply_stash "$@"
        ;;
 clear)
-       clear_stash
+       shift
+       clear_stash "$@"
        ;;
 create)
        if test $# -gt 0 && test "$1" = create
@@ -225,12 +290,27 @@ create)
        fi
        create_stash "$*" && echo "$w_commit"
        ;;
+drop)
+       shift
+       drop_stash "$@"
+       ;;
+pop)
+       shift
+       if apply_stash "$@"
+       then
+               test -z "$unstash_index" || shift
+               drop_stash "$@"
+       fi
+       ;;
+branch)
+       shift
+       apply_to_branch "$@"
+       ;;
 *)
        if test $# -eq 0
        then
                save_stash &&
-               echo >&2 '(To restore them type "git stash apply")' &&
-               git-reset --hard
+               echo '(To restore them type "git stash apply")'
        else
                usage
        fi