Merge branch 'ne/rev-cache' into pu
[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"
9 #include "color.h"
10
11 static char *user_format;
12
13 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
14 {
15         free(user_format);
16         user_format = xstrdup(cp);
17         if (is_tformat)
18                 rev->use_terminator = 1;
19         rev->commit_format = CMIT_FMT_USERFORMAT;
20 }
21
22 void get_commit_format(const char *arg, struct rev_info *rev)
23 {
24         int i;
25         static struct cmt_fmt_map {
26                 const char *n;
27                 size_t cmp_len;
28                 enum cmit_fmt v;
29         } cmt_fmts[] = {
30                 { "raw",        1,      CMIT_FMT_RAW },
31                 { "medium",     1,      CMIT_FMT_MEDIUM },
32                 { "short",      1,      CMIT_FMT_SHORT },
33                 { "email",      1,      CMIT_FMT_EMAIL },
34                 { "full",       5,      CMIT_FMT_FULL },
35                 { "fuller",     5,      CMIT_FMT_FULLER },
36                 { "oneline",    1,      CMIT_FMT_ONELINE },
37         };
38
39         rev->use_terminator = 0;
40         if (!arg || !*arg) {
41                 rev->commit_format = CMIT_FMT_DEFAULT;
42                 return;
43         }
44         if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
45                 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
46                 return;
47         }
48         for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
49                 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
50                     !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
51                         if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
52                                 rev->use_terminator = 1;
53                         rev->commit_format = cmt_fmts[i].v;
54                         return;
55                 }
56         }
57         if (strchr(arg, '%')) {
58                 save_user_format(rev, arg, 1);
59                 return;
60         }
61
62         die("invalid --pretty format: %s", arg);
63 }
64
65 /*
66  * Generic support for pretty-printing the header
67  */
68 static int get_one_line(const char *msg)
69 {
70         int ret = 0;
71
72         for (;;) {
73                 char c = *msg++;
74                 if (!c)
75                         break;
76                 ret++;
77                 if (c == '\n')
78                         break;
79         }
80         return ret;
81 }
82
83 /* High bit set, or ISO-2022-INT */
84 int non_ascii(int ch)
85 {
86         return !isascii(ch) || ch == '\033';
87 }
88
89 int has_non_ascii(const char *s)
90 {
91         int ch;
92         if (!s)
93                 return 0;
94         while ((ch = *s++) != '\0') {
95                 if (non_ascii(ch))
96                         return 1;
97         }
98         return 0;
99 }
100
101 static int is_rfc2047_special(char ch)
102 {
103         return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
104 }
105
106 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
107                        const char *encoding)
108 {
109         int i, last;
110
111         for (i = 0; i < len; i++) {
112                 int ch = line[i];
113                 if (non_ascii(ch))
114                         goto needquote;
115                 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
116                         goto needquote;
117         }
118         strbuf_add(sb, line, len);
119         return;
120
121 needquote:
122         strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
123         strbuf_addf(sb, "=?%s?q?", encoding);
124         for (i = last = 0; i < len; i++) {
125                 unsigned ch = line[i] & 0xFF;
126                 /*
127                  * We encode ' ' using '=20' even though rfc2047
128                  * allows using '_' for readability.  Unfortunately,
129                  * many programs do not understand this and just
130                  * leave the underscore in place.
131                  */
132                 if (is_rfc2047_special(ch) || ch == ' ') {
133                         strbuf_add(sb, line + last, i - last);
134                         strbuf_addf(sb, "=%02X", ch);
135                         last = i + 1;
136                 }
137         }
138         strbuf_add(sb, line + last, len - last);
139         strbuf_addstr(sb, "?=");
140 }
141
142 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
143                   const char *line, enum date_mode dmode,
144                   const char *encoding)
145 {
146         char *date;
147         int namelen;
148         unsigned long time;
149         int tz;
150
151         if (fmt == CMIT_FMT_ONELINE)
152                 return;
153         date = strchr(line, '>');
154         if (!date)
155                 return;
156         namelen = ++date - line;
157         time = strtoul(date, &date, 10);
158         tz = strtol(date, NULL, 10);
159
160         if (fmt == CMIT_FMT_EMAIL) {
161                 char *name_tail = strchr(line, '<');
162                 int display_name_length;
163                 if (!name_tail)
164                         return;
165                 while (line < name_tail && isspace(name_tail[-1]))
166                         name_tail--;
167                 display_name_length = name_tail - line;
168                 strbuf_addstr(sb, "From: ");
169                 add_rfc2047(sb, line, display_name_length, encoding);
170                 strbuf_add(sb, name_tail, namelen - display_name_length);
171                 strbuf_addch(sb, '\n');
172         } else {
173                 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
174                               (fmt == CMIT_FMT_FULLER) ? 4 : 0,
175                               "    ", namelen, line);
176         }
177         switch (fmt) {
178         case CMIT_FMT_MEDIUM:
179                 strbuf_addf(sb, "Date:   %s\n", show_date(time, tz, dmode));
180                 break;
181         case CMIT_FMT_EMAIL:
182                 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
183                 break;
184         case CMIT_FMT_FULLER:
185                 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
186                 break;
187         default:
188                 /* notin' */
189                 break;
190         }
191 }
192
193 static int is_empty_line(const char *line, int *len_p)
194 {
195         int len = *len_p;
196         while (len && isspace(line[len-1]))
197                 len--;
198         *len_p = len;
199         return !len;
200 }
201
202 static const char *skip_empty_lines(const char *msg)
203 {
204         for (;;) {
205                 int linelen = get_one_line(msg);
206                 int ll = linelen;
207                 if (!linelen)
208                         break;
209                 if (!is_empty_line(msg, &ll))
210                         break;
211                 msg += linelen;
212         }
213         return msg;
214 }
215
216 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
217                         const struct commit *commit, int abbrev)
218 {
219         struct commit_list *parent = commit->parents;
220
221         if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
222             !parent || !parent->next)
223                 return;
224
225         strbuf_addstr(sb, "Merge:");
226
227         while (parent) {
228                 struct commit *p = parent->item;
229                 const char *hex = NULL;
230                 if (abbrev)
231                         hex = find_unique_abbrev(p->object.sha1, abbrev);
232                 if (!hex)
233                         hex = sha1_to_hex(p->object.sha1);
234                 parent = parent->next;
235
236                 strbuf_addf(sb, " %s", hex);
237         }
238         strbuf_addch(sb, '\n');
239 }
240
241 static char *get_header(const struct commit *commit, const char *key)
242 {
243         int key_len = strlen(key);
244         const char *line = commit->buffer;
245
246         for (;;) {
247                 const char *eol = strchr(line, '\n'), *next;
248
249                 if (line == eol)
250                         return NULL;
251                 if (!eol) {
252                         eol = line + strlen(line);
253                         next = NULL;
254                 } else
255                         next = eol + 1;
256                 if (eol - line > key_len &&
257                     !strncmp(line, key, key_len) &&
258                     line[key_len] == ' ') {
259                         return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
260                 }
261                 line = next;
262         }
263 }
264
265 static char *replace_encoding_header(char *buf, const char *encoding)
266 {
267         struct strbuf tmp = STRBUF_INIT;
268         size_t start, len;
269         char *cp = buf;
270
271         /* guess if there is an encoding header before a \n\n */
272         while (strncmp(cp, "encoding ", strlen("encoding "))) {
273                 cp = strchr(cp, '\n');
274                 if (!cp || *++cp == '\n')
275                         return buf;
276         }
277         start = cp - buf;
278         cp = strchr(cp, '\n');
279         if (!cp)
280                 return buf; /* should not happen but be defensive */
281         len = cp + 1 - (buf + start);
282
283         strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
284         if (is_encoding_utf8(encoding)) {
285                 /* we have re-coded to UTF-8; drop the header */
286                 strbuf_remove(&tmp, start, len);
287         } else {
288                 /* just replaces XXXX in 'encoding XXXX\n' */
289                 strbuf_splice(&tmp, start + strlen("encoding "),
290                                           len - strlen("encoding \n"),
291                                           encoding, strlen(encoding));
292         }
293         return strbuf_detach(&tmp, NULL);
294 }
295
296 static char *logmsg_reencode(const struct commit *commit,
297                              const char *output_encoding)
298 {
299         static const char *utf8 = "UTF-8";
300         const char *use_encoding;
301         char *encoding;
302         char *out;
303
304         if (!*output_encoding)
305                 return NULL;
306         encoding = get_header(commit, "encoding");
307         use_encoding = encoding ? encoding : utf8;
308         if (!strcmp(use_encoding, output_encoding))
309                 if (encoding) /* we'll strip encoding header later */
310                         out = xstrdup(commit->buffer);
311                 else
312                         return NULL; /* nothing to do */
313         else
314                 out = reencode_string(commit->buffer,
315                                       output_encoding, use_encoding);
316         if (out)
317                 out = replace_encoding_header(out, output_encoding);
318
319         free(encoding);
320         return out;
321 }
322
323 static int mailmap_name(char *email, int email_len, char *name, int name_len)
324 {
325         static struct string_list *mail_map;
326         if (!mail_map) {
327                 mail_map = xcalloc(1, sizeof(*mail_map));
328                 read_mailmap(mail_map, NULL);
329         }
330         return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
331 }
332
333 static size_t format_person_part(struct strbuf *sb, char part,
334                                  const char *msg, int len, enum date_mode dmode)
335 {
336         /* currently all placeholders have same length */
337         const int placeholder_len = 2;
338         int start, end, tz = 0;
339         unsigned long date = 0;
340         char *ep;
341         const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
342         char person_name[1024];
343         char person_mail[1024];
344
345         /* advance 'end' to point to email start delimiter */
346         for (end = 0; end < len && msg[end] != '<'; end++)
347                 ; /* do nothing */
348
349         /*
350          * When end points at the '<' that we found, it should have
351          * matching '>' later, which means 'end' must be strictly
352          * below len - 1.
353          */
354         if (end >= len - 2)
355                 goto skip;
356
357         /* Seek for both name and email part */
358         name_start = msg;
359         name_end = msg+end;
360         while (name_end > name_start && isspace(*(name_end-1)))
361                 name_end--;
362         mail_start = msg+end+1;
363         mail_end = mail_start;
364         while (mail_end < msg_end && *mail_end != '>')
365                 mail_end++;
366         if (mail_end == msg_end)
367                 goto skip;
368         end = mail_end-msg;
369
370         if (part == 'N' || part == 'E') { /* mailmap lookup */
371                 strlcpy(person_name, name_start, name_end-name_start+1);
372                 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
373                 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
374                 name_start = person_name;
375                 name_end = name_start + strlen(person_name);
376                 mail_start = person_mail;
377                 mail_end = mail_start +  strlen(person_mail);
378         }
379         if (part == 'n' || part == 'N') {       /* name */
380                 strbuf_add(sb, name_start, name_end-name_start);
381                 return placeholder_len;
382         }
383         if (part == 'e' || part == 'E') {       /* email */
384                 strbuf_add(sb, mail_start, mail_end-mail_start);
385                 return placeholder_len;
386         }
387
388         /* advance 'start' to point to date start delimiter */
389         for (start = end + 1; start < len && isspace(msg[start]); start++)
390                 ; /* do nothing */
391         if (start >= len)
392                 goto skip;
393         date = strtoul(msg + start, &ep, 10);
394         if (msg + start == ep)
395                 goto skip;
396
397         if (part == 't') {      /* date, UNIX timestamp */
398                 strbuf_add(sb, msg + start, ep - (msg + start));
399                 return placeholder_len;
400         }
401
402         /* parse tz */
403         for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
404                 ; /* do nothing */
405         if (start + 1 < len) {
406                 tz = strtoul(msg + start + 1, NULL, 10);
407                 if (msg[start] == '-')
408                         tz = -tz;
409         }
410
411         switch (part) {
412         case 'd':       /* date */
413                 strbuf_addstr(sb, show_date(date, tz, dmode));
414                 return placeholder_len;
415         case 'D':       /* date, RFC2822 style */
416                 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
417                 return placeholder_len;
418         case 'r':       /* date, relative */
419                 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
420                 return placeholder_len;
421         case 'i':       /* date, ISO 8601 */
422                 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
423                 return placeholder_len;
424         }
425
426 skip:
427         /*
428          * bogus commit, 'sb' cannot be updated, but we still need to
429          * compute a valid return value.
430          */
431         if (part == 'n' || part == 'e' || part == 't' || part == 'd'
432             || part == 'D' || part == 'r' || part == 'i')
433                 return placeholder_len;
434
435         return 0; /* unknown placeholder */
436 }
437
438 struct chunk {
439         size_t off;
440         size_t len;
441 };
442
443 struct format_commit_context {
444         const struct commit *commit;
445         enum date_mode dmode;
446         unsigned commit_header_parsed:1;
447         unsigned commit_message_parsed:1;
448
449         /* These offsets are relative to the start of the commit message. */
450         struct chunk author;
451         struct chunk committer;
452         struct chunk encoding;
453         size_t message_off;
454         size_t subject_off;
455         size_t body_off;
456
457         /* The following ones are relative to the result struct strbuf. */
458         struct chunk abbrev_commit_hash;
459         struct chunk abbrev_tree_hash;
460         struct chunk abbrev_parent_hashes;
461 };
462
463 static int add_again(struct strbuf *sb, struct chunk *chunk)
464 {
465         if (chunk->len) {
466                 strbuf_adddup(sb, chunk->off, chunk->len);
467                 return 1;
468         }
469
470         /*
471          * We haven't seen this chunk before.  Our caller is surely
472          * going to add it the hard way now.  Remember the most likely
473          * start of the to-be-added chunk: the current end of the
474          * struct strbuf.
475          */
476         chunk->off = sb->len;
477         return 0;
478 }
479
480 static void parse_commit_header(struct format_commit_context *context)
481 {
482         const char *msg = context->commit->buffer;
483         int i;
484
485         for (i = 0; msg[i]; i++) {
486                 int eol;
487                 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
488                         ; /* do nothing */
489
490                 if (i == eol) {
491                         break;
492                 } else if (!prefixcmp(msg + i, "author ")) {
493                         context->author.off = i + 7;
494                         context->author.len = eol - i - 7;
495                 } else if (!prefixcmp(msg + i, "committer ")) {
496                         context->committer.off = i + 10;
497                         context->committer.len = eol - i - 10;
498                 } else if (!prefixcmp(msg + i, "encoding ")) {
499                         context->encoding.off = i + 9;
500                         context->encoding.len = eol - i - 9;
501                 }
502                 i = eol;
503         }
504         context->message_off = i;
505         context->commit_header_parsed = 1;
506 }
507
508 static int istitlechar(char c)
509 {
510         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
511                 (c >= '0' && c <= '9') || c == '.' || c == '_';
512 }
513
514 static void format_sanitized_subject(struct strbuf *sb, const char *msg)
515 {
516         size_t trimlen;
517         size_t start_len = sb->len;
518         int space = 2;
519
520         for (; *msg && *msg != '\n'; msg++) {
521                 if (istitlechar(*msg)) {
522                         if (space == 1)
523                                 strbuf_addch(sb, '-');
524                         space = 0;
525                         strbuf_addch(sb, *msg);
526                         if (*msg == '.')
527                                 while (*(msg+1) == '.')
528                                         msg++;
529                 } else
530                         space |= 1;
531         }
532
533         /* trim any trailing '.' or '-' characters */
534         trimlen = 0;
535         while (sb->len - trimlen > start_len &&
536                 (sb->buf[sb->len - 1 - trimlen] == '.'
537                 || sb->buf[sb->len - 1 - trimlen] == '-'))
538                 trimlen++;
539         strbuf_remove(sb, sb->len - trimlen, trimlen);
540 }
541
542 const char *format_subject(struct strbuf *sb, const char *msg,
543                            const char *line_separator)
544 {
545         int first = 1;
546
547         for (;;) {
548                 const char *line = msg;
549                 int linelen = get_one_line(line);
550
551                 msg += linelen;
552                 if (!linelen || is_empty_line(line, &linelen))
553                         break;
554
555                 if (!sb)
556                         continue;
557                 strbuf_grow(sb, linelen + 2);
558                 if (!first)
559                         strbuf_addstr(sb, line_separator);
560                 strbuf_add(sb, line, linelen);
561                 first = 0;
562         }
563         return msg;
564 }
565
566 static void parse_commit_message(struct format_commit_context *c)
567 {
568         const char *msg = c->commit->buffer + c->message_off;
569         const char *start = c->commit->buffer;
570
571         msg = skip_empty_lines(msg);
572         c->subject_off = msg - start;
573
574         msg = format_subject(NULL, msg, NULL);
575         msg = skip_empty_lines(msg);
576         c->body_off = msg - start;
577
578         c->commit_message_parsed = 1;
579 }
580
581 static void format_decoration(struct strbuf *sb, const struct commit *commit)
582 {
583         struct name_decoration *d;
584         const char *prefix = " (";
585
586         load_ref_decorations(DECORATE_SHORT_REFS);
587         d = lookup_decoration(&name_decoration, &commit->object);
588         while (d) {
589                 strbuf_addstr(sb, prefix);
590                 prefix = ", ";
591                 strbuf_addstr(sb, d->name);
592                 d = d->next;
593         }
594         if (prefix[0] == ',')
595                 strbuf_addch(sb, ')');
596 }
597
598 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
599                                void *context)
600 {
601         struct format_commit_context *c = context;
602         const struct commit *commit = c->commit;
603         const char *msg = commit->buffer;
604         struct commit_list *p;
605         int h1, h2;
606         const char *body = msg + c->body_off;
607         const char *end = NULL;
608
609         /* check if we have arguments to the placeholder */
610         if (placeholder[1] == '(')
611                 end = strchr(placeholder + 2, ')');
612
613         /* these are independent of the commit */
614         switch (placeholder[0]) {
615         case 'C':
616                 if (end) {
617                         char color[COLOR_MAXLEN];
618                         color_parse_mem(placeholder + 2,
619                                         end - (placeholder + 2),
620                                         "--pretty format", color);
621                         strbuf_addstr(sb, color);
622                         return end - placeholder + 1;
623                 }
624                 if (!prefixcmp(placeholder + 1, "red")) {
625                         strbuf_addstr(sb, GIT_COLOR_RED);
626                         return 4;
627                 } else if (!prefixcmp(placeholder + 1, "green")) {
628                         strbuf_addstr(sb, GIT_COLOR_GREEN);
629                         return 6;
630                 } else if (!prefixcmp(placeholder + 1, "blue")) {
631                         strbuf_addstr(sb, GIT_COLOR_BLUE);
632                         return 5;
633                 } else if (!prefixcmp(placeholder + 1, "reset")) {
634                         strbuf_addstr(sb, GIT_COLOR_RESET);
635                         return 6;
636                 } else
637                         return 0;
638         case 'n':               /* newline */
639                 strbuf_addch(sb, '\n');
640                 return 1;
641         case 'x':
642                 /* %x00 == NUL, %x0a == LF, etc. */
643                 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
644                     h1 <= 16 &&
645                     0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
646                     h2 <= 16) {
647                         strbuf_addch(sb, (h1<<4)|h2);
648                         return 3;
649                 } else
650                         return 0;
651         }
652
653         /* these depend on the commit */
654         if (!commit->object.parsed)
655                 parse_object(commit->object.sha1);
656
657         switch (placeholder[0]) {
658         case 'H':               /* commit hash */
659                 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
660                 return 1;
661         case 'h':               /* abbreviated commit hash */
662                 if (add_again(sb, &c->abbrev_commit_hash))
663                         return 1;
664                 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
665                                                      DEFAULT_ABBREV));
666                 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
667                 return 1;
668         case 'T':               /* tree hash */
669                 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
670                 return 1;
671         case 't':               /* abbreviated tree hash */
672                 if (add_again(sb, &c->abbrev_tree_hash))
673                         return 1;
674                 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
675                                                      DEFAULT_ABBREV));
676                 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
677                 return 1;
678         case 'P':               /* parent hashes */
679                 for (p = commit->parents; p; p = p->next) {
680                         if (p != commit->parents)
681                                 strbuf_addch(sb, ' ');
682                         strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
683                 }
684                 return 1;
685         case 'p':               /* abbreviated parent hashes */
686                 if (add_again(sb, &c->abbrev_parent_hashes))
687                         return 1;
688                 for (p = commit->parents; p; p = p->next) {
689                         if (p != commit->parents)
690                                 strbuf_addch(sb, ' ');
691                         strbuf_addstr(sb, find_unique_abbrev(
692                                         p->item->object.sha1, DEFAULT_ABBREV));
693                 }
694                 c->abbrev_parent_hashes.len = sb->len -
695                                               c->abbrev_parent_hashes.off;
696                 return 1;
697         case 'm':               /* left/right/bottom */
698                 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
699                                  ? '-'
700                                  : (commit->object.flags & SYMMETRIC_LEFT)
701                                  ? '<'
702                                  : '>');
703                 return 1;
704         case 'd':
705                 format_decoration(sb, commit);
706                 return 1;
707         }
708
709         /* For the rest we have to parse the commit header. */
710         if (!c->commit_header_parsed)
711                 parse_commit_header(c);
712
713         switch (placeholder[0]) {
714         case 'a':       /* author ... */
715                 return format_person_part(sb, placeholder[1],
716                                    msg + c->author.off, c->author.len,
717                                    c->dmode);
718         case 'c':       /* committer ... */
719                 return format_person_part(sb, placeholder[1],
720                                    msg + c->committer.off, c->committer.len,
721                                    c->dmode);
722         case 'e':       /* encoding */
723                 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
724                 return 1;
725         }
726
727         /* Now we need to parse the commit message. */
728         if (!c->commit_message_parsed)
729                 parse_commit_message(c);
730
731         switch (placeholder[0]) {
732         case 's':       /* subject */
733                 format_subject(sb, msg + c->subject_off, " ");
734                 return 1;
735         case 'f':       /* sanitized subject */
736                 format_sanitized_subject(sb, msg + c->subject_off);
737                 return 1;
738         case 'b':       /* body */
739                 strbuf_addstr(sb, body);
740                 return 1;
741         case 'B':       /* body without trailing newline */
742                 if (end) {
743                         char *endp = NULL;
744                         int indent = strtol(placeholder + 2, &endp, 10);
745                         if (placeholder + 2 == endp || *endp != ')' || indent < 0)
746                                 return 0;
747                         pp_remainder(CMIT_FMT_MEDIUM, &body, sb, indent);
748                         strbuf_rtrim(sb);
749                         return end - placeholder + 1;
750                 }
751                 strbuf_addstr(sb, body);
752                 strbuf_rtrim(sb);
753                 return 1;
754         }
755         return 0;       /* unknown placeholder */
756 }
757
758 void format_commit_message(const struct commit *commit,
759                            const void *format, struct strbuf *sb,
760                            enum date_mode dmode)
761 {
762         struct format_commit_context context;
763
764         memset(&context, 0, sizeof(context));
765         context.commit = commit;
766         context.dmode = dmode;
767         strbuf_expand(sb, format, format_commit_item, &context);
768 }
769
770 static void pp_header(enum cmit_fmt fmt,
771                       int abbrev,
772                       enum date_mode dmode,
773                       const char *encoding,
774                       const struct commit *commit,
775                       const char **msg_p,
776                       struct strbuf *sb)
777 {
778         int parents_shown = 0;
779
780         for (;;) {
781                 const char *line = *msg_p;
782                 int linelen = get_one_line(*msg_p);
783
784                 if (!linelen)
785                         return;
786                 *msg_p += linelen;
787
788                 if (linelen == 1)
789                         /* End of header */
790                         return;
791
792                 if (fmt == CMIT_FMT_RAW) {
793                         strbuf_add(sb, line, linelen);
794                         continue;
795                 }
796
797                 if (!memcmp(line, "parent ", 7)) {
798                         if (linelen != 48)
799                                 die("bad parent line in commit");
800                         continue;
801                 }
802
803                 if (!parents_shown) {
804                         struct commit_list *parent;
805                         int num;
806                         for (parent = commit->parents, num = 0;
807                              parent;
808                              parent = parent->next, num++)
809                                 ;
810                         /* with enough slop */
811                         strbuf_grow(sb, num * 50 + 20);
812                         add_merge_info(fmt, sb, commit, abbrev);
813                         parents_shown = 1;
814                 }
815
816                 /*
817                  * MEDIUM == DEFAULT shows only author with dates.
818                  * FULL shows both authors but not dates.
819                  * FULLER shows both authors and dates.
820                  */
821                 if (!memcmp(line, "author ", 7)) {
822                         strbuf_grow(sb, linelen + 80);
823                         pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
824                 }
825                 if (!memcmp(line, "committer ", 10) &&
826                     (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
827                         strbuf_grow(sb, linelen + 80);
828                         pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
829                 }
830         }
831 }
832
833 void pp_title_line(enum cmit_fmt fmt,
834                    const char **msg_p,
835                    struct strbuf *sb,
836                    const char *subject,
837                    const char *after_subject,
838                    const char *encoding,
839                    int need_8bit_cte)
840 {
841         const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
842         struct strbuf title;
843
844         strbuf_init(&title, 80);
845         *msg_p = format_subject(&title, *msg_p, line_separator);
846
847         strbuf_grow(sb, title.len + 1024);
848         if (subject) {
849                 strbuf_addstr(sb, subject);
850                 add_rfc2047(sb, title.buf, title.len, encoding);
851         } else {
852                 strbuf_addbuf(sb, &title);
853         }
854         strbuf_addch(sb, '\n');
855
856         if (need_8bit_cte > 0) {
857                 const char *header_fmt =
858                         "MIME-Version: 1.0\n"
859                         "Content-Type: text/plain; charset=%s\n"
860                         "Content-Transfer-Encoding: 8bit\n";
861                 strbuf_addf(sb, header_fmt, encoding);
862         }
863         if (after_subject) {
864                 strbuf_addstr(sb, after_subject);
865         }
866         if (fmt == CMIT_FMT_EMAIL) {
867                 strbuf_addch(sb, '\n');
868         }
869         strbuf_release(&title);
870 }
871
872 void pp_remainder(enum cmit_fmt fmt,
873                   const char **msg_p,
874                   struct strbuf *sb,
875                   int indent)
876 {
877         int first = 1;
878         for (;;) {
879                 const char *line = *msg_p;
880                 int linelen = get_one_line(line);
881                 *msg_p += linelen;
882
883                 if (!linelen)
884                         break;
885
886                 if (is_empty_line(line, &linelen)) {
887                         if (first)
888                                 continue;
889                         if (fmt == CMIT_FMT_SHORT)
890                                 break;
891                 }
892                 first = 0;
893
894                 strbuf_grow(sb, linelen + indent + 20);
895                 if (indent) {
896                         memset(sb->buf + sb->len, ' ', indent);
897                         strbuf_setlen(sb, sb->len + indent);
898                 }
899                 strbuf_add(sb, line, linelen);
900                 strbuf_addch(sb, '\n');
901         }
902 }
903
904 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
905 {
906         const char *encoding;
907
908         encoding = (git_log_output_encoding
909                     ? git_log_output_encoding
910                     : git_commit_encoding);
911         if (!encoding)
912                 encoding = "UTF-8";
913         if (encoding_p)
914                 *encoding_p = encoding;
915         return logmsg_reencode(commit, encoding);
916 }
917
918 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
919                          struct strbuf *sb, int abbrev,
920                          const char *subject, const char *after_subject,
921                          enum date_mode dmode, int need_8bit_cte)
922 {
923         unsigned long beginning_of_body;
924         int indent = 4;
925         const char *msg = commit->buffer;
926         char *reencoded;
927         const char *encoding;
928
929         if (fmt == CMIT_FMT_USERFORMAT) {
930                 format_commit_message(commit, user_format, sb, dmode);
931                 return;
932         }
933
934         reencoded = reencode_commit_message(commit, &encoding);
935         if (reencoded) {
936                 msg = reencoded;
937         }
938
939         if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
940                 indent = 0;
941
942         /*
943          * We need to check and emit Content-type: to mark it
944          * as 8-bit if we haven't done so.
945          */
946         if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
947                 int i, ch, in_body;
948
949                 for (in_body = i = 0; (ch = msg[i]); i++) {
950                         if (!in_body) {
951                                 /* author could be non 7-bit ASCII but
952                                  * the log may be so; skip over the
953                                  * header part first.
954                                  */
955                                 if (ch == '\n' && msg[i+1] == '\n')
956                                         in_body = 1;
957                         }
958                         else if (non_ascii(ch)) {
959                                 need_8bit_cte = 1;
960                                 break;
961                         }
962                 }
963         }
964
965         pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
966         if (fmt != CMIT_FMT_ONELINE && !subject) {
967                 strbuf_addch(sb, '\n');
968         }
969
970         /* Skip excess blank lines at the beginning of body, if any... */
971         msg = skip_empty_lines(msg);
972
973         /* These formats treat the title line specially. */
974         if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
975                 pp_title_line(fmt, &msg, sb, subject,
976                               after_subject, encoding, need_8bit_cte);
977
978         beginning_of_body = sb->len;
979         if (fmt != CMIT_FMT_ONELINE)
980                 pp_remainder(fmt, &msg, sb, indent);
981         strbuf_rtrim(sb);
982
983         /* Make sure there is an EOLN for the non-oneline case */
984         if (fmt != CMIT_FMT_ONELINE)
985                 strbuf_addch(sb, '\n');
986
987         /*
988          * The caller may append additional body text in e-mail
989          * format.  Make sure we did not strip the blank line
990          * between the header and the body.
991          */
992         if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
993                 strbuf_addch(sb, '\n');
994         free(reencoded);
995 }