Code

Add script for importing bits-and-pieces to Git.
[git.git] / git-stash.sh
index 531c7c31aca87db77e54c46ffb89899652a79fcd..d61c9d03bc216cc2f1ab004fe3ffba42ef121a68 100755 (executable)
@@ -162,10 +162,6 @@ show_stash () {
 }
 
 apply_stash () {
-       git update-index -q --refresh &&
-       git diff-files --quiet --ignore-submodules ||
-               die 'Cannot apply to a dirty working tree, please stage your changes'
-
        unstash_index=
 
        while test $# != 0
@@ -184,18 +180,27 @@ apply_stash () {
                shift
        done
 
-       # current index state
-       c_tree=$(git write-tree) ||
-               die 'Cannot apply a stash in the middle of a merge'
+       if test $# = 0
+       then
+               have_stash || die 'Nothing to apply'
+       fi
 
        # stash records the work tree, and is a merge between the
        # base commit (first parent) and the index tree (second parent).
-       s=$(git rev-parse --verify --default $ref_stash "$@") &&
-       w_tree=$(git rev-parse --verify "$s:") &&
-       b_tree=$(git rev-parse --verify "$s^1:") &&
-       i_tree=$(git rev-parse --verify "$s^2:") ||
+       s=$(git rev-parse --quiet --verify --default $ref_stash "$@") &&
+       w_tree=$(git rev-parse --quiet --verify "$s:") &&
+       b_tree=$(git rev-parse --quiet --verify "$s^1:") &&
+       i_tree=$(git rev-parse --quiet --verify "$s^2:") ||
                die "$*: no valid stashed state found"
 
+       git update-index -q --refresh &&
+       git diff-files --quiet --ignore-submodules ||
+               die 'Cannot apply to a dirty working tree, please stage your changes'
+
+       # current index state
+       c_tree=$(git write-tree) ||
+               die 'Cannot apply a stash in the middle of a merge'
+
        unstashed_index_tree=
        if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
                        test "$c_tree" != "$i_tree"
@@ -203,7 +208,7 @@ apply_stash () {
                git diff-tree --binary $s^2^..$s^2 | git apply --cached
                test $? -ne 0 &&
                        die 'Conflicts in index. Try without --index.'
-               unstashed_index_tree=$(git-write-tree) ||
+               unstashed_index_tree=$(git write-tree) ||
                        die 'Could not save index tree'
                git reset
        fi
@@ -219,7 +224,7 @@ apply_stash () {
        then
                export GIT_MERGE_VERBOSITY=0
        fi
-       if git-merge-recursive $b_tree -- $c_tree $w_tree
+       if git merge-recursive $b_tree -- $c_tree $w_tree
        then
                # No conflict
                if test -n "$unstashed_index_tree"
@@ -297,7 +302,7 @@ apply_to_branch () {
        fi
        stash=$2
 
-       git-checkout -b $branch $stash^ &&
+       git checkout -b $branch $stash^ &&
        apply_stash --index $stash &&
        drop_stash $stash
 }