diff options
author | Thomas Rast <trast@student.ethz.ch> | 2009-08-13 14:29:44 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-08-15 15:19:31 -0700 |
commit | dda1f2a5c3aca5072aada32eef159067ba16f0e9 (patch) | |
tree | 02fc359424824fd005e183044e2bc2a9d14df930 /git-stash.sh | |
parent | Implement 'git checkout --patch' (diff) | |
download | tgif-dda1f2a5c3aca5072aada32eef159067ba16f0e9.tar.xz |
Implement 'git stash save --patch'
This adds a hunk-based mode to git-stash. You can select hunks from
the difference between HEAD and worktree, and git-stash will build a
stash that reflects these changes. The index state of the stash is
the same as your current index, and we also let --patch imply
--keep-index.
Note that because the selected hunks are rolled back from the worktree
but not the index, the resulting state may appear somewhat confusing
if you had also staged these changes. This is not entirely
satisfactory, but due to the way stashes are applied, other solutions
would require a change to the stash format.
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git-stash.sh')
-rwxr-xr-x | git-stash.sh | 80 |
1 files changed, 66 insertions, 14 deletions
diff --git a/git-stash.sh b/git-stash.sh index 03e589f764..567aa5d725 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 } |