]> git.ipfire.org Git - thirdparty/git.git/commitdiff
grep: add --max-count command line option
authorCarlos López <00xc@protonmail.com>
Wed, 22 Jun 2022 19:47:32 +0000 (19:47 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 22 Jun 2022 20:23:29 +0000 (13:23 -0700)
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>
Documentation/git-grep.txt
builtin/grep.c
grep.c
grep.h
t/t7810-grep.sh

index 3d393fbac1bb3323c41ea2b74f6bba8f8ca39c00..58d944bd578af07d64b91c24958f5561661a353a 100644 (file)
@@ -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,14 @@ 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). A value of 0 will exit immediately with
+       a non-zero status.
+
 --threads <num>::
        Number of grep worker threads to use.
        See `grep.threads` in 'CONFIGURATION' for more information.
index bcb07ea7f75ba35079d0093248df0647292665a1..e6bcdf860cc96af2e70975eea2f66ac8ec8606eb 100644 (file)
@@ -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"));
diff --git a/grep.c b/grep.c
index 82eb7da1022ecc0c08a83ca7bc46e18602ff275b..52a894c989087c70f215fc46d9f17489de546d8c 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -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;
diff --git a/grep.h b/grep.h
index c722d25ed9d272fd333162a64ba367bc19ff77da..bdcadce61b8027adc760cd6e7fc51aa4e94c3d06 100644 (file)
--- 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] = "", \
index 69356011713fad1add8044471f01f9d6d3dee892..0f937990a062e30b5f11baa296c88472019ac338 100755 (executable)
@@ -77,6 +77,7 @@ test_expect_success setup '
        # Say hello.
        function hello() {
          echo "Hello world."
+         echo "Hello again."
        } # hello
 
        # Still a no-op.
@@ -595,6 +596,92 @@ test_expect_success 'grep --files-without-match --quiet' '
        test_must_be_empty actual
 '
 
+test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
+       test_must_fail git grep --max-count 0 foo >actual &&
+       test_must_be_empty actual
+'
+
+test_expect_success 'grep --max-count 3' '
+       cat >expected <<-EOF &&
+       file:foo mmap bar
+       file:foo_mmap bar
+       file:foo_mmap bar mmap
+       EOF
+       git grep --max-count 3 foo >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count -1 (no limit)' '
+       cat >expected <<-EOF &&
+       file:foo mmap bar
+       file:foo_mmap bar
+       file:foo_mmap bar mmap
+       file:foo mmap bar_mmap
+       file:foo_mmap bar mmap baz
+       EOF
+       git grep --max-count -1 foo >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --context 2' '
+       cat >expected <<-EOF &&
+       file-foo mmap bar
+       file:foo_mmap bar
+       file-foo_mmap bar mmap
+       EOF
+       git grep --max-count 1 --context 1 foo_mmap >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --show-function' '
+       cat >expected <<-EOF &&
+       hello.ps1=function hello() {
+       hello.ps1:  echo "Hello world."
+       EOF
+       git grep --max-count 1 --show-function Hello hello.ps1 >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 2 --show-function' '
+       cat >expected <<-EOF &&
+       hello.ps1=function hello() {
+       hello.ps1:  echo "Hello world."
+       hello.ps1:  echo "Hello again."
+       EOF
+       git grep --max-count 2 --show-function Hello hello.ps1 >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --count' '
+       cat >expected <<-EOF &&
+       hello.ps1:1
+       EOF
+       git grep --max-count 1 --count Hello hello.ps1 >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 (multiple files)' '
+       cat >expected <<-EOF &&
+       hello.c:#include <stdio.h>
+       hello.ps1:# No-op.
+       EOF
+       git grep --max-count 1 -e o -- hello.\* >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --context 1 (multiple files)' '
+       cat >expected <<-EOF &&
+       hello.c-#include <assert.h>
+       hello.c:#include <stdio.h>
+       hello.c-
+       --
+       hello.ps1:# No-op.
+       hello.ps1-function dummy() {}
+       EOF
+       git grep --max-count 1 --context 1 -e o -- hello.\* >actual &&
+       test_cmp expected actual
+'
+
 cat >expected <<EOF
 file:foo mmap bar_mmap
 EOF