Skip to content

Commit

Permalink
log --format: teach %C(auto,black) to respect color config
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Dec 18, 2012
1 parent 2581ad5 commit 3082517
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Documentation/pretty-formats.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ The placeholders are:
- '%Cgreen': switch color to green
- '%Cblue': switch color to blue
- '%Creset': reset color
- '%C(...)': color specification, as described in color.branch.* config option
- '%C(...)': color specification, as described in color.branch.* config option;
adding `auto,` at the beginning will emit color only when colors are
enabled for log output (by `color.diff`, `color.ui`, or `--color`, and
respecting the `auto` settings of the former if we are going to a
terminal)
- '%m': left, right or boundary mark
- '%n': newline
- '%%': a raw '%'
Expand Down
1 change: 1 addition & 0 deletions commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct pretty_print_context {
int show_notes;
struct reflog_walk_info *reflog_info;
const char *output_encoding;
int color;
};

struct userformat_want {
Expand Down
1 change: 1 addition & 0 deletions log-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ void show_log(struct rev_info *opt)
ctx.preserve_subject = opt->preserve_subject;
ctx.reflog_info = opt->reflog_info;
ctx.fmt = opt->commit_format;
ctx.color = opt->diffopt.use_color;
pretty_print_commit(&ctx, commit, &msgbuf);

if (opt->add_signoff)
Expand Down
13 changes: 10 additions & 3 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,19 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
switch (placeholder[0]) {
case 'C':
if (placeholder[1] == '(') {
const char *end = strchr(placeholder + 2, ')');
const char *begin = placeholder + 2;
const char *end = strchr(begin, ')');
char color[COLOR_MAXLEN];

if (!end)
return 0;
color_parse_mem(placeholder + 2,
end - (placeholder + 2),
if (!memcmp(begin, "auto,", 5)) {
if (!want_color(c->pretty_ctx->color))
return end - placeholder + 1;
begin += 5;
}
color_parse_mem(begin,
end - begin,
"--pretty format", color);
strbuf_addstr(sb, color);
return end - placeholder + 1;
Expand Down
55 changes: 55 additions & 0 deletions t/t6006-rev-list-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
test_description='git rev-list --pretty=format test'

. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-terminal.sh

test_tick
test_expect_success 'setup' '
Expand All @@ -19,6 +20,18 @@ test_format () {
"
}

# Feed to --format to provide predictable colored sequences.
AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
has_color () {
printf '\033[31mfoo\033[m\n' >expect &&
test_cmp expect "$1"
}

has_no_color () {
echo foo >expect &&
test_cmp expect "$1"
}

test_format percent %%h <<'EOF'
commit 131a310eb913d107dd3c09a65d1651175898735d
%h
Expand Down Expand Up @@ -124,6 +137,48 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foo
EOF

test_expect_success '%C(auto) does not enable color by default' '
git log --format=$AUTO_COLOR -1 >actual &&
has_no_color actual
'

test_expect_success '%C(auto) enables colors for color.diff' '
git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
has_color actual
'

test_expect_success '%C(auto) enables colors for color.ui' '
git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
has_color actual
'

test_expect_success '%C(auto) respects --color' '
git log --format=$AUTO_COLOR -1 --color >actual &&
has_color actual
'

test_expect_success '%C(auto) respects --no-color' '
git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
has_no_color actual
'

test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
(
TERM=vt100 && export TERM &&
test_terminal \
git log --format=$AUTO_COLOR -1 --color=auto >actual &&
has_color actual
)
'

test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
(
TERM=vt100 && export TERM &&
git log --format=$AUTO_COLOR -1 --color=auto >actual &&
has_no_color actual
)
'

cat >commit-msg <<'EOF'
Test printing of complex bodies
Expand Down

0 comments on commit 3082517

Please sign in to comment.