mirror of
https://github.com/git/git.git
synced 2025-03-15 08:12:22 +00:00
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
This commit is contained in:
commit
946a5aee3e
@ -144,7 +144,11 @@ The placeholders are:
|
|||||||
- '%Cgreen': switch color to green
|
- '%Cgreen': switch color to green
|
||||||
- '%Cblue': switch color to blue
|
- '%Cblue': switch color to blue
|
||||||
- '%Creset': reset color
|
- '%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
|
- '%m': left, right or boundary mark
|
||||||
- '%n': newline
|
- '%n': newline
|
||||||
- '%%': a raw '%'
|
- '%%': a raw '%'
|
||||||
|
1
commit.h
1
commit.h
@ -89,6 +89,7 @@ struct pretty_print_context {
|
|||||||
char *notes_message;
|
char *notes_message;
|
||||||
struct reflog_walk_info *reflog_info;
|
struct reflog_walk_info *reflog_info;
|
||||||
const char *output_encoding;
|
const char *output_encoding;
|
||||||
|
int color;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct userformat_want {
|
struct userformat_want {
|
||||||
|
@ -671,6 +671,7 @@ void show_log(struct rev_info *opt)
|
|||||||
ctx.preserve_subject = opt->preserve_subject;
|
ctx.preserve_subject = opt->preserve_subject;
|
||||||
ctx.reflog_info = opt->reflog_info;
|
ctx.reflog_info = opt->reflog_info;
|
||||||
ctx.fmt = opt->commit_format;
|
ctx.fmt = opt->commit_format;
|
||||||
|
ctx.color = opt->diffopt.use_color;
|
||||||
pretty_print_commit(&ctx, commit, &msgbuf);
|
pretty_print_commit(&ctx, commit, &msgbuf);
|
||||||
|
|
||||||
if (opt->add_signoff)
|
if (opt->add_signoff)
|
||||||
|
13
pretty.c
13
pretty.c
@ -960,12 +960,19 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
|
|||||||
switch (placeholder[0]) {
|
switch (placeholder[0]) {
|
||||||
case 'C':
|
case 'C':
|
||||||
if (placeholder[1] == '(') {
|
if (placeholder[1] == '(') {
|
||||||
const char *end = strchr(placeholder + 2, ')');
|
const char *begin = placeholder + 2;
|
||||||
|
const char *end = strchr(begin, ')');
|
||||||
char color[COLOR_MAXLEN];
|
char color[COLOR_MAXLEN];
|
||||||
|
|
||||||
if (!end)
|
if (!end)
|
||||||
return 0;
|
return 0;
|
||||||
color_parse_mem(placeholder + 2,
|
if (!memcmp(begin, "auto,", 5)) {
|
||||||
end - (placeholder + 2),
|
if (!want_color(c->pretty_ctx->color))
|
||||||
|
return end - placeholder + 1;
|
||||||
|
begin += 5;
|
||||||
|
}
|
||||||
|
color_parse_mem(begin,
|
||||||
|
end - begin,
|
||||||
"--pretty format", color);
|
"--pretty format", color);
|
||||||
strbuf_addstr(sb, color);
|
strbuf_addstr(sb, color);
|
||||||
return end - placeholder + 1;
|
return end - placeholder + 1;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
test_description='git rev-list --pretty=format test'
|
test_description='git rev-list --pretty=format test'
|
||||||
|
|
||||||
. ./test-lib.sh
|
. ./test-lib.sh
|
||||||
|
. "$TEST_DIRECTORY"/lib-terminal.sh
|
||||||
|
|
||||||
test_tick
|
test_tick
|
||||||
test_expect_success 'setup' '
|
test_expect_success 'setup' '
|
||||||
@ -11,12 +12,24 @@ touch foo && git add foo && git commit -m "added foo" &&
|
|||||||
'
|
'
|
||||||
|
|
||||||
# usage: test_format name format_string <expected_output
|
# usage: test_format name format_string <expected_output
|
||||||
test_format() {
|
test_format () {
|
||||||
cat >expect.$1
|
cat >expect.$1
|
||||||
test_expect_success "format $1" "
|
test_expect_success "format $1" "
|
||||||
git rev-list --pretty=format:'$2' master >output.$1 &&
|
git rev-list --pretty=format:'$2' master >output.$1 &&
|
||||||
test_cmp expect.$1 output.$1
|
test_cmp expect.$1 output.$1
|
||||||
"
|
"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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'
|
test_format percent %%h <<'EOF'
|
||||||
@ -124,6 +137,48 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
|
|||||||
[1;31;43mfoo[m
|
[1;31;43mfoo[m
|
||||||
EOF
|
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'
|
cat >commit-msg <<'EOF'
|
||||||
Test printing of complex bodies
|
Test printing of complex bodies
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user