Skip to content

Commit 55f1dc0

Browse files
committed
Merge branch 'jc/format-patch-noclobber' into pu
"git format-patch" used to overwrite an existing patch/cover-letter file. A new "--no-clobber" option stops it. Undecided but inclined to discard. * jc/format-patch-noclobber: format-patch: --no-clobber refrains from overwriting output files
2 parents cd315b3 + 0799f93 commit 55f1dc0

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

Documentation/git-format-patch.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SYNOPSIS
2525
[--[no-]cover-letter] [--quiet] [--notes[=<ref>]]
2626
[--interdiff=<previous>]
2727
[--range-diff=<previous> [--creation-factor=<percent>]]
28-
[--progress]
28+
[--progress] [--[no-]clobber]
2929
[<common diff options>]
3030
[ <since> | <revision range> ]
3131

@@ -93,6 +93,12 @@ include::diff-options.txt[]
9393
Use <dir> to store the resulting files, instead of the
9494
current working directory.
9595

96+
--clobber::
97+
--no-clobber::
98+
(experimental)
99+
Allow overwriting existing files, which is the default. To
100+
make the command refrain from overwriting, use `--no-clobber`.
101+
96102
-n::
97103
--numbered::
98104
Name output in '[PATCH n/m]' format, even with a single patch.

builtin/log.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,16 @@ static int git_format_config(const char *var, const char *value, void *cb)
871871
static const char *output_directory = NULL;
872872
static 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+
874882
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)
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,

t/t4014-format-patch.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,22 @@ test_expect_success 'failure to write cover-letter aborts gracefully' '
614614
test_must_fail git format-patch --no-renames --cover-letter -1
615615
'
616616

617+
test_expect_success 'refrain from overwriting a patch with --no-clobber' '
618+
rm -f 000[01]-*.patch &&
619+
git format-patch --no-clobber --no-renames --cover-letter -1 >filelist &&
620+
# empty the files output by the command ...
621+
for f in $(cat filelist)
622+
do
623+
: >"$f" || return 1
624+
done &&
625+
test_must_fail git format-patch --no-clobber --cover-letter --no-renames -1 &&
626+
# ... and make sure they stay empty
627+
for f in $(cat filelist)
628+
do
629+
! test -s "$f" || return 1
630+
done
631+
'
632+
617633
test_expect_success 'cover-letter inherits diff options' '
618634
git mv file foo &&
619635
git commit -m foo &&

0 commit comments

Comments
 (0)