Merge branch 'tb/grep-only-matching'
authorJunio C Hamano <gitster@pobox.com>
Thu, 2 Aug 2018 22:30:44 +0000 (15:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Aug 2018 22:30:44 +0000 (15:30 -0700)
"git grep" learned the "--only-matching" option.

* tb/grep-only-matching:
grep.c: teach 'git grep --only-matching'
grep.c: extract show_line_header()

1  2 
builtin/grep.c
grep.c
grep.h
t/t7810-grep.sh
diff --cc builtin/grep.c
Simple merge
diff --cc grep.c
index cd7fc6f66cadfcb38ad73f171b97a419610aad37,49a744f96b373c6b90cde4d2d090eea2aa18cbc3..2b26cee08d559ceba6dd5a83ae90aaa0155ebca8
--- 1/grep.c
--- 2/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;
        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)
diff --cc grep.h
Simple merge
diff --cc t/t7810-grep.sh
Simple merge