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