ref-filter: introduce match_atom_name()
Introduce match_atom_name() which helps in checking if a particular atom is the atom we're looking for and if it has a value attached to it or not. Use it instead of starts_with() for checking the value of %(color:...) atom. Write a test for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Thanks-to: Junio C Hamano <gitster@pobox.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
63d89fbce1
commit
40a7551d25
23
ref-filter.c
23
ref-filter.c
|
@ -189,6 +189,22 @@ static void pop_stack_element(struct ref_formatting_stack **stack)
|
||||||
*stack = prev;
|
*stack = prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int match_atom_name(const char *name, const char *atom_name, const char **val)
|
||||||
|
{
|
||||||
|
const char *body;
|
||||||
|
|
||||||
|
if (!skip_prefix(name, atom_name, &body))
|
||||||
|
return 0; /* doesn't even begin with "atom_name" */
|
||||||
|
if (!body[0]) {
|
||||||
|
*val = NULL; /* %(atom_name) and no customization */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (body[0] != ':')
|
||||||
|
return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
|
||||||
|
*val = body + 1; /* "atom_name:val" */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* In a format string, find the next occurrence of %(atom).
|
* In a format string, find the next occurrence of %(atom).
|
||||||
*/
|
*/
|
||||||
|
@ -687,6 +703,7 @@ static void populate_value(struct ref_array_item *ref)
|
||||||
int deref = 0;
|
int deref = 0;
|
||||||
const char *refname;
|
const char *refname;
|
||||||
const char *formatp;
|
const char *formatp;
|
||||||
|
const char *valp;
|
||||||
struct branch *branch = NULL;
|
struct branch *branch = NULL;
|
||||||
|
|
||||||
v->handler = append_atom;
|
v->handler = append_atom;
|
||||||
|
@ -721,10 +738,12 @@ static void populate_value(struct ref_array_item *ref)
|
||||||
refname = branch_get_push(branch, NULL);
|
refname = branch_get_push(branch, NULL);
|
||||||
if (!refname)
|
if (!refname)
|
||||||
continue;
|
continue;
|
||||||
} else if (starts_with(name, "color:")) {
|
} else if (match_atom_name(name, "color", &valp)) {
|
||||||
char color[COLOR_MAXLEN] = "";
|
char color[COLOR_MAXLEN] = "";
|
||||||
|
|
||||||
if (color_parse(name + 6, color) < 0)
|
if (!valp)
|
||||||
|
die(_("expected format: %%(color:<color>)"));
|
||||||
|
if (color_parse(valp, color) < 0)
|
||||||
die(_("unable to parse format"));
|
die(_("unable to parse format"));
|
||||||
v->s = xstrdup(color);
|
v->s = xstrdup(color);
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -81,4 +81,8 @@ test_expect_success 'filtering with --contains' '
|
||||||
test_cmp expect actual
|
test_cmp expect actual
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success '%(color) must fail' '
|
||||||
|
test_must_fail git for-each-ref --format="%(color)%(refname)"
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
|
Loading…
Reference in New Issue