7 static struct cmt_fmt_map
{
12 { "raw", 1, CMIT_FMT_RAW
},
13 { "medium", 1, CMIT_FMT_MEDIUM
},
14 { "short", 1, CMIT_FMT_SHORT
},
15 { "email", 1, CMIT_FMT_EMAIL
},
16 { "full", 5, CMIT_FMT_FULL
},
17 { "fuller", 5, CMIT_FMT_FULLER
},
18 { "oneline", 1, CMIT_FMT_ONELINE
},
19 { "format:", 7, CMIT_FMT_USERFORMAT
},
22 static char *user_format
;
24 enum cmit_fmt
get_commit_format(const char *arg
)
29 return CMIT_FMT_DEFAULT
;
32 if (!prefixcmp(arg
, "format:")) {
35 user_format
= xstrdup(arg
+ 7);
36 return CMIT_FMT_USERFORMAT
;
38 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
39 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
40 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
)))
44 die("invalid --pretty format: %s", arg
);
48 * Generic support for pretty-printing the header
50 static int get_one_line(const char *msg
)
65 /* High bit set, or ISO-2022-INT */
69 return ((ch
& 0x80) || (ch
== 0x1b));
72 static int is_rfc2047_special(char ch
)
74 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
77 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
82 for (i
= 0; i
< len
; i
++) {
86 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
89 strbuf_add(sb
, line
, len
);
93 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
94 strbuf_addf(sb
, "=?%s?q?", encoding
);
95 for (i
= last
= 0; i
< len
; i
++) {
96 unsigned ch
= line
[i
] & 0xFF;
98 * We encode ' ' using '=20' even though rfc2047
99 * allows using '_' for readability. Unfortunately,
100 * many programs do not understand this and just
101 * leave the underscore in place.
103 if (is_rfc2047_special(ch
) || ch
== ' ') {
104 strbuf_add(sb
, line
+ last
, i
- last
);
105 strbuf_addf(sb
, "=%02X", ch
);
109 strbuf_add(sb
, line
+ last
, len
- last
);
110 strbuf_addstr(sb
, "?=");
113 static void add_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
114 const char *line
, enum date_mode dmode
,
115 const char *encoding
)
121 const char *filler
= " ";
123 if (fmt
== CMIT_FMT_ONELINE
)
125 date
= strchr(line
, '>');
128 namelen
= ++date
- line
;
129 time
= strtoul(date
, &date
, 10);
130 tz
= strtol(date
, NULL
, 10);
132 if (fmt
== CMIT_FMT_EMAIL
) {
133 char *name_tail
= strchr(line
, '<');
134 int display_name_length
;
137 while (line
< name_tail
&& isspace(name_tail
[-1]))
139 display_name_length
= name_tail
- line
;
141 strbuf_addstr(sb
, "From: ");
142 add_rfc2047(sb
, line
, display_name_length
, encoding
);
143 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
144 strbuf_addch(sb
, '\n');
146 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
147 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
148 filler
, namelen
, line
);
151 case CMIT_FMT_MEDIUM
:
152 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
155 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
157 case CMIT_FMT_FULLER
:
158 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
166 static int is_empty_line(const char *line
, int *len_p
)
169 while (len
&& isspace(line
[len
-1]))
175 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
176 const struct commit
*commit
, int abbrev
)
178 struct commit_list
*parent
= commit
->parents
;
180 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
181 !parent
|| !parent
->next
)
184 strbuf_addstr(sb
, "Merge:");
187 struct commit
*p
= parent
->item
;
188 const char *hex
= NULL
;
191 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
193 hex
= sha1_to_hex(p
->object
.sha1
);
194 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
195 parent
= parent
->next
;
197 strbuf_addf(sb
, " %s%s", hex
, dots
);
199 strbuf_addch(sb
, '\n');
202 static char *get_header(const struct commit
*commit
, const char *key
)
204 int key_len
= strlen(key
);
205 const char *line
= commit
->buffer
;
208 const char *eol
= strchr(line
, '\n'), *next
;
213 eol
= line
+ strlen(line
);
217 if (eol
- line
> key_len
&&
218 !strncmp(line
, key
, key_len
) &&
219 line
[key_len
] == ' ') {
220 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
226 static char *replace_encoding_header(char *buf
, const char *encoding
)
232 /* guess if there is an encoding header before a \n\n */
233 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
234 cp
= strchr(cp
, '\n');
235 if (!cp
|| *++cp
== '\n')
239 cp
= strchr(cp
, '\n');
241 return buf
; /* should not happen but be defensive */
242 len
= cp
+ 1 - (buf
+ start
);
244 strbuf_init(&tmp
, 0);
245 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
246 if (is_encoding_utf8(encoding
)) {
247 /* we have re-coded to UTF-8; drop the header */
248 strbuf_remove(&tmp
, start
, len
);
250 /* just replaces XXXX in 'encoding XXXX\n' */
251 strbuf_splice(&tmp
, start
+ strlen("encoding "),
252 len
- strlen("encoding \n"),
253 encoding
, strlen(encoding
));
255 return strbuf_detach(&tmp
, NULL
);
258 static char *logmsg_reencode(const struct commit
*commit
,
259 const char *output_encoding
)
261 static const char *utf8
= "utf-8";
262 const char *use_encoding
;
266 if (!*output_encoding
)
268 encoding
= get_header(commit
, "encoding");
269 use_encoding
= encoding
? encoding
: utf8
;
270 if (!strcmp(use_encoding
, output_encoding
))
271 if (encoding
) /* we'll strip encoding header later */
272 out
= xstrdup(commit
->buffer
);
274 return NULL
; /* nothing to do */
276 out
= reencode_string(commit
->buffer
,
277 output_encoding
, use_encoding
);
279 out
= replace_encoding_header(out
, output_encoding
);
285 static void format_person_part(struct strbuf
*sb
, char part
,
286 const char *msg
, int len
)
288 int start
, end
, tz
= 0;
293 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
296 while (end
> 0 && isspace(msg
[end
- 1]))
298 if (part
== 'n') { /* name */
299 strbuf_add(sb
, msg
, end
);
307 for (end
= start
+ 1; end
< len
&& msg
[end
] != '>'; end
++)
313 if (part
== 'e') { /* email */
314 strbuf_add(sb
, msg
+ start
, end
- start
);
319 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
323 date
= strtoul(msg
+ start
, &ep
, 10);
324 if (msg
+ start
== ep
)
327 if (part
== 't') { /* date, UNIX timestamp */
328 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
333 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
335 if (start
+ 1 < len
) {
336 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
337 if (msg
[start
] == '-')
343 strbuf_addstr(sb
, show_date(date
, tz
, DATE_NORMAL
));
345 case 'D': /* date, RFC2822 style */
346 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
348 case 'r': /* date, relative */
349 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
351 case 'i': /* date, ISO 8601 */
352 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
357 static void format_commit_item(struct strbuf
*sb
, const char *placeholder
,
360 const struct commit
*commit
= context
;
361 struct commit_list
*p
;
363 enum { HEADER
, SUBJECT
, BODY
} state
;
364 const char *msg
= commit
->buffer
;
366 /* these are independent of the commit */
367 switch (placeholder
[0]) {
369 switch (placeholder
[3]) {
371 strbuf_addstr(sb
, "\033[31m");
373 case 'e': /* green */
374 strbuf_addstr(sb
, "\033[32m");
377 strbuf_addstr(sb
, "\033[34m");
379 case 's': /* reset color */
380 strbuf_addstr(sb
, "\033[m");
383 case 'n': /* newline */
384 strbuf_addch(sb
, '\n');
388 /* these depend on the commit */
389 if (!commit
->object
.parsed
)
390 parse_object(commit
->object
.sha1
);
392 switch (placeholder
[0]) {
393 case 'H': /* commit hash */
394 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
396 case 'h': /* abbreviated commit hash */
397 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
400 case 'T': /* tree hash */
401 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
403 case 't': /* abbreviated tree hash */
404 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
407 case 'P': /* parent hashes */
408 for (p
= commit
->parents
; p
; p
= p
->next
) {
409 if (p
!= commit
->parents
)
410 strbuf_addch(sb
, ' ');
411 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
414 case 'p': /* abbreviated parent hashes */
415 for (p
= commit
->parents
; p
; p
= p
->next
) {
416 if (p
!= commit
->parents
)
417 strbuf_addch(sb
, ' ');
418 strbuf_addstr(sb
, find_unique_abbrev(
419 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
422 case 'm': /* left/right/bottom */
423 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
425 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
431 /* For the rest we have to parse the commit header. */
432 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
434 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
437 if (state
== SUBJECT
) {
438 if (placeholder
[0] == 's') {
439 strbuf_add(sb
, msg
+ i
, eol
- i
);
446 /* strip empty lines */
447 while (msg
[eol
+ 1] == '\n')
449 } else if (!prefixcmp(msg
+ i
, "author ")) {
450 if (placeholder
[0] == 'a') {
451 format_person_part(sb
, placeholder
[1],
452 msg
+ i
+ 7, eol
- i
- 7);
455 } else if (!prefixcmp(msg
+ i
, "committer ")) {
456 if (placeholder
[0] == 'c') {
457 format_person_part(sb
, placeholder
[1],
458 msg
+ i
+ 10, eol
- i
- 10);
461 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
462 if (placeholder
[0] == 'e') {
463 strbuf_add(sb
, msg
+ i
+ 9, eol
- i
- 9);
469 if (msg
[i
] && placeholder
[0] == 'b') /* body */
470 strbuf_addstr(sb
, msg
+ i
);
473 void format_commit_message(const struct commit
*commit
,
474 const void *format
, struct strbuf
*sb
)
476 const char *placeholders
[] = {
477 "H", /* commit hash */
478 "h", /* abbreviated commit hash */
480 "t", /* abbreviated tree hash */
481 "P", /* parent hashes */
482 "p", /* abbreviated parent hashes */
483 "an", /* author name */
484 "ae", /* author email */
485 "ad", /* author date */
486 "aD", /* author date, RFC2822 style */
487 "ar", /* author date, relative */
488 "at", /* author date, UNIX timestamp */
489 "ai", /* author date, ISO 8601 */
490 "cn", /* committer name */
491 "ce", /* committer email */
492 "cd", /* committer date */
493 "cD", /* committer date, RFC2822 style */
494 "cr", /* committer date, relative */
495 "ct", /* committer date, UNIX timestamp */
496 "ci", /* committer date, ISO 8601 */
501 "Cgreen", /* green */
503 "Creset", /* reset color */
505 "m", /* left/right/bottom */
508 strbuf_expand(sb
, format
, placeholders
, format_commit_item
, (void *)commit
);
511 static void pp_header(enum cmit_fmt fmt
,
513 enum date_mode dmode
,
514 const char *encoding
,
515 const struct commit
*commit
,
519 int parents_shown
= 0;
522 const char *line
= *msg_p
;
523 int linelen
= get_one_line(*msg_p
);
533 if (fmt
== CMIT_FMT_RAW
) {
534 strbuf_add(sb
, line
, linelen
);
538 if (!memcmp(line
, "parent ", 7)) {
540 die("bad parent line in commit");
544 if (!parents_shown
) {
545 struct commit_list
*parent
;
547 for (parent
= commit
->parents
, num
= 0;
549 parent
= parent
->next
, num
++)
551 /* with enough slop */
552 strbuf_grow(sb
, num
* 50 + 20);
553 add_merge_info(fmt
, sb
, commit
, abbrev
);
558 * MEDIUM == DEFAULT shows only author with dates.
559 * FULL shows both authors but not dates.
560 * FULLER shows both authors and dates.
562 if (!memcmp(line
, "author ", 7)) {
563 strbuf_grow(sb
, linelen
+ 80);
564 add_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
566 if (!memcmp(line
, "committer ", 10) &&
567 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
568 strbuf_grow(sb
, linelen
+ 80);
569 add_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
574 static void pp_title_line(enum cmit_fmt fmt
,
578 const char *after_subject
,
579 const char *encoding
,
584 strbuf_init(&title
, 80);
587 const char *line
= *msg_p
;
588 int linelen
= get_one_line(line
);
591 if (!linelen
|| is_empty_line(line
, &linelen
))
594 strbuf_grow(&title
, linelen
+ 2);
596 if (fmt
== CMIT_FMT_EMAIL
) {
597 strbuf_addch(&title
, '\n');
599 strbuf_addch(&title
, ' ');
601 strbuf_add(&title
, line
, linelen
);
604 strbuf_grow(sb
, title
.len
+ 1024);
606 strbuf_addstr(sb
, subject
);
607 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
609 strbuf_addbuf(sb
, &title
);
611 strbuf_addch(sb
, '\n');
613 if (plain_non_ascii
) {
614 const char *header_fmt
=
615 "MIME-Version: 1.0\n"
616 "Content-Type: text/plain; charset=%s\n"
617 "Content-Transfer-Encoding: 8bit\n";
618 strbuf_addf(sb
, header_fmt
, encoding
);
621 strbuf_addstr(sb
, after_subject
);
623 if (fmt
== CMIT_FMT_EMAIL
) {
624 strbuf_addch(sb
, '\n');
626 strbuf_release(&title
);
629 static void pp_remainder(enum cmit_fmt fmt
,
636 const char *line
= *msg_p
;
637 int linelen
= get_one_line(line
);
643 if (is_empty_line(line
, &linelen
)) {
646 if (fmt
== CMIT_FMT_SHORT
)
651 strbuf_grow(sb
, linelen
+ indent
+ 20);
653 memset(sb
->buf
+ sb
->len
, ' ', indent
);
654 strbuf_setlen(sb
, sb
->len
+ indent
);
656 strbuf_add(sb
, line
, linelen
);
657 strbuf_addch(sb
, '\n');
661 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
662 struct strbuf
*sb
, int abbrev
,
663 const char *subject
, const char *after_subject
,
664 enum date_mode dmode
, int plain_non_ascii
)
666 unsigned long beginning_of_body
;
668 const char *msg
= commit
->buffer
;
670 const char *encoding
;
672 if (fmt
== CMIT_FMT_USERFORMAT
) {
673 format_commit_message(commit
, user_format
, sb
);
677 encoding
= (git_log_output_encoding
678 ? git_log_output_encoding
679 : git_commit_encoding
);
682 reencoded
= logmsg_reencode(commit
, encoding
);
687 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
690 /* After-subject is used to pass in Content-Type: multipart
691 * MIME header; in that case we do not have to do the
692 * plaintext content type even if the commit message has
693 * non 7-bit ASCII character. Otherwise, check if we need
694 * to say this is not a 7-bit ASCII.
696 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
699 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
701 /* author could be non 7-bit ASCII but
702 * the log may be so; skip over the
705 if (ch
== '\n' && msg
[i
+1] == '\n')
708 else if (non_ascii(ch
)) {
715 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
716 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
717 strbuf_addch(sb
, '\n');
720 /* Skip excess blank lines at the beginning of body, if any... */
722 int linelen
= get_one_line(msg
);
726 if (!is_empty_line(msg
, &ll
))
731 /* These formats treat the title line specially. */
732 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
733 pp_title_line(fmt
, &msg
, sb
, subject
,
734 after_subject
, encoding
, plain_non_ascii
);
736 beginning_of_body
= sb
->len
;
737 if (fmt
!= CMIT_FMT_ONELINE
)
738 pp_remainder(fmt
, &msg
, sb
, indent
);
741 /* Make sure there is an EOLN for the non-oneline case */
742 if (fmt
!= CMIT_FMT_ONELINE
)
743 strbuf_addch(sb
, '\n');
746 * The caller may append additional body text in e-mail
747 * format. Make sure we did not strip the blank line
748 * between the header and the body.
750 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
751 strbuf_addch(sb
, '\n');