Skip to content

Commit dd2e794

Browse files
trastgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9ecb2a7 commit dd2e794

12 files changed

+72
-46
lines changed

archive.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static void format_subst(const struct commit *commit,
3131
{
3232
char *to_free = NULL;
3333
struct strbuf fmt = STRBUF_INIT;
34+
struct pretty_print_context ctx = {0};
35+
ctx.date_mode = DATE_NORMAL;
3436

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

5052
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);
5254
len -= c + 1 - src;
5355
src = c + 1;
5456
}

builtin-branch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,9 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
387387

388388
commit = item->commit;
389389
if (commit && !parse_commit(commit)) {
390+
struct pretty_print_context ctx = {0};
390391
pretty_print_commit(CMIT_FMT_ONELINE, commit,
391-
&subject, 0, NULL, NULL, 0, 0);
392+
&subject, &ctx);
392393
sub = subject.buf;
393394
}
394395

builtin-checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ static void show_local_changes(struct object *head)
302302
static void describe_detached_head(char *msg, struct commit *commit)
303303
{
304304
struct strbuf sb = STRBUF_INIT;
305+
struct pretty_print_context ctx = {0};
305306
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);
307308
fprintf(stderr, "%s %s... %s\n", msg,
308309
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
309310
strbuf_release(&sb);

builtin-commit.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,10 @@ static const char *find_author_by_nickname(const char *name)
684684
prepare_revision_walk(&revs);
685685
commit = get_revision(&revs);
686686
if (commit) {
687+
struct pretty_print_context ctx = {0};
688+
ctx.date_mode = DATE_NORMAL;
687689
strbuf_release(&buf);
688-
format_commit_message(commit, "%an <%ae>", &buf, DATE_NORMAL);
690+
format_commit_message(commit, "%an <%ae>", &buf, &ctx);
689691
return strbuf_detach(&buf, NULL);
690692
}
691693
die("No existing author found with '%s'", name);
@@ -942,8 +944,10 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
942944
initial_commit ? " (root-commit)" : "");
943945

944946
if (!log_tree_commit(&rev, commit)) {
947+
struct pretty_print_context ctx = {0};
945948
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);
947951
printf("%s\n", buf.buf);
948952
strbuf_release(&buf);
949953
}

builtin-log.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,9 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
13041304

13051305
if (verbose) {
13061306
struct strbuf buf = STRBUF_INIT;
1307+
struct pretty_print_context ctx = {0};
13071308
pretty_print_commit(CMIT_FMT_ONELINE, commit,
1308-
&buf, 0, NULL, NULL, 0, 0);
1309+
&buf, &ctx);
13091310
printf("%c %s %s\n", sign,
13101311
sha1_to_hex(commit->object.sha1), buf.buf);
13111312
strbuf_release(&buf);

builtin-merge.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static void squash_message(void)
264264
struct strbuf out = STRBUF_INIT;
265265
struct commit_list *j;
266266
int fd;
267+
struct pretty_print_context ctx = {0};
267268

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

289+
ctx.abbrev = rev.abbrev;
290+
ctx.date_mode = rev.date_mode;
291+
288292
strbuf_addstr(&out, "Squashed commit of the following:\n");
289293
while ((commit = get_revision(&rev)) != NULL) {
290294
strbuf_addch(&out, '\n');
291295
strbuf_addf(&out, "commit %s\n",
292296
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);
295298
}
296299
if (write(fd, out.buf, out.len) < 0)
297300
die_errno("Writing SQUASH_MSG");

builtin-rev-list.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ static void show_commit(struct commit *commit, void *data)
9696

9797
if (revs->verbose_header && commit->buffer) {
9898
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);
102103
if (revs->graph) {
103104
if (buf.len) {
104105
if (revs->commit_format != CMIT_FMT_ONELINE)

builtin-shortlog.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
158158
sha1_to_hex(commit->object.sha1));
159159
if (log->user_format) {
160160
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);
164167
insert_one_record(log, author, buf.buf);
165168
strbuf_release(&buf);
166169
return;

builtin-show-branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ static void show_one_commit(struct commit *commit, int no_name)
293293
struct commit_name *name = commit->util;
294294

295295
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);
298298
pretty_str = pretty.buf;
299299
}
300300
if (!prefixcmp(pretty_str, "[PATCH] "))

commit.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ enum cmit_fmt {
6363
CMIT_FMT_UNSPECIFIED,
6464
};
6565

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+
6675
extern int non_ascii(int);
6776
extern int has_non_ascii(const char *text);
6877
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,
7180
extern void get_commit_format(const char *arg, struct rev_info *);
7281
extern void format_commit_message(const struct commit *commit,
7382
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);
8087
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
8188
const char *line, enum date_mode dmode,
8289
const char *encoding);

0 commit comments

Comments
 (0)