get_patch_filename(): drop "just-numbers" hack

The function chooses from three operating modes (format using the subject, the commit, or just number) based on NULL-ness of two of its parameters, which is an ugly hack for sharing only a bit of code. Separate out the "just numbers" part out to the callers. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 21, 2012 at 21:39 UTC 38ec23ac893e96a9027c1cf8112b3d97a0384d39
2 files changed +18 -16
builtin/log.c
+4 -1
@@ -684,7 +684,10 @@ static int reopen_stdout(struct commit *commit, const char *subject,
684 strbuf_addch(&filename, '/');
685 }
686
687 - get_patch_filename(&filename, commit, subject, rev);
687 + if (rev->numbered_files)
688 + strbuf_addf(&filename, "%d", rev->nr);
689 + else
690 + get_patch_filename(&filename, commit, subject, rev);
691
692 if (!quiet)
693 fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
log-tree.c
+14 -15
@@ -307,21 +307,18 @@ void get_patch_filename(struct strbuf *buf,
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
311 - strbuf_addf(buf, commit || subject ? "%04d-" : "%d", nr);
312 - if (commit || subject) {
313 - int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
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};
315 -
316 - if (subject)
317 - strbuf_addstr(buf, subject);
318 - else if (commit)
319 - format_commit_message(commit, "%f", buf, &ctx);
320 -
321 - if (max_len < buf->len)
322 - strbuf_setlen(buf, max_len);
323 - strbuf_addstr(buf, suffix);
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);
322 }
323
324 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
@@ -390,9 +387,11 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
387 mime_boundary_leader, opt->mime_boundary);
388 extra_headers = subject_buffer;
389
393 - get_patch_filename(&filename,
394 - opt->numbered_files ? NULL : commit, NULL,
395 - opt);
390 + if (opt->numbered_files)
391 + strbuf_addf(&filename, "%d", opt->nr);
392 + else
393 + get_patch_filename(&filename, commit, NULL,
394 + opt);
395 snprintf(buffer, sizeof(buffer) - 1,
396 "\n--%s%s\n"
397 "Content-Type: text/x-patch;"