git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] grep: add --max-count command line option

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 21, 2022, 16:27 UTC
Message-ID
<xmqq5ykuhthp.fsf@gitster.g>
In-Reply-To
<pull.1278.v2.git.git.1655789777023.gitgitgadget@gmail.com>
"Carlos L. via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 17 quoted lines
> From: =?UTF-8?q?Carlos=20L=C3=B3pez?= <00xc@protonmail.com>
>
> This patch adds a command line option analogous to that of GNU
> grep(1)'s -m / --max-count, which users might already be used to.
> This makes it possible to limit the amount of matches shown in the
> output while keeping the functionality of other options such as -C
> (show code context) or -p (show containing function), which would be
> difficult to do with a shell pipeline (e.g. head(1)).
>
> Signed-off-by: Carlos López 00xc@protonmail.com
> ---
> ...
>  Documentation/git-grep.txt | 8 ++++++++
>  builtin/grep.c             | 9 +++++++++
>  grep.c                     | 2 ++
>  grep.h                     | 2 ++
>  4 files changed, 21 insertions(+)
Tests?
Show 22 quoted lines
> diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
> index 3d393fbac1b..19b817d5e58 100644
> --- a/Documentation/git-grep.txt
> +++ b/Documentation/git-grep.txt
> @@ -23,6 +23,7 @@ SYNOPSIS
>  	   [--break] [--heading] [-p | --show-function]
>  	   [-A <post-context>] [-B <pre-context>] [-C <context>]
>  	   [-W | --function-context]
> +	   [(-m | --max-count) <num>]
>  	   [--threads <num>]
>  	   [-f <file>] [-e] <pattern>
>  	   [--and|--or|--not|(|)|-e <pattern>...]
> @@ -238,6 +239,13 @@ providing this option will cause it to die.
>  	`git diff` works out patch hunk headers (see 'Defining a
>  	custom hunk-header' in linkgit:gitattributes[5]).
>  
> +-m <num>::
> +--max-count <num>::
> +	Limit the amount of matches per file. When using the `-v` or
> +	`--invert-match` option, the search stops after the specified
> +	number of non-matches. A value of -1 will return unlimited
> +	results (the default).
Hmph ...
Show 7 quoted lines
> +	/*
> +	 * Optimize out the case where the amount of matches is limited to zero.
> +	 * We do this to keep results consistent with GNU grep(1).
> +	 */
> +	if (opt.max_count == 0)
> +		exit(EXIT_FAILURE);
> +

OK, so "stop before seeing any match" logically leads to "we found nothing, so exit with non-zero".

Show 10 quoted lines
> diff --git a/grep.c b/grep.c
> index 82eb7da1022..b32ab75cb6b 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -1686,6 +1686,8 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
>  		bol = eol + 1;
>  		if (!left)
>  			break;
> +		if (opt->max_count != -1 && count == opt->max_count)
> +			break;

I would have written it "if (0 <= opt->max_count && ...)". What happens when a trickster asks you to do "git grep -m -2"?

I guess what I am getting at is if we are better off saying that negative means unlimited, instead of special casing -1 like this. I didn't think it through so it may be perfectly possible that what you wrote makes more sense than "anything negative is unlimited".

I dunno.
>  		left--;
>  		lno++;
>  	}
Thanks.
Show 22 quoted lines
> diff --git a/grep.h b/grep.h
> index c722d25ed9d..bdcadce61b8 100644
> --- a/grep.h
> +++ b/grep.h
> @@ -171,6 +171,7 @@ struct grep_opt {
>  	int show_hunk_mark;
>  	int file_break;
>  	int heading;
> +	int max_count;
>  	void *priv;
>  
>  	void (*output)(struct grep_opt *opt, const void *data, size_t size);
> @@ -181,6 +182,7 @@ struct grep_opt {
>  	.relative = 1, \
>  	.pathname = 1, \
>  	.max_depth = -1, \
> +	.max_count = -1, \
>  	.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, \
>  	.colors = { \
>  		[GREP_COLOR_CONTEXT] = "", \
>
> base-commit: 5b71c59bc3b9365075e2a175aa7b6f2b0c84ce44
Previous: Carlos L. via GitGitGadgetNext: Carlos L.
Message 6 of 14 in “grep: add --max-count command line option”
  1. grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 20, 2022
  2. Paul EggertJun 20, 2022
  3. Carlos L.Jun 20, 2022
  4. Paul EggertJun 20, 2022
  5. grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 21, 2022
  6. Junio C HamanoJun 21, 2022
  7. Carlos L.Jun 22, 2022
  8. Junio C HamanoJun 22, 2022
  9. 0/2 grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 22, 2022
  10. 2/2 tests: add tests for grep --max-countCarlos López via GitGitGadget, Jun 22, 2022
  11. Junio C HamanoJun 22, 2022
  12. 1/2 grep: add --max-count command line optionCarlos López via GitGitGadget, Jun 22, 2022
  13. grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 22, 2022
  14. Carlos L.Jun 22, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.