ref-filter: add return value && strbuf to handlers

Continue removing die() calls from ref-filter formatting logic, so that it could be used by other commands. Change the signature of handlers by adding return value and strbuf parameter for errors. Return value equals 0 upon success and -1 upon failure. 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 3fc8439ce17dbe0b8d78b6d51a71b35f3f6ed30e
1 file changed +35 -16
ref-filter.c
+35 -16
@@ -400,7 +400,8 @@ struct ref_formatting_state {
400
401 struct atom_value {
402 const char *s;
403 - void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
403 + int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
404 + struct strbuf *err);
405 uintmax_t value; /* used for sorting when not FIELD_STR */
406 struct used_atom *atom;
407 };
@@ -494,7 +495,8 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
495 }
496 }
497
497 -static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
498 +static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
499 + struct strbuf *unused_err)
500 {
501 /*
502 * Quote formatting is only done when the stack has a single
@@ -506,6 +508,7 @@ static void append_atom(struct atom_value *v, struct ref_formatting_state *state
508 quote_formatting(&state->stack->output, v->s, state->quote_style);
509 else
510 strbuf_addstr(&state->stack->output, v->s);
511 + return 0;
512 }
513
514 static void push_stack_element(struct ref_formatting_stack **stack)
@@ -540,7 +543,8 @@ static void end_align_handler(struct ref_formatting_stack **stack)
543 strbuf_release(&s);
544 }
545
543 -static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
546 +static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
547 + struct strbuf *unused_err)
548 {
549 struct ref_formatting_stack *new_stack;
550
@@ -548,6 +552,7 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s
552 new_stack = state->stack;
553 new_stack->at_end = end_align_handler;
554 new_stack->at_end_data = &atomv->atom->u.align;
555 + return 0;
556 }
557
558 static void if_then_else_handler(struct ref_formatting_stack **stack)
@@ -585,7 +590,8 @@ static void if_then_else_handler(struct ref_formatting_stack **stack)
590 free(if_then_else);
591 }
592
588 -static void if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
593 +static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
594 + struct strbuf *unused_err)
595 {
596 struct ref_formatting_stack *new_stack;
597 struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
@@ -597,6 +603,7 @@ static void if_atom_handler(struct atom_value *atomv, struct ref_formatting_stat
603 new_stack = state->stack;
604 new_stack->at_end = if_then_else_handler;
605 new_stack->at_end_data = if_then_else;
606 + return 0;
607 }
608
609 static int is_empty(const char *s)
@@ -609,7 +616,8 @@ static int is_empty(const char *s)
616 return 1;
617 }
618
612 -static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
619 +static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
620 + struct strbuf *err)
621 {
622 struct ref_formatting_stack *cur = state->stack;
623 struct if_then_else *if_then_else = NULL;
@@ -617,11 +625,11 @@ static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_st
625 if (cur->at_end == if_then_else_handler)
626 if_then_else = (struct if_then_else *)cur->at_end_data;
627 if (!if_then_else)
620 - die(_("format: %%(then) atom used without an %%(if) atom"));
628 + return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom"));
629 if (if_then_else->then_atom_seen)
622 - die(_("format: %%(then) atom used more than once"));
630 + return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
631 if (if_then_else->else_atom_seen)
624 - die(_("format: %%(then) atom used after %%(else)"));
632 + return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
633 if_then_else->then_atom_seen = 1;
634 /*
635 * If the 'equals' or 'notequals' attribute is used then
@@ -637,9 +645,11 @@ static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_st
645 } else if (cur->output.len && !is_empty(cur->output.buf))
646 if_then_else->condition_satisfied = 1;
647 strbuf_reset(&cur->output);
648 + return 0;
649 }
650
642 -static void else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
651 +static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
652 + struct strbuf *err)
653 {
654 struct ref_formatting_stack *prev = state->stack;
655 struct if_then_else *if_then_else = NULL;
@@ -647,24 +657,26 @@ static void else_atom_handler(struct atom_value *atomv, struct ref_formatting_st
657 if (prev->at_end == if_then_else_handler)
658 if_then_else = (struct if_then_else *)prev->at_end_data;
659 if (!if_then_else)
650 - die(_("format: %%(else) atom used without an %%(if) atom"));
660 + return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom"));
661 if (!if_then_else->then_atom_seen)
652 - die(_("format: %%(else) atom used without a %%(then) atom"));
662 + return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom"));
663 if (if_then_else->else_atom_seen)
654 - die(_("format: %%(else) atom used more than once"));
664 + return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
665 if_then_else->else_atom_seen = 1;
666 push_stack_element(&state->stack);
667 state->stack->at_end_data = prev->at_end_data;
668 state->stack->at_end = prev->at_end;
669 + return 0;
670 }
671
661 -static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
672 +static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
673 + struct strbuf *err)
674 {
675 struct ref_formatting_stack *current = state->stack;
676 struct strbuf s = STRBUF_INIT;
677
678 if (!current->at_end)
667 - die(_("format: %%(end) atom used without corresponding atom"));
679 + return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
680 current->at_end(&state->stack);
681
682 /* Stack may have been popped within at_end(), hence reset the current pointer */
@@ -681,6 +693,7 @@ static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_sta
693 }
694 strbuf_release(&s);
695 pop_stack_element(&state->stack);
696 + return 0;
697 }
698
699 /*
@@ -2151,7 +2164,10 @@ int format_ref_array_item(struct ref_array_item *info,
2164 get_ref_atom_value(info,
2165 parse_ref_filter_atom(format, sp + 2, ep),
2166 &atomv);
2154 - atomv->handler(atomv, &state);
2167 + if (atomv->handler(atomv, &state, error_buf)) {
2168 + pop_stack_element(&state.stack);
2169 + return -1;
2170 + }
2171 }
2172 if (*cp) {
2173 sp = cp + strlen(cp);
@@ -2160,7 +2176,10 @@ int format_ref_array_item(struct ref_array_item *info,
2176 if (format->need_color_reset_at_eol) {
2177 struct atom_value resetv;
2178 resetv.s = GIT_COLOR_RESET;
2163 - append_atom(&resetv, &state);
2179 + if (append_atom(&resetv, &state, error_buf)) {
2180 + pop_stack_element(&state.stack);
2181 + return -1;
2182 + }
2183 }
2184 if (state.stack->prev) {
2185 pop_stack_element(&state.stack);