#!/bin/sh
# Copyright (c) 2007, Nanako Shiraishi
-USAGE='[ | save | list | show | apply | clear | drop | pop | 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=
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 () {
create_stash () {
stash_msg="$1"
+ git update-index -q --refresh
if no_changes
then
exit 0
}
save_stash () {
- stash_msg="$1"
+ keep_index=
+ case "$1" in
+ --keep-index)
+ keep_index=t
+ shift
+ esac
+
+ stash_msg="$*"
+ git update-index -q --refresh
if no_changes
then
echo 'No local changes to save'
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 () {
then
flags=--stat
fi
- s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@")
- w_commit=$(git rev-parse --verify "$s") &&
- b_commit=$(git rev-parse --verify "$s^") &&
+ w_commit=$(git rev-parse --verify --default $ref_stash "$@") &&
+ b_commit=$(git rev-parse --verify "$w_commit^") &&
git diff $flags $b_commit $w_commit
}
apply_stash () {
- git diff-files --quiet ||
- die 'Cannot restore on top of a dirty state'
+ 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=
case "$1" in
# 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 --revs-only --no-flags --default $ref_stash "$@") &&
+ 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:") ||
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 &&
shift
fi
# Verify supplied argument looks like a stash entry
- s=$(git rev-parse --revs-only --no-flags "$@") &&
+ s=$(git rev-parse --verify "$@") &&
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 ||
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)
;;
save)
shift
- save_stash "$*" && git-reset --hard
+ save_stash "$@"
;;
apply)
shift
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