From: Junio C Hamano Date: Sat, 15 Aug 2009 22:20:28 +0000 (-0700) Subject: Merge branch 'js/stash-dwim' into tr/reset-checkout-patch X-Git-Tag: v1.6.5-rc0~5^2~4 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/367ea191e659da05a2e79a7025d8628a02f8268a?ds=inline;hp=-c Merge branch 'js/stash-dwim' into tr/reset-checkout-patch * commit 'tr/reset-checkout-patch^^2': Make 'git stash -k' a short form for 'git stash save --keep-index' --- 367ea191e659da05a2e79a7025d8628a02f8268a diff --combined Documentation/git-stash.txt index 4b15459c83,a031836a26..3dc16a108a --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@@ -13,7 -13,8 +13,8 @@@ SYNOPSI 'git stash' drop [-q|--quiet] [] 'git stash' ( pop | apply ) [--index] [-q|--quiet] [] 'git stash' branch [] - 'git stash' [save [--patch] [--[no-]keep-index] [-q|--quiet] []] -'git stash' [save [-k|--keep-index] [-q|--quiet] []] ++'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] []] + 'git stash' [-k|--keep-index] 'git stash' clear 'git stash' create @@@ -42,7 -43,7 +43,7 @@@ is also possible) OPTIONS ------- -save [--keep-index] [-q|--quiet] []:: +save [--patch] [--[no-]keep-index] [-q|--quiet] []:: Save your local modifications to a new 'stash', and run `git reset --hard` to revert them. This is the default action when no @@@ -51,16 -52,6 +52,16 @@@ + If the `--keep-index` option is used, all changes already added to the index are left intact. ++ +With `--patch`, you can interactively select hunks from in the diff +between HEAD and the working tree to be stashed. The stash entry is +constructed such that its index state is the same as the index state +of your repository, and its worktree contains only the changes you +selected interactively. The selected changes are then rolled back +from your worktree. ++ +The `--patch` option implies `--keep-index`. You can use +`--no-keep-index` to override this. list []:: diff --combined git-stash.sh index 567aa5d725,13edc0eefd..81a72f68cc --- a/git-stash.sh +++ b/git-stash.sh @@@ -7,7 -7,8 +7,8 @@@ USAGE="list [ or: $dashless drop [-q|--quiet] [] or: $dashless ( pop | apply ) [--index] [-q|--quiet] [] or: $dashless branch [] - or: $dashless [save [--keep-index] [-q|--quiet] []] + or: $dashless [save [-k|--keep-index] [-q|--quiet] []] + or: $dashless [-k|--keep-index] or: $dashless clear" SUBDIRECTORY_OK=Yes @@@ -21,14 -22,6 +22,14 @@@ trap 'rm -f "$TMP-*"' 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 @@@ -76,44 -69,19 +77,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 @@@ -128,20 -96,12 +129,20 @@@ save_stash () { keep_index= + patch_mode= while test $# != 0 do case "$1" in - --keep-index) + -k|--keep-index) keep_index=t ;; + --no-keep-index) + keep_index= + ;; + -p|--patch) + patch_mode=t + keep_index=t + ;; -q|--quiet) GIT_QUIET=t ;; @@@ -172,22 -132,11 +173,22 @@@ 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 } @@@ -405,12 -354,13 +406,13 @@@ branch apply_to_branch "$@" ;; *) - if test $# -eq 0 - then - save_stash && + case $#,"$1" in + 0,|1,-k|1,--keep-index) + save_stash "$@" && say '(To restore them type "git stash apply")' - else + ;; + *) usage - fi + esac ;; esac