Skip to content

Commit e6ff7b3

Browse files
telezhnayagitster
authored andcommitted
ref-filter: change parsing function error handling
Continue removing die() calls from ref-filter formatting logic, so that it could be used by other commands. Change the signature of parse_ref_filter_atom() by adding strbuf parameter for error message. The function returns the position in the used_atom[] array (as before) for the given atom, or -1 to signal an error. 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 3fc8439 commit e6ff7b3

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

ref-filter.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ struct atom_value {
410410
* Used to parse format string and sort specifiers
411411
*/
412412
static int parse_ref_filter_atom(const struct ref_format *format,
413-
const char *atom, const char *ep)
413+
const char *atom, const char *ep,
414+
struct strbuf *err)
414415
{
415416
const char *sp;
416417
const char *arg;
@@ -420,7 +421,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
420421
if (*sp == '*' && sp < ep)
421422
sp++; /* deref */
422423
if (ep <= sp)
423-
die(_("malformed field name: %.*s"), (int)(ep-atom), atom);
424+
return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
425+
(int)(ep-atom), atom);
424426

425427
/* Do we have the atom already used elsewhere? */
426428
for (i = 0; i < used_atom_cnt; i++) {
@@ -446,7 +448,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
446448
}
447449

448450
if (ARRAY_SIZE(valid_atom) <= i)
449-
die(_("unknown field name: %.*s"), (int)(ep-atom), atom);
451+
return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
452+
(int)(ep-atom), atom);
450453

451454
/* Add it in, including the deref prefix */
452455
at = used_atom_cnt;
@@ -728,17 +731,21 @@ int verify_ref_format(struct ref_format *format)
728731

729732
format->need_color_reset_at_eol = 0;
730733
for (cp = format->format; *cp && (sp = find_next(cp)); ) {
734+
struct strbuf err = STRBUF_INIT;
731735
const char *color, *ep = strchr(sp, ')');
732736
int at;
733737

734738
if (!ep)
735739
return error(_("malformed format string %s"), sp);
736740
/* sp points at "%(" and ep points at the closing ")" */
737-
at = parse_ref_filter_atom(format, sp + 2, ep);
741+
at = parse_ref_filter_atom(format, sp + 2, ep, &err);
742+
if (at < 0)
743+
die("%s", err.buf);
738744
cp = ep + 1;
739745

740746
if (skip_prefix(used_atom[at].name, "color:", &color))
741747
format->need_color_reset_at_eol = !!strcmp(color, "reset");
748+
strbuf_release(&err);
742749
}
743750
if (format->need_color_reset_at_eol && !want_color(format->use_color))
744751
format->need_color_reset_at_eol = 0;
@@ -2157,13 +2164,17 @@ int format_ref_array_item(struct ref_array_item *info,
21572164

21582165
for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
21592166
struct atom_value *atomv;
2167+
int pos;
21602168

21612169
ep = strchr(sp, ')');
21622170
if (cp < sp)
21632171
append_literal(cp, sp, &state);
2164-
get_ref_atom_value(info,
2165-
parse_ref_filter_atom(format, sp + 2, ep),
2166-
&atomv);
2172+
pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
2173+
if (pos < 0) {
2174+
pop_stack_element(&state.stack);
2175+
return -1;
2176+
}
2177+
get_ref_atom_value(info, pos, &atomv);
21672178
if (atomv->handler(atomv, &state, error_buf)) {
21682179
pop_stack_element(&state.stack);
21692180
return -1;
@@ -2222,7 +2233,12 @@ static int parse_sorting_atom(const char *atom)
22222233
*/
22232234
struct ref_format dummy = REF_FORMAT_INIT;
22242235
const char *end = atom + strlen(atom);
2225-
return parse_ref_filter_atom(&dummy, atom, end);
2236+
struct strbuf err = STRBUF_INIT;
2237+
int res = parse_ref_filter_atom(&dummy, atom, end, &err);
2238+
if (res < 0)
2239+
die("%s", err.buf);
2240+
strbuf_release(&err);
2241+
return res;
22262242
}
22272243

22282244
/* If no sorting option is given, use refname to sort as default */

0 commit comments

Comments
 (0)