pretty: make it easier to add new formats
[git/dscho.git] / pretty.c
blobf795a9a18d735b9385ded29bb55ae6ae5e9ac046
1 #include "cache.h"
2 #include "commit.h"
3 #include "utf8.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "string-list.h"
7 #include "mailmap.h"
8 #include "log-tree.h"
9 #include "notes.h"
10 #include "color.h"
11 #include "reflog-walk.h"
13 static char *user_format;
14 static struct cmt_fmt_map {
15 const char *name;
16 enum cmit_fmt format;
17 int is_tformat;
18 } *commit_formats;
19 static size_t commit_formats_len;
20 static struct cmt_fmt_map *find_commit_format(const char *sought);
22 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
24 free(user_format);
25 user_format = xstrdup(cp);
26 if (is_tformat)
27 rev->use_terminator = 1;
28 rev->commit_format = CMIT_FMT_USERFORMAT;
31 static void setup_commit_formats(void)
33 struct cmt_fmt_map builtin_formats[] = {
34 { "raw", CMIT_FMT_RAW, 0 },
35 { "medium", CMIT_FMT_MEDIUM, 0 },
36 { "short", CMIT_FMT_SHORT, 0 },
37 { "email", CMIT_FMT_EMAIL, 0 },
38 { "fuller", CMIT_FMT_FULLER, 0 },
39 { "full", CMIT_FMT_FULL, 0 },
40 { "oneline", CMIT_FMT_ONELINE, 1 }
42 commit_formats_len = ARRAY_SIZE(builtin_formats);
43 commit_formats = xmalloc(commit_formats_len *
44 sizeof(*builtin_formats));
45 memcpy(commit_formats, builtin_formats,
46 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
49 static struct cmt_fmt_map *find_commit_format(const char *sought)
51 struct cmt_fmt_map *found = NULL;
52 size_t found_match_len = 0;
53 int i;
55 if (!commit_formats)
56 setup_commit_formats();
58 for (i = 0; i < commit_formats_len; i++) {
59 size_t match_len;
61 if (prefixcmp(commit_formats[i].name, sought))
62 continue;
64 match_len = strlen(commit_formats[i].name);
65 if (found == NULL || found_match_len > match_len) {
66 found = &commit_formats[i];
67 found_match_len = match_len;
70 return found;
73 void get_commit_format(const char *arg, struct rev_info *rev)
75 struct cmt_fmt_map *commit_format;
77 rev->use_terminator = 0;
78 if (!arg || !*arg) {
79 rev->commit_format = CMIT_FMT_DEFAULT;
80 return;
82 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
83 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
84 return;
87 if (strchr(arg, '%')) {
88 save_user_format(rev, arg, 1);
89 return;
92 commit_format = find_commit_format(arg);
93 if (!commit_format)
94 die("invalid --pretty format: %s", arg);
96 rev->commit_format = commit_format->format;
97 rev->use_terminator = commit_format->is_tformat;
101 * Generic support for pretty-printing the header
103 static int get_one_line(const char *msg)
105 int ret = 0;
107 for (;;) {
108 char c = *msg++;
109 if (!c)
110 break;
111 ret++;
112 if (c == '\n')
113 break;
115 return ret;
118 /* High bit set, or ISO-2022-INT */
119 static int non_ascii(int ch)
121 return !isascii(ch) || ch == '\033';
124 int has_non_ascii(const char *s)
126 int ch;
127 if (!s)
128 return 0;
129 while ((ch = *s++) != '\0') {
130 if (non_ascii(ch))
131 return 1;
133 return 0;
136 static int is_rfc2047_special(char ch)
138 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
141 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
142 const char *encoding)
144 int i, last;
146 for (i = 0; i < len; i++) {
147 int ch = line[i];
148 if (non_ascii(ch))
149 goto needquote;
150 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
151 goto needquote;
153 strbuf_add(sb, line, len);
154 return;
156 needquote:
157 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
158 strbuf_addf(sb, "=?%s?q?", encoding);
159 for (i = last = 0; i < len; i++) {
160 unsigned ch = line[i] & 0xFF;
162 * We encode ' ' using '=20' even though rfc2047
163 * allows using '_' for readability. Unfortunately,
164 * many programs do not understand this and just
165 * leave the underscore in place.
167 if (is_rfc2047_special(ch) || ch == ' ') {
168 strbuf_add(sb, line + last, i - last);
169 strbuf_addf(sb, "=%02X", ch);
170 last = i + 1;
173 strbuf_add(sb, line + last, len - last);
174 strbuf_addstr(sb, "?=");
177 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
178 const char *line, enum date_mode dmode,
179 const char *encoding)
181 char *date;
182 int namelen;
183 unsigned long time;
184 int tz;
186 if (fmt == CMIT_FMT_ONELINE)
187 return;
188 date = strchr(line, '>');
189 if (!date)
190 return;
191 namelen = ++date - line;
192 time = strtoul(date, &date, 10);
193 tz = strtol(date, NULL, 10);
195 if (fmt == CMIT_FMT_EMAIL) {
196 char *name_tail = strchr(line, '<');
197 int display_name_length;
198 if (!name_tail)
199 return;
200 while (line < name_tail && isspace(name_tail[-1]))
201 name_tail--;
202 display_name_length = name_tail - line;
203 strbuf_addstr(sb, "From: ");
204 add_rfc2047(sb, line, display_name_length, encoding);
205 strbuf_add(sb, name_tail, namelen - display_name_length);
206 strbuf_addch(sb, '\n');
207 } else {
208 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
209 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
210 " ", namelen, line);
212 switch (fmt) {
213 case CMIT_FMT_MEDIUM:
214 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
215 break;
216 case CMIT_FMT_EMAIL:
217 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
218 break;
219 case CMIT_FMT_FULLER:
220 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
221 break;
222 default:
223 /* notin' */
224 break;
228 static int is_empty_line(const char *line, int *len_p)
230 int len = *len_p;
231 while (len && isspace(line[len-1]))
232 len--;
233 *len_p = len;
234 return !len;
237 static const char *skip_empty_lines(const char *msg)
239 for (;;) {
240 int linelen = get_one_line(msg);
241 int ll = linelen;
242 if (!linelen)
243 break;
244 if (!is_empty_line(msg, &ll))
245 break;
246 msg += linelen;
248 return msg;
251 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
252 const struct commit *commit, int abbrev)
254 struct commit_list *parent = commit->parents;
256 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
257 !parent || !parent->next)
258 return;
260 strbuf_addstr(sb, "Merge:");
262 while (parent) {
263 struct commit *p = parent->item;
264 const char *hex = NULL;
265 if (abbrev)
266 hex = find_unique_abbrev(p->object.sha1, abbrev);
267 if (!hex)
268 hex = sha1_to_hex(p->object.sha1);
269 parent = parent->next;
271 strbuf_addf(sb, " %s", hex);
273 strbuf_addch(sb, '\n');
276 static char *get_header(const struct commit *commit, const char *key)
278 int key_len = strlen(key);
279 const char *line = commit->buffer;
281 for (;;) {
282 const char *eol = strchr(line, '\n'), *next;
284 if (line == eol)
285 return NULL;
286 if (!eol) {
287 eol = line + strlen(line);
288 next = NULL;
289 } else
290 next = eol + 1;
291 if (eol - line > key_len &&
292 !strncmp(line, key, key_len) &&
293 line[key_len] == ' ') {
294 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
296 line = next;
300 static char *replace_encoding_header(char *buf, const char *encoding)
302 struct strbuf tmp = STRBUF_INIT;
303 size_t start, len;
304 char *cp = buf;
306 /* guess if there is an encoding header before a \n\n */
307 while (strncmp(cp, "encoding ", strlen("encoding "))) {
308 cp = strchr(cp, '\n');
309 if (!cp || *++cp == '\n')
310 return buf;
312 start = cp - buf;
313 cp = strchr(cp, '\n');
314 if (!cp)
315 return buf; /* should not happen but be defensive */
316 len = cp + 1 - (buf + start);
318 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
319 if (is_encoding_utf8(encoding)) {
320 /* we have re-coded to UTF-8; drop the header */
321 strbuf_remove(&tmp, start, len);
322 } else {
323 /* just replaces XXXX in 'encoding XXXX\n' */
324 strbuf_splice(&tmp, start + strlen("encoding "),
325 len - strlen("encoding \n"),
326 encoding, strlen(encoding));
328 return strbuf_detach(&tmp, NULL);
331 static char *logmsg_reencode(const struct commit *commit,
332 const char *output_encoding)
334 static const char *utf8 = "UTF-8";
335 const char *use_encoding;
336 char *encoding;
337 char *out;
339 if (!*output_encoding)
340 return NULL;
341 encoding = get_header(commit, "encoding");
342 use_encoding = encoding ? encoding : utf8;
343 if (!strcmp(use_encoding, output_encoding))
344 if (encoding) /* we'll strip encoding header later */
345 out = xstrdup(commit->buffer);
346 else
347 return NULL; /* nothing to do */
348 else
349 out = reencode_string(commit->buffer,
350 output_encoding, use_encoding);
351 if (out)
352 out = replace_encoding_header(out, output_encoding);
354 free(encoding);
355 return out;
358 static int mailmap_name(char *email, int email_len, char *name, int name_len)
360 static struct string_list *mail_map;
361 if (!mail_map) {
362 mail_map = xcalloc(1, sizeof(*mail_map));
363 read_mailmap(mail_map, NULL);
365 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
368 static size_t format_person_part(struct strbuf *sb, char part,
369 const char *msg, int len, enum date_mode dmode)
371 /* currently all placeholders have same length */
372 const int placeholder_len = 2;
373 int start, end, tz = 0;
374 unsigned long date = 0;
375 char *ep;
376 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
377 char person_name[1024];
378 char person_mail[1024];
380 /* advance 'end' to point to email start delimiter */
381 for (end = 0; end < len && msg[end] != '<'; end++)
382 ; /* do nothing */
385 * When end points at the '<' that we found, it should have
386 * matching '>' later, which means 'end' must be strictly
387 * below len - 1.
389 if (end >= len - 2)
390 goto skip;
392 /* Seek for both name and email part */
393 name_start = msg;
394 name_end = msg+end;
395 while (name_end > name_start && isspace(*(name_end-1)))
396 name_end--;
397 mail_start = msg+end+1;
398 mail_end = mail_start;
399 while (mail_end < msg_end && *mail_end != '>')
400 mail_end++;
401 if (mail_end == msg_end)
402 goto skip;
403 end = mail_end-msg;
405 if (part == 'N' || part == 'E') { /* mailmap lookup */
406 strlcpy(person_name, name_start, name_end-name_start+1);
407 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
408 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
409 name_start = person_name;
410 name_end = name_start + strlen(person_name);
411 mail_start = person_mail;
412 mail_end = mail_start + strlen(person_mail);
414 if (part == 'n' || part == 'N') { /* name */
415 strbuf_add(sb, name_start, name_end-name_start);
416 return placeholder_len;
418 if (part == 'e' || part == 'E') { /* email */
419 strbuf_add(sb, mail_start, mail_end-mail_start);
420 return placeholder_len;
423 /* advance 'start' to point to date start delimiter */
424 for (start = end + 1; start < len && isspace(msg[start]); start++)
425 ; /* do nothing */
426 if (start >= len)
427 goto skip;
428 date = strtoul(msg + start, &ep, 10);
429 if (msg + start == ep)
430 goto skip;
432 if (part == 't') { /* date, UNIX timestamp */
433 strbuf_add(sb, msg + start, ep - (msg + start));
434 return placeholder_len;
437 /* parse tz */
438 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
439 ; /* do nothing */
440 if (start + 1 < len) {
441 tz = strtoul(msg + start + 1, NULL, 10);
442 if (msg[start] == '-')
443 tz = -tz;
446 switch (part) {
447 case 'd': /* date */
448 strbuf_addstr(sb, show_date(date, tz, dmode));
449 return placeholder_len;
450 case 'D': /* date, RFC2822 style */
451 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
452 return placeholder_len;
453 case 'r': /* date, relative */
454 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
455 return placeholder_len;
456 case 'i': /* date, ISO 8601 */
457 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
458 return placeholder_len;
461 skip:
463 * bogus commit, 'sb' cannot be updated, but we still need to
464 * compute a valid return value.
466 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
467 || part == 'D' || part == 'r' || part == 'i')
468 return placeholder_len;
470 return 0; /* unknown placeholder */
473 struct chunk {
474 size_t off;
475 size_t len;
478 struct format_commit_context {
479 const struct commit *commit;
480 const struct pretty_print_context *pretty_ctx;
481 unsigned commit_header_parsed:1;
482 unsigned commit_message_parsed:1;
483 size_t width, indent1, indent2;
485 /* These offsets are relative to the start of the commit message. */
486 struct chunk author;
487 struct chunk committer;
488 struct chunk encoding;
489 size_t message_off;
490 size_t subject_off;
491 size_t body_off;
493 /* The following ones are relative to the result struct strbuf. */
494 struct chunk abbrev_commit_hash;
495 struct chunk abbrev_tree_hash;
496 struct chunk abbrev_parent_hashes;
497 size_t wrap_start;
500 static int add_again(struct strbuf *sb, struct chunk *chunk)
502 if (chunk->len) {
503 strbuf_adddup(sb, chunk->off, chunk->len);
504 return 1;
508 * We haven't seen this chunk before. Our caller is surely
509 * going to add it the hard way now. Remember the most likely
510 * start of the to-be-added chunk: the current end of the
511 * struct strbuf.
513 chunk->off = sb->len;
514 return 0;
517 static void parse_commit_header(struct format_commit_context *context)
519 const char *msg = context->commit->buffer;
520 int i;
522 for (i = 0; msg[i]; i++) {
523 int eol;
524 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
525 ; /* do nothing */
527 if (i == eol) {
528 break;
529 } else if (!prefixcmp(msg + i, "author ")) {
530 context->author.off = i + 7;
531 context->author.len = eol - i - 7;
532 } else if (!prefixcmp(msg + i, "committer ")) {
533 context->committer.off = i + 10;
534 context->committer.len = eol - i - 10;
535 } else if (!prefixcmp(msg + i, "encoding ")) {
536 context->encoding.off = i + 9;
537 context->encoding.len = eol - i - 9;
539 i = eol;
541 context->message_off = i;
542 context->commit_header_parsed = 1;
545 static int istitlechar(char c)
547 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
548 (c >= '0' && c <= '9') || c == '.' || c == '_';
551 static void format_sanitized_subject(struct strbuf *sb, const char *msg)
553 size_t trimlen;
554 size_t start_len = sb->len;
555 int space = 2;
557 for (; *msg && *msg != '\n'; msg++) {
558 if (istitlechar(*msg)) {
559 if (space == 1)
560 strbuf_addch(sb, '-');
561 space = 0;
562 strbuf_addch(sb, *msg);
563 if (*msg == '.')
564 while (*(msg+1) == '.')
565 msg++;
566 } else
567 space |= 1;
570 /* trim any trailing '.' or '-' characters */
571 trimlen = 0;
572 while (sb->len - trimlen > start_len &&
573 (sb->buf[sb->len - 1 - trimlen] == '.'
574 || sb->buf[sb->len - 1 - trimlen] == '-'))
575 trimlen++;
576 strbuf_remove(sb, sb->len - trimlen, trimlen);
579 const char *format_subject(struct strbuf *sb, const char *msg,
580 const char *line_separator)
582 int first = 1;
584 for (;;) {
585 const char *line = msg;
586 int linelen = get_one_line(line);
588 msg += linelen;
589 if (!linelen || is_empty_line(line, &linelen))
590 break;
592 if (!sb)
593 continue;
594 strbuf_grow(sb, linelen + 2);
595 if (!first)
596 strbuf_addstr(sb, line_separator);
597 strbuf_add(sb, line, linelen);
598 first = 0;
600 return msg;
603 static void parse_commit_message(struct format_commit_context *c)
605 const char *msg = c->commit->buffer + c->message_off;
606 const char *start = c->commit->buffer;
608 msg = skip_empty_lines(msg);
609 c->subject_off = msg - start;
611 msg = format_subject(NULL, msg, NULL);
612 msg = skip_empty_lines(msg);
613 c->body_off = msg - start;
615 c->commit_message_parsed = 1;
618 static void format_decoration(struct strbuf *sb, const struct commit *commit)
620 struct name_decoration *d;
621 const char *prefix = " (";
623 load_ref_decorations(DECORATE_SHORT_REFS);
624 d = lookup_decoration(&name_decoration, &commit->object);
625 while (d) {
626 strbuf_addstr(sb, prefix);
627 prefix = ", ";
628 strbuf_addstr(sb, d->name);
629 d = d->next;
631 if (prefix[0] == ',')
632 strbuf_addch(sb, ')');
635 static void strbuf_wrap(struct strbuf *sb, size_t pos,
636 size_t width, size_t indent1, size_t indent2)
638 struct strbuf tmp = STRBUF_INIT;
640 if (pos)
641 strbuf_add(&tmp, sb->buf, pos);
642 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
643 (int) indent1, (int) indent2, (int) width);
644 strbuf_swap(&tmp, sb);
645 strbuf_release(&tmp);
648 static void rewrap_message_tail(struct strbuf *sb,
649 struct format_commit_context *c,
650 size_t new_width, size_t new_indent1,
651 size_t new_indent2)
653 if (c->width == new_width && c->indent1 == new_indent1 &&
654 c->indent2 == new_indent2)
655 return;
656 if (c->wrap_start < sb->len)
657 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
658 c->wrap_start = sb->len;
659 c->width = new_width;
660 c->indent1 = new_indent1;
661 c->indent2 = new_indent2;
664 static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
665 void *context)
667 struct format_commit_context *c = context;
668 const struct commit *commit = c->commit;
669 const char *msg = commit->buffer;
670 struct commit_list *p;
671 int h1, h2;
673 /* these are independent of the commit */
674 switch (placeholder[0]) {
675 case 'C':
676 if (placeholder[1] == '(') {
677 const char *end = strchr(placeholder + 2, ')');
678 char color[COLOR_MAXLEN];
679 if (!end)
680 return 0;
681 color_parse_mem(placeholder + 2,
682 end - (placeholder + 2),
683 "--pretty format", color);
684 strbuf_addstr(sb, color);
685 return end - placeholder + 1;
687 if (!prefixcmp(placeholder + 1, "red")) {
688 strbuf_addstr(sb, GIT_COLOR_RED);
689 return 4;
690 } else if (!prefixcmp(placeholder + 1, "green")) {
691 strbuf_addstr(sb, GIT_COLOR_GREEN);
692 return 6;
693 } else if (!prefixcmp(placeholder + 1, "blue")) {
694 strbuf_addstr(sb, GIT_COLOR_BLUE);
695 return 5;
696 } else if (!prefixcmp(placeholder + 1, "reset")) {
697 strbuf_addstr(sb, GIT_COLOR_RESET);
698 return 6;
699 } else
700 return 0;
701 case 'n': /* newline */
702 strbuf_addch(sb, '\n');
703 return 1;
704 case 'x':
705 /* %x00 == NUL, %x0a == LF, etc. */
706 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
707 h1 <= 16 &&
708 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
709 h2 <= 16) {
710 strbuf_addch(sb, (h1<<4)|h2);
711 return 3;
712 } else
713 return 0;
714 case 'w':
715 if (placeholder[1] == '(') {
716 unsigned long width = 0, indent1 = 0, indent2 = 0;
717 char *next;
718 const char *start = placeholder + 2;
719 const char *end = strchr(start, ')');
720 if (!end)
721 return 0;
722 if (end > start) {
723 width = strtoul(start, &next, 10);
724 if (*next == ',') {
725 indent1 = strtoul(next + 1, &next, 10);
726 if (*next == ',') {
727 indent2 = strtoul(next + 1,
728 &next, 10);
731 if (*next != ')')
732 return 0;
734 rewrap_message_tail(sb, c, width, indent1, indent2);
735 return end - placeholder + 1;
736 } else
737 return 0;
740 /* these depend on the commit */
741 if (!commit->object.parsed)
742 parse_object(commit->object.sha1);
744 switch (placeholder[0]) {
745 case 'H': /* commit hash */
746 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
747 return 1;
748 case 'h': /* abbreviated commit hash */
749 if (add_again(sb, &c->abbrev_commit_hash))
750 return 1;
751 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
752 DEFAULT_ABBREV));
753 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
754 return 1;
755 case 'T': /* tree hash */
756 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
757 return 1;
758 case 't': /* abbreviated tree hash */
759 if (add_again(sb, &c->abbrev_tree_hash))
760 return 1;
761 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
762 DEFAULT_ABBREV));
763 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
764 return 1;
765 case 'P': /* parent hashes */
766 for (p = commit->parents; p; p = p->next) {
767 if (p != commit->parents)
768 strbuf_addch(sb, ' ');
769 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
771 return 1;
772 case 'p': /* abbreviated parent hashes */
773 if (add_again(sb, &c->abbrev_parent_hashes))
774 return 1;
775 for (p = commit->parents; p; p = p->next) {
776 if (p != commit->parents)
777 strbuf_addch(sb, ' ');
778 strbuf_addstr(sb, find_unique_abbrev(
779 p->item->object.sha1, DEFAULT_ABBREV));
781 c->abbrev_parent_hashes.len = sb->len -
782 c->abbrev_parent_hashes.off;
783 return 1;
784 case 'm': /* left/right/bottom */
785 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
786 ? '-'
787 : (commit->object.flags & SYMMETRIC_LEFT)
788 ? '<'
789 : '>');
790 return 1;
791 case 'd':
792 format_decoration(sb, commit);
793 return 1;
794 case 'g': /* reflog info */
795 switch(placeholder[1]) {
796 case 'd': /* reflog selector */
797 case 'D':
798 if (c->pretty_ctx->reflog_info)
799 get_reflog_selector(sb,
800 c->pretty_ctx->reflog_info,
801 c->pretty_ctx->date_mode,
802 (placeholder[1] == 'd'));
803 return 2;
804 case 's': /* reflog message */
805 if (c->pretty_ctx->reflog_info)
806 get_reflog_message(sb, c->pretty_ctx->reflog_info);
807 return 2;
809 return 0; /* unknown %g placeholder */
810 case 'N':
811 if (c->pretty_ctx->show_notes) {
812 format_display_notes(commit->object.sha1, sb,
813 git_log_output_encoding ? git_log_output_encoding
814 : git_commit_encoding, 0);
815 return 1;
817 return 0;
820 /* For the rest we have to parse the commit header. */
821 if (!c->commit_header_parsed)
822 parse_commit_header(c);
824 switch (placeholder[0]) {
825 case 'a': /* author ... */
826 return format_person_part(sb, placeholder[1],
827 msg + c->author.off, c->author.len,
828 c->pretty_ctx->date_mode);
829 case 'c': /* committer ... */
830 return format_person_part(sb, placeholder[1],
831 msg + c->committer.off, c->committer.len,
832 c->pretty_ctx->date_mode);
833 case 'e': /* encoding */
834 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
835 return 1;
838 /* Now we need to parse the commit message. */
839 if (!c->commit_message_parsed)
840 parse_commit_message(c);
842 switch (placeholder[0]) {
843 case 's': /* subject */
844 format_subject(sb, msg + c->subject_off, " ");
845 return 1;
846 case 'f': /* sanitized subject */
847 format_sanitized_subject(sb, msg + c->subject_off);
848 return 1;
849 case 'b': /* body */
850 strbuf_addstr(sb, msg + c->body_off);
851 return 1;
853 return 0; /* unknown placeholder */
856 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
857 void *context)
859 int consumed;
860 size_t orig_len;
861 enum {
862 NO_MAGIC,
863 ADD_LF_BEFORE_NON_EMPTY,
864 DEL_LF_BEFORE_EMPTY,
865 } magic = NO_MAGIC;
867 switch (placeholder[0]) {
868 case '-':
869 magic = DEL_LF_BEFORE_EMPTY;
870 break;
871 case '+':
872 magic = ADD_LF_BEFORE_NON_EMPTY;
873 break;
874 default:
875 break;
877 if (magic != NO_MAGIC)
878 placeholder++;
880 orig_len = sb->len;
881 consumed = format_commit_one(sb, placeholder, context);
882 if (magic == NO_MAGIC)
883 return consumed;
885 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
886 while (sb->len && sb->buf[sb->len - 1] == '\n')
887 strbuf_setlen(sb, sb->len - 1);
888 } else if ((orig_len != sb->len) && magic == ADD_LF_BEFORE_NON_EMPTY) {
889 strbuf_insert(sb, orig_len, "\n", 1);
891 return consumed + 1;
894 static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
895 void *context)
897 struct userformat_want *w = context;
899 if (*placeholder == '+' || *placeholder == '-')
900 placeholder++;
902 switch (*placeholder) {
903 case 'N':
904 w->notes = 1;
905 break;
907 return 0;
910 void userformat_find_requirements(const char *fmt, struct userformat_want *w)
912 struct strbuf dummy = STRBUF_INIT;
914 if (!fmt) {
915 if (!user_format)
916 return;
917 fmt = user_format;
919 strbuf_expand(&dummy, user_format, userformat_want_item, w);
920 strbuf_release(&dummy);
923 void format_commit_message(const struct commit *commit,
924 const char *format, struct strbuf *sb,
925 const struct pretty_print_context *pretty_ctx)
927 struct format_commit_context context;
929 memset(&context, 0, sizeof(context));
930 context.commit = commit;
931 context.pretty_ctx = pretty_ctx;
932 context.wrap_start = sb->len;
933 strbuf_expand(sb, format, format_commit_item, &context);
934 rewrap_message_tail(sb, &context, 0, 0, 0);
937 static void pp_header(enum cmit_fmt fmt,
938 int abbrev,
939 enum date_mode dmode,
940 const char *encoding,
941 const struct commit *commit,
942 const char **msg_p,
943 struct strbuf *sb)
945 int parents_shown = 0;
947 for (;;) {
948 const char *line = *msg_p;
949 int linelen = get_one_line(*msg_p);
951 if (!linelen)
952 return;
953 *msg_p += linelen;
955 if (linelen == 1)
956 /* End of header */
957 return;
959 if (fmt == CMIT_FMT_RAW) {
960 strbuf_add(sb, line, linelen);
961 continue;
964 if (!memcmp(line, "parent ", 7)) {
965 if (linelen != 48)
966 die("bad parent line in commit");
967 continue;
970 if (!parents_shown) {
971 struct commit_list *parent;
972 int num;
973 for (parent = commit->parents, num = 0;
974 parent;
975 parent = parent->next, num++)
977 /* with enough slop */
978 strbuf_grow(sb, num * 50 + 20);
979 add_merge_info(fmt, sb, commit, abbrev);
980 parents_shown = 1;
984 * MEDIUM == DEFAULT shows only author with dates.
985 * FULL shows both authors but not dates.
986 * FULLER shows both authors and dates.
988 if (!memcmp(line, "author ", 7)) {
989 strbuf_grow(sb, linelen + 80);
990 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
992 if (!memcmp(line, "committer ", 10) &&
993 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
994 strbuf_grow(sb, linelen + 80);
995 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
1000 void pp_title_line(enum cmit_fmt fmt,
1001 const char **msg_p,
1002 struct strbuf *sb,
1003 const char *subject,
1004 const char *after_subject,
1005 const char *encoding,
1006 int need_8bit_cte)
1008 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
1009 struct strbuf title;
1011 strbuf_init(&title, 80);
1012 *msg_p = format_subject(&title, *msg_p, line_separator);
1014 strbuf_grow(sb, title.len + 1024);
1015 if (subject) {
1016 strbuf_addstr(sb, subject);
1017 add_rfc2047(sb, title.buf, title.len, encoding);
1018 } else {
1019 strbuf_addbuf(sb, &title);
1021 strbuf_addch(sb, '\n');
1023 if (need_8bit_cte > 0) {
1024 const char *header_fmt =
1025 "MIME-Version: 1.0\n"
1026 "Content-Type: text/plain; charset=%s\n"
1027 "Content-Transfer-Encoding: 8bit\n";
1028 strbuf_addf(sb, header_fmt, encoding);
1030 if (after_subject) {
1031 strbuf_addstr(sb, after_subject);
1033 if (fmt == CMIT_FMT_EMAIL) {
1034 strbuf_addch(sb, '\n');
1036 strbuf_release(&title);
1039 void pp_remainder(enum cmit_fmt fmt,
1040 const char **msg_p,
1041 struct strbuf *sb,
1042 int indent)
1044 int first = 1;
1045 for (;;) {
1046 const char *line = *msg_p;
1047 int linelen = get_one_line(line);
1048 *msg_p += linelen;
1050 if (!linelen)
1051 break;
1053 if (is_empty_line(line, &linelen)) {
1054 if (first)
1055 continue;
1056 if (fmt == CMIT_FMT_SHORT)
1057 break;
1059 first = 0;
1061 strbuf_grow(sb, linelen + indent + 20);
1062 if (indent) {
1063 memset(sb->buf + sb->len, ' ', indent);
1064 strbuf_setlen(sb, sb->len + indent);
1066 strbuf_add(sb, line, linelen);
1067 strbuf_addch(sb, '\n');
1071 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
1073 const char *encoding;
1075 encoding = (git_log_output_encoding
1076 ? git_log_output_encoding
1077 : git_commit_encoding);
1078 if (!encoding)
1079 encoding = "UTF-8";
1080 if (encoding_p)
1081 *encoding_p = encoding;
1082 return logmsg_reencode(commit, encoding);
1085 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1086 struct strbuf *sb,
1087 const struct pretty_print_context *context)
1089 unsigned long beginning_of_body;
1090 int indent = 4;
1091 const char *msg = commit->buffer;
1092 char *reencoded;
1093 const char *encoding;
1094 int need_8bit_cte = context->need_8bit_cte;
1096 if (fmt == CMIT_FMT_USERFORMAT) {
1097 format_commit_message(commit, user_format, sb, context);
1098 return;
1101 reencoded = reencode_commit_message(commit, &encoding);
1102 if (reencoded) {
1103 msg = reencoded;
1106 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1107 indent = 0;
1110 * We need to check and emit Content-type: to mark it
1111 * as 8-bit if we haven't done so.
1113 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1114 int i, ch, in_body;
1116 for (in_body = i = 0; (ch = msg[i]); i++) {
1117 if (!in_body) {
1118 /* author could be non 7-bit ASCII but
1119 * the log may be so; skip over the
1120 * header part first.
1122 if (ch == '\n' && msg[i+1] == '\n')
1123 in_body = 1;
1125 else if (non_ascii(ch)) {
1126 need_8bit_cte = 1;
1127 break;
1132 pp_header(fmt, context->abbrev, context->date_mode, encoding,
1133 commit, &msg, sb);
1134 if (fmt != CMIT_FMT_ONELINE && !context->subject) {
1135 strbuf_addch(sb, '\n');
1138 /* Skip excess blank lines at the beginning of body, if any... */
1139 msg = skip_empty_lines(msg);
1141 /* These formats treat the title line specially. */
1142 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1143 pp_title_line(fmt, &msg, sb, context->subject,
1144 context->after_subject, encoding, need_8bit_cte);
1146 beginning_of_body = sb->len;
1147 if (fmt != CMIT_FMT_ONELINE)
1148 pp_remainder(fmt, &msg, sb, indent);
1149 strbuf_rtrim(sb);
1151 /* Make sure there is an EOLN for the non-oneline case */
1152 if (fmt != CMIT_FMT_ONELINE)
1153 strbuf_addch(sb, '\n');
1156 * The caller may append additional body text in e-mail
1157 * format. Make sure we did not strip the blank line
1158 * between the header and the body.
1160 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1161 strbuf_addch(sb, '\n');
1163 if (context->show_notes)
1164 format_display_notes(commit->object.sha1, sb, encoding,
1165 NOTES_SHOW_HEADER | NOTES_INDENT);
1167 free(reencoded);