{"thread":{"id":"31488","subject":"[PATCH/RFC] grep: optionally show only the match","startedAt":"2012-09-09T21:58:25Z","lastAt":"2012-09-10T16:43:32Z","messageCount":2,"participants":["Marcus Karlsson","René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"198652","messageId":"1347227905-2398-1-git-send-email-mk@acc.umu.se","threadId":"31488","inReplyTo":null,"subject":"[PATCH/RFC] grep: optionally show only the match","fromName":"Marcus Karlsson","fromEmail":"mk@acc.umu.se","sentAt":"2012-09-09T21:58:25Z","receivedAt":"2012-09-09T21:58:25Z","isPatch":true,"sender":{"key":"mk@acc.umu.se","avatar":null},"body":"Make git-grep optionally omit the parts of the line before and after the\nmatch.\n\nSigned-off-by: Marcus Karlsson <mk@acc.umu.se>\n---\n Documentation/git-grep.txt | 8 +++++++-\n builtin/grep.c             | 2 ++\n grep.c                     | 7 +++++--\n grep.h                     | 1 +\n 4 files changed, 15 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt\nindex cfecf84..6ef22cb 100644\n--- a/Documentation/git-grep.txt\n+++ b/Documentation/git-grep.txt\n@@ -20,7 +20,8 @@ SYNOPSIS\n \t   [-c | --count] [--all-match] [-q | --quiet]\n \t   [--max-depth <depth>]\n \t   [--color[=<when>] | --no-color]\n-\t   [--break] [--heading] [-p | --show-function]\n+\t   [--break] [--heading] [-o | --only-matching]\n+\t   [-p | --show-function]\n \t   [-A <post-context>] [-B <pre-context>] [-C <context>]\n \t   [-W | --function-context]\n \t   [-f <file>] [-e] <pattern>\n@@ -183,6 +184,11 @@ OPTIONS\n \tShow the filename above the matches in that file instead of\n \tat the start of each shown line.\n \n+-o::\n+--only-matching::\n+\tShow only the part of the matching line that matched the\n+\tpattern.\n+\n -p::\n --show-function::\n \tShow the preceding line that contains the function name of\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 09ca4c9..56aba7b 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -782,6 +782,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"print empty line between matches from different files\")),\n \t\tOPT_BOOLEAN(0, \"heading\", &opt.heading,\n \t\t\tN_(\"show filename only once above matches from same file\")),\n+\t\tOPT_BOOLEAN('o', \"only-matching\", &opt.only_matching,\n+\t\t\tN_(\"show only the matching part of a matched line\")),\n \t\tOPT_GROUP(\"\"),\n \t\tOPT_CALLBACK('C', \"context\", &opt, N_(\"n\"),\n \t\t\tN_(\"show <n> context lines before and after matches\"),\ndiff --git a/grep.c b/grep.c\nindex 04e3ec6..9fc888e 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -827,7 +827,9 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,\n \t\t\tif (match.rm_so == match.rm_eo)\n \t\t\t\tbreak;\n \n-\t\t\toutput_color(opt, bol, match.rm_so, line_color);\n+\t\t\tif (opt->only_matching == 0)\n+\t\t\t\toutput_color(opt, bol, match.rm_so,\n+\t\t\t\t\t     line_color);\n \t\t\toutput_color(opt, bol + match.rm_so,\n \t\t\t\t     match.rm_eo - match.rm_so,\n \t\t\t\t     opt->color_match);\n@@ -837,7 +839,8 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,\n \t\t}\n \t\t*eol = ch;\n \t}\n-\toutput_color(opt, bol, rest, line_color);\n+\tif (opt->only_matching == 0)\n+\t\toutput_color(opt, bol, rest, line_color);\n \topt->output(opt, \"\\n\", 1);\n }\n \ndiff --git a/grep.h b/grep.h\nindex 75afb7b..4163102 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -127,6 +127,7 @@ struct grep_opt {\n \tint show_hunk_mark;\n \tint file_break;\n \tint heading;\n+\tint only_matching;\n \tvoid *priv;\n \n \tvoid (*output)(struct grep_opt *opt, const void *data, size_t size);\n-- \n1.7.12.289.g0ce9864.dirty\n"},{"id":"198692","messageId":"504E18B4.5050000@lsrfire.ath.cx","threadId":"31488","inReplyTo":"1347227905-2398-1-git-send-email-mk@acc.umu.se","subject":"Re: [PATCH/RFC] grep: optionally show only the match","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2012-09-10T16:43:32Z","receivedAt":"2012-09-10T16:43:32Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 09.09.2012 23:58, schrieb Marcus Karlsson:\n> Make git-grep optionally omit the parts of the line before and after the\n> match.\n>\n> Signed-off-by: Marcus Karlsson <mk@acc.umu.se>\n> ---\n>   Documentation/git-grep.txt | 8 +++++++-\n>   builtin/grep.c             | 2 ++\n>   grep.c                     | 7 +++++--\n>   grep.h                     | 1 +\n>   4 files changed, 15 insertions(+), 3 deletions(-)\n>\n> diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt\n> index cfecf84..6ef22cb 100644\n> --- a/Documentation/git-grep.txt\n> +++ b/Documentation/git-grep.txt\n> @@ -20,7 +20,8 @@ SYNOPSIS\n>   \t   [-c | --count] [--all-match] [-q | --quiet]\n>   \t   [--max-depth <depth>]\n>   \t   [--color[=<when>] | --no-color]\n> -\t   [--break] [--heading] [-p | --show-function]\n> +\t   [--break] [--heading] [-o | --only-matching]\n> +\t   [-p | --show-function]\n>   \t   [-A <post-context>] [-B <pre-context>] [-C <context>]\n>   \t   [-W | --function-context]\n>   \t   [-f <file>] [-e] <pattern>\n> @@ -183,6 +184,11 @@ OPTIONS\n>   \tShow the filename above the matches in that file instead of\n>   \tat the start of each shown line.\n>\n> +-o::\n> +--only-matching::\n> +\tShow only the part of the matching line that matched the\n> +\tpattern.\n> +\n>   -p::\n>   --show-function::\n>   \tShow the preceding line that contains the function name of\n> diff --git a/builtin/grep.c b/builtin/grep.c\n> index 09ca4c9..56aba7b 100644\n> --- a/builtin/grep.c\n> +++ b/builtin/grep.c\n> @@ -782,6 +782,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n>   \t\t\tN_(\"print empty line between matches from different files\")),\n>   \t\tOPT_BOOLEAN(0, \"heading\", &opt.heading,\n>   \t\t\tN_(\"show filename only once above matches from same file\")),\n> +\t\tOPT_BOOLEAN('o', \"only-matching\", &opt.only_matching,\n> +\t\t\tN_(\"show only the matching part of a matched line\")),\n>   \t\tOPT_GROUP(\"\"),\n>   \t\tOPT_CALLBACK('C', \"context\", &opt, N_(\"n\"),\n>   \t\t\tN_(\"show <n> context lines before and after matches\"),\n> diff --git a/grep.c b/grep.c\n> index 04e3ec6..9fc888e 100644\n> --- a/grep.c\n> +++ b/grep.c\n> @@ -827,7 +827,9 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,\n>   \t\t\tif (match.rm_so == match.rm_eo)\n>   \t\t\t\tbreak;\n>\n> -\t\t\toutput_color(opt, bol, match.rm_so, line_color);\n> +\t\t\tif (opt->only_matching == 0)\n> +\t\t\t\toutput_color(opt, bol, match.rm_so,\n> +\t\t\t\t\t     line_color);\n>   \t\t\toutput_color(opt, bol + match.rm_so,\n>   \t\t\t\t     match.rm_eo - match.rm_so,\n>   \t\t\t\t     opt->color_match);\n> @@ -837,7 +839,8 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,\n>   \t\t}\n>   \t\t*eol = ch;\n>   \t}\n> -\toutput_color(opt, bol, rest, line_color);\n> +\tif (opt->only_matching == 0)\n> +\t\toutput_color(opt, bol, rest, line_color);\n>   \topt->output(opt, \"\\n\", 1);\n>   }\n\nThe implementation keeps only the coloured parts.  However, they are not \nnecessarily the same as the matching parts.  This is more complicated \nwith git grep than with regular grep because the former has the \nadditional options --and and --not.  Consider this:\n\n\t$ git grep --not -e bla --or --not -e blub\n\nLines with only either \"bla\" or \"blub\" (or none of them) will be shown, \nlines with both not.  Both \"bla\" and \"blub\" will be highlighted.  The \nmatching part is always the whole shown line.\n\nRené\n"}]}