From: Junio C Hamano Date: Thu, 2 Aug 2018 22:30:44 +0000 (-0700) Subject: Merge branch 'tb/grep-only-matching' X-Git-Tag: v2.19.0-rc0~99 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=87ece7ce1133ebc5d339cad5f6f92c02e5fc712d;p=thirdparty%2Fgit.git Merge branch 'tb/grep-only-matching' "git grep" learned the "--only-matching" option. * tb/grep-only-matching: grep.c: teach 'git grep --only-matching' grep.c: extract show_line_header() --- 87ece7ce1133ebc5d339cad5f6f92c02e5fc712d diff --cc grep.c index cd7fc6f66c,49a744f96b..2b26cee08d --- a/grep.c +++ b/grep.c @@@ -56,15 -42,16 +56,16 @@@ void init_grep_defaults(void opt->pathname = 1; opt->max_depth = -1; opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED; - color_set(opt->color_context, ""); - color_set(opt->color_filename, ""); - color_set(opt->color_function, ""); - color_set(opt->color_lineno, ""); - color_set(opt->color_columnno, ""); - color_set(opt->color_match_context, GIT_COLOR_BOLD_RED); - color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED); - color_set(opt->color_selected, ""); - color_set(opt->color_sep, GIT_COLOR_CYAN); + color_set(opt->colors[GREP_COLOR_CONTEXT], ""); + color_set(opt->colors[GREP_COLOR_FILENAME], ""); + color_set(opt->colors[GREP_COLOR_FUNCTION], ""); + color_set(opt->colors[GREP_COLOR_LINENO], ""); + color_set(opt->colors[GREP_COLOR_COLUMNNO], ""); + color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED); + color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED); + color_set(opt->colors[GREP_COLOR_SELECTED], ""); + color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN); + opt->only_matching = 0; opt->color = -1; opt->output = std_output; } @@@ -1404,28 -1412,11 +1406,11 @@@ static int next_match(struct grep_opt * return hit; } - static void show_line(struct grep_opt *opt, char *bol, char *eol, - const char *name, unsigned lno, ssize_t cno, char sign) + static void show_line_header(struct grep_opt *opt, const char *name, + unsigned lno, ssize_t cno, char sign) { - int rest = eol - bol; - const char *match_color, *line_color = NULL; - - if (opt->file_break && opt->last_shown == 0) { - if (opt->show_hunk_mark) - opt->output(opt, "\n", 1); - } else if (opt->pre_context || opt->post_context || opt->funcbody) { - if (opt->last_shown == 0) { - if (opt->show_hunk_mark) { - output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); - opt->output(opt, "\n", 1); - } - } else if (lno > opt->last_shown + 1) { - output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); - opt->output(opt, "\n", 1); - } - } if (opt->heading && opt->last_shown == 0) { - output_color(opt, name, strlen(name), opt->color_filename); + output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); opt->output(opt, "\n", 1); } opt->last_shown = lno; @@@ -1448,25 -1439,57 +1433,57 @@@ if (opt->columnnum && cno) { char buf[32]; xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno); - output_color(opt, buf, strlen(buf), opt->color_columnno); + output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]); output_sep(opt, sign); } - if (opt->color) { + } + + static void show_line(struct grep_opt *opt, char *bol, char *eol, + const char *name, unsigned lno, ssize_t cno, char sign) + { + int rest = eol - bol; + const char *match_color = NULL; + const char *line_color = NULL; + + if (opt->file_break && opt->last_shown == 0) { + if (opt->show_hunk_mark) + opt->output(opt, "\n", 1); + } else if (opt->pre_context || opt->post_context || opt->funcbody) { + if (opt->last_shown == 0) { + if (opt->show_hunk_mark) { - output_color(opt, "--", 2, opt->color_sep); ++ output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); + opt->output(opt, "\n", 1); + } + } else if (lno > opt->last_shown + 1) { - output_color(opt, "--", 2, opt->color_sep); ++ output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); + opt->output(opt, "\n", 1); + } + } + if (!opt->only_matching) { + /* + * In case the line we're being called with contains more than + * one match, leave printing each header to the loop below. + */ + show_line_header(opt, name, lno, cno, sign); + } + if (opt->color || opt->only_matching) { regmatch_t match; enum grep_context ctx = GREP_CONTEXT_BODY; int ch = *eol; int eflags = 0; - if (sign == ':') - match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; - else - match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT]; - if (sign == ':') - line_color = opt->colors[GREP_COLOR_SELECTED]; - else if (sign == '-') - line_color = opt->colors[GREP_COLOR_CONTEXT]; - else if (sign == '=') - line_color = opt->colors[GREP_COLOR_FUNCTION]; + if (opt->color) { + if (sign == ':') - match_color = opt->color_match_selected; ++ match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; + else - match_color = opt->color_match_context; ++ match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT]; + if (sign == ':') - line_color = opt->color_selected; ++ line_color = opt->colors[GREP_COLOR_SELECTED]; + else if (sign == '-') - line_color = opt->color_context; ++ line_color = opt->colors[GREP_COLOR_CONTEXT]; + else if (sign == '=') - line_color = opt->color_function; ++ line_color = opt->colors[GREP_COLOR_FUNCTION]; + } *eol = '\0'; while (next_match(opt, bol, eol, ctx, &match, eflags)) { if (match.rm_so == match.rm_eo)