7 #include "string-list.h"
12 #include "reflog-walk.h"
13 #include "gpg-interface.h"
17 * The limit for formatting directives, which enable the caller to append
18 * arbitrarily many bytes to the formatted buffer. This includes padding
19 * and wrapping formatters.
21 #define FORMATTING_LIMIT (16 * 1024)
23 static char *user_format
;
24 static struct cmt_fmt_map
{
28 int expand_tabs_in_log
;
30 enum date_mode_type default_date_mode_type
;
31 const char *user_format
;
33 static size_t builtin_formats_len
;
34 static size_t commit_formats_len
;
35 static size_t commit_formats_alloc
;
36 static struct cmt_fmt_map
*find_commit_format(const char *sought
);
38 int commit_format_is_empty(enum cmit_fmt fmt
)
40 return fmt
== CMIT_FMT_USERFORMAT
&& !*user_format
;
43 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
46 user_format
= xstrdup(cp
);
48 rev
->use_terminator
= 1;
49 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
52 static int git_pretty_formats_config(const char *var
, const char *value
, void *cb
)
54 struct cmt_fmt_map
*commit_format
= NULL
;
59 if (!skip_prefix(var
, "pretty.", &name
))
62 for (i
= 0; i
< builtin_formats_len
; i
++) {
63 if (!strcmp(commit_formats
[i
].name
, name
))
67 for (i
= builtin_formats_len
; i
< commit_formats_len
; i
++) {
68 if (!strcmp(commit_formats
[i
].name
, name
)) {
69 commit_format
= &commit_formats
[i
];
75 ALLOC_GROW(commit_formats
, commit_formats_len
+1,
76 commit_formats_alloc
);
77 commit_format
= &commit_formats
[commit_formats_len
];
78 memset(commit_format
, 0, sizeof(*commit_format
));
82 commit_format
->name
= xstrdup(name
);
83 commit_format
->format
= CMIT_FMT_USERFORMAT
;
84 if (git_config_string(&fmt
, var
, value
))
87 if (skip_prefix(fmt
, "format:", &fmt
))
88 commit_format
->is_tformat
= 0;
89 else if (skip_prefix(fmt
, "tformat:", &fmt
) || strchr(fmt
, '%'))
90 commit_format
->is_tformat
= 1;
92 commit_format
->is_alias
= 1;
93 commit_format
->user_format
= fmt
;
98 static void setup_commit_formats(void)
100 struct cmt_fmt_map builtin_formats
[] = {
101 { "raw", CMIT_FMT_RAW
, 0, 0 },
102 { "medium", CMIT_FMT_MEDIUM
, 0, 8 },
103 { "short", CMIT_FMT_SHORT
, 0, 0 },
104 { "email", CMIT_FMT_EMAIL
, 0, 0 },
105 { "mboxrd", CMIT_FMT_MBOXRD
, 0, 0 },
106 { "fuller", CMIT_FMT_FULLER
, 0, 8 },
107 { "full", CMIT_FMT_FULL
, 0, 8 },
108 { "oneline", CMIT_FMT_ONELINE
, 1, 0 },
109 { "reference", CMIT_FMT_USERFORMAT
, 1, 0,
110 0, DATE_SHORT
, "%C(auto)%h (%s, %ad)" },
112 * Please update $__git_log_pretty_formats in
113 * git-completion.bash when you add new formats.
116 commit_formats_len
= ARRAY_SIZE(builtin_formats
);
117 builtin_formats_len
= commit_formats_len
;
118 ALLOC_GROW(commit_formats
, commit_formats_len
, commit_formats_alloc
);
119 COPY_ARRAY(commit_formats
, builtin_formats
,
120 ARRAY_SIZE(builtin_formats
));
122 git_config(git_pretty_formats_config
, NULL
);
125 static struct cmt_fmt_map
*find_commit_format_recursive(const char *sought
,
126 const char *original
,
127 int num_redirections
)
129 struct cmt_fmt_map
*found
= NULL
;
130 size_t found_match_len
= 0;
133 if (num_redirections
>= commit_formats_len
)
134 die("invalid --pretty format: "
135 "'%s' references an alias which points to itself",
138 for (i
= 0; i
< commit_formats_len
; i
++) {
141 if (!starts_with(commit_formats
[i
].name
, sought
))
144 match_len
= strlen(commit_formats
[i
].name
);
145 if (found
== NULL
|| found_match_len
> match_len
) {
146 found
= &commit_formats
[i
];
147 found_match_len
= match_len
;
151 if (found
&& found
->is_alias
) {
152 found
= find_commit_format_recursive(found
->user_format
,
160 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
163 setup_commit_formats();
165 return find_commit_format_recursive(sought
, sought
, 0);
168 void get_commit_format(const char *arg
, struct rev_info
*rev
)
170 struct cmt_fmt_map
*commit_format
;
172 rev
->use_terminator
= 0;
174 rev
->commit_format
= CMIT_FMT_DEFAULT
;
177 if (skip_prefix(arg
, "format:", &arg
)) {
178 save_user_format(rev
, arg
, 0);
182 if (!*arg
|| skip_prefix(arg
, "tformat:", &arg
) || strchr(arg
, '%')) {
183 save_user_format(rev
, arg
, 1);
187 commit_format
= find_commit_format(arg
);
189 die("invalid --pretty format: %s", arg
);
191 rev
->commit_format
= commit_format
->format
;
192 rev
->use_terminator
= commit_format
->is_tformat
;
193 rev
->expand_tabs_in_log_default
= commit_format
->expand_tabs_in_log
;
194 if (!rev
->date_mode_explicit
&& commit_format
->default_date_mode_type
)
195 rev
->date_mode
.type
= commit_format
->default_date_mode_type
;
196 if (commit_format
->format
== CMIT_FMT_USERFORMAT
) {
197 save_user_format(rev
, commit_format
->user_format
,
198 commit_format
->is_tformat
);
203 * Generic support for pretty-printing the header
205 static int get_one_line(const char *msg
)
220 /* High bit set, or ISO-2022-INT */
221 static int non_ascii(int ch
)
223 return !isascii(ch
) || ch
== '\033';
226 int has_non_ascii(const char *s
)
231 while ((ch
= *s
++) != '\0') {
238 static int is_rfc822_special(char ch
)
260 static int needs_rfc822_quoting(const char *s
, int len
)
263 for (i
= 0; i
< len
; i
++)
264 if (is_rfc822_special(s
[i
]))
269 static int last_line_length(struct strbuf
*sb
)
273 /* How many bytes are already used on the last line? */
274 for (i
= sb
->len
- 1; i
>= 0; i
--)
275 if (sb
->buf
[i
] == '\n')
277 return sb
->len
- (i
+ 1);
280 static void add_rfc822_quoted(struct strbuf
*out
, const char *s
, int len
)
284 /* just a guess, we may have to also backslash-quote */
285 strbuf_grow(out
, len
+ 2);
287 strbuf_addch(out
, '"');
288 for (i
= 0; i
< len
; i
++) {
292 strbuf_addch(out
, '\\');
295 strbuf_addch(out
, s
[i
]);
298 strbuf_addch(out
, '"');
306 static int is_rfc2047_special(char ch
, enum rfc2047_type type
)
309 * rfc2047, section 4.2:
311 * 8-bit values which correspond to printable ASCII characters other
312 * than "=", "?", and "_" (underscore), MAY be represented as those
313 * characters. (But see section 5 for restrictions.) In
314 * particular, SPACE and TAB MUST NOT be represented as themselves
315 * within encoded words.
319 * rule out non-ASCII characters and non-printable characters (the
320 * non-ASCII check should be redundant as isprint() is not localized
321 * and only knows about ASCII, but be defensive about that)
323 if (non_ascii(ch
) || !isprint(ch
))
327 * rule out special printable characters (' ' should be the only
328 * whitespace character considered printable, but be defensive and use
331 if (isspace(ch
) || ch
== '=' || ch
== '?' || ch
== '_')
335 * rfc2047, section 5.3:
337 * As a replacement for a 'word' entity within a 'phrase', for example,
338 * one that precedes an address in a From, To, or Cc header. The ABNF
339 * definition for 'phrase' from RFC 822 thus becomes:
341 * phrase = 1*( encoded-word / word )
343 * In this case the set of characters that may be used in a "Q"-encoded
344 * 'encoded-word' is restricted to: <upper and lower case ASCII
345 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
346 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
347 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
348 * 'special' by 'linear-white-space'.
351 if (type
!= RFC2047_ADDRESS
)
354 /* '=' and '_' are special cases and have been checked above */
355 return !(isalnum(ch
) || ch
== '!' || ch
== '*' || ch
== '+' || ch
== '-' || ch
== '/');
358 static int needs_rfc2047_encoding(const char *line
, int len
)
362 for (i
= 0; i
< len
; i
++) {
364 if (non_ascii(ch
) || ch
== '\n')
366 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
373 static void add_rfc2047(struct strbuf
*sb
, const char *line
, size_t len
,
374 const char *encoding
, enum rfc2047_type type
)
376 static const int max_encoded_length
= 76; /* per rfc2047 */
378 int line_len
= last_line_length(sb
);
380 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
381 strbuf_addf(sb
, "=?%s?q?", encoding
);
382 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
386 * RFC 2047, section 5 (3):
388 * Each 'encoded-word' MUST represent an integral number of
389 * characters. A multi-octet character may not be split across
390 * adjacent 'encoded- word's.
392 const unsigned char *p
= (const unsigned char *)line
;
393 int chrlen
= mbs_chrlen(&line
, &len
, encoding
);
394 int is_special
= (chrlen
> 1) || is_rfc2047_special(*p
, type
);
396 /* "=%02X" * chrlen, or the byte itself */
397 const char *encoded_fmt
= is_special
? "=%02X" : "%c";
398 int encoded_len
= is_special
? 3 * chrlen
: 1;
401 * According to RFC 2047, we could encode the special character
402 * ' ' (space) with '_' (underscore) for readability. But many
403 * programs do not understand this and just leave the
404 * underscore in place. Thus, we do nothing special here, which
405 * causes ' ' to be encoded as '=20', avoiding this problem.
408 if (line_len
+ encoded_len
+ 2 > max_encoded_length
) {
409 /* It won't fit with trailing "?=" --- break the line */
410 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
411 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
414 for (i
= 0; i
< chrlen
; i
++)
415 strbuf_addf(sb
, encoded_fmt
, p
[i
]);
416 line_len
+= encoded_len
;
418 strbuf_addstr(sb
, "?=");
421 const char *show_ident_date(const struct ident_split
*ident
,
422 const struct date_mode
*mode
)
424 timestamp_t date
= 0;
427 if (ident
->date_begin
&& ident
->date_end
)
428 date
= parse_timestamp(ident
->date_begin
, NULL
, 10);
429 if (date_overflows(date
))
432 if (ident
->tz_begin
&& ident
->tz_end
)
433 tz
= strtol(ident
->tz_begin
, NULL
, 10);
434 if (tz
>= INT_MAX
|| tz
<= INT_MIN
)
437 return show_date(date
, tz
, mode
);
440 void pp_user_info(struct pretty_print_context
*pp
,
441 const char *what
, struct strbuf
*sb
,
442 const char *line
, const char *encoding
)
444 struct ident_split ident
;
446 const char *mailbuf
, *namebuf
;
447 size_t namelen
, maillen
;
448 int max_length
= 78; /* per rfc2822 */
450 if (pp
->fmt
== CMIT_FMT_ONELINE
)
453 line_end
= strchrnul(line
, '\n');
454 if (split_ident_line(&ident
, line
, line_end
- line
))
457 mailbuf
= ident
.mail_begin
;
458 maillen
= ident
.mail_end
- ident
.mail_begin
;
459 namebuf
= ident
.name_begin
;
460 namelen
= ident
.name_end
- ident
.name_begin
;
463 map_user(pp
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
465 if (cmit_fmt_is_mail(pp
->fmt
)) {
466 if (pp
->from_ident
&& ident_cmp(pp
->from_ident
, &ident
)) {
467 struct strbuf buf
= STRBUF_INIT
;
469 strbuf_addstr(&buf
, "From: ");
470 strbuf_add(&buf
, namebuf
, namelen
);
471 strbuf_addstr(&buf
, " <");
472 strbuf_add(&buf
, mailbuf
, maillen
);
473 strbuf_addstr(&buf
, ">\n");
474 string_list_append(&pp
->in_body_headers
,
475 strbuf_detach(&buf
, NULL
));
477 mailbuf
= pp
->from_ident
->mail_begin
;
478 maillen
= pp
->from_ident
->mail_end
- mailbuf
;
479 namebuf
= pp
->from_ident
->name_begin
;
480 namelen
= pp
->from_ident
->name_end
- namebuf
;
483 strbuf_addstr(sb
, "From: ");
484 if (pp
->encode_email_headers
&&
485 needs_rfc2047_encoding(namebuf
, namelen
)) {
486 add_rfc2047(sb
, namebuf
, namelen
,
487 encoding
, RFC2047_ADDRESS
);
488 max_length
= 76; /* per rfc2047 */
489 } else if (needs_rfc822_quoting(namebuf
, namelen
)) {
490 struct strbuf quoted
= STRBUF_INIT
;
491 add_rfc822_quoted("ed
, namebuf
, namelen
);
492 strbuf_add_wrapped_bytes(sb
, quoted
.buf
, quoted
.len
,
494 strbuf_release("ed
);
496 strbuf_add_wrapped_bytes(sb
, namebuf
, namelen
,
501 last_line_length(sb
) + strlen(" <") + maillen
+ strlen(">"))
502 strbuf_addch(sb
, '\n');
503 strbuf_addf(sb
, " <%.*s>\n", (int)maillen
, mailbuf
);
505 strbuf_addf(sb
, "%s: %.*s%.*s <%.*s>\n", what
,
506 (pp
->fmt
== CMIT_FMT_FULLER
) ? 4 : 0, " ",
507 (int)namelen
, namebuf
, (int)maillen
, mailbuf
);
511 case CMIT_FMT_MEDIUM
:
512 strbuf_addf(sb
, "Date: %s\n",
513 show_ident_date(&ident
, &pp
->date_mode
));
516 case CMIT_FMT_MBOXRD
:
517 strbuf_addf(sb
, "Date: %s\n",
518 show_ident_date(&ident
, DATE_MODE(RFC2822
)));
520 case CMIT_FMT_FULLER
:
521 strbuf_addf(sb
, "%sDate: %s\n", what
,
522 show_ident_date(&ident
, &pp
->date_mode
));
530 static int is_blank_line(const char *line
, int *len_p
)
533 while (len
&& isspace(line
[len
- 1]))
539 const char *skip_blank_lines(const char *msg
)
542 int linelen
= get_one_line(msg
);
546 if (!is_blank_line(msg
, &ll
))
553 static void add_merge_info(const struct pretty_print_context
*pp
,
554 struct strbuf
*sb
, const struct commit
*commit
)
556 struct commit_list
*parent
= commit
->parents
;
558 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (cmit_fmt_is_mail(pp
->fmt
)) ||
559 !parent
|| !parent
->next
)
562 strbuf_addstr(sb
, "Merge:");
565 struct object_id
*oidp
= &parent
->item
->object
.oid
;
566 strbuf_addch(sb
, ' ');
568 strbuf_add_unique_abbrev(sb
, oidp
, pp
->abbrev
);
570 strbuf_addstr(sb
, oid_to_hex(oidp
));
571 parent
= parent
->next
;
573 strbuf_addch(sb
, '\n');
576 static char *get_header(const char *msg
, const char *key
)
579 const char *v
= find_commit_header(msg
, key
, &len
);
580 return v
? xmemdupz(v
, len
) : NULL
;
583 static char *replace_encoding_header(char *buf
, const char *encoding
)
585 struct strbuf tmp
= STRBUF_INIT
;
589 /* guess if there is an encoding header before a \n\n */
590 while (!starts_with(cp
, "encoding ")) {
591 cp
= strchr(cp
, '\n');
592 if (!cp
|| *++cp
== '\n')
596 cp
= strchr(cp
, '\n');
598 return buf
; /* should not happen but be defensive */
599 len
= cp
+ 1 - (buf
+ start
);
601 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
602 if (is_encoding_utf8(encoding
)) {
603 /* we have re-coded to UTF-8; drop the header */
604 strbuf_remove(&tmp
, start
, len
);
606 /* just replaces XXXX in 'encoding XXXX\n' */
607 strbuf_splice(&tmp
, start
+ strlen("encoding "),
608 len
- strlen("encoding \n"),
609 encoding
, strlen(encoding
));
611 return strbuf_detach(&tmp
, NULL
);
614 const char *repo_logmsg_reencode(struct repository
*r
,
615 const struct commit
*commit
,
616 char **commit_encoding
,
617 const char *output_encoding
)
619 static const char *utf8
= "UTF-8";
620 const char *use_encoding
;
622 const char *msg
= repo_get_commit_buffer(r
, commit
, NULL
);
625 if (!output_encoding
|| !*output_encoding
) {
627 *commit_encoding
= get_header(msg
, "encoding");
630 encoding
= get_header(msg
, "encoding");
632 *commit_encoding
= encoding
;
633 use_encoding
= encoding
? encoding
: utf8
;
634 if (same_encoding(use_encoding
, output_encoding
)) {
636 * No encoding work to be done. If we have no encoding header
637 * at all, then there's nothing to do, and we can return the
638 * message verbatim (whether newly allocated or not).
644 * Otherwise, we still want to munge the encoding header in the
645 * result, which will be done by modifying the buffer. If we
646 * are using a fresh copy, we can reuse it. But if we are using
647 * the cached copy from get_commit_buffer, we need to duplicate it
648 * to avoid munging the cached copy.
650 if (msg
== get_cached_commit_buffer(r
, commit
, NULL
))
657 * There's actual encoding work to do. Do the reencoding, which
658 * still leaves the header to be replaced in the next step. At
659 * this point, we are done with msg. If we allocated a fresh
660 * copy, we can free it.
662 out
= reencode_string(msg
, output_encoding
, use_encoding
);
664 repo_unuse_commit_buffer(r
, commit
, msg
);
668 * This replacement actually consumes the buffer we hand it, so we do
669 * not have to worry about freeing the old "out" here.
672 out
= replace_encoding_header(out
, output_encoding
);
674 if (!commit_encoding
)
677 * If the re-encoding failed, out might be NULL here; in that
678 * case we just return the commit message verbatim.
680 return out
? out
: msg
;
683 static int mailmap_name(const char **email
, size_t *email_len
,
684 const char **name
, size_t *name_len
)
686 static struct string_list
*mail_map
;
688 mail_map
= xcalloc(1, sizeof(*mail_map
));
689 read_mailmap(mail_map
, NULL
);
691 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
694 static size_t format_person_part(struct strbuf
*sb
, char part
,
695 const char *msg
, int len
,
696 const struct date_mode
*dmode
)
698 /* currently all placeholders have same length */
699 const int placeholder_len
= 2;
700 struct ident_split s
;
701 const char *name
, *mail
;
702 size_t maillen
, namelen
;
704 if (split_ident_line(&s
, msg
, len
) < 0)
708 namelen
= s
.name_end
- s
.name_begin
;
710 maillen
= s
.mail_end
- s
.mail_begin
;
712 if (part
== 'N' || part
== 'E' || part
== 'L') /* mailmap lookup */
713 mailmap_name(&mail
, &maillen
, &name
, &namelen
);
714 if (part
== 'n' || part
== 'N') { /* name */
715 strbuf_add(sb
, name
, namelen
);
716 return placeholder_len
;
718 if (part
== 'e' || part
== 'E') { /* email */
719 strbuf_add(sb
, mail
, maillen
);
720 return placeholder_len
;
722 if (part
== 'l' || part
== 'L') { /* local-part */
723 const char *at
= memchr(mail
, '@', maillen
);
726 strbuf_add(sb
, mail
, maillen
);
727 return placeholder_len
;
733 if (part
== 't') { /* date, UNIX timestamp */
734 strbuf_add(sb
, s
.date_begin
, s
.date_end
- s
.date_begin
);
735 return placeholder_len
;
740 strbuf_addstr(sb
, show_ident_date(&s
, dmode
));
741 return placeholder_len
;
742 case 'D': /* date, RFC2822 style */
743 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(RFC2822
)));
744 return placeholder_len
;
745 case 'r': /* date, relative */
746 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(RELATIVE
)));
747 return placeholder_len
;
748 case 'i': /* date, ISO 8601-like */
749 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(ISO8601
)));
750 return placeholder_len
;
751 case 'I': /* date, ISO 8601 strict */
752 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(ISO8601_STRICT
)));
753 return placeholder_len
;
755 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(SHORT
)));
756 return placeholder_len
;
761 * reading from either a bogus commit, or a reflog entry with
762 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
763 * to compute a valid return value.
765 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
766 || part
== 'D' || part
== 'r' || part
== 'i')
767 return placeholder_len
;
769 return 0; /* unknown placeholder */
781 flush_left_and_steal
,
792 struct format_commit_context
{
793 const struct commit
*commit
;
794 const struct pretty_print_context
*pretty_ctx
;
795 unsigned commit_header_parsed
:1;
796 unsigned commit_message_parsed
:1;
797 struct signature_check signature_check
;
798 enum flush_type flush_type
;
799 enum trunc_type truncate
;
801 char *commit_encoding
;
802 size_t width
, indent1
, indent2
;
806 /* These offsets are relative to the start of the commit message. */
808 struct chunk committer
;
813 /* The following ones are relative to the result struct strbuf. */
817 static void parse_commit_header(struct format_commit_context
*context
)
819 const char *msg
= context
->message
;
822 for (i
= 0; msg
[i
]; i
++) {
825 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
830 } else if (skip_prefix(msg
+ i
, "author ", &name
)) {
831 context
->author
.off
= name
- msg
;
832 context
->author
.len
= msg
+ eol
- name
;
833 } else if (skip_prefix(msg
+ i
, "committer ", &name
)) {
834 context
->committer
.off
= name
- msg
;
835 context
->committer
.len
= msg
+ eol
- name
;
839 context
->message_off
= i
;
840 context
->commit_header_parsed
= 1;
843 static int istitlechar(char c
)
845 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
846 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
849 void format_sanitized_subject(struct strbuf
*sb
, const char *msg
, size_t len
)
852 size_t start_len
= sb
->len
;
856 for (i
= 0; i
< len
; i
++) {
857 if (istitlechar(msg
[i
])) {
859 strbuf_addch(sb
, '-');
861 strbuf_addch(sb
, msg
[i
]);
863 while (msg
[i
+1] == '.')
869 /* trim any trailing '.' or '-' characters */
871 while (sb
->len
- trimlen
> start_len
&&
872 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
873 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
875 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
878 const char *format_subject(struct strbuf
*sb
, const char *msg
,
879 const char *line_separator
)
884 const char *line
= msg
;
885 int linelen
= get_one_line(line
);
888 if (!linelen
|| is_blank_line(line
, &linelen
))
893 strbuf_grow(sb
, linelen
+ 2);
895 strbuf_addstr(sb
, line_separator
);
896 strbuf_add(sb
, line
, linelen
);
902 static void parse_commit_message(struct format_commit_context
*c
)
904 const char *msg
= c
->message
+ c
->message_off
;
905 const char *start
= c
->message
;
907 msg
= skip_blank_lines(msg
);
908 c
->subject_off
= msg
- start
;
910 msg
= format_subject(NULL
, msg
, NULL
);
911 msg
= skip_blank_lines(msg
);
912 c
->body_off
= msg
- start
;
914 c
->commit_message_parsed
= 1;
917 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
918 size_t width
, size_t indent1
, size_t indent2
)
920 struct strbuf tmp
= STRBUF_INIT
;
923 strbuf_add(&tmp
, sb
->buf
, pos
);
924 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
925 cast_size_t_to_int(indent1
),
926 cast_size_t_to_int(indent2
),
927 cast_size_t_to_int(width
));
928 strbuf_swap(&tmp
, sb
);
929 strbuf_release(&tmp
);
932 static void rewrap_message_tail(struct strbuf
*sb
,
933 struct format_commit_context
*c
,
934 size_t new_width
, size_t new_indent1
,
937 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
938 c
->indent2
== new_indent2
)
940 if (c
->wrap_start
< sb
->len
)
941 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
942 c
->wrap_start
= sb
->len
;
943 c
->width
= new_width
;
944 c
->indent1
= new_indent1
;
945 c
->indent2
= new_indent2
;
948 static int format_reflog_person(struct strbuf
*sb
,
950 struct reflog_walk_info
*log
,
951 const struct date_mode
*dmode
)
958 ident
= get_reflog_ident(log
);
962 return format_person_part(sb
, part
, ident
, strlen(ident
), dmode
);
965 static size_t parse_color(struct strbuf
*sb
, /* in UTF-8 */
966 const char *placeholder
,
967 struct format_commit_context
*c
)
969 const char *rest
= placeholder
;
970 const char *basic_color
= NULL
;
972 if (placeholder
[1] == '(') {
973 const char *begin
= placeholder
+ 2;
974 const char *end
= strchr(begin
, ')');
975 char color
[COLOR_MAXLEN
];
980 if (skip_prefix(begin
, "auto,", &begin
)) {
981 if (!want_color(c
->pretty_ctx
->color
))
982 return end
- placeholder
+ 1;
983 } else if (skip_prefix(begin
, "always,", &begin
)) {
984 /* nothing to do; we do not respect want_color at all */
986 /* the default is the same as "auto" */
987 if (!want_color(c
->pretty_ctx
->color
))
988 return end
- placeholder
+ 1;
991 if (color_parse_mem(begin
, end
- begin
, color
) < 0)
992 die(_("unable to parse --pretty format"));
993 strbuf_addstr(sb
, color
);
994 return end
- placeholder
+ 1;
998 * We handle things like "%C(red)" above; for historical reasons, there
999 * are a few colors that can be specified without parentheses (and
1000 * they cannot support things like "auto" or "always" at all).
1002 if (skip_prefix(placeholder
+ 1, "red", &rest
))
1003 basic_color
= GIT_COLOR_RED
;
1004 else if (skip_prefix(placeholder
+ 1, "green", &rest
))
1005 basic_color
= GIT_COLOR_GREEN
;
1006 else if (skip_prefix(placeholder
+ 1, "blue", &rest
))
1007 basic_color
= GIT_COLOR_BLUE
;
1008 else if (skip_prefix(placeholder
+ 1, "reset", &rest
))
1009 basic_color
= GIT_COLOR_RESET
;
1011 if (basic_color
&& want_color(c
->pretty_ctx
->color
))
1012 strbuf_addstr(sb
, basic_color
);
1014 return rest
- placeholder
;
1017 static size_t parse_padding_placeholder(const char *placeholder
,
1018 struct format_commit_context
*c
)
1020 const char *ch
= placeholder
;
1021 enum flush_type flush_type
;
1026 flush_type
= flush_right
;
1030 flush_type
= flush_both
;
1032 } else if (*ch
== '>') {
1033 flush_type
= flush_left_and_steal
;
1036 flush_type
= flush_left
;
1042 /* the next value means "wide enough to that column" */
1049 const char *start
= ch
+ 1;
1050 const char *end
= start
+ strcspn(start
, ",)");
1053 if (!*end
|| end
== start
)
1055 width
= strtol(start
, &next
, 10);
1058 * We need to limit the amount of padding, or otherwise this
1059 * would allow the user to pad the buffer by arbitrarily many
1060 * bytes and thus cause resource exhaustion.
1062 if (width
< -FORMATTING_LIMIT
|| width
> FORMATTING_LIMIT
)
1065 if (next
== start
|| width
== 0)
1069 width
+= term_columns();
1073 c
->padding
= to_column
? -width
: width
;
1074 c
->flush_type
= flush_type
;
1078 end
= strchr(start
, ')');
1079 if (!end
|| end
== start
)
1081 if (starts_with(start
, "trunc)"))
1082 c
->truncate
= trunc_right
;
1083 else if (starts_with(start
, "ltrunc)"))
1084 c
->truncate
= trunc_left
;
1085 else if (starts_with(start
, "mtrunc)"))
1086 c
->truncate
= trunc_middle
;
1090 c
->truncate
= trunc_none
;
1092 return end
- placeholder
+ 1;
1097 static int match_placeholder_arg_value(const char *to_parse
, const char *candidate
,
1098 const char **end
, const char **valuestart
,
1103 if (!(skip_prefix(to_parse
, candidate
, &p
)))
1107 *valuestart
= p
+ 1;
1108 *valuelen
= strcspn(*valuestart
, ",)");
1109 p
= *valuestart
+ *valuelen
;
1111 if (*p
!= ',' && *p
!= ')')
1128 static int match_placeholder_bool_arg(const char *to_parse
, const char *candidate
,
1129 const char **end
, int *val
)
1136 if (!match_placeholder_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
1144 strval
= xstrndup(argval
, arglen
);
1145 v
= git_parse_maybe_bool(strval
);
1156 static int format_trailer_match_cb(const struct strbuf
*key
, void *ud
)
1158 const struct string_list
*list
= ud
;
1159 const struct string_list_item
*item
;
1161 for_each_string_list_item (item
, list
) {
1162 if (key
->len
== (uintptr_t)item
->util
&&
1163 !strncasecmp(item
->string
, key
->buf
, key
->len
))
1169 static size_t format_commit_one(struct strbuf
*sb
, /* in UTF-8 */
1170 const char *placeholder
,
1173 struct format_commit_context
*c
= context
;
1174 const struct commit
*commit
= c
->commit
;
1175 const char *msg
= c
->message
;
1176 struct commit_list
*p
;
1177 const char *arg
, *eol
;
1181 /* these are independent of the commit */
1182 res
= strbuf_expand_literal_cb(sb
, placeholder
, NULL
);
1186 switch (placeholder
[0]) {
1188 if (starts_with(placeholder
+ 1, "(auto)")) {
1189 c
->auto_color
= want_color(c
->pretty_ctx
->color
);
1190 if (c
->auto_color
&& sb
->len
)
1191 strbuf_addstr(sb
, GIT_COLOR_RESET
);
1192 return 7; /* consumed 7 bytes, "C(auto)" */
1194 int ret
= parse_color(sb
, placeholder
, c
);
1198 * Otherwise, we decided to treat %C<unknown>
1199 * as a literal string, and the previous
1200 * %C(auto) is still valid.
1205 if (placeholder
[1] == '(') {
1206 unsigned long width
= 0, indent1
= 0, indent2
= 0;
1208 const char *start
= placeholder
+ 2;
1209 const char *end
= strchr(start
, ')');
1213 width
= strtoul(start
, &next
, 10);
1215 indent1
= strtoul(next
+ 1, &next
, 10);
1217 indent2
= strtoul(next
+ 1,
1226 * We need to limit the format here as it allows the
1227 * user to prepend arbitrarily many bytes to the buffer
1230 if (width
> FORMATTING_LIMIT
||
1231 indent1
> FORMATTING_LIMIT
||
1232 indent2
> FORMATTING_LIMIT
)
1234 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
1235 return end
- placeholder
+ 1;
1241 return parse_padding_placeholder(placeholder
, c
);
1244 /* these depend on the commit */
1245 if (!commit
->object
.parsed
)
1246 parse_object(the_repository
, &commit
->object
.oid
);
1248 switch (placeholder
[0]) {
1249 case 'H': /* commit hash */
1250 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_COMMIT
));
1251 strbuf_addstr(sb
, oid_to_hex(&commit
->object
.oid
));
1252 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_RESET
));
1254 case 'h': /* abbreviated commit hash */
1255 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_COMMIT
));
1256 strbuf_add_unique_abbrev(sb
, &commit
->object
.oid
,
1257 c
->pretty_ctx
->abbrev
);
1258 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_RESET
));
1260 case 'T': /* tree hash */
1261 strbuf_addstr(sb
, oid_to_hex(get_commit_tree_oid(commit
)));
1263 case 't': /* abbreviated tree hash */
1264 strbuf_add_unique_abbrev(sb
,
1265 get_commit_tree_oid(commit
),
1266 c
->pretty_ctx
->abbrev
);
1268 case 'P': /* parent hashes */
1269 for (p
= commit
->parents
; p
; p
= p
->next
) {
1270 if (p
!= commit
->parents
)
1271 strbuf_addch(sb
, ' ');
1272 strbuf_addstr(sb
, oid_to_hex(&p
->item
->object
.oid
));
1275 case 'p': /* abbreviated parent hashes */
1276 for (p
= commit
->parents
; p
; p
= p
->next
) {
1277 if (p
!= commit
->parents
)
1278 strbuf_addch(sb
, ' ');
1279 strbuf_add_unique_abbrev(sb
, &p
->item
->object
.oid
,
1280 c
->pretty_ctx
->abbrev
);
1283 case 'm': /* left/right/bottom */
1284 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
1287 format_decorations(sb
, commit
, c
->auto_color
);
1290 format_decorations_extended(sb
, commit
, c
->auto_color
, "", ", ", "");
1292 case 'S': /* tag/branch like --source */
1293 if (!(c
->pretty_ctx
->rev
&& c
->pretty_ctx
->rev
->sources
))
1295 slot
= revision_sources_at(c
->pretty_ctx
->rev
->sources
, commit
);
1296 if (!(slot
&& *slot
))
1298 strbuf_addstr(sb
, *slot
);
1300 case 'g': /* reflog info */
1301 switch(placeholder
[1]) {
1302 case 'd': /* reflog selector */
1304 if (c
->pretty_ctx
->reflog_info
)
1305 get_reflog_selector(sb
,
1306 c
->pretty_ctx
->reflog_info
,
1307 &c
->pretty_ctx
->date_mode
,
1308 c
->pretty_ctx
->date_mode_explicit
,
1309 (placeholder
[1] == 'd'));
1311 case 's': /* reflog message */
1312 if (c
->pretty_ctx
->reflog_info
)
1313 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
1319 return format_reflog_person(sb
,
1321 c
->pretty_ctx
->reflog_info
,
1322 &c
->pretty_ctx
->date_mode
);
1324 return 0; /* unknown %g placeholder */
1326 if (c
->pretty_ctx
->notes_message
) {
1327 strbuf_addstr(sb
, c
->pretty_ctx
->notes_message
);
1333 if (placeholder
[0] == 'G') {
1334 if (!c
->signature_check
.result
)
1335 check_commit_signature(c
->commit
, &(c
->signature_check
));
1336 switch (placeholder
[1]) {
1338 if (c
->signature_check
.gpg_output
)
1339 strbuf_addstr(sb
, c
->signature_check
.gpg_output
);
1342 switch (c
->signature_check
.result
) {
1344 switch (c
->signature_check
.trust_level
) {
1345 case TRUST_UNDEFINED
:
1347 strbuf_addch(sb
, 'U');
1350 strbuf_addch(sb
, 'G');
1360 strbuf_addch(sb
, c
->signature_check
.result
);
1364 if (c
->signature_check
.signer
)
1365 strbuf_addstr(sb
, c
->signature_check
.signer
);
1368 if (c
->signature_check
.key
)
1369 strbuf_addstr(sb
, c
->signature_check
.key
);
1372 if (c
->signature_check
.fingerprint
)
1373 strbuf_addstr(sb
, c
->signature_check
.fingerprint
);
1376 if (c
->signature_check
.primary_key_fingerprint
)
1377 strbuf_addstr(sb
, c
->signature_check
.primary_key_fingerprint
);
1380 switch (c
->signature_check
.trust_level
) {
1381 case TRUST_UNDEFINED
:
1382 strbuf_addstr(sb
, "undefined");
1385 strbuf_addstr(sb
, "never");
1387 case TRUST_MARGINAL
:
1388 strbuf_addstr(sb
, "marginal");
1391 strbuf_addstr(sb
, "fully");
1393 case TRUST_ULTIMATE
:
1394 strbuf_addstr(sb
, "ultimate");
1405 /* For the rest we have to parse the commit header. */
1406 if (!c
->commit_header_parsed
)
1407 parse_commit_header(c
);
1409 switch (placeholder
[0]) {
1410 case 'a': /* author ... */
1411 return format_person_part(sb
, placeholder
[1],
1412 msg
+ c
->author
.off
, c
->author
.len
,
1413 &c
->pretty_ctx
->date_mode
);
1414 case 'c': /* committer ... */
1415 return format_person_part(sb
, placeholder
[1],
1416 msg
+ c
->committer
.off
, c
->committer
.len
,
1417 &c
->pretty_ctx
->date_mode
);
1418 case 'e': /* encoding */
1419 if (c
->commit_encoding
)
1420 strbuf_addstr(sb
, c
->commit_encoding
);
1422 case 'B': /* raw body */
1423 /* message_off is always left at the initial newline */
1424 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
1428 /* Now we need to parse the commit message. */
1429 if (!c
->commit_message_parsed
)
1430 parse_commit_message(c
);
1432 switch (placeholder
[0]) {
1433 case 's': /* subject */
1434 format_subject(sb
, msg
+ c
->subject_off
, " ");
1436 case 'f': /* sanitized subject */
1437 eol
= strchrnul(msg
+ c
->subject_off
, '\n');
1438 format_sanitized_subject(sb
, msg
+ c
->subject_off
, eol
- (msg
+ c
->subject_off
));
1440 case 'b': /* body */
1441 strbuf_addstr(sb
, msg
+ c
->body_off
);
1445 if (skip_prefix(placeholder
, "(trailers", &arg
)) {
1446 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
1447 struct string_list filter_list
= STRING_LIST_INIT_NODUP
;
1448 struct strbuf sepbuf
= STRBUF_INIT
;
1451 opts
.no_divider
= 1;
1459 if (match_placeholder_arg_value(arg
, "key", &arg
, &argval
, &arglen
)) {
1460 uintptr_t len
= arglen
;
1465 if (len
&& argval
[len
- 1] == ':')
1467 string_list_append(&filter_list
, argval
)->util
= (char *)len
;
1469 opts
.filter
= format_trailer_match_cb
;
1470 opts
.filter_data
= &filter_list
;
1471 opts
.only_trailers
= 1;
1472 } else if (match_placeholder_arg_value(arg
, "separator", &arg
, &argval
, &arglen
)) {
1475 strbuf_reset(&sepbuf
);
1476 fmt
= xstrndup(argval
, arglen
);
1477 strbuf_expand(&sepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
1479 opts
.separator
= &sepbuf
;
1480 } else if (!match_placeholder_bool_arg(arg
, "only", &arg
, &opts
.only_trailers
) &&
1481 !match_placeholder_bool_arg(arg
, "unfold", &arg
, &opts
.unfold
) &&
1482 !match_placeholder_bool_arg(arg
, "valueonly", &arg
, &opts
.value_only
))
1487 format_trailers_from_commit(sb
, msg
+ c
->subject_off
, &opts
);
1488 ret
= arg
- placeholder
+ 1;
1491 string_list_clear(&filter_list
, 0);
1492 strbuf_release(&sepbuf
);
1496 return 0; /* unknown placeholder */
1499 static size_t format_and_pad_commit(struct strbuf
*sb
, /* in UTF-8 */
1500 const char *placeholder
,
1501 struct format_commit_context
*c
)
1503 struct strbuf local_sb
= STRBUF_INIT
;
1504 size_t total_consumed
= 0;
1505 int len
, padding
= c
->padding
;
1508 const char *start
= strrchr(sb
->buf
, '\n');
1512 occupied
= utf8_strnwidth(start
, strlen(start
), 1);
1513 occupied
+= c
->pretty_ctx
->graph_width
;
1514 padding
= (-padding
) - occupied
;
1517 int modifier
= *placeholder
== 'C';
1518 size_t consumed
= format_commit_one(&local_sb
, placeholder
, c
);
1519 total_consumed
+= consumed
;
1524 placeholder
+= consumed
;
1525 if (*placeholder
!= '%')
1530 len
= utf8_strnwidth(local_sb
.buf
, local_sb
.len
, 1);
1532 if (c
->flush_type
== flush_left_and_steal
) {
1533 const char *ch
= sb
->buf
+ sb
->len
- 1;
1534 while (len
> padding
&& ch
> sb
->buf
) {
1541 /* check for trailing ansi sequences */
1545 while (p
> sb
->buf
&& ch
- p
< 10 && *p
!= '\033')
1548 ch
+ 1 - p
!= display_mode_esc_sequence_len(p
))
1551 * got a good ansi sequence, put it back to
1552 * local_sb as we're cutting sb
1554 strbuf_insert(&local_sb
, 0, p
, ch
+ 1 - p
);
1557 strbuf_setlen(sb
, ch
+ 1 - sb
->buf
);
1558 c
->flush_type
= flush_left
;
1561 if (len
> padding
) {
1562 switch (c
->truncate
) {
1564 strbuf_utf8_replace(&local_sb
,
1565 0, len
- (padding
- 2),
1569 strbuf_utf8_replace(&local_sb
,
1571 len
- (padding
- 2),
1575 strbuf_utf8_replace(&local_sb
,
1576 padding
- 2, len
- (padding
- 2),
1582 strbuf_addbuf(sb
, &local_sb
);
1584 size_t sb_len
= sb
->len
, offset
= 0;
1585 if (c
->flush_type
== flush_left
)
1586 offset
= padding
- len
;
1587 else if (c
->flush_type
== flush_both
)
1588 offset
= (padding
- len
) / 2;
1590 * we calculate padding in columns, now
1591 * convert it back to chars
1593 padding
= padding
- len
+ local_sb
.len
;
1594 strbuf_addchars(sb
, ' ', padding
);
1595 memcpy(sb
->buf
+ sb_len
+ offset
, local_sb
.buf
,
1598 strbuf_release(&local_sb
);
1599 c
->flush_type
= no_flush
;
1600 return total_consumed
;
1603 static size_t format_commit_item(struct strbuf
*sb
, /* in UTF-8 */
1604 const char *placeholder
,
1607 size_t consumed
, orig_len
;
1610 ADD_LF_BEFORE_NON_EMPTY
,
1611 DEL_LF_BEFORE_EMPTY
,
1612 ADD_SP_BEFORE_NON_EMPTY
1615 switch (placeholder
[0]) {
1617 magic
= DEL_LF_BEFORE_EMPTY
;
1620 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1623 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1628 if (magic
!= NO_MAGIC
) {
1631 switch (placeholder
[0]) {
1634 * `%+w()` cannot ever expand to a non-empty string,
1635 * and it potentially changes the layout of preceding
1636 * contents. We're thus not able to handle the magic in
1637 * this combination and refuse the pattern.
1644 if (((struct format_commit_context
*)context
)->flush_type
!= no_flush
)
1645 consumed
= format_and_pad_commit(sb
, placeholder
, context
);
1647 consumed
= format_commit_one(sb
, placeholder
, context
);
1648 if (magic
== NO_MAGIC
)
1651 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
1652 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
1653 strbuf_setlen(sb
, sb
->len
- 1);
1654 } else if (orig_len
!= sb
->len
) {
1655 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1656 strbuf_insertstr(sb
, orig_len
, "\n");
1657 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1658 strbuf_insertstr(sb
, orig_len
, " ");
1660 return consumed
+ 1;
1663 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
1666 struct userformat_want
*w
= context
;
1668 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1671 switch (*placeholder
) {
1682 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1684 struct strbuf dummy
= STRBUF_INIT
;
1691 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1692 strbuf_release(&dummy
);
1695 void repo_format_commit_message(struct repository
*r
,
1696 const struct commit
*commit
,
1697 const char *format
, struct strbuf
*sb
,
1698 const struct pretty_print_context
*pretty_ctx
)
1700 struct format_commit_context context
= {
1702 .pretty_ctx
= pretty_ctx
,
1703 .wrap_start
= sb
->len
1705 const char *output_enc
= pretty_ctx
->output_encoding
;
1706 const char *utf8
= "UTF-8";
1709 * convert a commit message to UTF-8 first
1710 * as far as 'format_commit_item' assumes it in UTF-8
1712 context
.message
= repo_logmsg_reencode(r
, commit
,
1713 &context
.commit_encoding
,
1716 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1717 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1719 /* then convert a commit message to an actual output encoding */
1721 if (same_encoding(utf8
, output_enc
))
1724 if (context
.commit_encoding
&&
1725 !same_encoding(context
.commit_encoding
, utf8
))
1726 output_enc
= context
.commit_encoding
;
1731 char *out
= reencode_string_len(sb
->buf
, sb
->len
,
1732 output_enc
, utf8
, &outsz
);
1734 strbuf_attach(sb
, out
, outsz
, outsz
+ 1);
1737 free(context
.commit_encoding
);
1738 repo_unuse_commit_buffer(r
, commit
, context
.message
);
1741 static void pp_header(struct pretty_print_context
*pp
,
1742 const char *encoding
,
1743 const struct commit
*commit
,
1747 int parents_shown
= 0;
1750 const char *name
, *line
= *msg_p
;
1751 int linelen
= get_one_line(*msg_p
);
1761 if (pp
->fmt
== CMIT_FMT_RAW
) {
1762 strbuf_add(sb
, line
, linelen
);
1766 if (starts_with(line
, "parent ")) {
1767 if (linelen
!= the_hash_algo
->hexsz
+ 8)
1768 die("bad parent line in commit");
1772 if (!parents_shown
) {
1773 unsigned num
= commit_list_count(commit
->parents
);
1774 /* with enough slop */
1775 strbuf_grow(sb
, num
* (GIT_MAX_HEXSZ
+ 10) + 20);
1776 add_merge_info(pp
, sb
, commit
);
1781 * MEDIUM == DEFAULT shows only author with dates.
1782 * FULL shows both authors but not dates.
1783 * FULLER shows both authors and dates.
1785 if (skip_prefix(line
, "author ", &name
)) {
1786 strbuf_grow(sb
, linelen
+ 80);
1787 pp_user_info(pp
, "Author", sb
, name
, encoding
);
1789 if (skip_prefix(line
, "committer ", &name
) &&
1790 (pp
->fmt
== CMIT_FMT_FULL
|| pp
->fmt
== CMIT_FMT_FULLER
)) {
1791 strbuf_grow(sb
, linelen
+ 80);
1792 pp_user_info(pp
, "Commit", sb
, name
, encoding
);
1797 void pp_title_line(struct pretty_print_context
*pp
,
1800 const char *encoding
,
1803 static const int max_length
= 78; /* per rfc2047 */
1804 struct strbuf title
;
1806 strbuf_init(&title
, 80);
1807 *msg_p
= format_subject(&title
, *msg_p
,
1808 pp
->preserve_subject
? "\n" : " ");
1810 strbuf_grow(sb
, title
.len
+ 1024);
1811 if (pp
->print_email_subject
) {
1813 fmt_output_email_subject(sb
, pp
->rev
);
1814 if (pp
->encode_email_headers
&&
1815 needs_rfc2047_encoding(title
.buf
, title
.len
))
1816 add_rfc2047(sb
, title
.buf
, title
.len
,
1817 encoding
, RFC2047_SUBJECT
);
1819 strbuf_add_wrapped_bytes(sb
, title
.buf
, title
.len
,
1820 -last_line_length(sb
), 1, max_length
);
1822 strbuf_addbuf(sb
, &title
);
1824 strbuf_addch(sb
, '\n');
1826 if (need_8bit_cte
== 0) {
1828 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
1829 if (has_non_ascii(pp
->in_body_headers
.items
[i
].string
)) {
1836 if (need_8bit_cte
> 0) {
1837 const char *header_fmt
=
1838 "MIME-Version: 1.0\n"
1839 "Content-Type: text/plain; charset=%s\n"
1840 "Content-Transfer-Encoding: 8bit\n";
1841 strbuf_addf(sb
, header_fmt
, encoding
);
1843 if (pp
->after_subject
) {
1844 strbuf_addstr(sb
, pp
->after_subject
);
1846 if (cmit_fmt_is_mail(pp
->fmt
)) {
1847 strbuf_addch(sb
, '\n');
1850 if (pp
->in_body_headers
.nr
) {
1852 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
1853 strbuf_addstr(sb
, pp
->in_body_headers
.items
[i
].string
);
1854 free(pp
->in_body_headers
.items
[i
].string
);
1856 string_list_clear(&pp
->in_body_headers
, 0);
1857 strbuf_addch(sb
, '\n');
1860 strbuf_release(&title
);
1863 static int pp_utf8_width(const char *start
, const char *end
)
1866 size_t remain
= end
- start
;
1869 int n
= utf8_width(&start
, &remain
);
1870 if (n
< 0 || !start
)
1877 static void strbuf_add_tabexpand(struct strbuf
*sb
, int tabwidth
,
1878 const char *line
, int linelen
)
1882 while ((tab
= memchr(line
, '\t', linelen
)) != NULL
) {
1883 int width
= pp_utf8_width(line
, tab
);
1886 * If it wasn't well-formed utf8, or it
1887 * had characters with badly defined
1888 * width (control characters etc), just
1889 * give up on trying to align things.
1894 /* Output the data .. */
1895 strbuf_add(sb
, line
, tab
- line
);
1897 /* .. and the de-tabified tab */
1898 strbuf_addchars(sb
, ' ', tabwidth
- (width
% tabwidth
));
1900 /* Skip over the printed part .. */
1901 linelen
-= tab
+ 1 - line
;
1906 * Print out everything after the last tab without
1907 * worrying about width - there's nothing more to
1910 strbuf_add(sb
, line
, linelen
);
1914 * pp_handle_indent() prints out the intendation, and
1915 * the whole line (without the final newline), after
1918 static void pp_handle_indent(struct pretty_print_context
*pp
,
1919 struct strbuf
*sb
, int indent
,
1920 const char *line
, int linelen
)
1922 strbuf_addchars(sb
, ' ', indent
);
1923 if (pp
->expand_tabs_in_log
)
1924 strbuf_add_tabexpand(sb
, pp
->expand_tabs_in_log
, line
, linelen
);
1926 strbuf_add(sb
, line
, linelen
);
1929 static int is_mboxrd_from(const char *line
, int len
)
1932 * a line matching /^From $/ here would only have len == 4
1933 * at this point because is_empty_line would've trimmed all
1936 return len
> 4 && starts_with(line
+ strspn(line
, ">"), "From ");
1939 void pp_remainder(struct pretty_print_context
*pp
,
1946 const char *line
= *msg_p
;
1947 int linelen
= get_one_line(line
);
1953 if (is_blank_line(line
, &linelen
)) {
1956 if (pp
->fmt
== CMIT_FMT_SHORT
)
1961 strbuf_grow(sb
, linelen
+ indent
+ 20);
1963 pp_handle_indent(pp
, sb
, indent
, line
, linelen
);
1964 else if (pp
->expand_tabs_in_log
)
1965 strbuf_add_tabexpand(sb
, pp
->expand_tabs_in_log
,
1968 if (pp
->fmt
== CMIT_FMT_MBOXRD
&&
1969 is_mboxrd_from(line
, linelen
))
1970 strbuf_addch(sb
, '>');
1972 strbuf_add(sb
, line
, linelen
);
1974 strbuf_addch(sb
, '\n');
1978 void pretty_print_commit(struct pretty_print_context
*pp
,
1979 const struct commit
*commit
,
1982 unsigned long beginning_of_body
;
1985 const char *reencoded
;
1986 const char *encoding
;
1987 int need_8bit_cte
= pp
->need_8bit_cte
;
1989 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
1990 format_commit_message(commit
, user_format
, sb
, pp
);
1994 encoding
= get_log_output_encoding();
1995 msg
= reencoded
= logmsg_reencode(commit
, NULL
, encoding
);
1997 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2001 * We need to check and emit Content-type: to mark it
2002 * as 8-bit if we haven't done so.
2004 if (cmit_fmt_is_mail(pp
->fmt
) && need_8bit_cte
== 0) {
2007 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
2009 /* author could be non 7-bit ASCII but
2010 * the log may be so; skip over the
2011 * header part first.
2013 if (ch
== '\n' && msg
[i
+1] == '\n')
2016 else if (non_ascii(ch
)) {
2023 pp_header(pp
, encoding
, commit
, &msg
, sb
);
2024 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->print_email_subject
) {
2025 strbuf_addch(sb
, '\n');
2028 /* Skip excess blank lines at the beginning of body, if any... */
2029 msg
= skip_blank_lines(msg
);
2031 /* These formats treat the title line specially. */
2032 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2033 pp_title_line(pp
, &msg
, sb
, encoding
, need_8bit_cte
);
2035 beginning_of_body
= sb
->len
;
2036 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2037 pp_remainder(pp
, &msg
, sb
, indent
);
2040 /* Make sure there is an EOLN for the non-oneline case */
2041 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2042 strbuf_addch(sb
, '\n');
2045 * The caller may append additional body text in e-mail
2046 * format. Make sure we did not strip the blank line
2047 * between the header and the body.
2049 if (cmit_fmt_is_mail(pp
->fmt
) && sb
->len
<= beginning_of_body
)
2050 strbuf_addch(sb
, '\n');
2052 unuse_commit_buffer(commit
, reencoded
);
2055 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
2058 struct pretty_print_context pp
= {0};
2060 pretty_print_commit(&pp
, commit
, sb
);