6 #include "string-list.h"
11 static char *user_format;
13 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
16 user_format = xstrdup(cp);
18 rev->use_terminator = 1;
19 rev->commit_format = CMIT_FMT_USERFORMAT;
22 void get_commit_format(const char *arg, struct rev_info *rev)
25 static struct cmt_fmt_map {
30 { "raw", 1, CMIT_FMT_RAW },
31 { "medium", 1, CMIT_FMT_MEDIUM },
32 { "short", 1, CMIT_FMT_SHORT },
33 { "email", 1, CMIT_FMT_EMAIL },
34 { "full", 5, CMIT_FMT_FULL },
35 { "fuller", 5, CMIT_FMT_FULLER },
36 { "oneline", 1, CMIT_FMT_ONELINE },
39 rev->use_terminator = 0;
41 rev->commit_format = CMIT_FMT_DEFAULT;
44 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
45 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
48 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
49 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
50 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
51 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
52 rev->use_terminator = 1;
53 rev->commit_format = cmt_fmts[i].v;
57 if (strchr(arg, '%')) {
58 save_user_format(rev, arg, 1);
62 die("invalid --pretty format: %s", arg);
66 * Generic support for pretty-printing the header
68 static int get_one_line(const char *msg)
83 /* High bit set, or ISO-2022-INT */
86 return !isascii(ch) || ch == '\033';
89 int has_non_ascii(const char *s)
94 while ((ch = *s++) != '\0') {
101 static int is_rfc2047_special(char ch)
103 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
106 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
107 const char *encoding)
111 for (i = 0; i < len; i++) {
115 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
118 strbuf_add(sb, line, len);
122 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
123 strbuf_addf(sb, "=?%s?q?", encoding);
124 for (i = last = 0; i < len; i++) {
125 unsigned ch = line[i] & 0xFF;
127 * We encode ' ' using '=20' even though rfc2047
128 * allows using '_' for readability. Unfortunately,
129 * many programs do not understand this and just
130 * leave the underscore in place.
132 if (is_rfc2047_special(ch) || ch == ' ') {
133 strbuf_add(sb, line + last, i - last);
134 strbuf_addf(sb, "=%02X", ch);
138 strbuf_add(sb, line + last, len - last);
139 strbuf_addstr(sb, "?=");
142 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
143 const char *line, enum date_mode dmode,
144 const char *encoding)
151 if (fmt == CMIT_FMT_ONELINE)
153 date = strchr(line, '>');
156 namelen = ++date - line;
157 time = strtoul(date, &date, 10);
158 tz = strtol(date, NULL, 10);
160 if (fmt == CMIT_FMT_EMAIL) {
161 char *name_tail = strchr(line, '<');
162 int display_name_length;
165 while (line < name_tail && isspace(name_tail[-1]))
167 display_name_length = name_tail - line;
168 strbuf_addstr(sb, "From: ");
169 add_rfc2047(sb, line, display_name_length, encoding);
170 strbuf_add(sb, name_tail, namelen - display_name_length);
171 strbuf_addch(sb, '\n');
173 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
174 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
178 case CMIT_FMT_MEDIUM:
179 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
182 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
184 case CMIT_FMT_FULLER:
185 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
193 static int is_empty_line(const char *line, int *len_p)
196 while (len && isspace(line[len-1]))
202 static const char *skip_empty_lines(const char *msg)
205 int linelen = get_one_line(msg);
209 if (!is_empty_line(msg, &ll))
216 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
217 const struct commit *commit, int abbrev)
219 struct commit_list *parent = commit->parents;
221 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
222 !parent || !parent->next)
225 strbuf_addstr(sb, "Merge:");
228 struct commit *p = parent->item;
229 const char *hex = NULL;
231 hex = find_unique_abbrev(p->object.sha1, abbrev);
233 hex = sha1_to_hex(p->object.sha1);
234 parent = parent->next;
236 strbuf_addf(sb, " %s", hex);
238 strbuf_addch(sb, '\n');
241 static char *get_header(const struct commit *commit, const char *key)
243 int key_len = strlen(key);
244 const char *line = commit->buffer;
247 const char *eol = strchr(line, '\n'), *next;
252 eol = line + strlen(line);
256 if (eol - line > key_len &&
257 !strncmp(line, key, key_len) &&
258 line[key_len] == ' ') {
259 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
265 static char *replace_encoding_header(char *buf, const char *encoding)
267 struct strbuf tmp = STRBUF_INIT;
271 /* guess if there is an encoding header before a \n\n */
272 while (strncmp(cp, "encoding ", strlen("encoding "))) {
273 cp = strchr(cp, '\n');
274 if (!cp || *++cp == '\n')
278 cp = strchr(cp, '\n');
280 return buf; /* should not happen but be defensive */
281 len = cp + 1 - (buf + start);
283 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
284 if (is_encoding_utf8(encoding)) {
285 /* we have re-coded to UTF-8; drop the header */
286 strbuf_remove(&tmp, start, len);
288 /* just replaces XXXX in 'encoding XXXX\n' */
289 strbuf_splice(&tmp, start + strlen("encoding "),
290 len - strlen("encoding \n"),
291 encoding, strlen(encoding));
293 return strbuf_detach(&tmp, NULL);
296 static char *logmsg_reencode(const struct commit *commit,
297 const char *output_encoding)
299 static const char *utf8 = "UTF-8";
300 const char *use_encoding;
304 if (!*output_encoding)
306 encoding = get_header(commit, "encoding");
307 use_encoding = encoding ? encoding : utf8;
308 if (!strcmp(use_encoding, output_encoding))
309 if (encoding) /* we'll strip encoding header later */
310 out = xstrdup(commit->buffer);
312 return NULL; /* nothing to do */
314 out = reencode_string(commit->buffer,
315 output_encoding, use_encoding);
317 out = replace_encoding_header(out, output_encoding);
323 static int mailmap_name(char *email, int email_len, char *name, int name_len)
325 static struct string_list *mail_map;
327 mail_map = xcalloc(1, sizeof(*mail_map));
328 read_mailmap(mail_map, NULL);
330 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
333 static size_t format_person_part(struct strbuf *sb, char part,
334 const char *msg, int len, enum date_mode dmode)
336 /* currently all placeholders have same length */
337 const int placeholder_len = 2;
338 int start, end, tz = 0;
339 unsigned long date = 0;
341 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
342 char person_name[1024];
343 char person_mail[1024];
345 /* advance 'end' to point to email start delimiter */
346 for (end = 0; end < len && msg[end] != '<'; end++)
350 * When end points at the '<' that we found, it should have
351 * matching '>' later, which means 'end' must be strictly
357 /* Seek for both name and email part */
360 while (name_end > name_start && isspace(*(name_end-1)))
362 mail_start = msg+end+1;
363 mail_end = mail_start;
364 while (mail_end < msg_end && *mail_end != '>')
366 if (mail_end == msg_end)
370 if (part == 'N' || part == 'E') { /* mailmap lookup */
371 strlcpy(person_name, name_start, name_end-name_start+1);
372 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
373 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
374 name_start = person_name;
375 name_end = name_start + strlen(person_name);
376 mail_start = person_mail;
377 mail_end = mail_start + strlen(person_mail);
379 if (part == 'n' || part == 'N') { /* name */
380 strbuf_add(sb, name_start, name_end-name_start);
381 return placeholder_len;
383 if (part == 'e' || part == 'E') { /* email */
384 strbuf_add(sb, mail_start, mail_end-mail_start);
385 return placeholder_len;
388 /* advance 'start' to point to date start delimiter */
389 for (start = end + 1; start < len && isspace(msg[start]); start++)
393 date = strtoul(msg + start, &ep, 10);
394 if (msg + start == ep)
397 if (part == 't') { /* date, UNIX timestamp */
398 strbuf_add(sb, msg + start, ep - (msg + start));
399 return placeholder_len;
403 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
405 if (start + 1 < len) {
406 tz = strtoul(msg + start + 1, NULL, 10);
407 if (msg[start] == '-')
413 strbuf_addstr(sb, show_date(date, tz, dmode));
414 return placeholder_len;
415 case 'D': /* date, RFC2822 style */
416 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
417 return placeholder_len;
418 case 'r': /* date, relative */
419 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
420 return placeholder_len;
421 case 'i': /* date, ISO 8601 */
422 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
423 return placeholder_len;
428 * bogus commit, 'sb' cannot be updated, but we still need to
429 * compute a valid return value.
431 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
432 || part == 'D' || part == 'r' || part == 'i')
433 return placeholder_len;
435 return 0; /* unknown placeholder */
443 struct format_commit_context {
444 const struct commit *commit;
445 enum date_mode dmode;
446 unsigned commit_header_parsed:1;
447 unsigned commit_message_parsed:1;
449 /* These offsets are relative to the start of the commit message. */
451 struct chunk committer;
452 struct chunk encoding;
457 /* The following ones are relative to the result struct strbuf. */
458 struct chunk abbrev_commit_hash;
459 struct chunk abbrev_tree_hash;
460 struct chunk abbrev_parent_hashes;
463 static int add_again(struct strbuf *sb, struct chunk *chunk)
466 strbuf_adddup(sb, chunk->off, chunk->len);
471 * We haven't seen this chunk before. Our caller is surely
472 * going to add it the hard way now. Remember the most likely
473 * start of the to-be-added chunk: the current end of the
476 chunk->off = sb->len;
480 static void parse_commit_header(struct format_commit_context *context)
482 const char *msg = context->commit->buffer;
485 for (i = 0; msg[i]; i++) {
487 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
492 } else if (!prefixcmp(msg + i, "author ")) {
493 context->author.off = i + 7;
494 context->author.len = eol - i - 7;
495 } else if (!prefixcmp(msg + i, "committer ")) {
496 context->committer.off = i + 10;
497 context->committer.len = eol - i - 10;
498 } else if (!prefixcmp(msg + i, "encoding ")) {
499 context->encoding.off = i + 9;
500 context->encoding.len = eol - i - 9;
504 context->message_off = i;
505 context->commit_header_parsed = 1;
508 static int istitlechar(char c)
510 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
511 (c >= '0' && c <= '9') || c == '.' || c == '_';
514 static void format_sanitized_subject(struct strbuf *sb, const char *msg)
517 size_t start_len = sb->len;
520 for (; *msg && *msg != '\n'; msg++) {
521 if (istitlechar(*msg)) {
523 strbuf_addch(sb, '-');
525 strbuf_addch(sb, *msg);
527 while (*(msg+1) == '.')
533 /* trim any trailing '.' or '-' characters */
535 while (sb->len - trimlen > start_len &&
536 (sb->buf[sb->len - 1 - trimlen] == '.'
537 || sb->buf[sb->len - 1 - trimlen] == '-'))
539 strbuf_remove(sb, sb->len - trimlen, trimlen);
542 const char *format_subject(struct strbuf *sb, const char *msg,
543 const char *line_separator)
548 const char *line = msg;
549 int linelen = get_one_line(line);
552 if (!linelen || is_empty_line(line, &linelen))
557 strbuf_grow(sb, linelen + 2);
559 strbuf_addstr(sb, line_separator);
560 strbuf_add(sb, line, linelen);
566 static void parse_commit_message(struct format_commit_context *c)
568 const char *msg = c->commit->buffer + c->message_off;
569 const char *start = c->commit->buffer;
571 msg = skip_empty_lines(msg);
572 c->subject_off = msg - start;
574 msg = format_subject(NULL, msg, NULL);
575 msg = skip_empty_lines(msg);
576 c->body_off = msg - start;
578 c->commit_message_parsed = 1;
581 static void format_decoration(struct strbuf *sb, const struct commit *commit)
583 struct name_decoration *d;
584 const char *prefix = " (";
586 load_ref_decorations(DECORATE_SHORT_REFS);
587 d = lookup_decoration(&name_decoration, &commit->object);
589 strbuf_addstr(sb, prefix);
591 strbuf_addstr(sb, d->name);
594 if (prefix[0] == ',')
595 strbuf_addch(sb, ')');
598 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
601 struct format_commit_context *c = context;
602 const struct commit *commit = c->commit;
603 const char *msg = commit->buffer;
604 struct commit_list *p;
607 /* these are independent of the commit */
608 switch (placeholder[0]) {
610 if (placeholder[1] == '(') {
611 const char *end = strchr(placeholder + 2, ')');
612 char color[COLOR_MAXLEN];
615 color_parse_mem(placeholder + 2,
616 end - (placeholder + 2),
617 "--pretty format", color);
618 strbuf_addstr(sb, color);
619 return end - placeholder + 1;
621 if (!prefixcmp(placeholder + 1, "red")) {
622 strbuf_addstr(sb, GIT_COLOR_RED);
624 } else if (!prefixcmp(placeholder + 1, "green")) {
625 strbuf_addstr(sb, GIT_COLOR_GREEN);
627 } else if (!prefixcmp(placeholder + 1, "blue")) {
628 strbuf_addstr(sb, GIT_COLOR_BLUE);
630 } else if (!prefixcmp(placeholder + 1, "reset")) {
631 strbuf_addstr(sb, GIT_COLOR_RESET);
635 case 'n': /* newline */
636 strbuf_addch(sb, '\n');
639 /* %x00 == NUL, %x0a == LF, etc. */
640 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
642 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
644 strbuf_addch(sb, (h1<<4)|h2);
650 /* these depend on the commit */
651 if (!commit->object.parsed)
652 parse_object(commit->object.sha1);
654 switch (placeholder[0]) {
655 case 'H': /* commit hash */
656 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
658 case 'h': /* abbreviated commit hash */
659 if (add_again(sb, &c->abbrev_commit_hash))
661 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
663 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
665 case 'T': /* tree hash */
666 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
668 case 't': /* abbreviated tree hash */
669 if (add_again(sb, &c->abbrev_tree_hash))
671 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
673 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
675 case 'P': /* parent hashes */
676 for (p = commit->parents; p; p = p->next) {
677 if (p != commit->parents)
678 strbuf_addch(sb, ' ');
679 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
682 case 'p': /* abbreviated parent hashes */
683 if (add_again(sb, &c->abbrev_parent_hashes))
685 for (p = commit->parents; p; p = p->next) {
686 if (p != commit->parents)
687 strbuf_addch(sb, ' ');
688 strbuf_addstr(sb, find_unique_abbrev(
689 p->item->object.sha1, DEFAULT_ABBREV));
691 c->abbrev_parent_hashes.len = sb->len -
692 c->abbrev_parent_hashes.off;
694 case 'm': /* left/right/bottom */
695 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
697 : (commit->object.flags & SYMMETRIC_LEFT)
702 format_decoration(sb, commit);
706 /* For the rest we have to parse the commit header. */
707 if (!c->commit_header_parsed)
708 parse_commit_header(c);
710 switch (placeholder[0]) {
711 case 'a': /* author ... */
712 return format_person_part(sb, placeholder[1],
713 msg + c->author.off, c->author.len,
715 case 'c': /* committer ... */
716 return format_person_part(sb, placeholder[1],
717 msg + c->committer.off, c->committer.len,
719 case 'e': /* encoding */
720 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
724 /* Now we need to parse the commit message. */
725 if (!c->commit_message_parsed)
726 parse_commit_message(c);
728 switch (placeholder[0]) {
729 case 's': /* subject */
730 format_subject(sb, msg + c->subject_off, " ");
732 case 'f': /* sanitized subject */
733 format_sanitized_subject(sb, msg + c->subject_off);
736 strbuf_addstr(sb, msg + c->body_off);
739 return 0; /* unknown placeholder */
742 void format_commit_message(const struct commit *commit,
743 const void *format, struct strbuf *sb,
744 enum date_mode dmode)
746 struct format_commit_context context;
748 memset(&context, 0, sizeof(context));
749 context.commit = commit;
750 context.dmode = dmode;
751 strbuf_expand(sb, format, format_commit_item, &context);
754 static void pp_header(enum cmit_fmt fmt,
756 enum date_mode dmode,
757 const char *encoding,
758 const struct commit *commit,
762 int parents_shown = 0;
765 const char *line = *msg_p;
766 int linelen = get_one_line(*msg_p);
776 if (fmt == CMIT_FMT_RAW) {
777 strbuf_add(sb, line, linelen);
781 if (!memcmp(line, "parent ", 7)) {
783 die("bad parent line in commit");
787 if (!parents_shown) {
788 struct commit_list *parent;
790 for (parent = commit->parents, num = 0;
792 parent = parent->next, num++)
794 /* with enough slop */
795 strbuf_grow(sb, num * 50 + 20);
796 add_merge_info(fmt, sb, commit, abbrev);
801 * MEDIUM == DEFAULT shows only author with dates.
802 * FULL shows both authors but not dates.
803 * FULLER shows both authors and dates.
805 if (!memcmp(line, "author ", 7)) {
806 strbuf_grow(sb, linelen + 80);
807 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
809 if (!memcmp(line, "committer ", 10) &&
810 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
811 strbuf_grow(sb, linelen + 80);
812 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
817 void pp_title_line(enum cmit_fmt fmt,
821 const char *after_subject,
822 const char *encoding,
825 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
828 strbuf_init(&title, 80);
829 *msg_p = format_subject(&title, *msg_p, line_separator);
831 strbuf_grow(sb, title.len + 1024);
833 strbuf_addstr(sb, subject);
834 add_rfc2047(sb, title.buf, title.len, encoding);
836 strbuf_addbuf(sb, &title);
838 strbuf_addch(sb, '\n');
840 if (need_8bit_cte > 0) {
841 const char *header_fmt =
842 "MIME-Version: 1.0\n"
843 "Content-Type: text/plain; charset=%s\n"
844 "Content-Transfer-Encoding: 8bit\n";
845 strbuf_addf(sb, header_fmt, encoding);
848 strbuf_addstr(sb, after_subject);
850 if (fmt == CMIT_FMT_EMAIL) {
851 strbuf_addch(sb, '\n');
853 strbuf_release(&title);
856 void pp_remainder(enum cmit_fmt fmt,
863 const char *line = *msg_p;
864 int linelen = get_one_line(line);
870 if (is_empty_line(line, &linelen)) {
873 if (fmt == CMIT_FMT_SHORT)
878 strbuf_grow(sb, linelen + indent + 20);
880 memset(sb->buf + sb->len, ' ', indent);
881 strbuf_setlen(sb, sb->len + indent);
883 strbuf_add(sb, line, linelen);
884 strbuf_addch(sb, '\n');
888 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
890 const char *encoding;
892 encoding = (git_log_output_encoding
893 ? git_log_output_encoding
894 : git_commit_encoding);
898 *encoding_p = encoding;
899 return logmsg_reencode(commit, encoding);
902 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
903 struct strbuf *sb, int abbrev,
904 const char *subject, const char *after_subject,
905 enum date_mode dmode, int need_8bit_cte)
907 unsigned long beginning_of_body;
909 const char *msg = commit->buffer;
911 const char *encoding;
913 if (fmt == CMIT_FMT_USERFORMAT) {
914 format_commit_message(commit, user_format, sb, dmode);
918 reencoded = reencode_commit_message(commit, &encoding);
923 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
927 * We need to check and emit Content-type: to mark it
928 * as 8-bit if we haven't done so.
930 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
933 for (in_body = i = 0; (ch = msg[i]); i++) {
935 /* author could be non 7-bit ASCII but
936 * the log may be so; skip over the
939 if (ch == '\n' && msg[i+1] == '\n')
942 else if (non_ascii(ch)) {
949 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
950 if (fmt != CMIT_FMT_ONELINE && !subject) {
951 strbuf_addch(sb, '\n');
954 /* Skip excess blank lines at the beginning of body, if any... */
955 msg = skip_empty_lines(msg);
957 /* These formats treat the title line specially. */
958 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
959 pp_title_line(fmt, &msg, sb, subject,
960 after_subject, encoding, need_8bit_cte);
962 beginning_of_body = sb->len;
963 if (fmt != CMIT_FMT_ONELINE)
964 pp_remainder(fmt, &msg, sb, indent);
967 /* Make sure there is an EOLN for the non-oneline case */
968 if (fmt != CMIT_FMT_ONELINE)
969 strbuf_addch(sb, '\n');
972 * The caller may append additional body text in e-mail
973 * format. Make sure we did not strip the blank line
974 * between the header and the body.
976 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
977 strbuf_addch(sb, '\n');