6 #include "string-list.h"
11 #include "reflog-walk.h"
13 static char *user_format
;
14 static struct cmt_fmt_map
{
19 const char *user_format
;
21 static size_t builtin_formats_len
;
22 static size_t commit_formats_len
;
23 static size_t commit_formats_alloc
;
24 static struct cmt_fmt_map
*find_commit_format(const char *sought
);
26 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
29 user_format
= xstrdup(cp
);
31 rev
->use_terminator
= 1;
32 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
35 static int git_pretty_formats_config(const char *var
, const char *value
, void *cb
)
37 struct cmt_fmt_map
*commit_format
= NULL
;
42 if (prefixcmp(var
, "pretty."))
45 name
= var
+ strlen("pretty.");
46 for (i
= 0; i
< builtin_formats_len
; i
++) {
47 if (!strcmp(commit_formats
[i
].name
, name
))
51 for (i
= builtin_formats_len
; i
< commit_formats_len
; i
++) {
52 if (!strcmp(commit_formats
[i
].name
, name
)) {
53 commit_format
= &commit_formats
[i
];
59 ALLOC_GROW(commit_formats
, commit_formats_len
+1,
60 commit_formats_alloc
);
61 commit_format
= &commit_formats
[commit_formats_len
];
62 memset(commit_format
, 0, sizeof(*commit_format
));
66 commit_format
->name
= xstrdup(name
);
67 commit_format
->format
= CMIT_FMT_USERFORMAT
;
68 git_config_string(&fmt
, var
, value
);
69 if (!prefixcmp(fmt
, "format:") || !prefixcmp(fmt
, "tformat:")) {
70 commit_format
->is_tformat
= fmt
[0] == 't';
71 fmt
= strchr(fmt
, ':') + 1;
72 } else if (strchr(fmt
, '%'))
73 commit_format
->is_tformat
= 1;
75 commit_format
->is_alias
= 1;
76 commit_format
->user_format
= fmt
;
81 static void setup_commit_formats(void)
83 struct cmt_fmt_map builtin_formats
[] = {
84 { "raw", CMIT_FMT_RAW
, 0 },
85 { "medium", CMIT_FMT_MEDIUM
, 0 },
86 { "short", CMIT_FMT_SHORT
, 0 },
87 { "email", CMIT_FMT_EMAIL
, 0 },
88 { "fuller", CMIT_FMT_FULLER
, 0 },
89 { "full", CMIT_FMT_FULL
, 0 },
90 { "oneline", CMIT_FMT_ONELINE
, 1 }
92 commit_formats_len
= ARRAY_SIZE(builtin_formats
);
93 builtin_formats_len
= commit_formats_len
;
94 ALLOC_GROW(commit_formats
, commit_formats_len
, commit_formats_alloc
);
95 memcpy(commit_formats
, builtin_formats
,
96 sizeof(*builtin_formats
)*ARRAY_SIZE(builtin_formats
));
98 git_config(git_pretty_formats_config
, NULL
);
101 static struct cmt_fmt_map
*find_commit_format_recursive(const char *sought
,
102 const char *original
,
103 int num_redirections
)
105 struct cmt_fmt_map
*found
= NULL
;
106 size_t found_match_len
= 0;
109 if (num_redirections
>= commit_formats_len
)
110 die("invalid --pretty format: "
111 "'%s' references an alias which points to itself",
114 for (i
= 0; i
< commit_formats_len
; i
++) {
117 if (prefixcmp(commit_formats
[i
].name
, sought
))
120 match_len
= strlen(commit_formats
[i
].name
);
121 if (found
== NULL
|| found_match_len
> match_len
) {
122 found
= &commit_formats
[i
];
123 found_match_len
= match_len
;
127 if (found
&& found
->is_alias
) {
128 found
= find_commit_format_recursive(found
->user_format
,
136 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
139 setup_commit_formats();
141 return find_commit_format_recursive(sought
, sought
, 0);
144 void get_commit_format(const char *arg
, struct rev_info
*rev
)
146 struct cmt_fmt_map
*commit_format
;
148 rev
->use_terminator
= 0;
150 rev
->commit_format
= CMIT_FMT_DEFAULT
;
153 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
154 save_user_format(rev
, strchr(arg
, ':') + 1, arg
[0] == 't');
158 if (strchr(arg
, '%')) {
159 save_user_format(rev
, arg
, 1);
163 commit_format
= find_commit_format(arg
);
165 die("invalid --pretty format: %s", arg
);
167 rev
->commit_format
= commit_format
->format
;
168 rev
->use_terminator
= commit_format
->is_tformat
;
169 if (commit_format
->format
== CMIT_FMT_USERFORMAT
) {
170 save_user_format(rev
, commit_format
->user_format
,
171 commit_format
->is_tformat
);
176 * Generic support for pretty-printing the header
178 static int get_one_line(const char *msg
)
193 /* High bit set, or ISO-2022-INT */
194 static int non_ascii(int ch
)
196 return !isascii(ch
) || ch
== '\033';
199 int has_non_ascii(const char *s
)
204 while ((ch
= *s
++) != '\0') {
211 static int is_rfc822_special(char ch
)
233 static int has_rfc822_specials(const char *s
, int len
)
236 for (i
= 0; i
< len
; i
++)
237 if (is_rfc822_special(s
[i
]))
242 static void add_rfc822_quoted(struct strbuf
*out
, const char *s
, int len
)
246 /* just a guess, we may have to also backslash-quote */
247 strbuf_grow(out
, len
+ 2);
249 strbuf_addch(out
, '"');
250 for (i
= 0; i
< len
; i
++) {
254 strbuf_addch(out
, '\\');
257 strbuf_addch(out
, s
[i
]);
260 strbuf_addch(out
, '"');
263 static int is_rfc2047_special(char ch
)
265 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
268 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
269 const char *encoding
)
271 static const int max_length
= 78; /* per rfc2822 */
275 /* How many bytes are already used on the current line? */
276 for (i
= sb
->len
- 1; i
>= 0; i
--)
277 if (sb
->buf
[i
] == '\n')
279 line_len
= sb
->len
- (i
+1);
281 for (i
= 0; i
< len
; i
++) {
283 if (non_ascii(ch
) || ch
== '\n')
285 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
288 strbuf_add_wrapped_bytes(sb
, line
, len
, 0, 1, max_length
- line_len
);
292 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
293 strbuf_addf(sb
, "=?%s?q?", encoding
);
294 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
295 for (i
= 0; i
< len
; i
++) {
296 unsigned ch
= line
[i
] & 0xFF;
298 if (line_len
>= max_length
- 2) {
299 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
300 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
304 * We encode ' ' using '=20' even though rfc2047
305 * allows using '_' for readability. Unfortunately,
306 * many programs do not understand this and just
307 * leave the underscore in place.
309 if (is_rfc2047_special(ch
) || ch
== ' ' || ch
== '\n') {
310 strbuf_addf(sb
, "=%02X", ch
);
314 strbuf_addch(sb
, ch
);
318 strbuf_addstr(sb
, "?=");
321 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
322 const char *line
, enum date_mode dmode
,
323 const char *encoding
)
330 if (fmt
== CMIT_FMT_ONELINE
)
332 date
= strchr(line
, '>');
335 namelen
= ++date
- line
;
336 time
= strtoul(date
, &date
, 10);
337 tz
= strtol(date
, NULL
, 10);
339 if (fmt
== CMIT_FMT_EMAIL
) {
340 char *name_tail
= strchr(line
, '<');
341 int display_name_length
;
344 while (line
< name_tail
&& isspace(name_tail
[-1]))
346 display_name_length
= name_tail
- line
;
347 strbuf_addstr(sb
, "From: ");
348 if (!has_rfc822_specials(line
, display_name_length
)) {
349 add_rfc2047(sb
, line
, display_name_length
, encoding
);
351 struct strbuf quoted
= STRBUF_INIT
;
352 add_rfc822_quoted("ed
, line
, display_name_length
);
353 add_rfc2047(sb
, quoted
.buf
, quoted
.len
, encoding
);
354 strbuf_release("ed
);
356 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
357 strbuf_addch(sb
, '\n');
359 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
360 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
364 case CMIT_FMT_MEDIUM
:
365 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
368 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
370 case CMIT_FMT_FULLER
:
371 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
379 static int is_empty_line(const char *line
, int *len_p
)
382 while (len
&& isspace(line
[len
-1]))
388 static const char *skip_empty_lines(const char *msg
)
391 int linelen
= get_one_line(msg
);
395 if (!is_empty_line(msg
, &ll
))
402 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
403 const struct commit
*commit
, int abbrev
)
405 struct commit_list
*parent
= commit
->parents
;
407 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
408 !parent
|| !parent
->next
)
411 strbuf_addstr(sb
, "Merge:");
414 struct commit
*p
= parent
->item
;
415 const char *hex
= NULL
;
417 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
419 hex
= sha1_to_hex(p
->object
.sha1
);
420 parent
= parent
->next
;
422 strbuf_addf(sb
, " %s", hex
);
424 strbuf_addch(sb
, '\n');
427 static char *get_header(const struct commit
*commit
, const char *key
)
429 int key_len
= strlen(key
);
430 const char *line
= commit
->buffer
;
433 const char *eol
= strchr(line
, '\n'), *next
;
438 eol
= line
+ strlen(line
);
442 if (eol
- line
> key_len
&&
443 !strncmp(line
, key
, key_len
) &&
444 line
[key_len
] == ' ') {
445 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
451 static char *replace_encoding_header(char *buf
, const char *encoding
)
453 struct strbuf tmp
= STRBUF_INIT
;
457 /* guess if there is an encoding header before a \n\n */
458 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
459 cp
= strchr(cp
, '\n');
460 if (!cp
|| *++cp
== '\n')
464 cp
= strchr(cp
, '\n');
466 return buf
; /* should not happen but be defensive */
467 len
= cp
+ 1 - (buf
+ start
);
469 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
470 if (is_encoding_utf8(encoding
)) {
471 /* we have re-coded to UTF-8; drop the header */
472 strbuf_remove(&tmp
, start
, len
);
474 /* just replaces XXXX in 'encoding XXXX\n' */
475 strbuf_splice(&tmp
, start
+ strlen("encoding "),
476 len
- strlen("encoding \n"),
477 encoding
, strlen(encoding
));
479 return strbuf_detach(&tmp
, NULL
);
482 char *logmsg_reencode(const struct commit
*commit
,
483 const char *output_encoding
)
485 static const char *utf8
= "UTF-8";
486 const char *use_encoding
;
490 if (!*output_encoding
)
492 encoding
= get_header(commit
, "encoding");
493 use_encoding
= encoding
? encoding
: utf8
;
494 if (!strcmp(use_encoding
, output_encoding
))
495 if (encoding
) /* we'll strip encoding header later */
496 out
= xstrdup(commit
->buffer
);
498 return NULL
; /* nothing to do */
500 out
= reencode_string(commit
->buffer
,
501 output_encoding
, use_encoding
);
503 out
= replace_encoding_header(out
, output_encoding
);
509 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
511 static struct string_list
*mail_map
;
513 mail_map
= xcalloc(1, sizeof(*mail_map
));
514 read_mailmap(mail_map
, NULL
);
516 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
519 static size_t format_person_part(struct strbuf
*sb
, char part
,
520 const char *msg
, int len
, enum date_mode dmode
)
522 /* currently all placeholders have same length */
523 const int placeholder_len
= 2;
524 int start
, end
, tz
= 0;
525 unsigned long date
= 0;
527 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
528 char person_name
[1024];
529 char person_mail
[1024];
531 /* advance 'end' to point to email start delimiter */
532 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
536 * When end points at the '<' that we found, it should have
537 * matching '>' later, which means 'end' must be strictly
543 /* Seek for both name and email part */
546 while (name_end
> name_start
&& isspace(*(name_end
-1)))
548 mail_start
= msg
+end
+1;
549 mail_end
= mail_start
;
550 while (mail_end
< msg_end
&& *mail_end
!= '>')
552 if (mail_end
== msg_end
)
556 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
557 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
558 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
559 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
560 name_start
= person_name
;
561 name_end
= name_start
+ strlen(person_name
);
562 mail_start
= person_mail
;
563 mail_end
= mail_start
+ strlen(person_mail
);
565 if (part
== 'n' || part
== 'N') { /* name */
566 strbuf_add(sb
, name_start
, name_end
-name_start
);
567 return placeholder_len
;
569 if (part
== 'e' || part
== 'E') { /* email */
570 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
571 return placeholder_len
;
574 /* advance 'start' to point to date start delimiter */
575 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
579 date
= strtoul(msg
+ start
, &ep
, 10);
580 if (msg
+ start
== ep
)
583 if (part
== 't') { /* date, UNIX timestamp */
584 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
585 return placeholder_len
;
589 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
591 if (start
+ 1 < len
) {
592 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
593 if (msg
[start
] == '-')
599 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
600 return placeholder_len
;
601 case 'D': /* date, RFC2822 style */
602 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
603 return placeholder_len
;
604 case 'r': /* date, relative */
605 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
606 return placeholder_len
;
607 case 'i': /* date, ISO 8601 */
608 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
609 return placeholder_len
;
614 * bogus commit, 'sb' cannot be updated, but we still need to
615 * compute a valid return value.
617 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
618 || part
== 'D' || part
== 'r' || part
== 'i')
619 return placeholder_len
;
621 return 0; /* unknown placeholder */
629 struct format_commit_context
{
630 const struct commit
*commit
;
631 const struct pretty_print_context
*pretty_ctx
;
632 unsigned commit_header_parsed
:1;
633 unsigned commit_message_parsed
:1;
635 size_t width
, indent1
, indent2
;
637 /* These offsets are relative to the start of the commit message. */
639 struct chunk committer
;
640 struct chunk encoding
;
645 /* The following ones are relative to the result struct strbuf. */
646 struct chunk abbrev_commit_hash
;
647 struct chunk abbrev_tree_hash
;
648 struct chunk abbrev_parent_hashes
;
652 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
655 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
660 * We haven't seen this chunk before. Our caller is surely
661 * going to add it the hard way now. Remember the most likely
662 * start of the to-be-added chunk: the current end of the
665 chunk
->off
= sb
->len
;
669 static void parse_commit_header(struct format_commit_context
*context
)
671 const char *msg
= context
->message
;
674 for (i
= 0; msg
[i
]; i
++) {
676 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
681 } else if (!prefixcmp(msg
+ i
, "author ")) {
682 context
->author
.off
= i
+ 7;
683 context
->author
.len
= eol
- i
- 7;
684 } else if (!prefixcmp(msg
+ i
, "committer ")) {
685 context
->committer
.off
= i
+ 10;
686 context
->committer
.len
= eol
- i
- 10;
687 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
688 context
->encoding
.off
= i
+ 9;
689 context
->encoding
.len
= eol
- i
- 9;
693 context
->message_off
= i
;
694 context
->commit_header_parsed
= 1;
697 static int istitlechar(char c
)
699 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
700 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
703 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
706 size_t start_len
= sb
->len
;
709 for (; *msg
&& *msg
!= '\n'; msg
++) {
710 if (istitlechar(*msg
)) {
712 strbuf_addch(sb
, '-');
714 strbuf_addch(sb
, *msg
);
716 while (*(msg
+1) == '.')
722 /* trim any trailing '.' or '-' characters */
724 while (sb
->len
- trimlen
> start_len
&&
725 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
726 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
728 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
731 const char *format_subject(struct strbuf
*sb
, const char *msg
,
732 const char *line_separator
)
737 const char *line
= msg
;
738 int linelen
= get_one_line(line
);
741 if (!linelen
|| is_empty_line(line
, &linelen
))
746 strbuf_grow(sb
, linelen
+ 2);
748 strbuf_addstr(sb
, line_separator
);
749 strbuf_add(sb
, line
, linelen
);
755 static void parse_commit_message(struct format_commit_context
*c
)
757 const char *msg
= c
->message
+ c
->message_off
;
758 const char *start
= c
->message
;
760 msg
= skip_empty_lines(msg
);
761 c
->subject_off
= msg
- start
;
763 msg
= format_subject(NULL
, msg
, NULL
);
764 msg
= skip_empty_lines(msg
);
765 c
->body_off
= msg
- start
;
767 c
->commit_message_parsed
= 1;
770 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
772 struct name_decoration
*d
;
773 const char *prefix
= " (";
775 load_ref_decorations(DECORATE_SHORT_REFS
);
776 d
= lookup_decoration(&name_decoration
, &commit
->object
);
778 strbuf_addstr(sb
, prefix
);
780 strbuf_addstr(sb
, d
->name
);
783 if (prefix
[0] == ',')
784 strbuf_addch(sb
, ')');
787 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
788 size_t width
, size_t indent1
, size_t indent2
)
790 struct strbuf tmp
= STRBUF_INIT
;
793 strbuf_add(&tmp
, sb
->buf
, pos
);
794 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
795 (int) indent1
, (int) indent2
, (int) width
);
796 strbuf_swap(&tmp
, sb
);
797 strbuf_release(&tmp
);
800 static void rewrap_message_tail(struct strbuf
*sb
,
801 struct format_commit_context
*c
,
802 size_t new_width
, size_t new_indent1
,
805 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
806 c
->indent2
== new_indent2
)
808 if (c
->wrap_start
< sb
->len
)
809 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
810 c
->wrap_start
= sb
->len
;
811 c
->width
= new_width
;
812 c
->indent1
= new_indent1
;
813 c
->indent2
= new_indent2
;
816 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
819 struct format_commit_context
*c
= context
;
820 const struct commit
*commit
= c
->commit
;
821 const char *msg
= c
->message
;
822 struct commit_list
*p
;
825 /* these are independent of the commit */
826 switch (placeholder
[0]) {
828 if (placeholder
[1] == '(') {
829 const char *end
= strchr(placeholder
+ 2, ')');
830 char color
[COLOR_MAXLEN
];
833 color_parse_mem(placeholder
+ 2,
834 end
- (placeholder
+ 2),
835 "--pretty format", color
);
836 strbuf_addstr(sb
, color
);
837 return end
- placeholder
+ 1;
839 if (!prefixcmp(placeholder
+ 1, "red")) {
840 strbuf_addstr(sb
, GIT_COLOR_RED
);
842 } else if (!prefixcmp(placeholder
+ 1, "green")) {
843 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
845 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
846 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
848 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
849 strbuf_addstr(sb
, GIT_COLOR_RESET
);
853 case 'n': /* newline */
854 strbuf_addch(sb
, '\n');
857 /* %x00 == NUL, %x0a == LF, etc. */
858 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
860 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
862 strbuf_addch(sb
, (h1
<<4)|h2
);
867 if (placeholder
[1] == '(') {
868 unsigned long width
= 0, indent1
= 0, indent2
= 0;
870 const char *start
= placeholder
+ 2;
871 const char *end
= strchr(start
, ')');
875 width
= strtoul(start
, &next
, 10);
877 indent1
= strtoul(next
+ 1, &next
, 10);
879 indent2
= strtoul(next
+ 1,
886 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
887 return end
- placeholder
+ 1;
892 /* these depend on the commit */
893 if (!commit
->object
.parsed
)
894 parse_object(commit
->object
.sha1
);
896 switch (placeholder
[0]) {
897 case 'H': /* commit hash */
898 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
900 case 'h': /* abbreviated commit hash */
901 if (add_again(sb
, &c
->abbrev_commit_hash
))
903 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
904 c
->pretty_ctx
->abbrev
));
905 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
907 case 'T': /* tree hash */
908 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
910 case 't': /* abbreviated tree hash */
911 if (add_again(sb
, &c
->abbrev_tree_hash
))
913 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
914 c
->pretty_ctx
->abbrev
));
915 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
917 case 'P': /* parent hashes */
918 for (p
= commit
->parents
; p
; p
= p
->next
) {
919 if (p
!= commit
->parents
)
920 strbuf_addch(sb
, ' ');
921 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
924 case 'p': /* abbreviated parent hashes */
925 if (add_again(sb
, &c
->abbrev_parent_hashes
))
927 for (p
= commit
->parents
; p
; p
= p
->next
) {
928 if (p
!= commit
->parents
)
929 strbuf_addch(sb
, ' ');
930 strbuf_addstr(sb
, find_unique_abbrev(
931 p
->item
->object
.sha1
,
932 c
->pretty_ctx
->abbrev
));
934 c
->abbrev_parent_hashes
.len
= sb
->len
-
935 c
->abbrev_parent_hashes
.off
;
937 case 'm': /* left/right/bottom */
938 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
941 format_decoration(sb
, commit
);
943 case 'g': /* reflog info */
944 switch(placeholder
[1]) {
945 case 'd': /* reflog selector */
947 if (c
->pretty_ctx
->reflog_info
)
948 get_reflog_selector(sb
,
949 c
->pretty_ctx
->reflog_info
,
950 c
->pretty_ctx
->date_mode
,
951 (placeholder
[1] == 'd'));
953 case 's': /* reflog message */
954 if (c
->pretty_ctx
->reflog_info
)
955 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
958 return 0; /* unknown %g placeholder */
960 if (c
->pretty_ctx
->show_notes
) {
961 format_display_notes(commit
->object
.sha1
, sb
,
962 get_log_output_encoding(), 0);
968 /* For the rest we have to parse the commit header. */
969 if (!c
->commit_header_parsed
)
970 parse_commit_header(c
);
972 switch (placeholder
[0]) {
973 case 'a': /* author ... */
974 return format_person_part(sb
, placeholder
[1],
975 msg
+ c
->author
.off
, c
->author
.len
,
976 c
->pretty_ctx
->date_mode
);
977 case 'c': /* committer ... */
978 return format_person_part(sb
, placeholder
[1],
979 msg
+ c
->committer
.off
, c
->committer
.len
,
980 c
->pretty_ctx
->date_mode
);
981 case 'e': /* encoding */
982 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
984 case 'B': /* raw body */
985 /* message_off is always left at the initial newline */
986 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
990 /* Now we need to parse the commit message. */
991 if (!c
->commit_message_parsed
)
992 parse_commit_message(c
);
994 switch (placeholder
[0]) {
995 case 's': /* subject */
996 format_subject(sb
, msg
+ c
->subject_off
, " ");
998 case 'f': /* sanitized subject */
999 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
1001 case 'b': /* body */
1002 strbuf_addstr(sb
, msg
+ c
->body_off
);
1005 return 0; /* unknown placeholder */
1008 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
1015 ADD_LF_BEFORE_NON_EMPTY
,
1016 DEL_LF_BEFORE_EMPTY
,
1017 ADD_SP_BEFORE_NON_EMPTY
1020 switch (placeholder
[0]) {
1022 magic
= DEL_LF_BEFORE_EMPTY
;
1025 magic
= ADD_LF_BEFORE_NON_EMPTY
;
1028 magic
= ADD_SP_BEFORE_NON_EMPTY
;
1033 if (magic
!= NO_MAGIC
)
1037 consumed
= format_commit_one(sb
, placeholder
, context
);
1038 if (magic
== NO_MAGIC
)
1041 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
1042 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
1043 strbuf_setlen(sb
, sb
->len
- 1);
1044 } else if (orig_len
!= sb
->len
) {
1045 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1046 strbuf_insert(sb
, orig_len
, "\n", 1);
1047 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1048 strbuf_insert(sb
, orig_len
, " ", 1);
1050 return consumed
+ 1;
1053 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
1056 struct userformat_want
*w
= context
;
1058 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1061 switch (*placeholder
) {
1069 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1071 struct strbuf dummy
= STRBUF_INIT
;
1078 strbuf_expand(&dummy
, fmt
, userformat_want_item
, w
);
1079 strbuf_release(&dummy
);
1082 void format_commit_message(const struct commit
*commit
,
1083 const char *format
, struct strbuf
*sb
,
1084 const struct pretty_print_context
*pretty_ctx
)
1086 struct format_commit_context context
;
1087 static const char utf8
[] = "UTF-8";
1089 const char *output_enc
= pretty_ctx
->output_encoding
;
1091 memset(&context
, 0, sizeof(context
));
1092 context
.commit
= commit
;
1093 context
.pretty_ctx
= pretty_ctx
;
1094 context
.wrap_start
= sb
->len
;
1095 context
.message
= commit
->buffer
;
1097 enc
= get_header(commit
, "encoding");
1098 enc
= enc
? enc
: utf8
;
1099 if (strcmp(enc
, output_enc
))
1100 context
.message
= logmsg_reencode(commit
, output_enc
);
1103 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1104 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1106 if (context
.message
!= commit
->buffer
)
1107 free(context
.message
);
1110 static void pp_header(enum cmit_fmt fmt
,
1112 enum date_mode dmode
,
1113 const char *encoding
,
1114 const struct commit
*commit
,
1118 int parents_shown
= 0;
1121 const char *line
= *msg_p
;
1122 int linelen
= get_one_line(*msg_p
);
1132 if (fmt
== CMIT_FMT_RAW
) {
1133 strbuf_add(sb
, line
, linelen
);
1137 if (!memcmp(line
, "parent ", 7)) {
1139 die("bad parent line in commit");
1143 if (!parents_shown
) {
1144 struct commit_list
*parent
;
1146 for (parent
= commit
->parents
, num
= 0;
1148 parent
= parent
->next
, num
++)
1150 /* with enough slop */
1151 strbuf_grow(sb
, num
* 50 + 20);
1152 add_merge_info(fmt
, sb
, commit
, abbrev
);
1157 * MEDIUM == DEFAULT shows only author with dates.
1158 * FULL shows both authors but not dates.
1159 * FULLER shows both authors and dates.
1161 if (!memcmp(line
, "author ", 7)) {
1162 strbuf_grow(sb
, linelen
+ 80);
1163 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
1165 if (!memcmp(line
, "committer ", 10) &&
1166 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
1167 strbuf_grow(sb
, linelen
+ 80);
1168 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
1173 void pp_title_line(enum cmit_fmt fmt
,
1176 const char *subject
,
1177 const char *after_subject
,
1178 const char *encoding
,
1181 struct strbuf title
;
1183 strbuf_init(&title
, 80);
1184 *msg_p
= format_subject(&title
, *msg_p
, " ");
1186 strbuf_grow(sb
, title
.len
+ 1024);
1188 strbuf_addstr(sb
, subject
);
1189 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
1191 strbuf_addbuf(sb
, &title
);
1193 strbuf_addch(sb
, '\n');
1195 if (need_8bit_cte
> 0) {
1196 const char *header_fmt
=
1197 "MIME-Version: 1.0\n"
1198 "Content-Type: text/plain; charset=%s\n"
1199 "Content-Transfer-Encoding: 8bit\n";
1200 strbuf_addf(sb
, header_fmt
, encoding
);
1202 if (after_subject
) {
1203 strbuf_addstr(sb
, after_subject
);
1205 if (fmt
== CMIT_FMT_EMAIL
) {
1206 strbuf_addch(sb
, '\n');
1208 strbuf_release(&title
);
1211 void pp_remainder(enum cmit_fmt fmt
,
1218 const char *line
= *msg_p
;
1219 int linelen
= get_one_line(line
);
1225 if (is_empty_line(line
, &linelen
)) {
1228 if (fmt
== CMIT_FMT_SHORT
)
1233 strbuf_grow(sb
, linelen
+ indent
+ 20);
1235 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1236 strbuf_setlen(sb
, sb
->len
+ indent
);
1238 strbuf_add(sb
, line
, linelen
);
1239 strbuf_addch(sb
, '\n');
1243 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1245 const char *encoding
;
1247 encoding
= get_log_output_encoding();
1249 *encoding_p
= encoding
;
1250 return logmsg_reencode(commit
, encoding
);
1253 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
1255 const struct pretty_print_context
*context
)
1257 unsigned long beginning_of_body
;
1259 const char *msg
= commit
->buffer
;
1261 const char *encoding
;
1262 int need_8bit_cte
= context
->need_8bit_cte
;
1264 if (fmt
== CMIT_FMT_USERFORMAT
) {
1265 format_commit_message(commit
, user_format
, sb
, context
);
1269 reencoded
= reencode_commit_message(commit
, &encoding
);
1274 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1278 * We need to check and emit Content-type: to mark it
1279 * as 8-bit if we haven't done so.
1281 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1284 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1286 /* author could be non 7-bit ASCII but
1287 * the log may be so; skip over the
1288 * header part first.
1290 if (ch
== '\n' && msg
[i
+1] == '\n')
1293 else if (non_ascii(ch
)) {
1300 pp_header(fmt
, context
->abbrev
, context
->date_mode
, encoding
,
1302 if (fmt
!= CMIT_FMT_ONELINE
&& !context
->subject
) {
1303 strbuf_addch(sb
, '\n');
1306 /* Skip excess blank lines at the beginning of body, if any... */
1307 msg
= skip_empty_lines(msg
);
1309 /* These formats treat the title line specially. */
1310 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1311 pp_title_line(fmt
, &msg
, sb
, context
->subject
,
1312 context
->after_subject
, encoding
, need_8bit_cte
);
1314 beginning_of_body
= sb
->len
;
1315 if (fmt
!= CMIT_FMT_ONELINE
)
1316 pp_remainder(fmt
, &msg
, sb
, indent
);
1319 /* Make sure there is an EOLN for the non-oneline case */
1320 if (fmt
!= CMIT_FMT_ONELINE
)
1321 strbuf_addch(sb
, '\n');
1324 * The caller may append additional body text in e-mail
1325 * format. Make sure we did not strip the blank line
1326 * between the header and the body.
1328 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1329 strbuf_addch(sb
, '\n');
1331 if (context
->show_notes
)
1332 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1333 NOTES_SHOW_HEADER
| NOTES_INDENT
);