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