diff options
author | Carlos López <00xc@protonmail.com> | 2022-06-22 22:47:32 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2022-06-22 23:23:29 +0300 |
commit | 68437ede53dccd1dea9e44e831a59de274d389de (patch) | |
tree | 4522d5e4501a87bd71ed5d0d0a3e7e1dd76b3cb7 /builtin/grep.c | |
parent | f770e9f396d48b567ef7b37d273e91ad570a3522 (diff) |
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>
Diffstat (limited to 'builtin/grep.c')
-rw-r--r-- | builtin/grep.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/builtin/grep.c b/builtin/grep.c index bcb07ea7f7..e6bcdf860c 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -961,6 +961,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix) OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored, N_("allow calling of grep(1) (ignored by this build)"), PARSE_OPT_NOCOMPLETE), + OPT_INTEGER('m', "max-count", &opt.max_count, + N_("maximum number of results per file")), OPT_END() }; grep_prefix = prefix; @@ -1101,6 +1103,13 @@ int cmd_grep(int argc, const char **argv, const char *prefix) if (recurse_submodules && untracked) die(_("--untracked not supported with --recurse-submodules")); + /* + * 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) + return 1; + if (show_in_pager) { if (num_threads > 1) warning(_("invalid option combination, ignoring --threads")); |