6 #include "string-list.h"
11 static char *user_format
;
13 void get_commit_format(const char *arg
, struct rev_info
*rev
)
16 static struct cmt_fmt_map
{
21 { "raw", 1, CMIT_FMT_RAW
},
22 { "medium", 1, CMIT_FMT_MEDIUM
},
23 { "short", 1, CMIT_FMT_SHORT
},
24 { "email", 1, CMIT_FMT_EMAIL
},
25 { "full", 5, CMIT_FMT_FULL
},
26 { "fuller", 5, CMIT_FMT_FULLER
},
27 { "oneline", 1, CMIT_FMT_ONELINE
},
30 rev
->use_terminator
= 0;
32 rev
->commit_format
= CMIT_FMT_DEFAULT
;
35 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
36 const char *cp
= strchr(arg
, ':') + 1;
38 user_format
= xstrdup(cp
);
40 rev
->use_terminator
= 1;
41 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
44 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
45 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
46 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
))) {
47 if (cmt_fmts
[i
].v
== CMIT_FMT_ONELINE
)
48 rev
->use_terminator
= 1;
49 rev
->commit_format
= cmt_fmts
[i
].v
;
54 die("invalid --pretty format: %s", arg
);
58 * Generic support for pretty-printing the header
60 static int get_one_line(const char *msg
)
75 /* High bit set, or ISO-2022-INT */
79 return ((ch
& 0x80) || (ch
== 0x1b));
82 static int is_rfc2047_special(char ch
)
84 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
87 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
92 for (i
= 0; i
< len
; i
++) {
96 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
99 strbuf_add(sb
, line
, len
);
103 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
104 strbuf_addf(sb
, "=?%s?q?", encoding
);
105 for (i
= last
= 0; i
< len
; i
++) {
106 unsigned ch
= line
[i
] & 0xFF;
108 * We encode ' ' using '=20' even though rfc2047
109 * allows using '_' for readability. Unfortunately,
110 * many programs do not understand this and just
111 * leave the underscore in place.
113 if (is_rfc2047_special(ch
) || ch
== ' ') {
114 strbuf_add(sb
, line
+ last
, i
- last
);
115 strbuf_addf(sb
, "=%02X", ch
);
119 strbuf_add(sb
, line
+ last
, len
- last
);
120 strbuf_addstr(sb
, "?=");
123 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
124 const char *line
, enum date_mode dmode
,
125 const char *encoding
)
131 const char *filler
= " ";
133 if (fmt
== CMIT_FMT_ONELINE
)
135 date
= strchr(line
, '>');
138 namelen
= ++date
- line
;
139 time
= strtoul(date
, &date
, 10);
140 tz
= strtol(date
, NULL
, 10);
142 if (fmt
== CMIT_FMT_EMAIL
) {
143 char *name_tail
= strchr(line
, '<');
144 int display_name_length
;
147 while (line
< name_tail
&& isspace(name_tail
[-1]))
149 display_name_length
= name_tail
- line
;
151 strbuf_addstr(sb
, "From: ");
152 add_rfc2047(sb
, line
, display_name_length
, encoding
);
153 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
154 strbuf_addch(sb
, '\n');
156 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
157 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
158 filler
, namelen
, line
);
161 case CMIT_FMT_MEDIUM
:
162 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
165 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
167 case CMIT_FMT_FULLER
:
168 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
176 static int is_empty_line(const char *line
, int *len_p
)
179 while (len
&& isspace(line
[len
-1]))
185 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
186 const struct commit
*commit
, int abbrev
)
188 struct commit_list
*parent
= commit
->parents
;
190 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
191 !parent
|| !parent
->next
)
194 strbuf_addstr(sb
, "Merge:");
197 struct commit
*p
= parent
->item
;
198 const char *hex
= NULL
;
201 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
203 hex
= sha1_to_hex(p
->object
.sha1
);
204 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
205 parent
= parent
->next
;
207 strbuf_addf(sb
, " %s%s", hex
, dots
);
209 strbuf_addch(sb
, '\n');
212 static char *get_header(const struct commit
*commit
, const char *key
)
214 int key_len
= strlen(key
);
215 const char *line
= commit
->buffer
;
218 const char *eol
= strchr(line
, '\n'), *next
;
223 eol
= line
+ strlen(line
);
227 if (eol
- line
> key_len
&&
228 !strncmp(line
, key
, key_len
) &&
229 line
[key_len
] == ' ') {
230 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
236 static char *replace_encoding_header(char *buf
, const char *encoding
)
238 struct strbuf tmp
= STRBUF_INIT
;
242 /* guess if there is an encoding header before a \n\n */
243 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
244 cp
= strchr(cp
, '\n');
245 if (!cp
|| *++cp
== '\n')
249 cp
= strchr(cp
, '\n');
251 return buf
; /* should not happen but be defensive */
252 len
= cp
+ 1 - (buf
+ start
);
254 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
255 if (is_encoding_utf8(encoding
)) {
256 /* we have re-coded to UTF-8; drop the header */
257 strbuf_remove(&tmp
, start
, len
);
259 /* just replaces XXXX in 'encoding XXXX\n' */
260 strbuf_splice(&tmp
, start
+ strlen("encoding "),
261 len
- strlen("encoding \n"),
262 encoding
, strlen(encoding
));
264 return strbuf_detach(&tmp
, NULL
);
267 static char *logmsg_reencode(const struct commit
*commit
,
268 const char *output_encoding
)
270 static const char *utf8
= "utf-8";
271 const char *use_encoding
;
275 if (!*output_encoding
)
277 encoding
= get_header(commit
, "encoding");
278 use_encoding
= encoding
? encoding
: utf8
;
279 if (!strcmp(use_encoding
, output_encoding
))
280 if (encoding
) /* we'll strip encoding header later */
281 out
= xstrdup(commit
->buffer
);
283 return NULL
; /* nothing to do */
285 out
= reencode_string(commit
->buffer
,
286 output_encoding
, use_encoding
);
288 out
= replace_encoding_header(out
, output_encoding
);
294 static int mailmap_name(struct strbuf
*sb
, const char *email
)
296 static struct string_list
*mail_map
;
300 mail_map
= xcalloc(1, sizeof(*mail_map
));
301 read_mailmap(mail_map
, ".mailmap", NULL
);
307 if (!map_email(mail_map
, email
, buffer
, sizeof(buffer
)))
309 strbuf_addstr(sb
, buffer
);
313 static size_t format_person_part(struct strbuf
*sb
, char part
,
314 const char *msg
, int len
, enum date_mode dmode
)
316 /* currently all placeholders have same length */
317 const int placeholder_len
= 2;
318 int start
, end
, tz
= 0;
319 unsigned long date
= 0;
322 /* advance 'end' to point to email start delimiter */
323 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
327 * When end points at the '<' that we found, it should have
328 * matching '>' later, which means 'end' must be strictly
334 if (part
== 'n' || part
== 'N') { /* name */
335 while (end
> 0 && isspace(msg
[end
- 1]))
337 if (part
!= 'N' || !msg
[end
] || !msg
[end
+ 1] ||
338 mailmap_name(sb
, msg
+ end
+ 2) < 0)
339 strbuf_add(sb
, msg
, end
);
340 return placeholder_len
;
342 start
= ++end
; /* save email start position */
344 /* advance 'end' to point to email end delimiter */
345 for ( ; end
< len
&& msg
[end
] != '>'; end
++)
351 if (part
== 'e') { /* email */
352 strbuf_add(sb
, msg
+ start
, end
- start
);
353 return placeholder_len
;
356 /* advance 'start' to point to date start delimiter */
357 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
361 date
= strtoul(msg
+ start
, &ep
, 10);
362 if (msg
+ start
== ep
)
365 if (part
== 't') { /* date, UNIX timestamp */
366 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
367 return placeholder_len
;
371 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
373 if (start
+ 1 < len
) {
374 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
375 if (msg
[start
] == '-')
381 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
382 return placeholder_len
;
383 case 'D': /* date, RFC2822 style */
384 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
385 return placeholder_len
;
386 case 'r': /* date, relative */
387 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
388 return placeholder_len
;
389 case 'i': /* date, ISO 8601 */
390 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
391 return placeholder_len
;
396 * bogus commit, 'sb' cannot be updated, but we still need to
397 * compute a valid return value.
399 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
400 || part
== 'D' || part
== 'r' || part
== 'i')
401 return placeholder_len
;
403 return 0; /* unknown placeholder */
411 struct format_commit_context
{
412 const struct commit
*commit
;
413 enum date_mode dmode
;
415 /* These offsets are relative to the start of the commit message. */
416 int commit_header_parsed
;
417 struct chunk subject
;
419 struct chunk committer
;
420 struct chunk encoding
;
423 /* The following ones are relative to the result struct strbuf. */
424 struct chunk abbrev_commit_hash
;
425 struct chunk abbrev_tree_hash
;
426 struct chunk abbrev_parent_hashes
;
429 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
432 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
437 * We haven't seen this chunk before. Our caller is surely
438 * going to add it the hard way now. Remember the most likely
439 * start of the to-be-added chunk: the current end of the
442 chunk
->off
= sb
->len
;
446 static void parse_commit_header(struct format_commit_context
*context
)
448 const char *msg
= context
->commit
->buffer
;
450 enum { HEADER
, SUBJECT
, BODY
} state
;
452 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
454 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
457 if (state
== SUBJECT
) {
458 context
->subject
.off
= i
;
459 context
->subject
.len
= eol
- i
;
464 /* strip empty lines */
465 while (msg
[eol
] == '\n' && msg
[eol
+ 1] == '\n')
467 } else if (!prefixcmp(msg
+ i
, "author ")) {
468 context
->author
.off
= i
+ 7;
469 context
->author
.len
= eol
- i
- 7;
470 } else if (!prefixcmp(msg
+ i
, "committer ")) {
471 context
->committer
.off
= i
+ 10;
472 context
->committer
.len
= eol
- i
- 10;
473 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
474 context
->encoding
.off
= i
+ 9;
475 context
->encoding
.len
= eol
- i
- 9;
481 context
->body_off
= i
;
482 context
->commit_header_parsed
= 1;
485 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
487 struct name_decoration
*d
;
488 const char *prefix
= " (";
490 load_ref_decorations();
491 d
= lookup_decoration(&name_decoration
, &commit
->object
);
493 strbuf_addstr(sb
, prefix
);
495 strbuf_addstr(sb
, d
->name
);
498 if (prefix
[0] == ',')
499 strbuf_addch(sb
, ')');
502 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
505 struct format_commit_context
*c
= context
;
506 const struct commit
*commit
= c
->commit
;
507 const char *msg
= commit
->buffer
;
508 struct commit_list
*p
;
511 /* these are independent of the commit */
512 switch (placeholder
[0]) {
514 if (!prefixcmp(placeholder
+ 1, "red")) {
515 strbuf_addstr(sb
, "\033[31m");
517 } else if (!prefixcmp(placeholder
+ 1, "green")) {
518 strbuf_addstr(sb
, "\033[32m");
520 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
521 strbuf_addstr(sb
, "\033[34m");
523 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
524 strbuf_addstr(sb
, "\033[m");
528 case 'n': /* newline */
529 strbuf_addch(sb
, '\n');
532 /* %x00 == NUL, %x0a == LF, etc. */
533 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
535 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
537 strbuf_addch(sb
, (h1
<<4)|h2
);
543 /* these depend on the commit */
544 if (!commit
->object
.parsed
)
545 parse_object(commit
->object
.sha1
);
547 switch (placeholder
[0]) {
548 case 'H': /* commit hash */
549 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
551 case 'h': /* abbreviated commit hash */
552 if (add_again(sb
, &c
->abbrev_commit_hash
))
554 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
556 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
558 case 'T': /* tree hash */
559 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
561 case 't': /* abbreviated tree hash */
562 if (add_again(sb
, &c
->abbrev_tree_hash
))
564 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
566 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
568 case 'P': /* parent hashes */
569 for (p
= commit
->parents
; p
; p
= p
->next
) {
570 if (p
!= commit
->parents
)
571 strbuf_addch(sb
, ' ');
572 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
575 case 'p': /* abbreviated parent hashes */
576 if (add_again(sb
, &c
->abbrev_parent_hashes
))
578 for (p
= commit
->parents
; p
; p
= p
->next
) {
579 if (p
!= commit
->parents
)
580 strbuf_addch(sb
, ' ');
581 strbuf_addstr(sb
, find_unique_abbrev(
582 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
584 c
->abbrev_parent_hashes
.len
= sb
->len
-
585 c
->abbrev_parent_hashes
.off
;
587 case 'm': /* left/right/bottom */
588 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
590 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
595 format_decoration(sb
, commit
);
599 /* For the rest we have to parse the commit header. */
600 if (!c
->commit_header_parsed
)
601 parse_commit_header(c
);
603 switch (placeholder
[0]) {
604 case 's': /* subject */
605 strbuf_add(sb
, msg
+ c
->subject
.off
, c
->subject
.len
);
607 case 'a': /* author ... */
608 return format_person_part(sb
, placeholder
[1],
609 msg
+ c
->author
.off
, c
->author
.len
,
611 case 'c': /* committer ... */
612 return format_person_part(sb
, placeholder
[1],
613 msg
+ c
->committer
.off
, c
->committer
.len
,
615 case 'e': /* encoding */
616 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
619 strbuf_addstr(sb
, msg
+ c
->body_off
);
622 return 0; /* unknown placeholder */
625 void format_commit_message(const struct commit
*commit
,
626 const void *format
, struct strbuf
*sb
,
627 enum date_mode dmode
)
629 struct format_commit_context context
;
631 memset(&context
, 0, sizeof(context
));
632 context
.commit
= commit
;
633 context
.dmode
= dmode
;
634 strbuf_expand(sb
, format
, format_commit_item
, &context
);
637 static void pp_header(enum cmit_fmt fmt
,
639 enum date_mode dmode
,
640 const char *encoding
,
641 const struct commit
*commit
,
645 int parents_shown
= 0;
648 const char *line
= *msg_p
;
649 int linelen
= get_one_line(*msg_p
);
659 if (fmt
== CMIT_FMT_RAW
) {
660 strbuf_add(sb
, line
, linelen
);
664 if (!memcmp(line
, "parent ", 7)) {
666 die("bad parent line in commit");
670 if (!parents_shown
) {
671 struct commit_list
*parent
;
673 for (parent
= commit
->parents
, num
= 0;
675 parent
= parent
->next
, num
++)
677 /* with enough slop */
678 strbuf_grow(sb
, num
* 50 + 20);
679 add_merge_info(fmt
, sb
, commit
, abbrev
);
684 * MEDIUM == DEFAULT shows only author with dates.
685 * FULL shows both authors but not dates.
686 * FULLER shows both authors and dates.
688 if (!memcmp(line
, "author ", 7)) {
689 strbuf_grow(sb
, linelen
+ 80);
690 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
692 if (!memcmp(line
, "committer ", 10) &&
693 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
694 strbuf_grow(sb
, linelen
+ 80);
695 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
700 void pp_title_line(enum cmit_fmt fmt
,
704 const char *after_subject
,
705 const char *encoding
,
710 strbuf_init(&title
, 80);
713 const char *line
= *msg_p
;
714 int linelen
= get_one_line(line
);
717 if (!linelen
|| is_empty_line(line
, &linelen
))
720 strbuf_grow(&title
, linelen
+ 2);
722 if (fmt
== CMIT_FMT_EMAIL
) {
723 strbuf_addch(&title
, '\n');
725 strbuf_addch(&title
, ' ');
727 strbuf_add(&title
, line
, linelen
);
730 strbuf_grow(sb
, title
.len
+ 1024);
732 strbuf_addstr(sb
, subject
);
733 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
735 strbuf_addbuf(sb
, &title
);
737 strbuf_addch(sb
, '\n');
739 if (need_8bit_cte
> 0) {
740 const char *header_fmt
=
741 "MIME-Version: 1.0\n"
742 "Content-Type: text/plain; charset=%s\n"
743 "Content-Transfer-Encoding: 8bit\n";
744 strbuf_addf(sb
, header_fmt
, encoding
);
747 strbuf_addstr(sb
, after_subject
);
749 if (fmt
== CMIT_FMT_EMAIL
) {
750 strbuf_addch(sb
, '\n');
752 strbuf_release(&title
);
755 void pp_remainder(enum cmit_fmt fmt
,
762 const char *line
= *msg_p
;
763 int linelen
= get_one_line(line
);
769 if (is_empty_line(line
, &linelen
)) {
772 if (fmt
== CMIT_FMT_SHORT
)
777 strbuf_grow(sb
, linelen
+ indent
+ 20);
779 memset(sb
->buf
+ sb
->len
, ' ', indent
);
780 strbuf_setlen(sb
, sb
->len
+ indent
);
782 strbuf_add(sb
, line
, linelen
);
783 strbuf_addch(sb
, '\n');
787 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
789 const char *encoding
;
791 encoding
= (git_log_output_encoding
792 ? git_log_output_encoding
793 : git_commit_encoding
);
797 *encoding_p
= encoding
;
798 return logmsg_reencode(commit
, encoding
);
801 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
802 struct strbuf
*sb
, int abbrev
,
803 const char *subject
, const char *after_subject
,
804 enum date_mode dmode
, int need_8bit_cte
)
806 unsigned long beginning_of_body
;
808 const char *msg
= commit
->buffer
;
810 const char *encoding
;
812 if (fmt
== CMIT_FMT_USERFORMAT
) {
813 format_commit_message(commit
, user_format
, sb
, dmode
);
817 reencoded
= reencode_commit_message(commit
, &encoding
);
822 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
826 * We need to check and emit Content-type: to mark it
827 * as 8-bit if we haven't done so.
829 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
832 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
834 /* author could be non 7-bit ASCII but
835 * the log may be so; skip over the
838 if (ch
== '\n' && msg
[i
+1] == '\n')
841 else if (non_ascii(ch
)) {
848 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
849 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
850 strbuf_addch(sb
, '\n');
853 /* Skip excess blank lines at the beginning of body, if any... */
855 int linelen
= get_one_line(msg
);
859 if (!is_empty_line(msg
, &ll
))
864 /* These formats treat the title line specially. */
865 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
866 pp_title_line(fmt
, &msg
, sb
, subject
,
867 after_subject
, encoding
, need_8bit_cte
);
869 beginning_of_body
= sb
->len
;
870 if (fmt
!= CMIT_FMT_ONELINE
)
871 pp_remainder(fmt
, &msg
, sb
, indent
);
874 /* Make sure there is an EOLN for the non-oneline case */
875 if (fmt
!= CMIT_FMT_ONELINE
)
876 strbuf_addch(sb
, '\n');
879 * The caller may append additional body text in e-mail
880 * format. Make sure we did not strip the blank line
881 * between the header and the body.
883 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
884 strbuf_addch(sb
, '\n');
886 if (fmt
!= CMIT_FMT_ONELINE
)
887 get_commit_notes(commit
, sb
, encoding
);