grep: add --max-count command line option

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
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Carlos López
2022-06-22 19:47:32 +00:00
committed by Junio C Hamano
parent f770e9f396
commit 68437ede53
5 changed files with 108 additions and 1 deletions

2
grep.c
View File

@@ -1615,7 +1615,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
return 0;
goto next_line;
}
if (hit) {
if (hit && (opt->max_count < 0 || count < opt->max_count)) {
count++;
if (opt->status_only)
return 1;