Skip to content

Commit 946a5ae

Browse files
committed
Merge branch 'jc/format-color-auto'
Introduce "log --format=%C(auto,blue)Foo%C(auto,reset)" that does not color its output when writing to a non-terminal. * jc/format-color-auto: log --format: teach %C(auto,black) to respect color config t6006: clean up whitespace
2 parents d2638e1 + 3082517 commit 946a5ae

5 files changed

Lines changed: 76 additions & 8 deletions

File tree

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
char *notes_message;
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
@@ -671,6 +671,7 @@ void show_log(struct rev_info *opt)
671671
ctx.preserve_subject = opt->preserve_subject;
672672
ctx.reflog_info = opt->reflog_info;
673673
ctx.fmt = opt->commit_format;
674+
ctx.color = opt->diffopt.use_color;
674675
pretty_print_commit(&ctx, commit, &msgbuf);
675676

676677
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: 59 additions & 4 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' '
@@ -11,12 +12,24 @@ touch foo && git add foo && git commit -m "added foo" &&
1112
'
1213

1314
# usage: test_format name format_string <expected_output
14-
test_format() {
15+
test_format () {
1516
cat >expect.$1
1617
test_expect_success "format $1" "
17-
git rev-list --pretty=format:'$2' master >output.$1 &&
18-
test_cmp expect.$1 output.$1
19-
"
18+
git rev-list --pretty=format:'$2' master >output.$1 &&
19+
test_cmp expect.$1 output.$1
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"
2033
}
2134

2235
test_format percent %%h <<'EOF'
@@ -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)