stash: clean untracked files before reset

If calling git stash -u on a repo that contains a file that is not ignored any more due to a current modification of the gitignore file, this file is stashed but not remove from the working tree. This is due to git-stash first doing a reset --hard which clears the .gitignore file modification and the call git clean, leaving the file untouched. This causes git stash pop to fail due to the file existing. This patch simply switches the order between cleaning and resetting and adds a test for this usecase. Reported-by: Sam Partington <sam@whiteoctober.co.uk> Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Morey-Chaisemartin committed Aug 11, 2017 at 19:14 UTC bbffd87d3223b2f5782918eab2e011931bdffdcc
2 files changed +23 -5
git-stash.sh
+6 -5
@@ -300,6 +300,12 @@ push_stash () {
300
301 if test -z "$patch_mode"
302 then
303 + test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
304 + if test -n "$untracked"
305 + then
306 + git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
307 + fi
308 +
309 if test $# != 0
310 then
311 git reset -q -- "$@"
@@ -309,11 +315,6 @@ push_stash () {
315 else
316 git reset --hard -q
317 fi
312 - test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
313 - if test -n "$untracked"
314 - then
315 - git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
316 - fi
318
319 if test "$keep_index" = "t" && test -n "$i_tree"
320 then
t/t3905-stash-include-untracked.sh
+17
@@ -211,4 +211,21 @@ test_expect_success 'stash push with $IFS character' '
211 test_path_is_file bar
212 '
213
214 +cat > .gitignore <<EOF
215 +ignored
216 +ignored.d/*
217 +EOF
218 +
219 +test_expect_success 'stash previously ignored file' '
220 + git reset HEAD &&
221 + git add .gitignore &&
222 + git commit -m "Add .gitignore" &&
223 + >ignored.d/foo &&
224 + echo "!ignored.d/foo" >> .gitignore &&
225 + git stash save --include-untracked &&
226 + test_path_is_missing ignored.d/foo &&
227 + git stash pop &&
228 + test_path_is_file ignored.d/foo
229 +'
230 +
231 test_done