Merge branch 'maint'
[git/git.git] / pretty.c
CommitLineData
93fc05eb
JS
1#include "cache.h"
2#include "commit.h"
93fc05eb
JS
3#include "utf8.h"
4#include "diff.h"
5#include "revision.h"
c455c87c 6#include "string-list.h"
e0cbc397 7#include "mailmap.h"
3b3d443f 8#include "log-tree.h"
93fc05eb 9
93fc05eb
JS
10static char *user_format;
11
4da45bef 12void get_commit_format(const char *arg, struct rev_info *rev)
93fc05eb
JS
13{
14 int i;
4da45bef
JH
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 };
93fc05eb 28
4da45bef
JH
29 rev->use_terminator = 0;
30 if (!arg || !*arg) {
31 rev->commit_format = CMIT_FMT_DEFAULT;
32 return;
33 }
4da45bef
JH
34 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
35 const char *cp = strchr(arg, ':') + 1;
8e0f7003 36 free(user_format);
4da45bef
JH
37 user_format = xstrdup(cp);
38 if (arg[0] == 't')
39 rev->use_terminator = 1;
40 rev->commit_format = CMIT_FMT_USERFORMAT;
41 return;
93fc05eb
JS
42 }
43 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
44 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
4da45bef
JH
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 }
93fc05eb
JS
51 }
52
53 die("invalid --pretty format: %s", arg);
54}
55
56/*
57 * Generic support for pretty-printing the header
58 */
59static int get_one_line(const char *msg)
60{
61 int ret = 0;
62
63 for (;;) {
64 char c = *msg++;
65 if (!c)
66 break;
67 ret++;
68 if (c == '\n')
69 break;
70 }
71 return ret;
72}
73
74/* High bit set, or ISO-2022-INT */
75int non_ascii(int ch)
76{
77 ch = (ch & 0xff);
78 return ((ch & 0x80) || (ch == 0x1b));
79}
80
81static int is_rfc2047_special(char ch)
82{
83 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
84}
85
86static void add_rfc2047(struct strbuf *sb, const char *line, int len,
87 const char *encoding)
88{
89 int i, last;
90
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;
100
101needquote:
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, "?=");
120}
121
b02bd65f
DB
122void 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)
93fc05eb
JS
125{
126 char *date;
127 int namelen;
128 unsigned long time;
129 int tz;
130 const char *filler = " ";
131
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);
140
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 }
173}
174
175static int is_empty_line(const char *line, int *len_p)
176{
177 int len = *len_p;
178 while (len && isspace(line[len-1]))
179 len--;
180 *len_p = len;
181 return !len;
182}
183
184static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
185 const struct commit *commit, int abbrev)
186{
187 struct commit_list *parent = commit->parents;
188
189 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
190 !parent || !parent->next)
191 return;
192
193 strbuf_addstr(sb, "Merge:");
194
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;
205
206 strbuf_addf(sb, " %s%s", hex, dots);
207 }
208 strbuf_addch(sb, '\n');
209}
210
211static char *get_header(const struct commit *commit, const char *key)
212{
213 int key_len = strlen(key);
214 const char *line = commit->buffer;
215
216 for (;;) {
217 const char *eol = strchr(line, '\n'), *next;
218
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);
230 }
231 line = next;
232 }
233}
234
235static char *replace_encoding_header(char *buf, const char *encoding)
236{
f285a2d7 237 struct strbuf tmp = STRBUF_INIT;
93fc05eb
JS
238 size_t start, len;
239 char *cp = buf;
240
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;
246 }
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);
252
93fc05eb
JS
253 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
254 if (is_encoding_utf8(encoding)) {
255 /* we have re-coded to UTF-8; drop the header */
256 strbuf_remove(&tmp, start, len);
257 } else {
258 /* just replaces XXXX in 'encoding XXXX\n' */
259 strbuf_splice(&tmp, start + strlen("encoding "),
260 len - strlen("encoding \n"),
261 encoding, strlen(encoding));
262 }
263 return strbuf_detach(&tmp, NULL);
264}
265
266static char *logmsg_reencode(const struct commit *commit,
267 const char *output_encoding)
268{
269 static const char *utf8 = "utf-8";
270 const char *use_encoding;
271 char *encoding;
272 char *out;
273
274 if (!*output_encoding)
275 return NULL;
276 encoding = get_header(commit, "encoding");
277 use_encoding = encoding ? encoding : utf8;
278 if (!strcmp(use_encoding, output_encoding))
279 if (encoding) /* we'll strip encoding header later */
280 out = xstrdup(commit->buffer);
281 else
282 return NULL; /* nothing to do */
283 else
284 out = reencode_string(commit->buffer,
285 output_encoding, use_encoding);
286 if (out)
287 out = replace_encoding_header(out, output_encoding);
288
289 free(encoding);
290 return out;
291}
292
e0cbc397
JS
293static int mailmap_name(struct strbuf *sb, const char *email)
294{
c455c87c 295 static struct string_list *mail_map;
e0cbc397
JS
296 char buffer[1024];
297
298 if (!mail_map) {
299 mail_map = xcalloc(1, sizeof(*mail_map));
300 read_mailmap(mail_map, ".mailmap", NULL);
301 }
302
303 if (!mail_map->nr)
304 return -1;
305
306 if (!map_email(mail_map, email, buffer, sizeof(buffer)))
307 return -1;
308 strbuf_addstr(sb, buffer);
309 return 0;
310}
311
c3a670de 312static size_t format_person_part(struct strbuf *sb, char part,
d36f8679 313 const char *msg, int len, enum date_mode dmode)
93fc05eb 314{
c3a670de
MC
315 /* currently all placeholders have same length */
316 const int placeholder_len = 2;
93fc05eb 317 int start, end, tz = 0;
c3a670de 318 unsigned long date = 0;
93fc05eb
JS
319 char *ep;
320
c3a670de 321 /* advance 'end' to point to email start delimiter */
93fc05eb
JS
322 for (end = 0; end < len && msg[end] != '<'; end++)
323 ; /* do nothing */
c3a670de 324
f7ab5c79 325 /*
c3a670de
MC
326 * When end points at the '<' that we found, it should have
327 * matching '>' later, which means 'end' must be strictly
328 * below len - 1.
f7ab5c79 329 */
c3a670de
MC
330 if (end >= len - 2)
331 goto skip;
332
e0cbc397 333 if (part == 'n' || part == 'N') { /* name */
c3a670de
MC
334 while (end > 0 && isspace(msg[end - 1]))
335 end--;
e0cbc397
JS
336 if (part != 'N' || !msg[end] || !msg[end + 1] ||
337 mailmap_name(sb, msg + end + 2) < 0)
338 strbuf_add(sb, msg, end);
c3a670de 339 return placeholder_len;
cde75e59 340 }
c3a670de 341 start = ++end; /* save email start position */
93fc05eb 342
c3a670de
MC
343 /* advance 'end' to point to email end delimiter */
344 for ( ; end < len && msg[end] != '>'; end++)
93fc05eb
JS
345 ; /* do nothing */
346
347 if (end >= len)
c3a670de 348 goto skip;
93fc05eb 349
cde75e59
RS
350 if (part == 'e') { /* email */
351 strbuf_add(sb, msg + start, end - start);
c3a670de 352 return placeholder_len;
cde75e59 353 }
93fc05eb 354
c3a670de 355 /* advance 'start' to point to date start delimiter */
93fc05eb
JS
356 for (start = end + 1; start < len && isspace(msg[start]); start++)
357 ; /* do nothing */
358 if (start >= len)
c3a670de 359 goto skip;
93fc05eb
JS
360 date = strtoul(msg + start, &ep, 10);
361 if (msg + start == ep)
c3a670de 362 goto skip;
93fc05eb 363
cde75e59
RS
364 if (part == 't') { /* date, UNIX timestamp */
365 strbuf_add(sb, msg + start, ep - (msg + start));
c3a670de 366 return placeholder_len;
cde75e59 367 }
93fc05eb
JS
368
369 /* parse tz */
370 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
371 ; /* do nothing */
372 if (start + 1 < len) {
373 tz = strtoul(msg + start + 1, NULL, 10);
374 if (msg[start] == '-')
375 tz = -tz;
376 }
377
cde75e59
RS
378 switch (part) {
379 case 'd': /* date */
d36f8679 380 strbuf_addstr(sb, show_date(date, tz, dmode));
c3a670de 381 return placeholder_len;
cde75e59
RS
382 case 'D': /* date, RFC2822 style */
383 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
c3a670de 384 return placeholder_len;
cde75e59
RS
385 case 'r': /* date, relative */
386 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
c3a670de 387 return placeholder_len;
cde75e59
RS
388 case 'i': /* date, ISO 8601 */
389 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
c3a670de 390 return placeholder_len;
cde75e59 391 }
c3a670de
MC
392
393skip:
394 /*
395 * bogus commit, 'sb' cannot be updated, but we still need to
396 * compute a valid return value.
397 */
398 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
399 || part == 'D' || part == 'r' || part == 'i')
400 return placeholder_len;
401
402 return 0; /* unknown placeholder */
93fc05eb
JS
403}
404
f29d5958
RS
405struct chunk {
406 size_t off;
407 size_t len;
408};
409
410struct format_commit_context {
411 const struct commit *commit;
d36f8679 412 enum date_mode dmode;
f29d5958
RS
413
414 /* These offsets are relative to the start of the commit message. */
415 int commit_header_parsed;
416 struct chunk subject;
417 struct chunk author;
418 struct chunk committer;
419 struct chunk encoding;
420 size_t body_off;
b9c62321
RS
421
422 /* The following ones are relative to the result struct strbuf. */
423 struct chunk abbrev_commit_hash;
424 struct chunk abbrev_tree_hash;
425 struct chunk abbrev_parent_hashes;
f29d5958
RS
426};
427
b9c62321
RS
428static int add_again(struct strbuf *sb, struct chunk *chunk)
429{
430 if (chunk->len) {
431 strbuf_adddup(sb, chunk->off, chunk->len);
432 return 1;
433 }
434
435 /*
436 * We haven't seen this chunk before. Our caller is surely
437 * going to add it the hard way now. Remember the most likely
438 * start of the to-be-added chunk: the current end of the
439 * struct strbuf.
440 */
441 chunk->off = sb->len;
442 return 0;
443}
444
f29d5958 445static void parse_commit_header(struct format_commit_context *context)
93fc05eb 446{
f29d5958 447 const char *msg = context->commit->buffer;
93fc05eb
JS
448 int i;
449 enum { HEADER, SUBJECT, BODY } state;
f29d5958
RS
450
451 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
452 int eol;
453 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
454 ; /* do nothing */
455
456 if (state == SUBJECT) {
457 context->subject.off = i;
458 context->subject.len = eol - i;
459 i = eol;
460 }
461 if (i == eol) {
462 state++;
463 /* strip empty lines */
7ed0988a 464 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
f29d5958
RS
465 eol++;
466 } else if (!prefixcmp(msg + i, "author ")) {
467 context->author.off = i + 7;
468 context->author.len = eol - i - 7;
469 } else if (!prefixcmp(msg + i, "committer ")) {
470 context->committer.off = i + 10;
471 context->committer.len = eol - i - 10;
472 } else if (!prefixcmp(msg + i, "encoding ")) {
473 context->encoding.off = i + 9;
474 context->encoding.len = eol - i - 9;
475 }
476 i = eol;
7ed0988a
RS
477 if (!msg[i])
478 break;
f29d5958
RS
479 }
480 context->body_off = i;
481 context->commit_header_parsed = 1;
482}
483
3b3d443f
RS
484static void format_decoration(struct strbuf *sb, const struct commit *commit)
485{
486 struct name_decoration *d;
487 const char *prefix = " (";
488
489 load_ref_decorations();
490 d = lookup_decoration(&name_decoration, &commit->object);
491 while (d) {
492 strbuf_addstr(sb, prefix);
493 prefix = ", ";
494 strbuf_addstr(sb, d->name);
495 d = d->next;
496 }
497 if (prefix[0] == ',')
498 strbuf_addch(sb, ')');
499}
500
c3a670de 501static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
f29d5958
RS
502 void *context)
503{
504 struct format_commit_context *c = context;
505 const struct commit *commit = c->commit;
93fc05eb 506 const char *msg = commit->buffer;
f29d5958 507 struct commit_list *p;
42c8c74c 508 int h1, h2;
93fc05eb 509
93fc05eb 510 /* these are independent of the commit */
cde75e59
RS
511 switch (placeholder[0]) {
512 case 'C':
c3a670de 513 if (!prefixcmp(placeholder + 1, "red")) {
cde75e59 514 strbuf_addstr(sb, "\033[31m");
c3a670de
MC
515 return 4;
516 } else if (!prefixcmp(placeholder + 1, "green")) {
cde75e59 517 strbuf_addstr(sb, "\033[32m");
c3a670de
MC
518 return 6;
519 } else if (!prefixcmp(placeholder + 1, "blue")) {
cde75e59 520 strbuf_addstr(sb, "\033[34m");
c3a670de
MC
521 return 5;
522 } else if (!prefixcmp(placeholder + 1, "reset")) {
cde75e59 523 strbuf_addstr(sb, "\033[m");
c3a670de
MC
524 return 6;
525 } else
526 return 0;
cde75e59
RS
527 case 'n': /* newline */
528 strbuf_addch(sb, '\n');
c3a670de 529 return 1;
42c8c74c
GS
530 case 'x':
531 /* %x00 == NUL, %x0a == LF, etc. */
532 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
533 h1 <= 16 &&
534 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
535 h2 <= 16) {
536 strbuf_addch(sb, (h1<<4)|h2);
537 return 3;
538 } else
539 return 0;
cde75e59 540 }
93fc05eb
JS
541
542 /* these depend on the commit */
543 if (!commit->object.parsed)
544 parse_object(commit->object.sha1);
93fc05eb 545
cde75e59
RS
546 switch (placeholder[0]) {
547 case 'H': /* commit hash */
548 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
c3a670de 549 return 1;
cde75e59 550 case 'h': /* abbreviated commit hash */
b9c62321 551 if (add_again(sb, &c->abbrev_commit_hash))
c3a670de 552 return 1;
cde75e59
RS
553 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
554 DEFAULT_ABBREV));
b9c62321 555 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
c3a670de 556 return 1;
cde75e59
RS
557 case 'T': /* tree hash */
558 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
c3a670de 559 return 1;
cde75e59 560 case 't': /* abbreviated tree hash */
b9c62321 561 if (add_again(sb, &c->abbrev_tree_hash))
c3a670de 562 return 1;
cde75e59
RS
563 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
564 DEFAULT_ABBREV));
b9c62321 565 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
c3a670de 566 return 1;
cde75e59
RS
567 case 'P': /* parent hashes */
568 for (p = commit->parents; p; p = p->next) {
569 if (p != commit->parents)
570 strbuf_addch(sb, ' ');
571 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
572 }
c3a670de 573 return 1;
cde75e59 574 case 'p': /* abbreviated parent hashes */
b9c62321 575 if (add_again(sb, &c->abbrev_parent_hashes))
c3a670de 576 return 1;
cde75e59
RS
577 for (p = commit->parents; p; p = p->next) {
578 if (p != commit->parents)
579 strbuf_addch(sb, ' ');
580 strbuf_addstr(sb, find_unique_abbrev(
581 p->item->object.sha1, DEFAULT_ABBREV));
582 }
b9c62321
RS
583 c->abbrev_parent_hashes.len = sb->len -
584 c->abbrev_parent_hashes.off;
c3a670de 585 return 1;
cde75e59
RS
586 case 'm': /* left/right/bottom */
587 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
588 ? '-'
589 : (commit->object.flags & SYMMETRIC_LEFT)
590 ? '<'
591 : '>');
c3a670de 592 return 1;
3b3d443f
RS
593 case 'd':
594 format_decoration(sb, commit);
595 return 1;
cde75e59
RS
596 }
597
598 /* For the rest we have to parse the commit header. */
f29d5958
RS
599 if (!c->commit_header_parsed)
600 parse_commit_header(c);
93fc05eb 601
f29d5958 602 switch (placeholder[0]) {
c3a670de 603 case 's': /* subject */
f29d5958 604 strbuf_add(sb, msg + c->subject.off, c->subject.len);
c3a670de
MC
605 return 1;
606 case 'a': /* author ... */
607 return format_person_part(sb, placeholder[1],
d36f8679
JK
608 msg + c->author.off, c->author.len,
609 c->dmode);
c3a670de
MC
610 case 'c': /* committer ... */
611 return format_person_part(sb, placeholder[1],
d36f8679
JK
612 msg + c->committer.off, c->committer.len,
613 c->dmode);
c3a670de 614 case 'e': /* encoding */
f29d5958 615 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
c3a670de
MC
616 return 1;
617 case 'b': /* body */
f29d5958 618 strbuf_addstr(sb, msg + c->body_off);
c3a670de 619 return 1;
93fc05eb 620 }
c3a670de 621 return 0; /* unknown placeholder */
cde75e59
RS
622}
623
624void format_commit_message(const struct commit *commit,
d36f8679
JK
625 const void *format, struct strbuf *sb,
626 enum date_mode dmode)
cde75e59 627{
f29d5958
RS
628 struct format_commit_context context;
629
630 memset(&context, 0, sizeof(context));
631 context.commit = commit;
d36f8679 632 context.dmode = dmode;
c3a670de 633 strbuf_expand(sb, format, format_commit_item, &context);
93fc05eb
JS
634}
635
636static void pp_header(enum cmit_fmt fmt,
637 int abbrev,
638 enum date_mode dmode,
639 const char *encoding,
640 const struct commit *commit,
641 const char **msg_p,
642 struct strbuf *sb)
643{
644 int parents_shown = 0;
645
646 for (;;) {
647 const char *line = *msg_p;
648 int linelen = get_one_line(*msg_p);
649
650 if (!linelen)
651 return;
652 *msg_p += linelen;
653
654 if (linelen == 1)
655 /* End of header */
656 return;
657
658 if (fmt == CMIT_FMT_RAW) {
659 strbuf_add(sb, line, linelen);
660 continue;
661 }
662
663 if (!memcmp(line, "parent ", 7)) {
664 if (linelen != 48)
665 die("bad parent line in commit");
666 continue;
667 }
668
669 if (!parents_shown) {
670 struct commit_list *parent;
671 int num;
672 for (parent = commit->parents, num = 0;
673 parent;
674 parent = parent->next, num++)
675 ;
676 /* with enough slop */
677 strbuf_grow(sb, num * 50 + 20);
678 add_merge_info(fmt, sb, commit, abbrev);
679 parents_shown = 1;
680 }
681
682 /*
683 * MEDIUM == DEFAULT shows only author with dates.
684 * FULL shows both authors but not dates.
685 * FULLER shows both authors and dates.
686 */
687 if (!memcmp(line, "author ", 7)) {
688 strbuf_grow(sb, linelen + 80);
b02bd65f 689 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
93fc05eb
JS
690 }
691 if (!memcmp(line, "committer ", 10) &&
692 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
693 strbuf_grow(sb, linelen + 80);
b02bd65f 694 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
93fc05eb
JS
695 }
696 }
697}
698
b02bd65f
DB
699void pp_title_line(enum cmit_fmt fmt,
700 const char **msg_p,
701 struct strbuf *sb,
702 const char *subject,
703 const char *after_subject,
704 const char *encoding,
267123b4 705 int need_8bit_cte)
93fc05eb
JS
706{
707 struct strbuf title;
708
709 strbuf_init(&title, 80);
710
711 for (;;) {
712 const char *line = *msg_p;
713 int linelen = get_one_line(line);
714
715 *msg_p += linelen;
716 if (!linelen || is_empty_line(line, &linelen))
717 break;
718
719 strbuf_grow(&title, linelen + 2);
720 if (title.len) {
721 if (fmt == CMIT_FMT_EMAIL) {
722 strbuf_addch(&title, '\n');
723 }
724 strbuf_addch(&title, ' ');
725 }
726 strbuf_add(&title, line, linelen);
727 }
728
729 strbuf_grow(sb, title.len + 1024);
730 if (subject) {
731 strbuf_addstr(sb, subject);
732 add_rfc2047(sb, title.buf, title.len, encoding);
733 } else {
734 strbuf_addbuf(sb, &title);
735 }
736 strbuf_addch(sb, '\n');
737
6bf4f1b4 738 if (need_8bit_cte > 0) {
93fc05eb
JS
739 const char *header_fmt =
740 "MIME-Version: 1.0\n"
741 "Content-Type: text/plain; charset=%s\n"
742 "Content-Transfer-Encoding: 8bit\n";
743 strbuf_addf(sb, header_fmt, encoding);
744 }
745 if (after_subject) {
746 strbuf_addstr(sb, after_subject);
747 }
748 if (fmt == CMIT_FMT_EMAIL) {
749 strbuf_addch(sb, '\n');
750 }
751 strbuf_release(&title);
752}
753
b02bd65f
DB
754void pp_remainder(enum cmit_fmt fmt,
755 const char **msg_p,
756 struct strbuf *sb,
757 int indent)
93fc05eb
JS
758{
759 int first = 1;
760 for (;;) {
761 const char *line = *msg_p;
762 int linelen = get_one_line(line);
763 *msg_p += linelen;
764
765 if (!linelen)
766 break;
767
768 if (is_empty_line(line, &linelen)) {
769 if (first)
770 continue;
771 if (fmt == CMIT_FMT_SHORT)
772 break;
773 }
774 first = 0;
775
776 strbuf_grow(sb, linelen + indent + 20);
777 if (indent) {
778 memset(sb->buf + sb->len, ' ', indent);
779 strbuf_setlen(sb, sb->len + indent);
780 }
781 strbuf_add(sb, line, linelen);
782 strbuf_addch(sb, '\n');
783 }
784}
785
786void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
6bf4f1b4
JH
787 struct strbuf *sb, int abbrev,
788 const char *subject, const char *after_subject,
789 enum date_mode dmode, int need_8bit_cte)
93fc05eb
JS
790{
791 unsigned long beginning_of_body;
792 int indent = 4;
793 const char *msg = commit->buffer;
794 char *reencoded;
795 const char *encoding;
796
797 if (fmt == CMIT_FMT_USERFORMAT) {
d36f8679 798 format_commit_message(commit, user_format, sb, dmode);
93fc05eb
JS
799 return;
800 }
801
802 encoding = (git_log_output_encoding
803 ? git_log_output_encoding
804 : git_commit_encoding);
805 if (!encoding)
806 encoding = "utf-8";
807 reencoded = logmsg_reencode(commit, encoding);
808 if (reencoded) {
809 msg = reencoded;
810 }
811
812 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
813 indent = 0;
814
6bf4f1b4
JH
815 /*
816 * We need to check and emit Content-type: to mark it
817 * as 8-bit if we haven't done so.
93fc05eb 818 */
6bf4f1b4 819 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
93fc05eb
JS
820 int i, ch, in_body;
821
822 for (in_body = i = 0; (ch = msg[i]); i++) {
823 if (!in_body) {
824 /* author could be non 7-bit ASCII but
825 * the log may be so; skip over the
826 * header part first.
827 */
828 if (ch == '\n' && msg[i+1] == '\n')
829 in_body = 1;
830 }
831 else if (non_ascii(ch)) {
6bf4f1b4 832 need_8bit_cte = 1;
93fc05eb
JS
833 break;
834 }
835 }
836 }
837
838 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
839 if (fmt != CMIT_FMT_ONELINE && !subject) {
840 strbuf_addch(sb, '\n');
841 }
842
843 /* Skip excess blank lines at the beginning of body, if any... */
844 for (;;) {
845 int linelen = get_one_line(msg);
846 int ll = linelen;
847 if (!linelen)
848 break;
849 if (!is_empty_line(msg, &ll))
850 break;
851 msg += linelen;
852 }
853
854 /* These formats treat the title line specially. */
855 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
856 pp_title_line(fmt, &msg, sb, subject,
6bf4f1b4 857 after_subject, encoding, need_8bit_cte);
93fc05eb
JS
858
859 beginning_of_body = sb->len;
860 if (fmt != CMIT_FMT_ONELINE)
861 pp_remainder(fmt, &msg, sb, indent);
862 strbuf_rtrim(sb);
863
864 /* Make sure there is an EOLN for the non-oneline case */
865 if (fmt != CMIT_FMT_ONELINE)
866 strbuf_addch(sb, '\n');
867
868 /*
869 * The caller may append additional body text in e-mail
870 * format. Make sure we did not strip the blank line
871 * between the header and the body.
872 */
873 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
874 strbuf_addch(sb, '\n');
875 free(reencoded);
876}