format-patch: simplify after-subject MIME header handling

In log_write_email_headers(), we append our MIME headers to the set of extra headers by creating a new strbuf, adding the existing headers, and then adding our new ones. We had to do it this way when our output buffer might point to the constant opt->extra_headers variable. But since the previous commit, we always make a local copy of that variable. Let's turn that into a strbuf, which lets the MIME code simply append to it. That simplifies the function and avoids a pointless extra copy of the headers. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 19, 2024 at 20:35 UTC 838ba014ce8226e0fb6c87b1b1c587fc61582323
1 file changed +6 -8
log-tree.c
+6 -8
@@ -474,12 +474,15 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
474 int *need_8bit_cte_p,
475 int maybe_multipart)
476 {
477 - char *extra_headers = xstrdup_or_null(opt->extra_headers);
477 + struct strbuf headers = STRBUF_INIT;
478 const char *name = oid_to_hex(opt->zero_commit ?
479 null_oid() : &commit->object.oid);
480
481 *need_8bit_cte_p = 0; /* unknown */
482
483 + if (opt->extra_headers)
484 + strbuf_addstr(&headers, opt->extra_headers);
485 +
486 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
487 graph_show_oneline(opt->graph);
488 if (opt->message_id) {
@@ -496,15 +499,13 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
499 graph_show_oneline(opt->graph);
500 }
501 if (opt->mime_boundary && maybe_multipart) {
499 - struct strbuf subject_buffer = STRBUF_INIT;
502 static struct strbuf buffer = STRBUF_INIT;
503 struct strbuf filename = STRBUF_INIT;
504 *need_8bit_cte_p = -1; /* NEVER */
505
506 strbuf_reset(&buffer);
507
506 - strbuf_addf(&subject_buffer,
507 - "%s"
508 + strbuf_addf(&headers,
509 "MIME-Version: 1.0\n"
510 "Content-Type: multipart/mixed;"
511 " boundary=\"%s%s\"\n"
@@ -515,11 +516,8 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
516 "Content-Type: text/plain; "
517 "charset=UTF-8; format=fixed\n"
518 "Content-Transfer-Encoding: 8bit\n\n",
518 - extra_headers ? extra_headers : "",
519 mime_boundary_leader, opt->mime_boundary,
520 mime_boundary_leader, opt->mime_boundary);
521 - free(extra_headers);
522 - extra_headers = strbuf_detach(&subject_buffer, NULL);
521
522 if (opt->numbered_files)
523 strbuf_addf(&filename, "%d", opt->nr);
@@ -539,7 +537,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
537 opt->diffopt.stat_sep = buffer.buf;
538 strbuf_release(&filename);
539 }
542 - *extra_headers_p = extra_headers;
540 + *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
541 }
542
543 static void show_sig_lines(struct rev_info *opt, int status, const char *bol)