The eleventh batch
[git/gitster.git] / pretty.c
blob098378720a44c71cdec69f1b3cb4485806357e2a
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "commit.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "hash.h"
9 #include "hex.h"
10 #include "utf8.h"
11 #include "diff.h"
12 #include "pager.h"
13 #include "revision.h"
14 #include "string-list.h"
15 #include "mailmap.h"
16 #include "log-tree.h"
17 #include "notes.h"
18 #include "color.h"
19 #include "reflog-walk.h"
20 #include "gpg-interface.h"
21 #include "trailer.h"
22 #include "run-command.h"
23 #include "object-name.h"
26 * The limit for formatting directives, which enable the caller to append
27 * arbitrarily many bytes to the formatted buffer. This includes padding
28 * and wrapping formatters.
30 #define FORMATTING_LIMIT (16 * 1024)
32 static char *user_format;
33 static struct cmt_fmt_map {
34 const char *name;
35 enum cmit_fmt format;
36 int is_tformat;
37 int expand_tabs_in_log;
38 int is_alias;
39 enum date_mode_type default_date_mode_type;
40 const char *user_format;
41 } *commit_formats;
42 static size_t builtin_formats_len;
43 static size_t commit_formats_len;
44 static size_t commit_formats_alloc;
45 static struct cmt_fmt_map *find_commit_format(const char *sought);
47 int commit_format_is_empty(enum cmit_fmt fmt)
49 return fmt == CMIT_FMT_USERFORMAT && !*user_format;
52 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
54 free(user_format);
55 user_format = xstrdup(cp);
56 if (is_tformat)
57 rev->use_terminator = 1;
58 rev->commit_format = CMIT_FMT_USERFORMAT;
61 static int git_pretty_formats_config(const char *var, const char *value,
62 const struct config_context *ctx UNUSED,
63 void *cb UNUSED)
65 struct cmt_fmt_map *commit_format = NULL;
66 const char *name, *stripped;
67 char *fmt;
68 int i;
70 if (!skip_prefix(var, "pretty.", &name))
71 return 0;
73 for (i = 0; i < builtin_formats_len; i++) {
74 if (!strcmp(commit_formats[i].name, name))
75 return 0;
78 for (i = builtin_formats_len; i < commit_formats_len; i++) {
79 if (!strcmp(commit_formats[i].name, name)) {
80 commit_format = &commit_formats[i];
81 break;
85 if (!commit_format) {
86 ALLOC_GROW(commit_formats, commit_formats_len+1,
87 commit_formats_alloc);
88 commit_format = &commit_formats[commit_formats_len];
89 memset(commit_format, 0, sizeof(*commit_format));
90 commit_formats_len++;
93 free((char *)commit_format->name);
94 commit_format->name = xstrdup(name);
95 commit_format->format = CMIT_FMT_USERFORMAT;
96 if (git_config_string(&fmt, var, value))
97 return -1;
99 free((char *)commit_format->user_format);
100 if (skip_prefix(fmt, "format:", &stripped)) {
101 commit_format->is_tformat = 0;
102 commit_format->user_format = xstrdup(stripped);
103 free(fmt);
104 } else if (skip_prefix(fmt, "tformat:", &stripped)) {
105 commit_format->is_tformat = 1;
106 commit_format->user_format = xstrdup(stripped);
107 free(fmt);
108 } else if (strchr(fmt, '%')) {
109 commit_format->is_tformat = 1;
110 commit_format->user_format = fmt;
111 } else {
112 commit_format->is_alias = 1;
113 commit_format->user_format = fmt;
116 return 0;
119 static void setup_commit_formats(void)
121 struct cmt_fmt_map builtin_formats[] = {
122 { "raw", CMIT_FMT_RAW, 0, 0 },
123 { "medium", CMIT_FMT_MEDIUM, 0, 8 },
124 { "short", CMIT_FMT_SHORT, 0, 0 },
125 { "email", CMIT_FMT_EMAIL, 0, 0 },
126 { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
127 { "fuller", CMIT_FMT_FULLER, 0, 8 },
128 { "full", CMIT_FMT_FULL, 0, 8 },
129 { "oneline", CMIT_FMT_ONELINE, 1, 0 },
130 { "reference", CMIT_FMT_USERFORMAT, 1, 0,
131 0, DATE_SHORT, "%C(auto)%h (%s, %ad)" },
133 * Please update $__git_log_pretty_formats in
134 * git-completion.bash when you add new formats.
137 commit_formats_len = ARRAY_SIZE(builtin_formats);
138 builtin_formats_len = commit_formats_len;
139 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
140 COPY_ARRAY(commit_formats, builtin_formats,
141 ARRAY_SIZE(builtin_formats));
143 git_config(git_pretty_formats_config, NULL);
146 static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
147 const char *original,
148 int num_redirections)
150 struct cmt_fmt_map *found = NULL;
151 size_t found_match_len = 0;
152 int i;
154 if (num_redirections >= commit_formats_len)
155 die("invalid --pretty format: "
156 "'%s' references an alias which points to itself",
157 original);
159 for (i = 0; i < commit_formats_len; i++) {
160 size_t match_len;
162 if (!istarts_with(commit_formats[i].name, sought))
163 continue;
165 match_len = strlen(commit_formats[i].name);
166 if (found == NULL || found_match_len > match_len) {
167 found = &commit_formats[i];
168 found_match_len = match_len;
172 if (found && found->is_alias) {
173 found = find_commit_format_recursive(found->user_format,
174 original,
175 num_redirections+1);
178 return found;
181 static struct cmt_fmt_map *find_commit_format(const char *sought)
183 if (!commit_formats)
184 setup_commit_formats();
186 return find_commit_format_recursive(sought, sought, 0);
189 void get_commit_format(const char *arg, struct rev_info *rev)
191 struct cmt_fmt_map *commit_format;
193 rev->use_terminator = 0;
194 if (!arg) {
195 rev->commit_format = CMIT_FMT_DEFAULT;
196 return;
198 if (skip_prefix(arg, "format:", &arg)) {
199 save_user_format(rev, arg, 0);
200 return;
203 if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
204 save_user_format(rev, arg, 1);
205 return;
208 commit_format = find_commit_format(arg);
209 if (!commit_format)
210 die("invalid --pretty format: %s", arg);
212 rev->commit_format = commit_format->format;
213 rev->use_terminator = commit_format->is_tformat;
214 rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
215 if (!rev->date_mode_explicit && commit_format->default_date_mode_type)
216 rev->date_mode.type = commit_format->default_date_mode_type;
217 if (commit_format->format == CMIT_FMT_USERFORMAT) {
218 save_user_format(rev, commit_format->user_format,
219 commit_format->is_tformat);
224 * Generic support for pretty-printing the header
226 static int get_one_line(const char *msg)
228 int ret = 0;
230 for (;;) {
231 char c = *msg++;
232 if (!c)
233 break;
234 ret++;
235 if (c == '\n')
236 break;
238 return ret;
241 /* High bit set, or ISO-2022-INT */
242 static int non_ascii(int ch)
244 return !isascii(ch) || ch == '\033';
247 int has_non_ascii(const char *s)
249 int ch;
250 if (!s)
251 return 0;
252 while ((ch = *s++) != '\0') {
253 if (non_ascii(ch))
254 return 1;
256 return 0;
259 static int is_rfc822_special(char ch)
261 switch (ch) {
262 case '(':
263 case ')':
264 case '<':
265 case '>':
266 case '[':
267 case ']':
268 case ':':
269 case ';':
270 case '@':
271 case ',':
272 case '.':
273 case '"':
274 case '\\':
275 return 1;
276 default:
277 return 0;
281 static int needs_rfc822_quoting(const char *s, int len)
283 int i;
284 for (i = 0; i < len; i++)
285 if (is_rfc822_special(s[i]))
286 return 1;
287 return 0;
290 static int last_line_length(struct strbuf *sb)
292 int i;
294 /* How many bytes are already used on the last line? */
295 for (i = sb->len - 1; i >= 0; i--)
296 if (sb->buf[i] == '\n')
297 break;
298 return sb->len - (i + 1);
301 static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
303 int i;
305 /* just a guess, we may have to also backslash-quote */
306 strbuf_grow(out, len + 2);
308 strbuf_addch(out, '"');
309 for (i = 0; i < len; i++) {
310 switch (s[i]) {
311 case '"':
312 case '\\':
313 strbuf_addch(out, '\\');
314 /* fall through */
315 default:
316 strbuf_addch(out, s[i]);
319 strbuf_addch(out, '"');
322 enum rfc2047_type {
323 RFC2047_SUBJECT,
324 RFC2047_ADDRESS
327 static int is_rfc2047_special(char ch, enum rfc2047_type type)
330 * rfc2047, section 4.2:
332 * 8-bit values which correspond to printable ASCII characters other
333 * than "=", "?", and "_" (underscore), MAY be represented as those
334 * characters. (But see section 5 for restrictions.) In
335 * particular, SPACE and TAB MUST NOT be represented as themselves
336 * within encoded words.
340 * rule out non-ASCII characters and non-printable characters (the
341 * non-ASCII check should be redundant as isprint() is not localized
342 * and only knows about ASCII, but be defensive about that)
344 if (non_ascii(ch) || !isprint(ch))
345 return 1;
348 * rule out special printable characters (' ' should be the only
349 * whitespace character considered printable, but be defensive and use
350 * isspace())
352 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
353 return 1;
356 * rfc2047, section 5.3:
358 * As a replacement for a 'word' entity within a 'phrase', for example,
359 * one that precedes an address in a From, To, or Cc header. The ABNF
360 * definition for 'phrase' from RFC 822 thus becomes:
362 * phrase = 1*( encoded-word / word )
364 * In this case the set of characters that may be used in a "Q"-encoded
365 * 'encoded-word' is restricted to: <upper and lower case ASCII
366 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
367 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
368 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
369 * 'special' by 'linear-white-space'.
372 if (type != RFC2047_ADDRESS)
373 return 0;
375 /* '=' and '_' are special cases and have been checked above */
376 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
379 static int needs_rfc2047_encoding(const char *line, int len)
381 int i;
383 for (i = 0; i < len; i++) {
384 int ch = line[i];
385 if (non_ascii(ch) || ch == '\n')
386 return 1;
387 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
388 return 1;
391 return 0;
394 static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
395 const char *encoding, enum rfc2047_type type)
397 static const int max_encoded_length = 76; /* per rfc2047 */
398 int i;
399 int line_len = last_line_length(sb);
401 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
402 strbuf_addf(sb, "=?%s?q?", encoding);
403 line_len += strlen(encoding) + 5; /* 5 for =??q? */
405 while (len) {
407 * RFC 2047, section 5 (3):
409 * Each 'encoded-word' MUST represent an integral number of
410 * characters. A multi-octet character may not be split across
411 * adjacent 'encoded- word's.
413 const unsigned char *p = (const unsigned char *)line;
414 int chrlen = mbs_chrlen(&line, &len, encoding);
415 int is_special = (chrlen > 1) || is_rfc2047_special(*p, type);
417 /* "=%02X" * chrlen, or the byte itself */
418 const char *encoded_fmt = is_special ? "=%02X" : "%c";
419 int encoded_len = is_special ? 3 * chrlen : 1;
422 * According to RFC 2047, we could encode the special character
423 * ' ' (space) with '_' (underscore) for readability. But many
424 * programs do not understand this and just leave the
425 * underscore in place. Thus, we do nothing special here, which
426 * causes ' ' to be encoded as '=20', avoiding this problem.
429 if (line_len + encoded_len + 2 > max_encoded_length) {
430 /* It won't fit with trailing "?=" --- break the line */
431 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
432 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
435 for (i = 0; i < chrlen; i++)
436 strbuf_addf(sb, encoded_fmt, p[i]);
437 line_len += encoded_len;
439 strbuf_addstr(sb, "?=");
442 const char *show_ident_date(const struct ident_split *ident,
443 struct date_mode mode)
445 timestamp_t date = 0;
446 long tz = 0;
448 if (ident->date_begin && ident->date_end)
449 date = parse_timestamp(ident->date_begin, NULL, 10);
450 if (date_overflows(date))
451 date = 0;
452 else {
453 if (ident->tz_begin && ident->tz_end)
454 tz = strtol(ident->tz_begin, NULL, 10);
455 if (tz >= INT_MAX || tz <= INT_MIN)
456 tz = 0;
458 return show_date(date, tz, mode);
461 static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
462 const char *buf, size_t buflen)
464 strbuf_addstr(sb, color);
465 strbuf_add(sb, buf, buflen);
466 if (*color)
467 strbuf_addstr(sb, GIT_COLOR_RESET);
470 static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
471 const char *line, size_t linelen,
472 int color, enum grep_context ctx,
473 enum grep_header_field field)
475 const char *buf, *eol, *line_color, *match_color;
476 regmatch_t match;
477 int eflags = 0;
479 buf = line;
480 eol = buf + linelen;
482 if (!opt || !want_color(color) || opt->invert)
483 goto end;
485 line_color = opt->colors[GREP_COLOR_SELECTED];
486 match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
488 while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) {
489 if (match.rm_so == match.rm_eo)
490 break;
492 strbuf_add_with_color(sb, line_color, buf, match.rm_so);
493 strbuf_add_with_color(sb, match_color, buf + match.rm_so,
494 match.rm_eo - match.rm_so);
495 buf += match.rm_eo;
496 eflags = REG_NOTBOL;
499 if (eflags)
500 strbuf_add_with_color(sb, line_color, buf, eol - buf);
501 else {
502 end:
503 strbuf_add(sb, buf, eol - buf);
507 static int use_in_body_from(const struct pretty_print_context *pp,
508 const struct ident_split *ident)
510 if (pp->rev && pp->rev->force_in_body_from)
511 return 1;
512 if (ident_cmp(pp->from_ident, ident))
513 return 1;
514 return 0;
517 void pp_user_info(struct pretty_print_context *pp,
518 const char *what, struct strbuf *sb,
519 const char *line, const char *encoding)
521 struct ident_split ident;
522 char *line_end;
523 const char *mailbuf, *namebuf;
524 size_t namelen, maillen;
525 int max_length = 78; /* per rfc2822 */
527 if (pp->fmt == CMIT_FMT_ONELINE)
528 return;
530 line_end = strchrnul(line, '\n');
531 if (split_ident_line(&ident, line, line_end - line))
532 return;
534 mailbuf = ident.mail_begin;
535 maillen = ident.mail_end - ident.mail_begin;
536 namebuf = ident.name_begin;
537 namelen = ident.name_end - ident.name_begin;
539 if (pp->mailmap)
540 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
542 if (cmit_fmt_is_mail(pp->fmt)) {
543 if (pp->from_ident && use_in_body_from(pp, &ident)) {
544 struct strbuf buf = STRBUF_INIT;
546 strbuf_addstr(&buf, "From: ");
547 strbuf_add(&buf, namebuf, namelen);
548 strbuf_addstr(&buf, " <");
549 strbuf_add(&buf, mailbuf, maillen);
550 strbuf_addstr(&buf, ">\n");
551 string_list_append(&pp->in_body_headers,
552 strbuf_detach(&buf, NULL));
554 mailbuf = pp->from_ident->mail_begin;
555 maillen = pp->from_ident->mail_end - mailbuf;
556 namebuf = pp->from_ident->name_begin;
557 namelen = pp->from_ident->name_end - namebuf;
560 strbuf_addstr(sb, "From: ");
561 if (pp->encode_email_headers &&
562 needs_rfc2047_encoding(namebuf, namelen)) {
563 add_rfc2047(sb, namebuf, namelen,
564 encoding, RFC2047_ADDRESS);
565 max_length = 76; /* per rfc2047 */
566 } else if (needs_rfc822_quoting(namebuf, namelen)) {
567 struct strbuf quoted = STRBUF_INIT;
568 add_rfc822_quoted(&quoted, namebuf, namelen);
569 strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
570 -6, 1, max_length);
571 strbuf_release(&quoted);
572 } else {
573 strbuf_add_wrapped_bytes(sb, namebuf, namelen,
574 -6, 1, max_length);
577 if (max_length <
578 last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
579 strbuf_addch(sb, '\n');
580 strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
581 } else {
582 struct strbuf id = STRBUF_INIT;
583 enum grep_header_field field = GREP_HEADER_FIELD_MAX;
584 struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
586 if (!strcmp(what, "Author"))
587 field = GREP_HEADER_AUTHOR;
588 else if (!strcmp(what, "Commit"))
589 field = GREP_HEADER_COMMITTER;
591 strbuf_addf(sb, "%s: ", what);
592 if (pp->fmt == CMIT_FMT_FULLER)
593 strbuf_addchars(sb, ' ', 4);
595 strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
596 (int)maillen, mailbuf);
598 append_line_with_color(sb, opt, id.buf, id.len, pp->color,
599 GREP_CONTEXT_HEAD, field);
600 strbuf_addch(sb, '\n');
601 strbuf_release(&id);
604 switch (pp->fmt) {
605 case CMIT_FMT_MEDIUM:
606 strbuf_addf(sb, "Date: %s\n",
607 show_ident_date(&ident, pp->date_mode));
608 break;
609 case CMIT_FMT_EMAIL:
610 case CMIT_FMT_MBOXRD:
611 strbuf_addf(sb, "Date: %s\n",
612 show_ident_date(&ident, DATE_MODE(RFC2822)));
613 break;
614 case CMIT_FMT_FULLER:
615 strbuf_addf(sb, "%sDate: %s\n", what,
616 show_ident_date(&ident, pp->date_mode));
617 break;
618 default:
619 /* notin' */
620 break;
624 static int is_blank_line(const char *line, int *len_p)
626 int len = *len_p;
627 while (len && isspace(line[len - 1]))
628 len--;
629 *len_p = len;
630 return !len;
633 const char *skip_blank_lines(const char *msg)
635 for (;;) {
636 int linelen = get_one_line(msg);
637 int ll = linelen;
638 if (!linelen)
639 break;
640 if (!is_blank_line(msg, &ll))
641 break;
642 msg += linelen;
644 return msg;
647 static void add_merge_info(const struct pretty_print_context *pp,
648 struct strbuf *sb, const struct commit *commit)
650 struct commit_list *parent = commit->parents;
652 if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
653 !parent || !parent->next)
654 return;
656 strbuf_addstr(sb, "Merge:");
658 while (parent) {
659 struct object_id *oidp = &parent->item->object.oid;
660 strbuf_addch(sb, ' ');
661 if (pp->abbrev)
662 strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
663 else
664 strbuf_addstr(sb, oid_to_hex(oidp));
665 parent = parent->next;
667 strbuf_addch(sb, '\n');
670 static char *get_header(const char *msg, const char *key)
672 size_t len;
673 const char *v = find_commit_header(msg, key, &len);
674 return v ? xmemdupz(v, len) : NULL;
677 static char *replace_encoding_header(char *buf, const char *encoding)
679 struct strbuf tmp = STRBUF_INIT;
680 size_t start, len;
681 char *cp = buf;
683 /* guess if there is an encoding header before a \n\n */
684 while (!starts_with(cp, "encoding ")) {
685 cp = strchr(cp, '\n');
686 if (!cp || *++cp == '\n')
687 return buf;
689 start = cp - buf;
690 cp = strchr(cp, '\n');
691 if (!cp)
692 return buf; /* should not happen but be defensive */
693 len = cp + 1 - (buf + start);
695 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
696 if (is_encoding_utf8(encoding)) {
697 /* we have re-coded to UTF-8; drop the header */
698 strbuf_remove(&tmp, start, len);
699 } else {
700 /* just replaces XXXX in 'encoding XXXX\n' */
701 strbuf_splice(&tmp, start + strlen("encoding "),
702 len - strlen("encoding \n"),
703 encoding, strlen(encoding));
705 return strbuf_detach(&tmp, NULL);
708 const char *repo_logmsg_reencode(struct repository *r,
709 const struct commit *commit,
710 char **commit_encoding,
711 const char *output_encoding)
713 static const char *utf8 = "UTF-8";
714 const char *use_encoding;
715 char *encoding;
716 const char *msg = repo_get_commit_buffer(r, commit, NULL);
717 char *out;
719 if (!output_encoding || !*output_encoding) {
720 if (commit_encoding)
721 *commit_encoding = get_header(msg, "encoding");
722 return msg;
724 encoding = get_header(msg, "encoding");
725 if (commit_encoding)
726 *commit_encoding = encoding;
727 use_encoding = encoding ? encoding : utf8;
728 if (same_encoding(use_encoding, output_encoding)) {
730 * No encoding work to be done. If we have no encoding header
731 * at all, then there's nothing to do, and we can return the
732 * message verbatim (whether newly allocated or not).
734 if (!encoding)
735 return msg;
738 * Otherwise, we still want to munge the encoding header in the
739 * result, which will be done by modifying the buffer. If we
740 * are using a fresh copy, we can reuse it. But if we are using
741 * the cached copy from repo_get_commit_buffer, we need to duplicate it
742 * to avoid munging the cached copy.
744 if (msg == get_cached_commit_buffer(r, commit, NULL))
745 out = xstrdup(msg);
746 else
747 out = (char *)msg;
749 else {
751 * There's actual encoding work to do. Do the reencoding, which
752 * still leaves the header to be replaced in the next step. At
753 * this point, we are done with msg. If we allocated a fresh
754 * copy, we can free it.
756 out = reencode_string(msg, output_encoding, use_encoding);
757 if (out)
758 repo_unuse_commit_buffer(r, commit, msg);
762 * This replacement actually consumes the buffer we hand it, so we do
763 * not have to worry about freeing the old "out" here.
765 if (out)
766 out = replace_encoding_header(out, output_encoding);
768 if (!commit_encoding)
769 free(encoding);
771 * If the re-encoding failed, out might be NULL here; in that
772 * case we just return the commit message verbatim.
774 return out ? out : msg;
777 static int mailmap_name(const char **email, size_t *email_len,
778 const char **name, size_t *name_len)
780 static struct string_list *mail_map;
781 if (!mail_map) {
782 CALLOC_ARRAY(mail_map, 1);
783 read_mailmap(mail_map);
785 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
788 static size_t format_person_part(struct strbuf *sb, char part,
789 const char *msg, int len,
790 struct date_mode dmode)
792 /* currently all placeholders have same length */
793 const int placeholder_len = 2;
794 struct ident_split s;
795 const char *name, *mail;
796 size_t maillen, namelen;
798 if (split_ident_line(&s, msg, len) < 0)
799 goto skip;
801 name = s.name_begin;
802 namelen = s.name_end - s.name_begin;
803 mail = s.mail_begin;
804 maillen = s.mail_end - s.mail_begin;
806 if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */
807 mailmap_name(&mail, &maillen, &name, &namelen);
808 if (part == 'n' || part == 'N') { /* name */
809 strbuf_add(sb, name, namelen);
810 return placeholder_len;
812 if (part == 'e' || part == 'E') { /* email */
813 strbuf_add(sb, mail, maillen);
814 return placeholder_len;
816 if (part == 'l' || part == 'L') { /* local-part */
817 const char *at = memchr(mail, '@', maillen);
818 if (at)
819 maillen = at - mail;
820 strbuf_add(sb, mail, maillen);
821 return placeholder_len;
824 if (!s.date_begin)
825 goto skip;
827 if (part == 't') { /* date, UNIX timestamp */
828 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
829 return placeholder_len;
832 switch (part) {
833 case 'd': /* date */
834 strbuf_addstr(sb, show_ident_date(&s, dmode));
835 return placeholder_len;
836 case 'D': /* date, RFC2822 style */
837 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
838 return placeholder_len;
839 case 'r': /* date, relative */
840 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
841 return placeholder_len;
842 case 'i': /* date, ISO 8601-like */
843 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
844 return placeholder_len;
845 case 'I': /* date, ISO 8601 strict */
846 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
847 return placeholder_len;
848 case 'h': /* date, human */
849 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(HUMAN)));
850 return placeholder_len;
851 case 's':
852 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT)));
853 return placeholder_len;
856 skip:
858 * reading from either a bogus commit, or a reflog entry with
859 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
860 * to compute a valid return value.
862 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
863 || part == 'D' || part == 'r' || part == 'i')
864 return placeholder_len;
866 return 0; /* unknown placeholder */
869 struct chunk {
870 size_t off;
871 size_t len;
874 enum flush_type {
875 no_flush,
876 flush_right,
877 flush_left,
878 flush_left_and_steal,
879 flush_both
882 enum trunc_type {
883 trunc_none,
884 trunc_left,
885 trunc_middle,
886 trunc_right
889 struct format_commit_context {
890 struct repository *repository;
891 const struct commit *commit;
892 const struct pretty_print_context *pretty_ctx;
893 unsigned commit_header_parsed:1;
894 unsigned commit_message_parsed:1;
895 struct signature_check signature_check;
896 enum flush_type flush_type;
897 enum trunc_type truncate;
898 const char *message;
899 char *commit_encoding;
900 size_t width, indent1, indent2;
901 int auto_color;
902 int padding;
904 /* These offsets are relative to the start of the commit message. */
905 struct chunk author;
906 struct chunk committer;
907 size_t message_off;
908 size_t subject_off;
909 size_t body_off;
911 /* The following ones are relative to the result struct strbuf. */
912 size_t wrap_start;
915 static void parse_commit_header(struct format_commit_context *context)
917 const char *msg = context->message;
918 int i;
920 for (i = 0; msg[i]; i++) {
921 const char *name;
922 int eol;
923 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
924 ; /* do nothing */
926 if (i == eol) {
927 break;
928 } else if (skip_prefix(msg + i, "author ", &name)) {
929 context->author.off = name - msg;
930 context->author.len = msg + eol - name;
931 } else if (skip_prefix(msg + i, "committer ", &name)) {
932 context->committer.off = name - msg;
933 context->committer.len = msg + eol - name;
935 i = eol;
937 context->message_off = i;
938 context->commit_header_parsed = 1;
941 static int istitlechar(char c)
943 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
944 (c >= '0' && c <= '9') || c == '.' || c == '_';
947 void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
949 size_t trimlen;
950 size_t start_len = sb->len;
951 int space = 2;
952 int i;
954 for (i = 0; i < len; i++) {
955 if (istitlechar(msg[i])) {
956 if (space == 1)
957 strbuf_addch(sb, '-');
958 space = 0;
959 strbuf_addch(sb, msg[i]);
960 if (msg[i] == '.')
961 while (msg[i+1] == '.')
962 i++;
963 } else
964 space |= 1;
967 /* trim any trailing '.' or '-' characters */
968 trimlen = 0;
969 while (sb->len - trimlen > start_len &&
970 (sb->buf[sb->len - 1 - trimlen] == '.'
971 || sb->buf[sb->len - 1 - trimlen] == '-'))
972 trimlen++;
973 strbuf_remove(sb, sb->len - trimlen, trimlen);
976 const char *format_subject(struct strbuf *sb, const char *msg,
977 const char *line_separator)
979 int first = 1;
981 for (;;) {
982 const char *line = msg;
983 int linelen = get_one_line(line);
985 msg += linelen;
986 if (!linelen || is_blank_line(line, &linelen))
987 break;
989 if (!sb)
990 continue;
991 strbuf_grow(sb, linelen + 2);
992 if (!first)
993 strbuf_addstr(sb, line_separator);
994 strbuf_add(sb, line, linelen);
995 first = 0;
997 return msg;
1000 static void parse_commit_message(struct format_commit_context *c)
1002 const char *msg = c->message + c->message_off;
1003 const char *start = c->message;
1005 msg = skip_blank_lines(msg);
1006 c->subject_off = msg - start;
1008 msg = format_subject(NULL, msg, NULL);
1009 msg = skip_blank_lines(msg);
1010 c->body_off = msg - start;
1012 c->commit_message_parsed = 1;
1015 static void strbuf_wrap(struct strbuf *sb, size_t pos,
1016 size_t width, size_t indent1, size_t indent2)
1018 struct strbuf tmp = STRBUF_INIT;
1020 if (pos)
1021 strbuf_add(&tmp, sb->buf, pos);
1022 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
1023 cast_size_t_to_int(indent1),
1024 cast_size_t_to_int(indent2),
1025 cast_size_t_to_int(width));
1026 strbuf_swap(&tmp, sb);
1027 strbuf_release(&tmp);
1030 static void rewrap_message_tail(struct strbuf *sb,
1031 struct format_commit_context *c,
1032 size_t new_width, size_t new_indent1,
1033 size_t new_indent2)
1035 if (c->width == new_width && c->indent1 == new_indent1 &&
1036 c->indent2 == new_indent2)
1037 return;
1038 if (c->wrap_start < sb->len)
1039 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
1040 c->wrap_start = sb->len;
1041 c->width = new_width;
1042 c->indent1 = new_indent1;
1043 c->indent2 = new_indent2;
1046 static int format_reflog_person(struct strbuf *sb,
1047 char part,
1048 struct reflog_walk_info *log,
1049 struct date_mode dmode)
1051 const char *ident;
1053 if (!log)
1054 return 2;
1056 ident = get_reflog_ident(log);
1057 if (!ident)
1058 return 2;
1060 return format_person_part(sb, part, ident, strlen(ident), dmode);
1063 static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
1064 const char *placeholder,
1065 struct format_commit_context *c)
1067 const char *rest = placeholder;
1068 const char *basic_color = NULL;
1070 if (placeholder[1] == '(') {
1071 const char *begin = placeholder + 2;
1072 const char *end = strchr(begin, ')');
1073 char color[COLOR_MAXLEN];
1075 if (!end)
1076 return 0;
1078 if (skip_prefix(begin, "auto,", &begin)) {
1079 if (!want_color(c->pretty_ctx->color))
1080 return end - placeholder + 1;
1081 } else if (skip_prefix(begin, "always,", &begin)) {
1082 /* nothing to do; we do not respect want_color at all */
1083 } else {
1084 /* the default is the same as "auto" */
1085 if (!want_color(c->pretty_ctx->color))
1086 return end - placeholder + 1;
1089 if (color_parse_mem(begin, end - begin, color) < 0)
1090 die(_("unable to parse --pretty format"));
1091 strbuf_addstr(sb, color);
1092 return end - placeholder + 1;
1096 * We handle things like "%C(red)" above; for historical reasons, there
1097 * are a few colors that can be specified without parentheses (and
1098 * they cannot support things like "auto" or "always" at all).
1100 if (skip_prefix(placeholder + 1, "red", &rest))
1101 basic_color = GIT_COLOR_RED;
1102 else if (skip_prefix(placeholder + 1, "green", &rest))
1103 basic_color = GIT_COLOR_GREEN;
1104 else if (skip_prefix(placeholder + 1, "blue", &rest))
1105 basic_color = GIT_COLOR_BLUE;
1106 else if (skip_prefix(placeholder + 1, "reset", &rest))
1107 basic_color = GIT_COLOR_RESET;
1109 if (basic_color && want_color(c->pretty_ctx->color))
1110 strbuf_addstr(sb, basic_color);
1112 return rest - placeholder;
1115 static size_t parse_padding_placeholder(const char *placeholder,
1116 struct format_commit_context *c)
1118 const char *ch = placeholder;
1119 enum flush_type flush_type;
1120 int to_column = 0;
1122 switch (*ch++) {
1123 case '<':
1124 flush_type = flush_right;
1125 break;
1126 case '>':
1127 if (*ch == '<') {
1128 flush_type = flush_both;
1129 ch++;
1130 } else if (*ch == '>') {
1131 flush_type = flush_left_and_steal;
1132 ch++;
1133 } else
1134 flush_type = flush_left;
1135 break;
1136 default:
1137 return 0;
1140 /* the next value means "wide enough to that column" */
1141 if (*ch == '|') {
1142 to_column = 1;
1143 ch++;
1146 if (*ch == '(') {
1147 const char *start = ch + 1;
1148 const char *end = start + strcspn(start, ",)");
1149 char *next;
1150 int width;
1151 if (!*end || end == start)
1152 return 0;
1153 width = strtol(start, &next, 10);
1156 * We need to limit the amount of padding, or otherwise this
1157 * would allow the user to pad the buffer by arbitrarily many
1158 * bytes and thus cause resource exhaustion.
1160 if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT)
1161 return 0;
1163 if (next == start || width == 0)
1164 return 0;
1165 if (width < 0) {
1166 if (to_column)
1167 width += term_columns();
1168 if (width < 0)
1169 return 0;
1171 c->padding = to_column ? -width : width;
1172 c->flush_type = flush_type;
1174 if (*end == ',') {
1175 start = end + 1;
1176 end = strchr(start, ')');
1177 if (!end || end == start)
1178 return 0;
1179 if (starts_with(start, "trunc)"))
1180 c->truncate = trunc_right;
1181 else if (starts_with(start, "ltrunc)"))
1182 c->truncate = trunc_left;
1183 else if (starts_with(start, "mtrunc)"))
1184 c->truncate = trunc_middle;
1185 else
1186 return 0;
1187 } else
1188 c->truncate = trunc_none;
1190 return end - placeholder + 1;
1192 return 0;
1195 static int match_placeholder_arg_value(const char *to_parse, const char *candidate,
1196 const char **end, const char **valuestart,
1197 size_t *valuelen)
1199 const char *p;
1201 if (!(skip_prefix(to_parse, candidate, &p)))
1202 return 0;
1203 if (valuestart) {
1204 if (*p == '=') {
1205 *valuestart = p + 1;
1206 *valuelen = strcspn(*valuestart, ",)");
1207 p = *valuestart + *valuelen;
1208 } else {
1209 if (*p != ',' && *p != ')')
1210 return 0;
1211 *valuestart = NULL;
1212 *valuelen = 0;
1215 if (*p == ',') {
1216 *end = p + 1;
1217 return 1;
1219 if (*p == ')') {
1220 *end = p;
1221 return 1;
1223 return 0;
1226 static int match_placeholder_bool_arg(const char *to_parse, const char *candidate,
1227 const char **end, int *val)
1229 const char *argval;
1230 char *strval;
1231 size_t arglen;
1232 int v;
1234 if (!match_placeholder_arg_value(to_parse, candidate, end, &argval, &arglen))
1235 return 0;
1237 if (!argval) {
1238 *val = 1;
1239 return 1;
1242 strval = xstrndup(argval, arglen);
1243 v = git_parse_maybe_bool(strval);
1244 free(strval);
1246 if (v == -1)
1247 return 0;
1249 *val = v;
1251 return 1;
1254 static int format_trailer_match_cb(const struct strbuf *key, void *ud)
1256 const struct string_list *list = ud;
1257 const struct string_list_item *item;
1259 for_each_string_list_item (item, list) {
1260 if (key->len == (uintptr_t)item->util &&
1261 !strncasecmp(item->string, key->buf, key->len))
1262 return 1;
1264 return 0;
1267 static struct strbuf *expand_string_arg(struct strbuf *sb,
1268 const char *argval, size_t arglen)
1270 char *fmt = xstrndup(argval, arglen);
1271 const char *format = fmt;
1273 strbuf_reset(sb);
1274 while (strbuf_expand_step(sb, &format)) {
1275 size_t len;
1277 if (skip_prefix(format, "%", &format))
1278 strbuf_addch(sb, '%');
1279 else if ((len = strbuf_expand_literal(sb, format)))
1280 format += len;
1281 else
1282 strbuf_addch(sb, '%');
1284 free(fmt);
1285 return sb;
1288 int format_set_trailers_options(struct process_trailer_options *opts,
1289 struct string_list *filter_list,
1290 struct strbuf *sepbuf,
1291 struct strbuf *kvsepbuf,
1292 const char **arg,
1293 char **invalid_arg)
1295 for (;;) {
1296 const char *argval;
1297 size_t arglen;
1299 if (**arg == ')')
1300 break;
1302 if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) {
1303 uintptr_t len = arglen;
1305 if (!argval)
1306 return -1;
1308 if (len && argval[len - 1] == ':')
1309 len--;
1310 string_list_append(filter_list, argval)->util = (char *)len;
1312 opts->filter = format_trailer_match_cb;
1313 opts->filter_data = filter_list;
1314 opts->only_trailers = 1;
1315 } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
1316 opts->separator = expand_string_arg(sepbuf, argval, arglen);
1317 } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
1318 opts->key_value_separator = expand_string_arg(kvsepbuf, argval, arglen);
1319 } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
1320 !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
1321 !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
1322 !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) {
1323 if (invalid_arg) {
1324 size_t len = strcspn(*arg, ",)");
1325 *invalid_arg = xstrndup(*arg, len);
1327 return -1;
1330 return 0;
1333 static size_t parse_describe_args(const char *start, struct strvec *args)
1335 struct {
1336 const char *name;
1337 enum {
1338 DESCRIBE_ARG_BOOL,
1339 DESCRIBE_ARG_INTEGER,
1340 DESCRIBE_ARG_STRING,
1341 } type;
1342 } option[] = {
1343 { "tags", DESCRIBE_ARG_BOOL},
1344 { "abbrev", DESCRIBE_ARG_INTEGER },
1345 { "exclude", DESCRIBE_ARG_STRING },
1346 { "match", DESCRIBE_ARG_STRING },
1348 const char *arg = start;
1350 for (;;) {
1351 int found = 0;
1352 const char *argval;
1353 size_t arglen = 0;
1354 int optval = 0;
1355 int i;
1357 for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
1358 switch (option[i].type) {
1359 case DESCRIBE_ARG_BOOL:
1360 if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) {
1361 if (optval)
1362 strvec_pushf(args, "--%s", option[i].name);
1363 else
1364 strvec_pushf(args, "--no-%s", option[i].name);
1365 found = 1;
1367 break;
1368 case DESCRIBE_ARG_INTEGER:
1369 if (match_placeholder_arg_value(arg, option[i].name, &arg,
1370 &argval, &arglen)) {
1371 char *endptr;
1372 if (!arglen)
1373 return 0;
1374 strtol(argval, &endptr, 10);
1375 if (endptr - argval != arglen)
1376 return 0;
1377 strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1378 found = 1;
1380 break;
1381 case DESCRIBE_ARG_STRING:
1382 if (match_placeholder_arg_value(arg, option[i].name, &arg,
1383 &argval, &arglen)) {
1384 if (!arglen)
1385 return 0;
1386 strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1387 found = 1;
1389 break;
1392 if (!found)
1393 break;
1396 return arg - start;
1400 static int parse_decoration_option(const char **arg,
1401 const char *name,
1402 char **opt)
1404 const char *argval;
1405 size_t arglen;
1407 if (match_placeholder_arg_value(*arg, name, arg, &argval, &arglen)) {
1408 struct strbuf sb = STRBUF_INIT;
1410 expand_string_arg(&sb, argval, arglen);
1411 *opt = strbuf_detach(&sb, NULL);
1412 return 1;
1414 return 0;
1417 static void parse_decoration_options(const char **arg,
1418 struct decoration_options *opts)
1420 while (parse_decoration_option(arg, "prefix", &opts->prefix) ||
1421 parse_decoration_option(arg, "suffix", &opts->suffix) ||
1422 parse_decoration_option(arg, "separator", &opts->separator) ||
1423 parse_decoration_option(arg, "pointer", &opts->pointer) ||
1424 parse_decoration_option(arg, "tag", &opts->tag))
1428 static void free_decoration_options(const struct decoration_options *opts)
1430 free(opts->prefix);
1431 free(opts->suffix);
1432 free(opts->separator);
1433 free(opts->pointer);
1434 free(opts->tag);
1437 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1438 const char *placeholder,
1439 void *context)
1441 struct format_commit_context *c = context;
1442 const struct commit *commit = c->commit;
1443 const char *msg = c->message;
1444 struct commit_list *p;
1445 const char *arg, *eol;
1446 size_t res;
1447 char **slot;
1449 /* these are independent of the commit */
1450 res = strbuf_expand_literal(sb, placeholder);
1451 if (res)
1452 return res;
1454 switch (placeholder[0]) {
1455 case 'C':
1456 if (starts_with(placeholder + 1, "(auto)")) {
1457 c->auto_color = want_color(c->pretty_ctx->color);
1458 if (c->auto_color && sb->len)
1459 strbuf_addstr(sb, GIT_COLOR_RESET);
1460 return 7; /* consumed 7 bytes, "C(auto)" */
1461 } else {
1462 int ret = parse_color(sb, placeholder, c);
1463 if (ret)
1464 c->auto_color = 0;
1466 * Otherwise, we decided to treat %C<unknown>
1467 * as a literal string, and the previous
1468 * %C(auto) is still valid.
1470 return ret;
1472 case 'w':
1473 if (placeholder[1] == '(') {
1474 unsigned long width = 0, indent1 = 0, indent2 = 0;
1475 char *next;
1476 const char *start = placeholder + 2;
1477 const char *end = strchr(start, ')');
1478 if (!end)
1479 return 0;
1480 if (end > start) {
1481 width = strtoul(start, &next, 10);
1482 if (*next == ',') {
1483 indent1 = strtoul(next + 1, &next, 10);
1484 if (*next == ',') {
1485 indent2 = strtoul(next + 1,
1486 &next, 10);
1489 if (*next != ')')
1490 return 0;
1494 * We need to limit the format here as it allows the
1495 * user to prepend arbitrarily many bytes to the buffer
1496 * when rewrapping.
1498 if (width > FORMATTING_LIMIT ||
1499 indent1 > FORMATTING_LIMIT ||
1500 indent2 > FORMATTING_LIMIT)
1501 return 0;
1502 rewrap_message_tail(sb, c, width, indent1, indent2);
1503 return end - placeholder + 1;
1504 } else
1505 return 0;
1507 case '<':
1508 case '>':
1509 return parse_padding_placeholder(placeholder, c);
1512 if (skip_prefix(placeholder, "(describe", &arg)) {
1513 struct child_process cmd = CHILD_PROCESS_INIT;
1514 struct strbuf out = STRBUF_INIT;
1515 struct strbuf err = STRBUF_INIT;
1516 struct pretty_print_describe_status *describe_status;
1518 describe_status = c->pretty_ctx->describe_status;
1519 if (describe_status) {
1520 if (!describe_status->max_invocations)
1521 return 0;
1522 describe_status->max_invocations--;
1525 cmd.git_cmd = 1;
1526 strvec_push(&cmd.args, "describe");
1528 if (*arg == ':') {
1529 arg++;
1530 arg += parse_describe_args(arg, &cmd.args);
1533 if (*arg != ')') {
1534 child_process_clear(&cmd);
1535 return 0;
1538 strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1539 pipe_command(&cmd, NULL, 0, &out, 0, &err, 0);
1540 strbuf_rtrim(&out);
1541 strbuf_addbuf(sb, &out);
1542 strbuf_release(&out);
1543 strbuf_release(&err);
1544 return arg - placeholder + 1;
1547 /* these depend on the commit */
1548 if (!commit->object.parsed)
1549 parse_object(the_repository, &commit->object.oid);
1551 switch (placeholder[0]) {
1552 case 'H': /* commit hash */
1553 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1554 strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
1555 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1556 return 1;
1557 case 'h': /* abbreviated commit hash */
1558 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1559 strbuf_add_unique_abbrev(sb, &commit->object.oid,
1560 c->pretty_ctx->abbrev);
1561 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1562 return 1;
1563 case 'T': /* tree hash */
1564 strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit)));
1565 return 1;
1566 case 't': /* abbreviated tree hash */
1567 strbuf_add_unique_abbrev(sb,
1568 get_commit_tree_oid(commit),
1569 c->pretty_ctx->abbrev);
1570 return 1;
1571 case 'P': /* parent hashes */
1572 for (p = commit->parents; p; p = p->next) {
1573 if (p != commit->parents)
1574 strbuf_addch(sb, ' ');
1575 strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
1577 return 1;
1578 case 'p': /* abbreviated parent hashes */
1579 for (p = commit->parents; p; p = p->next) {
1580 if (p != commit->parents)
1581 strbuf_addch(sb, ' ');
1582 strbuf_add_unique_abbrev(sb, &p->item->object.oid,
1583 c->pretty_ctx->abbrev);
1585 return 1;
1586 case 'm': /* left/right/bottom */
1587 strbuf_addstr(sb, get_revision_mark(NULL, commit));
1588 return 1;
1589 case 'd':
1590 format_decorations(sb, commit, c->auto_color, NULL);
1591 return 1;
1592 case 'D':
1594 const struct decoration_options opts = {
1595 .prefix = (char *) "",
1596 .suffix = (char *) "",
1599 format_decorations(sb, commit, c->auto_color, &opts);
1600 return 1;
1602 case 'S': /* tag/branch like --source */
1603 if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
1604 return 0;
1605 slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);
1606 if (!(slot && *slot))
1607 return 0;
1608 strbuf_addstr(sb, *slot);
1609 return 1;
1610 case 'g': /* reflog info */
1611 switch(placeholder[1]) {
1612 case 'd': /* reflog selector */
1613 case 'D':
1614 if (c->pretty_ctx->reflog_info)
1615 get_reflog_selector(sb,
1616 c->pretty_ctx->reflog_info,
1617 c->pretty_ctx->date_mode,
1618 c->pretty_ctx->date_mode_explicit,
1619 (placeholder[1] == 'd'));
1620 return 2;
1621 case 's': /* reflog message */
1622 if (c->pretty_ctx->reflog_info)
1623 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1624 return 2;
1625 case 'n':
1626 case 'N':
1627 case 'e':
1628 case 'E':
1629 return format_reflog_person(sb,
1630 placeholder[1],
1631 c->pretty_ctx->reflog_info,
1632 c->pretty_ctx->date_mode);
1634 return 0; /* unknown %g placeholder */
1635 case 'N':
1636 if (c->pretty_ctx->notes_message) {
1637 strbuf_addstr(sb, c->pretty_ctx->notes_message);
1638 return 1;
1640 return 0;
1643 if (placeholder[0] == 'G') {
1644 if (!c->signature_check.result)
1645 check_commit_signature(c->commit, &(c->signature_check));
1646 switch (placeholder[1]) {
1647 case 'G':
1648 if (c->signature_check.output)
1649 strbuf_addstr(sb, c->signature_check.output);
1650 break;
1651 case '?':
1652 switch (c->signature_check.result) {
1653 case 'G':
1654 switch (c->signature_check.trust_level) {
1655 case TRUST_UNDEFINED:
1656 case TRUST_NEVER:
1657 strbuf_addch(sb, 'U');
1658 break;
1659 default:
1660 strbuf_addch(sb, 'G');
1661 break;
1663 break;
1664 case 'B':
1665 case 'E':
1666 case 'N':
1667 case 'X':
1668 case 'Y':
1669 case 'R':
1670 strbuf_addch(sb, c->signature_check.result);
1672 break;
1673 case 'S':
1674 if (c->signature_check.signer)
1675 strbuf_addstr(sb, c->signature_check.signer);
1676 break;
1677 case 'K':
1678 if (c->signature_check.key)
1679 strbuf_addstr(sb, c->signature_check.key);
1680 break;
1681 case 'F':
1682 if (c->signature_check.fingerprint)
1683 strbuf_addstr(sb, c->signature_check.fingerprint);
1684 break;
1685 case 'P':
1686 if (c->signature_check.primary_key_fingerprint)
1687 strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
1688 break;
1689 case 'T':
1690 strbuf_addstr(sb, gpg_trust_level_to_str(c->signature_check.trust_level));
1691 break;
1692 default:
1693 return 0;
1695 return 2;
1698 if (skip_prefix(placeholder, "(decorate", &arg)) {
1699 struct decoration_options opts = { NULL };
1700 size_t ret = 0;
1702 if (*arg == ':') {
1703 arg++;
1704 parse_decoration_options(&arg, &opts);
1706 if (*arg == ')') {
1707 format_decorations(sb, commit, c->auto_color, &opts);
1708 ret = arg - placeholder + 1;
1711 free_decoration_options(&opts);
1712 return ret;
1715 /* For the rest we have to parse the commit header. */
1716 if (!c->commit_header_parsed) {
1717 msg = c->message =
1718 repo_logmsg_reencode(c->repository, commit,
1719 &c->commit_encoding, "UTF-8");
1720 parse_commit_header(c);
1723 switch (placeholder[0]) {
1724 case 'a': /* author ... */
1725 return format_person_part(sb, placeholder[1],
1726 msg + c->author.off, c->author.len,
1727 c->pretty_ctx->date_mode);
1728 case 'c': /* committer ... */
1729 return format_person_part(sb, placeholder[1],
1730 msg + c->committer.off, c->committer.len,
1731 c->pretty_ctx->date_mode);
1732 case 'e': /* encoding */
1733 if (c->commit_encoding)
1734 strbuf_addstr(sb, c->commit_encoding);
1735 return 1;
1736 case 'B': /* raw body */
1737 /* message_off is always left at the initial newline */
1738 strbuf_addstr(sb, msg + c->message_off + 1);
1739 return 1;
1742 /* Now we need to parse the commit message. */
1743 if (!c->commit_message_parsed)
1744 parse_commit_message(c);
1746 switch (placeholder[0]) {
1747 case 's': /* subject */
1748 format_subject(sb, msg + c->subject_off, " ");
1749 return 1;
1750 case 'f': /* sanitized subject */
1751 eol = strchrnul(msg + c->subject_off, '\n');
1752 format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
1753 return 1;
1754 case 'b': /* body */
1755 strbuf_addstr(sb, msg + c->body_off);
1756 return 1;
1759 if (skip_prefix(placeholder, "(trailers", &arg)) {
1760 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1761 struct string_list filter_list = STRING_LIST_INIT_NODUP;
1762 struct strbuf sepbuf = STRBUF_INIT;
1763 struct strbuf kvsepbuf = STRBUF_INIT;
1764 size_t ret = 0;
1766 opts.no_divider = 1;
1768 if (*arg == ':') {
1769 arg++;
1770 if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL))
1771 goto trailer_out;
1773 if (*arg == ')') {
1774 format_trailers_from_commit(&opts, msg + c->subject_off, sb);
1775 ret = arg - placeholder + 1;
1777 trailer_out:
1778 string_list_clear(&filter_list, 0);
1779 strbuf_release(&kvsepbuf);
1780 strbuf_release(&sepbuf);
1781 return ret;
1784 return 0; /* unknown placeholder */
1787 static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1788 const char *placeholder,
1789 struct format_commit_context *c)
1791 struct strbuf local_sb = STRBUF_INIT;
1792 size_t total_consumed = 0;
1793 int len, padding = c->padding;
1795 if (padding < 0) {
1796 const char *start = strrchr(sb->buf, '\n');
1797 int occupied;
1798 if (!start)
1799 start = sb->buf;
1800 occupied = utf8_strnwidth(start, strlen(start), 1);
1801 occupied += c->pretty_ctx->graph_width;
1802 padding = (-padding) - occupied;
1804 while (1) {
1805 int modifier = *placeholder == 'C';
1806 size_t consumed = format_commit_one(&local_sb, placeholder, c);
1807 total_consumed += consumed;
1809 if (!modifier)
1810 break;
1812 placeholder += consumed;
1813 if (*placeholder != '%')
1814 break;
1815 placeholder++;
1816 total_consumed++;
1818 len = utf8_strnwidth(local_sb.buf, local_sb.len, 1);
1820 if (c->flush_type == flush_left_and_steal) {
1821 const char *ch = sb->buf + sb->len - 1;
1822 while (len > padding && ch > sb->buf) {
1823 const char *p;
1824 if (*ch == ' ') {
1825 ch--;
1826 padding++;
1827 continue;
1829 /* check for trailing ansi sequences */
1830 if (*ch != 'm')
1831 break;
1832 p = ch - 1;
1833 while (p > sb->buf && ch - p < 10 && *p != '\033')
1834 p--;
1835 if (*p != '\033' ||
1836 ch + 1 - p != display_mode_esc_sequence_len(p))
1837 break;
1839 * got a good ansi sequence, put it back to
1840 * local_sb as we're cutting sb
1842 strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1843 ch = p - 1;
1845 strbuf_setlen(sb, ch + 1 - sb->buf);
1846 c->flush_type = flush_left;
1849 if (len > padding) {
1850 switch (c->truncate) {
1851 case trunc_left:
1852 strbuf_utf8_replace(&local_sb,
1853 0, len - (padding - 2),
1854 "..");
1855 break;
1856 case trunc_middle:
1857 strbuf_utf8_replace(&local_sb,
1858 padding / 2 - 1,
1859 len - (padding - 2),
1860 "..");
1861 break;
1862 case trunc_right:
1863 strbuf_utf8_replace(&local_sb,
1864 padding - 2, len - (padding - 2),
1865 "..");
1866 break;
1867 case trunc_none:
1868 break;
1870 strbuf_addbuf(sb, &local_sb);
1871 } else {
1872 size_t sb_len = sb->len, offset = 0;
1873 if (c->flush_type == flush_left)
1874 offset = padding - len;
1875 else if (c->flush_type == flush_both)
1876 offset = (padding - len) / 2;
1878 * we calculate padding in columns, now
1879 * convert it back to chars
1881 padding = padding - len + local_sb.len;
1882 strbuf_addchars(sb, ' ', padding);
1883 memcpy(sb->buf + sb_len + offset, local_sb.buf,
1884 local_sb.len);
1886 strbuf_release(&local_sb);
1887 c->flush_type = no_flush;
1888 return total_consumed;
1891 static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1892 const char *placeholder,
1893 struct format_commit_context *context)
1895 size_t consumed, orig_len;
1896 enum {
1897 NO_MAGIC,
1898 ADD_LF_BEFORE_NON_EMPTY,
1899 DEL_LF_BEFORE_EMPTY,
1900 ADD_SP_BEFORE_NON_EMPTY
1901 } magic = NO_MAGIC;
1903 switch (placeholder[0]) {
1904 case '-':
1905 magic = DEL_LF_BEFORE_EMPTY;
1906 break;
1907 case '+':
1908 magic = ADD_LF_BEFORE_NON_EMPTY;
1909 break;
1910 case ' ':
1911 magic = ADD_SP_BEFORE_NON_EMPTY;
1912 break;
1913 default:
1914 break;
1916 if (magic != NO_MAGIC) {
1917 placeholder++;
1919 switch (placeholder[0]) {
1920 case 'w':
1922 * `%+w()` cannot ever expand to a non-empty string,
1923 * and it potentially changes the layout of preceding
1924 * contents. We're thus not able to handle the magic in
1925 * this combination and refuse the pattern.
1927 return 0;
1931 orig_len = sb->len;
1932 if (context->flush_type == no_flush)
1933 consumed = format_commit_one(sb, placeholder, context);
1934 else
1935 consumed = format_and_pad_commit(sb, placeholder, context);
1936 if (magic == NO_MAGIC)
1937 return consumed;
1939 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1940 while (sb->len && sb->buf[sb->len - 1] == '\n')
1941 strbuf_setlen(sb, sb->len - 1);
1942 } else if (orig_len != sb->len) {
1943 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1944 strbuf_insertstr(sb, orig_len, "\n");
1945 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1946 strbuf_insertstr(sb, orig_len, " ");
1948 return consumed + 1;
1951 void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1953 if (!fmt) {
1954 if (!user_format)
1955 return;
1956 fmt = user_format;
1958 while ((fmt = strchr(fmt, '%'))) {
1959 fmt++;
1960 if (skip_prefix(fmt, "%", &fmt))
1961 continue;
1963 if (*fmt == '+' || *fmt == '-' || *fmt == ' ')
1964 fmt++;
1966 switch (*fmt) {
1967 case 'N':
1968 w->notes = 1;
1969 break;
1970 case 'S':
1971 w->source = 1;
1972 break;
1973 case 'd':
1974 case 'D':
1975 w->decorate = 1;
1976 break;
1977 case '(':
1978 if (starts_with(fmt + 1, "decorate"))
1979 w->decorate = 1;
1980 break;
1985 void repo_format_commit_message(struct repository *r,
1986 const struct commit *commit,
1987 const char *format, struct strbuf *sb,
1988 const struct pretty_print_context *pretty_ctx)
1990 struct format_commit_context context = {
1991 .repository = r,
1992 .commit = commit,
1993 .pretty_ctx = pretty_ctx,
1994 .wrap_start = sb->len
1996 const char *output_enc = pretty_ctx->output_encoding;
1997 const char *utf8 = "UTF-8";
1999 while (strbuf_expand_step(sb, &format)) {
2000 size_t len;
2002 if (skip_prefix(format, "%", &format))
2003 strbuf_addch(sb, '%');
2004 else if ((len = format_commit_item(sb, format, &context)))
2005 format += len;
2006 else
2007 strbuf_addch(sb, '%');
2009 rewrap_message_tail(sb, &context, 0, 0, 0);
2012 * Convert output to an actual output encoding; note that
2013 * format_commit_item() will always use UTF-8, so we don't
2014 * have to bother if that's what the output wants.
2016 if (output_enc) {
2017 if (same_encoding(utf8, output_enc))
2018 output_enc = NULL;
2019 } else {
2020 if (context.commit_encoding &&
2021 !same_encoding(context.commit_encoding, utf8))
2022 output_enc = context.commit_encoding;
2025 if (output_enc) {
2026 size_t outsz;
2027 char *out = reencode_string_len(sb->buf, sb->len,
2028 output_enc, utf8, &outsz);
2029 if (out)
2030 strbuf_attach(sb, out, outsz, outsz + 1);
2033 free(context.commit_encoding);
2034 repo_unuse_commit_buffer(r, commit, context.message);
2035 signature_check_clear(&context.signature_check);
2038 static void pp_header(struct pretty_print_context *pp,
2039 const char *encoding,
2040 const struct commit *commit,
2041 const char **msg_p,
2042 struct strbuf *sb)
2044 int parents_shown = 0;
2046 for (;;) {
2047 const char *name, *line = *msg_p;
2048 int linelen = get_one_line(*msg_p);
2050 if (!linelen)
2051 return;
2052 *msg_p += linelen;
2054 if (linelen == 1)
2055 /* End of header */
2056 return;
2058 if (pp->fmt == CMIT_FMT_RAW) {
2059 strbuf_add(sb, line, linelen);
2060 continue;
2063 if (starts_with(line, "parent ")) {
2064 if (linelen != the_hash_algo->hexsz + 8)
2065 die("bad parent line in commit");
2066 continue;
2069 if (!parents_shown) {
2070 unsigned num = commit_list_count(commit->parents);
2071 /* with enough slop */
2072 strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20);
2073 add_merge_info(pp, sb, commit);
2074 parents_shown = 1;
2078 * MEDIUM == DEFAULT shows only author with dates.
2079 * FULL shows both authors but not dates.
2080 * FULLER shows both authors and dates.
2082 if (skip_prefix(line, "author ", &name)) {
2083 strbuf_grow(sb, linelen + 80);
2084 pp_user_info(pp, "Author", sb, name, encoding);
2086 if (skip_prefix(line, "committer ", &name) &&
2087 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
2088 strbuf_grow(sb, linelen + 80);
2089 pp_user_info(pp, "Commit", sb, name, encoding);
2094 void pp_email_subject(struct pretty_print_context *pp,
2095 const char **msg_p,
2096 struct strbuf *sb,
2097 const char *encoding,
2098 int need_8bit_cte)
2100 static const int max_length = 78; /* per rfc2047 */
2101 struct strbuf title;
2103 strbuf_init(&title, 80);
2104 *msg_p = format_subject(&title, *msg_p,
2105 pp->preserve_subject ? "\n" : " ");
2107 strbuf_grow(sb, title.len + 1024);
2108 fmt_output_email_subject(sb, pp->rev);
2109 if (pp->encode_email_headers &&
2110 needs_rfc2047_encoding(title.buf, title.len))
2111 add_rfc2047(sb, title.buf, title.len,
2112 encoding, RFC2047_SUBJECT);
2113 else
2114 strbuf_add_wrapped_bytes(sb, title.buf, title.len,
2115 -last_line_length(sb), 1, max_length);
2116 strbuf_addch(sb, '\n');
2118 if (need_8bit_cte == 0) {
2119 int i;
2120 for (i = 0; i < pp->in_body_headers.nr; i++) {
2121 if (has_non_ascii(pp->in_body_headers.items[i].string)) {
2122 need_8bit_cte = 1;
2123 break;
2128 if (need_8bit_cte > 0) {
2129 const char *header_fmt =
2130 "MIME-Version: 1.0\n"
2131 "Content-Type: text/plain; charset=%s\n"
2132 "Content-Transfer-Encoding: 8bit\n";
2133 strbuf_addf(sb, header_fmt, encoding);
2135 if (pp->after_subject) {
2136 strbuf_addstr(sb, pp->after_subject);
2139 strbuf_addch(sb, '\n');
2141 if (pp->in_body_headers.nr) {
2142 int i;
2143 for (i = 0; i < pp->in_body_headers.nr; i++) {
2144 strbuf_addstr(sb, pp->in_body_headers.items[i].string);
2145 free(pp->in_body_headers.items[i].string);
2147 string_list_clear(&pp->in_body_headers, 0);
2148 strbuf_addch(sb, '\n');
2151 strbuf_release(&title);
2154 static int pp_utf8_width(const char *start, const char *end)
2156 int width = 0;
2157 size_t remain = end - start;
2159 while (remain) {
2160 int n = utf8_width(&start, &remain);
2161 if (n < 0 || !start)
2162 return -1;
2163 width += n;
2165 return width;
2168 static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt,
2169 int color, int tabwidth, const char *line,
2170 int linelen)
2172 const char *tab;
2174 while ((tab = memchr(line, '\t', linelen)) != NULL) {
2175 int width = pp_utf8_width(line, tab);
2178 * If it wasn't well-formed utf8, or it
2179 * had characters with badly defined
2180 * width (control characters etc), just
2181 * give up on trying to align things.
2183 if (width < 0)
2184 break;
2186 /* Output the data .. */
2187 append_line_with_color(sb, opt, line, tab - line, color,
2188 GREP_CONTEXT_BODY,
2189 GREP_HEADER_FIELD_MAX);
2191 /* .. and the de-tabified tab */
2192 strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
2194 /* Skip over the printed part .. */
2195 linelen -= tab + 1 - line;
2196 line = tab + 1;
2200 * Print out everything after the last tab without
2201 * worrying about width - there's nothing more to
2202 * align.
2204 append_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY,
2205 GREP_HEADER_FIELD_MAX);
2209 * pp_handle_indent() prints out the indentation, and
2210 * the whole line (without the final newline), after
2211 * de-tabifying.
2213 static void pp_handle_indent(struct pretty_print_context *pp,
2214 struct strbuf *sb, int indent,
2215 const char *line, int linelen)
2217 struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
2219 strbuf_addchars(sb, ' ', indent);
2220 if (pp->expand_tabs_in_log)
2221 strbuf_add_tabexpand(sb, opt, pp->color, pp->expand_tabs_in_log,
2222 line, linelen);
2223 else
2224 append_line_with_color(sb, opt, line, linelen, pp->color,
2225 GREP_CONTEXT_BODY,
2226 GREP_HEADER_FIELD_MAX);
2229 static int is_mboxrd_from(const char *line, int len)
2232 * a line matching /^From $/ here would only have len == 4
2233 * at this point because is_empty_line would've trimmed all
2234 * trailing space
2236 return len > 4 && starts_with(line + strspn(line, ">"), "From ");
2239 void pp_remainder(struct pretty_print_context *pp,
2240 const char **msg_p,
2241 struct strbuf *sb,
2242 int indent)
2244 struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
2245 int first = 1;
2247 for (;;) {
2248 const char *line = *msg_p;
2249 int linelen = get_one_line(line);
2250 *msg_p += linelen;
2252 if (!linelen)
2253 break;
2255 if (is_blank_line(line, &linelen)) {
2256 if (first)
2257 continue;
2258 if (pp->fmt == CMIT_FMT_SHORT)
2259 break;
2261 first = 0;
2263 strbuf_grow(sb, linelen + indent + 20);
2264 if (indent)
2265 pp_handle_indent(pp, sb, indent, line, linelen);
2266 else if (pp->expand_tabs_in_log)
2267 strbuf_add_tabexpand(sb, opt, pp->color,
2268 pp->expand_tabs_in_log, line,
2269 linelen);
2270 else {
2271 if (pp->fmt == CMIT_FMT_MBOXRD &&
2272 is_mboxrd_from(line, linelen))
2273 strbuf_addch(sb, '>');
2275 append_line_with_color(sb, opt, line, linelen,
2276 pp->color, GREP_CONTEXT_BODY,
2277 GREP_HEADER_FIELD_MAX);
2279 strbuf_addch(sb, '\n');
2283 void pretty_print_commit(struct pretty_print_context *pp,
2284 const struct commit *commit,
2285 struct strbuf *sb)
2287 unsigned long beginning_of_body;
2288 int indent = 4;
2289 const char *msg;
2290 const char *reencoded;
2291 const char *encoding;
2292 int need_8bit_cte = pp->need_8bit_cte;
2294 if (pp->fmt == CMIT_FMT_USERFORMAT) {
2295 repo_format_commit_message(the_repository, commit,
2296 user_format, sb, pp);
2297 return;
2300 encoding = get_log_output_encoding();
2301 msg = reencoded = repo_logmsg_reencode(the_repository, commit, NULL,
2302 encoding);
2304 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
2305 indent = 0;
2308 * We need to check and emit Content-type: to mark it
2309 * as 8-bit if we haven't done so.
2311 if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
2312 int i, ch, in_body;
2314 for (in_body = i = 0; (ch = msg[i]); i++) {
2315 if (!in_body) {
2316 /* author could be non 7-bit ASCII but
2317 * the log may be so; skip over the
2318 * header part first.
2320 if (ch == '\n' && msg[i+1] == '\n')
2321 in_body = 1;
2323 else if (non_ascii(ch)) {
2324 need_8bit_cte = 1;
2325 break;
2330 pp_header(pp, encoding, commit, &msg, sb);
2331 if (pp->fmt != CMIT_FMT_ONELINE && !cmit_fmt_is_mail(pp->fmt)) {
2332 strbuf_addch(sb, '\n');
2335 /* Skip excess blank lines at the beginning of body, if any... */
2336 msg = skip_blank_lines(msg);
2338 /* These formats treat the title line specially. */
2339 if (pp->fmt == CMIT_FMT_ONELINE) {
2340 msg = format_subject(sb, msg, " ");
2341 strbuf_addch(sb, '\n');
2342 } else if (cmit_fmt_is_mail(pp->fmt))
2343 pp_email_subject(pp, &msg, sb, encoding, need_8bit_cte);
2345 beginning_of_body = sb->len;
2346 if (pp->fmt != CMIT_FMT_ONELINE)
2347 pp_remainder(pp, &msg, sb, indent);
2348 strbuf_rtrim(sb);
2350 /* Make sure there is an EOLN for the non-oneline case */
2351 if (pp->fmt != CMIT_FMT_ONELINE)
2352 strbuf_addch(sb, '\n');
2355 * The caller may append additional body text in e-mail
2356 * format. Make sure we did not strip the blank line
2357 * between the header and the body.
2359 if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
2360 strbuf_addch(sb, '\n');
2362 repo_unuse_commit_buffer(the_repository, commit, reencoded);
2365 void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
2366 struct strbuf *sb)
2368 struct pretty_print_context pp = {0};
2369 pp.fmt = fmt;
2370 pretty_print_commit(&pp, commit, sb);