10 #include "string-list.h"
15 #include "reflog-walk.h"
16 #include "gpg-interface.h"
18 #include "run-command.h"
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.
25 #define FORMATTING_LIMIT (16 * 1024)
27 static char *user_format
;
28 static struct cmt_fmt_map
{
32 int expand_tabs_in_log
;
34 enum date_mode_type default_date_mode_type
;
35 const char *user_format
;
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
);
42 int commit_format_is_empty(enum cmit_fmt fmt
)
44 return fmt
== CMIT_FMT_USERFORMAT
&& !*user_format
;
47 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
50 user_format
= xstrdup(cp
);
52 rev
->use_terminator
= 1;
53 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
56 static int git_pretty_formats_config(const char *var
, const char *value
,
59 struct cmt_fmt_map
*commit_format
= NULL
;
64 if (!skip_prefix(var
, "pretty.", &name
))
67 for (i
= 0; i
< builtin_formats_len
; i
++) {
68 if (!strcmp(commit_formats
[i
].name
, name
))
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
];
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
));
87 commit_format
->name
= xstrdup(name
);
88 commit_format
->format
= CMIT_FMT_USERFORMAT
;
89 if (git_config_string(&fmt
, var
, value
))
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;
97 commit_format
->is_alias
= 1;
98 commit_format
->user_format
= fmt
;
103 static void setup_commit_formats(void)
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)" },
117 * Please update $__git_log_pretty_formats in
118 * git-completion.bash when you add new formats.
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
));
127 git_config(git_pretty_formats_config
, NULL
);
130 static struct cmt_fmt_map
*find_commit_format_recursive(const char *sought
,
131 const char *original
,
132 int num_redirections
)
134 struct cmt_fmt_map
*found
= NULL
;
135 size_t found_match_len
= 0;
138 if (num_redirections
>= commit_formats_len
)
139 die("invalid --pretty format: "
140 "'%s' references an alias which points to itself",
143 for (i
= 0; i
< commit_formats_len
; i
++) {
146 if (!starts_with(commit_formats
[i
].name
, sought
))
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
;
156 if (found
&& found
->is_alias
) {
157 found
= find_commit_format_recursive(found
->user_format
,
165 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
168 setup_commit_formats();
170 return find_commit_format_recursive(sought
, sought
, 0);
173 void get_commit_format(const char *arg
, struct rev_info
*rev
)
175 struct cmt_fmt_map
*commit_format
;
177 rev
->use_terminator
= 0;
179 rev
->commit_format
= CMIT_FMT_DEFAULT
;
182 if (skip_prefix(arg
, "format:", &arg
)) {
183 save_user_format(rev
, arg
, 0);
187 if (!*arg
|| skip_prefix(arg
, "tformat:", &arg
) || strchr(arg
, '%')) {
188 save_user_format(rev
, arg
, 1);
192 commit_format
= find_commit_format(arg
);
194 die("invalid --pretty format: %s", arg
);
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
);
208 * Generic support for pretty-printing the header
210 static int get_one_line(const char *msg
)
225 /* High bit set, or ISO-2022-INT */
226 static int non_ascii(int ch
)
228 return !isascii(ch
) || ch
== '\033';
231 int has_non_ascii(const char *s
)
236 while ((ch
= *s
++) != '\0') {
243 static int is_rfc822_special(char ch
)
265 static int needs_rfc822_quoting(const char *s
, int len
)
268 for (i
= 0; i
< len
; i
++)
269 if (is_rfc822_special(s
[i
]))
274 static int last_line_length(struct strbuf
*sb
)
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')
282 return sb
->len
- (i
+ 1);
285 static void add_rfc822_quoted(struct strbuf
*out
, const char *s
, int len
)
289 /* just a guess, we may have to also backslash-quote */
290 strbuf_grow(out
, len
+ 2);
292 strbuf_addch(out
, '"');
293 for (i
= 0; i
< len
; i
++) {
297 strbuf_addch(out
, '\\');
300 strbuf_addch(out
, s
[i
]);
303 strbuf_addch(out
, '"');
311 static int is_rfc2047_special(char ch
, enum rfc2047_type type
)
314 * rfc2047, section 4.2:
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.
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)
328 if (non_ascii(ch
) || !isprint(ch
))
332 * rule out special printable characters (' ' should be the only
333 * whitespace character considered printable, but be defensive and use
336 if (isspace(ch
) || ch
== '=' || ch
== '?' || ch
== '_')
340 * rfc2047, section 5.3:
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:
346 * phrase = 1*( encoded-word / word )
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'.
356 if (type
!= RFC2047_ADDRESS
)
359 /* '=' and '_' are special cases and have been checked above */
360 return !(isalnum(ch
) || ch
== '!' || ch
== '*' || ch
== '+' || ch
== '-' || ch
== '/');
363 static int needs_rfc2047_encoding(const char *line
, int len
)
367 for (i
= 0; i
< len
; i
++) {
369 if (non_ascii(ch
) || ch
== '\n')
371 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
378 static void add_rfc2047(struct strbuf
*sb
, const char *line
, size_t len
,
379 const char *encoding
, enum rfc2047_type type
)
381 static const int max_encoded_length
= 76; /* per rfc2047 */
383 int line_len
= last_line_length(sb
);
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? */
391 * RFC 2047, section 5 (3):
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.
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
);
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;
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.
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 */
419 for (i
= 0; i
< chrlen
; i
++)
420 strbuf_addf(sb
, encoded_fmt
, p
[i
]);
421 line_len
+= encoded_len
;
423 strbuf_addstr(sb
, "?=");
426 const char *show_ident_date(const struct ident_split
*ident
,
427 const struct date_mode
*mode
)
429 timestamp_t date
= 0;
432 if (ident
->date_begin
&& ident
->date_end
)
433 date
= parse_timestamp(ident
->date_begin
, NULL
, 10);
434 if (date_overflows(date
))
437 if (ident
->tz_begin
&& ident
->tz_end
)
438 tz
= strtol(ident
->tz_begin
, NULL
, 10);
439 if (tz
>= INT_MAX
|| tz
<= INT_MIN
)
442 return show_date(date
, tz
, mode
);
445 static inline void strbuf_add_with_color(struct strbuf
*sb
, const char *color
,
446 const char *buf
, size_t buflen
)
448 strbuf_addstr(sb
, color
);
449 strbuf_add(sb
, buf
, buflen
);
451 strbuf_addstr(sb
, GIT_COLOR_RESET
);
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
)
459 const char *buf
, *eol
, *line_color
, *match_color
;
466 if (!opt
|| !want_color(color
) || opt
->invert
)
469 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
470 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
472 while (grep_next_match(opt
, buf
, eol
, ctx
, &match
, field
, eflags
)) {
473 if (match
.rm_so
== match
.rm_eo
)
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
);
484 strbuf_add_with_color(sb
, line_color
, buf
, eol
- buf
);
487 strbuf_add(sb
, buf
, eol
- buf
);
491 static int use_in_body_from(const struct pretty_print_context
*pp
,
492 const struct ident_split
*ident
)
494 if (pp
->rev
&& pp
->rev
->force_in_body_from
)
496 if (ident_cmp(pp
->from_ident
, ident
))
501 void pp_user_info(struct pretty_print_context
*pp
,
502 const char *what
, struct strbuf
*sb
,
503 const char *line
, const char *encoding
)
505 struct ident_split ident
;
507 const char *mailbuf
, *namebuf
;
508 size_t namelen
, maillen
;
509 int max_length
= 78; /* per rfc2822 */
511 if (pp
->fmt
== CMIT_FMT_ONELINE
)
514 line_end
= strchrnul(line
, '\n');
515 if (split_ident_line(&ident
, line
, line_end
- line
))
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
;
524 map_user(pp
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
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
;
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
));
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
;
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("ed
, namebuf
, namelen
);
553 strbuf_add_wrapped_bytes(sb
, quoted
.buf
, quoted
.len
,
555 strbuf_release("ed
);
557 strbuf_add_wrapped_bytes(sb
, namebuf
, namelen
,
562 last_line_length(sb
) + strlen(" <") + maillen
+ strlen(">"))
563 strbuf_addch(sb
, '\n');
564 strbuf_addf(sb
, " <%.*s>\n", (int)maillen
, mailbuf
);
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
;
570 if (!strcmp(what
, "Author"))
571 field
= GREP_HEADER_AUTHOR
;
572 else if (!strcmp(what
, "Commit"))
573 field
= GREP_HEADER_COMMITTER
;
575 strbuf_addf(sb
, "%s: ", what
);
576 if (pp
->fmt
== CMIT_FMT_FULLER
)
577 strbuf_addchars(sb
, ' ', 4);
579 strbuf_addf(&id
, "%.*s <%.*s>", (int)namelen
, namebuf
,
580 (int)maillen
, mailbuf
);
582 append_line_with_color(sb
, opt
, id
.buf
, id
.len
, pp
->color
,
583 GREP_CONTEXT_HEAD
, field
);
584 strbuf_addch(sb
, '\n');
589 case CMIT_FMT_MEDIUM
:
590 strbuf_addf(sb
, "Date: %s\n",
591 show_ident_date(&ident
, &pp
->date_mode
));
594 case CMIT_FMT_MBOXRD
:
595 strbuf_addf(sb
, "Date: %s\n",
596 show_ident_date(&ident
, DATE_MODE(RFC2822
)));
598 case CMIT_FMT_FULLER
:
599 strbuf_addf(sb
, "%sDate: %s\n", what
,
600 show_ident_date(&ident
, &pp
->date_mode
));
608 static int is_blank_line(const char *line
, int *len_p
)
611 while (len
&& isspace(line
[len
- 1]))
617 const char *skip_blank_lines(const char *msg
)
620 int linelen
= get_one_line(msg
);
624 if (!is_blank_line(msg
, &ll
))
631 static void add_merge_info(const struct pretty_print_context
*pp
,
632 struct strbuf
*sb
, const struct commit
*commit
)
634 struct commit_list
*parent
= commit
->parents
;
636 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (cmit_fmt_is_mail(pp
->fmt
)) ||
637 !parent
|| !parent
->next
)
640 strbuf_addstr(sb
, "Merge:");
643 struct object_id
*oidp
= &parent
->item
->object
.oid
;
644 strbuf_addch(sb
, ' ');
646 strbuf_add_unique_abbrev(sb
, oidp
, pp
->abbrev
);
648 strbuf_addstr(sb
, oid_to_hex(oidp
));
649 parent
= parent
->next
;
651 strbuf_addch(sb
, '\n');
654 static char *get_header(const char *msg
, const char *key
)
657 const char *v
= find_commit_header(msg
, key
, &len
);
658 return v
? xmemdupz(v
, len
) : NULL
;
661 static char *replace_encoding_header(char *buf
, const char *encoding
)
663 struct strbuf tmp
= STRBUF_INIT
;
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')
674 cp
= strchr(cp
, '\n');
676 return buf
; /* should not happen but be defensive */
677 len
= cp
+ 1 - (buf
+ start
);
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
);
684 /* just replaces XXXX in 'encoding XXXX\n' */
685 strbuf_splice(&tmp
, start
+ strlen("encoding "),
686 len
- strlen("encoding \n"),
687 encoding
, strlen(encoding
));
689 return strbuf_detach(&tmp
, NULL
);
692 const char *repo_logmsg_reencode(struct repository
*r
,
693 const struct commit
*commit
,
694 char **commit_encoding
,
695 const char *output_encoding
)
697 static const char *utf8
= "UTF-8";
698 const char *use_encoding
;
700 const char *msg
= repo_get_commit_buffer(r
, commit
, NULL
);
703 if (!output_encoding
|| !*output_encoding
) {
705 *commit_encoding
= get_header(msg
, "encoding");
708 encoding
= get_header(msg
, "encoding");
710 *commit_encoding
= encoding
;
711 use_encoding
= encoding
? encoding
: utf8
;
712 if (same_encoding(use_encoding
, output_encoding
)) {
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).
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.
728 if (msg
== get_cached_commit_buffer(r
, commit
, NULL
))
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.
740 out
= reencode_string(msg
, output_encoding
, use_encoding
);
742 repo_unuse_commit_buffer(r
, commit
, msg
);
746 * This replacement actually consumes the buffer we hand it, so we do
747 * not have to worry about freeing the old "out" here.
750 out
= replace_encoding_header(out
, output_encoding
);
752 if (!commit_encoding
)
755 * If the re-encoding failed, out might be NULL here; in that
756 * case we just return the commit message verbatim.
758 return out
? out
: msg
;
761 static int mailmap_name(const char **email
, size_t *email_len
,
762 const char **name
, size_t *name_len
)
764 static struct string_list
*mail_map
;
766 CALLOC_ARRAY(mail_map
, 1);
767 read_mailmap(mail_map
);
769 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
772 static size_t format_person_part(struct strbuf
*sb
, char part
,
773 const char *msg
, int len
,
774 const struct date_mode
*dmode
)
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
;
782 if (split_ident_line(&s
, msg
, len
) < 0)
786 namelen
= s
.name_end
- s
.name_begin
;
788 maillen
= s
.mail_end
- s
.mail_begin
;
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
;
796 if (part
== 'e' || part
== 'E') { /* email */
797 strbuf_add(sb
, mail
, maillen
);
798 return placeholder_len
;
800 if (part
== 'l' || part
== 'L') { /* local-part */
801 const char *at
= memchr(mail
, '@', maillen
);
804 strbuf_add(sb
, mail
, maillen
);
805 return placeholder_len
;
811 if (part
== 't') { /* date, UNIX timestamp */
812 strbuf_add(sb
, s
.date_begin
, s
.date_end
- s
.date_begin
);
813 return placeholder_len
;
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
;
836 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(SHORT
)));
837 return placeholder_len
;
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.
846 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
847 || part
== 'D' || part
== 'r' || part
== 'i')
848 return placeholder_len
;
850 return 0; /* unknown placeholder */
862 flush_left_and_steal
,
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
;
883 char *commit_encoding
;
884 size_t width
, indent1
, indent2
;
888 /* These offsets are relative to the start of the commit message. */
890 struct chunk committer
;
895 /* The following ones are relative to the result struct strbuf. */
899 static void parse_commit_header(struct format_commit_context
*context
)
901 const char *msg
= context
->message
;
904 for (i
= 0; msg
[i
]; i
++) {
907 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
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
;
921 context
->message_off
= i
;
922 context
->commit_header_parsed
= 1;
925 static int istitlechar(char c
)
927 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
928 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
931 void format_sanitized_subject(struct strbuf
*sb
, const char *msg
, size_t len
)
934 size_t start_len
= sb
->len
;
938 for (i
= 0; i
< len
; i
++) {
939 if (istitlechar(msg
[i
])) {
941 strbuf_addch(sb
, '-');
943 strbuf_addch(sb
, msg
[i
]);
945 while (msg
[i
+1] == '.')
951 /* trim any trailing '.' or '-' characters */
953 while (sb
->len
- trimlen
> start_len
&&
954 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
955 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
957 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
960 const char *format_subject(struct strbuf
*sb
, const char *msg
,
961 const char *line_separator
)
966 const char *line
= msg
;
967 int linelen
= get_one_line(line
);
970 if (!linelen
|| is_blank_line(line
, &linelen
))
975 strbuf_grow(sb
, linelen
+ 2);
977 strbuf_addstr(sb
, line_separator
);
978 strbuf_add(sb
, line
, linelen
);
984 static void parse_commit_message(struct format_commit_context
*c
)
986 const char *msg
= c
->message
+ c
->message_off
;
987 const char *start
= c
->message
;
989 msg
= skip_blank_lines(msg
);
990 c
->subject_off
= msg
- start
;
992 msg
= format_subject(NULL
, msg
, NULL
);
993 msg
= skip_blank_lines(msg
);
994 c
->body_off
= msg
- start
;
996 c
->commit_message_parsed
= 1;
999 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
1000 size_t width
, size_t indent1
, size_t indent2
)
1002 struct strbuf tmp
= STRBUF_INIT
;
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
);
1014 static void rewrap_message_tail(struct strbuf
*sb
,
1015 struct format_commit_context
*c
,
1016 size_t new_width
, size_t new_indent1
,
1019 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
1020 c
->indent2
== new_indent2
)
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
;
1030 static int format_reflog_person(struct strbuf
*sb
,
1032 struct reflog_walk_info
*log
,
1033 const struct date_mode
*dmode
)
1040 ident
= get_reflog_ident(log
);
1044 return format_person_part(sb
, part
, ident
, strlen(ident
), dmode
);
1047 static size_t parse_color(struct strbuf
*sb
, /* in UTF-8 */
1048 const char *placeholder
,
1049 struct format_commit_context
*c
)
1051 const char *rest
= placeholder
;
1052 const char *basic_color
= NULL
;
1054 if (placeholder
[1] == '(') {
1055 const char *begin
= placeholder
+ 2;
1056 const char *end
= strchr(begin
, ')');
1057 char color
[COLOR_MAXLEN
];
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 */
1068 /* the default is the same as "auto" */
1069 if (!want_color(c
->pretty_ctx
->color
))
1070 return end
- placeholder
+ 1;
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;
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).
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
;
1093 if (basic_color
&& want_color(c
->pretty_ctx
->color
))
1094 strbuf_addstr(sb
, basic_color
);
1096 return rest
- placeholder
;
1099 static size_t parse_padding_placeholder(const char *placeholder
,
1100 struct format_commit_context
*c
)
1102 const char *ch
= placeholder
;
1103 enum flush_type flush_type
;
1108 flush_type
= flush_right
;
1112 flush_type
= flush_both
;
1114 } else if (*ch
== '>') {
1115 flush_type
= flush_left_and_steal
;
1118 flush_type
= flush_left
;
1124 /* the next value means "wide enough to that column" */
1131 const char *start
= ch
+ 1;
1132 const char *end
= start
+ strcspn(start
, ",)");
1135 if (!*end
|| end
== start
)
1137 width
= strtol(start
, &next
, 10);
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.
1144 if (width
< -FORMATTING_LIMIT
|| width
> FORMATTING_LIMIT
)
1147 if (next
== start
|| width
== 0)
1151 width
+= term_columns();
1155 c
->padding
= to_column
? -width
: width
;
1156 c
->flush_type
= flush_type
;
1160 end
= strchr(start
, ')');
1161 if (!end
|| end
== start
)
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
;
1172 c
->truncate
= trunc_none
;
1174 return end
- placeholder
+ 1;
1179 static int match_placeholder_arg_value(const char *to_parse
, const char *candidate
,
1180 const char **end
, const char **valuestart
,
1185 if (!(skip_prefix(to_parse
, candidate
, &p
)))
1189 *valuestart
= p
+ 1;
1190 *valuelen
= strcspn(*valuestart
, ",)");
1191 p
= *valuestart
+ *valuelen
;
1193 if (*p
!= ',' && *p
!= ')')
1210 static int match_placeholder_bool_arg(const char *to_parse
, const char *candidate
,
1211 const char **end
, int *val
)
1218 if (!match_placeholder_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
1226 strval
= xstrndup(argval
, arglen
);
1227 v
= git_parse_maybe_bool(strval
);
1238 static int format_trailer_match_cb(const struct strbuf
*key
, void *ud
)
1240 const struct string_list
*list
= ud
;
1241 const struct string_list_item
*item
;
1243 for_each_string_list_item (item
, list
) {
1244 if (key
->len
== (uintptr_t)item
->util
&&
1245 !strncasecmp(item
->string
, key
->buf
, key
->len
))
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
,
1265 if (match_placeholder_arg_value(*arg
, "key", arg
, &argval
, &arglen
)) {
1266 uintptr_t len
= arglen
;
1271 if (len
&& argval
[len
- 1] == ':')
1273 string_list_append(filter_list
, argval
)->util
= (char *)len
;
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
)) {
1281 strbuf_reset(sepbuf
);
1282 fmt
= xstrndup(argval
, arglen
);
1283 strbuf_expand(sepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
1285 opts
->separator
= sepbuf
;
1286 } else if (match_placeholder_arg_value(*arg
, "key_value_separator", arg
, &argval
, &arglen
)) {
1289 strbuf_reset(kvsepbuf
);
1290 fmt
= xstrndup(argval
, arglen
);
1291 strbuf_expand(kvsepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
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
)) {
1299 size_t len
= strcspn(*arg
, ",)");
1300 *invalid_arg
= xstrndup(*arg
, len
);
1308 static size_t parse_describe_args(const char *start
, struct strvec
*args
)
1314 DESCRIBE_ARG_INTEGER
,
1315 DESCRIBE_ARG_STRING
,
1318 { "tags", DESCRIBE_ARG_BOOL
},
1319 { "abbrev", DESCRIBE_ARG_INTEGER
},
1320 { "exclude", DESCRIBE_ARG_STRING
},
1321 { "match", DESCRIBE_ARG_STRING
},
1323 const char *arg
= start
;
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
)) {
1337 strvec_pushf(args
, "--%s", option
[i
].name
);
1339 strvec_pushf(args
, "--no-%s", option
[i
].name
);
1343 case DESCRIBE_ARG_INTEGER
:
1344 if (match_placeholder_arg_value(arg
, option
[i
].name
, &arg
,
1345 &argval
, &arglen
)) {
1349 strtol(argval
, &endptr
, 10);
1350 if (endptr
- argval
!= arglen
)
1352 strvec_pushf(args
, "--%s=%.*s", option
[i
].name
, (int)arglen
, argval
);
1356 case DESCRIBE_ARG_STRING
:
1357 if (match_placeholder_arg_value(arg
, option
[i
].name
, &arg
,
1358 &argval
, &arglen
)) {
1361 strvec_pushf(args
, "--%s=%.*s", option
[i
].name
, (int)arglen
, argval
);
1374 static size_t format_commit_one(struct strbuf
*sb
, /* in UTF-8 */
1375 const char *placeholder
,
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
;
1386 /* these are independent of the commit */
1387 res
= strbuf_expand_literal_cb(sb
, placeholder
, NULL
);
1391 switch (placeholder
[0]) {
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)" */
1399 int ret
= parse_color(sb
, placeholder
, c
);
1403 * Otherwise, we decided to treat %C<unknown>
1404 * as a literal string, and the previous
1405 * %C(auto) is still valid.
1410 if (placeholder
[1] == '(') {
1411 unsigned long width
= 0, indent1
= 0, indent2
= 0;
1413 const char *start
= placeholder
+ 2;
1414 const char *end
= strchr(start
, ')');
1418 width
= strtoul(start
, &next
, 10);
1420 indent1
= strtoul(next
+ 1, &next
, 10);
1422 indent2
= strtoul(next
+ 1,
1431 * We need to limit the format here as it allows the
1432 * user to prepend arbitrarily many bytes to the buffer
1435 if (width
> FORMATTING_LIMIT
||
1436 indent1
> FORMATTING_LIMIT
||
1437 indent2
> FORMATTING_LIMIT
)
1439 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
1440 return end
- placeholder
+ 1;
1446 return parse_padding_placeholder(placeholder
, c
);
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
;
1455 describe_status
= c
->pretty_ctx
->describe_status
;
1456 if (describe_status
) {
1457 if (!describe_status
->max_invocations
)
1459 describe_status
->max_invocations
--;
1463 strvec_push(&cmd
.args
, "describe");
1467 arg
+= parse_describe_args(arg
, &cmd
.args
);
1471 child_process_clear(&cmd
);
1475 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1476 pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0);
1478 strbuf_addbuf(sb
, &out
);
1479 strbuf_release(&out
);
1480 strbuf_release(&err
);
1481 return arg
- placeholder
+ 1;
1484 /* these depend on the commit */
1485 if (!commit
->object
.parsed
)
1486 parse_object(the_repository
, &commit
->object
.oid
);
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
));
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
));
1500 case 'T': /* tree hash */
1501 strbuf_addstr(sb
, oid_to_hex(get_commit_tree_oid(commit
)));
1503 case 't': /* abbreviated tree hash */
1504 strbuf_add_unique_abbrev(sb
,
1505 get_commit_tree_oid(commit
),
1506 c
->pretty_ctx
->abbrev
);
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
));
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
);
1523 case 'm': /* left/right/bottom */
1524 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
1527 format_decorations(sb
, commit
, c
->auto_color
);
1530 format_decorations_extended(sb
, commit
, c
->auto_color
, "", ", ", "");
1532 case 'S': /* tag/branch like --source */
1533 if (!(c
->pretty_ctx
->rev
&& c
->pretty_ctx
->rev
->sources
))
1535 slot
= revision_sources_at(c
->pretty_ctx
->rev
->sources
, commit
);
1536 if (!(slot
&& *slot
))
1538 strbuf_addstr(sb
, *slot
);
1540 case 'g': /* reflog info */
1541 switch(placeholder
[1]) {
1542 case 'd': /* reflog selector */
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'));
1551 case 's': /* reflog message */
1552 if (c
->pretty_ctx
->reflog_info
)
1553 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
1559 return format_reflog_person(sb
,
1561 c
->pretty_ctx
->reflog_info
,
1562 &c
->pretty_ctx
->date_mode
);
1564 return 0; /* unknown %g placeholder */
1566 if (c
->pretty_ctx
->notes_message
) {
1567 strbuf_addstr(sb
, c
->pretty_ctx
->notes_message
);
1573 if (placeholder
[0] == 'G') {
1574 if (!c
->signature_check
.result
)
1575 check_commit_signature(c
->commit
, &(c
->signature_check
));
1576 switch (placeholder
[1]) {
1578 if (c
->signature_check
.output
)
1579 strbuf_addstr(sb
, c
->signature_check
.output
);
1582 switch (c
->signature_check
.result
) {
1584 switch (c
->signature_check
.trust_level
) {
1585 case TRUST_UNDEFINED
:
1587 strbuf_addch(sb
, 'U');
1590 strbuf_addch(sb
, 'G');
1600 strbuf_addch(sb
, c
->signature_check
.result
);
1604 if (c
->signature_check
.signer
)
1605 strbuf_addstr(sb
, c
->signature_check
.signer
);
1608 if (c
->signature_check
.key
)
1609 strbuf_addstr(sb
, c
->signature_check
.key
);
1612 if (c
->signature_check
.fingerprint
)
1613 strbuf_addstr(sb
, c
->signature_check
.fingerprint
);
1616 if (c
->signature_check
.primary_key_fingerprint
)
1617 strbuf_addstr(sb
, c
->signature_check
.primary_key_fingerprint
);
1620 strbuf_addstr(sb
, gpg_trust_level_to_str(c
->signature_check
.trust_level
));
1628 /* For the rest we have to parse the commit header. */
1629 if (!c
->commit_header_parsed
) {
1631 repo_logmsg_reencode(c
->repository
, commit
,
1632 &c
->commit_encoding
, "UTF-8");
1633 parse_commit_header(c
);
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
);
1649 case 'B': /* raw body */
1650 /* message_off is always left at the initial newline */
1651 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
1655 /* Now we need to parse the commit message. */
1656 if (!c
->commit_message_parsed
)
1657 parse_commit_message(c
);
1659 switch (placeholder
[0]) {
1660 case 's': /* subject */
1661 format_subject(sb
, msg
+ c
->subject_off
, " ");
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
));
1667 case 'b': /* body */
1668 strbuf_addstr(sb
, msg
+ c
->body_off
);
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
;
1679 opts
.no_divider
= 1;
1683 if (format_set_trailers_options(&opts
, &filter_list
, &sepbuf
, &kvsepbuf
, &arg
, NULL
))
1687 format_trailers_from_commit(sb
, msg
+ c
->subject_off
, &opts
);
1688 ret
= arg
- placeholder
+ 1;
1691 string_list_clear(&filter_list
, 0);
1692 strbuf_release(&sepbuf
);
1696 return 0; /* unknown placeholder */
1699 static size_t format_and_pad_commit(struct strbuf
*sb
, /* in UTF-8 */
1700 const char *placeholder
,
1701 struct format_commit_context
*c
)
1703 struct strbuf local_sb
= STRBUF_INIT
;
1704 size_t total_consumed
= 0;
1705 int len
, padding
= c
->padding
;
1708 const char *start
= strrchr(sb
->buf
, '\n');
1712 occupied
= utf8_strnwidth(start
, strlen(start
), 1);
1713 occupied
+= c
->pretty_ctx
->graph_width
;
1714 padding
= (-padding
) - occupied
;
1717 int modifier
= *placeholder
== 'C';
1718 size_t consumed
= format_commit_one(&local_sb
, placeholder
, c
);
1719 total_consumed
+= consumed
;
1724 placeholder
+= consumed
;
1725 if (*placeholder
!= '%')
1730 len
= utf8_strnwidth(local_sb
.buf
, local_sb
.len
, 1);
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
) {
1741 /* check for trailing ansi sequences */
1745 while (p
> sb
->buf
&& ch
- p
< 10 && *p
!= '\033')
1748 ch
+ 1 - p
!= display_mode_esc_sequence_len(p
))
1751 * got a good ansi sequence, put it back to
1752 * local_sb as we're cutting sb
1754 strbuf_insert(&local_sb
, 0, p
, ch
+ 1 - p
);
1757 strbuf_setlen(sb
, ch
+ 1 - sb
->buf
);
1758 c
->flush_type
= flush_left
;
1761 if (len
> padding
) {
1762 switch (c
->truncate
) {
1764 strbuf_utf8_replace(&local_sb
,
1765 0, len
- (padding
- 2),
1769 strbuf_utf8_replace(&local_sb
,
1771 len
- (padding
- 2),
1775 strbuf_utf8_replace(&local_sb
,
1776 padding
- 2, len
- (padding
- 2),
1782 strbuf_addbuf(sb
, &local_sb
);
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;
1790 * we calculate padding in columns, now
1791 * convert it back to chars
1793 padding
= padding
- len
+ local_sb
.len
;
1794 strbuf_addchars(sb
, ' ', padding
);
1795 memcpy(sb
->buf
+ sb_len
+ offset
, local_sb
.buf
,
1798 strbuf_release(&local_sb
);
1799 c
->flush_type
= no_flush
;
1800 return total_consumed
;
1803 static size_t format_commit_item(struct strbuf
*sb
, /* in UTF-8 */
1804 const char *placeholder
,
1807 size_t consumed
, orig_len
;
1810 ADD_LF_BEFORE_NON_EMPTY
,
1811 DEL_LF_BEFORE_EMPTY
,
1812 ADD_SP_BEFORE_NON_EMPTY
1815 switch (placeholder
[0]) {
1817 magic
= DEL_LF_BEFORE_EMPTY
;
1820 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1823 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1828 if (magic
!= NO_MAGIC
) {
1831 switch (placeholder
[0]) {
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.
1844 if (((struct format_commit_context
*)context
)->flush_type
!= no_flush
)
1845 consumed
= format_and_pad_commit(sb
, placeholder
, context
);
1847 consumed
= format_commit_one(sb
, placeholder
, context
);
1848 if (magic
== NO_MAGIC
)
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
, " ");
1860 return consumed
+ 1;
1863 static size_t userformat_want_item(struct strbuf
*sb UNUSED
,
1864 const char *placeholder
,
1867 struct userformat_want
*w
= context
;
1869 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1872 switch (*placeholder
) {
1887 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1889 struct strbuf dummy
= STRBUF_INIT
;
1896 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1897 strbuf_release(&dummy
);
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
)
1905 struct format_commit_context context
= {
1908 .pretty_ctx
= pretty_ctx
,
1909 .wrap_start
= sb
->len
1911 const char *output_enc
= pretty_ctx
->output_encoding
;
1912 const char *utf8
= "UTF-8";
1914 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1915 rewrap_message_tail(sb
, &context
, 0, 0, 0);
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.
1923 if (same_encoding(utf8
, output_enc
))
1926 if (context
.commit_encoding
&&
1927 !same_encoding(context
.commit_encoding
, utf8
))
1928 output_enc
= context
.commit_encoding
;
1933 char *out
= reencode_string_len(sb
->buf
, sb
->len
,
1934 output_enc
, utf8
, &outsz
);
1936 strbuf_attach(sb
, out
, outsz
, outsz
+ 1);
1939 free(context
.commit_encoding
);
1940 repo_unuse_commit_buffer(r
, commit
, context
.message
);
1943 static void pp_header(struct pretty_print_context
*pp
,
1944 const char *encoding
,
1945 const struct commit
*commit
,
1949 int parents_shown
= 0;
1952 const char *name
, *line
= *msg_p
;
1953 int linelen
= get_one_line(*msg_p
);
1963 if (pp
->fmt
== CMIT_FMT_RAW
) {
1964 strbuf_add(sb
, line
, linelen
);
1968 if (starts_with(line
, "parent ")) {
1969 if (linelen
!= the_hash_algo
->hexsz
+ 8)
1970 die("bad parent line in commit");
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
);
1983 * MEDIUM == DEFAULT shows only author with dates.
1984 * FULL shows both authors but not dates.
1985 * FULLER shows both authors and dates.
1987 if (skip_prefix(line
, "author ", &name
)) {
1988 strbuf_grow(sb
, linelen
+ 80);
1989 pp_user_info(pp
, "Author", sb
, name
, encoding
);
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
);
1999 void pp_title_line(struct pretty_print_context
*pp
,
2002 const char *encoding
,
2005 static const int max_length
= 78; /* per rfc2047 */
2006 struct strbuf title
;
2008 strbuf_init(&title
, 80);
2009 *msg_p
= format_subject(&title
, *msg_p
,
2010 pp
->preserve_subject
? "\n" : " ");
2012 strbuf_grow(sb
, title
.len
+ 1024);
2013 if (pp
->print_email_subject
) {
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
);
2021 strbuf_add_wrapped_bytes(sb
, title
.buf
, title
.len
,
2022 -last_line_length(sb
), 1, max_length
);
2024 strbuf_addbuf(sb
, &title
);
2026 strbuf_addch(sb
, '\n');
2028 if (need_8bit_cte
== 0) {
2030 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
2031 if (has_non_ascii(pp
->in_body_headers
.items
[i
].string
)) {
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
);
2045 if (pp
->after_subject
) {
2046 strbuf_addstr(sb
, pp
->after_subject
);
2048 if (cmit_fmt_is_mail(pp
->fmt
)) {
2049 strbuf_addch(sb
, '\n');
2052 if (pp
->in_body_headers
.nr
) {
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
);
2058 string_list_clear(&pp
->in_body_headers
, 0);
2059 strbuf_addch(sb
, '\n');
2062 strbuf_release(&title
);
2065 static int pp_utf8_width(const char *start
, const char *end
)
2068 size_t remain
= end
- start
;
2071 int n
= utf8_width(&start
, &remain
);
2072 if (n
< 0 || !start
)
2079 static void strbuf_add_tabexpand(struct strbuf
*sb
, struct grep_opt
*opt
,
2080 int color
, int tabwidth
, const char *line
,
2085 while ((tab
= memchr(line
, '\t', linelen
)) != NULL
) {
2086 int width
= pp_utf8_width(line
, tab
);
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.
2097 /* Output the data .. */
2098 append_line_with_color(sb
, opt
, line
, tab
- line
, color
,
2100 GREP_HEADER_FIELD_MAX
);
2102 /* .. and the de-tabified tab */
2103 strbuf_addchars(sb
, ' ', tabwidth
- (width
% tabwidth
));
2105 /* Skip over the printed part .. */
2106 linelen
-= tab
+ 1 - line
;
2111 * Print out everything after the last tab without
2112 * worrying about width - there's nothing more to
2115 append_line_with_color(sb
, opt
, line
, linelen
, color
, GREP_CONTEXT_BODY
,
2116 GREP_HEADER_FIELD_MAX
);
2120 * pp_handle_indent() prints out the intendation, and
2121 * the whole line (without the final newline), after
2124 static void pp_handle_indent(struct pretty_print_context
*pp
,
2125 struct strbuf
*sb
, int indent
,
2126 const char *line
, int linelen
)
2128 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
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
,
2135 append_line_with_color(sb
, opt
, line
, linelen
, pp
->color
,
2137 GREP_HEADER_FIELD_MAX
);
2140 static int is_mboxrd_from(const char *line
, int len
)
2143 * a line matching /^From $/ here would only have len == 4
2144 * at this point because is_empty_line would've trimmed all
2147 return len
> 4 && starts_with(line
+ strspn(line
, ">"), "From ");
2150 void pp_remainder(struct pretty_print_context
*pp
,
2155 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
2159 const char *line
= *msg_p
;
2160 int linelen
= get_one_line(line
);
2166 if (is_blank_line(line
, &linelen
)) {
2169 if (pp
->fmt
== CMIT_FMT_SHORT
)
2174 strbuf_grow(sb
, linelen
+ indent
+ 20);
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
,
2182 if (pp
->fmt
== CMIT_FMT_MBOXRD
&&
2183 is_mboxrd_from(line
, linelen
))
2184 strbuf_addch(sb
, '>');
2186 append_line_with_color(sb
, opt
, line
, linelen
,
2187 pp
->color
, GREP_CONTEXT_BODY
,
2188 GREP_HEADER_FIELD_MAX
);
2190 strbuf_addch(sb
, '\n');
2194 void pretty_print_commit(struct pretty_print_context
*pp
,
2195 const struct commit
*commit
,
2198 unsigned long beginning_of_body
;
2201 const char *reencoded
;
2202 const char *encoding
;
2203 int need_8bit_cte
= pp
->need_8bit_cte
;
2205 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
2206 format_commit_message(commit
, user_format
, sb
, pp
);
2210 encoding
= get_log_output_encoding();
2211 msg
= reencoded
= logmsg_reencode(commit
, NULL
, encoding
);
2213 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2217 * We need to check and emit Content-type: to mark it
2218 * as 8-bit if we haven't done so.
2220 if (cmit_fmt_is_mail(pp
->fmt
) && need_8bit_cte
== 0) {
2223 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
2225 /* author could be non 7-bit ASCII but
2226 * the log may be so; skip over the
2227 * header part first.
2229 if (ch
== '\n' && msg
[i
+1] == '\n')
2232 else if (non_ascii(ch
)) {
2239 pp_header(pp
, encoding
, commit
, &msg
, sb
);
2240 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->print_email_subject
) {
2241 strbuf_addch(sb
, '\n');
2244 /* Skip excess blank lines at the beginning of body, if any... */
2245 msg
= skip_blank_lines(msg
);
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
);
2251 beginning_of_body
= sb
->len
;
2252 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2253 pp_remainder(pp
, &msg
, sb
, indent
);
2256 /* Make sure there is an EOLN for the non-oneline case */
2257 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2258 strbuf_addch(sb
, '\n');
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.
2265 if (cmit_fmt_is_mail(pp
->fmt
) && sb
->len
<= beginning_of_body
)
2266 strbuf_addch(sb
, '\n');
2268 unuse_commit_buffer(commit
, reencoded
);
2271 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
2274 struct pretty_print_context pp
= {0};
2276 pretty_print_commit(&pp
, commit
, sb
);