Make clone parse the default refspec with the normal code
[git/dscho.git] / pretty.c
blobf49929479f7e68a5214b6b55c31f10e6151c1787
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 ch = (ch & 0xff);
87 return ((ch & 0x80) || (ch == 0x1b));
90 static int is_rfc2047_special(char ch)
92 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
95 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
96 const char *encoding)
98 int i, last;
100 for (i = 0; i < len; i++) {
101 int ch = line[i];
102 if (non_ascii(ch))
103 goto needquote;
104 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
105 goto needquote;
107 strbuf_add(sb, line, len);
108 return;
110 needquote:
111 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
112 strbuf_addf(sb, "=?%s?q?", encoding);
113 for (i = last = 0; i < len; i++) {
114 unsigned ch = line[i] & 0xFF;
116 * We encode ' ' using '=20' even though rfc2047
117 * allows using '_' for readability. Unfortunately,
118 * many programs do not understand this and just
119 * leave the underscore in place.
121 if (is_rfc2047_special(ch) || ch == ' ') {
122 strbuf_add(sb, line + last, i - last);
123 strbuf_addf(sb, "=%02X", ch);
124 last = i + 1;
127 strbuf_add(sb, line + last, len - last);
128 strbuf_addstr(sb, "?=");
131 void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
132 const char *line, enum date_mode dmode,
133 const char *encoding)
135 char *date;
136 int namelen;
137 unsigned long time;
138 int tz;
139 const char *filler = " ";
141 if (fmt == CMIT_FMT_ONELINE)
142 return;
143 date = strchr(line, '>');
144 if (!date)
145 return;
146 namelen = ++date - line;
147 time = strtoul(date, &date, 10);
148 tz = strtol(date, NULL, 10);
150 if (fmt == CMIT_FMT_EMAIL) {
151 char *name_tail = strchr(line, '<');
152 int display_name_length;
153 if (!name_tail)
154 return;
155 while (line < name_tail && isspace(name_tail[-1]))
156 name_tail--;
157 display_name_length = name_tail - line;
158 filler = "";
159 strbuf_addstr(sb, "From: ");
160 add_rfc2047(sb, line, display_name_length, encoding);
161 strbuf_add(sb, name_tail, namelen - display_name_length);
162 strbuf_addch(sb, '\n');
163 } else {
164 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
165 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
166 filler, namelen, line);
168 switch (fmt) {
169 case CMIT_FMT_MEDIUM:
170 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
171 break;
172 case CMIT_FMT_EMAIL:
173 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
174 break;
175 case CMIT_FMT_FULLER:
176 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
177 break;
178 default:
179 /* notin' */
180 break;
184 static int is_empty_line(const char *line, int *len_p)
186 int len = *len_p;
187 while (len && isspace(line[len-1]))
188 len--;
189 *len_p = len;
190 return !len;
193 static const char *skip_empty_lines(const char *msg)
195 for (;;) {
196 int linelen = get_one_line(msg);
197 int ll = linelen;
198 if (!linelen)
199 break;
200 if (!is_empty_line(msg, &ll))
201 break;
202 msg += linelen;
204 return msg;
207 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
208 const struct commit *commit, int abbrev)
210 struct commit_list *parent = commit->parents;
212 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
213 !parent || !parent->next)
214 return;
216 strbuf_addstr(sb, "Merge:");
218 while (parent) {
219 struct commit *p = parent->item;
220 const char *hex = NULL;
221 if (abbrev)
222 hex = find_unique_abbrev(p->object.sha1, abbrev);
223 if (!hex)
224 hex = sha1_to_hex(p->object.sha1);
225 parent = parent->next;
227 strbuf_addf(sb, " %s", hex);
229 strbuf_addch(sb, '\n');
232 static char *get_header(const struct commit *commit, const char *key)
234 int key_len = strlen(key);
235 const char *line = commit->buffer;
237 for (;;) {
238 const char *eol = strchr(line, '\n'), *next;
240 if (line == eol)
241 return NULL;
242 if (!eol) {
243 eol = line + strlen(line);
244 next = NULL;
245 } else
246 next = eol + 1;
247 if (eol - line > key_len &&
248 !strncmp(line, key, key_len) &&
249 line[key_len] == ' ') {
250 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
252 line = next;
256 static char *replace_encoding_header(char *buf, const char *encoding)
258 struct strbuf tmp = STRBUF_INIT;
259 size_t start, len;
260 char *cp = buf;
262 /* guess if there is an encoding header before a \n\n */
263 while (strncmp(cp, "encoding ", strlen("encoding "))) {
264 cp = strchr(cp, '\n');
265 if (!cp || *++cp == '\n')
266 return buf;
268 start = cp - buf;
269 cp = strchr(cp, '\n');
270 if (!cp)
271 return buf; /* should not happen but be defensive */
272 len = cp + 1 - (buf + start);
274 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
275 if (is_encoding_utf8(encoding)) {
276 /* we have re-coded to UTF-8; drop the header */
277 strbuf_remove(&tmp, start, len);
278 } else {
279 /* just replaces XXXX in 'encoding XXXX\n' */
280 strbuf_splice(&tmp, start + strlen("encoding "),
281 len - strlen("encoding \n"),
282 encoding, strlen(encoding));
284 return strbuf_detach(&tmp, NULL);
287 static char *logmsg_reencode(const struct commit *commit,
288 const char *output_encoding)
290 static const char *utf8 = "utf-8";
291 const char *use_encoding;
292 char *encoding;
293 char *out;
295 if (!*output_encoding)
296 return NULL;
297 encoding = get_header(commit, "encoding");
298 use_encoding = encoding ? encoding : utf8;
299 if (!strcmp(use_encoding, output_encoding))
300 if (encoding) /* we'll strip encoding header later */
301 out = xstrdup(commit->buffer);
302 else
303 return NULL; /* nothing to do */
304 else
305 out = reencode_string(commit->buffer,
306 output_encoding, use_encoding);
307 if (out)
308 out = replace_encoding_header(out, output_encoding);
310 free(encoding);
311 return out;
314 static int mailmap_name(char *email, int email_len, char *name, int name_len)
316 static struct string_list *mail_map;
317 if (!mail_map) {
318 mail_map = xcalloc(1, sizeof(*mail_map));
319 read_mailmap(mail_map, NULL);
321 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
324 static size_t format_person_part(struct strbuf *sb, char part,
325 const char *msg, int len, enum date_mode dmode)
327 /* currently all placeholders have same length */
328 const int placeholder_len = 2;
329 int start, end, tz = 0;
330 unsigned long date = 0;
331 char *ep;
332 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
333 char person_name[1024];
334 char person_mail[1024];
336 /* advance 'end' to point to email start delimiter */
337 for (end = 0; end < len && msg[end] != '<'; end++)
338 ; /* do nothing */
341 * When end points at the '<' that we found, it should have
342 * matching '>' later, which means 'end' must be strictly
343 * below len - 1.
345 if (end >= len - 2)
346 goto skip;
348 /* Seek for both name and email part */
349 name_start = msg;
350 name_end = msg+end;
351 while (name_end > name_start && isspace(*(name_end-1)))
352 name_end--;
353 mail_start = msg+end+1;
354 mail_end = mail_start;
355 while (mail_end < msg_end && *mail_end != '>')
356 mail_end++;
357 if (mail_end == msg_end)
358 goto skip;
359 end = mail_end-msg;
361 if (part == 'N' || part == 'E') { /* mailmap lookup */
362 strlcpy(person_name, name_start, name_end-name_start+1);
363 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
364 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
365 name_start = person_name;
366 name_end = name_start + strlen(person_name);
367 mail_start = person_mail;
368 mail_end = mail_start + strlen(person_mail);
370 if (part == 'n' || part == 'N') { /* name */
371 strbuf_add(sb, name_start, name_end-name_start);
372 return placeholder_len;
374 if (part == 'e' || part == 'E') { /* email */
375 strbuf_add(sb, mail_start, mail_end-mail_start);
376 return placeholder_len;
379 /* advance 'start' to point to date start delimiter */
380 for (start = end + 1; start < len && isspace(msg[start]); start++)
381 ; /* do nothing */
382 if (start >= len)
383 goto skip;
384 date = strtoul(msg + start, &ep, 10);
385 if (msg + start == ep)
386 goto skip;
388 if (part == 't') { /* date, UNIX timestamp */
389 strbuf_add(sb, msg + start, ep - (msg + start));
390 return placeholder_len;
393 /* parse tz */
394 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
395 ; /* do nothing */
396 if (start + 1 < len) {
397 tz = strtoul(msg + start + 1, NULL, 10);
398 if (msg[start] == '-')
399 tz = -tz;
402 switch (part) {
403 case 'd': /* date */
404 strbuf_addstr(sb, show_date(date, tz, dmode));
405 return placeholder_len;
406 case 'D': /* date, RFC2822 style */
407 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
408 return placeholder_len;
409 case 'r': /* date, relative */
410 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
411 return placeholder_len;
412 case 'i': /* date, ISO 8601 */
413 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
414 return placeholder_len;
417 skip:
419 * bogus commit, 'sb' cannot be updated, but we still need to
420 * compute a valid return value.
422 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
423 || part == 'D' || part == 'r' || part == 'i')
424 return placeholder_len;
426 return 0; /* unknown placeholder */
429 struct chunk {
430 size_t off;
431 size_t len;
434 struct format_commit_context {
435 const struct commit *commit;
436 enum date_mode dmode;
437 unsigned commit_header_parsed:1;
438 unsigned commit_message_parsed:1;
440 /* These offsets are relative to the start of the commit message. */
441 struct chunk author;
442 struct chunk committer;
443 struct chunk encoding;
444 size_t message_off;
445 size_t subject_off;
446 size_t body_off;
448 /* The following ones are relative to the result struct strbuf. */
449 struct chunk abbrev_commit_hash;
450 struct chunk abbrev_tree_hash;
451 struct chunk abbrev_parent_hashes;
454 static int add_again(struct strbuf *sb, struct chunk *chunk)
456 if (chunk->len) {
457 strbuf_adddup(sb, chunk->off, chunk->len);
458 return 1;
462 * We haven't seen this chunk before. Our caller is surely
463 * going to add it the hard way now. Remember the most likely
464 * start of the to-be-added chunk: the current end of the
465 * struct strbuf.
467 chunk->off = sb->len;
468 return 0;
471 static void parse_commit_header(struct format_commit_context *context)
473 const char *msg = context->commit->buffer;
474 int i;
476 for (i = 0; msg[i]; i++) {
477 int eol;
478 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
479 ; /* do nothing */
481 if (i == eol) {
482 break;
483 } else if (!prefixcmp(msg + i, "author ")) {
484 context->author.off = i + 7;
485 context->author.len = eol - i - 7;
486 } else if (!prefixcmp(msg + i, "committer ")) {
487 context->committer.off = i + 10;
488 context->committer.len = eol - i - 10;
489 } else if (!prefixcmp(msg + i, "encoding ")) {
490 context->encoding.off = i + 9;
491 context->encoding.len = eol - i - 9;
493 i = eol;
495 context->message_off = i;
496 context->commit_header_parsed = 1;
499 const char *format_subject(struct strbuf *sb, const char *msg,
500 const char *line_separator)
502 int first = 1;
504 for (;;) {
505 const char *line = msg;
506 int linelen = get_one_line(line);
508 msg += linelen;
509 if (!linelen || is_empty_line(line, &linelen))
510 break;
512 if (!sb)
513 continue;
514 strbuf_grow(sb, linelen + 2);
515 if (!first)
516 strbuf_addstr(sb, line_separator);
517 strbuf_add(sb, line, linelen);
518 first = 0;
520 return msg;
523 static void parse_commit_message(struct format_commit_context *c)
525 const char *msg = c->commit->buffer + c->message_off;
526 const char *start = c->commit->buffer;
528 msg = skip_empty_lines(msg);
529 c->subject_off = msg - start;
531 msg = format_subject(NULL, msg, NULL);
532 msg = skip_empty_lines(msg);
533 c->body_off = msg - start;
535 c->commit_message_parsed = 1;
538 static void format_decoration(struct strbuf *sb, const struct commit *commit)
540 struct name_decoration *d;
541 const char *prefix = " (";
543 load_ref_decorations();
544 d = lookup_decoration(&name_decoration, &commit->object);
545 while (d) {
546 strbuf_addstr(sb, prefix);
547 prefix = ", ";
548 strbuf_addstr(sb, d->name);
549 d = d->next;
551 if (prefix[0] == ',')
552 strbuf_addch(sb, ')');
555 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
556 void *context)
558 struct format_commit_context *c = context;
559 const struct commit *commit = c->commit;
560 const char *msg = commit->buffer;
561 struct commit_list *p;
562 int h1, h2;
564 /* these are independent of the commit */
565 switch (placeholder[0]) {
566 case 'C':
567 if (placeholder[1] == '(') {
568 const char *end = strchr(placeholder + 2, ')');
569 char color[COLOR_MAXLEN];
570 if (!end)
571 return 0;
572 color_parse_mem(placeholder + 2,
573 end - (placeholder + 2),
574 "--pretty format", color);
575 strbuf_addstr(sb, color);
576 return end - placeholder + 1;
578 if (!prefixcmp(placeholder + 1, "red")) {
579 strbuf_addstr(sb, GIT_COLOR_RED);
580 return 4;
581 } else if (!prefixcmp(placeholder + 1, "green")) {
582 strbuf_addstr(sb, GIT_COLOR_GREEN);
583 return 6;
584 } else if (!prefixcmp(placeholder + 1, "blue")) {
585 strbuf_addstr(sb, GIT_COLOR_BLUE);
586 return 5;
587 } else if (!prefixcmp(placeholder + 1, "reset")) {
588 strbuf_addstr(sb, GIT_COLOR_RESET);
589 return 6;
590 } else
591 return 0;
592 case 'n': /* newline */
593 strbuf_addch(sb, '\n');
594 return 1;
595 case 'x':
596 /* %x00 == NUL, %x0a == LF, etc. */
597 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
598 h1 <= 16 &&
599 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
600 h2 <= 16) {
601 strbuf_addch(sb, (h1<<4)|h2);
602 return 3;
603 } else
604 return 0;
607 /* these depend on the commit */
608 if (!commit->object.parsed)
609 parse_object(commit->object.sha1);
611 switch (placeholder[0]) {
612 case 'H': /* commit hash */
613 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
614 return 1;
615 case 'h': /* abbreviated commit hash */
616 if (add_again(sb, &c->abbrev_commit_hash))
617 return 1;
618 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
619 DEFAULT_ABBREV));
620 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
621 return 1;
622 case 'T': /* tree hash */
623 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
624 return 1;
625 case 't': /* abbreviated tree hash */
626 if (add_again(sb, &c->abbrev_tree_hash))
627 return 1;
628 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
629 DEFAULT_ABBREV));
630 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
631 return 1;
632 case 'P': /* parent hashes */
633 for (p = commit->parents; p; p = p->next) {
634 if (p != commit->parents)
635 strbuf_addch(sb, ' ');
636 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
638 return 1;
639 case 'p': /* abbreviated parent hashes */
640 if (add_again(sb, &c->abbrev_parent_hashes))
641 return 1;
642 for (p = commit->parents; p; p = p->next) {
643 if (p != commit->parents)
644 strbuf_addch(sb, ' ');
645 strbuf_addstr(sb, find_unique_abbrev(
646 p->item->object.sha1, DEFAULT_ABBREV));
648 c->abbrev_parent_hashes.len = sb->len -
649 c->abbrev_parent_hashes.off;
650 return 1;
651 case 'm': /* left/right/bottom */
652 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
653 ? '-'
654 : (commit->object.flags & SYMMETRIC_LEFT)
655 ? '<'
656 : '>');
657 return 1;
658 case 'd':
659 format_decoration(sb, commit);
660 return 1;
663 /* For the rest we have to parse the commit header. */
664 if (!c->commit_header_parsed)
665 parse_commit_header(c);
667 switch (placeholder[0]) {
668 case 'a': /* author ... */
669 return format_person_part(sb, placeholder[1],
670 msg + c->author.off, c->author.len,
671 c->dmode);
672 case 'c': /* committer ... */
673 return format_person_part(sb, placeholder[1],
674 msg + c->committer.off, c->committer.len,
675 c->dmode);
676 case 'e': /* encoding */
677 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
678 return 1;
681 /* Now we need to parse the commit message. */
682 if (!c->commit_message_parsed)
683 parse_commit_message(c);
685 switch (placeholder[0]) {
686 case 's': /* subject */
687 format_subject(sb, msg + c->subject_off, " ");
688 return 1;
689 case 'b': /* body */
690 strbuf_addstr(sb, msg + c->body_off);
691 return 1;
693 return 0; /* unknown placeholder */
696 void format_commit_message(const struct commit *commit,
697 const void *format, struct strbuf *sb,
698 enum date_mode dmode)
700 struct format_commit_context context;
702 memset(&context, 0, sizeof(context));
703 context.commit = commit;
704 context.dmode = dmode;
705 strbuf_expand(sb, format, format_commit_item, &context);
708 static void pp_header(enum cmit_fmt fmt,
709 int abbrev,
710 enum date_mode dmode,
711 const char *encoding,
712 const struct commit *commit,
713 const char **msg_p,
714 struct strbuf *sb)
716 int parents_shown = 0;
718 for (;;) {
719 const char *line = *msg_p;
720 int linelen = get_one_line(*msg_p);
722 if (!linelen)
723 return;
724 *msg_p += linelen;
726 if (linelen == 1)
727 /* End of header */
728 return;
730 if (fmt == CMIT_FMT_RAW) {
731 strbuf_add(sb, line, linelen);
732 continue;
735 if (!memcmp(line, "parent ", 7)) {
736 if (linelen != 48)
737 die("bad parent line in commit");
738 continue;
741 if (!parents_shown) {
742 struct commit_list *parent;
743 int num;
744 for (parent = commit->parents, num = 0;
745 parent;
746 parent = parent->next, num++)
748 /* with enough slop */
749 strbuf_grow(sb, num * 50 + 20);
750 add_merge_info(fmt, sb, commit, abbrev);
751 parents_shown = 1;
755 * MEDIUM == DEFAULT shows only author with dates.
756 * FULL shows both authors but not dates.
757 * FULLER shows both authors and dates.
759 if (!memcmp(line, "author ", 7)) {
760 strbuf_grow(sb, linelen + 80);
761 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
763 if (!memcmp(line, "committer ", 10) &&
764 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
765 strbuf_grow(sb, linelen + 80);
766 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
771 void pp_title_line(enum cmit_fmt fmt,
772 const char **msg_p,
773 struct strbuf *sb,
774 const char *subject,
775 const char *after_subject,
776 const char *encoding,
777 int need_8bit_cte)
779 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
780 struct strbuf title;
782 strbuf_init(&title, 80);
783 *msg_p = format_subject(&title, *msg_p, line_separator);
785 strbuf_grow(sb, title.len + 1024);
786 if (subject) {
787 strbuf_addstr(sb, subject);
788 add_rfc2047(sb, title.buf, title.len, encoding);
789 } else {
790 strbuf_addbuf(sb, &title);
792 strbuf_addch(sb, '\n');
794 if (need_8bit_cte > 0) {
795 const char *header_fmt =
796 "MIME-Version: 1.0\n"
797 "Content-Type: text/plain; charset=%s\n"
798 "Content-Transfer-Encoding: 8bit\n";
799 strbuf_addf(sb, header_fmt, encoding);
801 if (after_subject) {
802 strbuf_addstr(sb, after_subject);
804 if (fmt == CMIT_FMT_EMAIL) {
805 strbuf_addch(sb, '\n');
807 strbuf_release(&title);
810 void pp_remainder(enum cmit_fmt fmt,
811 const char **msg_p,
812 struct strbuf *sb,
813 int indent)
815 int first = 1;
816 for (;;) {
817 const char *line = *msg_p;
818 int linelen = get_one_line(line);
819 *msg_p += linelen;
821 if (!linelen)
822 break;
824 if (is_empty_line(line, &linelen)) {
825 if (first)
826 continue;
827 if (fmt == CMIT_FMT_SHORT)
828 break;
830 first = 0;
832 strbuf_grow(sb, linelen + indent + 20);
833 if (indent) {
834 memset(sb->buf + sb->len, ' ', indent);
835 strbuf_setlen(sb, sb->len + indent);
837 strbuf_add(sb, line, linelen);
838 strbuf_addch(sb, '\n');
842 char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
844 const char *encoding;
846 encoding = (git_log_output_encoding
847 ? git_log_output_encoding
848 : git_commit_encoding);
849 if (!encoding)
850 encoding = "utf-8";
851 if (encoding_p)
852 *encoding_p = encoding;
853 return logmsg_reencode(commit, encoding);
856 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
857 struct strbuf *sb, int abbrev,
858 const char *subject, const char *after_subject,
859 enum date_mode dmode, int need_8bit_cte)
861 unsigned long beginning_of_body;
862 int indent = 4;
863 const char *msg = commit->buffer;
864 char *reencoded;
865 const char *encoding;
867 if (fmt == CMIT_FMT_USERFORMAT) {
868 format_commit_message(commit, user_format, sb, dmode);
869 return;
872 reencoded = reencode_commit_message(commit, &encoding);
873 if (reencoded) {
874 msg = reencoded;
877 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
878 indent = 0;
881 * We need to check and emit Content-type: to mark it
882 * as 8-bit if we haven't done so.
884 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
885 int i, ch, in_body;
887 for (in_body = i = 0; (ch = msg[i]); i++) {
888 if (!in_body) {
889 /* author could be non 7-bit ASCII but
890 * the log may be so; skip over the
891 * header part first.
893 if (ch == '\n' && msg[i+1] == '\n')
894 in_body = 1;
896 else if (non_ascii(ch)) {
897 need_8bit_cte = 1;
898 break;
903 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
904 if (fmt != CMIT_FMT_ONELINE && !subject) {
905 strbuf_addch(sb, '\n');
908 /* Skip excess blank lines at the beginning of body, if any... */
909 msg = skip_empty_lines(msg);
911 /* These formats treat the title line specially. */
912 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
913 pp_title_line(fmt, &msg, sb, subject,
914 after_subject, encoding, need_8bit_cte);
916 beginning_of_body = sb->len;
917 if (fmt != CMIT_FMT_ONELINE)
918 pp_remainder(fmt, &msg, sb, indent);
919 strbuf_rtrim(sb);
921 /* Make sure there is an EOLN for the non-oneline case */
922 if (fmt != CMIT_FMT_ONELINE)
923 strbuf_addch(sb, '\n');
926 * The caller may append additional body text in e-mail
927 * format. Make sure we did not strip the blank line
928 * between the header and the body.
930 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
931 strbuf_addch(sb, '\n');
932 free(reencoded);