@@ -887,8 +887,16 @@ static int git_format_config(const char *var, const char *value, void *cb)
887
887
static const char * output_directory = NULL ;
888
888
static int outdir_offset ;
889
889
890
+ static FILE * fopen_excl (const char * filename )
891
+ {
892
+ int fd = open (filename , O_CREAT | O_EXCL | O_WRONLY , 0666 );
893
+ if (fd < 0 )
894
+ return NULL ;
895
+ return fdopen (fd , "w" );
896
+ }
897
+
890
898
static int open_next_file (struct commit * commit , const char * subject ,
891
- struct rev_info * rev , int quiet )
899
+ struct rev_info * rev , int quiet , int clobber )
892
900
{
893
901
struct strbuf filename = STRBUF_INIT ;
894
902
int suffix_len = strlen (rev -> patch_suffix ) + 1 ;
@@ -913,7 +921,12 @@ static int open_next_file(struct commit *commit, const char *subject,
913
921
if (!quiet )
914
922
printf ("%s\n" , filename .buf + outdir_offset );
915
923
916
- if ((rev -> diffopt .file = fopen (filename .buf , "w" )) == NULL ) {
924
+ if (clobber )
925
+ rev -> diffopt .file = fopen (filename .buf , "w" );
926
+ else
927
+ rev -> diffopt .file = fopen_excl (filename .buf );
928
+
929
+ if (!rev -> diffopt .file ) {
917
930
error_errno (_ ("cannot open patch file %s" ), filename .buf );
918
931
strbuf_release (& filename );
919
932
return -1 ;
@@ -1050,7 +1063,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1050
1063
struct commit * origin ,
1051
1064
int nr , struct commit * * list ,
1052
1065
const char * branch_name ,
1053
- int quiet )
1066
+ int quiet ,
1067
+ int clobber )
1054
1068
{
1055
1069
const char * committer ;
1056
1070
const char * body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n" ;
@@ -1069,7 +1083,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1069
1083
committer = git_committer_info (0 );
1070
1084
1071
1085
if (!use_stdout &&
1072
- open_next_file (NULL , rev -> numbered_files ? NULL : "cover-letter" , rev , quiet ))
1086
+ open_next_file (NULL , rev -> numbered_files ? NULL : "cover-letter" ,
1087
+ rev , quiet , clobber ))
1073
1088
die (_ ("failed to create cover-letter file" ));
1074
1089
1075
1090
log_write_email_headers (rev , head , & pp .after_subject , & need_8bit_cte , 0 );
@@ -1533,6 +1548,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1533
1548
struct strbuf buf = STRBUF_INIT ;
1534
1549
int use_patch_format = 0 ;
1535
1550
int quiet = 0 ;
1551
+ int clobber = 1 ;
1536
1552
int reroll_count = -1 ;
1537
1553
char * branch_name = NULL ;
1538
1554
char * base_commit = NULL ;
@@ -1619,6 +1635,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1619
1635
OPT__QUIET (& quiet , N_ ("don't print the patch filenames" )),
1620
1636
OPT_BOOL (0 , "progress" , & show_progress ,
1621
1637
N_ ("show progress while generating patches" )),
1638
+ OPT_BOOL (0 , "clobber" , & clobber ,
1639
+ N_ ("allow overwriting output files" )),
1622
1640
OPT_CALLBACK (0 , "interdiff" , & idiff_prev , N_ ("rev" ),
1623
1641
N_ ("show changes against <rev> in cover letter or single patch" ),
1624
1642
parse_opt_object_name ),
@@ -1909,7 +1927,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1909
1927
if (thread )
1910
1928
gen_message_id (& rev , "cover" );
1911
1929
make_cover_letter (& rev , use_stdout ,
1912
- origin , nr , list , branch_name , quiet );
1930
+ origin , nr , list , branch_name ,
1931
+ quiet , clobber );
1913
1932
print_bases (& bases , rev .diffopt .file );
1914
1933
print_signature (rev .diffopt .file );
1915
1934
total ++ ;
@@ -1964,7 +1983,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1964
1983
}
1965
1984
1966
1985
if (!use_stdout &&
1967
- open_next_file (rev .numbered_files ? NULL : commit , NULL , & rev , quiet ))
1986
+ open_next_file (rev .numbered_files ? NULL : commit , NULL ,
1987
+ & rev , quiet , clobber ))
1968
1988
die (_ ("failed to create output files" ));
1969
1989
shown = log_tree_commit (& rev , commit );
1970
1990
free_commit_buffer (the_repository -> parsed_objects ,
0 commit comments