Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit d28b5d4

Browse files
committed
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 <[email protected]>
1 parent 38ec23a commit d28b5d4

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

builtin/log.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,10 @@ static int reopen_stdout(struct commit *commit, const char *subject,
686686

687687
if (rev->numbered_files)
688688
strbuf_addf(&filename, "%d", rev->nr);
689+
else if (commit)
690+
fmt_output_commit(&filename, commit, rev);
689691
else
690-
get_patch_filename(&filename, commit, subject, rev);
692+
fmt_output_subject(&filename, subject, rev);
691693

692694
if (!quiet)
693695
fprintf(realstdout, "%s\n", filename.buf + outdir_offset);

log-tree.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -299,26 +299,32 @@ static unsigned int digits_in_number(unsigned int number)
299299
return result;
300300
}
301301

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,
304304
struct rev_info *info)
305305
{
306306
const char *suffix = info->patch_suffix;
307307
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);
322328
}
323329

324330
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,
390396
if (opt->numbered_files)
391397
strbuf_addf(&filename, "%d", opt->nr);
392398
else
393-
get_patch_filename(&filename, commit, NULL,
394-
opt);
399+
fmt_output_commit(&filename, commit, opt);
395400
snprintf(buffer, sizeof(buffer) - 1,
396401
"\n--%s%s\n"
397402
"Content-Type: text/x-patch;"

log-tree.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
2121
void load_ref_decorations(int flags);
2222

2323
#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 *);
2726

2827
#endif

0 commit comments

Comments
 (0)