7 #include "string-list.h"
12 #include "reflog-walk.h"
13 #include "gpg-interface.h"
15 #include "run-command.h"
17 static char *user_format
;
18 static struct cmt_fmt_map
{
22 int expand_tabs_in_log
;
24 enum date_mode_type default_date_mode_type
;
25 const char *user_format
;
27 static size_t builtin_formats_len
;
28 static size_t commit_formats_len
;
29 static size_t commit_formats_alloc
;
30 static struct cmt_fmt_map
*find_commit_format(const char *sought
);
32 int commit_format_is_empty(enum cmit_fmt fmt
)
34 return fmt
== CMIT_FMT_USERFORMAT
&& !*user_format
;
37 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
40 user_format
= xstrdup(cp
);
42 rev
->use_terminator
= 1;
43 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
46 static int git_pretty_formats_config(const char *var
, const char *value
, void *cb
)
48 struct cmt_fmt_map
*commit_format
= NULL
;
53 if (!skip_prefix(var
, "pretty.", &name
))
56 for (i
= 0; i
< builtin_formats_len
; i
++) {
57 if (!strcmp(commit_formats
[i
].name
, name
))
61 for (i
= builtin_formats_len
; i
< commit_formats_len
; i
++) {
62 if (!strcmp(commit_formats
[i
].name
, name
)) {
63 commit_format
= &commit_formats
[i
];
69 ALLOC_GROW(commit_formats
, commit_formats_len
+1,
70 commit_formats_alloc
);
71 commit_format
= &commit_formats
[commit_formats_len
];
72 memset(commit_format
, 0, sizeof(*commit_format
));
76 commit_format
->name
= xstrdup(name
);
77 commit_format
->format
= CMIT_FMT_USERFORMAT
;
78 if (git_config_string(&fmt
, var
, value
))
81 if (skip_prefix(fmt
, "format:", &fmt
))
82 commit_format
->is_tformat
= 0;
83 else if (skip_prefix(fmt
, "tformat:", &fmt
) || strchr(fmt
, '%'))
84 commit_format
->is_tformat
= 1;
86 commit_format
->is_alias
= 1;
87 commit_format
->user_format
= fmt
;
92 static void setup_commit_formats(void)
94 struct cmt_fmt_map builtin_formats
[] = {
95 { "raw", CMIT_FMT_RAW
, 0, 0 },
96 { "medium", CMIT_FMT_MEDIUM
, 0, 8 },
97 { "short", CMIT_FMT_SHORT
, 0, 0 },
98 { "email", CMIT_FMT_EMAIL
, 0, 0 },
99 { "mboxrd", CMIT_FMT_MBOXRD
, 0, 0 },
100 { "fuller", CMIT_FMT_FULLER
, 0, 8 },
101 { "full", CMIT_FMT_FULL
, 0, 8 },
102 { "oneline", CMIT_FMT_ONELINE
, 1, 0 },
103 { "reference", CMIT_FMT_USERFORMAT
, 1, 0,
104 0, DATE_SHORT
, "%C(auto)%h (%s, %ad)" },
106 * Please update $__git_log_pretty_formats in
107 * git-completion.bash when you add new formats.
110 commit_formats_len
= ARRAY_SIZE(builtin_formats
);
111 builtin_formats_len
= commit_formats_len
;
112 ALLOC_GROW(commit_formats
, commit_formats_len
, commit_formats_alloc
);
113 COPY_ARRAY(commit_formats
, builtin_formats
,
114 ARRAY_SIZE(builtin_formats
));
116 git_config(git_pretty_formats_config
, NULL
);
119 static struct cmt_fmt_map
*find_commit_format_recursive(const char *sought
,
120 const char *original
,
121 int num_redirections
)
123 struct cmt_fmt_map
*found
= NULL
;
124 size_t found_match_len
= 0;
127 if (num_redirections
>= commit_formats_len
)
128 die("invalid --pretty format: "
129 "'%s' references an alias which points to itself",
132 for (i
= 0; i
< commit_formats_len
; i
++) {
135 if (!starts_with(commit_formats
[i
].name
, sought
))
138 match_len
= strlen(commit_formats
[i
].name
);
139 if (found
== NULL
|| found_match_len
> match_len
) {
140 found
= &commit_formats
[i
];
141 found_match_len
= match_len
;
145 if (found
&& found
->is_alias
) {
146 found
= find_commit_format_recursive(found
->user_format
,
154 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
157 setup_commit_formats();
159 return find_commit_format_recursive(sought
, sought
, 0);
162 void get_commit_format(const char *arg
, struct rev_info
*rev
)
164 struct cmt_fmt_map
*commit_format
;
166 rev
->use_terminator
= 0;
168 rev
->commit_format
= CMIT_FMT_DEFAULT
;
171 if (skip_prefix(arg
, "format:", &arg
)) {
172 save_user_format(rev
, arg
, 0);
176 if (!*arg
|| skip_prefix(arg
, "tformat:", &arg
) || strchr(arg
, '%')) {
177 save_user_format(rev
, arg
, 1);
181 commit_format
= find_commit_format(arg
);
183 die("invalid --pretty format: %s", arg
);
185 rev
->commit_format
= commit_format
->format
;
186 rev
->use_terminator
= commit_format
->is_tformat
;
187 rev
->expand_tabs_in_log_default
= commit_format
->expand_tabs_in_log
;
188 if (!rev
->date_mode_explicit
&& commit_format
->default_date_mode_type
)
189 rev
->date_mode
.type
= commit_format
->default_date_mode_type
;
190 if (commit_format
->format
== CMIT_FMT_USERFORMAT
) {
191 save_user_format(rev
, commit_format
->user_format
,
192 commit_format
->is_tformat
);
197 * Generic support for pretty-printing the header
199 static int get_one_line(const char *msg
)
214 /* High bit set, or ISO-2022-INT */
215 static int non_ascii(int ch
)
217 return !isascii(ch
) || ch
== '\033';
220 int has_non_ascii(const char *s
)
225 while ((ch
= *s
++) != '\0') {
232 static int is_rfc822_special(char ch
)
254 static int needs_rfc822_quoting(const char *s
, int len
)
257 for (i
= 0; i
< len
; i
++)
258 if (is_rfc822_special(s
[i
]))
263 static int last_line_length(struct strbuf
*sb
)
267 /* How many bytes are already used on the last line? */
268 for (i
= sb
->len
- 1; i
>= 0; i
--)
269 if (sb
->buf
[i
] == '\n')
271 return sb
->len
- (i
+ 1);
274 static void add_rfc822_quoted(struct strbuf
*out
, const char *s
, int len
)
278 /* just a guess, we may have to also backslash-quote */
279 strbuf_grow(out
, len
+ 2);
281 strbuf_addch(out
, '"');
282 for (i
= 0; i
< len
; i
++) {
286 strbuf_addch(out
, '\\');
289 strbuf_addch(out
, s
[i
]);
292 strbuf_addch(out
, '"');
300 static int is_rfc2047_special(char ch
, enum rfc2047_type type
)
303 * rfc2047, section 4.2:
305 * 8-bit values which correspond to printable ASCII characters other
306 * than "=", "?", and "_" (underscore), MAY be represented as those
307 * characters. (But see section 5 for restrictions.) In
308 * particular, SPACE and TAB MUST NOT be represented as themselves
309 * within encoded words.
313 * rule out non-ASCII characters and non-printable characters (the
314 * non-ASCII check should be redundant as isprint() is not localized
315 * and only knows about ASCII, but be defensive about that)
317 if (non_ascii(ch
) || !isprint(ch
))
321 * rule out special printable characters (' ' should be the only
322 * whitespace character considered printable, but be defensive and use
325 if (isspace(ch
) || ch
== '=' || ch
== '?' || ch
== '_')
329 * rfc2047, section 5.3:
331 * As a replacement for a 'word' entity within a 'phrase', for example,
332 * one that precedes an address in a From, To, or Cc header. The ABNF
333 * definition for 'phrase' from RFC 822 thus becomes:
335 * phrase = 1*( encoded-word / word )
337 * In this case the set of characters that may be used in a "Q"-encoded
338 * 'encoded-word' is restricted to: <upper and lower case ASCII
339 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
340 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
341 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
342 * 'special' by 'linear-white-space'.
345 if (type
!= RFC2047_ADDRESS
)
348 /* '=' and '_' are special cases and have been checked above */
349 return !(isalnum(ch
) || ch
== '!' || ch
== '*' || ch
== '+' || ch
== '-' || ch
== '/');
352 static int needs_rfc2047_encoding(const char *line
, int len
)
356 for (i
= 0; i
< len
; i
++) {
358 if (non_ascii(ch
) || ch
== '\n')
360 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
367 static void add_rfc2047(struct strbuf
*sb
, const char *line
, size_t len
,
368 const char *encoding
, enum rfc2047_type type
)
370 static const int max_encoded_length
= 76; /* per rfc2047 */
372 int line_len
= last_line_length(sb
);
374 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
375 strbuf_addf(sb
, "=?%s?q?", encoding
);
376 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
380 * RFC 2047, section 5 (3):
382 * Each 'encoded-word' MUST represent an integral number of
383 * characters. A multi-octet character may not be split across
384 * adjacent 'encoded- word's.
386 const unsigned char *p
= (const unsigned char *)line
;
387 int chrlen
= mbs_chrlen(&line
, &len
, encoding
);
388 int is_special
= (chrlen
> 1) || is_rfc2047_special(*p
, type
);
390 /* "=%02X" * chrlen, or the byte itself */
391 const char *encoded_fmt
= is_special
? "=%02X" : "%c";
392 int encoded_len
= is_special
? 3 * chrlen
: 1;
395 * According to RFC 2047, we could encode the special character
396 * ' ' (space) with '_' (underscore) for readability. But many
397 * programs do not understand this and just leave the
398 * underscore in place. Thus, we do nothing special here, which
399 * causes ' ' to be encoded as '=20', avoiding this problem.
402 if (line_len
+ encoded_len
+ 2 > max_encoded_length
) {
403 /* It won't fit with trailing "?=" --- break the line */
404 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
405 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
408 for (i
= 0; i
< chrlen
; i
++)
409 strbuf_addf(sb
, encoded_fmt
, p
[i
]);
410 line_len
+= encoded_len
;
412 strbuf_addstr(sb
, "?=");
415 const char *show_ident_date(const struct ident_split
*ident
,
416 const struct date_mode
*mode
)
418 timestamp_t date
= 0;
421 if (ident
->date_begin
&& ident
->date_end
)
422 date
= parse_timestamp(ident
->date_begin
, NULL
, 10);
423 if (date_overflows(date
))
426 if (ident
->tz_begin
&& ident
->tz_end
)
427 tz
= strtol(ident
->tz_begin
, NULL
, 10);
428 if (tz
>= INT_MAX
|| tz
<= INT_MIN
)
431 return show_date(date
, tz
, mode
);
434 static inline void strbuf_add_with_color(struct strbuf
*sb
, const char *color
,
435 const char *buf
, size_t buflen
)
437 strbuf_addstr(sb
, color
);
438 strbuf_add(sb
, buf
, buflen
);
440 strbuf_addstr(sb
, GIT_COLOR_RESET
);
443 static void append_line_with_color(struct strbuf
*sb
, struct grep_opt
*opt
,
444 const char *line
, size_t linelen
,
445 int color
, enum grep_context ctx
,
446 enum grep_header_field field
)
448 const char *buf
, *eol
, *line_color
, *match_color
;
455 if (!opt
|| !want_color(color
) || opt
->invert
)
458 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
459 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
461 while (grep_next_match(opt
, buf
, eol
, ctx
, &match
, field
, eflags
)) {
462 if (match
.rm_so
== match
.rm_eo
)
465 strbuf_add_with_color(sb
, line_color
, buf
, match
.rm_so
);
466 strbuf_add_with_color(sb
, match_color
, buf
+ match
.rm_so
,
467 match
.rm_eo
- match
.rm_so
);
473 strbuf_add_with_color(sb
, line_color
, buf
, eol
- buf
);
476 strbuf_add(sb
, buf
, eol
- buf
);
480 void pp_user_info(struct pretty_print_context
*pp
,
481 const char *what
, struct strbuf
*sb
,
482 const char *line
, const char *encoding
)
484 struct ident_split ident
;
486 const char *mailbuf
, *namebuf
;
487 size_t namelen
, maillen
;
488 int max_length
= 78; /* per rfc2822 */
490 if (pp
->fmt
== CMIT_FMT_ONELINE
)
493 line_end
= strchrnul(line
, '\n');
494 if (split_ident_line(&ident
, line
, line_end
- line
))
497 mailbuf
= ident
.mail_begin
;
498 maillen
= ident
.mail_end
- ident
.mail_begin
;
499 namebuf
= ident
.name_begin
;
500 namelen
= ident
.name_end
- ident
.name_begin
;
503 map_user(pp
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
505 if (cmit_fmt_is_mail(pp
->fmt
)) {
506 if (pp
->from_ident
&& ident_cmp(pp
->from_ident
, &ident
)) {
507 struct strbuf buf
= STRBUF_INIT
;
509 strbuf_addstr(&buf
, "From: ");
510 strbuf_add(&buf
, namebuf
, namelen
);
511 strbuf_addstr(&buf
, " <");
512 strbuf_add(&buf
, mailbuf
, maillen
);
513 strbuf_addstr(&buf
, ">\n");
514 string_list_append(&pp
->in_body_headers
,
515 strbuf_detach(&buf
, NULL
));
517 mailbuf
= pp
->from_ident
->mail_begin
;
518 maillen
= pp
->from_ident
->mail_end
- mailbuf
;
519 namebuf
= pp
->from_ident
->name_begin
;
520 namelen
= pp
->from_ident
->name_end
- namebuf
;
523 strbuf_addstr(sb
, "From: ");
524 if (pp
->encode_email_headers
&&
525 needs_rfc2047_encoding(namebuf
, namelen
)) {
526 add_rfc2047(sb
, namebuf
, namelen
,
527 encoding
, RFC2047_ADDRESS
);
528 max_length
= 76; /* per rfc2047 */
529 } else if (needs_rfc822_quoting(namebuf
, namelen
)) {
530 struct strbuf quoted
= STRBUF_INIT
;
531 add_rfc822_quoted("ed
, namebuf
, namelen
);
532 strbuf_add_wrapped_bytes(sb
, quoted
.buf
, quoted
.len
,
534 strbuf_release("ed
);
536 strbuf_add_wrapped_bytes(sb
, namebuf
, namelen
,
541 last_line_length(sb
) + strlen(" <") + maillen
+ strlen(">"))
542 strbuf_addch(sb
, '\n');
543 strbuf_addf(sb
, " <%.*s>\n", (int)maillen
, mailbuf
);
545 struct strbuf id
= STRBUF_INIT
;
546 enum grep_header_field field
= GREP_HEADER_FIELD_MAX
;
547 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
549 if (!strcmp(what
, "Author"))
550 field
= GREP_HEADER_AUTHOR
;
551 else if (!strcmp(what
, "Commit"))
552 field
= GREP_HEADER_COMMITTER
;
554 strbuf_addf(sb
, "%s: ", what
);
555 if (pp
->fmt
== CMIT_FMT_FULLER
)
556 strbuf_addchars(sb
, ' ', 4);
558 strbuf_addf(&id
, "%.*s <%.*s>", (int)namelen
, namebuf
,
559 (int)maillen
, mailbuf
);
561 append_line_with_color(sb
, opt
, id
.buf
, id
.len
, pp
->color
,
562 GREP_CONTEXT_HEAD
, field
);
563 strbuf_addch(sb
, '\n');
568 case CMIT_FMT_MEDIUM
:
569 strbuf_addf(sb
, "Date: %s\n",
570 show_ident_date(&ident
, &pp
->date_mode
));
573 case CMIT_FMT_MBOXRD
:
574 strbuf_addf(sb
, "Date: %s\n",
575 show_ident_date(&ident
, DATE_MODE(RFC2822
)));
577 case CMIT_FMT_FULLER
:
578 strbuf_addf(sb
, "%sDate: %s\n", what
,
579 show_ident_date(&ident
, &pp
->date_mode
));
587 static int is_blank_line(const char *line
, int *len_p
)
590 while (len
&& isspace(line
[len
- 1]))
596 const char *skip_blank_lines(const char *msg
)
599 int linelen
= get_one_line(msg
);
603 if (!is_blank_line(msg
, &ll
))
610 static void add_merge_info(const struct pretty_print_context
*pp
,
611 struct strbuf
*sb
, const struct commit
*commit
)
613 struct commit_list
*parent
= commit
->parents
;
615 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (cmit_fmt_is_mail(pp
->fmt
)) ||
616 !parent
|| !parent
->next
)
619 strbuf_addstr(sb
, "Merge:");
622 struct object_id
*oidp
= &parent
->item
->object
.oid
;
623 strbuf_addch(sb
, ' ');
625 strbuf_add_unique_abbrev(sb
, oidp
, pp
->abbrev
);
627 strbuf_addstr(sb
, oid_to_hex(oidp
));
628 parent
= parent
->next
;
630 strbuf_addch(sb
, '\n');
633 static char *get_header(const char *msg
, const char *key
)
636 const char *v
= find_commit_header(msg
, key
, &len
);
637 return v
? xmemdupz(v
, len
) : NULL
;
640 static char *replace_encoding_header(char *buf
, const char *encoding
)
642 struct strbuf tmp
= STRBUF_INIT
;
646 /* guess if there is an encoding header before a \n\n */
647 while (!starts_with(cp
, "encoding ")) {
648 cp
= strchr(cp
, '\n');
649 if (!cp
|| *++cp
== '\n')
653 cp
= strchr(cp
, '\n');
655 return buf
; /* should not happen but be defensive */
656 len
= cp
+ 1 - (buf
+ start
);
658 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
659 if (is_encoding_utf8(encoding
)) {
660 /* we have re-coded to UTF-8; drop the header */
661 strbuf_remove(&tmp
, start
, len
);
663 /* just replaces XXXX in 'encoding XXXX\n' */
664 strbuf_splice(&tmp
, start
+ strlen("encoding "),
665 len
- strlen("encoding \n"),
666 encoding
, strlen(encoding
));
668 return strbuf_detach(&tmp
, NULL
);
671 const char *repo_logmsg_reencode(struct repository
*r
,
672 const struct commit
*commit
,
673 char **commit_encoding
,
674 const char *output_encoding
)
676 static const char *utf8
= "UTF-8";
677 const char *use_encoding
;
679 const char *msg
= repo_get_commit_buffer(r
, commit
, NULL
);
682 if (!output_encoding
|| !*output_encoding
) {
684 *commit_encoding
= get_header(msg
, "encoding");
687 encoding
= get_header(msg
, "encoding");
689 *commit_encoding
= encoding
;
690 use_encoding
= encoding
? encoding
: utf8
;
691 if (same_encoding(use_encoding
, output_encoding
)) {
693 * No encoding work to be done. If we have no encoding header
694 * at all, then there's nothing to do, and we can return the
695 * message verbatim (whether newly allocated or not).
701 * Otherwise, we still want to munge the encoding header in the
702 * result, which will be done by modifying the buffer. If we
703 * are using a fresh copy, we can reuse it. But if we are using
704 * the cached copy from get_commit_buffer, we need to duplicate it
705 * to avoid munging the cached copy.
707 if (msg
== get_cached_commit_buffer(r
, commit
, NULL
))
714 * There's actual encoding work to do. Do the reencoding, which
715 * still leaves the header to be replaced in the next step. At
716 * this point, we are done with msg. If we allocated a fresh
717 * copy, we can free it.
719 out
= reencode_string(msg
, output_encoding
, use_encoding
);
721 repo_unuse_commit_buffer(r
, commit
, msg
);
725 * This replacement actually consumes the buffer we hand it, so we do
726 * not have to worry about freeing the old "out" here.
729 out
= replace_encoding_header(out
, output_encoding
);
731 if (!commit_encoding
)
734 * If the re-encoding failed, out might be NULL here; in that
735 * case we just return the commit message verbatim.
737 return out
? out
: msg
;
740 static int mailmap_name(const char **email
, size_t *email_len
,
741 const char **name
, size_t *name_len
)
743 static struct string_list
*mail_map
;
745 CALLOC_ARRAY(mail_map
, 1);
746 read_mailmap(mail_map
);
748 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
751 static size_t format_person_part(struct strbuf
*sb
, char part
,
752 const char *msg
, int len
,
753 const struct date_mode
*dmode
)
755 /* currently all placeholders have same length */
756 const int placeholder_len
= 2;
757 struct ident_split s
;
758 const char *name
, *mail
;
759 size_t maillen
, namelen
;
761 if (split_ident_line(&s
, msg
, len
) < 0)
765 namelen
= s
.name_end
- s
.name_begin
;
767 maillen
= s
.mail_end
- s
.mail_begin
;
769 if (part
== 'N' || part
== 'E' || part
== 'L') /* mailmap lookup */
770 mailmap_name(&mail
, &maillen
, &name
, &namelen
);
771 if (part
== 'n' || part
== 'N') { /* name */
772 strbuf_add(sb
, name
, namelen
);
773 return placeholder_len
;
775 if (part
== 'e' || part
== 'E') { /* email */
776 strbuf_add(sb
, mail
, maillen
);
777 return placeholder_len
;
779 if (part
== 'l' || part
== 'L') { /* local-part */
780 const char *at
= memchr(mail
, '@', maillen
);
783 strbuf_add(sb
, mail
, maillen
);
784 return placeholder_len
;
790 if (part
== 't') { /* date, UNIX timestamp */
791 strbuf_add(sb
, s
.date_begin
, s
.date_end
- s
.date_begin
);
792 return placeholder_len
;
797 strbuf_addstr(sb
, show_ident_date(&s
, dmode
));
798 return placeholder_len
;
799 case 'D': /* date, RFC2822 style */
800 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(RFC2822
)));
801 return placeholder_len
;
802 case 'r': /* date, relative */
803 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(RELATIVE
)));
804 return placeholder_len
;
805 case 'i': /* date, ISO 8601-like */
806 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(ISO8601
)));
807 return placeholder_len
;
808 case 'I': /* date, ISO 8601 strict */
809 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(ISO8601_STRICT
)));
810 return placeholder_len
;
811 case 'h': /* date, human */
812 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(HUMAN
)));
813 return placeholder_len
;
815 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(SHORT
)));
816 return placeholder_len
;
821 * reading from either a bogus commit, or a reflog entry with
822 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
823 * to compute a valid return value.
825 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
826 || part
== 'D' || part
== 'r' || part
== 'i')
827 return placeholder_len
;
829 return 0; /* unknown placeholder */
841 flush_left_and_steal
,
852 struct format_commit_context
{
853 struct repository
*repository
;
854 const struct commit
*commit
;
855 const struct pretty_print_context
*pretty_ctx
;
856 unsigned commit_header_parsed
:1;
857 unsigned commit_message_parsed
:1;
858 struct signature_check signature_check
;
859 enum flush_type flush_type
;
860 enum trunc_type truncate
;
862 char *commit_encoding
;
863 size_t width
, indent1
, indent2
;
867 /* These offsets are relative to the start of the commit message. */
869 struct chunk committer
;
874 /* The following ones are relative to the result struct strbuf. */
878 static void parse_commit_header(struct format_commit_context
*context
)
880 const char *msg
= context
->message
;
883 for (i
= 0; msg
[i
]; i
++) {
886 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
891 } else if (skip_prefix(msg
+ i
, "author ", &name
)) {
892 context
->author
.off
= name
- msg
;
893 context
->author
.len
= msg
+ eol
- name
;
894 } else if (skip_prefix(msg
+ i
, "committer ", &name
)) {
895 context
->committer
.off
= name
- msg
;
896 context
->committer
.len
= msg
+ eol
- name
;
900 context
->message_off
= i
;
901 context
->commit_header_parsed
= 1;
904 static int istitlechar(char c
)
906 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
907 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
910 void format_sanitized_subject(struct strbuf
*sb
, const char *msg
, size_t len
)
913 size_t start_len
= sb
->len
;
917 for (i
= 0; i
< len
; i
++) {
918 if (istitlechar(msg
[i
])) {
920 strbuf_addch(sb
, '-');
922 strbuf_addch(sb
, msg
[i
]);
924 while (msg
[i
+1] == '.')
930 /* trim any trailing '.' or '-' characters */
932 while (sb
->len
- trimlen
> start_len
&&
933 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
934 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
936 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
939 const char *format_subject(struct strbuf
*sb
, const char *msg
,
940 const char *line_separator
)
945 const char *line
= msg
;
946 int linelen
= get_one_line(line
);
949 if (!linelen
|| is_blank_line(line
, &linelen
))
954 strbuf_grow(sb
, linelen
+ 2);
956 strbuf_addstr(sb
, line_separator
);
957 strbuf_add(sb
, line
, linelen
);
963 static void parse_commit_message(struct format_commit_context
*c
)
965 const char *msg
= c
->message
+ c
->message_off
;
966 const char *start
= c
->message
;
968 msg
= skip_blank_lines(msg
);
969 c
->subject_off
= msg
- start
;
971 msg
= format_subject(NULL
, msg
, NULL
);
972 msg
= skip_blank_lines(msg
);
973 c
->body_off
= msg
- start
;
975 c
->commit_message_parsed
= 1;
978 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
979 size_t width
, size_t indent1
, size_t indent2
)
981 struct strbuf tmp
= STRBUF_INIT
;
984 strbuf_add(&tmp
, sb
->buf
, pos
);
985 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
986 (int) indent1
, (int) indent2
, (int) width
);
987 strbuf_swap(&tmp
, sb
);
988 strbuf_release(&tmp
);
991 static void rewrap_message_tail(struct strbuf
*sb
,
992 struct format_commit_context
*c
,
993 size_t new_width
, size_t new_indent1
,
996 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
997 c
->indent2
== new_indent2
)
999 if (c
->wrap_start
< sb
->len
)
1000 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
1001 c
->wrap_start
= sb
->len
;
1002 c
->width
= new_width
;
1003 c
->indent1
= new_indent1
;
1004 c
->indent2
= new_indent2
;
1007 static int format_reflog_person(struct strbuf
*sb
,
1009 struct reflog_walk_info
*log
,
1010 const struct date_mode
*dmode
)
1017 ident
= get_reflog_ident(log
);
1021 return format_person_part(sb
, part
, ident
, strlen(ident
), dmode
);
1024 static size_t parse_color(struct strbuf
*sb
, /* in UTF-8 */
1025 const char *placeholder
,
1026 struct format_commit_context
*c
)
1028 const char *rest
= placeholder
;
1029 const char *basic_color
= NULL
;
1031 if (placeholder
[1] == '(') {
1032 const char *begin
= placeholder
+ 2;
1033 const char *end
= strchr(begin
, ')');
1034 char color
[COLOR_MAXLEN
];
1039 if (skip_prefix(begin
, "auto,", &begin
)) {
1040 if (!want_color(c
->pretty_ctx
->color
))
1041 return end
- placeholder
+ 1;
1042 } else if (skip_prefix(begin
, "always,", &begin
)) {
1043 /* nothing to do; we do not respect want_color at all */
1045 /* the default is the same as "auto" */
1046 if (!want_color(c
->pretty_ctx
->color
))
1047 return end
- placeholder
+ 1;
1050 if (color_parse_mem(begin
, end
- begin
, color
) < 0)
1051 die(_("unable to parse --pretty format"));
1052 strbuf_addstr(sb
, color
);
1053 return end
- placeholder
+ 1;
1057 * We handle things like "%C(red)" above; for historical reasons, there
1058 * are a few colors that can be specified without parentheses (and
1059 * they cannot support things like "auto" or "always" at all).
1061 if (skip_prefix(placeholder
+ 1, "red", &rest
))
1062 basic_color
= GIT_COLOR_RED
;
1063 else if (skip_prefix(placeholder
+ 1, "green", &rest
))
1064 basic_color
= GIT_COLOR_GREEN
;
1065 else if (skip_prefix(placeholder
+ 1, "blue", &rest
))
1066 basic_color
= GIT_COLOR_BLUE
;
1067 else if (skip_prefix(placeholder
+ 1, "reset", &rest
))
1068 basic_color
= GIT_COLOR_RESET
;
1070 if (basic_color
&& want_color(c
->pretty_ctx
->color
))
1071 strbuf_addstr(sb
, basic_color
);
1073 return rest
- placeholder
;
1076 static size_t parse_padding_placeholder(const char *placeholder
,
1077 struct format_commit_context
*c
)
1079 const char *ch
= placeholder
;
1080 enum flush_type flush_type
;
1085 flush_type
= flush_right
;
1089 flush_type
= flush_both
;
1091 } else if (*ch
== '>') {
1092 flush_type
= flush_left_and_steal
;
1095 flush_type
= flush_left
;
1101 /* the next value means "wide enough to that column" */
1108 const char *start
= ch
+ 1;
1109 const char *end
= start
+ strcspn(start
, ",)");
1112 if (!end
|| end
== start
)
1114 width
= strtol(start
, &next
, 10);
1115 if (next
== start
|| width
== 0)
1119 width
+= term_columns();
1123 c
->padding
= to_column
? -width
: width
;
1124 c
->flush_type
= flush_type
;
1128 end
= strchr(start
, ')');
1129 if (!end
|| end
== start
)
1131 if (starts_with(start
, "trunc)"))
1132 c
->truncate
= trunc_right
;
1133 else if (starts_with(start
, "ltrunc)"))
1134 c
->truncate
= trunc_left
;
1135 else if (starts_with(start
, "mtrunc)"))
1136 c
->truncate
= trunc_middle
;
1140 c
->truncate
= trunc_none
;
1142 return end
- placeholder
+ 1;
1147 static int match_placeholder_arg_value(const char *to_parse
, const char *candidate
,
1148 const char **end
, const char **valuestart
,
1153 if (!(skip_prefix(to_parse
, candidate
, &p
)))
1157 *valuestart
= p
+ 1;
1158 *valuelen
= strcspn(*valuestart
, ",)");
1159 p
= *valuestart
+ *valuelen
;
1161 if (*p
!= ',' && *p
!= ')')
1178 static int match_placeholder_bool_arg(const char *to_parse
, const char *candidate
,
1179 const char **end
, int *val
)
1186 if (!match_placeholder_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
1194 strval
= xstrndup(argval
, arglen
);
1195 v
= git_parse_maybe_bool(strval
);
1206 static int format_trailer_match_cb(const struct strbuf
*key
, void *ud
)
1208 const struct string_list
*list
= ud
;
1209 const struct string_list_item
*item
;
1211 for_each_string_list_item (item
, list
) {
1212 if (key
->len
== (uintptr_t)item
->util
&&
1213 !strncasecmp(item
->string
, key
->buf
, key
->len
))
1219 int format_set_trailers_options(struct process_trailer_options
*opts
,
1220 struct string_list
*filter_list
,
1221 struct strbuf
*sepbuf
,
1222 struct strbuf
*kvsepbuf
,
1233 if (match_placeholder_arg_value(*arg
, "key", arg
, &argval
, &arglen
)) {
1234 uintptr_t len
= arglen
;
1239 if (len
&& argval
[len
- 1] == ':')
1241 string_list_append(filter_list
, argval
)->util
= (char *)len
;
1243 opts
->filter
= format_trailer_match_cb
;
1244 opts
->filter_data
= filter_list
;
1245 opts
->only_trailers
= 1;
1246 } else if (match_placeholder_arg_value(*arg
, "separator", arg
, &argval
, &arglen
)) {
1249 strbuf_reset(sepbuf
);
1250 fmt
= xstrndup(argval
, arglen
);
1251 strbuf_expand(sepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
1253 opts
->separator
= sepbuf
;
1254 } else if (match_placeholder_arg_value(*arg
, "key_value_separator", arg
, &argval
, &arglen
)) {
1257 strbuf_reset(kvsepbuf
);
1258 fmt
= xstrndup(argval
, arglen
);
1259 strbuf_expand(kvsepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
1261 opts
->key_value_separator
= kvsepbuf
;
1262 } else if (!match_placeholder_bool_arg(*arg
, "only", arg
, &opts
->only_trailers
) &&
1263 !match_placeholder_bool_arg(*arg
, "unfold", arg
, &opts
->unfold
) &&
1264 !match_placeholder_bool_arg(*arg
, "keyonly", arg
, &opts
->key_only
) &&
1265 !match_placeholder_bool_arg(*arg
, "valueonly", arg
, &opts
->value_only
)) {
1267 size_t len
= strcspn(*arg
, ",)");
1268 *invalid_arg
= xstrndup(*arg
, len
);
1276 static size_t parse_describe_args(const char *start
, struct strvec
*args
)
1278 const char *options
[] = { "match", "exclude" };
1279 const char *arg
= start
;
1282 const char *matched
= NULL
;
1287 for (i
= 0; i
< ARRAY_SIZE(options
); i
++) {
1288 if (match_placeholder_arg_value(arg
, options
[i
], &arg
,
1289 &argval
, &arglen
)) {
1290 matched
= options
[i
];
1299 strvec_pushf(args
, "--%s=%.*s", matched
, (int)arglen
, argval
);
1304 static size_t format_commit_one(struct strbuf
*sb
, /* in UTF-8 */
1305 const char *placeholder
,
1308 struct format_commit_context
*c
= context
;
1309 const struct commit
*commit
= c
->commit
;
1310 const char *msg
= c
->message
;
1311 struct commit_list
*p
;
1312 const char *arg
, *eol
;
1316 /* these are independent of the commit */
1317 res
= strbuf_expand_literal_cb(sb
, placeholder
, NULL
);
1321 switch (placeholder
[0]) {
1323 if (starts_with(placeholder
+ 1, "(auto)")) {
1324 c
->auto_color
= want_color(c
->pretty_ctx
->color
);
1325 if (c
->auto_color
&& sb
->len
)
1326 strbuf_addstr(sb
, GIT_COLOR_RESET
);
1327 return 7; /* consumed 7 bytes, "C(auto)" */
1329 int ret
= parse_color(sb
, placeholder
, c
);
1333 * Otherwise, we decided to treat %C<unknown>
1334 * as a literal string, and the previous
1335 * %C(auto) is still valid.
1340 if (placeholder
[1] == '(') {
1341 unsigned long width
= 0, indent1
= 0, indent2
= 0;
1343 const char *start
= placeholder
+ 2;
1344 const char *end
= strchr(start
, ')');
1348 width
= strtoul(start
, &next
, 10);
1350 indent1
= strtoul(next
+ 1, &next
, 10);
1352 indent2
= strtoul(next
+ 1,
1359 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
1360 return end
- placeholder
+ 1;
1366 return parse_padding_placeholder(placeholder
, c
);
1369 if (skip_prefix(placeholder
, "(describe", &arg
)) {
1370 struct child_process cmd
= CHILD_PROCESS_INIT
;
1371 struct strbuf out
= STRBUF_INIT
;
1372 struct strbuf err
= STRBUF_INIT
;
1373 struct pretty_print_describe_status
*describe_status
;
1375 describe_status
= c
->pretty_ctx
->describe_status
;
1376 if (describe_status
) {
1377 if (!describe_status
->max_invocations
)
1379 describe_status
->max_invocations
--;
1383 strvec_push(&cmd
.args
, "describe");
1387 arg
+= parse_describe_args(arg
, &cmd
.args
);
1391 child_process_clear(&cmd
);
1395 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1396 pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0);
1398 strbuf_addbuf(sb
, &out
);
1399 strbuf_release(&out
);
1400 strbuf_release(&err
);
1401 return arg
- placeholder
+ 1;
1404 /* these depend on the commit */
1405 if (!commit
->object
.parsed
)
1406 parse_object(the_repository
, &commit
->object
.oid
);
1408 switch (placeholder
[0]) {
1409 case 'H': /* commit hash */
1410 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_COMMIT
));
1411 strbuf_addstr(sb
, oid_to_hex(&commit
->object
.oid
));
1412 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_RESET
));
1414 case 'h': /* abbreviated commit hash */
1415 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_COMMIT
));
1416 strbuf_add_unique_abbrev(sb
, &commit
->object
.oid
,
1417 c
->pretty_ctx
->abbrev
);
1418 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_RESET
));
1420 case 'T': /* tree hash */
1421 strbuf_addstr(sb
, oid_to_hex(get_commit_tree_oid(commit
)));
1423 case 't': /* abbreviated tree hash */
1424 strbuf_add_unique_abbrev(sb
,
1425 get_commit_tree_oid(commit
),
1426 c
->pretty_ctx
->abbrev
);
1428 case 'P': /* parent hashes */
1429 for (p
= commit
->parents
; p
; p
= p
->next
) {
1430 if (p
!= commit
->parents
)
1431 strbuf_addch(sb
, ' ');
1432 strbuf_addstr(sb
, oid_to_hex(&p
->item
->object
.oid
));
1435 case 'p': /* abbreviated parent hashes */
1436 for (p
= commit
->parents
; p
; p
= p
->next
) {
1437 if (p
!= commit
->parents
)
1438 strbuf_addch(sb
, ' ');
1439 strbuf_add_unique_abbrev(sb
, &p
->item
->object
.oid
,
1440 c
->pretty_ctx
->abbrev
);
1443 case 'm': /* left/right/bottom */
1444 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
1447 format_decorations(sb
, commit
, c
->auto_color
);
1450 format_decorations_extended(sb
, commit
, c
->auto_color
, "", ", ", "");
1452 case 'S': /* tag/branch like --source */
1453 if (!(c
->pretty_ctx
->rev
&& c
->pretty_ctx
->rev
->sources
))
1455 slot
= revision_sources_at(c
->pretty_ctx
->rev
->sources
, commit
);
1456 if (!(slot
&& *slot
))
1458 strbuf_addstr(sb
, *slot
);
1460 case 'g': /* reflog info */
1461 switch(placeholder
[1]) {
1462 case 'd': /* reflog selector */
1464 if (c
->pretty_ctx
->reflog_info
)
1465 get_reflog_selector(sb
,
1466 c
->pretty_ctx
->reflog_info
,
1467 &c
->pretty_ctx
->date_mode
,
1468 c
->pretty_ctx
->date_mode_explicit
,
1469 (placeholder
[1] == 'd'));
1471 case 's': /* reflog message */
1472 if (c
->pretty_ctx
->reflog_info
)
1473 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
1479 return format_reflog_person(sb
,
1481 c
->pretty_ctx
->reflog_info
,
1482 &c
->pretty_ctx
->date_mode
);
1484 return 0; /* unknown %g placeholder */
1486 if (c
->pretty_ctx
->notes_message
) {
1487 strbuf_addstr(sb
, c
->pretty_ctx
->notes_message
);
1493 if (placeholder
[0] == 'G') {
1494 if (!c
->signature_check
.result
)
1495 check_commit_signature(c
->commit
, &(c
->signature_check
));
1496 switch (placeholder
[1]) {
1498 if (c
->signature_check
.output
)
1499 strbuf_addstr(sb
, c
->signature_check
.output
);
1502 switch (c
->signature_check
.result
) {
1504 switch (c
->signature_check
.trust_level
) {
1505 case TRUST_UNDEFINED
:
1507 strbuf_addch(sb
, 'U');
1510 strbuf_addch(sb
, 'G');
1520 strbuf_addch(sb
, c
->signature_check
.result
);
1524 if (c
->signature_check
.signer
)
1525 strbuf_addstr(sb
, c
->signature_check
.signer
);
1528 if (c
->signature_check
.key
)
1529 strbuf_addstr(sb
, c
->signature_check
.key
);
1532 if (c
->signature_check
.fingerprint
)
1533 strbuf_addstr(sb
, c
->signature_check
.fingerprint
);
1536 if (c
->signature_check
.primary_key_fingerprint
)
1537 strbuf_addstr(sb
, c
->signature_check
.primary_key_fingerprint
);
1540 switch (c
->signature_check
.trust_level
) {
1541 case TRUST_UNDEFINED
:
1542 strbuf_addstr(sb
, "undefined");
1545 strbuf_addstr(sb
, "never");
1547 case TRUST_MARGINAL
:
1548 strbuf_addstr(sb
, "marginal");
1551 strbuf_addstr(sb
, "fully");
1553 case TRUST_ULTIMATE
:
1554 strbuf_addstr(sb
, "ultimate");
1564 /* For the rest we have to parse the commit header. */
1565 if (!c
->commit_header_parsed
) {
1567 repo_logmsg_reencode(c
->repository
, commit
,
1568 &c
->commit_encoding
, "UTF-8");
1569 parse_commit_header(c
);
1572 switch (placeholder
[0]) {
1573 case 'a': /* author ... */
1574 return format_person_part(sb
, placeholder
[1],
1575 msg
+ c
->author
.off
, c
->author
.len
,
1576 &c
->pretty_ctx
->date_mode
);
1577 case 'c': /* committer ... */
1578 return format_person_part(sb
, placeholder
[1],
1579 msg
+ c
->committer
.off
, c
->committer
.len
,
1580 &c
->pretty_ctx
->date_mode
);
1581 case 'e': /* encoding */
1582 if (c
->commit_encoding
)
1583 strbuf_addstr(sb
, c
->commit_encoding
);
1585 case 'B': /* raw body */
1586 /* message_off is always left at the initial newline */
1587 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
1591 /* Now we need to parse the commit message. */
1592 if (!c
->commit_message_parsed
)
1593 parse_commit_message(c
);
1595 switch (placeholder
[0]) {
1596 case 's': /* subject */
1597 format_subject(sb
, msg
+ c
->subject_off
, " ");
1599 case 'f': /* sanitized subject */
1600 eol
= strchrnul(msg
+ c
->subject_off
, '\n');
1601 format_sanitized_subject(sb
, msg
+ c
->subject_off
, eol
- (msg
+ c
->subject_off
));
1603 case 'b': /* body */
1604 strbuf_addstr(sb
, msg
+ c
->body_off
);
1608 if (skip_prefix(placeholder
, "(trailers", &arg
)) {
1609 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
1610 struct string_list filter_list
= STRING_LIST_INIT_NODUP
;
1611 struct strbuf sepbuf
= STRBUF_INIT
;
1612 struct strbuf kvsepbuf
= STRBUF_INIT
;
1615 opts
.no_divider
= 1;
1619 if (format_set_trailers_options(&opts
, &filter_list
, &sepbuf
, &kvsepbuf
, &arg
, NULL
))
1623 format_trailers_from_commit(sb
, msg
+ c
->subject_off
, &opts
);
1624 ret
= arg
- placeholder
+ 1;
1627 string_list_clear(&filter_list
, 0);
1628 strbuf_release(&sepbuf
);
1632 return 0; /* unknown placeholder */
1635 static size_t format_and_pad_commit(struct strbuf
*sb
, /* in UTF-8 */
1636 const char *placeholder
,
1637 struct format_commit_context
*c
)
1639 struct strbuf local_sb
= STRBUF_INIT
;
1640 int total_consumed
= 0, len
, padding
= c
->padding
;
1642 const char *start
= strrchr(sb
->buf
, '\n');
1646 occupied
= utf8_strnwidth(start
, -1, 1);
1647 occupied
+= c
->pretty_ctx
->graph_width
;
1648 padding
= (-padding
) - occupied
;
1651 int modifier
= *placeholder
== 'C';
1652 int consumed
= format_commit_one(&local_sb
, placeholder
, c
);
1653 total_consumed
+= consumed
;
1658 placeholder
+= consumed
;
1659 if (*placeholder
!= '%')
1664 len
= utf8_strnwidth(local_sb
.buf
, -1, 1);
1666 if (c
->flush_type
== flush_left_and_steal
) {
1667 const char *ch
= sb
->buf
+ sb
->len
- 1;
1668 while (len
> padding
&& ch
> sb
->buf
) {
1675 /* check for trailing ansi sequences */
1679 while (ch
- p
< 10 && *p
!= '\033')
1682 ch
+ 1 - p
!= display_mode_esc_sequence_len(p
))
1685 * got a good ansi sequence, put it back to
1686 * local_sb as we're cutting sb
1688 strbuf_insert(&local_sb
, 0, p
, ch
+ 1 - p
);
1691 strbuf_setlen(sb
, ch
+ 1 - sb
->buf
);
1692 c
->flush_type
= flush_left
;
1695 if (len
> padding
) {
1696 switch (c
->truncate
) {
1698 strbuf_utf8_replace(&local_sb
,
1699 0, len
- (padding
- 2),
1703 strbuf_utf8_replace(&local_sb
,
1705 len
- (padding
- 2),
1709 strbuf_utf8_replace(&local_sb
,
1710 padding
- 2, len
- (padding
- 2),
1716 strbuf_addbuf(sb
, &local_sb
);
1718 int sb_len
= sb
->len
, offset
= 0;
1719 if (c
->flush_type
== flush_left
)
1720 offset
= padding
- len
;
1721 else if (c
->flush_type
== flush_both
)
1722 offset
= (padding
- len
) / 2;
1724 * we calculate padding in columns, now
1725 * convert it back to chars
1727 padding
= padding
- len
+ local_sb
.len
;
1728 strbuf_addchars(sb
, ' ', padding
);
1729 memcpy(sb
->buf
+ sb_len
+ offset
, local_sb
.buf
,
1732 strbuf_release(&local_sb
);
1733 c
->flush_type
= no_flush
;
1734 return total_consumed
;
1737 static size_t format_commit_item(struct strbuf
*sb
, /* in UTF-8 */
1738 const char *placeholder
,
1745 ADD_LF_BEFORE_NON_EMPTY
,
1746 DEL_LF_BEFORE_EMPTY
,
1747 ADD_SP_BEFORE_NON_EMPTY
1750 switch (placeholder
[0]) {
1752 magic
= DEL_LF_BEFORE_EMPTY
;
1755 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1758 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1763 if (magic
!= NO_MAGIC
)
1767 if (((struct format_commit_context
*)context
)->flush_type
!= no_flush
)
1768 consumed
= format_and_pad_commit(sb
, placeholder
, context
);
1770 consumed
= format_commit_one(sb
, placeholder
, context
);
1771 if (magic
== NO_MAGIC
)
1774 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
1775 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
1776 strbuf_setlen(sb
, sb
->len
- 1);
1777 } else if (orig_len
!= sb
->len
) {
1778 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1779 strbuf_insertstr(sb
, orig_len
, "\n");
1780 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1781 strbuf_insertstr(sb
, orig_len
, " ");
1783 return consumed
+ 1;
1786 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
1789 struct userformat_want
*w
= context
;
1791 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1794 switch (*placeholder
) {
1809 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1811 struct strbuf dummy
= STRBUF_INIT
;
1818 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1819 strbuf_release(&dummy
);
1822 void repo_format_commit_message(struct repository
*r
,
1823 const struct commit
*commit
,
1824 const char *format
, struct strbuf
*sb
,
1825 const struct pretty_print_context
*pretty_ctx
)
1827 struct format_commit_context context
= {
1830 .pretty_ctx
= pretty_ctx
,
1831 .wrap_start
= sb
->len
1833 const char *output_enc
= pretty_ctx
->output_encoding
;
1834 const char *utf8
= "UTF-8";
1836 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1837 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1840 * Convert output to an actual output encoding; note that
1841 * format_commit_item() will always use UTF-8, so we don't
1842 * have to bother if that's what the output wants.
1845 if (same_encoding(utf8
, output_enc
))
1848 if (context
.commit_encoding
&&
1849 !same_encoding(context
.commit_encoding
, utf8
))
1850 output_enc
= context
.commit_encoding
;
1855 char *out
= reencode_string_len(sb
->buf
, sb
->len
,
1856 output_enc
, utf8
, &outsz
);
1858 strbuf_attach(sb
, out
, outsz
, outsz
+ 1);
1861 free(context
.commit_encoding
);
1862 repo_unuse_commit_buffer(r
, commit
, context
.message
);
1865 static void pp_header(struct pretty_print_context
*pp
,
1866 const char *encoding
,
1867 const struct commit
*commit
,
1871 int parents_shown
= 0;
1874 const char *name
, *line
= *msg_p
;
1875 int linelen
= get_one_line(*msg_p
);
1885 if (pp
->fmt
== CMIT_FMT_RAW
) {
1886 strbuf_add(sb
, line
, linelen
);
1890 if (starts_with(line
, "parent ")) {
1891 if (linelen
!= the_hash_algo
->hexsz
+ 8)
1892 die("bad parent line in commit");
1896 if (!parents_shown
) {
1897 unsigned num
= commit_list_count(commit
->parents
);
1898 /* with enough slop */
1899 strbuf_grow(sb
, num
* (GIT_MAX_HEXSZ
+ 10) + 20);
1900 add_merge_info(pp
, sb
, commit
);
1905 * MEDIUM == DEFAULT shows only author with dates.
1906 * FULL shows both authors but not dates.
1907 * FULLER shows both authors and dates.
1909 if (skip_prefix(line
, "author ", &name
)) {
1910 strbuf_grow(sb
, linelen
+ 80);
1911 pp_user_info(pp
, "Author", sb
, name
, encoding
);
1913 if (skip_prefix(line
, "committer ", &name
) &&
1914 (pp
->fmt
== CMIT_FMT_FULL
|| pp
->fmt
== CMIT_FMT_FULLER
)) {
1915 strbuf_grow(sb
, linelen
+ 80);
1916 pp_user_info(pp
, "Commit", sb
, name
, encoding
);
1921 void pp_title_line(struct pretty_print_context
*pp
,
1924 const char *encoding
,
1927 static const int max_length
= 78; /* per rfc2047 */
1928 struct strbuf title
;
1930 strbuf_init(&title
, 80);
1931 *msg_p
= format_subject(&title
, *msg_p
,
1932 pp
->preserve_subject
? "\n" : " ");
1934 strbuf_grow(sb
, title
.len
+ 1024);
1935 if (pp
->print_email_subject
) {
1937 fmt_output_email_subject(sb
, pp
->rev
);
1938 if (pp
->encode_email_headers
&&
1939 needs_rfc2047_encoding(title
.buf
, title
.len
))
1940 add_rfc2047(sb
, title
.buf
, title
.len
,
1941 encoding
, RFC2047_SUBJECT
);
1943 strbuf_add_wrapped_bytes(sb
, title
.buf
, title
.len
,
1944 -last_line_length(sb
), 1, max_length
);
1946 strbuf_addbuf(sb
, &title
);
1948 strbuf_addch(sb
, '\n');
1950 if (need_8bit_cte
== 0) {
1952 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
1953 if (has_non_ascii(pp
->in_body_headers
.items
[i
].string
)) {
1960 if (need_8bit_cte
> 0) {
1961 const char *header_fmt
=
1962 "MIME-Version: 1.0\n"
1963 "Content-Type: text/plain; charset=%s\n"
1964 "Content-Transfer-Encoding: 8bit\n";
1965 strbuf_addf(sb
, header_fmt
, encoding
);
1967 if (pp
->after_subject
) {
1968 strbuf_addstr(sb
, pp
->after_subject
);
1970 if (cmit_fmt_is_mail(pp
->fmt
)) {
1971 strbuf_addch(sb
, '\n');
1974 if (pp
->in_body_headers
.nr
) {
1976 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
1977 strbuf_addstr(sb
, pp
->in_body_headers
.items
[i
].string
);
1978 free(pp
->in_body_headers
.items
[i
].string
);
1980 string_list_clear(&pp
->in_body_headers
, 0);
1981 strbuf_addch(sb
, '\n');
1984 strbuf_release(&title
);
1987 static int pp_utf8_width(const char *start
, const char *end
)
1990 size_t remain
= end
- start
;
1993 int n
= utf8_width(&start
, &remain
);
1994 if (n
< 0 || !start
)
2001 static void strbuf_add_tabexpand(struct strbuf
*sb
, struct grep_opt
*opt
,
2002 int color
, int tabwidth
, const char *line
,
2007 while ((tab
= memchr(line
, '\t', linelen
)) != NULL
) {
2008 int width
= pp_utf8_width(line
, tab
);
2011 * If it wasn't well-formed utf8, or it
2012 * had characters with badly defined
2013 * width (control characters etc), just
2014 * give up on trying to align things.
2019 /* Output the data .. */
2020 append_line_with_color(sb
, opt
, line
, tab
- line
, color
,
2022 GREP_HEADER_FIELD_MAX
);
2024 /* .. and the de-tabified tab */
2025 strbuf_addchars(sb
, ' ', tabwidth
- (width
% tabwidth
));
2027 /* Skip over the printed part .. */
2028 linelen
-= tab
+ 1 - line
;
2033 * Print out everything after the last tab without
2034 * worrying about width - there's nothing more to
2037 append_line_with_color(sb
, opt
, line
, linelen
, color
, GREP_CONTEXT_BODY
,
2038 GREP_HEADER_FIELD_MAX
);
2042 * pp_handle_indent() prints out the intendation, and
2043 * the whole line (without the final newline), after
2046 static void pp_handle_indent(struct pretty_print_context
*pp
,
2047 struct strbuf
*sb
, int indent
,
2048 const char *line
, int linelen
)
2050 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
2052 strbuf_addchars(sb
, ' ', indent
);
2053 if (pp
->expand_tabs_in_log
)
2054 strbuf_add_tabexpand(sb
, opt
, pp
->color
, pp
->expand_tabs_in_log
,
2057 append_line_with_color(sb
, opt
, line
, linelen
, pp
->color
,
2059 GREP_HEADER_FIELD_MAX
);
2062 static int is_mboxrd_from(const char *line
, int len
)
2065 * a line matching /^From $/ here would only have len == 4
2066 * at this point because is_empty_line would've trimmed all
2069 return len
> 4 && starts_with(line
+ strspn(line
, ">"), "From ");
2072 void pp_remainder(struct pretty_print_context
*pp
,
2077 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
2081 const char *line
= *msg_p
;
2082 int linelen
= get_one_line(line
);
2088 if (is_blank_line(line
, &linelen
)) {
2091 if (pp
->fmt
== CMIT_FMT_SHORT
)
2096 strbuf_grow(sb
, linelen
+ indent
+ 20);
2098 pp_handle_indent(pp
, sb
, indent
, line
, linelen
);
2099 else if (pp
->expand_tabs_in_log
)
2100 strbuf_add_tabexpand(sb
, opt
, pp
->color
,
2101 pp
->expand_tabs_in_log
, line
,
2104 if (pp
->fmt
== CMIT_FMT_MBOXRD
&&
2105 is_mboxrd_from(line
, linelen
))
2106 strbuf_addch(sb
, '>');
2108 append_line_with_color(sb
, opt
, line
, linelen
,
2109 pp
->color
, GREP_CONTEXT_BODY
,
2110 GREP_HEADER_FIELD_MAX
);
2112 strbuf_addch(sb
, '\n');
2116 void pretty_print_commit(struct pretty_print_context
*pp
,
2117 const struct commit
*commit
,
2120 unsigned long beginning_of_body
;
2123 const char *reencoded
;
2124 const char *encoding
;
2125 int need_8bit_cte
= pp
->need_8bit_cte
;
2127 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
2128 format_commit_message(commit
, user_format
, sb
, pp
);
2132 encoding
= get_log_output_encoding();
2133 msg
= reencoded
= logmsg_reencode(commit
, NULL
, encoding
);
2135 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2139 * We need to check and emit Content-type: to mark it
2140 * as 8-bit if we haven't done so.
2142 if (cmit_fmt_is_mail(pp
->fmt
) && need_8bit_cte
== 0) {
2145 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
2147 /* author could be non 7-bit ASCII but
2148 * the log may be so; skip over the
2149 * header part first.
2151 if (ch
== '\n' && msg
[i
+1] == '\n')
2154 else if (non_ascii(ch
)) {
2161 pp_header(pp
, encoding
, commit
, &msg
, sb
);
2162 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->print_email_subject
) {
2163 strbuf_addch(sb
, '\n');
2166 /* Skip excess blank lines at the beginning of body, if any... */
2167 msg
= skip_blank_lines(msg
);
2169 /* These formats treat the title line specially. */
2170 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2171 pp_title_line(pp
, &msg
, sb
, encoding
, need_8bit_cte
);
2173 beginning_of_body
= sb
->len
;
2174 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2175 pp_remainder(pp
, &msg
, sb
, indent
);
2178 /* Make sure there is an EOLN for the non-oneline case */
2179 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2180 strbuf_addch(sb
, '\n');
2183 * The caller may append additional body text in e-mail
2184 * format. Make sure we did not strip the blank line
2185 * between the header and the body.
2187 if (cmit_fmt_is_mail(pp
->fmt
) && sb
->len
<= beginning_of_body
)
2188 strbuf_addch(sb
, '\n');
2190 unuse_commit_buffer(commit
, reencoded
);
2193 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
2196 struct pretty_print_context pp
= {0};
2198 pretty_print_commit(&pp
, commit
, sb
);