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