Skip to content

Commit 3fc8439

Browse files
telezhnayagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3019eca commit 3fc8439

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

ref-filter.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ struct ref_formatting_state {
400400

401401
struct atom_value {
402402
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);
404405
uintmax_t value; /* used for sorting when not FIELD_STR */
405406
struct used_atom *atom;
406407
};
@@ -494,7 +495,8 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
494495
}
495496
}
496497

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)
498500
{
499501
/*
500502
* 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
506508
quote_formatting(&state->stack->output, v->s, state->quote_style);
507509
else
508510
strbuf_addstr(&state->stack->output, v->s);
511+
return 0;
509512
}
510513

511514
static void push_stack_element(struct ref_formatting_stack **stack)
@@ -540,14 +543,16 @@ static void end_align_handler(struct ref_formatting_stack **stack)
540543
strbuf_release(&s);
541544
}
542545

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)
544548
{
545549
struct ref_formatting_stack *new_stack;
546550

547551
push_stack_element(&state->stack);
548552
new_stack = state->stack;
549553
new_stack->at_end = end_align_handler;
550554
new_stack->at_end_data = &atomv->atom->u.align;
555+
return 0;
551556
}
552557

553558
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)
585590
free(if_then_else);
586591
}
587592

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)
589595
{
590596
struct ref_formatting_stack *new_stack;
591597
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
597603
new_stack = state->stack;
598604
new_stack->at_end = if_then_else_handler;
599605
new_stack->at_end_data = if_then_else;
606+
return 0;
600607
}
601608

602609
static int is_empty(const char *s)
@@ -609,19 +616,20 @@ static int is_empty(const char *s)
609616
return 1;
610617
}
611618

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)
613621
{
614622
struct ref_formatting_stack *cur = state->stack;
615623
struct if_then_else *if_then_else = NULL;
616624

617625
if (cur->at_end == if_then_else_handler)
618626
if_then_else = (struct if_then_else *)cur->at_end_data;
619627
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"));
621629
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"));
623631
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)"));
625633
if_then_else->then_atom_seen = 1;
626634
/*
627635
* If the 'equals' or 'notequals' attribute is used then
@@ -637,34 +645,38 @@ static void then_atom_handler(struct atom_value *atomv, struct ref_formatting_st
637645
} else if (cur->output.len && !is_empty(cur->output.buf))
638646
if_then_else->condition_satisfied = 1;
639647
strbuf_reset(&cur->output);
648+
return 0;
640649
}
641650

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)
643653
{
644654
struct ref_formatting_stack *prev = state->stack;
645655
struct if_then_else *if_then_else = NULL;
646656

647657
if (prev->at_end == if_then_else_handler)
648658
if_then_else = (struct if_then_else *)prev->at_end_data;
649659
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"));
651661
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"));
653663
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"));
655665
if_then_else->else_atom_seen = 1;
656666
push_stack_element(&state->stack);
657667
state->stack->at_end_data = prev->at_end_data;
658668
state->stack->at_end = prev->at_end;
669+
return 0;
659670
}
660671

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)
662674
{
663675
struct ref_formatting_stack *current = state->stack;
664676
struct strbuf s = STRBUF_INIT;
665677

666678
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"));
668680
current->at_end(&state->stack);
669681

670682
/* 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
681693
}
682694
strbuf_release(&s);
683695
pop_stack_element(&state->stack);
696+
return 0;
684697
}
685698

686699
/*
@@ -2151,7 +2164,10 @@ int format_ref_array_item(struct ref_array_item *info,
21512164
get_ref_atom_value(info,
21522165
parse_ref_filter_atom(format, sp + 2, ep),
21532166
&atomv);
2154-
atomv->handler(atomv, &state);
2167+
if (atomv->handler(atomv, &state, error_buf)) {
2168+
pop_stack_element(&state.stack);
2169+
return -1;
2170+
}
21552171
}
21562172
if (*cp) {
21572173
sp = cp + strlen(cp);
@@ -2160,7 +2176,10 @@ int format_ref_array_item(struct ref_array_item *info,
21602176
if (format->need_color_reset_at_eol) {
21612177
struct atom_value resetv;
21622178
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+
}
21642183
}
21652184
if (state.stack->prev) {
21662185
pop_stack_element(&state.stack);

0 commit comments

Comments
 (0)