ref-filter: introduce color_atom_parser()
Introduce color_atom_parser() which will parse a "color" atom and store its color in the "used_atom" structure for further usage in populate_value(). Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karthik Nayak committed
Feb 17, 2016 at 23:36 UTC
fd935cc7e8e2fdf7ad837dc74509c75877a489ba
1 file changed
+16
-9
ref-filter.c
+16
-9
@@ -29,10 +29,21 @@ typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
29
static struct used_atom {
30
const char *name;
31
cmp_type type;
32
+ union {
33
+ char color[COLOR_MAXLEN];
34
+ } u;
35
} *used_atom;
36
static int used_atom_cnt, need_tagged, need_symref;
37
static int need_color_reset_at_eol;
38
39
+static void color_atom_parser(struct used_atom *atom, const char *color_value)
40
+{
41
+ if (!color_value)
42
+ die(_("expected format: %%(color:<color>)"));
43
+ if (color_parse(color_value, atom->u.color) < 0)
44
+ die(_("unrecognized color: %%(color:%s)"), color_value);
45
+}
46
+
47
static struct {
48
const char *name;
49
cmp_type cmp_type;
@@ -70,7 +81,7 @@ static struct {
81
{ "symref" },
82
{ "flag" },
83
{ "HEAD" },
73
- { "color" },
84
+ { "color", FIELD_STR, color_atom_parser },
85
{ "align" },
86
{ "end" },
87
};
@@ -158,6 +169,7 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
169
used_atom[at].type = valid_atom[i].cmp_type;
170
if (arg)
171
arg = used_atom[at].name + (arg - atom) + 1;
172
+ memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
173
if (valid_atom[i].parser)
174
valid_atom[i].parser(&used_atom[at], arg);
175
if (*atom == '*')
@@ -816,6 +828,7 @@ static void populate_value(struct ref_array_item *ref)
828
829
/* Fill in specials first */
830
for (i = 0; i < used_atom_cnt; i++) {
831
+ struct used_atom *atom = &used_atom[i];
832
const char *name = used_atom[i].name;
833
struct atom_value *v = &ref->value[i];
834
int deref = 0;
@@ -856,14 +869,8 @@ static void populate_value(struct ref_array_item *ref)
869
refname = branch_get_push(branch, NULL);
870
if (!refname)
871
continue;
859
- } else if (match_atom_name(name, "color", &valp)) {
860
- char color[COLOR_MAXLEN] = "";
861
-
862
- if (!valp)
863
- die(_("expected format: %%(color:<color>)"));
864
- if (color_parse(valp, color) < 0)
865
- die(_("unable to parse format"));
866
- v->s = xstrdup(color);
872
+ } else if (starts_with(name, "color:")) {
873
+ v->s = atom->u.color;
874
continue;
875
} else if (!strcmp(name, "flag")) {
876
char buf[256], *cp = buf;