Skip to content

Commit 305a681

Browse files
peffgitster
authored andcommitted
format-patch: return an allocated string from log_write_email_headers()
When pretty-printing a commit in the email format, we have to fill in the "after subject" field of the pretty_print_context with any extra headers the user provided (e.g., from "--to" or "--cc" options) plus any special MIME headers. We return an out-pointer that sometimes points to a newly heap-allocated string and sometimes not. To avoid leaking, we store the allocated version in a buffer with static lifetime, which is ugly. Worse, as we extend the header feature, we'll end up having to repeat this ugly pattern. Instead, let's have our out-pointer pass ownership back to the caller, and duplicate the string when necessary. This does mean one extra allocation per commit when you use extra headers, but in the context of format-patch which is showing diffs, I don't think that's even measurable. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 82363d9 commit 305a681

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

builtin/log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
13701370
encoding, need_8bit_cte);
13711371
fprintf(rev->diffopt.file, "%s\n", sb.buf);
13721372

1373+
free(pp.after_subject);
13731374
strbuf_release(&sb);
13741375

13751376
shortlog_init(&log);

log-tree.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,11 @@ void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
470470
}
471471

472472
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
473-
const char **extra_headers_p,
473+
char **extra_headers_p,
474474
int *need_8bit_cte_p,
475475
int maybe_multipart)
476476
{
477-
const char *extra_headers = opt->extra_headers;
477+
char *extra_headers = xstrdup_or_null(opt->extra_headers);
478478
const char *name = oid_to_hex(opt->zero_commit ?
479479
null_oid() : &commit->object.oid);
480480

@@ -496,12 +496,11 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
496496
graph_show_oneline(opt->graph);
497497
}
498498
if (opt->mime_boundary && maybe_multipart) {
499-
static struct strbuf subject_buffer = STRBUF_INIT;
499+
struct strbuf subject_buffer = STRBUF_INIT;
500500
static struct strbuf buffer = STRBUF_INIT;
501501
struct strbuf filename = STRBUF_INIT;
502502
*need_8bit_cte_p = -1; /* NEVER */
503503

504-
strbuf_reset(&subject_buffer);
505504
strbuf_reset(&buffer);
506505

507506
strbuf_addf(&subject_buffer,
@@ -519,7 +518,8 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
519518
extra_headers ? extra_headers : "",
520519
mime_boundary_leader, opt->mime_boundary,
521520
mime_boundary_leader, opt->mime_boundary);
522-
extra_headers = subject_buffer.buf;
521+
free(extra_headers);
522+
extra_headers = strbuf_detach(&subject_buffer, NULL);
523523

524524
if (opt->numbered_files)
525525
strbuf_addf(&filename, "%d", opt->nr);
@@ -854,6 +854,7 @@ void show_log(struct rev_info *opt)
854854

855855
strbuf_release(&msgbuf);
856856
free(ctx.notes_message);
857+
free(ctx.after_subject);
857858

858859
if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
859860
struct diff_queue_struct dq;

log-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void format_decorations(struct strbuf *sb, const struct commit *commit,
2929
int use_color, const struct decoration_options *opts);
3030
void show_decorations(struct rev_info *opt, struct commit *commit);
3131
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
32-
const char **extra_headers_p,
32+
char **extra_headers_p,
3333
int *need_8bit_cte_p,
3434
int maybe_multipart);
3535
void load_ref_decorations(struct decoration_filter *filter, int flags);

pretty.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct pretty_print_context {
3535
*/
3636
enum cmit_fmt fmt;
3737
int abbrev;
38-
const char *after_subject;
38+
char *after_subject;
3939
int preserve_subject;
4040
struct date_mode date_mode;
4141
unsigned date_mode_explicit:1;

0 commit comments

Comments
 (0)