From 21ec98a75d9cecaeb1493462da593343441661b6 Mon Sep 17 00:00:00 2001
From: Dan McGee <dpmcgee@gmail.com>
Date: Thu, 7 Apr 2011 12:04:19 -0500
Subject: [PATCH 1/2] stash: add two more tests for --no-keep-index

One of these passes just fine; the other one exposes a problem where
command line flag order matters for --no-keep-index and --patch
interaction.

Signed-off-by: Dan McGee <dpmcgee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t3903-stash.sh       |  8 ++++++++
 t/t3904-stash-patch.sh | 12 ++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index f62aaf5816f..bca2df15702 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -218,6 +218,14 @@ test_expect_success 'stash -k' '
 	test bar,bar4 = $(cat file),$(cat file2)
 '
 
+test_expect_success 'stash --no-keep-index' '
+	echo bar33 > file &&
+	echo bar44 > file2 &&
+	git add file2 &&
+	git stash --no-keep-index &&
+	test bar,bar2 = $(cat file),$(cat file2)
+'
+
 test_expect_success 'stash --invalid-option' '
 	echo bar5 > file &&
 	echo bar6 > file2 &&
diff --git a/t/t3904-stash-patch.sh b/t/t3904-stash-patch.sh
index 1e7193ac0bc..4d5878950be 100755
--- a/t/t3904-stash-patch.sh
+++ b/t/t3904-stash-patch.sh
@@ -48,6 +48,18 @@ test_expect_success PERL 'git stash -p --no-keep-index' '
 	verify_state bar dummy bar_index
 '
 
+test_expect_failure PERL 'git stash --no-keep-index -p' '
+	set_state dir/foo work index &&
+	set_state bar bar_work bar_index &&
+	(echo n; echo y) | git stash save --no-keep-index -p &&
+	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 PERL 'none of this moved HEAD' '
 	verify_saved_head
 '

From 3a243f7024f21c2c2205cd861f3bb756b0ec43a0 Mon Sep 17 00:00:00 2001
From: Dan McGee <dpmcgee@gmail.com>
Date: Thu, 7 Apr 2011 12:04:20 -0500
Subject: [PATCH 2/2] stash: ensure --no-keep-index and --patch can be used in
 any order

Don't assume one comes after the other on the command line. Use a
three-state variable to track and check its value accordingly.

Signed-off-by: Dan McGee <dpmcgee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 git-stash.sh           | 9 +++++----
 t/t3904-stash-patch.sh | 2 +-
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/git-stash.sh b/git-stash.sh
index a305fb19f11..fd668840906 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -136,11 +136,12 @@ save_stash () {
 			keep_index=t
 			;;
 		--no-keep-index)
-			keep_index=
+			keep_index=n
 			;;
 		-p|--patch)
 			patch_mode=t
-			keep_index=t
+			# only default to keep if we don't already have an override
+			test -z "$keep_index" && keep_index=t
 			;;
 		-q|--quiet)
 			GIT_QUIET=t
@@ -185,7 +186,7 @@ save_stash () {
 	then
 		git reset --hard ${GIT_QUIET:+-q}
 
-		if test -n "$keep_index" && test -n $i_tree
+		if test "$keep_index" = "t" && test -n $i_tree
 		then
 			git read-tree --reset -u $i_tree
 		fi
@@ -193,7 +194,7 @@ save_stash () {
 		git apply -R < "$TMP-patch" ||
 		die "Cannot remove worktree changes"
 
-		if test -z "$keep_index"
+		if test "$keep_index" != "t"
 		then
 			git reset
 		fi
diff --git a/t/t3904-stash-patch.sh b/t/t3904-stash-patch.sh
index 4d5878950be..781fd716815 100755
--- a/t/t3904-stash-patch.sh
+++ b/t/t3904-stash-patch.sh
@@ -48,7 +48,7 @@ test_expect_success PERL 'git stash -p --no-keep-index' '
 	verify_state bar dummy bar_index
 '
 
-test_expect_failure PERL 'git stash --no-keep-index -p' '
+test_expect_success PERL 'git stash --no-keep-index -p' '
 	set_state dir/foo work index &&
 	set_state bar bar_work bar_index &&
 	(echo n; echo y) | git stash save --no-keep-index -p &&