6 #include "string-list.h"
11 #include "reflog-walk.h"
13 static char *user_format;
14 static struct cmt_fmt_map {
19 const char *user_format;
21 static size_t commit_formats_len;
22 static struct cmt_fmt_map *find_commit_format(const char *sought);
24 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
27 user_format = xstrdup(cp);
29 rev->use_terminator = 1;
30 rev->commit_format = CMIT_FMT_USERFORMAT;
33 static void setup_commit_formats(void)
35 struct cmt_fmt_map builtin_formats[] = {
36 { "raw", CMIT_FMT_RAW, 0 },
37 { "medium", CMIT_FMT_MEDIUM, 0 },
38 { "short", CMIT_FMT_SHORT, 0 },
39 { "email", CMIT_FMT_EMAIL, 0 },
40 { "fuller", CMIT_FMT_FULLER, 0 },
41 { "full", CMIT_FMT_FULL, 0 },
42 { "oneline", CMIT_FMT_ONELINE, 1 }
44 commit_formats_len = ARRAY_SIZE(builtin_formats);
45 commit_formats = xmalloc(commit_formats_len *
46 sizeof(*builtin_formats));
47 memcpy(commit_formats, builtin_formats,
48 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
51 static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
55 struct cmt_fmt_map *found = NULL;
56 size_t found_match_len = 0;
59 if (num_redirections >= commit_formats_len)
60 die("invalid --pretty format: "
61 "'%s' references an alias which points to itself",
64 for (i = 0; i < commit_formats_len; i++) {
67 if (prefixcmp(commit_formats[i].name, sought))
70 match_len = strlen(commit_formats[i].name);
71 if (found == NULL || found_match_len > match_len) {
72 found = &commit_formats[i];
73 found_match_len = match_len;
77 if (found && found->is_alias) {
78 found = find_commit_format_recursive(found->user_format,
86 static struct cmt_fmt_map *find_commit_format(const char *sought)
89 setup_commit_formats();
91 return find_commit_format_recursive(sought, sought, 0);
94 void get_commit_format(const char *arg, struct rev_info *rev)
96 struct cmt_fmt_map *commit_format;
98 rev->use_terminator = 0;
100 rev->commit_format = CMIT_FMT_DEFAULT;
103 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
104 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
108 if (strchr(arg, '%')) {
109 save_user_format(rev, arg, 1);
113 commit_format = find_commit_format(arg);
115 die("invalid --pretty format: %s", arg);
117 rev->commit_format = commit_format->format;
118 rev->use_terminator = commit_format->is_tformat;
122 * Generic support for pretty-printing the header
124 static int get_one_line(const char *msg)
139 /* High bit set, or ISO-2022-INT */
140 static int non_ascii(int ch)
142 return !isascii(ch) || ch == '\033';
145 int has_non_ascii(const char *s)
150 while ((ch = *s++) != '\0') {
157 static int is_rfc2047_special(char ch)
159 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
162 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
163 const char *encoding)
167 for (i = 0; i < len; i++) {
171 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
174 strbuf_add(sb, line, len);
178 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
179 strbuf_addf(sb, "=?%s?q?", encoding);
180 for (i = last = 0; i < len; i++) {
181 unsigned ch = line[i] & 0xFF;
183 * We encode ' ' using '=20' even though rfc2047
184 * allows using '_' for readability. Unfortunately,
185 * many programs do not understand this and just
186 * leave the underscore in place.
188 if (is_rfc2047_special(ch) || ch == ' ') {
189 strbuf_add(sb, line + last, i - last);
190 strbuf_addf(sb, "=%02X", ch);
194 strbuf_add(sb, line + last, len - last);
195 strbuf_addstr(sb, "?=");
198 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
199 const char *line, enum date_mode dmode,
200 const char *encoding)
207 if (fmt == CMIT_FMT_ONELINE)
209 date = strchr(line, '>');
212 namelen = ++date - line;
213 time = strtoul(date, &date, 10);
214 tz = strtol(date, NULL, 10);
216 if (fmt == CMIT_FMT_EMAIL) {
217 char *name_tail = strchr(line, '<');
218 int display_name_length;
221 while (line < name_tail && isspace(name_tail[-1]))
223 display_name_length = name_tail - line;
224 strbuf_addstr(sb, "From: ");
225 add_rfc2047(sb, line, display_name_length, encoding);
226 strbuf_add(sb, name_tail, namelen - display_name_length);
227 strbuf_addch(sb, '\n');
229 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
230 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
234 case CMIT_FMT_MEDIUM:
235 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
238 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
240 case CMIT_FMT_FULLER:
241 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
249 static int is_empty_line(const char *line, int *len_p)
252 while (len && isspace(line[len-1]))
258 static const char *skip_empty_lines(const char *msg)
261 int linelen = get_one_line(msg);
265 if (!is_empty_line(msg, &ll))
272 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
273 const struct commit *commit, int abbrev)
275 struct commit_list *parent = commit->parents;
277 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
278 !parent || !parent->next)
281 strbuf_addstr(sb, "Merge:");
284 struct commit *p = parent->item;
285 const char *hex = NULL;
287 hex = find_unique_abbrev(p->object.sha1, abbrev);
289 hex = sha1_to_hex(p->object.sha1);
290 parent = parent->next;
292 strbuf_addf(sb, " %s", hex);
294 strbuf_addch(sb, '\n');
297 static char *get_header(const struct commit *commit, const char *key)
299 int key_len = strlen(key);
300 const char *line = commit->buffer;
303 const char *eol = strchr(line, '\n'), *next;
308 eol = line + strlen(line);
312 if (eol - line > key_len &&
313 !strncmp(line, key, key_len) &&
314 line[key_len] == ' ') {
315 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
321 static char *replace_encoding_header(char *buf, const char *encoding)
323 struct strbuf tmp = STRBUF_INIT;
327 /* guess if there is an encoding header before a \n\n */
328 while (strncmp(cp, "encoding ", strlen("encoding "))) {
329 cp = strchr(cp, '\n');
330 if (!cp || *++cp == '\n')
334 cp = strchr(cp, '\n');
336 return buf; /* should not happen but be defensive */
337 len = cp + 1 - (buf + start);
339 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
340 if (is_encoding_utf8(encoding)) {
341 /* we have re-coded to UTF-8; drop the header */
342 strbuf_remove(&tmp, start, len);
344 /* just replaces XXXX in 'encoding XXXX\n' */
345 strbuf_splice(&tmp, start + strlen("encoding "),
346 len - strlen("encoding \n"),
347 encoding, strlen(encoding));
349 return strbuf_detach(&tmp, NULL);
352 static char *logmsg_reencode(const struct commit *commit,
353 const char *output_encoding)
355 static const char *utf8 = "UTF-8";
356 const char *use_encoding;
360 if (!*output_encoding)
362 encoding = get_header(commit, "encoding");
363 use_encoding = encoding ? encoding : utf8;
364 if (!strcmp(use_encoding, output_encoding))
365 if (encoding) /* we'll strip encoding header later */
366 out = xstrdup(commit->buffer);
368 return NULL; /* nothing to do */
370 out = reencode_string(commit->buffer,
371 output_encoding, use_encoding);
373 out = replace_encoding_header(out, output_encoding);
379 static int mailmap_name(char *email, int email_len, char *name, int name_len)
381 static struct string_list *mail_map;
383 mail_map = xcalloc(1, sizeof(*mail_map));
384 read_mailmap(mail_map, NULL);
386 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
389 static size_t format_person_part(struct strbuf *sb, char part,
390 const char *msg, int len, enum date_mode dmode)
392 /* currently all placeholders have same length */
393 const int placeholder_len = 2;
394 int start, end, tz = 0;
395 unsigned long date = 0;
397 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
398 char person_name[1024];
399 char person_mail[1024];
401 /* advance 'end' to point to email start delimiter */
402 for (end = 0; end < len && msg[end] != '<'; end++)
406 * When end points at the '<' that we found, it should have
407 * matching '>' later, which means 'end' must be strictly
413 /* Seek for both name and email part */
416 while (name_end > name_start && isspace(*(name_end-1)))
418 mail_start = msg+end+1;
419 mail_end = mail_start;
420 while (mail_end < msg_end && *mail_end != '>')
422 if (mail_end == msg_end)
426 if (part == 'N' || part == 'E') { /* mailmap lookup */
427 strlcpy(person_name, name_start, name_end-name_start+1);
428 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
429 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
430 name_start = person_name;
431 name_end = name_start + strlen(person_name);
432 mail_start = person_mail;
433 mail_end = mail_start + strlen(person_mail);
435 if (part == 'n' || part == 'N') { /* name */
436 strbuf_add(sb, name_start, name_end-name_start);
437 return placeholder_len;
439 if (part == 'e' || part == 'E') { /* email */
440 strbuf_add(sb, mail_start, mail_end-mail_start);
441 return placeholder_len;
444 /* advance 'start' to point to date start delimiter */
445 for (start = end + 1; start < len && isspace(msg[start]); start++)
449 date = strtoul(msg + start, &ep, 10);
450 if (msg + start == ep)
453 if (part == 't') { /* date, UNIX timestamp */
454 strbuf_add(sb, msg + start, ep - (msg + start));
455 return placeholder_len;
459 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
461 if (start + 1 < len) {
462 tz = strtoul(msg + start + 1, NULL, 10);
463 if (msg[start] == '-')
469 strbuf_addstr(sb, show_date(date, tz, dmode));
470 return placeholder_len;
471 case 'D': /* date, RFC2822 style */
472 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
473 return placeholder_len;
474 case 'r': /* date, relative */
475 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
476 return placeholder_len;
477 case 'i': /* date, ISO 8601 */
478 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
479 return placeholder_len;
484 * bogus commit, 'sb' cannot be updated, but we still need to
485 * compute a valid return value.
487 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
488 || part == 'D' || part == 'r' || part == 'i')
489 return placeholder_len;
491 return 0; /* unknown placeholder */
499 struct format_commit_context {
500 const struct commit *commit;
501 const struct pretty_print_context *pretty_ctx;
502 unsigned commit_header_parsed:1;
503 unsigned commit_message_parsed:1;
504 size_t width, indent1, indent2;
506 /* These offsets are relative to the start of the commit message. */
508 struct chunk committer;
509 struct chunk encoding;
514 /* The following ones are relative to the result struct strbuf. */
515 struct chunk abbrev_commit_hash;
516 struct chunk abbrev_tree_hash;
517 struct chunk abbrev_parent_hashes;
521 static int add_again(struct strbuf *sb, struct chunk *chunk)
524 strbuf_adddup(sb, chunk->off, chunk->len);
529 * We haven't seen this chunk before. Our caller is surely
530 * going to add it the hard way now. Remember the most likely
531 * start of the to-be-added chunk: the current end of the
534 chunk->off = sb->len;
538 static void parse_commit_header(struct format_commit_context *context)
540 const char *msg = context->commit->buffer;
543 for (i = 0; msg[i]; i++) {
545 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
550 } else if (!prefixcmp(msg + i, "author ")) {
551 context->author.off = i + 7;
552 context->author.len = eol - i - 7;
553 } else if (!prefixcmp(msg + i, "committer ")) {
554 context->committer.off = i + 10;
555 context->committer.len = eol - i - 10;
556 } else if (!prefixcmp(msg + i, "encoding ")) {
557 context->encoding.off = i + 9;
558 context->encoding.len = eol - i - 9;
562 context->message_off = i;
563 context->commit_header_parsed = 1;
566 static int istitlechar(char c)
568 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
569 (c >= '0' && c <= '9') || c == '.' || c == '_';
572 static void format_sanitized_subject(struct strbuf *sb, const char *msg)
575 size_t start_len = sb->len;
578 for (; *msg && *msg != '\n'; msg++) {
579 if (istitlechar(*msg)) {
581 strbuf_addch(sb, '-');
583 strbuf_addch(sb, *msg);
585 while (*(msg+1) == '.')
591 /* trim any trailing '.' or '-' characters */
593 while (sb->len - trimlen > start_len &&
594 (sb->buf[sb->len - 1 - trimlen] == '.'
595 || sb->buf[sb->len - 1 - trimlen] == '-'))
597 strbuf_remove(sb, sb->len - trimlen, trimlen);
600 const char *format_subject(struct strbuf *sb, const char *msg,
601 const char *line_separator)
606 const char *line = msg;
607 int linelen = get_one_line(line);
610 if (!linelen || is_empty_line(line, &linelen))
615 strbuf_grow(sb, linelen + 2);
617 strbuf_addstr(sb, line_separator);
618 strbuf_add(sb, line, linelen);
624 static void parse_commit_message(struct format_commit_context *c)
626 const char *msg = c->commit->buffer + c->message_off;
627 const char *start = c->commit->buffer;
629 msg = skip_empty_lines(msg);
630 c->subject_off = msg - start;
632 msg = format_subject(NULL, msg, NULL);
633 msg = skip_empty_lines(msg);
634 c->body_off = msg - start;
636 c->commit_message_parsed = 1;
639 static void format_decoration(struct strbuf *sb, const struct commit *commit)
641 struct name_decoration *d;
642 const char *prefix = " (";
644 load_ref_decorations(DECORATE_SHORT_REFS);
645 d = lookup_decoration(&name_decoration, &commit->object);
647 strbuf_addstr(sb, prefix);
649 strbuf_addstr(sb, d->name);
652 if (prefix[0] == ',')
653 strbuf_addch(sb, ')');
656 static void strbuf_wrap(struct strbuf *sb, size_t pos,
657 size_t width, size_t indent1, size_t indent2)
659 struct strbuf tmp = STRBUF_INIT;
662 strbuf_add(&tmp, sb->buf, pos);
663 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
664 (int) indent1, (int) indent2, (int) width);
665 strbuf_swap(&tmp, sb);
666 strbuf_release(&tmp);
669 static void rewrap_message_tail(struct strbuf *sb,
670 struct format_commit_context *c,
671 size_t new_width, size_t new_indent1,
674 if (c->width == new_width && c->indent1 == new_indent1 &&
675 c->indent2 == new_indent2)
677 if (c->wrap_start < sb->len)
678 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
679 c->wrap_start = sb->len;
680 c->width = new_width;
681 c->indent1 = new_indent1;
682 c->indent2 = new_indent2;
685 static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
688 struct format_commit_context *c = context;
689 const struct commit *commit = c->commit;
690 const char *msg = commit->buffer;
691 struct commit_list *p;
694 /* these are independent of the commit */
695 switch (placeholder[0]) {
697 if (placeholder[1] == '(') {
698 const char *end = strchr(placeholder + 2, ')');
699 char color[COLOR_MAXLEN];
702 color_parse_mem(placeholder + 2,
703 end - (placeholder + 2),
704 "--pretty format", color);
705 strbuf_addstr(sb, color);
706 return end - placeholder + 1;
708 if (!prefixcmp(placeholder + 1, "red")) {
709 strbuf_addstr(sb, GIT_COLOR_RED);
711 } else if (!prefixcmp(placeholder + 1, "green")) {
712 strbuf_addstr(sb, GIT_COLOR_GREEN);
714 } else if (!prefixcmp(placeholder + 1, "blue")) {
715 strbuf_addstr(sb, GIT_COLOR_BLUE);
717 } else if (!prefixcmp(placeholder + 1, "reset")) {
718 strbuf_addstr(sb, GIT_COLOR_RESET);
722 case 'n': /* newline */
723 strbuf_addch(sb, '\n');
726 /* %x00 == NUL, %x0a == LF, etc. */
727 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
729 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
731 strbuf_addch(sb, (h1<<4)|h2);
736 if (placeholder[1] == '(') {
737 unsigned long width = 0, indent1 = 0, indent2 = 0;
739 const char *start = placeholder + 2;
740 const char *end = strchr(start, ')');
744 width = strtoul(start, &next, 10);
746 indent1 = strtoul(next + 1, &next, 10);
748 indent2 = strtoul(next + 1,
755 rewrap_message_tail(sb, c, width, indent1, indent2);
756 return end - placeholder + 1;
761 /* these depend on the commit */
762 if (!commit->object.parsed)
763 parse_object(commit->object.sha1);
765 switch (placeholder[0]) {
766 case 'H': /* commit hash */
767 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
769 case 'h': /* abbreviated commit hash */
770 if (add_again(sb, &c->abbrev_commit_hash))
772 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
774 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
776 case 'T': /* tree hash */
777 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
779 case 't': /* abbreviated tree hash */
780 if (add_again(sb, &c->abbrev_tree_hash))
782 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
784 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
786 case 'P': /* parent hashes */
787 for (p = commit->parents; p; p = p->next) {
788 if (p != commit->parents)
789 strbuf_addch(sb, ' ');
790 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
793 case 'p': /* abbreviated parent hashes */
794 if (add_again(sb, &c->abbrev_parent_hashes))
796 for (p = commit->parents; p; p = p->next) {
797 if (p != commit->parents)
798 strbuf_addch(sb, ' ');
799 strbuf_addstr(sb, find_unique_abbrev(
800 p->item->object.sha1, DEFAULT_ABBREV));
802 c->abbrev_parent_hashes.len = sb->len -
803 c->abbrev_parent_hashes.off;
805 case 'm': /* left/right/bottom */
806 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
808 : (commit->object.flags & SYMMETRIC_LEFT)
813 format_decoration(sb, commit);
815 case 'g': /* reflog info */
816 switch(placeholder[1]) {
817 case 'd': /* reflog selector */
819 if (c->pretty_ctx->reflog_info)
820 get_reflog_selector(sb,
821 c->pretty_ctx->reflog_info,
822 c->pretty_ctx->date_mode,
823 (placeholder[1] == 'd'));
825 case 's': /* reflog message */
826 if (c->pretty_ctx->reflog_info)
827 get_reflog_message(sb, c->pretty_ctx->reflog_info);
830 return 0; /* unknown %g placeholder */
832 if (c->pretty_ctx->show_notes) {
833 format_display_notes(commit->object.sha1, sb,
834 git_log_output_encoding ? git_log_output_encoding
835 : git_commit_encoding, 0);
841 /* For the rest we have to parse the commit header. */
842 if (!c->commit_header_parsed)
843 parse_commit_header(c);
845 switch (placeholder[0]) {
846 case 'a': /* author ... */
847 return format_person_part(sb, placeholder[1],
848 msg + c->author.off, c->author.len,
849 c->pretty_ctx->date_mode);
850 case 'c': /* committer ... */
851 return format_person_part(sb, placeholder[1],
852 msg + c->committer.off, c->committer.len,
853 c->pretty_ctx->date_mode);
854 case 'e': /* encoding */
855 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
859 /* Now we need to parse the commit message. */
860 if (!c->commit_message_parsed)
861 parse_commit_message(c);
863 switch (placeholder[0]) {
864 case 's': /* subject */
865 format_subject(sb, msg + c->subject_off, " ");
867 case 'f': /* sanitized subject */
868 format_sanitized_subject(sb, msg + c->subject_off);
871 strbuf_addstr(sb, msg + c->body_off);
874 return 0; /* unknown placeholder */
877 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
884 ADD_LF_BEFORE_NON_EMPTY,
888 switch (placeholder[0]) {
890 magic = DEL_LF_BEFORE_EMPTY;
893 magic = ADD_LF_BEFORE_NON_EMPTY;
898 if (magic != NO_MAGIC)
902 consumed = format_commit_one(sb, placeholder, context);
903 if (magic == NO_MAGIC)
906 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
907 while (sb->len && sb->buf[sb->len - 1] == '\n')
908 strbuf_setlen(sb, sb->len - 1);
909 } else if ((orig_len != sb->len) && magic == ADD_LF_BEFORE_NON_EMPTY) {
910 strbuf_insert(sb, orig_len, "\n", 1);
915 static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
918 struct userformat_want *w = context;
920 if (*placeholder == '+' || *placeholder == '-')
923 switch (*placeholder) {
931 void userformat_find_requirements(const char *fmt, struct userformat_want *w)
933 struct strbuf dummy = STRBUF_INIT;
940 strbuf_expand(&dummy, user_format, userformat_want_item, w);
941 strbuf_release(&dummy);
944 void format_commit_message(const struct commit *commit,
945 const char *format, struct strbuf *sb,
946 const struct pretty_print_context *pretty_ctx)
948 struct format_commit_context context;
950 memset(&context, 0, sizeof(context));
951 context.commit = commit;
952 context.pretty_ctx = pretty_ctx;
953 context.wrap_start = sb->len;
954 strbuf_expand(sb, format, format_commit_item, &context);
955 rewrap_message_tail(sb, &context, 0, 0, 0);
958 static void pp_header(enum cmit_fmt fmt,
960 enum date_mode dmode,
961 const char *encoding,
962 const struct commit *commit,
966 int parents_shown = 0;
969 const char *line = *msg_p;
970 int linelen = get_one_line(*msg_p);
980 if (fmt == CMIT_FMT_RAW) {
981 strbuf_add(sb, line, linelen);
985 if (!memcmp(line, "parent ", 7)) {
987 die("bad parent line in commit");
991 if (!parents_shown) {
992 struct commit_list *parent;
994 for (parent = commit->parents, num = 0;
996 parent = parent->next, num++)
998 /* with enough slop */
999 strbuf_grow(sb, num * 50 + 20);
1000 add_merge_info(fmt, sb, commit, abbrev);
1005 * MEDIUM == DEFAULT shows only author with dates.
1006 * FULL shows both authors but not dates.
1007 * FULLER shows both authors and dates.
1009 if (!memcmp(line, "author ", 7)) {
1010 strbuf_grow(sb, linelen + 80);
1011 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
1013 if (!memcmp(line, "committer ", 10) &&
1014 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
1015 strbuf_grow(sb, linelen + 80);
1016 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
1021 void pp_title_line(enum cmit_fmt fmt,
1024 const char *subject,
1025 const char *after_subject,
1026 const char *encoding,
1029 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
1030 struct strbuf title;
1032 strbuf_init(&title, 80);
1033 *msg_p = format_subject(&title, *msg_p, line_separator);
1035 strbuf_grow(sb, title.len + 1024);
1037 strbuf_addstr(sb, subject);
1038 add_rfc2047(sb, title.buf, title.len, encoding);
1040 strbuf_addbuf(sb, &title);
1042 strbuf_addch(sb, '\n');
1044 if (need_8bit_cte > 0) {
1045 const char *header_fmt =
1046 "MIME-Version: 1.0\n"
1047 "Content-Type: text/plain; charset=%s\n"
1048 "Content-Transfer-Encoding: 8bit\n";
1049 strbuf_addf(sb, header_fmt, encoding);
1051 if (after_subject) {
1052 strbuf_addstr(sb, after_subject);
1054 if (fmt == CMIT_FMT_EMAIL) {
1055 strbuf_addch(sb, '\n');
1057 strbuf_release(&title);
1060 void pp_remainder(enum cmit_fmt fmt,
1067 const char *line = *msg_p;
1068 int linelen = get_one_line(line);
1074 if (is_empty_line(line, &linelen)) {
1077 if (fmt == CMIT_FMT_SHORT)
1082 strbuf_grow(sb, linelen + indent + 20);
1084 memset(sb->buf + sb->len, ' ', indent);
1085 strbuf_setlen(sb, sb->len + indent);
1087 strbuf_add(sb, line, linelen);
1088 strbuf_addch(sb, '\n');
1092 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
1094 const char *encoding;
1096 encoding = (git_log_output_encoding
1097 ? git_log_output_encoding
1098 : git_commit_encoding);
1102 *encoding_p = encoding;
1103 return logmsg_reencode(commit, encoding);
1106 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1108 const struct pretty_print_context *context)
1110 unsigned long beginning_of_body;
1112 const char *msg = commit->buffer;
1114 const char *encoding;
1115 int need_8bit_cte = context->need_8bit_cte;
1117 if (fmt == CMIT_FMT_USERFORMAT) {
1118 format_commit_message(commit, user_format, sb, context);
1122 reencoded = reencode_commit_message(commit, &encoding);
1127 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1131 * We need to check and emit Content-type: to mark it
1132 * as 8-bit if we haven't done so.
1134 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1137 for (in_body = i = 0; (ch = msg[i]); i++) {
1139 /* author could be non 7-bit ASCII but
1140 * the log may be so; skip over the
1141 * header part first.
1143 if (ch == '\n' && msg[i+1] == '\n')
1146 else if (non_ascii(ch)) {
1153 pp_header(fmt, context->abbrev, context->date_mode, encoding,
1155 if (fmt != CMIT_FMT_ONELINE && !context->subject) {
1156 strbuf_addch(sb, '\n');
1159 /* Skip excess blank lines at the beginning of body, if any... */
1160 msg = skip_empty_lines(msg);
1162 /* These formats treat the title line specially. */
1163 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1164 pp_title_line(fmt, &msg, sb, context->subject,
1165 context->after_subject, encoding, need_8bit_cte);
1167 beginning_of_body = sb->len;
1168 if (fmt != CMIT_FMT_ONELINE)
1169 pp_remainder(fmt, &msg, sb, indent);
1172 /* Make sure there is an EOLN for the non-oneline case */
1173 if (fmt != CMIT_FMT_ONELINE)
1174 strbuf_addch(sb, '\n');
1177 * The caller may append additional body text in e-mail
1178 * format. Make sure we did not strip the blank line
1179 * between the header and the body.
1181 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1182 strbuf_addch(sb, '\n');
1184 if (context->show_notes)
1185 format_display_notes(commit->object.sha1, sb, encoding,
1186 NOTES_SHOW_HEADER | NOTES_INDENT);