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