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>

Jeff King committed May 26, 2011 at 18:27 UTC 6bf139440c192e157b9c0dab701fa2100fbb1e1e
7 files changed +75 -76
builtin/log.c
+11 -10
@@ -327,9 +327,11 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
327 static void show_tagger(char *buf, int len, struct rev_info *rev)
328 {
329 struct strbuf out = STRBUF_INIT;
330 + struct pretty_print_context pp = {0};
331
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());
335 printf("%s", out.buf);
336 strbuf_release(&out);
337 }
@@ -715,10 +717,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
717 int nr, struct commit **list, struct commit *head)
718 {
719 const char *committer;
718 - const char *subject_start = NULL;
720 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
721 const char *msg;
721 - const char *extra_headers = rev->extra_headers;
722 struct shortlog log;
723 struct strbuf sb = STRBUF_INIT;
724 int i;
@@ -726,6 +726,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
726 struct diff_options opts;
727 int need_8bit_cte = 0;
728 struct commit *commit = NULL;
729 + struct pretty_print_context pp = {0};
730
731 if (rev->commit_format != CMIT_FMT_EMAIL)
732 die("Cover letter needs email format");
@@ -757,7 +758,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
758 free(commit);
759 }
760
760 - log_write_email_headers(rev, head, &subject_start, &extra_headers,
761 + log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
762 &need_8bit_cte);
763
764 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,
766 need_8bit_cte = 1;
767
768 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);
774 printf("%s\n", sb.buf);
775
776 strbuf_release(&sb);
builtin/merge.c
+2 -1
@@ -333,13 +333,14 @@ static void squash_message(void)
333
334 ctx.abbrev = rev.abbrev;
335 ctx.date_mode = rev.date_mode;
336 + ctx.fmt = rev.commit_format;
337
338 strbuf_addstr(&out, "Squashed commit of the following:\n");
339 while ((commit = get_revision(&rev)) != NULL) {
340 strbuf_addch(&out, '\n');
341 strbuf_addf(&out, "commit %s\n",
342 sha1_to_hex(commit->object.sha1));
342 - pretty_print_commit(rev.commit_format, commit, &out, &ctx);
343 + pretty_print_commit(&ctx, commit, &out);
344 }
345 if (write(fd, out.buf, out.len) < 0)
346 die_errno("Writing SQUASH_MSG");
builtin/rev-list.c
+2 -1
@@ -108,7 +108,8 @@ static void show_commit(struct commit *commit, void *data)
108 struct pretty_print_context ctx = {0};
109 ctx.abbrev = revs->abbrev;
110 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);
113 if (revs->graph) {
114 if (buf.len) {
115 if (revs->commit_format != CMIT_FMT_ONELINE)
builtin/shortlog.c
+2 -1
@@ -161,11 +161,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
161 sha1_to_hex(commit->object.sha1));
162 if (log->user_format) {
163 struct pretty_print_context ctx = {0};
164 + ctx.fmt = CMIT_FMT_USERFORMAT;
165 ctx.abbrev = log->abbrev;
166 ctx.subject = "";
167 ctx.after_subject = "";
168 ctx.date_mode = DATE_NORMAL;
168 - pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx);
169 + pretty_print_commit(&ctx, commit, &ufbuf);
170 buffer = ufbuf.buf;
171 } else if (*buffer) {
172 buffer++;
commit.h
+9 -10
@@ -70,6 +70,7 @@ enum cmit_fmt {
70
71 struct pretty_print_context
72 {
73 + enum cmit_fmt fmt;
74 int abbrev;
75 const char *subject;
76 const char *after_subject;
@@ -95,22 +96,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want
96 extern void format_commit_message(const struct commit *commit,
97 const char *format, struct strbuf *sb,
98 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);
102 extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
103 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,
108 const char **msg_p,
109 struct strbuf *sb,
109 - const char *subject,
110 - const char *after_subject,
110 const char *encoding,
111 int need_8bit_cte);
113 -void pp_remainder(enum cmit_fmt fmt,
112 +void pp_remainder(const struct pretty_print_context *pp,
113 const char **msg_p,
114 struct strbuf *sb,
115 int indent);
log-tree.c
+2 -1
@@ -505,7 +505,8 @@ void show_log(struct rev_info *opt)
505 ctx.abbrev = opt->diffopt.abbrev;
506 ctx.after_subject = extra_headers;
507 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);
510
511 if (opt->add_signoff)
512 append_signoff(&msgbuf, opt->add_signoff);
pretty.c
+47 -52
@@ -266,16 +266,16 @@ needquote:
266 strbuf_addstr(sb, "?=");
267 }
268
269 -void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
270 - const char *line, enum date_mode dmode,
271 - const char *encoding)
269 +void pp_user_info(const struct pretty_print_context *pp,
270 + const char *what, struct strbuf *sb,
271 + const char *line, const char *encoding)
272 {
273 char *date;
274 int namelen;
275 unsigned long time;
276 int tz;
277
278 - if (fmt == CMIT_FMT_ONELINE)
278 + if (pp->fmt == CMIT_FMT_ONELINE)
279 return;
280 date = strchr(line, '>');
281 if (!date)
@@ -284,7 +284,7 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
284 time = strtoul(date, &date, 10);
285 tz = strtol(date, NULL, 10);
286
287 - if (fmt == CMIT_FMT_EMAIL) {
287 + if (pp->fmt == CMIT_FMT_EMAIL) {
288 char *name_tail = strchr(line, '<');
289 int display_name_length;
290 int final_line;
@@ -307,18 +307,18 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
307 strbuf_addch(sb, '\n');
308 } else {
309 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
310 - (fmt == CMIT_FMT_FULLER) ? 4 : 0,
310 + (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
311 " ", namelen, line);
312 }
313 - switch (fmt) {
313 + switch (pp->fmt) {
314 case CMIT_FMT_MEDIUM:
315 - strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
315 + strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode));
316 break;
317 case CMIT_FMT_EMAIL:
318 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
319 break;
320 case CMIT_FMT_FULLER:
321 - strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
321 + strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
322 break;
323 default:
324 /* notin' */
@@ -349,12 +349,12 @@ static const char *skip_empty_lines(const char *msg)
349 return msg;
350 }
351
352 -static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
353 - const struct commit *commit, int abbrev)
352 +static void add_merge_info(const struct pretty_print_context *pp,
353 + struct strbuf *sb, const struct commit *commit)
354 {
355 struct commit_list *parent = commit->parents;
356
357 - if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
357 + if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
358 !parent || !parent->next)
359 return;
360
@@ -363,8 +363,8 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
363 while (parent) {
364 struct commit *p = parent->item;
365 const char *hex = NULL;
366 - if (abbrev)
367 - hex = find_unique_abbrev(p->object.sha1, abbrev);
366 + if (pp->abbrev)
367 + hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
368 if (!hex)
369 hex = sha1_to_hex(p->object.sha1);
370 parent = parent->next;
@@ -1061,9 +1061,7 @@ void format_commit_message(const struct commit *commit,
1061 free(context.message);
1062 }
1063
1064 -static void pp_header(enum cmit_fmt fmt,
1065 - int abbrev,
1066 - enum date_mode dmode,
1064 +static void pp_header(const struct pretty_print_context *pp,
1065 const char *encoding,
1066 const struct commit *commit,
1067 const char **msg_p,
@@ -1083,7 +1081,7 @@ static void pp_header(enum cmit_fmt fmt,
1081 /* End of header */
1082 return;
1083
1086 - if (fmt == CMIT_FMT_RAW) {
1084 + if (pp->fmt == CMIT_FMT_RAW) {
1085 strbuf_add(sb, line, linelen);
1086 continue;
1087 }
@@ -1103,7 +1101,7 @@ static void pp_header(enum cmit_fmt fmt,
1101 ;
1102 /* with enough slop */
1103 strbuf_grow(sb, num * 50 + 20);
1106 - add_merge_info(fmt, sb, commit, abbrev);
1104 + add_merge_info(pp, sb, commit);
1105 parents_shown = 1;
1106 }
1107
@@ -1114,21 +1112,19 @@ static void pp_header(enum cmit_fmt fmt,
1112 */
1113 if (!memcmp(line, "author ", 7)) {
1114 strbuf_grow(sb, linelen + 80);
1117 - pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
1115 + pp_user_info(pp, "Author", sb, line + 7, encoding);
1116 }
1117 if (!memcmp(line, "committer ", 10) &&
1120 - (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
1118 + (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
1119 strbuf_grow(sb, linelen + 80);
1122 - pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
1120 + pp_user_info(pp, "Commit", sb, line + 10, encoding);
1121 }
1122 }
1123 }
1124
1127 -void pp_title_line(enum cmit_fmt fmt,
1125 +void pp_title_line(const struct pretty_print_context *pp,
1126 const char **msg_p,
1127 struct strbuf *sb,
1130 - const char *subject,
1131 - const char *after_subject,
1128 const char *encoding,
1129 int need_8bit_cte)
1130 {
@@ -1138,8 +1134,8 @@ void pp_title_line(enum cmit_fmt fmt,
1134 *msg_p = format_subject(&title, *msg_p, " ");
1135
1136 strbuf_grow(sb, title.len + 1024);
1141 - if (subject) {
1142 - strbuf_addstr(sb, subject);
1137 + if (pp->subject) {
1138 + strbuf_addstr(sb, pp->subject);
1139 add_rfc2047(sb, title.buf, title.len, encoding);
1140 } else {
1141 strbuf_addbuf(sb, &title);
@@ -1153,16 +1149,16 @@ void pp_title_line(enum cmit_fmt fmt,
1149 "Content-Transfer-Encoding: 8bit\n";
1150 strbuf_addf(sb, header_fmt, encoding);
1151 }
1156 - if (after_subject) {
1157 - strbuf_addstr(sb, after_subject);
1152 + if (pp->after_subject) {
1153 + strbuf_addstr(sb, pp->after_subject);
1154 }
1159 - if (fmt == CMIT_FMT_EMAIL) {
1155 + if (pp->fmt == CMIT_FMT_EMAIL) {
1156 strbuf_addch(sb, '\n');
1157 }
1158 strbuf_release(&title);
1159 }
1160
1165 -void pp_remainder(enum cmit_fmt fmt,
1161 +void pp_remainder(const struct pretty_print_context *pp,
1162 const char **msg_p,
1163 struct strbuf *sb,
1164 int indent)
@@ -1179,7 +1175,7 @@ void pp_remainder(enum cmit_fmt fmt,
1175 if (is_empty_line(line, &linelen)) {
1176 if (first)
1177 continue;
1182 - if (fmt == CMIT_FMT_SHORT)
1178 + if (pp->fmt == CMIT_FMT_SHORT)
1179 break;
1180 }
1181 first = 0;
@@ -1204,19 +1200,19 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
1200 return logmsg_reencode(commit, encoding);
1201 }
1202
1207 -void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1208 - struct strbuf *sb,
1209 - const struct pretty_print_context *context)
1203 +void pretty_print_commit(const struct pretty_print_context *pp,
1204 + const struct commit *commit,
1205 + struct strbuf *sb)
1206 {
1207 unsigned long beginning_of_body;
1208 int indent = 4;
1209 const char *msg = commit->buffer;
1210 char *reencoded;
1211 const char *encoding;
1216 - int need_8bit_cte = context->need_8bit_cte;
1212 + int need_8bit_cte = pp->need_8bit_cte;
1213
1218 - if (fmt == CMIT_FMT_USERFORMAT) {
1219 - format_commit_message(commit, user_format, sb, context);
1214 + if (pp->fmt == CMIT_FMT_USERFORMAT) {
1215 + format_commit_message(commit, user_format, sb, pp);
1216 return;
1217 }
1218
@@ -1225,14 +1221,14 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1221 msg = reencoded;
1222 }
1223
1228 - if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1224 + if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1225 indent = 0;
1226
1227 /*
1228 * We need to check and emit Content-type: to mark it
1229 * as 8-bit if we haven't done so.
1230 */
1235 - if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1231 + if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1232 int i, ch, in_body;
1233
1234 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,
1247 }
1248 }
1249
1254 - pp_header(fmt, context->abbrev, context->date_mode, encoding,
1255 - commit, &msg, sb);
1256 - if (fmt != CMIT_FMT_ONELINE && !context->subject) {
1250 + pp_header(pp, encoding, commit, &msg, sb);
1251 + if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
1252 strbuf_addch(sb, '\n');
1253 }
1254
@@ -1261,17 +1256,16 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1256 msg = skip_empty_lines(msg);
1257
1258 /* These formats treat the title line specially. */
1264 - if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1265 - pp_title_line(fmt, &msg, sb, context->subject,
1266 - context->after_subject, encoding, need_8bit_cte);
1259 + if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1260 + pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
1261
1262 beginning_of_body = sb->len;
1269 - if (fmt != CMIT_FMT_ONELINE)
1270 - pp_remainder(fmt, &msg, sb, indent);
1263 + if (pp->fmt != CMIT_FMT_ONELINE)
1264 + pp_remainder(pp, &msg, sb, indent);
1265 strbuf_rtrim(sb);
1266
1267 /* Make sure there is an EOLN for the non-oneline case */
1274 - if (fmt != CMIT_FMT_ONELINE)
1268 + if (pp->fmt != CMIT_FMT_ONELINE)
1269 strbuf_addch(sb, '\n');
1270
1271 /*
@@ -1279,10 +1273,10 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1273 * format. Make sure we did not strip the blank line
1274 * between the header and the body.
1275 */
1282 - if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1276 + if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1277 strbuf_addch(sb, '\n');
1278
1285 - if (context->show_notes)
1279 + if (pp->show_notes)
1280 format_display_notes(commit->object.sha1, sb, encoding,
1281 NOTES_SHOW_HEADER | NOTES_INDENT);
1282
@@ -1293,5 +1287,6 @@ void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
1287 struct strbuf *sb)
1288 {
1289 struct pretty_print_context pp = {0};
1296 - pretty_print_commit(fmt, commit, sb, &pp);
1290 + pp.fmt = fmt;
1291 + pretty_print_commit(&pp, commit, sb);
1292 }