@@ -400,7 +400,8 @@ struct ref_formatting_state {
400
400
401
401
struct atom_value {
402
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 );
404
405
uintmax_t value ; /* used for sorting when not FIELD_STR */
405
406
struct used_atom * atom ;
406
407
};
@@ -494,7 +495,8 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
494
495
}
495
496
}
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 )
498
500
{
499
501
/*
500
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
506
508
quote_formatting (& state -> stack -> output , v -> s , state -> quote_style );
507
509
else
508
510
strbuf_addstr (& state -> stack -> output , v -> s );
511
+ return 0 ;
509
512
}
510
513
511
514
static void push_stack_element (struct ref_formatting_stack * * stack )
@@ -540,14 +543,16 @@ static void end_align_handler(struct ref_formatting_stack **stack)
540
543
strbuf_release (& s );
541
544
}
542
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 )
544
548
{
545
549
struct ref_formatting_stack * new_stack ;
546
550
547
551
push_stack_element (& state -> stack );
548
552
new_stack = state -> stack ;
549
553
new_stack -> at_end = end_align_handler ;
550
554
new_stack -> at_end_data = & atomv -> atom -> u .align ;
555
+ return 0 ;
551
556
}
552
557
553
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)
585
590
free (if_then_else );
586
591
}
587
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 )
589
595
{
590
596
struct ref_formatting_stack * new_stack ;
591
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
597
603
new_stack = state -> stack ;
598
604
new_stack -> at_end = if_then_else_handler ;
599
605
new_stack -> at_end_data = if_then_else ;
606
+ return 0 ;
600
607
}
601
608
602
609
static int is_empty (const char * s )
@@ -609,19 +616,20 @@ static int is_empty(const char *s)
609
616
return 1 ;
610
617
}
611
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 )
613
621
{
614
622
struct ref_formatting_stack * cur = state -> stack ;
615
623
struct if_then_else * if_then_else = NULL ;
616
624
617
625
if (cur -> at_end == if_then_else_handler )
618
626
if_then_else = (struct if_then_else * )cur -> at_end_data ;
619
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" ));
621
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" ));
623
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)" ));
625
633
if_then_else -> then_atom_seen = 1 ;
626
634
/*
627
635
* 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
637
645
} else if (cur -> output .len && !is_empty (cur -> output .buf ))
638
646
if_then_else -> condition_satisfied = 1 ;
639
647
strbuf_reset (& cur -> output );
648
+ return 0 ;
640
649
}
641
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 )
643
653
{
644
654
struct ref_formatting_stack * prev = state -> stack ;
645
655
struct if_then_else * if_then_else = NULL ;
646
656
647
657
if (prev -> at_end == if_then_else_handler )
648
658
if_then_else = (struct if_then_else * )prev -> at_end_data ;
649
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" ));
651
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" ));
653
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" ));
655
665
if_then_else -> else_atom_seen = 1 ;
656
666
push_stack_element (& state -> stack );
657
667
state -> stack -> at_end_data = prev -> at_end_data ;
658
668
state -> stack -> at_end = prev -> at_end ;
669
+ return 0 ;
659
670
}
660
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 )
662
674
{
663
675
struct ref_formatting_stack * current = state -> stack ;
664
676
struct strbuf s = STRBUF_INIT ;
665
677
666
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" ));
668
680
current -> at_end (& state -> stack );
669
681
670
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
681
693
}
682
694
strbuf_release (& s );
683
695
pop_stack_element (& state -> stack );
696
+ return 0 ;
684
697
}
685
698
686
699
/*
@@ -2151,7 +2164,10 @@ int format_ref_array_item(struct ref_array_item *info,
2151
2164
get_ref_atom_value (info ,
2152
2165
parse_ref_filter_atom (format , sp + 2 , ep ),
2153
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
+ }
2155
2171
}
2156
2172
if (* cp ) {
2157
2173
sp = cp + strlen (cp );
@@ -2160,7 +2176,10 @@ int format_ref_array_item(struct ref_array_item *info,
2160
2176
if (format -> need_color_reset_at_eol ) {
2161
2177
struct atom_value resetv ;
2162
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
+ }
2164
2183
}
2165
2184
if (state .stack -> prev ) {
2166
2185
pop_stack_element (& state .stack );
0 commit comments