connect.c: remove a few globals by using git_config callback data
[git/spearce.git] / pretty.c
blobc0184080998734450b250aa7dc72c427e1ecac8a
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 static int is_rfc2047_special(char ch)
91 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
94 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
95 const char *encoding)
97 int i, last;
99 for (i = 0; i < len; i++) {
100 int ch = line[i];
101 if (non_ascii(ch))
102 goto needquote;
103 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
104 goto needquote;
106 strbuf_add(sb, line, len);
107 return;
109 needquote:
110 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
111 strbuf_addf(sb, "=?%s?q?", encoding);
112 for (i = last = 0; i < len; i++) {
113 unsigned ch = line[i] & 0xFF;
115 * We encode ' ' using '=20' even though rfc2047
116 * allows using '_' for readability. Unfortunately,
117 * many programs do not understand this and just
118 * leave the underscore in place.
120 if (is_rfc2047_special(ch) || ch == ' ') {
121 strbuf_add(sb, line + last, i - last);
122 strbuf_addf(sb, "=%02X", ch);
123 last = i + 1;
126 strbuf_add(sb, line + last, len - last);
127 strbuf_addstr(sb, "?=");
130 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
131 const char *line, enum date_mode dmode,
132 const char *encoding)
134 char *date;
135 int namelen;
136 unsigned long time;
137 int tz;
138 const char *filler = " ";
140 if (fmt == CMIT_FMT_ONELINE)
141 return;
142 date = strchr(line, '>');
143 if (!date)
144 return;
145 namelen = ++date - line;
146 time = strtoul(date, &date, 10);
147 tz = strtol(date, NULL, 10);
149 if (fmt == CMIT_FMT_EMAIL) {
150 char *name_tail = strchr(line, '<');
151 int display_name_length;
152 if (!name_tail)
153 return;
154 while (line < name_tail && isspace(name_tail[-1]))
155 name_tail--;
156 display_name_length = name_tail - line;
157 filler = "";
158 strbuf_addstr(sb, "From: ");
159 add_rfc2047(sb, line, display_name_length, encoding);
160 strbuf_add(sb, name_tail, namelen - display_name_length);
161 strbuf_addch(sb, '\n');
162 } else {
163 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
164 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
165 filler, namelen, line);
167 switch (fmt) {
168 case CMIT_FMT_MEDIUM:
169 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
170 break;
171 case CMIT_FMT_EMAIL:
172 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
173 break;
174 case CMIT_FMT_FULLER:
175 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
176 break;
177 default:
178 /* notin' */
179 break;
183 static int is_empty_line(const char *line, int *len_p)
185 int len = *len_p;
186 while (len && isspace(line[len-1]))
187 len--;
188 *len_p = len;
189 return !len;
192 static const char *skip_empty_lines(const char *msg)
194 for (;;) {
195 int linelen = get_one_line(msg);
196 int ll = linelen;
197 if (!linelen)
198 break;
199 if (!is_empty_line(msg, &ll))
200 break;
201 msg += linelen;
203 return msg;
206 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
207 const struct commit *commit, int abbrev)
209 struct commit_list *parent = commit->parents;
211 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
212 !parent || !parent->next)
213 return;
215 strbuf_addstr(sb, "Merge:");
217 while (parent) {
218 struct commit *p = parent->item;
219 const char *hex = NULL;
220 if (abbrev)
221 hex = find_unique_abbrev(p->object.sha1, abbrev);
222 if (!hex)
223 hex = sha1_to_hex(p->object.sha1);
224 parent = parent->next;
226 strbuf_addf(sb, " %s", hex);
228 strbuf_addch(sb, '\n');
231 static char *get_header(const struct commit *commit, const char *key)
233 int key_len = strlen(key);
234 const char *line = commit->buffer;
236 for (;;) {
237 const char *eol = strchr(line, '\n'), *next;
239 if (line == eol)
240 return NULL;
241 if (!eol) {
242 eol = line + strlen(line);
243 next = NULL;
244 } else
245 next = eol + 1;
246 if (eol - line > key_len &&
247 !strncmp(line, key, key_len) &&
248 line[key_len] == ' ') {
249 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
251 line = next;
255 static char *replace_encoding_header(char *buf, const char *encoding)
257 struct strbuf tmp = STRBUF_INIT;
258 size_t start, len;
259 char *cp = buf;
261 /* guess if there is an encoding header before a \n\n */
262 while (strncmp(cp, "encoding ", strlen("encoding "))) {
263 cp = strchr(cp, '\n');
264 if (!cp || *++cp == '\n')
265 return buf;
267 start = cp - buf;
268 cp = strchr(cp, '\n');
269 if (!cp)
270 return buf; /* should not happen but be defensive */
271 len = cp + 1 - (buf + start);
273 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
274 if (is_encoding_utf8(encoding)) {
275 /* we have re-coded to UTF-8; drop the header */
276 strbuf_remove(&tmp, start, len);
277 } else {
278 /* just replaces XXXX in 'encoding XXXX\n' */
279 strbuf_splice(&tmp, start + strlen("encoding "),
280 len - strlen("encoding \n"),
281 encoding, strlen(encoding));
283 return strbuf_detach(&tmp, NULL);
286 static char *logmsg_reencode(const struct commit *commit,
287 const char *output_encoding)
289 static const char *utf8 = "utf-8";
290 const char *use_encoding;
291 char *encoding;
292 char *out;
294 if (!*output_encoding)
295 return NULL;
296 encoding = get_header(commit, "encoding");
297 use_encoding = encoding ? encoding : utf8;
298 if (!strcmp(use_encoding, output_encoding))
299 if (encoding) /* we'll strip encoding header later */
300 out = xstrdup(commit->buffer);
301 else
302 return NULL; /* nothing to do */
303 else
304 out = reencode_string(commit->buffer,
305 output_encoding, use_encoding);
306 if (out)
307 out = replace_encoding_header(out, output_encoding);
309 free(encoding);
310 return out;
313 static int mailmap_name(char *email, int email_len, char *name, int name_len)
315 static struct string_list *mail_map;
316 if (!mail_map) {
317 mail_map = xcalloc(1, sizeof(*mail_map));
318 read_mailmap(mail_map, NULL);
320 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
323 static size_t format_person_part(struct strbuf *sb, char part,
324 const char *msg, int len, enum date_mode dmode)
326 /* currently all placeholders have same length */
327 const int placeholder_len = 2;
328 int start, end, tz = 0;
329 unsigned long date = 0;
330 char *ep;
331 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
332 char person_name[1024];
333 char person_mail[1024];
335 /* advance 'end' to point to email start delimiter */
336 for (end = 0; end < len && msg[end] != '<'; end++)
337 ; /* do nothing */
340 * When end points at the '<' that we found, it should have
341 * matching '>' later, which means 'end' must be strictly
342 * below len - 1.
344 if (end >= len - 2)
345 goto skip;
347 /* Seek for both name and email part */
348 name_start = msg;
349 name_end = msg+end;
350 while (name_end > name_start && isspace(*(name_end-1)))
351 name_end--;
352 mail_start = msg+end+1;
353 mail_end = mail_start;
354 while (mail_end < msg_end && *mail_end != '>')
355 mail_end++;
356 if (mail_end == msg_end)
357 goto skip;
358 end = mail_end-msg;
360 if (part == 'N' || part == 'E') { /* mailmap lookup */
361 strlcpy(person_name, name_start, name_end-name_start+1);
362 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
363 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
364 name_start = person_name;
365 name_end = name_start + strlen(person_name);
366 mail_start = person_mail;
367 mail_end = mail_start + strlen(person_mail);
369 if (part == 'n' || part == 'N') { /* name */
370 strbuf_add(sb, name_start, name_end-name_start);
371 return placeholder_len;
373 if (part == 'e' || part == 'E') { /* email */
374 strbuf_add(sb, mail_start, mail_end-mail_start);
375 return placeholder_len;
378 /* advance 'start' to point to date start delimiter */
379 for (start = end + 1; start < len && isspace(msg[start]); start++)
380 ; /* do nothing */
381 if (start >= len)
382 goto skip;
383 date = strtoul(msg + start, &ep, 10);
384 if (msg + start == ep)
385 goto skip;
387 if (part == 't') { /* date, UNIX timestamp */
388 strbuf_add(sb, msg + start, ep - (msg + start));
389 return placeholder_len;
392 /* parse tz */
393 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
394 ; /* do nothing */
395 if (start + 1 < len) {
396 tz = strtoul(msg + start + 1, NULL, 10);
397 if (msg[start] == '-')
398 tz = -tz;
401 switch (part) {
402 case 'd': /* date */
403 strbuf_addstr(sb, show_date(date, tz, dmode));
404 return placeholder_len;
405 case 'D': /* date, RFC2822 style */
406 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
407 return placeholder_len;
408 case 'r': /* date, relative */
409 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
410 return placeholder_len;
411 case 'i': /* date, ISO 8601 */
412 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
413 return placeholder_len;
416 skip:
418 * bogus commit, 'sb' cannot be updated, but we still need to
419 * compute a valid return value.
421 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
422 || part == 'D' || part == 'r' || part == 'i')
423 return placeholder_len;
425 return 0; /* unknown placeholder */
428 struct chunk {
429 size_t off;
430 size_t len;
433 struct format_commit_context {
434 const struct commit *commit;
435 enum date_mode dmode;
436 unsigned commit_header_parsed:1;
437 unsigned commit_message_parsed:1;
439 /* These offsets are relative to the start of the commit message. */
440 struct chunk author;
441 struct chunk committer;
442 struct chunk encoding;
443 size_t message_off;
444 size_t subject_off;
445 size_t body_off;
447 /* The following ones are relative to the result struct strbuf. */
448 struct chunk abbrev_commit_hash;
449 struct chunk abbrev_tree_hash;
450 struct chunk abbrev_parent_hashes;
453 static int add_again(struct strbuf *sb, struct chunk *chunk)
455 if (chunk->len) {
456 strbuf_adddup(sb, chunk->off, chunk->len);
457 return 1;
461 * We haven't seen this chunk before. Our caller is surely
462 * going to add it the hard way now. Remember the most likely
463 * start of the to-be-added chunk: the current end of the
464 * struct strbuf.
466 chunk->off = sb->len;
467 return 0;
470 static void parse_commit_header(struct format_commit_context *context)
472 const char *msg = context->commit->buffer;
473 int i;
475 for (i = 0; msg[i]; i++) {
476 int eol;
477 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
478 ; /* do nothing */
480 if (i == eol) {
481 break;
482 } else if (!prefixcmp(msg + i, "author ")) {
483 context->author.off = i + 7;
484 context->author.len = eol - i - 7;
485 } else if (!prefixcmp(msg + i, "committer ")) {
486 context->committer.off = i + 10;
487 context->committer.len = eol - i - 10;
488 } else if (!prefixcmp(msg + i, "encoding ")) {
489 context->encoding.off = i + 9;
490 context->encoding.len = eol - i - 9;
492 i = eol;
494 context->message_off = i;
495 context->commit_header_parsed = 1;
498 const char *format_subject(struct strbuf *sb, const char *msg,
499 const char *line_separator)
501 int first = 1;
503 for (;;) {
504 const char *line = msg;
505 int linelen = get_one_line(line);
507 msg += linelen;
508 if (!linelen || is_empty_line(line, &linelen))
509 break;
511 if (!sb)
512 continue;
513 strbuf_grow(sb, linelen + 2);
514 if (!first)
515 strbuf_addstr(sb, line_separator);
516 strbuf_add(sb, line, linelen);
517 first = 0;
519 return msg;
522 static void parse_commit_message(struct format_commit_context *c)
524 const char *msg = c->commit->buffer + c->message_off;
525 const char *start = c->commit->buffer;
527 msg = skip_empty_lines(msg);
528 c->subject_off = msg - start;
530 msg = format_subject(NULL, msg, NULL);
531 msg = skip_empty_lines(msg);
532 c->body_off = msg - start;
534 c->commit_message_parsed = 1;
537 static void format_decoration(struct strbuf *sb, const struct commit *commit)
539 struct name_decoration *d;
540 const char *prefix = " (";
542 load_ref_decorations();
543 d = lookup_decoration(&name_decoration, &commit->object);
544 while (d) {
545 strbuf_addstr(sb, prefix);
546 prefix = ", ";
547 strbuf_addstr(sb, d->name);
548 d = d->next;
550 if (prefix[0] == ',')
551 strbuf_addch(sb, ')');
554 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
555 void *context)
557 struct format_commit_context *c = context;
558 const struct commit *commit = c->commit;
559 const char *msg = commit->buffer;
560 struct commit_list *p;
561 int h1, h2;
563 /* these are independent of the commit */
564 switch (placeholder[0]) {
565 case 'C':
566 if (placeholder[1] == '(') {
567 const char *end = strchr(placeholder + 2, ')');
568 char color[COLOR_MAXLEN];
569 if (!end)
570 return 0;
571 color_parse_mem(placeholder + 2,
572 end - (placeholder + 2),
573 "--pretty format", color);
574 strbuf_addstr(sb, color);
575 return end - placeholder + 1;
577 if (!prefixcmp(placeholder + 1, "red")) {
578 strbuf_addstr(sb, GIT_COLOR_RED);
579 return 4;
580 } else if (!prefixcmp(placeholder + 1, "green")) {
581 strbuf_addstr(sb, GIT_COLOR_GREEN);
582 return 6;
583 } else if (!prefixcmp(placeholder + 1, "blue")) {
584 strbuf_addstr(sb, GIT_COLOR_BLUE);
585 return 5;
586 } else if (!prefixcmp(placeholder + 1, "reset")) {
587 strbuf_addstr(sb, GIT_COLOR_RESET);
588 return 6;
589 } else
590 return 0;
591 case 'n': /* newline */
592 strbuf_addch(sb, '\n');
593 return 1;
594 case 'x':
595 /* %x00 == NUL, %x0a == LF, etc. */
596 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
597 h1 <= 16 &&
598 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
599 h2 <= 16) {
600 strbuf_addch(sb, (h1<<4)|h2);
601 return 3;
602 } else
603 return 0;
606 /* these depend on the commit */
607 if (!commit->object.parsed)
608 parse_object(commit->object.sha1);
610 switch (placeholder[0]) {
611 case 'H': /* commit hash */
612 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
613 return 1;
614 case 'h': /* abbreviated commit hash */
615 if (add_again(sb, &c->abbrev_commit_hash))
616 return 1;
617 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
618 DEFAULT_ABBREV));
619 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
620 return 1;
621 case 'T': /* tree hash */
622 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
623 return 1;
624 case 't': /* abbreviated tree hash */
625 if (add_again(sb, &c->abbrev_tree_hash))
626 return 1;
627 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
628 DEFAULT_ABBREV));
629 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
630 return 1;
631 case 'P': /* parent hashes */
632 for (p = commit->parents; p; p = p->next) {
633 if (p != commit->parents)
634 strbuf_addch(sb, ' ');
635 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
637 return 1;
638 case 'p': /* abbreviated parent hashes */
639 if (add_again(sb, &c->abbrev_parent_hashes))
640 return 1;
641 for (p = commit->parents; p; p = p->next) {
642 if (p != commit->parents)
643 strbuf_addch(sb, ' ');
644 strbuf_addstr(sb, find_unique_abbrev(
645 p->item->object.sha1, DEFAULT_ABBREV));
647 c->abbrev_parent_hashes.len = sb->len -
648 c->abbrev_parent_hashes.off;
649 return 1;
650 case 'm': /* left/right/bottom */
651 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
652 ? '-'
653 : (commit->object.flags & SYMMETRIC_LEFT)
654 ? '<'
655 : '>');
656 return 1;
657 case 'd':
658 format_decoration(sb, commit);
659 return 1;
662 /* For the rest we have to parse the commit header. */
663 if (!c->commit_header_parsed)
664 parse_commit_header(c);
666 switch (placeholder[0]) {
667 case 'a': /* author ... */
668 return format_person_part(sb, placeholder[1],
669 msg + c->author.off, c->author.len,
670 c->dmode);
671 case 'c': /* committer ... */
672 return format_person_part(sb, placeholder[1],
673 msg + c->committer.off, c->committer.len,
674 c->dmode);
675 case 'e': /* encoding */
676 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
677 return 1;
680 /* Now we need to parse the commit message. */
681 if (!c->commit_message_parsed)
682 parse_commit_message(c);
684 switch (placeholder[0]) {
685 case 's': /* subject */
686 format_subject(sb, msg + c->subject_off, " ");
687 return 1;
688 case 'b': /* body */
689 strbuf_addstr(sb, msg + c->body_off);
690 return 1;
692 return 0; /* unknown placeholder */
695 void format_commit_message(const struct commit *commit,
696 const void *format, struct strbuf *sb,
697 enum date_mode dmode)
699 struct format_commit_context context;
701 memset(&context, 0, sizeof(context));
702 context.commit = commit;
703 context.dmode = dmode;
704 strbuf_expand(sb, format, format_commit_item, &context);
707 static void pp_header(enum cmit_fmt fmt,
708 int abbrev,
709 enum date_mode dmode,
710 const char *encoding,
711 const struct commit *commit,
712 const char **msg_p,
713 struct strbuf *sb)
715 int parents_shown = 0;
717 for (;;) {
718 const char *line = *msg_p;
719 int linelen = get_one_line(*msg_p);
721 if (!linelen)
722 return;
723 *msg_p += linelen;
725 if (linelen == 1)
726 /* End of header */
727 return;
729 if (fmt == CMIT_FMT_RAW) {
730 strbuf_add(sb, line, linelen);
731 continue;
734 if (!memcmp(line, "parent ", 7)) {
735 if (linelen != 48)
736 die("bad parent line in commit");
737 continue;
740 if (!parents_shown) {
741 struct commit_list *parent;
742 int num;
743 for (parent = commit->parents, num = 0;
744 parent;
745 parent = parent->next, num++)
747 /* with enough slop */
748 strbuf_grow(sb, num * 50 + 20);
749 add_merge_info(fmt, sb, commit, abbrev);
750 parents_shown = 1;
754 * MEDIUM == DEFAULT shows only author with dates.
755 * FULL shows both authors but not dates.
756 * FULLER shows both authors and dates.
758 if (!memcmp(line, "author ", 7)) {
759 strbuf_grow(sb, linelen + 80);
760 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
762 if (!memcmp(line, "committer ", 10) &&
763 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
764 strbuf_grow(sb, linelen + 80);
765 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
770 void pp_title_line(enum cmit_fmt fmt,
771 const char **msg_p,
772 struct strbuf *sb,
773 const char *subject,
774 const char *after_subject,
775 const char *encoding,
776 int need_8bit_cte)
778 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
779 struct strbuf title;
781 strbuf_init(&title, 80);
782 *msg_p = format_subject(&title, *msg_p, line_separator);
784 strbuf_grow(sb, title.len + 1024);
785 if (subject) {
786 strbuf_addstr(sb, subject);
787 add_rfc2047(sb, title.buf, title.len, encoding);
788 } else {
789 strbuf_addbuf(sb, &title);
791 strbuf_addch(sb, '\n');
793 if (need_8bit_cte > 0) {
794 const char *header_fmt =
795 "MIME-Version: 1.0\n"
796 "Content-Type: text/plain; charset=%s\n"
797 "Content-Transfer-Encoding: 8bit\n";
798 strbuf_addf(sb, header_fmt, encoding);
800 if (after_subject) {
801 strbuf_addstr(sb, after_subject);
803 if (fmt == CMIT_FMT_EMAIL) {
804 strbuf_addch(sb, '\n');
806 strbuf_release(&title);
809 void pp_remainder(enum cmit_fmt fmt,
810 const char **msg_p,
811 struct strbuf *sb,
812 int indent)
814 int first = 1;
815 for (;;) {
816 const char *line = *msg_p;
817 int linelen = get_one_line(line);
818 *msg_p += linelen;
820 if (!linelen)
821 break;
823 if (is_empty_line(line, &linelen)) {
824 if (first)
825 continue;
826 if (fmt == CMIT_FMT_SHORT)
827 break;
829 first = 0;
831 strbuf_grow(sb, linelen + indent + 20);
832 if (indent) {
833 memset(sb->buf + sb->len, ' ', indent);
834 strbuf_setlen(sb, sb->len + indent);
836 strbuf_add(sb, line, linelen);
837 strbuf_addch(sb, '\n');
841 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
843 const char *encoding;
845 encoding = (git_log_output_encoding
846 ? git_log_output_encoding
847 : git_commit_encoding);
848 if (!encoding)
849 encoding = "utf-8";
850 if (encoding_p)
851 *encoding_p = encoding;
852 return logmsg_reencode(commit, encoding);
855 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
856 struct strbuf *sb, int abbrev,
857 const char *subject, const char *after_subject,
858 enum date_mode dmode, int need_8bit_cte)
860 unsigned long beginning_of_body;
861 int indent = 4;
862 const char *msg = commit->buffer;
863 char *reencoded;
864 const char *encoding;
866 if (fmt == CMIT_FMT_USERFORMAT) {
867 format_commit_message(commit, user_format, sb, dmode);
868 return;
871 reencoded = reencode_commit_message(commit, &encoding);
872 if (reencoded) {
873 msg = reencoded;
876 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
877 indent = 0;
880 * We need to check and emit Content-type: to mark it
881 * as 8-bit if we haven't done so.
883 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
884 int i, ch, in_body;
886 for (in_body = i = 0; (ch = msg[i]); i++) {
887 if (!in_body) {
888 /* author could be non 7-bit ASCII but
889 * the log may be so; skip over the
890 * header part first.
892 if (ch == '\n' && msg[i+1] == '\n')
893 in_body = 1;
895 else if (non_ascii(ch)) {
896 need_8bit_cte = 1;
897 break;
902 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
903 if (fmt != CMIT_FMT_ONELINE && !subject) {
904 strbuf_addch(sb, '\n');
907 /* Skip excess blank lines at the beginning of body, if any... */
908 msg = skip_empty_lines(msg);
910 /* These formats treat the title line specially. */
911 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
912 pp_title_line(fmt, &msg, sb, subject,
913 after_subject, encoding, need_8bit_cte);
915 beginning_of_body = sb->len;
916 if (fmt != CMIT_FMT_ONELINE)
917 pp_remainder(fmt, &msg, sb, indent);
918 strbuf_rtrim(sb);
920 /* Make sure there is an EOLN for the non-oneline case */
921 if (fmt != CMIT_FMT_ONELINE)
922 strbuf_addch(sb, '\n');
925 * The caller may append additional body text in e-mail
926 * format. Make sure we did not strip the blank line
927 * between the header and the body.
929 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
930 strbuf_addch(sb, '\n');
931 free(reencoded);