format-patch: introduce helper function last_line_length()
[git.git] / pretty.c
blob482402d2838ce3a8f8700dea47afc1cdc37f38e3
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"
12 #include "gpg-interface.h"
14 static char *user_format;
15 static struct cmt_fmt_map {
16 const char *name;
17 enum cmit_fmt format;
18 int is_tformat;
19 int is_alias;
20 const char *user_format;
21 } *commit_formats;
22 static size_t builtin_formats_len;
23 static size_t commit_formats_len;
24 static size_t commit_formats_alloc;
25 static struct cmt_fmt_map *find_commit_format(const char *sought);
27 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
29 free(user_format);
30 user_format = xstrdup(cp);
31 if (is_tformat)
32 rev->use_terminator = 1;
33 rev->commit_format = CMIT_FMT_USERFORMAT;
36 static int git_pretty_formats_config(const char *var, const char *value, void *cb)
38 struct cmt_fmt_map *commit_format = NULL;
39 const char *name;
40 const char *fmt;
41 int i;
43 if (prefixcmp(var, "pretty."))
44 return 0;
46 name = var + strlen("pretty.");
47 for (i = 0; i < builtin_formats_len; i++) {
48 if (!strcmp(commit_formats[i].name, name))
49 return 0;
52 for (i = builtin_formats_len; i < commit_formats_len; i++) {
53 if (!strcmp(commit_formats[i].name, name)) {
54 commit_format = &commit_formats[i];
55 break;
59 if (!commit_format) {
60 ALLOC_GROW(commit_formats, commit_formats_len+1,
61 commit_formats_alloc);
62 commit_format = &commit_formats[commit_formats_len];
63 memset(commit_format, 0, sizeof(*commit_format));
64 commit_formats_len++;
67 commit_format->name = xstrdup(name);
68 commit_format->format = CMIT_FMT_USERFORMAT;
69 git_config_string(&fmt, var, value);
70 if (!prefixcmp(fmt, "format:") || !prefixcmp(fmt, "tformat:")) {
71 commit_format->is_tformat = fmt[0] == 't';
72 fmt = strchr(fmt, ':') + 1;
73 } else if (strchr(fmt, '%'))
74 commit_format->is_tformat = 1;
75 else
76 commit_format->is_alias = 1;
77 commit_format->user_format = fmt;
79 return 0;
82 static void setup_commit_formats(void)
84 struct cmt_fmt_map builtin_formats[] = {
85 { "raw", CMIT_FMT_RAW, 0 },
86 { "medium", CMIT_FMT_MEDIUM, 0 },
87 { "short", CMIT_FMT_SHORT, 0 },
88 { "email", CMIT_FMT_EMAIL, 0 },
89 { "fuller", CMIT_FMT_FULLER, 0 },
90 { "full", CMIT_FMT_FULL, 0 },
91 { "oneline", CMIT_FMT_ONELINE, 1 }
93 commit_formats_len = ARRAY_SIZE(builtin_formats);
94 builtin_formats_len = commit_formats_len;
95 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
96 memcpy(commit_formats, builtin_formats,
97 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
99 git_config(git_pretty_formats_config, NULL);
102 static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
103 const char *original,
104 int num_redirections)
106 struct cmt_fmt_map *found = NULL;
107 size_t found_match_len = 0;
108 int i;
110 if (num_redirections >= commit_formats_len)
111 die("invalid --pretty format: "
112 "'%s' references an alias which points to itself",
113 original);
115 for (i = 0; i < commit_formats_len; i++) {
116 size_t match_len;
118 if (prefixcmp(commit_formats[i].name, sought))
119 continue;
121 match_len = strlen(commit_formats[i].name);
122 if (found == NULL || found_match_len > match_len) {
123 found = &commit_formats[i];
124 found_match_len = match_len;
128 if (found && found->is_alias) {
129 found = find_commit_format_recursive(found->user_format,
130 original,
131 num_redirections+1);
134 return found;
137 static struct cmt_fmt_map *find_commit_format(const char *sought)
139 if (!commit_formats)
140 setup_commit_formats();
142 return find_commit_format_recursive(sought, sought, 0);
145 void get_commit_format(const char *arg, struct rev_info *rev)
147 struct cmt_fmt_map *commit_format;
149 rev->use_terminator = 0;
150 if (!arg || !*arg) {
151 rev->commit_format = CMIT_FMT_DEFAULT;
152 return;
154 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
155 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
156 return;
159 if (strchr(arg, '%')) {
160 save_user_format(rev, arg, 1);
161 return;
164 commit_format = find_commit_format(arg);
165 if (!commit_format)
166 die("invalid --pretty format: %s", arg);
168 rev->commit_format = commit_format->format;
169 rev->use_terminator = commit_format->is_tformat;
170 if (commit_format->format == CMIT_FMT_USERFORMAT) {
171 save_user_format(rev, commit_format->user_format,
172 commit_format->is_tformat);
177 * Generic support for pretty-printing the header
179 static int get_one_line(const char *msg)
181 int ret = 0;
183 for (;;) {
184 char c = *msg++;
185 if (!c)
186 break;
187 ret++;
188 if (c == '\n')
189 break;
191 return ret;
194 /* High bit set, or ISO-2022-INT */
195 static int non_ascii(int ch)
197 return !isascii(ch) || ch == '\033';
200 int has_non_ascii(const char *s)
202 int ch;
203 if (!s)
204 return 0;
205 while ((ch = *s++) != '\0') {
206 if (non_ascii(ch))
207 return 1;
209 return 0;
212 static int is_rfc822_special(char ch)
214 switch (ch) {
215 case '(':
216 case ')':
217 case '<':
218 case '>':
219 case '[':
220 case ']':
221 case ':':
222 case ';':
223 case '@':
224 case ',':
225 case '.':
226 case '"':
227 case '\\':
228 return 1;
229 default:
230 return 0;
234 static int has_rfc822_specials(const char *s, int len)
236 int i;
237 for (i = 0; i < len; i++)
238 if (is_rfc822_special(s[i]))
239 return 1;
240 return 0;
243 static int last_line_length(struct strbuf *sb)
245 int i;
247 /* How many bytes are already used on the last line? */
248 for (i = sb->len - 1; i >= 0; i--)
249 if (sb->buf[i] == '\n')
250 break;
251 return sb->len - (i + 1);
254 static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
256 int i;
258 /* just a guess, we may have to also backslash-quote */
259 strbuf_grow(out, len + 2);
261 strbuf_addch(out, '"');
262 for (i = 0; i < len; i++) {
263 switch (s[i]) {
264 case '"':
265 case '\\':
266 strbuf_addch(out, '\\');
267 /* fall through */
268 default:
269 strbuf_addch(out, s[i]);
272 strbuf_addch(out, '"');
275 static int is_rfc2047_special(char ch)
277 if (ch == ' ' || ch == '\n')
278 return 1;
280 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
283 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
284 const char *encoding)
286 static const int max_length = 78; /* per rfc2822 */
287 static const int max_encoded_length = 76; /* per rfc2047 */
288 int i;
289 int line_len = last_line_length(sb);
291 for (i = 0; i < len; i++) {
292 int ch = line[i];
293 if (non_ascii(ch) || ch == '\n')
294 goto needquote;
295 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
296 goto needquote;
298 strbuf_add_wrapped_bytes(sb, line, len, -line_len, 1, max_length);
299 return;
301 needquote:
302 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
303 strbuf_addf(sb, "=?%s?q?", encoding);
304 line_len += strlen(encoding) + 5; /* 5 for =??q? */
305 for (i = 0; i < len; i++) {
306 unsigned ch = line[i] & 0xFF;
307 int is_special = is_rfc2047_special(ch);
310 * According to RFC 2047, we could encode the special character
311 * ' ' (space) with '_' (underscore) for readability. But many
312 * programs do not understand this and just leave the
313 * underscore in place. Thus, we do nothing special here, which
314 * causes ' ' to be encoded as '=20', avoiding this problem.
317 if (line_len + 2 + (is_special ? 3 : 1) > max_encoded_length) {
318 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
319 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
322 if (is_special) {
323 strbuf_addf(sb, "=%02X", ch);
324 line_len += 3;
325 } else {
326 strbuf_addch(sb, ch);
327 line_len++;
330 strbuf_addstr(sb, "?=");
333 void pp_user_info(const struct pretty_print_context *pp,
334 const char *what, struct strbuf *sb,
335 const char *line, const char *encoding)
337 char *date;
338 int namelen;
339 unsigned long time;
340 int tz;
342 if (pp->fmt == CMIT_FMT_ONELINE)
343 return;
344 date = strchr(line, '>');
345 if (!date)
346 return;
347 namelen = ++date - line;
348 time = strtoul(date, &date, 10);
349 tz = strtol(date, NULL, 10);
351 if (pp->fmt == CMIT_FMT_EMAIL) {
352 char *name_tail = strchr(line, '<');
353 int display_name_length;
354 if (!name_tail)
355 return;
356 while (line < name_tail && isspace(name_tail[-1]))
357 name_tail--;
358 display_name_length = name_tail - line;
359 strbuf_addstr(sb, "From: ");
360 if (!has_rfc822_specials(line, display_name_length)) {
361 add_rfc2047(sb, line, display_name_length, encoding);
362 } else {
363 struct strbuf quoted = STRBUF_INIT;
364 add_rfc822_quoted(&quoted, line, display_name_length);
365 add_rfc2047(sb, quoted.buf, quoted.len, encoding);
366 strbuf_release(&quoted);
368 if (namelen - display_name_length + last_line_length(sb) > 78) {
369 strbuf_addch(sb, '\n');
370 if (!isspace(name_tail[0]))
371 strbuf_addch(sb, ' ');
373 strbuf_add(sb, name_tail, namelen - display_name_length);
374 strbuf_addch(sb, '\n');
375 } else {
376 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
377 (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
378 " ", namelen, line);
380 switch (pp->fmt) {
381 case CMIT_FMT_MEDIUM:
382 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode));
383 break;
384 case CMIT_FMT_EMAIL:
385 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
386 break;
387 case CMIT_FMT_FULLER:
388 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
389 break;
390 default:
391 /* notin' */
392 break;
396 static int is_empty_line(const char *line, int *len_p)
398 int len = *len_p;
399 while (len && isspace(line[len-1]))
400 len--;
401 *len_p = len;
402 return !len;
405 static const char *skip_empty_lines(const char *msg)
407 for (;;) {
408 int linelen = get_one_line(msg);
409 int ll = linelen;
410 if (!linelen)
411 break;
412 if (!is_empty_line(msg, &ll))
413 break;
414 msg += linelen;
416 return msg;
419 static void add_merge_info(const struct pretty_print_context *pp,
420 struct strbuf *sb, const struct commit *commit)
422 struct commit_list *parent = commit->parents;
424 if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
425 !parent || !parent->next)
426 return;
428 strbuf_addstr(sb, "Merge:");
430 while (parent) {
431 struct commit *p = parent->item;
432 const char *hex = NULL;
433 if (pp->abbrev)
434 hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
435 if (!hex)
436 hex = sha1_to_hex(p->object.sha1);
437 parent = parent->next;
439 strbuf_addf(sb, " %s", hex);
441 strbuf_addch(sb, '\n');
444 static char *get_header(const struct commit *commit, const char *key)
446 int key_len = strlen(key);
447 const char *line = commit->buffer;
449 while (line) {
450 const char *eol = strchr(line, '\n'), *next;
452 if (line == eol)
453 return NULL;
454 if (!eol) {
455 warning("malformed commit (header is missing newline): %s",
456 sha1_to_hex(commit->object.sha1));
457 eol = line + strlen(line);
458 next = NULL;
459 } else
460 next = eol + 1;
461 if (eol - line > key_len &&
462 !strncmp(line, key, key_len) &&
463 line[key_len] == ' ') {
464 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
466 line = next;
468 return NULL;
471 static char *replace_encoding_header(char *buf, const char *encoding)
473 struct strbuf tmp = STRBUF_INIT;
474 size_t start, len;
475 char *cp = buf;
477 /* guess if there is an encoding header before a \n\n */
478 while (strncmp(cp, "encoding ", strlen("encoding "))) {
479 cp = strchr(cp, '\n');
480 if (!cp || *++cp == '\n')
481 return buf;
483 start = cp - buf;
484 cp = strchr(cp, '\n');
485 if (!cp)
486 return buf; /* should not happen but be defensive */
487 len = cp + 1 - (buf + start);
489 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
490 if (is_encoding_utf8(encoding)) {
491 /* we have re-coded to UTF-8; drop the header */
492 strbuf_remove(&tmp, start, len);
493 } else {
494 /* just replaces XXXX in 'encoding XXXX\n' */
495 strbuf_splice(&tmp, start + strlen("encoding "),
496 len - strlen("encoding \n"),
497 encoding, strlen(encoding));
499 return strbuf_detach(&tmp, NULL);
502 char *logmsg_reencode(const struct commit *commit,
503 const char *output_encoding)
505 static const char *utf8 = "UTF-8";
506 const char *use_encoding;
507 char *encoding;
508 char *out;
510 if (!*output_encoding)
511 return NULL;
512 encoding = get_header(commit, "encoding");
513 use_encoding = encoding ? encoding : utf8;
514 if (!strcmp(use_encoding, output_encoding))
515 if (encoding) /* we'll strip encoding header later */
516 out = xstrdup(commit->buffer);
517 else
518 return NULL; /* nothing to do */
519 else
520 out = reencode_string(commit->buffer,
521 output_encoding, use_encoding);
522 if (out)
523 out = replace_encoding_header(out, output_encoding);
525 free(encoding);
526 return out;
529 static int mailmap_name(char *email, int email_len, char *name, int name_len)
531 static struct string_list *mail_map;
532 if (!mail_map) {
533 mail_map = xcalloc(1, sizeof(*mail_map));
534 read_mailmap(mail_map, NULL);
536 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
539 static size_t format_person_part(struct strbuf *sb, char part,
540 const char *msg, int len, enum date_mode dmode)
542 /* currently all placeholders have same length */
543 const int placeholder_len = 2;
544 int tz;
545 unsigned long date = 0;
546 char person_name[1024];
547 char person_mail[1024];
548 struct ident_split s;
549 const char *name_start, *name_end, *mail_start, *mail_end;
551 if (split_ident_line(&s, msg, len) < 0)
552 goto skip;
554 name_start = s.name_begin;
555 name_end = s.name_end;
556 mail_start = s.mail_begin;
557 mail_end = s.mail_end;
559 if (part == 'N' || part == 'E') { /* mailmap lookup */
560 snprintf(person_name, sizeof(person_name), "%.*s",
561 (int)(name_end - name_start), name_start);
562 snprintf(person_mail, sizeof(person_mail), "%.*s",
563 (int)(mail_end - mail_start), mail_start);
564 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
565 name_start = person_name;
566 name_end = name_start + strlen(person_name);
567 mail_start = person_mail;
568 mail_end = mail_start + strlen(person_mail);
570 if (part == 'n' || part == 'N') { /* name */
571 strbuf_add(sb, name_start, name_end-name_start);
572 return placeholder_len;
574 if (part == 'e' || part == 'E') { /* email */
575 strbuf_add(sb, mail_start, mail_end-mail_start);
576 return placeholder_len;
579 if (!s.date_begin)
580 goto skip;
582 date = strtoul(s.date_begin, NULL, 10);
584 if (part == 't') { /* date, UNIX timestamp */
585 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
586 return placeholder_len;
589 /* parse tz */
590 tz = strtoul(s.tz_begin + 1, NULL, 10);
591 if (*s.tz_begin == '-')
592 tz = -tz;
594 switch (part) {
595 case 'd': /* date */
596 strbuf_addstr(sb, show_date(date, tz, dmode));
597 return placeholder_len;
598 case 'D': /* date, RFC2822 style */
599 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
600 return placeholder_len;
601 case 'r': /* date, relative */
602 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
603 return placeholder_len;
604 case 'i': /* date, ISO 8601 */
605 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
606 return placeholder_len;
609 skip:
611 * reading from either a bogus commit, or a reflog entry with
612 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
613 * to compute a valid return value.
615 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
616 || part == 'D' || part == 'r' || part == 'i')
617 return placeholder_len;
619 return 0; /* unknown placeholder */
622 struct chunk {
623 size_t off;
624 size_t len;
627 struct format_commit_context {
628 const struct commit *commit;
629 const struct pretty_print_context *pretty_ctx;
630 unsigned commit_header_parsed:1;
631 unsigned commit_message_parsed:1;
632 unsigned commit_signature_parsed:1;
633 struct {
634 char *gpg_output;
635 char good_bad;
636 char *signer;
637 } signature;
638 char *message;
639 size_t width, indent1, indent2;
641 /* These offsets are relative to the start of the commit message. */
642 struct chunk author;
643 struct chunk committer;
644 struct chunk encoding;
645 size_t message_off;
646 size_t subject_off;
647 size_t body_off;
649 /* The following ones are relative to the result struct strbuf. */
650 struct chunk abbrev_commit_hash;
651 struct chunk abbrev_tree_hash;
652 struct chunk abbrev_parent_hashes;
653 size_t wrap_start;
656 static int add_again(struct strbuf *sb, struct chunk *chunk)
658 if (chunk->len) {
659 strbuf_adddup(sb, chunk->off, chunk->len);
660 return 1;
664 * We haven't seen this chunk before. Our caller is surely
665 * going to add it the hard way now. Remember the most likely
666 * start of the to-be-added chunk: the current end of the
667 * struct strbuf.
669 chunk->off = sb->len;
670 return 0;
673 static void parse_commit_header(struct format_commit_context *context)
675 const char *msg = context->message;
676 int i;
678 for (i = 0; msg[i]; i++) {
679 int eol;
680 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
681 ; /* do nothing */
683 if (i == eol) {
684 break;
685 } else if (!prefixcmp(msg + i, "author ")) {
686 context->author.off = i + 7;
687 context->author.len = eol - i - 7;
688 } else if (!prefixcmp(msg + i, "committer ")) {
689 context->committer.off = i + 10;
690 context->committer.len = eol - i - 10;
691 } else if (!prefixcmp(msg + i, "encoding ")) {
692 context->encoding.off = i + 9;
693 context->encoding.len = eol - i - 9;
695 i = eol;
697 context->message_off = i;
698 context->commit_header_parsed = 1;
701 static int istitlechar(char c)
703 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
704 (c >= '0' && c <= '9') || c == '.' || c == '_';
707 static void format_sanitized_subject(struct strbuf *sb, const char *msg)
709 size_t trimlen;
710 size_t start_len = sb->len;
711 int space = 2;
713 for (; *msg && *msg != '\n'; msg++) {
714 if (istitlechar(*msg)) {
715 if (space == 1)
716 strbuf_addch(sb, '-');
717 space = 0;
718 strbuf_addch(sb, *msg);
719 if (*msg == '.')
720 while (*(msg+1) == '.')
721 msg++;
722 } else
723 space |= 1;
726 /* trim any trailing '.' or '-' characters */
727 trimlen = 0;
728 while (sb->len - trimlen > start_len &&
729 (sb->buf[sb->len - 1 - trimlen] == '.'
730 || sb->buf[sb->len - 1 - trimlen] == '-'))
731 trimlen++;
732 strbuf_remove(sb, sb->len - trimlen, trimlen);
735 const char *format_subject(struct strbuf *sb, const char *msg,
736 const char *line_separator)
738 int first = 1;
740 for (;;) {
741 const char *line = msg;
742 int linelen = get_one_line(line);
744 msg += linelen;
745 if (!linelen || is_empty_line(line, &linelen))
746 break;
748 if (!sb)
749 continue;
750 strbuf_grow(sb, linelen + 2);
751 if (!first)
752 strbuf_addstr(sb, line_separator);
753 strbuf_add(sb, line, linelen);
754 first = 0;
756 return msg;
759 static void parse_commit_message(struct format_commit_context *c)
761 const char *msg = c->message + c->message_off;
762 const char *start = c->message;
764 msg = skip_empty_lines(msg);
765 c->subject_off = msg - start;
767 msg = format_subject(NULL, msg, NULL);
768 msg = skip_empty_lines(msg);
769 c->body_off = msg - start;
771 c->commit_message_parsed = 1;
774 static void format_decoration(struct strbuf *sb, const struct commit *commit)
776 struct name_decoration *d;
777 const char *prefix = " (";
779 load_ref_decorations(DECORATE_SHORT_REFS);
780 d = lookup_decoration(&name_decoration, &commit->object);
781 while (d) {
782 strbuf_addstr(sb, prefix);
783 prefix = ", ";
784 strbuf_addstr(sb, d->name);
785 d = d->next;
787 if (prefix[0] == ',')
788 strbuf_addch(sb, ')');
791 static void strbuf_wrap(struct strbuf *sb, size_t pos,
792 size_t width, size_t indent1, size_t indent2)
794 struct strbuf tmp = STRBUF_INIT;
796 if (pos)
797 strbuf_add(&tmp, sb->buf, pos);
798 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
799 (int) indent1, (int) indent2, (int) width);
800 strbuf_swap(&tmp, sb);
801 strbuf_release(&tmp);
804 static void rewrap_message_tail(struct strbuf *sb,
805 struct format_commit_context *c,
806 size_t new_width, size_t new_indent1,
807 size_t new_indent2)
809 if (c->width == new_width && c->indent1 == new_indent1 &&
810 c->indent2 == new_indent2)
811 return;
812 if (c->wrap_start < sb->len)
813 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
814 c->wrap_start = sb->len;
815 c->width = new_width;
816 c->indent1 = new_indent1;
817 c->indent2 = new_indent2;
820 static struct {
821 char result;
822 const char *check;
823 } signature_check[] = {
824 { 'G', ": Good signature from " },
825 { 'B', ": BAD signature from " },
828 static void parse_signature_lines(struct format_commit_context *ctx)
830 const char *buf = ctx->signature.gpg_output;
831 int i;
833 for (i = 0; i < ARRAY_SIZE(signature_check); i++) {
834 const char *found = strstr(buf, signature_check[i].check);
835 const char *next;
836 if (!found)
837 continue;
838 ctx->signature.good_bad = signature_check[i].result;
839 found += strlen(signature_check[i].check);
840 next = strchrnul(found, '\n');
841 ctx->signature.signer = xmemdupz(found, next - found);
842 break;
846 static void parse_commit_signature(struct format_commit_context *ctx)
848 struct strbuf payload = STRBUF_INIT;
849 struct strbuf signature = STRBUF_INIT;
850 struct strbuf gpg_output = STRBUF_INIT;
851 int status;
853 ctx->commit_signature_parsed = 1;
855 if (parse_signed_commit(ctx->commit->object.sha1,
856 &payload, &signature) <= 0)
857 goto out;
858 status = verify_signed_buffer(payload.buf, payload.len,
859 signature.buf, signature.len,
860 &gpg_output);
861 if (status && !gpg_output.len)
862 goto out;
863 ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL);
864 parse_signature_lines(ctx);
866 out:
867 strbuf_release(&gpg_output);
868 strbuf_release(&payload);
869 strbuf_release(&signature);
873 static int format_reflog_person(struct strbuf *sb,
874 char part,
875 struct reflog_walk_info *log,
876 enum date_mode dmode)
878 const char *ident;
880 if (!log)
881 return 2;
883 ident = get_reflog_ident(log);
884 if (!ident)
885 return 2;
887 return format_person_part(sb, part, ident, strlen(ident), dmode);
890 static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
891 void *context)
893 struct format_commit_context *c = context;
894 const struct commit *commit = c->commit;
895 const char *msg = c->message;
896 struct commit_list *p;
897 int h1, h2;
899 /* these are independent of the commit */
900 switch (placeholder[0]) {
901 case 'C':
902 if (placeholder[1] == '(') {
903 const char *end = strchr(placeholder + 2, ')');
904 char color[COLOR_MAXLEN];
905 if (!end)
906 return 0;
907 color_parse_mem(placeholder + 2,
908 end - (placeholder + 2),
909 "--pretty format", color);
910 strbuf_addstr(sb, color);
911 return end - placeholder + 1;
913 if (!prefixcmp(placeholder + 1, "red")) {
914 strbuf_addstr(sb, GIT_COLOR_RED);
915 return 4;
916 } else if (!prefixcmp(placeholder + 1, "green")) {
917 strbuf_addstr(sb, GIT_COLOR_GREEN);
918 return 6;
919 } else if (!prefixcmp(placeholder + 1, "blue")) {
920 strbuf_addstr(sb, GIT_COLOR_BLUE);
921 return 5;
922 } else if (!prefixcmp(placeholder + 1, "reset")) {
923 strbuf_addstr(sb, GIT_COLOR_RESET);
924 return 6;
925 } else
926 return 0;
927 case 'n': /* newline */
928 strbuf_addch(sb, '\n');
929 return 1;
930 case 'x':
931 /* %x00 == NUL, %x0a == LF, etc. */
932 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
933 h1 <= 16 &&
934 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
935 h2 <= 16) {
936 strbuf_addch(sb, (h1<<4)|h2);
937 return 3;
938 } else
939 return 0;
940 case 'w':
941 if (placeholder[1] == '(') {
942 unsigned long width = 0, indent1 = 0, indent2 = 0;
943 char *next;
944 const char *start = placeholder + 2;
945 const char *end = strchr(start, ')');
946 if (!end)
947 return 0;
948 if (end > start) {
949 width = strtoul(start, &next, 10);
950 if (*next == ',') {
951 indent1 = strtoul(next + 1, &next, 10);
952 if (*next == ',') {
953 indent2 = strtoul(next + 1,
954 &next, 10);
957 if (*next != ')')
958 return 0;
960 rewrap_message_tail(sb, c, width, indent1, indent2);
961 return end - placeholder + 1;
962 } else
963 return 0;
966 /* these depend on the commit */
967 if (!commit->object.parsed)
968 parse_object(commit->object.sha1);
970 switch (placeholder[0]) {
971 case 'H': /* commit hash */
972 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
973 return 1;
974 case 'h': /* abbreviated commit hash */
975 if (add_again(sb, &c->abbrev_commit_hash))
976 return 1;
977 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
978 c->pretty_ctx->abbrev));
979 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
980 return 1;
981 case 'T': /* tree hash */
982 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
983 return 1;
984 case 't': /* abbreviated tree hash */
985 if (add_again(sb, &c->abbrev_tree_hash))
986 return 1;
987 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
988 c->pretty_ctx->abbrev));
989 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
990 return 1;
991 case 'P': /* parent hashes */
992 for (p = commit->parents; p; p = p->next) {
993 if (p != commit->parents)
994 strbuf_addch(sb, ' ');
995 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
997 return 1;
998 case 'p': /* abbreviated parent hashes */
999 if (add_again(sb, &c->abbrev_parent_hashes))
1000 return 1;
1001 for (p = commit->parents; p; p = p->next) {
1002 if (p != commit->parents)
1003 strbuf_addch(sb, ' ');
1004 strbuf_addstr(sb, find_unique_abbrev(
1005 p->item->object.sha1,
1006 c->pretty_ctx->abbrev));
1008 c->abbrev_parent_hashes.len = sb->len -
1009 c->abbrev_parent_hashes.off;
1010 return 1;
1011 case 'm': /* left/right/bottom */
1012 strbuf_addstr(sb, get_revision_mark(NULL, commit));
1013 return 1;
1014 case 'd':
1015 format_decoration(sb, commit);
1016 return 1;
1017 case 'g': /* reflog info */
1018 switch(placeholder[1]) {
1019 case 'd': /* reflog selector */
1020 case 'D':
1021 if (c->pretty_ctx->reflog_info)
1022 get_reflog_selector(sb,
1023 c->pretty_ctx->reflog_info,
1024 c->pretty_ctx->date_mode,
1025 c->pretty_ctx->date_mode_explicit,
1026 (placeholder[1] == 'd'));
1027 return 2;
1028 case 's': /* reflog message */
1029 if (c->pretty_ctx->reflog_info)
1030 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1031 return 2;
1032 case 'n':
1033 case 'N':
1034 case 'e':
1035 case 'E':
1036 return format_reflog_person(sb,
1037 placeholder[1],
1038 c->pretty_ctx->reflog_info,
1039 c->pretty_ctx->date_mode);
1041 return 0; /* unknown %g placeholder */
1042 case 'N':
1043 if (c->pretty_ctx->show_notes) {
1044 format_display_notes(commit->object.sha1, sb,
1045 get_log_output_encoding(), 0);
1046 return 1;
1048 return 0;
1051 if (placeholder[0] == 'G') {
1052 if (!c->commit_signature_parsed)
1053 parse_commit_signature(c);
1054 switch (placeholder[1]) {
1055 case 'G':
1056 if (c->signature.gpg_output)
1057 strbuf_addstr(sb, c->signature.gpg_output);
1058 break;
1059 case '?':
1060 switch (c->signature.good_bad) {
1061 case 'G':
1062 case 'B':
1063 strbuf_addch(sb, c->signature.good_bad);
1065 break;
1066 case 'S':
1067 if (c->signature.signer)
1068 strbuf_addstr(sb, c->signature.signer);
1069 break;
1071 return 2;
1075 /* For the rest we have to parse the commit header. */
1076 if (!c->commit_header_parsed)
1077 parse_commit_header(c);
1079 switch (placeholder[0]) {
1080 case 'a': /* author ... */
1081 return format_person_part(sb, placeholder[1],
1082 msg + c->author.off, c->author.len,
1083 c->pretty_ctx->date_mode);
1084 case 'c': /* committer ... */
1085 return format_person_part(sb, placeholder[1],
1086 msg + c->committer.off, c->committer.len,
1087 c->pretty_ctx->date_mode);
1088 case 'e': /* encoding */
1089 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
1090 return 1;
1091 case 'B': /* raw body */
1092 /* message_off is always left at the initial newline */
1093 strbuf_addstr(sb, msg + c->message_off + 1);
1094 return 1;
1097 /* Now we need to parse the commit message. */
1098 if (!c->commit_message_parsed)
1099 parse_commit_message(c);
1101 switch (placeholder[0]) {
1102 case 's': /* subject */
1103 format_subject(sb, msg + c->subject_off, " ");
1104 return 1;
1105 case 'f': /* sanitized subject */
1106 format_sanitized_subject(sb, msg + c->subject_off);
1107 return 1;
1108 case 'b': /* body */
1109 strbuf_addstr(sb, msg + c->body_off);
1110 return 1;
1112 return 0; /* unknown placeholder */
1115 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
1116 void *context)
1118 int consumed;
1119 size_t orig_len;
1120 enum {
1121 NO_MAGIC,
1122 ADD_LF_BEFORE_NON_EMPTY,
1123 DEL_LF_BEFORE_EMPTY,
1124 ADD_SP_BEFORE_NON_EMPTY
1125 } magic = NO_MAGIC;
1127 switch (placeholder[0]) {
1128 case '-':
1129 magic = DEL_LF_BEFORE_EMPTY;
1130 break;
1131 case '+':
1132 magic = ADD_LF_BEFORE_NON_EMPTY;
1133 break;
1134 case ' ':
1135 magic = ADD_SP_BEFORE_NON_EMPTY;
1136 break;
1137 default:
1138 break;
1140 if (magic != NO_MAGIC)
1141 placeholder++;
1143 orig_len = sb->len;
1144 consumed = format_commit_one(sb, placeholder, context);
1145 if (magic == NO_MAGIC)
1146 return consumed;
1148 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1149 while (sb->len && sb->buf[sb->len - 1] == '\n')
1150 strbuf_setlen(sb, sb->len - 1);
1151 } else if (orig_len != sb->len) {
1152 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1153 strbuf_insert(sb, orig_len, "\n", 1);
1154 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1155 strbuf_insert(sb, orig_len, " ", 1);
1157 return consumed + 1;
1160 static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1161 void *context)
1163 struct userformat_want *w = context;
1165 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
1166 placeholder++;
1168 switch (*placeholder) {
1169 case 'N':
1170 w->notes = 1;
1171 break;
1173 return 0;
1176 void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1178 struct strbuf dummy = STRBUF_INIT;
1180 if (!fmt) {
1181 if (!user_format)
1182 return;
1183 fmt = user_format;
1185 strbuf_expand(&dummy, fmt, userformat_want_item, w);
1186 strbuf_release(&dummy);
1189 void format_commit_message(const struct commit *commit,
1190 const char *format, struct strbuf *sb,
1191 const struct pretty_print_context *pretty_ctx)
1193 struct format_commit_context context;
1194 static const char utf8[] = "UTF-8";
1195 const char *output_enc = pretty_ctx->output_encoding;
1197 memset(&context, 0, sizeof(context));
1198 context.commit = commit;
1199 context.pretty_ctx = pretty_ctx;
1200 context.wrap_start = sb->len;
1201 context.message = commit->buffer;
1202 if (output_enc) {
1203 char *enc = get_header(commit, "encoding");
1204 if (strcmp(enc ? enc : utf8, output_enc)) {
1205 context.message = logmsg_reencode(commit, output_enc);
1206 if (!context.message)
1207 context.message = commit->buffer;
1209 free(enc);
1212 strbuf_expand(sb, format, format_commit_item, &context);
1213 rewrap_message_tail(sb, &context, 0, 0, 0);
1215 if (context.message != commit->buffer)
1216 free(context.message);
1217 free(context.signature.gpg_output);
1218 free(context.signature.signer);
1221 static void pp_header(const struct pretty_print_context *pp,
1222 const char *encoding,
1223 const struct commit *commit,
1224 const char **msg_p,
1225 struct strbuf *sb)
1227 int parents_shown = 0;
1229 for (;;) {
1230 const char *line = *msg_p;
1231 int linelen = get_one_line(*msg_p);
1233 if (!linelen)
1234 return;
1235 *msg_p += linelen;
1237 if (linelen == 1)
1238 /* End of header */
1239 return;
1241 if (pp->fmt == CMIT_FMT_RAW) {
1242 strbuf_add(sb, line, linelen);
1243 continue;
1246 if (!memcmp(line, "parent ", 7)) {
1247 if (linelen != 48)
1248 die("bad parent line in commit");
1249 continue;
1252 if (!parents_shown) {
1253 struct commit_list *parent;
1254 int num;
1255 for (parent = commit->parents, num = 0;
1256 parent;
1257 parent = parent->next, num++)
1259 /* with enough slop */
1260 strbuf_grow(sb, num * 50 + 20);
1261 add_merge_info(pp, sb, commit);
1262 parents_shown = 1;
1266 * MEDIUM == DEFAULT shows only author with dates.
1267 * FULL shows both authors but not dates.
1268 * FULLER shows both authors and dates.
1270 if (!memcmp(line, "author ", 7)) {
1271 strbuf_grow(sb, linelen + 80);
1272 pp_user_info(pp, "Author", sb, line + 7, encoding);
1274 if (!memcmp(line, "committer ", 10) &&
1275 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
1276 strbuf_grow(sb, linelen + 80);
1277 pp_user_info(pp, "Commit", sb, line + 10, encoding);
1282 void pp_title_line(const struct pretty_print_context *pp,
1283 const char **msg_p,
1284 struct strbuf *sb,
1285 const char *encoding,
1286 int need_8bit_cte)
1288 struct strbuf title;
1290 strbuf_init(&title, 80);
1291 *msg_p = format_subject(&title, *msg_p,
1292 pp->preserve_subject ? "\n" : " ");
1294 strbuf_grow(sb, title.len + 1024);
1295 if (pp->subject) {
1296 strbuf_addstr(sb, pp->subject);
1297 add_rfc2047(sb, title.buf, title.len, encoding);
1298 } else {
1299 strbuf_addbuf(sb, &title);
1301 strbuf_addch(sb, '\n');
1303 if (need_8bit_cte > 0) {
1304 const char *header_fmt =
1305 "MIME-Version: 1.0\n"
1306 "Content-Type: text/plain; charset=%s\n"
1307 "Content-Transfer-Encoding: 8bit\n";
1308 strbuf_addf(sb, header_fmt, encoding);
1310 if (pp->after_subject) {
1311 strbuf_addstr(sb, pp->after_subject);
1313 if (pp->fmt == CMIT_FMT_EMAIL) {
1314 strbuf_addch(sb, '\n');
1316 strbuf_release(&title);
1319 void pp_remainder(const struct pretty_print_context *pp,
1320 const char **msg_p,
1321 struct strbuf *sb,
1322 int indent)
1324 int first = 1;
1325 for (;;) {
1326 const char *line = *msg_p;
1327 int linelen = get_one_line(line);
1328 *msg_p += linelen;
1330 if (!linelen)
1331 break;
1333 if (is_empty_line(line, &linelen)) {
1334 if (first)
1335 continue;
1336 if (pp->fmt == CMIT_FMT_SHORT)
1337 break;
1339 first = 0;
1341 strbuf_grow(sb, linelen + indent + 20);
1342 if (indent) {
1343 memset(sb->buf + sb->len, ' ', indent);
1344 strbuf_setlen(sb, sb->len + indent);
1346 strbuf_add(sb, line, linelen);
1347 strbuf_addch(sb, '\n');
1351 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
1353 const char *encoding;
1355 encoding = get_log_output_encoding();
1356 if (encoding_p)
1357 *encoding_p = encoding;
1358 return logmsg_reencode(commit, encoding);
1361 void pretty_print_commit(const struct pretty_print_context *pp,
1362 const struct commit *commit,
1363 struct strbuf *sb)
1365 unsigned long beginning_of_body;
1366 int indent = 4;
1367 const char *msg = commit->buffer;
1368 char *reencoded;
1369 const char *encoding;
1370 int need_8bit_cte = pp->need_8bit_cte;
1372 if (pp->fmt == CMIT_FMT_USERFORMAT) {
1373 format_commit_message(commit, user_format, sb, pp);
1374 return;
1377 reencoded = reencode_commit_message(commit, &encoding);
1378 if (reencoded) {
1379 msg = reencoded;
1382 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1383 indent = 0;
1386 * We need to check and emit Content-type: to mark it
1387 * as 8-bit if we haven't done so.
1389 if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1390 int i, ch, in_body;
1392 for (in_body = i = 0; (ch = msg[i]); i++) {
1393 if (!in_body) {
1394 /* author could be non 7-bit ASCII but
1395 * the log may be so; skip over the
1396 * header part first.
1398 if (ch == '\n' && msg[i+1] == '\n')
1399 in_body = 1;
1401 else if (non_ascii(ch)) {
1402 need_8bit_cte = 1;
1403 break;
1408 pp_header(pp, encoding, commit, &msg, sb);
1409 if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
1410 strbuf_addch(sb, '\n');
1413 /* Skip excess blank lines at the beginning of body, if any... */
1414 msg = skip_empty_lines(msg);
1416 /* These formats treat the title line specially. */
1417 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1418 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
1420 beginning_of_body = sb->len;
1421 if (pp->fmt != CMIT_FMT_ONELINE)
1422 pp_remainder(pp, &msg, sb, indent);
1423 strbuf_rtrim(sb);
1425 /* Make sure there is an EOLN for the non-oneline case */
1426 if (pp->fmt != CMIT_FMT_ONELINE)
1427 strbuf_addch(sb, '\n');
1430 * The caller may append additional body text in e-mail
1431 * format. Make sure we did not strip the blank line
1432 * between the header and the body.
1434 if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1435 strbuf_addch(sb, '\n');
1437 if (pp->show_notes)
1438 format_display_notes(commit->object.sha1, sb, encoding,
1439 NOTES_SHOW_HEADER | NOTES_INDENT);
1441 free(reencoded);
1444 void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
1445 struct strbuf *sb)
1447 struct pretty_print_context pp = {0};
1448 pp.fmt = fmt;
1449 pretty_print_commit(&pp, commit, sb);