6 #include "string-list.h"
10 static char *user_format
;
12 void get_commit_format(const char *arg
, struct rev_info
*rev
)
15 static struct cmt_fmt_map
{
20 { "raw", 1, CMIT_FMT_RAW
},
21 { "medium", 1, CMIT_FMT_MEDIUM
},
22 { "short", 1, CMIT_FMT_SHORT
},
23 { "email", 1, CMIT_FMT_EMAIL
},
24 { "full", 5, CMIT_FMT_FULL
},
25 { "fuller", 5, CMIT_FMT_FULLER
},
26 { "oneline", 1, CMIT_FMT_ONELINE
},
29 rev
->use_terminator
= 0;
31 rev
->commit_format
= CMIT_FMT_DEFAULT
;
34 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
35 const char *cp
= strchr(arg
, ':') + 1;
37 user_format
= xstrdup(cp
);
39 rev
->use_terminator
= 1;
40 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
43 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
44 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
45 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
))) {
46 if (cmt_fmts
[i
].v
== CMIT_FMT_ONELINE
)
47 rev
->use_terminator
= 1;
48 rev
->commit_format
= cmt_fmts
[i
].v
;
53 die("invalid --pretty format: %s", arg
);
57 * Generic support for pretty-printing the header
59 static int get_one_line(const char *msg
)
74 /* High bit set, or ISO-2022-INT */
78 return ((ch
& 0x80) || (ch
== 0x1b));
81 static int is_rfc2047_special(char ch
)
83 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
86 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
91 for (i
= 0; i
< len
; i
++) {
95 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
98 strbuf_add(sb
, line
, len
);
102 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
103 strbuf_addf(sb
, "=?%s?q?", encoding
);
104 for (i
= last
= 0; i
< len
; i
++) {
105 unsigned ch
= line
[i
] & 0xFF;
107 * We encode ' ' using '=20' even though rfc2047
108 * allows using '_' for readability. Unfortunately,
109 * many programs do not understand this and just
110 * leave the underscore in place.
112 if (is_rfc2047_special(ch
) || ch
== ' ') {
113 strbuf_add(sb
, line
+ last
, i
- last
);
114 strbuf_addf(sb
, "=%02X", ch
);
118 strbuf_add(sb
, line
+ last
, len
- last
);
119 strbuf_addstr(sb
, "?=");
122 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
123 const char *line
, enum date_mode dmode
,
124 const char *encoding
)
130 const char *filler
= " ";
132 if (fmt
== CMIT_FMT_ONELINE
)
134 date
= strchr(line
, '>');
137 namelen
= ++date
- line
;
138 time
= strtoul(date
, &date
, 10);
139 tz
= strtol(date
, NULL
, 10);
141 if (fmt
== CMIT_FMT_EMAIL
) {
142 char *name_tail
= strchr(line
, '<');
143 int display_name_length
;
146 while (line
< name_tail
&& isspace(name_tail
[-1]))
148 display_name_length
= name_tail
- line
;
150 strbuf_addstr(sb
, "From: ");
151 add_rfc2047(sb
, line
, display_name_length
, encoding
);
152 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
153 strbuf_addch(sb
, '\n');
155 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
156 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
157 filler
, namelen
, line
);
160 case CMIT_FMT_MEDIUM
:
161 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
164 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
166 case CMIT_FMT_FULLER
:
167 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
175 static int is_empty_line(const char *line
, int *len_p
)
178 while (len
&& isspace(line
[len
-1]))
184 static const char *skip_empty_lines(const char *msg
)
187 int linelen
= get_one_line(msg
);
191 if (!is_empty_line(msg
, &ll
))
198 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
199 const struct commit
*commit
, int abbrev
)
201 struct commit_list
*parent
= commit
->parents
;
203 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
204 !parent
|| !parent
->next
)
207 strbuf_addstr(sb
, "Merge:");
210 struct commit
*p
= parent
->item
;
211 const char *hex
= NULL
;
214 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
216 hex
= sha1_to_hex(p
->object
.sha1
);
217 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
218 parent
= parent
->next
;
220 strbuf_addf(sb
, " %s%s", hex
, dots
);
222 strbuf_addch(sb
, '\n');
225 static char *get_header(const struct commit
*commit
, const char *key
)
227 int key_len
= strlen(key
);
228 const char *line
= commit
->buffer
;
231 const char *eol
= strchr(line
, '\n'), *next
;
236 eol
= line
+ strlen(line
);
240 if (eol
- line
> key_len
&&
241 !strncmp(line
, key
, key_len
) &&
242 line
[key_len
] == ' ') {
243 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
249 static char *replace_encoding_header(char *buf
, const char *encoding
)
251 struct strbuf tmp
= STRBUF_INIT
;
255 /* guess if there is an encoding header before a \n\n */
256 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
257 cp
= strchr(cp
, '\n');
258 if (!cp
|| *++cp
== '\n')
262 cp
= strchr(cp
, '\n');
264 return buf
; /* should not happen but be defensive */
265 len
= cp
+ 1 - (buf
+ start
);
267 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
268 if (is_encoding_utf8(encoding
)) {
269 /* we have re-coded to UTF-8; drop the header */
270 strbuf_remove(&tmp
, start
, len
);
272 /* just replaces XXXX in 'encoding XXXX\n' */
273 strbuf_splice(&tmp
, start
+ strlen("encoding "),
274 len
- strlen("encoding \n"),
275 encoding
, strlen(encoding
));
277 return strbuf_detach(&tmp
, NULL
);
280 static char *logmsg_reencode(const struct commit
*commit
,
281 const char *output_encoding
)
283 static const char *utf8
= "utf-8";
284 const char *use_encoding
;
288 if (!*output_encoding
)
290 encoding
= get_header(commit
, "encoding");
291 use_encoding
= encoding
? encoding
: utf8
;
292 if (!strcmp(use_encoding
, output_encoding
))
293 if (encoding
) /* we'll strip encoding header later */
294 out
= xstrdup(commit
->buffer
);
296 return NULL
; /* nothing to do */
298 out
= reencode_string(commit
->buffer
,
299 output_encoding
, use_encoding
);
301 out
= replace_encoding_header(out
, output_encoding
);
307 static int mailmap_name(struct strbuf
*sb
, const char *email
)
309 static struct string_list
*mail_map
;
313 mail_map
= xcalloc(1, sizeof(*mail_map
));
314 read_mailmap(mail_map
, ".mailmap", NULL
);
320 if (!map_email(mail_map
, email
, buffer
, sizeof(buffer
)))
322 strbuf_addstr(sb
, buffer
);
326 static size_t format_person_part(struct strbuf
*sb
, char part
,
327 const char *msg
, int len
, enum date_mode dmode
)
329 /* currently all placeholders have same length */
330 const int placeholder_len
= 2;
331 int start
, end
, tz
= 0;
332 unsigned long date
= 0;
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 if (part
== 'n' || part
== 'N') { /* name */
348 while (end
> 0 && isspace(msg
[end
- 1]))
350 if (part
!= 'N' || !msg
[end
] || !msg
[end
+ 1] ||
351 mailmap_name(sb
, msg
+ end
+ 2) < 0)
352 strbuf_add(sb
, msg
, end
);
353 return placeholder_len
;
355 start
= ++end
; /* save email start position */
357 /* advance 'end' to point to email end delimiter */
358 for ( ; end
< len
&& msg
[end
] != '>'; end
++)
364 if (part
== 'e') { /* email */
365 strbuf_add(sb
, msg
+ start
, end
- start
);
366 return placeholder_len
;
369 /* advance 'start' to point to date start delimiter */
370 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
374 date
= strtoul(msg
+ start
, &ep
, 10);
375 if (msg
+ start
== ep
)
378 if (part
== 't') { /* date, UNIX timestamp */
379 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
380 return placeholder_len
;
384 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
386 if (start
+ 1 < len
) {
387 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
388 if (msg
[start
] == '-')
394 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
395 return placeholder_len
;
396 case 'D': /* date, RFC2822 style */
397 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
398 return placeholder_len
;
399 case 'r': /* date, relative */
400 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
401 return placeholder_len
;
402 case 'i': /* date, ISO 8601 */
403 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
404 return placeholder_len
;
409 * bogus commit, 'sb' cannot be updated, but we still need to
410 * compute a valid return value.
412 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
413 || part
== 'D' || part
== 'r' || part
== 'i')
414 return placeholder_len
;
416 return 0; /* unknown placeholder */
424 struct format_commit_context
{
425 const struct commit
*commit
;
426 enum date_mode dmode
;
428 /* These offsets are relative to the start of the commit message. */
429 int commit_header_parsed
;
430 struct chunk subject
;
432 struct chunk committer
;
433 struct chunk encoding
;
436 /* The following ones are relative to the result struct strbuf. */
437 struct chunk abbrev_commit_hash
;
438 struct chunk abbrev_tree_hash
;
439 struct chunk abbrev_parent_hashes
;
442 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
445 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
450 * We haven't seen this chunk before. Our caller is surely
451 * going to add it the hard way now. Remember the most likely
452 * start of the to-be-added chunk: the current end of the
455 chunk
->off
= sb
->len
;
459 static void parse_commit_header(struct format_commit_context
*context
)
461 const char *msg
= context
->commit
->buffer
;
463 enum { HEADER
, SUBJECT
, BODY
} state
;
465 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
467 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
470 if (state
== SUBJECT
) {
471 context
->subject
.off
= i
;
472 context
->subject
.len
= eol
- i
;
477 /* strip empty lines */
478 while (msg
[eol
] == '\n' && msg
[eol
+ 1] == '\n')
480 } else if (!prefixcmp(msg
+ i
, "author ")) {
481 context
->author
.off
= i
+ 7;
482 context
->author
.len
= eol
- i
- 7;
483 } else if (!prefixcmp(msg
+ i
, "committer ")) {
484 context
->committer
.off
= i
+ 10;
485 context
->committer
.len
= eol
- i
- 10;
486 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
487 context
->encoding
.off
= i
+ 9;
488 context
->encoding
.len
= eol
- i
- 9;
494 context
->body_off
= i
;
495 context
->commit_header_parsed
= 1;
498 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
500 struct name_decoration
*d
;
501 const char *prefix
= " (";
503 load_ref_decorations();
504 d
= lookup_decoration(&name_decoration
, &commit
->object
);
506 strbuf_addstr(sb
, prefix
);
508 strbuf_addstr(sb
, d
->name
);
511 if (prefix
[0] == ',')
512 strbuf_addch(sb
, ')');
515 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
518 struct format_commit_context
*c
= context
;
519 const struct commit
*commit
= c
->commit
;
520 const char *msg
= commit
->buffer
;
521 struct commit_list
*p
;
524 /* these are independent of the commit */
525 switch (placeholder
[0]) {
527 if (!prefixcmp(placeholder
+ 1, "red")) {
528 strbuf_addstr(sb
, "\033[31m");
530 } else if (!prefixcmp(placeholder
+ 1, "green")) {
531 strbuf_addstr(sb
, "\033[32m");
533 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
534 strbuf_addstr(sb
, "\033[34m");
536 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
537 strbuf_addstr(sb
, "\033[m");
541 case 'n': /* newline */
542 strbuf_addch(sb
, '\n');
545 /* %x00 == NUL, %x0a == LF, etc. */
546 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
548 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
550 strbuf_addch(sb
, (h1
<<4)|h2
);
556 /* these depend on the commit */
557 if (!commit
->object
.parsed
)
558 parse_object(commit
->object
.sha1
);
560 switch (placeholder
[0]) {
561 case 'H': /* commit hash */
562 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
564 case 'h': /* abbreviated commit hash */
565 if (add_again(sb
, &c
->abbrev_commit_hash
))
567 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
569 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
571 case 'T': /* tree hash */
572 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
574 case 't': /* abbreviated tree hash */
575 if (add_again(sb
, &c
->abbrev_tree_hash
))
577 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
579 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
581 case 'P': /* parent hashes */
582 for (p
= commit
->parents
; p
; p
= p
->next
) {
583 if (p
!= commit
->parents
)
584 strbuf_addch(sb
, ' ');
585 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
588 case 'p': /* abbreviated parent hashes */
589 if (add_again(sb
, &c
->abbrev_parent_hashes
))
591 for (p
= commit
->parents
; p
; p
= p
->next
) {
592 if (p
!= commit
->parents
)
593 strbuf_addch(sb
, ' ');
594 strbuf_addstr(sb
, find_unique_abbrev(
595 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
597 c
->abbrev_parent_hashes
.len
= sb
->len
-
598 c
->abbrev_parent_hashes
.off
;
600 case 'm': /* left/right/bottom */
601 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
603 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
608 format_decoration(sb
, commit
);
612 /* For the rest we have to parse the commit header. */
613 if (!c
->commit_header_parsed
)
614 parse_commit_header(c
);
616 switch (placeholder
[0]) {
617 case 's': /* subject */
618 strbuf_add(sb
, msg
+ c
->subject
.off
, c
->subject
.len
);
620 case 'a': /* author ... */
621 return format_person_part(sb
, placeholder
[1],
622 msg
+ c
->author
.off
, c
->author
.len
,
624 case 'c': /* committer ... */
625 return format_person_part(sb
, placeholder
[1],
626 msg
+ c
->committer
.off
, c
->committer
.len
,
628 case 'e': /* encoding */
629 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
632 strbuf_addstr(sb
, msg
+ c
->body_off
);
635 return 0; /* unknown placeholder */
638 void format_commit_message(const struct commit
*commit
,
639 const void *format
, struct strbuf
*sb
,
640 enum date_mode dmode
)
642 struct format_commit_context context
;
644 memset(&context
, 0, sizeof(context
));
645 context
.commit
= commit
;
646 context
.dmode
= dmode
;
647 strbuf_expand(sb
, format
, format_commit_item
, &context
);
650 static void pp_header(enum cmit_fmt fmt
,
652 enum date_mode dmode
,
653 const char *encoding
,
654 const struct commit
*commit
,
658 int parents_shown
= 0;
661 const char *line
= *msg_p
;
662 int linelen
= get_one_line(*msg_p
);
672 if (fmt
== CMIT_FMT_RAW
) {
673 strbuf_add(sb
, line
, linelen
);
677 if (!memcmp(line
, "parent ", 7)) {
679 die("bad parent line in commit");
683 if (!parents_shown
) {
684 struct commit_list
*parent
;
686 for (parent
= commit
->parents
, num
= 0;
688 parent
= parent
->next
, num
++)
690 /* with enough slop */
691 strbuf_grow(sb
, num
* 50 + 20);
692 add_merge_info(fmt
, sb
, commit
, abbrev
);
697 * MEDIUM == DEFAULT shows only author with dates.
698 * FULL shows both authors but not dates.
699 * FULLER shows both authors and dates.
701 if (!memcmp(line
, "author ", 7)) {
702 strbuf_grow(sb
, linelen
+ 80);
703 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
705 if (!memcmp(line
, "committer ", 10) &&
706 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
707 strbuf_grow(sb
, linelen
+ 80);
708 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
713 void pp_title_line(enum cmit_fmt fmt
,
717 const char *after_subject
,
718 const char *encoding
,
723 strbuf_init(&title
, 80);
726 const char *line
= *msg_p
;
727 int linelen
= get_one_line(line
);
730 if (!linelen
|| is_empty_line(line
, &linelen
))
733 strbuf_grow(&title
, linelen
+ 2);
735 if (fmt
== CMIT_FMT_EMAIL
) {
736 strbuf_addch(&title
, '\n');
738 strbuf_addch(&title
, ' ');
740 strbuf_add(&title
, line
, linelen
);
743 strbuf_grow(sb
, title
.len
+ 1024);
745 strbuf_addstr(sb
, subject
);
746 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
748 strbuf_addbuf(sb
, &title
);
750 strbuf_addch(sb
, '\n');
752 if (need_8bit_cte
> 0) {
753 const char *header_fmt
=
754 "MIME-Version: 1.0\n"
755 "Content-Type: text/plain; charset=%s\n"
756 "Content-Transfer-Encoding: 8bit\n";
757 strbuf_addf(sb
, header_fmt
, encoding
);
760 strbuf_addstr(sb
, after_subject
);
762 if (fmt
== CMIT_FMT_EMAIL
) {
763 strbuf_addch(sb
, '\n');
765 strbuf_release(&title
);
768 void pp_remainder(enum cmit_fmt fmt
,
775 const char *line
= *msg_p
;
776 int linelen
= get_one_line(line
);
782 if (is_empty_line(line
, &linelen
)) {
785 if (fmt
== CMIT_FMT_SHORT
)
790 strbuf_grow(sb
, linelen
+ indent
+ 20);
792 memset(sb
->buf
+ sb
->len
, ' ', indent
);
793 strbuf_setlen(sb
, sb
->len
+ indent
);
795 strbuf_add(sb
, line
, linelen
);
796 strbuf_addch(sb
, '\n');
800 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
802 const char *encoding
;
804 encoding
= (git_log_output_encoding
805 ? git_log_output_encoding
806 : git_commit_encoding
);
810 *encoding_p
= encoding
;
811 return logmsg_reencode(commit
, encoding
);
814 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
815 struct strbuf
*sb
, int abbrev
,
816 const char *subject
, const char *after_subject
,
817 enum date_mode dmode
, int need_8bit_cte
)
819 unsigned long beginning_of_body
;
821 const char *msg
= commit
->buffer
;
823 const char *encoding
;
825 if (fmt
== CMIT_FMT_USERFORMAT
) {
826 format_commit_message(commit
, user_format
, sb
, dmode
);
830 reencoded
= reencode_commit_message(commit
, &encoding
);
835 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
839 * We need to check and emit Content-type: to mark it
840 * as 8-bit if we haven't done so.
842 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
845 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
847 /* author could be non 7-bit ASCII but
848 * the log may be so; skip over the
851 if (ch
== '\n' && msg
[i
+1] == '\n')
854 else if (non_ascii(ch
)) {
861 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
862 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
863 strbuf_addch(sb
, '\n');
866 /* Skip excess blank lines at the beginning of body, if any... */
867 msg
= skip_empty_lines(msg
);
869 /* These formats treat the title line specially. */
870 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
871 pp_title_line(fmt
, &msg
, sb
, subject
,
872 after_subject
, encoding
, need_8bit_cte
);
874 beginning_of_body
= sb
->len
;
875 if (fmt
!= CMIT_FMT_ONELINE
)
876 pp_remainder(fmt
, &msg
, sb
, indent
);
879 /* Make sure there is an EOLN for the non-oneline case */
880 if (fmt
!= CMIT_FMT_ONELINE
)
881 strbuf_addch(sb
, '\n');
884 * The caller may append additional body text in e-mail
885 * format. Make sure we did not strip the blank line
886 * between the header and the body.
888 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
889 strbuf_addch(sb
, '\n');