Skip to content

Commit 4f3e3b3

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c58fc85 commit 4f3e3b3

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ if::
158158
evaluating the string before %(then), this is useful when we
159159
use the %(HEAD) atom which prints either "*" or " " and we
160160
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.
161164

162165
In addition to the above, for commit and tag objects, the header
163166
field names (`tree`, `parent`, `object`, `type`, and `tag`) can

ref-filter.c

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
#include "trailer.h"
1717

1818
typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
19+
typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
1920

2021
struct align {
2122
align_type position;
2223
unsigned int width;
2324
};
2425

2526
struct if_then_else {
27+
cmp_status cmp_status;
28+
const char *str;
2629
unsigned int then_atom_seen : 1,
2730
else_atom_seen : 1,
2831
condition_satisfied : 1;
@@ -50,6 +53,10 @@ static struct used_atom {
5053
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
5154
unsigned int nlines;
5255
} contents;
56+
struct {
57+
cmp_status cmp_status;
58+
const char *str;
59+
} if_then_else;
5360
enum { O_FULL, O_SHORT } objectname;
5461
} u;
5562
} *used_atom;
@@ -179,6 +186,21 @@ static void align_atom_parser(struct used_atom *atom, const char *arg)
179186
string_list_clear(&params, 0);
180187
}
181188

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+
182204
static struct {
183205
const char *name;
184206
cmp_type cmp_type;
@@ -220,7 +242,7 @@ static struct {
220242
{ "color", FIELD_STR, color_atom_parser },
221243
{ "align", FIELD_STR, align_atom_parser },
222244
{ "end" },
223-
{ "if" },
245+
{ "if", FIELD_STR, if_atom_parser },
224246
{ "then" },
225247
{ "else" },
226248
};
@@ -422,6 +444,9 @@ static void if_atom_handler(struct atom_value *atomv, struct ref_formatting_stat
422444
struct ref_formatting_stack *new;
423445
struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
424446

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+
425450
push_stack_element(&state->stack);
426451
new = state->stack;
427452
new->at_end = if_then_else_handler;
@@ -453,10 +478,17 @@ static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_st
453478
die(_("format: %%(then) atom used after %%(else)"));
454479
if_then_else->then_atom_seen = 1;
455480
/*
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.
458484
*/
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))
460492
if_then_else->condition_satisfied = 1;
461493
strbuf_reset(&cur->output);
462494
}
@@ -1158,7 +1190,11 @@ static void populate_value(struct ref_array_item *ref)
11581190
} else if (!strcmp(name, "end")) {
11591191
v->handler = end_atom_handler;
11601192
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);
11621198
v->handler = if_atom_handler;
11631199
continue;
11641200
} else if (!strcmp(name, "then")) {

t/t6302-for-each-ref-filter.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,22 @@ test_expect_success 'ignore spaces in %(if) atom usage' '
403403
test_cmp expect actual
404404
'
405405

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+
406424
test_done

0 commit comments

Comments
 (0)