stash: don't delete untracked files that match pathspec

Currently when 'git stash push -- <pathspec>' is used, untracked files that match the pathspec will be deleted, even though they do not end up in a stash anywhere. This is because the original commit introducing the pathspec feature in git stash push (df6bba0937 ("stash: teach 'push' (and 'create_stash') to honor pathspec", 2017-02-28)) used the sequence of 'git reset <pathspec> && git ls-files --modified <pathspec> | git checkout-index && git clean <pathspec>'. The intention was to emulate what 'git reset --hard -- <pathspec>' would do. The call to 'git clean' was supposed to clean up the files that were unstaged by 'git reset'. This would work fine if the pathspec doesn't match any files that were untracked before 'git stash push -- <pathspec>'. However if <pathspec> matches a file that was untracked before invoking the 'stash' command, all untracked files matching the pathspec would inadvertently be deleted as well, even though they wouldn't end up in the stash, and are therefore lost. This behaviour was never what was intended, only blobs that also end up in the stash should be reset to their state in HEAD, previously untracked files should be left alone. To achieve this, first match what's in the index and what's in the working tree by adding all changes to the index, ask diff-index what changed between HEAD and the current index, and then apply that patch in reverse to get rid of the changes, which includes removal of added files and resurrection of removed files. Reported-by: Reid Price <reid.price@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Jan 6, 2018 at 00:24 UTC bba067d2faf047597bc76f885fb0cf87894b5ed1
2 files changed +34 -3
git-stash.sh
+2 -3
@@ -315,10 +315,9 @@ push_stash () {
315
316 if test $# != 0
317 then
318 - git reset -q -- "$@"
319 - git ls-files -z --modified -- "$@" |
318 + git add -u -- "$@" |
319 git checkout-index -z --force --stdin
321 - git clean --force -q -d -- "$@"
320 + git diff-index -p --cached --binary HEAD -- "$@" | git apply --index -R
321 else
322 git reset --hard -q
323 fi
t/t3903-stash.sh
+32
@@ -971,4 +971,36 @@ test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
971 test foo,bar = $(cat foo),$(cat bar)
972 '
973
974 +test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
975 + git reset &&
976 + >subdir/untracked &&
977 + >subdir/tracked1 &&
978 + >subdir/tracked2 &&
979 + git add subdir/tracked* &&
980 + git stash -- subdir/ &&
981 + test_path_is_missing subdir/tracked1 &&
982 + test_path_is_missing subdir/tracked2 &&
983 + test_path_is_file subdir/untracked &&
984 + git stash pop &&
985 + test_path_is_file subdir/tracked1 &&
986 + test_path_is_file subdir/tracked2 &&
987 + test_path_is_file subdir/untracked
988 +'
989 +
990 +test_expect_success 'stash -- <subdir> works with binary files' '
991 + git reset &&
992 + >subdir/untracked &&
993 + >subdir/tracked &&
994 + cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
995 + git add subdir/tracked* &&
996 + git stash -- subdir/ &&
997 + test_path_is_missing subdir/tracked &&
998 + test_path_is_missing subdir/tracked-binary &&
999 + test_path_is_file subdir/untracked &&
1000 + git stash pop &&
1001 + test_path_is_file subdir/tracked &&
1002 + test_path_is_file subdir/tracked-binary &&
1003 + test_path_is_file subdir/untracked
1004 +'
1005 +
1006 test_done