format-patch: allow additional generated content in make_cover_letter()

make_cover_letter() returns early when it lacks sufficient state to emit a diffstat, which makes it difficult to extend the function to reliably emit additional generated content. Work around this shortcoming by factoring diffstat-printing logic out to its own function and calling it as needed without otherwise inhibiting normal control flow. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 22, 2018 at 05:57 UTC fa5b7ea670f4c5ee377e7fce799300829eabb291
1 file changed +23 -20
builtin/log.c
+23 -20
@@ -997,6 +997,26 @@ static char *find_branch_name(struct rev_info *rev)
997 return branch;
998 }
999
1000 +static void show_diffstat(struct rev_info *rev,
1001 + struct commit *origin, struct commit *head)
1002 +{
1003 + struct diff_options opts;
1004 +
1005 + memcpy(&opts, &rev->diffopt, sizeof(opts));
1006 + opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1007 + opts.stat_width = MAIL_DEFAULT_WRAP;
1008 +
1009 + diff_setup_done(&opts);
1010 +
1011 + diff_tree_oid(get_commit_tree_oid(origin),
1012 + get_commit_tree_oid(head),
1013 + "", &opts);
1014 + diffcore_std(&opts);
1015 + diff_flush(&opts);
1016 +
1017 + fprintf(rev->diffopt.file, "\n");
1018 +}
1019 +
1020 static void make_cover_letter(struct rev_info *rev, int use_stdout,
1021 struct commit *origin,
1022 int nr, struct commit **list,
@@ -1010,7 +1030,6 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1030 struct strbuf sb = STRBUF_INIT;
1031 int i;
1032 const char *encoding = "UTF-8";
1013 - struct diff_options opts;
1033 int need_8bit_cte = 0;
1034 struct pretty_print_context pp = {0};
1035 struct commit *head = list[0];
@@ -1060,25 +1079,9 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1079
1080 shortlog_output(&log);
1081
1063 - /*
1064 - * We can only do diffstat with a unique reference point
1065 - */
1066 - if (!origin)
1067 - return;
1068 -
1069 - memcpy(&opts, &rev->diffopt, sizeof(opts));
1070 - opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1071 - opts.stat_width = MAIL_DEFAULT_WRAP;
1072 -
1073 - diff_setup_done(&opts);
1074 -
1075 - diff_tree_oid(get_commit_tree_oid(origin),
1076 - get_commit_tree_oid(head),
1077 - "", &opts);
1078 - diffcore_std(&opts);
1079 - diff_flush(&opts);
1080 -
1081 - fprintf(rev->diffopt.file, "\n");
1082 + /* We can only do diffstat with a unique reference point */
1083 + if (origin)
1084 + show_diffstat(rev, origin, head);
1085 }
1086
1087 static const char *clean_message_id(const char *msg_id)