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>
Karthik Nayak committed
Sep 11, 2015 at 20:29 UTC
40a7551d25efac7979ee1a675c05e67313205444
2 files changed
+25
-2
ref-filter.c
+21
-2
@@ -189,6 +189,22 @@ static void pop_stack_element(struct ref_formatting_stack **stack)
189
*stack = prev;
190
}
191
192
+static int match_atom_name(const char *name, const char *atom_name, const char **val)
193
+{
194
+ const char *body;
195
+
196
+ if (!skip_prefix(name, atom_name, &body))
197
+ return 0; /* doesn't even begin with "atom_name" */
198
+ if (!body[0]) {
199
+ *val = NULL; /* %(atom_name) and no customization */
200
+ return 1;
201
+ }
202
+ if (body[0] != ':')
203
+ return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
204
+ *val = body + 1; /* "atom_name:val" */
205
+ return 1;
206
+}
207
+
208
/*
209
* In a format string, find the next occurrence of %(atom).
210
*/
@@ -687,6 +703,7 @@ static void populate_value(struct ref_array_item *ref)
703
int deref = 0;
704
const char *refname;
705
const char *formatp;
706
+ const char *valp;
707
struct branch *branch = NULL;
708
709
v->handler = append_atom;
@@ -721,10 +738,12 @@ static void populate_value(struct ref_array_item *ref)
738
refname = branch_get_push(branch, NULL);
739
if (!refname)
740
continue;
724
- } else if (starts_with(name, "color:")) {
741
+ } else if (match_atom_name(name, "color", &valp)) {
742
char color[COLOR_MAXLEN] = "";
743
727
- if (color_parse(name + 6, color) < 0)
744
+ if (!valp)
745
+ die(_("expected format: %%(color:<color>)"));
746
+ if (color_parse(valp, color) < 0)
747
die(_("unable to parse format"));
748
v->s = xstrdup(color);
749
continue;
t/t6302-for-each-ref-filter.sh
+4
@@ -81,4 +81,8 @@ test_expect_success 'filtering with --contains' '
81
test_cmp expect actual
82
'
83
84
+test_expect_success '%(color) must fail' '
85
+ test_must_fail git for-each-ref --format="%(color)%(refname)"
86
+'
87
+
88
test_done