Re: [PATCH v3 1/2] stash push: avoid printing errors

2018-03-16 Thread Junio C Hamano
Thomas Gummerer  writes:

> @@ -322,9 +322,12 @@ push_stash () {
>  
>   if test $# != 0
>   then
> - git add -u -- "$@" |
> - git checkout-index -z --force --stdin

This obviously is not something this patch breaks, but I am finding
this pipeline that was already here quite puzzling.

The "add -u" is about adding the changes in paths that match the
pathspec to the index; the output from it is meant for human
consumption and certainly is not something "--stdin" should expect
to be understandable, let alone with "-z".

... goes and digs ...

I think you mis-copied the suggestion in

https://public-inbox.org/git/xmqqpo7byjwb@gitster.mtv.corp.google.com/

when you made bba067d2 ("stash: don't delete untracked files that
match pathspec", 2018-01-06), and nobody caught that breakage during
the review.

> - git diff-index -p --cached --binary HEAD -- "$@" | git 
> apply --index -R
> + if git ls-files --error-unmatch -- "$@" >/dev/null 
> 2>/dev/null
> + then
> + git add -u -- "$@" |
> + git checkout-index -z --force --stdin

And the same breakage is inherited here; just drop "|" and
downstream "checkout-index" and you should be OK.

> + git diff-index -p --cached --binary HEAD -- 
> "$@" | git apply --index -R

And while at it, let's split this to two lines after "|".

> + fi



[PATCH v3 1/2] stash push: avoid printing errors

2018-03-16 Thread Thomas Gummerer
Currently 'git stash push -u -- ' prints the following errors
if  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
', and then use a command chain involving 'git add -u
' and 'git apply' to clear the changes to files that are in
the index and were stashed.

As the  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 making sure to only call this command chain if there are
still files that match  after the call to 'git clean'.

Reported-by: Marc Strapetz 
Signed-off-by: Thomas Gummerer 
---
 git-stash.sh |  9 ++---
 t/t3903-stash.sh | 16 
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/git-stash.sh b/git-stash.sh
index fc8f8ae640..4de9f9bea8 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -322,9 +322,12 @@ push_stash () {
 
if test $# != 0
then
-   git add -u -- "$@" |
-   git checkout-index -z --force --stdin
-   git diff-index -p --cached --binary HEAD -- "$@" | git 
apply --index -R
+   if git ls-files --error-unmatch -- "$@" >/dev/null 
2>/dev/null
+   then
+   git add -u -- "$@" |
+   git checkout-index -z --force --stdin
+   git diff-index -p --cached --binary HEAD -- 
"$@" | git apply --index -R
+   fi
else
git reset --hard -q
fi
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index aefde7b172..8b1a8d2982 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1096,4 +1096,20 @@ test_expect_success 'stash --  works with binary 
files' '
test_path_is_file subdir/untracked
 '
 
+test_expect_success 'stash -u --  doesnt print error' '
+   >untracked &&
+   git stash push -u -- untracked 2>actual &&
+   test_path_is_missing untracked &&
+   test_line_count = 0 actual
+'
+
+test_expect_success 'stash -u --  leaves rest of working tree in 
place' '
+   >tracked &&
+   git add tracked &&
+   >untracked &&
+   git stash push -u -- untracked &&
+   test_path_is_missing untracked &&
+   test_path_is_file tracked
+'
+
 test_done
-- 
2.16.2.804.g6dcf76e11