6 #include "string-list.h"
11 static char *user_format
;
13 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
16 user_format
= xstrdup(cp
);
18 rev
->use_terminator
= 1;
19 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
22 void get_commit_format(const char *arg
, struct rev_info
*rev
)
25 static struct cmt_fmt_map
{
30 { "raw", 1, CMIT_FMT_RAW
},
31 { "medium", 1, CMIT_FMT_MEDIUM
},
32 { "short", 1, CMIT_FMT_SHORT
},
33 { "email", 1, CMIT_FMT_EMAIL
},
34 { "full", 5, CMIT_FMT_FULL
},
35 { "fuller", 5, CMIT_FMT_FULLER
},
36 { "oneline", 1, CMIT_FMT_ONELINE
},
39 rev
->use_terminator
= 0;
41 rev
->commit_format
= CMIT_FMT_DEFAULT
;
44 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
45 save_user_format(rev
, strchr(arg
, ':') + 1, arg
[0] == 't');
48 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
49 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
50 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
))) {
51 if (cmt_fmts
[i
].v
== CMIT_FMT_ONELINE
)
52 rev
->use_terminator
= 1;
53 rev
->commit_format
= cmt_fmts
[i
].v
;
57 if (strchr(arg
, '%')) {
58 save_user_format(rev
, arg
, 1);
62 die("invalid --pretty format: %s", arg
);
66 * Generic support for pretty-printing the header
68 static int get_one_line(const char *msg
)
83 /* High bit set, or ISO-2022-INT */
86 return !isascii(ch
) || ch
== '\033';
89 int has_non_ascii(const char *s
)
94 while ((ch
= *s
++) != '\0') {
101 static int is_rfc2047_special(char ch
)
103 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
106 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
107 const char *encoding
)
111 for (i
= 0; i
< len
; i
++) {
115 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
118 strbuf_add(sb
, line
, len
);
122 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
123 strbuf_addf(sb
, "=?%s?q?", encoding
);
124 for (i
= last
= 0; i
< len
; i
++) {
125 unsigned ch
= line
[i
] & 0xFF;
127 * We encode ' ' using '=20' even though rfc2047
128 * allows using '_' for readability. Unfortunately,
129 * many programs do not understand this and just
130 * leave the underscore in place.
132 if (is_rfc2047_special(ch
) || ch
== ' ') {
133 strbuf_add(sb
, line
+ last
, i
- last
);
134 strbuf_addf(sb
, "=%02X", ch
);
138 strbuf_add(sb
, line
+ last
, len
- last
);
139 strbuf_addstr(sb
, "?=");
142 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
143 const char *line
, enum date_mode dmode
,
144 const char *encoding
)
151 if (fmt
== CMIT_FMT_ONELINE
)
153 date
= strchr(line
, '>');
156 namelen
= ++date
- line
;
157 time
= strtoul(date
, &date
, 10);
158 tz
= strtol(date
, NULL
, 10);
160 if (fmt
== CMIT_FMT_EMAIL
) {
161 char *name_tail
= strchr(line
, '<');
162 int display_name_length
;
165 while (line
< name_tail
&& isspace(name_tail
[-1]))
167 display_name_length
= name_tail
- line
;
168 strbuf_addstr(sb
, "From: ");
169 add_rfc2047(sb
, line
, display_name_length
, encoding
);
170 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
171 strbuf_addch(sb
, '\n');
173 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
174 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
178 case CMIT_FMT_MEDIUM
:
179 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
182 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
184 case CMIT_FMT_FULLER
:
185 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
193 static int is_empty_line(const char *line
, int *len_p
)
196 while (len
&& isspace(line
[len
-1]))
202 static const char *skip_empty_lines(const char *msg
)
205 int linelen
= get_one_line(msg
);
209 if (!is_empty_line(msg
, &ll
))
216 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
217 const struct commit
*commit
, int abbrev
)
219 struct commit_list
*parent
= commit
->parents
;
221 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
222 !parent
|| !parent
->next
)
225 strbuf_addstr(sb
, "Merge:");
228 struct commit
*p
= parent
->item
;
229 const char *hex
= NULL
;
231 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
233 hex
= sha1_to_hex(p
->object
.sha1
);
234 parent
= parent
->next
;
236 strbuf_addf(sb
, " %s", hex
);
238 strbuf_addch(sb
, '\n');
241 static char *get_header(const struct commit
*commit
, const char *key
)
243 int key_len
= strlen(key
);
244 const char *line
= commit
->buffer
;
247 const char *eol
= strchr(line
, '\n'), *next
;
252 eol
= line
+ strlen(line
);
256 if (eol
- line
> key_len
&&
257 !strncmp(line
, key
, key_len
) &&
258 line
[key_len
] == ' ') {
259 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
265 static char *replace_encoding_header(char *buf
, const char *encoding
)
267 struct strbuf tmp
= STRBUF_INIT
;
271 /* guess if there is an encoding header before a \n\n */
272 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
273 cp
= strchr(cp
, '\n');
274 if (!cp
|| *++cp
== '\n')
278 cp
= strchr(cp
, '\n');
280 return buf
; /* should not happen but be defensive */
281 len
= cp
+ 1 - (buf
+ start
);
283 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
284 if (is_encoding_utf8(encoding
)) {
285 /* we have re-coded to UTF-8; drop the header */
286 strbuf_remove(&tmp
, start
, len
);
288 /* just replaces XXXX in 'encoding XXXX\n' */
289 strbuf_splice(&tmp
, start
+ strlen("encoding "),
290 len
- strlen("encoding \n"),
291 encoding
, strlen(encoding
));
293 return strbuf_detach(&tmp
, NULL
);
296 static char *logmsg_reencode(const struct commit
*commit
,
297 const char *output_encoding
)
299 static const char *utf8
= "UTF-8";
300 const char *use_encoding
;
304 if (!*output_encoding
)
306 encoding
= get_header(commit
, "encoding");
307 use_encoding
= encoding
? encoding
: utf8
;
308 if (!strcmp(use_encoding
, output_encoding
))
309 if (encoding
) /* we'll strip encoding header later */
310 out
= xstrdup(commit
->buffer
);
312 return NULL
; /* nothing to do */
314 out
= reencode_string(commit
->buffer
,
315 output_encoding
, use_encoding
);
317 out
= replace_encoding_header(out
, output_encoding
);
323 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
325 static struct string_list
*mail_map
;
327 mail_map
= xcalloc(1, sizeof(*mail_map
));
328 read_mailmap(mail_map
, NULL
);
330 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
333 static size_t format_person_part(struct strbuf
*sb
, char part
,
334 const char *msg
, int len
, enum date_mode dmode
)
336 /* currently all placeholders have same length */
337 const int placeholder_len
= 2;
338 int start
, end
, tz
= 0;
339 unsigned long date
= 0;
341 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
342 char person_name
[1024];
343 char person_mail
[1024];
345 /* advance 'end' to point to email start delimiter */
346 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
350 * When end points at the '<' that we found, it should have
351 * matching '>' later, which means 'end' must be strictly
357 /* Seek for both name and email part */
360 while (name_end
> name_start
&& isspace(*(name_end
-1)))
362 mail_start
= msg
+end
+1;
363 mail_end
= mail_start
;
364 while (mail_end
< msg_end
&& *mail_end
!= '>')
366 if (mail_end
== msg_end
)
370 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
371 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
372 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
373 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
374 name_start
= person_name
;
375 name_end
= name_start
+ strlen(person_name
);
376 mail_start
= person_mail
;
377 mail_end
= mail_start
+ strlen(person_mail
);
379 if (part
== 'n' || part
== 'N') { /* name */
380 strbuf_add(sb
, name_start
, name_end
-name_start
);
381 return placeholder_len
;
383 if (part
== 'e' || part
== 'E') { /* email */
384 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
385 return placeholder_len
;
388 /* advance 'start' to point to date start delimiter */
389 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
393 date
= strtoul(msg
+ start
, &ep
, 10);
394 if (msg
+ start
== ep
)
397 if (part
== 't') { /* date, UNIX timestamp */
398 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
399 return placeholder_len
;
403 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
405 if (start
+ 1 < len
) {
406 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
407 if (msg
[start
] == '-')
413 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
414 return placeholder_len
;
415 case 'D': /* date, RFC2822 style */
416 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
417 return placeholder_len
;
418 case 'r': /* date, relative */
419 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
420 return placeholder_len
;
421 case 'i': /* date, ISO 8601 */
422 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
423 return placeholder_len
;
428 * bogus commit, 'sb' cannot be updated, but we still need to
429 * compute a valid return value.
431 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
432 || part
== 'D' || part
== 'r' || part
== 'i')
433 return placeholder_len
;
435 return 0; /* unknown placeholder */
443 struct format_commit_context
{
444 const struct commit
*commit
;
445 enum date_mode dmode
;
446 unsigned commit_header_parsed
:1;
447 unsigned commit_message_parsed
:1;
448 size_t width
, indent1
, indent2
;
450 /* These offsets are relative to the start of the commit message. */
452 struct chunk committer
;
453 struct chunk encoding
;
458 /* The following ones are relative to the result struct strbuf. */
459 struct chunk abbrev_commit_hash
;
460 struct chunk abbrev_tree_hash
;
461 struct chunk abbrev_parent_hashes
;
465 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
468 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
473 * We haven't seen this chunk before. Our caller is surely
474 * going to add it the hard way now. Remember the most likely
475 * start of the to-be-added chunk: the current end of the
478 chunk
->off
= sb
->len
;
482 static void parse_commit_header(struct format_commit_context
*context
)
484 const char *msg
= context
->commit
->buffer
;
487 for (i
= 0; msg
[i
]; i
++) {
489 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
494 } else if (!prefixcmp(msg
+ i
, "author ")) {
495 context
->author
.off
= i
+ 7;
496 context
->author
.len
= eol
- i
- 7;
497 } else if (!prefixcmp(msg
+ i
, "committer ")) {
498 context
->committer
.off
= i
+ 10;
499 context
->committer
.len
= eol
- i
- 10;
500 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
501 context
->encoding
.off
= i
+ 9;
502 context
->encoding
.len
= eol
- i
- 9;
506 context
->message_off
= i
;
507 context
->commit_header_parsed
= 1;
510 static int istitlechar(char c
)
512 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
513 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
516 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
519 size_t start_len
= sb
->len
;
522 for (; *msg
&& *msg
!= '\n'; msg
++) {
523 if (istitlechar(*msg
)) {
525 strbuf_addch(sb
, '-');
527 strbuf_addch(sb
, *msg
);
529 while (*(msg
+1) == '.')
535 /* trim any trailing '.' or '-' characters */
537 while (sb
->len
- trimlen
> start_len
&&
538 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
539 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
541 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
544 const char *format_subject(struct strbuf
*sb
, const char *msg
,
545 const char *line_separator
)
550 const char *line
= msg
;
551 int linelen
= get_one_line(line
);
554 if (!linelen
|| is_empty_line(line
, &linelen
))
559 strbuf_grow(sb
, linelen
+ 2);
561 strbuf_addstr(sb
, line_separator
);
562 strbuf_add(sb
, line
, linelen
);
568 static void parse_commit_message(struct format_commit_context
*c
)
570 const char *msg
= c
->commit
->buffer
+ c
->message_off
;
571 const char *start
= c
->commit
->buffer
;
573 msg
= skip_empty_lines(msg
);
574 c
->subject_off
= msg
- start
;
576 msg
= format_subject(NULL
, msg
, NULL
);
577 msg
= skip_empty_lines(msg
);
578 c
->body_off
= msg
- start
;
580 c
->commit_message_parsed
= 1;
583 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
585 struct name_decoration
*d
;
586 const char *prefix
= " (";
588 load_ref_decorations(DECORATE_SHORT_REFS
);
589 d
= lookup_decoration(&name_decoration
, &commit
->object
);
591 strbuf_addstr(sb
, prefix
);
593 strbuf_addstr(sb
, d
->name
);
596 if (prefix
[0] == ',')
597 strbuf_addch(sb
, ')');
600 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
601 size_t width
, size_t indent1
, size_t indent2
)
603 struct strbuf tmp
= STRBUF_INIT
;
606 strbuf_add(&tmp
, sb
->buf
, pos
);
607 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
608 (int) indent1
, (int) indent2
, (int) width
);
609 strbuf_swap(&tmp
, sb
);
610 strbuf_release(&tmp
);
613 static void rewrap_message_tail(struct strbuf
*sb
,
614 struct format_commit_context
*c
,
615 size_t new_width
, size_t new_indent1
,
618 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
619 c
->indent2
== new_indent2
)
621 if (c
->wrap_start
< sb
->len
)
622 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
623 c
->wrap_start
= sb
->len
;
624 c
->width
= new_width
;
625 c
->indent1
= new_indent1
;
626 c
->indent2
= new_indent2
;
629 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
632 struct format_commit_context
*c
= context
;
633 const struct commit
*commit
= c
->commit
;
634 const char *msg
= commit
->buffer
;
635 struct commit_list
*p
;
638 /* these are independent of the commit */
639 switch (placeholder
[0]) {
641 if (placeholder
[1] == '(') {
642 const char *end
= strchr(placeholder
+ 2, ')');
643 char color
[COLOR_MAXLEN
];
646 color_parse_mem(placeholder
+ 2,
647 end
- (placeholder
+ 2),
648 "--pretty format", color
);
649 strbuf_addstr(sb
, color
);
650 return end
- placeholder
+ 1;
652 if (!prefixcmp(placeholder
+ 1, "red")) {
653 strbuf_addstr(sb
, GIT_COLOR_RED
);
655 } else if (!prefixcmp(placeholder
+ 1, "green")) {
656 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
658 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
659 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
661 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
662 strbuf_addstr(sb
, GIT_COLOR_RESET
);
666 case 'n': /* newline */
667 strbuf_addch(sb
, '\n');
670 /* %x00 == NUL, %x0a == LF, etc. */
671 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
673 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
675 strbuf_addch(sb
, (h1
<<4)|h2
);
680 if (placeholder
[1] == '(') {
681 unsigned long width
= 0, indent1
= 0, indent2
= 0;
683 const char *start
= placeholder
+ 2;
684 const char *end
= strchr(start
, ')');
688 width
= strtoul(start
, &next
, 10);
690 indent1
= strtoul(next
+ 1, &next
, 10);
692 indent2
= strtoul(next
+ 1,
699 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
700 return end
- placeholder
+ 1;
705 /* these depend on the commit */
706 if (!commit
->object
.parsed
)
707 parse_object(commit
->object
.sha1
);
709 switch (placeholder
[0]) {
710 case 'H': /* commit hash */
711 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
713 case 'h': /* abbreviated commit hash */
714 if (add_again(sb
, &c
->abbrev_commit_hash
))
716 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
718 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
720 case 'T': /* tree hash */
721 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
723 case 't': /* abbreviated tree hash */
724 if (add_again(sb
, &c
->abbrev_tree_hash
))
726 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
728 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
730 case 'P': /* parent hashes */
731 for (p
= commit
->parents
; p
; p
= p
->next
) {
732 if (p
!= commit
->parents
)
733 strbuf_addch(sb
, ' ');
734 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
737 case 'p': /* abbreviated parent hashes */
738 if (add_again(sb
, &c
->abbrev_parent_hashes
))
740 for (p
= commit
->parents
; p
; p
= p
->next
) {
741 if (p
!= commit
->parents
)
742 strbuf_addch(sb
, ' ');
743 strbuf_addstr(sb
, find_unique_abbrev(
744 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
746 c
->abbrev_parent_hashes
.len
= sb
->len
-
747 c
->abbrev_parent_hashes
.off
;
749 case 'm': /* left/right/bottom */
750 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
752 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
757 format_decoration(sb
, commit
);
761 /* For the rest we have to parse the commit header. */
762 if (!c
->commit_header_parsed
)
763 parse_commit_header(c
);
765 switch (placeholder
[0]) {
766 case 'a': /* author ... */
767 return format_person_part(sb
, placeholder
[1],
768 msg
+ c
->author
.off
, c
->author
.len
,
770 case 'c': /* committer ... */
771 return format_person_part(sb
, placeholder
[1],
772 msg
+ c
->committer
.off
, c
->committer
.len
,
774 case 'e': /* encoding */
775 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
779 /* Now we need to parse the commit message. */
780 if (!c
->commit_message_parsed
)
781 parse_commit_message(c
);
783 switch (placeholder
[0]) {
784 case 's': /* subject */
785 format_subject(sb
, msg
+ c
->subject_off
, " ");
787 case 'f': /* sanitized subject */
788 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
791 strbuf_addstr(sb
, msg
+ c
->body_off
);
794 return 0; /* unknown placeholder */
797 void format_commit_message(const struct commit
*commit
,
798 const char *format
, struct strbuf
*sb
,
799 enum date_mode dmode
)
801 struct format_commit_context context
;
803 memset(&context
, 0, sizeof(context
));
804 context
.commit
= commit
;
805 context
.dmode
= dmode
;
806 context
.wrap_start
= sb
->len
;
807 strbuf_expand(sb
, format
, format_commit_item
, &context
);
808 rewrap_message_tail(sb
, &context
, 0, 0, 0);
811 static void pp_header(enum cmit_fmt fmt
,
813 enum date_mode dmode
,
814 const char *encoding
,
815 const struct commit
*commit
,
819 int parents_shown
= 0;
822 const char *line
= *msg_p
;
823 int linelen
= get_one_line(*msg_p
);
833 if (fmt
== CMIT_FMT_RAW
) {
834 strbuf_add(sb
, line
, linelen
);
838 if (!memcmp(line
, "parent ", 7)) {
840 die("bad parent line in commit");
844 if (!parents_shown
) {
845 struct commit_list
*parent
;
847 for (parent
= commit
->parents
, num
= 0;
849 parent
= parent
->next
, num
++)
851 /* with enough slop */
852 strbuf_grow(sb
, num
* 50 + 20);
853 add_merge_info(fmt
, sb
, commit
, abbrev
);
858 * MEDIUM == DEFAULT shows only author with dates.
859 * FULL shows both authors but not dates.
860 * FULLER shows both authors and dates.
862 if (!memcmp(line
, "author ", 7)) {
863 strbuf_grow(sb
, linelen
+ 80);
864 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
866 if (!memcmp(line
, "committer ", 10) &&
867 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
868 strbuf_grow(sb
, linelen
+ 80);
869 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
874 void pp_title_line(enum cmit_fmt fmt
,
878 const char *after_subject
,
879 const char *encoding
,
882 const char *line_separator
= (fmt
== CMIT_FMT_EMAIL
) ? "\n " : " ";
885 strbuf_init(&title
, 80);
886 *msg_p
= format_subject(&title
, *msg_p
, line_separator
);
888 strbuf_grow(sb
, title
.len
+ 1024);
890 strbuf_addstr(sb
, subject
);
891 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
893 strbuf_addbuf(sb
, &title
);
895 strbuf_addch(sb
, '\n');
897 if (need_8bit_cte
> 0) {
898 const char *header_fmt
=
899 "MIME-Version: 1.0\n"
900 "Content-Type: text/plain; charset=%s\n"
901 "Content-Transfer-Encoding: 8bit\n";
902 strbuf_addf(sb
, header_fmt
, encoding
);
905 strbuf_addstr(sb
, after_subject
);
907 if (fmt
== CMIT_FMT_EMAIL
) {
908 strbuf_addch(sb
, '\n');
910 strbuf_release(&title
);
913 void pp_remainder(enum cmit_fmt fmt
,
920 const char *line
= *msg_p
;
921 int linelen
= get_one_line(line
);
927 if (is_empty_line(line
, &linelen
)) {
930 if (fmt
== CMIT_FMT_SHORT
)
935 strbuf_grow(sb
, linelen
+ indent
+ 20);
937 memset(sb
->buf
+ sb
->len
, ' ', indent
);
938 strbuf_setlen(sb
, sb
->len
+ indent
);
940 strbuf_add(sb
, line
, linelen
);
941 strbuf_addch(sb
, '\n');
945 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
947 const char *encoding
;
949 encoding
= (git_log_output_encoding
950 ? git_log_output_encoding
951 : git_commit_encoding
);
955 *encoding_p
= encoding
;
956 return logmsg_reencode(commit
, encoding
);
959 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
960 struct strbuf
*sb
, int abbrev
,
961 const char *subject
, const char *after_subject
,
962 enum date_mode dmode
, int need_8bit_cte
)
964 unsigned long beginning_of_body
;
966 const char *msg
= commit
->buffer
;
968 const char *encoding
;
970 if (fmt
== CMIT_FMT_USERFORMAT
) {
971 format_commit_message(commit
, user_format
, sb
, dmode
);
975 reencoded
= reencode_commit_message(commit
, &encoding
);
980 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
984 * We need to check and emit Content-type: to mark it
985 * as 8-bit if we haven't done so.
987 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
990 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
992 /* author could be non 7-bit ASCII but
993 * the log may be so; skip over the
996 if (ch
== '\n' && msg
[i
+1] == '\n')
999 else if (non_ascii(ch
)) {
1006 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
1007 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
1008 strbuf_addch(sb
, '\n');
1011 /* Skip excess blank lines at the beginning of body, if any... */
1012 msg
= skip_empty_lines(msg
);
1014 /* These formats treat the title line specially. */
1015 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1016 pp_title_line(fmt
, &msg
, sb
, subject
,
1017 after_subject
, encoding
, need_8bit_cte
);
1019 beginning_of_body
= sb
->len
;
1020 if (fmt
!= CMIT_FMT_ONELINE
)
1021 pp_remainder(fmt
, &msg
, sb
, indent
);
1024 /* Make sure there is an EOLN for the non-oneline case */
1025 if (fmt
!= CMIT_FMT_ONELINE
)
1026 strbuf_addch(sb
, '\n');
1029 * The caller may append additional body text in e-mail
1030 * format. Make sure we did not strip the blank line
1031 * between the header and the body.
1033 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1034 strbuf_addch(sb
, '\n');