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