log-tree: factor out fmt_output_email_subject()

Use a strbuf to store the subject prefix string and move its construction into its own function. This gets rid of two arbitrary length limits and allows the string to be added by callers directly. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 1, 2017 at 12:36 UTC 8ffc8dc6bab4cf3d2364dd54b2de6c3afdb48610
2 files changed +21 -20
log-tree.c
+20 -20
@@ -332,35 +332,33 @@ void fmt_output_commit(struct strbuf *filename,
332 strbuf_release(&subject);
333 }
334
335 +void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
336 +{
337 + if (opt->total > 0) {
338 + strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
339 + opt->subject_prefix,
340 + *opt->subject_prefix ? " " : "",
341 + digits_in_number(opt->total),
342 + opt->nr, opt->total);
343 + } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
344 + strbuf_addf(sb, "Subject: [%s] ",
345 + opt->subject_prefix);
346 + } else {
347 + strbuf_addstr(sb, "Subject: ");
348 + }
349 +}
350 +
351 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
352 const char **subject_p,
353 const char **extra_headers_p,
354 int *need_8bit_cte_p)
355 {
340 - const char *subject = NULL;
356 + static struct strbuf subject = STRBUF_INIT;
357 const char *extra_headers = opt->extra_headers;
358 const char *name = oid_to_hex(opt->zero_commit ?
359 &null_oid : &commit->object.oid);
360
361 *need_8bit_cte_p = 0; /* unknown */
346 - if (opt->total > 0) {
347 - static char buffer[64];
348 - snprintf(buffer, sizeof(buffer),
349 - "Subject: [%s%s%0*d/%d] ",
350 - opt->subject_prefix,
351 - *opt->subject_prefix ? " " : "",
352 - digits_in_number(opt->total),
353 - opt->nr, opt->total);
354 - subject = buffer;
355 - } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
356 - static char buffer[256];
357 - snprintf(buffer, sizeof(buffer),
358 - "Subject: [%s] ",
359 - opt->subject_prefix);
360 - subject = buffer;
361 - } else {
362 - subject = "Subject: ";
363 - }
362
363 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
364 graph_show_oneline(opt->graph);
@@ -417,7 +415,9 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
415 opt->diffopt.stat_sep = buffer;
416 strbuf_release(&filename);
417 }
420 - *subject_p = subject;
418 + strbuf_reset(&subject);
419 + fmt_output_email_subject(&subject, opt);
420 + *subject_p = subject.buf;
421 *extra_headers_p = extra_headers;
422 }
423
log-tree.h
+1
@@ -30,5 +30,6 @@ void load_ref_decorations(int flags);
30 #define FORMAT_PATCH_NAME_MAX 64
31 void fmt_output_commit(struct strbuf *, struct commit *, struct rev_info *);
32 void fmt_output_subject(struct strbuf *, const char *subject, struct rev_info *);
33 +void fmt_output_email_subject(struct strbuf *, struct rev_info *);
34
35 #endif