Skip to content

Commit b15a3e0

Browse files
ethomsongitster
authored andcommitted
format_commit_message: honor color=auto for %C(auto)
git-log(1) documents that when specifying the `%C(auto)` format placeholder will "turn on auto coloring on the next %placeholders until the color is switched again." However, when `%C(auto)` is used, the present implementation will turn colors on unconditionally (even if the color configuration is turned off for the current context - for example, `--no-color` was specified or the color is `auto` and the output is not a tty). Update `format_commit_one` to examine the current context when a format string of `%C(auto)` is specified, which ensures that we will not unconditionally write colors. This brings that behavior in line with the behavior of `%C(auto,<colorname>)`, and allows the user the ability to specify that color should be displayed only when the output is a tty. Additionally, add a test for `%C(auto)` and update the existing tests for `%C(auto,...)` as they were misidentified as being applicable to `%C(auto)`. Tests from Jeff King. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Edward Thomson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0f8e831 commit b15a3e0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

pretty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10611061
switch (placeholder[0]) {
10621062
case 'C':
10631063
if (starts_with(placeholder + 1, "(auto)")) {
1064-
c->auto_color = 1;
1064+
c->auto_color = want_color(c->pretty_ctx->color);
10651065
return 7; /* consumed 7 bytes, "C(auto)" */
10661066
} else {
10671067
int ret = parse_color(sb, placeholder, c);

t/t6006-rev-list-format.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,45 +184,57 @@ commit $head1
184184
foo
185185
EOF
186186

187-
test_expect_success '%C(auto) does not enable color by default' '
187+
test_expect_success '%C(auto,...) does not enable color by default' '
188188
git log --format=$AUTO_COLOR -1 >actual &&
189189
has_no_color actual
190190
'
191191

192-
test_expect_success '%C(auto) enables colors for color.diff' '
192+
test_expect_success '%C(auto,...) enables colors for color.diff' '
193193
git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
194194
has_color actual
195195
'
196196

197-
test_expect_success '%C(auto) enables colors for color.ui' '
197+
test_expect_success '%C(auto,...) enables colors for color.ui' '
198198
git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
199199
has_color actual
200200
'
201201

202-
test_expect_success '%C(auto) respects --color' '
202+
test_expect_success '%C(auto,...) respects --color' '
203203
git log --format=$AUTO_COLOR -1 --color >actual &&
204204
has_color actual
205205
'
206206

207-
test_expect_success '%C(auto) respects --no-color' '
207+
test_expect_success '%C(auto,...) respects --no-color' '
208208
git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
209209
has_no_color actual
210210
'
211211

212-
test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
212+
test_expect_success TTY '%C(auto,...) respects --color=auto (stdout is tty)' '
213213
test_terminal env TERM=vt100 \
214214
git log --format=$AUTO_COLOR -1 --color=auto >actual &&
215215
has_color actual
216216
'
217217

218-
test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
218+
test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
219219
(
220220
TERM=vt100 && export TERM &&
221221
git log --format=$AUTO_COLOR -1 --color=auto >actual &&
222222
has_no_color actual
223223
)
224224
'
225225

226+
test_expect_success '%C(auto) respects --color' '
227+
git log --color --format="%C(auto)%H" -1 >actual &&
228+
printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
229+
test_cmp expect actual
230+
'
231+
232+
test_expect_success '%C(auto) respects --no-color' '
233+
git log --no-color --format="%C(auto)%H" -1 >actual &&
234+
git rev-parse HEAD >expect &&
235+
test_cmp expect actual
236+
'
237+
226238
iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
227239
Test printing of complex bodies
228240

0 commit comments

Comments
 (0)