Skip to content

Commit cd2ef59

Browse files
bebarinogitster
authored andcommitted
format-patch: pass a commit to reopen_stdout()
We use the commit to generate the patch filename in reopen_stdout() before we redirect stdout. The cover letter codepath creates a dummy commit with the desired subject line 'cover letter'. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6df514a commit cd2ef59

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

builtin-log.c

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -539,30 +539,29 @@ static FILE *realstdout = NULL;
539539
static const char *output_directory = NULL;
540540
static int outdir_offset;
541541

542-
static int reopen_stdout(const char *oneline, struct rev_info *rev)
542+
static int reopen_stdout(struct commit *commit, struct rev_info *rev)
543543
{
544-
char filename[PATH_MAX];
545-
int len = 0;
544+
struct strbuf filename = STRBUF_INIT;
546545
int suffix_len = strlen(fmt_patch_suffix) + 1;
547546

548547
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)
553551
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, '/');
556554
}
557555

558-
strncpy(filename + len, oneline, PATH_MAX - len);
556+
get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
559557

560558
if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
561-
fprintf(realstdout, "%s\n", filename + outdir_offset);
559+
fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
562560

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);
565563

564+
strbuf_release(&filename);
566565
return 0;
567566
}
568567

@@ -643,26 +642,42 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
643642
const char *encoding = "utf-8";
644643
struct diff_options opts;
645644
int need_8bit_cte = 0;
646-
char filename[PATH_MAX];
645+
struct commit *commit = NULL;
647646

648647
if (rev->commit_format != CMIT_FMT_EMAIL)
649648
die("Cover letter needs email format");
650649

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);
655652

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))
657670
return;
658671

659-
head_sha1 = sha1_to_hex(head->object.sha1);
672+
if (commit) {
673+
674+
free(commit->buffer);
675+
free(commit);
676+
}
660677

661678
log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
662679
&need_8bit_cte);
663680

664-
committer = git_committer_info(0);
665-
666681
msg = body;
667682
pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
668683
encoding);
@@ -766,7 +781,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
766781
struct patch_ids ids;
767782
char *add_signoff = NULL;
768783
struct strbuf buf = STRBUF_INIT;
769-
struct strbuf patch_filename = STRBUF_INIT;
770784

771785
git_config(git_format_config, NULL);
772786
init_revisions(&rev, prefix);
@@ -1070,11 +1084,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10701084
gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
10711085
}
10721086

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))
10761089
die("Failed to create output files");
1077-
strbuf_setlen(&patch_filename, 0);
10781090
shown = log_tree_commit(&rev, commit);
10791091
free(commit->buffer);
10801092
commit->buffer = NULL;
@@ -1098,7 +1110,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10981110
if (!use_stdout)
10991111
fclose(stdout);
11001112
}
1101-
strbuf_release(&patch_filename);
11021113
free(list);
11031114
if (ignore_if_in_upstream)
11041115
free_patch_ids(&ids);

0 commit comments

Comments
 (0)