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 */
87 return ((ch & 0x80) || (ch == 0x1b));
90 static int is_rfc2047_special(char ch)
92 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
95 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
100 for (i = 0; i < len; i++) {
104 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
107 strbuf_add(sb, line, len);
111 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
112 strbuf_addf(sb, "=?%s?q?", encoding);
113 for (i = last = 0; i < len; i++) {
114 unsigned ch = line[i] & 0xFF;
116 * We encode ' ' using '=20' even though rfc2047
117 * allows using '_' for readability. Unfortunately,
118 * many programs do not understand this and just
119 * leave the underscore in place.
121 if (is_rfc2047_special(ch) || ch == ' ') {
122 strbuf_add(sb, line + last, i - last);
123 strbuf_addf(sb, "=%02X", ch);
127 strbuf_add(sb, line + last, len - last);
128 strbuf_addstr(sb, "?=");
131 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
132 const char *line, enum date_mode dmode,
133 const char *encoding)
139 const char *filler = " ";
141 if (fmt == CMIT_FMT_ONELINE)
143 date = strchr(line, '>');
146 namelen = ++date - line;
147 time = strtoul(date, &date, 10);
148 tz = strtol(date, NULL, 10);
150 if (fmt == CMIT_FMT_EMAIL) {
151 char *name_tail = strchr(line, '<');
152 int display_name_length;
155 while (line < name_tail && isspace(name_tail[-1]))
157 display_name_length = name_tail - line;
159 strbuf_addstr(sb, "From: ");
160 add_rfc2047(sb, line, display_name_length, encoding);
161 strbuf_add(sb, name_tail, namelen - display_name_length);
162 strbuf_addch(sb, '\n');
164 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
165 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
166 filler, namelen, line);
169 case CMIT_FMT_MEDIUM:
170 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
173 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
175 case CMIT_FMT_FULLER:
176 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
184 static int is_empty_line(const char *line, int *len_p)
187 while (len && isspace(line[len-1]))
193 static const char *skip_empty_lines(const char *msg)
196 int linelen = get_one_line(msg);
200 if (!is_empty_line(msg, &ll))
207 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
208 const struct commit *commit, int abbrev)
210 struct commit_list *parent = commit->parents;
212 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
213 !parent || !parent->next)
216 strbuf_addstr(sb, "Merge:");
219 struct commit *p = parent->item;
220 const char *hex = NULL;
222 hex = find_unique_abbrev(p->object.sha1, abbrev);
224 hex = sha1_to_hex(p->object.sha1);
225 parent = parent->next;
227 strbuf_addf(sb, " %s", hex);
229 strbuf_addch(sb, '\n');
232 static char *get_header(const struct commit *commit, const char *key)
234 int key_len = strlen(key);
235 const char *line = commit->buffer;
238 const char *eol = strchr(line, '\n'), *next;
243 eol = line + strlen(line);
247 if (eol - line > key_len &&
248 !strncmp(line, key, key_len) &&
249 line[key_len] == ' ') {
250 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
256 static char *replace_encoding_header(char *buf, const char *encoding)
258 struct strbuf tmp = STRBUF_INIT;
262 /* guess if there is an encoding header before a \n\n */
263 while (strncmp(cp, "encoding ", strlen("encoding "))) {
264 cp = strchr(cp, '\n');
265 if (!cp || *++cp == '\n')
269 cp = strchr(cp, '\n');
271 return buf; /* should not happen but be defensive */
272 len = cp + 1 - (buf + start);
274 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
275 if (is_encoding_utf8(encoding)) {
276 /* we have re-coded to UTF-8; drop the header */
277 strbuf_remove(&tmp, start, len);
279 /* just replaces XXXX in 'encoding XXXX\n' */
280 strbuf_splice(&tmp, start + strlen("encoding "),
281 len - strlen("encoding \n"),
282 encoding, strlen(encoding));
284 return strbuf_detach(&tmp, NULL);
287 static char *logmsg_reencode(const struct commit *commit,
288 const char *output_encoding)
290 static const char *utf8 = "utf-8";
291 const char *use_encoding;
295 if (!*output_encoding)
297 encoding = get_header(commit, "encoding");
298 use_encoding = encoding ? encoding : utf8;
299 if (!strcmp(use_encoding, output_encoding))
300 if (encoding) /* we'll strip encoding header later */
301 out = xstrdup(commit->buffer);
303 return NULL; /* nothing to do */
305 out = reencode_string(commit->buffer,
306 output_encoding, use_encoding);
308 out = replace_encoding_header(out, output_encoding);
314 static int mailmap_name(char *email, int email_len, char *name, int name_len)
316 static struct string_list *mail_map;
318 mail_map = xcalloc(1, sizeof(*mail_map));
319 read_mailmap(mail_map, NULL);
321 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
324 static size_t format_person_part(struct strbuf *sb, char part,
325 const char *msg, int len, enum date_mode dmode)
327 /* currently all placeholders have same length */
328 const int placeholder_len = 2;
329 int start, end, tz = 0;
330 unsigned long date = 0;
332 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
333 char person_name[1024];
334 char person_mail[1024];
336 /* advance 'end' to point to email start delimiter */
337 for (end = 0; end < len && msg[end] != '<'; end++)
341 * When end points at the '<' that we found, it should have
342 * matching '>' later, which means 'end' must be strictly
348 /* Seek for both name and email part */
351 while (name_end > name_start && isspace(*(name_end-1)))
353 mail_start = msg+end+1;
354 mail_end = mail_start;
355 while (mail_end < msg_end && *mail_end != '>')
357 if (mail_end == msg_end)
361 if (part == 'N' || part == 'E') { /* mailmap lookup */
362 strlcpy(person_name, name_start, name_end-name_start+1);
363 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
364 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
365 name_start = person_name;
366 name_end = name_start + strlen(person_name);
367 mail_start = person_mail;
368 mail_end = mail_start + strlen(person_mail);
370 if (part == 'n' || part == 'N') { /* name */
371 strbuf_add(sb, name_start, name_end-name_start);
372 return placeholder_len;
374 if (part == 'e' || part == 'E') { /* email */
375 strbuf_add(sb, mail_start, mail_end-mail_start);
376 return placeholder_len;
379 /* advance 'start' to point to date start delimiter */
380 for (start = end + 1; start < len && isspace(msg[start]); start++)
384 date = strtoul(msg + start, &ep, 10);
385 if (msg + start == ep)
388 if (part == 't') { /* date, UNIX timestamp */
389 strbuf_add(sb, msg + start, ep - (msg + start));
390 return placeholder_len;
394 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
396 if (start + 1 < len) {
397 tz = strtoul(msg + start + 1, NULL, 10);
398 if (msg[start] == '-')
404 strbuf_addstr(sb, show_date(date, tz, dmode));
405 return placeholder_len;
406 case 'D': /* date, RFC2822 style */
407 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
408 return placeholder_len;
409 case 'r': /* date, relative */
410 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
411 return placeholder_len;
412 case 'i': /* date, ISO 8601 */
413 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
414 return placeholder_len;
419 * bogus commit, 'sb' cannot be updated, but we still need to
420 * compute a valid return value.
422 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
423 || part == 'D' || part == 'r' || part == 'i')
424 return placeholder_len;
426 return 0; /* unknown placeholder */
434 struct format_commit_context {
435 const struct commit *commit;
436 enum date_mode dmode;
437 unsigned commit_header_parsed:1;
438 unsigned commit_message_parsed:1;
440 /* These offsets are relative to the start of the commit message. */
442 struct chunk committer;
443 struct chunk encoding;
448 /* The following ones are relative to the result struct strbuf. */
449 struct chunk abbrev_commit_hash;
450 struct chunk abbrev_tree_hash;
451 struct chunk abbrev_parent_hashes;
454 static int add_again(struct strbuf *sb, struct chunk *chunk)
457 strbuf_adddup(sb, chunk->off, chunk->len);
462 * We haven't seen this chunk before. Our caller is surely
463 * going to add it the hard way now. Remember the most likely
464 * start of the to-be-added chunk: the current end of the
467 chunk->off = sb->len;
471 static void parse_commit_header(struct format_commit_context *context)
473 const char *msg = context->commit->buffer;
476 for (i = 0; msg[i]; i++) {
478 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
483 } else if (!prefixcmp(msg + i, "author ")) {
484 context->author.off = i + 7;
485 context->author.len = eol - i - 7;
486 } else if (!prefixcmp(msg + i, "committer ")) {
487 context->committer.off = i + 10;
488 context->committer.len = eol - i - 10;
489 } else if (!prefixcmp(msg + i, "encoding ")) {
490 context->encoding.off = i + 9;
491 context->encoding.len = eol - i - 9;
495 context->message_off = i;
496 context->commit_header_parsed = 1;
499 const char *format_subject(struct strbuf *sb, const char *msg,
500 const char *line_separator)
505 const char *line = msg;
506 int linelen = get_one_line(line);
509 if (!linelen || is_empty_line(line, &linelen))
514 strbuf_grow(sb, linelen + 2);
516 strbuf_addstr(sb, line_separator);
517 strbuf_add(sb, line, linelen);
523 static void parse_commit_message(struct format_commit_context *c)
525 const char *msg = c->commit->buffer + c->message_off;
526 const char *start = c->commit->buffer;
528 msg = skip_empty_lines(msg);
529 c->subject_off = msg - start;
531 msg = format_subject(NULL, msg, NULL);
532 msg = skip_empty_lines(msg);
533 c->body_off = msg - start;
535 c->commit_message_parsed = 1;
538 static void format_decoration(struct strbuf *sb, const struct commit *commit)
540 struct name_decoration *d;
541 const char *prefix = " (";
543 load_ref_decorations();
544 d = lookup_decoration(&name_decoration, &commit->object);
546 strbuf_addstr(sb, prefix);
548 strbuf_addstr(sb, d->name);
551 if (prefix[0] == ',')
552 strbuf_addch(sb, ')');
555 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
558 struct format_commit_context *c = context;
559 const struct commit *commit = c->commit;
560 const char *msg = commit->buffer;
561 struct commit_list *p;
564 /* these are independent of the commit */
565 switch (placeholder[0]) {
567 if (placeholder[1] == '(') {
568 const char *end = strchr(placeholder + 2, ')');
569 char color[COLOR_MAXLEN];
572 color_parse_mem(placeholder + 2,
573 end - (placeholder + 2),
574 "--pretty format", color);
575 strbuf_addstr(sb, color);
576 return end - placeholder + 1;
578 if (!prefixcmp(placeholder + 1, "red")) {
579 strbuf_addstr(sb, GIT_COLOR_RED);
581 } else if (!prefixcmp(placeholder + 1, "green")) {
582 strbuf_addstr(sb, GIT_COLOR_GREEN);
584 } else if (!prefixcmp(placeholder + 1, "blue")) {
585 strbuf_addstr(sb, GIT_COLOR_BLUE);
587 } else if (!prefixcmp(placeholder + 1, "reset")) {
588 strbuf_addstr(sb, GIT_COLOR_RESET);
592 case 'n': /* newline */
593 strbuf_addch(sb, '\n');
596 /* %x00 == NUL, %x0a == LF, etc. */
597 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
599 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
601 strbuf_addch(sb, (h1<<4)|h2);
607 /* these depend on the commit */
608 if (!commit->object.parsed)
609 parse_object(commit->object.sha1);
611 switch (placeholder[0]) {
612 case 'H': /* commit hash */
613 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
615 case 'h': /* abbreviated commit hash */
616 if (add_again(sb, &c->abbrev_commit_hash))
618 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
620 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
622 case 'T': /* tree hash */
623 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
625 case 't': /* abbreviated tree hash */
626 if (add_again(sb, &c->abbrev_tree_hash))
628 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
630 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
632 case 'P': /* parent hashes */
633 for (p = commit->parents; p; p = p->next) {
634 if (p != commit->parents)
635 strbuf_addch(sb, ' ');
636 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
639 case 'p': /* abbreviated parent hashes */
640 if (add_again(sb, &c->abbrev_parent_hashes))
642 for (p = commit->parents; p; p = p->next) {
643 if (p != commit->parents)
644 strbuf_addch(sb, ' ');
645 strbuf_addstr(sb, find_unique_abbrev(
646 p->item->object.sha1, DEFAULT_ABBREV));
648 c->abbrev_parent_hashes.len = sb->len -
649 c->abbrev_parent_hashes.off;
651 case 'm': /* left/right/bottom */
652 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
654 : (commit->object.flags & SYMMETRIC_LEFT)
659 format_decoration(sb, commit);
663 /* For the rest we have to parse the commit header. */
664 if (!c->commit_header_parsed)
665 parse_commit_header(c);
667 switch (placeholder[0]) {
668 case 'a': /* author ... */
669 return format_person_part(sb, placeholder[1],
670 msg + c->author.off, c->author.len,
672 case 'c': /* committer ... */
673 return format_person_part(sb, placeholder[1],
674 msg + c->committer.off, c->committer.len,
676 case 'e': /* encoding */
677 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
681 /* Now we need to parse the commit message. */
682 if (!c->commit_message_parsed)
683 parse_commit_message(c);
685 switch (placeholder[0]) {
686 case 's': /* subject */
687 format_subject(sb, msg + c->subject_off, " ");
690 strbuf_addstr(sb, msg + c->body_off);
693 return 0; /* unknown placeholder */
696 void format_commit_message(const struct commit *commit,
697 const void *format, struct strbuf *sb,
698 enum date_mode dmode)
700 struct format_commit_context context;
702 memset(&context, 0, sizeof(context));
703 context.commit = commit;
704 context.dmode = dmode;
705 strbuf_expand(sb, format, format_commit_item, &context);
708 static void pp_header(enum cmit_fmt fmt,
710 enum date_mode dmode,
711 const char *encoding,
712 const struct commit *commit,
716 int parents_shown = 0;
719 const char *line = *msg_p;
720 int linelen = get_one_line(*msg_p);
730 if (fmt == CMIT_FMT_RAW) {
731 strbuf_add(sb, line, linelen);
735 if (!memcmp(line, "parent ", 7)) {
737 die("bad parent line in commit");
741 if (!parents_shown) {
742 struct commit_list *parent;
744 for (parent = commit->parents, num = 0;
746 parent = parent->next, num++)
748 /* with enough slop */
749 strbuf_grow(sb, num * 50 + 20);
750 add_merge_info(fmt, sb, commit, abbrev);
755 * MEDIUM == DEFAULT shows only author with dates.
756 * FULL shows both authors but not dates.
757 * FULLER shows both authors and dates.
759 if (!memcmp(line, "author ", 7)) {
760 strbuf_grow(sb, linelen + 80);
761 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
763 if (!memcmp(line, "committer ", 10) &&
764 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
765 strbuf_grow(sb, linelen + 80);
766 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
771 void pp_title_line(enum cmit_fmt fmt,
775 const char *after_subject,
776 const char *encoding,
779 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
782 strbuf_init(&title, 80);
783 *msg_p = format_subject(&title, *msg_p, line_separator);
785 strbuf_grow(sb, title.len + 1024);
787 strbuf_addstr(sb, subject);
788 add_rfc2047(sb, title.buf, title.len, encoding);
790 strbuf_addbuf(sb, &title);
792 strbuf_addch(sb, '\n');
794 if (need_8bit_cte > 0) {
795 const char *header_fmt =
796 "MIME-Version: 1.0\n"
797 "Content-Type: text/plain; charset=%s\n"
798 "Content-Transfer-Encoding: 8bit\n";
799 strbuf_addf(sb, header_fmt, encoding);
802 strbuf_addstr(sb, after_subject);
804 if (fmt == CMIT_FMT_EMAIL) {
805 strbuf_addch(sb, '\n');
807 strbuf_release(&title);
810 void pp_remainder(enum cmit_fmt fmt,
817 const char *line = *msg_p;
818 int linelen = get_one_line(line);
824 if (is_empty_line(line, &linelen)) {
827 if (fmt == CMIT_FMT_SHORT)
832 strbuf_grow(sb, linelen + indent + 20);
834 memset(sb->buf + sb->len, ' ', indent);
835 strbuf_setlen(sb, sb->len + indent);
837 strbuf_add(sb, line, linelen);
838 strbuf_addch(sb, '\n');
842 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
844 const char *encoding;
846 encoding = (git_log_output_encoding
847 ? git_log_output_encoding
848 : git_commit_encoding);
852 *encoding_p = encoding;
853 return logmsg_reencode(commit, encoding);
856 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
857 struct strbuf *sb, int abbrev,
858 const char *subject, const char *after_subject,
859 enum date_mode dmode, int need_8bit_cte)
861 unsigned long beginning_of_body;
863 const char *msg = commit->buffer;
865 const char *encoding;
867 if (fmt == CMIT_FMT_USERFORMAT) {
868 format_commit_message(commit, user_format, sb, dmode);
872 reencoded = reencode_commit_message(commit, &encoding);
877 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
881 * We need to check and emit Content-type: to mark it
882 * as 8-bit if we haven't done so.
884 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
887 for (in_body = i = 0; (ch = msg[i]); i++) {
889 /* author could be non 7-bit ASCII but
890 * the log may be so; skip over the
893 if (ch == '\n' && msg[i+1] == '\n')
896 else if (non_ascii(ch)) {
903 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
904 if (fmt != CMIT_FMT_ONELINE && !subject) {
905 strbuf_addch(sb, '\n');
908 /* Skip excess blank lines at the beginning of body, if any... */
909 msg = skip_empty_lines(msg);
911 /* These formats treat the title line specially. */
912 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
913 pp_title_line(fmt, &msg, sb, subject,
914 after_subject, encoding, need_8bit_cte);
916 beginning_of_body = sb->len;
917 if (fmt != CMIT_FMT_ONELINE)
918 pp_remainder(fmt, &msg, sb, indent);
921 /* Make sure there is an EOLN for the non-oneline case */
922 if (fmt != CMIT_FMT_ONELINE)
923 strbuf_addch(sb, '\n');
926 * The caller may append additional body text in e-mail
927 * format. Make sure we did not strip the blank line
928 * between the header and the body.
930 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
931 strbuf_addch(sb, '\n');