pretty: factor out format_subject()
[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
a0109668
RS
184static const char *skip_empty_lines(const char *msg)
185{
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;
196}
197
93fc05eb
JS
198static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
199 const struct commit *commit, int abbrev)
200{
201 struct commit_list *parent = commit->parents;
202
203 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
204 !parent || !parent->next)
205 return;
206
207 strbuf_addstr(sb, "Merge:");
208
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;
219
220 strbuf_addf(sb, " %s%s", hex, dots);
221 }
222 strbuf_addch(sb, '\n');
223}
224
225static char *get_header(const struct commit *commit, const char *key)
226{
227 int key_len = strlen(key);
228 const char *line = commit->buffer;
229
230 for (;;) {
231 const char *eol = strchr(line, '\n'), *next;
232
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 }
247}
248
249static char *replace_encoding_header(char *buf, const char *encoding)
250{
f285a2d7 251 struct strbuf tmp = STRBUF_INIT;
93fc05eb
JS
252 size_t start, len;
253 char *cp = buf;
254
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);
266
93fc05eb
JS
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);
278}
279
280static char *logmsg_reencode(const struct commit *commit,
281 const char *output_encoding)
282{
283 static const char *utf8 = "utf-8";
284 const char *use_encoding;
285 char *encoding;
286 char *out;
287
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);
302
303 free(encoding);
304 return out;
305}
306
e0cbc397
JS
307static int mailmap_name(struct strbuf *sb, const char *email)
308{
c455c87c 309 static struct string_list *mail_map;
e0cbc397
JS
310 char buffer[1024];
311
312 if (!mail_map) {
313 mail_map = xcalloc(1, sizeof(*mail_map));
314 read_mailmap(mail_map, ".mailmap", NULL);
315 }
316
317 if (!mail_map->nr)
318 return -1;
319
320 if (!map_email(mail_map, email, buffer, sizeof(buffer)))
321 return -1;
322 strbuf_addstr(sb, buffer);
323 return 0;
324}
325
c3a670de 326static size_t format_person_part(struct strbuf *sb, char part,
d36f8679 327 const char *msg, int len, enum date_mode dmode)
93fc05eb 328{
c3a670de
MC
329 /* currently all placeholders have same length */
330 const int placeholder_len = 2;
93fc05eb 331 int start, end, tz = 0;
c3a670de 332 unsigned long date = 0;
93fc05eb
JS
333 char *ep;
334
c3a670de 335 /* advance 'end' to point to email start delimiter */
93fc05eb
JS
336 for (end = 0; end < len && msg[end] != '<'; end++)
337 ; /* do nothing */
c3a670de 338
f7ab5c79 339 /*
c3a670de
MC
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.
f7ab5c79 343 */
c3a670de
MC
344 if (end >= len - 2)
345 goto skip;
346
e0cbc397 347 if (part == 'n' || part == 'N') { /* name */
c3a670de
MC
348 while (end > 0 && isspace(msg[end - 1]))
349 end--;
e0cbc397
JS
350 if (part != 'N' || !msg[end] || !msg[end + 1] ||
351 mailmap_name(sb, msg + end + 2) < 0)
352 strbuf_add(sb, msg, end);
c3a670de 353 return placeholder_len;
cde75e59 354 }
c3a670de 355 start = ++end; /* save email start position */
93fc05eb 356
c3a670de
MC
357 /* advance 'end' to point to email end delimiter */
358 for ( ; end < len && msg[end] != '>'; end++)
93fc05eb
JS
359 ; /* do nothing */
360
361 if (end >= len)
c3a670de 362 goto skip;
93fc05eb 363
cde75e59
RS
364 if (part == 'e') { /* email */
365 strbuf_add(sb, msg + start, end - start);
c3a670de 366 return placeholder_len;
cde75e59 367 }
93fc05eb 368
c3a670de 369 /* advance 'start' to point to date start delimiter */
93fc05eb
JS
370 for (start = end + 1; start < len && isspace(msg[start]); start++)
371 ; /* do nothing */
372 if (start >= len)
c3a670de 373 goto skip;
93fc05eb
JS
374 date = strtoul(msg + start, &ep, 10);
375 if (msg + start == ep)
c3a670de 376 goto skip;
93fc05eb 377
cde75e59
RS
378 if (part == 't') { /* date, UNIX timestamp */
379 strbuf_add(sb, msg + start, ep - (msg + start));
c3a670de 380 return placeholder_len;
cde75e59 381 }
93fc05eb
JS
382
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 }
391
cde75e59
RS
392 switch (part) {
393 case 'd': /* date */
d36f8679 394 strbuf_addstr(sb, show_date(date, tz, dmode));
c3a670de 395 return placeholder_len;
cde75e59
RS
396 case 'D': /* date, RFC2822 style */
397 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
c3a670de 398 return placeholder_len;
cde75e59
RS
399 case 'r': /* date, relative */
400 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
c3a670de 401 return placeholder_len;
cde75e59
RS
402 case 'i': /* date, ISO 8601 */
403 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
c3a670de 404 return placeholder_len;
cde75e59 405 }
c3a670de
MC
406
407skip:
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;
415
416 return 0; /* unknown placeholder */
93fc05eb
JS
417}
418
f29d5958
RS
419struct chunk {
420 size_t off;
421 size_t len;
422};
423
424struct format_commit_context {
425 const struct commit *commit;
d36f8679 426 enum date_mode dmode;
f29d5958
RS
427
428 /* These offsets are relative to the start of the commit message. */
429 int commit_header_parsed;
430 struct chunk subject;
431 struct chunk author;
432 struct chunk committer;
433 struct chunk encoding;
434 size_t body_off;
b9c62321
RS
435
436 /* The following ones are relative to the result struct strbuf. */
437 struct chunk abbrev_commit_hash;
438 struct chunk abbrev_tree_hash;
439 struct chunk abbrev_parent_hashes;
f29d5958
RS
440};
441
b9c62321
RS
442static int add_again(struct strbuf *sb, struct chunk *chunk)
443{
444 if (chunk->len) {
445 strbuf_adddup(sb, chunk->off, chunk->len);
446 return 1;
447 }
448
449 /*
450 * We haven't seen this chunk before. Our caller is surely
451 * going to add it the hard way now. Remember the most likely
452 * start of the to-be-added chunk: the current end of the
453 * struct strbuf.
454 */
455 chunk->off = sb->len;
456 return 0;
457}
458
f29d5958 459static void parse_commit_header(struct format_commit_context *context)
93fc05eb 460{
f29d5958 461 const char *msg = context->commit->buffer;
93fc05eb
JS
462 int i;
463 enum { HEADER, SUBJECT, BODY } state;
f29d5958
RS
464
465 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
466 int eol;
467 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
468 ; /* do nothing */
469
470 if (state == SUBJECT) {
471 context->subject.off = i;
472 context->subject.len = eol - i;
473 i = eol;
474 }
475 if (i == eol) {
476 state++;
477 /* strip empty lines */
7ed0988a 478 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
f29d5958
RS
479 eol++;
480 } else if (!prefixcmp(msg + i, "author ")) {
481 context->author.off = i + 7;
482 context->author.len = eol - i - 7;
483 } else if (!prefixcmp(msg + i, "committer ")) {
484 context->committer.off = i + 10;
485 context->committer.len = eol - i - 10;
486 } else if (!prefixcmp(msg + i, "encoding ")) {
487 context->encoding.off = i + 9;
488 context->encoding.len = eol - i - 9;
489 }
490 i = eol;
7ed0988a
RS
491 if (!msg[i])
492 break;
f29d5958
RS
493 }
494 context->body_off = i;
495 context->commit_header_parsed = 1;
496}
497
88c44735
RS
498static const char *format_subject(struct strbuf *sb, const char *msg,
499 const char *line_separator)
500{
501 int first = 1;
502
503 for (;;) {
504 const char *line = msg;
505 int linelen = get_one_line(line);
506
507 msg += linelen;
508 if (!linelen || is_empty_line(line, &linelen))
509 break;
510
511 strbuf_grow(sb, linelen + 2);
512 if (!first)
513 strbuf_addstr(sb, line_separator);
514 strbuf_add(sb, line, linelen);
515 first = 0;
516 }
517 return msg;
518}
519
3b3d443f
RS
520static void format_decoration(struct strbuf *sb, const struct commit *commit)
521{
522 struct name_decoration *d;
523 const char *prefix = " (";
524
525 load_ref_decorations();
526 d = lookup_decoration(&name_decoration, &commit->object);
527 while (d) {
528 strbuf_addstr(sb, prefix);
529 prefix = ", ";
530 strbuf_addstr(sb, d->name);
531 d = d->next;
532 }
533 if (prefix[0] == ',')
534 strbuf_addch(sb, ')');
535}
536
c3a670de 537static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
f29d5958
RS
538 void *context)
539{
540 struct format_commit_context *c = context;
541 const struct commit *commit = c->commit;
93fc05eb 542 const char *msg = commit->buffer;
f29d5958 543 struct commit_list *p;
42c8c74c 544 int h1, h2;
93fc05eb 545
93fc05eb 546 /* these are independent of the commit */
cde75e59
RS
547 switch (placeholder[0]) {
548 case 'C':
c3a670de 549 if (!prefixcmp(placeholder + 1, "red")) {
cde75e59 550 strbuf_addstr(sb, "\033[31m");
c3a670de
MC
551 return 4;
552 } else if (!prefixcmp(placeholder + 1, "green")) {
cde75e59 553 strbuf_addstr(sb, "\033[32m");
c3a670de
MC
554 return 6;
555 } else if (!prefixcmp(placeholder + 1, "blue")) {
cde75e59 556 strbuf_addstr(sb, "\033[34m");
c3a670de
MC
557 return 5;
558 } else if (!prefixcmp(placeholder + 1, "reset")) {
cde75e59 559 strbuf_addstr(sb, "\033[m");
c3a670de
MC
560 return 6;
561 } else
562 return 0;
cde75e59
RS
563 case 'n': /* newline */
564 strbuf_addch(sb, '\n');
c3a670de 565 return 1;
42c8c74c
GS
566 case 'x':
567 /* %x00 == NUL, %x0a == LF, etc. */
568 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
569 h1 <= 16 &&
570 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
571 h2 <= 16) {
572 strbuf_addch(sb, (h1<<4)|h2);
573 return 3;
574 } else
575 return 0;
cde75e59 576 }
93fc05eb
JS
577
578 /* these depend on the commit */
579 if (!commit->object.parsed)
580 parse_object(commit->object.sha1);
93fc05eb 581
cde75e59
RS
582 switch (placeholder[0]) {
583 case 'H': /* commit hash */
584 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
c3a670de 585 return 1;
cde75e59 586 case 'h': /* abbreviated commit hash */
b9c62321 587 if (add_again(sb, &c->abbrev_commit_hash))
c3a670de 588 return 1;
cde75e59
RS
589 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
590 DEFAULT_ABBREV));
b9c62321 591 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
c3a670de 592 return 1;
cde75e59
RS
593 case 'T': /* tree hash */
594 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
c3a670de 595 return 1;
cde75e59 596 case 't': /* abbreviated tree hash */
b9c62321 597 if (add_again(sb, &c->abbrev_tree_hash))
c3a670de 598 return 1;
cde75e59
RS
599 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
600 DEFAULT_ABBREV));
b9c62321 601 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
c3a670de 602 return 1;
cde75e59
RS
603 case 'P': /* parent hashes */
604 for (p = commit->parents; p; p = p->next) {
605 if (p != commit->parents)
606 strbuf_addch(sb, ' ');
607 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
608 }
c3a670de 609 return 1;
cde75e59 610 case 'p': /* abbreviated parent hashes */
b9c62321 611 if (add_again(sb, &c->abbrev_parent_hashes))
c3a670de 612 return 1;
cde75e59
RS
613 for (p = commit->parents; p; p = p->next) {
614 if (p != commit->parents)
615 strbuf_addch(sb, ' ');
616 strbuf_addstr(sb, find_unique_abbrev(
617 p->item->object.sha1, DEFAULT_ABBREV));
618 }
b9c62321
RS
619 c->abbrev_parent_hashes.len = sb->len -
620 c->abbrev_parent_hashes.off;
c3a670de 621 return 1;
cde75e59
RS
622 case 'm': /* left/right/bottom */
623 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
624 ? '-'
625 : (commit->object.flags & SYMMETRIC_LEFT)
626 ? '<'
627 : '>');
c3a670de 628 return 1;
3b3d443f
RS
629 case 'd':
630 format_decoration(sb, commit);
631 return 1;
cde75e59
RS
632 }
633
634 /* For the rest we have to parse the commit header. */
f29d5958
RS
635 if (!c->commit_header_parsed)
636 parse_commit_header(c);
93fc05eb 637
f29d5958 638 switch (placeholder[0]) {
c3a670de 639 case 's': /* subject */
f29d5958 640 strbuf_add(sb, msg + c->subject.off, c->subject.len);
c3a670de
MC
641 return 1;
642 case 'a': /* author ... */
643 return format_person_part(sb, placeholder[1],
d36f8679
JK
644 msg + c->author.off, c->author.len,
645 c->dmode);
c3a670de
MC
646 case 'c': /* committer ... */
647 return format_person_part(sb, placeholder[1],
d36f8679
JK
648 msg + c->committer.off, c->committer.len,
649 c->dmode);
c3a670de 650 case 'e': /* encoding */
f29d5958 651 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
c3a670de
MC
652 return 1;
653 case 'b': /* body */
f29d5958 654 strbuf_addstr(sb, msg + c->body_off);
c3a670de 655 return 1;
93fc05eb 656 }
c3a670de 657 return 0; /* unknown placeholder */
cde75e59
RS
658}
659
660void format_commit_message(const struct commit *commit,
d36f8679
JK
661 const void *format, struct strbuf *sb,
662 enum date_mode dmode)
cde75e59 663{
f29d5958
RS
664 struct format_commit_context context;
665
666 memset(&context, 0, sizeof(context));
667 context.commit = commit;
d36f8679 668 context.dmode = dmode;
c3a670de 669 strbuf_expand(sb, format, format_commit_item, &context);
93fc05eb
JS
670}
671
672static void pp_header(enum cmit_fmt fmt,
673 int abbrev,
674 enum date_mode dmode,
675 const char *encoding,
676 const struct commit *commit,
677 const char **msg_p,
678 struct strbuf *sb)
679{
680 int parents_shown = 0;
681
682 for (;;) {
683 const char *line = *msg_p;
684 int linelen = get_one_line(*msg_p);
685
686 if (!linelen)
687 return;
688 *msg_p += linelen;
689
690 if (linelen == 1)
691 /* End of header */
692 return;
693
694 if (fmt == CMIT_FMT_RAW) {
695 strbuf_add(sb, line, linelen);
696 continue;
697 }
698
699 if (!memcmp(line, "parent ", 7)) {
700 if (linelen != 48)
701 die("bad parent line in commit");
702 continue;
703 }
704
705 if (!parents_shown) {
706 struct commit_list *parent;
707 int num;
708 for (parent = commit->parents, num = 0;
709 parent;
710 parent = parent->next, num++)
711 ;
712 /* with enough slop */
713 strbuf_grow(sb, num * 50 + 20);
714 add_merge_info(fmt, sb, commit, abbrev);
715 parents_shown = 1;
716 }
717
718 /*
719 * MEDIUM == DEFAULT shows only author with dates.
720 * FULL shows both authors but not dates.
721 * FULLER shows both authors and dates.
722 */
723 if (!memcmp(line, "author ", 7)) {
724 strbuf_grow(sb, linelen + 80);
b02bd65f 725 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
93fc05eb
JS
726 }
727 if (!memcmp(line, "committer ", 10) &&
728 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
729 strbuf_grow(sb, linelen + 80);
b02bd65f 730 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
93fc05eb
JS
731 }
732 }
733}
734
b02bd65f
DB
735void pp_title_line(enum cmit_fmt fmt,
736 const char **msg_p,
737 struct strbuf *sb,
738 const char *subject,
739 const char *after_subject,
740 const char *encoding,
267123b4 741 int need_8bit_cte)
93fc05eb 742{
88c44735 743 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
93fc05eb
JS
744 struct strbuf title;
745
746 strbuf_init(&title, 80);
88c44735 747 *msg_p = format_subject(&title, *msg_p, line_separator);
93fc05eb
JS
748
749 strbuf_grow(sb, title.len + 1024);
750 if (subject) {
751 strbuf_addstr(sb, subject);
752 add_rfc2047(sb, title.buf, title.len, encoding);
753 } else {
754 strbuf_addbuf(sb, &title);
755 }
756 strbuf_addch(sb, '\n');
757
6bf4f1b4 758 if (need_8bit_cte > 0) {
93fc05eb
JS
759 const char *header_fmt =
760 "MIME-Version: 1.0\n"
761 "Content-Type: text/plain; charset=%s\n"
762 "Content-Transfer-Encoding: 8bit\n";
763 strbuf_addf(sb, header_fmt, encoding);
764 }
765 if (after_subject) {
766 strbuf_addstr(sb, after_subject);
767 }
768 if (fmt == CMIT_FMT_EMAIL) {
769 strbuf_addch(sb, '\n');
770 }
771 strbuf_release(&title);
772}
773
b02bd65f
DB
774void pp_remainder(enum cmit_fmt fmt,
775 const char **msg_p,
776 struct strbuf *sb,
777 int indent)
93fc05eb
JS
778{
779 int first = 1;
780 for (;;) {
781 const char *line = *msg_p;
782 int linelen = get_one_line(line);
783 *msg_p += linelen;
784
785 if (!linelen)
786 break;
787
788 if (is_empty_line(line, &linelen)) {
789 if (first)
790 continue;
791 if (fmt == CMIT_FMT_SHORT)
792 break;
793 }
794 first = 0;
795
796 strbuf_grow(sb, linelen + indent + 20);
797 if (indent) {
798 memset(sb->buf + sb->len, ' ', indent);
799 strbuf_setlen(sb, sb->len + indent);
800 }
801 strbuf_add(sb, line, linelen);
802 strbuf_addch(sb, '\n');
803 }
804}
805
69cd8f63
AG
806char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
807{
808 const char *encoding;
809
810 encoding = (git_log_output_encoding
811 ? git_log_output_encoding
812 : git_commit_encoding);
813 if (!encoding)
814 encoding = "utf-8";
815 if (encoding_p)
816 *encoding_p = encoding;
817 return logmsg_reencode(commit, encoding);
818}
819
93fc05eb 820void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
6bf4f1b4
JH
821 struct strbuf *sb, int abbrev,
822 const char *subject, const char *after_subject,
823 enum date_mode dmode, int need_8bit_cte)
93fc05eb
JS
824{
825 unsigned long beginning_of_body;
826 int indent = 4;
827 const char *msg = commit->buffer;
828 char *reencoded;
829 const char *encoding;
830
831 if (fmt == CMIT_FMT_USERFORMAT) {
d36f8679 832 format_commit_message(commit, user_format, sb, dmode);
93fc05eb
JS
833 return;
834 }
835
69cd8f63 836 reencoded = reencode_commit_message(commit, &encoding);
93fc05eb
JS
837 if (reencoded) {
838 msg = reencoded;
839 }
840
841 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
842 indent = 0;
843
6bf4f1b4
JH
844 /*
845 * We need to check and emit Content-type: to mark it
846 * as 8-bit if we haven't done so.
93fc05eb 847 */
6bf4f1b4 848 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
93fc05eb
JS
849 int i, ch, in_body;
850
851 for (in_body = i = 0; (ch = msg[i]); i++) {
852 if (!in_body) {
853 /* author could be non 7-bit ASCII but
854 * the log may be so; skip over the
855 * header part first.
856 */
857 if (ch == '\n' && msg[i+1] == '\n')
858 in_body = 1;
859 }
860 else if (non_ascii(ch)) {
6bf4f1b4 861 need_8bit_cte = 1;
93fc05eb
JS
862 break;
863 }
864 }
865 }
866
867 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
868 if (fmt != CMIT_FMT_ONELINE && !subject) {
869 strbuf_addch(sb, '\n');
870 }
871
872 /* Skip excess blank lines at the beginning of body, if any... */
a0109668 873 msg = skip_empty_lines(msg);
93fc05eb
JS
874
875 /* These formats treat the title line specially. */
876 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
877 pp_title_line(fmt, &msg, sb, subject,
6bf4f1b4 878 after_subject, encoding, need_8bit_cte);
93fc05eb
JS
879
880 beginning_of_body = sb->len;
881 if (fmt != CMIT_FMT_ONELINE)
882 pp_remainder(fmt, &msg, sb, indent);
883 strbuf_rtrim(sb);
884
885 /* Make sure there is an EOLN for the non-oneline case */
886 if (fmt != CMIT_FMT_ONELINE)
887 strbuf_addch(sb, '\n');
888
889 /*
890 * The caller may append additional body text in e-mail
891 * format. Make sure we did not strip the blank line
892 * between the header and the body.
893 */
894 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
895 strbuf_addch(sb, '\n');
896 free(reencoded);
897}