Skip to content

Commit fd935cc

Browse files
KarthikNayakgitster
authored andcommitted
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>
1 parent 4de707e commit fd935cc

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

ref-filter.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,21 @@ typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
2929
static struct used_atom {
3030
const char *name;
3131
cmp_type type;
32+
union {
33+
char color[COLOR_MAXLEN];
34+
} u;
3235
} *used_atom;
3336
static int used_atom_cnt, need_tagged, need_symref;
3437
static int need_color_reset_at_eol;
3538

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+
3647
static struct {
3748
const char *name;
3849
cmp_type cmp_type;
@@ -70,7 +81,7 @@ static struct {
7081
{ "symref" },
7182
{ "flag" },
7283
{ "HEAD" },
73-
{ "color" },
84+
{ "color", FIELD_STR, color_atom_parser },
7485
{ "align" },
7586
{ "end" },
7687
};
@@ -158,6 +169,7 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
158169
used_atom[at].type = valid_atom[i].cmp_type;
159170
if (arg)
160171
arg = used_atom[at].name + (arg - atom) + 1;
172+
memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
161173
if (valid_atom[i].parser)
162174
valid_atom[i].parser(&used_atom[at], arg);
163175
if (*atom == '*')
@@ -816,6 +828,7 @@ static void populate_value(struct ref_array_item *ref)
816828

817829
/* Fill in specials first */
818830
for (i = 0; i < used_atom_cnt; i++) {
831+
struct used_atom *atom = &used_atom[i];
819832
const char *name = used_atom[i].name;
820833
struct atom_value *v = &ref->value[i];
821834
int deref = 0;
@@ -856,14 +869,8 @@ static void populate_value(struct ref_array_item *ref)
856869
refname = branch_get_push(branch, NULL);
857870
if (!refname)
858871
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;
867874
continue;
868875
} else if (!strcmp(name, "flag")) {
869876
char buf[256], *cp = buf;

0 commit comments

Comments
 (0)