stash: keep untracked files intact in stash -k

Currently when there are untracked changes in a file "one" and in a file "two" in the repository and the user uses: git stash push -k one all changes in "two" are wiped out completely. That is clearly not the intended result. Make sure that only the files given in the pathspec are changed when git stash push -k <pathspec> is used. Reported-by: Jeff King <peff@peff.net> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Mar 21, 2017 at 22:12 UTC e0e7f99ea400808cd11af72425c721c8b44193ca
2 files changed +17 -1
git-stash.sh
+3 -1
@@ -314,7 +314,9 @@ push_stash () {
314
315 if test "$keep_index" = "t" && test -n "$i_tree"
316 then
317 - git read-tree --reset -u $i_tree
317 + git read-tree --reset $i_tree
318 + git ls-files -z --modified -- "$@" |
319 + git checkout-index -z --force --stdin
320 fi
321 else
322 git apply -R < "$TMP-patch" ||
t/t3903-stash.sh
+14
@@ -907,4 +907,18 @@ test_expect_success 'stash without verb with pathspec' '
907 test_path_is_file bar
908 '
909
910 +test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
911 + git reset &&
912 + >foo &&
913 + >bar &&
914 + git add foo bar &&
915 + git commit -m "test" &&
916 + echo "foo" >foo &&
917 + echo "bar" >bar &&
918 + git stash -k -- foo &&
919 + test "",bar = $(cat foo),$(cat bar) &&
920 + git stash pop &&
921 + test foo,bar = $(cat foo),$(cat bar)
922 +'
923 +
924 test_done