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 <olyatelezhnaya@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Olga Telezhnaya committed Mar 29, 2018 at 12:49 UTC e6ff7b3bb55ad458130906f35226054deb789224
1 file changed +24 -8
ref-filter.c
+24 -8
@@ -410,7 +410,8 @@ struct atom_value {
410 * Used to parse format string and sort specifiers
411 */
412 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)
415 {
416 const char *sp;
417 const char *arg;
@@ -420,7 +421,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
421 if (*sp == '*' && sp < ep)
422 sp++; /* deref */
423 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);
426
427 /* Do we have the atom already used elsewhere? */
428 for (i = 0; i < used_atom_cnt; i++) {
@@ -446,7 +448,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
448 }
449
450 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);
453
454 /* Add it in, including the deref prefix */
455 at = used_atom_cnt;
@@ -728,17 +731,21 @@ int verify_ref_format(struct ref_format *format)
731
732 format->need_color_reset_at_eol = 0;
733 for (cp = format->format; *cp && (sp = find_next(cp)); ) {
734 + struct strbuf err = STRBUF_INIT;
735 const char *color, *ep = strchr(sp, ')');
736 int at;
737
738 if (!ep)
739 return error(_("malformed format string %s"), sp);
740 /* 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);
744 cp = ep + 1;
745
746 if (skip_prefix(used_atom[at].name, "color:", &color))
747 format->need_color_reset_at_eol = !!strcmp(color, "reset");
748 + strbuf_release(&err);
749 }
750 if (format->need_color_reset_at_eol && !want_color(format->use_color))
751 format->need_color_reset_at_eol = 0;
@@ -2157,13 +2164,17 @@ int format_ref_array_item(struct ref_array_item *info,
2164
2165 for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
2166 struct atom_value *atomv;
2167 + int pos;
2168
2169 ep = strchr(sp, ')');
2170 if (cp < sp)
2171 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);
2178 if (atomv->handler(atomv, &state, error_buf)) {
2179 pop_stack_element(&state.stack);
2180 return -1;
@@ -2222,7 +2233,12 @@ static int parse_sorting_atom(const char *atom)
2233 */
2234 struct ref_format dummy = REF_FORMAT_INIT;
2235 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;
2242 }
2243
2244 /* If no sorting option is given, use refname to sort as default */