@@ -778,6 +778,7 @@ struct format_commit_context {
778778 char * message ;
779779 char * commit_encoding ;
780780 size_t width , indent1 , indent2 ;
781+ int auto_color ;
781782
782783 /* These offsets are relative to the start of the commit message. */
783784 struct chunk author ;
@@ -1005,7 +1006,20 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10051006 /* these are independent of the commit */
10061007 switch (placeholder [0 ]) {
10071008 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+ }
10091023 case 'n' : /* newline */
10101024 strbuf_addch (sb , '\n' );
10111025 return 1 ;
@@ -1051,13 +1065,19 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10511065
10521066 switch (placeholder [0 ]) {
10531067 case 'H' : /* commit hash */
1068+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_COMMIT ));
10541069 strbuf_addstr (sb , sha1_to_hex (commit -> object .sha1 ));
1070+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_RESET ));
10551071 return 1 ;
10561072 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 ));
10581076 return 1 ;
1077+ }
10591078 strbuf_addstr (sb , find_unique_abbrev (commit -> object .sha1 ,
10601079 c -> pretty_ctx -> abbrev ));
1080+ strbuf_addstr (sb , diff_get_color (c -> auto_color , DIFF_RESET ));
10611081 c -> abbrev_commit_hash .len = sb -> len - c -> abbrev_commit_hash .off ;
10621082 return 1 ;
10631083 case 'T' : /* tree hash */
@@ -1095,7 +1115,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10951115 return 1 ;
10961116 case 'd' :
10971117 load_ref_decorations (DECORATE_SHORT_REFS );
1098- format_decorations (sb , commit , 0 );
1118+ format_decorations (sb , commit , c -> auto_color );
10991119 return 1 ;
11001120 case 'g' : /* reflog info */
11011121 switch (placeholder [1 ]) {
0 commit comments