@@ -871,8 +871,16 @@ static int git_format_config(const char *var, const char *value, void *cb)
871
871
static const char * output_directory = NULL ;
872
872
static int outdir_offset ;
873
873
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
+
874
882
static 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 )
876
884
{
877
885
struct strbuf filename = STRBUF_INIT ;
878
886
int suffix_len = strlen (rev -> patch_suffix ) + 1 ;
@@ -897,7 +905,12 @@ static int open_next_file(struct commit *commit, const char *subject,
897
905
if (!quiet )
898
906
printf ("%s\n" , filename .buf + outdir_offset );
899
907
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 ) {
901
914
error_errno (_ ("cannot open patch file %s" ), filename .buf );
902
915
strbuf_release (& filename );
903
916
return -1 ;
@@ -1034,7 +1047,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1034
1047
struct commit * origin ,
1035
1048
int nr , struct commit * * list ,
1036
1049
const char * branch_name ,
1037
- int quiet )
1050
+ int quiet ,
1051
+ int clobber )
1038
1052
{
1039
1053
const char * committer ;
1040
1054
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,
1053
1067
committer = git_committer_info (0 );
1054
1068
1055
1069
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 ))
1057
1072
die (_ ("failed to create cover-letter file" ));
1058
1073
1059
1074
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)
1517
1532
struct strbuf buf = STRBUF_INIT ;
1518
1533
int use_patch_format = 0 ;
1519
1534
int quiet = 0 ;
1535
+ int clobber = 1 ;
1520
1536
int reroll_count = -1 ;
1521
1537
char * branch_name = NULL ;
1522
1538
char * base_commit = NULL ;
@@ -1603,6 +1619,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1603
1619
OPT__QUIET (& quiet , N_ ("don't print the patch filenames" )),
1604
1620
OPT_BOOL (0 , "progress" , & show_progress ,
1605
1621
N_ ("show progress while generating patches" )),
1622
+ OPT_BOOL (0 , "clobber" , & clobber ,
1623
+ N_ ("allow overwriting output files" )),
1606
1624
OPT_CALLBACK (0 , "interdiff" , & idiff_prev , N_ ("rev" ),
1607
1625
N_ ("show changes against <rev> in cover letter or single patch" ),
1608
1626
parse_opt_object_name ),
@@ -1893,7 +1911,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1893
1911
if (thread )
1894
1912
gen_message_id (& rev , "cover" );
1895
1913
make_cover_letter (& rev , use_stdout ,
1896
- origin , nr , list , branch_name , quiet );
1914
+ origin , nr , list , branch_name ,
1915
+ quiet , clobber );
1897
1916
print_bases (& bases , rev .diffopt .file );
1898
1917
print_signature (rev .diffopt .file );
1899
1918
total ++ ;
@@ -1948,7 +1967,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1948
1967
}
1949
1968
1950
1969
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 ))
1952
1972
die (_ ("failed to create output files" ));
1953
1973
shown = log_tree_commit (& rev , commit );
1954
1974
free_commit_buffer (the_repository -> parsed_objects ,
0 commit comments