Skip to content

Commit 6bf1394

Browse files
peffgitster
authored andcommitted
clean up calling conventions for pretty.c functions
We have a pretty_print_context representing the parameters for a pretty-print session, but we did not use it uniformly. As a result, functions kept growing more and more arguments. Let's clean this up in a few ways: 1. All pretty-print pp_* functions now take a context. This lets us reduce the number of arguments to these functions, since we were just passing around the context values separately. 2. The context argument now has a cmit_fmt field, which was passed around separately. That's one less argument per function. 3. The context argument always comes first, which makes calling a little more uniform. This drops lines from some callers, and adds lines in a few places (because we need an extra line to set the context's fmt field). Overall, we don't save many lines, but the lines that are there are a lot simpler and more readable. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8b8a537 commit 6bf1394

File tree

7 files changed

+75
-76
lines changed

7 files changed

+75
-76
lines changed

builtin/log.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
327327
static void show_tagger(char *buf, int len, struct rev_info *rev)
328328
{
329329
struct strbuf out = STRBUF_INIT;
330+
struct pretty_print_context pp = {0};
330331

331-
pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
332-
get_log_output_encoding());
332+
pp.fmt = rev->commit_format;
333+
pp.date_mode = rev->date_mode;
334+
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
333335
printf("%s", out.buf);
334336
strbuf_release(&out);
335337
}
@@ -715,17 +717,16 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
715717
int nr, struct commit **list, struct commit *head)
716718
{
717719
const char *committer;
718-
const char *subject_start = NULL;
719720
const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
720721
const char *msg;
721-
const char *extra_headers = rev->extra_headers;
722722
struct shortlog log;
723723
struct strbuf sb = STRBUF_INIT;
724724
int i;
725725
const char *encoding = "UTF-8";
726726
struct diff_options opts;
727727
int need_8bit_cte = 0;
728728
struct commit *commit = NULL;
729+
struct pretty_print_context pp = {0};
729730

730731
if (rev->commit_format != CMIT_FMT_EMAIL)
731732
die("Cover letter needs email format");
@@ -757,19 +758,19 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
757758
free(commit);
758759
}
759760

760-
log_write_email_headers(rev, head, &subject_start, &extra_headers,
761+
log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
761762
&need_8bit_cte);
762763

763764
for (i = 0; !need_8bit_cte && i < nr; i++)
764765
if (has_non_ascii(list[i]->buffer))
765766
need_8bit_cte = 1;
766767

767768
msg = body;
768-
pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
769-
encoding);
770-
pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
771-
encoding, need_8bit_cte);
772-
pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
769+
pp.fmt = CMIT_FMT_EMAIL;
770+
pp.date_mode = DATE_RFC2822;
771+
pp_user_info(&pp, NULL, &sb, committer, encoding);
772+
pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
773+
pp_remainder(&pp, &msg, &sb, 0);
773774
printf("%s\n", sb.buf);
774775

775776
strbuf_release(&sb);

builtin/merge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,14 @@ static void squash_message(void)
333333

334334
ctx.abbrev = rev.abbrev;
335335
ctx.date_mode = rev.date_mode;
336+
ctx.fmt = rev.commit_format;
336337

337338
strbuf_addstr(&out, "Squashed commit of the following:\n");
338339
while ((commit = get_revision(&rev)) != NULL) {
339340
strbuf_addch(&out, '\n');
340341
strbuf_addf(&out, "commit %s\n",
341342
sha1_to_hex(commit->object.sha1));
342-
pretty_print_commit(rev.commit_format, commit, &out, &ctx);
343+
pretty_print_commit(&ctx, commit, &out);
343344
}
344345
if (write(fd, out.buf, out.len) < 0)
345346
die_errno("Writing SQUASH_MSG");

builtin/rev-list.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ static void show_commit(struct commit *commit, void *data)
108108
struct pretty_print_context ctx = {0};
109109
ctx.abbrev = revs->abbrev;
110110
ctx.date_mode = revs->date_mode;
111-
pretty_print_commit(revs->commit_format, commit, &buf, &ctx);
111+
ctx.fmt = revs->commit_format;
112+
pretty_print_commit(&ctx, commit, &buf);
112113
if (revs->graph) {
113114
if (buf.len) {
114115
if (revs->commit_format != CMIT_FMT_ONELINE)

builtin/shortlog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
161161
sha1_to_hex(commit->object.sha1));
162162
if (log->user_format) {
163163
struct pretty_print_context ctx = {0};
164+
ctx.fmt = CMIT_FMT_USERFORMAT;
164165
ctx.abbrev = log->abbrev;
165166
ctx.subject = "";
166167
ctx.after_subject = "";
167168
ctx.date_mode = DATE_NORMAL;
168-
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx);
169+
pretty_print_commit(&ctx, commit, &ufbuf);
169170
buffer = ufbuf.buf;
170171
} else if (*buffer) {
171172
buffer++;

commit.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ enum cmit_fmt {
7070

7171
struct pretty_print_context
7272
{
73+
enum cmit_fmt fmt;
7374
int abbrev;
7475
const char *subject;
7576
const char *after_subject;
@@ -95,22 +96,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want
9596
extern void format_commit_message(const struct commit *commit,
9697
const char *format, struct strbuf *sb,
9798
const struct pretty_print_context *context);
98-
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
99-
struct strbuf *sb,
100-
const struct pretty_print_context *context);
99+
extern void pretty_print_commit(const struct pretty_print_context *pp,
100+
const struct commit *commit,
101+
struct strbuf *sb);
101102
extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
102103
struct strbuf *sb);
103-
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
104-
const char *line, enum date_mode dmode,
105-
const char *encoding);
106-
void pp_title_line(enum cmit_fmt fmt,
104+
void pp_user_info(const struct pretty_print_context *pp,
105+
const char *what, struct strbuf *sb,
106+
const char *line, const char *encoding);
107+
void pp_title_line(const struct pretty_print_context *pp,
107108
const char **msg_p,
108109
struct strbuf *sb,
109-
const char *subject,
110-
const char *after_subject,
111110
const char *encoding,
112111
int need_8bit_cte);
113-
void pp_remainder(enum cmit_fmt fmt,
112+
void pp_remainder(const struct pretty_print_context *pp,
114113
const char **msg_p,
115114
struct strbuf *sb,
116115
int indent);

log-tree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ void show_log(struct rev_info *opt)
505505
ctx.abbrev = opt->diffopt.abbrev;
506506
ctx.after_subject = extra_headers;
507507
ctx.reflog_info = opt->reflog_info;
508-
pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);
508+
ctx.fmt = opt->commit_format;
509+
pretty_print_commit(&ctx, commit, &msgbuf);
509510

510511
if (opt->add_signoff)
511512
append_signoff(&msgbuf, opt->add_signoff);

0 commit comments

Comments
 (0)