Skip to content

Commit 74efea9

Browse files
telezhnayagitster
authored andcommitted
ref-filter: add return value to parsers
Continue removing die() calls from ref-filter formatting logic, so that it could be used by other commands. Change the signature of parsers by adding return value and strbuf parameter for error message. Return value equals 0 upon success and -1 upon failure. Upon failure, error message is appended to the strbuf. Signed-off-by: Olga Telezhnaia <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6ff7b3 commit 74efea9

File tree

1 file changed

+89
-49
lines changed

1 file changed

+89
-49
lines changed

ref-filter.c

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,25 @@ static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
114114
return ret;
115115
}
116116

117-
static void color_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *color_value)
117+
static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,
118+
const char *color_value, struct strbuf *err)
118119
{
119120
if (!color_value)
120-
die(_("expected format: %%(color:<color>)"));
121+
return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
121122
if (color_parse(color_value, atom->u.color) < 0)
122-
die(_("unrecognized color: %%(color:%s)"), color_value);
123+
return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
124+
color_value);
123125
/*
124126
* We check this after we've parsed the color, which lets us complain
125127
* about syntactically bogus color names even if they won't be used.
126128
*/
127129
if (!want_color(format->use_color))
128130
color_parse("", atom->u.color);
131+
return 0;
129132
}
130133

131-
static void refname_atom_parser_internal(struct refname_atom *atom,
132-
const char *arg, const char *name)
134+
static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
135+
const char *name, struct strbuf *err)
133136
{
134137
if (!arg)
135138
atom->option = R_NORMAL;
@@ -139,16 +142,18 @@ static void refname_atom_parser_internal(struct refname_atom *atom,
139142
skip_prefix(arg, "strip=", &arg)) {
140143
atom->option = R_LSTRIP;
141144
if (strtol_i(arg, 10, &atom->lstrip))
142-
die(_("Integer value expected refname:lstrip=%s"), arg);
145+
return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
143146
} else if (skip_prefix(arg, "rstrip=", &arg)) {
144147
atom->option = R_RSTRIP;
145148
if (strtol_i(arg, 10, &atom->rstrip))
146-
die(_("Integer value expected refname:rstrip=%s"), arg);
149+
return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
147150
} else
148-
die(_("unrecognized %%(%s) argument: %s"), name, arg);
151+
return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
152+
return 0;
149153
}
150154

151-
static void remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
155+
static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,
156+
const char *arg, struct strbuf *err)
152157
{
153158
struct string_list params = STRING_LIST_INIT_DUP;
154159
int i;
@@ -158,9 +163,8 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
158163

159164
if (!arg) {
160165
atom->u.remote_ref.option = RR_REF;
161-
refname_atom_parser_internal(&atom->u.remote_ref.refname,
162-
arg, atom->name);
163-
return;
166+
return refname_atom_parser_internal(&atom->u.remote_ref.refname,
167+
arg, atom->name, err);
164168
}
165169

166170
atom->u.remote_ref.nobracket = 0;
@@ -183,29 +187,38 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
183187
atom->u.remote_ref.push_remote = 1;
184188
} else {
185189
atom->u.remote_ref.option = RR_REF;
186-
refname_atom_parser_internal(&atom->u.remote_ref.refname,
187-
arg, atom->name);
190+
if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
191+
arg, atom->name, err)) {
192+
string_list_clear(&params, 0);
193+
return -1;
194+
}
188195
}
189196
}
190197

191198
string_list_clear(&params, 0);
199+
return 0;
192200
}
193201

194-
static void body_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
202+
static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,
203+
const char *arg, struct strbuf *err)
195204
{
196205
if (arg)
197-
die(_("%%(body) does not take arguments"));
206+
return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
198207
atom->u.contents.option = C_BODY_DEP;
208+
return 0;
199209
}
200210

201-
static void subject_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
211+
static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
212+
const char *arg, struct strbuf *err)
202213
{
203214
if (arg)
204-
die(_("%%(subject) does not take arguments"));
215+
return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments"));
205216
atom->u.contents.option = C_SUB;
217+
return 0;
206218
}
207219

208-
static void trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
220+
static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,
221+
const char *arg, struct strbuf *err)
209222
{
210223
struct string_list params = STRING_LIST_INIT_DUP;
211224
int i;
@@ -218,15 +231,20 @@ static void trailers_atom_parser(const struct ref_format *format, struct used_at
218231
atom->u.contents.trailer_opts.unfold = 1;
219232
else if (!strcmp(s, "only"))
220233
atom->u.contents.trailer_opts.only_trailers = 1;
221-
else
222-
die(_("unknown %%(trailers) argument: %s"), s);
234+
else {
235+
strbuf_addf(err, _("unknown %%(trailers) argument: %s"), s);
236+
string_list_clear(&params, 0);
237+
return -1;
238+
}
223239
}
224240
}
225241
atom->u.contents.option = C_TRAILERS;
226242
string_list_clear(&params, 0);
243+
return 0;
227244
}
228245

229-
static void contents_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
246+
static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
247+
const char *arg, struct strbuf *err)
230248
{
231249
if (!arg)
232250
atom->u.contents.option = C_BARE;
@@ -238,16 +256,19 @@ static void contents_atom_parser(const struct ref_format *format, struct used_at
238256
atom->u.contents.option = C_SUB;
239257
else if (skip_prefix(arg, "trailers", &arg)) {
240258
skip_prefix(arg, ":", &arg);
241-
trailers_atom_parser(format, atom, *arg ? arg : NULL);
259+
if (trailers_atom_parser(format, atom, *arg ? arg : NULL, err))
260+
return -1;
242261
} else if (skip_prefix(arg, "lines=", &arg)) {
243262
atom->u.contents.option = C_LINES;
244263
if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
245-
die(_("positive value expected contents:lines=%s"), arg);
264+
return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
246265
} else
247-
die(_("unrecognized %%(contents) argument: %s"), arg);
266+
return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg);
267+
return 0;
248268
}
249269

250-
static void objectname_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
270+
static int objectname_atom_parser(const struct ref_format *format, struct used_atom *atom,
271+
const char *arg, struct strbuf *err)
251272
{
252273
if (!arg)
253274
atom->u.objectname.option = O_FULL;
@@ -257,16 +278,18 @@ static void objectname_atom_parser(const struct ref_format *format, struct used_
257278
atom->u.objectname.option = O_LENGTH;
258279
if (strtoul_ui(arg, 10, &atom->u.objectname.length) ||
259280
atom->u.objectname.length == 0)
260-
die(_("positive value expected objectname:short=%s"), arg);
281+
return strbuf_addf_ret(err, -1, _("positive value expected objectname:short=%s"), arg);
261282
if (atom->u.objectname.length < MINIMUM_ABBREV)
262283
atom->u.objectname.length = MINIMUM_ABBREV;
263284
} else
264-
die(_("unrecognized %%(objectname) argument: %s"), arg);
285+
return strbuf_addf_ret(err, -1, _("unrecognized %%(objectname) argument: %s"), arg);
286+
return 0;
265287
}
266288

267-
static void refname_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
289+
static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
290+
const char *arg, struct strbuf *err)
268291
{
269-
refname_atom_parser_internal(&atom->u.refname, arg, atom->name);
292+
return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
270293
}
271294

272295
static align_type parse_align_position(const char *s)
@@ -280,15 +303,16 @@ static align_type parse_align_position(const char *s)
280303
return -1;
281304
}
282305

283-
static void align_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
306+
static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,
307+
const char *arg, struct strbuf *err)
284308
{
285309
struct align *align = &atom->u.align;
286310
struct string_list params = STRING_LIST_INIT_DUP;
287311
int i;
288312
unsigned int width = ~0U;
289313

290314
if (!arg)
291-
die(_("expected format: %%(align:<width>,<position>)"));
315+
return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
292316

293317
align->position = ALIGN_LEFT;
294318

@@ -299,49 +323,65 @@ static void align_atom_parser(const struct ref_format *format, struct used_atom
299323

300324
if (skip_prefix(s, "position=", &s)) {
301325
position = parse_align_position(s);
302-
if (position < 0)
303-
die(_("unrecognized position:%s"), s);
326+
if (position < 0) {
327+
strbuf_addf(err, _("unrecognized position:%s"), s);
328+
string_list_clear(&params, 0);
329+
return -1;
330+
}
304331
align->position = position;
305332
} else if (skip_prefix(s, "width=", &s)) {
306-
if (strtoul_ui(s, 10, &width))
307-
die(_("unrecognized width:%s"), s);
333+
if (strtoul_ui(s, 10, &width)) {
334+
strbuf_addf(err, _("unrecognized width:%s"), s);
335+
string_list_clear(&params, 0);
336+
return -1;
337+
}
308338
} else if (!strtoul_ui(s, 10, &width))
309339
;
310340
else if ((position = parse_align_position(s)) >= 0)
311341
align->position = position;
312-
else
313-
die(_("unrecognized %%(align) argument: %s"), s);
342+
else {
343+
strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s);
344+
string_list_clear(&params, 0);
345+
return -1;
346+
}
314347
}
315348

316-
if (width == ~0U)
317-
die(_("positive width expected with the %%(align) atom"));
349+
if (width == ~0U) {
350+
string_list_clear(&params, 0);
351+
return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
352+
}
318353
align->width = width;
319354
string_list_clear(&params, 0);
355+
return 0;
320356
}
321357

322-
static void if_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
358+
static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,
359+
const char *arg, struct strbuf *err)
323360
{
324361
if (!arg) {
325362
atom->u.if_then_else.cmp_status = COMPARE_NONE;
326-
return;
363+
return 0;
327364
} else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
328365
atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
329366
} else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
330367
atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
331-
} else {
332-
die(_("unrecognized %%(if) argument: %s"), arg);
333-
}
368+
} else
369+
return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg);
370+
return 0;
334371
}
335372

336-
static void head_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
373+
static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,
374+
const char *arg, struct strbuf *unused_err)
337375
{
338376
atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
377+
return 0;
339378
}
340379

341380
static struct {
342381
const char *name;
343382
cmp_type cmp_type;
344-
void (*parser)(const struct ref_format *format, struct used_atom *atom, const char *arg);
383+
int (*parser)(const struct ref_format *format, struct used_atom *atom,
384+
const char *arg, struct strbuf *err);
345385
} valid_atom[] = {
346386
{ "refname" , FIELD_STR, refname_atom_parser },
347387
{ "objecttype" },
@@ -468,8 +508,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
468508
}
469509
}
470510
memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
471-
if (valid_atom[i].parser)
472-
valid_atom[i].parser(format, &used_atom[at], arg);
511+
if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
512+
return -1;
473513
if (*atom == '*')
474514
need_tagged = 1;
475515
if (!strcmp(valid_atom[i].name, "symref"))

0 commit comments

Comments
 (0)