@@ -29,6 +29,12 @@ struct trailer_item {
2929 struct list_head list ;
3030 char * token ;
3131 char * value ;
32+ };
33+
34+ struct arg_item {
35+ struct list_head list ;
36+ char * token ;
37+ char * value ;
3238 struct conf_info conf ;
3339};
3440
@@ -62,7 +68,7 @@ static size_t token_len_without_separator(const char *token, size_t len)
6268 return len ;
6369}
6470
65- static int same_token (struct trailer_item * a , struct trailer_item * b )
71+ static int same_token (struct trailer_item * a , struct arg_item * b )
6672{
6773 size_t a_len = token_len_without_separator (a -> token , strlen (a -> token ));
6874 size_t b_len = token_len_without_separator (b -> token , strlen (b -> token ));
@@ -71,12 +77,12 @@ static int same_token(struct trailer_item *a, struct trailer_item *b)
7177 return !strncasecmp (a -> token , b -> token , min_len );
7278}
7379
74- static int same_value (struct trailer_item * a , struct trailer_item * b )
80+ static int same_value (struct trailer_item * a , struct arg_item * b )
7581{
7682 return !strcasecmp (a -> value , b -> value );
7783}
7884
79- static int same_trailer (struct trailer_item * a , struct trailer_item * b )
85+ static int same_trailer (struct trailer_item * a , struct arg_item * b )
8086{
8187 return same_token (a , b ) && same_value (a , b );
8288}
@@ -97,6 +103,13 @@ static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *
97103}
98104
99105static void free_trailer_item (struct trailer_item * item )
106+ {
107+ free (item -> token );
108+ free (item -> value );
109+ free (item );
110+ }
111+
112+ static void free_arg_item (struct arg_item * item )
100113{
101114 free (item -> conf .name );
102115 free (item -> conf .key );
@@ -137,17 +150,29 @@ static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
137150 }
138151}
139152
153+ static struct trailer_item * trailer_from_arg (struct arg_item * arg_tok )
154+ {
155+ struct trailer_item * new = xcalloc (sizeof (* new ), 1 );
156+ new -> token = arg_tok -> token ;
157+ new -> value = arg_tok -> value ;
158+ arg_tok -> token = arg_tok -> value = NULL ;
159+ free_arg_item (arg_tok );
160+ return new ;
161+ }
162+
140163static void add_arg_to_input_list (struct trailer_item * on_tok ,
141- struct trailer_item * arg_tok )
164+ struct arg_item * arg_tok )
142165{
143- if (after_or_end (arg_tok -> conf .where ))
144- list_add (& arg_tok -> list , & on_tok -> list );
166+ int aoe = after_or_end (arg_tok -> conf .where );
167+ struct trailer_item * to_add = trailer_from_arg (arg_tok );
168+ if (aoe )
169+ list_add (& to_add -> list , & on_tok -> list );
145170 else
146- list_add_tail (& arg_tok -> list , & on_tok -> list );
171+ list_add_tail (& to_add -> list , & on_tok -> list );
147172}
148173
149174static int check_if_different (struct trailer_item * in_tok ,
150- struct trailer_item * arg_tok ,
175+ struct arg_item * arg_tok ,
151176 int check_all ,
152177 struct list_head * head )
153178{
@@ -200,7 +225,7 @@ static char *apply_command(const char *command, const char *arg)
200225 return result ;
201226}
202227
203- static void apply_item_command (struct trailer_item * in_tok , struct trailer_item * arg_tok )
228+ static void apply_item_command (struct trailer_item * in_tok , struct arg_item * arg_tok )
204229{
205230 if (arg_tok -> conf .command ) {
206231 const char * arg ;
@@ -218,13 +243,13 @@ static void apply_item_command(struct trailer_item *in_tok, struct trailer_item
218243}
219244
220245static void apply_arg_if_exists (struct trailer_item * in_tok ,
221- struct trailer_item * arg_tok ,
246+ struct arg_item * arg_tok ,
222247 struct trailer_item * on_tok ,
223248 struct list_head * head )
224249{
225250 switch (arg_tok -> conf .if_exists ) {
226251 case EXISTS_DO_NOTHING :
227- free_trailer_item (arg_tok );
252+ free_arg_item (arg_tok );
228253 break ;
229254 case EXISTS_REPLACE :
230255 apply_item_command (in_tok , arg_tok );
@@ -241,39 +266,41 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
241266 if (check_if_different (in_tok , arg_tok , 1 , head ))
242267 add_arg_to_input_list (on_tok , arg_tok );
243268 else
244- free_trailer_item (arg_tok );
269+ free_arg_item (arg_tok );
245270 break ;
246271 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR :
247272 apply_item_command (in_tok , arg_tok );
248273 if (check_if_different (on_tok , arg_tok , 0 , head ))
249274 add_arg_to_input_list (on_tok , arg_tok );
250275 else
251- free_trailer_item (arg_tok );
276+ free_arg_item (arg_tok );
252277 break ;
253278 }
254279}
255280
256281static void apply_arg_if_missing (struct list_head * head ,
257- struct trailer_item * arg_tok )
282+ struct arg_item * arg_tok )
258283{
259284 enum action_where where ;
285+ struct trailer_item * to_add ;
260286
261287 switch (arg_tok -> conf .if_missing ) {
262288 case MISSING_DO_NOTHING :
263- free_trailer_item (arg_tok );
289+ free_arg_item (arg_tok );
264290 break ;
265291 case MISSING_ADD :
266292 where = arg_tok -> conf .where ;
267293 apply_item_command (NULL , arg_tok );
294+ to_add = trailer_from_arg (arg_tok );
268295 if (after_or_end (where ))
269- list_add_tail (& arg_tok -> list , head );
296+ list_add_tail (& to_add -> list , head );
270297 else
271- list_add (& arg_tok -> list , head );
298+ list_add (& to_add -> list , head );
272299 }
273300}
274301
275302static int find_same_and_apply_arg (struct list_head * head ,
276- struct trailer_item * arg_tok )
303+ struct arg_item * arg_tok )
277304{
278305 struct list_head * pos ;
279306 struct trailer_item * in_tok ;
@@ -306,11 +333,11 @@ static void process_trailers_lists(struct list_head *head,
306333 struct list_head * arg_head )
307334{
308335 struct list_head * pos , * p ;
309- struct trailer_item * arg_tok ;
336+ struct arg_item * arg_tok ;
310337
311338 list_for_each_safe (pos , p , arg_head ) {
312339 int applied = 0 ;
313- arg_tok = list_entry (pos , struct trailer_item , list );
340+ arg_tok = list_entry (pos , struct arg_item , list );
314341
315342 list_del (pos );
316343
@@ -375,20 +402,20 @@ static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
375402 dst -> command = xstrdup (src -> command );
376403}
377404
378- static struct trailer_item * get_conf_item (const char * name )
405+ static struct arg_item * get_conf_item (const char * name )
379406{
380407 struct list_head * pos ;
381- struct trailer_item * item ;
408+ struct arg_item * item ;
382409
383410 /* Look up item with same name */
384411 list_for_each (pos , & conf_head ) {
385- item = list_entry (pos , struct trailer_item , list );
412+ item = list_entry (pos , struct arg_item , list );
386413 if (!strcasecmp (item -> conf .name , name ))
387414 return item ;
388415 }
389416
390417 /* Item does not already exists, create it */
391- item = xcalloc (sizeof (struct trailer_item ), 1 );
418+ item = xcalloc (sizeof (* item ), 1 );
392419 duplicate_conf (& item -> conf , & default_conf_info );
393420 item -> conf .name = xstrdup (name );
394421
@@ -442,7 +469,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v
442469static int git_trailer_config (const char * conf_key , const char * value , void * cb )
443470{
444471 const char * trailer_item , * variable_name ;
445- struct trailer_item * item ;
472+ struct arg_item * item ;
446473 struct conf_info * conf ;
447474 char * name = NULL ;
448475 enum trailer_info_type type ;
@@ -500,7 +527,7 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
500527 return 0 ;
501528}
502529
503- static const char * token_from_item (struct trailer_item * item , char * tok )
530+ static const char * token_from_item (struct arg_item * item , char * tok )
504531{
505532 if (item -> conf .key )
506533 return item -> conf .key ;
@@ -509,7 +536,7 @@ static const char *token_from_item(struct trailer_item *item, char *tok)
509536 return item -> conf .name ;
510537}
511538
512- static int token_matches_item (const char * tok , struct trailer_item * item , int tok_len )
539+ static int token_matches_item (const char * tok , struct arg_item * item , int tok_len )
513540{
514541 if (!strncasecmp (tok , item -> conf .name , tok_len ))
515542 return 1 ;
@@ -521,7 +548,7 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val,
521548{
522549 size_t len ;
523550 struct strbuf seps = STRBUF_INIT ;
524- struct trailer_item * item ;
551+ struct arg_item * item ;
525552 int tok_len ;
526553 struct list_head * pos ;
527554
@@ -547,12 +574,14 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val,
547574
548575 /* Lookup if the token matches something in the config */
549576 tok_len = token_len_without_separator (tok -> buf , tok -> len );
550- * conf = & default_conf_info ;
577+ if (conf )
578+ * conf = & default_conf_info ;
551579 list_for_each (pos , & conf_head ) {
552- item = list_entry (pos , struct trailer_item , list );
580+ item = list_entry (pos , struct arg_item , list );
553581 if (token_matches_item (tok -> buf , item , tok_len )) {
554582 char * tok_buf = strbuf_detach (tok , NULL );
555- * conf = & item -> conf ;
583+ if (conf )
584+ * conf = & item -> conf ;
556585 strbuf_addstr (tok , token_from_item (item , tok_buf ));
557586 free (tok_buf );
558587 break ;
@@ -562,43 +591,51 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val,
562591 return 0 ;
563592}
564593
565- static void add_trailer_item (struct list_head * head , char * tok , char * val ,
566- const struct conf_info * conf )
594+ static void add_trailer_item (struct list_head * head , char * tok , char * val )
567595{
568596 struct trailer_item * new = xcalloc (sizeof (* new ), 1 );
569597 new -> token = tok ;
570598 new -> value = val ;
571- duplicate_conf (& new -> conf , conf );
572599 list_add_tail (& new -> list , head );
573600}
574601
602+ static void add_arg_item (struct list_head * arg_head , char * tok , char * val ,
603+ const struct conf_info * conf )
604+ {
605+ struct arg_item * new = xcalloc (sizeof (* new ), 1 );
606+ new -> token = tok ;
607+ new -> value = val ;
608+ duplicate_conf (& new -> conf , conf );
609+ list_add_tail (& new -> list , arg_head );
610+ }
611+
575612static void process_command_line_args (struct list_head * arg_head ,
576613 struct string_list * trailers )
577614{
578615 struct string_list_item * tr ;
579- struct trailer_item * item ;
616+ struct arg_item * item ;
580617 struct strbuf tok = STRBUF_INIT ;
581618 struct strbuf val = STRBUF_INIT ;
582619 const struct conf_info * conf ;
583620 struct list_head * pos ;
584621
585- /* Add a trailer item for each configured trailer with a command */
622+ /* Add an arg item for each configured trailer with a command */
586623 list_for_each (pos , & conf_head ) {
587- item = list_entry (pos , struct trailer_item , list );
624+ item = list_entry (pos , struct arg_item , list );
588625 if (item -> conf .command )
589- add_trailer_item (arg_head ,
590- xstrdup (token_from_item (item , NULL )),
591- xstrdup ("" ),
592- & item -> conf );
626+ add_arg_item (arg_head ,
627+ xstrdup (token_from_item (item , NULL )),
628+ xstrdup ("" ),
629+ & item -> conf );
593630 }
594631
595- /* Add a trailer item for each trailer on the command line */
632+ /* Add an arg item for each trailer on the command line */
596633 for_each_string_list_item (tr , trailers ) {
597634 if (!parse_trailer (& tok , & val , & conf , tr -> string ))
598- add_trailer_item (arg_head ,
599- strbuf_detach (& tok , NULL ),
600- strbuf_detach (& val , NULL ),
601- conf );
635+ add_arg_item (arg_head ,
636+ strbuf_detach (& tok , NULL ),
637+ strbuf_detach (& val , NULL ),
638+ conf );
602639 }
603640}
604641
@@ -721,7 +758,6 @@ static int process_input_file(FILE *outfile,
721758 int patch_start , trailer_start , trailer_end , i ;
722759 struct strbuf tok = STRBUF_INIT ;
723760 struct strbuf val = STRBUF_INIT ;
724- const struct conf_info * conf ;
725761
726762 /* Get the line count */
727763 while (lines [count ])
@@ -740,11 +776,10 @@ static int process_input_file(FILE *outfile,
740776 /* Parse trailer lines */
741777 for (i = trailer_start ; i < trailer_end ; i ++ ) {
742778 if (lines [i ]-> buf [0 ] != comment_line_char &&
743- !parse_trailer (& tok , & val , & conf , lines [i ]-> buf ))
779+ !parse_trailer (& tok , & val , NULL , lines [i ]-> buf ))
744780 add_trailer_item (head ,
745781 strbuf_detach (& tok , NULL ),
746- strbuf_detach (& val , NULL ),
747- conf );
782+ strbuf_detach (& val , NULL ));
748783 }
749784
750785 return trailer_end ;
0 commit comments