log --format: teach %C(auto,black) to respect color config

Traditionally, %C(color attr) always emitted the ANSI color sequence; it was up to the scripts that wanted to conditionally color their output to omit %C(...) specifier when they do not want colors. Optionally allow "auto," to be prefixed to the color, so that the output is colored iff we would color regular "log" output (e.g., taking into account color.* and --color command line options). Tests and pretty_context bits by Jeff King <peff@peff.net>. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 17, 2012 at 17:56 UTC 30825178fb72e3664bd1bda7c02c62e300e2e5ce
5 files changed +72 -4
Documentation/pretty-formats.txt
+5 -1
@@ -144,7 +144,11 @@ The placeholders are:
144 - '%Cgreen': switch color to green
145 - '%Cblue': switch color to blue
146 - '%Creset': reset color
147 -- '%C(...)': color specification, as described in color.branch.* config option
147 +- '%C(...)': color specification, as described in color.branch.* config option;
148 + adding `auto,` at the beginning will emit color only when colors are
149 + enabled for log output (by `color.diff`, `color.ui`, or `--color`, and
150 + respecting the `auto` settings of the former if we are going to a
151 + terminal)
152 - '%m': left, right or boundary mark
153 - '%n': newline
154 - '%%': a raw '%'
commit.h
+1
@@ -89,6 +89,7 @@ struct pretty_print_context {
89 int show_notes;
90 struct reflog_walk_info *reflog_info;
91 const char *output_encoding;
92 + int color;
93 };
94
95 struct userformat_want {
log-tree.c
+1
@@ -660,6 +660,7 @@ void show_log(struct rev_info *opt)
660 ctx.preserve_subject = opt->preserve_subject;
661 ctx.reflog_info = opt->reflog_info;
662 ctx.fmt = opt->commit_format;
663 + ctx.color = opt->diffopt.use_color;
664 pretty_print_commit(&ctx, commit, &msgbuf);
665
666 if (opt->add_signoff)
pretty.c
+10 -3
@@ -960,12 +960,19 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
960 switch (placeholder[0]) {
961 case 'C':
962 if (placeholder[1] == '(') {
963 - const char *end = strchr(placeholder + 2, ')');
963 + const char *begin = placeholder + 2;
964 + const char *end = strchr(begin, ')');
965 char color[COLOR_MAXLEN];
966 +
967 if (!end)
968 return 0;
967 - color_parse_mem(placeholder + 2,
968 - end - (placeholder + 2),
969 + if (!memcmp(begin, "auto,", 5)) {
970 + if (!want_color(c->pretty_ctx->color))
971 + return end - placeholder + 1;
972 + begin += 5;
973 + }
974 + color_parse_mem(begin,
975 + end - begin,
976 "--pretty format", color);
977 strbuf_addstr(sb, color);
978 return end - placeholder + 1;
t/t6006-rev-list-format.sh
+55
@@ -3,6 +3,7 @@
3 test_description='git rev-list --pretty=format test'
4
5 . ./test-lib.sh
6 +. "$TEST_DIRECTORY"/lib-terminal.sh
7
8 test_tick
9 test_expect_success 'setup' '
@@ -19,6 +20,18 @@ test_format () {
20 "
21 }
22
23 +# Feed to --format to provide predictable colored sequences.
24 +AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
25 +has_color () {
26 + printf '\033[31mfoo\033[m\n' >expect &&
27 + test_cmp expect "$1"
28 +}
29 +
30 +has_no_color () {
31 + echo foo >expect &&
32 + test_cmp expect "$1"
33 +}
34 +
35 test_format percent %%h <<'EOF'
36 commit 131a310eb913d107dd3c09a65d1651175898735d
37 %h
@@ -124,6 +137,48 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
137 foo
138 EOF
139
140 +test_expect_success '%C(auto) does not enable color by default' '
141 + git log --format=$AUTO_COLOR -1 >actual &&
142 + has_no_color actual
143 +'
144 +
145 +test_expect_success '%C(auto) enables colors for color.diff' '
146 + git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
147 + has_color actual
148 +'
149 +
150 +test_expect_success '%C(auto) enables colors for color.ui' '
151 + git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
152 + has_color actual
153 +'
154 +
155 +test_expect_success '%C(auto) respects --color' '
156 + git log --format=$AUTO_COLOR -1 --color >actual &&
157 + has_color actual
158 +'
159 +
160 +test_expect_success '%C(auto) respects --no-color' '
161 + git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
162 + has_no_color actual
163 +'
164 +
165 +test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
166 + (
167 + TERM=vt100 && export TERM &&
168 + test_terminal \
169 + git log --format=$AUTO_COLOR -1 --color=auto >actual &&
170 + has_color actual
171 + )
172 +'
173 +
174 +test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
175 + (
176 + TERM=vt100 && export TERM &&
177 + git log --format=$AUTO_COLOR -1 --color=auto >actual &&
178 + has_no_color actual
179 + )
180 +'
181 +
182 cat >commit-msg <<'EOF'
183 Test printing of complex bodies
184