1616
1717typedef enum { FIELD_STR , FIELD_ULONG , FIELD_TIME } cmp_type ;
1818
19+ struct align {
20+ align_type position ;
21+ unsigned int width ;
22+ };
23+
1924/*
2025 * An atom is a valid field atom listed below, possibly prefixed with
2126 * a "*" to denote deref_tag().
@@ -31,6 +36,7 @@ static struct used_atom {
3136 cmp_type type ;
3237 union {
3338 char color [COLOR_MAXLEN ];
39+ struct align align ;
3440 } u ;
3541} * used_atom ;
3642static int used_atom_cnt , need_tagged , need_symref ;
@@ -55,6 +61,37 @@ static align_type parse_align_position(const char *s)
5561 return -1 ;
5662}
5763
64+ static void align_atom_parser (struct used_atom * atom , const char * arg )
65+ {
66+ struct align * align = & atom -> u .align ;
67+ struct string_list params = STRING_LIST_INIT_DUP ;
68+ int i ;
69+ unsigned int width = ~0U ;
70+
71+ if (!arg )
72+ die (_ ("expected format: %%(align:<width>,<position>)" ));
73+
74+ align -> position = ALIGN_LEFT ;
75+
76+ string_list_split (& params , arg , ',' , -1 );
77+ for (i = 0 ; i < params .nr ; i ++ ) {
78+ const char * s = params .items [i ].string ;
79+ int position ;
80+
81+ if (!strtoul_ui (s , 10 , & width ))
82+ ;
83+ else if ((position = parse_align_position (s )) >= 0 )
84+ align -> position = position ;
85+ else
86+ die (_ ("unrecognized %%(align) argument: %s" ), s );
87+ }
88+
89+ if (width == ~0U )
90+ die (_ ("positive width expected with the %%(align) atom" ));
91+ align -> width = width ;
92+ string_list_clear (& params , 0 );
93+ }
94+
5895static struct {
5996 const char * name ;
6097 cmp_type cmp_type ;
@@ -93,17 +130,12 @@ static struct {
93130 { "flag" },
94131 { "HEAD" },
95132 { "color" , FIELD_STR , color_atom_parser },
96- { "align" },
133+ { "align" , FIELD_STR , align_atom_parser },
97134 { "end" },
98135};
99136
100137#define REF_FORMATTING_STATE_INIT { 0, NULL }
101138
102- struct align {
103- align_type position ;
104- unsigned int width ;
105- };
106-
107139struct contents {
108140 unsigned int lines ;
109141 struct object_id oid ;
@@ -288,22 +320,6 @@ static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_sta
288320 pop_stack_element (& state -> stack );
289321}
290322
291- static int match_atom_name (const char * name , const char * atom_name , const char * * val )
292- {
293- const char * body ;
294-
295- if (!skip_prefix (name , atom_name , & body ))
296- return 0 ; /* doesn't even begin with "atom_name" */
297- if (!body [0 ]) {
298- * val = NULL ; /* %(atom_name) and no customization */
299- return 1 ;
300- }
301- if (body [0 ] != ':' )
302- return 0 ; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
303- * val = body + 1 ; /* "atom_name:val" */
304- return 1 ;
305- }
306-
307323/*
308324 * In a format string, find the next occurrence of %(atom).
309325 */
@@ -845,7 +861,6 @@ static void populate_value(struct ref_array_item *ref)
845861 int deref = 0 ;
846862 const char * refname ;
847863 const char * formatp ;
848- const char * valp ;
849864 struct branch * branch = NULL ;
850865
851866 v -> handler = append_atom ;
@@ -909,34 +924,8 @@ static void populate_value(struct ref_array_item *ref)
909924 else
910925 v -> s = " " ;
911926 continue ;
912- } else if (match_atom_name (name , "align" , & valp )) {
913- struct align * align = & v -> u .align ;
914- struct string_list params = STRING_LIST_INIT_DUP ;
915- int i ;
916- int width = -1 ;
917-
918- if (!valp )
919- die (_ ("expected format: %%(align:<width>,<position>)" ));
920-
921- align -> position = ALIGN_LEFT ;
922-
923- string_list_split (& params , valp , ',' , -1 );
924- for (i = 0 ; i < params .nr ; i ++ ) {
925- const char * s = params .items [i ].string ;
926- int position ;
927-
928- if (!strtoul_ui (s , 10 , (unsigned int * )& width ))
929- ;
930- else if ((position = parse_align_position (s )) >= 0 )
931- align -> position = position ;
932- else
933- die (_ ("improper format entered align:%s" ), s );
934- }
935-
936- if (width < 0 )
937- die (_ ("positive width expected with the %%(align) atom" ));
938- align -> width = width ;
939- string_list_clear (& params , 0 );
927+ } else if (starts_with (name , "align" )) {
928+ v -> u .align = atom -> u .align ;
940929 v -> handler = align_atom_handler ;
941930 continue ;
942931 } else if (!strcmp (name , "end" )) {
0 commit comments