@@ -458,35 +458,28 @@ static int auto_number = 1;
458458
459459static char * default_attach = NULL ;
460460
461- static char * * extra_hdr ;
462- static int extra_hdr_nr ;
463- static int extra_hdr_alloc ;
464-
465- static char * * extra_to ;
466- static int extra_to_nr ;
467- static int extra_to_alloc ;
468-
469- static char * * extra_cc ;
470- static int extra_cc_nr ;
471- static int extra_cc_alloc ;
461+ static struct string_list extra_hdr ;
462+ static struct string_list extra_to ;
463+ static struct string_list extra_cc ;
472464
473465static void add_header (const char * value )
474466{
467+ struct string_list_item * item ;
475468 int len = strlen (value );
476469 while (len && value [len - 1 ] == '\n' )
477470 len -- ;
471+
478472 if (!strncasecmp (value , "to: " , 4 )) {
479- ALLOC_GROW (extra_to , extra_to_nr + 1 , extra_to_alloc );
480- extra_to [extra_to_nr ++ ] = xstrndup (value + 4 , len - 4 );
481- return ;
482- }
483- if (!strncasecmp (value , "cc: " , 4 )) {
484- ALLOC_GROW (extra_cc , extra_cc_nr + 1 , extra_cc_alloc );
485- extra_cc [extra_cc_nr ++ ] = xstrndup (value + 4 , len - 4 );
486- return ;
473+ item = string_list_append (value + 4 , & extra_to );
474+ len -= 4 ;
475+ } else if (!strncasecmp (value , "cc: " , 4 )) {
476+ item = string_list_append (value + 4 , & extra_cc );
477+ len -= 4 ;
478+ } else {
479+ item = string_list_append (value , & extra_hdr );
487480 }
488- ALLOC_GROW ( extra_hdr , extra_hdr_nr + 1 , extra_hdr_alloc );
489- extra_hdr [ extra_hdr_nr ++ ] = xstrndup ( value , len ) ;
481+
482+ item -> string [ len ] = '\0' ;
490483}
491484
492485#define THREAD_SHALLOW 1
@@ -504,11 +497,16 @@ static int git_format_config(const char *var, const char *value, void *cb)
504497 }
505498 if (!strcmp (var , "format.suffix" ))
506499 return git_config_string (& fmt_patch_suffix , var , value );
500+ if (!strcmp (var , "format.to" )) {
501+ if (!value )
502+ return config_error_nonbool (var );
503+ string_list_append (value , & extra_to );
504+ return 0 ;
505+ }
507506 if (!strcmp (var , "format.cc" )) {
508507 if (!value )
509508 return config_error_nonbool (var );
510- ALLOC_GROW (extra_cc , extra_cc_nr + 1 , extra_cc_alloc );
511- extra_cc [extra_cc_nr ++ ] = xstrdup (value );
509+ string_list_append (value , & extra_cc );
512510 return 0 ;
513511 }
514512 if (!strcmp (var , "diff.color" ) || !strcmp (var , "color.diff" )) {
@@ -871,14 +869,31 @@ static int inline_callback(const struct option *opt, const char *arg, int unset)
871869
872870static int header_callback (const struct option * opt , const char * arg , int unset )
873871{
874- add_header (arg );
872+ if (unset ) {
873+ string_list_clear (& extra_hdr , 0 );
874+ string_list_clear (& extra_to , 0 );
875+ string_list_clear (& extra_cc , 0 );
876+ } else {
877+ add_header (arg );
878+ }
879+ return 0 ;
880+ }
881+
882+ static int to_callback (const struct option * opt , const char * arg , int unset )
883+ {
884+ if (unset )
885+ string_list_clear (& extra_to , 0 );
886+ else
887+ string_list_append (arg , & extra_to );
875888 return 0 ;
876889}
877890
878891static int cc_callback (const struct option * opt , const char * arg , int unset )
879892{
880- ALLOC_GROW (extra_cc , extra_cc_nr + 1 , extra_cc_alloc );
881- extra_cc [extra_cc_nr ++ ] = xstrdup (arg );
893+ if (unset )
894+ string_list_clear (& extra_cc , 0 );
895+ else
896+ string_list_append (arg , & extra_cc );
882897 return 0 ;
883898}
884899
@@ -937,10 +952,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
937952 PARSE_OPT_NONEG | PARSE_OPT_NOARG },
938953 OPT_GROUP ("Messaging" ),
939954 { OPTION_CALLBACK , 0 , "add-header" , NULL , "header" ,
940- "add email header" , PARSE_OPT_NONEG ,
941- header_callback },
955+ "add email header" , 0 , header_callback },
956+ { OPTION_CALLBACK , 0 , "to" , NULL , "email" , "add To: header" ,
957+ 0 , to_callback },
942958 { OPTION_CALLBACK , 0 , "cc" , NULL , "email" , "add Cc: header" ,
943- PARSE_OPT_NONEG , cc_callback },
959+ 0 , cc_callback },
944960 OPT_STRING (0 , "in-reply-to" , & in_reply_to , "message-id" ,
945961 "make first mail a reply to <message-id>" ),
946962 { OPTION_CALLBACK , 0 , "attach" , & rev , "boundary" ,
@@ -956,6 +972,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
956972 OPT_END ()
957973 };
958974
975+ extra_hdr .strdup_strings = 1 ;
976+ extra_to .strdup_strings = 1 ;
977+ extra_cc .strdup_strings = 1 ;
959978 git_config (git_format_config , NULL );
960979 init_revisions (& rev , prefix );
961980 rev .commit_format = CMIT_FMT_EMAIL ;
@@ -992,29 +1011,29 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
9921011 add_signoff = xmemdupz (committer , endpos - committer + 1 );
9931012 }
9941013
995- for (i = 0 ; i < extra_hdr_nr ; i ++ ) {
996- strbuf_addstr (& buf , extra_hdr [i ]);
1014+ for (i = 0 ; i < extra_hdr . nr ; i ++ ) {
1015+ strbuf_addstr (& buf , extra_hdr . items [i ]. string );
9971016 strbuf_addch (& buf , '\n' );
9981017 }
9991018
1000- if (extra_to_nr )
1019+ if (extra_to . nr )
10011020 strbuf_addstr (& buf , "To: " );
1002- for (i = 0 ; i < extra_to_nr ; i ++ ) {
1021+ for (i = 0 ; i < extra_to . nr ; i ++ ) {
10031022 if (i )
10041023 strbuf_addstr (& buf , " " );
1005- strbuf_addstr (& buf , extra_to [i ]);
1006- if (i + 1 < extra_to_nr )
1024+ strbuf_addstr (& buf , extra_to . items [i ]. string );
1025+ if (i + 1 < extra_to . nr )
10071026 strbuf_addch (& buf , ',' );
10081027 strbuf_addch (& buf , '\n' );
10091028 }
10101029
1011- if (extra_cc_nr )
1030+ if (extra_cc . nr )
10121031 strbuf_addstr (& buf , "Cc: " );
1013- for (i = 0 ; i < extra_cc_nr ; i ++ ) {
1032+ for (i = 0 ; i < extra_cc . nr ; i ++ ) {
10141033 if (i )
10151034 strbuf_addstr (& buf , " " );
1016- strbuf_addstr (& buf , extra_cc [i ]);
1017- if (i + 1 < extra_cc_nr )
1035+ strbuf_addstr (& buf , extra_cc . items [i ]. string );
1036+ if (i + 1 < extra_cc . nr )
10181037 strbuf_addch (& buf , ',' );
10191038 strbuf_addch (& buf , '\n' );
10201039 }
@@ -1223,6 +1242,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
12231242 fclose (stdout );
12241243 }
12251244 free (list );
1245+ string_list_clear (& extra_to , 0 );
1246+ string_list_clear (& extra_cc , 0 );
1247+ string_list_clear (& extra_hdr , 0 );
12261248 if (ignore_if_in_upstream )
12271249 free_patch_ids (& ids );
12281250 return 0 ;
0 commit comments