From ea41cfc4f54f884582dbda307287f12bb1fc15e9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 27 Jul 2009 20:37:10 +0200 Subject: Make 'git stash -k' a short form for 'git stash save --keep-index' To save me from the carpal tunnel syndrome, make 'git stash' accept the short option '-k' instead of '--keep-index', and for even more convenience, let's DWIM when this developer forgot to type the 'save' command. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-stash.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'git-stash.sh') diff --git a/git-stash.sh b/git-stash.sh index 03e589f764..13edc0eefd 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -7,7 +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 @@ -98,7 +99,7 @@ save_stash () { while test $# != 0 do case "$1" in - --keep-index) + -k|--keep-index) keep_index=t ;; -q|--quiet) @@ -353,12 +354,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 -- cgit v1.3-5-g9baa From dda1f2a5c3aca5072aada32eef159067ba16f0e9 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Thu, 13 Aug 2009 14:29:44 +0200 Subject: 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 Signed-off-by: Junio C Hamano --- Documentation/git-stash.txt | 14 ++++++-- git-add--interactive.perl | 14 ++++++-- git-stash.sh | 80 +++++++++++++++++++++++++++++++++++++-------- t/t3904-stash-patch.sh | 55 +++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 18 deletions(-) create mode 100755 t/t3904-stash-patch.sh (limited to 'git-stash.sh') diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt index 1c64a02fe5..4b15459c83 100644 --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@ -13,7 +13,7 @@ SYNOPSIS 'git stash' drop [-q|--quiet] [] 'git stash' ( pop | apply ) [--index] [-q|--quiet] [] 'git stash' branch [] -'git stash' [save [--keep-index] [-q|--quiet] []] +'git stash' [save [--patch] [--[no-]keep-index] [-q|--quiet] []] 'git stash' clear 'git stash' create @@ -42,7 +42,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,6 +51,16 @@ save [--keep-index] [-q|--quiet] []:: + 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 --git a/git-add--interactive.perl b/git-add--interactive.perl index 21746d5f28..8f66b825dd 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -76,6 +76,7 @@ my $patch_mode_revision; sub apply_patch; sub apply_patch_for_checkout_commit; +sub apply_patch_for_stash; my %patch_modes = ( 'stage' => { @@ -87,6 +88,15 @@ my %patch_modes = ( PARTICIPLE => 'staging', FILTER => 'file-only', }, + 'stash' => { + DIFF => 'diff-index -p HEAD', + APPLY => sub { apply_patch 'apply --cached', @_; }, + APPLY_CHECK => 'apply --cached', + VERB => 'Stash', + TARGET => '', + PARTICIPLE => 'stashing', + FILTER => undef, + }, 'reset_head' => { DIFF => 'diff-index -p --cached', APPLY => sub { apply_patch 'apply -R --cached', @_; }, @@ -1493,8 +1503,8 @@ sub process_args { 'checkout_head' : 'checkout_nothead'); $arg = shift @ARGV or die "missing --"; } - } elsif ($1 eq 'stage') { - $patch_mode = 'stage'; + } elsif ($1 eq 'stage' or $1 eq 'stash') { + $patch_mode = $1; $arg = shift @ARGV or die "missing --"; } else { die "unknown --patch mode: $1"; 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 } diff --git a/t/t3904-stash-patch.sh b/t/t3904-stash-patch.sh new file mode 100755 index 0000000000..f37e3bc6ec --- /dev/null +++ b/t/t3904-stash-patch.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +test_description='git checkout --patch' +. ./lib-patch-mode.sh + +test_expect_success 'setup' ' + mkdir dir && + echo parent > dir/foo && + echo dummy > bar && + git add bar dir/foo && + git commit -m initial && + test_tick && + test_commit second dir/foo head && + echo index > dir/foo && + git add dir/foo && + set_and_save_state bar bar_work bar_index && + save_head +' + +# note: bar sorts before dir, so the first 'n' is always to skip 'bar' + +test_expect_success 'saying "n" does nothing' ' + set_state dir/foo work index + (echo n; echo n) | test_must_fail git stash save -p && + verify_state dir/foo work index && + verify_saved_state bar +' + +test_expect_success 'git stash -p' ' + (echo n; echo y) | git stash save -p && + verify_state dir/foo head index && + verify_saved_state bar && + git reset --hard && + git stash apply && + verify_state dir/foo work head && + verify_state bar dummy dummy +' + +test_expect_success 'git stash -p --no-keep-index' ' + set_state dir/foo work index && + set_state bar bar_work bar_index && + (echo n; echo y) | git stash save -p --no-keep-index && + verify_state dir/foo head head && + verify_state bar bar_work dummy && + git reset --hard && + git stash apply --index && + verify_state dir/foo work index && + verify_state bar dummy bar_index +' + +test_expect_success 'none of this moved HEAD' ' + verify_saved_head +' + +test_done -- cgit v1.3-5-g9baa From f300fab5440f25aabb22c6d1fec411ee10e154b1 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Thu, 13 Aug 2009 14:29:45 +0200 Subject: DWIM 'git stash save -p' for 'git stash -p' Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- Documentation/git-stash.txt | 2 +- git-stash.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'git-stash.sh') diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt index 3dc16a108a..1c4ed412a1 100644 --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@ -14,7 +14,7 @@ SYNOPSIS 'git stash' ( pop | apply ) [--index] [-q|--quiet] [] 'git stash' branch [] 'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] []] -'git stash' [-k|--keep-index] +'git stash' [-p|--patch|-k|--keep-index] 'git stash' clear 'git stash' create diff --git a/git-stash.sh b/git-stash.sh index 81a72f68cc..9fd72894c4 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -406,8 +406,8 @@ branch) apply_to_branch "$@" ;; *) - case $#,"$1" in - 0,|1,-k|1,--keep-index) + case $#,"$1","$2" in + 0,,|1,-k,|1,--keep-index,|1,-p,|1,--patch,|2,-p,--no-keep-index|2,--patch,--no-keep-index) save_stash "$@" && say '(To restore them type "git stash apply")' ;; -- cgit v1.3-5-g9baa From 3c2eb80fe3f3c7efbb25e929df9f70d7c896a5ef Mon Sep 17 00:00:00 2001 From: Matthieu Moy Date: Tue, 18 Aug 2009 23:38:40 +0200 Subject: stash: simplify defaulting to "save" and reject unknown options With the earlier DWIM patches, certain combination of options defaulted to the "save" command correctly while certain equally valid combination did not. For example, "git stash -k" were Ok but "git stash -q -k" did not work. This makes the logic of defaulting to "save" much simpler. If there are no non-flag arguments, it is clear that there is no command word, and we default to "save" subcommand. This rule prevents "git stash -q apply" from quietly creating a stash with "apply" as the message. This also teaches "git stash save" to reject an unknown option. This is to keep a mistyped "git stash save --quite" from creating a stash with a message "--quite", and this safety is more important with the new logic to default to "save" with any option-looking argument without an explicit comand word. [jc: this is based on Matthieu's 3-patch series, and a follow-up discussion, and he and Peff take all the credit; if I have introduced bugs while reworking, they are mine.] Signed-off-by: Matthieu Moy Signed-off-by: Junio C Hamano --- Documentation/git-stash.txt | 9 +++++---- git-stash.sh | 27 +++++++++++++++++++++++---- t/t3903-stash.sh | 11 +++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) (limited to 'git-stash.sh') diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt index 1c4ed412a1..30a249cbc5 100644 --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@ -14,7 +14,6 @@ SYNOPSIS 'git stash' ( pop | apply ) [--index] [-q|--quiet] [] 'git stash' branch [] 'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] []] -'git stash' [-p|--patch|-k|--keep-index] 'git stash' clear 'git stash' create @@ -46,9 +45,11 @@ OPTIONS 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 - subcommand is given. The part is optional and gives - the description along with the stashed state. + --hard` to revert them. The part is optional and gives + the description along with the stashed state. For quickly making + a snapshot, you can omit _both_ "save" and , but giving + only does not trigger this action to prevent a misspelled + subcommand from making an unwanted stash. + If the `--keep-index` option is used, all changes already added to the index are left intact. diff --git a/git-stash.sh b/git-stash.sh index 9fd72894c4..f24337613b 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -8,7 +8,6 @@ USAGE="list [] or: $dashless ( pop | apply ) [--index] [-q|--quiet] [] or: $dashless branch [] or: $dashless [save [-k|--keep-index] [-q|--quiet] []] - or: $dashless [-k|--keep-index] or: $dashless clear" SUBDIRECTORY_OK=Yes @@ -146,6 +145,14 @@ save_stash () { -q|--quiet) GIT_QUIET=t ;; + --) + shift + break + ;; + -*) + echo "error: unknown option for 'stash save': $1" + usage + ;; *) break ;; @@ -355,6 +362,18 @@ apply_to_branch () { drop_stash $stash } +# The default command is "save" if nothing but options are given +seen_non_option= +for opt +do + case "$opt" in + -*) ;; + *) seen_non_option=t; break ;; + esac +done + +test -n "$seen_non_option" || set "save" "$@" + # Main command set case "$1" in list) @@ -406,9 +425,9 @@ branch) apply_to_branch "$@" ;; *) - case $#,"$1","$2" in - 0,,|1,-k,|1,--keep-index,|1,-p,|1,--patch,|2,-p,--no-keep-index|2,--patch,--no-keep-index) - save_stash "$@" && + case $# in + 0) + save_stash && say '(To restore them type "git stash apply")' ;; *) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index e16ad93d2c..5514f74b30 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -208,4 +208,15 @@ test_expect_success 'stash -k' ' test bar,bar4 = $(cat file),$(cat file2) ' +test_expect_success 'stash --invalid-option' ' + echo bar5 > file && + echo bar6 > file2 && + git add file2 && + test_must_fail git stash --invalid-option && + test_must_fail git stash save --invalid-option && + test bar5,bar6 = $(cat file),$(cat file2) && + git stash -- -message-starting-with-dash && + test bar,bar2 = $(cat file),$(cat file2) +' + test_done -- cgit v1.3-5-g9baa