grep.c: teach 'git grep --only-matching'
Teach 'git grep --only-matching', a new option to only print the matching part(s) of a line. For instance, a line containing the following (taken from README.md:27): (`man gitcvs-migration` or `git help cvs-migration` if git is Is printed as follows: $ git grep --line-number --column --only-matching -e git -- \ README.md | grep ":27" README.md:27:7:git README.md:27:16:git README.md:27:38:git The patch works mostly as one would expect, with the exception of a few considerations that are worth mentioning here. Like GNU grep, this patch ignores --only-matching when --invert (-v) is given. There is a sensible answer here, but parity with the behavior of other tools is preferred. Because a line might contain more than one match, there are special considerations pertaining to when to print line headers, newlines, and how to increment the match column offset. The line header and newlines are handled as a special case within the main loop to avoid polluting the surrounding code with conditionals that have large blocks. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
c707ded332
commit
9d8db06eb4
@ -843,6 +843,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||
OPT_BOOL_F('z', "null", &opt.null_following_name,
|
||||
N_("print NUL after filenames"),
|
||||
PARSE_OPT_NOCOMPLETE),
|
||||
OPT_BOOL('o', "only-matching", &opt.only_matching,
|
||||
N_("show only matching parts of a line")),
|
||||
OPT_BOOL('c', "count", &opt.count,
|
||||
N_("show the number of matches instead of matching lines")),
|
||||
OPT__COLOR(&opt.color, N_("highlight matches")),
|
||||
@ -962,6 +964,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||
if (!opt.pattern_list)
|
||||
die(_("no pattern given."));
|
||||
|
||||
/* --only-matching has no effect with --invert. */
|
||||
if (opt.invert)
|
||||
opt.only_matching = 0;
|
||||
|
||||
/*
|
||||
* We have to find "--" in a separate pass, because its presence
|
||||
* influences how we will parse arguments that come before it.
|
||||
|
Reference in New Issue
Block a user