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>
maint
Thomas Rast 2009-10-19 17:48:08 +02:00 committed by Junio C Hamano
parent 9ecb2a7f49
commit dd2e794a21
12 changed files with 72 additions and 46 deletions

View File

@ -31,6 +31,8 @@ static void format_subst(const struct commit *commit,
{ {
char *to_free = NULL; char *to_free = NULL;
struct strbuf fmt = STRBUF_INIT; struct strbuf fmt = STRBUF_INIT;
struct pretty_print_context ctx = {0};
ctx.date_mode = DATE_NORMAL;


if (src == buf->buf) if (src == buf->buf)
to_free = strbuf_detach(buf, NULL); to_free = strbuf_detach(buf, NULL);
@ -48,7 +50,7 @@ static void format_subst(const struct commit *commit,
strbuf_add(&fmt, b + 8, c - b - 8); strbuf_add(&fmt, b + 8, c - b - 8);


strbuf_add(buf, src, b - src); strbuf_add(buf, src, b - src);
format_commit_message(commit, fmt.buf, buf, DATE_NORMAL); format_commit_message(commit, fmt.buf, buf, &ctx);
len -= c + 1 - src; len -= c + 1 - src;
src = c + 1; src = c + 1;
} }

View File

@ -387,8 +387,9 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,


commit = item->commit; commit = item->commit;
if (commit && !parse_commit(commit)) { if (commit && !parse_commit(commit)) {
struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_ONELINE, commit, pretty_print_commit(CMIT_FMT_ONELINE, commit,
&subject, 0, NULL, NULL, 0, 0); &subject, &ctx);
sub = subject.buf; sub = subject.buf;
} }



View File

@ -302,8 +302,9 @@ static void show_local_changes(struct object *head)
static void describe_detached_head(char *msg, struct commit *commit) static void describe_detached_head(char *msg, struct commit *commit)
{ {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct pretty_print_context ctx = {0};
parse_commit(commit); parse_commit(commit);
pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0); pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, &ctx);
fprintf(stderr, "%s %s... %s\n", msg, fprintf(stderr, "%s %s... %s\n", msg,
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf); find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
strbuf_release(&sb); strbuf_release(&sb);

View File

@ -684,8 +684,10 @@ static const char *find_author_by_nickname(const char *name)
prepare_revision_walk(&revs); prepare_revision_walk(&revs);
commit = get_revision(&revs); commit = get_revision(&revs);
if (commit) { if (commit) {
struct pretty_print_context ctx = {0};
ctx.date_mode = DATE_NORMAL;
strbuf_release(&buf); strbuf_release(&buf);
format_commit_message(commit, "%an <%ae>", &buf, DATE_NORMAL); format_commit_message(commit, "%an <%ae>", &buf, &ctx);
return strbuf_detach(&buf, NULL); return strbuf_detach(&buf, NULL);
} }
die("No existing author found with '%s'", name); die("No existing author found with '%s'", name);
@ -942,8 +944,10 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
initial_commit ? " (root-commit)" : ""); initial_commit ? " (root-commit)" : "");


if (!log_tree_commit(&rev, commit)) { if (!log_tree_commit(&rev, commit)) {
struct pretty_print_context ctx = {0};
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
format_commit_message(commit, format + 7, &buf, DATE_NORMAL); ctx.date_mode = DATE_NORMAL;
format_commit_message(commit, format + 7, &buf, &ctx);
printf("%s\n", buf.buf); printf("%s\n", buf.buf);
strbuf_release(&buf); strbuf_release(&buf);
} }

View File

@ -1304,8 +1304,9 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)


if (verbose) { if (verbose) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_ONELINE, commit, pretty_print_commit(CMIT_FMT_ONELINE, commit,
&buf, 0, NULL, NULL, 0, 0); &buf, &ctx);
printf("%c %s %s\n", sign, printf("%c %s %s\n", sign,
sha1_to_hex(commit->object.sha1), buf.buf); sha1_to_hex(commit->object.sha1), buf.buf);
strbuf_release(&buf); strbuf_release(&buf);

View File

@ -264,6 +264,7 @@ static void squash_message(void)
struct strbuf out = STRBUF_INIT; struct strbuf out = STRBUF_INIT;
struct commit_list *j; struct commit_list *j;
int fd; int fd;
struct pretty_print_context ctx = {0};


printf("Squash commit -- not updating HEAD\n"); printf("Squash commit -- not updating HEAD\n");
fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666); fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666);
@ -285,13 +286,15 @@ static void squash_message(void)
if (prepare_revision_walk(&rev)) if (prepare_revision_walk(&rev))
die("revision walk setup failed"); die("revision walk setup failed");


ctx.abbrev = rev.abbrev;
ctx.date_mode = rev.date_mode;

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, rev.abbrev, pretty_print_commit(rev.commit_format, commit, &out, &ctx);
NULL, NULL, rev.date_mode, 0);
} }
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

@ -96,9 +96,10 @@ static void show_commit(struct commit *commit, void *data)


if (revs->verbose_header && commit->buffer) { if (revs->verbose_header && commit->buffer) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
pretty_print_commit(revs->commit_format, commit, struct pretty_print_context ctx = {0};
&buf, revs->abbrev, NULL, NULL, ctx.abbrev = revs->abbrev;
revs->date_mode, 0); ctx.date_mode = revs->date_mode;
pretty_print_commit(revs->commit_format, commit, &buf, &ctx);
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

@ -158,9 +158,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 strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;

struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf, ctx.abbrev = DEFAULT_ABBREV;
DEFAULT_ABBREV, "", "", DATE_NORMAL, 0); ctx.subject = "";
ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL;
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf, &ctx);
insert_one_record(log, author, buf.buf); insert_one_record(log, author, buf.buf);
strbuf_release(&buf); strbuf_release(&buf);
return; return;

View File

@ -293,8 +293,8 @@ static void show_one_commit(struct commit *commit, int no_name)
struct commit_name *name = commit->util; struct commit_name *name = commit->util;


if (commit->object.parsed) { if (commit->object.parsed) {
pretty_print_commit(CMIT_FMT_ONELINE, commit, struct pretty_print_context ctx = {0};
&pretty, 0, NULL, NULL, 0, 0); pretty_print_commit(CMIT_FMT_ONELINE, commit, &pretty, &ctx);
pretty_str = pretty.buf; pretty_str = pretty.buf;
} }
if (!prefixcmp(pretty_str, "[PATCH] ")) if (!prefixcmp(pretty_str, "[PATCH] "))

View File

@ -63,6 +63,15 @@ enum cmit_fmt {
CMIT_FMT_UNSPECIFIED, CMIT_FMT_UNSPECIFIED,
}; };


struct pretty_print_context
{
int abbrev;
const char *subject;
const char *after_subject;
enum date_mode date_mode;
int need_8bit_cte;
};

extern int non_ascii(int); extern int non_ascii(int);
extern int has_non_ascii(const char *text); extern int has_non_ascii(const char *text);
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */ 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,
extern void get_commit_format(const char *arg, struct rev_info *); extern void get_commit_format(const char *arg, struct rev_info *);
extern void format_commit_message(const struct commit *commit, extern void format_commit_message(const struct commit *commit,
const void *format, struct strbuf *sb, const void *format, struct strbuf *sb,
enum date_mode dmode); const struct pretty_print_context *context);
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*, extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *, struct strbuf *sb,
int abbrev, const char *subject, const struct pretty_print_context *context);
const char *after_subject, enum date_mode,
int need_8bit_cte);
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
const char *line, enum date_mode dmode, const char *line, enum date_mode dmode,
const char *encoding); const char *encoding);

View File

@ -179,8 +179,10 @@ void get_patch_filename(struct commit *commit, int nr, const char *suffix,
strbuf_addf(buf, commit ? "%04d-" : "%d", nr); strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
if (commit) { if (commit) {
int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len; int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
struct pretty_print_context ctx = {0};
ctx.date_mode = DATE_NORMAL;


format_commit_message(commit, "%f", buf, DATE_NORMAL); format_commit_message(commit, "%f", buf, &ctx);
if (max_len < buf->len) if (max_len < buf->len)
strbuf_setlen(buf, max_len); strbuf_setlen(buf, max_len);
strbuf_addstr(buf, suffix); strbuf_addstr(buf, suffix);
@ -277,10 +279,9 @@ void show_log(struct rev_info *opt)
struct strbuf msgbuf = STRBUF_INIT; struct strbuf msgbuf = STRBUF_INIT;
struct log_info *log = opt->loginfo; struct log_info *log = opt->loginfo;
struct commit *commit = log->commit, *parent = log->parent; struct commit *commit = log->commit, *parent = log->parent;
int abbrev = opt->diffopt.abbrev;
int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40; int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
const char *subject = NULL, *extra_headers = opt->extra_headers; const char *extra_headers = opt->extra_headers;
int need_8bit_cte = 0; struct pretty_print_context ctx = {0};


opt->loginfo = NULL; opt->loginfo = NULL;
if (!opt->verbose_header) { if (!opt->verbose_header) {
@ -347,8 +348,8 @@ void show_log(struct rev_info *opt)
*/ */


if (opt->commit_format == CMIT_FMT_EMAIL) { if (opt->commit_format == CMIT_FMT_EMAIL) {
log_write_email_headers(opt, commit, &subject, &extra_headers, log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
&need_8bit_cte); &ctx.need_8bit_cte);
} else if (opt->commit_format != CMIT_FMT_USERFORMAT) { } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout); fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
if (opt->commit_format != CMIT_FMT_ONELINE) if (opt->commit_format != CMIT_FMT_ONELINE)
@ -405,11 +406,12 @@ void show_log(struct rev_info *opt)
/* /*
* And then the pretty-printed message itself * And then the pretty-printed message itself
*/ */
if (need_8bit_cte >= 0) if (ctx.need_8bit_cte >= 0)
need_8bit_cte = has_non_ascii(opt->add_signoff); ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
pretty_print_commit(opt->commit_format, commit, &msgbuf, ctx.date_mode = opt->date_mode;
abbrev, subject, extra_headers, opt->date_mode, ctx.abbrev = opt->diffopt.abbrev;
need_8bit_cte); ctx.after_subject = extra_headers;
pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);


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

View File

@ -442,7 +442,7 @@ struct chunk {


struct format_commit_context { struct format_commit_context {
const struct commit *commit; const struct commit *commit;
enum date_mode dmode; const struct pretty_print_context *pretty_ctx;
unsigned commit_header_parsed:1; unsigned commit_header_parsed:1;
unsigned commit_message_parsed:1; unsigned commit_message_parsed:1;


@ -711,11 +711,11 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
case 'a': /* author ... */ case 'a': /* author ... */
return format_person_part(sb, placeholder[1], return format_person_part(sb, placeholder[1],
msg + c->author.off, c->author.len, msg + c->author.off, c->author.len,
c->dmode); c->pretty_ctx->date_mode);
case 'c': /* committer ... */ case 'c': /* committer ... */
return format_person_part(sb, placeholder[1], return format_person_part(sb, placeholder[1],
msg + c->committer.off, c->committer.len, msg + c->committer.off, c->committer.len,
c->dmode); c->pretty_ctx->date_mode);
case 'e': /* encoding */ case 'e': /* encoding */
strbuf_add(sb, msg + c->encoding.off, c->encoding.len); strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
return 1; return 1;
@ -741,13 +741,13 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,


void format_commit_message(const struct commit *commit, void format_commit_message(const struct commit *commit,
const void *format, struct strbuf *sb, const void *format, struct strbuf *sb,
enum date_mode dmode) const struct pretty_print_context *pretty_ctx)
{ {
struct format_commit_context context; struct format_commit_context context;


memset(&context, 0, sizeof(context)); memset(&context, 0, sizeof(context));
context.commit = commit; context.commit = commit;
context.dmode = dmode; context.pretty_ctx = pretty_ctx;
strbuf_expand(sb, format, format_commit_item, &context); strbuf_expand(sb, format, format_commit_item, &context);
} }


@ -900,18 +900,18 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
} }


void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb, int abbrev, struct strbuf *sb,
const char *subject, const char *after_subject, const struct pretty_print_context *context)
enum date_mode dmode, int need_8bit_cte)
{ {
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;


if (fmt == CMIT_FMT_USERFORMAT) { if (fmt == CMIT_FMT_USERFORMAT) {
format_commit_message(commit, user_format, sb, dmode); format_commit_message(commit, user_format, sb, context);
return; return;
} }


@ -946,8 +946,9 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
} }
} }


pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb); pp_header(fmt, context->abbrev, context->date_mode, encoding,
if (fmt != CMIT_FMT_ONELINE && !subject) { commit, &msg, sb);
if (fmt != CMIT_FMT_ONELINE && !context->subject) {
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} }


@ -956,8 +957,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,


/* These formats treat the title line specially. */ /* These formats treat the title line specially. */
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
pp_title_line(fmt, &msg, sb, subject, pp_title_line(fmt, &msg, sb, context->subject,
after_subject, 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 (fmt != CMIT_FMT_ONELINE)