6 #include "string-list.h"
12 static char *user_format;
14 void get_commit_format(const char *arg, struct rev_info *rev)
17 static struct cmt_fmt_map {
22 { "raw", 1, CMIT_FMT_RAW },
23 { "medium", 1, CMIT_FMT_MEDIUM },
24 { "short", 1, CMIT_FMT_SHORT },
25 { "email", 1, CMIT_FMT_EMAIL },
26 { "full", 5, CMIT_FMT_FULL },
27 { "fuller", 5, CMIT_FMT_FULLER },
28 { "oneline", 1, CMIT_FMT_ONELINE },
31 rev->use_terminator = 0;
33 rev->commit_format = CMIT_FMT_DEFAULT;
36 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
37 const char *cp = strchr(arg, ':') + 1;
39 user_format = xstrdup(cp);
41 rev->use_terminator = 1;
42 rev->commit_format = CMIT_FMT_USERFORMAT;
45 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
46 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
47 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
48 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
49 rev->use_terminator = 1;
50 rev->commit_format = cmt_fmts[i].v;
55 die("invalid --pretty format: %s", arg);
59 * Generic support for pretty-printing the header
61 static int get_one_line(const char *msg)
76 /* High bit set, or ISO-2022-INT */
80 return ((ch & 0x80) || (ch == 0x1b));
83 static int is_rfc2047_special(char ch)
85 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
88 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
93 for (i = 0; i < len; i++) {
97 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
100 strbuf_add(sb, line, len);
104 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
105 strbuf_addf(sb, "=?%s?q?", encoding);
106 for (i = last = 0; i < len; i++) {
107 unsigned ch = line[i] & 0xFF;
109 * We encode ' ' using '=20' even though rfc2047
110 * allows using '_' for readability. Unfortunately,
111 * many programs do not understand this and just
112 * leave the underscore in place.
114 if (is_rfc2047_special(ch) || ch == ' ') {
115 strbuf_add(sb, line + last, i - last);
116 strbuf_addf(sb, "=%02X", ch);
120 strbuf_add(sb, line + last, len - last);
121 strbuf_addstr(sb, "?=");
124 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
125 const char *line, enum date_mode dmode,
126 const char *encoding)
132 const char *filler = " ";
134 if (fmt == CMIT_FMT_ONELINE)
136 date = strchr(line, '>');
139 namelen = ++date - line;
140 time = strtoul(date, &date, 10);
141 tz = strtol(date, NULL, 10);
143 if (fmt == CMIT_FMT_EMAIL) {
144 char *name_tail = strchr(line, '<');
145 int display_name_length;
148 while (line < name_tail && isspace(name_tail[-1]))
150 display_name_length = name_tail - line;
152 strbuf_addstr(sb, "From: ");
153 add_rfc2047(sb, line, display_name_length, encoding);
154 strbuf_add(sb, name_tail, namelen - display_name_length);
155 strbuf_addch(sb, '\n');
157 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
158 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
159 filler, namelen, line);
162 case CMIT_FMT_MEDIUM:
163 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
166 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
168 case CMIT_FMT_FULLER:
169 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
177 static int is_empty_line(const char *line, int *len_p)
180 while (len && isspace(line[len-1]))
186 static const char *skip_empty_lines(const char *msg)
189 int linelen = get_one_line(msg);
193 if (!is_empty_line(msg, &ll))
200 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
201 const struct commit *commit, int abbrev)
203 struct commit_list *parent = commit->parents;
205 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
206 !parent || !parent->next)
209 strbuf_addstr(sb, "Merge:");
212 struct commit *p = parent->item;
213 const char *hex = NULL;
216 hex = find_unique_abbrev(p->object.sha1, abbrev);
218 hex = sha1_to_hex(p->object.sha1);
219 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
220 parent = parent->next;
222 strbuf_addf(sb, " %s%s", hex, dots);
224 strbuf_addch(sb, '\n');
227 static char *get_header(const struct commit *commit, const char *key)
229 int key_len = strlen(key);
230 const char *line = commit->buffer;
233 const char *eol = strchr(line, '\n'), *next;
238 eol = line + strlen(line);
242 if (eol - line > key_len &&
243 !strncmp(line, key, key_len) &&
244 line[key_len] == ' ') {
245 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
251 static char *replace_encoding_header(char *buf, const char *encoding)
253 struct strbuf tmp = STRBUF_INIT;
257 /* guess if there is an encoding header before a \n\n */
258 while (strncmp(cp, "encoding ", strlen("encoding "))) {
259 cp = strchr(cp, '\n');
260 if (!cp || *++cp == '\n')
264 cp = strchr(cp, '\n');
266 return buf; /* should not happen but be defensive */
267 len = cp + 1 - (buf + start);
269 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
270 if (is_encoding_utf8(encoding)) {
271 /* we have re-coded to UTF-8; drop the header */
272 strbuf_remove(&tmp, start, len);
274 /* just replaces XXXX in 'encoding XXXX\n' */
275 strbuf_splice(&tmp, start + strlen("encoding "),
276 len - strlen("encoding \n"),
277 encoding, strlen(encoding));
279 return strbuf_detach(&tmp, NULL);
282 static char *logmsg_reencode(const struct commit *commit,
283 const char *output_encoding)
285 static const char *utf8 = "utf-8";
286 const char *use_encoding;
290 if (!*output_encoding)
292 encoding = get_header(commit, "encoding");
293 use_encoding = encoding ? encoding : utf8;
294 if (!strcmp(use_encoding, output_encoding))
295 if (encoding) /* we'll strip encoding header later */
296 out = xstrdup(commit->buffer);
298 return NULL; /* nothing to do */
300 out = reencode_string(commit->buffer,
301 output_encoding, use_encoding);
303 out = replace_encoding_header(out, output_encoding);
309 static int mailmap_name(struct strbuf *sb, const char *email)
311 static struct string_list *mail_map;
315 mail_map = xcalloc(1, sizeof(*mail_map));
316 read_mailmap(mail_map, ".mailmap", NULL);
322 if (!map_email(mail_map, email, buffer, sizeof(buffer)))
324 strbuf_addstr(sb, buffer);
328 static size_t format_person_part(struct strbuf *sb, char part,
329 const char *msg, int len, enum date_mode dmode)
331 /* currently all placeholders have same length */
332 const int placeholder_len = 2;
333 int start, end, tz = 0;
334 unsigned long date = 0;
337 /* advance 'end' to point to email start delimiter */
338 for (end = 0; end < len && msg[end] != '<'; end++)
342 * When end points at the '<' that we found, it should have
343 * matching '>' later, which means 'end' must be strictly
349 if (part == 'n' || part == 'N') { /* name */
350 while (end > 0 && isspace(msg[end - 1]))
352 if (part != 'N' || !msg[end] || !msg[end + 1] ||
353 mailmap_name(sb, msg + end + 2) < 0)
354 strbuf_add(sb, msg, end);
355 return placeholder_len;
357 start = ++end; /* save email start position */
359 /* advance 'end' to point to email end delimiter */
360 for ( ; end < len && msg[end] != '>'; end++)
366 if (part == 'e') { /* email */
367 strbuf_add(sb, msg + start, end - start);
368 return placeholder_len;
371 /* advance 'start' to point to date start delimiter */
372 for (start = end + 1; start < len && isspace(msg[start]); start++)
376 date = strtoul(msg + start, &ep, 10);
377 if (msg + start == ep)
380 if (part == 't') { /* date, UNIX timestamp */
381 strbuf_add(sb, msg + start, ep - (msg + start));
382 return placeholder_len;
386 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
388 if (start + 1 < len) {
389 tz = strtoul(msg + start + 1, NULL, 10);
390 if (msg[start] == '-')
396 strbuf_addstr(sb, show_date(date, tz, dmode));
397 return placeholder_len;
398 case 'D': /* date, RFC2822 style */
399 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
400 return placeholder_len;
401 case 'r': /* date, relative */
402 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
403 return placeholder_len;
404 case 'i': /* date, ISO 8601 */
405 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
406 return placeholder_len;
411 * bogus commit, 'sb' cannot be updated, but we still need to
412 * compute a valid return value.
414 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
415 || part == 'D' || part == 'r' || part == 'i')
416 return placeholder_len;
418 return 0; /* unknown placeholder */
426 struct format_commit_context {
427 const struct commit *commit;
428 enum date_mode dmode;
429 unsigned commit_header_parsed:1;
430 unsigned commit_message_parsed:1;
432 /* These offsets are relative to the start of the commit message. */
434 struct chunk committer;
435 struct chunk encoding;
440 /* The following ones are relative to the result struct strbuf. */
441 struct chunk abbrev_commit_hash;
442 struct chunk abbrev_tree_hash;
443 struct chunk abbrev_parent_hashes;
446 static int add_again(struct strbuf *sb, struct chunk *chunk)
449 strbuf_adddup(sb, chunk->off, chunk->len);
454 * We haven't seen this chunk before. Our caller is surely
455 * going to add it the hard way now. Remember the most likely
456 * start of the to-be-added chunk: the current end of the
459 chunk->off = sb->len;
463 static void parse_commit_header(struct format_commit_context *context)
465 const char *msg = context->commit->buffer;
468 for (i = 0; msg[i]; i++) {
470 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
475 } else if (!prefixcmp(msg + i, "author ")) {
476 context->author.off = i + 7;
477 context->author.len = eol - i - 7;
478 } else if (!prefixcmp(msg + i, "committer ")) {
479 context->committer.off = i + 10;
480 context->committer.len = eol - i - 10;
481 } else if (!prefixcmp(msg + i, "encoding ")) {
482 context->encoding.off = i + 9;
483 context->encoding.len = eol - i - 9;
487 context->message_off = i;
488 context->commit_header_parsed = 1;
491 const char *format_subject(struct strbuf *sb, const char *msg,
492 const char *line_separator)
497 const char *line = msg;
498 int linelen = get_one_line(line);
501 if (!linelen || is_empty_line(line, &linelen))
506 strbuf_grow(sb, linelen + 2);
508 strbuf_addstr(sb, line_separator);
509 strbuf_add(sb, line, linelen);
515 static void parse_commit_message(struct format_commit_context *c)
517 const char *msg = c->commit->buffer + c->message_off;
518 const char *start = c->commit->buffer;
520 msg = skip_empty_lines(msg);
521 c->subject_off = msg - start;
523 msg = format_subject(NULL, msg, NULL);
524 msg = skip_empty_lines(msg);
525 c->body_off = msg - start;
527 c->commit_message_parsed = 1;
530 static void format_decoration(struct strbuf *sb, const struct commit *commit)
532 struct name_decoration *d;
533 const char *prefix = " (";
535 load_ref_decorations();
536 d = lookup_decoration(&name_decoration, &commit->object);
538 strbuf_addstr(sb, prefix);
540 strbuf_addstr(sb, d->name);
543 if (prefix[0] == ',')
544 strbuf_addch(sb, ')');
547 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
550 struct format_commit_context *c = context;
551 const struct commit *commit = c->commit;
552 const char *msg = commit->buffer;
553 struct commit_list *p;
556 /* these are independent of the commit */
557 switch (placeholder[0]) {
559 if (placeholder[1] == '(') {
560 const char *end = strchr(placeholder + 2, ')');
561 char color[COLOR_MAXLEN];
564 color_parse_mem(placeholder + 2,
565 end - (placeholder + 2),
566 "--pretty format", color);
567 strbuf_addstr(sb, color);
568 return end - placeholder + 1;
570 if (!prefixcmp(placeholder + 1, "red")) {
571 strbuf_addstr(sb, "\033[31m");
573 } else if (!prefixcmp(placeholder + 1, "green")) {
574 strbuf_addstr(sb, "\033[32m");
576 } else if (!prefixcmp(placeholder + 1, "blue")) {
577 strbuf_addstr(sb, "\033[34m");
579 } else if (!prefixcmp(placeholder + 1, "reset")) {
580 strbuf_addstr(sb, "\033[m");
584 case 'n': /* newline */
585 strbuf_addch(sb, '\n');
588 /* %x00 == NUL, %x0a == LF, etc. */
589 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
591 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
593 strbuf_addch(sb, (h1<<4)|h2);
599 /* these depend on the commit */
600 if (!commit->object.parsed)
601 parse_object(commit->object.sha1);
603 switch (placeholder[0]) {
604 case 'H': /* commit hash */
605 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
607 case 'h': /* abbreviated commit hash */
608 if (add_again(sb, &c->abbrev_commit_hash))
610 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
612 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
614 case 'T': /* tree hash */
615 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
617 case 't': /* abbreviated tree hash */
618 if (add_again(sb, &c->abbrev_tree_hash))
620 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
622 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
624 case 'P': /* parent hashes */
625 for (p = commit->parents; p; p = p->next) {
626 if (p != commit->parents)
627 strbuf_addch(sb, ' ');
628 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
631 case 'p': /* abbreviated parent hashes */
632 if (add_again(sb, &c->abbrev_parent_hashes))
634 for (p = commit->parents; p; p = p->next) {
635 if (p != commit->parents)
636 strbuf_addch(sb, ' ');
637 strbuf_addstr(sb, find_unique_abbrev(
638 p->item->object.sha1, DEFAULT_ABBREV));
640 c->abbrev_parent_hashes.len = sb->len -
641 c->abbrev_parent_hashes.off;
643 case 'm': /* left/right/bottom */
644 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
646 : (commit->object.flags & SYMMETRIC_LEFT)
651 format_decoration(sb, commit);
655 /* For the rest we have to parse the commit header. */
656 if (!c->commit_header_parsed)
657 parse_commit_header(c);
659 switch (placeholder[0]) {
660 case 'a': /* author ... */
661 return format_person_part(sb, placeholder[1],
662 msg + c->author.off, c->author.len,
664 case 'c': /* committer ... */
665 return format_person_part(sb, placeholder[1],
666 msg + c->committer.off, c->committer.len,
668 case 'e': /* encoding */
669 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
673 /* Now we need to parse the commit message. */
674 if (!c->commit_message_parsed)
675 parse_commit_message(c);
677 switch (placeholder[0]) {
678 case 's': /* subject */
679 format_subject(sb, msg + c->subject_off, " ");
682 strbuf_addstr(sb, msg + c->body_off);
685 return 0; /* unknown placeholder */
688 void format_commit_message(const struct commit *commit,
689 const void *format, struct strbuf *sb,
690 enum date_mode dmode)
692 struct format_commit_context context;
694 memset(&context, 0, sizeof(context));
695 context.commit = commit;
696 context.dmode = dmode;
697 strbuf_expand(sb, format, format_commit_item, &context);
700 static void pp_header(enum cmit_fmt fmt,
702 enum date_mode dmode,
703 const char *encoding,
704 const struct commit *commit,
708 int parents_shown = 0;
711 const char *line = *msg_p;
712 int linelen = get_one_line(*msg_p);
722 if (fmt == CMIT_FMT_RAW) {
723 strbuf_add(sb, line, linelen);
727 if (!memcmp(line, "parent ", 7)) {
729 die("bad parent line in commit");
733 if (!parents_shown) {
734 struct commit_list *parent;
736 for (parent = commit->parents, num = 0;
738 parent = parent->next, num++)
740 /* with enough slop */
741 strbuf_grow(sb, num * 50 + 20);
742 add_merge_info(fmt, sb, commit, abbrev);
747 * MEDIUM == DEFAULT shows only author with dates.
748 * FULL shows both authors but not dates.
749 * FULLER shows both authors and dates.
751 if (!memcmp(line, "author ", 7)) {
752 strbuf_grow(sb, linelen + 80);
753 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
755 if (!memcmp(line, "committer ", 10) &&
756 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
757 strbuf_grow(sb, linelen + 80);
758 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
763 void pp_title_line(enum cmit_fmt fmt,
767 const char *after_subject,
768 const char *encoding,
771 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
774 strbuf_init(&title, 80);
775 *msg_p = format_subject(&title, *msg_p, line_separator);
777 strbuf_grow(sb, title.len + 1024);
779 strbuf_addstr(sb, subject);
780 add_rfc2047(sb, title.buf, title.len, encoding);
782 strbuf_addbuf(sb, &title);
784 strbuf_addch(sb, '\n');
786 if (need_8bit_cte > 0) {
787 const char *header_fmt =
788 "MIME-Version: 1.0\n"
789 "Content-Type: text/plain; charset=%s\n"
790 "Content-Transfer-Encoding: 8bit\n";
791 strbuf_addf(sb, header_fmt, encoding);
794 strbuf_addstr(sb, after_subject);
796 if (fmt == CMIT_FMT_EMAIL) {
797 strbuf_addch(sb, '\n');
799 strbuf_release(&title);
802 void pp_remainder(enum cmit_fmt fmt,
809 const char *line = *msg_p;
810 int linelen = get_one_line(line);
816 if (is_empty_line(line, &linelen)) {
819 if (fmt == CMIT_FMT_SHORT)
824 strbuf_grow(sb, linelen + indent + 20);
826 memset(sb->buf + sb->len, ' ', indent);
827 strbuf_setlen(sb, sb->len + indent);
829 strbuf_add(sb, line, linelen);
830 strbuf_addch(sb, '\n');
834 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
836 const char *encoding;
838 encoding = (git_log_output_encoding
839 ? git_log_output_encoding
840 : git_commit_encoding);
844 *encoding_p = encoding;
845 return logmsg_reencode(commit, encoding);
848 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
849 struct strbuf *sb, int abbrev,
850 const char *subject, const char *after_subject,
851 enum date_mode dmode, int need_8bit_cte)
853 unsigned long beginning_of_body;
855 const char *msg = commit->buffer;
857 const char *encoding;
859 if (fmt == CMIT_FMT_USERFORMAT) {
860 format_commit_message(commit, user_format, sb, dmode);
864 reencoded = reencode_commit_message(commit, &encoding);
869 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
873 * We need to check and emit Content-type: to mark it
874 * as 8-bit if we haven't done so.
876 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
879 for (in_body = i = 0; (ch = msg[i]); i++) {
881 /* author could be non 7-bit ASCII but
882 * the log may be so; skip over the
885 if (ch == '\n' && msg[i+1] == '\n')
888 else if (non_ascii(ch)) {
895 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
896 if (fmt != CMIT_FMT_ONELINE && !subject) {
897 strbuf_addch(sb, '\n');
900 /* Skip excess blank lines at the beginning of body, if any... */
901 msg = skip_empty_lines(msg);
903 /* These formats treat the title line specially. */
904 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
905 pp_title_line(fmt, &msg, sb, subject,
906 after_subject, encoding, need_8bit_cte);
908 beginning_of_body = sb->len;
909 if (fmt != CMIT_FMT_ONELINE)
910 pp_remainder(fmt, &msg, sb, indent);
913 /* Make sure there is an EOLN for the non-oneline case */
914 if (fmt != CMIT_FMT_ONELINE)
915 strbuf_addch(sb, '\n');
918 * The caller may append additional body text in e-mail
919 * format. Make sure we did not strip the blank line
920 * between the header and the body.
922 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
923 strbuf_addch(sb, '\n');
925 if (fmt != CMIT_FMT_ONELINE)
926 get_commit_notes(commit, sb, encoding);