@@ -539,30 +539,29 @@ static FILE *realstdout = NULL;
539
539
static const char * output_directory = NULL ;
540
540
static int outdir_offset ;
541
541
542
- static int reopen_stdout (const char * oneline , struct rev_info * rev )
542
+ static int reopen_stdout (struct commit * commit , struct rev_info * rev )
543
543
{
544
- char filename [PATH_MAX ];
545
- int len = 0 ;
544
+ struct strbuf filename = STRBUF_INIT ;
546
545
int suffix_len = strlen (fmt_patch_suffix ) + 1 ;
547
546
548
547
if (output_directory ) {
549
- len = snprintf (filename , sizeof (filename ), "%s" ,
550
- output_directory );
551
- if (len >=
552
- sizeof (filename ) - FORMAT_PATCH_NAME_MAX - suffix_len )
548
+ strbuf_addstr (& filename , output_directory );
549
+ if (filename .len >=
550
+ PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len )
553
551
return error ("name of output directory is too long" );
554
- if (filename [ len - 1 ] != '/' )
555
- filename [ len ++ ] = '/' ;
552
+ if (filename . buf [ filename . len - 1 ] != '/' )
553
+ strbuf_addch ( & filename , '/' ) ;
556
554
}
557
555
558
- strncpy ( filename + len , oneline , PATH_MAX - len );
556
+ get_patch_filename ( commit , rev -> nr , fmt_patch_suffix , & filename );
559
557
560
558
if (!DIFF_OPT_TST (& rev -> diffopt , QUIET ))
561
- fprintf (realstdout , "%s\n" , filename + outdir_offset );
559
+ fprintf (realstdout , "%s\n" , filename . buf + outdir_offset );
562
560
563
- if (freopen (filename , "w" , stdout ) == NULL )
564
- return error ("Cannot open patch file %s" ,filename );
561
+ if (freopen (filename . buf , "w" , stdout ) == NULL )
562
+ return error ("Cannot open patch file %s" , filename . buf );
565
563
564
+ strbuf_release (& filename );
566
565
return 0 ;
567
566
}
568
567
@@ -643,26 +642,42 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
643
642
const char * encoding = "utf-8" ;
644
643
struct diff_options opts ;
645
644
int need_8bit_cte = 0 ;
646
- char filename [ PATH_MAX ] ;
645
+ struct commit * commit = NULL ;
647
646
648
647
if (rev -> commit_format != CMIT_FMT_EMAIL )
649
648
die ("Cover letter needs email format" );
650
649
651
- if (numbered_files )
652
- sprintf (filename , "0" );
653
- else
654
- sprintf (filename , "%04d-cover-letter%s" , 0 , fmt_patch_suffix );
650
+ committer = git_committer_info (0 );
651
+ head_sha1 = sha1_to_hex (head -> object .sha1 );
655
652
656
- if (!use_stdout && reopen_stdout (filename , rev ))
653
+ if (!numbered_files ) {
654
+ /*
655
+ * We fake a commit for the cover letter so we get the filename
656
+ * desired.
657
+ */
658
+ commit = xcalloc (1 , sizeof (* commit ));
659
+ commit -> buffer = xmalloc (400 );
660
+ snprintf (commit -> buffer , 400 ,
661
+ "tree 0000000000000000000000000000000000000000\n"
662
+ "parent %s\n"
663
+ "author %s\n"
664
+ "committer %s\n\n"
665
+ "cover letter\n" ,
666
+ head_sha1 , committer , committer );
667
+ }
668
+
669
+ if (!use_stdout && reopen_stdout (commit , rev ))
657
670
return ;
658
671
659
- head_sha1 = sha1_to_hex (head -> object .sha1 );
672
+ if (commit ) {
673
+
674
+ free (commit -> buffer );
675
+ free (commit );
676
+ }
660
677
661
678
log_write_email_headers (rev , head_sha1 , & subject_start , & extra_headers ,
662
679
& need_8bit_cte );
663
680
664
- committer = git_committer_info (0 );
665
-
666
681
msg = body ;
667
682
pp_user_info (NULL , CMIT_FMT_EMAIL , & sb , committer , DATE_RFC2822 ,
668
683
encoding );
@@ -766,7 +781,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
766
781
struct patch_ids ids ;
767
782
char * add_signoff = NULL ;
768
783
struct strbuf buf = STRBUF_INIT ;
769
- struct strbuf patch_filename = STRBUF_INIT ;
770
784
771
785
git_config (git_format_config , NULL );
772
786
init_revisions (& rev , prefix );
@@ -1070,11 +1084,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1070
1084
gen_message_id (& rev , sha1_to_hex (commit -> object .sha1 ));
1071
1085
}
1072
1086
1073
- get_patch_filename (numbered_files ? NULL : commit , rev .nr ,
1074
- fmt_patch_suffix , & patch_filename );
1075
- if (!use_stdout && reopen_stdout (patch_filename .buf , & rev ))
1087
+ if (!use_stdout && reopen_stdout (numbered_files ? NULL : commit ,
1088
+ & rev ))
1076
1089
die ("Failed to create output files" );
1077
- strbuf_setlen (& patch_filename , 0 );
1078
1090
shown = log_tree_commit (& rev , commit );
1079
1091
free (commit -> buffer );
1080
1092
commit -> buffer = NULL ;
@@ -1098,7 +1110,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1098
1110
if (!use_stdout )
1099
1111
fclose (stdout );
1100
1112
}
1101
- strbuf_release (& patch_filename );
1102
1113
free (list );
1103
1114
if (ignore_if_in_upstream )
1104
1115
free_patch_ids (& ids );
0 commit comments