From: "Carlos L. via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Carlos L." <00xc@protonmail.com>, "Carlos López" <00xc@protonmail.com>
Subject: [PATCH] grep: add --max-count command line option
Date: Thu, 12 May 2022 13:20:09 +0000 [thread overview]
Message-ID: <pull.1264.git.git.1652361610103.gitgitgadget@gmail.com> (raw)
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>
---
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
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1264%2F00xc%2Fmaster-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1264/00xc/master-v1
Pull-Request: https://github.com/git/git/pull/1264
Documentation/git-grep.txt | 7 +++++++
builtin/grep.c | 2 ++
grep.c | 2 ++
grep.h | 1 +
4 files changed, 12 insertions(+)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 3d393fbac1b..02b36046475 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,12 @@ 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. Setting this option to 0 has no effect.
+
--threads <num>::
Number of grep worker threads to use.
See `grep.threads` in 'CONFIGURATION' for more information.
diff --git a/builtin/grep.c b/builtin/grep.c
index bcb07ea7f75..ba1894d5675 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 (default: 0, no limit)")),
OPT_END()
};
grep_prefix = prefix;
diff --git a/grep.c b/grep.c
index 82eb7da1022..173b6c27b6e 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 && count == opt->max_count)
+ break;
left--;
lno++;
}
diff --git a/grep.h b/grep.h
index c722d25ed9d..25836f34314 100644
--- a/grep.h
+++ b/grep.h
@@ -171,6 +171,7 @@ struct grep_opt {
int show_hunk_mark;
int file_break;
int heading;
+ unsigned max_count;
void *priv;
void (*output)(struct grep_opt *opt, const void *data, size_t size);
base-commit: 277cf0bc36094f6dc4297d8c9cef79df045b735d
--
gitgitgadget
next reply other threads:[~2022-05-12 13:20 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-12 13:20 Carlos L. via GitGitGadget [this message]
2022-05-14 18:16 ` [PATCH] grep: add --max-count command line option Martin Ågren
2022-05-16 5:57 ` Junio C Hamano
2022-05-16 7:28 ` Paul Eggert
2022-05-16 8:38 ` Carlos L.
2022-05-16 15:36 ` Junio C Hamano
2022-05-17 5:53 ` Paul Eggert
2022-05-16 15:18 ` Junio C Hamano
-- strict thread matches above, loose matches on Subject: below --
2022-06-20 15:49 Carlos L. via GitGitGadget
2022-06-20 15:57 ` Paul Eggert
2022-06-20 16:25 ` Carlos L.
2022-06-20 16:32 ` Paul Eggert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=pull.1264.git.git.1652361610103.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=00xc@protonmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.