@matheusbsbs / matheus-bagliano / commits / dd2e794a21
Fork 0

Refactor pretty_print_commit arguments into a struct

pretty_print_commit() has a bunch of rarely-used arguments, and introducing more of them requires yet another update of all the call sites. Refactor most of them into a struct to make future extensions easier. The ones that stay "plain" arguments were chosen on the grounds that all callers put real arguments there, whereas some callers have 0/NULL for all arguments that were factored into the struct. We declare the struct 'const' to ensure none of the callers are bitten by the changed (no longer call-by-value) semantics. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Oct 19, 2009 at 17:48 UTC dd2e794a214350711db46c4e08d9b19188a7d63a
12 files changed +72 -46
archive.c
+3 -1
@@ -31,6 +31,8 @@ static void format_subst(const struct commit *commit,
31 {
32 char *to_free = NULL;
33 struct strbuf fmt = STRBUF_INIT;
34 + struct pretty_print_context ctx = {0};
35 + ctx.date_mode = DATE_NORMAL;
36
37 if (src == buf->buf)
38 to_free = strbuf_detach(buf, NULL);
@@ -48,7 +50,7 @@ static void format_subst(const struct commit *commit,
50 strbuf_add(&fmt, b + 8, c - b - 8);
51
52 strbuf_add(buf, src, b - src);
51 - format_commit_message(commit, fmt.buf, buf, DATE_NORMAL);
53 + format_commit_message(commit, fmt.buf, buf, &ctx);
54 len -= c + 1 - src;
55 src = c + 1;
56 }
builtin-branch.c
+2 -1
@@ -387,8 +387,9 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
387
388 commit = item->commit;
389 if (commit && !parse_commit(commit)) {
390 + struct pretty_print_context ctx = {0};
391 pretty_print_commit(CMIT_FMT_ONELINE, commit,
391 - &subject, 0, NULL, NULL, 0, 0);
392 + &subject, &ctx);
393 sub = subject.buf;
394 }
395
builtin-checkout.c
+2 -1
@@ -302,8 +302,9 @@ static void show_local_changes(struct object *head)
302 static void describe_detached_head(char *msg, struct commit *commit)
303 {
304 struct strbuf sb = STRBUF_INIT;
305 + struct pretty_print_context ctx = {0};
306 parse_commit(commit);
306 - pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0);
307 + pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, &ctx);
308 fprintf(stderr, "%s %s... %s\n", msg,
309 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
310 strbuf_release(&sb);
builtin-commit.c
+6 -2
@@ -684,8 +684,10 @@ static const char *find_author_by_nickname(const char *name)
684 prepare_revision_walk(&revs);
685 commit = get_revision(&revs);
686 if (commit) {
687 + struct pretty_print_context ctx = {0};
688 + ctx.date_mode = DATE_NORMAL;
689 strbuf_release(&buf);
688 - format_commit_message(commit, "%an <%ae>", &buf, DATE_NORMAL);
690 + format_commit_message(commit, "%an <%ae>", &buf, &ctx);
691 return strbuf_detach(&buf, NULL);
692 }
693 die("No existing author found with '%s'", name);
@@ -942,8 +944,10 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
944 initial_commit ? " (root-commit)" : "");
945
946 if (!log_tree_commit(&rev, commit)) {
947 + struct pretty_print_context ctx = {0};
948 struct strbuf buf = STRBUF_INIT;
946 - format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
949 + ctx.date_mode = DATE_NORMAL;
950 + format_commit_message(commit, format + 7, &buf, &ctx);
951 printf("%s\n", buf.buf);
952 strbuf_release(&buf);
953 }
builtin-log.c
+2 -1
@@ -1304,8 +1304,9 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
1304
1305 if (verbose) {
1306 struct strbuf buf = STRBUF_INIT;
1307 + struct pretty_print_context ctx = {0};
1308 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1308 - &buf, 0, NULL, NULL, 0, 0);
1309 + &buf, &ctx);
1310 printf("%c %s %s\n", sign,
1311 sha1_to_hex(commit->object.sha1), buf.buf);
1312 strbuf_release(&buf);
builtin-merge.c
+5 -2
@@ -264,6 +264,7 @@ static void squash_message(void)
264 struct strbuf out = STRBUF_INIT;
265 struct commit_list *j;
266 int fd;
267 + struct pretty_print_context ctx = {0};
268
269 printf("Squash commit -- not updating HEAD\n");
270 fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666);
@@ -285,13 +286,15 @@ static void squash_message(void)
286 if (prepare_revision_walk(&rev))
287 die("revision walk setup failed");
288
289 + ctx.abbrev = rev.abbrev;
290 + ctx.date_mode = rev.date_mode;
291 +
292 strbuf_addstr(&out, "Squashed commit of the following:\n");
293 while ((commit = get_revision(&rev)) != NULL) {
294 strbuf_addch(&out, '\n');
295 strbuf_addf(&out, "commit %s\n",
296 sha1_to_hex(commit->object.sha1));
293 - pretty_print_commit(rev.commit_format, commit, &out, rev.abbrev,
294 - NULL, NULL, rev.date_mode, 0);
297 + pretty_print_commit(rev.commit_format, commit, &out, &ctx);
298 }
299 if (write(fd, out.buf, out.len) < 0)
300 die_errno("Writing SQUASH_MSG");
builtin-rev-list.c
+4 -3
@@ -96,9 +96,10 @@ static void show_commit(struct commit *commit, void *data)
96
97 if (revs->verbose_header && commit->buffer) {
98 struct strbuf buf = STRBUF_INIT;
99 - pretty_print_commit(revs->commit_format, commit,
100 - &buf, revs->abbrev, NULL, NULL,
101 - revs->date_mode, 0);
99 + struct pretty_print_context ctx = {0};
100 + ctx.abbrev = revs->abbrev;
101 + ctx.date_mode = revs->date_mode;
102 + pretty_print_commit(revs->commit_format, commit, &buf, &ctx);
103 if (revs->graph) {
104 if (buf.len) {
105 if (revs->commit_format != CMIT_FMT_ONELINE)
builtin-shortlog.c
+6 -3
@@ -158,9 +158,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
158 sha1_to_hex(commit->object.sha1));
159 if (log->user_format) {
160 struct strbuf buf = STRBUF_INIT;
161 -
162 - pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
163 - DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
161 + struct pretty_print_context ctx = {0};
162 + ctx.abbrev = DEFAULT_ABBREV;
163 + ctx.subject = "";
164 + ctx.after_subject = "";
165 + ctx.date_mode = DATE_NORMAL;
166 + pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf, &ctx);
167 insert_one_record(log, author, buf.buf);
168 strbuf_release(&buf);
169 return;
builtin-show-branch.c
+2 -2
@@ -293,8 +293,8 @@ static void show_one_commit(struct commit *commit, int no_name)
293 struct commit_name *name = commit->util;
294
295 if (commit->object.parsed) {
296 - pretty_print_commit(CMIT_FMT_ONELINE, commit,
297 - &pretty, 0, NULL, NULL, 0, 0);
296 + struct pretty_print_context ctx = {0};
297 + pretty_print_commit(CMIT_FMT_ONELINE, commit, &pretty, &ctx);
298 pretty_str = pretty.buf;
299 }
300 if (!prefixcmp(pretty_str, "[PATCH] "))
commit.h
+13 -6
@@ -63,6 +63,15 @@ enum cmit_fmt {
63 CMIT_FMT_UNSPECIFIED,
64 };
65
66 +struct pretty_print_context
67 +{
68 + int abbrev;
69 + const char *subject;
70 + const char *after_subject;
71 + enum date_mode date_mode;
72 + int need_8bit_cte;
73 +};
74 +
75 extern int non_ascii(int);
76 extern int has_non_ascii(const char *text);
77 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
@@ -71,12 +80,10 @@ extern char *reencode_commit_message(const struct commit *commit,
80 extern void get_commit_format(const char *arg, struct rev_info *);
81 extern void format_commit_message(const struct commit *commit,
82 const void *format, struct strbuf *sb,
74 - enum date_mode dmode);
75 -extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,
76 - struct strbuf *,
77 - int abbrev, const char *subject,
78 - const char *after_subject, enum date_mode,
79 - int need_8bit_cte);
83 + const struct pretty_print_context *context);
84 +extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
85 + struct strbuf *sb,
86 + const struct pretty_print_context *context);
87 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
88 const char *line, enum date_mode dmode,
89 const char *encoding);
log-tree.c
+13 -11
@@ -179,8 +179,10 @@ void get_patch_filename(struct commit *commit, int nr, const char *suffix,
179 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
180 if (commit) {
181 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
182 + struct pretty_print_context ctx = {0};
183 + ctx.date_mode = DATE_NORMAL;
184
183 - format_commit_message(commit, "%f", buf, DATE_NORMAL);
185 + format_commit_message(commit, "%f", buf, &ctx);
186 if (max_len < buf->len)
187 strbuf_setlen(buf, max_len);
188 strbuf_addstr(buf, suffix);
@@ -277,10 +279,9 @@ void show_log(struct rev_info *opt)
279 struct strbuf msgbuf = STRBUF_INIT;
280 struct log_info *log = opt->loginfo;
281 struct commit *commit = log->commit, *parent = log->parent;
280 - int abbrev = opt->diffopt.abbrev;
282 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
282 - const char *subject = NULL, *extra_headers = opt->extra_headers;
283 - int need_8bit_cte = 0;
283 + const char *extra_headers = opt->extra_headers;
284 + struct pretty_print_context ctx = {0};
285
286 opt->loginfo = NULL;
287 if (!opt->verbose_header) {
@@ -347,8 +348,8 @@ void show_log(struct rev_info *opt)
348 */
349
350 if (opt->commit_format == CMIT_FMT_EMAIL) {
350 - log_write_email_headers(opt, commit, &subject, &extra_headers,
351 - &need_8bit_cte);
351 + log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
352 + &ctx.need_8bit_cte);
353 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
354 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
355 if (opt->commit_format != CMIT_FMT_ONELINE)
@@ -405,11 +406,12 @@ void show_log(struct rev_info *opt)
406 /*
407 * And then the pretty-printed message itself
408 */
408 - if (need_8bit_cte >= 0)
409 - need_8bit_cte = has_non_ascii(opt->add_signoff);
410 - pretty_print_commit(opt->commit_format, commit, &msgbuf,
411 - abbrev, subject, extra_headers, opt->date_mode,
412 - need_8bit_cte);
409 + if (ctx.need_8bit_cte >= 0)
410 + ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
411 + ctx.date_mode = opt->date_mode;
412 + ctx.abbrev = opt->diffopt.abbrev;
413 + ctx.after_subject = extra_headers;
414 + pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);
415
416 if (opt->add_signoff)
417 append_signoff(&msgbuf, opt->add_signoff);
pretty.c
+14 -13
@@ -442,7 +442,7 @@ struct chunk {
442
443 struct format_commit_context {
444 const struct commit *commit;
445 - enum date_mode dmode;
445 + const struct pretty_print_context *pretty_ctx;
446 unsigned commit_header_parsed:1;
447 unsigned commit_message_parsed:1;
448
@@ -711,11 +711,11 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
711 case 'a': /* author ... */
712 return format_person_part(sb, placeholder[1],
713 msg + c->author.off, c->author.len,
714 - c->dmode);
714 + c->pretty_ctx->date_mode);
715 case 'c': /* committer ... */
716 return format_person_part(sb, placeholder[1],
717 msg + c->committer.off, c->committer.len,
718 - c->dmode);
718 + c->pretty_ctx->date_mode);
719 case 'e': /* encoding */
720 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
721 return 1;
@@ -741,13 +741,13 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
741
742 void format_commit_message(const struct commit *commit,
743 const void *format, struct strbuf *sb,
744 - enum date_mode dmode)
744 + const struct pretty_print_context *pretty_ctx)
745 {
746 struct format_commit_context context;
747
748 memset(&context, 0, sizeof(context));
749 context.commit = commit;
750 - context.dmode = dmode;
750 + context.pretty_ctx = pretty_ctx;
751 strbuf_expand(sb, format, format_commit_item, &context);
752 }
753
@@ -900,18 +900,18 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
900 }
901
902 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
903 - struct strbuf *sb, int abbrev,
904 - const char *subject, const char *after_subject,
905 - enum date_mode dmode, int need_8bit_cte)
903 + struct strbuf *sb,
904 + const struct pretty_print_context *context)
905 {
906 unsigned long beginning_of_body;
907 int indent = 4;
908 const char *msg = commit->buffer;
909 char *reencoded;
910 const char *encoding;
911 + int need_8bit_cte = context->need_8bit_cte;
912
913 if (fmt == CMIT_FMT_USERFORMAT) {
914 - format_commit_message(commit, user_format, sb, dmode);
914 + format_commit_message(commit, user_format, sb, context);
915 return;
916 }
917
@@ -946,8 +946,9 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
946 }
947 }
948
949 - pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
950 - if (fmt != CMIT_FMT_ONELINE && !subject) {
949 + pp_header(fmt, context->abbrev, context->date_mode, encoding,
950 + commit, &msg, sb);
951 + if (fmt != CMIT_FMT_ONELINE && !context->subject) {
952 strbuf_addch(sb, '\n');
953 }
954
@@ -956,8 +957,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
957
958 /* These formats treat the title line specially. */
959 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
959 - pp_title_line(fmt, &msg, sb, subject,
960 - after_subject, encoding, need_8bit_cte);
960 + pp_title_line(fmt, &msg, sb, context->subject,
961 + context->after_subject, encoding, need_8bit_cte);
962
963 beginning_of_body = sb->len;
964 if (fmt != CMIT_FMT_ONELINE)