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