@matheusbsbs / matheus-bagliano / commits / c002922adc
Fork 0

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>

Jeff King committed Jan 17, 2009 at 10:38 UTC c002922adce90b69ac56a86eeb77b249afa88eb2
3 files changed +21 -1
Documentation/pretty-formats.txt
+1
@@ -124,6 +124,7 @@ The placeholders are:
124 - '%Cgreen': switch color to green
125 - '%Cblue': switch color to blue
126 - '%Creset': reset color
127 +- '%C(...)': color specification, as described in color.branch.* config option
128 - '%m': left, right or boundary mark
129 - '%n': newline
130 - '%x00': print a byte from a hex code
pretty.c
+12
@@ -6,6 +6,7 @@
6 #include "string-list.h"
7 #include "mailmap.h"
8 #include "log-tree.h"
9 +#include "color.h"
10
11 static char *user_format;
12
@@ -554,6 +555,17 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
555 /* these are independent of the commit */
556 switch (placeholder[0]) {
557 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 + }
569 if (!prefixcmp(placeholder + 1, "red")) {
570 strbuf_addstr(sb, "\033[31m");
571 return 4;
t/t6006-rev-list-format.sh
+8 -1
@@ -14,7 +14,7 @@ touch foo && git add foo && git commit -m "added foo" &&
14 test_format() {
15 cat >expect.$1
16 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 &&
18 test_cmp expect.$1 output.$1
19 "
20 }
@@ -101,6 +101,13 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
101 foobarbazxyzzy
102 EOF
103
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 +
111 cat >commit-msg <<'EOF'
112 Test printing of complex bodies
113