@@ -778,6 +778,7 @@ struct format_commit_context {
778
778
char * message ;
779
779
char * commit_encoding ;
780
780
size_t width , indent1 , indent2 ;
781
+ int auto_color ;
781
782
782
783
/* These offsets are relative to the start of the commit message. */
783
784
struct chunk author ;
@@ -1005,7 +1006,20 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1005
1006
/* these are independent of the commit */
1006
1007
switch (placeholder [0 ]) {
1007
1008
case 'C' :
1008
- return parse_color (sb , placeholder , c );
1009
+ if (!prefixcmp (placeholder + 1 , "(auto)" )) {
1010
+ c -> auto_color = 1 ;
1011
+ return 7 ; /* consumed 7 bytes, "C(auto)" */
1012
+ } else {
1013
+ int ret = parse_color (sb , placeholder , c );
1014
+ if (ret )
1015
+ c -> auto_color = 0 ;
1016
+ /*
1017
+ * Otherwise, we decided to treat %C<unknown>
1018
+ * as a literal string, and the previous
1019
+ * %C(auto) is still valid.
1020
+ */
1021
+ return ret ;
1022
+ }
1009
1023
case 'n' : /* newline */
1010
1024
strbuf_addch (sb , '\n' );
1011
1025
return 1 ;
@@ -1051,13 +1065,19 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1051
1065
1052
1066
switch (placeholder [0 ]) {
1053
1067
case 'H' : /* commit hash */
1068
+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_COMMIT ));
1054
1069
strbuf_addstr (sb , sha1_to_hex (commit -> object .sha1 ));
1070
+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_RESET ));
1055
1071
return 1 ;
1056
1072
case 'h' : /* abbreviated commit hash */
1057
- if (add_again (sb , & c -> abbrev_commit_hash ))
1073
+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_COMMIT ));
1074
+ if (add_again (sb , & c -> abbrev_commit_hash )) {
1075
+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_RESET ));
1058
1076
return 1 ;
1077
+ }
1059
1078
strbuf_addstr (sb , find_unique_abbrev (commit -> object .sha1 ,
1060
1079
c -> pretty_ctx -> abbrev ));
1080
+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_RESET ));
1061
1081
c -> abbrev_commit_hash .len = sb -> len - c -> abbrev_commit_hash .off ;
1062
1082
return 1 ;
1063
1083
case 'T' : /* tree hash */
@@ -1095,7 +1115,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1095
1115
return 1 ;
1096
1116
case 'd' :
1097
1117
load_ref_decorations (DECORATE_SHORT_REFS );
1098
- format_decorations (sb , commit , 0 );
1118
+ format_decorations (sb , commit , c -> auto_color );
1099
1119
return 1 ;
1100
1120
case 'g' : /* reflog info */
1101
1121
switch (placeholder [1 ]) {
0 commit comments