expand --pretty=format color options

Currently, the only colors available to --pretty=format
users are red, green, and blue. Rather than expand it with a
few new colors, this patch makes the usual config color
syntax available, including more colors, backgrounds, and
attributes.

Because colors are no longer bounded to a single word (e.g.,
%Cred), this uses a more advanced syntax that features a
beginning and end delimiter (but the old syntax still
works). So you can now do:

  git log --pretty=tformat:'%C(yellow)%h%C(reset) %s'

to emulate --pretty=oneline, or even

  git log --pretty=tformat:'%C(cyan magenta bold)%s%C(reset)'

if you want to relive the awesomeness of 4-color CGA.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2009-01-17 10:38:46 -05:00 committed by Junio C Hamano
parent 5ef8d77a75
commit c002922adc
3 changed files with 21 additions and 1 deletions

View file

@ -124,6 +124,7 @@ 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
- '%m': left, right or boundary mark
- '%n': newline
- '%x00': print a byte from a hex code

View file

@ -6,6 +6,7 @@
#include "string-list.h"
#include "mailmap.h"
#include "log-tree.h"
#include "color.h"
static char *user_format;
@ -554,6 +555,17 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
/* these are independent of the commit */
switch (placeholder[0]) {
case 'C':
if (placeholder[1] == '(') {
const char *end = strchr(placeholder + 2, ')');
char color[COLOR_MAXLEN];
if (!end)
return 0;
color_parse_mem(placeholder + 2,
end - (placeholder + 2),
"--pretty format", color);
strbuf_addstr(sb, color);
return end - placeholder + 1;
}
if (!prefixcmp(placeholder + 1, "red")) {
strbuf_addstr(sb, "\033[31m");
return 4;

View file

@ -14,7 +14,7 @@ touch foo && git add foo && git commit -m "added foo" &&
test_format() {
cat >expect.$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
"
}
@ -101,6 +101,13 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foobarbazxyzzy
EOF
test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<'EOF'
commit 131a310eb913d107dd3c09a65d1651175898735d
foo
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foo
EOF
cat >commit-msg <<'EOF'
Test printing of complex bodies