Skip to content

Commit 833622a

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d97e4fa commit 833622a

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

git-stash.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,16 @@ push_stash () {
308308
if test -z "$patch_mode"
309309
then
310310
test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
311-
if test -n "$untracked"
311+
if test -n "$untracked" && test $# = 0
312312
then
313313
git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
314314
fi
315315

316316
if test $# != 0
317317
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 -- "$@"
319321
git diff-index -p --cached --binary HEAD -- "$@" |
320322
git apply --index -R
321323
else

t/t3905-stash-include-untracked.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,50 @@ test_expect_success 'stash previously ignored file' '
228228
test_path_is_file ignored.d/foo
229229
'
230230

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+
231277
test_done

0 commit comments

Comments
 (0)