6 #include "string-list.h"
10 #include "reflog-walk.h"
12 static char *user_format
;
14 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
17 user_format
= xstrdup(cp
);
19 rev
->use_terminator
= 1;
20 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
23 void get_commit_format(const char *arg
, struct rev_info
*rev
)
26 static struct cmt_fmt_map
{
31 { "raw", 1, CMIT_FMT_RAW
},
32 { "medium", 1, CMIT_FMT_MEDIUM
},
33 { "short", 1, CMIT_FMT_SHORT
},
34 { "email", 1, CMIT_FMT_EMAIL
},
35 { "full", 5, CMIT_FMT_FULL
},
36 { "fuller", 5, CMIT_FMT_FULLER
},
37 { "oneline", 1, CMIT_FMT_ONELINE
},
40 rev
->use_terminator
= 0;
42 rev
->commit_format
= CMIT_FMT_DEFAULT
;
45 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
46 save_user_format(rev
, strchr(arg
, ':') + 1, arg
[0] == 't');
49 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
50 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
51 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
))) {
52 if (cmt_fmts
[i
].v
== CMIT_FMT_ONELINE
)
53 rev
->use_terminator
= 1;
54 rev
->commit_format
= cmt_fmts
[i
].v
;
58 if (strchr(arg
, '%')) {
59 save_user_format(rev
, arg
, 1);
63 die("invalid --pretty format: %s", arg
);
67 * Generic support for pretty-printing the header
69 static int get_one_line(const char *msg
)
84 /* High bit set, or ISO-2022-INT */
87 return !isascii(ch
) || ch
== '\033';
90 int has_non_ascii(const char *s
)
95 while ((ch
= *s
++) != '\0') {
102 static int is_rfc2047_special(char ch
)
104 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
107 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
108 const char *encoding
)
112 for (i
= 0; i
< len
; i
++) {
116 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
119 strbuf_add(sb
, line
, len
);
123 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
124 strbuf_addf(sb
, "=?%s?q?", encoding
);
125 for (i
= last
= 0; i
< len
; i
++) {
126 unsigned ch
= line
[i
] & 0xFF;
128 * We encode ' ' using '=20' even though rfc2047
129 * allows using '_' for readability. Unfortunately,
130 * many programs do not understand this and just
131 * leave the underscore in place.
133 if (is_rfc2047_special(ch
) || ch
== ' ') {
134 strbuf_add(sb
, line
+ last
, i
- last
);
135 strbuf_addf(sb
, "=%02X", ch
);
139 strbuf_add(sb
, line
+ last
, len
- last
);
140 strbuf_addstr(sb
, "?=");
143 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
144 const char *line
, enum date_mode dmode
,
145 const char *encoding
)
152 if (fmt
== CMIT_FMT_ONELINE
)
154 date
= strchr(line
, '>');
157 namelen
= ++date
- line
;
158 time
= strtoul(date
, &date
, 10);
159 tz
= strtol(date
, NULL
, 10);
161 if (fmt
== CMIT_FMT_EMAIL
) {
162 char *name_tail
= strchr(line
, '<');
163 int display_name_length
;
166 while (line
< name_tail
&& isspace(name_tail
[-1]))
168 display_name_length
= name_tail
- line
;
169 strbuf_addstr(sb
, "From: ");
170 add_rfc2047(sb
, line
, display_name_length
, encoding
);
171 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
172 strbuf_addch(sb
, '\n');
174 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
175 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
179 case CMIT_FMT_MEDIUM
:
180 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
183 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
185 case CMIT_FMT_FULLER
:
186 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
194 static int is_empty_line(const char *line
, int *len_p
)
197 while (len
&& isspace(line
[len
-1]))
203 static const char *skip_empty_lines(const char *msg
)
206 int linelen
= get_one_line(msg
);
210 if (!is_empty_line(msg
, &ll
))
217 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
218 const struct commit
*commit
, int abbrev
)
220 struct commit_list
*parent
= commit
->parents
;
222 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
223 !parent
|| !parent
->next
)
226 strbuf_addstr(sb
, "Merge:");
229 struct commit
*p
= parent
->item
;
230 const char *hex
= NULL
;
232 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
234 hex
= sha1_to_hex(p
->object
.sha1
);
235 parent
= parent
->next
;
237 strbuf_addf(sb
, " %s", hex
);
239 strbuf_addch(sb
, '\n');
242 static char *get_header(const struct commit
*commit
, const char *key
)
244 int key_len
= strlen(key
);
245 const char *line
= commit
->buffer
;
248 const char *eol
= strchr(line
, '\n'), *next
;
253 eol
= line
+ strlen(line
);
257 if (eol
- line
> key_len
&&
258 !strncmp(line
, key
, key_len
) &&
259 line
[key_len
] == ' ') {
260 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
266 static char *replace_encoding_header(char *buf
, const char *encoding
)
268 struct strbuf tmp
= STRBUF_INIT
;
272 /* guess if there is an encoding header before a \n\n */
273 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
274 cp
= strchr(cp
, '\n');
275 if (!cp
|| *++cp
== '\n')
279 cp
= strchr(cp
, '\n');
281 return buf
; /* should not happen but be defensive */
282 len
= cp
+ 1 - (buf
+ start
);
284 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
285 if (is_encoding_utf8(encoding
)) {
286 /* we have re-coded to UTF-8; drop the header */
287 strbuf_remove(&tmp
, start
, len
);
289 /* just replaces XXXX in 'encoding XXXX\n' */
290 strbuf_splice(&tmp
, start
+ strlen("encoding "),
291 len
- strlen("encoding \n"),
292 encoding
, strlen(encoding
));
294 return strbuf_detach(&tmp
, NULL
);
297 static char *logmsg_reencode(const struct commit
*commit
,
298 const char *output_encoding
)
300 static const char *utf8
= "UTF-8";
301 const char *use_encoding
;
305 if (!*output_encoding
)
307 encoding
= get_header(commit
, "encoding");
308 use_encoding
= encoding
? encoding
: utf8
;
309 if (!strcmp(use_encoding
, output_encoding
))
310 if (encoding
) /* we'll strip encoding header later */
311 out
= xstrdup(commit
->buffer
);
313 return NULL
; /* nothing to do */
315 out
= reencode_string(commit
->buffer
,
316 output_encoding
, use_encoding
);
318 out
= replace_encoding_header(out
, output_encoding
);
324 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
326 static struct string_list
*mail_map
;
328 mail_map
= xcalloc(1, sizeof(*mail_map
));
329 read_mailmap(mail_map
, NULL
);
331 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
334 static size_t format_person_part(struct strbuf
*sb
, char part
,
335 const char *msg
, int len
, enum date_mode dmode
)
337 /* currently all placeholders have same length */
338 const int placeholder_len
= 2;
339 int start
, end
, tz
= 0;
340 unsigned long date
= 0;
342 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
343 char person_name
[1024];
344 char person_mail
[1024];
346 /* advance 'end' to point to email start delimiter */
347 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
351 * When end points at the '<' that we found, it should have
352 * matching '>' later, which means 'end' must be strictly
358 /* Seek for both name and email part */
361 while (name_end
> name_start
&& isspace(*(name_end
-1)))
363 mail_start
= msg
+end
+1;
364 mail_end
= mail_start
;
365 while (mail_end
< msg_end
&& *mail_end
!= '>')
367 if (mail_end
== msg_end
)
371 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
372 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
373 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
374 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
375 name_start
= person_name
;
376 name_end
= name_start
+ strlen(person_name
);
377 mail_start
= person_mail
;
378 mail_end
= mail_start
+ strlen(person_mail
);
380 if (part
== 'n' || part
== 'N') { /* name */
381 strbuf_add(sb
, name_start
, name_end
-name_start
);
382 return placeholder_len
;
384 if (part
== 'e' || part
== 'E') { /* email */
385 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
386 return placeholder_len
;
389 /* advance 'start' to point to date start delimiter */
390 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
394 date
= strtoul(msg
+ start
, &ep
, 10);
395 if (msg
+ start
== ep
)
398 if (part
== 't') { /* date, UNIX timestamp */
399 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
400 return placeholder_len
;
404 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
406 if (start
+ 1 < len
) {
407 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
408 if (msg
[start
] == '-')
414 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
415 return placeholder_len
;
416 case 'D': /* date, RFC2822 style */
417 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
418 return placeholder_len
;
419 case 'r': /* date, relative */
420 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
421 return placeholder_len
;
422 case 'i': /* date, ISO 8601 */
423 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
424 return placeholder_len
;
429 * bogus commit, 'sb' cannot be updated, but we still need to
430 * compute a valid return value.
432 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
433 || part
== 'D' || part
== 'r' || part
== 'i')
434 return placeholder_len
;
436 return 0; /* unknown placeholder */
444 struct format_commit_context
{
445 const struct commit
*commit
;
446 const struct pretty_print_context
*pretty_ctx
;
447 unsigned commit_header_parsed
:1;
448 unsigned commit_message_parsed
:1;
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
;
464 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
467 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
472 * We haven't seen this chunk before. Our caller is surely
473 * going to add it the hard way now. Remember the most likely
474 * start of the to-be-added chunk: the current end of the
477 chunk
->off
= sb
->len
;
481 static void parse_commit_header(struct format_commit_context
*context
)
483 const char *msg
= context
->commit
->buffer
;
486 for (i
= 0; msg
[i
]; i
++) {
488 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
493 } else if (!prefixcmp(msg
+ i
, "author ")) {
494 context
->author
.off
= i
+ 7;
495 context
->author
.len
= eol
- i
- 7;
496 } else if (!prefixcmp(msg
+ i
, "committer ")) {
497 context
->committer
.off
= i
+ 10;
498 context
->committer
.len
= eol
- i
- 10;
499 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
500 context
->encoding
.off
= i
+ 9;
501 context
->encoding
.len
= eol
- i
- 9;
505 context
->message_off
= i
;
506 context
->commit_header_parsed
= 1;
509 static int istitlechar(char c
)
511 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
512 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
515 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
518 size_t start_len
= sb
->len
;
521 for (; *msg
&& *msg
!= '\n'; msg
++) {
522 if (istitlechar(*msg
)) {
524 strbuf_addch(sb
, '-');
526 strbuf_addch(sb
, *msg
);
528 while (*(msg
+1) == '.')
534 /* trim any trailing '.' or '-' characters */
536 while (sb
->len
- trimlen
> start_len
&&
537 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
538 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
540 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
543 const char *format_subject(struct strbuf
*sb
, const char *msg
,
544 const char *line_separator
)
549 const char *line
= msg
;
550 int linelen
= get_one_line(line
);
553 if (!linelen
|| is_empty_line(line
, &linelen
))
558 strbuf_grow(sb
, linelen
+ 2);
560 strbuf_addstr(sb
, line_separator
);
561 strbuf_add(sb
, line
, linelen
);
567 static void parse_commit_message(struct format_commit_context
*c
)
569 const char *msg
= c
->commit
->buffer
+ c
->message_off
;
570 const char *start
= c
->commit
->buffer
;
572 msg
= skip_empty_lines(msg
);
573 c
->subject_off
= msg
- start
;
575 msg
= format_subject(NULL
, msg
, NULL
);
576 msg
= skip_empty_lines(msg
);
577 c
->body_off
= msg
- start
;
579 c
->commit_message_parsed
= 1;
582 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
584 struct name_decoration
*d
;
585 const char *prefix
= " (";
587 load_ref_decorations(DECORATE_SHORT_REFS
);
588 d
= lookup_decoration(&name_decoration
, &commit
->object
);
590 strbuf_addstr(sb
, prefix
);
592 strbuf_addstr(sb
, d
->name
);
595 if (prefix
[0] == ',')
596 strbuf_addch(sb
, ')');
599 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
602 struct format_commit_context
*c
= context
;
603 const struct commit
*commit
= c
->commit
;
604 const char *msg
= commit
->buffer
;
605 struct commit_list
*p
;
608 /* these are independent of the commit */
609 switch (placeholder
[0]) {
611 if (placeholder
[1] == '(') {
612 const char *end
= strchr(placeholder
+ 2, ')');
613 char color
[COLOR_MAXLEN
];
616 color_parse_mem(placeholder
+ 2,
617 end
- (placeholder
+ 2),
618 "--pretty format", color
);
619 strbuf_addstr(sb
, color
);
620 return end
- placeholder
+ 1;
622 if (!prefixcmp(placeholder
+ 1, "red")) {
623 strbuf_addstr(sb
, GIT_COLOR_RED
);
625 } else if (!prefixcmp(placeholder
+ 1, "green")) {
626 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
628 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
629 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
631 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
632 strbuf_addstr(sb
, GIT_COLOR_RESET
);
636 case 'n': /* newline */
637 strbuf_addch(sb
, '\n');
640 /* %x00 == NUL, %x0a == LF, etc. */
641 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
643 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
645 strbuf_addch(sb
, (h1
<<4)|h2
);
651 /* these depend on the commit */
652 if (!commit
->object
.parsed
)
653 parse_object(commit
->object
.sha1
);
655 switch (placeholder
[0]) {
656 case 'H': /* commit hash */
657 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
659 case 'h': /* abbreviated commit hash */
660 if (add_again(sb
, &c
->abbrev_commit_hash
))
662 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
664 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
666 case 'T': /* tree hash */
667 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
669 case 't': /* abbreviated tree hash */
670 if (add_again(sb
, &c
->abbrev_tree_hash
))
672 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
674 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
676 case 'P': /* parent hashes */
677 for (p
= commit
->parents
; p
; p
= p
->next
) {
678 if (p
!= commit
->parents
)
679 strbuf_addch(sb
, ' ');
680 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
683 case 'p': /* abbreviated parent hashes */
684 if (add_again(sb
, &c
->abbrev_parent_hashes
))
686 for (p
= commit
->parents
; p
; p
= p
->next
) {
687 if (p
!= commit
->parents
)
688 strbuf_addch(sb
, ' ');
689 strbuf_addstr(sb
, find_unique_abbrev(
690 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
692 c
->abbrev_parent_hashes
.len
= sb
->len
-
693 c
->abbrev_parent_hashes
.off
;
695 case 'm': /* left/right/bottom */
696 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
698 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
703 format_decoration(sb
, commit
);
705 case 'g': /* reflog info */
706 switch(placeholder
[1]) {
707 case 'd': /* reflog selector */
709 if (c
->pretty_ctx
->reflog_info
)
710 get_reflog_selector(sb
,
711 c
->pretty_ctx
->reflog_info
,
712 c
->pretty_ctx
->date_mode
,
713 (placeholder
[1] == 'd'));
715 case 's': /* reflog message */
716 if (c
->pretty_ctx
->reflog_info
)
717 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
720 return 0; /* unknown %g placeholder */
723 /* For the rest we have to parse the commit header. */
724 if (!c
->commit_header_parsed
)
725 parse_commit_header(c
);
727 switch (placeholder
[0]) {
728 case 'a': /* author ... */
729 return format_person_part(sb
, placeholder
[1],
730 msg
+ c
->author
.off
, c
->author
.len
,
731 c
->pretty_ctx
->date_mode
);
732 case 'c': /* committer ... */
733 return format_person_part(sb
, placeholder
[1],
734 msg
+ c
->committer
.off
, c
->committer
.len
,
735 c
->pretty_ctx
->date_mode
);
736 case 'e': /* encoding */
737 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
741 /* Now we need to parse the commit message. */
742 if (!c
->commit_message_parsed
)
743 parse_commit_message(c
);
745 switch (placeholder
[0]) {
746 case 's': /* subject */
747 format_subject(sb
, msg
+ c
->subject_off
, " ");
749 case 'f': /* sanitized subject */
750 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
753 strbuf_addstr(sb
, msg
+ c
->body_off
);
756 return 0; /* unknown placeholder */
759 void format_commit_message(const struct commit
*commit
,
760 const char *format
, struct strbuf
*sb
,
761 const struct pretty_print_context
*pretty_ctx
)
763 struct format_commit_context context
;
765 memset(&context
, 0, sizeof(context
));
766 context
.commit
= commit
;
767 context
.pretty_ctx
= pretty_ctx
;
768 strbuf_expand(sb
, format
, format_commit_item
, &context
);
771 static void pp_header(enum cmit_fmt fmt
,
773 enum date_mode dmode
,
774 const char *encoding
,
775 const struct commit
*commit
,
779 int parents_shown
= 0;
782 const char *line
= *msg_p
;
783 int linelen
= get_one_line(*msg_p
);
793 if (fmt
== CMIT_FMT_RAW
) {
794 strbuf_add(sb
, line
, linelen
);
798 if (!memcmp(line
, "parent ", 7)) {
800 die("bad parent line in commit");
804 if (!parents_shown
) {
805 struct commit_list
*parent
;
807 for (parent
= commit
->parents
, num
= 0;
809 parent
= parent
->next
, num
++)
811 /* with enough slop */
812 strbuf_grow(sb
, num
* 50 + 20);
813 add_merge_info(fmt
, sb
, commit
, abbrev
);
818 * MEDIUM == DEFAULT shows only author with dates.
819 * FULL shows both authors but not dates.
820 * FULLER shows both authors and dates.
822 if (!memcmp(line
, "author ", 7)) {
823 strbuf_grow(sb
, linelen
+ 80);
824 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
826 if (!memcmp(line
, "committer ", 10) &&
827 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
828 strbuf_grow(sb
, linelen
+ 80);
829 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
834 void pp_title_line(enum cmit_fmt fmt
,
838 const char *after_subject
,
839 const char *encoding
,
842 const char *line_separator
= (fmt
== CMIT_FMT_EMAIL
) ? "\n " : " ";
845 strbuf_init(&title
, 80);
846 *msg_p
= format_subject(&title
, *msg_p
, line_separator
);
848 strbuf_grow(sb
, title
.len
+ 1024);
850 strbuf_addstr(sb
, subject
);
851 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
853 strbuf_addbuf(sb
, &title
);
855 strbuf_addch(sb
, '\n');
857 if (need_8bit_cte
> 0) {
858 const char *header_fmt
=
859 "MIME-Version: 1.0\n"
860 "Content-Type: text/plain; charset=%s\n"
861 "Content-Transfer-Encoding: 8bit\n";
862 strbuf_addf(sb
, header_fmt
, encoding
);
865 strbuf_addstr(sb
, after_subject
);
867 if (fmt
== CMIT_FMT_EMAIL
) {
868 strbuf_addch(sb
, '\n');
870 strbuf_release(&title
);
873 void pp_remainder(enum cmit_fmt fmt
,
880 const char *line
= *msg_p
;
881 int linelen
= get_one_line(line
);
887 if (is_empty_line(line
, &linelen
)) {
890 if (fmt
== CMIT_FMT_SHORT
)
895 strbuf_grow(sb
, linelen
+ indent
+ 20);
897 memset(sb
->buf
+ sb
->len
, ' ', indent
);
898 strbuf_setlen(sb
, sb
->len
+ indent
);
900 strbuf_add(sb
, line
, linelen
);
901 strbuf_addch(sb
, '\n');
905 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
907 const char *encoding
;
909 encoding
= (git_log_output_encoding
910 ? git_log_output_encoding
911 : git_commit_encoding
);
915 *encoding_p
= encoding
;
916 return logmsg_reencode(commit
, encoding
);
919 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
921 const struct pretty_print_context
*context
)
923 unsigned long beginning_of_body
;
925 const char *msg
= commit
->buffer
;
927 const char *encoding
;
928 int need_8bit_cte
= context
->need_8bit_cte
;
930 if (fmt
== CMIT_FMT_USERFORMAT
) {
931 format_commit_message(commit
, user_format
, sb
, context
);
935 reencoded
= reencode_commit_message(commit
, &encoding
);
940 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
944 * We need to check and emit Content-type: to mark it
945 * as 8-bit if we haven't done so.
947 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
950 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
952 /* author could be non 7-bit ASCII but
953 * the log may be so; skip over the
956 if (ch
== '\n' && msg
[i
+1] == '\n')
959 else if (non_ascii(ch
)) {
966 pp_header(fmt
, context
->abbrev
, context
->date_mode
, encoding
,
968 if (fmt
!= CMIT_FMT_ONELINE
&& !context
->subject
) {
969 strbuf_addch(sb
, '\n');
972 /* Skip excess blank lines at the beginning of body, if any... */
973 msg
= skip_empty_lines(msg
);
975 /* These formats treat the title line specially. */
976 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
977 pp_title_line(fmt
, &msg
, sb
, context
->subject
,
978 context
->after_subject
, encoding
, need_8bit_cte
);
980 beginning_of_body
= sb
->len
;
981 if (fmt
!= CMIT_FMT_ONELINE
)
982 pp_remainder(fmt
, &msg
, sb
, indent
);
985 /* Make sure there is an EOLN for the non-oneline case */
986 if (fmt
!= CMIT_FMT_ONELINE
)
987 strbuf_addch(sb
, '\n');
990 * The caller may append additional body text in e-mail
991 * format. Make sure we did not strip the blank line
992 * between the header and the body.
994 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
995 strbuf_addch(sb
, '\n');