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