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