@@ -431,6 +431,52 @@ const char *show_ident_date(const struct ident_split *ident,
431
431
return show_date (date , tz , mode );
432
432
}
433
433
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
+
434
480
void pp_user_info (struct pretty_print_context * pp ,
435
481
const char * what , struct strbuf * sb ,
436
482
const char * line , const char * encoding )
@@ -496,9 +542,26 @@ void pp_user_info(struct pretty_print_context *pp,
496
542
strbuf_addch (sb , '\n' );
497
543
strbuf_addf (sb , " <%.*s>\n" , (int )maillen , mailbuf );
498
544
} 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 );
502
565
}
503
566
504
567
switch (pp -> fmt ) {
@@ -1939,8 +2002,9 @@ static int pp_utf8_width(const char *start, const char *end)
1939
2002
return width ;
1940
2003
}
1941
2004
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 )
1944
2008
{
1945
2009
const char * tab ;
1946
2010
@@ -1957,7 +2021,9 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
1957
2021
break ;
1958
2022
1959
2023
/* 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 );
1961
2027
1962
2028
/* .. and the de-tabified tab */
1963
2029
strbuf_addchars (sb , ' ' , tabwidth - (width % tabwidth ));
@@ -1972,7 +2038,8 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
1972
2038
* worrying about width - there's nothing more to
1973
2039
* align.
1974
2040
*/
1975
- strbuf_add (sb , line , linelen );
2041
+ append_line_with_color (sb , opt , line , linelen , color , GREP_CONTEXT_BODY ,
2042
+ GREP_HEADER_FIELD_MAX );
1976
2043
}
1977
2044
1978
2045
/*
@@ -1984,11 +2051,16 @@ static void pp_handle_indent(struct pretty_print_context *pp,
1984
2051
struct strbuf * sb , int indent ,
1985
2052
const char * line , int linelen )
1986
2053
{
2054
+ struct grep_opt * opt = pp -> rev ? & pp -> rev -> grep_filter : NULL ;
2055
+
1987
2056
strbuf_addchars (sb , ' ' , indent );
1988
2057
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 );
1990
2060
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 );
1992
2064
}
1993
2065
1994
2066
static int is_mboxrd_from (const char * line , int len )
@@ -2006,7 +2078,9 @@ void pp_remainder(struct pretty_print_context *pp,
2006
2078
struct strbuf * sb ,
2007
2079
int indent )
2008
2080
{
2081
+ struct grep_opt * opt = pp -> rev ? & pp -> rev -> grep_filter : NULL ;
2009
2082
int first = 1 ;
2083
+
2010
2084
for (;;) {
2011
2085
const char * line = * msg_p ;
2012
2086
int linelen = get_one_line (line );
@@ -2027,14 +2101,17 @@ void pp_remainder(struct pretty_print_context *pp,
2027
2101
if (indent )
2028
2102
pp_handle_indent (pp , sb , indent , line , linelen );
2029
2103
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 );
2032
2107
else {
2033
2108
if (pp -> fmt == CMIT_FMT_MBOXRD &&
2034
2109
is_mboxrd_from (line , linelen ))
2035
2110
strbuf_addch (sb , '>' );
2036
2111
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 );
2038
2115
}
2039
2116
strbuf_addch (sb , '\n' );
2040
2117
}
0 commit comments