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
, '"');
275 static int is_rfc2047_special(char ch
)
277 if (ch
== ' ' || ch
== '\n')
280 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
283 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
284 const char *encoding
)
286 static const int max_length
= 78; /* per rfc2822 */
287 static const int max_encoded_length
= 76; /* per rfc2047 */
289 int line_len
= last_line_length(sb
);
291 for (i
= 0; i
< len
; i
++) {
293 if (non_ascii(ch
) || ch
== '\n')
295 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
298 strbuf_add_wrapped_bytes(sb
, line
, len
, -line_len
, 1, max_length
);
302 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
303 strbuf_addf(sb
, "=?%s?q?", encoding
);
304 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
305 for (i
= 0; i
< len
; i
++) {
306 unsigned ch
= line
[i
] & 0xFF;
307 int is_special
= is_rfc2047_special(ch
);
310 * According to RFC 2047, we could encode the special character
311 * ' ' (space) with '_' (underscore) for readability. But many
312 * programs do not understand this and just leave the
313 * underscore in place. Thus, we do nothing special here, which
314 * causes ' ' to be encoded as '=20', avoiding this problem.
317 if (line_len
+ 2 + (is_special
? 3 : 1) > max_encoded_length
) {
318 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
319 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
323 strbuf_addf(sb
, "=%02X", ch
);
326 strbuf_addch(sb
, ch
);
330 strbuf_addstr(sb
, "?=");
333 void pp_user_info(const struct pretty_print_context
*pp
,
334 const char *what
, struct strbuf
*sb
,
335 const char *line
, const char *encoding
)
342 if (pp
->fmt
== CMIT_FMT_ONELINE
)
344 date
= strchr(line
, '>');
347 namelen
= ++date
- line
;
348 time
= strtoul(date
, &date
, 10);
349 tz
= strtol(date
, NULL
, 10);
351 if (pp
->fmt
== CMIT_FMT_EMAIL
) {
352 char *name_tail
= strchr(line
, '<');
353 int display_name_length
;
356 while (line
< name_tail
&& isspace(name_tail
[-1]))
358 display_name_length
= name_tail
- line
;
359 strbuf_addstr(sb
, "From: ");
360 if (!has_rfc822_specials(line
, display_name_length
)) {
361 add_rfc2047(sb
, line
, display_name_length
, encoding
);
363 struct strbuf quoted
= STRBUF_INIT
;
364 add_rfc822_quoted("ed
, line
, display_name_length
);
365 add_rfc2047(sb
, quoted
.buf
, quoted
.len
, encoding
);
366 strbuf_release("ed
);
368 if (namelen
- display_name_length
+ last_line_length(sb
) > 78) {
369 strbuf_addch(sb
, '\n');
370 if (!isspace(name_tail
[0]))
371 strbuf_addch(sb
, ' ');
373 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
374 strbuf_addch(sb
, '\n');
376 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
377 (pp
->fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
381 case CMIT_FMT_MEDIUM
:
382 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, pp
->date_mode
));
385 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
387 case CMIT_FMT_FULLER
:
388 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, pp
->date_mode
));
396 static int is_empty_line(const char *line
, int *len_p
)
399 while (len
&& isspace(line
[len
-1]))
405 static const char *skip_empty_lines(const char *msg
)
408 int linelen
= get_one_line(msg
);
412 if (!is_empty_line(msg
, &ll
))
419 static void add_merge_info(const struct pretty_print_context
*pp
,
420 struct strbuf
*sb
, const struct commit
*commit
)
422 struct commit_list
*parent
= commit
->parents
;
424 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (pp
->fmt
== CMIT_FMT_EMAIL
) ||
425 !parent
|| !parent
->next
)
428 strbuf_addstr(sb
, "Merge:");
431 struct commit
*p
= parent
->item
;
432 const char *hex
= NULL
;
434 hex
= find_unique_abbrev(p
->object
.sha1
, pp
->abbrev
);
436 hex
= sha1_to_hex(p
->object
.sha1
);
437 parent
= parent
->next
;
439 strbuf_addf(sb
, " %s", hex
);
441 strbuf_addch(sb
, '\n');
444 static char *get_header(const struct commit
*commit
, const char *key
)
446 int key_len
= strlen(key
);
447 const char *line
= commit
->buffer
;
450 const char *eol
= strchr(line
, '\n'), *next
;
455 warning("malformed commit (header is missing newline): %s",
456 sha1_to_hex(commit
->object
.sha1
));
457 eol
= line
+ strlen(line
);
461 if (eol
- line
> key_len
&&
462 !strncmp(line
, key
, key_len
) &&
463 line
[key_len
] == ' ') {
464 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
471 static char *replace_encoding_header(char *buf
, const char *encoding
)
473 struct strbuf tmp
= STRBUF_INIT
;
477 /* guess if there is an encoding header before a \n\n */
478 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
479 cp
= strchr(cp
, '\n');
480 if (!cp
|| *++cp
== '\n')
484 cp
= strchr(cp
, '\n');
486 return buf
; /* should not happen but be defensive */
487 len
= cp
+ 1 - (buf
+ start
);
489 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
490 if (is_encoding_utf8(encoding
)) {
491 /* we have re-coded to UTF-8; drop the header */
492 strbuf_remove(&tmp
, start
, len
);
494 /* just replaces XXXX in 'encoding XXXX\n' */
495 strbuf_splice(&tmp
, start
+ strlen("encoding "),
496 len
- strlen("encoding \n"),
497 encoding
, strlen(encoding
));
499 return strbuf_detach(&tmp
, NULL
);
502 char *logmsg_reencode(const struct commit
*commit
,
503 const char *output_encoding
)
505 static const char *utf8
= "UTF-8";
506 const char *use_encoding
;
510 if (!*output_encoding
)
512 encoding
= get_header(commit
, "encoding");
513 use_encoding
= encoding
? encoding
: utf8
;
514 if (!strcmp(use_encoding
, output_encoding
))
515 if (encoding
) /* we'll strip encoding header later */
516 out
= xstrdup(commit
->buffer
);
518 return NULL
; /* nothing to do */
520 out
= reencode_string(commit
->buffer
,
521 output_encoding
, use_encoding
);
523 out
= replace_encoding_header(out
, output_encoding
);
529 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
531 static struct string_list
*mail_map
;
533 mail_map
= xcalloc(1, sizeof(*mail_map
));
534 read_mailmap(mail_map
, NULL
);
536 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
539 static size_t format_person_part(struct strbuf
*sb
, char part
,
540 const char *msg
, int len
, enum date_mode dmode
)
542 /* currently all placeholders have same length */
543 const int placeholder_len
= 2;
545 unsigned long date
= 0;
546 char person_name
[1024];
547 char person_mail
[1024];
548 struct ident_split s
;
549 const char *name_start
, *name_end
, *mail_start
, *mail_end
;
551 if (split_ident_line(&s
, msg
, len
) < 0)
554 name_start
= s
.name_begin
;
555 name_end
= s
.name_end
;
556 mail_start
= s
.mail_begin
;
557 mail_end
= s
.mail_end
;
559 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
560 snprintf(person_name
, sizeof(person_name
), "%.*s",
561 (int)(name_end
- name_start
), name_start
);
562 snprintf(person_mail
, sizeof(person_mail
), "%.*s",
563 (int)(mail_end
- mail_start
), mail_start
);
564 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
565 name_start
= person_name
;
566 name_end
= name_start
+ strlen(person_name
);
567 mail_start
= person_mail
;
568 mail_end
= mail_start
+ strlen(person_mail
);
570 if (part
== 'n' || part
== 'N') { /* name */
571 strbuf_add(sb
, name_start
, name_end
-name_start
);
572 return placeholder_len
;
574 if (part
== 'e' || part
== 'E') { /* email */
575 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
576 return placeholder_len
;
582 date
= strtoul(s
.date_begin
, NULL
, 10);
584 if (part
== 't') { /* date, UNIX timestamp */
585 strbuf_add(sb
, s
.date_begin
, s
.date_end
- s
.date_begin
);
586 return placeholder_len
;
590 tz
= strtoul(s
.tz_begin
+ 1, NULL
, 10);
591 if (*s
.tz_begin
== '-')
596 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
597 return placeholder_len
;
598 case 'D': /* date, RFC2822 style */
599 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
600 return placeholder_len
;
601 case 'r': /* date, relative */
602 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
603 return placeholder_len
;
604 case 'i': /* date, ISO 8601 */
605 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
606 return placeholder_len
;
611 * reading from either a bogus commit, or a reflog entry with
612 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
613 * to compute a valid return value.
615 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
616 || part
== 'D' || part
== 'r' || part
== 'i')
617 return placeholder_len
;
619 return 0; /* unknown placeholder */
627 struct format_commit_context
{
628 const struct commit
*commit
;
629 const struct pretty_print_context
*pretty_ctx
;
630 unsigned commit_header_parsed
:1;
631 unsigned commit_message_parsed
:1;
632 unsigned commit_signature_parsed
:1;
639 size_t width
, indent1
, indent2
;
641 /* These offsets are relative to the start of the commit message. */
643 struct chunk committer
;
644 struct chunk encoding
;
649 /* The following ones are relative to the result struct strbuf. */
650 struct chunk abbrev_commit_hash
;
651 struct chunk abbrev_tree_hash
;
652 struct chunk abbrev_parent_hashes
;
656 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
659 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
664 * We haven't seen this chunk before. Our caller is surely
665 * going to add it the hard way now. Remember the most likely
666 * start of the to-be-added chunk: the current end of the
669 chunk
->off
= sb
->len
;
673 static void parse_commit_header(struct format_commit_context
*context
)
675 const char *msg
= context
->message
;
678 for (i
= 0; msg
[i
]; i
++) {
680 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
685 } else if (!prefixcmp(msg
+ i
, "author ")) {
686 context
->author
.off
= i
+ 7;
687 context
->author
.len
= eol
- i
- 7;
688 } else if (!prefixcmp(msg
+ i
, "committer ")) {
689 context
->committer
.off
= i
+ 10;
690 context
->committer
.len
= eol
- i
- 10;
691 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
692 context
->encoding
.off
= i
+ 9;
693 context
->encoding
.len
= eol
- i
- 9;
697 context
->message_off
= i
;
698 context
->commit_header_parsed
= 1;
701 static int istitlechar(char c
)
703 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
704 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
707 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
710 size_t start_len
= sb
->len
;
713 for (; *msg
&& *msg
!= '\n'; msg
++) {
714 if (istitlechar(*msg
)) {
716 strbuf_addch(sb
, '-');
718 strbuf_addch(sb
, *msg
);
720 while (*(msg
+1) == '.')
726 /* trim any trailing '.' or '-' characters */
728 while (sb
->len
- trimlen
> start_len
&&
729 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
730 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
732 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
735 const char *format_subject(struct strbuf
*sb
, const char *msg
,
736 const char *line_separator
)
741 const char *line
= msg
;
742 int linelen
= get_one_line(line
);
745 if (!linelen
|| is_empty_line(line
, &linelen
))
750 strbuf_grow(sb
, linelen
+ 2);
752 strbuf_addstr(sb
, line_separator
);
753 strbuf_add(sb
, line
, linelen
);
759 static void parse_commit_message(struct format_commit_context
*c
)
761 const char *msg
= c
->message
+ c
->message_off
;
762 const char *start
= c
->message
;
764 msg
= skip_empty_lines(msg
);
765 c
->subject_off
= msg
- start
;
767 msg
= format_subject(NULL
, msg
, NULL
);
768 msg
= skip_empty_lines(msg
);
769 c
->body_off
= msg
- start
;
771 c
->commit_message_parsed
= 1;
774 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
776 struct name_decoration
*d
;
777 const char *prefix
= " (";
779 load_ref_decorations(DECORATE_SHORT_REFS
);
780 d
= lookup_decoration(&name_decoration
, &commit
->object
);
782 strbuf_addstr(sb
, prefix
);
784 strbuf_addstr(sb
, d
->name
);
787 if (prefix
[0] == ',')
788 strbuf_addch(sb
, ')');
791 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
792 size_t width
, size_t indent1
, size_t indent2
)
794 struct strbuf tmp
= STRBUF_INIT
;
797 strbuf_add(&tmp
, sb
->buf
, pos
);
798 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
799 (int) indent1
, (int) indent2
, (int) width
);
800 strbuf_swap(&tmp
, sb
);
801 strbuf_release(&tmp
);
804 static void rewrap_message_tail(struct strbuf
*sb
,
805 struct format_commit_context
*c
,
806 size_t new_width
, size_t new_indent1
,
809 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
810 c
->indent2
== new_indent2
)
812 if (c
->wrap_start
< sb
->len
)
813 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
814 c
->wrap_start
= sb
->len
;
815 c
->width
= new_width
;
816 c
->indent1
= new_indent1
;
817 c
->indent2
= new_indent2
;
823 } signature_check
[] = {
824 { 'G', ": Good signature from " },
825 { 'B', ": BAD signature from " },
828 static void parse_signature_lines(struct format_commit_context
*ctx
)
830 const char *buf
= ctx
->signature
.gpg_output
;
833 for (i
= 0; i
< ARRAY_SIZE(signature_check
); i
++) {
834 const char *found
= strstr(buf
, signature_check
[i
].check
);
838 ctx
->signature
.good_bad
= signature_check
[i
].result
;
839 found
+= strlen(signature_check
[i
].check
);
840 next
= strchrnul(found
, '\n');
841 ctx
->signature
.signer
= xmemdupz(found
, next
- found
);
846 static void parse_commit_signature(struct format_commit_context
*ctx
)
848 struct strbuf payload
= STRBUF_INIT
;
849 struct strbuf signature
= STRBUF_INIT
;
850 struct strbuf gpg_output
= STRBUF_INIT
;
853 ctx
->commit_signature_parsed
= 1;
855 if (parse_signed_commit(ctx
->commit
->object
.sha1
,
856 &payload
, &signature
) <= 0)
858 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
859 signature
.buf
, signature
.len
,
861 if (status
&& !gpg_output
.len
)
863 ctx
->signature
.gpg_output
= strbuf_detach(&gpg_output
, NULL
);
864 parse_signature_lines(ctx
);
867 strbuf_release(&gpg_output
);
868 strbuf_release(&payload
);
869 strbuf_release(&signature
);
873 static int format_reflog_person(struct strbuf
*sb
,
875 struct reflog_walk_info
*log
,
876 enum date_mode dmode
)
883 ident
= get_reflog_ident(log
);
887 return format_person_part(sb
, part
, ident
, strlen(ident
), dmode
);
890 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
893 struct format_commit_context
*c
= context
;
894 const struct commit
*commit
= c
->commit
;
895 const char *msg
= c
->message
;
896 struct commit_list
*p
;
899 /* these are independent of the commit */
900 switch (placeholder
[0]) {
902 if (placeholder
[1] == '(') {
903 const char *end
= strchr(placeholder
+ 2, ')');
904 char color
[COLOR_MAXLEN
];
907 color_parse_mem(placeholder
+ 2,
908 end
- (placeholder
+ 2),
909 "--pretty format", color
);
910 strbuf_addstr(sb
, color
);
911 return end
- placeholder
+ 1;
913 if (!prefixcmp(placeholder
+ 1, "red")) {
914 strbuf_addstr(sb
, GIT_COLOR_RED
);
916 } else if (!prefixcmp(placeholder
+ 1, "green")) {
917 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
919 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
920 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
922 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
923 strbuf_addstr(sb
, GIT_COLOR_RESET
);
927 case 'n': /* newline */
928 strbuf_addch(sb
, '\n');
931 /* %x00 == NUL, %x0a == LF, etc. */
932 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
934 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
936 strbuf_addch(sb
, (h1
<<4)|h2
);
941 if (placeholder
[1] == '(') {
942 unsigned long width
= 0, indent1
= 0, indent2
= 0;
944 const char *start
= placeholder
+ 2;
945 const char *end
= strchr(start
, ')');
949 width
= strtoul(start
, &next
, 10);
951 indent1
= strtoul(next
+ 1, &next
, 10);
953 indent2
= strtoul(next
+ 1,
960 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
961 return end
- placeholder
+ 1;
966 /* these depend on the commit */
967 if (!commit
->object
.parsed
)
968 parse_object(commit
->object
.sha1
);
970 switch (placeholder
[0]) {
971 case 'H': /* commit hash */
972 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
974 case 'h': /* abbreviated commit hash */
975 if (add_again(sb
, &c
->abbrev_commit_hash
))
977 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
978 c
->pretty_ctx
->abbrev
));
979 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
981 case 'T': /* tree hash */
982 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
984 case 't': /* abbreviated tree hash */
985 if (add_again(sb
, &c
->abbrev_tree_hash
))
987 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
988 c
->pretty_ctx
->abbrev
));
989 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
991 case 'P': /* parent hashes */
992 for (p
= commit
->parents
; p
; p
= p
->next
) {
993 if (p
!= commit
->parents
)
994 strbuf_addch(sb
, ' ');
995 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
998 case 'p': /* abbreviated parent hashes */
999 if (add_again(sb
, &c
->abbrev_parent_hashes
))
1001 for (p
= commit
->parents
; p
; p
= p
->next
) {
1002 if (p
!= commit
->parents
)
1003 strbuf_addch(sb
, ' ');
1004 strbuf_addstr(sb
, find_unique_abbrev(
1005 p
->item
->object
.sha1
,
1006 c
->pretty_ctx
->abbrev
));
1008 c
->abbrev_parent_hashes
.len
= sb
->len
-
1009 c
->abbrev_parent_hashes
.off
;
1011 case 'm': /* left/right/bottom */
1012 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
1015 format_decoration(sb
, commit
);
1017 case 'g': /* reflog info */
1018 switch(placeholder
[1]) {
1019 case 'd': /* reflog selector */
1021 if (c
->pretty_ctx
->reflog_info
)
1022 get_reflog_selector(sb
,
1023 c
->pretty_ctx
->reflog_info
,
1024 c
->pretty_ctx
->date_mode
,
1025 c
->pretty_ctx
->date_mode_explicit
,
1026 (placeholder
[1] == 'd'));
1028 case 's': /* reflog message */
1029 if (c
->pretty_ctx
->reflog_info
)
1030 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
1036 return format_reflog_person(sb
,
1038 c
->pretty_ctx
->reflog_info
,
1039 c
->pretty_ctx
->date_mode
);
1041 return 0; /* unknown %g placeholder */
1043 if (c
->pretty_ctx
->show_notes
) {
1044 format_display_notes(commit
->object
.sha1
, sb
,
1045 get_log_output_encoding(), 0);
1051 if (placeholder
[0] == 'G') {
1052 if (!c
->commit_signature_parsed
)
1053 parse_commit_signature(c
);
1054 switch (placeholder
[1]) {
1056 if (c
->signature
.gpg_output
)
1057 strbuf_addstr(sb
, c
->signature
.gpg_output
);
1060 switch (c
->signature
.good_bad
) {
1063 strbuf_addch(sb
, c
->signature
.good_bad
);
1067 if (c
->signature
.signer
)
1068 strbuf_addstr(sb
, c
->signature
.signer
);
1075 /* For the rest we have to parse the commit header. */
1076 if (!c
->commit_header_parsed
)
1077 parse_commit_header(c
);
1079 switch (placeholder
[0]) {
1080 case 'a': /* author ... */
1081 return format_person_part(sb
, placeholder
[1],
1082 msg
+ c
->author
.off
, c
->author
.len
,
1083 c
->pretty_ctx
->date_mode
);
1084 case 'c': /* committer ... */
1085 return format_person_part(sb
, placeholder
[1],
1086 msg
+ c
->committer
.off
, c
->committer
.len
,
1087 c
->pretty_ctx
->date_mode
);
1088 case 'e': /* encoding */
1089 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
1091 case 'B': /* raw body */
1092 /* message_off is always left at the initial newline */
1093 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
1097 /* Now we need to parse the commit message. */
1098 if (!c
->commit_message_parsed
)
1099 parse_commit_message(c
);
1101 switch (placeholder
[0]) {
1102 case 's': /* subject */
1103 format_subject(sb
, msg
+ c
->subject_off
, " ");
1105 case 'f': /* sanitized subject */
1106 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
1108 case 'b': /* body */
1109 strbuf_addstr(sb
, msg
+ c
->body_off
);
1112 return 0; /* unknown placeholder */
1115 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
1122 ADD_LF_BEFORE_NON_EMPTY
,
1123 DEL_LF_BEFORE_EMPTY
,
1124 ADD_SP_BEFORE_NON_EMPTY
1127 switch (placeholder
[0]) {
1129 magic
= DEL_LF_BEFORE_EMPTY
;
1132 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1135 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1140 if (magic
!= NO_MAGIC
)
1144 consumed
= format_commit_one(sb
, placeholder
, context
);
1145 if (magic
== NO_MAGIC
)
1148 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
1149 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
1150 strbuf_setlen(sb
, sb
->len
- 1);
1151 } else if (orig_len
!= sb
->len
) {
1152 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1153 strbuf_insert(sb
, orig_len
, "\n", 1);
1154 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1155 strbuf_insert(sb
, orig_len
, " ", 1);
1157 return consumed
+ 1;
1160 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
1163 struct userformat_want
*w
= context
;
1165 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1168 switch (*placeholder
) {
1176 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1178 struct strbuf dummy
= STRBUF_INIT
;
1185 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1186 strbuf_release(&dummy
);
1189 void format_commit_message(const struct commit
*commit
,
1190 const char *format
, struct strbuf
*sb
,
1191 const struct pretty_print_context
*pretty_ctx
)
1193 struct format_commit_context context
;
1194 static const char utf8
[] = "UTF-8";
1195 const char *output_enc
= pretty_ctx
->output_encoding
;
1197 memset(&context
, 0, sizeof(context
));
1198 context
.commit
= commit
;
1199 context
.pretty_ctx
= pretty_ctx
;
1200 context
.wrap_start
= sb
->len
;
1201 context
.message
= commit
->buffer
;
1203 char *enc
= get_header(commit
, "encoding");
1204 if (strcmp(enc
? enc
: utf8
, output_enc
)) {
1205 context
.message
= logmsg_reencode(commit
, output_enc
);
1206 if (!context
.message
)
1207 context
.message
= commit
->buffer
;
1212 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1213 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1215 if (context
.message
!= commit
->buffer
)
1216 free(context
.message
);
1217 free(context
.signature
.gpg_output
);
1218 free(context
.signature
.signer
);
1221 static void pp_header(const struct pretty_print_context
*pp
,
1222 const char *encoding
,
1223 const struct commit
*commit
,
1227 int parents_shown
= 0;
1230 const char *line
= *msg_p
;
1231 int linelen
= get_one_line(*msg_p
);
1241 if (pp
->fmt
== CMIT_FMT_RAW
) {
1242 strbuf_add(sb
, line
, linelen
);
1246 if (!memcmp(line
, "parent ", 7)) {
1248 die("bad parent line in commit");
1252 if (!parents_shown
) {
1253 struct commit_list
*parent
;
1255 for (parent
= commit
->parents
, num
= 0;
1257 parent
= parent
->next
, num
++)
1259 /* with enough slop */
1260 strbuf_grow(sb
, num
* 50 + 20);
1261 add_merge_info(pp
, sb
, commit
);
1266 * MEDIUM == DEFAULT shows only author with dates.
1267 * FULL shows both authors but not dates.
1268 * FULLER shows both authors and dates.
1270 if (!memcmp(line
, "author ", 7)) {
1271 strbuf_grow(sb
, linelen
+ 80);
1272 pp_user_info(pp
, "Author", sb
, line
+ 7, encoding
);
1274 if (!memcmp(line
, "committer ", 10) &&
1275 (pp
->fmt
== CMIT_FMT_FULL
|| pp
->fmt
== CMIT_FMT_FULLER
)) {
1276 strbuf_grow(sb
, linelen
+ 80);
1277 pp_user_info(pp
, "Commit", sb
, line
+ 10, encoding
);
1282 void pp_title_line(const struct pretty_print_context
*pp
,
1285 const char *encoding
,
1288 struct strbuf title
;
1290 strbuf_init(&title
, 80);
1291 *msg_p
= format_subject(&title
, *msg_p
,
1292 pp
->preserve_subject
? "\n" : " ");
1294 strbuf_grow(sb
, title
.len
+ 1024);
1296 strbuf_addstr(sb
, pp
->subject
);
1297 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
1299 strbuf_addbuf(sb
, &title
);
1301 strbuf_addch(sb
, '\n');
1303 if (need_8bit_cte
> 0) {
1304 const char *header_fmt
=
1305 "MIME-Version: 1.0\n"
1306 "Content-Type: text/plain; charset=%s\n"
1307 "Content-Transfer-Encoding: 8bit\n";
1308 strbuf_addf(sb
, header_fmt
, encoding
);
1310 if (pp
->after_subject
) {
1311 strbuf_addstr(sb
, pp
->after_subject
);
1313 if (pp
->fmt
== CMIT_FMT_EMAIL
) {
1314 strbuf_addch(sb
, '\n');
1316 strbuf_release(&title
);
1319 void pp_remainder(const struct pretty_print_context
*pp
,
1326 const char *line
= *msg_p
;
1327 int linelen
= get_one_line(line
);
1333 if (is_empty_line(line
, &linelen
)) {
1336 if (pp
->fmt
== CMIT_FMT_SHORT
)
1341 strbuf_grow(sb
, linelen
+ indent
+ 20);
1343 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1344 strbuf_setlen(sb
, sb
->len
+ indent
);
1346 strbuf_add(sb
, line
, linelen
);
1347 strbuf_addch(sb
, '\n');
1351 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1353 const char *encoding
;
1355 encoding
= get_log_output_encoding();
1357 *encoding_p
= encoding
;
1358 return logmsg_reencode(commit
, encoding
);
1361 void pretty_print_commit(const struct pretty_print_context
*pp
,
1362 const struct commit
*commit
,
1365 unsigned long beginning_of_body
;
1367 const char *msg
= commit
->buffer
;
1369 const char *encoding
;
1370 int need_8bit_cte
= pp
->need_8bit_cte
;
1372 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
1373 format_commit_message(commit
, user_format
, sb
, pp
);
1377 reencoded
= reencode_commit_message(commit
, &encoding
);
1382 if (pp
->fmt
== CMIT_FMT_ONELINE
|| pp
->fmt
== CMIT_FMT_EMAIL
)
1386 * We need to check and emit Content-type: to mark it
1387 * as 8-bit if we haven't done so.
1389 if (pp
->fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1392 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1394 /* author could be non 7-bit ASCII but
1395 * the log may be so; skip over the
1396 * header part first.
1398 if (ch
== '\n' && msg
[i
+1] == '\n')
1401 else if (non_ascii(ch
)) {
1408 pp_header(pp
, encoding
, commit
, &msg
, sb
);
1409 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->subject
) {
1410 strbuf_addch(sb
, '\n');
1413 /* Skip excess blank lines at the beginning of body, if any... */
1414 msg
= skip_empty_lines(msg
);
1416 /* These formats treat the title line specially. */
1417 if (pp
->fmt
== CMIT_FMT_ONELINE
|| pp
->fmt
== CMIT_FMT_EMAIL
)
1418 pp_title_line(pp
, &msg
, sb
, encoding
, need_8bit_cte
);
1420 beginning_of_body
= sb
->len
;
1421 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
1422 pp_remainder(pp
, &msg
, sb
, indent
);
1425 /* Make sure there is an EOLN for the non-oneline case */
1426 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
1427 strbuf_addch(sb
, '\n');
1430 * The caller may append additional body text in e-mail
1431 * format. Make sure we did not strip the blank line
1432 * between the header and the body.
1434 if (pp
->fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1435 strbuf_addch(sb
, '\n');
1438 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1439 NOTES_SHOW_HEADER
| NOTES_INDENT
);
1444 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
1447 struct pretty_print_context pp
= {0};
1449 pretty_print_commit(&pp
, commit
, sb
);