@@ -431,6 +431,52 @@ const char *show_ident_date(const struct ident_split *ident,
431431 return show_date (date , tz , mode );
432432}
433433
434+ static inline void strbuf_add_with_color (struct strbuf * sb , const char * color ,
435+ const char * buf , size_t buflen )
436+ {
437+ strbuf_addstr (sb , color );
438+ strbuf_add (sb , buf , buflen );
439+ if (* color )
440+ strbuf_addstr (sb , GIT_COLOR_RESET );
441+ }
442+
443+ static void append_line_with_color (struct strbuf * sb , struct grep_opt * opt ,
444+ const char * line , size_t linelen ,
445+ int color , enum grep_context ctx ,
446+ enum grep_header_field field )
447+ {
448+ const char * buf , * eol , * line_color , * match_color ;
449+ regmatch_t match ;
450+ int eflags = 0 ;
451+
452+ buf = line ;
453+ eol = buf + linelen ;
454+
455+ if (!opt || !want_color (color ) || opt -> invert )
456+ goto end ;
457+
458+ line_color = opt -> colors [GREP_COLOR_SELECTED ];
459+ match_color = opt -> colors [GREP_COLOR_MATCH_SELECTED ];
460+
461+ while (grep_next_match (opt , buf , eol , ctx , & match , field , eflags )) {
462+ if (match .rm_so == match .rm_eo )
463+ break ;
464+
465+ strbuf_add_with_color (sb , line_color , buf , match .rm_so );
466+ strbuf_add_with_color (sb , match_color , buf + match .rm_so ,
467+ match .rm_eo - match .rm_so );
468+ buf += match .rm_eo ;
469+ eflags = REG_NOTBOL ;
470+ }
471+
472+ if (eflags )
473+ strbuf_add_with_color (sb , line_color , buf , eol - buf );
474+ else {
475+ end :
476+ strbuf_add (sb , buf , eol - buf );
477+ }
478+ }
479+
434480void pp_user_info (struct pretty_print_context * pp ,
435481 const char * what , struct strbuf * sb ,
436482 const char * line , const char * encoding )
@@ -496,9 +542,26 @@ void pp_user_info(struct pretty_print_context *pp,
496542 strbuf_addch (sb , '\n' );
497543 strbuf_addf (sb , " <%.*s>\n" , (int )maillen , mailbuf );
498544 } else {
499- strbuf_addf (sb , "%s: %.*s%.*s <%.*s>\n" , what ,
500- (pp -> fmt == CMIT_FMT_FULLER ) ? 4 : 0 , " " ,
501- (int )namelen , namebuf , (int )maillen , mailbuf );
545+ struct strbuf id = STRBUF_INIT ;
546+ enum grep_header_field field = GREP_HEADER_FIELD_MAX ;
547+ struct grep_opt * opt = pp -> rev ? & pp -> rev -> grep_filter : NULL ;
548+
549+ if (!strcmp (what , "Author" ))
550+ field = GREP_HEADER_AUTHOR ;
551+ else if (!strcmp (what , "Commit" ))
552+ field = GREP_HEADER_COMMITTER ;
553+
554+ strbuf_addf (sb , "%s: " , what );
555+ if (pp -> fmt == CMIT_FMT_FULLER )
556+ strbuf_addchars (sb , ' ' , 4 );
557+
558+ strbuf_addf (& id , "%.*s <%.*s>" , (int )namelen , namebuf ,
559+ (int )maillen , mailbuf );
560+
561+ append_line_with_color (sb , opt , id .buf , id .len , pp -> color ,
562+ GREP_CONTEXT_HEAD , field );
563+ strbuf_addch (sb , '\n' );
564+ strbuf_release (& id );
502565 }
503566
504567 switch (pp -> fmt ) {
@@ -1939,8 +2002,9 @@ static int pp_utf8_width(const char *start, const char *end)
19392002 return width ;
19402003}
19412004
1942- static void strbuf_add_tabexpand (struct strbuf * sb , int tabwidth ,
1943- const char * line , int linelen )
2005+ static void strbuf_add_tabexpand (struct strbuf * sb , struct grep_opt * opt ,
2006+ int color , int tabwidth , const char * line ,
2007+ int linelen )
19442008{
19452009 const char * tab ;
19462010
@@ -1957,7 +2021,9 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
19572021 break ;
19582022
19592023 /* Output the data .. */
1960- strbuf_add (sb , line , tab - line );
2024+ append_line_with_color (sb , opt , line , tab - line , color ,
2025+ GREP_CONTEXT_BODY ,
2026+ GREP_HEADER_FIELD_MAX );
19612027
19622028 /* .. and the de-tabified tab */
19632029 strbuf_addchars (sb , ' ' , tabwidth - (width % tabwidth ));
@@ -1972,7 +2038,8 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
19722038 * worrying about width - there's nothing more to
19732039 * align.
19742040 */
1975- strbuf_add (sb , line , linelen );
2041+ append_line_with_color (sb , opt , line , linelen , color , GREP_CONTEXT_BODY ,
2042+ GREP_HEADER_FIELD_MAX );
19762043}
19772044
19782045/*
@@ -1984,11 +2051,16 @@ static void pp_handle_indent(struct pretty_print_context *pp,
19842051 struct strbuf * sb , int indent ,
19852052 const char * line , int linelen )
19862053{
2054+ struct grep_opt * opt = pp -> rev ? & pp -> rev -> grep_filter : NULL ;
2055+
19872056 strbuf_addchars (sb , ' ' , indent );
19882057 if (pp -> expand_tabs_in_log )
1989- strbuf_add_tabexpand (sb , pp -> expand_tabs_in_log , line , linelen );
2058+ strbuf_add_tabexpand (sb , opt , pp -> color , pp -> expand_tabs_in_log ,
2059+ line , linelen );
19902060 else
1991- strbuf_add (sb , line , linelen );
2061+ append_line_with_color (sb , opt , line , linelen , pp -> color ,
2062+ GREP_CONTEXT_BODY ,
2063+ GREP_HEADER_FIELD_MAX );
19922064}
19932065
19942066static int is_mboxrd_from (const char * line , int len )
@@ -2006,7 +2078,9 @@ void pp_remainder(struct pretty_print_context *pp,
20062078 struct strbuf * sb ,
20072079 int indent )
20082080{
2081+ struct grep_opt * opt = pp -> rev ? & pp -> rev -> grep_filter : NULL ;
20092082 int first = 1 ;
2083+
20102084 for (;;) {
20112085 const char * line = * msg_p ;
20122086 int linelen = get_one_line (line );
@@ -2027,14 +2101,17 @@ void pp_remainder(struct pretty_print_context *pp,
20272101 if (indent )
20282102 pp_handle_indent (pp , sb , indent , line , linelen );
20292103 else if (pp -> expand_tabs_in_log )
2030- strbuf_add_tabexpand (sb , pp -> expand_tabs_in_log ,
2031- line , linelen );
2104+ strbuf_add_tabexpand (sb , opt , pp -> color ,
2105+ pp -> expand_tabs_in_log , line ,
2106+ linelen );
20322107 else {
20332108 if (pp -> fmt == CMIT_FMT_MBOXRD &&
20342109 is_mboxrd_from (line , linelen ))
20352110 strbuf_addch (sb , '>' );
20362111
2037- strbuf_add (sb , line , linelen );
2112+ append_line_with_color (sb , opt , line , linelen ,
2113+ pp -> color , GREP_CONTEXT_BODY ,
2114+ GREP_HEADER_FIELD_MAX );
20382115 }
20392116 strbuf_addch (sb , '\n' );
20402117 }
0 commit comments