Skip to content

Commit 3082517

Browse files
committed
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 <[email protected]>. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2581ad5 commit 3082517

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

Documentation/pretty-formats.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ The placeholders are:
144144
- '%Cgreen': switch color to green
145145
- '%Cblue': switch color to blue
146146
- '%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)
148152
- '%m': left, right or boundary mark
149153
- '%n': newline
150154
- '%%': a raw '%'

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct pretty_print_context {
8989
int show_notes;
9090
struct reflog_walk_info *reflog_info;
9191
const char *output_encoding;
92+
int color;
9293
};
9394

9495
struct userformat_want {

log-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ void show_log(struct rev_info *opt)
660660
ctx.preserve_subject = opt->preserve_subject;
661661
ctx.reflog_info = opt->reflog_info;
662662
ctx.fmt = opt->commit_format;
663+
ctx.color = opt->diffopt.use_color;
663664
pretty_print_commit(&ctx, commit, &msgbuf);
664665

665666
if (opt->add_signoff)

pretty.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -960,12 +960,19 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
960960
switch (placeholder[0]) {
961961
case 'C':
962962
if (placeholder[1] == '(') {
963-
const char *end = strchr(placeholder + 2, ')');
963+
const char *begin = placeholder + 2;
964+
const char *end = strchr(begin, ')');
964965
char color[COLOR_MAXLEN];
966+
965967
if (!end)
966968
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,
969976
"--pretty format", color);
970977
strbuf_addstr(sb, color);
971978
return end - placeholder + 1;

t/t6006-rev-list-format.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test_description='git rev-list --pretty=format test'
44

55
. ./test-lib.sh
6+
. "$TEST_DIRECTORY"/lib-terminal.sh
67

78
test_tick
89
test_expect_success 'setup' '
@@ -19,6 +20,18 @@ test_format () {
1920
"
2021
}
2122

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+
2235
test_format percent %%h <<'EOF'
2336
commit 131a310eb913d107dd3c09a65d1651175898735d
2437
%h
@@ -124,6 +137,48 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
124137
foo
125138
EOF
126139

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+
127182
cat >commit-msg <<'EOF'
128183
Test printing of complex bodies
129184

0 commit comments

Comments
 (0)