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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2011-05-26 18:27:49 -04:00 committed by Junio C Hamano
parent 8b8a53744f
commit 6bf139440c
7 changed files with 75 additions and 76 deletions

View File

@ -327,9 +327,11 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
static void show_tagger(char *buf, int len, struct rev_info *rev) static void show_tagger(char *buf, int len, struct rev_info *rev)
{ {
struct strbuf out = STRBUF_INIT; struct strbuf out = STRBUF_INIT;
struct pretty_print_context pp = {0};
pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode, pp.fmt = rev->commit_format;
get_log_output_encoding()); pp.date_mode = rev->date_mode;
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
printf("%s", out.buf); printf("%s", out.buf);
strbuf_release(&out); strbuf_release(&out);
} }
@ -715,10 +717,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
int nr, struct commit **list, struct commit *head) int nr, struct commit **list, struct commit *head)
{ {
const char *committer; const char *committer;
const char *subject_start = NULL;
const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
const char *msg; const char *msg;
const char *extra_headers = rev->extra_headers;
struct shortlog log; struct shortlog log;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
int i; int i;
@ -726,6 +726,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
struct diff_options opts; struct diff_options opts;
int need_8bit_cte = 0; int need_8bit_cte = 0;
struct commit *commit = NULL; struct commit *commit = NULL;
struct pretty_print_context pp = {0};
if (rev->commit_format != CMIT_FMT_EMAIL) if (rev->commit_format != CMIT_FMT_EMAIL)
die("Cover letter needs email format"); die("Cover letter needs email format");
@ -757,7 +758,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
free(commit); free(commit);
} }
log_write_email_headers(rev, head, &subject_start, &extra_headers, log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
&need_8bit_cte); &need_8bit_cte);
for (i = 0; !need_8bit_cte && i < nr; i++) for (i = 0; !need_8bit_cte && i < nr; i++)
@ -765,11 +766,11 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
need_8bit_cte = 1; need_8bit_cte = 1;
msg = body; msg = body;
pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822, pp.fmt = CMIT_FMT_EMAIL;
encoding); pp.date_mode = DATE_RFC2822;
pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers, pp_user_info(&pp, NULL, &sb, committer, encoding);
encoding, need_8bit_cte); pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0); pp_remainder(&pp, &msg, &sb, 0);
printf("%s\n", sb.buf); printf("%s\n", sb.buf);
strbuf_release(&sb); strbuf_release(&sb);

View File

@ -333,13 +333,14 @@ static void squash_message(void)
ctx.abbrev = rev.abbrev; ctx.abbrev = rev.abbrev;
ctx.date_mode = rev.date_mode; ctx.date_mode = rev.date_mode;
ctx.fmt = rev.commit_format;
strbuf_addstr(&out, "Squashed commit of the following:\n"); strbuf_addstr(&out, "Squashed commit of the following:\n");
while ((commit = get_revision(&rev)) != NULL) { while ((commit = get_revision(&rev)) != NULL) {
strbuf_addch(&out, '\n'); strbuf_addch(&out, '\n');
strbuf_addf(&out, "commit %s\n", strbuf_addf(&out, "commit %s\n",
sha1_to_hex(commit->object.sha1)); sha1_to_hex(commit->object.sha1));
pretty_print_commit(rev.commit_format, commit, &out, &ctx); pretty_print_commit(&ctx, commit, &out);
} }
if (write(fd, out.buf, out.len) < 0) if (write(fd, out.buf, out.len) < 0)
die_errno("Writing SQUASH_MSG"); die_errno("Writing SQUASH_MSG");

View File

@ -108,7 +108,8 @@ static void show_commit(struct commit *commit, void *data)
struct pretty_print_context ctx = {0}; struct pretty_print_context ctx = {0};
ctx.abbrev = revs->abbrev; ctx.abbrev = revs->abbrev;
ctx.date_mode = revs->date_mode; ctx.date_mode = revs->date_mode;
pretty_print_commit(revs->commit_format, commit, &buf, &ctx); ctx.fmt = revs->commit_format;
pretty_print_commit(&ctx, commit, &buf);
if (revs->graph) { if (revs->graph) {
if (buf.len) { if (buf.len) {
if (revs->commit_format != CMIT_FMT_ONELINE) if (revs->commit_format != CMIT_FMT_ONELINE)

View File

@ -161,11 +161,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
sha1_to_hex(commit->object.sha1)); sha1_to_hex(commit->object.sha1));
if (log->user_format) { if (log->user_format) {
struct pretty_print_context ctx = {0}; struct pretty_print_context ctx = {0};
ctx.fmt = CMIT_FMT_USERFORMAT;
ctx.abbrev = log->abbrev; ctx.abbrev = log->abbrev;
ctx.subject = ""; ctx.subject = "";
ctx.after_subject = ""; ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL; ctx.date_mode = DATE_NORMAL;
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx); pretty_print_commit(&ctx, commit, &ufbuf);
buffer = ufbuf.buf; buffer = ufbuf.buf;
} else if (*buffer) { } else if (*buffer) {
buffer++; buffer++;

View File

@ -70,6 +70,7 @@ enum cmit_fmt {
struct pretty_print_context struct pretty_print_context
{ {
enum cmit_fmt fmt;
int abbrev; int abbrev;
const char *subject; const char *subject;
const char *after_subject; const char *after_subject;
@ -95,22 +96,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want
extern void format_commit_message(const struct commit *commit, extern void format_commit_message(const struct commit *commit,
const char *format, struct strbuf *sb, const char *format, struct strbuf *sb,
const struct pretty_print_context *context); const struct pretty_print_context *context);
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, extern void pretty_print_commit(const struct pretty_print_context *pp,
struct strbuf *sb, const struct commit *commit,
const struct pretty_print_context *context); struct strbuf *sb);
extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit, extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb); struct strbuf *sb);
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, void pp_user_info(const struct pretty_print_context *pp,
const char *line, enum date_mode dmode, const char *what, struct strbuf *sb,
const char *encoding); const char *line, const char *encoding);
void pp_title_line(enum cmit_fmt fmt, void pp_title_line(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding, const char *encoding,
int need_8bit_cte); int need_8bit_cte);
void pp_remainder(enum cmit_fmt fmt, void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
int indent); int indent);

View File

@ -505,7 +505,8 @@ void show_log(struct rev_info *opt)
ctx.abbrev = opt->diffopt.abbrev; ctx.abbrev = opt->diffopt.abbrev;
ctx.after_subject = extra_headers; ctx.after_subject = extra_headers;
ctx.reflog_info = opt->reflog_info; ctx.reflog_info = opt->reflog_info;
pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx); ctx.fmt = opt->commit_format;
pretty_print_commit(&ctx, commit, &msgbuf);
if (opt->add_signoff) if (opt->add_signoff)
append_signoff(&msgbuf, opt->add_signoff); append_signoff(&msgbuf, opt->add_signoff);

View File

@ -266,16 +266,16 @@ needquote:
strbuf_addstr(sb, "?="); strbuf_addstr(sb, "?=");
} }
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, void pp_user_info(const struct pretty_print_context *pp,
const char *line, enum date_mode dmode, const char *what, struct strbuf *sb,
const char *encoding) const char *line, const char *encoding)
{ {
char *date; char *date;
int namelen; int namelen;
unsigned long time; unsigned long time;
int tz; int tz;
if (fmt == CMIT_FMT_ONELINE) if (pp->fmt == CMIT_FMT_ONELINE)
return; return;
date = strchr(line, '>'); date = strchr(line, '>');
if (!date) if (!date)
@ -284,7 +284,7 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
time = strtoul(date, &date, 10); time = strtoul(date, &date, 10);
tz = strtol(date, NULL, 10); tz = strtol(date, NULL, 10);
if (fmt == CMIT_FMT_EMAIL) { if (pp->fmt == CMIT_FMT_EMAIL) {
char *name_tail = strchr(line, '<'); char *name_tail = strchr(line, '<');
int display_name_length; int display_name_length;
int final_line; int final_line;
@ -307,18 +307,18 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} else { } else {
strbuf_addf(sb, "%s: %.*s%.*s\n", what, strbuf_addf(sb, "%s: %.*s%.*s\n", what,
(fmt == CMIT_FMT_FULLER) ? 4 : 0, (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
" ", namelen, line); " ", namelen, line);
} }
switch (fmt) { switch (pp->fmt) {
case CMIT_FMT_MEDIUM: case CMIT_FMT_MEDIUM:
strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode)); strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode));
break; break;
case CMIT_FMT_EMAIL: case CMIT_FMT_EMAIL:
strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822)); strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
break; break;
case CMIT_FMT_FULLER: case CMIT_FMT_FULLER:
strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode)); strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
break; break;
default: default:
/* notin' */ /* notin' */
@ -349,12 +349,12 @@ static const char *skip_empty_lines(const char *msg)
return msg; return msg;
} }
static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb, static void add_merge_info(const struct pretty_print_context *pp,
const struct commit *commit, int abbrev) struct strbuf *sb, const struct commit *commit)
{ {
struct commit_list *parent = commit->parents; struct commit_list *parent = commit->parents;
if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
!parent || !parent->next) !parent || !parent->next)
return; return;
@ -363,8 +363,8 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
while (parent) { while (parent) {
struct commit *p = parent->item; struct commit *p = parent->item;
const char *hex = NULL; const char *hex = NULL;
if (abbrev) if (pp->abbrev)
hex = find_unique_abbrev(p->object.sha1, abbrev); hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
if (!hex) if (!hex)
hex = sha1_to_hex(p->object.sha1); hex = sha1_to_hex(p->object.sha1);
parent = parent->next; parent = parent->next;
@ -1061,9 +1061,7 @@ void format_commit_message(const struct commit *commit,
free(context.message); free(context.message);
} }
static void pp_header(enum cmit_fmt fmt, static void pp_header(const struct pretty_print_context *pp,
int abbrev,
enum date_mode dmode,
const char *encoding, const char *encoding,
const struct commit *commit, const struct commit *commit,
const char **msg_p, const char **msg_p,
@ -1083,7 +1081,7 @@ static void pp_header(enum cmit_fmt fmt,
/* End of header */ /* End of header */
return; return;
if (fmt == CMIT_FMT_RAW) { if (pp->fmt == CMIT_FMT_RAW) {
strbuf_add(sb, line, linelen); strbuf_add(sb, line, linelen);
continue; continue;
} }
@ -1103,7 +1101,7 @@ static void pp_header(enum cmit_fmt fmt,
; ;
/* with enough slop */ /* with enough slop */
strbuf_grow(sb, num * 50 + 20); strbuf_grow(sb, num * 50 + 20);
add_merge_info(fmt, sb, commit, abbrev); add_merge_info(pp, sb, commit);
parents_shown = 1; parents_shown = 1;
} }
@ -1114,21 +1112,19 @@ static void pp_header(enum cmit_fmt fmt,
*/ */
if (!memcmp(line, "author ", 7)) { if (!memcmp(line, "author ", 7)) {
strbuf_grow(sb, linelen + 80); strbuf_grow(sb, linelen + 80);
pp_user_info("Author", fmt, sb, line + 7, dmode, encoding); pp_user_info(pp, "Author", sb, line + 7, encoding);
} }
if (!memcmp(line, "committer ", 10) && if (!memcmp(line, "committer ", 10) &&
(fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) { (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
strbuf_grow(sb, linelen + 80); strbuf_grow(sb, linelen + 80);
pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding); pp_user_info(pp, "Commit", sb, line + 10, encoding);
} }
} }
} }
void pp_title_line(enum cmit_fmt fmt, void pp_title_line(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding, const char *encoding,
int need_8bit_cte) int need_8bit_cte)
{ {
@ -1138,8 +1134,8 @@ void pp_title_line(enum cmit_fmt fmt,
*msg_p = format_subject(&title, *msg_p, " "); *msg_p = format_subject(&title, *msg_p, " ");
strbuf_grow(sb, title.len + 1024); strbuf_grow(sb, title.len + 1024);
if (subject) { if (pp->subject) {
strbuf_addstr(sb, subject); strbuf_addstr(sb, pp->subject);
add_rfc2047(sb, title.buf, title.len, encoding); add_rfc2047(sb, title.buf, title.len, encoding);
} else { } else {
strbuf_addbuf(sb, &title); strbuf_addbuf(sb, &title);
@ -1153,16 +1149,16 @@ void pp_title_line(enum cmit_fmt fmt,
"Content-Transfer-Encoding: 8bit\n"; "Content-Transfer-Encoding: 8bit\n";
strbuf_addf(sb, header_fmt, encoding); strbuf_addf(sb, header_fmt, encoding);
} }
if (after_subject) { if (pp->after_subject) {
strbuf_addstr(sb, after_subject); strbuf_addstr(sb, pp->after_subject);
} }
if (fmt == CMIT_FMT_EMAIL) { if (pp->fmt == CMIT_FMT_EMAIL) {
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} }
strbuf_release(&title); strbuf_release(&title);
} }
void pp_remainder(enum cmit_fmt fmt, void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
int indent) int indent)
@ -1179,7 +1175,7 @@ void pp_remainder(enum cmit_fmt fmt,
if (is_empty_line(line, &linelen)) { if (is_empty_line(line, &linelen)) {
if (first) if (first)
continue; continue;
if (fmt == CMIT_FMT_SHORT) if (pp->fmt == CMIT_FMT_SHORT)
break; break;
} }
first = 0; first = 0;
@ -1204,19 +1200,19 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
return logmsg_reencode(commit, encoding); return logmsg_reencode(commit, encoding);
} }
void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, void pretty_print_commit(const struct pretty_print_context *pp,
struct strbuf *sb, const struct commit *commit,
const struct pretty_print_context *context) struct strbuf *sb)
{ {
unsigned long beginning_of_body; unsigned long beginning_of_body;
int indent = 4; int indent = 4;
const char *msg = commit->buffer; const char *msg = commit->buffer;
char *reencoded; char *reencoded;
const char *encoding; const char *encoding;
int need_8bit_cte = context->need_8bit_cte; int need_8bit_cte = pp->need_8bit_cte;
if (fmt == CMIT_FMT_USERFORMAT) { if (pp->fmt == CMIT_FMT_USERFORMAT) {
format_commit_message(commit, user_format, sb, context); format_commit_message(commit, user_format, sb, pp);
return; return;
} }
@ -1225,14 +1221,14 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
msg = reencoded; msg = reencoded;
} }
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
indent = 0; indent = 0;
/* /*
* We need to check and emit Content-type: to mark it * We need to check and emit Content-type: to mark it
* as 8-bit if we haven't done so. * as 8-bit if we haven't done so.
*/ */
if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) { if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
int i, ch, in_body; int i, ch, in_body;
for (in_body = i = 0; (ch = msg[i]); i++) { for (in_body = i = 0; (ch = msg[i]); i++) {
@ -1251,9 +1247,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
} }
} }
pp_header(fmt, context->abbrev, context->date_mode, encoding, pp_header(pp, encoding, commit, &msg, sb);
commit, &msg, sb); if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
if (fmt != CMIT_FMT_ONELINE && !context->subject) {
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} }
@ -1261,17 +1256,16 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
msg = skip_empty_lines(msg); msg = skip_empty_lines(msg);
/* These formats treat the title line specially. */ /* These formats treat the title line specially. */
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
pp_title_line(fmt, &msg, sb, context->subject, pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
context->after_subject, encoding, need_8bit_cte);
beginning_of_body = sb->len; beginning_of_body = sb->len;
if (fmt != CMIT_FMT_ONELINE) if (pp->fmt != CMIT_FMT_ONELINE)
pp_remainder(fmt, &msg, sb, indent); pp_remainder(pp, &msg, sb, indent);
strbuf_rtrim(sb); strbuf_rtrim(sb);
/* Make sure there is an EOLN for the non-oneline case */ /* Make sure there is an EOLN for the non-oneline case */
if (fmt != CMIT_FMT_ONELINE) if (pp->fmt != CMIT_FMT_ONELINE)
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
/* /*
@ -1279,10 +1273,10 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
* format. Make sure we did not strip the blank line * format. Make sure we did not strip the blank line
* between the header and the body. * between the header and the body.
*/ */
if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body) if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
if (context->show_notes) if (pp->show_notes)
format_display_notes(commit->object.sha1, sb, encoding, format_display_notes(commit->object.sha1, sb, encoding,
NOTES_SHOW_HEADER | NOTES_INDENT); NOTES_SHOW_HEADER | NOTES_INDENT);
@ -1293,5 +1287,6 @@ void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb) struct strbuf *sb)
{ {
struct pretty_print_context pp = {0}; struct pretty_print_context pp = {0};
pretty_print_commit(fmt, commit, sb, &pp); pp.fmt = fmt;
pretty_print_commit(&pp, commit, sb);
} }