Merge branch 'js/stash-dwim' into tr/reset-checkout-patch
authorJunio C Hamano <gitster@pobox.com>
Sat, 15 Aug 2009 22:20:28 +0000 (15:20 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 15 Aug 2009 22:20:28 +0000 (15:20 -0700)
* commit 'tr/reset-checkout-patch^^2':
Make 'git stash -k' a short form for 'git stash save --keep-index'

1  2 
Documentation/git-stash.txt
git-stash.sh
index 4b15459c8374c1a83d4ffa9e0fd1a984aada7621,a031836a26c562deb490819f9a7fe7e0c0034e9f..3dc16a108a15bdb4ea7a86d30dbaa6822969229a
@@@ -13,7 -13,8 +13,8 @@@ SYNOPSI
  'git stash' drop [-q|--quiet] [<stash>]
  'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
  'git stash' branch <branchname> [<stash>]
- 'git stash' [save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]]
 -'git stash' [save [-k|--keep-index] [-q|--quiet] [<message>]]
++'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]
+ '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] [<message>]::
 +save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]::
  
        Save your local modifications to a new 'stash', and run `git reset
        --hard` to revert them.  This is the default action when no
  +
  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 [<options>]::
  
diff --combined git-stash.sh
index 567aa5d725e38750b860b01a43ba01914e180ea1,13edc0eefd56507930055f57fa48c3bfb6863108..81a72f68cc47aed881b618f893d49c776fe52ae8
@@@ -7,7 -7,8 +7,8 @@@ USAGE="list [<options>
     or: $dashless drop [-q|--quiet] [<stash>]
     or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
     or: $dashless branch <branchname> [<stash>]
-    or: $dashless [save [--keep-index] [-q|--quiet] [<message>]]
+    or: $dashless [save [-k|--keep-index] [-q|--quiet] [<message>]]
+    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
  
  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
                        ;;
                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