7 #include "string-list.h"
12 #include "reflog-walk.h"
13 #include "gpg-interface.h"
16 static char *user_format;
17 static struct cmt_fmt_map {
21 int expand_tabs_in_log;
23 enum date_mode_type default_date_mode_type;
24 const char *user_format;
26 static size_t builtin_formats_len;
27 static size_t commit_formats_len;
28 static size_t commit_formats_alloc;
29 static struct cmt_fmt_map *find_commit_format(const char *sought);
31 int commit_format_is_empty(enum cmit_fmt fmt)
33 return fmt == CMIT_FMT_USERFORMAT && !*user_format;
36 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
39 user_format = xstrdup(cp);
41 rev->use_terminator = 1;
42 rev->commit_format = CMIT_FMT_USERFORMAT;
45 static int git_pretty_formats_config(const char *var, const char *value, void *cb)
47 struct cmt_fmt_map *commit_format = NULL;
52 if (!skip_prefix(var, "pretty.", &name))
55 for (i = 0; i < builtin_formats_len; i++) {
56 if (!strcmp(commit_formats[i].name, name))
60 for (i = builtin_formats_len; i < commit_formats_len; i++) {
61 if (!strcmp(commit_formats[i].name, name)) {
62 commit_format = &commit_formats[i];
68 ALLOC_GROW(commit_formats, commit_formats_len+1,
69 commit_formats_alloc);
70 commit_format = &commit_formats[commit_formats_len];
71 memset(commit_format, 0, sizeof(*commit_format));
75 commit_format->name = xstrdup(name);
76 commit_format->format = CMIT_FMT_USERFORMAT;
77 if (git_config_string(&fmt, var, value))
80 if (skip_prefix(fmt, "format:", &fmt))
81 commit_format->is_tformat = 0;
82 else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
83 commit_format->is_tformat = 1;
85 commit_format->is_alias = 1;
86 commit_format->user_format = fmt;
91 static void setup_commit_formats(void)
93 struct cmt_fmt_map builtin_formats[] = {
94 { "raw", CMIT_FMT_RAW, 0, 0 },
95 { "medium", CMIT_FMT_MEDIUM, 0, 8 },
96 { "short", CMIT_FMT_SHORT, 0, 0 },
97 { "email", CMIT_FMT_EMAIL, 0, 0 },
98 { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
99 { "fuller", CMIT_FMT_FULLER, 0, 8 },
100 { "full", CMIT_FMT_FULL, 0, 8 },
101 { "oneline", CMIT_FMT_ONELINE, 1, 0 },
102 { "reference", CMIT_FMT_USERFORMAT, 1, 0,
103 0, DATE_SHORT, "%C(auto)%h (%s, %ad)" },
105 * Please update $__git_log_pretty_formats in
106 * git-completion.bash when you add new formats.
109 commit_formats_len = ARRAY_SIZE(builtin_formats);
110 builtin_formats_len = commit_formats_len;
111 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
112 COPY_ARRAY(commit_formats, builtin_formats,
113 ARRAY_SIZE(builtin_formats));
115 git_config(git_pretty_formats_config, NULL);
118 static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
119 const char *original,
120 int num_redirections)
122 struct cmt_fmt_map *found = NULL;
123 size_t found_match_len = 0;
126 if (num_redirections >= commit_formats_len)
127 die("invalid --pretty format: "
128 "'%s' references an alias which points to itself",
131 for (i = 0; i < commit_formats_len; i++) {
134 if (!starts_with(commit_formats[i].name, sought))
137 match_len = strlen(commit_formats[i].name);
138 if (found == NULL || found_match_len > match_len) {
139 found = &commit_formats[i];
140 found_match_len = match_len;
144 if (found && found->is_alias) {
145 found = find_commit_format_recursive(found->user_format,
153 static struct cmt_fmt_map *find_commit_format(const char *sought)
156 setup_commit_formats();
158 return find_commit_format_recursive(sought, sought, 0);
161 void get_commit_format(const char *arg, struct rev_info *rev)
163 struct cmt_fmt_map *commit_format;
165 rev->use_terminator = 0;
167 rev->commit_format = CMIT_FMT_DEFAULT;
170 if (skip_prefix(arg, "format:", &arg)) {
171 save_user_format(rev, arg, 0);
175 if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
176 save_user_format(rev, arg, 1);
180 commit_format = find_commit_format(arg);
182 die("invalid --pretty format: %s", arg);
184 rev->commit_format = commit_format->format;
185 rev->use_terminator = commit_format->is_tformat;
186 rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
187 if (!rev->date_mode_explicit && commit_format->default_date_mode_type)
188 rev->date_mode.type = commit_format->default_date_mode_type;
189 if (commit_format->format == CMIT_FMT_USERFORMAT) {
190 save_user_format(rev, commit_format->user_format,
191 commit_format->is_tformat);
196 * Generic support for pretty-printing the header
198 static int get_one_line(const char *msg)
213 /* High bit set, or ISO-2022-INT */
214 static int non_ascii(int ch)
216 return !isascii(ch) || ch == '\033';
219 int has_non_ascii(const char *s)
224 while ((ch = *s++) != '\0') {
231 static int is_rfc822_special(char ch)
253 static int needs_rfc822_quoting(const char *s, int len)
256 for (i = 0; i < len; i++)
257 if (is_rfc822_special(s[i]))
262 static int last_line_length(struct strbuf *sb)
266 /* How many bytes are already used on the last line? */
267 for (i = sb->len - 1; i >= 0; i--)
268 if (sb->buf[i] == '\n')
270 return sb->len - (i + 1);
273 static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
277 /* just a guess, we may have to also backslash-quote */
278 strbuf_grow(out, len + 2);
280 strbuf_addch(out, '"');
281 for (i = 0; i < len; i++) {
285 strbuf_addch(out, '\\');
288 strbuf_addch(out, s[i]);
291 strbuf_addch(out, '"');
299 static int is_rfc2047_special(char ch, enum rfc2047_type type)
302 * rfc2047, section 4.2:
304 * 8-bit values which correspond to printable ASCII characters other
305 * than "=", "?", and "_" (underscore), MAY be represented as those
306 * characters. (But see section 5 for restrictions.) In
307 * particular, SPACE and TAB MUST NOT be represented as themselves
308 * within encoded words.
312 * rule out non-ASCII characters and non-printable characters (the
313 * non-ASCII check should be redundant as isprint() is not localized
314 * and only knows about ASCII, but be defensive about that)
316 if (non_ascii(ch) || !isprint(ch))
320 * rule out special printable characters (' ' should be the only
321 * whitespace character considered printable, but be defensive and use
324 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
328 * rfc2047, section 5.3:
330 * As a replacement for a 'word' entity within a 'phrase', for example,
331 * one that precedes an address in a From, To, or Cc header. The ABNF
332 * definition for 'phrase' from RFC 822 thus becomes:
334 * phrase = 1*( encoded-word / word )
336 * In this case the set of characters that may be used in a "Q"-encoded
337 * 'encoded-word' is restricted to: <upper and lower case ASCII
338 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
339 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
340 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
341 * 'special' by 'linear-white-space'.
344 if (type != RFC2047_ADDRESS)
347 /* '=' and '_' are special cases and have been checked above */
348 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
351 static int needs_rfc2047_encoding(const char *line, int len)
355 for (i = 0; i < len; i++) {
357 if (non_ascii(ch) || ch == '\n')
359 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
366 static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
367 const char *encoding, enum rfc2047_type type)
369 static const int max_encoded_length = 76; /* per rfc2047 */
371 int line_len = last_line_length(sb);
373 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
374 strbuf_addf(sb, "=?%s?q?", encoding);
375 line_len += strlen(encoding) + 5; /* 5 for =??q? */
379 * RFC 2047, section 5 (3):
381 * Each 'encoded-word' MUST represent an integral number of
382 * characters. A multi-octet character may not be split across
383 * adjacent 'encoded- word's.
385 const unsigned char *p = (const unsigned char *)line;
386 int chrlen = mbs_chrlen(&line, &len, encoding);
387 int is_special = (chrlen > 1) || is_rfc2047_special(*p, type);
389 /* "=%02X" * chrlen, or the byte itself */
390 const char *encoded_fmt = is_special ? "=%02X" : "%c";
391 int encoded_len = is_special ? 3 * chrlen : 1;
394 * According to RFC 2047, we could encode the special character
395 * ' ' (space) with '_' (underscore) for readability. But many
396 * programs do not understand this and just leave the
397 * underscore in place. Thus, we do nothing special here, which
398 * causes ' ' to be encoded as '=20', avoiding this problem.
401 if (line_len + encoded_len + 2 > max_encoded_length) {
402 /* It won't fit with trailing "?=" --- break the line */
403 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
404 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
407 for (i = 0; i < chrlen; i++)
408 strbuf_addf(sb, encoded_fmt, p[i]);
409 line_len += encoded_len;
411 strbuf_addstr(sb, "?=");
414 const char *show_ident_date(const struct ident_split *ident,
415 const struct date_mode *mode)
417 timestamp_t date = 0;
420 if (ident->date_begin && ident->date_end)
421 date = parse_timestamp(ident->date_begin, NULL, 10);
422 if (date_overflows(date))
425 if (ident->tz_begin && ident->tz_end)
426 tz = strtol(ident->tz_begin, NULL, 10);
427 if (tz >= INT_MAX || tz <= INT_MIN)
430 return show_date(date, tz, mode);
433 void pp_user_info(struct pretty_print_context *pp,
434 const char *what, struct strbuf *sb,
435 const char *line, const char *encoding)
437 struct ident_split ident;
439 const char *mailbuf, *namebuf;
440 size_t namelen, maillen;
441 int max_length = 78; /* per rfc2822 */
443 if (pp->fmt == CMIT_FMT_ONELINE)
446 line_end = strchrnul(line, '\n');
447 if (split_ident_line(&ident, line, line_end - line))
450 mailbuf = ident.mail_begin;
451 maillen = ident.mail_end - ident.mail_begin;
452 namebuf = ident.name_begin;
453 namelen = ident.name_end - ident.name_begin;
456 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
458 if (cmit_fmt_is_mail(pp->fmt)) {
459 if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
460 struct strbuf buf = STRBUF_INIT;
462 strbuf_addstr(&buf, "From: ");
463 strbuf_add(&buf, namebuf, namelen);
464 strbuf_addstr(&buf, " <");
465 strbuf_add(&buf, mailbuf, maillen);
466 strbuf_addstr(&buf, ">\n");
467 string_list_append(&pp->in_body_headers,
468 strbuf_detach(&buf, NULL));
470 mailbuf = pp->from_ident->mail_begin;
471 maillen = pp->from_ident->mail_end - mailbuf;
472 namebuf = pp->from_ident->name_begin;
473 namelen = pp->from_ident->name_end - namebuf;
476 strbuf_addstr(sb, "From: ");
477 if (pp->encode_email_headers &&
478 needs_rfc2047_encoding(namebuf, namelen)) {
479 add_rfc2047(sb, namebuf, namelen,
480 encoding, RFC2047_ADDRESS);
481 max_length = 76; /* per rfc2047 */
482 } else if (needs_rfc822_quoting(namebuf, namelen)) {
483 struct strbuf quoted = STRBUF_INIT;
484 add_rfc822_quoted("ed, namebuf, namelen);
485 strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
487 strbuf_release("ed);
489 strbuf_add_wrapped_bytes(sb, namebuf, namelen,
494 last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
495 strbuf_addch(sb, '\n');
496 strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
498 strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
499 (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
500 (int)namelen, namebuf, (int)maillen, mailbuf);
504 case CMIT_FMT_MEDIUM:
505 strbuf_addf(sb, "Date: %s\n",
506 show_ident_date(&ident, &pp->date_mode));
509 case CMIT_FMT_MBOXRD:
510 strbuf_addf(sb, "Date: %s\n",
511 show_ident_date(&ident, DATE_MODE(RFC2822)));
513 case CMIT_FMT_FULLER:
514 strbuf_addf(sb, "%sDate: %s\n", what,
515 show_ident_date(&ident, &pp->date_mode));
523 static int is_blank_line(const char *line, int *len_p)
526 while (len && isspace(line[len - 1]))
532 const char *skip_blank_lines(const char *msg)
535 int linelen = get_one_line(msg);
539 if (!is_blank_line(msg, &ll))
546 static void add_merge_info(const struct pretty_print_context *pp,
547 struct strbuf *sb, const struct commit *commit)
549 struct commit_list *parent = commit->parents;
551 if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
552 !parent || !parent->next)
555 strbuf_addstr(sb, "Merge:");
558 struct object_id *oidp = &parent->item->object.oid;
559 strbuf_addch(sb, ' ');
561 strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
563 strbuf_addstr(sb, oid_to_hex(oidp));
564 parent = parent->next;
566 strbuf_addch(sb, '\n');
569 static char *get_header(const char *msg, const char *key)
572 const char *v = find_commit_header(msg, key, &len);
573 return v ? xmemdupz(v, len) : NULL;
576 static char *replace_encoding_header(char *buf, const char *encoding)
578 struct strbuf tmp = STRBUF_INIT;
582 /* guess if there is an encoding header before a \n\n */
583 while (!starts_with(cp, "encoding ")) {
584 cp = strchr(cp, '\n');
585 if (!cp || *++cp == '\n')
589 cp = strchr(cp, '\n');
591 return buf; /* should not happen but be defensive */
592 len = cp + 1 - (buf + start);
594 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
595 if (is_encoding_utf8(encoding)) {
596 /* we have re-coded to UTF-8; drop the header */
597 strbuf_remove(&tmp, start, len);
599 /* just replaces XXXX in 'encoding XXXX\n' */
600 strbuf_splice(&tmp, start + strlen("encoding "),
601 len - strlen("encoding \n"),
602 encoding, strlen(encoding));
604 return strbuf_detach(&tmp, NULL);
607 const char *repo_logmsg_reencode(struct repository *r,
608 const struct commit *commit,
609 char **commit_encoding,
610 const char *output_encoding)
612 static const char *utf8 = "UTF-8";
613 const char *use_encoding;
615 const char *msg = repo_get_commit_buffer(r, commit, NULL);
618 if (!output_encoding || !*output_encoding) {
620 *commit_encoding = get_header(msg, "encoding");
623 encoding = get_header(msg, "encoding");
625 *commit_encoding = encoding;
626 use_encoding = encoding ? encoding : utf8;
627 if (same_encoding(use_encoding, output_encoding)) {
629 * No encoding work to be done. If we have no encoding header
630 * at all, then there's nothing to do, and we can return the
631 * message verbatim (whether newly allocated or not).
637 * Otherwise, we still want to munge the encoding header in the
638 * result, which will be done by modifying the buffer. If we
639 * are using a fresh copy, we can reuse it. But if we are using
640 * the cached copy from get_commit_buffer, we need to duplicate it
641 * to avoid munging the cached copy.
643 if (msg == get_cached_commit_buffer(r, commit, NULL))
650 * There's actual encoding work to do. Do the reencoding, which
651 * still leaves the header to be replaced in the next step. At
652 * this point, we are done with msg. If we allocated a fresh
653 * copy, we can free it.
655 out = reencode_string(msg, output_encoding, use_encoding);
657 repo_unuse_commit_buffer(r, commit, msg);
661 * This replacement actually consumes the buffer we hand it, so we do
662 * not have to worry about freeing the old "out" here.
665 out = replace_encoding_header(out, output_encoding);
667 if (!commit_encoding)
670 * If the re-encoding failed, out might be NULL here; in that
671 * case we just return the commit message verbatim.
673 return out ? out : msg;
676 static int mailmap_name(const char **email, size_t *email_len,
677 const char **name, size_t *name_len)
679 static struct string_list *mail_map;
681 mail_map = xcalloc(1, sizeof(*mail_map));
682 read_mailmap(mail_map);
684 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
687 static size_t format_person_part(struct strbuf *sb, char part,
688 const char *msg, int len,
689 const struct date_mode *dmode)
691 /* currently all placeholders have same length */
692 const int placeholder_len = 2;
693 struct ident_split s;
694 const char *name, *mail;
695 size_t maillen, namelen;
697 if (split_ident_line(&s, msg, len) < 0)
701 namelen = s.name_end - s.name_begin;
703 maillen = s.mail_end - s.mail_begin;
705 if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */
706 mailmap_name(&mail, &maillen, &name, &namelen);
707 if (part == 'n' || part == 'N') { /* name */
708 strbuf_add(sb, name, namelen);
709 return placeholder_len;
711 if (part == 'e' || part == 'E') { /* email */
712 strbuf_add(sb, mail, maillen);
713 return placeholder_len;
715 if (part == 'l' || part == 'L') { /* local-part */
716 const char *at = memchr(mail, '@', maillen);
719 strbuf_add(sb, mail, maillen);
720 return placeholder_len;
726 if (part == 't') { /* date, UNIX timestamp */
727 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
728 return placeholder_len;
733 strbuf_addstr(sb, show_ident_date(&s, dmode));
734 return placeholder_len;
735 case 'D': /* date, RFC2822 style */
736 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
737 return placeholder_len;
738 case 'r': /* date, relative */
739 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
740 return placeholder_len;
741 case 'i': /* date, ISO 8601-like */
742 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
743 return placeholder_len;
744 case 'I': /* date, ISO 8601 strict */
745 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
746 return placeholder_len;
748 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT)));
749 return placeholder_len;
754 * reading from either a bogus commit, or a reflog entry with
755 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
756 * to compute a valid return value.
758 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
759 || part == 'D' || part == 'r' || part == 'i')
760 return placeholder_len;
762 return 0; /* unknown placeholder */
774 flush_left_and_steal,
785 struct format_commit_context {
786 struct repository *repository;
787 const struct commit *commit;
788 const struct pretty_print_context *pretty_ctx;
789 unsigned commit_header_parsed:1;
790 unsigned commit_message_parsed:1;
791 struct signature_check signature_check;
792 enum flush_type flush_type;
793 enum trunc_type truncate;
795 char *commit_encoding;
796 size_t width, indent1, indent2;
800 /* These offsets are relative to the start of the commit message. */
802 struct chunk committer;
807 /* The following ones are relative to the result struct strbuf. */
811 static void parse_commit_header(struct format_commit_context *context)
813 const char *msg = context->message;
816 for (i = 0; msg[i]; i++) {
819 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
824 } else if (skip_prefix(msg + i, "author ", &name)) {
825 context->author.off = name - msg;
826 context->author.len = msg + eol - name;
827 } else if (skip_prefix(msg + i, "committer ", &name)) {
828 context->committer.off = name - msg;
829 context->committer.len = msg + eol - name;
833 context->message_off = i;
834 context->commit_header_parsed = 1;
837 static int istitlechar(char c)
839 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
840 (c >= '0' && c <= '9') || c == '.' || c == '_';
843 void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
846 size_t start_len = sb->len;
850 for (i = 0; i < len; i++) {
851 if (istitlechar(msg[i])) {
853 strbuf_addch(sb, '-');
855 strbuf_addch(sb, msg[i]);
857 while (msg[i+1] == '.')
863 /* trim any trailing '.' or '-' characters */
865 while (sb->len - trimlen > start_len &&
866 (sb->buf[sb->len - 1 - trimlen] == '.'
867 || sb->buf[sb->len - 1 - trimlen] == '-'))
869 strbuf_remove(sb, sb->len - trimlen, trimlen);
872 const char *format_subject(struct strbuf *sb, const char *msg,
873 const char *line_separator)
878 const char *line = msg;
879 int linelen = get_one_line(line);
882 if (!linelen || is_blank_line(line, &linelen))
887 strbuf_grow(sb, linelen + 2);
889 strbuf_addstr(sb, line_separator);
890 strbuf_add(sb, line, linelen);
896 static void parse_commit_message(struct format_commit_context *c)
898 const char *msg = c->message + c->message_off;
899 const char *start = c->message;
901 msg = skip_blank_lines(msg);
902 c->subject_off = msg - start;
904 msg = format_subject(NULL, msg, NULL);
905 msg = skip_blank_lines(msg);
906 c->body_off = msg - start;
908 c->commit_message_parsed = 1;
911 static void strbuf_wrap(struct strbuf *sb, size_t pos,
912 size_t width, size_t indent1, size_t indent2)
914 struct strbuf tmp = STRBUF_INIT;
917 strbuf_add(&tmp, sb->buf, pos);
918 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
919 (int) indent1, (int) indent2, (int) width);
920 strbuf_swap(&tmp, sb);
921 strbuf_release(&tmp);
924 static void rewrap_message_tail(struct strbuf *sb,
925 struct format_commit_context *c,
926 size_t new_width, size_t new_indent1,
929 if (c->width == new_width && c->indent1 == new_indent1 &&
930 c->indent2 == new_indent2)
932 if (c->wrap_start < sb->len)
933 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
934 c->wrap_start = sb->len;
935 c->width = new_width;
936 c->indent1 = new_indent1;
937 c->indent2 = new_indent2;
940 static int format_reflog_person(struct strbuf *sb,
942 struct reflog_walk_info *log,
943 const struct date_mode *dmode)
950 ident = get_reflog_ident(log);
954 return format_person_part(sb, part, ident, strlen(ident), dmode);
957 static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
958 const char *placeholder,
959 struct format_commit_context *c)
961 const char *rest = placeholder;
962 const char *basic_color = NULL;
964 if (placeholder[1] == '(') {
965 const char *begin = placeholder + 2;
966 const char *end = strchr(begin, ')');
967 char color[COLOR_MAXLEN];
972 if (skip_prefix(begin, "auto,", &begin)) {
973 if (!want_color(c->pretty_ctx->color))
974 return end - placeholder + 1;
975 } else if (skip_prefix(begin, "always,", &begin)) {
976 /* nothing to do; we do not respect want_color at all */
978 /* the default is the same as "auto" */
979 if (!want_color(c->pretty_ctx->color))
980 return end - placeholder + 1;
983 if (color_parse_mem(begin, end - begin, color) < 0)
984 die(_("unable to parse --pretty format"));
985 strbuf_addstr(sb, color);
986 return end - placeholder + 1;
990 * We handle things like "%C(red)" above; for historical reasons, there
991 * are a few colors that can be specified without parentheses (and
992 * they cannot support things like "auto" or "always" at all).
994 if (skip_prefix(placeholder + 1, "red", &rest))
995 basic_color = GIT_COLOR_RED;
996 else if (skip_prefix(placeholder + 1, "green", &rest))
997 basic_color = GIT_COLOR_GREEN;
998 else if (skip_prefix(placeholder + 1, "blue", &rest))
999 basic_color = GIT_COLOR_BLUE;
1000 else if (skip_prefix(placeholder + 1, "reset", &rest))
1001 basic_color = GIT_COLOR_RESET;
1003 if (basic_color && want_color(c->pretty_ctx->color))
1004 strbuf_addstr(sb, basic_color);
1006 return rest - placeholder;
1009 static size_t parse_padding_placeholder(const char *placeholder,
1010 struct format_commit_context *c)
1012 const char *ch = placeholder;
1013 enum flush_type flush_type;
1018 flush_type = flush_right;
1022 flush_type = flush_both;
1024 } else if (*ch == '>') {
1025 flush_type = flush_left_and_steal;
1028 flush_type = flush_left;
1034 /* the next value means "wide enough to that column" */
1041 const char *start = ch + 1;
1042 const char *end = start + strcspn(start, ",)");
1045 if (!end || end == start)
1047 width = strtol(start, &next, 10);
1048 if (next == start || width == 0)
1052 width += term_columns();
1056 c->padding = to_column ? -width : width;
1057 c->flush_type = flush_type;
1061 end = strchr(start, ')');
1062 if (!end || end == start)
1064 if (starts_with(start, "trunc)"))
1065 c->truncate = trunc_right;
1066 else if (starts_with(start, "ltrunc)"))
1067 c->truncate = trunc_left;
1068 else if (starts_with(start, "mtrunc)"))
1069 c->truncate = trunc_middle;
1073 c->truncate = trunc_none;
1075 return end - placeholder + 1;
1080 static int match_placeholder_arg_value(const char *to_parse, const char *candidate,
1081 const char **end, const char **valuestart,
1086 if (!(skip_prefix(to_parse, candidate, &p)))
1090 *valuestart = p + 1;
1091 *valuelen = strcspn(*valuestart, ",)");
1092 p = *valuestart + *valuelen;
1094 if (*p != ',' && *p != ')')
1111 static int match_placeholder_bool_arg(const char *to_parse, const char *candidate,
1112 const char **end, int *val)
1119 if (!match_placeholder_arg_value(to_parse, candidate, end, &argval, &arglen))
1127 strval = xstrndup(argval, arglen);
1128 v = git_parse_maybe_bool(strval);
1139 static int format_trailer_match_cb(const struct strbuf *key, void *ud)
1141 const struct string_list *list = ud;
1142 const struct string_list_item *item;
1144 for_each_string_list_item (item, list) {
1145 if (key->len == (uintptr_t)item->util &&
1146 !strncasecmp(item->string, key->buf, key->len))
1152 int format_set_trailers_options(struct process_trailer_options *opts,
1153 struct string_list *filter_list,
1154 struct strbuf *sepbuf,
1155 struct strbuf *kvsepbuf,
1166 if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) {
1167 uintptr_t len = arglen;
1172 if (len && argval[len - 1] == ':')
1174 string_list_append(filter_list, argval)->util = (char *)len;
1176 opts->filter = format_trailer_match_cb;
1177 opts->filter_data = filter_list;
1178 opts->only_trailers = 1;
1179 } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
1182 strbuf_reset(sepbuf);
1183 fmt = xstrndup(argval, arglen);
1184 strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL);
1186 opts->separator = sepbuf;
1187 } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
1190 strbuf_reset(kvsepbuf);
1191 fmt = xstrndup(argval, arglen);
1192 strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL);
1194 opts->key_value_separator = kvsepbuf;
1195 } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
1196 !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
1197 !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
1198 !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) {
1200 size_t len = strcspn(*arg, ",)");
1201 *invalid_arg = xstrndup(*arg, len);
1209 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1210 const char *placeholder,
1213 struct format_commit_context *c = context;
1214 const struct commit *commit = c->commit;
1215 const char *msg = c->message;
1216 struct commit_list *p;
1217 const char *arg, *eol;
1221 /* these are independent of the commit */
1222 res = strbuf_expand_literal_cb(sb, placeholder, NULL);
1226 switch (placeholder[0]) {
1228 if (starts_with(placeholder + 1, "(auto)")) {
1229 c->auto_color = want_color(c->pretty_ctx->color);
1230 if (c->auto_color && sb->len)
1231 strbuf_addstr(sb, GIT_COLOR_RESET);
1232 return 7; /* consumed 7 bytes, "C(auto)" */
1234 int ret = parse_color(sb, placeholder, c);
1238 * Otherwise, we decided to treat %C<unknown>
1239 * as a literal string, and the previous
1240 * %C(auto) is still valid.
1245 if (placeholder[1] == '(') {
1246 unsigned long width = 0, indent1 = 0, indent2 = 0;
1248 const char *start = placeholder + 2;
1249 const char *end = strchr(start, ')');
1253 width = strtoul(start, &next, 10);
1255 indent1 = strtoul(next + 1, &next, 10);
1257 indent2 = strtoul(next + 1,
1264 rewrap_message_tail(sb, c, width, indent1, indent2);
1265 return end - placeholder + 1;
1271 return parse_padding_placeholder(placeholder, c);
1274 /* these depend on the commit */
1275 if (!commit->object.parsed)
1276 parse_object(the_repository, &commit->object.oid);
1278 switch (placeholder[0]) {
1279 case 'H': /* commit hash */
1280 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1281 strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
1282 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1284 case 'h': /* abbreviated commit hash */
1285 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1286 strbuf_add_unique_abbrev(sb, &commit->object.oid,
1287 c->pretty_ctx->abbrev);
1288 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1290 case 'T': /* tree hash */
1291 strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit)));
1293 case 't': /* abbreviated tree hash */
1294 strbuf_add_unique_abbrev(sb,
1295 get_commit_tree_oid(commit),
1296 c->pretty_ctx->abbrev);
1298 case 'P': /* parent hashes */
1299 for (p = commit->parents; p; p = p->next) {
1300 if (p != commit->parents)
1301 strbuf_addch(sb, ' ');
1302 strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
1305 case 'p': /* abbreviated parent hashes */
1306 for (p = commit->parents; p; p = p->next) {
1307 if (p != commit->parents)
1308 strbuf_addch(sb, ' ');
1309 strbuf_add_unique_abbrev(sb, &p->item->object.oid,
1310 c->pretty_ctx->abbrev);
1313 case 'm': /* left/right/bottom */
1314 strbuf_addstr(sb, get_revision_mark(NULL, commit));
1317 format_decorations(sb, commit, c->auto_color);
1320 format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1322 case 'S': /* tag/branch like --source */
1323 if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
1325 slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);
1326 if (!(slot && *slot))
1328 strbuf_addstr(sb, *slot);
1330 case 'g': /* reflog info */
1331 switch(placeholder[1]) {
1332 case 'd': /* reflog selector */
1334 if (c->pretty_ctx->reflog_info)
1335 get_reflog_selector(sb,
1336 c->pretty_ctx->reflog_info,
1337 &c->pretty_ctx->date_mode,
1338 c->pretty_ctx->date_mode_explicit,
1339 (placeholder[1] == 'd'));
1341 case 's': /* reflog message */
1342 if (c->pretty_ctx->reflog_info)
1343 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1349 return format_reflog_person(sb,
1351 c->pretty_ctx->reflog_info,
1352 &c->pretty_ctx->date_mode);
1354 return 0; /* unknown %g placeholder */
1356 if (c->pretty_ctx->notes_message) {
1357 strbuf_addstr(sb, c->pretty_ctx->notes_message);
1363 if (placeholder[0] == 'G') {
1364 if (!c->signature_check.result)
1365 check_commit_signature(c->commit, &(c->signature_check));
1366 switch (placeholder[1]) {
1368 if (c->signature_check.gpg_output)
1369 strbuf_addstr(sb, c->signature_check.gpg_output);
1372 switch (c->signature_check.result) {
1374 switch (c->signature_check.trust_level) {
1375 case TRUST_UNDEFINED:
1377 strbuf_addch(sb, 'U');
1380 strbuf_addch(sb, 'G');
1390 strbuf_addch(sb, c->signature_check.result);
1394 if (c->signature_check.signer)
1395 strbuf_addstr(sb, c->signature_check.signer);
1398 if (c->signature_check.key)
1399 strbuf_addstr(sb, c->signature_check.key);
1402 if (c->signature_check.fingerprint)
1403 strbuf_addstr(sb, c->signature_check.fingerprint);
1406 if (c->signature_check.primary_key_fingerprint)
1407 strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
1410 switch (c->signature_check.trust_level) {
1411 case TRUST_UNDEFINED:
1412 strbuf_addstr(sb, "undefined");
1415 strbuf_addstr(sb, "never");
1417 case TRUST_MARGINAL:
1418 strbuf_addstr(sb, "marginal");
1421 strbuf_addstr(sb, "fully");
1423 case TRUST_ULTIMATE:
1424 strbuf_addstr(sb, "ultimate");
1434 /* For the rest we have to parse the commit header. */
1435 if (!c->commit_header_parsed) {
1437 repo_logmsg_reencode(c->repository, commit,
1438 &c->commit_encoding, "UTF-8");
1439 parse_commit_header(c);
1442 switch (placeholder[0]) {
1443 case 'a': /* author ... */
1444 return format_person_part(sb, placeholder[1],
1445 msg + c->author.off, c->author.len,
1446 &c->pretty_ctx->date_mode);
1447 case 'c': /* committer ... */
1448 return format_person_part(sb, placeholder[1],
1449 msg + c->committer.off, c->committer.len,
1450 &c->pretty_ctx->date_mode);
1451 case 'e': /* encoding */
1452 if (c->commit_encoding)
1453 strbuf_addstr(sb, c->commit_encoding);
1455 case 'B': /* raw body */
1456 /* message_off is always left at the initial newline */
1457 strbuf_addstr(sb, msg + c->message_off + 1);
1461 /* Now we need to parse the commit message. */
1462 if (!c->commit_message_parsed)
1463 parse_commit_message(c);
1465 switch (placeholder[0]) {
1466 case 's': /* subject */
1467 format_subject(sb, msg + c->subject_off, " ");
1469 case 'f': /* sanitized subject */
1470 eol = strchrnul(msg + c->subject_off, '\n');
1471 format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
1473 case 'b': /* body */
1474 strbuf_addstr(sb, msg + c->body_off);
1478 if (skip_prefix(placeholder, "(trailers", &arg)) {
1479 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1480 struct string_list filter_list = STRING_LIST_INIT_NODUP;
1481 struct strbuf sepbuf = STRBUF_INIT;
1482 struct strbuf kvsepbuf = STRBUF_INIT;
1485 opts.no_divider = 1;
1489 if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL))
1493 format_trailers_from_commit(sb, msg + c->subject_off, &opts);
1494 ret = arg - placeholder + 1;
1497 string_list_clear(&filter_list, 0);
1498 strbuf_release(&sepbuf);
1502 return 0; /* unknown placeholder */
1505 static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1506 const char *placeholder,
1507 struct format_commit_context *c)
1509 struct strbuf local_sb = STRBUF_INIT;
1510 int total_consumed = 0, len, padding = c->padding;
1512 const char *start = strrchr(sb->buf, '\n');
1516 occupied = utf8_strnwidth(start, -1, 1);
1517 occupied += c->pretty_ctx->graph_width;
1518 padding = (-padding) - occupied;
1521 int modifier = *placeholder == 'C';
1522 int consumed = format_commit_one(&local_sb, placeholder, c);
1523 total_consumed += consumed;
1528 placeholder += consumed;
1529 if (*placeholder != '%')
1534 len = utf8_strnwidth(local_sb.buf, -1, 1);
1536 if (c->flush_type == flush_left_and_steal) {
1537 const char *ch = sb->buf + sb->len - 1;
1538 while (len > padding && ch > sb->buf) {
1545 /* check for trailing ansi sequences */
1549 while (ch - p < 10 && *p != '\033')
1552 ch + 1 - p != display_mode_esc_sequence_len(p))
1555 * got a good ansi sequence, put it back to
1556 * local_sb as we're cutting sb
1558 strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1561 strbuf_setlen(sb, ch + 1 - sb->buf);
1562 c->flush_type = flush_left;
1565 if (len > padding) {
1566 switch (c->truncate) {
1568 strbuf_utf8_replace(&local_sb,
1569 0, len - (padding - 2),
1573 strbuf_utf8_replace(&local_sb,
1575 len - (padding - 2),
1579 strbuf_utf8_replace(&local_sb,
1580 padding - 2, len - (padding - 2),
1586 strbuf_addbuf(sb, &local_sb);
1588 int sb_len = sb->len, offset = 0;
1589 if (c->flush_type == flush_left)
1590 offset = padding - len;
1591 else if (c->flush_type == flush_both)
1592 offset = (padding - len) / 2;
1594 * we calculate padding in columns, now
1595 * convert it back to chars
1597 padding = padding - len + local_sb.len;
1598 strbuf_addchars(sb, ' ', padding);
1599 memcpy(sb->buf + sb_len + offset, local_sb.buf,
1602 strbuf_release(&local_sb);
1603 c->flush_type = no_flush;
1604 return total_consumed;
1607 static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1608 const char *placeholder,
1615 ADD_LF_BEFORE_NON_EMPTY,
1616 DEL_LF_BEFORE_EMPTY,
1617 ADD_SP_BEFORE_NON_EMPTY
1620 switch (placeholder[0]) {
1622 magic = DEL_LF_BEFORE_EMPTY;
1625 magic = ADD_LF_BEFORE_NON_EMPTY;
1628 magic = ADD_SP_BEFORE_NON_EMPTY;
1633 if (magic != NO_MAGIC)
1637 if (((struct format_commit_context *)context)->flush_type != no_flush)
1638 consumed = format_and_pad_commit(sb, placeholder, context);
1640 consumed = format_commit_one(sb, placeholder, context);
1641 if (magic == NO_MAGIC)
1644 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1645 while (sb->len && sb->buf[sb->len - 1] == '\n')
1646 strbuf_setlen(sb, sb->len - 1);
1647 } else if (orig_len != sb->len) {
1648 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1649 strbuf_insertstr(sb, orig_len, "\n");
1650 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1651 strbuf_insertstr(sb, orig_len, " ");
1653 return consumed + 1;
1656 static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1659 struct userformat_want *w = context;
1661 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
1664 switch (*placeholder) {
1675 void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1677 struct strbuf dummy = STRBUF_INIT;
1684 strbuf_expand(&dummy, fmt, userformat_want_item, w);
1685 strbuf_release(&dummy);
1688 void repo_format_commit_message(struct repository *r,
1689 const struct commit *commit,
1690 const char *format, struct strbuf *sb,
1691 const struct pretty_print_context *pretty_ctx)
1693 struct format_commit_context context = {
1696 .pretty_ctx = pretty_ctx,
1697 .wrap_start = sb->len
1699 const char *output_enc = pretty_ctx->output_encoding;
1700 const char *utf8 = "UTF-8";
1702 strbuf_expand(sb, format, format_commit_item, &context);
1703 rewrap_message_tail(sb, &context, 0, 0, 0);
1706 * Convert output to an actual output encoding; note that
1707 * format_commit_item() will always use UTF-8, so we don't
1708 * have to bother if that's what the output wants.
1711 if (same_encoding(utf8, output_enc))
1714 if (context.commit_encoding &&
1715 !same_encoding(context.commit_encoding, utf8))
1716 output_enc = context.commit_encoding;
1721 char *out = reencode_string_len(sb->buf, sb->len,
1722 output_enc, utf8, &outsz);
1724 strbuf_attach(sb, out, outsz, outsz + 1);
1727 free(context.commit_encoding);
1728 repo_unuse_commit_buffer(r, commit, context.message);
1731 static void pp_header(struct pretty_print_context *pp,
1732 const char *encoding,
1733 const struct commit *commit,
1737 int parents_shown = 0;
1740 const char *name, *line = *msg_p;
1741 int linelen = get_one_line(*msg_p);
1751 if (pp->fmt == CMIT_FMT_RAW) {
1752 strbuf_add(sb, line, linelen);
1756 if (starts_with(line, "parent ")) {
1757 if (linelen != the_hash_algo->hexsz + 8)
1758 die("bad parent line in commit");
1762 if (!parents_shown) {
1763 unsigned num = commit_list_count(commit->parents);
1764 /* with enough slop */
1765 strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20);
1766 add_merge_info(pp, sb, commit);
1771 * MEDIUM == DEFAULT shows only author with dates.
1772 * FULL shows both authors but not dates.
1773 * FULLER shows both authors and dates.
1775 if (skip_prefix(line, "author ", &name)) {
1776 strbuf_grow(sb, linelen + 80);
1777 pp_user_info(pp, "Author", sb, name, encoding);
1779 if (skip_prefix(line, "committer ", &name) &&
1780 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
1781 strbuf_grow(sb, linelen + 80);
1782 pp_user_info(pp, "Commit", sb, name, encoding);
1787 void pp_title_line(struct pretty_print_context *pp,
1790 const char *encoding,
1793 static const int max_length = 78; /* per rfc2047 */
1794 struct strbuf title;
1796 strbuf_init(&title, 80);
1797 *msg_p = format_subject(&title, *msg_p,
1798 pp->preserve_subject ? "\n" : " ");
1800 strbuf_grow(sb, title.len + 1024);
1801 if (pp->print_email_subject) {
1803 fmt_output_email_subject(sb, pp->rev);
1804 if (pp->encode_email_headers &&
1805 needs_rfc2047_encoding(title.buf, title.len))
1806 add_rfc2047(sb, title.buf, title.len,
1807 encoding, RFC2047_SUBJECT);
1809 strbuf_add_wrapped_bytes(sb, title.buf, title.len,
1810 -last_line_length(sb), 1, max_length);
1812 strbuf_addbuf(sb, &title);
1814 strbuf_addch(sb, '\n');
1816 if (need_8bit_cte == 0) {
1818 for (i = 0; i < pp->in_body_headers.nr; i++) {
1819 if (has_non_ascii(pp->in_body_headers.items[i].string)) {
1826 if (need_8bit_cte > 0) {
1827 const char *header_fmt =
1828 "MIME-Version: 1.0\n"
1829 "Content-Type: text/plain; charset=%s\n"
1830 "Content-Transfer-Encoding: 8bit\n";
1831 strbuf_addf(sb, header_fmt, encoding);
1833 if (pp->after_subject) {
1834 strbuf_addstr(sb, pp->after_subject);
1836 if (cmit_fmt_is_mail(pp->fmt)) {
1837 strbuf_addch(sb, '\n');
1840 if (pp->in_body_headers.nr) {
1842 for (i = 0; i < pp->in_body_headers.nr; i++) {
1843 strbuf_addstr(sb, pp->in_body_headers.items[i].string);
1844 free(pp->in_body_headers.items[i].string);
1846 string_list_clear(&pp->in_body_headers, 0);
1847 strbuf_addch(sb, '\n');
1850 strbuf_release(&title);
1853 static int pp_utf8_width(const char *start, const char *end)
1856 size_t remain = end - start;
1859 int n = utf8_width(&start, &remain);
1860 if (n < 0 || !start)
1867 static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
1868 const char *line, int linelen)
1872 while ((tab = memchr(line, '\t', linelen)) != NULL) {
1873 int width = pp_utf8_width(line, tab);
1876 * If it wasn't well-formed utf8, or it
1877 * had characters with badly defined
1878 * width (control characters etc), just
1879 * give up on trying to align things.
1884 /* Output the data .. */
1885 strbuf_add(sb, line, tab - line);
1887 /* .. and the de-tabified tab */
1888 strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
1890 /* Skip over the printed part .. */
1891 linelen -= tab + 1 - line;
1896 * Print out everything after the last tab without
1897 * worrying about width - there's nothing more to
1900 strbuf_add(sb, line, linelen);
1904 * pp_handle_indent() prints out the intendation, and
1905 * the whole line (without the final newline), after
1908 static void pp_handle_indent(struct pretty_print_context *pp,
1909 struct strbuf *sb, int indent,
1910 const char *line, int linelen)
1912 strbuf_addchars(sb, ' ', indent);
1913 if (pp->expand_tabs_in_log)
1914 strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
1916 strbuf_add(sb, line, linelen);
1919 static int is_mboxrd_from(const char *line, int len)
1922 * a line matching /^From $/ here would only have len == 4
1923 * at this point because is_empty_line would've trimmed all
1926 return len > 4 && starts_with(line + strspn(line, ">"), "From ");
1929 void pp_remainder(struct pretty_print_context *pp,
1936 const char *line = *msg_p;
1937 int linelen = get_one_line(line);
1943 if (is_blank_line(line, &linelen)) {
1946 if (pp->fmt == CMIT_FMT_SHORT)
1951 strbuf_grow(sb, linelen + indent + 20);
1953 pp_handle_indent(pp, sb, indent, line, linelen);
1954 else if (pp->expand_tabs_in_log)
1955 strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
1958 if (pp->fmt == CMIT_FMT_MBOXRD &&
1959 is_mboxrd_from(line, linelen))
1960 strbuf_addch(sb, '>');
1962 strbuf_add(sb, line, linelen);
1964 strbuf_addch(sb, '\n');
1968 void pretty_print_commit(struct pretty_print_context *pp,
1969 const struct commit *commit,
1972 unsigned long beginning_of_body;
1975 const char *reencoded;
1976 const char *encoding;
1977 int need_8bit_cte = pp->need_8bit_cte;
1979 if (pp->fmt == CMIT_FMT_USERFORMAT) {
1980 format_commit_message(commit, user_format, sb, pp);
1984 encoding = get_log_output_encoding();
1985 msg = reencoded = logmsg_reencode(commit, NULL, encoding);
1987 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
1991 * We need to check and emit Content-type: to mark it
1992 * as 8-bit if we haven't done so.
1994 if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
1997 for (in_body = i = 0; (ch = msg[i]); i++) {
1999 /* author could be non 7-bit ASCII but
2000 * the log may be so; skip over the
2001 * header part first.
2003 if (ch == '\n' && msg[i+1] == '\n')
2006 else if (non_ascii(ch)) {
2013 pp_header(pp, encoding, commit, &msg, sb);
2014 if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
2015 strbuf_addch(sb, '\n');
2018 /* Skip excess blank lines at the beginning of body, if any... */
2019 msg = skip_blank_lines(msg);
2021 /* These formats treat the title line specially. */
2022 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
2023 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
2025 beginning_of_body = sb->len;
2026 if (pp->fmt != CMIT_FMT_ONELINE)
2027 pp_remainder(pp, &msg, sb, indent);
2030 /* Make sure there is an EOLN for the non-oneline case */
2031 if (pp->fmt != CMIT_FMT_ONELINE)
2032 strbuf_addch(sb, '\n');
2035 * The caller may append additional body text in e-mail
2036 * format. Make sure we did not strip the blank line
2037 * between the header and the body.
2039 if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
2040 strbuf_addch(sb, '\n');
2042 unuse_commit_buffer(commit, reencoded);
2045 void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
2048 struct pretty_print_context pp = {0};
2050 pretty_print_commit(&pp, commit, sb);