Merge branch 'dp/cywginstat'
[git/spearce.git] / pretty.c
blob8beafa08d3927e943a94279f5d776d6a45c673c5
1 #include "cache.h"
2 #include "commit.h"
3 #include "utf8.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "string-list.h"
7 #include "mailmap.h"
8 #include "log-tree.h"
10 static char *user_format;
12 void get_commit_format(const char *arg, struct rev_info *rev)
14 int i;
15 static struct cmt_fmt_map {
16 const char *n;
17 size_t cmp_len;
18 enum cmit_fmt v;
19 } cmt_fmts[] = {
20 { "raw", 1, CMIT_FMT_RAW },
21 { "medium", 1, CMIT_FMT_MEDIUM },
22 { "short", 1, CMIT_FMT_SHORT },
23 { "email", 1, CMIT_FMT_EMAIL },
24 { "full", 5, CMIT_FMT_FULL },
25 { "fuller", 5, CMIT_FMT_FULLER },
26 { "oneline", 1, CMIT_FMT_ONELINE },
29 rev->use_terminator = 0;
30 if (!arg || !*arg) {
31 rev->commit_format = CMIT_FMT_DEFAULT;
32 return;
34 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
35 const char *cp = strchr(arg, ':') + 1;
36 free(user_format);
37 user_format = xstrdup(cp);
38 if (arg[0] == 't')
39 rev->use_terminator = 1;
40 rev->commit_format = CMIT_FMT_USERFORMAT;
41 return;
43 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
44 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
45 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
46 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
47 rev->use_terminator = 1;
48 rev->commit_format = cmt_fmts[i].v;
49 return;
53 die("invalid --pretty format: %s", arg);
57 * Generic support for pretty-printing the header
59 static int get_one_line(const char *msg)
61 int ret = 0;
63 for (;;) {
64 char c = *msg++;
65 if (!c)
66 break;
67 ret++;
68 if (c == '\n')
69 break;
71 return ret;
74 /* High bit set, or ISO-2022-INT */
75 int non_ascii(int ch)
77 ch = (ch & 0xff);
78 return ((ch & 0x80) || (ch == 0x1b));
81 static int is_rfc2047_special(char ch)
83 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
86 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
87 const char *encoding)
89 int i, last;
91 for (i = 0; i < len; i++) {
92 int ch = line[i];
93 if (non_ascii(ch))
94 goto needquote;
95 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
96 goto needquote;
98 strbuf_add(sb, line, len);
99 return;
101 needquote:
102 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
103 strbuf_addf(sb, "=?%s?q?", encoding);
104 for (i = last = 0; i < len; i++) {
105 unsigned ch = line[i] & 0xFF;
107 * We encode ' ' using '=20' even though rfc2047
108 * allows using '_' for readability. Unfortunately,
109 * many programs do not understand this and just
110 * leave the underscore in place.
112 if (is_rfc2047_special(ch) || ch == ' ') {
113 strbuf_add(sb, line + last, i - last);
114 strbuf_addf(sb, "=%02X", ch);
115 last = i + 1;
118 strbuf_add(sb, line + last, len - last);
119 strbuf_addstr(sb, "?=");
122 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
123 const char *line, enum date_mode dmode,
124 const char *encoding)
126 char *date;
127 int namelen;
128 unsigned long time;
129 int tz;
130 const char *filler = " ";
132 if (fmt == CMIT_FMT_ONELINE)
133 return;
134 date = strchr(line, '>');
135 if (!date)
136 return;
137 namelen = ++date - line;
138 time = strtoul(date, &date, 10);
139 tz = strtol(date, NULL, 10);
141 if (fmt == CMIT_FMT_EMAIL) {
142 char *name_tail = strchr(line, '<');
143 int display_name_length;
144 if (!name_tail)
145 return;
146 while (line < name_tail && isspace(name_tail[-1]))
147 name_tail--;
148 display_name_length = name_tail - line;
149 filler = "";
150 strbuf_addstr(sb, "From: ");
151 add_rfc2047(sb, line, display_name_length, encoding);
152 strbuf_add(sb, name_tail, namelen - display_name_length);
153 strbuf_addch(sb, '\n');
154 } else {
155 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
156 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
157 filler, namelen, line);
159 switch (fmt) {
160 case CMIT_FMT_MEDIUM:
161 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
162 break;
163 case CMIT_FMT_EMAIL:
164 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
165 break;
166 case CMIT_FMT_FULLER:
167 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
168 break;
169 default:
170 /* notin' */
171 break;
175 static int is_empty_line(const char *line, int *len_p)
177 int len = *len_p;
178 while (len && isspace(line[len-1]))
179 len--;
180 *len_p = len;
181 return !len;
184 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
185 const struct commit *commit, int abbrev)
187 struct commit_list *parent = commit->parents;
189 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
190 !parent || !parent->next)
191 return;
193 strbuf_addstr(sb, "Merge:");
195 while (parent) {
196 struct commit *p = parent->item;
197 const char *hex = NULL;
198 const char *dots;
199 if (abbrev)
200 hex = find_unique_abbrev(p->object.sha1, abbrev);
201 if (!hex)
202 hex = sha1_to_hex(p->object.sha1);
203 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
204 parent = parent->next;
206 strbuf_addf(sb, " %s%s", hex, dots);
208 strbuf_addch(sb, '\n');
211 static char *get_header(const struct commit *commit, const char *key)
213 int key_len = strlen(key);
214 const char *line = commit->buffer;
216 for (;;) {
217 const char *eol = strchr(line, '\n'), *next;
219 if (line == eol)
220 return NULL;
221 if (!eol) {
222 eol = line + strlen(line);
223 next = NULL;
224 } else
225 next = eol + 1;
226 if (eol - line > key_len &&
227 !strncmp(line, key, key_len) &&
228 line[key_len] == ' ') {
229 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
231 line = next;
235 static char *replace_encoding_header(char *buf, const char *encoding)
237 struct strbuf tmp;
238 size_t start, len;
239 char *cp = buf;
241 /* guess if there is an encoding header before a \n\n */
242 while (strncmp(cp, "encoding ", strlen("encoding "))) {
243 cp = strchr(cp, '\n');
244 if (!cp || *++cp == '\n')
245 return buf;
247 start = cp - buf;
248 cp = strchr(cp, '\n');
249 if (!cp)
250 return buf; /* should not happen but be defensive */
251 len = cp + 1 - (buf + start);
253 strbuf_init(&tmp, 0);
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);
258 } else {
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;
272 char *encoding;
273 char *out;
275 if (!*output_encoding)
276 return NULL;
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);
282 else
283 return NULL; /* nothing to do */
284 else
285 out = reencode_string(commit->buffer,
286 output_encoding, use_encoding);
287 if (out)
288 out = replace_encoding_header(out, output_encoding);
290 free(encoding);
291 return out;
294 static int mailmap_name(struct strbuf *sb, const char *email)
296 static struct string_list *mail_map;
297 char buffer[1024];
299 if (!mail_map) {
300 mail_map = xcalloc(1, sizeof(*mail_map));
301 read_mailmap(mail_map, ".mailmap", NULL);
304 if (!mail_map->nr)
305 return -1;
307 if (!map_email(mail_map, email, buffer, sizeof(buffer)))
308 return -1;
309 strbuf_addstr(sb, buffer);
310 return 0;
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;
320 char *ep;
322 /* advance 'end' to point to email start delimiter */
323 for (end = 0; end < len && msg[end] != '<'; end++)
324 ; /* do nothing */
327 * When end points at the '<' that we found, it should have
328 * matching '>' later, which means 'end' must be strictly
329 * below len - 1.
331 if (end >= len - 2)
332 goto skip;
334 if (part == 'n' || part == 'N') { /* name */
335 while (end > 0 && isspace(msg[end - 1]))
336 end--;
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++)
346 ; /* do nothing */
348 if (end >= len)
349 goto skip;
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++)
358 ; /* do nothing */
359 if (start >= len)
360 goto skip;
361 date = strtoul(msg + start, &ep, 10);
362 if (msg + start == ep)
363 goto skip;
365 if (part == 't') { /* date, UNIX timestamp */
366 strbuf_add(sb, msg + start, ep - (msg + start));
367 return placeholder_len;
370 /* parse tz */
371 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
372 ; /* do nothing */
373 if (start + 1 < len) {
374 tz = strtoul(msg + start + 1, NULL, 10);
375 if (msg[start] == '-')
376 tz = -tz;
379 switch (part) {
380 case 'd': /* date */
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;
394 skip:
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 */
406 struct chunk {
407 size_t off;
408 size_t len;
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;
418 struct chunk author;
419 struct chunk committer;
420 struct chunk encoding;
421 size_t body_off;
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)
431 if (chunk->len) {
432 strbuf_adddup(sb, chunk->off, chunk->len);
433 return 1;
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
440 * struct strbuf.
442 chunk->off = sb->len;
443 return 0;
446 static void parse_commit_header(struct format_commit_context *context)
448 const char *msg = context->commit->buffer;
449 int i;
450 enum { HEADER, SUBJECT, BODY } state;
452 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
453 int eol;
454 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
455 ; /* do nothing */
457 if (state == SUBJECT) {
458 context->subject.off = i;
459 context->subject.len = eol - i;
460 i = eol;
462 if (i == eol) {
463 state++;
464 /* strip empty lines */
465 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
466 eol++;
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;
477 i = eol;
478 if (!msg[i])
479 break;
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);
492 while (d) {
493 strbuf_addstr(sb, prefix);
494 prefix = ", ";
495 strbuf_addstr(sb, d->name);
496 d = d->next;
498 if (prefix[0] == ',')
499 strbuf_addch(sb, ')');
502 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
503 void *context)
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;
509 int h1, h2;
511 /* these are independent of the commit */
512 switch (placeholder[0]) {
513 case 'C':
514 if (!prefixcmp(placeholder + 1, "red")) {
515 strbuf_addstr(sb, "\033[31m");
516 return 4;
517 } else if (!prefixcmp(placeholder + 1, "green")) {
518 strbuf_addstr(sb, "\033[32m");
519 return 6;
520 } else if (!prefixcmp(placeholder + 1, "blue")) {
521 strbuf_addstr(sb, "\033[34m");
522 return 5;
523 } else if (!prefixcmp(placeholder + 1, "reset")) {
524 strbuf_addstr(sb, "\033[m");
525 return 6;
526 } else
527 return 0;
528 case 'n': /* newline */
529 strbuf_addch(sb, '\n');
530 return 1;
531 case 'x':
532 /* %x00 == NUL, %x0a == LF, etc. */
533 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
534 h1 <= 16 &&
535 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
536 h2 <= 16) {
537 strbuf_addch(sb, (h1<<4)|h2);
538 return 3;
539 } else
540 return 0;
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));
550 return 1;
551 case 'h': /* abbreviated commit hash */
552 if (add_again(sb, &c->abbrev_commit_hash))
553 return 1;
554 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
555 DEFAULT_ABBREV));
556 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
557 return 1;
558 case 'T': /* tree hash */
559 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
560 return 1;
561 case 't': /* abbreviated tree hash */
562 if (add_again(sb, &c->abbrev_tree_hash))
563 return 1;
564 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
565 DEFAULT_ABBREV));
566 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
567 return 1;
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));
574 return 1;
575 case 'p': /* abbreviated parent hashes */
576 if (add_again(sb, &c->abbrev_parent_hashes))
577 return 1;
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;
586 return 1;
587 case 'm': /* left/right/bottom */
588 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
589 ? '-'
590 : (commit->object.flags & SYMMETRIC_LEFT)
591 ? '<'
592 : '>');
593 return 1;
594 case 'd':
595 format_decoration(sb, commit);
596 return 1;
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);
606 return 1;
607 case 'a': /* author ... */
608 return format_person_part(sb, placeholder[1],
609 msg + c->author.off, c->author.len,
610 c->dmode);
611 case 'c': /* committer ... */
612 return format_person_part(sb, placeholder[1],
613 msg + c->committer.off, c->committer.len,
614 c->dmode);
615 case 'e': /* encoding */
616 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
617 return 1;
618 case 'b': /* body */
619 strbuf_addstr(sb, msg + c->body_off);
620 return 1;
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,
638 int abbrev,
639 enum date_mode dmode,
640 const char *encoding,
641 const struct commit *commit,
642 const char **msg_p,
643 struct strbuf *sb)
645 int parents_shown = 0;
647 for (;;) {
648 const char *line = *msg_p;
649 int linelen = get_one_line(*msg_p);
651 if (!linelen)
652 return;
653 *msg_p += linelen;
655 if (linelen == 1)
656 /* End of header */
657 return;
659 if (fmt == CMIT_FMT_RAW) {
660 strbuf_add(sb, line, linelen);
661 continue;
664 if (!memcmp(line, "parent ", 7)) {
665 if (linelen != 48)
666 die("bad parent line in commit");
667 continue;
670 if (!parents_shown) {
671 struct commit_list *parent;
672 int num;
673 for (parent = commit->parents, num = 0;
674 parent;
675 parent = parent->next, num++)
677 /* with enough slop */
678 strbuf_grow(sb, num * 50 + 20);
679 add_merge_info(fmt, sb, commit, abbrev);
680 parents_shown = 1;
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,
701 const char **msg_p,
702 struct strbuf *sb,
703 const char *subject,
704 const char *after_subject,
705 const char *encoding,
706 int need_8bit_cte)
708 struct strbuf title;
710 strbuf_init(&title, 80);
712 for (;;) {
713 const char *line = *msg_p;
714 int linelen = get_one_line(line);
716 *msg_p += linelen;
717 if (!linelen || is_empty_line(line, &linelen))
718 break;
720 strbuf_grow(&title, linelen + 2);
721 if (title.len) {
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);
731 if (subject) {
732 strbuf_addstr(sb, subject);
733 add_rfc2047(sb, title.buf, title.len, encoding);
734 } else {
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);
746 if (after_subject) {
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,
756 const char **msg_p,
757 struct strbuf *sb,
758 int indent)
760 int first = 1;
761 for (;;) {
762 const char *line = *msg_p;
763 int linelen = get_one_line(line);
764 *msg_p += linelen;
766 if (!linelen)
767 break;
769 if (is_empty_line(line, &linelen)) {
770 if (first)
771 continue;
772 if (fmt == CMIT_FMT_SHORT)
773 break;
775 first = 0;
777 strbuf_grow(sb, linelen + indent + 20);
778 if (indent) {
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 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
788 struct strbuf *sb, int abbrev,
789 const char *subject, const char *after_subject,
790 enum date_mode dmode, int need_8bit_cte)
792 unsigned long beginning_of_body;
793 int indent = 4;
794 const char *msg = commit->buffer;
795 char *reencoded;
796 const char *encoding;
798 if (fmt == CMIT_FMT_USERFORMAT) {
799 format_commit_message(commit, user_format, sb, dmode);
800 return;
803 encoding = (git_log_output_encoding
804 ? git_log_output_encoding
805 : git_commit_encoding);
806 if (!encoding)
807 encoding = "utf-8";
808 reencoded = logmsg_reencode(commit, encoding);
809 if (reencoded) {
810 msg = reencoded;
813 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
814 indent = 0;
817 * We need to check and emit Content-type: to mark it
818 * as 8-bit if we haven't done so.
820 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
821 int i, ch, in_body;
823 for (in_body = i = 0; (ch = msg[i]); i++) {
824 if (!in_body) {
825 /* author could be non 7-bit ASCII but
826 * the log may be so; skip over the
827 * header part first.
829 if (ch == '\n' && msg[i+1] == '\n')
830 in_body = 1;
832 else if (non_ascii(ch)) {
833 need_8bit_cte = 1;
834 break;
839 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
840 if (fmt != CMIT_FMT_ONELINE && !subject) {
841 strbuf_addch(sb, '\n');
844 /* Skip excess blank lines at the beginning of body, if any... */
845 for (;;) {
846 int linelen = get_one_line(msg);
847 int ll = linelen;
848 if (!linelen)
849 break;
850 if (!is_empty_line(msg, &ll))
851 break;
852 msg += linelen;
855 /* These formats treat the title line specially. */
856 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
857 pp_title_line(fmt, &msg, sb, subject,
858 after_subject, encoding, need_8bit_cte);
860 beginning_of_body = sb->len;
861 if (fmt != CMIT_FMT_ONELINE)
862 pp_remainder(fmt, &msg, sb, indent);
863 strbuf_rtrim(sb);
865 /* Make sure there is an EOLN for the non-oneline case */
866 if (fmt != CMIT_FMT_ONELINE)
867 strbuf_addch(sb, '\n');
870 * The caller may append additional body text in e-mail
871 * format. Make sure we did not strip the blank line
872 * between the header and the body.
874 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
875 strbuf_addch(sb, '\n');
876 free(reencoded);