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