From: "Carlos L. via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Martin Ågren [ ]" <martin.agren@gmail.com>,
"Paul Eggert [ ]" <eggert@cs.ucla.edu>,
"Carlos L." <00xc@protonmail.com>
Subject: [PATCH v3 0/2] grep: add --max-count command line option
Date: Wed, 22 Jun 2022 17:07:06 +0000 [thread overview]
Message-ID: <pull.1278.v3.git.git.1655917628.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1278.v2.git.git.1655789777023.gitgitgadget@gmail.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
Carlos López (2):
grep: add --max-count command line option
tests: add tests for grep --max-count
Documentation/git-grep.txt | 9 +++++
builtin/grep.c | 9 +++++
grep.c | 2 +-
grep.h | 2 +
t/t7810-grep.sh | 83 ++++++++++++++++++++++++++++++++++++++
5 files changed, 104 insertions(+), 1 deletion(-)
base-commit: 5b71c59bc3b9365075e2a175aa7b6f2b0c84ce44
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1278%2F00xc%2Fmaster-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1278/00xc/master-v3
Pull-Request: https://github.com/git/git/pull/1278
Range-diff vs v2:
1: ee7eb298854 ! 1: 5bf7244437e grep: add --max-count command line option
@@ Documentation/git-grep.txt: providing this option will cause it to die.
+ 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).
++ results (the default). A value of 0 will exit immediately with
++ a non-zero status.
+
--threads <num>::
Number of grep worker threads to use.
@@ builtin/grep.c: int cmd_grep(int argc, const char **argv, const char *prefix)
+ * We do this to keep results consistent with GNU grep(1).
+ */
+ if (opt.max_count == 0)
-+ exit(EXIT_FAILURE);
++ return 1;
+
if (show_in_pager) {
if (num_threads > 1)
@@ builtin/grep.c: int cmd_grep(int argc, const char **argv, const char *prefix)
## grep.c ##
@@ grep.c: 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;
- left--;
- lno++;
- }
+ return 0;
+ goto next_line;
+ }
+- if (hit) {
++ if (hit && (opt->max_count < 0 || count < opt->max_count)) {
+ count++;
+ if (opt->status_only)
+ return 1;
## grep.h ##
@@ grep.h: struct grep_opt {
-: ----------- > 2: 525958af877 tests: add tests for grep --max-count
--
gitgitgadget
next prev parent reply other threads:[~2022-06-22 17:09 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-20 15:49 [PATCH] grep: add --max-count command line option 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
2022-06-21 5:36 ` [PATCH v2] " Carlos L. via GitGitGadget
2022-06-21 16:27 ` Junio C Hamano
2022-06-22 6:41 ` Carlos L.
2022-06-22 6:56 ` Junio C Hamano
[not found] ` <220622.86mte5knbe.gmgdl@evledraar.gmail.com>
2022-06-22 13:23 ` Carlos L.
2022-06-22 17:07 ` Carlos L. via GitGitGadget [this message]
2022-06-22 17:07 ` [PATCH v3 1/2] " Carlos López via GitGitGadget
2022-06-22 17:07 ` [PATCH v3 2/2] tests: add tests for grep --max-count Carlos López via GitGitGadget
2022-06-22 18:10 ` Junio C Hamano
2022-06-22 19:47 ` [PATCH v4] grep: add --max-count command line option Carlos L. via GitGitGadget
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=pull.1278.v3.git.git.1655917628.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=00xc@protonmail.com \
--cc=eggert@cs.ucla.edu \
--cc=git@vger.kernel.org \
--cc=martin.agren@gmail.com \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).