6 #include "string-list.h"
11 static char *user_format;
13 void get_commit_format(const char *arg, struct rev_info *rev)
16 static struct cmt_fmt_map {
21 { "raw", 1, CMIT_FMT_RAW },
22 { "medium", 1, CMIT_FMT_MEDIUM },
23 { "short", 1, CMIT_FMT_SHORT },
24 { "email", 1, CMIT_FMT_EMAIL },
25 { "full", 5, CMIT_FMT_FULL },
26 { "fuller", 5, CMIT_FMT_FULLER },
27 { "oneline", 1, CMIT_FMT_ONELINE },
30 rev->use_terminator = 0;
32 rev->commit_format = CMIT_FMT_DEFAULT;
35 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
36 const char *cp = strchr(arg, ':') + 1;
38 user_format = xstrdup(cp);
40 rev->use_terminator = 1;
41 rev->commit_format = CMIT_FMT_USERFORMAT;
44 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
45 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
46 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
47 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
48 rev->use_terminator = 1;
49 rev->commit_format = cmt_fmts[i].v;
54 die("invalid --pretty format: %s", arg);
58 * Generic support for pretty-printing the header
60 static int get_one_line(const char *msg)
75 /* High bit set, or ISO-2022-INT */
79 return ((ch & 0x80) || (ch == 0x1b));
82 static int is_rfc2047_special(char ch)
84 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
87 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
92 for (i = 0; i < len; i++) {
96 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
99 strbuf_add(sb, line, len);
103 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
104 strbuf_addf(sb, "=?%s?q?", encoding);
105 for (i = last = 0; i < len; i++) {
106 unsigned ch = line[i] & 0xFF;
108 * We encode ' ' using '=20' even though rfc2047
109 * allows using '_' for readability. Unfortunately,
110 * many programs do not understand this and just
111 * leave the underscore in place.
113 if (is_rfc2047_special(ch) || ch == ' ') {
114 strbuf_add(sb, line + last, i - last);
115 strbuf_addf(sb, "=%02X", ch);
119 strbuf_add(sb, line + last, len - last);
120 strbuf_addstr(sb, "?=");
123 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
124 const char *line, enum date_mode dmode,
125 const char *encoding)
131 const char *filler = " ";
133 if (fmt == CMIT_FMT_ONELINE)
135 date = strchr(line, '>');
138 namelen = ++date - line;
139 time = strtoul(date, &date, 10);
140 tz = strtol(date, NULL, 10);
142 if (fmt == CMIT_FMT_EMAIL) {
143 char *name_tail = strchr(line, '<');
144 int display_name_length;
147 while (line < name_tail && isspace(name_tail[-1]))
149 display_name_length = name_tail - line;
151 strbuf_addstr(sb, "From: ");
152 add_rfc2047(sb, line, display_name_length, encoding);
153 strbuf_add(sb, name_tail, namelen - display_name_length);
154 strbuf_addch(sb, '\n');
156 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
157 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
158 filler, namelen, line);
161 case CMIT_FMT_MEDIUM:
162 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
165 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
167 case CMIT_FMT_FULLER:
168 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
176 static int is_empty_line(const char *line, int *len_p)
179 while (len && isspace(line[len-1]))
185 static const char *skip_empty_lines(const char *msg)
188 int linelen = get_one_line(msg);
192 if (!is_empty_line(msg, &ll))
199 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
200 const struct commit *commit, int abbrev)
202 struct commit_list *parent = commit->parents;
204 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
205 !parent || !parent->next)
208 strbuf_addstr(sb, "Merge:");
211 struct commit *p = parent->item;
212 const char *hex = NULL;
214 hex = find_unique_abbrev(p->object.sha1, abbrev);
216 hex = sha1_to_hex(p->object.sha1);
217 parent = parent->next;
219 strbuf_addf(sb, " %s", hex);
221 strbuf_addch(sb, '\n');
224 static char *get_header(const struct commit *commit, const char *key)
226 int key_len = strlen(key);
227 const char *line = commit->buffer;
230 const char *eol = strchr(line, '\n'), *next;
235 eol = line + strlen(line);
239 if (eol - line > key_len &&
240 !strncmp(line, key, key_len) &&
241 line[key_len] == ' ') {
242 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
248 static char *replace_encoding_header(char *buf, const char *encoding)
250 struct strbuf tmp = STRBUF_INIT;
254 /* guess if there is an encoding header before a \n\n */
255 while (strncmp(cp, "encoding ", strlen("encoding "))) {
256 cp = strchr(cp, '\n');
257 if (!cp || *++cp == '\n')
261 cp = strchr(cp, '\n');
263 return buf; /* should not happen but be defensive */
264 len = cp + 1 - (buf + start);
266 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
267 if (is_encoding_utf8(encoding)) {
268 /* we have re-coded to UTF-8; drop the header */
269 strbuf_remove(&tmp, start, len);
271 /* just replaces XXXX in 'encoding XXXX\n' */
272 strbuf_splice(&tmp, start + strlen("encoding "),
273 len - strlen("encoding \n"),
274 encoding, strlen(encoding));
276 return strbuf_detach(&tmp, NULL);
279 static char *logmsg_reencode(const struct commit *commit,
280 const char *output_encoding)
282 static const char *utf8 = "utf-8";
283 const char *use_encoding;
287 if (!*output_encoding)
289 encoding = get_header(commit, "encoding");
290 use_encoding = encoding ? encoding : utf8;
291 if (!strcmp(use_encoding, output_encoding))
292 if (encoding) /* we'll strip encoding header later */
293 out = xstrdup(commit->buffer);
295 return NULL; /* nothing to do */
297 out = reencode_string(commit->buffer,
298 output_encoding, use_encoding);
300 out = replace_encoding_header(out, output_encoding);
306 static int mailmap_name(struct strbuf *sb, const char *email)
308 static struct string_list *mail_map;
312 mail_map = xcalloc(1, sizeof(*mail_map));
313 read_mailmap(mail_map, ".mailmap", NULL);
319 if (!map_email(mail_map, email, buffer, sizeof(buffer)))
321 strbuf_addstr(sb, buffer);
325 static size_t format_person_part(struct strbuf *sb, char part,
326 const char *msg, int len, enum date_mode dmode)
328 /* currently all placeholders have same length */
329 const int placeholder_len = 2;
330 int start, end, tz = 0;
331 unsigned long date = 0;
334 /* advance 'end' to point to email start delimiter */
335 for (end = 0; end < len && msg[end] != '<'; end++)
339 * When end points at the '<' that we found, it should have
340 * matching '>' later, which means 'end' must be strictly
346 if (part == 'n' || part == 'N') { /* name */
347 while (end > 0 && isspace(msg[end - 1]))
349 if (part != 'N' || !msg[end] || !msg[end + 1] ||
350 mailmap_name(sb, msg + end + 2) < 0)
351 strbuf_add(sb, msg, end);
352 return placeholder_len;
354 start = ++end; /* save email start position */
356 /* advance 'end' to point to email end delimiter */
357 for ( ; end < len && msg[end] != '>'; end++)
363 if (part == 'e') { /* email */
364 strbuf_add(sb, msg + start, end - start);
365 return placeholder_len;
368 /* advance 'start' to point to date start delimiter */
369 for (start = end + 1; start < len && isspace(msg[start]); start++)
373 date = strtoul(msg + start, &ep, 10);
374 if (msg + start == ep)
377 if (part == 't') { /* date, UNIX timestamp */
378 strbuf_add(sb, msg + start, ep - (msg + start));
379 return placeholder_len;
383 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
385 if (start + 1 < len) {
386 tz = strtoul(msg + start + 1, NULL, 10);
387 if (msg[start] == '-')
393 strbuf_addstr(sb, show_date(date, tz, dmode));
394 return placeholder_len;
395 case 'D': /* date, RFC2822 style */
396 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
397 return placeholder_len;
398 case 'r': /* date, relative */
399 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
400 return placeholder_len;
401 case 'i': /* date, ISO 8601 */
402 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
403 return placeholder_len;
408 * bogus commit, 'sb' cannot be updated, but we still need to
409 * compute a valid return value.
411 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
412 || part == 'D' || part == 'r' || part == 'i')
413 return placeholder_len;
415 return 0; /* unknown placeholder */
423 struct format_commit_context {
424 const struct commit *commit;
425 enum date_mode dmode;
426 unsigned commit_header_parsed:1;
427 unsigned commit_message_parsed:1;
429 /* These offsets are relative to the start of the commit message. */
431 struct chunk committer;
432 struct chunk encoding;
437 /* The following ones are relative to the result struct strbuf. */
438 struct chunk abbrev_commit_hash;
439 struct chunk abbrev_tree_hash;
440 struct chunk abbrev_parent_hashes;
443 static int add_again(struct strbuf *sb, struct chunk *chunk)
446 strbuf_adddup(sb, chunk->off, chunk->len);
451 * We haven't seen this chunk before. Our caller is surely
452 * going to add it the hard way now. Remember the most likely
453 * start of the to-be-added chunk: the current end of the
456 chunk->off = sb->len;
460 static void parse_commit_header(struct format_commit_context *context)
462 const char *msg = context->commit->buffer;
465 for (i = 0; msg[i]; i++) {
467 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
472 } else if (!prefixcmp(msg + i, "author ")) {
473 context->author.off = i + 7;
474 context->author.len = eol - i - 7;
475 } else if (!prefixcmp(msg + i, "committer ")) {
476 context->committer.off = i + 10;
477 context->committer.len = eol - i - 10;
478 } else if (!prefixcmp(msg + i, "encoding ")) {
479 context->encoding.off = i + 9;
480 context->encoding.len = eol - i - 9;
484 context->message_off = i;
485 context->commit_header_parsed = 1;
488 const char *format_subject(struct strbuf *sb, const char *msg,
489 const char *line_separator)
494 const char *line = msg;
495 int linelen = get_one_line(line);
498 if (!linelen || is_empty_line(line, &linelen))
503 strbuf_grow(sb, linelen + 2);
505 strbuf_addstr(sb, line_separator);
506 strbuf_add(sb, line, linelen);
512 static void parse_commit_message(struct format_commit_context *c)
514 const char *msg = c->commit->buffer + c->message_off;
515 const char *start = c->commit->buffer;
517 msg = skip_empty_lines(msg);
518 c->subject_off = msg - start;
520 msg = format_subject(NULL, msg, NULL);
521 msg = skip_empty_lines(msg);
522 c->body_off = msg - start;
524 c->commit_message_parsed = 1;
527 static void format_decoration(struct strbuf *sb, const struct commit *commit)
529 struct name_decoration *d;
530 const char *prefix = " (";
532 load_ref_decorations();
533 d = lookup_decoration(&name_decoration, &commit->object);
535 strbuf_addstr(sb, prefix);
537 strbuf_addstr(sb, d->name);
540 if (prefix[0] == ',')
541 strbuf_addch(sb, ')');
544 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
547 struct format_commit_context *c = context;
548 const struct commit *commit = c->commit;
549 const char *msg = commit->buffer;
550 struct commit_list *p;
553 /* these are independent of the commit */
554 switch (placeholder[0]) {
556 if (placeholder[1] == '(') {
557 const char *end = strchr(placeholder + 2, ')');
558 char color[COLOR_MAXLEN];
561 color_parse_mem(placeholder + 2,
562 end - (placeholder + 2),
563 "--pretty format", color);
564 strbuf_addstr(sb, color);
565 return end - placeholder + 1;
567 if (!prefixcmp(placeholder + 1, "red")) {
568 strbuf_addstr(sb, "\033[31m");
570 } else if (!prefixcmp(placeholder + 1, "green")) {
571 strbuf_addstr(sb, "\033[32m");
573 } else if (!prefixcmp(placeholder + 1, "blue")) {
574 strbuf_addstr(sb, "\033[34m");
576 } else if (!prefixcmp(placeholder + 1, "reset")) {
577 strbuf_addstr(sb, "\033[m");
581 case 'n': /* newline */
582 strbuf_addch(sb, '\n');
585 /* %x00 == NUL, %x0a == LF, etc. */
586 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
588 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
590 strbuf_addch(sb, (h1<<4)|h2);
596 /* these depend on the commit */
597 if (!commit->object.parsed)
598 parse_object(commit->object.sha1);
600 switch (placeholder[0]) {
601 case 'H': /* commit hash */
602 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
604 case 'h': /* abbreviated commit hash */
605 if (add_again(sb, &c->abbrev_commit_hash))
607 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
609 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
611 case 'T': /* tree hash */
612 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
614 case 't': /* abbreviated tree hash */
615 if (add_again(sb, &c->abbrev_tree_hash))
617 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
619 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
621 case 'P': /* parent hashes */
622 for (p = commit->parents; p; p = p->next) {
623 if (p != commit->parents)
624 strbuf_addch(sb, ' ');
625 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
628 case 'p': /* abbreviated parent hashes */
629 if (add_again(sb, &c->abbrev_parent_hashes))
631 for (p = commit->parents; p; p = p->next) {
632 if (p != commit->parents)
633 strbuf_addch(sb, ' ');
634 strbuf_addstr(sb, find_unique_abbrev(
635 p->item->object.sha1, DEFAULT_ABBREV));
637 c->abbrev_parent_hashes.len = sb->len -
638 c->abbrev_parent_hashes.off;
640 case 'm': /* left/right/bottom */
641 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
643 : (commit->object.flags & SYMMETRIC_LEFT)
648 format_decoration(sb, commit);
652 /* For the rest we have to parse the commit header. */
653 if (!c->commit_header_parsed)
654 parse_commit_header(c);
656 switch (placeholder[0]) {
657 case 'a': /* author ... */
658 return format_person_part(sb, placeholder[1],
659 msg + c->author.off, c->author.len,
661 case 'c': /* committer ... */
662 return format_person_part(sb, placeholder[1],
663 msg + c->committer.off, c->committer.len,
665 case 'e': /* encoding */
666 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
670 /* Now we need to parse the commit message. */
671 if (!c->commit_message_parsed)
672 parse_commit_message(c);
674 switch (placeholder[0]) {
675 case 's': /* subject */
676 format_subject(sb, msg + c->subject_off, " ");
679 strbuf_addstr(sb, msg + c->body_off);
682 return 0; /* unknown placeholder */
685 void format_commit_message(const struct commit *commit,
686 const void *format, struct strbuf *sb,
687 enum date_mode dmode)
689 struct format_commit_context context;
691 memset(&context, 0, sizeof(context));
692 context.commit = commit;
693 context.dmode = dmode;
694 strbuf_expand(sb, format, format_commit_item, &context);
697 static void pp_header(enum cmit_fmt fmt,
699 enum date_mode dmode,
700 const char *encoding,
701 const struct commit *commit,
705 int parents_shown = 0;
708 const char *line = *msg_p;
709 int linelen = get_one_line(*msg_p);
719 if (fmt == CMIT_FMT_RAW) {
720 strbuf_add(sb, line, linelen);
724 if (!memcmp(line, "parent ", 7)) {
726 die("bad parent line in commit");
730 if (!parents_shown) {
731 struct commit_list *parent;
733 for (parent = commit->parents, num = 0;
735 parent = parent->next, num++)
737 /* with enough slop */
738 strbuf_grow(sb, num * 50 + 20);
739 add_merge_info(fmt, sb, commit, abbrev);
744 * MEDIUM == DEFAULT shows only author with dates.
745 * FULL shows both authors but not dates.
746 * FULLER shows both authors and dates.
748 if (!memcmp(line, "author ", 7)) {
749 strbuf_grow(sb, linelen + 80);
750 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
752 if (!memcmp(line, "committer ", 10) &&
753 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
754 strbuf_grow(sb, linelen + 80);
755 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
760 void pp_title_line(enum cmit_fmt fmt,
764 const char *after_subject,
765 const char *encoding,
768 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
771 strbuf_init(&title, 80);
772 *msg_p = format_subject(&title, *msg_p, line_separator);
774 strbuf_grow(sb, title.len + 1024);
776 strbuf_addstr(sb, subject);
777 add_rfc2047(sb, title.buf, title.len, encoding);
779 strbuf_addbuf(sb, &title);
781 strbuf_addch(sb, '\n');
783 if (need_8bit_cte > 0) {
784 const char *header_fmt =
785 "MIME-Version: 1.0\n"
786 "Content-Type: text/plain; charset=%s\n"
787 "Content-Transfer-Encoding: 8bit\n";
788 strbuf_addf(sb, header_fmt, encoding);
791 strbuf_addstr(sb, after_subject);
793 if (fmt == CMIT_FMT_EMAIL) {
794 strbuf_addch(sb, '\n');
796 strbuf_release(&title);
799 void pp_remainder(enum cmit_fmt fmt,
806 const char *line = *msg_p;
807 int linelen = get_one_line(line);
813 if (is_empty_line(line, &linelen)) {
816 if (fmt == CMIT_FMT_SHORT)
821 strbuf_grow(sb, linelen + indent + 20);
823 memset(sb->buf + sb->len, ' ', indent);
824 strbuf_setlen(sb, sb->len + indent);
826 strbuf_add(sb, line, linelen);
827 strbuf_addch(sb, '\n');
831 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
833 const char *encoding;
835 encoding = (git_log_output_encoding
836 ? git_log_output_encoding
837 : git_commit_encoding);
841 *encoding_p = encoding;
842 return logmsg_reencode(commit, encoding);
845 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
846 struct strbuf *sb, int abbrev,
847 const char *subject, const char *after_subject,
848 enum date_mode dmode, int need_8bit_cte)
850 unsigned long beginning_of_body;
852 const char *msg = commit->buffer;
854 const char *encoding;
856 if (fmt == CMIT_FMT_USERFORMAT) {
857 format_commit_message(commit, user_format, sb, dmode);
861 reencoded = reencode_commit_message(commit, &encoding);
866 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
870 * We need to check and emit Content-type: to mark it
871 * as 8-bit if we haven't done so.
873 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
876 for (in_body = i = 0; (ch = msg[i]); i++) {
878 /* author could be non 7-bit ASCII but
879 * the log may be so; skip over the
882 if (ch == '\n' && msg[i+1] == '\n')
885 else if (non_ascii(ch)) {
892 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
893 if (fmt != CMIT_FMT_ONELINE && !subject) {
894 strbuf_addch(sb, '\n');
897 /* Skip excess blank lines at the beginning of body, if any... */
898 msg = skip_empty_lines(msg);
900 /* These formats treat the title line specially. */
901 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
902 pp_title_line(fmt, &msg, sb, subject,
903 after_subject, encoding, need_8bit_cte);
905 beginning_of_body = sb->len;
906 if (fmt != CMIT_FMT_ONELINE)
907 pp_remainder(fmt, &msg, sb, indent);
910 /* Make sure there is an EOLN for the non-oneline case */
911 if (fmt != CMIT_FMT_ONELINE)
912 strbuf_addch(sb, '\n');
915 * The caller may append additional body text in e-mail
916 * format. Make sure we did not strip the blank line
917 * between the header and the body.
919 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
920 strbuf_addch(sb, '\n');