@matheusbsbs / matheus-bagliano / commits / cd2ef591c8
Fork 0

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 <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed Mar 22, 2009 at 19:14 UTC cd2ef591c8e753fe5295ac3c6f1dee481f00a185
1 file changed +39 -28
builtin-log.c
+39 -28
@@ -539,30 +539,29 @@ static FILE *realstdout = NULL;
539 static const char *output_directory = NULL;
540 static int outdir_offset;
541
542 -static int reopen_stdout(const char *oneline, struct rev_info *rev)
542 +static int reopen_stdout(struct commit *commit, struct rev_info *rev)
543 {
544 - char filename[PATH_MAX];
545 - int len = 0;
544 + struct strbuf filename = STRBUF_INIT;
545 int suffix_len = strlen(fmt_patch_suffix) + 1;
546
547 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)
551 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, '/');
554 }
555
558 - strncpy(filename + len, oneline, PATH_MAX - len);
556 + get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
557
558 if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
561 - fprintf(realstdout, "%s\n", filename + outdir_offset);
559 + fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
560
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);
563
564 + strbuf_release(&filename);
565 return 0;
566 }
567
@@ -643,26 +642,42 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
642 const char *encoding = "utf-8";
643 struct diff_options opts;
644 int need_8bit_cte = 0;
646 - char filename[PATH_MAX];
645 + struct commit *commit = NULL;
646
647 if (rev->commit_format != CMIT_FMT_EMAIL)
648 die("Cover letter needs email format");
649
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);
652
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))
670 return;
671
659 - head_sha1 = sha1_to_hex(head->object.sha1);
672 + if (commit) {
673 +
674 + free(commit->buffer);
675 + free(commit);
676 + }
677
678 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
679 &need_8bit_cte);
680
664 - committer = git_committer_info(0);
665 -
681 msg = body;
682 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
683 encoding);
@@ -766,7 +781,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
781 struct patch_ids ids;
782 char *add_signoff = NULL;
783 struct strbuf buf = STRBUF_INIT;
769 - struct strbuf patch_filename = STRBUF_INIT;
784
785 git_config(git_format_config, NULL);
786 init_revisions(&rev, prefix);
@@ -1070,11 +1084,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1084 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1085 }
1086
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))
1089 die("Failed to create output files");
1077 - strbuf_setlen(&patch_filename, 0);
1090 shown = log_tree_commit(&rev, commit);
1091 free(commit->buffer);
1092 commit->buffer = NULL;
@@ -1098,7 +1110,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1110 if (!use_stdout)
1111 fclose(stdout);
1112 }
1101 - strbuf_release(&patch_filename);
1113 free(list);
1114 if (ignore_if_in_upstream)
1115 free_patch_ids(&ids);