6 #include "string-list.h"
9 static char *user_format
;
11 void get_commit_format(const char *arg
, struct rev_info
*rev
)
14 static struct cmt_fmt_map
{
19 { "raw", 1, CMIT_FMT_RAW
},
20 { "medium", 1, CMIT_FMT_MEDIUM
},
21 { "short", 1, CMIT_FMT_SHORT
},
22 { "email", 1, CMIT_FMT_EMAIL
},
23 { "full", 5, CMIT_FMT_FULL
},
24 { "fuller", 5, CMIT_FMT_FULLER
},
25 { "oneline", 1, CMIT_FMT_ONELINE
},
28 rev
->use_terminator
= 0;
30 rev
->commit_format
= CMIT_FMT_DEFAULT
;
33 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
34 const char *cp
= strchr(arg
, ':') + 1;
36 user_format
= xstrdup(cp
);
38 rev
->use_terminator
= 1;
39 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
42 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
43 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
44 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
))) {
45 if (cmt_fmts
[i
].v
== CMIT_FMT_ONELINE
)
46 rev
->use_terminator
= 1;
47 rev
->commit_format
= cmt_fmts
[i
].v
;
52 die("invalid --pretty format: %s", arg
);
56 * Generic support for pretty-printing the header
58 static int get_one_line(const char *msg
)
73 /* High bit set, or ISO-2022-INT */
77 return ((ch
& 0x80) || (ch
== 0x1b));
80 static int is_rfc2047_special(char ch
)
82 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
85 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
90 for (i
= 0; i
< len
; i
++) {
94 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
97 strbuf_add(sb
, line
, len
);
101 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
102 strbuf_addf(sb
, "=?%s?q?", encoding
);
103 for (i
= last
= 0; i
< len
; i
++) {
104 unsigned ch
= line
[i
] & 0xFF;
106 * We encode ' ' using '=20' even though rfc2047
107 * allows using '_' for readability. Unfortunately,
108 * many programs do not understand this and just
109 * leave the underscore in place.
111 if (is_rfc2047_special(ch
) || ch
== ' ') {
112 strbuf_add(sb
, line
+ last
, i
- last
);
113 strbuf_addf(sb
, "=%02X", ch
);
117 strbuf_add(sb
, line
+ last
, len
- last
);
118 strbuf_addstr(sb
, "?=");
121 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
122 const char *line
, enum date_mode dmode
,
123 const char *encoding
)
129 const char *filler
= " ";
131 if (fmt
== CMIT_FMT_ONELINE
)
133 date
= strchr(line
, '>');
136 namelen
= ++date
- line
;
137 time
= strtoul(date
, &date
, 10);
138 tz
= strtol(date
, NULL
, 10);
140 if (fmt
== CMIT_FMT_EMAIL
) {
141 char *name_tail
= strchr(line
, '<');
142 int display_name_length
;
145 while (line
< name_tail
&& isspace(name_tail
[-1]))
147 display_name_length
= name_tail
- line
;
149 strbuf_addstr(sb
, "From: ");
150 add_rfc2047(sb
, line
, display_name_length
, encoding
);
151 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
152 strbuf_addch(sb
, '\n');
154 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
155 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
156 filler
, namelen
, line
);
159 case CMIT_FMT_MEDIUM
:
160 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
163 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
165 case CMIT_FMT_FULLER
:
166 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
174 static int is_empty_line(const char *line
, int *len_p
)
177 while (len
&& isspace(line
[len
-1]))
183 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
184 const struct commit
*commit
, int abbrev
)
186 struct commit_list
*parent
= commit
->parents
;
188 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
189 !parent
|| !parent
->next
)
192 strbuf_addstr(sb
, "Merge:");
195 struct commit
*p
= parent
->item
;
196 const char *hex
= NULL
;
199 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
201 hex
= sha1_to_hex(p
->object
.sha1
);
202 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
203 parent
= parent
->next
;
205 strbuf_addf(sb
, " %s%s", hex
, dots
);
207 strbuf_addch(sb
, '\n');
210 static char *get_header(const struct commit
*commit
, const char *key
)
212 int key_len
= strlen(key
);
213 const char *line
= commit
->buffer
;
216 const char *eol
= strchr(line
, '\n'), *next
;
221 eol
= line
+ strlen(line
);
225 if (eol
- line
> key_len
&&
226 !strncmp(line
, key
, key_len
) &&
227 line
[key_len
] == ' ') {
228 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
234 static char *replace_encoding_header(char *buf
, const char *encoding
)
240 /* guess if there is an encoding header before a \n\n */
241 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
242 cp
= strchr(cp
, '\n');
243 if (!cp
|| *++cp
== '\n')
247 cp
= strchr(cp
, '\n');
249 return buf
; /* should not happen but be defensive */
250 len
= cp
+ 1 - (buf
+ start
);
252 strbuf_init(&tmp
, 0);
253 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
254 if (is_encoding_utf8(encoding
)) {
255 /* we have re-coded to UTF-8; drop the header */
256 strbuf_remove(&tmp
, start
, len
);
258 /* just replaces XXXX in 'encoding XXXX\n' */
259 strbuf_splice(&tmp
, start
+ strlen("encoding "),
260 len
- strlen("encoding \n"),
261 encoding
, strlen(encoding
));
263 return strbuf_detach(&tmp
, NULL
);
266 static char *logmsg_reencode(const struct commit
*commit
,
267 const char *output_encoding
)
269 static const char *utf8
= "utf-8";
270 const char *use_encoding
;
274 if (!*output_encoding
)
276 encoding
= get_header(commit
, "encoding");
277 use_encoding
= encoding
? encoding
: utf8
;
278 if (!strcmp(use_encoding
, output_encoding
))
279 if (encoding
) /* we'll strip encoding header later */
280 out
= xstrdup(commit
->buffer
);
282 return NULL
; /* nothing to do */
284 out
= reencode_string(commit
->buffer
,
285 output_encoding
, use_encoding
);
287 out
= replace_encoding_header(out
, output_encoding
);
293 static int mailmap_name(struct strbuf
*sb
, const char *email
)
295 static struct string_list
*mail_map
;
299 mail_map
= xcalloc(1, sizeof(*mail_map
));
300 read_mailmap(mail_map
, ".mailmap", NULL
);
306 if (!map_email(mail_map
, email
, buffer
, sizeof(buffer
)))
308 strbuf_addstr(sb
, buffer
);
312 static size_t format_person_part(struct strbuf
*sb
, char part
,
313 const char *msg
, int len
, enum date_mode dmode
)
315 /* currently all placeholders have same length */
316 const int placeholder_len
= 2;
317 int start
, end
, tz
= 0;
318 unsigned long date
= 0;
321 /* advance 'end' to point to email start delimiter */
322 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
326 * When end points at the '<' that we found, it should have
327 * matching '>' later, which means 'end' must be strictly
333 if (part
== 'n' || part
== 'N') { /* name */
334 while (end
> 0 && isspace(msg
[end
- 1]))
336 if (part
!= 'N' || !msg
[end
] || !msg
[end
+ 1] ||
337 mailmap_name(sb
, msg
+ end
+ 2) < 0)
338 strbuf_add(sb
, msg
, end
);
339 return placeholder_len
;
341 start
= ++end
; /* save email start position */
343 /* advance 'end' to point to email end delimiter */
344 for ( ; end
< len
&& msg
[end
] != '>'; end
++)
350 if (part
== 'e') { /* email */
351 strbuf_add(sb
, msg
+ start
, end
- start
);
352 return placeholder_len
;
355 /* advance 'start' to point to date start delimiter */
356 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
360 date
= strtoul(msg
+ start
, &ep
, 10);
361 if (msg
+ start
== ep
)
364 if (part
== 't') { /* date, UNIX timestamp */
365 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
366 return placeholder_len
;
370 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
372 if (start
+ 1 < len
) {
373 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
374 if (msg
[start
] == '-')
380 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
381 return placeholder_len
;
382 case 'D': /* date, RFC2822 style */
383 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
384 return placeholder_len
;
385 case 'r': /* date, relative */
386 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
387 return placeholder_len
;
388 case 'i': /* date, ISO 8601 */
389 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
390 return placeholder_len
;
395 * bogus commit, 'sb' cannot be updated, but we still need to
396 * compute a valid return value.
398 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
399 || part
== 'D' || part
== 'r' || part
== 'i')
400 return placeholder_len
;
402 return 0; /* unknown placeholder */
410 struct format_commit_context
{
411 const struct commit
*commit
;
412 enum date_mode dmode
;
414 /* These offsets are relative to the start of the commit message. */
415 int commit_header_parsed
;
416 struct chunk subject
;
418 struct chunk committer
;
419 struct chunk encoding
;
422 /* The following ones are relative to the result struct strbuf. */
423 struct chunk abbrev_commit_hash
;
424 struct chunk abbrev_tree_hash
;
425 struct chunk abbrev_parent_hashes
;
428 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
431 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
436 * We haven't seen this chunk before. Our caller is surely
437 * going to add it the hard way now. Remember the most likely
438 * start of the to-be-added chunk: the current end of the
441 chunk
->off
= sb
->len
;
445 static void parse_commit_header(struct format_commit_context
*context
)
447 const char *msg
= context
->commit
->buffer
;
449 enum { HEADER
, SUBJECT
, BODY
} state
;
451 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
453 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
456 if (state
== SUBJECT
) {
457 context
->subject
.off
= i
;
458 context
->subject
.len
= eol
- i
;
463 /* strip empty lines */
464 while (msg
[eol
] == '\n' && msg
[eol
+ 1] == '\n')
466 } else if (!prefixcmp(msg
+ i
, "author ")) {
467 context
->author
.off
= i
+ 7;
468 context
->author
.len
= eol
- i
- 7;
469 } else if (!prefixcmp(msg
+ i
, "committer ")) {
470 context
->committer
.off
= i
+ 10;
471 context
->committer
.len
= eol
- i
- 10;
472 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
473 context
->encoding
.off
= i
+ 9;
474 context
->encoding
.len
= eol
- i
- 9;
480 context
->body_off
= i
;
481 context
->commit_header_parsed
= 1;
484 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
487 struct format_commit_context
*c
= context
;
488 const struct commit
*commit
= c
->commit
;
489 const char *msg
= commit
->buffer
;
490 struct commit_list
*p
;
493 /* these are independent of the commit */
494 switch (placeholder
[0]) {
496 if (!prefixcmp(placeholder
+ 1, "red")) {
497 strbuf_addstr(sb
, "\033[31m");
499 } else if (!prefixcmp(placeholder
+ 1, "green")) {
500 strbuf_addstr(sb
, "\033[32m");
502 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
503 strbuf_addstr(sb
, "\033[34m");
505 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
506 strbuf_addstr(sb
, "\033[m");
510 case 'n': /* newline */
511 strbuf_addch(sb
, '\n');
514 /* %x00 == NUL, %x0a == LF, etc. */
515 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
517 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
519 strbuf_addch(sb
, (h1
<<4)|h2
);
525 /* these depend on the commit */
526 if (!commit
->object
.parsed
)
527 parse_object(commit
->object
.sha1
);
529 switch (placeholder
[0]) {
530 case 'H': /* commit hash */
531 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
533 case 'h': /* abbreviated commit hash */
534 if (add_again(sb
, &c
->abbrev_commit_hash
))
536 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
538 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
540 case 'T': /* tree hash */
541 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
543 case 't': /* abbreviated tree hash */
544 if (add_again(sb
, &c
->abbrev_tree_hash
))
546 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
548 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
550 case 'P': /* parent hashes */
551 for (p
= commit
->parents
; p
; p
= p
->next
) {
552 if (p
!= commit
->parents
)
553 strbuf_addch(sb
, ' ');
554 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
557 case 'p': /* abbreviated parent hashes */
558 if (add_again(sb
, &c
->abbrev_parent_hashes
))
560 for (p
= commit
->parents
; p
; p
= p
->next
) {
561 if (p
!= commit
->parents
)
562 strbuf_addch(sb
, ' ');
563 strbuf_addstr(sb
, find_unique_abbrev(
564 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
566 c
->abbrev_parent_hashes
.len
= sb
->len
-
567 c
->abbrev_parent_hashes
.off
;
569 case 'm': /* left/right/bottom */
570 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
572 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
578 /* For the rest we have to parse the commit header. */
579 if (!c
->commit_header_parsed
)
580 parse_commit_header(c
);
582 switch (placeholder
[0]) {
583 case 's': /* subject */
584 strbuf_add(sb
, msg
+ c
->subject
.off
, c
->subject
.len
);
586 case 'a': /* author ... */
587 return format_person_part(sb
, placeholder
[1],
588 msg
+ c
->author
.off
, c
->author
.len
,
590 case 'c': /* committer ... */
591 return format_person_part(sb
, placeholder
[1],
592 msg
+ c
->committer
.off
, c
->committer
.len
,
594 case 'e': /* encoding */
595 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
598 strbuf_addstr(sb
, msg
+ c
->body_off
);
601 return 0; /* unknown placeholder */
604 void format_commit_message(const struct commit
*commit
,
605 const void *format
, struct strbuf
*sb
,
606 enum date_mode dmode
)
608 struct format_commit_context context
;
610 memset(&context
, 0, sizeof(context
));
611 context
.commit
= commit
;
612 context
.dmode
= dmode
;
613 strbuf_expand(sb
, format
, format_commit_item
, &context
);
616 static void pp_header(enum cmit_fmt fmt
,
618 enum date_mode dmode
,
619 const char *encoding
,
620 const struct commit
*commit
,
624 int parents_shown
= 0;
627 const char *line
= *msg_p
;
628 int linelen
= get_one_line(*msg_p
);
638 if (fmt
== CMIT_FMT_RAW
) {
639 strbuf_add(sb
, line
, linelen
);
643 if (!memcmp(line
, "parent ", 7)) {
645 die("bad parent line in commit");
649 if (!parents_shown
) {
650 struct commit_list
*parent
;
652 for (parent
= commit
->parents
, num
= 0;
654 parent
= parent
->next
, num
++)
656 /* with enough slop */
657 strbuf_grow(sb
, num
* 50 + 20);
658 add_merge_info(fmt
, sb
, commit
, abbrev
);
663 * MEDIUM == DEFAULT shows only author with dates.
664 * FULL shows both authors but not dates.
665 * FULLER shows both authors and dates.
667 if (!memcmp(line
, "author ", 7)) {
668 strbuf_grow(sb
, linelen
+ 80);
669 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
671 if (!memcmp(line
, "committer ", 10) &&
672 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
673 strbuf_grow(sb
, linelen
+ 80);
674 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
679 void pp_title_line(enum cmit_fmt fmt
,
683 const char *after_subject
,
684 const char *encoding
,
689 strbuf_init(&title
, 80);
692 const char *line
= *msg_p
;
693 int linelen
= get_one_line(line
);
696 if (!linelen
|| is_empty_line(line
, &linelen
))
699 strbuf_grow(&title
, linelen
+ 2);
701 if (fmt
== CMIT_FMT_EMAIL
) {
702 strbuf_addch(&title
, '\n');
704 strbuf_addch(&title
, ' ');
706 strbuf_add(&title
, line
, linelen
);
709 strbuf_grow(sb
, title
.len
+ 1024);
711 strbuf_addstr(sb
, subject
);
712 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
714 strbuf_addbuf(sb
, &title
);
716 strbuf_addch(sb
, '\n');
718 if (need_8bit_cte
> 0) {
719 const char *header_fmt
=
720 "MIME-Version: 1.0\n"
721 "Content-Type: text/plain; charset=%s\n"
722 "Content-Transfer-Encoding: 8bit\n";
723 strbuf_addf(sb
, header_fmt
, encoding
);
726 strbuf_addstr(sb
, after_subject
);
728 if (fmt
== CMIT_FMT_EMAIL
) {
729 strbuf_addch(sb
, '\n');
731 strbuf_release(&title
);
734 void pp_remainder(enum cmit_fmt fmt
,
741 const char *line
= *msg_p
;
742 int linelen
= get_one_line(line
);
748 if (is_empty_line(line
, &linelen
)) {
751 if (fmt
== CMIT_FMT_SHORT
)
756 strbuf_grow(sb
, linelen
+ indent
+ 20);
758 memset(sb
->buf
+ sb
->len
, ' ', indent
);
759 strbuf_setlen(sb
, sb
->len
+ indent
);
761 strbuf_add(sb
, line
, linelen
);
762 strbuf_addch(sb
, '\n');
766 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
767 struct strbuf
*sb
, int abbrev
,
768 const char *subject
, const char *after_subject
,
769 enum date_mode dmode
, int need_8bit_cte
)
771 unsigned long beginning_of_body
;
773 const char *msg
= commit
->buffer
;
775 const char *encoding
;
777 if (fmt
== CMIT_FMT_USERFORMAT
) {
778 format_commit_message(commit
, user_format
, sb
, dmode
);
782 encoding
= (git_log_output_encoding
783 ? git_log_output_encoding
784 : git_commit_encoding
);
787 reencoded
= logmsg_reencode(commit
, encoding
);
792 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
796 * We need to check and emit Content-type: to mark it
797 * as 8-bit if we haven't done so.
799 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
802 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
804 /* author could be non 7-bit ASCII but
805 * the log may be so; skip over the
808 if (ch
== '\n' && msg
[i
+1] == '\n')
811 else if (non_ascii(ch
)) {
818 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
819 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
820 strbuf_addch(sb
, '\n');
823 /* Skip excess blank lines at the beginning of body, if any... */
825 int linelen
= get_one_line(msg
);
829 if (!is_empty_line(msg
, &ll
))
834 /* These formats treat the title line specially. */
835 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
836 pp_title_line(fmt
, &msg
, sb
, subject
,
837 after_subject
, encoding
, need_8bit_cte
);
839 beginning_of_body
= sb
->len
;
840 if (fmt
!= CMIT_FMT_ONELINE
)
841 pp_remainder(fmt
, &msg
, sb
, indent
);
844 /* Make sure there is an EOLN for the non-oneline case */
845 if (fmt
!= CMIT_FMT_ONELINE
)
846 strbuf_addch(sb
, '\n');
849 * The caller may append additional body text in e-mail
850 * format. Make sure we did not strip the blank line
851 * between the header and the body.
853 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
854 strbuf_addch(sb
, '\n');