Skip to content

Commit a031815

Browse files
committed
Merge branch 'jk/pretty-subject-cleanup'
Code clean-up in the "git log" machinery that implements custom log message formatting. * jk/pretty-subject-cleanup: format-patch: fix leak of empty header string format-patch: simplify after-subject MIME header handling format-patch: return an allocated string from log_write_email_headers() log: do not set up extra_headers for non-email formats pretty: drop print_email_subject flag pretty: split oneline and email subject printing shortlog: stop setting pp.print_email_subject
2 parents ccdc7d9 + 1c10b8e commit a031815

File tree

7 files changed

+38
-46
lines changed

7 files changed

+38
-46
lines changed

builtin/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ static void prepare_cover_text(struct pretty_print_context *pp,
12971297
subject = subject_sb.buf;
12981298

12991299
do_pp:
1300-
pp_title_line(pp, &subject, sb, encoding, need_8bit_cte);
1300+
pp_email_subject(pp, &subject, sb, encoding, need_8bit_cte);
13011301
pp_remainder(pp, &body, sb, 0);
13021302

13031303
strbuf_release(&description_sb);
@@ -1364,13 +1364,13 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
13641364
pp.fmt = CMIT_FMT_EMAIL;
13651365
pp.date_mode.type = DATE_RFC2822;
13661366
pp.rev = rev;
1367-
pp.print_email_subject = 1;
13681367
pp.encode_email_headers = rev->encode_email_headers;
13691368
pp_user_info(&pp, NULL, &sb, committer, encoding);
13701369
prepare_cover_text(&pp, description_file, branch_name, &sb,
13711370
encoding, need_8bit_cte);
13721371
fprintf(rev->diffopt.file, "%s\n", sb.buf);
13731372

1373+
free(pp.after_subject);
13741374
strbuf_release(&sb);
13751375

13761376
shortlog_init(&log);

builtin/rev-list.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ static void show_commit(struct commit *commit, void *data)
219219
ctx.fmt = revs->commit_format;
220220
ctx.output_encoding = get_log_output_encoding();
221221
ctx.color = revs->diffopt.use_color;
222+
ctx.rev = revs;
222223
pretty_print_commit(&ctx, commit, &buf);
223224
if (buf.len) {
224225
if (revs->commit_format != CMIT_FMT_ONELINE)

builtin/shortlog.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
245245

246246
ctx.fmt = CMIT_FMT_USERFORMAT;
247247
ctx.abbrev = log->abbrev;
248-
ctx.print_email_subject = 1;
249248
ctx.date_mode = log->date_mode;
250249
ctx.output_encoding = get_log_output_encoding();
251250

log-tree.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,19 @@ 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+
struct strbuf headers = STRBUF_INIT;
478478
const char *name = oid_to_hex(opt->zero_commit ?
479479
null_oid() : &commit->object.oid);
480480

481481
*need_8bit_cte_p = 0; /* unknown */
482482

483+
if (opt->extra_headers && *opt->extra_headers)
484+
strbuf_addstr(&headers, opt->extra_headers);
485+
483486
fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
484487
graph_show_oneline(opt->graph);
485488
if (opt->message_id) {
@@ -496,16 +499,13 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
496499
graph_show_oneline(opt->graph);
497500
}
498501
if (opt->mime_boundary && maybe_multipart) {
499-
static struct strbuf subject_buffer = STRBUF_INIT;
500502
static struct strbuf buffer = STRBUF_INIT;
501503
struct strbuf filename = STRBUF_INIT;
502504
*need_8bit_cte_p = -1; /* NEVER */
503505

504-
strbuf_reset(&subject_buffer);
505506
strbuf_reset(&buffer);
506507

507-
strbuf_addf(&subject_buffer,
508-
"%s"
508+
strbuf_addf(&headers,
509509
"MIME-Version: 1.0\n"
510510
"Content-Type: multipart/mixed;"
511511
" boundary=\"%s%s\"\n"
@@ -516,10 +516,8 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
516516
"Content-Type: text/plain; "
517517
"charset=UTF-8; format=fixed\n"
518518
"Content-Transfer-Encoding: 8bit\n\n",
519-
extra_headers ? extra_headers : "",
520519
mime_boundary_leader, opt->mime_boundary,
521520
mime_boundary_leader, opt->mime_boundary);
522-
extra_headers = subject_buffer.buf;
523521

524522
if (opt->numbered_files)
525523
strbuf_addf(&filename, "%d", opt->nr);
@@ -539,7 +537,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
539537
opt->diffopt.stat_sep = buffer.buf;
540538
strbuf_release(&filename);
541539
}
542-
*extra_headers_p = extra_headers;
540+
*extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
543541
}
544542

545543
static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
@@ -678,7 +676,6 @@ void show_log(struct rev_info *opt)
678676
struct log_info *log = opt->loginfo;
679677
struct commit *commit = log->commit, *parent = log->parent;
680678
int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
681-
const char *extra_headers = opt->extra_headers;
682679
struct pretty_print_context ctx = {0};
683680

684681
opt->loginfo = NULL;
@@ -739,10 +736,9 @@ void show_log(struct rev_info *opt)
739736
*/
740737

741738
if (cmit_fmt_is_mail(opt->commit_format)) {
742-
log_write_email_headers(opt, commit, &extra_headers,
739+
log_write_email_headers(opt, commit, &ctx.after_subject,
743740
&ctx.need_8bit_cte, 1);
744741
ctx.rev = opt;
745-
ctx.print_email_subject = 1;
746742
} else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
747743
fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
748744
if (opt->commit_format != CMIT_FMT_ONELINE)
@@ -808,7 +804,6 @@ void show_log(struct rev_info *opt)
808804
ctx.date_mode = opt->date_mode;
809805
ctx.date_mode_explicit = opt->date_mode_explicit;
810806
ctx.abbrev = opt->diffopt.abbrev;
811-
ctx.after_subject = extra_headers;
812807
ctx.preserve_subject = opt->preserve_subject;
813808
ctx.encode_email_headers = opt->encode_email_headers;
814809
ctx.reflog_info = opt->reflog_info;
@@ -857,6 +852,7 @@ void show_log(struct rev_info *opt)
857852

858853
strbuf_release(&msgbuf);
859854
free(ctx.notes_message);
855+
free(ctx.after_subject);
860856

861857
if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
862858
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.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,11 +2077,11 @@ static void pp_header(struct pretty_print_context *pp,
20772077
}
20782078
}
20792079

2080-
void pp_title_line(struct pretty_print_context *pp,
2081-
const char **msg_p,
2082-
struct strbuf *sb,
2083-
const char *encoding,
2084-
int need_8bit_cte)
2080+
void pp_email_subject(struct pretty_print_context *pp,
2081+
const char **msg_p,
2082+
struct strbuf *sb,
2083+
const char *encoding,
2084+
int need_8bit_cte)
20852085
{
20862086
static const int max_length = 78; /* per rfc2047 */
20872087
struct strbuf title;
@@ -2091,19 +2091,14 @@ void pp_title_line(struct pretty_print_context *pp,
20912091
pp->preserve_subject ? "\n" : " ");
20922092

20932093
strbuf_grow(sb, title.len + 1024);
2094-
if (pp->print_email_subject) {
2095-
if (pp->rev)
2096-
fmt_output_email_subject(sb, pp->rev);
2097-
if (pp->encode_email_headers &&
2098-
needs_rfc2047_encoding(title.buf, title.len))
2099-
add_rfc2047(sb, title.buf, title.len,
2100-
encoding, RFC2047_SUBJECT);
2101-
else
2102-
strbuf_add_wrapped_bytes(sb, title.buf, title.len,
2094+
fmt_output_email_subject(sb, pp->rev);
2095+
if (pp->encode_email_headers &&
2096+
needs_rfc2047_encoding(title.buf, title.len))
2097+
add_rfc2047(sb, title.buf, title.len,
2098+
encoding, RFC2047_SUBJECT);
2099+
else
2100+
strbuf_add_wrapped_bytes(sb, title.buf, title.len,
21032101
-last_line_length(sb), 1, max_length);
2104-
} else {
2105-
strbuf_addbuf(sb, &title);
2106-
}
21072102
strbuf_addch(sb, '\n');
21082103

21092104
if (need_8bit_cte == 0) {
@@ -2126,9 +2121,8 @@ void pp_title_line(struct pretty_print_context *pp,
21262121
if (pp->after_subject) {
21272122
strbuf_addstr(sb, pp->after_subject);
21282123
}
2129-
if (cmit_fmt_is_mail(pp->fmt)) {
2130-
strbuf_addch(sb, '\n');
2131-
}
2124+
2125+
strbuf_addch(sb, '\n');
21322126

21332127
if (pp->in_body_headers.nr) {
21342128
int i;
@@ -2320,16 +2314,19 @@ void pretty_print_commit(struct pretty_print_context *pp,
23202314
}
23212315

23222316
pp_header(pp, encoding, commit, &msg, sb);
2323-
if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
2317+
if (pp->fmt != CMIT_FMT_ONELINE && !cmit_fmt_is_mail(pp->fmt)) {
23242318
strbuf_addch(sb, '\n');
23252319
}
23262320

23272321
/* Skip excess blank lines at the beginning of body, if any... */
23282322
msg = skip_blank_lines(msg);
23292323

23302324
/* These formats treat the title line specially. */
2331-
if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
2332-
pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
2325+
if (pp->fmt == CMIT_FMT_ONELINE) {
2326+
msg = format_subject(sb, msg, " ");
2327+
strbuf_addch(sb, '\n');
2328+
} else if (cmit_fmt_is_mail(pp->fmt))
2329+
pp_email_subject(pp, &msg, sb, encoding, need_8bit_cte);
23332330

23342331
beginning_of_body = sb->len;
23352332
if (pp->fmt != CMIT_FMT_ONELINE)

pretty.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ 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;
42-
int print_email_subject;
4342
int expand_tabs_in_log;
4443
int need_8bit_cte;
4544
char *notes_message;
@@ -96,13 +95,13 @@ void pp_user_info(struct pretty_print_context *pp, const char *what,
9695
const char *encoding);
9796

9897
/*
99-
* Format title line of commit message taken from "msg_p" and
98+
* Format subject line of commit message taken from "msg_p" and
10099
* put it into "sb".
101100
* First line of "msg_p" is also affected.
102101
*/
103-
void pp_title_line(struct pretty_print_context *pp, const char **msg_p,
104-
struct strbuf *sb, const char *encoding,
105-
int need_8bit_cte);
102+
void pp_email_subject(struct pretty_print_context *pp, const char **msg_p,
103+
struct strbuf *sb, const char *encoding,
104+
int need_8bit_cte);
106105

107106
/*
108107
* Get current state of commit message from "msg_p" and continue formatting

0 commit comments

Comments
 (0)