9 #include "string-list.h"
14 #include "reflog-walk.h"
15 #include "gpg-interface.h"
17 #include "run-command.h"
20 * The limit for formatting directives, which enable the caller to append
21 * arbitrarily many bytes to the formatted buffer. This includes padding
22 * and wrapping formatters.
24 #define FORMATTING_LIMIT (16 * 1024)
26 static char *user_format
;
27 static struct cmt_fmt_map
{
31 int expand_tabs_in_log
;
33 enum date_mode_type default_date_mode_type
;
34 const char *user_format
;
36 static size_t builtin_formats_len
;
37 static size_t commit_formats_len
;
38 static size_t commit_formats_alloc
;
39 static struct cmt_fmt_map
*find_commit_format(const char *sought
);
41 int commit_format_is_empty(enum cmit_fmt fmt
)
43 return fmt
== CMIT_FMT_USERFORMAT
&& !*user_format
;
46 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
49 user_format
= xstrdup(cp
);
51 rev
->use_terminator
= 1;
52 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
55 static int git_pretty_formats_config(const char *var
, const char *value
,
58 struct cmt_fmt_map
*commit_format
= NULL
;
63 if (!skip_prefix(var
, "pretty.", &name
))
66 for (i
= 0; i
< builtin_formats_len
; i
++) {
67 if (!strcmp(commit_formats
[i
].name
, name
))
71 for (i
= builtin_formats_len
; i
< commit_formats_len
; i
++) {
72 if (!strcmp(commit_formats
[i
].name
, name
)) {
73 commit_format
= &commit_formats
[i
];
79 ALLOC_GROW(commit_formats
, commit_formats_len
+1,
80 commit_formats_alloc
);
81 commit_format
= &commit_formats
[commit_formats_len
];
82 memset(commit_format
, 0, sizeof(*commit_format
));
86 commit_format
->name
= xstrdup(name
);
87 commit_format
->format
= CMIT_FMT_USERFORMAT
;
88 if (git_config_string(&fmt
, var
, value
))
91 if (skip_prefix(fmt
, "format:", &fmt
))
92 commit_format
->is_tformat
= 0;
93 else if (skip_prefix(fmt
, "tformat:", &fmt
) || strchr(fmt
, '%'))
94 commit_format
->is_tformat
= 1;
96 commit_format
->is_alias
= 1;
97 commit_format
->user_format
= fmt
;
102 static void setup_commit_formats(void)
104 struct cmt_fmt_map builtin_formats
[] = {
105 { "raw", CMIT_FMT_RAW
, 0, 0 },
106 { "medium", CMIT_FMT_MEDIUM
, 0, 8 },
107 { "short", CMIT_FMT_SHORT
, 0, 0 },
108 { "email", CMIT_FMT_EMAIL
, 0, 0 },
109 { "mboxrd", CMIT_FMT_MBOXRD
, 0, 0 },
110 { "fuller", CMIT_FMT_FULLER
, 0, 8 },
111 { "full", CMIT_FMT_FULL
, 0, 8 },
112 { "oneline", CMIT_FMT_ONELINE
, 1, 0 },
113 { "reference", CMIT_FMT_USERFORMAT
, 1, 0,
114 0, DATE_SHORT
, "%C(auto)%h (%s, %ad)" },
116 * Please update $__git_log_pretty_formats in
117 * git-completion.bash when you add new formats.
120 commit_formats_len
= ARRAY_SIZE(builtin_formats
);
121 builtin_formats_len
= commit_formats_len
;
122 ALLOC_GROW(commit_formats
, commit_formats_len
, commit_formats_alloc
);
123 COPY_ARRAY(commit_formats
, builtin_formats
,
124 ARRAY_SIZE(builtin_formats
));
126 git_config(git_pretty_formats_config
, NULL
);
129 static struct cmt_fmt_map
*find_commit_format_recursive(const char *sought
,
130 const char *original
,
131 int num_redirections
)
133 struct cmt_fmt_map
*found
= NULL
;
134 size_t found_match_len
= 0;
137 if (num_redirections
>= commit_formats_len
)
138 die("invalid --pretty format: "
139 "'%s' references an alias which points to itself",
142 for (i
= 0; i
< commit_formats_len
; i
++) {
145 if (!starts_with(commit_formats
[i
].name
, sought
))
148 match_len
= strlen(commit_formats
[i
].name
);
149 if (found
== NULL
|| found_match_len
> match_len
) {
150 found
= &commit_formats
[i
];
151 found_match_len
= match_len
;
155 if (found
&& found
->is_alias
) {
156 found
= find_commit_format_recursive(found
->user_format
,
164 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
167 setup_commit_formats();
169 return find_commit_format_recursive(sought
, sought
, 0);
172 void get_commit_format(const char *arg
, struct rev_info
*rev
)
174 struct cmt_fmt_map
*commit_format
;
176 rev
->use_terminator
= 0;
178 rev
->commit_format
= CMIT_FMT_DEFAULT
;
181 if (skip_prefix(arg
, "format:", &arg
)) {
182 save_user_format(rev
, arg
, 0);
186 if (!*arg
|| skip_prefix(arg
, "tformat:", &arg
) || strchr(arg
, '%')) {
187 save_user_format(rev
, arg
, 1);
191 commit_format
= find_commit_format(arg
);
193 die("invalid --pretty format: %s", arg
);
195 rev
->commit_format
= commit_format
->format
;
196 rev
->use_terminator
= commit_format
->is_tformat
;
197 rev
->expand_tabs_in_log_default
= commit_format
->expand_tabs_in_log
;
198 if (!rev
->date_mode_explicit
&& commit_format
->default_date_mode_type
)
199 rev
->date_mode
.type
= commit_format
->default_date_mode_type
;
200 if (commit_format
->format
== CMIT_FMT_USERFORMAT
) {
201 save_user_format(rev
, commit_format
->user_format
,
202 commit_format
->is_tformat
);
207 * Generic support for pretty-printing the header
209 static int get_one_line(const char *msg
)
224 /* High bit set, or ISO-2022-INT */
225 static int non_ascii(int ch
)
227 return !isascii(ch
) || ch
== '\033';
230 int has_non_ascii(const char *s
)
235 while ((ch
= *s
++) != '\0') {
242 static int is_rfc822_special(char ch
)
264 static int needs_rfc822_quoting(const char *s
, int len
)
267 for (i
= 0; i
< len
; i
++)
268 if (is_rfc822_special(s
[i
]))
273 static int last_line_length(struct strbuf
*sb
)
277 /* How many bytes are already used on the last line? */
278 for (i
= sb
->len
- 1; i
>= 0; i
--)
279 if (sb
->buf
[i
] == '\n')
281 return sb
->len
- (i
+ 1);
284 static void add_rfc822_quoted(struct strbuf
*out
, const char *s
, int len
)
288 /* just a guess, we may have to also backslash-quote */
289 strbuf_grow(out
, len
+ 2);
291 strbuf_addch(out
, '"');
292 for (i
= 0; i
< len
; i
++) {
296 strbuf_addch(out
, '\\');
299 strbuf_addch(out
, s
[i
]);
302 strbuf_addch(out
, '"');
310 static int is_rfc2047_special(char ch
, enum rfc2047_type type
)
313 * rfc2047, section 4.2:
315 * 8-bit values which correspond to printable ASCII characters other
316 * than "=", "?", and "_" (underscore), MAY be represented as those
317 * characters. (But see section 5 for restrictions.) In
318 * particular, SPACE and TAB MUST NOT be represented as themselves
319 * within encoded words.
323 * rule out non-ASCII characters and non-printable characters (the
324 * non-ASCII check should be redundant as isprint() is not localized
325 * and only knows about ASCII, but be defensive about that)
327 if (non_ascii(ch
) || !isprint(ch
))
331 * rule out special printable characters (' ' should be the only
332 * whitespace character considered printable, but be defensive and use
335 if (isspace(ch
) || ch
== '=' || ch
== '?' || ch
== '_')
339 * rfc2047, section 5.3:
341 * As a replacement for a 'word' entity within a 'phrase', for example,
342 * one that precedes an address in a From, To, or Cc header. The ABNF
343 * definition for 'phrase' from RFC 822 thus becomes:
345 * phrase = 1*( encoded-word / word )
347 * In this case the set of characters that may be used in a "Q"-encoded
348 * 'encoded-word' is restricted to: <upper and lower case ASCII
349 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
350 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
351 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
352 * 'special' by 'linear-white-space'.
355 if (type
!= RFC2047_ADDRESS
)
358 /* '=' and '_' are special cases and have been checked above */
359 return !(isalnum(ch
) || ch
== '!' || ch
== '*' || ch
== '+' || ch
== '-' || ch
== '/');
362 static int needs_rfc2047_encoding(const char *line
, int len
)
366 for (i
= 0; i
< len
; i
++) {
368 if (non_ascii(ch
) || ch
== '\n')
370 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
377 static void add_rfc2047(struct strbuf
*sb
, const char *line
, size_t len
,
378 const char *encoding
, enum rfc2047_type type
)
380 static const int max_encoded_length
= 76; /* per rfc2047 */
382 int line_len
= last_line_length(sb
);
384 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
385 strbuf_addf(sb
, "=?%s?q?", encoding
);
386 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
390 * RFC 2047, section 5 (3):
392 * Each 'encoded-word' MUST represent an integral number of
393 * characters. A multi-octet character may not be split across
394 * adjacent 'encoded- word's.
396 const unsigned char *p
= (const unsigned char *)line
;
397 int chrlen
= mbs_chrlen(&line
, &len
, encoding
);
398 int is_special
= (chrlen
> 1) || is_rfc2047_special(*p
, type
);
400 /* "=%02X" * chrlen, or the byte itself */
401 const char *encoded_fmt
= is_special
? "=%02X" : "%c";
402 int encoded_len
= is_special
? 3 * chrlen
: 1;
405 * According to RFC 2047, we could encode the special character
406 * ' ' (space) with '_' (underscore) for readability. But many
407 * programs do not understand this and just leave the
408 * underscore in place. Thus, we do nothing special here, which
409 * causes ' ' to be encoded as '=20', avoiding this problem.
412 if (line_len
+ encoded_len
+ 2 > max_encoded_length
) {
413 /* It won't fit with trailing "?=" --- break the line */
414 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
415 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
418 for (i
= 0; i
< chrlen
; i
++)
419 strbuf_addf(sb
, encoded_fmt
, p
[i
]);
420 line_len
+= encoded_len
;
422 strbuf_addstr(sb
, "?=");
425 const char *show_ident_date(const struct ident_split
*ident
,
426 const struct date_mode
*mode
)
428 timestamp_t date
= 0;
431 if (ident
->date_begin
&& ident
->date_end
)
432 date
= parse_timestamp(ident
->date_begin
, NULL
, 10);
433 if (date_overflows(date
))
436 if (ident
->tz_begin
&& ident
->tz_end
)
437 tz
= strtol(ident
->tz_begin
, NULL
, 10);
438 if (tz
>= INT_MAX
|| tz
<= INT_MIN
)
441 return show_date(date
, tz
, mode
);
444 static inline void strbuf_add_with_color(struct strbuf
*sb
, const char *color
,
445 const char *buf
, size_t buflen
)
447 strbuf_addstr(sb
, color
);
448 strbuf_add(sb
, buf
, buflen
);
450 strbuf_addstr(sb
, GIT_COLOR_RESET
);
453 static void append_line_with_color(struct strbuf
*sb
, struct grep_opt
*opt
,
454 const char *line
, size_t linelen
,
455 int color
, enum grep_context ctx
,
456 enum grep_header_field field
)
458 const char *buf
, *eol
, *line_color
, *match_color
;
465 if (!opt
|| !want_color(color
) || opt
->invert
)
468 line_color
= opt
->colors
[GREP_COLOR_SELECTED
];
469 match_color
= opt
->colors
[GREP_COLOR_MATCH_SELECTED
];
471 while (grep_next_match(opt
, buf
, eol
, ctx
, &match
, field
, eflags
)) {
472 if (match
.rm_so
== match
.rm_eo
)
475 strbuf_add_with_color(sb
, line_color
, buf
, match
.rm_so
);
476 strbuf_add_with_color(sb
, match_color
, buf
+ match
.rm_so
,
477 match
.rm_eo
- match
.rm_so
);
483 strbuf_add_with_color(sb
, line_color
, buf
, eol
- buf
);
486 strbuf_add(sb
, buf
, eol
- buf
);
490 static int use_in_body_from(const struct pretty_print_context
*pp
,
491 const struct ident_split
*ident
)
493 if (pp
->rev
&& pp
->rev
->force_in_body_from
)
495 if (ident_cmp(pp
->from_ident
, ident
))
500 void pp_user_info(struct pretty_print_context
*pp
,
501 const char *what
, struct strbuf
*sb
,
502 const char *line
, const char *encoding
)
504 struct ident_split ident
;
506 const char *mailbuf
, *namebuf
;
507 size_t namelen
, maillen
;
508 int max_length
= 78; /* per rfc2822 */
510 if (pp
->fmt
== CMIT_FMT_ONELINE
)
513 line_end
= strchrnul(line
, '\n');
514 if (split_ident_line(&ident
, line
, line_end
- line
))
517 mailbuf
= ident
.mail_begin
;
518 maillen
= ident
.mail_end
- ident
.mail_begin
;
519 namebuf
= ident
.name_begin
;
520 namelen
= ident
.name_end
- ident
.name_begin
;
523 map_user(pp
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
525 if (cmit_fmt_is_mail(pp
->fmt
)) {
526 if (pp
->from_ident
&& use_in_body_from(pp
, &ident
)) {
527 struct strbuf buf
= STRBUF_INIT
;
529 strbuf_addstr(&buf
, "From: ");
530 strbuf_add(&buf
, namebuf
, namelen
);
531 strbuf_addstr(&buf
, " <");
532 strbuf_add(&buf
, mailbuf
, maillen
);
533 strbuf_addstr(&buf
, ">\n");
534 string_list_append(&pp
->in_body_headers
,
535 strbuf_detach(&buf
, NULL
));
537 mailbuf
= pp
->from_ident
->mail_begin
;
538 maillen
= pp
->from_ident
->mail_end
- mailbuf
;
539 namebuf
= pp
->from_ident
->name_begin
;
540 namelen
= pp
->from_ident
->name_end
- namebuf
;
543 strbuf_addstr(sb
, "From: ");
544 if (pp
->encode_email_headers
&&
545 needs_rfc2047_encoding(namebuf
, namelen
)) {
546 add_rfc2047(sb
, namebuf
, namelen
,
547 encoding
, RFC2047_ADDRESS
);
548 max_length
= 76; /* per rfc2047 */
549 } else if (needs_rfc822_quoting(namebuf
, namelen
)) {
550 struct strbuf quoted
= STRBUF_INIT
;
551 add_rfc822_quoted("ed
, namebuf
, namelen
);
552 strbuf_add_wrapped_bytes(sb
, quoted
.buf
, quoted
.len
,
554 strbuf_release("ed
);
556 strbuf_add_wrapped_bytes(sb
, namebuf
, namelen
,
561 last_line_length(sb
) + strlen(" <") + maillen
+ strlen(">"))
562 strbuf_addch(sb
, '\n');
563 strbuf_addf(sb
, " <%.*s>\n", (int)maillen
, mailbuf
);
565 struct strbuf id
= STRBUF_INIT
;
566 enum grep_header_field field
= GREP_HEADER_FIELD_MAX
;
567 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
569 if (!strcmp(what
, "Author"))
570 field
= GREP_HEADER_AUTHOR
;
571 else if (!strcmp(what
, "Commit"))
572 field
= GREP_HEADER_COMMITTER
;
574 strbuf_addf(sb
, "%s: ", what
);
575 if (pp
->fmt
== CMIT_FMT_FULLER
)
576 strbuf_addchars(sb
, ' ', 4);
578 strbuf_addf(&id
, "%.*s <%.*s>", (int)namelen
, namebuf
,
579 (int)maillen
, mailbuf
);
581 append_line_with_color(sb
, opt
, id
.buf
, id
.len
, pp
->color
,
582 GREP_CONTEXT_HEAD
, field
);
583 strbuf_addch(sb
, '\n');
588 case CMIT_FMT_MEDIUM
:
589 strbuf_addf(sb
, "Date: %s\n",
590 show_ident_date(&ident
, &pp
->date_mode
));
593 case CMIT_FMT_MBOXRD
:
594 strbuf_addf(sb
, "Date: %s\n",
595 show_ident_date(&ident
, DATE_MODE(RFC2822
)));
597 case CMIT_FMT_FULLER
:
598 strbuf_addf(sb
, "%sDate: %s\n", what
,
599 show_ident_date(&ident
, &pp
->date_mode
));
607 static int is_blank_line(const char *line
, int *len_p
)
610 while (len
&& isspace(line
[len
- 1]))
616 const char *skip_blank_lines(const char *msg
)
619 int linelen
= get_one_line(msg
);
623 if (!is_blank_line(msg
, &ll
))
630 static void add_merge_info(const struct pretty_print_context
*pp
,
631 struct strbuf
*sb
, const struct commit
*commit
)
633 struct commit_list
*parent
= commit
->parents
;
635 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (cmit_fmt_is_mail(pp
->fmt
)) ||
636 !parent
|| !parent
->next
)
639 strbuf_addstr(sb
, "Merge:");
642 struct object_id
*oidp
= &parent
->item
->object
.oid
;
643 strbuf_addch(sb
, ' ');
645 strbuf_add_unique_abbrev(sb
, oidp
, pp
->abbrev
);
647 strbuf_addstr(sb
, oid_to_hex(oidp
));
648 parent
= parent
->next
;
650 strbuf_addch(sb
, '\n');
653 static char *get_header(const char *msg
, const char *key
)
656 const char *v
= find_commit_header(msg
, key
, &len
);
657 return v
? xmemdupz(v
, len
) : NULL
;
660 static char *replace_encoding_header(char *buf
, const char *encoding
)
662 struct strbuf tmp
= STRBUF_INIT
;
666 /* guess if there is an encoding header before a \n\n */
667 while (!starts_with(cp
, "encoding ")) {
668 cp
= strchr(cp
, '\n');
669 if (!cp
|| *++cp
== '\n')
673 cp
= strchr(cp
, '\n');
675 return buf
; /* should not happen but be defensive */
676 len
= cp
+ 1 - (buf
+ start
);
678 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
679 if (is_encoding_utf8(encoding
)) {
680 /* we have re-coded to UTF-8; drop the header */
681 strbuf_remove(&tmp
, start
, len
);
683 /* just replaces XXXX in 'encoding XXXX\n' */
684 strbuf_splice(&tmp
, start
+ strlen("encoding "),
685 len
- strlen("encoding \n"),
686 encoding
, strlen(encoding
));
688 return strbuf_detach(&tmp
, NULL
);
691 const char *repo_logmsg_reencode(struct repository
*r
,
692 const struct commit
*commit
,
693 char **commit_encoding
,
694 const char *output_encoding
)
696 static const char *utf8
= "UTF-8";
697 const char *use_encoding
;
699 const char *msg
= repo_get_commit_buffer(r
, commit
, NULL
);
702 if (!output_encoding
|| !*output_encoding
) {
704 *commit_encoding
= get_header(msg
, "encoding");
707 encoding
= get_header(msg
, "encoding");
709 *commit_encoding
= encoding
;
710 use_encoding
= encoding
? encoding
: utf8
;
711 if (same_encoding(use_encoding
, output_encoding
)) {
713 * No encoding work to be done. If we have no encoding header
714 * at all, then there's nothing to do, and we can return the
715 * message verbatim (whether newly allocated or not).
721 * Otherwise, we still want to munge the encoding header in the
722 * result, which will be done by modifying the buffer. If we
723 * are using a fresh copy, we can reuse it. But if we are using
724 * the cached copy from get_commit_buffer, we need to duplicate it
725 * to avoid munging the cached copy.
727 if (msg
== get_cached_commit_buffer(r
, commit
, NULL
))
734 * There's actual encoding work to do. Do the reencoding, which
735 * still leaves the header to be replaced in the next step. At
736 * this point, we are done with msg. If we allocated a fresh
737 * copy, we can free it.
739 out
= reencode_string(msg
, output_encoding
, use_encoding
);
741 repo_unuse_commit_buffer(r
, commit
, msg
);
745 * This replacement actually consumes the buffer we hand it, so we do
746 * not have to worry about freeing the old "out" here.
749 out
= replace_encoding_header(out
, output_encoding
);
751 if (!commit_encoding
)
754 * If the re-encoding failed, out might be NULL here; in that
755 * case we just return the commit message verbatim.
757 return out
? out
: msg
;
760 static int mailmap_name(const char **email
, size_t *email_len
,
761 const char **name
, size_t *name_len
)
763 static struct string_list
*mail_map
;
765 CALLOC_ARRAY(mail_map
, 1);
766 read_mailmap(mail_map
);
768 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
771 static size_t format_person_part(struct strbuf
*sb
, char part
,
772 const char *msg
, int len
,
773 const struct date_mode
*dmode
)
775 /* currently all placeholders have same length */
776 const int placeholder_len
= 2;
777 struct ident_split s
;
778 const char *name
, *mail
;
779 size_t maillen
, namelen
;
781 if (split_ident_line(&s
, msg
, len
) < 0)
785 namelen
= s
.name_end
- s
.name_begin
;
787 maillen
= s
.mail_end
- s
.mail_begin
;
789 if (part
== 'N' || part
== 'E' || part
== 'L') /* mailmap lookup */
790 mailmap_name(&mail
, &maillen
, &name
, &namelen
);
791 if (part
== 'n' || part
== 'N') { /* name */
792 strbuf_add(sb
, name
, namelen
);
793 return placeholder_len
;
795 if (part
== 'e' || part
== 'E') { /* email */
796 strbuf_add(sb
, mail
, maillen
);
797 return placeholder_len
;
799 if (part
== 'l' || part
== 'L') { /* local-part */
800 const char *at
= memchr(mail
, '@', maillen
);
803 strbuf_add(sb
, mail
, maillen
);
804 return placeholder_len
;
810 if (part
== 't') { /* date, UNIX timestamp */
811 strbuf_add(sb
, s
.date_begin
, s
.date_end
- s
.date_begin
);
812 return placeholder_len
;
817 strbuf_addstr(sb
, show_ident_date(&s
, dmode
));
818 return placeholder_len
;
819 case 'D': /* date, RFC2822 style */
820 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(RFC2822
)));
821 return placeholder_len
;
822 case 'r': /* date, relative */
823 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(RELATIVE
)));
824 return placeholder_len
;
825 case 'i': /* date, ISO 8601-like */
826 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(ISO8601
)));
827 return placeholder_len
;
828 case 'I': /* date, ISO 8601 strict */
829 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(ISO8601_STRICT
)));
830 return placeholder_len
;
831 case 'h': /* date, human */
832 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(HUMAN
)));
833 return placeholder_len
;
835 strbuf_addstr(sb
, show_ident_date(&s
, DATE_MODE(SHORT
)));
836 return placeholder_len
;
841 * reading from either a bogus commit, or a reflog entry with
842 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
843 * to compute a valid return value.
845 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
846 || part
== 'D' || part
== 'r' || part
== 'i')
847 return placeholder_len
;
849 return 0; /* unknown placeholder */
861 flush_left_and_steal
,
872 struct format_commit_context
{
873 struct repository
*repository
;
874 const struct commit
*commit
;
875 const struct pretty_print_context
*pretty_ctx
;
876 unsigned commit_header_parsed
:1;
877 unsigned commit_message_parsed
:1;
878 struct signature_check signature_check
;
879 enum flush_type flush_type
;
880 enum trunc_type truncate
;
882 char *commit_encoding
;
883 size_t width
, indent1
, indent2
;
887 /* These offsets are relative to the start of the commit message. */
889 struct chunk committer
;
894 /* The following ones are relative to the result struct strbuf. */
898 static void parse_commit_header(struct format_commit_context
*context
)
900 const char *msg
= context
->message
;
903 for (i
= 0; msg
[i
]; i
++) {
906 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
911 } else if (skip_prefix(msg
+ i
, "author ", &name
)) {
912 context
->author
.off
= name
- msg
;
913 context
->author
.len
= msg
+ eol
- name
;
914 } else if (skip_prefix(msg
+ i
, "committer ", &name
)) {
915 context
->committer
.off
= name
- msg
;
916 context
->committer
.len
= msg
+ eol
- name
;
920 context
->message_off
= i
;
921 context
->commit_header_parsed
= 1;
924 static int istitlechar(char c
)
926 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
927 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
930 void format_sanitized_subject(struct strbuf
*sb
, const char *msg
, size_t len
)
933 size_t start_len
= sb
->len
;
937 for (i
= 0; i
< len
; i
++) {
938 if (istitlechar(msg
[i
])) {
940 strbuf_addch(sb
, '-');
942 strbuf_addch(sb
, msg
[i
]);
944 while (msg
[i
+1] == '.')
950 /* trim any trailing '.' or '-' characters */
952 while (sb
->len
- trimlen
> start_len
&&
953 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
954 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
956 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
959 const char *format_subject(struct strbuf
*sb
, const char *msg
,
960 const char *line_separator
)
965 const char *line
= msg
;
966 int linelen
= get_one_line(line
);
969 if (!linelen
|| is_blank_line(line
, &linelen
))
974 strbuf_grow(sb
, linelen
+ 2);
976 strbuf_addstr(sb
, line_separator
);
977 strbuf_add(sb
, line
, linelen
);
983 static void parse_commit_message(struct format_commit_context
*c
)
985 const char *msg
= c
->message
+ c
->message_off
;
986 const char *start
= c
->message
;
988 msg
= skip_blank_lines(msg
);
989 c
->subject_off
= msg
- start
;
991 msg
= format_subject(NULL
, msg
, NULL
);
992 msg
= skip_blank_lines(msg
);
993 c
->body_off
= msg
- start
;
995 c
->commit_message_parsed
= 1;
998 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
999 size_t width
, size_t indent1
, size_t indent2
)
1001 struct strbuf tmp
= STRBUF_INIT
;
1004 strbuf_add(&tmp
, sb
->buf
, pos
);
1005 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
1006 cast_size_t_to_int(indent1
),
1007 cast_size_t_to_int(indent2
),
1008 cast_size_t_to_int(width
));
1009 strbuf_swap(&tmp
, sb
);
1010 strbuf_release(&tmp
);
1013 static void rewrap_message_tail(struct strbuf
*sb
,
1014 struct format_commit_context
*c
,
1015 size_t new_width
, size_t new_indent1
,
1018 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
1019 c
->indent2
== new_indent2
)
1021 if (c
->wrap_start
< sb
->len
)
1022 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
1023 c
->wrap_start
= sb
->len
;
1024 c
->width
= new_width
;
1025 c
->indent1
= new_indent1
;
1026 c
->indent2
= new_indent2
;
1029 static int format_reflog_person(struct strbuf
*sb
,
1031 struct reflog_walk_info
*log
,
1032 const struct date_mode
*dmode
)
1039 ident
= get_reflog_ident(log
);
1043 return format_person_part(sb
, part
, ident
, strlen(ident
), dmode
);
1046 static size_t parse_color(struct strbuf
*sb
, /* in UTF-8 */
1047 const char *placeholder
,
1048 struct format_commit_context
*c
)
1050 const char *rest
= placeholder
;
1051 const char *basic_color
= NULL
;
1053 if (placeholder
[1] == '(') {
1054 const char *begin
= placeholder
+ 2;
1055 const char *end
= strchr(begin
, ')');
1056 char color
[COLOR_MAXLEN
];
1061 if (skip_prefix(begin
, "auto,", &begin
)) {
1062 if (!want_color(c
->pretty_ctx
->color
))
1063 return end
- placeholder
+ 1;
1064 } else if (skip_prefix(begin
, "always,", &begin
)) {
1065 /* nothing to do; we do not respect want_color at all */
1067 /* the default is the same as "auto" */
1068 if (!want_color(c
->pretty_ctx
->color
))
1069 return end
- placeholder
+ 1;
1072 if (color_parse_mem(begin
, end
- begin
, color
) < 0)
1073 die(_("unable to parse --pretty format"));
1074 strbuf_addstr(sb
, color
);
1075 return end
- placeholder
+ 1;
1079 * We handle things like "%C(red)" above; for historical reasons, there
1080 * are a few colors that can be specified without parentheses (and
1081 * they cannot support things like "auto" or "always" at all).
1083 if (skip_prefix(placeholder
+ 1, "red", &rest
))
1084 basic_color
= GIT_COLOR_RED
;
1085 else if (skip_prefix(placeholder
+ 1, "green", &rest
))
1086 basic_color
= GIT_COLOR_GREEN
;
1087 else if (skip_prefix(placeholder
+ 1, "blue", &rest
))
1088 basic_color
= GIT_COLOR_BLUE
;
1089 else if (skip_prefix(placeholder
+ 1, "reset", &rest
))
1090 basic_color
= GIT_COLOR_RESET
;
1092 if (basic_color
&& want_color(c
->pretty_ctx
->color
))
1093 strbuf_addstr(sb
, basic_color
);
1095 return rest
- placeholder
;
1098 static size_t parse_padding_placeholder(const char *placeholder
,
1099 struct format_commit_context
*c
)
1101 const char *ch
= placeholder
;
1102 enum flush_type flush_type
;
1107 flush_type
= flush_right
;
1111 flush_type
= flush_both
;
1113 } else if (*ch
== '>') {
1114 flush_type
= flush_left_and_steal
;
1117 flush_type
= flush_left
;
1123 /* the next value means "wide enough to that column" */
1130 const char *start
= ch
+ 1;
1131 const char *end
= start
+ strcspn(start
, ",)");
1134 if (!*end
|| end
== start
)
1136 width
= strtol(start
, &next
, 10);
1139 * We need to limit the amount of padding, or otherwise this
1140 * would allow the user to pad the buffer by arbitrarily many
1141 * bytes and thus cause resource exhaustion.
1143 if (width
< -FORMATTING_LIMIT
|| width
> FORMATTING_LIMIT
)
1146 if (next
== start
|| width
== 0)
1150 width
+= term_columns();
1154 c
->padding
= to_column
? -width
: width
;
1155 c
->flush_type
= flush_type
;
1159 end
= strchr(start
, ')');
1160 if (!end
|| end
== start
)
1162 if (starts_with(start
, "trunc)"))
1163 c
->truncate
= trunc_right
;
1164 else if (starts_with(start
, "ltrunc)"))
1165 c
->truncate
= trunc_left
;
1166 else if (starts_with(start
, "mtrunc)"))
1167 c
->truncate
= trunc_middle
;
1171 c
->truncate
= trunc_none
;
1173 return end
- placeholder
+ 1;
1178 static int match_placeholder_arg_value(const char *to_parse
, const char *candidate
,
1179 const char **end
, const char **valuestart
,
1184 if (!(skip_prefix(to_parse
, candidate
, &p
)))
1188 *valuestart
= p
+ 1;
1189 *valuelen
= strcspn(*valuestart
, ",)");
1190 p
= *valuestart
+ *valuelen
;
1192 if (*p
!= ',' && *p
!= ')')
1209 static int match_placeholder_bool_arg(const char *to_parse
, const char *candidate
,
1210 const char **end
, int *val
)
1217 if (!match_placeholder_arg_value(to_parse
, candidate
, end
, &argval
, &arglen
))
1225 strval
= xstrndup(argval
, arglen
);
1226 v
= git_parse_maybe_bool(strval
);
1237 static int format_trailer_match_cb(const struct strbuf
*key
, void *ud
)
1239 const struct string_list
*list
= ud
;
1240 const struct string_list_item
*item
;
1242 for_each_string_list_item (item
, list
) {
1243 if (key
->len
== (uintptr_t)item
->util
&&
1244 !strncasecmp(item
->string
, key
->buf
, key
->len
))
1250 int format_set_trailers_options(struct process_trailer_options
*opts
,
1251 struct string_list
*filter_list
,
1252 struct strbuf
*sepbuf
,
1253 struct strbuf
*kvsepbuf
,
1264 if (match_placeholder_arg_value(*arg
, "key", arg
, &argval
, &arglen
)) {
1265 uintptr_t len
= arglen
;
1270 if (len
&& argval
[len
- 1] == ':')
1272 string_list_append(filter_list
, argval
)->util
= (char *)len
;
1274 opts
->filter
= format_trailer_match_cb
;
1275 opts
->filter_data
= filter_list
;
1276 opts
->only_trailers
= 1;
1277 } else if (match_placeholder_arg_value(*arg
, "separator", arg
, &argval
, &arglen
)) {
1280 strbuf_reset(sepbuf
);
1281 fmt
= xstrndup(argval
, arglen
);
1282 strbuf_expand(sepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
1284 opts
->separator
= sepbuf
;
1285 } else if (match_placeholder_arg_value(*arg
, "key_value_separator", arg
, &argval
, &arglen
)) {
1288 strbuf_reset(kvsepbuf
);
1289 fmt
= xstrndup(argval
, arglen
);
1290 strbuf_expand(kvsepbuf
, fmt
, strbuf_expand_literal_cb
, NULL
);
1292 opts
->key_value_separator
= kvsepbuf
;
1293 } else if (!match_placeholder_bool_arg(*arg
, "only", arg
, &opts
->only_trailers
) &&
1294 !match_placeholder_bool_arg(*arg
, "unfold", arg
, &opts
->unfold
) &&
1295 !match_placeholder_bool_arg(*arg
, "keyonly", arg
, &opts
->key_only
) &&
1296 !match_placeholder_bool_arg(*arg
, "valueonly", arg
, &opts
->value_only
)) {
1298 size_t len
= strcspn(*arg
, ",)");
1299 *invalid_arg
= xstrndup(*arg
, len
);
1307 static size_t parse_describe_args(const char *start
, struct strvec
*args
)
1313 DESCRIBE_ARG_INTEGER
,
1314 DESCRIBE_ARG_STRING
,
1317 { "tags", DESCRIBE_ARG_BOOL
},
1318 { "abbrev", DESCRIBE_ARG_INTEGER
},
1319 { "exclude", DESCRIBE_ARG_STRING
},
1320 { "match", DESCRIBE_ARG_STRING
},
1322 const char *arg
= start
;
1331 for (i
= 0; !found
&& i
< ARRAY_SIZE(option
); i
++) {
1332 switch (option
[i
].type
) {
1333 case DESCRIBE_ARG_BOOL
:
1334 if (match_placeholder_bool_arg(arg
, option
[i
].name
, &arg
, &optval
)) {
1336 strvec_pushf(args
, "--%s", option
[i
].name
);
1338 strvec_pushf(args
, "--no-%s", option
[i
].name
);
1342 case DESCRIBE_ARG_INTEGER
:
1343 if (match_placeholder_arg_value(arg
, option
[i
].name
, &arg
,
1344 &argval
, &arglen
)) {
1348 strtol(argval
, &endptr
, 10);
1349 if (endptr
- argval
!= arglen
)
1351 strvec_pushf(args
, "--%s=%.*s", option
[i
].name
, (int)arglen
, argval
);
1355 case DESCRIBE_ARG_STRING
:
1356 if (match_placeholder_arg_value(arg
, option
[i
].name
, &arg
,
1357 &argval
, &arglen
)) {
1360 strvec_pushf(args
, "--%s=%.*s", option
[i
].name
, (int)arglen
, argval
);
1373 static size_t format_commit_one(struct strbuf
*sb
, /* in UTF-8 */
1374 const char *placeholder
,
1377 struct format_commit_context
*c
= context
;
1378 const struct commit
*commit
= c
->commit
;
1379 const char *msg
= c
->message
;
1380 struct commit_list
*p
;
1381 const char *arg
, *eol
;
1385 /* these are independent of the commit */
1386 res
= strbuf_expand_literal_cb(sb
, placeholder
, NULL
);
1390 switch (placeholder
[0]) {
1392 if (starts_with(placeholder
+ 1, "(auto)")) {
1393 c
->auto_color
= want_color(c
->pretty_ctx
->color
);
1394 if (c
->auto_color
&& sb
->len
)
1395 strbuf_addstr(sb
, GIT_COLOR_RESET
);
1396 return 7; /* consumed 7 bytes, "C(auto)" */
1398 int ret
= parse_color(sb
, placeholder
, c
);
1402 * Otherwise, we decided to treat %C<unknown>
1403 * as a literal string, and the previous
1404 * %C(auto) is still valid.
1409 if (placeholder
[1] == '(') {
1410 unsigned long width
= 0, indent1
= 0, indent2
= 0;
1412 const char *start
= placeholder
+ 2;
1413 const char *end
= strchr(start
, ')');
1417 width
= strtoul(start
, &next
, 10);
1419 indent1
= strtoul(next
+ 1, &next
, 10);
1421 indent2
= strtoul(next
+ 1,
1430 * We need to limit the format here as it allows the
1431 * user to prepend arbitrarily many bytes to the buffer
1434 if (width
> FORMATTING_LIMIT
||
1435 indent1
> FORMATTING_LIMIT
||
1436 indent2
> FORMATTING_LIMIT
)
1438 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
1439 return end
- placeholder
+ 1;
1445 return parse_padding_placeholder(placeholder
, c
);
1448 if (skip_prefix(placeholder
, "(describe", &arg
)) {
1449 struct child_process cmd
= CHILD_PROCESS_INIT
;
1450 struct strbuf out
= STRBUF_INIT
;
1451 struct strbuf err
= STRBUF_INIT
;
1452 struct pretty_print_describe_status
*describe_status
;
1454 describe_status
= c
->pretty_ctx
->describe_status
;
1455 if (describe_status
) {
1456 if (!describe_status
->max_invocations
)
1458 describe_status
->max_invocations
--;
1462 strvec_push(&cmd
.args
, "describe");
1466 arg
+= parse_describe_args(arg
, &cmd
.args
);
1470 child_process_clear(&cmd
);
1474 strvec_push(&cmd
.args
, oid_to_hex(&commit
->object
.oid
));
1475 pipe_command(&cmd
, NULL
, 0, &out
, 0, &err
, 0);
1477 strbuf_addbuf(sb
, &out
);
1478 strbuf_release(&out
);
1479 strbuf_release(&err
);
1480 return arg
- placeholder
+ 1;
1483 /* these depend on the commit */
1484 if (!commit
->object
.parsed
)
1485 parse_object(the_repository
, &commit
->object
.oid
);
1487 switch (placeholder
[0]) {
1488 case 'H': /* commit hash */
1489 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_COMMIT
));
1490 strbuf_addstr(sb
, oid_to_hex(&commit
->object
.oid
));
1491 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_RESET
));
1493 case 'h': /* abbreviated commit hash */
1494 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_COMMIT
));
1495 strbuf_add_unique_abbrev(sb
, &commit
->object
.oid
,
1496 c
->pretty_ctx
->abbrev
);
1497 strbuf_addstr(sb
, diff_get_color(c
->auto_color
, DIFF_RESET
));
1499 case 'T': /* tree hash */
1500 strbuf_addstr(sb
, oid_to_hex(get_commit_tree_oid(commit
)));
1502 case 't': /* abbreviated tree hash */
1503 strbuf_add_unique_abbrev(sb
,
1504 get_commit_tree_oid(commit
),
1505 c
->pretty_ctx
->abbrev
);
1507 case 'P': /* parent hashes */
1508 for (p
= commit
->parents
; p
; p
= p
->next
) {
1509 if (p
!= commit
->parents
)
1510 strbuf_addch(sb
, ' ');
1511 strbuf_addstr(sb
, oid_to_hex(&p
->item
->object
.oid
));
1514 case 'p': /* abbreviated parent hashes */
1515 for (p
= commit
->parents
; p
; p
= p
->next
) {
1516 if (p
!= commit
->parents
)
1517 strbuf_addch(sb
, ' ');
1518 strbuf_add_unique_abbrev(sb
, &p
->item
->object
.oid
,
1519 c
->pretty_ctx
->abbrev
);
1522 case 'm': /* left/right/bottom */
1523 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
1526 format_decorations(sb
, commit
, c
->auto_color
);
1529 format_decorations_extended(sb
, commit
, c
->auto_color
, "", ", ", "");
1531 case 'S': /* tag/branch like --source */
1532 if (!(c
->pretty_ctx
->rev
&& c
->pretty_ctx
->rev
->sources
))
1534 slot
= revision_sources_at(c
->pretty_ctx
->rev
->sources
, commit
);
1535 if (!(slot
&& *slot
))
1537 strbuf_addstr(sb
, *slot
);
1539 case 'g': /* reflog info */
1540 switch(placeholder
[1]) {
1541 case 'd': /* reflog selector */
1543 if (c
->pretty_ctx
->reflog_info
)
1544 get_reflog_selector(sb
,
1545 c
->pretty_ctx
->reflog_info
,
1546 &c
->pretty_ctx
->date_mode
,
1547 c
->pretty_ctx
->date_mode_explicit
,
1548 (placeholder
[1] == 'd'));
1550 case 's': /* reflog message */
1551 if (c
->pretty_ctx
->reflog_info
)
1552 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
1558 return format_reflog_person(sb
,
1560 c
->pretty_ctx
->reflog_info
,
1561 &c
->pretty_ctx
->date_mode
);
1563 return 0; /* unknown %g placeholder */
1565 if (c
->pretty_ctx
->notes_message
) {
1566 strbuf_addstr(sb
, c
->pretty_ctx
->notes_message
);
1572 if (placeholder
[0] == 'G') {
1573 if (!c
->signature_check
.result
)
1574 check_commit_signature(c
->commit
, &(c
->signature_check
));
1575 switch (placeholder
[1]) {
1577 if (c
->signature_check
.output
)
1578 strbuf_addstr(sb
, c
->signature_check
.output
);
1581 switch (c
->signature_check
.result
) {
1583 switch (c
->signature_check
.trust_level
) {
1584 case TRUST_UNDEFINED
:
1586 strbuf_addch(sb
, 'U');
1589 strbuf_addch(sb
, 'G');
1599 strbuf_addch(sb
, c
->signature_check
.result
);
1603 if (c
->signature_check
.signer
)
1604 strbuf_addstr(sb
, c
->signature_check
.signer
);
1607 if (c
->signature_check
.key
)
1608 strbuf_addstr(sb
, c
->signature_check
.key
);
1611 if (c
->signature_check
.fingerprint
)
1612 strbuf_addstr(sb
, c
->signature_check
.fingerprint
);
1615 if (c
->signature_check
.primary_key_fingerprint
)
1616 strbuf_addstr(sb
, c
->signature_check
.primary_key_fingerprint
);
1619 strbuf_addstr(sb
, gpg_trust_level_to_str(c
->signature_check
.trust_level
));
1627 /* For the rest we have to parse the commit header. */
1628 if (!c
->commit_header_parsed
) {
1630 repo_logmsg_reencode(c
->repository
, commit
,
1631 &c
->commit_encoding
, "UTF-8");
1632 parse_commit_header(c
);
1635 switch (placeholder
[0]) {
1636 case 'a': /* author ... */
1637 return format_person_part(sb
, placeholder
[1],
1638 msg
+ c
->author
.off
, c
->author
.len
,
1639 &c
->pretty_ctx
->date_mode
);
1640 case 'c': /* committer ... */
1641 return format_person_part(sb
, placeholder
[1],
1642 msg
+ c
->committer
.off
, c
->committer
.len
,
1643 &c
->pretty_ctx
->date_mode
);
1644 case 'e': /* encoding */
1645 if (c
->commit_encoding
)
1646 strbuf_addstr(sb
, c
->commit_encoding
);
1648 case 'B': /* raw body */
1649 /* message_off is always left at the initial newline */
1650 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
1654 /* Now we need to parse the commit message. */
1655 if (!c
->commit_message_parsed
)
1656 parse_commit_message(c
);
1658 switch (placeholder
[0]) {
1659 case 's': /* subject */
1660 format_subject(sb
, msg
+ c
->subject_off
, " ");
1662 case 'f': /* sanitized subject */
1663 eol
= strchrnul(msg
+ c
->subject_off
, '\n');
1664 format_sanitized_subject(sb
, msg
+ c
->subject_off
, eol
- (msg
+ c
->subject_off
));
1666 case 'b': /* body */
1667 strbuf_addstr(sb
, msg
+ c
->body_off
);
1671 if (skip_prefix(placeholder
, "(trailers", &arg
)) {
1672 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
1673 struct string_list filter_list
= STRING_LIST_INIT_NODUP
;
1674 struct strbuf sepbuf
= STRBUF_INIT
;
1675 struct strbuf kvsepbuf
= STRBUF_INIT
;
1678 opts
.no_divider
= 1;
1682 if (format_set_trailers_options(&opts
, &filter_list
, &sepbuf
, &kvsepbuf
, &arg
, NULL
))
1686 format_trailers_from_commit(sb
, msg
+ c
->subject_off
, &opts
);
1687 ret
= arg
- placeholder
+ 1;
1690 string_list_clear(&filter_list
, 0);
1691 strbuf_release(&sepbuf
);
1695 return 0; /* unknown placeholder */
1698 static size_t format_and_pad_commit(struct strbuf
*sb
, /* in UTF-8 */
1699 const char *placeholder
,
1700 struct format_commit_context
*c
)
1702 struct strbuf local_sb
= STRBUF_INIT
;
1703 size_t total_consumed
= 0;
1704 int len
, padding
= c
->padding
;
1707 const char *start
= strrchr(sb
->buf
, '\n');
1711 occupied
= utf8_strnwidth(start
, strlen(start
), 1);
1712 occupied
+= c
->pretty_ctx
->graph_width
;
1713 padding
= (-padding
) - occupied
;
1716 int modifier
= *placeholder
== 'C';
1717 size_t consumed
= format_commit_one(&local_sb
, placeholder
, c
);
1718 total_consumed
+= consumed
;
1723 placeholder
+= consumed
;
1724 if (*placeholder
!= '%')
1729 len
= utf8_strnwidth(local_sb
.buf
, local_sb
.len
, 1);
1731 if (c
->flush_type
== flush_left_and_steal
) {
1732 const char *ch
= sb
->buf
+ sb
->len
- 1;
1733 while (len
> padding
&& ch
> sb
->buf
) {
1740 /* check for trailing ansi sequences */
1744 while (p
> sb
->buf
&& ch
- p
< 10 && *p
!= '\033')
1747 ch
+ 1 - p
!= display_mode_esc_sequence_len(p
))
1750 * got a good ansi sequence, put it back to
1751 * local_sb as we're cutting sb
1753 strbuf_insert(&local_sb
, 0, p
, ch
+ 1 - p
);
1756 strbuf_setlen(sb
, ch
+ 1 - sb
->buf
);
1757 c
->flush_type
= flush_left
;
1760 if (len
> padding
) {
1761 switch (c
->truncate
) {
1763 strbuf_utf8_replace(&local_sb
,
1764 0, len
- (padding
- 2),
1768 strbuf_utf8_replace(&local_sb
,
1770 len
- (padding
- 2),
1774 strbuf_utf8_replace(&local_sb
,
1775 padding
- 2, len
- (padding
- 2),
1781 strbuf_addbuf(sb
, &local_sb
);
1783 size_t sb_len
= sb
->len
, offset
= 0;
1784 if (c
->flush_type
== flush_left
)
1785 offset
= padding
- len
;
1786 else if (c
->flush_type
== flush_both
)
1787 offset
= (padding
- len
) / 2;
1789 * we calculate padding in columns, now
1790 * convert it back to chars
1792 padding
= padding
- len
+ local_sb
.len
;
1793 strbuf_addchars(sb
, ' ', padding
);
1794 memcpy(sb
->buf
+ sb_len
+ offset
, local_sb
.buf
,
1797 strbuf_release(&local_sb
);
1798 c
->flush_type
= no_flush
;
1799 return total_consumed
;
1802 static size_t format_commit_item(struct strbuf
*sb
, /* in UTF-8 */
1803 const char *placeholder
,
1806 size_t consumed
, orig_len
;
1809 ADD_LF_BEFORE_NON_EMPTY
,
1810 DEL_LF_BEFORE_EMPTY
,
1811 ADD_SP_BEFORE_NON_EMPTY
1814 switch (placeholder
[0]) {
1816 magic
= DEL_LF_BEFORE_EMPTY
;
1819 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1822 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1827 if (magic
!= NO_MAGIC
) {
1830 switch (placeholder
[0]) {
1833 * `%+w()` cannot ever expand to a non-empty string,
1834 * and it potentially changes the layout of preceding
1835 * contents. We're thus not able to handle the magic in
1836 * this combination and refuse the pattern.
1843 if (((struct format_commit_context
*)context
)->flush_type
!= no_flush
)
1844 consumed
= format_and_pad_commit(sb
, placeholder
, context
);
1846 consumed
= format_commit_one(sb
, placeholder
, context
);
1847 if (magic
== NO_MAGIC
)
1850 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
1851 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
1852 strbuf_setlen(sb
, sb
->len
- 1);
1853 } else if (orig_len
!= sb
->len
) {
1854 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1855 strbuf_insertstr(sb
, orig_len
, "\n");
1856 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1857 strbuf_insertstr(sb
, orig_len
, " ");
1859 return consumed
+ 1;
1862 static size_t userformat_want_item(struct strbuf
*sb UNUSED
,
1863 const char *placeholder
,
1866 struct userformat_want
*w
= context
;
1868 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1871 switch (*placeholder
) {
1886 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1888 struct strbuf dummy
= STRBUF_INIT
;
1895 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1896 strbuf_release(&dummy
);
1899 void repo_format_commit_message(struct repository
*r
,
1900 const struct commit
*commit
,
1901 const char *format
, struct strbuf
*sb
,
1902 const struct pretty_print_context
*pretty_ctx
)
1904 struct format_commit_context context
= {
1907 .pretty_ctx
= pretty_ctx
,
1908 .wrap_start
= sb
->len
1910 const char *output_enc
= pretty_ctx
->output_encoding
;
1911 const char *utf8
= "UTF-8";
1913 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1914 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1917 * Convert output to an actual output encoding; note that
1918 * format_commit_item() will always use UTF-8, so we don't
1919 * have to bother if that's what the output wants.
1922 if (same_encoding(utf8
, output_enc
))
1925 if (context
.commit_encoding
&&
1926 !same_encoding(context
.commit_encoding
, utf8
))
1927 output_enc
= context
.commit_encoding
;
1932 char *out
= reencode_string_len(sb
->buf
, sb
->len
,
1933 output_enc
, utf8
, &outsz
);
1935 strbuf_attach(sb
, out
, outsz
, outsz
+ 1);
1938 free(context
.commit_encoding
);
1939 repo_unuse_commit_buffer(r
, commit
, context
.message
);
1942 static void pp_header(struct pretty_print_context
*pp
,
1943 const char *encoding
,
1944 const struct commit
*commit
,
1948 int parents_shown
= 0;
1951 const char *name
, *line
= *msg_p
;
1952 int linelen
= get_one_line(*msg_p
);
1962 if (pp
->fmt
== CMIT_FMT_RAW
) {
1963 strbuf_add(sb
, line
, linelen
);
1967 if (starts_with(line
, "parent ")) {
1968 if (linelen
!= the_hash_algo
->hexsz
+ 8)
1969 die("bad parent line in commit");
1973 if (!parents_shown
) {
1974 unsigned num
= commit_list_count(commit
->parents
);
1975 /* with enough slop */
1976 strbuf_grow(sb
, num
* (GIT_MAX_HEXSZ
+ 10) + 20);
1977 add_merge_info(pp
, sb
, commit
);
1982 * MEDIUM == DEFAULT shows only author with dates.
1983 * FULL shows both authors but not dates.
1984 * FULLER shows both authors and dates.
1986 if (skip_prefix(line
, "author ", &name
)) {
1987 strbuf_grow(sb
, linelen
+ 80);
1988 pp_user_info(pp
, "Author", sb
, name
, encoding
);
1990 if (skip_prefix(line
, "committer ", &name
) &&
1991 (pp
->fmt
== CMIT_FMT_FULL
|| pp
->fmt
== CMIT_FMT_FULLER
)) {
1992 strbuf_grow(sb
, linelen
+ 80);
1993 pp_user_info(pp
, "Commit", sb
, name
, encoding
);
1998 void pp_title_line(struct pretty_print_context
*pp
,
2001 const char *encoding
,
2004 static const int max_length
= 78; /* per rfc2047 */
2005 struct strbuf title
;
2007 strbuf_init(&title
, 80);
2008 *msg_p
= format_subject(&title
, *msg_p
,
2009 pp
->preserve_subject
? "\n" : " ");
2011 strbuf_grow(sb
, title
.len
+ 1024);
2012 if (pp
->print_email_subject
) {
2014 fmt_output_email_subject(sb
, pp
->rev
);
2015 if (pp
->encode_email_headers
&&
2016 needs_rfc2047_encoding(title
.buf
, title
.len
))
2017 add_rfc2047(sb
, title
.buf
, title
.len
,
2018 encoding
, RFC2047_SUBJECT
);
2020 strbuf_add_wrapped_bytes(sb
, title
.buf
, title
.len
,
2021 -last_line_length(sb
), 1, max_length
);
2023 strbuf_addbuf(sb
, &title
);
2025 strbuf_addch(sb
, '\n');
2027 if (need_8bit_cte
== 0) {
2029 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
2030 if (has_non_ascii(pp
->in_body_headers
.items
[i
].string
)) {
2037 if (need_8bit_cte
> 0) {
2038 const char *header_fmt
=
2039 "MIME-Version: 1.0\n"
2040 "Content-Type: text/plain; charset=%s\n"
2041 "Content-Transfer-Encoding: 8bit\n";
2042 strbuf_addf(sb
, header_fmt
, encoding
);
2044 if (pp
->after_subject
) {
2045 strbuf_addstr(sb
, pp
->after_subject
);
2047 if (cmit_fmt_is_mail(pp
->fmt
)) {
2048 strbuf_addch(sb
, '\n');
2051 if (pp
->in_body_headers
.nr
) {
2053 for (i
= 0; i
< pp
->in_body_headers
.nr
; i
++) {
2054 strbuf_addstr(sb
, pp
->in_body_headers
.items
[i
].string
);
2055 free(pp
->in_body_headers
.items
[i
].string
);
2057 string_list_clear(&pp
->in_body_headers
, 0);
2058 strbuf_addch(sb
, '\n');
2061 strbuf_release(&title
);
2064 static int pp_utf8_width(const char *start
, const char *end
)
2067 size_t remain
= end
- start
;
2070 int n
= utf8_width(&start
, &remain
);
2071 if (n
< 0 || !start
)
2078 static void strbuf_add_tabexpand(struct strbuf
*sb
, struct grep_opt
*opt
,
2079 int color
, int tabwidth
, const char *line
,
2084 while ((tab
= memchr(line
, '\t', linelen
)) != NULL
) {
2085 int width
= pp_utf8_width(line
, tab
);
2088 * If it wasn't well-formed utf8, or it
2089 * had characters with badly defined
2090 * width (control characters etc), just
2091 * give up on trying to align things.
2096 /* Output the data .. */
2097 append_line_with_color(sb
, opt
, line
, tab
- line
, color
,
2099 GREP_HEADER_FIELD_MAX
);
2101 /* .. and the de-tabified tab */
2102 strbuf_addchars(sb
, ' ', tabwidth
- (width
% tabwidth
));
2104 /* Skip over the printed part .. */
2105 linelen
-= tab
+ 1 - line
;
2110 * Print out everything after the last tab without
2111 * worrying about width - there's nothing more to
2114 append_line_with_color(sb
, opt
, line
, linelen
, color
, GREP_CONTEXT_BODY
,
2115 GREP_HEADER_FIELD_MAX
);
2119 * pp_handle_indent() prints out the intendation, and
2120 * the whole line (without the final newline), after
2123 static void pp_handle_indent(struct pretty_print_context
*pp
,
2124 struct strbuf
*sb
, int indent
,
2125 const char *line
, int linelen
)
2127 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
2129 strbuf_addchars(sb
, ' ', indent
);
2130 if (pp
->expand_tabs_in_log
)
2131 strbuf_add_tabexpand(sb
, opt
, pp
->color
, pp
->expand_tabs_in_log
,
2134 append_line_with_color(sb
, opt
, line
, linelen
, pp
->color
,
2136 GREP_HEADER_FIELD_MAX
);
2139 static int is_mboxrd_from(const char *line
, int len
)
2142 * a line matching /^From $/ here would only have len == 4
2143 * at this point because is_empty_line would've trimmed all
2146 return len
> 4 && starts_with(line
+ strspn(line
, ">"), "From ");
2149 void pp_remainder(struct pretty_print_context
*pp
,
2154 struct grep_opt
*opt
= pp
->rev
? &pp
->rev
->grep_filter
: NULL
;
2158 const char *line
= *msg_p
;
2159 int linelen
= get_one_line(line
);
2165 if (is_blank_line(line
, &linelen
)) {
2168 if (pp
->fmt
== CMIT_FMT_SHORT
)
2173 strbuf_grow(sb
, linelen
+ indent
+ 20);
2175 pp_handle_indent(pp
, sb
, indent
, line
, linelen
);
2176 else if (pp
->expand_tabs_in_log
)
2177 strbuf_add_tabexpand(sb
, opt
, pp
->color
,
2178 pp
->expand_tabs_in_log
, line
,
2181 if (pp
->fmt
== CMIT_FMT_MBOXRD
&&
2182 is_mboxrd_from(line
, linelen
))
2183 strbuf_addch(sb
, '>');
2185 append_line_with_color(sb
, opt
, line
, linelen
,
2186 pp
->color
, GREP_CONTEXT_BODY
,
2187 GREP_HEADER_FIELD_MAX
);
2189 strbuf_addch(sb
, '\n');
2193 void pretty_print_commit(struct pretty_print_context
*pp
,
2194 const struct commit
*commit
,
2197 unsigned long beginning_of_body
;
2200 const char *reencoded
;
2201 const char *encoding
;
2202 int need_8bit_cte
= pp
->need_8bit_cte
;
2204 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
2205 format_commit_message(commit
, user_format
, sb
, pp
);
2209 encoding
= get_log_output_encoding();
2210 msg
= reencoded
= logmsg_reencode(commit
, NULL
, encoding
);
2212 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2216 * We need to check and emit Content-type: to mark it
2217 * as 8-bit if we haven't done so.
2219 if (cmit_fmt_is_mail(pp
->fmt
) && need_8bit_cte
== 0) {
2222 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
2224 /* author could be non 7-bit ASCII but
2225 * the log may be so; skip over the
2226 * header part first.
2228 if (ch
== '\n' && msg
[i
+1] == '\n')
2231 else if (non_ascii(ch
)) {
2238 pp_header(pp
, encoding
, commit
, &msg
, sb
);
2239 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->print_email_subject
) {
2240 strbuf_addch(sb
, '\n');
2243 /* Skip excess blank lines at the beginning of body, if any... */
2244 msg
= skip_blank_lines(msg
);
2246 /* These formats treat the title line specially. */
2247 if (pp
->fmt
== CMIT_FMT_ONELINE
|| cmit_fmt_is_mail(pp
->fmt
))
2248 pp_title_line(pp
, &msg
, sb
, encoding
, need_8bit_cte
);
2250 beginning_of_body
= sb
->len
;
2251 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2252 pp_remainder(pp
, &msg
, sb
, indent
);
2255 /* Make sure there is an EOLN for the non-oneline case */
2256 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
2257 strbuf_addch(sb
, '\n');
2260 * The caller may append additional body text in e-mail
2261 * format. Make sure we did not strip the blank line
2262 * between the header and the body.
2264 if (cmit_fmt_is_mail(pp
->fmt
) && sb
->len
<= beginning_of_body
)
2265 strbuf_addch(sb
, '\n');
2267 unuse_commit_buffer(commit
, reencoded
);
2270 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
2273 struct pretty_print_context pp
= {0};
2275 pretty_print_commit(&pp
, commit
, sb
);