@@ -871,8 +871,16 @@ static int git_format_config(const char *var, const char *value, void *cb)
871871static const char * output_directory = NULL ;
872872static int outdir_offset ;
873873
874+ static FILE * fopen_excl (const char * filename )
875+ {
876+ int fd = open (filename , O_CREAT | O_EXCL | O_WRONLY , 0666 );
877+ if (fd < 0 )
878+ return NULL ;
879+ return fdopen (fd , "w" );
880+ }
881+
874882static int open_next_file (struct commit * commit , const char * subject ,
875- struct rev_info * rev , int quiet )
883+ struct rev_info * rev , int quiet , int clobber )
876884{
877885 struct strbuf filename = STRBUF_INIT ;
878886 int suffix_len = strlen (rev -> patch_suffix ) + 1 ;
@@ -897,7 +905,12 @@ static int open_next_file(struct commit *commit, const char *subject,
897905 if (!quiet )
898906 printf ("%s\n" , filename .buf + outdir_offset );
899907
900- if ((rev -> diffopt .file = fopen (filename .buf , "w" )) == NULL ) {
908+ if (clobber )
909+ rev -> diffopt .file = fopen (filename .buf , "w" );
910+ else
911+ rev -> diffopt .file = fopen_excl (filename .buf );
912+
913+ if (!rev -> diffopt .file ) {
901914 error_errno (_ ("cannot open patch file %s" ), filename .buf );
902915 strbuf_release (& filename );
903916 return -1 ;
@@ -1034,7 +1047,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
10341047 struct commit * origin ,
10351048 int nr , struct commit * * list ,
10361049 const char * branch_name ,
1037- int quiet )
1050+ int quiet ,
1051+ int clobber )
10381052{
10391053 const char * committer ;
10401054 const char * body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n" ;
@@ -1053,7 +1067,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
10531067 committer = git_committer_info (0 );
10541068
10551069 if (!use_stdout &&
1056- open_next_file (NULL , rev -> numbered_files ? NULL : "cover-letter" , rev , quiet ))
1070+ open_next_file (NULL , rev -> numbered_files ? NULL : "cover-letter" ,
1071+ rev , quiet , clobber ))
10571072 die (_ ("failed to create cover-letter file" ));
10581073
10591074 log_write_email_headers (rev , head , & pp .after_subject , & need_8bit_cte , 0 );
@@ -1517,6 +1532,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
15171532 struct strbuf buf = STRBUF_INIT ;
15181533 int use_patch_format = 0 ;
15191534 int quiet = 0 ;
1535+ int clobber = 1 ;
15201536 int reroll_count = -1 ;
15211537 char * branch_name = NULL ;
15221538 char * base_commit = NULL ;
@@ -1603,6 +1619,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
16031619 OPT__QUIET (& quiet , N_ ("don't print the patch filenames" )),
16041620 OPT_BOOL (0 , "progress" , & show_progress ,
16051621 N_ ("show progress while generating patches" )),
1622+ OPT_BOOL (0 , "clobber" , & clobber ,
1623+ N_ ("allow overwriting output files" )),
16061624 OPT_CALLBACK (0 , "interdiff" , & idiff_prev , N_ ("rev" ),
16071625 N_ ("show changes against <rev> in cover letter or single patch" ),
16081626 parse_opt_object_name ),
@@ -1893,7 +1911,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18931911 if (thread )
18941912 gen_message_id (& rev , "cover" );
18951913 make_cover_letter (& rev , use_stdout ,
1896- origin , nr , list , branch_name , quiet );
1914+ origin , nr , list , branch_name ,
1915+ quiet , clobber );
18971916 print_bases (& bases , rev .diffopt .file );
18981917 print_signature (rev .diffopt .file );
18991918 total ++ ;
@@ -1948,7 +1967,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19481967 }
19491968
19501969 if (!use_stdout &&
1951- open_next_file (rev .numbered_files ? NULL : commit , NULL , & rev , quiet ))
1970+ open_next_file (rev .numbered_files ? NULL : commit , NULL ,
1971+ & rev , quiet , clobber ))
19521972 die (_ ("failed to create output files" ));
19531973 shown = log_tree_commit (& rev , commit );
19541974 free_commit_buffer (the_repository -> parsed_objects ,
0 commit comments