git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH/RFC] git-grep: correct exit code with --quiet and -L
@ 2017-08-15 20:35 Anthony Sottile
  2017-08-15 21:33 ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Anthony Sottile @ 2017-08-15 20:35 UTC (permalink / raw)
  To: git; +Cc: Anthony Sottile

The handling of `status_only` no longer interferes with the handling of
`unmatch_name_only`.  `--quiet` no longer affects the exit code when using
`-L`/`--files-without-match`.

Signed-off-by: Anthony Sottile <asottile@umich.edu>
---
 grep.c          | 9 +++++----
 t/t7810-grep.sh | 5 +++++
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/grep.c b/grep.c
index 2efec0e..a893d09 100644
--- a/grep.c
+++ b/grep.c
@@ -1755,7 +1755,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
 		}
 		if (hit) {
 			count++;
-			if (opt->status_only)
+			if (!opt->unmatch_name_only && opt->status_only)
 				return 1;
 			if (opt->name_only) {
 				show_name(opt, gs->name);
@@ -1820,13 +1820,14 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
 	if (collect_hits)
 		return 0;
 
-	if (opt->status_only)
-		return 0;
 	if (opt->unmatch_name_only) {
 		/* We did not see any hit, so we want to show this */
-		show_name(opt, gs->name);
+		if (!opt->status_only)
+			show_name(opt, gs->name);
 		return 1;
 	}
+	if (opt->status_only)
+		return 0;
 
 	xdiff_clear_find_func(&xecfg);
 	opt->priv = NULL;
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index f106387..2a6679c 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -374,6 +374,11 @@ test_expect_success 'grep -L -C' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep --files-without-match --quiet' '
+	git grep --files-without-match --quiet nonexistent_string >actual &&
+	test_cmp /dev/null actual
+'
+
 cat >expected <<EOF
 file:foo mmap bar_mmap
 EOF
-- 
2.14.GIT


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH/RFC] git-grep: correct exit code with --quiet and -L
  2017-08-15 20:35 Anthony Sottile
@ 2017-08-15 21:33 ` Junio C Hamano
  2017-08-15 21:41   ` Anthony Sottile
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2017-08-15 21:33 UTC (permalink / raw)
  To: Anthony Sottile; +Cc: git

Anthony Sottile <asottile@umich.edu> writes:

> The handling of `status_only` no longer interferes with the handling of
> `unmatch_name_only`.  `--quiet` no longer affects the exit code when using
> `-L`/`--files-without-match`.

I agree with the above statement of yours that --quiet should not
affect what the exit status is.

But I am not sure what the exit code from these commands _should_
be:

    $ git grep -L qfwfq \*.h    ;# no file matches
    $ git grep -L \# \*.h       ;# some but not all files match
    $ git grep -L . \*.h        ;# all files match

with or without --quiet.  I seem to get 0, 0, 1, which I am not sure
is correct.  I do recall writing "git grep" _without_ thinking what
the exit code should be when we added --files-without-match, so the
exit status the current code gives out may be just a random garbage.

Asking GNU grep (because --files-without-match is not a POSIX thing):

    $ grep -L qfwfq *.h          ;# no file matches
    $ grep -L \# *.h             ;# some but not all files match
    $ grep -L . *.h              ;# all files match

I seem to get 1, 0, 0.  So the exit status should reflect if there
was _any_ hit from any file that were inspected.

> @@ -1755,7 +1755,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
>  		}
>  		if (hit) {
>  			count++;
> -			if (opt->status_only)
> +			if (!opt->unmatch_name_only && opt->status_only)
>  				return 1;
>  			if (opt->name_only) {
>  				show_name(opt, gs->name);

Does the change in this hunk have any effect?

Just before this hunk there is this code:

		/* "grep -v -e foo -e bla" should list lines
		 * that do not have either, so inversion should
		 * be done outside.
		 */
		if (opt->invert)
			hit = !hit;
		if (opt->unmatch_name_only) {
			if (hit)
				return 0;
			goto next_line;

If (opt->unmatch_name_only && hit) then the function would have
already returned and the control wouldn't have reached here.

Which would mean that when the control reaches the line this hunk
touches, either one of these must be false, and because we are
inside "if (hit)", opt->unmatch_name_only must be false.

> @@ -1820,13 +1820,14 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
>  	if (collect_hits)
>  		return 0;
>  
> -	if (opt->status_only)
> -		return 0;
>  	if (opt->unmatch_name_only) {
>  		/* We did not see any hit, so we want to show this */
> -		show_name(opt, gs->name);
> +		if (!opt->status_only)
> +			show_name(opt, gs->name);
>  		return 1;
>  	}
> +	if (opt->status_only)
> +		return 0;

This hunk makes sense to me (provided if the semantics we want out
of --files-without-match is sensible, which is dubious), even though
I would have limited the change to just a single line, i.e.

	if (opt->status_only)
-		return 0;
+		return opt->unmatch_name_only;
	if (opt->unmatch_name_only) {
		/* We did not see any hit, ... */

But I suspect we want to fix the exit code not to be affected by
the "--files-without-match" option in the first place, so all the
code changes we see in this patch might be moot X-<.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH/RFC] git-grep: correct exit code with --quiet and -L
  2017-08-15 21:33 ` Junio C Hamano
@ 2017-08-15 21:41   ` Anthony Sottile
  2017-08-15 22:24     ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Anthony Sottile @ 2017-08-15 21:41 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List

Ah yes, I didn't intend to include the first hunk (forgot to amend it
out when formatting the patch).

I think git's exit codes for -L actually make more sense than the GNU
exit codes (especially when comparing with `grep` vs `grep -v`) --
that is, produce `0` when the search is successful (producing
*something* on stdout) and `1` when the search fails.

Shall I create a new mail with the adjusted patch as suggested above?
(I'm not familiar with the expected workflow).

Anthony

On Tue, Aug 15, 2017 at 2:33 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Anthony Sottile <asottile@umich.edu> writes:
>
>> The handling of `status_only` no longer interferes with the handling of
>> `unmatch_name_only`.  `--quiet` no longer affects the exit code when using
>> `-L`/`--files-without-match`.
>
> I agree with the above statement of yours that --quiet should not
> affect what the exit status is.
>
> But I am not sure what the exit code from these commands _should_
> be:
>
>     $ git grep -L qfwfq \*.h    ;# no file matches
>     $ git grep -L \# \*.h       ;# some but not all files match
>     $ git grep -L . \*.h        ;# all files match
>
> with or without --quiet.  I seem to get 0, 0, 1, which I am not sure
> is correct.  I do recall writing "git grep" _without_ thinking what
> the exit code should be when we added --files-without-match, so the
> exit status the current code gives out may be just a random garbage.
>
> Asking GNU grep (because --files-without-match is not a POSIX thing):
>
>     $ grep -L qfwfq *.h          ;# no file matches
>     $ grep -L \# *.h             ;# some but not all files match
>     $ grep -L . *.h              ;# all files match
>
> I seem to get 1, 0, 0.  So the exit status should reflect if there
> was _any_ hit from any file that were inspected.
>
>> @@ -1755,7 +1755,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
>>               }
>>               if (hit) {
>>                       count++;
>> -                     if (opt->status_only)
>> +                     if (!opt->unmatch_name_only && opt->status_only)
>>                               return 1;
>>                       if (opt->name_only) {
>>                               show_name(opt, gs->name);
>
> Does the change in this hunk have any effect?
>
> Just before this hunk there is this code:
>
>                 /* "grep -v -e foo -e bla" should list lines
>                  * that do not have either, so inversion should
>                  * be done outside.
>                  */
>                 if (opt->invert)
>                         hit = !hit;
>                 if (opt->unmatch_name_only) {
>                         if (hit)
>                                 return 0;
>                         goto next_line;
>
> If (opt->unmatch_name_only && hit) then the function would have
> already returned and the control wouldn't have reached here.
>
> Which would mean that when the control reaches the line this hunk
> touches, either one of these must be false, and because we are
> inside "if (hit)", opt->unmatch_name_only must be false.
>
>> @@ -1820,13 +1820,14 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
>>       if (collect_hits)
>>               return 0;
>>
>> -     if (opt->status_only)
>> -             return 0;
>>       if (opt->unmatch_name_only) {
>>               /* We did not see any hit, so we want to show this */
>> -             show_name(opt, gs->name);
>> +             if (!opt->status_only)
>> +                     show_name(opt, gs->name);
>>               return 1;
>>       }
>> +     if (opt->status_only)
>> +             return 0;
>
> This hunk makes sense to me (provided if the semantics we want out
> of --files-without-match is sensible, which is dubious), even though
> I would have limited the change to just a single line, i.e.
>
>         if (opt->status_only)
> -               return 0;
> +               return opt->unmatch_name_only;
>         if (opt->unmatch_name_only) {
>                 /* We did not see any hit, ... */
>
> But I suspect we want to fix the exit code not to be affected by
> the "--files-without-match" option in the first place, so all the
> code changes we see in this patch might be moot X-<.
>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH/RFC] git-grep: correct exit code with --quiet and -L
  2017-08-15 21:41   ` Anthony Sottile
@ 2017-08-15 22:24     ` Junio C Hamano
  2017-08-15 22:43       ` Anthony Sottile
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2017-08-15 22:24 UTC (permalink / raw)
  To: Anthony Sottile; +Cc: Git Mailing List

Anthony Sottile <asottile@umich.edu> writes:

> Ah yes, I didn't intend to include the first hunk (forgot to amend it
> out when formatting the patch).
>
> I think git's exit codes for -L actually make more sense than the GNU
> exit codes (especially when comparing with `grep` vs `grep -v`) --
> that is, produce `0` when the search is successful (producing
> *something* on stdout) and `1` when the search fails.
>
> Shall I create a new mail with the adjusted patch as suggested above?

I do not mind seeing an updated patch that does not change the exit
status (as you seem to like what we have currently that contradicts
what GNU grep does) but makes it consistent between "--quiet" and
"--no-quiet".  But I would not be surprised if people seeing this
exchange from the sideline are already working on fixing the exit
status and also making sure that the fixed code would produce the
same corrected exit status with or without "--quiet", so an updated
patch from you will likely conflict with their effort.

So if I were you, I'd wait to see what other people would say about
the actual exit codes we give when "git grep -L" is run without the
"--quiet" option, and if they are also happy with the current exit
code, then send in an updated patch.

Thanks.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH/RFC] git-grep: correct exit code with --quiet and -L
  2017-08-15 22:24     ` Junio C Hamano
@ 2017-08-15 22:43       ` Anthony Sottile
  0 siblings, 0 replies; 7+ messages in thread
From: Anthony Sottile @ 2017-08-15 22:43 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List

Sounds good, I'll wait.

I've also created a mailing list entry on the gnu grep mailing list as
I believe the current exit status for --files-without-match is
inconsistent:

http://lists.gnu.org/archive/html/bug-grep/2017-08/msg00010.html

Anthony

On Tue, Aug 15, 2017 at 3:24 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Anthony Sottile <asottile@umich.edu> writes:
>
>> Ah yes, I didn't intend to include the first hunk (forgot to amend it
>> out when formatting the patch).
>>
>> I think git's exit codes for -L actually make more sense than the GNU
>> exit codes (especially when comparing with `grep` vs `grep -v`) --
>> that is, produce `0` when the search is successful (producing
>> *something* on stdout) and `1` when the search fails.
>>
>> Shall I create a new mail with the adjusted patch as suggested above?
>
> I do not mind seeing an updated patch that does not change the exit
> status (as you seem to like what we have currently that contradicts
> what GNU grep does) but makes it consistent between "--quiet" and
> "--no-quiet".  But I would not be surprised if people seeing this
> exchange from the sideline are already working on fixing the exit
> status and also making sure that the fixed code would produce the
> same corrected exit status with or without "--quiet", so an updated
> patch from you will likely conflict with their effort.
>
> So if I were you, I'd wait to see what other people would say about
> the actual exit codes we give when "git grep -L" is run without the
> "--quiet" option, and if they are also happy with the current exit
> code, then send in an updated patch.
>
> Thanks.
>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH/RFC] git-grep: correct exit code with --quiet and -L
@ 2017-08-18  1:38 Anthony Sottile
  2017-08-18  2:22 ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Anthony Sottile @ 2017-08-18  1:38 UTC (permalink / raw)
  To: git; +Cc: Anthony Sottile

The handling of `status_only` no longer interferes with the handling of
`unmatch_name_only`.  `--quiet` no longer affects the exit code when using
`-L`/`--files-without-match`.

Signed-off-by: Anthony Sottile <asottile@umich.edu>
---
 grep.c          | 2 +-
 t/t7810-grep.sh | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/grep.c b/grep.c
index 2efec0e..c9e7cc7 100644
--- a/grep.c
+++ b/grep.c
@@ -1821,7 +1821,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
 		return 0;
 
 	if (opt->status_only)
-		return 0;
+		return opt->unmatch_name_only;
 	if (opt->unmatch_name_only) {
 		/* We did not see any hit, so we want to show this */
 		show_name(opt, gs->name);
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index f106387..2a6679c 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -374,6 +374,11 @@ test_expect_success 'grep -L -C' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep --files-without-match --quiet' '
+	git grep --files-without-match --quiet nonexistent_string >actual &&
+	test_cmp /dev/null actual
+'
+
 cat >expected <<EOF
 file:foo mmap bar_mmap
 EOF
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH/RFC] git-grep: correct exit code with --quiet and -L
  2017-08-18  1:38 [PATCH/RFC] git-grep: correct exit code with --quiet and -L Anthony Sottile
@ 2017-08-18  2:22 ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2017-08-18  2:22 UTC (permalink / raw)
  To: Anthony Sottile; +Cc: git

Anthony Sottile <asottile@umich.edu> writes:

> The handling of `status_only` no longer interferes with the handling of
> `unmatch_name_only`.  `--quiet` no longer affects the exit code when using
> `-L`/`--files-without-match`.
>
> Signed-off-by: Anthony Sottile <asottile@umich.edu>
> ---

Thanks, Will queue.

>  grep.c          | 2 +-
>  t/t7810-grep.sh | 5 +++++
>  2 files changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/grep.c b/grep.c
> index 2efec0e..c9e7cc7 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -1821,7 +1821,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
>  		return 0;
>  
>  	if (opt->status_only)
> -		return 0;
> +		return opt->unmatch_name_only;
>  	if (opt->unmatch_name_only) {
>  		/* We did not see any hit, so we want to show this */
>  		show_name(opt, gs->name);
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index f106387..2a6679c 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -374,6 +374,11 @@ test_expect_success 'grep -L -C' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'grep --files-without-match --quiet' '
> +	git grep --files-without-match --quiet nonexistent_string >actual &&
> +	test_cmp /dev/null actual
> +'
> +
>  cat >expected <<EOF
>  file:foo mmap bar_mmap
>  EOF

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-08-18  2:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-18  1:38 [PATCH/RFC] git-grep: correct exit code with --quiet and -L Anthony Sottile
2017-08-18  2:22 ` Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2017-08-15 20:35 Anthony Sottile
2017-08-15 21:33 ` Junio C Hamano
2017-08-15 21:41   ` Anthony Sottile
2017-08-15 22:24     ` Junio C Hamano
2017-08-15 22:43       ` Anthony Sottile

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).