]> git.ipfire.org Git - thirdparty/git.git/blame - pretty.c
format-patch: make rfc2047 encoding more strict
[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"
a97a7468 9#include "notes.h"
c002922a 10#include "color.h"
8f8f5476 11#include "reflog-walk.h"
f6667c5e 12#include "gpg-interface.h"
93fc05eb 13
93fc05eb 14static char *user_format;
40957891
WP
15static struct cmt_fmt_map {
16 const char *name;
17 enum cmit_fmt format;
18 int is_tformat;
2d7671ef
WP
19 int is_alias;
20 const char *user_format;
40957891 21} *commit_formats;
8028184e 22static size_t builtin_formats_len;
40957891 23static size_t commit_formats_len;
8028184e 24static size_t commit_formats_alloc;
40957891 25static struct cmt_fmt_map *find_commit_format(const char *sought);
93fc05eb 26
36407548
NS
27static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
28{
29 free(user_format);
30 user_format = xstrdup(cp);
31 if (is_tformat)
32 rev->use_terminator = 1;
33 rev->commit_format = CMIT_FMT_USERFORMAT;
34}
35
8028184e 36static int git_pretty_formats_config(const char *var, const char *value, void *cb)
93fc05eb 37{
8028184e
WP
38 struct cmt_fmt_map *commit_format = NULL;
39 const char *name;
40 const char *fmt;
93fc05eb 41 int i;
8028184e
WP
42
43 if (prefixcmp(var, "pretty."))
44 return 0;
45
46 name = var + strlen("pretty.");
47 for (i = 0; i < builtin_formats_len; i++) {
48 if (!strcmp(commit_formats[i].name, name))
49 return 0;
50 }
51
52 for (i = builtin_formats_len; i < commit_formats_len; i++) {
53 if (!strcmp(commit_formats[i].name, name)) {
54 commit_format = &commit_formats[i];
55 break;
56 }
57 }
58
59 if (!commit_format) {
60 ALLOC_GROW(commit_formats, commit_formats_len+1,
61 commit_formats_alloc);
62 commit_format = &commit_formats[commit_formats_len];
95a2618f 63 memset(commit_format, 0, sizeof(*commit_format));
8028184e
WP
64 commit_formats_len++;
65 }
66
67 commit_format->name = xstrdup(name);
68 commit_format->format = CMIT_FMT_USERFORMAT;
69 git_config_string(&fmt, var, value);
70 if (!prefixcmp(fmt, "format:") || !prefixcmp(fmt, "tformat:")) {
71 commit_format->is_tformat = fmt[0] == 't';
72 fmt = strchr(fmt, ':') + 1;
73 } else if (strchr(fmt, '%'))
74 commit_format->is_tformat = 1;
75 else
76 commit_format->is_alias = 1;
77 commit_format->user_format = fmt;
78
79 return 0;
80}
81
40957891 82static void setup_commit_formats(void)
93fc05eb 83{
40957891
WP
84 struct cmt_fmt_map builtin_formats[] = {
85 { "raw", CMIT_FMT_RAW, 0 },
86 { "medium", CMIT_FMT_MEDIUM, 0 },
87 { "short", CMIT_FMT_SHORT, 0 },
88 { "email", CMIT_FMT_EMAIL, 0 },
89 { "fuller", CMIT_FMT_FULLER, 0 },
90 { "full", CMIT_FMT_FULL, 0 },
91 { "oneline", CMIT_FMT_ONELINE, 1 }
4da45bef 92 };
40957891 93 commit_formats_len = ARRAY_SIZE(builtin_formats);
8028184e
WP
94 builtin_formats_len = commit_formats_len;
95 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
40957891
WP
96 memcpy(commit_formats, builtin_formats,
97 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
8028184e
WP
98
99 git_config(git_pretty_formats_config, NULL);
40957891
WP
100}
101
2d7671ef
WP
102static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
103 const char *original,
104 int num_redirections)
40957891
WP
105{
106 struct cmt_fmt_map *found = NULL;
107 size_t found_match_len = 0;
108 int i;
109
2d7671ef
WP
110 if (num_redirections >= commit_formats_len)
111 die("invalid --pretty format: "
112 "'%s' references an alias which points to itself",
113 original);
40957891
WP
114
115 for (i = 0; i < commit_formats_len; i++) {
116 size_t match_len;
117
118 if (prefixcmp(commit_formats[i].name, sought))
119 continue;
120
121 match_len = strlen(commit_formats[i].name);
122 if (found == NULL || found_match_len > match_len) {
123 found = &commit_formats[i];
124 found_match_len = match_len;
125 }
126 }
2d7671ef
WP
127
128 if (found && found->is_alias) {
129 found = find_commit_format_recursive(found->user_format,
130 original,
131 num_redirections+1);
132 }
133
40957891
WP
134 return found;
135}
136
2d7671ef
WP
137static struct cmt_fmt_map *find_commit_format(const char *sought)
138{
139 if (!commit_formats)
140 setup_commit_formats();
141
142 return find_commit_format_recursive(sought, sought, 0);
143}
144
40957891
WP
145void get_commit_format(const char *arg, struct rev_info *rev)
146{
147 struct cmt_fmt_map *commit_format;
93fc05eb 148
4da45bef
JH
149 rev->use_terminator = 0;
150 if (!arg || !*arg) {
151 rev->commit_format = CMIT_FMT_DEFAULT;
152 return;
153 }
4da45bef 154 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
36407548 155 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
4da45bef 156 return;
93fc05eb 157 }
40957891 158
36407548
NS
159 if (strchr(arg, '%')) {
160 save_user_format(rev, arg, 1);
161 return;
162 }
93fc05eb 163
40957891
WP
164 commit_format = find_commit_format(arg);
165 if (!commit_format)
166 die("invalid --pretty format: %s", arg);
167
168 rev->commit_format = commit_format->format;
169 rev->use_terminator = commit_format->is_tformat;
8028184e
WP
170 if (commit_format->format == CMIT_FMT_USERFORMAT) {
171 save_user_format(rev, commit_format->user_format,
172 commit_format->is_tformat);
173 }
93fc05eb
JS
174}
175
176/*
177 * Generic support for pretty-printing the header
178 */
179static int get_one_line(const char *msg)
180{
181 int ret = 0;
182
183 for (;;) {
184 char c = *msg++;
185 if (!c)
186 break;
187 ret++;
188 if (c == '\n')
189 break;
190 }
191 return ret;
192}
193
194/* High bit set, or ISO-2022-INT */
cc571142 195static int non_ascii(int ch)
93fc05eb 196{
c2e9364a 197 return !isascii(ch) || ch == '\033';
93fc05eb
JS
198}
199
28e9cf65
JS
200int has_non_ascii(const char *s)
201{
202 int ch;
203 if (!s)
204 return 0;
205 while ((ch = *s++) != '\0') {
206 if (non_ascii(ch))
207 return 1;
208 }
209 return 0;
210}
211
4d03c18a
JK
212static int is_rfc822_special(char ch)
213{
214 switch (ch) {
215 case '(':
216 case ')':
217 case '<':
218 case '>':
219 case '[':
220 case ']':
221 case ':':
222 case ';':
223 case '@':
224 case ',':
225 case '.':
226 case '"':
227 case '\\':
228 return 1;
229 default:
230 return 0;
231 }
232}
233
234static int has_rfc822_specials(const char *s, int len)
235{
236 int i;
237 for (i = 0; i < len; i++)
238 if (is_rfc822_special(s[i]))
239 return 1;
240 return 0;
241}
242
f9b7204b
JS
243static int last_line_length(struct strbuf *sb)
244{
245 int i;
246
247 /* How many bytes are already used on the last line? */
248 for (i = sb->len - 1; i >= 0; i--)
249 if (sb->buf[i] == '\n')
250 break;
251 return sb->len - (i + 1);
252}
253
4d03c18a
JK
254static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
255{
256 int i;
257
258 /* just a guess, we may have to also backslash-quote */
259 strbuf_grow(out, len + 2);
260
261 strbuf_addch(out, '"');
262 for (i = 0; i < len; i++) {
263 switch (s[i]) {
264 case '"':
265 case '\\':
266 strbuf_addch(out, '\\');
267 /* fall through */
268 default:
269 strbuf_addch(out, s[i]);
270 }
271 }
272 strbuf_addch(out, '"');
273}
274
0fcec2ce
JS
275enum rfc2047_type {
276 RFC2047_SUBJECT,
277 RFC2047_ADDRESS,
278};
279
280static int is_rfc2047_special(char ch, enum rfc2047_type type)
93fc05eb 281{
0fcec2ce
JS
282 /*
283 * rfc2047, section 4.2:
284 *
285 * 8-bit values which correspond to printable ASCII characters other
286 * than "=", "?", and "_" (underscore), MAY be represented as those
287 * characters. (But see section 5 for restrictions.) In
288 * particular, SPACE and TAB MUST NOT be represented as themselves
289 * within encoded words.
290 */
291
292 /*
293 * rule out non-ASCII characters and non-printable characters (the
294 * non-ASCII check should be redundant as isprint() is not localized
295 * and only knows about ASCII, but be defensive about that)
296 */
297 if (non_ascii(ch) || !isprint(ch))
298 return 1;
299
300 /*
301 * rule out special printable characters (' ' should be the only
302 * whitespace character considered printable, but be defensive and use
303 * isspace())
304 */
305 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
94f6cdf6
JS
306 return 1;
307
0fcec2ce
JS
308 /*
309 * rfc2047, section 5.3:
310 *
311 * As a replacement for a 'word' entity within a 'phrase', for example,
312 * one that precedes an address in a From, To, or Cc header. The ABNF
313 * definition for 'phrase' from RFC 822 thus becomes:
314 *
315 * phrase = 1*( encoded-word / word )
316 *
317 * In this case the set of characters that may be used in a "Q"-encoded
318 * 'encoded-word' is restricted to: <upper and lower case ASCII
319 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
320 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
321 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
322 * 'special' by 'linear-white-space'.
323 */
324
325 if (type != RFC2047_ADDRESS)
326 return 0;
327
328 /* '=' and '_' are special cases and have been checked above */
329 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
93fc05eb
JS
330}
331
332static void add_rfc2047(struct strbuf *sb, const char *line, int len,
0fcec2ce 333 const char *encoding, enum rfc2047_type type)
93fc05eb 334{
a1f6baa5 335 static const int max_length = 78; /* per rfc2822 */
94f6cdf6 336 static const int max_encoded_length = 76; /* per rfc2047 */
a1f6baa5 337 int i;
f9b7204b 338 int line_len = last_line_length(sb);
93fc05eb
JS
339
340 for (i = 0; i < len; i++) {
341 int ch = line[i];
c22e7de3 342 if (non_ascii(ch) || ch == '\n')
93fc05eb
JS
343 goto needquote;
344 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
345 goto needquote;
346 }
7a76e68a 347 strbuf_add_wrapped_bytes(sb, line, len, -line_len, 1, max_length);
93fc05eb
JS
348 return;
349
350needquote:
351 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
352 strbuf_addf(sb, "=?%s?q?", encoding);
a1f6baa5
JK
353 line_len += strlen(encoding) + 5; /* 5 for =??q? */
354 for (i = 0; i < len; i++) {
93fc05eb 355 unsigned ch = line[i] & 0xFF;
0fcec2ce 356 int is_special = is_rfc2047_special(ch, type);
94f6cdf6
JS
357
358 /*
359 * According to RFC 2047, we could encode the special character
360 * ' ' (space) with '_' (underscore) for readability. But many
361 * programs do not understand this and just leave the
362 * underscore in place. Thus, we do nothing special here, which
363 * causes ' ' to be encoded as '=20', avoiding this problem.
364 */
a1f6baa5 365
94f6cdf6 366 if (line_len + 2 + (is_special ? 3 : 1) > max_encoded_length) {
a1f6baa5
JK
367 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
368 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
369 }
370
94f6cdf6 371 if (is_special) {
93fc05eb 372 strbuf_addf(sb, "=%02X", ch);
a1f6baa5 373 line_len += 3;
94f6cdf6 374 } else {
a1f6baa5
JK
375 strbuf_addch(sb, ch);
376 line_len++;
93fc05eb
JS
377 }
378 }
93fc05eb
JS
379 strbuf_addstr(sb, "?=");
380}
381
6bf13944
JK
382void pp_user_info(const struct pretty_print_context *pp,
383 const char *what, struct strbuf *sb,
384 const char *line, const char *encoding)
93fc05eb
JS
385{
386 char *date;
387 int namelen;
388 unsigned long time;
389 int tz;
93fc05eb 390
6bf13944 391 if (pp->fmt == CMIT_FMT_ONELINE)
93fc05eb
JS
392 return;
393 date = strchr(line, '>');
394 if (!date)
395 return;
396 namelen = ++date - line;
397 time = strtoul(date, &date, 10);
398 tz = strtol(date, NULL, 10);
399
6bf13944 400 if (pp->fmt == CMIT_FMT_EMAIL) {
93fc05eb
JS
401 char *name_tail = strchr(line, '<');
402 int display_name_length;
403 if (!name_tail)
404 return;
405 while (line < name_tail && isspace(name_tail[-1]))
406 name_tail--;
407 display_name_length = name_tail - line;
93fc05eb 408 strbuf_addstr(sb, "From: ");
4d03c18a 409 if (!has_rfc822_specials(line, display_name_length)) {
0fcec2ce
JS
410 add_rfc2047(sb, line, display_name_length,
411 encoding, RFC2047_ADDRESS);
4d03c18a
JK
412 } else {
413 struct strbuf quoted = STRBUF_INIT;
414 add_rfc822_quoted(&quoted, line, display_name_length);
0fcec2ce
JS
415 add_rfc2047(sb, quoted.buf, quoted.len,
416 encoding, RFC2047_ADDRESS);
4d03c18a
JK
417 strbuf_release(&quoted);
418 }
f9b7204b 419 if (namelen - display_name_length + last_line_length(sb) > 78) {
990f6e30
JK
420 strbuf_addch(sb, '\n');
421 if (!isspace(name_tail[0]))
422 strbuf_addch(sb, ' ');
423 }
93fc05eb
JS
424 strbuf_add(sb, name_tail, namelen - display_name_length);
425 strbuf_addch(sb, '\n');
426 } else {
427 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
6bf13944 428 (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
8e76bf3f 429 " ", namelen, line);
93fc05eb 430 }
6bf13944 431 switch (pp->fmt) {
93fc05eb 432 case CMIT_FMT_MEDIUM:
6bf13944 433 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode));
93fc05eb
JS
434 break;
435 case CMIT_FMT_EMAIL:
436 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
437 break;
438 case CMIT_FMT_FULLER:
6bf13944 439 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
93fc05eb
JS
440 break;
441 default:
442 /* notin' */
443 break;
444 }
445}
446
447static int is_empty_line(const char *line, int *len_p)
448{
449 int len = *len_p;
450 while (len && isspace(line[len-1]))
451 len--;
452 *len_p = len;
453 return !len;
454}
455
a0109668
RS
456static const char *skip_empty_lines(const char *msg)
457{
458 for (;;) {
459 int linelen = get_one_line(msg);
460 int ll = linelen;
461 if (!linelen)
462 break;
463 if (!is_empty_line(msg, &ll))
464 break;
465 msg += linelen;
466 }
467 return msg;
468}
469
6bf13944
JK
470static void add_merge_info(const struct pretty_print_context *pp,
471 struct strbuf *sb, const struct commit *commit)
93fc05eb
JS
472{
473 struct commit_list *parent = commit->parents;
474
6bf13944 475 if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
93fc05eb
JS
476 !parent || !parent->next)
477 return;
478
479 strbuf_addstr(sb, "Merge:");
480
481 while (parent) {
482 struct commit *p = parent->item;
483 const char *hex = NULL;
6bf13944
JK
484 if (pp->abbrev)
485 hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
93fc05eb
JS
486 if (!hex)
487 hex = sha1_to_hex(p->object.sha1);
93fc05eb
JS
488 parent = parent->next;
489
7fcda920 490 strbuf_addf(sb, " %s", hex);
93fc05eb
JS
491 }
492 strbuf_addch(sb, '\n');
493}
494
495static char *get_header(const struct commit *commit, const char *key)
496{
497 int key_len = strlen(key);
498 const char *line = commit->buffer;
499
a9c7a8a8 500 while (line) {
93fc05eb
JS
501 const char *eol = strchr(line, '\n'), *next;
502
503 if (line == eol)
504 return NULL;
505 if (!eol) {
a9c7a8a8
JK
506 warning("malformed commit (header is missing newline): %s",
507 sha1_to_hex(commit->object.sha1));
93fc05eb
JS
508 eol = line + strlen(line);
509 next = NULL;
510 } else
511 next = eol + 1;
512 if (eol - line > key_len &&
513 !strncmp(line, key, key_len) &&
514 line[key_len] == ' ') {
515 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
516 }
517 line = next;
518 }
a9c7a8a8 519 return NULL;
93fc05eb
JS
520}
521
522static char *replace_encoding_header(char *buf, const char *encoding)
523{
f285a2d7 524 struct strbuf tmp = STRBUF_INIT;
93fc05eb
JS
525 size_t start, len;
526 char *cp = buf;
527
528 /* guess if there is an encoding header before a \n\n */
529 while (strncmp(cp, "encoding ", strlen("encoding "))) {
530 cp = strchr(cp, '\n');
531 if (!cp || *++cp == '\n')
532 return buf;
533 }
534 start = cp - buf;
535 cp = strchr(cp, '\n');
536 if (!cp)
537 return buf; /* should not happen but be defensive */
538 len = cp + 1 - (buf + start);
539
93fc05eb
JS
540 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
541 if (is_encoding_utf8(encoding)) {
542 /* we have re-coded to UTF-8; drop the header */
543 strbuf_remove(&tmp, start, len);
544 } else {
545 /* just replaces XXXX in 'encoding XXXX\n' */
546 strbuf_splice(&tmp, start + strlen("encoding "),
547 len - strlen("encoding \n"),
548 encoding, strlen(encoding));
549 }
550 return strbuf_detach(&tmp, NULL);
551}
552
177b29dc
PN
553char *logmsg_reencode(const struct commit *commit,
554 const char *output_encoding)
93fc05eb 555{
330db18c 556 static const char *utf8 = "UTF-8";
93fc05eb
JS
557 const char *use_encoding;
558 char *encoding;
559 char *out;
560
561 if (!*output_encoding)
562 return NULL;
563 encoding = get_header(commit, "encoding");
564 use_encoding = encoding ? encoding : utf8;
565 if (!strcmp(use_encoding, output_encoding))
566 if (encoding) /* we'll strip encoding header later */
567 out = xstrdup(commit->buffer);
568 else
569 return NULL; /* nothing to do */
570 else
571 out = reencode_string(commit->buffer,
572 output_encoding, use_encoding);
573 if (out)
574 out = replace_encoding_header(out, output_encoding);
575
576 free(encoding);
577 return out;
578}
579
d20d654f 580static int mailmap_name(char *email, int email_len, char *name, int name_len)
e0cbc397 581{
c455c87c 582 static struct string_list *mail_map;
e0cbc397
JS
583 if (!mail_map) {
584 mail_map = xcalloc(1, sizeof(*mail_map));
d551a488 585 read_mailmap(mail_map, NULL);
e0cbc397 586 }
d20d654f 587 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
e0cbc397
JS
588}
589
c3a670de 590static size_t format_person_part(struct strbuf *sb, char part,
d36f8679 591 const char *msg, int len, enum date_mode dmode)
93fc05eb 592{
c3a670de
MC
593 /* currently all placeholders have same length */
594 const int placeholder_len = 2;
4b340cfa 595 int tz;
c3a670de 596 unsigned long date = 0;
d20d654f
MSO
597 char person_name[1024];
598 char person_mail[1024];
4b340cfa
JH
599 struct ident_split s;
600 const char *name_start, *name_end, *mail_start, *mail_end;
93fc05eb 601
4b340cfa 602 if (split_ident_line(&s, msg, len) < 0)
c3a670de
MC
603 goto skip;
604
4b340cfa
JH
605 name_start = s.name_begin;
606 name_end = s.name_end;
607 mail_start = s.mail_begin;
608 mail_end = s.mail_end;
d20d654f
MSO
609
610 if (part == 'N' || part == 'E') { /* mailmap lookup */
c9b4e9e5
JK
611 snprintf(person_name, sizeof(person_name), "%.*s",
612 (int)(name_end - name_start), name_start);
613 snprintf(person_mail, sizeof(person_mail), "%.*s",
614 (int)(mail_end - mail_start), mail_start);
d20d654f
MSO
615 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
616 name_start = person_name;
617 name_end = name_start + strlen(person_name);
618 mail_start = person_mail;
619 mail_end = mail_start + strlen(person_mail);
620 }
e0cbc397 621 if (part == 'n' || part == 'N') { /* name */
d20d654f 622 strbuf_add(sb, name_start, name_end-name_start);
c3a670de 623 return placeholder_len;
cde75e59 624 }
d20d654f
MSO
625 if (part == 'e' || part == 'E') { /* email */
626 strbuf_add(sb, mail_start, mail_end-mail_start);
c3a670de 627 return placeholder_len;
cde75e59 628 }
93fc05eb 629
4b340cfa 630 if (!s.date_begin)
c3a670de 631 goto skip;
93fc05eb 632
4b340cfa
JH
633 date = strtoul(s.date_begin, NULL, 10);
634
cde75e59 635 if (part == 't') { /* date, UNIX timestamp */
4b340cfa 636 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
c3a670de 637 return placeholder_len;
cde75e59 638 }
93fc05eb
JS
639
640 /* parse tz */
4b340cfa
JH
641 tz = strtoul(s.tz_begin + 1, NULL, 10);
642 if (*s.tz_begin == '-')
643 tz = -tz;
93fc05eb 644
cde75e59
RS
645 switch (part) {
646 case 'd': /* date */
d36f8679 647 strbuf_addstr(sb, show_date(date, tz, dmode));
c3a670de 648 return placeholder_len;
cde75e59
RS
649 case 'D': /* date, RFC2822 style */
650 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
c3a670de 651 return placeholder_len;
cde75e59
RS
652 case 'r': /* date, relative */
653 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
c3a670de 654 return placeholder_len;
cde75e59
RS
655 case 'i': /* date, ISO 8601 */
656 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
c3a670de 657 return placeholder_len;
cde75e59 658 }
c3a670de
MC
659
660skip:
661 /*
4b340cfa
JH
662 * reading from either a bogus commit, or a reflog entry with
663 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
664 * to compute a valid return value.
c3a670de
MC
665 */
666 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
667 || part == 'D' || part == 'r' || part == 'i')
668 return placeholder_len;
669
670 return 0; /* unknown placeholder */
93fc05eb
JS
671}
672
f29d5958
RS
673struct chunk {
674 size_t off;
675 size_t len;
676};
677
678struct format_commit_context {
679 const struct commit *commit;
dd2e794a 680 const struct pretty_print_context *pretty_ctx;
f53bd743
RS
681 unsigned commit_header_parsed:1;
682 unsigned commit_message_parsed:1;
f6667c5e
JH
683 unsigned commit_signature_parsed:1;
684 struct {
685 char *gpg_output;
686 char good_bad;
687 char *signer;
688 } signature;
177b29dc 689 char *message;
02edd56b 690 size_t width, indent1, indent2;
f29d5958
RS
691
692 /* These offsets are relative to the start of the commit message. */
f29d5958
RS
693 struct chunk author;
694 struct chunk committer;
695 struct chunk encoding;
f53bd743
RS
696 size_t message_off;
697 size_t subject_off;
f29d5958 698 size_t body_off;
b9c62321
RS
699
700 /* The following ones are relative to the result struct strbuf. */
701 struct chunk abbrev_commit_hash;
702 struct chunk abbrev_tree_hash;
703 struct chunk abbrev_parent_hashes;
02edd56b 704 size_t wrap_start;
f29d5958
RS
705};
706
b9c62321
RS
707static int add_again(struct strbuf *sb, struct chunk *chunk)
708{
709 if (chunk->len) {
710 strbuf_adddup(sb, chunk->off, chunk->len);
711 return 1;
712 }
713
714 /*
715 * We haven't seen this chunk before. Our caller is surely
716 * going to add it the hard way now. Remember the most likely
717 * start of the to-be-added chunk: the current end of the
718 * struct strbuf.
719 */
720 chunk->off = sb->len;
721 return 0;
722}
723
f29d5958 724static void parse_commit_header(struct format_commit_context *context)
93fc05eb 725{
177b29dc 726 const char *msg = context->message;
93fc05eb 727 int i;
f29d5958 728
f53bd743 729 for (i = 0; msg[i]; i++) {
f29d5958
RS
730 int eol;
731 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
732 ; /* do nothing */
733
f29d5958 734 if (i == eol) {
f53bd743 735 break;
f29d5958
RS
736 } else if (!prefixcmp(msg + i, "author ")) {
737 context->author.off = i + 7;
738 context->author.len = eol - i - 7;
739 } else if (!prefixcmp(msg + i, "committer ")) {
740 context->committer.off = i + 10;
741 context->committer.len = eol - i - 10;
742 } else if (!prefixcmp(msg + i, "encoding ")) {
743 context->encoding.off = i + 9;
744 context->encoding.len = eol - i - 9;
745 }
746 i = eol;
747 }
f53bd743 748 context->message_off = i;
f29d5958
RS
749 context->commit_header_parsed = 1;
750}
751
46d164b0
SB
752static int istitlechar(char c)
753{
754 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
755 (c >= '0' && c <= '9') || c == '.' || c == '_';
756}
757
758static void format_sanitized_subject(struct strbuf *sb, const char *msg)
759{
760 size_t trimlen;
871d21d4 761 size_t start_len = sb->len;
46d164b0
SB
762 int space = 2;
763
764 for (; *msg && *msg != '\n'; msg++) {
765 if (istitlechar(*msg)) {
766 if (space == 1)
767 strbuf_addch(sb, '-');
768 space = 0;
769 strbuf_addch(sb, *msg);
770 if (*msg == '.')
771 while (*(msg+1) == '.')
772 msg++;
773 } else
774 space |= 1;
775 }
776
777 /* trim any trailing '.' or '-' characters */
778 trimlen = 0;
871d21d4
SB
779 while (sb->len - trimlen > start_len &&
780 (sb->buf[sb->len - 1 - trimlen] == '.'
781 || sb->buf[sb->len - 1 - trimlen] == '-'))
46d164b0
SB
782 trimlen++;
783 strbuf_remove(sb, sb->len - trimlen, trimlen);
784}
785
cec08717
RS
786const char *format_subject(struct strbuf *sb, const char *msg,
787 const char *line_separator)
88c44735
RS
788{
789 int first = 1;
790
791 for (;;) {
792 const char *line = msg;
793 int linelen = get_one_line(line);
794
795 msg += linelen;
796 if (!linelen || is_empty_line(line, &linelen))
797 break;
798
f53bd743
RS
799 if (!sb)
800 continue;
88c44735
RS
801 strbuf_grow(sb, linelen + 2);
802 if (!first)
803 strbuf_addstr(sb, line_separator);
804 strbuf_add(sb, line, linelen);
805 first = 0;
806 }
807 return msg;
808}
809
f53bd743
RS
810static void parse_commit_message(struct format_commit_context *c)
811{
177b29dc
PN
812 const char *msg = c->message + c->message_off;
813 const char *start = c->message;
f53bd743
RS
814
815 msg = skip_empty_lines(msg);
816 c->subject_off = msg - start;
817
818 msg = format_subject(NULL, msg, NULL);
819 msg = skip_empty_lines(msg);
820 c->body_off = msg - start;
821
822 c->commit_message_parsed = 1;
823}
824
3b3d443f
RS
825static void format_decoration(struct strbuf *sb, const struct commit *commit)
826{
827 struct name_decoration *d;
828 const char *prefix = " (";
829
33e7018c 830 load_ref_decorations(DECORATE_SHORT_REFS);
3b3d443f
RS
831 d = lookup_decoration(&name_decoration, &commit->object);
832 while (d) {
833 strbuf_addstr(sb, prefix);
834 prefix = ", ";
835 strbuf_addstr(sb, d->name);
836 d = d->next;
837 }
838 if (prefix[0] == ',')
839 strbuf_addch(sb, ')');
840}
841
02edd56b
RS
842static void strbuf_wrap(struct strbuf *sb, size_t pos,
843 size_t width, size_t indent1, size_t indent2)
844{
845 struct strbuf tmp = STRBUF_INIT;
846
847 if (pos)
848 strbuf_add(&tmp, sb->buf, pos);
849 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
850 (int) indent1, (int) indent2, (int) width);
851 strbuf_swap(&tmp, sb);
852 strbuf_release(&tmp);
853}
854
855static void rewrap_message_tail(struct strbuf *sb,
856 struct format_commit_context *c,
857 size_t new_width, size_t new_indent1,
858 size_t new_indent2)
859{
860 if (c->width == new_width && c->indent1 == new_indent1 &&
861 c->indent2 == new_indent2)
862 return;
32ca4249 863 if (c->wrap_start < sb->len)
02edd56b
RS
864 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
865 c->wrap_start = sb->len;
866 c->width = new_width;
867 c->indent1 = new_indent1;
868 c->indent2 = new_indent2;
869}
870
f6667c5e
JH
871static struct {
872 char result;
873 const char *check;
874} signature_check[] = {
875 { 'G', ": Good signature from " },
876 { 'B', ": BAD signature from " },
877};
878
879static void parse_signature_lines(struct format_commit_context *ctx)
880{
881 const char *buf = ctx->signature.gpg_output;
882 int i;
883
884 for (i = 0; i < ARRAY_SIZE(signature_check); i++) {
885 const char *found = strstr(buf, signature_check[i].check);
886 const char *next;
887 if (!found)
888 continue;
889 ctx->signature.good_bad = signature_check[i].result;
890 found += strlen(signature_check[i].check);
891 next = strchrnul(found, '\n');
892 ctx->signature.signer = xmemdupz(found, next - found);
893 break;
894 }
895}
896
897static void parse_commit_signature(struct format_commit_context *ctx)
898{
899 struct strbuf payload = STRBUF_INIT;
900 struct strbuf signature = STRBUF_INIT;
901 struct strbuf gpg_output = STRBUF_INIT;
902 int status;
903
904 ctx->commit_signature_parsed = 1;
905
906 if (parse_signed_commit(ctx->commit->object.sha1,
907 &payload, &signature) <= 0)
908 goto out;
909 status = verify_signed_buffer(payload.buf, payload.len,
910 signature.buf, signature.len,
911 &gpg_output);
912 if (status && !gpg_output.len)
913 goto out;
914 ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL);
915 parse_signature_lines(ctx);
916
917 out:
918 strbuf_release(&gpg_output);
919 strbuf_release(&payload);
920 strbuf_release(&signature);
921}
922
923
cd1957f5
JK
924static int format_reflog_person(struct strbuf *sb,
925 char part,
926 struct reflog_walk_info *log,
927 enum date_mode dmode)
928{
929 const char *ident;
930
931 if (!log)
932 return 2;
933
934 ident = get_reflog_ident(log);
935 if (!ident)
936 return 2;
937
938 return format_person_part(sb, part, ident, strlen(ident), dmode);
939}
940
9fa708da
JH
941static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
942 void *context)
f29d5958
RS
943{
944 struct format_commit_context *c = context;
945 const struct commit *commit = c->commit;
177b29dc 946 const char *msg = c->message;
f29d5958 947 struct commit_list *p;
42c8c74c 948 int h1, h2;
93fc05eb 949
93fc05eb 950 /* these are independent of the commit */
cde75e59
RS
951 switch (placeholder[0]) {
952 case 'C':
c002922a
JK
953 if (placeholder[1] == '(') {
954 const char *end = strchr(placeholder + 2, ')');
955 char color[COLOR_MAXLEN];
956 if (!end)
957 return 0;
958 color_parse_mem(placeholder + 2,
959 end - (placeholder + 2),
960 "--pretty format", color);
961 strbuf_addstr(sb, color);
962 return end - placeholder + 1;
963 }
c3a670de 964 if (!prefixcmp(placeholder + 1, "red")) {
dc6ebd4c 965 strbuf_addstr(sb, GIT_COLOR_RED);
c3a670de
MC
966 return 4;
967 } else if (!prefixcmp(placeholder + 1, "green")) {
dc6ebd4c 968 strbuf_addstr(sb, GIT_COLOR_GREEN);
c3a670de
MC
969 return 6;
970 } else if (!prefixcmp(placeholder + 1, "blue")) {
dc6ebd4c 971 strbuf_addstr(sb, GIT_COLOR_BLUE);
c3a670de
MC
972 return 5;
973 } else if (!prefixcmp(placeholder + 1, "reset")) {
dc6ebd4c 974 strbuf_addstr(sb, GIT_COLOR_RESET);
c3a670de
MC
975 return 6;
976 } else
977 return 0;
cde75e59
RS
978 case 'n': /* newline */
979 strbuf_addch(sb, '\n');
c3a670de 980 return 1;
42c8c74c
GS
981 case 'x':
982 /* %x00 == NUL, %x0a == LF, etc. */
983 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
984 h1 <= 16 &&
985 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
986 h2 <= 16) {
987 strbuf_addch(sb, (h1<<4)|h2);
988 return 3;
989 } else
990 return 0;
02edd56b
RS
991 case 'w':
992 if (placeholder[1] == '(') {
993 unsigned long width = 0, indent1 = 0, indent2 = 0;
994 char *next;
995 const char *start = placeholder + 2;
996 const char *end = strchr(start, ')');
997 if (!end)
998 return 0;
999 if (end > start) {
1000 width = strtoul(start, &next, 10);
1001 if (*next == ',') {
1002 indent1 = strtoul(next + 1, &next, 10);
1003 if (*next == ',') {
1004 indent2 = strtoul(next + 1,
1005 &next, 10);
1006 }
1007 }
1008 if (*next != ')')
1009 return 0;
1010 }
1011 rewrap_message_tail(sb, c, width, indent1, indent2);
1012 return end - placeholder + 1;
1013 } else
1014 return 0;
cde75e59 1015 }
93fc05eb
JS
1016
1017 /* these depend on the commit */
1018 if (!commit->object.parsed)
1019 parse_object(commit->object.sha1);
93fc05eb 1020
cde75e59
RS
1021 switch (placeholder[0]) {
1022 case 'H': /* commit hash */
1023 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
c3a670de 1024 return 1;
cde75e59 1025 case 'h': /* abbreviated commit hash */
b9c62321 1026 if (add_again(sb, &c->abbrev_commit_hash))
c3a670de 1027 return 1;
cde75e59 1028 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
c1977021 1029 c->pretty_ctx->abbrev));
b9c62321 1030 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
c3a670de 1031 return 1;
cde75e59
RS
1032 case 'T': /* tree hash */
1033 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
c3a670de 1034 return 1;
cde75e59 1035 case 't': /* abbreviated tree hash */
b9c62321 1036 if (add_again(sb, &c->abbrev_tree_hash))
c3a670de 1037 return 1;
cde75e59 1038 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
c1977021 1039 c->pretty_ctx->abbrev));
b9c62321 1040 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
c3a670de 1041 return 1;
cde75e59
RS
1042 case 'P': /* parent hashes */
1043 for (p = commit->parents; p; p = p->next) {
1044 if (p != commit->parents)
1045 strbuf_addch(sb, ' ');
1046 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
1047 }
c3a670de 1048 return 1;
cde75e59 1049 case 'p': /* abbreviated parent hashes */
b9c62321 1050 if (add_again(sb, &c->abbrev_parent_hashes))
c3a670de 1051 return 1;
cde75e59
RS
1052 for (p = commit->parents; p; p = p->next) {
1053 if (p != commit->parents)
1054 strbuf_addch(sb, ' ');
1055 strbuf_addstr(sb, find_unique_abbrev(
c1977021
WP
1056 p->item->object.sha1,
1057 c->pretty_ctx->abbrev));
cde75e59 1058 }
b9c62321
RS
1059 c->abbrev_parent_hashes.len = sb->len -
1060 c->abbrev_parent_hashes.off;
c3a670de 1061 return 1;
cde75e59 1062 case 'm': /* left/right/bottom */
1df2d656 1063 strbuf_addstr(sb, get_revision_mark(NULL, commit));
c3a670de 1064 return 1;
3b3d443f
RS
1065 case 'd':
1066 format_decoration(sb, commit);
1067 return 1;
8f8f5476
TR
1068 case 'g': /* reflog info */
1069 switch(placeholder[1]) {
1070 case 'd': /* reflog selector */
1071 case 'D':
1072 if (c->pretty_ctx->reflog_info)
1073 get_reflog_selector(sb,
1074 c->pretty_ctx->reflog_info,
1075 c->pretty_ctx->date_mode,
55ccf85a 1076 c->pretty_ctx->date_mode_explicit,
8f8f5476
TR
1077 (placeholder[1] == 'd'));
1078 return 2;
1079 case 's': /* reflog message */
1080 if (c->pretty_ctx->reflog_info)
1081 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1082 return 2;
cd1957f5
JK
1083 case 'n':
1084 case 'N':
1085 case 'e':
1086 case 'E':
1087 return format_reflog_person(sb,
1088 placeholder[1],
1089 c->pretty_ctx->reflog_info,
1090 c->pretty_ctx->date_mode);
8f8f5476
TR
1091 }
1092 return 0; /* unknown %g placeholder */
8b208f02 1093 case 'N':
5b163603
JG
1094 if (c->pretty_ctx->show_notes) {
1095 format_display_notes(commit->object.sha1, sb,
a6fa5992 1096 get_log_output_encoding(), 0);
5b163603
JG
1097 return 1;
1098 }
1099 return 0;
cde75e59
RS
1100 }
1101
f6667c5e
JH
1102 if (placeholder[0] == 'G') {
1103 if (!c->commit_signature_parsed)
1104 parse_commit_signature(c);
1105 switch (placeholder[1]) {
1106 case 'G':
1107 if (c->signature.gpg_output)
1108 strbuf_addstr(sb, c->signature.gpg_output);
1109 break;
1110 case '?':
1111 switch (c->signature.good_bad) {
1112 case 'G':
1113 case 'B':
1114 strbuf_addch(sb, c->signature.good_bad);
1115 }
1116 break;
1117 case 'S':
1118 if (c->signature.signer)
1119 strbuf_addstr(sb, c->signature.signer);
1120 break;
1121 }
1122 return 2;
1123 }
1124
1125
cde75e59 1126 /* For the rest we have to parse the commit header. */
f29d5958
RS
1127 if (!c->commit_header_parsed)
1128 parse_commit_header(c);
93fc05eb 1129
f29d5958 1130 switch (placeholder[0]) {
c3a670de
MC
1131 case 'a': /* author ... */
1132 return format_person_part(sb, placeholder[1],
d36f8679 1133 msg + c->author.off, c->author.len,
dd2e794a 1134 c->pretty_ctx->date_mode);
c3a670de
MC
1135 case 'c': /* committer ... */
1136 return format_person_part(sb, placeholder[1],
d36f8679 1137 msg + c->committer.off, c->committer.len,
dd2e794a 1138 c->pretty_ctx->date_mode);
c3a670de 1139 case 'e': /* encoding */
f29d5958 1140 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
c3a670de 1141 return 1;
1367b12a
EB
1142 case 'B': /* raw body */
1143 /* message_off is always left at the initial newline */
1144 strbuf_addstr(sb, msg + c->message_off + 1);
1145 return 1;
f53bd743
RS
1146 }
1147
1148 /* Now we need to parse the commit message. */
1149 if (!c->commit_message_parsed)
1150 parse_commit_message(c);
1151
1152 switch (placeholder[0]) {
1153 case 's': /* subject */
1154 format_subject(sb, msg + c->subject_off, " ");
1155 return 1;
46d164b0
SB
1156 case 'f': /* sanitized subject */
1157 format_sanitized_subject(sb, msg + c->subject_off);
1158 return 1;
c3a670de 1159 case 'b': /* body */
f29d5958 1160 strbuf_addstr(sb, msg + c->body_off);
c3a670de 1161 return 1;
93fc05eb 1162 }
c3a670de 1163 return 0; /* unknown placeholder */
cde75e59
RS
1164}
1165
9fa708da
JH
1166static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
1167 void *context)
1168{
1169 int consumed;
1170 size_t orig_len;
1171 enum {
1172 NO_MAGIC,
1173 ADD_LF_BEFORE_NON_EMPTY,
1174 DEL_LF_BEFORE_EMPTY,
223a923c 1175 ADD_SP_BEFORE_NON_EMPTY
9fa708da
JH
1176 } magic = NO_MAGIC;
1177
1178 switch (placeholder[0]) {
1179 case '-':
1180 magic = DEL_LF_BEFORE_EMPTY;
1181 break;
1182 case '+':
1183 magic = ADD_LF_BEFORE_NON_EMPTY;
1184 break;
7b88176e
MG
1185 case ' ':
1186 magic = ADD_SP_BEFORE_NON_EMPTY;
1187 break;
9fa708da
JH
1188 default:
1189 break;
1190 }
1191 if (magic != NO_MAGIC)
1192 placeholder++;
1193
1194 orig_len = sb->len;
1195 consumed = format_commit_one(sb, placeholder, context);
1196 if (magic == NO_MAGIC)
1197 return consumed;
1198
1199 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1200 while (sb->len && sb->buf[sb->len - 1] == '\n')
1201 strbuf_setlen(sb, sb->len - 1);
7b88176e
MG
1202 } else if (orig_len != sb->len) {
1203 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1204 strbuf_insert(sb, orig_len, "\n", 1);
1205 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1206 strbuf_insert(sb, orig_len, " ", 1);
9fa708da
JH
1207 }
1208 return consumed + 1;
1209}
1210
5b163603
JG
1211static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1212 void *context)
1213{
1214 struct userformat_want *w = context;
1215
7b88176e 1216 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
5b163603
JG
1217 placeholder++;
1218
1219 switch (*placeholder) {
1220 case 'N':
1221 w->notes = 1;
1222 break;
1223 }
1224 return 0;
1225}
1226
1227void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1228{
1229 struct strbuf dummy = STRBUF_INIT;
1230
1231 if (!fmt) {
1232 if (!user_format)
1233 return;
1234 fmt = user_format;
1235 }
a6253d10 1236 strbuf_expand(&dummy, fmt, userformat_want_item, w);
5b163603
JG
1237 strbuf_release(&dummy);
1238}
1239
cde75e59 1240void format_commit_message(const struct commit *commit,
7f98ebc8 1241 const char *format, struct strbuf *sb,
dd2e794a 1242 const struct pretty_print_context *pretty_ctx)
cde75e59 1243{
f29d5958 1244 struct format_commit_context context;
177b29dc 1245 static const char utf8[] = "UTF-8";
177b29dc 1246 const char *output_enc = pretty_ctx->output_encoding;
f29d5958
RS
1247
1248 memset(&context, 0, sizeof(context));
1249 context.commit = commit;
dd2e794a 1250 context.pretty_ctx = pretty_ctx;
02edd56b 1251 context.wrap_start = sb->len;
177b29dc
PN
1252 context.message = commit->buffer;
1253 if (output_enc) {
9cd7a92b 1254 char *enc = get_header(commit, "encoding");
1d5bd615 1255 if (strcmp(enc ? enc : utf8, output_enc)) {
177b29dc 1256 context.message = logmsg_reencode(commit, output_enc);
1d5bd615
NTND
1257 if (!context.message)
1258 context.message = commit->buffer;
1259 }
9cd7a92b 1260 free(enc);
177b29dc
PN
1261 }
1262
c3a670de 1263 strbuf_expand(sb, format, format_commit_item, &context);
02edd56b 1264 rewrap_message_tail(sb, &context, 0, 0, 0);
177b29dc
PN
1265
1266 if (context.message != commit->buffer)
1267 free(context.message);
f6667c5e
JH
1268 free(context.signature.gpg_output);
1269 free(context.signature.signer);
93fc05eb
JS
1270}
1271
6bf13944 1272static void pp_header(const struct pretty_print_context *pp,
93fc05eb
JS
1273 const char *encoding,
1274 const struct commit *commit,
1275 const char **msg_p,
1276 struct strbuf *sb)
1277{
1278 int parents_shown = 0;
1279
1280 for (;;) {
1281 const char *line = *msg_p;
1282 int linelen = get_one_line(*msg_p);
1283
1284 if (!linelen)
1285 return;
1286 *msg_p += linelen;
1287
1288 if (linelen == 1)
1289 /* End of header */
1290 return;
1291
6bf13944 1292 if (pp->fmt == CMIT_FMT_RAW) {
93fc05eb
JS
1293 strbuf_add(sb, line, linelen);
1294 continue;
1295 }
1296
1297 if (!memcmp(line, "parent ", 7)) {
1298 if (linelen != 48)
1299 die("bad parent line in commit");
1300 continue;
1301 }
1302
1303 if (!parents_shown) {
1304 struct commit_list *parent;
1305 int num;
1306 for (parent = commit->parents, num = 0;
1307 parent;
1308 parent = parent->next, num++)
1309 ;
1310 /* with enough slop */
1311 strbuf_grow(sb, num * 50 + 20);
6bf13944 1312 add_merge_info(pp, sb, commit);
93fc05eb
JS
1313 parents_shown = 1;
1314 }
1315
1316 /*
1317 * MEDIUM == DEFAULT shows only author with dates.
1318 * FULL shows both authors but not dates.
1319 * FULLER shows both authors and dates.
1320 */
1321 if (!memcmp(line, "author ", 7)) {
1322 strbuf_grow(sb, linelen + 80);
6bf13944 1323 pp_user_info(pp, "Author", sb, line + 7, encoding);
93fc05eb
JS
1324 }
1325 if (!memcmp(line, "committer ", 10) &&
6bf13944 1326 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
93fc05eb 1327 strbuf_grow(sb, linelen + 80);
6bf13944 1328 pp_user_info(pp, "Commit", sb, line + 10, encoding);
93fc05eb
JS
1329 }
1330 }
1331}
1332
6bf13944 1333void pp_title_line(const struct pretty_print_context *pp,
b02bd65f
DB
1334 const char **msg_p,
1335 struct strbuf *sb,
b02bd65f 1336 const char *encoding,
267123b4 1337 int need_8bit_cte)
93fc05eb
JS
1338{
1339 struct strbuf title;
1340
1341 strbuf_init(&title, 80);
9553d2b2
JK
1342 *msg_p = format_subject(&title, *msg_p,
1343 pp->preserve_subject ? "\n" : " ");
93fc05eb
JS
1344
1345 strbuf_grow(sb, title.len + 1024);
6bf13944
JK
1346 if (pp->subject) {
1347 strbuf_addstr(sb, pp->subject);
0fcec2ce 1348 add_rfc2047(sb, title.buf, title.len, encoding, RFC2047_SUBJECT);
93fc05eb
JS
1349 } else {
1350 strbuf_addbuf(sb, &title);
1351 }
1352 strbuf_addch(sb, '\n');
1353
6bf4f1b4 1354 if (need_8bit_cte > 0) {
93fc05eb
JS
1355 const char *header_fmt =
1356 "MIME-Version: 1.0\n"
1357 "Content-Type: text/plain; charset=%s\n"
1358 "Content-Transfer-Encoding: 8bit\n";
1359 strbuf_addf(sb, header_fmt, encoding);
1360 }
6bf13944
JK
1361 if (pp->after_subject) {
1362 strbuf_addstr(sb, pp->after_subject);
93fc05eb 1363 }
6bf13944 1364 if (pp->fmt == CMIT_FMT_EMAIL) {
93fc05eb
JS
1365 strbuf_addch(sb, '\n');
1366 }
1367 strbuf_release(&title);
1368}
1369
6bf13944 1370void pp_remainder(const struct pretty_print_context *pp,
b02bd65f
DB
1371 const char **msg_p,
1372 struct strbuf *sb,
1373 int indent)
93fc05eb
JS
1374{
1375 int first = 1;
1376 for (;;) {
1377 const char *line = *msg_p;
1378 int linelen = get_one_line(line);
1379 *msg_p += linelen;
1380
1381 if (!linelen)
1382 break;
1383
1384 if (is_empty_line(line, &linelen)) {
1385 if (first)
1386 continue;
6bf13944 1387 if (pp->fmt == CMIT_FMT_SHORT)
93fc05eb
JS
1388 break;
1389 }
1390 first = 0;
1391
1392 strbuf_grow(sb, linelen + indent + 20);
1393 if (indent) {
1394 memset(sb->buf + sb->len, ' ', indent);
1395 strbuf_setlen(sb, sb->len + indent);
1396 }
1397 strbuf_add(sb, line, linelen);
1398 strbuf_addch(sb, '\n');
1399 }
1400}
1401
69cd8f63
AG
1402char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
1403{
1404 const char *encoding;
1405
a6fa5992 1406 encoding = get_log_output_encoding();
69cd8f63
AG
1407 if (encoding_p)
1408 *encoding_p = encoding;
1409 return logmsg_reencode(commit, encoding);
1410}
1411
6bf13944
JK
1412void pretty_print_commit(const struct pretty_print_context *pp,
1413 const struct commit *commit,
1414 struct strbuf *sb)
93fc05eb
JS
1415{
1416 unsigned long beginning_of_body;
1417 int indent = 4;
1418 const char *msg = commit->buffer;
1419 char *reencoded;
1420 const char *encoding;
6bf13944 1421 int need_8bit_cte = pp->need_8bit_cte;
93fc05eb 1422
6bf13944
JK
1423 if (pp->fmt == CMIT_FMT_USERFORMAT) {
1424 format_commit_message(commit, user_format, sb, pp);
93fc05eb
JS
1425 return;
1426 }
1427
69cd8f63 1428 reencoded = reencode_commit_message(commit, &encoding);
93fc05eb
JS
1429 if (reencoded) {
1430 msg = reencoded;
1431 }
1432
6bf13944 1433 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
93fc05eb
JS
1434 indent = 0;
1435
6bf4f1b4
JH
1436 /*
1437 * We need to check and emit Content-type: to mark it
1438 * as 8-bit if we haven't done so.
93fc05eb 1439 */
6bf13944 1440 if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
93fc05eb
JS
1441 int i, ch, in_body;
1442
1443 for (in_body = i = 0; (ch = msg[i]); i++) {
1444 if (!in_body) {
1445 /* author could be non 7-bit ASCII but
1446 * the log may be so; skip over the
1447 * header part first.
1448 */
1449 if (ch == '\n' && msg[i+1] == '\n')
1450 in_body = 1;
1451 }
1452 else if (non_ascii(ch)) {
6bf4f1b4 1453 need_8bit_cte = 1;
93fc05eb
JS
1454 break;
1455 }
1456 }
1457 }
1458
6bf13944
JK
1459 pp_header(pp, encoding, commit, &msg, sb);
1460 if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
93fc05eb
JS
1461 strbuf_addch(sb, '\n');
1462 }
1463
1464 /* Skip excess blank lines at the beginning of body, if any... */
a0109668 1465 msg = skip_empty_lines(msg);
93fc05eb
JS
1466
1467 /* These formats treat the title line specially. */
6bf13944
JK
1468 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1469 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
93fc05eb
JS
1470
1471 beginning_of_body = sb->len;
6bf13944
JK
1472 if (pp->fmt != CMIT_FMT_ONELINE)
1473 pp_remainder(pp, &msg, sb, indent);
93fc05eb
JS
1474 strbuf_rtrim(sb);
1475
1476 /* Make sure there is an EOLN for the non-oneline case */
6bf13944 1477 if (pp->fmt != CMIT_FMT_ONELINE)
93fc05eb
JS
1478 strbuf_addch(sb, '\n');
1479
1480 /*
1481 * The caller may append additional body text in e-mail
1482 * format. Make sure we did not strip the blank line
1483 * between the header and the body.
1484 */
6bf13944 1485 if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
93fc05eb 1486 strbuf_addch(sb, '\n');
a97a7468 1487
6bf13944 1488 if (pp->show_notes)
894a9d33
TR
1489 format_display_notes(commit->object.sha1, sb, encoding,
1490 NOTES_SHOW_HEADER | NOTES_INDENT);
a97a7468 1491
93fc05eb
JS
1492 free(reencoded);
1493}
8b8a5374
JK
1494
1495void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
1496 struct strbuf *sb)
1497{
1498 struct pretty_print_context pp = {0};
6bf13944
JK
1499 pp.fmt = fmt;
1500 pretty_print_commit(&pp, commit, sb);
8b8a5374 1501}