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