6 #include "string-list.h"
10 static char *user_format;
12 void get_commit_format(const char *arg, struct rev_info *rev)
15 static struct cmt_fmt_map {
20 { "raw", 1, CMIT_FMT_RAW },
21 { "medium", 1, CMIT_FMT_MEDIUM },
22 { "short", 1, CMIT_FMT_SHORT },
23 { "email", 1, CMIT_FMT_EMAIL },
24 { "full", 5, CMIT_FMT_FULL },
25 { "fuller", 5, CMIT_FMT_FULLER },
26 { "oneline", 1, CMIT_FMT_ONELINE },
29 rev->use_terminator = 0;
31 rev->commit_format = CMIT_FMT_DEFAULT;
34 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
35 const char *cp = strchr(arg, ':') + 1;
37 user_format = xstrdup(cp);
39 rev->use_terminator = 1;
40 rev->commit_format = CMIT_FMT_USERFORMAT;
43 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
44 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
45 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
46 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
47 rev->use_terminator = 1;
48 rev->commit_format = cmt_fmts[i].v;
53 die("invalid --pretty format: %s", arg);
57 * Generic support for pretty-printing the header
59 static int get_one_line(const char *msg)
74 /* High bit set, or ISO-2022-INT */
78 return ((ch & 0x80) || (ch == 0x1b));
81 static int is_rfc2047_special(char ch)
83 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
86 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
91 for (i = 0; i < len; i++) {
95 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
98 strbuf_add(sb, line, len);
102 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
103 strbuf_addf(sb, "=?%s?q?", encoding);
104 for (i = last = 0; i < len; i++) {
105 unsigned ch = line[i] & 0xFF;
107 * We encode ' ' using '=20' even though rfc2047
108 * allows using '_' for readability. Unfortunately,
109 * many programs do not understand this and just
110 * leave the underscore in place.
112 if (is_rfc2047_special(ch) || ch == ' ') {
113 strbuf_add(sb, line + last, i - last);
114 strbuf_addf(sb, "=%02X", ch);
118 strbuf_add(sb, line + last, len - last);
119 strbuf_addstr(sb, "?=");
122 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
123 const char *line, enum date_mode dmode,
124 const char *encoding)
130 const char *filler = " ";
132 if (fmt == CMIT_FMT_ONELINE)
134 date = strchr(line, '>');
137 namelen = ++date - line;
138 time = strtoul(date, &date, 10);
139 tz = strtol(date, NULL, 10);
141 if (fmt == CMIT_FMT_EMAIL) {
142 char *name_tail = strchr(line, '<');
143 int display_name_length;
146 while (line < name_tail && isspace(name_tail[-1]))
148 display_name_length = name_tail - line;
150 strbuf_addstr(sb, "From: ");
151 add_rfc2047(sb, line, display_name_length, encoding);
152 strbuf_add(sb, name_tail, namelen - display_name_length);
153 strbuf_addch(sb, '\n');
155 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
156 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
157 filler, namelen, line);
160 case CMIT_FMT_MEDIUM:
161 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
164 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
166 case CMIT_FMT_FULLER:
167 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
175 static int is_empty_line(const char *line, int *len_p)
178 while (len && isspace(line[len-1]))
184 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
185 const struct commit *commit, int abbrev)
187 struct commit_list *parent = commit->parents;
189 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
190 !parent || !parent->next)
193 strbuf_addstr(sb, "Merge:");
196 struct commit *p = parent->item;
197 const char *hex = NULL;
200 hex = find_unique_abbrev(p->object.sha1, abbrev);
202 hex = sha1_to_hex(p->object.sha1);
203 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
204 parent = parent->next;
206 strbuf_addf(sb, " %s%s", hex, dots);
208 strbuf_addch(sb, '\n');
211 static char *get_header(const struct commit *commit, const char *key)
213 int key_len = strlen(key);
214 const char *line = commit->buffer;
217 const char *eol = strchr(line, '\n'), *next;
222 eol = line + strlen(line);
226 if (eol - line > key_len &&
227 !strncmp(line, key, key_len) &&
228 line[key_len] == ' ') {
229 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
235 static char *replace_encoding_header(char *buf, const char *encoding)
241 /* guess if there is an encoding header before a \n\n */
242 while (strncmp(cp, "encoding ", strlen("encoding "))) {
243 cp = strchr(cp, '\n');
244 if (!cp || *++cp == '\n')
248 cp = strchr(cp, '\n');
250 return buf; /* should not happen but be defensive */
251 len = cp + 1 - (buf + start);
253 strbuf_init(&tmp, 0);
254 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
255 if (is_encoding_utf8(encoding)) {
256 /* we have re-coded to UTF-8; drop the header */
257 strbuf_remove(&tmp, start, len);
259 /* just replaces XXXX in 'encoding XXXX\n' */
260 strbuf_splice(&tmp, start + strlen("encoding "),
261 len - strlen("encoding \n"),
262 encoding, strlen(encoding));
264 return strbuf_detach(&tmp, NULL);
267 static char *logmsg_reencode(const struct commit *commit,
268 const char *output_encoding)
270 static const char *utf8 = "utf-8";
271 const char *use_encoding;
275 if (!*output_encoding)
277 encoding = get_header(commit, "encoding");
278 use_encoding = encoding ? encoding : utf8;
279 if (!strcmp(use_encoding, output_encoding))
280 if (encoding) /* we'll strip encoding header later */
281 out = xstrdup(commit->buffer);
283 return NULL; /* nothing to do */
285 out = reencode_string(commit->buffer,
286 output_encoding, use_encoding);
288 out = replace_encoding_header(out, output_encoding);
294 static int mailmap_name(struct strbuf *sb, const char *email)
296 static struct string_list *mail_map;
300 mail_map = xcalloc(1, sizeof(*mail_map));
301 read_mailmap(mail_map, ".mailmap", NULL);
307 if (!map_email(mail_map, email, buffer, sizeof(buffer)))
309 strbuf_addstr(sb, buffer);
313 static size_t format_person_part(struct strbuf *sb, char part,
314 const char *msg, int len, enum date_mode dmode)
316 /* currently all placeholders have same length */
317 const int placeholder_len = 2;
318 int start, end, tz = 0;
319 unsigned long date = 0;
322 /* advance 'end' to point to email start delimiter */
323 for (end = 0; end < len && msg[end] != '<'; end++)
327 * When end points at the '<' that we found, it should have
328 * matching '>' later, which means 'end' must be strictly
334 if (part == 'n' || part == 'N') { /* name */
335 while (end > 0 && isspace(msg[end - 1]))
337 if (part != 'N' || !msg[end] || !msg[end + 1] ||
338 mailmap_name(sb, msg + end + 2) < 0)
339 strbuf_add(sb, msg, end);
340 return placeholder_len;
342 start = ++end; /* save email start position */
344 /* advance 'end' to point to email end delimiter */
345 for ( ; end < len && msg[end] != '>'; end++)
351 if (part == 'e') { /* email */
352 strbuf_add(sb, msg + start, end - start);
353 return placeholder_len;
356 /* advance 'start' to point to date start delimiter */
357 for (start = end + 1; start < len && isspace(msg[start]); start++)
361 date = strtoul(msg + start, &ep, 10);
362 if (msg + start == ep)
365 if (part == 't') { /* date, UNIX timestamp */
366 strbuf_add(sb, msg + start, ep - (msg + start));
367 return placeholder_len;
371 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
373 if (start + 1 < len) {
374 tz = strtoul(msg + start + 1, NULL, 10);
375 if (msg[start] == '-')
381 strbuf_addstr(sb, show_date(date, tz, dmode));
382 return placeholder_len;
383 case 'D': /* date, RFC2822 style */
384 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
385 return placeholder_len;
386 case 'r': /* date, relative */
387 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
388 return placeholder_len;
389 case 'i': /* date, ISO 8601 */
390 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
391 return placeholder_len;
396 * bogus commit, 'sb' cannot be updated, but we still need to
397 * compute a valid return value.
399 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
400 || part == 'D' || part == 'r' || part == 'i')
401 return placeholder_len;
403 return 0; /* unknown placeholder */
411 struct format_commit_context {
412 const struct commit *commit;
413 enum date_mode dmode;
415 /* These offsets are relative to the start of the commit message. */
416 int commit_header_parsed;
417 struct chunk subject;
419 struct chunk committer;
420 struct chunk encoding;
423 /* The following ones are relative to the result struct strbuf. */
424 struct chunk abbrev_commit_hash;
425 struct chunk abbrev_tree_hash;
426 struct chunk abbrev_parent_hashes;
429 static int add_again(struct strbuf *sb, struct chunk *chunk)
432 strbuf_adddup(sb, chunk->off, chunk->len);
437 * We haven't seen this chunk before. Our caller is surely
438 * going to add it the hard way now. Remember the most likely
439 * start of the to-be-added chunk: the current end of the
442 chunk->off = sb->len;
446 static void parse_commit_header(struct format_commit_context *context)
448 const char *msg = context->commit->buffer;
450 enum { HEADER, SUBJECT, BODY } state;
452 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
454 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
457 if (state == SUBJECT) {
458 context->subject.off = i;
459 context->subject.len = eol - i;
464 /* strip empty lines */
465 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
467 } else if (!prefixcmp(msg + i, "author ")) {
468 context->author.off = i + 7;
469 context->author.len = eol - i - 7;
470 } else if (!prefixcmp(msg + i, "committer ")) {
471 context->committer.off = i + 10;
472 context->committer.len = eol - i - 10;
473 } else if (!prefixcmp(msg + i, "encoding ")) {
474 context->encoding.off = i + 9;
475 context->encoding.len = eol - i - 9;
481 context->body_off = i;
482 context->commit_header_parsed = 1;
485 static void format_decoration(struct strbuf *sb, const struct commit *commit)
487 struct name_decoration *d;
488 const char *prefix = " (";
490 load_ref_decorations();
491 d = lookup_decoration(&name_decoration, &commit->object);
493 strbuf_addstr(sb, prefix);
495 strbuf_addstr(sb, d->name);
498 if (prefix[0] == ',')
499 strbuf_addch(sb, ')');
502 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
505 struct format_commit_context *c = context;
506 const struct commit *commit = c->commit;
507 const char *msg = commit->buffer;
508 struct commit_list *p;
511 /* these are independent of the commit */
512 switch (placeholder[0]) {
514 if (!prefixcmp(placeholder + 1, "red")) {
515 strbuf_addstr(sb, "\033[31m");
517 } else if (!prefixcmp(placeholder + 1, "green")) {
518 strbuf_addstr(sb, "\033[32m");
520 } else if (!prefixcmp(placeholder + 1, "blue")) {
521 strbuf_addstr(sb, "\033[34m");
523 } else if (!prefixcmp(placeholder + 1, "reset")) {
524 strbuf_addstr(sb, "\033[m");
528 case 'n': /* newline */
529 strbuf_addch(sb, '\n');
532 /* %x00 == NUL, %x0a == LF, etc. */
533 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
535 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
537 strbuf_addch(sb, (h1<<4)|h2);
543 /* these depend on the commit */
544 if (!commit->object.parsed)
545 parse_object(commit->object.sha1);
547 switch (placeholder[0]) {
548 case 'H': /* commit hash */
549 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
551 case 'h': /* abbreviated commit hash */
552 if (add_again(sb, &c->abbrev_commit_hash))
554 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
556 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
558 case 'T': /* tree hash */
559 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
561 case 't': /* abbreviated tree hash */
562 if (add_again(sb, &c->abbrev_tree_hash))
564 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
566 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
568 case 'P': /* parent hashes */
569 for (p = commit->parents; p; p = p->next) {
570 if (p != commit->parents)
571 strbuf_addch(sb, ' ');
572 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
575 case 'p': /* abbreviated parent hashes */
576 if (add_again(sb, &c->abbrev_parent_hashes))
578 for (p = commit->parents; p; p = p->next) {
579 if (p != commit->parents)
580 strbuf_addch(sb, ' ');
581 strbuf_addstr(sb, find_unique_abbrev(
582 p->item->object.sha1, DEFAULT_ABBREV));
584 c->abbrev_parent_hashes.len = sb->len -
585 c->abbrev_parent_hashes.off;
587 case 'm': /* left/right/bottom */
588 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
590 : (commit->object.flags & SYMMETRIC_LEFT)
595 format_decoration(sb, commit);
599 /* For the rest we have to parse the commit header. */
600 if (!c->commit_header_parsed)
601 parse_commit_header(c);
603 switch (placeholder[0]) {
604 case 's': /* subject */
605 strbuf_add(sb, msg + c->subject.off, c->subject.len);
607 case 'a': /* author ... */
608 return format_person_part(sb, placeholder[1],
609 msg + c->author.off, c->author.len,
611 case 'c': /* committer ... */
612 return format_person_part(sb, placeholder[1],
613 msg + c->committer.off, c->committer.len,
615 case 'e': /* encoding */
616 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
619 strbuf_addstr(sb, msg + c->body_off);
622 return 0; /* unknown placeholder */
625 void format_commit_message(const struct commit *commit,
626 const void *format, struct strbuf *sb,
627 enum date_mode dmode)
629 struct format_commit_context context;
631 memset(&context, 0, sizeof(context));
632 context.commit = commit;
633 context.dmode = dmode;
634 strbuf_expand(sb, format, format_commit_item, &context);
637 static void pp_header(enum cmit_fmt fmt,
639 enum date_mode dmode,
640 const char *encoding,
641 const struct commit *commit,
645 int parents_shown = 0;
648 const char *line = *msg_p;
649 int linelen = get_one_line(*msg_p);
659 if (fmt == CMIT_FMT_RAW) {
660 strbuf_add(sb, line, linelen);
664 if (!memcmp(line, "parent ", 7)) {
666 die("bad parent line in commit");
670 if (!parents_shown) {
671 struct commit_list *parent;
673 for (parent = commit->parents, num = 0;
675 parent = parent->next, num++)
677 /* with enough slop */
678 strbuf_grow(sb, num * 50 + 20);
679 add_merge_info(fmt, sb, commit, abbrev);
684 * MEDIUM == DEFAULT shows only author with dates.
685 * FULL shows both authors but not dates.
686 * FULLER shows both authors and dates.
688 if (!memcmp(line, "author ", 7)) {
689 strbuf_grow(sb, linelen + 80);
690 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
692 if (!memcmp(line, "committer ", 10) &&
693 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
694 strbuf_grow(sb, linelen + 80);
695 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
700 void pp_title_line(enum cmit_fmt fmt,
704 const char *after_subject,
705 const char *encoding,
710 strbuf_init(&title, 80);
713 const char *line = *msg_p;
714 int linelen = get_one_line(line);
717 if (!linelen || is_empty_line(line, &linelen))
720 strbuf_grow(&title, linelen + 2);
722 if (fmt == CMIT_FMT_EMAIL) {
723 strbuf_addch(&title, '\n');
725 strbuf_addch(&title, ' ');
727 strbuf_add(&title, line, linelen);
730 strbuf_grow(sb, title.len + 1024);
732 strbuf_addstr(sb, subject);
733 add_rfc2047(sb, title.buf, title.len, encoding);
735 strbuf_addbuf(sb, &title);
737 strbuf_addch(sb, '\n');
739 if (need_8bit_cte > 0) {
740 const char *header_fmt =
741 "MIME-Version: 1.0\n"
742 "Content-Type: text/plain; charset=%s\n"
743 "Content-Transfer-Encoding: 8bit\n";
744 strbuf_addf(sb, header_fmt, encoding);
747 strbuf_addstr(sb, after_subject);
749 if (fmt == CMIT_FMT_EMAIL) {
750 strbuf_addch(sb, '\n');
752 strbuf_release(&title);
755 void pp_remainder(enum cmit_fmt fmt,
762 const char *line = *msg_p;
763 int linelen = get_one_line(line);
769 if (is_empty_line(line, &linelen)) {
772 if (fmt == CMIT_FMT_SHORT)
777 strbuf_grow(sb, linelen + indent + 20);
779 memset(sb->buf + sb->len, ' ', indent);
780 strbuf_setlen(sb, sb->len + indent);
782 strbuf_add(sb, line, linelen);
783 strbuf_addch(sb, '\n');
787 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
788 struct strbuf *sb, int abbrev,
789 const char *subject, const char *after_subject,
790 enum date_mode dmode, int need_8bit_cte)
792 unsigned long beginning_of_body;
794 const char *msg = commit->buffer;
796 const char *encoding;
798 if (fmt == CMIT_FMT_USERFORMAT) {
799 format_commit_message(commit, user_format, sb, dmode);
803 encoding = (git_log_output_encoding
804 ? git_log_output_encoding
805 : git_commit_encoding);
808 reencoded = logmsg_reencode(commit, encoding);
813 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
817 * We need to check and emit Content-type: to mark it
818 * as 8-bit if we haven't done so.
820 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
823 for (in_body = i = 0; (ch = msg[i]); i++) {
825 /* author could be non 7-bit ASCII but
826 * the log may be so; skip over the
829 if (ch == '\n' && msg[i+1] == '\n')
832 else if (non_ascii(ch)) {
839 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
840 if (fmt != CMIT_FMT_ONELINE && !subject) {
841 strbuf_addch(sb, '\n');
844 /* Skip excess blank lines at the beginning of body, if any... */
846 int linelen = get_one_line(msg);
850 if (!is_empty_line(msg, &ll))
855 /* These formats treat the title line specially. */
856 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
857 pp_title_line(fmt, &msg, sb, subject,
858 after_subject, encoding, need_8bit_cte);
860 beginning_of_body = sb->len;
861 if (fmt != CMIT_FMT_ONELINE)
862 pp_remainder(fmt, &msg, sb, indent);
865 /* Make sure there is an EOLN for the non-oneline case */
866 if (fmt != CMIT_FMT_ONELINE)
867 strbuf_addch(sb, '\n');
870 * The caller may append additional body text in e-mail
871 * format. Make sure we did not strip the blank line
872 * between the header and the body.
874 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
875 strbuf_addch(sb, '\n');