Add "%w" to pretty formats, which rewraps the commit message
[git/dscho.git] / pretty.c
blob3b69fa4359aef04807cf225b1f4fa1300abe5c23
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 "color.h"
11 static char *user_format;
13 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
15 free(user_format);
16 user_format = xstrdup(cp);
17 if (is_tformat)
18 rev->use_terminator = 1;
19 rev->commit_format = CMIT_FMT_USERFORMAT;
22 void get_commit_format(const char *arg, struct rev_info *rev)
24 int i;
25 static struct cmt_fmt_map {
26 const char *n;
27 size_t cmp_len;
28 enum cmit_fmt v;
29 } cmt_fmts[] = {
30 { "raw", 1, CMIT_FMT_RAW },
31 { "medium", 1, CMIT_FMT_MEDIUM },
32 { "short", 1, CMIT_FMT_SHORT },
33 { "email", 1, CMIT_FMT_EMAIL },
34 { "full", 5, CMIT_FMT_FULL },
35 { "fuller", 5, CMIT_FMT_FULLER },
36 { "oneline", 1, CMIT_FMT_ONELINE },
39 rev->use_terminator = 0;
40 if (!arg || !*arg) {
41 rev->commit_format = CMIT_FMT_DEFAULT;
42 return;
44 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
45 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
46 return;
48 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
49 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
50 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
51 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
52 rev->use_terminator = 1;
53 rev->commit_format = cmt_fmts[i].v;
54 return;
57 if (strchr(arg, '%')) {
58 save_user_format(rev, arg, 1);
59 return;
62 die("invalid --pretty format: %s", arg);
66 * Generic support for pretty-printing the header
68 static int get_one_line(const char *msg)
70 int ret = 0;
72 for (;;) {
73 char c = *msg++;
74 if (!c)
75 break;
76 ret++;
77 if (c == '\n')
78 break;
80 return ret;
83 /* High bit set, or ISO-2022-INT */
84 int non_ascii(int ch)
86 return !isascii(ch) || ch == '\033';
89 int has_non_ascii(const char *s)
91 int ch;
92 if (!s)
93 return 0;
94 while ((ch = *s++) != '\0') {
95 if (non_ascii(ch))
96 return 1;
98 return 0;
101 static int is_rfc2047_special(char ch)
103 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
106 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
107 const char *encoding)
109 int i, last;
111 for (i = 0; i < len; i++) {
112 int ch = line[i];
113 if (non_ascii(ch))
114 goto needquote;
115 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
116 goto needquote;
118 strbuf_add(sb, line, len);
119 return;
121 needquote:
122 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
123 strbuf_addf(sb, "=?%s?q?", encoding);
124 for (i = last = 0; i < len; i++) {
125 unsigned ch = line[i] & 0xFF;
127 * We encode ' ' using '=20' even though rfc2047
128 * allows using '_' for readability. Unfortunately,
129 * many programs do not understand this and just
130 * leave the underscore in place.
132 if (is_rfc2047_special(ch) || ch == ' ') {
133 strbuf_add(sb, line + last, i - last);
134 strbuf_addf(sb, "=%02X", ch);
135 last = i + 1;
138 strbuf_add(sb, line + last, len - last);
139 strbuf_addstr(sb, "?=");
142 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
143 const char *line, enum date_mode dmode,
144 const char *encoding)
146 char *date;
147 int namelen;
148 unsigned long time;
149 int tz;
151 if (fmt == CMIT_FMT_ONELINE)
152 return;
153 date = strchr(line, '>');
154 if (!date)
155 return;
156 namelen = ++date - line;
157 time = strtoul(date, &date, 10);
158 tz = strtol(date, NULL, 10);
160 if (fmt == CMIT_FMT_EMAIL) {
161 char *name_tail = strchr(line, '<');
162 int display_name_length;
163 if (!name_tail)
164 return;
165 while (line < name_tail && isspace(name_tail[-1]))
166 name_tail--;
167 display_name_length = name_tail - line;
168 strbuf_addstr(sb, "From: ");
169 add_rfc2047(sb, line, display_name_length, encoding);
170 strbuf_add(sb, name_tail, namelen - display_name_length);
171 strbuf_addch(sb, '\n');
172 } else {
173 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
174 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
175 " ", namelen, line);
177 switch (fmt) {
178 case CMIT_FMT_MEDIUM:
179 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
180 break;
181 case CMIT_FMT_EMAIL:
182 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
183 break;
184 case CMIT_FMT_FULLER:
185 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
186 break;
187 default:
188 /* notin' */
189 break;
193 static int is_empty_line(const char *line, int *len_p)
195 int len = *len_p;
196 while (len && isspace(line[len-1]))
197 len--;
198 *len_p = len;
199 return !len;
202 static const char *skip_empty_lines(const char *msg)
204 for (;;) {
205 int linelen = get_one_line(msg);
206 int ll = linelen;
207 if (!linelen)
208 break;
209 if (!is_empty_line(msg, &ll))
210 break;
211 msg += linelen;
213 return msg;
216 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
217 const struct commit *commit, int abbrev)
219 struct commit_list *parent = commit->parents;
221 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
222 !parent || !parent->next)
223 return;
225 strbuf_addstr(sb, "Merge:");
227 while (parent) {
228 struct commit *p = parent->item;
229 const char *hex = NULL;
230 if (abbrev)
231 hex = find_unique_abbrev(p->object.sha1, abbrev);
232 if (!hex)
233 hex = sha1_to_hex(p->object.sha1);
234 parent = parent->next;
236 strbuf_addf(sb, " %s", hex);
238 strbuf_addch(sb, '\n');
241 static char *get_header(const struct commit *commit, const char *key)
243 int key_len = strlen(key);
244 const char *line = commit->buffer;
246 for (;;) {
247 const char *eol = strchr(line, '\n'), *next;
249 if (line == eol)
250 return NULL;
251 if (!eol) {
252 eol = line + strlen(line);
253 next = NULL;
254 } else
255 next = eol + 1;
256 if (eol - line > key_len &&
257 !strncmp(line, key, key_len) &&
258 line[key_len] == ' ') {
259 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
261 line = next;
265 static char *replace_encoding_header(char *buf, const char *encoding)
267 struct strbuf tmp = STRBUF_INIT;
268 size_t start, len;
269 char *cp = buf;
271 /* guess if there is an encoding header before a \n\n */
272 while (strncmp(cp, "encoding ", strlen("encoding "))) {
273 cp = strchr(cp, '\n');
274 if (!cp || *++cp == '\n')
275 return buf;
277 start = cp - buf;
278 cp = strchr(cp, '\n');
279 if (!cp)
280 return buf; /* should not happen but be defensive */
281 len = cp + 1 - (buf + start);
283 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
284 if (is_encoding_utf8(encoding)) {
285 /* we have re-coded to UTF-8; drop the header */
286 strbuf_remove(&tmp, start, len);
287 } else {
288 /* just replaces XXXX in 'encoding XXXX\n' */
289 strbuf_splice(&tmp, start + strlen("encoding "),
290 len - strlen("encoding \n"),
291 encoding, strlen(encoding));
293 return strbuf_detach(&tmp, NULL);
296 static char *logmsg_reencode(const struct commit *commit,
297 const char *output_encoding)
299 static const char *utf8 = "UTF-8";
300 const char *use_encoding;
301 char *encoding;
302 char *out;
304 if (!*output_encoding)
305 return NULL;
306 encoding = get_header(commit, "encoding");
307 use_encoding = encoding ? encoding : utf8;
308 if (!strcmp(use_encoding, output_encoding))
309 if (encoding) /* we'll strip encoding header later */
310 out = xstrdup(commit->buffer);
311 else
312 return NULL; /* nothing to do */
313 else
314 out = reencode_string(commit->buffer,
315 output_encoding, use_encoding);
316 if (out)
317 out = replace_encoding_header(out, output_encoding);
319 free(encoding);
320 return out;
323 static int mailmap_name(char *email, int email_len, char *name, int name_len)
325 static struct string_list *mail_map;
326 if (!mail_map) {
327 mail_map = xcalloc(1, sizeof(*mail_map));
328 read_mailmap(mail_map, NULL);
330 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
333 static size_t format_person_part(struct strbuf *sb, char part,
334 const char *msg, int len, enum date_mode dmode)
336 /* currently all placeholders have same length */
337 const int placeholder_len = 2;
338 int start, end, tz = 0;
339 unsigned long date = 0;
340 char *ep;
341 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
342 char person_name[1024];
343 char person_mail[1024];
345 /* advance 'end' to point to email start delimiter */
346 for (end = 0; end < len && msg[end] != '<'; end++)
347 ; /* do nothing */
350 * When end points at the '<' that we found, it should have
351 * matching '>' later, which means 'end' must be strictly
352 * below len - 1.
354 if (end >= len - 2)
355 goto skip;
357 /* Seek for both name and email part */
358 name_start = msg;
359 name_end = msg+end;
360 while (name_end > name_start && isspace(*(name_end-1)))
361 name_end--;
362 mail_start = msg+end+1;
363 mail_end = mail_start;
364 while (mail_end < msg_end && *mail_end != '>')
365 mail_end++;
366 if (mail_end == msg_end)
367 goto skip;
368 end = mail_end-msg;
370 if (part == 'N' || part == 'E') { /* mailmap lookup */
371 strlcpy(person_name, name_start, name_end-name_start+1);
372 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
373 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
374 name_start = person_name;
375 name_end = name_start + strlen(person_name);
376 mail_start = person_mail;
377 mail_end = mail_start + strlen(person_mail);
379 if (part == 'n' || part == 'N') { /* name */
380 strbuf_add(sb, name_start, name_end-name_start);
381 return placeholder_len;
383 if (part == 'e' || part == 'E') { /* email */
384 strbuf_add(sb, mail_start, mail_end-mail_start);
385 return placeholder_len;
388 /* advance 'start' to point to date start delimiter */
389 for (start = end + 1; start < len && isspace(msg[start]); start++)
390 ; /* do nothing */
391 if (start >= len)
392 goto skip;
393 date = strtoul(msg + start, &ep, 10);
394 if (msg + start == ep)
395 goto skip;
397 if (part == 't') { /* date, UNIX timestamp */
398 strbuf_add(sb, msg + start, ep - (msg + start));
399 return placeholder_len;
402 /* parse tz */
403 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
404 ; /* do nothing */
405 if (start + 1 < len) {
406 tz = strtoul(msg + start + 1, NULL, 10);
407 if (msg[start] == '-')
408 tz = -tz;
411 switch (part) {
412 case 'd': /* date */
413 strbuf_addstr(sb, show_date(date, tz, dmode));
414 return placeholder_len;
415 case 'D': /* date, RFC2822 style */
416 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
417 return placeholder_len;
418 case 'r': /* date, relative */
419 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
420 return placeholder_len;
421 case 'i': /* date, ISO 8601 */
422 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
423 return placeholder_len;
426 skip:
428 * bogus commit, 'sb' cannot be updated, but we still need to
429 * compute a valid return value.
431 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
432 || part == 'D' || part == 'r' || part == 'i')
433 return placeholder_len;
435 return 0; /* unknown placeholder */
438 struct chunk {
439 size_t off;
440 size_t len;
443 struct format_commit_context {
444 const struct commit *commit;
445 enum date_mode dmode;
446 unsigned commit_header_parsed:1;
447 unsigned commit_message_parsed:1;
449 /* These offsets are relative to the start of the commit message. */
450 struct chunk author;
451 struct chunk committer;
452 struct chunk encoding;
453 size_t message_off;
454 size_t subject_off;
455 size_t body_off;
457 /* The following ones are relative to the result struct strbuf. */
458 struct chunk abbrev_commit_hash;
459 struct chunk abbrev_tree_hash;
460 struct chunk abbrev_parent_hashes;
462 /* for rewrapping */
463 int indent1, indent2, max_columns, rewrap_pacmanned;
466 static int add_again(struct strbuf *sb, struct chunk *chunk)
468 if (chunk->len) {
469 strbuf_adddup(sb, chunk->off, chunk->len);
470 return 1;
474 * We haven't seen this chunk before. Our caller is surely
475 * going to add it the hard way now. Remember the most likely
476 * start of the to-be-added chunk: the current end of the
477 * struct strbuf.
479 chunk->off = sb->len;
480 return 0;
483 static void parse_commit_header(struct format_commit_context *context)
485 const char *msg = context->commit->buffer;
486 int i;
488 for (i = 0; msg[i]; i++) {
489 int eol;
490 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
491 ; /* do nothing */
493 if (i == eol) {
494 break;
495 } else if (!prefixcmp(msg + i, "author ")) {
496 context->author.off = i + 7;
497 context->author.len = eol - i - 7;
498 } else if (!prefixcmp(msg + i, "committer ")) {
499 context->committer.off = i + 10;
500 context->committer.len = eol - i - 10;
501 } else if (!prefixcmp(msg + i, "encoding ")) {
502 context->encoding.off = i + 9;
503 context->encoding.len = eol - i - 9;
505 i = eol;
507 context->message_off = i;
508 context->commit_header_parsed = 1;
511 static int istitlechar(char c)
513 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
514 (c >= '0' && c <= '9') || c == '.' || c == '_';
517 static void format_sanitized_subject(struct strbuf *sb, const char *msg)
519 size_t trimlen;
520 size_t start_len = sb->len;
521 int space = 2;
523 for (; *msg && *msg != '\n'; msg++) {
524 if (istitlechar(*msg)) {
525 if (space == 1)
526 strbuf_addch(sb, '-');
527 space = 0;
528 strbuf_addch(sb, *msg);
529 if (*msg == '.')
530 while (*(msg+1) == '.')
531 msg++;
532 } else
533 space |= 1;
536 /* trim any trailing '.' or '-' characters */
537 trimlen = 0;
538 while (sb->len - trimlen > start_len &&
539 (sb->buf[sb->len - 1 - trimlen] == '.'
540 || sb->buf[sb->len - 1 - trimlen] == '-'))
541 trimlen++;
542 strbuf_remove(sb, sb->len - trimlen, trimlen);
545 const char *format_subject(struct strbuf *sb, const char *msg,
546 const char *line_separator)
548 int first = 1;
550 for (;;) {
551 const char *line = msg;
552 int linelen = get_one_line(line);
554 msg += linelen;
555 if (!linelen || is_empty_line(line, &linelen))
556 break;
558 if (!sb)
559 continue;
560 strbuf_grow(sb, linelen + 2);
561 if (!first)
562 strbuf_addstr(sb, line_separator);
563 strbuf_add(sb, line, linelen);
564 first = 0;
566 return msg;
569 static void parse_commit_message(struct format_commit_context *c)
571 const char *msg = c->commit->buffer + c->message_off;
572 const char *start = c->commit->buffer;
574 msg = skip_empty_lines(msg);
575 c->subject_off = msg - start;
577 msg = format_subject(NULL, msg, NULL);
578 msg = skip_empty_lines(msg);
579 c->body_off = msg - start;
581 c->commit_message_parsed = 1;
584 static void format_decoration(struct strbuf *sb, const struct commit *commit)
586 struct name_decoration *d;
587 const char *prefix = " (";
589 load_ref_decorations(DECORATE_SHORT_REFS);
590 d = lookup_decoration(&name_decoration, &commit->object);
591 while (d) {
592 strbuf_addstr(sb, prefix);
593 prefix = ", ";
594 strbuf_addstr(sb, d->name);
595 d = d->next;
597 if (prefix[0] == ',')
598 strbuf_addch(sb, ')');
601 static int rewrap_text(struct strbuf *sb, struct format_commit_context *c,
602 const char *params, const char *text)
604 if (!c->max_columns) {
605 c->indent1 = 4;
606 c->indent2 = 4;
607 c->max_columns = 80;
608 c->rewrap_pacmanned = 1;
610 if (params[0] == '(') {
611 char *p;
613 c->indent1 = strtoul(params + 1, &p, 0);
614 if (*p == ',') {
615 c->indent2 = strtoul(p + 1, &p, 0);
616 if (*p == ',')
617 c->max_columns = strtoul(p + 1, &p, 0);
619 if (*p != ')') {
620 error ("Invalid parameters: %.*s",
621 (int)(p - params), params);
622 return 0;
624 c->rewrap_pacmanned = p + 1 - params;
628 strbuf_add_wrapped_text(sb, text,
629 c->indent1, c->indent2, c->max_columns);
630 return c->rewrap_pacmanned;
633 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
634 void *context)
636 struct format_commit_context *c = context;
637 const struct commit *commit = c->commit;
638 const char *msg = commit->buffer;
639 struct commit_list *p;
640 int h1, h2;
642 /* these are independent of the commit */
643 switch (placeholder[0]) {
644 case 'C':
645 if (placeholder[1] == '(') {
646 const char *end = strchr(placeholder + 2, ')');
647 char color[COLOR_MAXLEN];
648 if (!end)
649 return 0;
650 color_parse_mem(placeholder + 2,
651 end - (placeholder + 2),
652 "--pretty format", color);
653 strbuf_addstr(sb, color);
654 return end - placeholder + 1;
656 if (!prefixcmp(placeholder + 1, "red")) {
657 strbuf_addstr(sb, GIT_COLOR_RED);
658 return 4;
659 } else if (!prefixcmp(placeholder + 1, "green")) {
660 strbuf_addstr(sb, GIT_COLOR_GREEN);
661 return 6;
662 } else if (!prefixcmp(placeholder + 1, "blue")) {
663 strbuf_addstr(sb, GIT_COLOR_BLUE);
664 return 5;
665 } else if (!prefixcmp(placeholder + 1, "reset")) {
666 strbuf_addstr(sb, GIT_COLOR_RESET);
667 return 6;
668 } else
669 return 0;
670 case 'n': /* newline */
671 strbuf_addch(sb, '\n');
672 return 1;
673 case 'x':
674 /* %x00 == NUL, %x0a == LF, etc. */
675 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
676 h1 <= 16 &&
677 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
678 h2 <= 16) {
679 strbuf_addch(sb, (h1<<4)|h2);
680 return 3;
681 } else
682 return 0;
685 /* these depend on the commit */
686 if (!commit->object.parsed)
687 parse_object(commit->object.sha1);
689 switch (placeholder[0]) {
690 case 'H': /* commit hash */
691 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
692 return 1;
693 case 'h': /* abbreviated commit hash */
694 if (add_again(sb, &c->abbrev_commit_hash))
695 return 1;
696 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
697 DEFAULT_ABBREV));
698 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
699 return 1;
700 case 'T': /* tree hash */
701 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
702 return 1;
703 case 't': /* abbreviated tree hash */
704 if (add_again(sb, &c->abbrev_tree_hash))
705 return 1;
706 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
707 DEFAULT_ABBREV));
708 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
709 return 1;
710 case 'P': /* parent hashes */
711 for (p = commit->parents; p; p = p->next) {
712 if (p != commit->parents)
713 strbuf_addch(sb, ' ');
714 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
716 return 1;
717 case 'p': /* abbreviated parent hashes */
718 if (add_again(sb, &c->abbrev_parent_hashes))
719 return 1;
720 for (p = commit->parents; p; p = p->next) {
721 if (p != commit->parents)
722 strbuf_addch(sb, ' ');
723 strbuf_addstr(sb, find_unique_abbrev(
724 p->item->object.sha1, DEFAULT_ABBREV));
726 c->abbrev_parent_hashes.len = sb->len -
727 c->abbrev_parent_hashes.off;
728 return 1;
729 case 'm': /* left/right/bottom */
730 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
731 ? '-'
732 : (commit->object.flags & SYMMETRIC_LEFT)
733 ? '<'
734 : '>');
735 return 1;
736 case 'd':
737 format_decoration(sb, commit);
738 return 1;
741 /* For the rest we have to parse the commit header. */
742 if (!c->commit_header_parsed)
743 parse_commit_header(c);
745 switch (placeholder[0]) {
746 case 'a': /* author ... */
747 return format_person_part(sb, placeholder[1],
748 msg + c->author.off, c->author.len,
749 c->dmode);
750 case 'c': /* committer ... */
751 return format_person_part(sb, placeholder[1],
752 msg + c->committer.off, c->committer.len,
753 c->dmode);
754 case 'e': /* encoding */
755 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
756 return 1;
759 /* Now we need to parse the commit message. */
760 if (!c->commit_message_parsed)
761 parse_commit_message(c);
763 switch (placeholder[0]) {
764 case 's': /* subject */
765 format_subject(sb, msg + c->subject_off, " ");
766 return 1;
767 case 'f': /* sanitized subject */
768 format_sanitized_subject(sb, msg + c->subject_off);
769 return 1;
770 case 'b': /* body */
771 strbuf_addstr(sb, msg + c->body_off);
772 return 1;
773 case 'w': /* re-wrapped body */
774 return rewrap_text(sb, c, placeholder + 1,
775 msg + c->subject_off) + 1;
777 return 0; /* unknown placeholder */
780 void format_commit_message(const struct commit *commit,
781 const void *format, struct strbuf *sb,
782 enum date_mode dmode)
784 struct format_commit_context context;
786 memset(&context, 0, sizeof(context));
787 context.commit = commit;
788 context.dmode = dmode;
789 strbuf_expand(sb, format, format_commit_item, &context);
792 static void pp_header(enum cmit_fmt fmt,
793 int abbrev,
794 enum date_mode dmode,
795 const char *encoding,
796 const struct commit *commit,
797 const char **msg_p,
798 struct strbuf *sb)
800 int parents_shown = 0;
802 for (;;) {
803 const char *line = *msg_p;
804 int linelen = get_one_line(*msg_p);
806 if (!linelen)
807 return;
808 *msg_p += linelen;
810 if (linelen == 1)
811 /* End of header */
812 return;
814 if (fmt == CMIT_FMT_RAW) {
815 strbuf_add(sb, line, linelen);
816 continue;
819 if (!memcmp(line, "parent ", 7)) {
820 if (linelen != 48)
821 die("bad parent line in commit");
822 continue;
825 if (!parents_shown) {
826 struct commit_list *parent;
827 int num;
828 for (parent = commit->parents, num = 0;
829 parent;
830 parent = parent->next, num++)
832 /* with enough slop */
833 strbuf_grow(sb, num * 50 + 20);
834 add_merge_info(fmt, sb, commit, abbrev);
835 parents_shown = 1;
839 * MEDIUM == DEFAULT shows only author with dates.
840 * FULL shows both authors but not dates.
841 * FULLER shows both authors and dates.
843 if (!memcmp(line, "author ", 7)) {
844 strbuf_grow(sb, linelen + 80);
845 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
847 if (!memcmp(line, "committer ", 10) &&
848 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
849 strbuf_grow(sb, linelen + 80);
850 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
855 void pp_title_line(enum cmit_fmt fmt,
856 const char **msg_p,
857 struct strbuf *sb,
858 const char *subject,
859 const char *after_subject,
860 const char *encoding,
861 int need_8bit_cte)
863 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
864 struct strbuf title;
866 strbuf_init(&title, 80);
867 *msg_p = format_subject(&title, *msg_p, line_separator);
869 strbuf_grow(sb, title.len + 1024);
870 if (subject) {
871 strbuf_addstr(sb, subject);
872 add_rfc2047(sb, title.buf, title.len, encoding);
873 } else {
874 strbuf_addbuf(sb, &title);
876 strbuf_addch(sb, '\n');
878 if (need_8bit_cte > 0) {
879 const char *header_fmt =
880 "MIME-Version: 1.0\n"
881 "Content-Type: text/plain; charset=%s\n"
882 "Content-Transfer-Encoding: 8bit\n";
883 strbuf_addf(sb, header_fmt, encoding);
885 if (after_subject) {
886 strbuf_addstr(sb, after_subject);
888 if (fmt == CMIT_FMT_EMAIL) {
889 strbuf_addch(sb, '\n');
891 strbuf_release(&title);
894 void pp_remainder(enum cmit_fmt fmt,
895 const char **msg_p,
896 struct strbuf *sb,
897 int indent)
899 int first = 1;
900 for (;;) {
901 const char *line = *msg_p;
902 int linelen = get_one_line(line);
903 *msg_p += linelen;
905 if (!linelen)
906 break;
908 if (is_empty_line(line, &linelen)) {
909 if (first)
910 continue;
911 if (fmt == CMIT_FMT_SHORT)
912 break;
914 first = 0;
916 strbuf_grow(sb, linelen + indent + 20);
917 if (indent) {
918 memset(sb->buf + sb->len, ' ', indent);
919 strbuf_setlen(sb, sb->len + indent);
921 strbuf_add(sb, line, linelen);
922 strbuf_addch(sb, '\n');
926 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
928 const char *encoding;
930 encoding = (git_log_output_encoding
931 ? git_log_output_encoding
932 : git_commit_encoding);
933 if (!encoding)
934 encoding = "UTF-8";
935 if (encoding_p)
936 *encoding_p = encoding;
937 return logmsg_reencode(commit, encoding);
940 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
941 struct strbuf *sb, int abbrev,
942 const char *subject, const char *after_subject,
943 enum date_mode dmode, int need_8bit_cte)
945 unsigned long beginning_of_body;
946 int indent = 4;
947 const char *msg = commit->buffer;
948 char *reencoded;
949 const char *encoding;
951 if (fmt == CMIT_FMT_USERFORMAT) {
952 format_commit_message(commit, user_format, sb, dmode);
953 return;
956 reencoded = reencode_commit_message(commit, &encoding);
957 if (reencoded) {
958 msg = reencoded;
961 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
962 indent = 0;
965 * We need to check and emit Content-type: to mark it
966 * as 8-bit if we haven't done so.
968 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
969 int i, ch, in_body;
971 for (in_body = i = 0; (ch = msg[i]); i++) {
972 if (!in_body) {
973 /* author could be non 7-bit ASCII but
974 * the log may be so; skip over the
975 * header part first.
977 if (ch == '\n' && msg[i+1] == '\n')
978 in_body = 1;
980 else if (non_ascii(ch)) {
981 need_8bit_cte = 1;
982 break;
987 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
988 if (fmt != CMIT_FMT_ONELINE && !subject) {
989 strbuf_addch(sb, '\n');
992 /* Skip excess blank lines at the beginning of body, if any... */
993 msg = skip_empty_lines(msg);
995 /* These formats treat the title line specially. */
996 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
997 pp_title_line(fmt, &msg, sb, subject,
998 after_subject, encoding, need_8bit_cte);
1000 beginning_of_body = sb->len;
1001 if (fmt != CMIT_FMT_ONELINE)
1002 pp_remainder(fmt, &msg, sb, indent);
1003 strbuf_rtrim(sb);
1005 /* Make sure there is an EOLN for the non-oneline case */
1006 if (fmt != CMIT_FMT_ONELINE)
1007 strbuf_addch(sb, '\n');
1010 * The caller may append additional body text in e-mail
1011 * format. Make sure we did not strip the blank line
1012 * between the header and the body.
1014 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1015 strbuf_addch(sb, '\n');
1016 free(reencoded);