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>
This commit is contained in:
Junio C Hamano
2012-12-21 22:06:01 -08:00
parent 38ec23ac89
commit d28b5d47ab
3 changed files with 27 additions and 21 deletions

View File

@ -686,8 +686,10 @@ static int reopen_stdout(struct commit *commit, const char *subject,
if (rev->numbered_files) if (rev->numbered_files)
strbuf_addf(&filename, "%d", rev->nr); strbuf_addf(&filename, "%d", rev->nr);
else if (commit)
fmt_output_commit(&filename, commit, rev);
else else
get_patch_filename(&filename, commit, subject, rev); fmt_output_subject(&filename, subject, rev);
if (!quiet) if (!quiet)
fprintf(realstdout, "%s\n", filename.buf + outdir_offset); fprintf(realstdout, "%s\n", filename.buf + outdir_offset);

View File

@ -299,26 +299,32 @@ static unsigned int digits_in_number(unsigned int number)
return result; return result;
} }
void get_patch_filename(struct strbuf *buf, void fmt_output_subject(struct strbuf *filename,
struct commit *commit, const char *subject, const char *subject,
struct rev_info *info) struct rev_info *info)
{ {
const char *suffix = info->patch_suffix; const char *suffix = info->patch_suffix;
int nr = info->nr; int nr = info->nr;
int suffix_len = strlen(suffix) + 1; int start_len = filename->len;
int start_len = buf->len; int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
strbuf_addf(buf, "%04d-", nr); strbuf_addf(filename, "%04d-%s", nr, subject);
if (subject)
strbuf_addstr(buf, subject); if (max_len < filename->len)
else if (commit) { strbuf_setlen(filename, max_len);
struct pretty_print_context ctx = {0}; strbuf_addstr(filename, suffix);
format_commit_message(commit, "%f", buf, &ctx); }
}
if (max_len < buf->len) void fmt_output_commit(struct strbuf *filename,
strbuf_setlen(buf, max_len); struct commit *commit,
strbuf_addstr(buf, suffix); struct rev_info *info)
{
struct pretty_print_context ctx = {0};
struct strbuf subject = STRBUF_INIT;
format_commit_message(commit, "%f", &subject, &ctx);
fmt_output_subject(filename, subject.buf, info);
strbuf_release(&subject);
} }
void log_write_email_headers(struct rev_info *opt, struct commit *commit, 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,
if (opt->numbered_files) if (opt->numbered_files)
strbuf_addf(&filename, "%d", opt->nr); strbuf_addf(&filename, "%d", opt->nr);
else else
get_patch_filename(&filename, commit, NULL, fmt_output_commit(&filename, commit, opt);
opt);
snprintf(buffer, sizeof(buffer) - 1, snprintf(buffer, sizeof(buffer) - 1,
"\n--%s%s\n" "\n--%s%s\n"
"Content-Type: text/x-patch;" "Content-Type: text/x-patch;"

View File

@ -21,8 +21,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
void load_ref_decorations(int flags); void load_ref_decorations(int flags);
#define FORMAT_PATCH_NAME_MAX 64 #define FORMAT_PATCH_NAME_MAX 64
void get_patch_filename(struct strbuf *buf, void fmt_output_commit(struct strbuf *, struct commit *, struct rev_info *);
struct commit *commit, const char *subject, void fmt_output_subject(struct strbuf *, const char *subject, struct rev_info *);
struct rev_info *);
#endif #endif