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_rfc2047_special(char ch
)
213 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
216 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
217 const char *encoding
)
219 static const int max_length
= 78; /* per rfc2822 */
223 /* How many bytes are already used on the current line? */
224 for (i
= sb
->len
- 1; i
>= 0; i
--)
225 if (sb
->buf
[i
] == '\n')
227 line_len
= sb
->len
- (i
+1);
229 for (i
= 0; i
< len
; i
++) {
231 if (non_ascii(ch
) || ch
== '\n')
233 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
236 strbuf_add_wrapped_bytes(sb
, line
, len
, 0, 1, max_length
- line_len
);
240 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
241 strbuf_addf(sb
, "=?%s?q?", encoding
);
242 line_len
+= strlen(encoding
) + 5; /* 5 for =??q? */
243 for (i
= 0; i
< len
; i
++) {
244 unsigned ch
= line
[i
] & 0xFF;
246 if (line_len
>= max_length
- 2) {
247 strbuf_addf(sb
, "?=\n =?%s?q?", encoding
);
248 line_len
= strlen(encoding
) + 5 + 1; /* =??q? plus SP */
252 * We encode ' ' using '=20' even though rfc2047
253 * allows using '_' for readability. Unfortunately,
254 * many programs do not understand this and just
255 * leave the underscore in place.
257 if (is_rfc2047_special(ch
) || ch
== ' ' || ch
== '\n') {
258 strbuf_addf(sb
, "=%02X", ch
);
262 strbuf_addch(sb
, ch
);
266 strbuf_addstr(sb
, "?=");
269 void pp_user_info(const struct pretty_print_context
*pp
,
270 const char *what
, struct strbuf
*sb
,
271 const char *line
, const char *encoding
)
278 if (pp
->fmt
== CMIT_FMT_ONELINE
)
280 date
= strchr(line
, '>');
283 namelen
= ++date
- line
;
284 time
= strtoul(date
, &date
, 10);
285 tz
= strtol(date
, NULL
, 10);
287 if (pp
->fmt
== CMIT_FMT_EMAIL
) {
288 char *name_tail
= strchr(line
, '<');
289 int display_name_length
;
293 while (line
< name_tail
&& isspace(name_tail
[-1]))
295 display_name_length
= name_tail
- line
;
296 strbuf_addstr(sb
, "From: ");
297 add_rfc2047(sb
, line
, display_name_length
, encoding
);
298 for (final_line
= 0; final_line
< sb
->len
; final_line
++)
299 if (sb
->buf
[sb
->len
- final_line
- 1] == '\n')
301 if (namelen
- display_name_length
+ final_line
> 78) {
302 strbuf_addch(sb
, '\n');
303 if (!isspace(name_tail
[0]))
304 strbuf_addch(sb
, ' ');
306 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
307 strbuf_addch(sb
, '\n');
309 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
310 (pp
->fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
314 case CMIT_FMT_MEDIUM
:
315 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, pp
->date_mode
));
318 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
320 case CMIT_FMT_FULLER
:
321 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, pp
->date_mode
));
329 static int is_empty_line(const char *line
, int *len_p
)
332 while (len
&& isspace(line
[len
-1]))
338 static const char *skip_empty_lines(const char *msg
)
341 int linelen
= get_one_line(msg
);
345 if (!is_empty_line(msg
, &ll
))
352 static void add_merge_info(const struct pretty_print_context
*pp
,
353 struct strbuf
*sb
, const struct commit
*commit
)
355 struct commit_list
*parent
= commit
->parents
;
357 if ((pp
->fmt
== CMIT_FMT_ONELINE
) || (pp
->fmt
== CMIT_FMT_EMAIL
) ||
358 !parent
|| !parent
->next
)
361 strbuf_addstr(sb
, "Merge:");
364 struct commit
*p
= parent
->item
;
365 const char *hex
= NULL
;
367 hex
= find_unique_abbrev(p
->object
.sha1
, pp
->abbrev
);
369 hex
= sha1_to_hex(p
->object
.sha1
);
370 parent
= parent
->next
;
372 strbuf_addf(sb
, " %s", hex
);
374 strbuf_addch(sb
, '\n');
377 static char *get_header(const struct commit
*commit
, const char *key
)
379 int key_len
= strlen(key
);
380 const char *line
= commit
->buffer
;
383 const char *eol
= strchr(line
, '\n'), *next
;
388 eol
= line
+ strlen(line
);
392 if (eol
- line
> key_len
&&
393 !strncmp(line
, key
, key_len
) &&
394 line
[key_len
] == ' ') {
395 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
401 static char *replace_encoding_header(char *buf
, const char *encoding
)
403 struct strbuf tmp
= STRBUF_INIT
;
407 /* guess if there is an encoding header before a \n\n */
408 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
409 cp
= strchr(cp
, '\n');
410 if (!cp
|| *++cp
== '\n')
414 cp
= strchr(cp
, '\n');
416 return buf
; /* should not happen but be defensive */
417 len
= cp
+ 1 - (buf
+ start
);
419 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
420 if (is_encoding_utf8(encoding
)) {
421 /* we have re-coded to UTF-8; drop the header */
422 strbuf_remove(&tmp
, start
, len
);
424 /* just replaces XXXX in 'encoding XXXX\n' */
425 strbuf_splice(&tmp
, start
+ strlen("encoding "),
426 len
- strlen("encoding \n"),
427 encoding
, strlen(encoding
));
429 return strbuf_detach(&tmp
, NULL
);
432 char *logmsg_reencode(const struct commit
*commit
,
433 const char *output_encoding
)
435 static const char *utf8
= "UTF-8";
436 const char *use_encoding
;
440 if (!*output_encoding
)
442 encoding
= get_header(commit
, "encoding");
443 use_encoding
= encoding
? encoding
: utf8
;
444 if (!strcmp(use_encoding
, output_encoding
))
445 if (encoding
) /* we'll strip encoding header later */
446 out
= xstrdup(commit
->buffer
);
448 return NULL
; /* nothing to do */
450 out
= reencode_string(commit
->buffer
,
451 output_encoding
, use_encoding
);
453 out
= replace_encoding_header(out
, output_encoding
);
459 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
461 static struct string_list
*mail_map
;
463 mail_map
= xcalloc(1, sizeof(*mail_map
));
464 read_mailmap(mail_map
, NULL
);
466 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
469 static size_t format_person_part(struct strbuf
*sb
, char part
,
470 const char *msg
, int len
, enum date_mode dmode
)
472 /* currently all placeholders have same length */
473 const int placeholder_len
= 2;
474 int start
, end
, tz
= 0;
475 unsigned long date
= 0;
477 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
478 char person_name
[1024];
479 char person_mail
[1024];
481 /* advance 'end' to point to email start delimiter */
482 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
486 * When end points at the '<' that we found, it should have
487 * matching '>' later, which means 'end' must be strictly
493 /* Seek for both name and email part */
496 while (name_end
> name_start
&& isspace(*(name_end
-1)))
498 mail_start
= msg
+end
+1;
499 mail_end
= mail_start
;
500 while (mail_end
< msg_end
&& *mail_end
!= '>')
502 if (mail_end
== msg_end
)
506 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
507 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
508 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
509 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
510 name_start
= person_name
;
511 name_end
= name_start
+ strlen(person_name
);
512 mail_start
= person_mail
;
513 mail_end
= mail_start
+ strlen(person_mail
);
515 if (part
== 'n' || part
== 'N') { /* name */
516 strbuf_add(sb
, name_start
, name_end
-name_start
);
517 return placeholder_len
;
519 if (part
== 'e' || part
== 'E') { /* email */
520 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
521 return placeholder_len
;
524 /* advance 'start' to point to date start delimiter */
525 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
529 date
= strtoul(msg
+ start
, &ep
, 10);
530 if (msg
+ start
== ep
)
533 if (part
== 't') { /* date, UNIX timestamp */
534 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
535 return placeholder_len
;
539 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
541 if (start
+ 1 < len
) {
542 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
543 if (msg
[start
] == '-')
549 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
550 return placeholder_len
;
551 case 'D': /* date, RFC2822 style */
552 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
553 return placeholder_len
;
554 case 'r': /* date, relative */
555 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
556 return placeholder_len
;
557 case 'i': /* date, ISO 8601 */
558 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
559 return placeholder_len
;
564 * bogus commit, 'sb' cannot be updated, but we still need to
565 * compute a valid return value.
567 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
568 || part
== 'D' || part
== 'r' || part
== 'i')
569 return placeholder_len
;
571 return 0; /* unknown placeholder */
579 struct format_commit_context
{
580 const struct commit
*commit
;
581 const struct pretty_print_context
*pretty_ctx
;
582 unsigned commit_header_parsed
:1;
583 unsigned commit_message_parsed
:1;
585 size_t width
, indent1
, indent2
;
587 /* These offsets are relative to the start of the commit message. */
589 struct chunk committer
;
590 struct chunk encoding
;
595 /* The following ones are relative to the result struct strbuf. */
596 struct chunk abbrev_commit_hash
;
597 struct chunk abbrev_tree_hash
;
598 struct chunk abbrev_parent_hashes
;
602 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
605 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
610 * We haven't seen this chunk before. Our caller is surely
611 * going to add it the hard way now. Remember the most likely
612 * start of the to-be-added chunk: the current end of the
615 chunk
->off
= sb
->len
;
619 static void parse_commit_header(struct format_commit_context
*context
)
621 const char *msg
= context
->message
;
624 for (i
= 0; msg
[i
]; i
++) {
626 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
631 } else if (!prefixcmp(msg
+ i
, "author ")) {
632 context
->author
.off
= i
+ 7;
633 context
->author
.len
= eol
- i
- 7;
634 } else if (!prefixcmp(msg
+ i
, "committer ")) {
635 context
->committer
.off
= i
+ 10;
636 context
->committer
.len
= eol
- i
- 10;
637 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
638 context
->encoding
.off
= i
+ 9;
639 context
->encoding
.len
= eol
- i
- 9;
643 context
->message_off
= i
;
644 context
->commit_header_parsed
= 1;
647 static int istitlechar(char c
)
649 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
650 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
653 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
656 size_t start_len
= sb
->len
;
659 for (; *msg
&& *msg
!= '\n'; msg
++) {
660 if (istitlechar(*msg
)) {
662 strbuf_addch(sb
, '-');
664 strbuf_addch(sb
, *msg
);
666 while (*(msg
+1) == '.')
672 /* trim any trailing '.' or '-' characters */
674 while (sb
->len
- trimlen
> start_len
&&
675 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
676 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
678 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
681 const char *format_subject(struct strbuf
*sb
, const char *msg
,
682 const char *line_separator
)
687 const char *line
= msg
;
688 int linelen
= get_one_line(line
);
691 if (!linelen
|| is_empty_line(line
, &linelen
))
696 strbuf_grow(sb
, linelen
+ 2);
698 strbuf_addstr(sb
, line_separator
);
699 strbuf_add(sb
, line
, linelen
);
705 static void parse_commit_message(struct format_commit_context
*c
)
707 const char *msg
= c
->message
+ c
->message_off
;
708 const char *start
= c
->message
;
710 msg
= skip_empty_lines(msg
);
711 c
->subject_off
= msg
- start
;
713 msg
= format_subject(NULL
, msg
, NULL
);
714 msg
= skip_empty_lines(msg
);
715 c
->body_off
= msg
- start
;
717 c
->commit_message_parsed
= 1;
720 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
722 struct name_decoration
*d
;
723 const char *prefix
= " (";
725 load_ref_decorations(DECORATE_SHORT_REFS
);
726 d
= lookup_decoration(&name_decoration
, &commit
->object
);
728 strbuf_addstr(sb
, prefix
);
730 strbuf_addstr(sb
, d
->name
);
733 if (prefix
[0] == ',')
734 strbuf_addch(sb
, ')');
737 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
738 size_t width
, size_t indent1
, size_t indent2
)
740 struct strbuf tmp
= STRBUF_INIT
;
743 strbuf_add(&tmp
, sb
->buf
, pos
);
744 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
745 (int) indent1
, (int) indent2
, (int) width
);
746 strbuf_swap(&tmp
, sb
);
747 strbuf_release(&tmp
);
750 static void rewrap_message_tail(struct strbuf
*sb
,
751 struct format_commit_context
*c
,
752 size_t new_width
, size_t new_indent1
,
755 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
756 c
->indent2
== new_indent2
)
758 if (c
->wrap_start
< sb
->len
)
759 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
760 c
->wrap_start
= sb
->len
;
761 c
->width
= new_width
;
762 c
->indent1
= new_indent1
;
763 c
->indent2
= new_indent2
;
766 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
769 struct format_commit_context
*c
= context
;
770 const struct commit
*commit
= c
->commit
;
771 const char *msg
= c
->message
;
772 struct commit_list
*p
;
775 /* these are independent of the commit */
776 switch (placeholder
[0]) {
778 if (placeholder
[1] == '(') {
779 const char *end
= strchr(placeholder
+ 2, ')');
780 char color
[COLOR_MAXLEN
];
783 color_parse_mem(placeholder
+ 2,
784 end
- (placeholder
+ 2),
785 "--pretty format", color
);
786 strbuf_addstr(sb
, color
);
787 return end
- placeholder
+ 1;
789 if (!prefixcmp(placeholder
+ 1, "red")) {
790 strbuf_addstr(sb
, GIT_COLOR_RED
);
792 } else if (!prefixcmp(placeholder
+ 1, "green")) {
793 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
795 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
796 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
798 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
799 strbuf_addstr(sb
, GIT_COLOR_RESET
);
803 case 'n': /* newline */
804 strbuf_addch(sb
, '\n');
807 /* %x00 == NUL, %x0a == LF, etc. */
808 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
810 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
812 strbuf_addch(sb
, (h1
<<4)|h2
);
817 if (placeholder
[1] == '(') {
818 unsigned long width
= 0, indent1
= 0, indent2
= 0;
820 const char *start
= placeholder
+ 2;
821 const char *end
= strchr(start
, ')');
825 width
= strtoul(start
, &next
, 10);
827 indent1
= strtoul(next
+ 1, &next
, 10);
829 indent2
= strtoul(next
+ 1,
836 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
837 return end
- placeholder
+ 1;
842 /* these depend on the commit */
843 if (!commit
->object
.parsed
)
844 parse_object(commit
->object
.sha1
);
846 switch (placeholder
[0]) {
847 case 'H': /* commit hash */
848 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
850 case 'h': /* abbreviated commit hash */
851 if (add_again(sb
, &c
->abbrev_commit_hash
))
853 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
854 c
->pretty_ctx
->abbrev
));
855 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
857 case 'T': /* tree hash */
858 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
860 case 't': /* abbreviated tree hash */
861 if (add_again(sb
, &c
->abbrev_tree_hash
))
863 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
864 c
->pretty_ctx
->abbrev
));
865 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
867 case 'P': /* parent hashes */
868 for (p
= commit
->parents
; p
; p
= p
->next
) {
869 if (p
!= commit
->parents
)
870 strbuf_addch(sb
, ' ');
871 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
874 case 'p': /* abbreviated parent hashes */
875 if (add_again(sb
, &c
->abbrev_parent_hashes
))
877 for (p
= commit
->parents
; p
; p
= p
->next
) {
878 if (p
!= commit
->parents
)
879 strbuf_addch(sb
, ' ');
880 strbuf_addstr(sb
, find_unique_abbrev(
881 p
->item
->object
.sha1
,
882 c
->pretty_ctx
->abbrev
));
884 c
->abbrev_parent_hashes
.len
= sb
->len
-
885 c
->abbrev_parent_hashes
.off
;
887 case 'm': /* left/right/bottom */
888 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
890 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
895 format_decoration(sb
, commit
);
897 case 'g': /* reflog info */
898 switch(placeholder
[1]) {
899 case 'd': /* reflog selector */
901 if (c
->pretty_ctx
->reflog_info
)
902 get_reflog_selector(sb
,
903 c
->pretty_ctx
->reflog_info
,
904 c
->pretty_ctx
->date_mode
,
905 (placeholder
[1] == 'd'));
907 case 's': /* reflog message */
908 if (c
->pretty_ctx
->reflog_info
)
909 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
912 return 0; /* unknown %g placeholder */
914 if (c
->pretty_ctx
->show_notes
) {
915 format_display_notes(commit
->object
.sha1
, sb
,
916 get_log_output_encoding(), 0);
922 /* For the rest we have to parse the commit header. */
923 if (!c
->commit_header_parsed
)
924 parse_commit_header(c
);
926 switch (placeholder
[0]) {
927 case 'a': /* author ... */
928 return format_person_part(sb
, placeholder
[1],
929 msg
+ c
->author
.off
, c
->author
.len
,
930 c
->pretty_ctx
->date_mode
);
931 case 'c': /* committer ... */
932 return format_person_part(sb
, placeholder
[1],
933 msg
+ c
->committer
.off
, c
->committer
.len
,
934 c
->pretty_ctx
->date_mode
);
935 case 'e': /* encoding */
936 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
938 case 'B': /* raw body */
939 /* message_off is always left at the initial newline */
940 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
944 /* Now we need to parse the commit message. */
945 if (!c
->commit_message_parsed
)
946 parse_commit_message(c
);
948 switch (placeholder
[0]) {
949 case 's': /* subject */
950 format_subject(sb
, msg
+ c
->subject_off
, " ");
952 case 'f': /* sanitized subject */
953 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
956 strbuf_addstr(sb
, msg
+ c
->body_off
);
959 return 0; /* unknown placeholder */
962 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
969 ADD_LF_BEFORE_NON_EMPTY
,
971 ADD_SP_BEFORE_NON_EMPTY
974 switch (placeholder
[0]) {
976 magic
= DEL_LF_BEFORE_EMPTY
;
979 magic
= ADD_LF_BEFORE_NON_EMPTY
;
982 magic
= ADD_SP_BEFORE_NON_EMPTY
;
987 if (magic
!= NO_MAGIC
)
991 consumed
= format_commit_one(sb
, placeholder
, context
);
992 if (magic
== NO_MAGIC
)
995 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
996 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
997 strbuf_setlen(sb
, sb
->len
- 1);
998 } else if (orig_len
!= sb
->len
) {
999 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
1000 strbuf_insert(sb
, orig_len
, "\n", 1);
1001 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
1002 strbuf_insert(sb
, orig_len
, " ", 1);
1004 return consumed
+ 1;
1007 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
1010 struct userformat_want
*w
= context
;
1012 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1015 switch (*placeholder
) {
1023 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1025 struct strbuf dummy
= STRBUF_INIT
;
1032 strbuf_expand(&dummy
, user_format
, userformat_want_item
, w
);
1033 strbuf_release(&dummy
);
1036 void format_commit_message(const struct commit
*commit
,
1037 const char *format
, struct strbuf
*sb
,
1038 const struct pretty_print_context
*pretty_ctx
)
1040 struct format_commit_context context
;
1041 static const char utf8
[] = "UTF-8";
1043 const char *output_enc
= pretty_ctx
->output_encoding
;
1045 memset(&context
, 0, sizeof(context
));
1046 context
.commit
= commit
;
1047 context
.pretty_ctx
= pretty_ctx
;
1048 context
.wrap_start
= sb
->len
;
1049 context
.message
= commit
->buffer
;
1051 enc
= get_header(commit
, "encoding");
1052 enc
= enc
? enc
: utf8
;
1053 if (strcmp(enc
, output_enc
))
1054 context
.message
= logmsg_reencode(commit
, output_enc
);
1057 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1058 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1060 if (context
.message
!= commit
->buffer
)
1061 free(context
.message
);
1064 static void pp_header(const struct pretty_print_context
*pp
,
1065 const char *encoding
,
1066 const struct commit
*commit
,
1070 int parents_shown
= 0;
1073 const char *line
= *msg_p
;
1074 int linelen
= get_one_line(*msg_p
);
1084 if (pp
->fmt
== CMIT_FMT_RAW
) {
1085 strbuf_add(sb
, line
, linelen
);
1089 if (!memcmp(line
, "parent ", 7)) {
1091 die("bad parent line in commit");
1095 if (!parents_shown
) {
1096 struct commit_list
*parent
;
1098 for (parent
= commit
->parents
, num
= 0;
1100 parent
= parent
->next
, num
++)
1102 /* with enough slop */
1103 strbuf_grow(sb
, num
* 50 + 20);
1104 add_merge_info(pp
, sb
, commit
);
1109 * MEDIUM == DEFAULT shows only author with dates.
1110 * FULL shows both authors but not dates.
1111 * FULLER shows both authors and dates.
1113 if (!memcmp(line
, "author ", 7)) {
1114 strbuf_grow(sb
, linelen
+ 80);
1115 pp_user_info(pp
, "Author", sb
, line
+ 7, encoding
);
1117 if (!memcmp(line
, "committer ", 10) &&
1118 (pp
->fmt
== CMIT_FMT_FULL
|| pp
->fmt
== CMIT_FMT_FULLER
)) {
1119 strbuf_grow(sb
, linelen
+ 80);
1120 pp_user_info(pp
, "Commit", sb
, line
+ 10, encoding
);
1125 void pp_title_line(const struct pretty_print_context
*pp
,
1128 const char *encoding
,
1131 struct strbuf title
;
1133 strbuf_init(&title
, 80);
1134 *msg_p
= format_subject(&title
, *msg_p
,
1135 pp
->preserve_subject
? "\n" : " ");
1137 strbuf_grow(sb
, title
.len
+ 1024);
1139 strbuf_addstr(sb
, pp
->subject
);
1140 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
1142 strbuf_addbuf(sb
, &title
);
1144 strbuf_addch(sb
, '\n');
1146 if (need_8bit_cte
> 0) {
1147 const char *header_fmt
=
1148 "MIME-Version: 1.0\n"
1149 "Content-Type: text/plain; charset=%s\n"
1150 "Content-Transfer-Encoding: 8bit\n";
1151 strbuf_addf(sb
, header_fmt
, encoding
);
1153 if (pp
->after_subject
) {
1154 strbuf_addstr(sb
, pp
->after_subject
);
1156 if (pp
->fmt
== CMIT_FMT_EMAIL
) {
1157 strbuf_addch(sb
, '\n');
1159 strbuf_release(&title
);
1162 void pp_remainder(const struct pretty_print_context
*pp
,
1169 const char *line
= *msg_p
;
1170 int linelen
= get_one_line(line
);
1176 if (is_empty_line(line
, &linelen
)) {
1179 if (pp
->fmt
== CMIT_FMT_SHORT
)
1184 strbuf_grow(sb
, linelen
+ indent
+ 20);
1186 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1187 strbuf_setlen(sb
, sb
->len
+ indent
);
1189 strbuf_add(sb
, line
, linelen
);
1190 strbuf_addch(sb
, '\n');
1194 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1196 const char *encoding
;
1198 encoding
= get_log_output_encoding();
1200 *encoding_p
= encoding
;
1201 return logmsg_reencode(commit
, encoding
);
1204 void pretty_print_commit(const struct pretty_print_context
*pp
,
1205 const struct commit
*commit
,
1208 unsigned long beginning_of_body
;
1210 const char *msg
= commit
->buffer
;
1212 const char *encoding
;
1213 int need_8bit_cte
= pp
->need_8bit_cte
;
1215 if (pp
->fmt
== CMIT_FMT_USERFORMAT
) {
1216 format_commit_message(commit
, user_format
, sb
, pp
);
1220 reencoded
= reencode_commit_message(commit
, &encoding
);
1225 if (pp
->fmt
== CMIT_FMT_ONELINE
|| pp
->fmt
== CMIT_FMT_EMAIL
)
1229 * We need to check and emit Content-type: to mark it
1230 * as 8-bit if we haven't done so.
1232 if (pp
->fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1235 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1237 /* author could be non 7-bit ASCII but
1238 * the log may be so; skip over the
1239 * header part first.
1241 if (ch
== '\n' && msg
[i
+1] == '\n')
1244 else if (non_ascii(ch
)) {
1251 pp_header(pp
, encoding
, commit
, &msg
, sb
);
1252 if (pp
->fmt
!= CMIT_FMT_ONELINE
&& !pp
->subject
) {
1253 strbuf_addch(sb
, '\n');
1256 /* Skip excess blank lines at the beginning of body, if any... */
1257 msg
= skip_empty_lines(msg
);
1259 /* These formats treat the title line specially. */
1260 if (pp
->fmt
== CMIT_FMT_ONELINE
|| pp
->fmt
== CMIT_FMT_EMAIL
)
1261 pp_title_line(pp
, &msg
, sb
, encoding
, need_8bit_cte
);
1263 beginning_of_body
= sb
->len
;
1264 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
1265 pp_remainder(pp
, &msg
, sb
, indent
);
1268 /* Make sure there is an EOLN for the non-oneline case */
1269 if (pp
->fmt
!= CMIT_FMT_ONELINE
)
1270 strbuf_addch(sb
, '\n');
1273 * The caller may append additional body text in e-mail
1274 * format. Make sure we did not strip the blank line
1275 * between the header and the body.
1277 if (pp
->fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1278 strbuf_addch(sb
, '\n');
1281 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1282 NOTES_SHOW_HEADER
| NOTES_INDENT
);
1287 void pp_commit_easy(enum cmit_fmt fmt
, const struct commit
*commit
,
1290 struct pretty_print_context pp
= {0};
1292 pretty_print_commit(&pp
, commit
, sb
);