* [PATCH/RFC] grep: optionally show only the match
@ 2012-09-09 21:58 Marcus Karlsson
2012-09-10 16:43 ` René Scharfe
0 siblings, 1 reply; 2+ messages in thread
From: Marcus Karlsson @ 2012-09-09 21:58 UTC (permalink / raw)
To: git; +Cc: gitster, Marcus Karlsson
Make git-grep optionally omit the parts of the line before and after the
match.
Signed-off-by: Marcus Karlsson <mk@acc.umu.se>
---
Documentation/git-grep.txt | 8 +++++++-
builtin/grep.c | 2 ++
grep.c | 7 +++++--
grep.h | 1 +
4 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index cfecf84..6ef22cb 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -20,7 +20,8 @@ SYNOPSIS
[-c | --count] [--all-match] [-q | --quiet]
[--max-depth <depth>]
[--color[=<when>] | --no-color]
- [--break] [--heading] [-p | --show-function]
+ [--break] [--heading] [-o | --only-matching]
+ [-p | --show-function]
[-A <post-context>] [-B <pre-context>] [-C <context>]
[-W | --function-context]
[-f <file>] [-e] <pattern>
@@ -183,6 +184,11 @@ OPTIONS
Show the filename above the matches in that file instead of
at the start of each shown line.
+-o::
+--only-matching::
+ Show only the part of the matching line that matched the
+ pattern.
+
-p::
--show-function::
Show the preceding line that contains the function name of
diff --git a/builtin/grep.c b/builtin/grep.c
index 09ca4c9..56aba7b 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -782,6 +782,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
N_("print empty line between matches from different files")),
OPT_BOOLEAN(0, "heading", &opt.heading,
N_("show filename only once above matches from same file")),
+ OPT_BOOLEAN('o', "only-matching", &opt.only_matching,
+ N_("show only the matching part of a matched line")),
OPT_GROUP(""),
OPT_CALLBACK('C', "context", &opt, N_("n"),
N_("show <n> context lines before and after matches"),
diff --git a/grep.c b/grep.c
index 04e3ec6..9fc888e 100644
--- a/grep.c
+++ b/grep.c
@@ -827,7 +827,9 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
if (match.rm_so == match.rm_eo)
break;
- output_color(opt, bol, match.rm_so, line_color);
+ if (opt->only_matching == 0)
+ output_color(opt, bol, match.rm_so,
+ line_color);
output_color(opt, bol + match.rm_so,
match.rm_eo - match.rm_so,
opt->color_match);
@@ -837,7 +839,8 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
}
*eol = ch;
}
- output_color(opt, bol, rest, line_color);
+ if (opt->only_matching == 0)
+ output_color(opt, bol, rest, line_color);
opt->output(opt, "\n", 1);
}
diff --git a/grep.h b/grep.h
index 75afb7b..4163102 100644
--- a/grep.h
+++ b/grep.h
@@ -127,6 +127,7 @@ struct grep_opt {
int show_hunk_mark;
int file_break;
int heading;
+ int only_matching;
void *priv;
void (*output)(struct grep_opt *opt, const void *data, size_t size);
--
1.7.12.289.g0ce9864.dirty
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH/RFC] grep: optionally show only the match
2012-09-09 21:58 [PATCH/RFC] grep: optionally show only the match Marcus Karlsson
@ 2012-09-10 16:43 ` René Scharfe
0 siblings, 0 replies; 2+ messages in thread
From: René Scharfe @ 2012-09-10 16:43 UTC (permalink / raw)
To: Marcus Karlsson; +Cc: git, gitster
Am 09.09.2012 23:58, schrieb Marcus Karlsson:
> Make git-grep optionally omit the parts of the line before and after the
> match.
>
> Signed-off-by: Marcus Karlsson <mk@acc.umu.se>
> ---
> Documentation/git-grep.txt | 8 +++++++-
> builtin/grep.c | 2 ++
> grep.c | 7 +++++--
> grep.h | 1 +
> 4 files changed, 15 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
> index cfecf84..6ef22cb 100644
> --- a/Documentation/git-grep.txt
> +++ b/Documentation/git-grep.txt
> @@ -20,7 +20,8 @@ SYNOPSIS
> [-c | --count] [--all-match] [-q | --quiet]
> [--max-depth <depth>]
> [--color[=<when>] | --no-color]
> - [--break] [--heading] [-p | --show-function]
> + [--break] [--heading] [-o | --only-matching]
> + [-p | --show-function]
> [-A <post-context>] [-B <pre-context>] [-C <context>]
> [-W | --function-context]
> [-f <file>] [-e] <pattern>
> @@ -183,6 +184,11 @@ OPTIONS
> Show the filename above the matches in that file instead of
> at the start of each shown line.
>
> +-o::
> +--only-matching::
> + Show only the part of the matching line that matched the
> + pattern.
> +
> -p::
> --show-function::
> Show the preceding line that contains the function name of
> diff --git a/builtin/grep.c b/builtin/grep.c
> index 09ca4c9..56aba7b 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -782,6 +782,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
> N_("print empty line between matches from different files")),
> OPT_BOOLEAN(0, "heading", &opt.heading,
> N_("show filename only once above matches from same file")),
> + OPT_BOOLEAN('o', "only-matching", &opt.only_matching,
> + N_("show only the matching part of a matched line")),
> OPT_GROUP(""),
> OPT_CALLBACK('C', "context", &opt, N_("n"),
> N_("show <n> context lines before and after matches"),
> diff --git a/grep.c b/grep.c
> index 04e3ec6..9fc888e 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -827,7 +827,9 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
> if (match.rm_so == match.rm_eo)
> break;
>
> - output_color(opt, bol, match.rm_so, line_color);
> + if (opt->only_matching == 0)
> + output_color(opt, bol, match.rm_so,
> + line_color);
> output_color(opt, bol + match.rm_so,
> match.rm_eo - match.rm_so,
> opt->color_match);
> @@ -837,7 +839,8 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
> }
> *eol = ch;
> }
> - output_color(opt, bol, rest, line_color);
> + if (opt->only_matching == 0)
> + output_color(opt, bol, rest, line_color);
> opt->output(opt, "\n", 1);
> }
The implementation keeps only the coloured parts. However, they are not
necessarily the same as the matching parts. This is more complicated
with git grep than with regular grep because the former has the
additional options --and and --not. Consider this:
$ git grep --not -e bla --or --not -e blub
Lines with only either "bla" or "blub" (or none of them) will be shown,
lines with both not. Both "bla" and "blub" will be highlighted. The
matching part is always the whole shown line.
René
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-09-10 16:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-09 21:58 [PATCH/RFC] grep: optionally show only the match Marcus Karlsson
2012-09-10 16:43 ` René Scharfe
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 NNTP newsgroup(s).