7 static char *user_format;
9 void get_commit_format(const char *arg, struct rev_info *rev)
12 static struct cmt_fmt_map {
17 { "raw", 1, CMIT_FMT_RAW },
18 { "medium", 1, CMIT_FMT_MEDIUM },
19 { "short", 1, CMIT_FMT_SHORT },
20 { "email", 1, CMIT_FMT_EMAIL },
21 { "full", 5, CMIT_FMT_FULL },
22 { "fuller", 5, CMIT_FMT_FULLER },
23 { "oneline", 1, CMIT_FMT_ONELINE },
26 rev->use_terminator = 0;
28 rev->commit_format = CMIT_FMT_DEFAULT;
31 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
32 const char *cp = strchr(arg, ':') + 1;
34 user_format = xstrdup(cp);
36 rev->use_terminator = 1;
37 rev->commit_format = CMIT_FMT_USERFORMAT;
40 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
41 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
42 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
43 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
44 rev->use_terminator = 1;
45 rev->commit_format = cmt_fmts[i].v;
50 die("invalid --pretty format: %s", arg);
54 * Generic support for pretty-printing the header
56 static int get_one_line(const char *msg)
71 /* High bit set, or ISO-2022-INT */
75 return ((ch & 0x80) || (ch == 0x1b));
78 static int is_rfc2047_special(char ch)
80 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
83 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
88 for (i = 0; i < len; i++) {
92 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
95 strbuf_add(sb, line, len);
99 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
100 strbuf_addf(sb, "=?%s?q?", encoding);
101 for (i = last = 0; i < len; i++) {
102 unsigned ch = line[i] & 0xFF;
104 * We encode ' ' using '=20' even though rfc2047
105 * allows using '_' for readability. Unfortunately,
106 * many programs do not understand this and just
107 * leave the underscore in place.
109 if (is_rfc2047_special(ch) || ch == ' ') {
110 strbuf_add(sb, line + last, i - last);
111 strbuf_addf(sb, "=%02X", ch);
115 strbuf_add(sb, line + last, len - last);
116 strbuf_addstr(sb, "?=");
119 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
120 const char *line, enum date_mode dmode,
121 const char *encoding)
127 const char *filler = " ";
129 if (fmt == CMIT_FMT_ONELINE)
131 date = strchr(line, '>');
134 namelen = ++date - line;
135 time = strtoul(date, &date, 10);
136 tz = strtol(date, NULL, 10);
138 if (fmt == CMIT_FMT_EMAIL) {
139 char *name_tail = strchr(line, '<');
140 int display_name_length;
143 while (line < name_tail && isspace(name_tail[-1]))
145 display_name_length = name_tail - line;
147 strbuf_addstr(sb, "From: ");
148 add_rfc2047(sb, line, display_name_length, encoding);
149 strbuf_add(sb, name_tail, namelen - display_name_length);
150 strbuf_addch(sb, '\n');
152 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
153 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
154 filler, namelen, line);
157 case CMIT_FMT_MEDIUM:
158 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
161 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
163 case CMIT_FMT_FULLER:
164 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
172 static int is_empty_line(const char *line, int *len_p)
175 while (len && isspace(line[len-1]))
181 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
182 const struct commit *commit, int abbrev)
184 struct commit_list *parent = commit->parents;
186 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
187 !parent || !parent->next)
190 strbuf_addstr(sb, "Merge:");
193 struct commit *p = parent->item;
194 const char *hex = NULL;
197 hex = find_unique_abbrev(p->object.sha1, abbrev);
199 hex = sha1_to_hex(p->object.sha1);
200 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
201 parent = parent->next;
203 strbuf_addf(sb, " %s%s", hex, dots);
205 strbuf_addch(sb, '\n');
208 static char *get_header(const struct commit *commit, const char *key)
210 int key_len = strlen(key);
211 const char *line = commit->buffer;
214 const char *eol = strchr(line, '\n'), *next;
219 eol = line + strlen(line);
223 if (eol - line > key_len &&
224 !strncmp(line, key, key_len) &&
225 line[key_len] == ' ') {
226 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
232 static char *replace_encoding_header(char *buf, const char *encoding)
238 /* guess if there is an encoding header before a \n\n */
239 while (strncmp(cp, "encoding ", strlen("encoding "))) {
240 cp = strchr(cp, '\n');
241 if (!cp || *++cp == '\n')
245 cp = strchr(cp, '\n');
247 return buf; /* should not happen but be defensive */
248 len = cp + 1 - (buf + start);
250 strbuf_init(&tmp, 0);
251 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
252 if (is_encoding_utf8(encoding)) {
253 /* we have re-coded to UTF-8; drop the header */
254 strbuf_remove(&tmp, start, len);
256 /* just replaces XXXX in 'encoding XXXX\n' */
257 strbuf_splice(&tmp, start + strlen("encoding "),
258 len - strlen("encoding \n"),
259 encoding, strlen(encoding));
261 return strbuf_detach(&tmp, NULL);
264 static char *logmsg_reencode(const struct commit *commit,
265 const char *output_encoding)
267 static const char *utf8 = "utf-8";
268 const char *use_encoding;
272 if (!*output_encoding)
274 encoding = get_header(commit, "encoding");
275 use_encoding = encoding ? encoding : utf8;
276 if (!strcmp(use_encoding, output_encoding))
277 if (encoding) /* we'll strip encoding header later */
278 out = xstrdup(commit->buffer);
280 return NULL; /* nothing to do */
282 out = reencode_string(commit->buffer,
283 output_encoding, use_encoding);
285 out = replace_encoding_header(out, output_encoding);
291 static size_t format_person_part(struct strbuf *sb, char part,
292 const char *msg, int len)
294 /* currently all placeholders have same length */
295 const int placeholder_len = 2;
296 int start, end, tz = 0;
297 unsigned long date = 0;
300 /* advance 'end' to point to email start delimiter */
301 for (end = 0; end < len && msg[end] != '<'; end++)
305 * When end points at the '<' that we found, it should have
306 * matching '>' later, which means 'end' must be strictly
312 if (part == 'n') { /* name */
313 while (end > 0 && isspace(msg[end - 1]))
315 strbuf_add(sb, msg, end);
316 return placeholder_len;
318 start = ++end; /* save email start position */
320 /* advance 'end' to point to email end delimiter */
321 for ( ; end < len && msg[end] != '>'; end++)
327 if (part == 'e') { /* email */
328 strbuf_add(sb, msg + start, end - start);
329 return placeholder_len;
332 /* advance 'start' to point to date start delimiter */
333 for (start = end + 1; start < len && isspace(msg[start]); start++)
337 date = strtoul(msg + start, &ep, 10);
338 if (msg + start == ep)
341 if (part == 't') { /* date, UNIX timestamp */
342 strbuf_add(sb, msg + start, ep - (msg + start));
343 return placeholder_len;
347 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
349 if (start + 1 < len) {
350 tz = strtoul(msg + start + 1, NULL, 10);
351 if (msg[start] == '-')
357 strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
358 return placeholder_len;
359 case 'D': /* date, RFC2822 style */
360 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
361 return placeholder_len;
362 case 'r': /* date, relative */
363 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
364 return placeholder_len;
365 case 'i': /* date, ISO 8601 */
366 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
367 return placeholder_len;
372 * bogus commit, 'sb' cannot be updated, but we still need to
373 * compute a valid return value.
375 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
376 || part == 'D' || part == 'r' || part == 'i')
377 return placeholder_len;
379 return 0; /* unknown placeholder */
387 struct format_commit_context {
388 const struct commit *commit;
390 /* These offsets are relative to the start of the commit message. */
391 int commit_header_parsed;
392 struct chunk subject;
394 struct chunk committer;
395 struct chunk encoding;
398 /* The following ones are relative to the result struct strbuf. */
399 struct chunk abbrev_commit_hash;
400 struct chunk abbrev_tree_hash;
401 struct chunk abbrev_parent_hashes;
404 static int add_again(struct strbuf *sb, struct chunk *chunk)
407 strbuf_adddup(sb, chunk->off, chunk->len);
412 * We haven't seen this chunk before. Our caller is surely
413 * going to add it the hard way now. Remember the most likely
414 * start of the to-be-added chunk: the current end of the
417 chunk->off = sb->len;
421 static void parse_commit_header(struct format_commit_context *context)
423 const char *msg = context->commit->buffer;
425 enum { HEADER, SUBJECT, BODY } state;
427 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
429 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
432 if (state == SUBJECT) {
433 context->subject.off = i;
434 context->subject.len = eol - i;
439 /* strip empty lines */
440 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
442 } else if (!prefixcmp(msg + i, "author ")) {
443 context->author.off = i + 7;
444 context->author.len = eol - i - 7;
445 } else if (!prefixcmp(msg + i, "committer ")) {
446 context->committer.off = i + 10;
447 context->committer.len = eol - i - 10;
448 } else if (!prefixcmp(msg + i, "encoding ")) {
449 context->encoding.off = i + 9;
450 context->encoding.len = eol - i - 9;
456 context->body_off = i;
457 context->commit_header_parsed = 1;
460 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
463 struct format_commit_context *c = context;
464 const struct commit *commit = c->commit;
465 const char *msg = commit->buffer;
466 struct commit_list *p;
469 /* these are independent of the commit */
470 switch (placeholder[0]) {
472 if (!prefixcmp(placeholder + 1, "red")) {
473 strbuf_addstr(sb, "\033[31m");
475 } else if (!prefixcmp(placeholder + 1, "green")) {
476 strbuf_addstr(sb, "\033[32m");
478 } else if (!prefixcmp(placeholder + 1, "blue")) {
479 strbuf_addstr(sb, "\033[34m");
481 } else if (!prefixcmp(placeholder + 1, "reset")) {
482 strbuf_addstr(sb, "\033[m");
486 case 'n': /* newline */
487 strbuf_addch(sb, '\n');
490 /* %x00 == NUL, %x0a == LF, etc. */
491 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
493 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
495 strbuf_addch(sb, (h1<<4)|h2);
501 /* these depend on the commit */
502 if (!commit->object.parsed)
503 parse_object(commit->object.sha1);
505 switch (placeholder[0]) {
506 case 'H': /* commit hash */
507 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
509 case 'h': /* abbreviated commit hash */
510 if (add_again(sb, &c->abbrev_commit_hash))
512 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
514 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
516 case 'T': /* tree hash */
517 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
519 case 't': /* abbreviated tree hash */
520 if (add_again(sb, &c->abbrev_tree_hash))
522 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
524 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
526 case 'P': /* parent hashes */
527 for (p = commit->parents; p; p = p->next) {
528 if (p != commit->parents)
529 strbuf_addch(sb, ' ');
530 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
533 case 'p': /* abbreviated parent hashes */
534 if (add_again(sb, &c->abbrev_parent_hashes))
536 for (p = commit->parents; p; p = p->next) {
537 if (p != commit->parents)
538 strbuf_addch(sb, ' ');
539 strbuf_addstr(sb, find_unique_abbrev(
540 p->item->object.sha1, DEFAULT_ABBREV));
542 c->abbrev_parent_hashes.len = sb->len -
543 c->abbrev_parent_hashes.off;
545 case 'm': /* left/right/bottom */
546 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
548 : (commit->object.flags & SYMMETRIC_LEFT)
554 /* For the rest we have to parse the commit header. */
555 if (!c->commit_header_parsed)
556 parse_commit_header(c);
558 switch (placeholder[0]) {
559 case 's': /* subject */
560 strbuf_add(sb, msg + c->subject.off, c->subject.len);
562 case 'a': /* author ... */
563 return format_person_part(sb, placeholder[1],
564 msg + c->author.off, c->author.len);
565 case 'c': /* committer ... */
566 return format_person_part(sb, placeholder[1],
567 msg + c->committer.off, c->committer.len);
568 case 'e': /* encoding */
569 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
572 strbuf_addstr(sb, msg + c->body_off);
575 return 0; /* unknown placeholder */
578 void format_commit_message(const struct commit *commit,
579 const void *format, struct strbuf *sb)
581 struct format_commit_context context;
583 memset(&context, 0, sizeof(context));
584 context.commit = commit;
585 strbuf_expand(sb, format, format_commit_item, &context);
588 static void pp_header(enum cmit_fmt fmt,
590 enum date_mode dmode,
591 const char *encoding,
592 const struct commit *commit,
596 int parents_shown = 0;
599 const char *line = *msg_p;
600 int linelen = get_one_line(*msg_p);
610 if (fmt == CMIT_FMT_RAW) {
611 strbuf_add(sb, line, linelen);
615 if (!memcmp(line, "parent ", 7)) {
617 die("bad parent line in commit");
621 if (!parents_shown) {
622 struct commit_list *parent;
624 for (parent = commit->parents, num = 0;
626 parent = parent->next, num++)
628 /* with enough slop */
629 strbuf_grow(sb, num * 50 + 20);
630 add_merge_info(fmt, sb, commit, abbrev);
635 * MEDIUM == DEFAULT shows only author with dates.
636 * FULL shows both authors but not dates.
637 * FULLER shows both authors and dates.
639 if (!memcmp(line, "author ", 7)) {
640 strbuf_grow(sb, linelen + 80);
641 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
643 if (!memcmp(line, "committer ", 10) &&
644 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
645 strbuf_grow(sb, linelen + 80);
646 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
651 void pp_title_line(enum cmit_fmt fmt,
655 const char *after_subject,
656 const char *encoding,
661 strbuf_init(&title, 80);
664 const char *line = *msg_p;
665 int linelen = get_one_line(line);
668 if (!linelen || is_empty_line(line, &linelen))
671 strbuf_grow(&title, linelen + 2);
673 if (fmt == CMIT_FMT_EMAIL) {
674 strbuf_addch(&title, '\n');
676 strbuf_addch(&title, ' ');
678 strbuf_add(&title, line, linelen);
681 strbuf_grow(sb, title.len + 1024);
683 strbuf_addstr(sb, subject);
684 add_rfc2047(sb, title.buf, title.len, encoding);
686 strbuf_addbuf(sb, &title);
688 strbuf_addch(sb, '\n');
690 if (need_8bit_cte > 0) {
691 const char *header_fmt =
692 "MIME-Version: 1.0\n"
693 "Content-Type: text/plain; charset=%s\n"
694 "Content-Transfer-Encoding: 8bit\n";
695 strbuf_addf(sb, header_fmt, encoding);
698 strbuf_addstr(sb, after_subject);
700 if (fmt == CMIT_FMT_EMAIL) {
701 strbuf_addch(sb, '\n');
703 strbuf_release(&title);
706 void pp_remainder(enum cmit_fmt fmt,
713 const char *line = *msg_p;
714 int linelen = get_one_line(line);
720 if (is_empty_line(line, &linelen)) {
723 if (fmt == CMIT_FMT_SHORT)
728 strbuf_grow(sb, linelen + indent + 20);
730 memset(sb->buf + sb->len, ' ', indent);
731 strbuf_setlen(sb, sb->len + indent);
733 strbuf_add(sb, line, linelen);
734 strbuf_addch(sb, '\n');
738 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
739 struct strbuf *sb, int abbrev,
740 const char *subject, const char *after_subject,
741 enum date_mode dmode, int need_8bit_cte)
743 unsigned long beginning_of_body;
745 const char *msg = commit->buffer;
747 const char *encoding;
749 if (fmt == CMIT_FMT_USERFORMAT) {
750 format_commit_message(commit, user_format, sb);
754 encoding = (git_log_output_encoding
755 ? git_log_output_encoding
756 : git_commit_encoding);
759 reencoded = logmsg_reencode(commit, encoding);
764 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
768 * We need to check and emit Content-type: to mark it
769 * as 8-bit if we haven't done so.
771 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
774 for (in_body = i = 0; (ch = msg[i]); i++) {
776 /* author could be non 7-bit ASCII but
777 * the log may be so; skip over the
780 if (ch == '\n' && msg[i+1] == '\n')
783 else if (non_ascii(ch)) {
790 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
791 if (fmt != CMIT_FMT_ONELINE && !subject) {
792 strbuf_addch(sb, '\n');
795 /* Skip excess blank lines at the beginning of body, if any... */
797 int linelen = get_one_line(msg);
801 if (!is_empty_line(msg, &ll))
806 /* These formats treat the title line specially. */
807 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
808 pp_title_line(fmt, &msg, sb, subject,
809 after_subject, encoding, need_8bit_cte);
811 beginning_of_body = sb->len;
812 if (fmt != CMIT_FMT_ONELINE)
813 pp_remainder(fmt, &msg, sb, indent);
816 /* Make sure there is an EOLN for the non-oneline case */
817 if (fmt != CMIT_FMT_ONELINE)
818 strbuf_addch(sb, '\n');
821 * The caller may append additional body text in e-mail
822 * format. Make sure we did not strip the blank line
823 * between the header and the body.
825 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
826 strbuf_addch(sb, '\n');