Skip to content
/ git Public
forked from git/git

Commit

Permalink
clean up calling conventions for pretty.c functions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
peff authored and gitster committed May 26, 2011
1 parent 8b8a537 commit 6bf1394
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 76 deletions.
21 changes: 11 additions & 10 deletions builtin/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
struct strbuf out = STRBUF_INIT;
struct pretty_print_context pp = {0};

pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
get_log_output_encoding());
pp.fmt = rev->commit_format;
pp.date_mode = rev->date_mode;
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
printf("%s", out.buf);
strbuf_release(&out);
}
Expand Down Expand Up @@ -715,17 +717,16 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
int nr, struct commit **list, struct commit *head)
{
const char *committer;
const char *subject_start = NULL;
const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
const char *msg;
const char *extra_headers = rev->extra_headers;
struct shortlog log;
struct strbuf sb = STRBUF_INIT;
int i;
const char *encoding = "UTF-8";
struct diff_options opts;
int need_8bit_cte = 0;
struct commit *commit = NULL;
struct pretty_print_context pp = {0};

if (rev->commit_format != CMIT_FMT_EMAIL)
die("Cover letter needs email format");
Expand Down Expand Up @@ -757,19 +758,19 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
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);

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

msg = body;
pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
encoding);
pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
encoding, need_8bit_cte);
pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
pp.fmt = CMIT_FMT_EMAIL;
pp.date_mode = DATE_RFC2822;
pp_user_info(&pp, NULL, &sb, committer, encoding);
pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
pp_remainder(&pp, &msg, &sb, 0);
printf("%s\n", sb.buf);

strbuf_release(&sb);
Expand Down
3 changes: 2 additions & 1 deletion builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,14 @@ static void squash_message(void)

ctx.abbrev = rev.abbrev;
ctx.date_mode = rev.date_mode;
ctx.fmt = rev.commit_format;

strbuf_addstr(&out, "Squashed commit of the following:\n");
while ((commit = get_revision(&rev)) != NULL) {
strbuf_addch(&out, '\n');
strbuf_addf(&out, "commit %s\n",
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)
die_errno("Writing SQUASH_MSG");
Expand Down
3 changes: 2 additions & 1 deletion builtin/rev-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ static void show_commit(struct commit *commit, void *data)
struct pretty_print_context ctx = {0};
ctx.abbrev = revs->abbrev;
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 (buf.len) {
if (revs->commit_format != CMIT_FMT_ONELINE)
Expand Down
3 changes: 2 additions & 1 deletion builtin/shortlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
sha1_to_hex(commit->object.sha1));
if (log->user_format) {
struct pretty_print_context ctx = {0};
ctx.fmt = CMIT_FMT_USERFORMAT;
ctx.abbrev = log->abbrev;
ctx.subject = "";
ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL;
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx);
pretty_print_commit(&ctx, commit, &ufbuf);
buffer = ufbuf.buf;
} else if (*buffer) {
buffer++;
Expand Down
19 changes: 9 additions & 10 deletions commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum cmit_fmt {

struct pretty_print_context
{
enum cmit_fmt fmt;
int abbrev;
const char *subject;
const char *after_subject;
Expand All @@ -95,22 +96,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want
extern void format_commit_message(const struct commit *commit,
const char *format, struct strbuf *sb,
const struct pretty_print_context *context);
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb,
const struct pretty_print_context *context);
extern void pretty_print_commit(const struct pretty_print_context *pp,
const struct commit *commit,
struct strbuf *sb);
extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
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 *encoding);
void pp_title_line(enum cmit_fmt fmt,
void pp_user_info(const struct pretty_print_context *pp,
const char *what, struct strbuf *sb,
const char *line, const char *encoding);
void pp_title_line(const struct pretty_print_context *pp,
const char **msg_p,
struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding,
int need_8bit_cte);
void pp_remainder(enum cmit_fmt fmt,
void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p,
struct strbuf *sb,
int indent);
Expand Down
3 changes: 2 additions & 1 deletion log-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ void show_log(struct rev_info *opt)
ctx.abbrev = opt->diffopt.abbrev;
ctx.after_subject = extra_headers;
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)
append_signoff(&msgbuf, opt->add_signoff);
Expand Down
Loading

0 comments on commit 6bf1394

Please sign in to comment.