Skip to content

Commit c002922

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5ef8d77 commit c002922

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Documentation/pretty-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ The placeholders are:
124124
- '%Cgreen': switch color to green
125125
- '%Cblue': switch color to blue
126126
- '%Creset': reset color
127+
- '%C(...)': color specification, as described in color.branch.* config option
127128
- '%m': left, right or boundary mark
128129
- '%n': newline
129130
- '%x00': print a byte from a hex code

pretty.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "string-list.h"
77
#include "mailmap.h"
88
#include "log-tree.h"
9+
#include "color.h"
910

1011
static char *user_format;
1112

@@ -554,6 +555,17 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
554555
/* these are independent of the commit */
555556
switch (placeholder[0]) {
556557
case 'C':
558+
if (placeholder[1] == '(') {
559+
const char *end = strchr(placeholder + 2, ')');
560+
char color[COLOR_MAXLEN];
561+
if (!end)
562+
return 0;
563+
color_parse_mem(placeholder + 2,
564+
end - (placeholder + 2),
565+
"--pretty format", color);
566+
strbuf_addstr(sb, color);
567+
return end - placeholder + 1;
568+
}
557569
if (!prefixcmp(placeholder + 1, "red")) {
558570
strbuf_addstr(sb, "\033[31m");
559571
return 4;

t/t6006-rev-list-format.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ touch foo && git add foo && git commit -m "added foo" &&
1414
test_format() {
1515
cat >expect.$1
1616
test_expect_success "format $1" "
17-
git rev-list --pretty=format:$2 master >output.$1 &&
17+
git rev-list --pretty=format:'$2' master >output.$1 &&
1818
test_cmp expect.$1 output.$1
1919
"
2020
}
@@ -101,6 +101,13 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
101101
foobarbazxyzzy
102102
EOF
103103

104+
test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<'EOF'
105+
commit 131a310eb913d107dd3c09a65d1651175898735d
106+
foo
107+
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
108+
foo
109+
EOF
110+
104111
cat >commit-msg <<'EOF'
105112
Test printing of complex bodies
106113

0 commit comments

Comments
 (0)