6 #include "string-list.h"
11 #include "reflog-walk.h"
12 #include "gpg-interface.h"
14 static char *user_format
;
15 static struct cmt_fmt_map
{
20 const char *user_format
;
22 static size_t builtin_formats_len
;
23 static size_t commit_formats_len
;
24 static size_t commit_formats_alloc
;
25 static struct cmt_fmt_map
*find_commit_format(const char *sought
);
27 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
30 user_format
= xstrdup(cp
);
32 rev
->use_terminator
= 1;
33 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
36 static int git_pretty_formats_config(const char *var
, const char *value
, void *cb
)
38 struct cmt_fmt_map
*commit_format
= NULL
;
43 if (prefixcmp(var
, "pretty."))
46 name
= var
+ strlen("pretty.");
47 for (i
= 0; i
< builtin_formats_len
; i
++) {
48 if (!strcmp(commit_formats
[i
].name
, name
))
52 for (i
= builtin_formats_len
; i
< commit_formats_len
; i
++) {
53 if (!strcmp(commit_formats
[i
].name
, name
)) {
54 commit_format
= &commit_formats
[i
];
60 ALLOC_GROW(commit_formats
, commit_formats_len
+1,
61 commit_formats_alloc
);
62 commit_format
= &commit_formats
[commit_formats_len
];
63 memset(commit_format
, 0, sizeof(*commit_format
));
67 commit_format
->name
= xstrdup(name
);
68 commit_format
->format
= CMIT_FMT_USERFORMAT
;
69 git_config_string(&fmt
, var
, value
);
70 if (!prefixcmp(fmt
, "format:") || !prefixcmp(fmt
, "tformat:")) {
71 commit_format
->is_tformat
= fmt
[0] == 't';
72 fmt
= strchr(fmt
, ':') + 1;
73 } else if (strchr(fmt
, '%'))
74 commit_format
->is_tformat
= 1;
76 commit_format
->is_alias
= 1;
77 commit_format
->user_format
= fmt
;
82 static void setup_commit_formats(void)
84 struct cmt_fmt_map builtin_formats
[] = {
85 { "raw", CMIT_FMT_RAW
, 0 },
86 { "medium", CMIT_FMT_MEDIUM
, 0 },
87 { "short", CMIT_FMT_SHORT
, 0 },
88 { "email", CMIT_FMT_EMAIL
, 0 },
89 { "fuller", CMIT_FMT_FULLER
, 0 },
90 { "full", CMIT_FMT_FULL
, 0 },
91 { "oneline", CMIT_FMT_ONELINE
, 1 }
93 commit_formats_len
= ARRAY_SIZE(builtin_formats
);
94 builtin_formats_len
= commit_formats_len
;
95 ALLOC_GROW(commit_formats
, commit_formats_len
, commit_formats_alloc
);
96 memcpy(commit_formats
, builtin_formats
,
97 sizeof(*builtin_formats
)*ARRAY_SIZE(builtin_formats
));
99 git_config(git_pretty_formats_config
, NULL
);
102 static struct cmt_fmt_map
*find_commit_format_recursive(const char *sought
,
103 const char *original
,
104 int num_redirections
)
106 struct cmt_fmt_map
*found
= NULL
;
107 size_t found_match_len
= 0;
110 if (num_redirections
>= commit_formats_len
)
111 die("invalid --pretty format: "
112 "'%s' references an alias which points to itself",
115 for (i
= 0; i
< commit_formats_len
; i
++) {
118 if (prefixcmp(commit_formats
[i
].name
, sought
))
121 match_len
= strlen(commit_formats
[i
].name
);
122 if (found
== NULL
|| found_match_len
> match_len
) {
123 found
= &commit_formats
[i
];
124 found_match_len
= match_len
;
128 if (found
&& found
->is_alias
) {
129 found
= find_commit_format_recursive(found
->user_format
,
137 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
140 setup_commit_formats();
142 return find_commit_format_recursive(sought
, sought
, 0);
145 void get_commit_format(const char *arg
, struct rev_info
*rev
)
147 struct cmt_fmt_map
*commit_format
;
149 rev
->use_terminator
= 0;
151 rev
->commit_format
= CMIT_FMT_DEFAULT
;
154 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
155 save_user_format(rev
, strchr(arg
, ':') + 1, arg
[0] == 't');
159 if (strchr(arg
, '%')) {
160 save_user_format(rev
, arg
, 1);
164 commit_format
= find_commit_format(arg
);
166 die("invalid --pretty format: %s", arg
);
168 rev
->commit_format
= commit_format
->format
;
169 rev
->use_terminator
= commit_format
->is_tformat
;
170 if (commit_format
->format
== CMIT_FMT_USERFORMAT
) {
171 save_user_format(rev
, commit_format
->user_format
,
172 commit_format
->is_tformat
);
177 * Generic support for pretty-printing the header
179 static int get_one_line(const char *msg
)
194 /* High bit set, or ISO-2022-INT */
195 static int non_ascii(int ch
)
197 return !isascii(ch
) || ch
== '\033';
200 int has_non_ascii(const char *s
)
205 while ((ch
= *s
++) != '\0') {
212 static int is_rfc822_special(char ch
)
234 static int has_rfc822_specials(const char *s
, int len
)
237 for (i
= 0; i
< len
; i
++)
238 if (is_rfc822_special(s
[i
]))
243 static int last_line_length(struct strbuf
*sb
)
247 /* How many bytes are already used on the last line? */
248 for (i
= sb
->len
- 1; i
>= 0; i
--)
249 if (sb
->buf
[i
] == '\n')
251 return sb
->len
- (i
+ 1);
254 static void add_rfc822_quoted(struct strbuf
*out
, const char *s
, int len
)
258 /* just a guess, we may have to also backslash-quote */
259 strbuf_grow(out
, len
+ 2);
261 strbuf_addch(out
, '"');
262 for (i
= 0; i
< len
; i
++) {
266 strbuf_addch(out
, '\\');
269 strbuf_addch(out
, s
[i
]);
272 strbuf_addch(out
, '"');
280 static int is_rfc2047_special(char ch
, enum rfc2047_type type
)
283 * rfc2047, section 4.2:
285 * 8-bit values which correspond to printable ASCII characters other
286 * than "=", "?", and "_" (underscore), MAY be represented as those
287 * characters. (But see section 5 for restrictions.) In
288 * particular, SPACE and TAB MUST NOT be represented as themselves
289 * within encoded words.
293 * rule out non-ASCII characters and non-printable characters (the
294 * non-ASCII check should be redundant as isprint() is not localized
295 * and only knows about ASCII, but be defensive about that)
297 if (non_ascii(ch
) || !isprint(ch
))
301 * rule out special printable characters (' ' should be the only
302 * whitespace character considered printable, but be defensive and use
305 if (isspace(ch
) || ch
== '=' || ch
== '?' || ch
== '_')
309 * rfc2047, section 5.3:
311 * As a replacement for a 'word' entity within a 'phrase', for example,
312 * one that precedes an address in a From, To, or Cc header. The ABNF
313 * definition for 'phrase' from RFC 822 thus becomes:
315 * phrase = 1*( encoded-word / word )
317 * In this case the set of characters that may be used in a "Q"-encoded
318 * 'encoded-word' is restricted to: <upper and lower case ASCII
319 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
320 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
321 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
322 * 'special' by 'linear-white-space'.
325 if (type
!= RFC2047_ADDRESS
)
328 /* '=' and '_' are special cases and have been checked above */
329 return !(isalnum(ch
) || ch
== '!' || ch
== '*' || ch
== '+' || ch
== '-' || ch
== '/');
332 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
333 const char *encoding
, enum rfc2047_type type
)
335 static const int max_length
= 78; /* per rfc2822 */
336 static const int max_encoded_length
= 76; /* per rfc2047 */
338 int line_len
= last_line_length(sb
);
340 for (i
= 0; i
< len
; i
++) {
342 if (non_ascii(ch
) || ch
== '\n')
344 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
347 strbuf_add_wrapped_bytes(sb
, line
, len
, -line_len
, 1, max_length
);
351 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
352 strbuf_addf(sb
, "=?%s?q?", encoding
);
353 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
354 for (i
= 0; i
< len
; i
++) {
355 unsigned ch
= line
[i
] & 0xFF;
356 int is_special
= is_rfc2047_special(ch
, type
);
359 * According to RFC 2047, we could encode the special character
360 * ' ' (space) with '_' (underscore) for readability. But many
361 * programs do not understand this and just leave the
362 * underscore in place. Thus, we do nothing special here, which
363 * causes ' ' to be encoded as '=20', avoiding this problem.
366 if (line_len
+ 2 + (is_special
? 3 : 1) > max_encoded_length
) {
367 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
368 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
372 strbuf_addf(sb
, "=%02X", ch
);
375 strbuf_addch(sb
, ch
);
379 strbuf_addstr(sb
, "?=");
382 void pp_user_info(const struct pretty_print_context
*pp
,
383 const char *what
, struct strbuf
*sb
,
384 const char *line
, const char *encoding
)
391 if (pp
->fmt
== CMIT_FMT_ONELINE
)
393 date
= strchr(line
, '>');
396 namelen
= ++date
- line
;
397 time
= strtoul(date
, &date
, 10);
398 tz
= strtol(date
, NULL
, 10);
400 if (pp
->fmt
== CMIT_FMT_EMAIL
) {
401 char *name_tail
= strchr(line
, '<');
402 int display_name_length
;
405 while (line
< name_tail
&& isspace(name_tail
[-1]))
407 display_name_length
= name_tail
- line
;
408 strbuf_addstr(sb
, "From: ");
409 if (!has_rfc822_specials(line
, display_name_length
)) {
410 add_rfc2047(sb
, line
, display_name_length
,
411 encoding
, RFC2047_ADDRESS
);
413 struct strbuf quoted
= STRBUF_INIT
;
414 add_rfc822_quoted("ed
, line
, display_name_length
);
415 add_rfc2047(sb
, quoted
.buf
, quoted
.len
,
416 encoding
, RFC2047_ADDRESS
);
417 strbuf_release("ed
);
419 if (namelen
- display_name_length
+ last_line_length(sb
) > 78) {
420 strbuf_addch(sb
, '\n');
421 if (!isspace(name_tail
[0]))
422 strbuf_addch(sb
, ' ');
424 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
425 strbuf_addch(sb
, '\n');
427 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
428 (pp
->fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
432 case CMIT_FMT_MEDIUM
:
433 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, pp
->date_mode
));
436 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
438 case CMIT_FMT_FULLER
:
439 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, pp
->date_mode
));
447 static int is_empty_line(const char *line
, int *len_p
)
450 while (len
&& isspace(line
[len
-1]))
456 static const char *skip_empty_lines(const char *msg
)
459 int linelen
= get_one_line(msg
);
463 if (!is_empty_line(msg
, &ll
))
470 static void add_merge_info(const struct pretty_print_context
*pp
,
471 struct strbuf
*sb
, const struct commit
*commit
)
473 struct commit_list
*parent
= commit
->parents
;
475 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (pp
->fmt
== CMIT_FMT_EMAIL
) ||
476 !parent
|| !parent
->next
)
479 strbuf_addstr(sb
, "Merge:");
482 struct commit
*p
= parent
->item
;
483 const char *hex
= NULL
;
485 hex
= find_unique_abbrev(p
->object
.sha1
, pp
->abbrev
);
487 hex
= sha1_to_hex(p
->object
.sha1
);
488 parent
= parent
->next
;
490 strbuf_addf(sb
, " %s", hex
);
492 strbuf_addch(sb
, '\n');
495 static char *get_header(const struct commit
*commit
, const char *key
)
497 int key_len
= strlen(key
);
498 const char *line
= commit
->buffer
;
501 const char *eol
= strchr(line
, '\n'), *next
;
506 warning("malformed commit (header is missing newline): %s",
507 sha1_to_hex(commit
->object
.sha1
));
508 eol
= line
+ strlen(line
);
512 if (eol
- line
> key_len
&&
513 !strncmp(line
, key
, key_len
) &&
514 line
[key_len
] == ' ') {
515 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
522 static char *replace_encoding_header(char *buf
, const char *encoding
)
524 struct strbuf tmp
= STRBUF_INIT
;
528 /* guess if there is an encoding header before a \n\n */
529 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
530 cp
= strchr(cp
, '\n');
531 if (!cp
|| *++cp
== '\n')
535 cp
= strchr(cp
, '\n');
537 return buf
; /* should not happen but be defensive */
538 len
= cp
+ 1 - (buf
+ start
);
540 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
541 if (is_encoding_utf8(encoding
)) {
542 /* we have re-coded to UTF-8; drop the header */
543 strbuf_remove(&tmp
, start
, len
);
545 /* just replaces XXXX in 'encoding XXXX\n' */
546 strbuf_splice(&tmp
, start
+ strlen("encoding "),
547 len
- strlen("encoding \n"),
548 encoding
, strlen(encoding
));
550 return strbuf_detach(&tmp
, NULL
);
553 char *logmsg_reencode(const struct commit
*commit
,
554 const char *output_encoding
)
556 static const char *utf8
= "UTF-8";
557 const char *use_encoding
;
561 if (!*output_encoding
)
563 encoding
= get_header(commit
, "encoding");
564 use_encoding
= encoding
? encoding
: utf8
;
565 if (!strcmp(use_encoding
, output_encoding
))
566 if (encoding
) /* we'll strip encoding header later */
567 out
= xstrdup(commit
->buffer
);
569 return NULL
; /* nothing to do */
571 out
= reencode_string(commit
->buffer
,
572 output_encoding
, use_encoding
);
574 out
= replace_encoding_header(out
, output_encoding
);
580 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
582 static struct string_list
*mail_map
;
584 mail_map
= xcalloc(1, sizeof(*mail_map
));
585 read_mailmap(mail_map
, NULL
);
587 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
590 static size_t format_person_part(struct strbuf
*sb
, char part
,
591 const char *msg
, int len
, enum date_mode dmode
)
593 /* currently all placeholders have same length */
594 const int placeholder_len
= 2;
596 unsigned long date
= 0;
597 char person_name
[1024];
598 char person_mail
[1024];
599 struct ident_split s
;
600 const char *name_start
, *name_end
, *mail_start
, *mail_end
;
602 if (split_ident_line(&s
, msg
, len
) < 0)
605 name_start
= s
.name_begin
;
606 name_end
= s
.name_end
;
607 mail_start
= s
.mail_begin
;
608 mail_end
= s
.mail_end
;
610 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
611 snprintf(person_name
, sizeof(person_name
), "%.*s",
612 (int)(name_end
- name_start
), name_start
);
613 snprintf(person_mail
, sizeof(person_mail
), "%.*s",
614 (int)(mail_end
- mail_start
), mail_start
);
615 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
616 name_start
= person_name
;
617 name_end
= name_start
+ strlen(person_name
);
618 mail_start
= person_mail
;
619 mail_end
= mail_start
+ strlen(person_mail
);
621 if (part
== 'n' || part
== 'N') { /* name */
622 strbuf_add(sb
, name_start
, name_end
-name_start
);
623 return placeholder_len
;
625 if (part
== 'e' || part
== 'E') { /* email */
626 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
627 return placeholder_len
;
633 date
= strtoul(s
.date_begin
, NULL
, 10);
635 if (part
== 't') { /* date, UNIX timestamp */
636 strbuf_add(sb
, s
.date_begin
, s
.date_end
- s
.date_begin
);
637 return placeholder_len
;
641 tz
= strtoul(s
.tz_begin
+ 1, NULL
, 10);
642 if (*s
.tz_begin
== '-')
647 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
648 return placeholder_len
;
649 case 'D': /* date, RFC2822 style */
650 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
651 return placeholder_len
;
652 case 'r': /* date, relative */
653 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
654 return placeholder_len
;
655 case 'i': /* date, ISO 8601 */
656 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
657 return placeholder_len
;
662 * reading from either a bogus commit, or a reflog entry with
663 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
664 * to compute a valid return value.
666 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
667 || part
== 'D' || part
== 'r' || part
== 'i')
668 return placeholder_len
;
670 return 0; /* unknown placeholder */
678 struct format_commit_context
{
679 const struct commit
*commit
;
680 const struct pretty_print_context
*pretty_ctx
;
681 unsigned commit_header_parsed
:1;
682 unsigned commit_message_parsed
:1;
683 unsigned commit_signature_parsed
:1;
690 size_t width
, indent1
, indent2
;
692 /* These offsets are relative to the start of the commit message. */
694 struct chunk committer
;
695 struct chunk encoding
;
700 /* The following ones are relative to the result struct strbuf. */
701 struct chunk abbrev_commit_hash
;
702 struct chunk abbrev_tree_hash
;
703 struct chunk abbrev_parent_hashes
;
707 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
710 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
715 * We haven't seen this chunk before. Our caller is surely
716 * going to add it the hard way now. Remember the most likely
717 * start of the to-be-added chunk: the current end of the
720 chunk
->off
= sb
->len
;
724 static void parse_commit_header(struct format_commit_context
*context
)
726 const char *msg
= context
->message
;
729 for (i
= 0; msg
[i
]; i
++) {
731 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
736 } else if (!prefixcmp(msg
+ i
, "author ")) {
737 context
->author
.off
= i
+ 7;
738 context
->author
.len
= eol
- i
- 7;
739 } else if (!prefixcmp(msg
+ i
, "committer ")) {
740 context
->committer
.off
= i
+ 10;
741 context
->committer
.len
= eol
- i
- 10;
742 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
743 context
->encoding
.off
= i
+ 9;
744 context
->encoding
.len
= eol
- i
- 9;
748 context
->message_off
= i
;
749 context
->commit_header_parsed
= 1;
752 static int istitlechar(char c
)
754 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
755 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
758 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
761 size_t start_len
= sb
->len
;
764 for (; *msg
&& *msg
!= '\n'; msg
++) {
765 if (istitlechar(*msg
)) {
767 strbuf_addch(sb
, '-');
769 strbuf_addch(sb
, *msg
);
771 while (*(msg
+1) == '.')
777 /* trim any trailing '.' or '-' characters */
779 while (sb
->len
- trimlen
> start_len
&&
780 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
781 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
783 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
786 const char *format_subject(struct strbuf
*sb
, const char *msg
,
787 const char *line_separator
)
792 const char *line
= msg
;
793 int linelen
= get_one_line(line
);
796 if (!linelen
|| is_empty_line(line
, &linelen
))
801 strbuf_grow(sb
, linelen
+ 2);
803 strbuf_addstr(sb
, line_separator
);
804 strbuf_add(sb
, line
, linelen
);
810 static void parse_commit_message(struct format_commit_context
*c
)
812 const char *msg
= c
->message
+ c
->message_off
;
813 const char *start
= c
->message
;
815 msg
= skip_empty_lines(msg
);
816 c
->subject_off
= msg
- start
;
818 msg
= format_subject(NULL
, msg
, NULL
);
819 msg
= skip_empty_lines(msg
);
820 c
->body_off
= msg
- start
;
822 c
->commit_message_parsed
= 1;
825 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
827 struct name_decoration
*d
;
828 const char *prefix
= " (";
830 load_ref_decorations(DECORATE_SHORT_REFS
);
831 d
= lookup_decoration(&name_decoration
, &commit
->object
);
833 strbuf_addstr(sb
, prefix
);
835 strbuf_addstr(sb
, d
->name
);
838 if (prefix
[0] == ',')
839 strbuf_addch(sb
, ')');
842 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
843 size_t width
, size_t indent1
, size_t indent2
)
845 struct strbuf tmp
= STRBUF_INIT
;
848 strbuf_add(&tmp
, sb
->buf
, pos
);
849 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
850 (int) indent1
, (int) indent2
, (int) width
);
851 strbuf_swap(&tmp
, sb
);
852 strbuf_release(&tmp
);
855 static void rewrap_message_tail(struct strbuf
*sb
,
856 struct format_commit_context
*c
,
857 size_t new_width
, size_t new_indent1
,
860 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
861 c
->indent2
== new_indent2
)
863 if (c
->wrap_start
< sb
->len
)
864 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
865 c
->wrap_start
= sb
->len
;
866 c
->width
= new_width
;
867 c
->indent1
= new_indent1
;
868 c
->indent2
= new_indent2
;
874 } signature_check
[] = {
875 { 'G', ": Good signature from " },
876 { 'B', ": BAD signature from " },
879 static void parse_signature_lines(struct format_commit_context
*ctx
)
881 const char *buf
= ctx
->signature
.gpg_output
;
884 for (i
= 0; i
< ARRAY_SIZE(signature_check
); i
++) {
885 const char *found
= strstr(buf
, signature_check
[i
].check
);
889 ctx
->signature
.good_bad
= signature_check
[i
].result
;
890 found
+= strlen(signature_check
[i
].check
);
891 next
= strchrnul(found
, '\n');
892 ctx
->signature
.signer
= xmemdupz(found
, next
- found
);
897 static void parse_commit_signature(struct format_commit_context
*ctx
)
899 struct strbuf payload
= STRBUF_INIT
;
900 struct strbuf signature
= STRBUF_INIT
;
901 struct strbuf gpg_output
= STRBUF_INIT
;
904 ctx
->commit_signature_parsed
= 1;
906 if (parse_signed_commit(ctx
->commit
->object
.sha1
,
907 &payload
, &signature
) <= 0)
909 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
910 signature
.buf
, signature
.len
,
912 if (status
&& !gpg_output
.len
)
914 ctx
->signature
.gpg_output
= strbuf_detach(&gpg_output
, NULL
);
915 parse_signature_lines(ctx
);
918 strbuf_release(&gpg_output
);
919 strbuf_release(&payload
);
920 strbuf_release(&signature
);
924 static int format_reflog_person(struct strbuf
*sb
,
926 struct reflog_walk_info
*log
,
927 enum date_mode dmode
)
934 ident
= get_reflog_ident(log
);
938 return format_person_part(sb
, part
, ident
, strlen(ident
), dmode
);
941 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
944 struct format_commit_context
*c
= context
;
945 const struct commit
*commit
= c
->commit
;
946 const char *msg
= c
->message
;
947 struct commit_list
*p
;
950 /* these are independent of the commit */
951 switch (placeholder
[0]) {
953 if (placeholder
[1] == '(') {
954 const char *end
= strchr(placeholder
+ 2, ')');
955 char color
[COLOR_MAXLEN
];
958 color_parse_mem(placeholder
+ 2,
959 end
- (placeholder
+ 2),
960 "--pretty format", color
);
961 strbuf_addstr(sb
, color
);
962 return end
- placeholder
+ 1;
964 if (!prefixcmp(placeholder
+ 1, "red")) {
965 strbuf_addstr(sb
, GIT_COLOR_RED
);
967 } else if (!prefixcmp(placeholder
+ 1, "green")) {
968 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
970 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
971 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
973 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
974 strbuf_addstr(sb
, GIT_COLOR_RESET
);
978 case 'n': /* newline */
979 strbuf_addch(sb
, '\n');
982 /* %x00 == NUL, %x0a == LF, etc. */
983 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
985 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
987 strbuf_addch(sb
, (h1
<<4)|h2
);
992 if (placeholder
[1] == '(') {
993 unsigned long width
= 0, indent1
= 0, indent2
= 0;
995 const char *start
= placeholder
+ 2;
996 const char *end
= strchr(start
, ')');
1000 width
= strtoul(start
, &next
, 10);
1002 indent1
= strtoul(next
+ 1, &next
, 10);
1004 indent2
= strtoul(next
+ 1,
1011 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
1012 return end
- placeholder
+ 1;
1017 /* these depend on the commit */
1018 if (!commit
->object
.parsed
)
1019 parse_object(commit
->object
.sha1
);
1021 switch (placeholder
[0]) {
1022 case 'H': /* commit hash */
1023 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
1025 case 'h': /* abbreviated commit hash */
1026 if (add_again(sb
, &c
->abbrev_commit_hash
))
1028 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
1029 c
->pretty_ctx
->abbrev
));
1030 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
1032 case 'T': /* tree hash */
1033 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
1035 case 't': /* abbreviated tree hash */
1036 if (add_again(sb
, &c
->abbrev_tree_hash
))
1038 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
1039 c
->pretty_ctx
->abbrev
));
1040 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
1042 case 'P': /* parent hashes */
1043 for (p
= commit
->parents
; p
; p
= p
->next
) {
1044 if (p
!= commit
->parents
)
1045 strbuf_addch(sb
, ' ');
1046 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
1049 case 'p': /* abbreviated parent hashes */
1050 if (add_again(sb
, &c
->abbrev_parent_hashes
))
1052 for (p
= commit
->parents
; p
; p
= p
->next
) {
1053 if (p
!= commit
->parents
)
1054 strbuf_addch(sb
, ' ');
1055 strbuf_addstr(sb
, find_unique_abbrev(
1056 p
->item
->object
.sha1
,
1057 c
->pretty_ctx
->abbrev
));
1059 c
->abbrev_parent_hashes
.len
= sb
->len
-
1060 c
->abbrev_parent_hashes
.off
;
1062 case 'm': /* left/right/bottom */
1063 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
1066 format_decoration(sb
, commit
);
1068 case 'g': /* reflog info */
1069 switch(placeholder
[1]) {
1070 case 'd': /* reflog selector */
1072 if (c
->pretty_ctx
->reflog_info
)
1073 get_reflog_selector(sb
,
1074 c
->pretty_ctx
->reflog_info
,
1075 c
->pretty_ctx
->date_mode
,
1076 c
->pretty_ctx
->date_mode_explicit
,
1077 (placeholder
[1] == 'd'));
1079 case 's': /* reflog message */
1080 if (c
->pretty_ctx
->reflog_info
)
1081 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
1087 return format_reflog_person(sb
,
1089 c
->pretty_ctx
->reflog_info
,
1090 c
->pretty_ctx
->date_mode
);
1092 return 0; /* unknown %g placeholder */
1094 if (c
->pretty_ctx
->show_notes
) {
1095 format_display_notes(commit
->object
.sha1
, sb
,
1096 get_log_output_encoding(), 0);
1102 if (placeholder
[0] == 'G') {
1103 if (!c
->commit_signature_parsed
)
1104 parse_commit_signature(c
);
1105 switch (placeholder
[1]) {
1107 if (c
->signature
.gpg_output
)
1108 strbuf_addstr(sb
, c
->signature
.gpg_output
);
1111 switch (c
->signature
.good_bad
) {
1114 strbuf_addch(sb
, c
->signature
.good_bad
);
1118 if (c
->signature
.signer
)
1119 strbuf_addstr(sb
, c
->signature
.signer
);
1126 /* For the rest we have to parse the commit header. */
1127 if (!c
->commit_header_parsed
)
1128 parse_commit_header(c
);
1130 switch (placeholder
[0]) {
1131 case 'a': /* author ... */
1132 return format_person_part(sb
, placeholder
[1],
1133 msg
+ c
->author
.off
, c
->author
.len
,
1134 c
->pretty_ctx
->date_mode
);
1135 case 'c': /* committer ... */
1136 return format_person_part(sb
, placeholder
[1],
1137 msg
+ c
->committer
.off
, c
->committer
.len
,
1138 c
->pretty_ctx
->date_mode
);
1139 case 'e': /* encoding */
1140 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
1142 case 'B': /* raw body */
1143 /* message_off is always left at the initial newline */
1144 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
1148 /* Now we need to parse the commit message. */
1149 if (!c
->commit_message_parsed
)
1150 parse_commit_message(c
);
1152 switch (placeholder
[0]) {
1153 case 's': /* subject */
1154 format_subject(sb
, msg
+ c
->subject_off
, " ");
1156 case 'f': /* sanitized subject */
1157 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
1159 case 'b': /* body */
1160 strbuf_addstr(sb
, msg
+ c
->body_off
);
1163 return 0; /* unknown placeholder */
1166 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
1173 ADD_LF_BEFORE_NON_EMPTY
,
1174 DEL_LF_BEFORE_EMPTY
,
1175 ADD_SP_BEFORE_NON_EMPTY
1178 switch (placeholder
[0]) {
1180 magic
= DEL_LF_BEFORE_EMPTY
;
1183 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1186 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1191 if (magic
!= NO_MAGIC
)
1195 consumed
= format_commit_one(sb
, placeholder
, context
);
1196 if (magic
== NO_MAGIC
)
1199 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
1200 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
1201 strbuf_setlen(sb
, sb
->len
- 1);
1202 } else if (orig_len
!= sb
->len
) {
1203 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1204 strbuf_insert(sb
, orig_len
, "\n", 1);
1205 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1206 strbuf_insert(sb
, orig_len
, " ", 1);
1208 return consumed
+ 1;
1211 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
1214 struct userformat_want
*w
= context
;
1216 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1219 switch (*placeholder
) {
1227 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1229 struct strbuf dummy
= STRBUF_INIT
;
1236 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1237 strbuf_release(&dummy
);
1240 void format_commit_message(const struct commit
*commit
,
1241 const char *format
, struct strbuf
*sb
,
1242 const struct pretty_print_context
*pretty_ctx
)
1244 struct format_commit_context context
;
1245 static const char utf8
[] = "UTF-8";
1246 const char *output_enc
= pretty_ctx
->output_encoding
;
1248 memset(&context
, 0, sizeof(context
));
1249 context
.commit
= commit
;
1250 context
.pretty_ctx
= pretty_ctx
;
1251 context
.wrap_start
= sb
->len
;
1252 context
.message
= commit
->buffer
;
1254 char *enc
= get_header(commit
, "encoding");
1255 if (strcmp(enc
? enc
: utf8
, output_enc
)) {
1256 context
.message
= logmsg_reencode(commit
, output_enc
);
1257 if (!context
.message
)
1258 context
.message
= commit
->buffer
;
1263 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1264 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1266 if (context
.message
!= commit
->buffer
)
1267 free(context
.message
);
1268 free(context
.signature
.gpg_output
);
1269 free(context
.signature
.signer
);
1272 static void pp_header(const struct pretty_print_context
*pp
,
1273 const char *encoding
,
1274 const struct commit
*commit
,
1278 int parents_shown
= 0;
1281 const char *line
= *msg_p
;
1282 int linelen
= get_one_line(*msg_p
);
1292 if (pp
->fmt
== CMIT_FMT_RAW
) {
1293 strbuf_add(sb
, line
, linelen
);
1297 if (!memcmp(line
, "parent ", 7)) {
1299 die("bad parent line in commit");
1303 if (!parents_shown
) {
1304 struct commit_list
*parent
;
1306 for (parent
= commit
->parents
, num
= 0;
1308 parent
= parent
->next
, num
++)
1310 /* with enough slop */
1311 strbuf_grow(sb
, num
* 50 + 20);
1312 add_merge_info(pp
, sb
, commit
);
1317 * MEDIUM == DEFAULT shows only author with dates.
1318 * FULL shows both authors but not dates.
1319 * FULLER shows both authors and dates.
1321 if (!memcmp(line
, "author ", 7)) {
1322 strbuf_grow(sb
, linelen
+ 80);
1323 pp_user_info(pp
, "Author", sb
, line
+ 7, encoding
);
1325 if (!memcmp(line
, "committer ", 10) &&
1326 (pp
->fmt
== CMIT_FMT_FULL
|| pp
->fmt
== CMIT_FMT_FULLER
)) {
1327 strbuf_grow(sb
, linelen
+ 80);
1328 pp_user_info(pp
, "Commit", sb
, line
+ 10, encoding
);
1333 void pp_title_line(const struct pretty_print_context
*pp
,
1336 const char *encoding
,
1339 struct strbuf title
;
1341 strbuf_init(&title
, 80);
1342 *msg_p
= format_subject(&title
, *msg_p
,
1343 pp
->preserve_subject
? "\n" : " ");
1345 strbuf_grow(sb
, title
.len
+ 1024);
1347 strbuf_addstr(sb
, pp
->subject
);
1348 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
, RFC2047_SUBJECT
);
1350 strbuf_addbuf(sb
, &title
);
1352 strbuf_addch(sb
, '\n');
1354 if (need_8bit_cte
> 0) {
1355 const char *header_fmt
=
1356 "MIME-Version: 1.0\n"
1357 "Content-Type: text/plain; charset=%s\n"
1358 "Content-Transfer-Encoding: 8bit\n";
1359 strbuf_addf(sb
, header_fmt
, encoding
);
1361 if (pp
->after_subject
) {
1362 strbuf_addstr(sb
, pp
->after_subject
);
1364 if (pp
->fmt
== CMIT_FMT_EMAIL
) {
1365 strbuf_addch(sb
, '\n');
1367 strbuf_release(&title
);
1370 void pp_remainder(const struct pretty_print_context
*pp
,
1377 const char *line
= *msg_p
;
1378 int linelen
= get_one_line(line
);
1384 if (is_empty_line(line
, &linelen
)) {
1387 if (pp
->fmt
== CMIT_FMT_SHORT
)
1392 strbuf_grow(sb
, linelen
+ indent
+ 20);
1394 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1395 strbuf_setlen(sb
, sb
->len
+ indent
);
1397 strbuf_add(sb
, line
, linelen
);
1398 strbuf_addch(sb
, '\n');
1402 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1404 const char *encoding
;
1406 encoding
= get_log_output_encoding();
1408 *encoding_p
= encoding
;
1409 return logmsg_reencode(commit
, encoding
);
1412 void pretty_print_commit(const struct pretty_print_context
*pp
,
1413 const struct commit
*commit
,
1416 unsigned long beginning_of_body
;
1418 const char *msg
= commit
->buffer
;
1420 const char *encoding
;
1421 int need_8bit_cte
= pp
->need_8bit_cte
;
1423 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
1424 format_commit_message(commit
, user_format
, sb
, pp
);
1428 reencoded
= reencode_commit_message(commit
, &encoding
);
1433 if (pp
->fmt
== CMIT_FMT_ONELINE
|| pp
->fmt
== CMIT_FMT_EMAIL
)
1437 * We need to check and emit Content-type: to mark it
1438 * as 8-bit if we haven't done so.
1440 if (pp
->fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1443 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1445 /* author could be non 7-bit ASCII but
1446 * the log may be so; skip over the
1447 * header part first.
1449 if (ch
== '\n' && msg
[i
+1] == '\n')
1452 else if (non_ascii(ch
)) {
1459 pp_header(pp
, encoding
, commit
, &msg
, sb
);
1460 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->subject
) {
1461 strbuf_addch(sb
, '\n');
1464 /* Skip excess blank lines at the beginning of body, if any... */
1465 msg
= skip_empty_lines(msg
);
1467 /* These formats treat the title line specially. */
1468 if (pp
->fmt
== CMIT_FMT_ONELINE
|| pp
->fmt
== CMIT_FMT_EMAIL
)
1469 pp_title_line(pp
, &msg
, sb
, encoding
, need_8bit_cte
);
1471 beginning_of_body
= sb
->len
;
1472 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
1473 pp_remainder(pp
, &msg
, sb
, indent
);
1476 /* Make sure there is an EOLN for the non-oneline case */
1477 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
1478 strbuf_addch(sb
, '\n');
1481 * The caller may append additional body text in e-mail
1482 * format. Make sure we did not strip the blank line
1483 * between the header and the body.
1485 if (pp
->fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1486 strbuf_addch(sb
, '\n');
1489 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1490 NOTES_SHOW_HEADER
| NOTES_INDENT
);
1495 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
1498 struct pretty_print_context pp
= {0};
1500 pretty_print_commit(&pp
, commit
, sb
);