stash push: avoid printing errors

'git stash push -u -- <pathspec>' prints the following errors if <pathspec> only matches untracked files: fatal: pathspec 'untracked' did not match any files error: unrecognized input This is because we first clean up the untracked files using 'git clean <pathspec>', and then use a command chain involving 'git add -u <pathspec>' and 'git apply' to clear the changes to files that are in the index and were stashed. As the <pathspec> only includes untracked files that were already removed by 'git clean', the 'git add' call will barf, and so will 'git apply', as there are no changes that need to be applied. Fix this by avoiding the 'git clean' if a pathspec is given, and use the pipeline that's used for pathspec mode to get rid of the untracked files as well. Reported-by: Marc Strapetz <marc.strapetz@syntevo.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Mar 19, 2018 at 23:21 UTC 833622a945a677ec690e5ffb05a637e425591f1d
2 files changed +50 -2
git-stash.sh
+4 -2
@@ -308,14 +308,16 @@ push_stash () {
308 if test -z "$patch_mode"
309 then
310 test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
311 - if test -n "$untracked"
311 + if test -n "$untracked" && test $# = 0
312 then
313 git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
314 fi
315
316 if test $# != 0
317 then
318 - git add -u -- "$@"
318 + test -z "$untracked" && UPDATE_OPTION="-u" || UPDATE_OPTION=
319 + test "$untracked" = "all" && FORCE_OPTION="--force" || FORCE_OPTION=
320 + git add $UPDATE_OPTION $FORCE_OPTION -- "$@"
321 git diff-index -p --cached --binary HEAD -- "$@" |
322 git apply --index -R
323 else
t/t3905-stash-include-untracked.sh
+46
@@ -228,4 +228,50 @@ test_expect_success 'stash previously ignored file' '
228 test_path_is_file ignored.d/foo
229 '
230
231 +test_expect_success 'stash -u -- <untracked> doesnt print error' '
232 + >untracked &&
233 + git stash push -u -- untracked 2>actual &&
234 + test_path_is_missing untracked &&
235 + test_line_count = 0 actual
236 +'
237 +
238 +test_expect_success 'stash -u -- <untracked> leaves rest of working tree in place' '
239 + >tracked &&
240 + git add tracked &&
241 + >untracked &&
242 + git stash push -u -- untracked &&
243 + test_path_is_missing untracked &&
244 + test_path_is_file tracked
245 +'
246 +
247 +test_expect_success 'stash -u -- <tracked> <untracked> clears changes in both' '
248 + >tracked &&
249 + git add tracked &&
250 + >untracked &&
251 + git stash push -u -- tracked untracked &&
252 + test_path_is_missing tracked &&
253 + test_path_is_missing untracked
254 +'
255 +
256 +test_expect_success 'stash --all -- <ignored> stashes ignored file' '
257 + >ignored.d/bar &&
258 + git stash push --all -- ignored.d/bar &&
259 + test_path_is_missing ignored.d/bar
260 +'
261 +
262 +test_expect_success 'stash --all -- <tracked> <ignored> clears changes in both' '
263 + >tracked &&
264 + git add tracked &&
265 + >ignored.d/bar &&
266 + git stash push --all -- tracked ignored.d/bar &&
267 + test_path_is_missing tracked &&
268 + test_path_is_missing ignored.d/bar
269 +'
270 +
271 +test_expect_success 'stash -u -- <ignored> leaves ignored file alone' '
272 + >ignored.d/bar &&
273 + git stash push -u -- ignored.d/bar &&
274 + test_path_is_file ignored.d/bar
275 +'
276 +
277 test_done