summaryrefslogtreecommitdiff
path: root/git-stash.sh
diff options
context:
space:
mode:
Diffstat (limited to 'git-stash.sh')
-rwxr-xr-xgit-stash.sh80
1 files changed, 66 insertions, 14 deletions
diff --git a/git-stash.sh b/git-stash.sh
index 03e589f..567aa5d 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -21,6 +21,14 @@ trap 'rm -f "$TMP-*"' 0
ref_stash=refs/stash
+if git config --get-colorbool color.interactive; then
+ help_color="$(git config --get-color color.interactive.help 'red bold')"
+ reset_color="$(git config --get-color '' reset)"
+else
+ help_color=
+ reset_color=
+fi
+
no_changes () {
git diff-index --quiet --cached HEAD --ignore-submodules -- &&
git diff-files --quiet --ignore-submodules
@@ -68,19 +76,44 @@ create_stash () {
git commit-tree $i_tree -p $b_commit) ||
die "Cannot save the current index state"
- # state of the working tree
- w_tree=$( (
+ if test -z "$patch_mode"
+ then
+
+ # state of the working tree
+ w_tree=$( (
+ rm -f "$TMP-index" &&
+ cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
+ GIT_INDEX_FILE="$TMP-index" &&
+ export GIT_INDEX_FILE &&
+ git read-tree -m $i_tree &&
+ git add -u &&
+ git write-tree &&
+ rm -f "$TMP-index"
+ ) ) ||
+ die "Cannot save the current worktree state"
+
+ else
+
rm -f "$TMP-index" &&
- cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
- GIT_INDEX_FILE="$TMP-index" &&
- export GIT_INDEX_FILE &&
- git read-tree -m $i_tree &&
- git add -u &&
- git write-tree &&
- rm -f "$TMP-index"
- ) ) ||
+ GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
+
+ # find out what the user wants
+ GIT_INDEX_FILE="$TMP-index" \
+ git add--interactive --patch=stash -- &&
+
+ # state of the working tree
+ w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
die "Cannot save the current worktree state"
+ git diff-tree -p HEAD $w_tree > "$TMP-patch" &&
+ test -s "$TMP-patch" ||
+ die "No changes selected"
+
+ rm -f "$TMP-index" ||
+ die "Cannot remove temporary index (can't happen)"
+
+ fi
+
# create the stash
if test -z "$stash_msg"
then
@@ -95,12 +128,20 @@ create_stash () {
save_stash () {
keep_index=
+ patch_mode=
while test $# != 0
do
case "$1" in
--keep-index)
keep_index=t
;;
+ --no-keep-index)
+ keep_index=
+ ;;
+ -p|--patch)
+ patch_mode=t
+ keep_index=t
+ ;;
-q|--quiet)
GIT_QUIET=t
;;
@@ -131,11 +172,22 @@ save_stash () {
die "Cannot save the current status"
say Saved working directory and index state "$stash_msg"
- git reset --hard ${GIT_QUIET:+-q}
-
- if test -n "$keep_index" && test -n $i_tree
+ if test -z "$patch_mode"
then
- git read-tree --reset -u $i_tree
+ git reset --hard ${GIT_QUIET:+-q}
+
+ if test -n "$keep_index" && test -n $i_tree
+ then
+ git read-tree --reset -u $i_tree
+ fi
+ else
+ git apply -R < "$TMP-patch" ||
+ die "Cannot remove worktree changes"
+
+ if test -z "$keep_index"
+ then
+ git reset
+ fi
fi
}