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 static int is_rfc2047_special(char ch)
91 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
94 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
99 for (i = 0; i < len; i++) {
103 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
106 strbuf_add(sb, line, len);
110 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
111 strbuf_addf(sb, "=?%s?q?", encoding);
112 for (i = last = 0; i < len; i++) {
113 unsigned ch = line[i] & 0xFF;
115 * We encode ' ' using '=20' even though rfc2047
116 * allows using '_' for readability. Unfortunately,
117 * many programs do not understand this and just
118 * leave the underscore in place.
120 if (is_rfc2047_special(ch) || ch == ' ') {
121 strbuf_add(sb, line + last, i - last);
122 strbuf_addf(sb, "=%02X", ch);
126 strbuf_add(sb, line + last, len - last);
127 strbuf_addstr(sb, "?=");
130 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
131 const char *line, enum date_mode dmode,
132 const char *encoding)
138 const char *filler = " ";
140 if (fmt == CMIT_FMT_ONELINE)
142 date = strchr(line, '>');
145 namelen = ++date - line;
146 time = strtoul(date, &date, 10);
147 tz = strtol(date, NULL, 10);
149 if (fmt == CMIT_FMT_EMAIL) {
150 char *name_tail = strchr(line, '<');
151 int display_name_length;
154 while (line < name_tail && isspace(name_tail[-1]))
156 display_name_length = name_tail - line;
158 strbuf_addstr(sb, "From: ");
159 add_rfc2047(sb, line, display_name_length, encoding);
160 strbuf_add(sb, name_tail, namelen - display_name_length);
161 strbuf_addch(sb, '\n');
163 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
164 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
165 filler, namelen, line);
168 case CMIT_FMT_MEDIUM:
169 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
172 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
174 case CMIT_FMT_FULLER:
175 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
183 static int is_empty_line(const char *line, int *len_p)
186 while (len && isspace(line[len-1]))
192 static const char *skip_empty_lines(const char *msg)
195 int linelen = get_one_line(msg);
199 if (!is_empty_line(msg, &ll))
206 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
207 const struct commit *commit, int abbrev)
209 struct commit_list *parent = commit->parents;
211 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
212 !parent || !parent->next)
215 strbuf_addstr(sb, "Merge:");
218 struct commit *p = parent->item;
219 const char *hex = NULL;
221 hex = find_unique_abbrev(p->object.sha1, abbrev);
223 hex = sha1_to_hex(p->object.sha1);
224 parent = parent->next;
226 strbuf_addf(sb, " %s", hex);
228 strbuf_addch(sb, '\n');
231 static char *get_header(const struct commit *commit, const char *key)
233 int key_len = strlen(key);
234 const char *line = commit->buffer;
237 const char *eol = strchr(line, '\n'), *next;
242 eol = line + strlen(line);
246 if (eol - line > key_len &&
247 !strncmp(line, key, key_len) &&
248 line[key_len] == ' ') {
249 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
255 static char *replace_encoding_header(char *buf, const char *encoding)
257 struct strbuf tmp = STRBUF_INIT;
261 /* guess if there is an encoding header before a \n\n */
262 while (strncmp(cp, "encoding ", strlen("encoding "))) {
263 cp = strchr(cp, '\n');
264 if (!cp || *++cp == '\n')
268 cp = strchr(cp, '\n');
270 return buf; /* should not happen but be defensive */
271 len = cp + 1 - (buf + start);
273 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
274 if (is_encoding_utf8(encoding)) {
275 /* we have re-coded to UTF-8; drop the header */
276 strbuf_remove(&tmp, start, len);
278 /* just replaces XXXX in 'encoding XXXX\n' */
279 strbuf_splice(&tmp, start + strlen("encoding "),
280 len - strlen("encoding \n"),
281 encoding, strlen(encoding));
283 return strbuf_detach(&tmp, NULL);
286 static char *logmsg_reencode(const struct commit *commit,
287 const char *output_encoding)
289 static const char *utf8 = "utf-8";
290 const char *use_encoding;
294 if (!*output_encoding)
296 encoding = get_header(commit, "encoding");
297 use_encoding = encoding ? encoding : utf8;
298 if (!strcmp(use_encoding, output_encoding))
299 if (encoding) /* we'll strip encoding header later */
300 out = xstrdup(commit->buffer);
302 return NULL; /* nothing to do */
304 out = reencode_string(commit->buffer,
305 output_encoding, use_encoding);
307 out = replace_encoding_header(out, output_encoding);
313 static int mailmap_name(char *email, int email_len, char *name, int name_len)
315 static struct string_list *mail_map;
317 mail_map = xcalloc(1, sizeof(*mail_map));
318 read_mailmap(mail_map, NULL);
320 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
323 static size_t format_person_part(struct strbuf *sb, char part,
324 const char *msg, int len, enum date_mode dmode)
326 /* currently all placeholders have same length */
327 const int placeholder_len = 2;
328 int start, end, tz = 0;
329 unsigned long date = 0;
331 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
332 char person_name[1024];
333 char person_mail[1024];
335 /* advance 'end' to point to email start delimiter */
336 for (end = 0; end < len && msg[end] != '<'; end++)
340 * When end points at the '<' that we found, it should have
341 * matching '>' later, which means 'end' must be strictly
347 /* Seek for both name and email part */
350 while (name_end > name_start && isspace(*(name_end-1)))
352 mail_start = msg+end+1;
353 mail_end = mail_start;
354 while (mail_end < msg_end && *mail_end != '>')
356 if (mail_end == msg_end)
360 if (part == 'N' || part == 'E') { /* mailmap lookup */
361 strlcpy(person_name, name_start, name_end-name_start+1);
362 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
363 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
364 name_start = person_name;
365 name_end = name_start + strlen(person_name);
366 mail_start = person_mail;
367 mail_end = mail_start + strlen(person_mail);
369 if (part == 'n' || part == 'N') { /* name */
370 strbuf_add(sb, name_start, name_end-name_start);
371 return placeholder_len;
373 if (part == 'e' || part == 'E') { /* email */
374 strbuf_add(sb, mail_start, mail_end-mail_start);
375 return placeholder_len;
378 /* advance 'start' to point to date start delimiter */
379 for (start = end + 1; start < len && isspace(msg[start]); start++)
383 date = strtoul(msg + start, &ep, 10);
384 if (msg + start == ep)
387 if (part == 't') { /* date, UNIX timestamp */
388 strbuf_add(sb, msg + start, ep - (msg + start));
389 return placeholder_len;
393 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
395 if (start + 1 < len) {
396 tz = strtoul(msg + start + 1, NULL, 10);
397 if (msg[start] == '-')
403 strbuf_addstr(sb, show_date(date, tz, dmode));
404 return placeholder_len;
405 case 'D': /* date, RFC2822 style */
406 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
407 return placeholder_len;
408 case 'r': /* date, relative */
409 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
410 return placeholder_len;
411 case 'i': /* date, ISO 8601 */
412 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
413 return placeholder_len;
418 * bogus commit, 'sb' cannot be updated, but we still need to
419 * compute a valid return value.
421 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
422 || part == 'D' || part == 'r' || part == 'i')
423 return placeholder_len;
425 return 0; /* unknown placeholder */
433 struct format_commit_context {
434 const struct commit *commit;
435 enum date_mode dmode;
436 unsigned commit_header_parsed:1;
437 unsigned commit_message_parsed:1;
439 /* These offsets are relative to the start of the commit message. */
441 struct chunk committer;
442 struct chunk encoding;
447 /* The following ones are relative to the result struct strbuf. */
448 struct chunk abbrev_commit_hash;
449 struct chunk abbrev_tree_hash;
450 struct chunk abbrev_parent_hashes;
453 static int add_again(struct strbuf *sb, struct chunk *chunk)
456 strbuf_adddup(sb, chunk->off, chunk->len);
461 * We haven't seen this chunk before. Our caller is surely
462 * going to add it the hard way now. Remember the most likely
463 * start of the to-be-added chunk: the current end of the
466 chunk->off = sb->len;
470 static void parse_commit_header(struct format_commit_context *context)
472 const char *msg = context->commit->buffer;
475 for (i = 0; msg[i]; i++) {
477 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
482 } else if (!prefixcmp(msg + i, "author ")) {
483 context->author.off = i + 7;
484 context->author.len = eol - i - 7;
485 } else if (!prefixcmp(msg + i, "committer ")) {
486 context->committer.off = i + 10;
487 context->committer.len = eol - i - 10;
488 } else if (!prefixcmp(msg + i, "encoding ")) {
489 context->encoding.off = i + 9;
490 context->encoding.len = eol - i - 9;
494 context->message_off = i;
495 context->commit_header_parsed = 1;
498 const char *format_subject(struct strbuf *sb, const char *msg,
499 const char *line_separator)
504 const char *line = msg;
505 int linelen = get_one_line(line);
508 if (!linelen || is_empty_line(line, &linelen))
513 strbuf_grow(sb, linelen + 2);
515 strbuf_addstr(sb, line_separator);
516 strbuf_add(sb, line, linelen);
522 static void parse_commit_message(struct format_commit_context *c)
524 const char *msg = c->commit->buffer + c->message_off;
525 const char *start = c->commit->buffer;
527 msg = skip_empty_lines(msg);
528 c->subject_off = msg - start;
530 msg = format_subject(NULL, msg, NULL);
531 msg = skip_empty_lines(msg);
532 c->body_off = msg - start;
534 c->commit_message_parsed = 1;
537 static void format_decoration(struct strbuf *sb, const struct commit *commit)
539 struct name_decoration *d;
540 const char *prefix = " (";
542 load_ref_decorations();
543 d = lookup_decoration(&name_decoration, &commit->object);
545 strbuf_addstr(sb, prefix);
547 strbuf_addstr(sb, d->name);
550 if (prefix[0] == ',')
551 strbuf_addch(sb, ')');
554 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
557 struct format_commit_context *c = context;
558 const struct commit *commit = c->commit;
559 const char *msg = commit->buffer;
560 struct commit_list *p;
563 /* these are independent of the commit */
564 switch (placeholder[0]) {
566 if (placeholder[1] == '(') {
567 const char *end = strchr(placeholder + 2, ')');
568 char color[COLOR_MAXLEN];
571 color_parse_mem(placeholder + 2,
572 end - (placeholder + 2),
573 "--pretty format", color);
574 strbuf_addstr(sb, color);
575 return end - placeholder + 1;
577 if (!prefixcmp(placeholder + 1, "red")) {
578 strbuf_addstr(sb, GIT_COLOR_RED);
580 } else if (!prefixcmp(placeholder + 1, "green")) {
581 strbuf_addstr(sb, GIT_COLOR_GREEN);
583 } else if (!prefixcmp(placeholder + 1, "blue")) {
584 strbuf_addstr(sb, GIT_COLOR_BLUE);
586 } else if (!prefixcmp(placeholder + 1, "reset")) {
587 strbuf_addstr(sb, GIT_COLOR_RESET);
591 case 'n': /* newline */
592 strbuf_addch(sb, '\n');
595 /* %x00 == NUL, %x0a == LF, etc. */
596 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
598 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
600 strbuf_addch(sb, (h1<<4)|h2);
606 /* these depend on the commit */
607 if (!commit->object.parsed)
608 parse_object(commit->object.sha1);
610 switch (placeholder[0]) {
611 case 'H': /* commit hash */
612 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
614 case 'h': /* abbreviated commit hash */
615 if (add_again(sb, &c->abbrev_commit_hash))
617 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
619 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
621 case 'T': /* tree hash */
622 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
624 case 't': /* abbreviated tree hash */
625 if (add_again(sb, &c->abbrev_tree_hash))
627 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
629 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
631 case 'P': /* parent hashes */
632 for (p = commit->parents; p; p = p->next) {
633 if (p != commit->parents)
634 strbuf_addch(sb, ' ');
635 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
638 case 'p': /* abbreviated parent hashes */
639 if (add_again(sb, &c->abbrev_parent_hashes))
641 for (p = commit->parents; p; p = p->next) {
642 if (p != commit->parents)
643 strbuf_addch(sb, ' ');
644 strbuf_addstr(sb, find_unique_abbrev(
645 p->item->object.sha1, DEFAULT_ABBREV));
647 c->abbrev_parent_hashes.len = sb->len -
648 c->abbrev_parent_hashes.off;
650 case 'm': /* left/right/bottom */
651 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
653 : (commit->object.flags & SYMMETRIC_LEFT)
658 format_decoration(sb, commit);
662 /* For the rest we have to parse the commit header. */
663 if (!c->commit_header_parsed)
664 parse_commit_header(c);
666 switch (placeholder[0]) {
667 case 'a': /* author ... */
668 return format_person_part(sb, placeholder[1],
669 msg + c->author.off, c->author.len,
671 case 'c': /* committer ... */
672 return format_person_part(sb, placeholder[1],
673 msg + c->committer.off, c->committer.len,
675 case 'e': /* encoding */
676 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
680 /* Now we need to parse the commit message. */
681 if (!c->commit_message_parsed)
682 parse_commit_message(c);
684 switch (placeholder[0]) {
685 case 's': /* subject */
686 format_subject(sb, msg + c->subject_off, " ");
689 strbuf_addstr(sb, msg + c->body_off);
692 return 0; /* unknown placeholder */
695 void format_commit_message(const struct commit *commit,
696 const void *format, struct strbuf *sb,
697 enum date_mode dmode)
699 struct format_commit_context context;
701 memset(&context, 0, sizeof(context));
702 context.commit = commit;
703 context.dmode = dmode;
704 strbuf_expand(sb, format, format_commit_item, &context);
707 static void pp_header(enum cmit_fmt fmt,
709 enum date_mode dmode,
710 const char *encoding,
711 const struct commit *commit,
715 int parents_shown = 0;
718 const char *line = *msg_p;
719 int linelen = get_one_line(*msg_p);
729 if (fmt == CMIT_FMT_RAW) {
730 strbuf_add(sb, line, linelen);
734 if (!memcmp(line, "parent ", 7)) {
736 die("bad parent line in commit");
740 if (!parents_shown) {
741 struct commit_list *parent;
743 for (parent = commit->parents, num = 0;
745 parent = parent->next, num++)
747 /* with enough slop */
748 strbuf_grow(sb, num * 50 + 20);
749 add_merge_info(fmt, sb, commit, abbrev);
754 * MEDIUM == DEFAULT shows only author with dates.
755 * FULL shows both authors but not dates.
756 * FULLER shows both authors and dates.
758 if (!memcmp(line, "author ", 7)) {
759 strbuf_grow(sb, linelen + 80);
760 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
762 if (!memcmp(line, "committer ", 10) &&
763 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
764 strbuf_grow(sb, linelen + 80);
765 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
770 void pp_title_line(enum cmit_fmt fmt,
774 const char *after_subject,
775 const char *encoding,
778 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
781 strbuf_init(&title, 80);
782 *msg_p = format_subject(&title, *msg_p, line_separator);
784 strbuf_grow(sb, title.len + 1024);
786 strbuf_addstr(sb, subject);
787 add_rfc2047(sb, title.buf, title.len, encoding);
789 strbuf_addbuf(sb, &title);
791 strbuf_addch(sb, '\n');
793 if (need_8bit_cte > 0) {
794 const char *header_fmt =
795 "MIME-Version: 1.0\n"
796 "Content-Type: text/plain; charset=%s\n"
797 "Content-Transfer-Encoding: 8bit\n";
798 strbuf_addf(sb, header_fmt, encoding);
801 strbuf_addstr(sb, after_subject);
803 if (fmt == CMIT_FMT_EMAIL) {
804 strbuf_addch(sb, '\n');
806 strbuf_release(&title);
809 void pp_remainder(enum cmit_fmt fmt,
816 const char *line = *msg_p;
817 int linelen = get_one_line(line);
823 if (is_empty_line(line, &linelen)) {
826 if (fmt == CMIT_FMT_SHORT)
831 strbuf_grow(sb, linelen + indent + 20);
833 memset(sb->buf + sb->len, ' ', indent);
834 strbuf_setlen(sb, sb->len + indent);
836 strbuf_add(sb, line, linelen);
837 strbuf_addch(sb, '\n');
841 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
843 const char *encoding;
845 encoding = (git_log_output_encoding
846 ? git_log_output_encoding
847 : git_commit_encoding);
851 *encoding_p = encoding;
852 return logmsg_reencode(commit, encoding);
855 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
856 struct strbuf *sb, int abbrev,
857 const char *subject, const char *after_subject,
858 enum date_mode dmode, int need_8bit_cte)
860 unsigned long beginning_of_body;
862 const char *msg = commit->buffer;
864 const char *encoding;
866 if (fmt == CMIT_FMT_USERFORMAT) {
867 format_commit_message(commit, user_format, sb, dmode);
871 reencoded = reencode_commit_message(commit, &encoding);
876 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
880 * We need to check and emit Content-type: to mark it
881 * as 8-bit if we haven't done so.
883 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
886 for (in_body = i = 0; (ch = msg[i]); i++) {
888 /* author could be non 7-bit ASCII but
889 * the log may be so; skip over the
892 if (ch == '\n' && msg[i+1] == '\n')
895 else if (non_ascii(ch)) {
902 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
903 if (fmt != CMIT_FMT_ONELINE && !subject) {
904 strbuf_addch(sb, '\n');
907 /* Skip excess blank lines at the beginning of body, if any... */
908 msg = skip_empty_lines(msg);
910 /* These formats treat the title line specially. */
911 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
912 pp_title_line(fmt, &msg, sb, subject,
913 after_subject, encoding, need_8bit_cte);
915 beginning_of_body = sb->len;
916 if (fmt != CMIT_FMT_ONELINE)
917 pp_remainder(fmt, &msg, sb, indent);
920 /* Make sure there is an EOLN for the non-oneline case */
921 if (fmt != CMIT_FMT_ONELINE)
922 strbuf_addch(sb, '\n');
925 * The caller may append additional body text in e-mail
926 * format. Make sure we did not strip the blank line
927 * between the header and the body.
929 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
930 strbuf_addch(sb, '\n');