1
0
mirror of https://github.com/git/git.git synced 2024-09-29 12:01:30 +02:00

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>
This commit is contained in:
Olga Telezhnaya 2018-03-29 12:49:45 +00:00 committed by Junio C Hamano
parent 3fc8439ce1
commit e6ff7b3bb5

@ -410,7 +410,8 @@ struct atom_value {
* Used to parse format string and sort specifiers * Used to parse format string and sort specifiers
*/ */
static int parse_ref_filter_atom(const struct ref_format *format, static int parse_ref_filter_atom(const struct ref_format *format,
const char *atom, const char *ep) const char *atom, const char *ep,
struct strbuf *err)
{ {
const char *sp; const char *sp;
const char *arg; const char *arg;
@ -420,7 +421,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
if (*sp == '*' && sp < ep) if (*sp == '*' && sp < ep)
sp++; /* deref */ sp++; /* deref */
if (ep <= sp) if (ep <= sp)
die(_("malformed field name: %.*s"), (int)(ep-atom), atom); return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
(int)(ep-atom), atom);
/* Do we have the atom already used elsewhere? */ /* Do we have the atom already used elsewhere? */
for (i = 0; i < used_atom_cnt; i++) { for (i = 0; i < used_atom_cnt; i++) {
@ -446,7 +448,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
} }
if (ARRAY_SIZE(valid_atom) <= i) if (ARRAY_SIZE(valid_atom) <= i)
die(_("unknown field name: %.*s"), (int)(ep-atom), atom); return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
(int)(ep-atom), atom);
/* Add it in, including the deref prefix */ /* Add it in, including the deref prefix */
at = used_atom_cnt; at = used_atom_cnt;
@ -728,17 +731,21 @@ int verify_ref_format(struct ref_format *format)
format->need_color_reset_at_eol = 0; format->need_color_reset_at_eol = 0;
for (cp = format->format; *cp && (sp = find_next(cp)); ) { for (cp = format->format; *cp && (sp = find_next(cp)); ) {
struct strbuf err = STRBUF_INIT;
const char *color, *ep = strchr(sp, ')'); const char *color, *ep = strchr(sp, ')');
int at; int at;
if (!ep) if (!ep)
return error(_("malformed format string %s"), sp); return error(_("malformed format string %s"), sp);
/* sp points at "%(" and ep points at the closing ")" */ /* sp points at "%(" and ep points at the closing ")" */
at = parse_ref_filter_atom(format, sp + 2, ep); at = parse_ref_filter_atom(format, sp + 2, ep, &err);
if (at < 0)
die("%s", err.buf);
cp = ep + 1; cp = ep + 1;
if (skip_prefix(used_atom[at].name, "color:", &color)) if (skip_prefix(used_atom[at].name, "color:", &color))
format->need_color_reset_at_eol = !!strcmp(color, "reset"); format->need_color_reset_at_eol = !!strcmp(color, "reset");
strbuf_release(&err);
} }
if (format->need_color_reset_at_eol && !want_color(format->use_color)) if (format->need_color_reset_at_eol && !want_color(format->use_color))
format->need_color_reset_at_eol = 0; format->need_color_reset_at_eol = 0;
@ -2157,13 +2164,17 @@ int format_ref_array_item(struct ref_array_item *info,
for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) { for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
struct atom_value *atomv; struct atom_value *atomv;
int pos;
ep = strchr(sp, ')'); ep = strchr(sp, ')');
if (cp < sp) if (cp < sp)
append_literal(cp, sp, &state); append_literal(cp, sp, &state);
get_ref_atom_value(info, pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
parse_ref_filter_atom(format, sp + 2, ep), if (pos < 0) {
&atomv); pop_stack_element(&state.stack);
return -1;
}
get_ref_atom_value(info, pos, &atomv);
if (atomv->handler(atomv, &state, error_buf)) { if (atomv->handler(atomv, &state, error_buf)) {
pop_stack_element(&state.stack); pop_stack_element(&state.stack);
return -1; return -1;
@ -2222,7 +2233,12 @@ static int parse_sorting_atom(const char *atom)
*/ */
struct ref_format dummy = REF_FORMAT_INIT; struct ref_format dummy = REF_FORMAT_INIT;
const char *end = atom + strlen(atom); const char *end = atom + strlen(atom);
return parse_ref_filter_atom(&dummy, atom, end); struct strbuf err = STRBUF_INIT;
int res = parse_ref_filter_atom(&dummy, atom, end, &err);
if (res < 0)
die("%s", err.buf);
strbuf_release(&err);
return res;
} }
/* If no sorting option is given, use refname to sort as default */ /* If no sorting option is given, use refname to sort as default */