@@ -500,10 +500,31 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
500500 return 0 ;
501501}
502502
503- static int parse_trailer (struct strbuf * tok , struct strbuf * val , const char * trailer )
503+ static const char * token_from_item (struct trailer_item * item , char * tok )
504+ {
505+ if (item -> conf .key )
506+ return item -> conf .key ;
507+ if (tok )
508+ return tok ;
509+ return item -> conf .name ;
510+ }
511+
512+ static int token_matches_item (const char * tok , struct trailer_item * item , int tok_len )
513+ {
514+ if (!strncasecmp (tok , item -> conf .name , tok_len ))
515+ return 1 ;
516+ return item -> conf .key ? !strncasecmp (tok , item -> conf .key , tok_len ) : 0 ;
517+ }
518+
519+ static int parse_trailer (struct strbuf * tok , struct strbuf * val ,
520+ const struct conf_info * * conf , const char * trailer )
504521{
505522 size_t len ;
506523 struct strbuf seps = STRBUF_INIT ;
524+ struct trailer_item * item ;
525+ int tok_len ;
526+ struct list_head * pos ;
527+
507528 strbuf_addstr (& seps , separators );
508529 strbuf_addch (& seps , '=' );
509530 len = strcspn (trailer , seps .buf );
@@ -523,74 +544,31 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tra
523544 strbuf_addstr (tok , trailer );
524545 strbuf_trim (tok );
525546 }
526- return 0 ;
527- }
528-
529- static const char * token_from_item (struct trailer_item * item , char * tok )
530- {
531- if (item -> conf .key )
532- return item -> conf .key ;
533- if (tok )
534- return tok ;
535- return item -> conf .name ;
536- }
537-
538- static struct trailer_item * new_trailer_item (struct trailer_item * conf_item ,
539- char * tok , char * val )
540- {
541- struct trailer_item * new = xcalloc (sizeof (* new ), 1 );
542- new -> value = val ? val : xstrdup ("" );
543-
544- if (conf_item ) {
545- duplicate_conf (& new -> conf , & conf_item -> conf );
546- new -> token = xstrdup (token_from_item (conf_item , tok ));
547- free (tok );
548- } else {
549- duplicate_conf (& new -> conf , & default_conf_info );
550- new -> token = tok ;
551- }
552-
553- return new ;
554- }
555-
556- static int token_matches_item (const char * tok , struct trailer_item * item , int tok_len )
557- {
558- if (!strncasecmp (tok , item -> conf .name , tok_len ))
559- return 1 ;
560- return item -> conf .key ? !strncasecmp (tok , item -> conf .key , tok_len ) : 0 ;
561- }
562-
563- static struct trailer_item * create_trailer_item (const char * string )
564- {
565- struct strbuf tok = STRBUF_INIT ;
566- struct strbuf val = STRBUF_INIT ;
567- struct trailer_item * item ;
568- int tok_len ;
569- struct list_head * pos ;
570-
571- if (parse_trailer (& tok , & val , string ))
572- return NULL ;
573-
574- tok_len = token_len_without_separator (tok .buf , tok .len );
575547
576548 /* Lookup if the token matches something in the config */
549+ tok_len = token_len_without_separator (tok -> buf , tok -> len );
550+ * conf = & default_conf_info ;
577551 list_for_each (pos , & conf_head ) {
578552 item = list_entry (pos , struct trailer_item , list );
579- if (token_matches_item (tok .buf , item , tok_len ))
580- return new_trailer_item (item ,
581- strbuf_detach (& tok , NULL ),
582- strbuf_detach (& val , NULL ));
553+ if (token_matches_item (tok -> buf , item , tok_len )) {
554+ char * tok_buf = strbuf_detach (tok , NULL );
555+ * conf = & item -> conf ;
556+ strbuf_addstr (tok , token_from_item (item , tok_buf ));
557+ free (tok_buf );
558+ break ;
559+ }
583560 }
584561
585- return new_trailer_item (NULL ,
586- strbuf_detach (& tok , NULL ),
587- strbuf_detach (& val , NULL ));
562+ return 0 ;
588563}
589564
590- static void add_trailer_item (struct list_head * head , struct trailer_item * new )
565+ static void add_trailer_item (struct list_head * head , char * tok , char * val ,
566+ const struct conf_info * conf )
591567{
592- if (!new )
593- return ;
568+ struct trailer_item * new = xcalloc (sizeof (* new ), 1 );
569+ new -> token = tok ;
570+ new -> value = val ;
571+ duplicate_conf (& new -> conf , conf );
594572 list_add_tail (& new -> list , head );
595573}
596574
@@ -599,21 +577,28 @@ static void process_command_line_args(struct list_head *arg_head,
599577{
600578 struct string_list_item * tr ;
601579 struct trailer_item * item ;
580+ struct strbuf tok = STRBUF_INIT ;
581+ struct strbuf val = STRBUF_INIT ;
582+ const struct conf_info * conf ;
602583 struct list_head * pos ;
603584
604585 /* Add a trailer item for each configured trailer with a command */
605586 list_for_each (pos , & conf_head ) {
606587 item = list_entry (pos , struct trailer_item , list );
607- if (item -> conf .command ) {
608- struct trailer_item * new = new_trailer_item (item , NULL , NULL );
609- add_trailer_item (arg_head , new );
610- }
588+ if (item -> conf .command )
589+ add_trailer_item (arg_head ,
590+ xstrdup (token_from_item (item , NULL )),
591+ xstrdup ("" ),
592+ & item -> conf );
611593 }
612594
613595 /* Add a trailer item for each trailer on the command line */
614596 for_each_string_list_item (tr , trailers ) {
615- struct trailer_item * new = create_trailer_item (tr -> string );
616- add_trailer_item (arg_head , new );
597+ 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 );
617602 }
618603}
619604
@@ -734,6 +719,9 @@ static int process_input_file(FILE *outfile,
734719{
735720 int count = 0 ;
736721 int patch_start , trailer_start , trailer_end , i ;
722+ struct strbuf tok = STRBUF_INIT ;
723+ struct strbuf val = STRBUF_INIT ;
724+ const struct conf_info * conf ;
737725
738726 /* Get the line count */
739727 while (lines [count ])
@@ -751,10 +739,12 @@ static int process_input_file(FILE *outfile,
751739
752740 /* Parse trailer lines */
753741 for (i = trailer_start ; i < trailer_end ; i ++ ) {
754- if (lines [i ]-> buf [0 ] != comment_line_char ) {
755- struct trailer_item * new = create_trailer_item (lines [i ]-> buf );
756- add_trailer_item (head , new );
757- }
742+ if (lines [i ]-> buf [0 ] != comment_line_char &&
743+ !parse_trailer (& tok , & val , & conf , lines [i ]-> buf ))
744+ add_trailer_item (head ,
745+ strbuf_detach (& tok , NULL ),
746+ strbuf_detach (& val , NULL ),
747+ conf );
758748 }
759749
760750 return trailer_end ;
0 commit comments