]> git.ipfire.org Git - thirdparty/git.git/blob - pretty.c
9d7922dcc603a1d7235ad0cb035f638ccaf5a497
[thirdparty/git.git] / pretty.c
1 #include "cache.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "utf8.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "string-list.h"
11 #include "mailmap.h"
12 #include "log-tree.h"
13 #include "notes.h"
14 #include "color.h"
15 #include "reflog-walk.h"
16 #include "gpg-interface.h"
17 #include "trailer.h"
18 #include "run-command.h"
19
20 /*
21 * The limit for formatting directives, which enable the caller to append
22 * arbitrarily many bytes to the formatted buffer. This includes padding
23 * and wrapping formatters.
24 */
25 #define FORMATTING_LIMIT (16 * 1024)
26
27 static char *user_format;
28 static struct cmt_fmt_map {
29 const char *name;
30 enum cmit_fmt format;
31 int is_tformat;
32 int expand_tabs_in_log;
33 int is_alias;
34 enum date_mode_type default_date_mode_type;
35 const char *user_format;
36 } *commit_formats;
37 static size_t builtin_formats_len;
38 static size_t commit_formats_len;
39 static size_t commit_formats_alloc;
40 static struct cmt_fmt_map *find_commit_format(const char *sought);
41
42 int commit_format_is_empty(enum cmit_fmt fmt)
43 {
44 return fmt == CMIT_FMT_USERFORMAT && !*user_format;
45 }
46
47 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
48 {
49 free(user_format);
50 user_format = xstrdup(cp);
51 if (is_tformat)
52 rev->use_terminator = 1;
53 rev->commit_format = CMIT_FMT_USERFORMAT;
54 }
55
56 static int git_pretty_formats_config(const char *var, const char *value,
57 void *cb UNUSED)
58 {
59 struct cmt_fmt_map *commit_format = NULL;
60 const char *name;
61 const char *fmt;
62 int i;
63
64 if (!skip_prefix(var, "pretty.", &name))
65 return 0;
66
67 for (i = 0; i < builtin_formats_len; i++) {
68 if (!strcmp(commit_formats[i].name, name))
69 return 0;
70 }
71
72 for (i = builtin_formats_len; i < commit_formats_len; i++) {
73 if (!strcmp(commit_formats[i].name, name)) {
74 commit_format = &commit_formats[i];
75 break;
76 }
77 }
78
79 if (!commit_format) {
80 ALLOC_GROW(commit_formats, commit_formats_len+1,
81 commit_formats_alloc);
82 commit_format = &commit_formats[commit_formats_len];
83 memset(commit_format, 0, sizeof(*commit_format));
84 commit_formats_len++;
85 }
86
87 commit_format->name = xstrdup(name);
88 commit_format->format = CMIT_FMT_USERFORMAT;
89 if (git_config_string(&fmt, var, value))
90 return -1;
91
92 if (skip_prefix(fmt, "format:", &fmt))
93 commit_format->is_tformat = 0;
94 else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
95 commit_format->is_tformat = 1;
96 else
97 commit_format->is_alias = 1;
98 commit_format->user_format = fmt;
99
100 return 0;
101 }
102
103 static void setup_commit_formats(void)
104 {
105 struct cmt_fmt_map builtin_formats[] = {
106 { "raw", CMIT_FMT_RAW, 0, 0 },
107 { "medium", CMIT_FMT_MEDIUM, 0, 8 },
108 { "short", CMIT_FMT_SHORT, 0, 0 },
109 { "email", CMIT_FMT_EMAIL, 0, 0 },
110 { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
111 { "fuller", CMIT_FMT_FULLER, 0, 8 },
112 { "full", CMIT_FMT_FULL, 0, 8 },
113 { "oneline", CMIT_FMT_ONELINE, 1, 0 },
114 { "reference", CMIT_FMT_USERFORMAT, 1, 0,
115 0, DATE_SHORT, "%C(auto)%h (%s, %ad)" },
116 /*
117 * Please update $__git_log_pretty_formats in
118 * git-completion.bash when you add new formats.
119 */
120 };
121 commit_formats_len = ARRAY_SIZE(builtin_formats);
122 builtin_formats_len = commit_formats_len;
123 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
124 COPY_ARRAY(commit_formats, builtin_formats,
125 ARRAY_SIZE(builtin_formats));
126
127 git_config(git_pretty_formats_config, NULL);
128 }
129
130 static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
131 const char *original,
132 int num_redirections)
133 {
134 struct cmt_fmt_map *found = NULL;
135 size_t found_match_len = 0;
136 int i;
137
138 if (num_redirections >= commit_formats_len)
139 die("invalid --pretty format: "
140 "'%s' references an alias which points to itself",
141 original);
142
143 for (i = 0; i < commit_formats_len; i++) {
144 size_t match_len;
145
146 if (!starts_with(commit_formats[i].name, sought))
147 continue;
148
149 match_len = strlen(commit_formats[i].name);
150 if (found == NULL || found_match_len > match_len) {
151 found = &commit_formats[i];
152 found_match_len = match_len;
153 }
154 }
155
156 if (found && found->is_alias) {
157 found = find_commit_format_recursive(found->user_format,
158 original,
159 num_redirections+1);
160 }
161
162 return found;
163 }
164
165 static struct cmt_fmt_map *find_commit_format(const char *sought)
166 {
167 if (!commit_formats)
168 setup_commit_formats();
169
170 return find_commit_format_recursive(sought, sought, 0);
171 }
172
173 void get_commit_format(const char *arg, struct rev_info *rev)
174 {
175 struct cmt_fmt_map *commit_format;
176
177 rev->use_terminator = 0;
178 if (!arg) {
179 rev->commit_format = CMIT_FMT_DEFAULT;
180 return;
181 }
182 if (skip_prefix(arg, "format:", &arg)) {
183 save_user_format(rev, arg, 0);
184 return;
185 }
186
187 if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
188 save_user_format(rev, arg, 1);
189 return;
190 }
191
192 commit_format = find_commit_format(arg);
193 if (!commit_format)
194 die("invalid --pretty format: %s", arg);
195
196 rev->commit_format = commit_format->format;
197 rev->use_terminator = commit_format->is_tformat;
198 rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
199 if (!rev->date_mode_explicit && commit_format->default_date_mode_type)
200 rev->date_mode.type = commit_format->default_date_mode_type;
201 if (commit_format->format == CMIT_FMT_USERFORMAT) {
202 save_user_format(rev, commit_format->user_format,
203 commit_format->is_tformat);
204 }
205 }
206
207 /*
208 * Generic support for pretty-printing the header
209 */
210 static int get_one_line(const char *msg)
211 {
212 int ret = 0;
213
214 for (;;) {
215 char c = *msg++;
216 if (!c)
217 break;
218 ret++;
219 if (c == '\n')
220 break;
221 }
222 return ret;
223 }
224
225 /* High bit set, or ISO-2022-INT */
226 static int non_ascii(int ch)
227 {
228 return !isascii(ch) || ch == '\033';
229 }
230
231 int has_non_ascii(const char *s)
232 {
233 int ch;
234 if (!s)
235 return 0;
236 while ((ch = *s++) != '\0') {
237 if (non_ascii(ch))
238 return 1;
239 }
240 return 0;
241 }
242
243 static int is_rfc822_special(char ch)
244 {
245 switch (ch) {
246 case '(':
247 case ')':
248 case '<':
249 case '>':
250 case '[':
251 case ']':
252 case ':':
253 case ';':
254 case '@':
255 case ',':
256 case '.':
257 case '"':
258 case '\\':
259 return 1;
260 default:
261 return 0;
262 }
263 }
264
265 static int needs_rfc822_quoting(const char *s, int len)
266 {
267 int i;
268 for (i = 0; i < len; i++)
269 if (is_rfc822_special(s[i]))
270 return 1;
271 return 0;
272 }
273
274 static int last_line_length(struct strbuf *sb)
275 {
276 int i;
277
278 /* How many bytes are already used on the last line? */
279 for (i = sb->len - 1; i >= 0; i--)
280 if (sb->buf[i] == '\n')
281 break;
282 return sb->len - (i + 1);
283 }
284
285 static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
286 {
287 int i;
288
289 /* just a guess, we may have to also backslash-quote */
290 strbuf_grow(out, len + 2);
291
292 strbuf_addch(out, '"');
293 for (i = 0; i < len; i++) {
294 switch (s[i]) {
295 case '"':
296 case '\\':
297 strbuf_addch(out, '\\');
298 /* fall through */
299 default:
300 strbuf_addch(out, s[i]);
301 }
302 }
303 strbuf_addch(out, '"');
304 }
305
306 enum rfc2047_type {
307 RFC2047_SUBJECT,
308 RFC2047_ADDRESS
309 };
310
311 static int is_rfc2047_special(char ch, enum rfc2047_type type)
312 {
313 /*
314 * rfc2047, section 4.2:
315 *
316 * 8-bit values which correspond to printable ASCII characters other
317 * than "=", "?", and "_" (underscore), MAY be represented as those
318 * characters. (But see section 5 for restrictions.) In
319 * particular, SPACE and TAB MUST NOT be represented as themselves
320 * within encoded words.
321 */
322
323 /*
324 * rule out non-ASCII characters and non-printable characters (the
325 * non-ASCII check should be redundant as isprint() is not localized
326 * and only knows about ASCII, but be defensive about that)
327 */
328 if (non_ascii(ch) || !isprint(ch))
329 return 1;
330
331 /*
332 * rule out special printable characters (' ' should be the only
333 * whitespace character considered printable, but be defensive and use
334 * isspace())
335 */
336 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
337 return 1;
338
339 /*
340 * rfc2047, section 5.3:
341 *
342 * As a replacement for a 'word' entity within a 'phrase', for example,
343 * one that precedes an address in a From, To, or Cc header. The ABNF
344 * definition for 'phrase' from RFC 822 thus becomes:
345 *
346 * phrase = 1*( encoded-word / word )
347 *
348 * In this case the set of characters that may be used in a "Q"-encoded
349 * 'encoded-word' is restricted to: <upper and lower case ASCII
350 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
351 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
352 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
353 * 'special' by 'linear-white-space'.
354 */
355
356 if (type != RFC2047_ADDRESS)
357 return 0;
358
359 /* '=' and '_' are special cases and have been checked above */
360 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
361 }
362
363 static int needs_rfc2047_encoding(const char *line, int len)
364 {
365 int i;
366
367 for (i = 0; i < len; i++) {
368 int ch = line[i];
369 if (non_ascii(ch) || ch == '\n')
370 return 1;
371 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
372 return 1;
373 }
374
375 return 0;
376 }
377
378 static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
379 const char *encoding, enum rfc2047_type type)
380 {
381 static const int max_encoded_length = 76; /* per rfc2047 */
382 int i;
383 int line_len = last_line_length(sb);
384
385 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
386 strbuf_addf(sb, "=?%s?q?", encoding);
387 line_len += strlen(encoding) + 5; /* 5 for =??q? */
388
389 while (len) {
390 /*
391 * RFC 2047, section 5 (3):
392 *
393 * Each 'encoded-word' MUST represent an integral number of
394 * characters. A multi-octet character may not be split across
395 * adjacent 'encoded- word's.
396 */
397 const unsigned char *p = (const unsigned char *)line;
398 int chrlen = mbs_chrlen(&line, &len, encoding);
399 int is_special = (chrlen > 1) || is_rfc2047_special(*p, type);
400
401 /* "=%02X" * chrlen, or the byte itself */
402 const char *encoded_fmt = is_special ? "=%02X" : "%c";
403 int encoded_len = is_special ? 3 * chrlen : 1;
404
405 /*
406 * According to RFC 2047, we could encode the special character
407 * ' ' (space) with '_' (underscore) for readability. But many
408 * programs do not understand this and just leave the
409 * underscore in place. Thus, we do nothing special here, which
410 * causes ' ' to be encoded as '=20', avoiding this problem.
411 */
412
413 if (line_len + encoded_len + 2 > max_encoded_length) {
414 /* It won't fit with trailing "?=" --- break the line */
415 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
416 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
417 }
418
419 for (i = 0; i < chrlen; i++)
420 strbuf_addf(sb, encoded_fmt, p[i]);
421 line_len += encoded_len;
422 }
423 strbuf_addstr(sb, "?=");
424 }
425
426 const char *show_ident_date(const struct ident_split *ident,
427 const struct date_mode *mode)
428 {
429 timestamp_t date = 0;
430 long tz = 0;
431
432 if (ident->date_begin && ident->date_end)
433 date = parse_timestamp(ident->date_begin, NULL, 10);
434 if (date_overflows(date))
435 date = 0;
436 else {
437 if (ident->tz_begin && ident->tz_end)
438 tz = strtol(ident->tz_begin, NULL, 10);
439 if (tz >= INT_MAX || tz <= INT_MIN)
440 tz = 0;
441 }
442 return show_date(date, tz, mode);
443 }
444
445 static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
446 const char *buf, size_t buflen)
447 {
448 strbuf_addstr(sb, color);
449 strbuf_add(sb, buf, buflen);
450 if (*color)
451 strbuf_addstr(sb, GIT_COLOR_RESET);
452 }
453
454 static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
455 const char *line, size_t linelen,
456 int color, enum grep_context ctx,
457 enum grep_header_field field)
458 {
459 const char *buf, *eol, *line_color, *match_color;
460 regmatch_t match;
461 int eflags = 0;
462
463 buf = line;
464 eol = buf + linelen;
465
466 if (!opt || !want_color(color) || opt->invert)
467 goto end;
468
469 line_color = opt->colors[GREP_COLOR_SELECTED];
470 match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
471
472 while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) {
473 if (match.rm_so == match.rm_eo)
474 break;
475
476 strbuf_add_with_color(sb, line_color, buf, match.rm_so);
477 strbuf_add_with_color(sb, match_color, buf + match.rm_so,
478 match.rm_eo - match.rm_so);
479 buf += match.rm_eo;
480 eflags = REG_NOTBOL;
481 }
482
483 if (eflags)
484 strbuf_add_with_color(sb, line_color, buf, eol - buf);
485 else {
486 end:
487 strbuf_add(sb, buf, eol - buf);
488 }
489 }
490
491 static int use_in_body_from(const struct pretty_print_context *pp,
492 const struct ident_split *ident)
493 {
494 if (pp->rev && pp->rev->force_in_body_from)
495 return 1;
496 if (ident_cmp(pp->from_ident, ident))
497 return 1;
498 return 0;
499 }
500
501 void pp_user_info(struct pretty_print_context *pp,
502 const char *what, struct strbuf *sb,
503 const char *line, const char *encoding)
504 {
505 struct ident_split ident;
506 char *line_end;
507 const char *mailbuf, *namebuf;
508 size_t namelen, maillen;
509 int max_length = 78; /* per rfc2822 */
510
511 if (pp->fmt == CMIT_FMT_ONELINE)
512 return;
513
514 line_end = strchrnul(line, '\n');
515 if (split_ident_line(&ident, line, line_end - line))
516 return;
517
518 mailbuf = ident.mail_begin;
519 maillen = ident.mail_end - ident.mail_begin;
520 namebuf = ident.name_begin;
521 namelen = ident.name_end - ident.name_begin;
522
523 if (pp->mailmap)
524 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
525
526 if (cmit_fmt_is_mail(pp->fmt)) {
527 if (pp->from_ident && use_in_body_from(pp, &ident)) {
528 struct strbuf buf = STRBUF_INIT;
529
530 strbuf_addstr(&buf, "From: ");
531 strbuf_add(&buf, namebuf, namelen);
532 strbuf_addstr(&buf, " <");
533 strbuf_add(&buf, mailbuf, maillen);
534 strbuf_addstr(&buf, ">\n");
535 string_list_append(&pp->in_body_headers,
536 strbuf_detach(&buf, NULL));
537
538 mailbuf = pp->from_ident->mail_begin;
539 maillen = pp->from_ident->mail_end - mailbuf;
540 namebuf = pp->from_ident->name_begin;
541 namelen = pp->from_ident->name_end - namebuf;
542 }
543
544 strbuf_addstr(sb, "From: ");
545 if (pp->encode_email_headers &&
546 needs_rfc2047_encoding(namebuf, namelen)) {
547 add_rfc2047(sb, namebuf, namelen,
548 encoding, RFC2047_ADDRESS);
549 max_length = 76; /* per rfc2047 */
550 } else if (needs_rfc822_quoting(namebuf, namelen)) {
551 struct strbuf quoted = STRBUF_INIT;
552 add_rfc822_quoted(&quoted, namebuf, namelen);
553 strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
554 -6, 1, max_length);
555 strbuf_release(&quoted);
556 } else {
557 strbuf_add_wrapped_bytes(sb, namebuf, namelen,
558 -6, 1, max_length);
559 }
560
561 if (max_length <
562 last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
563 strbuf_addch(sb, '\n');
564 strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
565 } else {
566 struct strbuf id = STRBUF_INIT;
567 enum grep_header_field field = GREP_HEADER_FIELD_MAX;
568 struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
569
570 if (!strcmp(what, "Author"))
571 field = GREP_HEADER_AUTHOR;
572 else if (!strcmp(what, "Commit"))
573 field = GREP_HEADER_COMMITTER;
574
575 strbuf_addf(sb, "%s: ", what);
576 if (pp->fmt == CMIT_FMT_FULLER)
577 strbuf_addchars(sb, ' ', 4);
578
579 strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
580 (int)maillen, mailbuf);
581
582 append_line_with_color(sb, opt, id.buf, id.len, pp->color,
583 GREP_CONTEXT_HEAD, field);
584 strbuf_addch(sb, '\n');
585 strbuf_release(&id);
586 }
587
588 switch (pp->fmt) {
589 case CMIT_FMT_MEDIUM:
590 strbuf_addf(sb, "Date: %s\n",
591 show_ident_date(&ident, &pp->date_mode));
592 break;
593 case CMIT_FMT_EMAIL:
594 case CMIT_FMT_MBOXRD:
595 strbuf_addf(sb, "Date: %s\n",
596 show_ident_date(&ident, DATE_MODE(RFC2822)));
597 break;
598 case CMIT_FMT_FULLER:
599 strbuf_addf(sb, "%sDate: %s\n", what,
600 show_ident_date(&ident, &pp->date_mode));
601 break;
602 default:
603 /* notin' */
604 break;
605 }
606 }
607
608 static int is_blank_line(const char *line, int *len_p)
609 {
610 int len = *len_p;
611 while (len && isspace(line[len - 1]))
612 len--;
613 *len_p = len;
614 return !len;
615 }
616
617 const char *skip_blank_lines(const char *msg)
618 {
619 for (;;) {
620 int linelen = get_one_line(msg);
621 int ll = linelen;
622 if (!linelen)
623 break;
624 if (!is_blank_line(msg, &ll))
625 break;
626 msg += linelen;
627 }
628 return msg;
629 }
630
631 static void add_merge_info(const struct pretty_print_context *pp,
632 struct strbuf *sb, const struct commit *commit)
633 {
634 struct commit_list *parent = commit->parents;
635
636 if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
637 !parent || !parent->next)
638 return;
639
640 strbuf_addstr(sb, "Merge:");
641
642 while (parent) {
643 struct object_id *oidp = &parent->item->object.oid;
644 strbuf_addch(sb, ' ');
645 if (pp->abbrev)
646 strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
647 else
648 strbuf_addstr(sb, oid_to_hex(oidp));
649 parent = parent->next;
650 }
651 strbuf_addch(sb, '\n');
652 }
653
654 static char *get_header(const char *msg, const char *key)
655 {
656 size_t len;
657 const char *v = find_commit_header(msg, key, &len);
658 return v ? xmemdupz(v, len) : NULL;
659 }
660
661 static char *replace_encoding_header(char *buf, const char *encoding)
662 {
663 struct strbuf tmp = STRBUF_INIT;
664 size_t start, len;
665 char *cp = buf;
666
667 /* guess if there is an encoding header before a \n\n */
668 while (!starts_with(cp, "encoding ")) {
669 cp = strchr(cp, '\n');
670 if (!cp || *++cp == '\n')
671 return buf;
672 }
673 start = cp - buf;
674 cp = strchr(cp, '\n');
675 if (!cp)
676 return buf; /* should not happen but be defensive */
677 len = cp + 1 - (buf + start);
678
679 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
680 if (is_encoding_utf8(encoding)) {
681 /* we have re-coded to UTF-8; drop the header */
682 strbuf_remove(&tmp, start, len);
683 } else {
684 /* just replaces XXXX in 'encoding XXXX\n' */
685 strbuf_splice(&tmp, start + strlen("encoding "),
686 len - strlen("encoding \n"),
687 encoding, strlen(encoding));
688 }
689 return strbuf_detach(&tmp, NULL);
690 }
691
692 const char *repo_logmsg_reencode(struct repository *r,
693 const struct commit *commit,
694 char **commit_encoding,
695 const char *output_encoding)
696 {
697 static const char *utf8 = "UTF-8";
698 const char *use_encoding;
699 char *encoding;
700 const char *msg = repo_get_commit_buffer(r, commit, NULL);
701 char *out;
702
703 if (!output_encoding || !*output_encoding) {
704 if (commit_encoding)
705 *commit_encoding = get_header(msg, "encoding");
706 return msg;
707 }
708 encoding = get_header(msg, "encoding");
709 if (commit_encoding)
710 *commit_encoding = encoding;
711 use_encoding = encoding ? encoding : utf8;
712 if (same_encoding(use_encoding, output_encoding)) {
713 /*
714 * No encoding work to be done. If we have no encoding header
715 * at all, then there's nothing to do, and we can return the
716 * message verbatim (whether newly allocated or not).
717 */
718 if (!encoding)
719 return msg;
720
721 /*
722 * Otherwise, we still want to munge the encoding header in the
723 * result, which will be done by modifying the buffer. If we
724 * are using a fresh copy, we can reuse it. But if we are using
725 * the cached copy from get_commit_buffer, we need to duplicate it
726 * to avoid munging the cached copy.
727 */
728 if (msg == get_cached_commit_buffer(r, commit, NULL))
729 out = xstrdup(msg);
730 else
731 out = (char *)msg;
732 }
733 else {
734 /*
735 * There's actual encoding work to do. Do the reencoding, which
736 * still leaves the header to be replaced in the next step. At
737 * this point, we are done with msg. If we allocated a fresh
738 * copy, we can free it.
739 */
740 out = reencode_string(msg, output_encoding, use_encoding);
741 if (out)
742 repo_unuse_commit_buffer(r, commit, msg);
743 }
744
745 /*
746 * This replacement actually consumes the buffer we hand it, so we do
747 * not have to worry about freeing the old "out" here.
748 */
749 if (out)
750 out = replace_encoding_header(out, output_encoding);
751
752 if (!commit_encoding)
753 free(encoding);
754 /*
755 * If the re-encoding failed, out might be NULL here; in that
756 * case we just return the commit message verbatim.
757 */
758 return out ? out : msg;
759 }
760
761 static int mailmap_name(const char **email, size_t *email_len,
762 const char **name, size_t *name_len)
763 {
764 static struct string_list *mail_map;
765 if (!mail_map) {
766 CALLOC_ARRAY(mail_map, 1);
767 read_mailmap(mail_map);
768 }
769 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
770 }
771
772 static size_t format_person_part(struct strbuf *sb, char part,
773 const char *msg, int len,
774 const struct date_mode *dmode)
775 {
776 /* currently all placeholders have same length */
777 const int placeholder_len = 2;
778 struct ident_split s;
779 const char *name, *mail;
780 size_t maillen, namelen;
781
782 if (split_ident_line(&s, msg, len) < 0)
783 goto skip;
784
785 name = s.name_begin;
786 namelen = s.name_end - s.name_begin;
787 mail = s.mail_begin;
788 maillen = s.mail_end - s.mail_begin;
789
790 if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */
791 mailmap_name(&mail, &maillen, &name, &namelen);
792 if (part == 'n' || part == 'N') { /* name */
793 strbuf_add(sb, name, namelen);
794 return placeholder_len;
795 }
796 if (part == 'e' || part == 'E') { /* email */
797 strbuf_add(sb, mail, maillen);
798 return placeholder_len;
799 }
800 if (part == 'l' || part == 'L') { /* local-part */
801 const char *at = memchr(mail, '@', maillen);
802 if (at)
803 maillen = at - mail;
804 strbuf_add(sb, mail, maillen);
805 return placeholder_len;
806 }
807
808 if (!s.date_begin)
809 goto skip;
810
811 if (part == 't') { /* date, UNIX timestamp */
812 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
813 return placeholder_len;
814 }
815
816 switch (part) {
817 case 'd': /* date */
818 strbuf_addstr(sb, show_ident_date(&s, dmode));
819 return placeholder_len;
820 case 'D': /* date, RFC2822 style */
821 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
822 return placeholder_len;
823 case 'r': /* date, relative */
824 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
825 return placeholder_len;
826 case 'i': /* date, ISO 8601-like */
827 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
828 return placeholder_len;
829 case 'I': /* date, ISO 8601 strict */
830 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
831 return placeholder_len;
832 case 'h': /* date, human */
833 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(HUMAN)));
834 return placeholder_len;
835 case 's':
836 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT)));
837 return placeholder_len;
838 }
839
840 skip:
841 /*
842 * reading from either a bogus commit, or a reflog entry with
843 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
844 * to compute a valid return value.
845 */
846 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
847 || part == 'D' || part == 'r' || part == 'i')
848 return placeholder_len;
849
850 return 0; /* unknown placeholder */
851 }
852
853 struct chunk {
854 size_t off;
855 size_t len;
856 };
857
858 enum flush_type {
859 no_flush,
860 flush_right,
861 flush_left,
862 flush_left_and_steal,
863 flush_both
864 };
865
866 enum trunc_type {
867 trunc_none,
868 trunc_left,
869 trunc_middle,
870 trunc_right
871 };
872
873 struct format_commit_context {
874 struct repository *repository;
875 const struct commit *commit;
876 const struct pretty_print_context *pretty_ctx;
877 unsigned commit_header_parsed:1;
878 unsigned commit_message_parsed:1;
879 struct signature_check signature_check;
880 enum flush_type flush_type;
881 enum trunc_type truncate;
882 const char *message;
883 char *commit_encoding;
884 size_t width, indent1, indent2;
885 int auto_color;
886 int padding;
887
888 /* These offsets are relative to the start of the commit message. */
889 struct chunk author;
890 struct chunk committer;
891 size_t message_off;
892 size_t subject_off;
893 size_t body_off;
894
895 /* The following ones are relative to the result struct strbuf. */
896 size_t wrap_start;
897 };
898
899 static void parse_commit_header(struct format_commit_context *context)
900 {
901 const char *msg = context->message;
902 int i;
903
904 for (i = 0; msg[i]; i++) {
905 const char *name;
906 int eol;
907 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
908 ; /* do nothing */
909
910 if (i == eol) {
911 break;
912 } else if (skip_prefix(msg + i, "author ", &name)) {
913 context->author.off = name - msg;
914 context->author.len = msg + eol - name;
915 } else if (skip_prefix(msg + i, "committer ", &name)) {
916 context->committer.off = name - msg;
917 context->committer.len = msg + eol - name;
918 }
919 i = eol;
920 }
921 context->message_off = i;
922 context->commit_header_parsed = 1;
923 }
924
925 static int istitlechar(char c)
926 {
927 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
928 (c >= '0' && c <= '9') || c == '.' || c == '_';
929 }
930
931 void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
932 {
933 size_t trimlen;
934 size_t start_len = sb->len;
935 int space = 2;
936 int i;
937
938 for (i = 0; i < len; i++) {
939 if (istitlechar(msg[i])) {
940 if (space == 1)
941 strbuf_addch(sb, '-');
942 space = 0;
943 strbuf_addch(sb, msg[i]);
944 if (msg[i] == '.')
945 while (msg[i+1] == '.')
946 i++;
947 } else
948 space |= 1;
949 }
950
951 /* trim any trailing '.' or '-' characters */
952 trimlen = 0;
953 while (sb->len - trimlen > start_len &&
954 (sb->buf[sb->len - 1 - trimlen] == '.'
955 || sb->buf[sb->len - 1 - trimlen] == '-'))
956 trimlen++;
957 strbuf_remove(sb, sb->len - trimlen, trimlen);
958 }
959
960 const char *format_subject(struct strbuf *sb, const char *msg,
961 const char *line_separator)
962 {
963 int first = 1;
964
965 for (;;) {
966 const char *line = msg;
967 int linelen = get_one_line(line);
968
969 msg += linelen;
970 if (!linelen || is_blank_line(line, &linelen))
971 break;
972
973 if (!sb)
974 continue;
975 strbuf_grow(sb, linelen + 2);
976 if (!first)
977 strbuf_addstr(sb, line_separator);
978 strbuf_add(sb, line, linelen);
979 first = 0;
980 }
981 return msg;
982 }
983
984 static void parse_commit_message(struct format_commit_context *c)
985 {
986 const char *msg = c->message + c->message_off;
987 const char *start = c->message;
988
989 msg = skip_blank_lines(msg);
990 c->subject_off = msg - start;
991
992 msg = format_subject(NULL, msg, NULL);
993 msg = skip_blank_lines(msg);
994 c->body_off = msg - start;
995
996 c->commit_message_parsed = 1;
997 }
998
999 static void strbuf_wrap(struct strbuf *sb, size_t pos,
1000 size_t width, size_t indent1, size_t indent2)
1001 {
1002 struct strbuf tmp = STRBUF_INIT;
1003
1004 if (pos)
1005 strbuf_add(&tmp, sb->buf, pos);
1006 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
1007 cast_size_t_to_int(indent1),
1008 cast_size_t_to_int(indent2),
1009 cast_size_t_to_int(width));
1010 strbuf_swap(&tmp, sb);
1011 strbuf_release(&tmp);
1012 }
1013
1014 static void rewrap_message_tail(struct strbuf *sb,
1015 struct format_commit_context *c,
1016 size_t new_width, size_t new_indent1,
1017 size_t new_indent2)
1018 {
1019 if (c->width == new_width && c->indent1 == new_indent1 &&
1020 c->indent2 == new_indent2)
1021 return;
1022 if (c->wrap_start < sb->len)
1023 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
1024 c->wrap_start = sb->len;
1025 c->width = new_width;
1026 c->indent1 = new_indent1;
1027 c->indent2 = new_indent2;
1028 }
1029
1030 static int format_reflog_person(struct strbuf *sb,
1031 char part,
1032 struct reflog_walk_info *log,
1033 const struct date_mode *dmode)
1034 {
1035 const char *ident;
1036
1037 if (!log)
1038 return 2;
1039
1040 ident = get_reflog_ident(log);
1041 if (!ident)
1042 return 2;
1043
1044 return format_person_part(sb, part, ident, strlen(ident), dmode);
1045 }
1046
1047 static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
1048 const char *placeholder,
1049 struct format_commit_context *c)
1050 {
1051 const char *rest = placeholder;
1052 const char *basic_color = NULL;
1053
1054 if (placeholder[1] == '(') {
1055 const char *begin = placeholder + 2;
1056 const char *end = strchr(begin, ')');
1057 char color[COLOR_MAXLEN];
1058
1059 if (!end)
1060 return 0;
1061
1062 if (skip_prefix(begin, "auto,", &begin)) {
1063 if (!want_color(c->pretty_ctx->color))
1064 return end - placeholder + 1;
1065 } else if (skip_prefix(begin, "always,", &begin)) {
1066 /* nothing to do; we do not respect want_color at all */
1067 } else {
1068 /* the default is the same as "auto" */
1069 if (!want_color(c->pretty_ctx->color))
1070 return end - placeholder + 1;
1071 }
1072
1073 if (color_parse_mem(begin, end - begin, color) < 0)
1074 die(_("unable to parse --pretty format"));
1075 strbuf_addstr(sb, color);
1076 return end - placeholder + 1;
1077 }
1078
1079 /*
1080 * We handle things like "%C(red)" above; for historical reasons, there
1081 * are a few colors that can be specified without parentheses (and
1082 * they cannot support things like "auto" or "always" at all).
1083 */
1084 if (skip_prefix(placeholder + 1, "red", &rest))
1085 basic_color = GIT_COLOR_RED;
1086 else if (skip_prefix(placeholder + 1, "green", &rest))
1087 basic_color = GIT_COLOR_GREEN;
1088 else if (skip_prefix(placeholder + 1, "blue", &rest))
1089 basic_color = GIT_COLOR_BLUE;
1090 else if (skip_prefix(placeholder + 1, "reset", &rest))
1091 basic_color = GIT_COLOR_RESET;
1092
1093 if (basic_color && want_color(c->pretty_ctx->color))
1094 strbuf_addstr(sb, basic_color);
1095
1096 return rest - placeholder;
1097 }
1098
1099 static size_t parse_padding_placeholder(const char *placeholder,
1100 struct format_commit_context *c)
1101 {
1102 const char *ch = placeholder;
1103 enum flush_type flush_type;
1104 int to_column = 0;
1105
1106 switch (*ch++) {
1107 case '<':
1108 flush_type = flush_right;
1109 break;
1110 case '>':
1111 if (*ch == '<') {
1112 flush_type = flush_both;
1113 ch++;
1114 } else if (*ch == '>') {
1115 flush_type = flush_left_and_steal;
1116 ch++;
1117 } else
1118 flush_type = flush_left;
1119 break;
1120 default:
1121 return 0;
1122 }
1123
1124 /* the next value means "wide enough to that column" */
1125 if (*ch == '|') {
1126 to_column = 1;
1127 ch++;
1128 }
1129
1130 if (*ch == '(') {
1131 const char *start = ch + 1;
1132 const char *end = start + strcspn(start, ",)");
1133 char *next;
1134 int width;
1135 if (!*end || end == start)
1136 return 0;
1137 width = strtol(start, &next, 10);
1138
1139 /*
1140 * We need to limit the amount of padding, or otherwise this
1141 * would allow the user to pad the buffer by arbitrarily many
1142 * bytes and thus cause resource exhaustion.
1143 */
1144 if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT)
1145 return 0;
1146
1147 if (next == start || width == 0)
1148 return 0;
1149 if (width < 0) {
1150 if (to_column)
1151 width += term_columns();
1152 if (width < 0)
1153 return 0;
1154 }
1155 c->padding = to_column ? -width : width;
1156 c->flush_type = flush_type;
1157
1158 if (*end == ',') {
1159 start = end + 1;
1160 end = strchr(start, ')');
1161 if (!end || end == start)
1162 return 0;
1163 if (starts_with(start, "trunc)"))
1164 c->truncate = trunc_right;
1165 else if (starts_with(start, "ltrunc)"))
1166 c->truncate = trunc_left;
1167 else if (starts_with(start, "mtrunc)"))
1168 c->truncate = trunc_middle;
1169 else
1170 return 0;
1171 } else
1172 c->truncate = trunc_none;
1173
1174 return end - placeholder + 1;
1175 }
1176 return 0;
1177 }
1178
1179 static int match_placeholder_arg_value(const char *to_parse, const char *candidate,
1180 const char **end, const char **valuestart,
1181 size_t *valuelen)
1182 {
1183 const char *p;
1184
1185 if (!(skip_prefix(to_parse, candidate, &p)))
1186 return 0;
1187 if (valuestart) {
1188 if (*p == '=') {
1189 *valuestart = p + 1;
1190 *valuelen = strcspn(*valuestart, ",)");
1191 p = *valuestart + *valuelen;
1192 } else {
1193 if (*p != ',' && *p != ')')
1194 return 0;
1195 *valuestart = NULL;
1196 *valuelen = 0;
1197 }
1198 }
1199 if (*p == ',') {
1200 *end = p + 1;
1201 return 1;
1202 }
1203 if (*p == ')') {
1204 *end = p;
1205 return 1;
1206 }
1207 return 0;
1208 }
1209
1210 static int match_placeholder_bool_arg(const char *to_parse, const char *candidate,
1211 const char **end, int *val)
1212 {
1213 const char *argval;
1214 char *strval;
1215 size_t arglen;
1216 int v;
1217
1218 if (!match_placeholder_arg_value(to_parse, candidate, end, &argval, &arglen))
1219 return 0;
1220
1221 if (!argval) {
1222 *val = 1;
1223 return 1;
1224 }
1225
1226 strval = xstrndup(argval, arglen);
1227 v = git_parse_maybe_bool(strval);
1228 free(strval);
1229
1230 if (v == -1)
1231 return 0;
1232
1233 *val = v;
1234
1235 return 1;
1236 }
1237
1238 static int format_trailer_match_cb(const struct strbuf *key, void *ud)
1239 {
1240 const struct string_list *list = ud;
1241 const struct string_list_item *item;
1242
1243 for_each_string_list_item (item, list) {
1244 if (key->len == (uintptr_t)item->util &&
1245 !strncasecmp(item->string, key->buf, key->len))
1246 return 1;
1247 }
1248 return 0;
1249 }
1250
1251 int format_set_trailers_options(struct process_trailer_options *opts,
1252 struct string_list *filter_list,
1253 struct strbuf *sepbuf,
1254 struct strbuf *kvsepbuf,
1255 const char **arg,
1256 char **invalid_arg)
1257 {
1258 for (;;) {
1259 const char *argval;
1260 size_t arglen;
1261
1262 if (**arg == ')')
1263 break;
1264
1265 if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) {
1266 uintptr_t len = arglen;
1267
1268 if (!argval)
1269 return -1;
1270
1271 if (len && argval[len - 1] == ':')
1272 len--;
1273 string_list_append(filter_list, argval)->util = (char *)len;
1274
1275 opts->filter = format_trailer_match_cb;
1276 opts->filter_data = filter_list;
1277 opts->only_trailers = 1;
1278 } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
1279 char *fmt;
1280
1281 strbuf_reset(sepbuf);
1282 fmt = xstrndup(argval, arglen);
1283 strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL);
1284 free(fmt);
1285 opts->separator = sepbuf;
1286 } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
1287 char *fmt;
1288
1289 strbuf_reset(kvsepbuf);
1290 fmt = xstrndup(argval, arglen);
1291 strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL);
1292 free(fmt);
1293 opts->key_value_separator = kvsepbuf;
1294 } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
1295 !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
1296 !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
1297 !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) {
1298 if (invalid_arg) {
1299 size_t len = strcspn(*arg, ",)");
1300 *invalid_arg = xstrndup(*arg, len);
1301 }
1302 return -1;
1303 }
1304 }
1305 return 0;
1306 }
1307
1308 static size_t parse_describe_args(const char *start, struct strvec *args)
1309 {
1310 struct {
1311 char *name;
1312 enum {
1313 DESCRIBE_ARG_BOOL,
1314 DESCRIBE_ARG_INTEGER,
1315 DESCRIBE_ARG_STRING,
1316 } type;
1317 } option[] = {
1318 { "tags", DESCRIBE_ARG_BOOL},
1319 { "abbrev", DESCRIBE_ARG_INTEGER },
1320 { "exclude", DESCRIBE_ARG_STRING },
1321 { "match", DESCRIBE_ARG_STRING },
1322 };
1323 const char *arg = start;
1324
1325 for (;;) {
1326 int found = 0;
1327 const char *argval;
1328 size_t arglen = 0;
1329 int optval = 0;
1330 int i;
1331
1332 for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
1333 switch (option[i].type) {
1334 case DESCRIBE_ARG_BOOL:
1335 if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) {
1336 if (optval)
1337 strvec_pushf(args, "--%s", option[i].name);
1338 else
1339 strvec_pushf(args, "--no-%s", option[i].name);
1340 found = 1;
1341 }
1342 break;
1343 case DESCRIBE_ARG_INTEGER:
1344 if (match_placeholder_arg_value(arg, option[i].name, &arg,
1345 &argval, &arglen)) {
1346 char *endptr;
1347 if (!arglen)
1348 return 0;
1349 strtol(argval, &endptr, 10);
1350 if (endptr - argval != arglen)
1351 return 0;
1352 strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1353 found = 1;
1354 }
1355 break;
1356 case DESCRIBE_ARG_STRING:
1357 if (match_placeholder_arg_value(arg, option[i].name, &arg,
1358 &argval, &arglen)) {
1359 if (!arglen)
1360 return 0;
1361 strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1362 found = 1;
1363 }
1364 break;
1365 }
1366 }
1367 if (!found)
1368 break;
1369
1370 }
1371 return arg - start;
1372 }
1373
1374 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1375 const char *placeholder,
1376 void *context)
1377 {
1378 struct format_commit_context *c = context;
1379 const struct commit *commit = c->commit;
1380 const char *msg = c->message;
1381 struct commit_list *p;
1382 const char *arg, *eol;
1383 size_t res;
1384 char **slot;
1385
1386 /* these are independent of the commit */
1387 res = strbuf_expand_literal_cb(sb, placeholder, NULL);
1388 if (res)
1389 return res;
1390
1391 switch (placeholder[0]) {
1392 case 'C':
1393 if (starts_with(placeholder + 1, "(auto)")) {
1394 c->auto_color = want_color(c->pretty_ctx->color);
1395 if (c->auto_color && sb->len)
1396 strbuf_addstr(sb, GIT_COLOR_RESET);
1397 return 7; /* consumed 7 bytes, "C(auto)" */
1398 } else {
1399 int ret = parse_color(sb, placeholder, c);
1400 if (ret)
1401 c->auto_color = 0;
1402 /*
1403 * Otherwise, we decided to treat %C<unknown>
1404 * as a literal string, and the previous
1405 * %C(auto) is still valid.
1406 */
1407 return ret;
1408 }
1409 case 'w':
1410 if (placeholder[1] == '(') {
1411 unsigned long width = 0, indent1 = 0, indent2 = 0;
1412 char *next;
1413 const char *start = placeholder + 2;
1414 const char *end = strchr(start, ')');
1415 if (!end)
1416 return 0;
1417 if (end > start) {
1418 width = strtoul(start, &next, 10);
1419 if (*next == ',') {
1420 indent1 = strtoul(next + 1, &next, 10);
1421 if (*next == ',') {
1422 indent2 = strtoul(next + 1,
1423 &next, 10);
1424 }
1425 }
1426 if (*next != ')')
1427 return 0;
1428 }
1429
1430 /*
1431 * We need to limit the format here as it allows the
1432 * user to prepend arbitrarily many bytes to the buffer
1433 * when rewrapping.
1434 */
1435 if (width > FORMATTING_LIMIT ||
1436 indent1 > FORMATTING_LIMIT ||
1437 indent2 > FORMATTING_LIMIT)
1438 return 0;
1439 rewrap_message_tail(sb, c, width, indent1, indent2);
1440 return end - placeholder + 1;
1441 } else
1442 return 0;
1443
1444 case '<':
1445 case '>':
1446 return parse_padding_placeholder(placeholder, c);
1447 }
1448
1449 if (skip_prefix(placeholder, "(describe", &arg)) {
1450 struct child_process cmd = CHILD_PROCESS_INIT;
1451 struct strbuf out = STRBUF_INIT;
1452 struct strbuf err = STRBUF_INIT;
1453 struct pretty_print_describe_status *describe_status;
1454
1455 describe_status = c->pretty_ctx->describe_status;
1456 if (describe_status) {
1457 if (!describe_status->max_invocations)
1458 return 0;
1459 describe_status->max_invocations--;
1460 }
1461
1462 cmd.git_cmd = 1;
1463 strvec_push(&cmd.args, "describe");
1464
1465 if (*arg == ':') {
1466 arg++;
1467 arg += parse_describe_args(arg, &cmd.args);
1468 }
1469
1470 if (*arg != ')') {
1471 child_process_clear(&cmd);
1472 return 0;
1473 }
1474
1475 strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1476 pipe_command(&cmd, NULL, 0, &out, 0, &err, 0);
1477 strbuf_rtrim(&out);
1478 strbuf_addbuf(sb, &out);
1479 strbuf_release(&out);
1480 strbuf_release(&err);
1481 return arg - placeholder + 1;
1482 }
1483
1484 /* these depend on the commit */
1485 if (!commit->object.parsed)
1486 parse_object(the_repository, &commit->object.oid);
1487
1488 switch (placeholder[0]) {
1489 case 'H': /* commit hash */
1490 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1491 strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
1492 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1493 return 1;
1494 case 'h': /* abbreviated commit hash */
1495 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1496 strbuf_add_unique_abbrev(sb, &commit->object.oid,
1497 c->pretty_ctx->abbrev);
1498 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
1499 return 1;
1500 case 'T': /* tree hash */
1501 strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit)));
1502 return 1;
1503 case 't': /* abbreviated tree hash */
1504 strbuf_add_unique_abbrev(sb,
1505 get_commit_tree_oid(commit),
1506 c->pretty_ctx->abbrev);
1507 return 1;
1508 case 'P': /* parent hashes */
1509 for (p = commit->parents; p; p = p->next) {
1510 if (p != commit->parents)
1511 strbuf_addch(sb, ' ');
1512 strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
1513 }
1514 return 1;
1515 case 'p': /* abbreviated parent hashes */
1516 for (p = commit->parents; p; p = p->next) {
1517 if (p != commit->parents)
1518 strbuf_addch(sb, ' ');
1519 strbuf_add_unique_abbrev(sb, &p->item->object.oid,
1520 c->pretty_ctx->abbrev);
1521 }
1522 return 1;
1523 case 'm': /* left/right/bottom */
1524 strbuf_addstr(sb, get_revision_mark(NULL, commit));
1525 return 1;
1526 case 'd':
1527 format_decorations(sb, commit, c->auto_color);
1528 return 1;
1529 case 'D':
1530 format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1531 return 1;
1532 case 'S': /* tag/branch like --source */
1533 if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
1534 return 0;
1535 slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);
1536 if (!(slot && *slot))
1537 return 0;
1538 strbuf_addstr(sb, *slot);
1539 return 1;
1540 case 'g': /* reflog info */
1541 switch(placeholder[1]) {
1542 case 'd': /* reflog selector */
1543 case 'D':
1544 if (c->pretty_ctx->reflog_info)
1545 get_reflog_selector(sb,
1546 c->pretty_ctx->reflog_info,
1547 &c->pretty_ctx->date_mode,
1548 c->pretty_ctx->date_mode_explicit,
1549 (placeholder[1] == 'd'));
1550 return 2;
1551 case 's': /* reflog message */
1552 if (c->pretty_ctx->reflog_info)
1553 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1554 return 2;
1555 case 'n':
1556 case 'N':
1557 case 'e':
1558 case 'E':
1559 return format_reflog_person(sb,
1560 placeholder[1],
1561 c->pretty_ctx->reflog_info,
1562 &c->pretty_ctx->date_mode);
1563 }
1564 return 0; /* unknown %g placeholder */
1565 case 'N':
1566 if (c->pretty_ctx->notes_message) {
1567 strbuf_addstr(sb, c->pretty_ctx->notes_message);
1568 return 1;
1569 }
1570 return 0;
1571 }
1572
1573 if (placeholder[0] == 'G') {
1574 if (!c->signature_check.result)
1575 check_commit_signature(c->commit, &(c->signature_check));
1576 switch (placeholder[1]) {
1577 case 'G':
1578 if (c->signature_check.output)
1579 strbuf_addstr(sb, c->signature_check.output);
1580 break;
1581 case '?':
1582 switch (c->signature_check.result) {
1583 case 'G':
1584 switch (c->signature_check.trust_level) {
1585 case TRUST_UNDEFINED:
1586 case TRUST_NEVER:
1587 strbuf_addch(sb, 'U');
1588 break;
1589 default:
1590 strbuf_addch(sb, 'G');
1591 break;
1592 }
1593 break;
1594 case 'B':
1595 case 'E':
1596 case 'N':
1597 case 'X':
1598 case 'Y':
1599 case 'R':
1600 strbuf_addch(sb, c->signature_check.result);
1601 }
1602 break;
1603 case 'S':
1604 if (c->signature_check.signer)
1605 strbuf_addstr(sb, c->signature_check.signer);
1606 break;
1607 case 'K':
1608 if (c->signature_check.key)
1609 strbuf_addstr(sb, c->signature_check.key);
1610 break;
1611 case 'F':
1612 if (c->signature_check.fingerprint)
1613 strbuf_addstr(sb, c->signature_check.fingerprint);
1614 break;
1615 case 'P':
1616 if (c->signature_check.primary_key_fingerprint)
1617 strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
1618 break;
1619 case 'T':
1620 strbuf_addstr(sb, gpg_trust_level_to_str(c->signature_check.trust_level));
1621 break;
1622 default:
1623 return 0;
1624 }
1625 return 2;
1626 }
1627
1628 /* For the rest we have to parse the commit header. */
1629 if (!c->commit_header_parsed) {
1630 msg = c->message =
1631 repo_logmsg_reencode(c->repository, commit,
1632 &c->commit_encoding, "UTF-8");
1633 parse_commit_header(c);
1634 }
1635
1636 switch (placeholder[0]) {
1637 case 'a': /* author ... */
1638 return format_person_part(sb, placeholder[1],
1639 msg + c->author.off, c->author.len,
1640 &c->pretty_ctx->date_mode);
1641 case 'c': /* committer ... */
1642 return format_person_part(sb, placeholder[1],
1643 msg + c->committer.off, c->committer.len,
1644 &c->pretty_ctx->date_mode);
1645 case 'e': /* encoding */
1646 if (c->commit_encoding)
1647 strbuf_addstr(sb, c->commit_encoding);
1648 return 1;
1649 case 'B': /* raw body */
1650 /* message_off is always left at the initial newline */
1651 strbuf_addstr(sb, msg + c->message_off + 1);
1652 return 1;
1653 }
1654
1655 /* Now we need to parse the commit message. */
1656 if (!c->commit_message_parsed)
1657 parse_commit_message(c);
1658
1659 switch (placeholder[0]) {
1660 case 's': /* subject */
1661 format_subject(sb, msg + c->subject_off, " ");
1662 return 1;
1663 case 'f': /* sanitized subject */
1664 eol = strchrnul(msg + c->subject_off, '\n');
1665 format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
1666 return 1;
1667 case 'b': /* body */
1668 strbuf_addstr(sb, msg + c->body_off);
1669 return 1;
1670 }
1671
1672 if (skip_prefix(placeholder, "(trailers", &arg)) {
1673 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1674 struct string_list filter_list = STRING_LIST_INIT_NODUP;
1675 struct strbuf sepbuf = STRBUF_INIT;
1676 struct strbuf kvsepbuf = STRBUF_INIT;
1677 size_t ret = 0;
1678
1679 opts.no_divider = 1;
1680
1681 if (*arg == ':') {
1682 arg++;
1683 if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL))
1684 goto trailer_out;
1685 }
1686 if (*arg == ')') {
1687 format_trailers_from_commit(sb, msg + c->subject_off, &opts);
1688 ret = arg - placeholder + 1;
1689 }
1690 trailer_out:
1691 string_list_clear(&filter_list, 0);
1692 strbuf_release(&sepbuf);
1693 return ret;
1694 }
1695
1696 return 0; /* unknown placeholder */
1697 }
1698
1699 static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1700 const char *placeholder,
1701 struct format_commit_context *c)
1702 {
1703 struct strbuf local_sb = STRBUF_INIT;
1704 size_t total_consumed = 0;
1705 int len, padding = c->padding;
1706
1707 if (padding < 0) {
1708 const char *start = strrchr(sb->buf, '\n');
1709 int occupied;
1710 if (!start)
1711 start = sb->buf;
1712 occupied = utf8_strnwidth(start, strlen(start), 1);
1713 occupied += c->pretty_ctx->graph_width;
1714 padding = (-padding) - occupied;
1715 }
1716 while (1) {
1717 int modifier = *placeholder == 'C';
1718 size_t consumed = format_commit_one(&local_sb, placeholder, c);
1719 total_consumed += consumed;
1720
1721 if (!modifier)
1722 break;
1723
1724 placeholder += consumed;
1725 if (*placeholder != '%')
1726 break;
1727 placeholder++;
1728 total_consumed++;
1729 }
1730 len = utf8_strnwidth(local_sb.buf, local_sb.len, 1);
1731
1732 if (c->flush_type == flush_left_and_steal) {
1733 const char *ch = sb->buf + sb->len - 1;
1734 while (len > padding && ch > sb->buf) {
1735 const char *p;
1736 if (*ch == ' ') {
1737 ch--;
1738 padding++;
1739 continue;
1740 }
1741 /* check for trailing ansi sequences */
1742 if (*ch != 'm')
1743 break;
1744 p = ch - 1;
1745 while (p > sb->buf && ch - p < 10 && *p != '\033')
1746 p--;
1747 if (*p != '\033' ||
1748 ch + 1 - p != display_mode_esc_sequence_len(p))
1749 break;
1750 /*
1751 * got a good ansi sequence, put it back to
1752 * local_sb as we're cutting sb
1753 */
1754 strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1755 ch = p - 1;
1756 }
1757 strbuf_setlen(sb, ch + 1 - sb->buf);
1758 c->flush_type = flush_left;
1759 }
1760
1761 if (len > padding) {
1762 switch (c->truncate) {
1763 case trunc_left:
1764 strbuf_utf8_replace(&local_sb,
1765 0, len - (padding - 2),
1766 "..");
1767 break;
1768 case trunc_middle:
1769 strbuf_utf8_replace(&local_sb,
1770 padding / 2 - 1,
1771 len - (padding - 2),
1772 "..");
1773 break;
1774 case trunc_right:
1775 strbuf_utf8_replace(&local_sb,
1776 padding - 2, len - (padding - 2),
1777 "..");
1778 break;
1779 case trunc_none:
1780 break;
1781 }
1782 strbuf_addbuf(sb, &local_sb);
1783 } else {
1784 size_t sb_len = sb->len, offset = 0;
1785 if (c->flush_type == flush_left)
1786 offset = padding - len;
1787 else if (c->flush_type == flush_both)
1788 offset = (padding - len) / 2;
1789 /*
1790 * we calculate padding in columns, now
1791 * convert it back to chars
1792 */
1793 padding = padding - len + local_sb.len;
1794 strbuf_addchars(sb, ' ', padding);
1795 memcpy(sb->buf + sb_len + offset, local_sb.buf,
1796 local_sb.len);
1797 }
1798 strbuf_release(&local_sb);
1799 c->flush_type = no_flush;
1800 return total_consumed;
1801 }
1802
1803 static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1804 const char *placeholder,
1805 void *context)
1806 {
1807 size_t consumed, orig_len;
1808 enum {
1809 NO_MAGIC,
1810 ADD_LF_BEFORE_NON_EMPTY,
1811 DEL_LF_BEFORE_EMPTY,
1812 ADD_SP_BEFORE_NON_EMPTY
1813 } magic = NO_MAGIC;
1814
1815 switch (placeholder[0]) {
1816 case '-':
1817 magic = DEL_LF_BEFORE_EMPTY;
1818 break;
1819 case '+':
1820 magic = ADD_LF_BEFORE_NON_EMPTY;
1821 break;
1822 case ' ':
1823 magic = ADD_SP_BEFORE_NON_EMPTY;
1824 break;
1825 default:
1826 break;
1827 }
1828 if (magic != NO_MAGIC) {
1829 placeholder++;
1830
1831 switch (placeholder[0]) {
1832 case 'w':
1833 /*
1834 * `%+w()` cannot ever expand to a non-empty string,
1835 * and it potentially changes the layout of preceding
1836 * contents. We're thus not able to handle the magic in
1837 * this combination and refuse the pattern.
1838 */
1839 return 0;
1840 };
1841 }
1842
1843 orig_len = sb->len;
1844 if (((struct format_commit_context *)context)->flush_type != no_flush)
1845 consumed = format_and_pad_commit(sb, placeholder, context);
1846 else
1847 consumed = format_commit_one(sb, placeholder, context);
1848 if (magic == NO_MAGIC)
1849 return consumed;
1850
1851 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1852 while (sb->len && sb->buf[sb->len - 1] == '\n')
1853 strbuf_setlen(sb, sb->len - 1);
1854 } else if (orig_len != sb->len) {
1855 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1856 strbuf_insertstr(sb, orig_len, "\n");
1857 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1858 strbuf_insertstr(sb, orig_len, " ");
1859 }
1860 return consumed + 1;
1861 }
1862
1863 static size_t userformat_want_item(struct strbuf *sb UNUSED,
1864 const char *placeholder,
1865 void *context)
1866 {
1867 struct userformat_want *w = context;
1868
1869 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
1870 placeholder++;
1871
1872 switch (*placeholder) {
1873 case 'N':
1874 w->notes = 1;
1875 break;
1876 case 'S':
1877 w->source = 1;
1878 break;
1879 case 'd':
1880 case 'D':
1881 w->decorate = 1;
1882 break;
1883 }
1884 return 0;
1885 }
1886
1887 void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1888 {
1889 struct strbuf dummy = STRBUF_INIT;
1890
1891 if (!fmt) {
1892 if (!user_format)
1893 return;
1894 fmt = user_format;
1895 }
1896 strbuf_expand(&dummy, fmt, userformat_want_item, w);
1897 strbuf_release(&dummy);
1898 }
1899
1900 void repo_format_commit_message(struct repository *r,
1901 const struct commit *commit,
1902 const char *format, struct strbuf *sb,
1903 const struct pretty_print_context *pretty_ctx)
1904 {
1905 struct format_commit_context context = {
1906 .repository = r,
1907 .commit = commit,
1908 .pretty_ctx = pretty_ctx,
1909 .wrap_start = sb->len
1910 };
1911 const char *output_enc = pretty_ctx->output_encoding;
1912 const char *utf8 = "UTF-8";
1913
1914 strbuf_expand(sb, format, format_commit_item, &context);
1915 rewrap_message_tail(sb, &context, 0, 0, 0);
1916
1917 /*
1918 * Convert output to an actual output encoding; note that
1919 * format_commit_item() will always use UTF-8, so we don't
1920 * have to bother if that's what the output wants.
1921 */
1922 if (output_enc) {
1923 if (same_encoding(utf8, output_enc))
1924 output_enc = NULL;
1925 } else {
1926 if (context.commit_encoding &&
1927 !same_encoding(context.commit_encoding, utf8))
1928 output_enc = context.commit_encoding;
1929 }
1930
1931 if (output_enc) {
1932 size_t outsz;
1933 char *out = reencode_string_len(sb->buf, sb->len,
1934 output_enc, utf8, &outsz);
1935 if (out)
1936 strbuf_attach(sb, out, outsz, outsz + 1);
1937 }
1938
1939 free(context.commit_encoding);
1940 repo_unuse_commit_buffer(r, commit, context.message);
1941 }
1942
1943 static void pp_header(struct pretty_print_context *pp,
1944 const char *encoding,
1945 const struct commit *commit,
1946 const char **msg_p,
1947 struct strbuf *sb)
1948 {
1949 int parents_shown = 0;
1950
1951 for (;;) {
1952 const char *name, *line = *msg_p;
1953 int linelen = get_one_line(*msg_p);
1954
1955 if (!linelen)
1956 return;
1957 *msg_p += linelen;
1958
1959 if (linelen == 1)
1960 /* End of header */
1961 return;
1962
1963 if (pp->fmt == CMIT_FMT_RAW) {
1964 strbuf_add(sb, line, linelen);
1965 continue;
1966 }
1967
1968 if (starts_with(line, "parent ")) {
1969 if (linelen != the_hash_algo->hexsz + 8)
1970 die("bad parent line in commit");
1971 continue;
1972 }
1973
1974 if (!parents_shown) {
1975 unsigned num = commit_list_count(commit->parents);
1976 /* with enough slop */
1977 strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20);
1978 add_merge_info(pp, sb, commit);
1979 parents_shown = 1;
1980 }
1981
1982 /*
1983 * MEDIUM == DEFAULT shows only author with dates.
1984 * FULL shows both authors but not dates.
1985 * FULLER shows both authors and dates.
1986 */
1987 if (skip_prefix(line, "author ", &name)) {
1988 strbuf_grow(sb, linelen + 80);
1989 pp_user_info(pp, "Author", sb, name, encoding);
1990 }
1991 if (skip_prefix(line, "committer ", &name) &&
1992 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
1993 strbuf_grow(sb, linelen + 80);
1994 pp_user_info(pp, "Commit", sb, name, encoding);
1995 }
1996 }
1997 }
1998
1999 void pp_title_line(struct pretty_print_context *pp,
2000 const char **msg_p,
2001 struct strbuf *sb,
2002 const char *encoding,
2003 int need_8bit_cte)
2004 {
2005 static const int max_length = 78; /* per rfc2047 */
2006 struct strbuf title;
2007
2008 strbuf_init(&title, 80);
2009 *msg_p = format_subject(&title, *msg_p,
2010 pp->preserve_subject ? "\n" : " ");
2011
2012 strbuf_grow(sb, title.len + 1024);
2013 if (pp->print_email_subject) {
2014 if (pp->rev)
2015 fmt_output_email_subject(sb, pp->rev);
2016 if (pp->encode_email_headers &&
2017 needs_rfc2047_encoding(title.buf, title.len))
2018 add_rfc2047(sb, title.buf, title.len,
2019 encoding, RFC2047_SUBJECT);
2020 else
2021 strbuf_add_wrapped_bytes(sb, title.buf, title.len,
2022 -last_line_length(sb), 1, max_length);
2023 } else {
2024 strbuf_addbuf(sb, &title);
2025 }
2026 strbuf_addch(sb, '\n');
2027
2028 if (need_8bit_cte == 0) {
2029 int i;
2030 for (i = 0; i < pp->in_body_headers.nr; i++) {
2031 if (has_non_ascii(pp->in_body_headers.items[i].string)) {
2032 need_8bit_cte = 1;
2033 break;
2034 }
2035 }
2036 }
2037
2038 if (need_8bit_cte > 0) {
2039 const char *header_fmt =
2040 "MIME-Version: 1.0\n"
2041 "Content-Type: text/plain; charset=%s\n"
2042 "Content-Transfer-Encoding: 8bit\n";
2043 strbuf_addf(sb, header_fmt, encoding);
2044 }
2045 if (pp->after_subject) {
2046 strbuf_addstr(sb, pp->after_subject);
2047 }
2048 if (cmit_fmt_is_mail(pp->fmt)) {
2049 strbuf_addch(sb, '\n');
2050 }
2051
2052 if (pp->in_body_headers.nr) {
2053 int i;
2054 for (i = 0; i < pp->in_body_headers.nr; i++) {
2055 strbuf_addstr(sb, pp->in_body_headers.items[i].string);
2056 free(pp->in_body_headers.items[i].string);
2057 }
2058 string_list_clear(&pp->in_body_headers, 0);
2059 strbuf_addch(sb, '\n');
2060 }
2061
2062 strbuf_release(&title);
2063 }
2064
2065 static int pp_utf8_width(const char *start, const char *end)
2066 {
2067 int width = 0;
2068 size_t remain = end - start;
2069
2070 while (remain) {
2071 int n = utf8_width(&start, &remain);
2072 if (n < 0 || !start)
2073 return -1;
2074 width += n;
2075 }
2076 return width;
2077 }
2078
2079 static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt,
2080 int color, int tabwidth, const char *line,
2081 int linelen)
2082 {
2083 const char *tab;
2084
2085 while ((tab = memchr(line, '\t', linelen)) != NULL) {
2086 int width = pp_utf8_width(line, tab);
2087
2088 /*
2089 * If it wasn't well-formed utf8, or it
2090 * had characters with badly defined
2091 * width (control characters etc), just
2092 * give up on trying to align things.
2093 */
2094 if (width < 0)
2095 break;
2096
2097 /* Output the data .. */
2098 append_line_with_color(sb, opt, line, tab - line, color,
2099 GREP_CONTEXT_BODY,
2100 GREP_HEADER_FIELD_MAX);
2101
2102 /* .. and the de-tabified tab */
2103 strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
2104
2105 /* Skip over the printed part .. */
2106 linelen -= tab + 1 - line;
2107 line = tab + 1;
2108 }
2109
2110 /*
2111 * Print out everything after the last tab without
2112 * worrying about width - there's nothing more to
2113 * align.
2114 */
2115 append_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY,
2116 GREP_HEADER_FIELD_MAX);
2117 }
2118
2119 /*
2120 * pp_handle_indent() prints out the intendation, and
2121 * the whole line (without the final newline), after
2122 * de-tabifying.
2123 */
2124 static void pp_handle_indent(struct pretty_print_context *pp,
2125 struct strbuf *sb, int indent,
2126 const char *line, int linelen)
2127 {
2128 struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
2129
2130 strbuf_addchars(sb, ' ', indent);
2131 if (pp->expand_tabs_in_log)
2132 strbuf_add_tabexpand(sb, opt, pp->color, pp->expand_tabs_in_log,
2133 line, linelen);
2134 else
2135 append_line_with_color(sb, opt, line, linelen, pp->color,
2136 GREP_CONTEXT_BODY,
2137 GREP_HEADER_FIELD_MAX);
2138 }
2139
2140 static int is_mboxrd_from(const char *line, int len)
2141 {
2142 /*
2143 * a line matching /^From $/ here would only have len == 4
2144 * at this point because is_empty_line would've trimmed all
2145 * trailing space
2146 */
2147 return len > 4 && starts_with(line + strspn(line, ">"), "From ");
2148 }
2149
2150 void pp_remainder(struct pretty_print_context *pp,
2151 const char **msg_p,
2152 struct strbuf *sb,
2153 int indent)
2154 {
2155 struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
2156 int first = 1;
2157
2158 for (;;) {
2159 const char *line = *msg_p;
2160 int linelen = get_one_line(line);
2161 *msg_p += linelen;
2162
2163 if (!linelen)
2164 break;
2165
2166 if (is_blank_line(line, &linelen)) {
2167 if (first)
2168 continue;
2169 if (pp->fmt == CMIT_FMT_SHORT)
2170 break;
2171 }
2172 first = 0;
2173
2174 strbuf_grow(sb, linelen + indent + 20);
2175 if (indent)
2176 pp_handle_indent(pp, sb, indent, line, linelen);
2177 else if (pp->expand_tabs_in_log)
2178 strbuf_add_tabexpand(sb, opt, pp->color,
2179 pp->expand_tabs_in_log, line,
2180 linelen);
2181 else {
2182 if (pp->fmt == CMIT_FMT_MBOXRD &&
2183 is_mboxrd_from(line, linelen))
2184 strbuf_addch(sb, '>');
2185
2186 append_line_with_color(sb, opt, line, linelen,
2187 pp->color, GREP_CONTEXT_BODY,
2188 GREP_HEADER_FIELD_MAX);
2189 }
2190 strbuf_addch(sb, '\n');
2191 }
2192 }
2193
2194 void pretty_print_commit(struct pretty_print_context *pp,
2195 const struct commit *commit,
2196 struct strbuf *sb)
2197 {
2198 unsigned long beginning_of_body;
2199 int indent = 4;
2200 const char *msg;
2201 const char *reencoded;
2202 const char *encoding;
2203 int need_8bit_cte = pp->need_8bit_cte;
2204
2205 if (pp->fmt == CMIT_FMT_USERFORMAT) {
2206 format_commit_message(commit, user_format, sb, pp);
2207 return;
2208 }
2209
2210 encoding = get_log_output_encoding();
2211 msg = reencoded = logmsg_reencode(commit, NULL, encoding);
2212
2213 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
2214 indent = 0;
2215
2216 /*
2217 * We need to check and emit Content-type: to mark it
2218 * as 8-bit if we haven't done so.
2219 */
2220 if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
2221 int i, ch, in_body;
2222
2223 for (in_body = i = 0; (ch = msg[i]); i++) {
2224 if (!in_body) {
2225 /* author could be non 7-bit ASCII but
2226 * the log may be so; skip over the
2227 * header part first.
2228 */
2229 if (ch == '\n' && msg[i+1] == '\n')
2230 in_body = 1;
2231 }
2232 else if (non_ascii(ch)) {
2233 need_8bit_cte = 1;
2234 break;
2235 }
2236 }
2237 }
2238
2239 pp_header(pp, encoding, commit, &msg, sb);
2240 if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
2241 strbuf_addch(sb, '\n');
2242 }
2243
2244 /* Skip excess blank lines at the beginning of body, if any... */
2245 msg = skip_blank_lines(msg);
2246
2247 /* These formats treat the title line specially. */
2248 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
2249 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
2250
2251 beginning_of_body = sb->len;
2252 if (pp->fmt != CMIT_FMT_ONELINE)
2253 pp_remainder(pp, &msg, sb, indent);
2254 strbuf_rtrim(sb);
2255
2256 /* Make sure there is an EOLN for the non-oneline case */
2257 if (pp->fmt != CMIT_FMT_ONELINE)
2258 strbuf_addch(sb, '\n');
2259
2260 /*
2261 * The caller may append additional body text in e-mail
2262 * format. Make sure we did not strip the blank line
2263 * between the header and the body.
2264 */
2265 if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
2266 strbuf_addch(sb, '\n');
2267
2268 unuse_commit_buffer(commit, reencoded);
2269 }
2270
2271 void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
2272 struct strbuf *sb)
2273 {
2274 struct pretty_print_context pp = {0};
2275 pp.fmt = fmt;
2276 pretty_print_commit(&pp, commit, sb);
2277 }