ref-filter: implement %(if:equals=<string>) and %(if:notequals=<string>)

Implement %(if:equals=<string>) wherein the if condition is only satisfied if the value obtained between the %(if:...) and %(then) atom is the same as the given '<string>'. Similarly, implement (if:notequals=<string>) wherein the if condition is only satisfied if the value obtained between the %(if:...) and %(then) atom is different from the given '<string>'. This is done by introducing 'if_atom_parser()' which parses the given %(if) atom and then stores the data in used_atom which is later passed on to the used_atom of the %(then) atom, so that it can do the required comparisons. Add tests and documentation for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jan 10, 2017 at 14:19 UTC 4f3e3b37fae818e0e370d14fd56479d3a7d68b6e
3 files changed +62 -5
Documentation/git-for-each-ref.txt
+3
@@ -158,6 +158,9 @@ if::
158 evaluating the string before %(then), this is useful when we
159 use the %(HEAD) atom which prints either "*" or " " and we
160 want to apply the 'if' condition only on the 'HEAD' ref.
161 + Append ":equals=<string>" or ":notequals=<string>" to compare
162 + the value between the %(if:...) and %(then) atoms with the
163 + given string.
164
165 In addition to the above, for commit and tag objects, the header
166 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
ref-filter.c
+41 -5
@@ -16,6 +16,7 @@
16 #include "trailer.h"
17
18 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
19 +typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
20
21 struct align {
22 align_type position;
@@ -23,6 +24,8 @@ struct align {
24 };
25
26 struct if_then_else {
27 + cmp_status cmp_status;
28 + const char *str;
29 unsigned int then_atom_seen : 1,
30 else_atom_seen : 1,
31 condition_satisfied : 1;
@@ -50,6 +53,10 @@ static struct used_atom {
53 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
54 unsigned int nlines;
55 } contents;
56 + struct {
57 + cmp_status cmp_status;
58 + const char *str;
59 + } if_then_else;
60 enum { O_FULL, O_SHORT } objectname;
61 } u;
62 } *used_atom;
@@ -179,6 +186,21 @@ static void align_atom_parser(struct used_atom *atom, const char *arg)
186 string_list_clear(&params, 0);
187 }
188
189 +static void if_atom_parser(struct used_atom *atom, const char *arg)
190 +{
191 + if (!arg) {
192 + atom->u.if_then_else.cmp_status = COMPARE_NONE;
193 + return;
194 + } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
195 + atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
196 + } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
197 + atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
198 + } else {
199 + die(_("unrecognized %%(if) argument: %s"), arg);
200 + }
201 +}
202 +
203 +
204 static struct {
205 const char *name;
206 cmp_type cmp_type;
@@ -220,7 +242,7 @@ static struct {
242 { "color", FIELD_STR, color_atom_parser },
243 { "align", FIELD_STR, align_atom_parser },
244 { "end" },
223 - { "if" },
245 + { "if", FIELD_STR, if_atom_parser },
246 { "then" },
247 { "else" },
248 };
@@ -422,6 +444,9 @@ static void if_atom_handler(struct atom_value *atomv, struct ref_formatting_stat
444 struct ref_formatting_stack *new;
445 struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
446
447 + if_then_else->str = atomv->atom->u.if_then_else.str;
448 + if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
449 +
450 push_stack_element(&state->stack);
451 new = state->stack;
452 new->at_end = if_then_else_handler;
@@ -453,10 +478,17 @@ static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_st
478 die(_("format: %%(then) atom used after %%(else)"));
479 if_then_else->then_atom_seen = 1;
480 /*
456 - * If there exists non-empty string between the 'if' and
457 - * 'then' atom then the 'if' condition is satisfied.
481 + * If the 'equals' or 'notequals' attribute is used then
482 + * perform the required comparison. If not, only non-empty
483 + * strings satisfy the 'if' condition.
484 */
459 - if (cur->output.len && !is_empty(cur->output.buf))
485 + if (if_then_else->cmp_status == COMPARE_EQUAL) {
486 + if (!strcmp(if_then_else->str, cur->output.buf))
487 + if_then_else->condition_satisfied = 1;
488 + } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
489 + if (strcmp(if_then_else->str, cur->output.buf))
490 + if_then_else->condition_satisfied = 1;
491 + } else if (cur->output.len && !is_empty(cur->output.buf))
492 if_then_else->condition_satisfied = 1;
493 strbuf_reset(&cur->output);
494 }
@@ -1158,7 +1190,11 @@ static void populate_value(struct ref_array_item *ref)
1190 } else if (!strcmp(name, "end")) {
1191 v->handler = end_atom_handler;
1192 continue;
1161 - } else if (!strcmp(name, "if")) {
1193 + } else if (starts_with(name, "if")) {
1194 + const char *s;
1195 +
1196 + if (skip_prefix(name, "if:", &s))
1197 + v->s = xstrdup(s);
1198 v->handler = if_atom_handler;
1199 continue;
1200 } else if (!strcmp(name, "then")) {
t/t6302-for-each-ref-filter.sh
+18
@@ -403,4 +403,22 @@ test_expect_success 'ignore spaces in %(if) atom usage' '
403 test_cmp expect actual
404 '
405
406 +test_expect_success 'check %(if:equals=<string>)' '
407 + git for-each-ref --format="%(if:equals=master)%(refname:short)%(then)Found master%(else)Not master%(end)" refs/heads/ >actual &&
408 + cat >expect <<-\EOF &&
409 + Found master
410 + Not master
411 + EOF
412 + test_cmp expect actual
413 +'
414 +
415 +test_expect_success 'check %(if:notequals=<string>)' '
416 + git for-each-ref --format="%(if:notequals=master)%(refname:short)%(then)Not master%(else)Found master%(end)" refs/heads/ >actual &&
417 + cat >expect <<-\EOF &&
418 + Found master
419 + Not master
420 + EOF
421 + test_cmp expect actual
422 +'
423 +
424 test_done