@@ -500,10 +500,31 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
500
500
return 0 ;
501
501
}
502
502
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 )
504
521
{
505
522
size_t len ;
506
523
struct strbuf seps = STRBUF_INIT ;
524
+ struct trailer_item * item ;
525
+ int tok_len ;
526
+ struct list_head * pos ;
527
+
507
528
strbuf_addstr (& seps , separators );
508
529
strbuf_addch (& seps , '=' );
509
530
len = strcspn (trailer , seps .buf );
@@ -523,74 +544,31 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tra
523
544
strbuf_addstr (tok , trailer );
524
545
strbuf_trim (tok );
525
546
}
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 );
575
547
576
548
/* 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 ;
577
551
list_for_each (pos , & conf_head ) {
578
552
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
+ }
583
560
}
584
561
585
- return new_trailer_item (NULL ,
586
- strbuf_detach (& tok , NULL ),
587
- strbuf_detach (& val , NULL ));
562
+ return 0 ;
588
563
}
589
564
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 )
591
567
{
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 );
594
572
list_add_tail (& new -> list , head );
595
573
}
596
574
@@ -599,21 +577,28 @@ static void process_command_line_args(struct list_head *arg_head,
599
577
{
600
578
struct string_list_item * tr ;
601
579
struct trailer_item * item ;
580
+ struct strbuf tok = STRBUF_INIT ;
581
+ struct strbuf val = STRBUF_INIT ;
582
+ const struct conf_info * conf ;
602
583
struct list_head * pos ;
603
584
604
585
/* Add a trailer item for each configured trailer with a command */
605
586
list_for_each (pos , & conf_head ) {
606
587
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 );
611
593
}
612
594
613
595
/* Add a trailer item for each trailer on the command line */
614
596
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 );
617
602
}
618
603
}
619
604
@@ -734,6 +719,9 @@ static int process_input_file(FILE *outfile,
734
719
{
735
720
int count = 0 ;
736
721
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 ;
737
725
738
726
/* Get the line count */
739
727
while (lines [count ])
@@ -751,10 +739,12 @@ static int process_input_file(FILE *outfile,
751
739
752
740
/* Parse trailer lines */
753
741
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 );
758
748
}
759
749
760
750
return trailer_end ;
0 commit comments