1#include "cache.h"
2#include "commit.h"
3#include "utf8.h"
4#include "diff.h"
5#include "revision.h"
6
7static struct cmt_fmt_map {
8 const char *n;
9 size_t cmp_len;
10 enum cmit_fmt v;
11} cmt_fmts[] = {
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},
20};
21
22static char *user_format;
23
24enum cmit_fmt get_commit_format(const char *arg)
25{
26 int i;
27
28 if (!arg || !*arg)
29 return CMIT_FMT_DEFAULT;
30 if (*arg == '=')
31 arg++;
32 if (!prefixcmp(arg, "format:")) {
33 if (user_format)
34 free(user_format);
35 user_format = xstrdup(arg + 7);
36 return CMIT_FMT_USERFORMAT;
37 }
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)))
41 return cmt_fmts[i].v;
42 }
43
44 die("invalid --pretty format: %s", arg);
45}
46
47/*
48 * Generic support for pretty-printing the header
49 */
50static int get_one_line(const char *msg)
51{
52 int ret = 0;
53
54 for (;;) {
55 char c = *msg++;
56 if (!c)
57 break;
58 ret++;
59 if (c == '\n')
60 break;
61 }
62 return ret;
63}
64
65/* High bit set, or ISO-2022-INT */
66int non_ascii(int ch)
67{
68 ch = (ch & 0xff);
69 return ((ch & 0x80) || (ch == 0x1b));
70}
71
72static int is_rfc2047_special(char ch)
73{
74 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
75}
76
77static void add_rfc2047(struct strbuf *sb, const char *line, int len,
78 const char *encoding)
79{
80 int i, last;
81
82 for (i = 0; i < len; i++) {
83 int ch = line[i];
84 if (non_ascii(ch))
85 goto needquote;
86 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
87 goto needquote;
88 }
89 strbuf_add(sb, line, len);
90 return;
91
92needquote:
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;
97 /*
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.
102 */
103 if (is_rfc2047_special(ch) || ch == ' ') {
104 strbuf_add(sb, line + last, i - last);
105 strbuf_addf(sb, "=%02X", ch);
106 last = i + 1;
107 }
108 }
109 strbuf_add(sb, line + last, len - last);
110 strbuf_addstr(sb, "?=");
111}
112
113static 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)
116{
117 char *date;
118 int namelen;
119 unsigned long time;
120 int tz;
121 const char *filler = " ";
122
123 if (fmt == CMIT_FMT_ONELINE)
124 return;
125 date = strchr(line, '>');
126 if (!date)
127 return;
128 namelen = ++date - line;
129 time = strtoul(date, &date, 10);
130 tz = strtol(date, NULL, 10);
131
132 if (fmt == CMIT_FMT_EMAIL) {
133 char *name_tail = strchr(line, '<');
134 int display_name_length;
135 if (!name_tail)
136 return;
137 while (line < name_tail && isspace(name_tail[-1]))
138 name_tail--;
139 display_name_length = name_tail - line;
140 filler = "";
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');
145 } else {
146 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
147 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
148 filler, namelen, line);
149 }
150 switch (fmt) {
151 case CMIT_FMT_MEDIUM:
152 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
153 break;
154 case CMIT_FMT_EMAIL:
155 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
156 break;
157 case CMIT_FMT_FULLER:
158 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
159 break;
160 default:
161 /* notin' */
162 break;
163 }
164}
165
166static int is_empty_line(const char *line, int *len_p)
167{
168 int len = *len_p;
169 while (len && isspace(line[len-1]))
170 len--;
171 *len_p = len;
172 return !len;
173}
174
175static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
176 const struct commit *commit, int abbrev)
177{
178 struct commit_list *parent = commit->parents;
179
180 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
181 !parent || !parent->next)
182 return;
183
184 strbuf_addstr(sb, "Merge:");
185
186 while (parent) {
187 struct commit *p = parent->item;
188 const char *hex = NULL;
189 const char *dots;
190 if (abbrev)
191 hex = find_unique_abbrev(p->object.sha1, abbrev);
192 if (!hex)
193 hex = sha1_to_hex(p->object.sha1);
194 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
195 parent = parent->next;
196
197 strbuf_addf(sb, " %s%s", hex, dots);
198 }
199 strbuf_addch(sb, '\n');
200}
201
202static char *get_header(const struct commit *commit, const char *key)
203{
204 int key_len = strlen(key);
205 const char *line = commit->buffer;
206
207 for (;;) {
208 const char *eol = strchr(line, '\n'), *next;
209
210 if (line == eol)
211 return NULL;
212 if (!eol) {
213 eol = line + strlen(line);
214 next = NULL;
215 } else
216 next = eol + 1;
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);
221 }
222 line = next;
223 }
224}
225
226static char *replace_encoding_header(char *buf, const char *encoding)
227{
228 struct strbuf tmp;
229 size_t start, len;
230 char *cp = buf;
231
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')
236 return buf;
237 }
238 start = cp - buf;
239 cp = strchr(cp, '\n');
240 if (!cp)
241 return buf; /* should not happen but be defensive */
242 len = cp + 1 - (buf + start);
243
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);
249 } else {
250 /* just replaces XXXX in 'encoding XXXX\n' */
251 strbuf_splice(&tmp, start + strlen("encoding "),
252 len - strlen("encoding \n"),
253 encoding, strlen(encoding));
254 }
255 return strbuf_detach(&tmp, NULL);
256}
257
258static char *logmsg_reencode(const struct commit *commit,
259 const char *output_encoding)
260{
261 static const char *utf8 = "utf-8";
262 const char *use_encoding;
263 char *encoding;
264 char *out;
265
266 if (!*output_encoding)
267 return NULL;
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);
273 else
274 return NULL; /* nothing to do */
275 else
276 out = reencode_string(commit->buffer,
277 output_encoding, use_encoding);
278 if (out)
279 out = replace_encoding_header(out, output_encoding);
280
281 free(encoding);
282 return out;
283}
284
285static void format_person_part(struct strbuf *sb, char part,
286 const char *msg, int len)
287{
288 int start, end, tz = 0;
289 unsigned long date;
290 char *ep;
291
292 /* parse name */
293 for (end = 0; end < len && msg[end] != '<'; end++)
294 ; /* do nothing */
295 /*
296 * If it does not even have a '<' and '>', that is
297 * quite a bogus commit author and we discard it;
298 * this is in line with add_user_info() that is used
299 * in the normal codepath. When end points at the '<'
300 * that we found, it should have matching '>' later,
301 * which means start (beginning of email address) must
302 * be strictly below len.
303 */
304 start = end + 1;
305 if (start >= len - 1)
306 return;
307 while (end > 0 && isspace(msg[end - 1]))
308 end--;
309 if (part == 'n') { /* name */
310 strbuf_add(sb, msg, end);
311 return;
312 }
313
314 /* parse email */
315 for (end = start; end < len && msg[end] != '>'; end++)
316 ; /* do nothing */
317
318 if (end >= len)
319 return;
320
321 if (part == 'e') { /* email */
322 strbuf_add(sb, msg + start, end - start);
323 return;
324 }
325
326 /* parse date */
327 for (start = end + 1; start < len && isspace(msg[start]); start++)
328 ; /* do nothing */
329 if (start >= len)
330 return;
331 date = strtoul(msg + start, &ep, 10);
332 if (msg + start == ep)
333 return;
334
335 if (part == 't') { /* date, UNIX timestamp */
336 strbuf_add(sb, msg + start, ep - (msg + start));
337 return;
338 }
339
340 /* parse tz */
341 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
342 ; /* do nothing */
343 if (start + 1 < len) {
344 tz = strtoul(msg + start + 1, NULL, 10);
345 if (msg[start] == '-')
346 tz = -tz;
347 }
348
349 switch (part) {
350 case 'd': /* date */
351 strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
352 return;
353 case 'D': /* date, RFC2822 style */
354 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
355 return;
356 case 'r': /* date, relative */
357 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
358 return;
359 case 'i': /* date, ISO 8601 */
360 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
361 return;
362 }
363}
364
365struct chunk {
366 size_t off;
367 size_t len;
368};
369
370struct format_commit_context {
371 const struct commit *commit;
372
373 /* These offsets are relative to the start of the commit message. */
374 int commit_header_parsed;
375 struct chunk subject;
376 struct chunk author;
377 struct chunk committer;
378 struct chunk encoding;
379 size_t body_off;
380
381 /* The following ones are relative to the result struct strbuf. */
382 struct chunk abbrev_commit_hash;
383 struct chunk abbrev_tree_hash;
384 struct chunk abbrev_parent_hashes;
385};
386
387static int add_again(struct strbuf *sb, struct chunk *chunk)
388{
389 if (chunk->len) {
390 strbuf_adddup(sb, chunk->off, chunk->len);
391 return 1;
392 }
393
394 /*
395 * We haven't seen this chunk before. Our caller is surely
396 * going to add it the hard way now. Remember the most likely
397 * start of the to-be-added chunk: the current end of the
398 * struct strbuf.
399 */
400 chunk->off = sb->len;
401 return 0;
402}
403
404static void parse_commit_header(struct format_commit_context *context)
405{
406 const char *msg = context->commit->buffer;
407 int i;
408 enum { HEADER, SUBJECT, BODY } state;
409
410 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
411 int eol;
412 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
413 ; /* do nothing */
414
415 if (state == SUBJECT) {
416 context->subject.off = i;
417 context->subject.len = eol - i;
418 i = eol;
419 }
420 if (i == eol) {
421 state++;
422 /* strip empty lines */
423 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
424 eol++;
425 } else if (!prefixcmp(msg + i, "author ")) {
426 context->author.off = i + 7;
427 context->author.len = eol - i - 7;
428 } else if (!prefixcmp(msg + i, "committer ")) {
429 context->committer.off = i + 10;
430 context->committer.len = eol - i - 10;
431 } else if (!prefixcmp(msg + i, "encoding ")) {
432 context->encoding.off = i + 9;
433 context->encoding.len = eol - i - 9;
434 }
435 i = eol;
436 if (!msg[i])
437 break;
438 }
439 context->body_off = i;
440 context->commit_header_parsed = 1;
441}
442
443static void format_commit_item(struct strbuf *sb, const char *placeholder,
444 void *context)
445{
446 struct format_commit_context *c = context;
447 const struct commit *commit = c->commit;
448 const char *msg = commit->buffer;
449 struct commit_list *p;
450
451 /* these are independent of the commit */
452 switch (placeholder[0]) {
453 case 'C':
454 switch (placeholder[3]) {
455 case 'd': /* red */
456 strbuf_addstr(sb, "\033[31m");
457 return;
458 case 'e': /* green */
459 strbuf_addstr(sb, "\033[32m");
460 return;
461 case 'u': /* blue */
462 strbuf_addstr(sb, "\033[34m");
463 return;
464 case 's': /* reset color */
465 strbuf_addstr(sb, "\033[m");
466 return;
467 }
468 case 'n': /* newline */
469 strbuf_addch(sb, '\n');
470 return;
471 }
472
473 /* these depend on the commit */
474 if (!commit->object.parsed)
475 parse_object(commit->object.sha1);
476
477 switch (placeholder[0]) {
478 case 'H': /* commit hash */
479 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
480 return;
481 case 'h': /* abbreviated commit hash */
482 if (add_again(sb, &c->abbrev_commit_hash))
483 return;
484 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
485 DEFAULT_ABBREV));
486 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
487 return;
488 case 'T': /* tree hash */
489 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
490 return;
491 case 't': /* abbreviated tree hash */
492 if (add_again(sb, &c->abbrev_tree_hash))
493 return;
494 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
495 DEFAULT_ABBREV));
496 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
497 return;
498 case 'P': /* parent hashes */
499 for (p = commit->parents; p; p = p->next) {
500 if (p != commit->parents)
501 strbuf_addch(sb, ' ');
502 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
503 }
504 return;
505 case 'p': /* abbreviated parent hashes */
506 if (add_again(sb, &c->abbrev_parent_hashes))
507 return;
508 for (p = commit->parents; p; p = p->next) {
509 if (p != commit->parents)
510 strbuf_addch(sb, ' ');
511 strbuf_addstr(sb, find_unique_abbrev(
512 p->item->object.sha1, DEFAULT_ABBREV));
513 }
514 c->abbrev_parent_hashes.len = sb->len -
515 c->abbrev_parent_hashes.off;
516 return;
517 case 'm': /* left/right/bottom */
518 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
519 ? '-'
520 : (commit->object.flags & SYMMETRIC_LEFT)
521 ? '<'
522 : '>');
523 return;
524 }
525
526 /* For the rest we have to parse the commit header. */
527 if (!c->commit_header_parsed)
528 parse_commit_header(c);
529
530 switch (placeholder[0]) {
531 case 's':
532 strbuf_add(sb, msg + c->subject.off, c->subject.len);
533 return;
534 case 'a':
535 format_person_part(sb, placeholder[1],
536 msg + c->author.off, c->author.len);
537 return;
538 case 'c':
539 format_person_part(sb, placeholder[1],
540 msg + c->committer.off, c->committer.len);
541 return;
542 case 'e':
543 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
544 return;
545 case 'b':
546 strbuf_addstr(sb, msg + c->body_off);
547 return;
548 }
549}
550
551void format_commit_message(const struct commit *commit,
552 const void *format, struct strbuf *sb)
553{
554 const char *placeholders[] = {
555 "H", /* commit hash */
556 "h", /* abbreviated commit hash */
557 "T", /* tree hash */
558 "t", /* abbreviated tree hash */
559 "P", /* parent hashes */
560 "p", /* abbreviated parent hashes */
561 "an", /* author name */
562 "ae", /* author email */
563 "ad", /* author date */
564 "aD", /* author date, RFC2822 style */
565 "ar", /* author date, relative */
566 "at", /* author date, UNIX timestamp */
567 "ai", /* author date, ISO 8601 */
568 "cn", /* committer name */
569 "ce", /* committer email */
570 "cd", /* committer date */
571 "cD", /* committer date, RFC2822 style */
572 "cr", /* committer date, relative */
573 "ct", /* committer date, UNIX timestamp */
574 "ci", /* committer date, ISO 8601 */
575 "e", /* encoding */
576 "s", /* subject */
577 "b", /* body */
578 "Cred", /* red */
579 "Cgreen", /* green */
580 "Cblue", /* blue */
581 "Creset", /* reset color */
582 "n", /* newline */
583 "m", /* left/right/bottom */
584 NULL
585 };
586 struct format_commit_context context;
587
588 memset(&context, 0, sizeof(context));
589 context.commit = commit;
590 strbuf_expand(sb, format, placeholders, format_commit_item, &context);
591}
592
593static void pp_header(enum cmit_fmt fmt,
594 int abbrev,
595 enum date_mode dmode,
596 const char *encoding,
597 const struct commit *commit,
598 const char **msg_p,
599 struct strbuf *sb)
600{
601 int parents_shown = 0;
602
603 for (;;) {
604 const char *line = *msg_p;
605 int linelen = get_one_line(*msg_p);
606
607 if (!linelen)
608 return;
609 *msg_p += linelen;
610
611 if (linelen == 1)
612 /* End of header */
613 return;
614
615 if (fmt == CMIT_FMT_RAW) {
616 strbuf_add(sb, line, linelen);
617 continue;
618 }
619
620 if (!memcmp(line, "parent ", 7)) {
621 if (linelen != 48)
622 die("bad parent line in commit");
623 continue;
624 }
625
626 if (!parents_shown) {
627 struct commit_list *parent;
628 int num;
629 for (parent = commit->parents, num = 0;
630 parent;
631 parent = parent->next, num++)
632 ;
633 /* with enough slop */
634 strbuf_grow(sb, num * 50 + 20);
635 add_merge_info(fmt, sb, commit, abbrev);
636 parents_shown = 1;
637 }
638
639 /*
640 * MEDIUM == DEFAULT shows only author with dates.
641 * FULL shows both authors but not dates.
642 * FULLER shows both authors and dates.
643 */
644 if (!memcmp(line, "author ", 7)) {
645 strbuf_grow(sb, linelen + 80);
646 add_user_info("Author", fmt, sb, line + 7, dmode, encoding);
647 }
648 if (!memcmp(line, "committer ", 10) &&
649 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
650 strbuf_grow(sb, linelen + 80);
651 add_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
652 }
653 }
654}
655
656static void pp_title_line(enum cmit_fmt fmt,
657 const char **msg_p,
658 struct strbuf *sb,
659 const char *subject,
660 const char *after_subject,
661 const char *encoding,
662 int need_8bit_cte)
663{
664 struct strbuf title;
665
666 strbuf_init(&title, 80);
667
668 for (;;) {
669 const char *line = *msg_p;
670 int linelen = get_one_line(line);
671
672 *msg_p += linelen;
673 if (!linelen || is_empty_line(line, &linelen))
674 break;
675
676 strbuf_grow(&title, linelen + 2);
677 if (title.len) {
678 if (fmt == CMIT_FMT_EMAIL) {
679 strbuf_addch(&title, '\n');
680 }
681 strbuf_addch(&title, ' ');
682 }
683 strbuf_add(&title, line, linelen);
684 }
685
686 strbuf_grow(sb, title.len + 1024);
687 if (subject) {
688 strbuf_addstr(sb, subject);
689 add_rfc2047(sb, title.buf, title.len, encoding);
690 } else {
691 strbuf_addbuf(sb, &title);
692 }
693 strbuf_addch(sb, '\n');
694
695 if (need_8bit_cte > 0) {
696 const char *header_fmt =
697 "MIME-Version: 1.0\n"
698 "Content-Type: text/plain; charset=%s\n"
699 "Content-Transfer-Encoding: 8bit\n";
700 strbuf_addf(sb, header_fmt, encoding);
701 }
702 if (after_subject) {
703 strbuf_addstr(sb, after_subject);
704 }
705 if (fmt == CMIT_FMT_EMAIL) {
706 strbuf_addch(sb, '\n');
707 }
708 strbuf_release(&title);
709}
710
711static void pp_remainder(enum cmit_fmt fmt,
712 const char **msg_p,
713 struct strbuf *sb,
714 int indent)
715{
716 int first = 1;
717 for (;;) {
718 const char *line = *msg_p;
719 int linelen = get_one_line(line);
720 *msg_p += linelen;
721
722 if (!linelen)
723 break;
724
725 if (is_empty_line(line, &linelen)) {
726 if (first)
727 continue;
728 if (fmt == CMIT_FMT_SHORT)
729 break;
730 }
731 first = 0;
732
733 strbuf_grow(sb, linelen + indent + 20);
734 if (indent) {
735 memset(sb->buf + sb->len, ' ', indent);
736 strbuf_setlen(sb, sb->len + indent);
737 }
738 strbuf_add(sb, line, linelen);
739 strbuf_addch(sb, '\n');
740 }
741}
742
743void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
744 struct strbuf *sb, int abbrev,
745 const char *subject, const char *after_subject,
746 enum date_mode dmode, int need_8bit_cte)
747{
748 unsigned long beginning_of_body;
749 int indent = 4;
750 const char *msg = commit->buffer;
751 char *reencoded;
752 const char *encoding;
753
754 if (fmt == CMIT_FMT_USERFORMAT) {
755 format_commit_message(commit, user_format, sb);
756 return;
757 }
758
759 encoding = (git_log_output_encoding
760 ? git_log_output_encoding
761 : git_commit_encoding);
762 if (!encoding)
763 encoding = "utf-8";
764 reencoded = logmsg_reencode(commit, encoding);
765 if (reencoded) {
766 msg = reencoded;
767 }
768
769 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
770 indent = 0;
771
772 /*
773 * We need to check and emit Content-type: to mark it
774 * as 8-bit if we haven't done so.
775 */
776 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
777 int i, ch, in_body;
778
779 for (in_body = i = 0; (ch = msg[i]); i++) {
780 if (!in_body) {
781 /* author could be non 7-bit ASCII but
782 * the log may be so; skip over the
783 * header part first.
784 */
785 if (ch == '\n' && msg[i+1] == '\n')
786 in_body = 1;
787 }
788 else if (non_ascii(ch)) {
789 need_8bit_cte = 1;
790 break;
791 }
792 }
793 }
794
795 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
796 if (fmt != CMIT_FMT_ONELINE && !subject) {
797 strbuf_addch(sb, '\n');
798 }
799
800 /* Skip excess blank lines at the beginning of body, if any... */
801 for (;;) {
802 int linelen = get_one_line(msg);
803 int ll = linelen;
804 if (!linelen)
805 break;
806 if (!is_empty_line(msg, &ll))
807 break;
808 msg += linelen;
809 }
810
811 /* These formats treat the title line specially. */
812 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
813 pp_title_line(fmt, &msg, sb, subject,
814 after_subject, encoding, need_8bit_cte);
815
816 beginning_of_body = sb->len;
817 if (fmt != CMIT_FMT_ONELINE)
818 pp_remainder(fmt, &msg, sb, indent);
819 strbuf_rtrim(sb);
820
821 /* Make sure there is an EOLN for the non-oneline case */
822 if (fmt != CMIT_FMT_ONELINE)
823 strbuf_addch(sb, '\n');
824
825 /*
826 * The caller may append additional body text in e-mail
827 * format. Make sure we did not strip the blank line
828 * between the header and the body.
829 */
830 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
831 strbuf_addch(sb, '\n');
832 free(reencoded);
833}