make it easier for people who just want to get rid of 'git gc --auto'
[git/spearce.git] / pretty.c
blob16bfb86cd3ce6d6b471cdc313114563ca78837dc
1 #include "cache.h"
2 #include "commit.h"
3 #include "utf8.h"
4 #include "diff.h"
5 #include "revision.h"
7 static struct cmt_fmt_map {
8 const char *n;
9 size_t cmp_len;
10 enum cmit_fmt v;
11 } cmt_fmts[] = {
12 { "raw", 1, CMIT_FMT_RAW },
13 { "medium", 1, CMIT_FMT_MEDIUM },
14 { "short", 1, CMIT_FMT_SHORT },
15 { "email", 1, CMIT_FMT_EMAIL },
16 { "full", 5, CMIT_FMT_FULL },
17 { "fuller", 5, CMIT_FMT_FULLER },
18 { "oneline", 1, CMIT_FMT_ONELINE },
19 { "format:", 7, CMIT_FMT_USERFORMAT},
22 static char *user_format;
24 enum cmit_fmt get_commit_format(const char *arg)
26 int i;
28 if (!arg || !*arg)
29 return CMIT_FMT_DEFAULT;
30 if (*arg == '=')
31 arg++;
32 if (!prefixcmp(arg, "format:")) {
33 free(user_format);
34 user_format = xstrdup(arg + 7);
35 return CMIT_FMT_USERFORMAT;
37 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
38 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
39 !strncmp(arg, cmt_fmts[i].n, strlen(arg)))
40 return cmt_fmts[i].v;
43 die("invalid --pretty format: %s", arg);
47 * Generic support for pretty-printing the header
49 static int get_one_line(const char *msg)
51 int ret = 0;
53 for (;;) {
54 char c = *msg++;
55 if (!c)
56 break;
57 ret++;
58 if (c == '\n')
59 break;
61 return ret;
64 /* High bit set, or ISO-2022-INT */
65 int non_ascii(int ch)
67 ch = (ch & 0xff);
68 return ((ch & 0x80) || (ch == 0x1b));
71 static int is_rfc2047_special(char ch)
73 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
76 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
77 const char *encoding)
79 int i, last;
81 for (i = 0; i < len; i++) {
82 int ch = line[i];
83 if (non_ascii(ch))
84 goto needquote;
85 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
86 goto needquote;
88 strbuf_add(sb, line, len);
89 return;
91 needquote:
92 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
93 strbuf_addf(sb, "=?%s?q?", encoding);
94 for (i = last = 0; i < len; i++) {
95 unsigned ch = line[i] & 0xFF;
97 * We encode ' ' using '=20' even though rfc2047
98 * allows using '_' for readability. Unfortunately,
99 * many programs do not understand this and just
100 * leave the underscore in place.
102 if (is_rfc2047_special(ch) || ch == ' ') {
103 strbuf_add(sb, line + last, i - last);
104 strbuf_addf(sb, "=%02X", ch);
105 last = i + 1;
108 strbuf_add(sb, line + last, len - last);
109 strbuf_addstr(sb, "?=");
112 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
113 const char *line, enum date_mode dmode,
114 const char *encoding)
116 char *date;
117 int namelen;
118 unsigned long time;
119 int tz;
120 const char *filler = " ";
122 if (fmt == CMIT_FMT_ONELINE)
123 return;
124 date = strchr(line, '>');
125 if (!date)
126 return;
127 namelen = ++date - line;
128 time = strtoul(date, &date, 10);
129 tz = strtol(date, NULL, 10);
131 if (fmt == CMIT_FMT_EMAIL) {
132 char *name_tail = strchr(line, '<');
133 int display_name_length;
134 if (!name_tail)
135 return;
136 while (line < name_tail && isspace(name_tail[-1]))
137 name_tail--;
138 display_name_length = name_tail - line;
139 filler = "";
140 strbuf_addstr(sb, "From: ");
141 add_rfc2047(sb, line, display_name_length, encoding);
142 strbuf_add(sb, name_tail, namelen - display_name_length);
143 strbuf_addch(sb, '\n');
144 } else {
145 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
146 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
147 filler, namelen, line);
149 switch (fmt) {
150 case CMIT_FMT_MEDIUM:
151 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
152 break;
153 case CMIT_FMT_EMAIL:
154 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
155 break;
156 case CMIT_FMT_FULLER:
157 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
158 break;
159 default:
160 /* notin' */
161 break;
165 static int is_empty_line(const char *line, int *len_p)
167 int len = *len_p;
168 while (len && isspace(line[len-1]))
169 len--;
170 *len_p = len;
171 return !len;
174 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
175 const struct commit *commit, int abbrev)
177 struct commit_list *parent = commit->parents;
179 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
180 !parent || !parent->next)
181 return;
183 strbuf_addstr(sb, "Merge:");
185 while (parent) {
186 struct commit *p = parent->item;
187 const char *hex = NULL;
188 const char *dots;
189 if (abbrev)
190 hex = find_unique_abbrev(p->object.sha1, abbrev);
191 if (!hex)
192 hex = sha1_to_hex(p->object.sha1);
193 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
194 parent = parent->next;
196 strbuf_addf(sb, " %s%s", hex, dots);
198 strbuf_addch(sb, '\n');
201 static char *get_header(const struct commit *commit, const char *key)
203 int key_len = strlen(key);
204 const char *line = commit->buffer;
206 for (;;) {
207 const char *eol = strchr(line, '\n'), *next;
209 if (line == eol)
210 return NULL;
211 if (!eol) {
212 eol = line + strlen(line);
213 next = NULL;
214 } else
215 next = eol + 1;
216 if (eol - line > key_len &&
217 !strncmp(line, key, key_len) &&
218 line[key_len] == ' ') {
219 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
221 line = next;
225 static char *replace_encoding_header(char *buf, const char *encoding)
227 struct strbuf tmp;
228 size_t start, len;
229 char *cp = buf;
231 /* guess if there is an encoding header before a \n\n */
232 while (strncmp(cp, "encoding ", strlen("encoding "))) {
233 cp = strchr(cp, '\n');
234 if (!cp || *++cp == '\n')
235 return buf;
237 start = cp - buf;
238 cp = strchr(cp, '\n');
239 if (!cp)
240 return buf; /* should not happen but be defensive */
241 len = cp + 1 - (buf + start);
243 strbuf_init(&tmp, 0);
244 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
245 if (is_encoding_utf8(encoding)) {
246 /* we have re-coded to UTF-8; drop the header */
247 strbuf_remove(&tmp, start, len);
248 } else {
249 /* just replaces XXXX in 'encoding XXXX\n' */
250 strbuf_splice(&tmp, start + strlen("encoding "),
251 len - strlen("encoding \n"),
252 encoding, strlen(encoding));
254 return strbuf_detach(&tmp, NULL);
257 static char *logmsg_reencode(const struct commit *commit,
258 const char *output_encoding)
260 static const char *utf8 = "utf-8";
261 const char *use_encoding;
262 char *encoding;
263 char *out;
265 if (!*output_encoding)
266 return NULL;
267 encoding = get_header(commit, "encoding");
268 use_encoding = encoding ? encoding : utf8;
269 if (!strcmp(use_encoding, output_encoding))
270 if (encoding) /* we'll strip encoding header later */
271 out = xstrdup(commit->buffer);
272 else
273 return NULL; /* nothing to do */
274 else
275 out = reencode_string(commit->buffer,
276 output_encoding, use_encoding);
277 if (out)
278 out = replace_encoding_header(out, output_encoding);
280 free(encoding);
281 return out;
284 static size_t format_person_part(struct strbuf *sb, char part,
285 const char *msg, int len)
287 /* currently all placeholders have same length */
288 const int placeholder_len = 2;
289 int start, end, tz = 0;
290 unsigned long date = 0;
291 char *ep;
293 /* advance 'end' to point to email start delimiter */
294 for (end = 0; end < len && msg[end] != '<'; end++)
295 ; /* do nothing */
298 * When end points at the '<' that we found, it should have
299 * matching '>' later, which means 'end' must be strictly
300 * below len - 1.
302 if (end >= len - 2)
303 goto skip;
305 if (part == 'n') { /* name */
306 while (end > 0 && isspace(msg[end - 1]))
307 end--;
308 strbuf_add(sb, msg, end);
309 return placeholder_len;
311 start = ++end; /* save email start position */
313 /* advance 'end' to point to email end delimiter */
314 for ( ; end < len && msg[end] != '>'; end++)
315 ; /* do nothing */
317 if (end >= len)
318 goto skip;
320 if (part == 'e') { /* email */
321 strbuf_add(sb, msg + start, end - start);
322 return placeholder_len;
325 /* advance 'start' to point to date start delimiter */
326 for (start = end + 1; start < len && isspace(msg[start]); start++)
327 ; /* do nothing */
328 if (start >= len)
329 goto skip;
330 date = strtoul(msg + start, &ep, 10);
331 if (msg + start == ep)
332 goto skip;
334 if (part == 't') { /* date, UNIX timestamp */
335 strbuf_add(sb, msg + start, ep - (msg + start));
336 return placeholder_len;
339 /* parse tz */
340 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
341 ; /* do nothing */
342 if (start + 1 < len) {
343 tz = strtoul(msg + start + 1, NULL, 10);
344 if (msg[start] == '-')
345 tz = -tz;
348 switch (part) {
349 case 'd': /* date */
350 strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
351 return placeholder_len;
352 case 'D': /* date, RFC2822 style */
353 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
354 return placeholder_len;
355 case 'r': /* date, relative */
356 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
357 return placeholder_len;
358 case 'i': /* date, ISO 8601 */
359 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
360 return placeholder_len;
363 skip:
365 * bogus commit, 'sb' cannot be updated, but we still need to
366 * compute a valid return value.
368 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
369 || part == 'D' || part == 'r' || part == 'i')
370 return placeholder_len;
372 return 0; /* unknown placeholder */
375 struct chunk {
376 size_t off;
377 size_t len;
380 struct format_commit_context {
381 const struct commit *commit;
383 /* These offsets are relative to the start of the commit message. */
384 int commit_header_parsed;
385 struct chunk subject;
386 struct chunk author;
387 struct chunk committer;
388 struct chunk encoding;
389 size_t body_off;
391 /* The following ones are relative to the result struct strbuf. */
392 struct chunk abbrev_commit_hash;
393 struct chunk abbrev_tree_hash;
394 struct chunk abbrev_parent_hashes;
397 static int add_again(struct strbuf *sb, struct chunk *chunk)
399 if (chunk->len) {
400 strbuf_adddup(sb, chunk->off, chunk->len);
401 return 1;
405 * We haven't seen this chunk before. Our caller is surely
406 * going to add it the hard way now. Remember the most likely
407 * start of the to-be-added chunk: the current end of the
408 * struct strbuf.
410 chunk->off = sb->len;
411 return 0;
414 static void parse_commit_header(struct format_commit_context *context)
416 const char *msg = context->commit->buffer;
417 int i;
418 enum { HEADER, SUBJECT, BODY } state;
420 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
421 int eol;
422 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
423 ; /* do nothing */
425 if (state == SUBJECT) {
426 context->subject.off = i;
427 context->subject.len = eol - i;
428 i = eol;
430 if (i == eol) {
431 state++;
432 /* strip empty lines */
433 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
434 eol++;
435 } else if (!prefixcmp(msg + i, "author ")) {
436 context->author.off = i + 7;
437 context->author.len = eol - i - 7;
438 } else if (!prefixcmp(msg + i, "committer ")) {
439 context->committer.off = i + 10;
440 context->committer.len = eol - i - 10;
441 } else if (!prefixcmp(msg + i, "encoding ")) {
442 context->encoding.off = i + 9;
443 context->encoding.len = eol - i - 9;
445 i = eol;
446 if (!msg[i])
447 break;
449 context->body_off = i;
450 context->commit_header_parsed = 1;
453 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
454 void *context)
456 struct format_commit_context *c = context;
457 const struct commit *commit = c->commit;
458 const char *msg = commit->buffer;
459 struct commit_list *p;
461 /* these are independent of the commit */
462 switch (placeholder[0]) {
463 case 'C':
464 if (!prefixcmp(placeholder + 1, "red")) {
465 strbuf_addstr(sb, "\033[31m");
466 return 4;
467 } else if (!prefixcmp(placeholder + 1, "green")) {
468 strbuf_addstr(sb, "\033[32m");
469 return 6;
470 } else if (!prefixcmp(placeholder + 1, "blue")) {
471 strbuf_addstr(sb, "\033[34m");
472 return 5;
473 } else if (!prefixcmp(placeholder + 1, "reset")) {
474 strbuf_addstr(sb, "\033[m");
475 return 6;
476 } else
477 return 0;
478 case 'n': /* newline */
479 strbuf_addch(sb, '\n');
480 return 1;
483 /* these depend on the commit */
484 if (!commit->object.parsed)
485 parse_object(commit->object.sha1);
487 switch (placeholder[0]) {
488 case 'H': /* commit hash */
489 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
490 return 1;
491 case 'h': /* abbreviated commit hash */
492 if (add_again(sb, &c->abbrev_commit_hash))
493 return 1;
494 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
495 DEFAULT_ABBREV));
496 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
497 return 1;
498 case 'T': /* tree hash */
499 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
500 return 1;
501 case 't': /* abbreviated tree hash */
502 if (add_again(sb, &c->abbrev_tree_hash))
503 return 1;
504 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
505 DEFAULT_ABBREV));
506 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
507 return 1;
508 case 'P': /* parent hashes */
509 for (p = commit->parents; p; p = p->next) {
510 if (p != commit->parents)
511 strbuf_addch(sb, ' ');
512 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
514 return 1;
515 case 'p': /* abbreviated parent hashes */
516 if (add_again(sb, &c->abbrev_parent_hashes))
517 return 1;
518 for (p = commit->parents; p; p = p->next) {
519 if (p != commit->parents)
520 strbuf_addch(sb, ' ');
521 strbuf_addstr(sb, find_unique_abbrev(
522 p->item->object.sha1, DEFAULT_ABBREV));
524 c->abbrev_parent_hashes.len = sb->len -
525 c->abbrev_parent_hashes.off;
526 return 1;
527 case 'm': /* left/right/bottom */
528 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
529 ? '-'
530 : (commit->object.flags & SYMMETRIC_LEFT)
531 ? '<'
532 : '>');
533 return 1;
536 /* For the rest we have to parse the commit header. */
537 if (!c->commit_header_parsed)
538 parse_commit_header(c);
540 switch (placeholder[0]) {
541 case 's': /* subject */
542 strbuf_add(sb, msg + c->subject.off, c->subject.len);
543 return 1;
544 case 'a': /* author ... */
545 return format_person_part(sb, placeholder[1],
546 msg + c->author.off, c->author.len);
547 case 'c': /* committer ... */
548 return format_person_part(sb, placeholder[1],
549 msg + c->committer.off, c->committer.len);
550 case 'e': /* encoding */
551 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
552 return 1;
553 case 'b': /* body */
554 strbuf_addstr(sb, msg + c->body_off);
555 return 1;
557 return 0; /* unknown placeholder */
560 void format_commit_message(const struct commit *commit,
561 const void *format, struct strbuf *sb)
563 struct format_commit_context context;
565 memset(&context, 0, sizeof(context));
566 context.commit = commit;
567 strbuf_expand(sb, format, format_commit_item, &context);
570 static void pp_header(enum cmit_fmt fmt,
571 int abbrev,
572 enum date_mode dmode,
573 const char *encoding,
574 const struct commit *commit,
575 const char **msg_p,
576 struct strbuf *sb)
578 int parents_shown = 0;
580 for (;;) {
581 const char *line = *msg_p;
582 int linelen = get_one_line(*msg_p);
584 if (!linelen)
585 return;
586 *msg_p += linelen;
588 if (linelen == 1)
589 /* End of header */
590 return;
592 if (fmt == CMIT_FMT_RAW) {
593 strbuf_add(sb, line, linelen);
594 continue;
597 if (!memcmp(line, "parent ", 7)) {
598 if (linelen != 48)
599 die("bad parent line in commit");
600 continue;
603 if (!parents_shown) {
604 struct commit_list *parent;
605 int num;
606 for (parent = commit->parents, num = 0;
607 parent;
608 parent = parent->next, num++)
610 /* with enough slop */
611 strbuf_grow(sb, num * 50 + 20);
612 add_merge_info(fmt, sb, commit, abbrev);
613 parents_shown = 1;
617 * MEDIUM == DEFAULT shows only author with dates.
618 * FULL shows both authors but not dates.
619 * FULLER shows both authors and dates.
621 if (!memcmp(line, "author ", 7)) {
622 strbuf_grow(sb, linelen + 80);
623 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
625 if (!memcmp(line, "committer ", 10) &&
626 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
627 strbuf_grow(sb, linelen + 80);
628 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
633 void pp_title_line(enum cmit_fmt fmt,
634 const char **msg_p,
635 struct strbuf *sb,
636 const char *subject,
637 const char *after_subject,
638 const char *encoding,
639 int need_8bit_cte)
641 struct strbuf title;
643 strbuf_init(&title, 80);
645 for (;;) {
646 const char *line = *msg_p;
647 int linelen = get_one_line(line);
649 *msg_p += linelen;
650 if (!linelen || is_empty_line(line, &linelen))
651 break;
653 strbuf_grow(&title, linelen + 2);
654 if (title.len) {
655 if (fmt == CMIT_FMT_EMAIL) {
656 strbuf_addch(&title, '\n');
658 strbuf_addch(&title, ' ');
660 strbuf_add(&title, line, linelen);
663 strbuf_grow(sb, title.len + 1024);
664 if (subject) {
665 strbuf_addstr(sb, subject);
666 add_rfc2047(sb, title.buf, title.len, encoding);
667 } else {
668 strbuf_addbuf(sb, &title);
670 strbuf_addch(sb, '\n');
672 if (need_8bit_cte > 0) {
673 const char *header_fmt =
674 "MIME-Version: 1.0\n"
675 "Content-Type: text/plain; charset=%s\n"
676 "Content-Transfer-Encoding: 8bit\n";
677 strbuf_addf(sb, header_fmt, encoding);
679 if (after_subject) {
680 strbuf_addstr(sb, after_subject);
682 if (fmt == CMIT_FMT_EMAIL) {
683 strbuf_addch(sb, '\n');
685 strbuf_release(&title);
688 void pp_remainder(enum cmit_fmt fmt,
689 const char **msg_p,
690 struct strbuf *sb,
691 int indent)
693 int first = 1;
694 for (;;) {
695 const char *line = *msg_p;
696 int linelen = get_one_line(line);
697 *msg_p += linelen;
699 if (!linelen)
700 break;
702 if (is_empty_line(line, &linelen)) {
703 if (first)
704 continue;
705 if (fmt == CMIT_FMT_SHORT)
706 break;
708 first = 0;
710 strbuf_grow(sb, linelen + indent + 20);
711 if (indent) {
712 memset(sb->buf + sb->len, ' ', indent);
713 strbuf_setlen(sb, sb->len + indent);
715 strbuf_add(sb, line, linelen);
716 strbuf_addch(sb, '\n');
720 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
721 struct strbuf *sb, int abbrev,
722 const char *subject, const char *after_subject,
723 enum date_mode dmode, int need_8bit_cte)
725 unsigned long beginning_of_body;
726 int indent = 4;
727 const char *msg = commit->buffer;
728 char *reencoded;
729 const char *encoding;
731 if (fmt == CMIT_FMT_USERFORMAT) {
732 format_commit_message(commit, user_format, sb);
733 return;
736 encoding = (git_log_output_encoding
737 ? git_log_output_encoding
738 : git_commit_encoding);
739 if (!encoding)
740 encoding = "utf-8";
741 reencoded = logmsg_reencode(commit, encoding);
742 if (reencoded) {
743 msg = reencoded;
746 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
747 indent = 0;
750 * We need to check and emit Content-type: to mark it
751 * as 8-bit if we haven't done so.
753 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
754 int i, ch, in_body;
756 for (in_body = i = 0; (ch = msg[i]); i++) {
757 if (!in_body) {
758 /* author could be non 7-bit ASCII but
759 * the log may be so; skip over the
760 * header part first.
762 if (ch == '\n' && msg[i+1] == '\n')
763 in_body = 1;
765 else if (non_ascii(ch)) {
766 need_8bit_cte = 1;
767 break;
772 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
773 if (fmt != CMIT_FMT_ONELINE && !subject) {
774 strbuf_addch(sb, '\n');
777 /* Skip excess blank lines at the beginning of body, if any... */
778 for (;;) {
779 int linelen = get_one_line(msg);
780 int ll = linelen;
781 if (!linelen)
782 break;
783 if (!is_empty_line(msg, &ll))
784 break;
785 msg += linelen;
788 /* These formats treat the title line specially. */
789 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
790 pp_title_line(fmt, &msg, sb, subject,
791 after_subject, encoding, need_8bit_cte);
793 beginning_of_body = sb->len;
794 if (fmt != CMIT_FMT_ONELINE)
795 pp_remainder(fmt, &msg, sb, indent);
796 strbuf_rtrim(sb);
798 /* Make sure there is an EOLN for the non-oneline case */
799 if (fmt != CMIT_FMT_ONELINE)
800 strbuf_addch(sb, '\n');
803 * The caller may append additional body text in e-mail
804 * format. Make sure we did not strip the blank line
805 * between the header and the body.
807 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
808 strbuf_addch(sb, '\n');
809 free(reencoded);