get_patch_filename(): split into two functions

The function switched between two operating modes depending on the NULL-ness of its two parameters, as a hacky way to share small part of implementation, sacrificing cleanliness of the API. Implement "fmt_output_subject()" function that takes a subject string and gives the name for the output file, and on top of it, implement "fmt_output_commit()" function that takes a commit and gives the name for the output file. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 21, 2012 at 22:06 UTC d28b5d47ab72a91d5090748f8f8baaf6ffa084fc
3 files changed +28 -22
builtin/log.c
+3 -1
@@ -686,8 +686,10 @@ static int reopen_stdout(struct commit *commit, const char *subject,
686
687 if (rev->numbered_files)
688 strbuf_addf(&filename, "%d", rev->nr);
689 + else if (commit)
690 + fmt_output_commit(&filename, commit, rev);
691 else
690 - get_patch_filename(&filename, commit, subject, rev);
692 + fmt_output_subject(&filename, subject, rev);
693
694 if (!quiet)
695 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
log-tree.c
+23 -18
@@ -299,26 +299,32 @@ static unsigned int digits_in_number(unsigned int number)
299 return result;
300 }
301
302 -void get_patch_filename(struct strbuf *buf,
303 - struct commit *commit, const char *subject,
302 +void fmt_output_subject(struct strbuf *filename,
303 + const char *subject,
304 struct rev_info *info)
305 {
306 const char *suffix = info->patch_suffix;
307 int nr = info->nr;
308 - int suffix_len = strlen(suffix) + 1;
309 - int start_len = buf->len;
310 - int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
311 -
312 - strbuf_addf(buf, "%04d-", nr);
313 - if (subject)
314 - strbuf_addstr(buf, subject);
315 - else if (commit) {
316 - struct pretty_print_context ctx = {0};
317 - format_commit_message(commit, "%f", buf, &ctx);
318 - }
319 - if (max_len < buf->len)
320 - strbuf_setlen(buf, max_len);
321 - strbuf_addstr(buf, suffix);
308 + int start_len = filename->len;
309 + int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
310 +
311 + strbuf_addf(filename, "%04d-%s", nr, subject);
312 +
313 + if (max_len < filename->len)
314 + strbuf_setlen(filename, max_len);
315 + strbuf_addstr(filename, suffix);
316 +}
317 +
318 +void fmt_output_commit(struct strbuf *filename,
319 + struct commit *commit,
320 + struct rev_info *info)
321 +{
322 + struct pretty_print_context ctx = {0};
323 + struct strbuf subject = STRBUF_INIT;
324 +
325 + format_commit_message(commit, "%f", &subject, &ctx);
326 + fmt_output_subject(filename, subject.buf, info);
327 + strbuf_release(&subject);
328 }
329
330 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
@@ -390,8 +396,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
396 if (opt->numbered_files)
397 strbuf_addf(&filename, "%d", opt->nr);
398 else
393 - get_patch_filename(&filename, commit, NULL,
394 - opt);
399 + fmt_output_commit(&filename, commit, opt);
400 snprintf(buffer, sizeof(buffer) - 1,
401 "\n--%s%s\n"
402 "Content-Type: text/x-patch;"
log-tree.h
+2 -3
@@ -21,8 +21,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
21 void load_ref_decorations(int flags);
22
23 #define FORMAT_PATCH_NAME_MAX 64
24 -void get_patch_filename(struct strbuf *buf,
25 - struct commit *commit, const char *subject,
26 - struct rev_info *);
24 +void fmt_output_commit(struct strbuf *, struct commit *, struct rev_info *);
25 +void fmt_output_subject(struct strbuf *, const char *subject, struct rev_info *);
26
27 #endif