Code

Merge branch 'maint-1.6.0' into maint-1.6.1
[git.git] / pretty.c
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)
13 {
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 },
27         };
29         rev->use_terminator = 0;
30         if (!arg || !*arg) {
31                 rev->commit_format = CMIT_FMT_DEFAULT;
32                 return;
33         }
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;
42         }
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;
50                 }
51         }
53         die("invalid --pretty format: %s", arg);
54 }
56 /*
57  * Generic support for pretty-printing the header
58  */
59 static int get_one_line(const char *msg)
60 {
61         int ret = 0;
63         for (;;) {
64                 char c = *msg++;
65                 if (!c)
66                         break;
67                 ret++;
68                 if (c == '\n')
69                         break;
70         }
71         return ret;
72 }
74 /* High bit set, or ISO-2022-INT */
75 int non_ascii(int ch)
76 {
77         ch = (ch & 0xff);
78         return ((ch & 0x80) || (ch == 0x1b));
79 }
81 static int is_rfc2047_special(char ch)
82 {
83         return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
84 }
86 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
87                        const char *encoding)
88 {
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;
97         }
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;
106                 /*
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.
111                  */
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;
116                 }
117         }
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);
158         }
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;
172         }
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 const char *skip_empty_lines(const char *msg)
186         for (;;) {
187                 int linelen = get_one_line(msg);
188                 int ll = linelen;
189                 if (!linelen)
190                         break;
191                 if (!is_empty_line(msg, &ll))
192                         break;
193                 msg += linelen;
194         }
195         return msg;
198 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
199                         const struct commit *commit, int abbrev)
201         struct commit_list *parent = commit->parents;
203         if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
204             !parent || !parent->next)
205                 return;
207         strbuf_addstr(sb, "Merge:");
209         while (parent) {
210                 struct commit *p = parent->item;
211                 const char *hex = NULL;
212                 const char *dots;
213                 if (abbrev)
214                         hex = find_unique_abbrev(p->object.sha1, abbrev);
215                 if (!hex)
216                         hex = sha1_to_hex(p->object.sha1);
217                 dots = (abbrev && strlen(hex) != 40) ?  "..." : "";
218                 parent = parent->next;
220                 strbuf_addf(sb, " %s%s", hex, dots);
221         }
222         strbuf_addch(sb, '\n');
225 static char *get_header(const struct commit *commit, const char *key)
227         int key_len = strlen(key);
228         const char *line = commit->buffer;
230         for (;;) {
231                 const char *eol = strchr(line, '\n'), *next;
233                 if (line == eol)
234                         return NULL;
235                 if (!eol) {
236                         eol = line + strlen(line);
237                         next = NULL;
238                 } else
239                         next = eol + 1;
240                 if (eol - line > key_len &&
241                     !strncmp(line, key, key_len) &&
242                     line[key_len] == ' ') {
243                         return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
244                 }
245                 line = next;
246         }
249 static char *replace_encoding_header(char *buf, const char *encoding)
251         struct strbuf tmp = STRBUF_INIT;
252         size_t start, len;
253         char *cp = buf;
255         /* guess if there is an encoding header before a \n\n */
256         while (strncmp(cp, "encoding ", strlen("encoding "))) {
257                 cp = strchr(cp, '\n');
258                 if (!cp || *++cp == '\n')
259                         return buf;
260         }
261         start = cp - buf;
262         cp = strchr(cp, '\n');
263         if (!cp)
264                 return buf; /* should not happen but be defensive */
265         len = cp + 1 - (buf + start);
267         strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
268         if (is_encoding_utf8(encoding)) {
269                 /* we have re-coded to UTF-8; drop the header */
270                 strbuf_remove(&tmp, start, len);
271         } else {
272                 /* just replaces XXXX in 'encoding XXXX\n' */
273                 strbuf_splice(&tmp, start + strlen("encoding "),
274                                           len - strlen("encoding \n"),
275                                           encoding, strlen(encoding));
276         }
277         return strbuf_detach(&tmp, NULL);
280 static char *logmsg_reencode(const struct commit *commit,
281                              const char *output_encoding)
283         static const char *utf8 = "utf-8";
284         const char *use_encoding;
285         char *encoding;
286         char *out;
288         if (!*output_encoding)
289                 return NULL;
290         encoding = get_header(commit, "encoding");
291         use_encoding = encoding ? encoding : utf8;
292         if (!strcmp(use_encoding, output_encoding))
293                 if (encoding) /* we'll strip encoding header later */
294                         out = xstrdup(commit->buffer);
295                 else
296                         return NULL; /* nothing to do */
297         else
298                 out = reencode_string(commit->buffer,
299                                       output_encoding, use_encoding);
300         if (out)
301                 out = replace_encoding_header(out, output_encoding);
303         free(encoding);
304         return out;
307 static int mailmap_name(struct strbuf *sb, const char *email)
309         static struct string_list *mail_map;
310         char buffer[1024];
312         if (!mail_map) {
313                 mail_map = xcalloc(1, sizeof(*mail_map));
314                 read_mailmap(mail_map, ".mailmap", NULL);
315         }
317         if (!mail_map->nr)
318                 return -1;
320         if (!map_email(mail_map, email, buffer, sizeof(buffer)))
321                 return -1;
322         strbuf_addstr(sb, buffer);
323         return 0;
326 static size_t format_person_part(struct strbuf *sb, char part,
327                                  const char *msg, int len, enum date_mode dmode)
329         /* currently all placeholders have same length */
330         const int placeholder_len = 2;
331         int start, end, tz = 0;
332         unsigned long date = 0;
333         char *ep;
335         /* advance 'end' to point to email start delimiter */
336         for (end = 0; end < len && msg[end] != '<'; end++)
337                 ; /* do nothing */
339         /*
340          * When end points at the '<' that we found, it should have
341          * matching '>' later, which means 'end' must be strictly
342          * below len - 1.
343          */
344         if (end >= len - 2)
345                 goto skip;
347         if (part == 'n' || part == 'N') {       /* name */
348                 while (end > 0 && isspace(msg[end - 1]))
349                         end--;
350                 if (part != 'N' || !msg[end] || !msg[end + 1] ||
351                     mailmap_name(sb, msg + end + 2) < 0)
352                         strbuf_add(sb, msg, end);
353                 return placeholder_len;
354         }
355         start = ++end; /* save email start position */
357         /* advance 'end' to point to email end delimiter */
358         for ( ; end < len && msg[end] != '>'; end++)
359                 ; /* do nothing */
361         if (end >= len)
362                 goto skip;
364         if (part == 'e') {      /* email */
365                 strbuf_add(sb, msg + start, end - start);
366                 return placeholder_len;
367         }
369         /* advance 'start' to point to date start delimiter */
370         for (start = end + 1; start < len && isspace(msg[start]); start++)
371                 ; /* do nothing */
372         if (start >= len)
373                 goto skip;
374         date = strtoul(msg + start, &ep, 10);
375         if (msg + start == ep)
376                 goto skip;
378         if (part == 't') {      /* date, UNIX timestamp */
379                 strbuf_add(sb, msg + start, ep - (msg + start));
380                 return placeholder_len;
381         }
383         /* parse tz */
384         for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
385                 ; /* do nothing */
386         if (start + 1 < len) {
387                 tz = strtoul(msg + start + 1, NULL, 10);
388                 if (msg[start] == '-')
389                         tz = -tz;
390         }
392         switch (part) {
393         case 'd':       /* date */
394                 strbuf_addstr(sb, show_date(date, tz, dmode));
395                 return placeholder_len;
396         case 'D':       /* date, RFC2822 style */
397                 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
398                 return placeholder_len;
399         case 'r':       /* date, relative */
400                 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
401                 return placeholder_len;
402         case 'i':       /* date, ISO 8601 */
403                 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
404                 return placeholder_len;
405         }
407 skip:
408         /*
409          * bogus commit, 'sb' cannot be updated, but we still need to
410          * compute a valid return value.
411          */
412         if (part == 'n' || part == 'e' || part == 't' || part == 'd'
413             || part == 'D' || part == 'r' || part == 'i')
414                 return placeholder_len;
416         return 0; /* unknown placeholder */
419 struct chunk {
420         size_t off;
421         size_t len;
422 };
424 struct format_commit_context {
425         const struct commit *commit;
426         enum date_mode dmode;
427         unsigned commit_header_parsed:1;
428         unsigned commit_message_parsed:1;
430         /* These offsets are relative to the start of the commit message. */
431         struct chunk author;
432         struct chunk committer;
433         struct chunk encoding;
434         size_t message_off;
435         size_t subject_off;
436         size_t body_off;
438         /* The following ones are relative to the result struct strbuf. */
439         struct chunk abbrev_commit_hash;
440         struct chunk abbrev_tree_hash;
441         struct chunk abbrev_parent_hashes;
442 };
444 static int add_again(struct strbuf *sb, struct chunk *chunk)
446         if (chunk->len) {
447                 strbuf_adddup(sb, chunk->off, chunk->len);
448                 return 1;
449         }
451         /*
452          * We haven't seen this chunk before.  Our caller is surely
453          * going to add it the hard way now.  Remember the most likely
454          * start of the to-be-added chunk: the current end of the
455          * struct strbuf.
456          */
457         chunk->off = sb->len;
458         return 0;
461 static void parse_commit_header(struct format_commit_context *context)
463         const char *msg = context->commit->buffer;
464         int i;
466         for (i = 0; msg[i]; i++) {
467                 int eol;
468                 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
469                         ; /* do nothing */
471                 if (i == eol) {
472                         break;
473                 } else if (!prefixcmp(msg + i, "author ")) {
474                         context->author.off = i + 7;
475                         context->author.len = eol - i - 7;
476                 } else if (!prefixcmp(msg + i, "committer ")) {
477                         context->committer.off = i + 10;
478                         context->committer.len = eol - i - 10;
479                 } else if (!prefixcmp(msg + i, "encoding ")) {
480                         context->encoding.off = i + 9;
481                         context->encoding.len = eol - i - 9;
482                 }
483                 i = eol;
484         }
485         context->message_off = i;
486         context->commit_header_parsed = 1;
489 const char *format_subject(struct strbuf *sb, const char *msg,
490                            const char *line_separator)
492         int first = 1;
494         for (;;) {
495                 const char *line = msg;
496                 int linelen = get_one_line(line);
498                 msg += linelen;
499                 if (!linelen || is_empty_line(line, &linelen))
500                         break;
502                 if (!sb)
503                         continue;
504                 strbuf_grow(sb, linelen + 2);
505                 if (!first)
506                         strbuf_addstr(sb, line_separator);
507                 strbuf_add(sb, line, linelen);
508                 first = 0;
509         }
510         return msg;
513 static void parse_commit_message(struct format_commit_context *c)
515         const char *msg = c->commit->buffer + c->message_off;
516         const char *start = c->commit->buffer;
518         msg = skip_empty_lines(msg);
519         c->subject_off = msg - start;
521         msg = format_subject(NULL, msg, NULL);
522         msg = skip_empty_lines(msg);
523         c->body_off = msg - start;
525         c->commit_message_parsed = 1;
528 static void format_decoration(struct strbuf *sb, const struct commit *commit)
530         struct name_decoration *d;
531         const char *prefix = " (";
533         load_ref_decorations();
534         d = lookup_decoration(&name_decoration, &commit->object);
535         while (d) {
536                 strbuf_addstr(sb, prefix);
537                 prefix = ", ";
538                 strbuf_addstr(sb, d->name);
539                 d = d->next;
540         }
541         if (prefix[0] == ',')
542                 strbuf_addch(sb, ')');
545 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
546                                void *context)
548         struct format_commit_context *c = context;
549         const struct commit *commit = c->commit;
550         const char *msg = commit->buffer;
551         struct commit_list *p;
552         int h1, h2;
554         /* these are independent of the commit */
555         switch (placeholder[0]) {
556         case 'C':
557                 if (!prefixcmp(placeholder + 1, "red")) {
558                         strbuf_addstr(sb, "\033[31m");
559                         return 4;
560                 } else if (!prefixcmp(placeholder + 1, "green")) {
561                         strbuf_addstr(sb, "\033[32m");
562                         return 6;
563                 } else if (!prefixcmp(placeholder + 1, "blue")) {
564                         strbuf_addstr(sb, "\033[34m");
565                         return 5;
566                 } else if (!prefixcmp(placeholder + 1, "reset")) {
567                         strbuf_addstr(sb, "\033[m");
568                         return 6;
569                 } else
570                         return 0;
571         case 'n':               /* newline */
572                 strbuf_addch(sb, '\n');
573                 return 1;
574         case 'x':
575                 /* %x00 == NUL, %x0a == LF, etc. */
576                 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
577                     h1 <= 16 &&
578                     0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
579                     h2 <= 16) {
580                         strbuf_addch(sb, (h1<<4)|h2);
581                         return 3;
582                 } else
583                         return 0;
584         }
586         /* these depend on the commit */
587         if (!commit->object.parsed)
588                 parse_object(commit->object.sha1);
590         switch (placeholder[0]) {
591         case 'H':               /* commit hash */
592                 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
593                 return 1;
594         case 'h':               /* abbreviated commit hash */
595                 if (add_again(sb, &c->abbrev_commit_hash))
596                         return 1;
597                 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
598                                                      DEFAULT_ABBREV));
599                 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
600                 return 1;
601         case 'T':               /* tree hash */
602                 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
603                 return 1;
604         case 't':               /* abbreviated tree hash */
605                 if (add_again(sb, &c->abbrev_tree_hash))
606                         return 1;
607                 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
608                                                      DEFAULT_ABBREV));
609                 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
610                 return 1;
611         case 'P':               /* parent hashes */
612                 for (p = commit->parents; p; p = p->next) {
613                         if (p != commit->parents)
614                                 strbuf_addch(sb, ' ');
615                         strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
616                 }
617                 return 1;
618         case 'p':               /* abbreviated parent hashes */
619                 if (add_again(sb, &c->abbrev_parent_hashes))
620                         return 1;
621                 for (p = commit->parents; p; p = p->next) {
622                         if (p != commit->parents)
623                                 strbuf_addch(sb, ' ');
624                         strbuf_addstr(sb, find_unique_abbrev(
625                                         p->item->object.sha1, DEFAULT_ABBREV));
626                 }
627                 c->abbrev_parent_hashes.len = sb->len -
628                                               c->abbrev_parent_hashes.off;
629                 return 1;
630         case 'm':               /* left/right/bottom */
631                 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
632                                  ? '-'
633                                  : (commit->object.flags & SYMMETRIC_LEFT)
634                                  ? '<'
635                                  : '>');
636                 return 1;
637         case 'd':
638                 format_decoration(sb, commit);
639                 return 1;
640         }
642         /* For the rest we have to parse the commit header. */
643         if (!c->commit_header_parsed)
644                 parse_commit_header(c);
646         switch (placeholder[0]) {
647         case 'a':       /* author ... */
648                 return format_person_part(sb, placeholder[1],
649                                    msg + c->author.off, c->author.len,
650                                    c->dmode);
651         case 'c':       /* committer ... */
652                 return format_person_part(sb, placeholder[1],
653                                    msg + c->committer.off, c->committer.len,
654                                    c->dmode);
655         case 'e':       /* encoding */
656                 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
657                 return 1;
658         }
660         /* Now we need to parse the commit message. */
661         if (!c->commit_message_parsed)
662                 parse_commit_message(c);
664         switch (placeholder[0]) {
665         case 's':       /* subject */
666                 format_subject(sb, msg + c->subject_off, " ");
667                 return 1;
668         case 'b':       /* body */
669                 strbuf_addstr(sb, msg + c->body_off);
670                 return 1;
671         }
672         return 0;       /* unknown placeholder */
675 void format_commit_message(const struct commit *commit,
676                            const void *format, struct strbuf *sb,
677                            enum date_mode dmode)
679         struct format_commit_context context;
681         memset(&context, 0, sizeof(context));
682         context.commit = commit;
683         context.dmode = dmode;
684         strbuf_expand(sb, format, format_commit_item, &context);
687 static void pp_header(enum cmit_fmt fmt,
688                       int abbrev,
689                       enum date_mode dmode,
690                       const char *encoding,
691                       const struct commit *commit,
692                       const char **msg_p,
693                       struct strbuf *sb)
695         int parents_shown = 0;
697         for (;;) {
698                 const char *line = *msg_p;
699                 int linelen = get_one_line(*msg_p);
701                 if (!linelen)
702                         return;
703                 *msg_p += linelen;
705                 if (linelen == 1)
706                         /* End of header */
707                         return;
709                 if (fmt == CMIT_FMT_RAW) {
710                         strbuf_add(sb, line, linelen);
711                         continue;
712                 }
714                 if (!memcmp(line, "parent ", 7)) {
715                         if (linelen != 48)
716                                 die("bad parent line in commit");
717                         continue;
718                 }
720                 if (!parents_shown) {
721                         struct commit_list *parent;
722                         int num;
723                         for (parent = commit->parents, num = 0;
724                              parent;
725                              parent = parent->next, num++)
726                                 ;
727                         /* with enough slop */
728                         strbuf_grow(sb, num * 50 + 20);
729                         add_merge_info(fmt, sb, commit, abbrev);
730                         parents_shown = 1;
731                 }
733                 /*
734                  * MEDIUM == DEFAULT shows only author with dates.
735                  * FULL shows both authors but not dates.
736                  * FULLER shows both authors and dates.
737                  */
738                 if (!memcmp(line, "author ", 7)) {
739                         strbuf_grow(sb, linelen + 80);
740                         pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
741                 }
742                 if (!memcmp(line, "committer ", 10) &&
743                     (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
744                         strbuf_grow(sb, linelen + 80);
745                         pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
746                 }
747         }
750 void pp_title_line(enum cmit_fmt fmt,
751                    const char **msg_p,
752                    struct strbuf *sb,
753                    const char *subject,
754                    const char *after_subject,
755                    const char *encoding,
756                    int need_8bit_cte)
758         const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
759         struct strbuf title;
761         strbuf_init(&title, 80);
762         *msg_p = format_subject(&title, *msg_p, line_separator);
764         strbuf_grow(sb, title.len + 1024);
765         if (subject) {
766                 strbuf_addstr(sb, subject);
767                 add_rfc2047(sb, title.buf, title.len, encoding);
768         } else {
769                 strbuf_addbuf(sb, &title);
770         }
771         strbuf_addch(sb, '\n');
773         if (need_8bit_cte > 0) {
774                 const char *header_fmt =
775                         "MIME-Version: 1.0\n"
776                         "Content-Type: text/plain; charset=%s\n"
777                         "Content-Transfer-Encoding: 8bit\n";
778                 strbuf_addf(sb, header_fmt, encoding);
779         }
780         if (after_subject) {
781                 strbuf_addstr(sb, after_subject);
782         }
783         if (fmt == CMIT_FMT_EMAIL) {
784                 strbuf_addch(sb, '\n');
785         }
786         strbuf_release(&title);
789 void pp_remainder(enum cmit_fmt fmt,
790                   const char **msg_p,
791                   struct strbuf *sb,
792                   int indent)
794         int first = 1;
795         for (;;) {
796                 const char *line = *msg_p;
797                 int linelen = get_one_line(line);
798                 *msg_p += linelen;
800                 if (!linelen)
801                         break;
803                 if (is_empty_line(line, &linelen)) {
804                         if (first)
805                                 continue;
806                         if (fmt == CMIT_FMT_SHORT)
807                                 break;
808                 }
809                 first = 0;
811                 strbuf_grow(sb, linelen + indent + 20);
812                 if (indent) {
813                         memset(sb->buf + sb->len, ' ', indent);
814                         strbuf_setlen(sb, sb->len + indent);
815                 }
816                 strbuf_add(sb, line, linelen);
817                 strbuf_addch(sb, '\n');
818         }
821 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
823         const char *encoding;
825         encoding = (git_log_output_encoding
826                     ? git_log_output_encoding
827                     : git_commit_encoding);
828         if (!encoding)
829                 encoding = "utf-8";
830         if (encoding_p)
831                 *encoding_p = encoding;
832         return logmsg_reencode(commit, encoding);
835 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
836                          struct strbuf *sb, int abbrev,
837                          const char *subject, const char *after_subject,
838                          enum date_mode dmode, int need_8bit_cte)
840         unsigned long beginning_of_body;
841         int indent = 4;
842         const char *msg = commit->buffer;
843         char *reencoded;
844         const char *encoding;
846         if (fmt == CMIT_FMT_USERFORMAT) {
847                 format_commit_message(commit, user_format, sb, dmode);
848                 return;
849         }
851         reencoded = reencode_commit_message(commit, &encoding);
852         if (reencoded) {
853                 msg = reencoded;
854         }
856         if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
857                 indent = 0;
859         /*
860          * We need to check and emit Content-type: to mark it
861          * as 8-bit if we haven't done so.
862          */
863         if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
864                 int i, ch, in_body;
866                 for (in_body = i = 0; (ch = msg[i]); i++) {
867                         if (!in_body) {
868                                 /* author could be non 7-bit ASCII but
869                                  * the log may be so; skip over the
870                                  * header part first.
871                                  */
872                                 if (ch == '\n' && msg[i+1] == '\n')
873                                         in_body = 1;
874                         }
875                         else if (non_ascii(ch)) {
876                                 need_8bit_cte = 1;
877                                 break;
878                         }
879                 }
880         }
882         pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
883         if (fmt != CMIT_FMT_ONELINE && !subject) {
884                 strbuf_addch(sb, '\n');
885         }
887         /* Skip excess blank lines at the beginning of body, if any... */
888         msg = skip_empty_lines(msg);
890         /* These formats treat the title line specially. */
891         if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
892                 pp_title_line(fmt, &msg, sb, subject,
893                               after_subject, encoding, need_8bit_cte);
895         beginning_of_body = sb->len;
896         if (fmt != CMIT_FMT_ONELINE)
897                 pp_remainder(fmt, &msg, sb, indent);
898         strbuf_rtrim(sb);
900         /* Make sure there is an EOLN for the non-oneline case */
901         if (fmt != CMIT_FMT_ONELINE)
902                 strbuf_addch(sb, '\n');
904         /*
905          * The caller may append additional body text in e-mail
906          * format.  Make sure we did not strip the blank line
907          * between the header and the body.
908          */
909         if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
910                 strbuf_addch(sb, '\n');
911         free(reencoded);