Skip to content

Commit 35e6afd

Browse files
committed
Merge branch 'jk/color-parse'
* jk/color-parse: Optimize color_parse_mem expand --pretty=format color options color: make it easier for non-config to parse color specs
2 parents a14f154 + 2c2dc7c commit 35e6afd

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
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

color.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
4040
}
4141

4242
void color_parse(const char *value, const char *var, char *dst)
43+
{
44+
color_parse_mem(value, strlen(value), var, dst);
45+
}
46+
47+
void color_parse_mem(const char *value, int value_len, const char *var,
48+
char *dst)
4349
{
4450
const char *ptr = value;
51+
int len = value_len;
4552
int attr = -1;
4653
int fg = -2;
4754
int bg = -2;
4855

49-
if (!strcasecmp(value, "reset")) {
56+
if (!strncasecmp(value, "reset", len)) {
5057
strcpy(dst, "\033[m");
5158
return;
5259
}
5360

5461
/* [fg [bg]] [attr] */
55-
while (*ptr) {
62+
while (len > 0) {
5663
const char *word = ptr;
57-
int val, len = 0;
64+
int val, wordlen = 0;
5865

59-
while (word[len] && !isspace(word[len]))
60-
len++;
66+
while (len > 0 && !isspace(word[wordlen])) {
67+
wordlen++;
68+
len--;
69+
}
6170

62-
ptr = word + len;
63-
while (*ptr && isspace(*ptr))
71+
ptr = word + wordlen;
72+
while (len > 0 && isspace(*ptr)) {
6473
ptr++;
74+
len--;
75+
}
6576

66-
val = parse_color(word, len);
77+
val = parse_color(word, wordlen);
6778
if (val >= -1) {
6879
if (fg == -2) {
6980
fg = val;
@@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
7586
}
7687
goto bad;
7788
}
78-
val = parse_attr(word, len);
89+
val = parse_attr(word, wordlen);
7990
if (val < 0 || attr != -1)
8091
goto bad;
8192
attr = val;
@@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
115126
*dst = 0;
116127
return;
117128
bad:
118-
die("bad config value '%s' for variable '%s'", value, var);
129+
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
119130
}
120131

121132
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)

color.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ extern int git_use_color_default;
1616
int git_color_default_config(const char *var, const char *value, void *cb);
1717

1818
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
19-
void color_parse(const char *var, const char *value, char *dst);
19+
void color_parse(const char *value, const char *var, char *dst);
20+
void color_parse_mem(const char *value, int len, const char *var, char *dst);
2021
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
2122
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
2223

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)