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 char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
270 const char *line
, enum date_mode dmode
,
271 const char *encoding
)
278 if (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 (fmt
== CMIT_FMT_EMAIL
) {
288 char *name_tail
= strchr(line
, '<');
289 int display_name_length
;
292 while (line
< name_tail
&& isspace(name_tail
[-1]))
294 display_name_length
= name_tail
- line
;
295 strbuf_addstr(sb
, "From: ");
296 add_rfc2047(sb
, line
, display_name_length
, encoding
);
297 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
298 strbuf_addch(sb
, '\n');
300 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
301 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
305 case CMIT_FMT_MEDIUM
:
306 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
309 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
311 case CMIT_FMT_FULLER
:
312 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
320 static int is_empty_line(const char *line
, int *len_p
)
323 while (len
&& isspace(line
[len
-1]))
329 static const char *skip_empty_lines(const char *msg
)
332 int linelen
= get_one_line(msg
);
336 if (!is_empty_line(msg
, &ll
))
343 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
344 const struct commit
*commit
, int abbrev
)
346 struct commit_list
*parent
= commit
->parents
;
348 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
349 !parent
|| !parent
->next
)
352 strbuf_addstr(sb
, "Merge:");
355 struct commit
*p
= parent
->item
;
356 const char *hex
= NULL
;
358 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
360 hex
= sha1_to_hex(p
->object
.sha1
);
361 parent
= parent
->next
;
363 strbuf_addf(sb
, " %s", hex
);
365 strbuf_addch(sb
, '\n');
368 static char *get_header(const struct commit
*commit
, const char *key
)
370 int key_len
= strlen(key
);
371 const char *line
= commit
->buffer
;
374 const char *eol
= strchr(line
, '\n'), *next
;
379 eol
= line
+ strlen(line
);
383 if (eol
- line
> key_len
&&
384 !strncmp(line
, key
, key_len
) &&
385 line
[key_len
] == ' ') {
386 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
392 static char *replace_encoding_header(char *buf
, const char *encoding
)
394 struct strbuf tmp
= STRBUF_INIT
;
398 /* guess if there is an encoding header before a \n\n */
399 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
400 cp
= strchr(cp
, '\n');
401 if (!cp
|| *++cp
== '\n')
405 cp
= strchr(cp
, '\n');
407 return buf
; /* should not happen but be defensive */
408 len
= cp
+ 1 - (buf
+ start
);
410 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
411 if (is_encoding_utf8(encoding
)) {
412 /* we have re-coded to UTF-8; drop the header */
413 strbuf_remove(&tmp
, start
, len
);
415 /* just replaces XXXX in 'encoding XXXX\n' */
416 strbuf_splice(&tmp
, start
+ strlen("encoding "),
417 len
- strlen("encoding \n"),
418 encoding
, strlen(encoding
));
420 return strbuf_detach(&tmp
, NULL
);
423 char *logmsg_reencode(const struct commit
*commit
,
424 const char *output_encoding
)
426 static const char *utf8
= "UTF-8";
427 const char *use_encoding
;
431 if (!*output_encoding
)
433 encoding
= get_header(commit
, "encoding");
434 use_encoding
= encoding
? encoding
: utf8
;
435 if (!strcmp(use_encoding
, output_encoding
))
436 if (encoding
) /* we'll strip encoding header later */
437 out
= xstrdup(commit
->buffer
);
439 return NULL
; /* nothing to do */
441 out
= reencode_string(commit
->buffer
,
442 output_encoding
, use_encoding
);
444 out
= replace_encoding_header(out
, output_encoding
);
450 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
452 static struct string_list
*mail_map
;
454 mail_map
= xcalloc(1, sizeof(*mail_map
));
455 read_mailmap(mail_map
, NULL
);
457 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
460 static size_t format_person_part(struct strbuf
*sb
, char part
,
461 const char *msg
, int len
, enum date_mode dmode
)
463 /* currently all placeholders have same length */
464 const int placeholder_len
= 2;
465 int start
, end
, tz
= 0;
466 unsigned long date
= 0;
468 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
469 char person_name
[1024];
470 char person_mail
[1024];
472 /* advance 'end' to point to email start delimiter */
473 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
477 * When end points at the '<' that we found, it should have
478 * matching '>' later, which means 'end' must be strictly
484 /* Seek for both name and email part */
487 while (name_end
> name_start
&& isspace(*(name_end
-1)))
489 mail_start
= msg
+end
+1;
490 mail_end
= mail_start
;
491 while (mail_end
< msg_end
&& *mail_end
!= '>')
493 if (mail_end
== msg_end
)
497 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
498 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
499 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
500 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
501 name_start
= person_name
;
502 name_end
= name_start
+ strlen(person_name
);
503 mail_start
= person_mail
;
504 mail_end
= mail_start
+ strlen(person_mail
);
506 if (part
== 'n' || part
== 'N') { /* name */
507 strbuf_add(sb
, name_start
, name_end
-name_start
);
508 return placeholder_len
;
510 if (part
== 'e' || part
== 'E') { /* email */
511 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
512 return placeholder_len
;
515 /* advance 'start' to point to date start delimiter */
516 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
520 date
= strtoul(msg
+ start
, &ep
, 10);
521 if (msg
+ start
== ep
)
524 if (part
== 't') { /* date, UNIX timestamp */
525 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
526 return placeholder_len
;
530 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
532 if (start
+ 1 < len
) {
533 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
534 if (msg
[start
] == '-')
540 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
541 return placeholder_len
;
542 case 'D': /* date, RFC2822 style */
543 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
544 return placeholder_len
;
545 case 'r': /* date, relative */
546 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
547 return placeholder_len
;
548 case 'i': /* date, ISO 8601 */
549 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
550 return placeholder_len
;
555 * bogus commit, 'sb' cannot be updated, but we still need to
556 * compute a valid return value.
558 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
559 || part
== 'D' || part
== 'r' || part
== 'i')
560 return placeholder_len
;
562 return 0; /* unknown placeholder */
570 struct format_commit_context
{
571 const struct commit
*commit
;
572 const struct pretty_print_context
*pretty_ctx
;
573 unsigned commit_header_parsed
:1;
574 unsigned commit_message_parsed
:1;
576 size_t width
, indent1
, indent2
;
578 /* These offsets are relative to the start of the commit message. */
580 struct chunk committer
;
581 struct chunk encoding
;
586 /* The following ones are relative to the result struct strbuf. */
587 struct chunk abbrev_commit_hash
;
588 struct chunk abbrev_tree_hash
;
589 struct chunk abbrev_parent_hashes
;
593 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
596 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
601 * We haven't seen this chunk before. Our caller is surely
602 * going to add it the hard way now. Remember the most likely
603 * start of the to-be-added chunk: the current end of the
606 chunk
->off
= sb
->len
;
610 static void parse_commit_header(struct format_commit_context
*context
)
612 const char *msg
= context
->message
;
615 for (i
= 0; msg
[i
]; i
++) {
617 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
622 } else if (!prefixcmp(msg
+ i
, "author ")) {
623 context
->author
.off
= i
+ 7;
624 context
->author
.len
= eol
- i
- 7;
625 } else if (!prefixcmp(msg
+ i
, "committer ")) {
626 context
->committer
.off
= i
+ 10;
627 context
->committer
.len
= eol
- i
- 10;
628 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
629 context
->encoding
.off
= i
+ 9;
630 context
->encoding
.len
= eol
- i
- 9;
634 context
->message_off
= i
;
635 context
->commit_header_parsed
= 1;
638 static int istitlechar(char c
)
640 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
641 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
644 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
647 size_t start_len
= sb
->len
;
650 for (; *msg
&& *msg
!= '\n'; msg
++) {
651 if (istitlechar(*msg
)) {
653 strbuf_addch(sb
, '-');
655 strbuf_addch(sb
, *msg
);
657 while (*(msg
+1) == '.')
663 /* trim any trailing '.' or '-' characters */
665 while (sb
->len
- trimlen
> start_len
&&
666 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
667 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
669 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
672 const char *format_subject(struct strbuf
*sb
, const char *msg
,
673 const char *line_separator
)
678 const char *line
= msg
;
679 int linelen
= get_one_line(line
);
682 if (!linelen
|| is_empty_line(line
, &linelen
))
687 strbuf_grow(sb
, linelen
+ 2);
689 strbuf_addstr(sb
, line_separator
);
690 strbuf_add(sb
, line
, linelen
);
696 static void parse_commit_message(struct format_commit_context
*c
)
698 const char *msg
= c
->message
+ c
->message_off
;
699 const char *start
= c
->message
;
701 msg
= skip_empty_lines(msg
);
702 c
->subject_off
= msg
- start
;
704 msg
= format_subject(NULL
, msg
, NULL
);
705 msg
= skip_empty_lines(msg
);
706 c
->body_off
= msg
- start
;
708 c
->commit_message_parsed
= 1;
711 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
713 struct name_decoration
*d
;
714 const char *prefix
= " (";
716 load_ref_decorations(DECORATE_SHORT_REFS
);
717 d
= lookup_decoration(&name_decoration
, &commit
->object
);
719 strbuf_addstr(sb
, prefix
);
721 strbuf_addstr(sb
, d
->name
);
724 if (prefix
[0] == ',')
725 strbuf_addch(sb
, ')');
728 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
729 size_t width
, size_t indent1
, size_t indent2
)
731 struct strbuf tmp
= STRBUF_INIT
;
734 strbuf_add(&tmp
, sb
->buf
, pos
);
735 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
736 (int) indent1
, (int) indent2
, (int) width
);
737 strbuf_swap(&tmp
, sb
);
738 strbuf_release(&tmp
);
741 static void rewrap_message_tail(struct strbuf
*sb
,
742 struct format_commit_context
*c
,
743 size_t new_width
, size_t new_indent1
,
746 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
747 c
->indent2
== new_indent2
)
749 if (c
->wrap_start
< sb
->len
)
750 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
751 c
->wrap_start
= sb
->len
;
752 c
->width
= new_width
;
753 c
->indent1
= new_indent1
;
754 c
->indent2
= new_indent2
;
757 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
760 struct format_commit_context
*c
= context
;
761 const struct commit
*commit
= c
->commit
;
762 const char *msg
= c
->message
;
763 struct commit_list
*p
;
766 /* these are independent of the commit */
767 switch (placeholder
[0]) {
769 if (placeholder
[1] == '(') {
770 const char *end
= strchr(placeholder
+ 2, ')');
771 char color
[COLOR_MAXLEN
];
774 color_parse_mem(placeholder
+ 2,
775 end
- (placeholder
+ 2),
776 "--pretty format", color
);
777 strbuf_addstr(sb
, color
);
778 return end
- placeholder
+ 1;
780 if (!prefixcmp(placeholder
+ 1, "red")) {
781 strbuf_addstr(sb
, GIT_COLOR_RED
);
783 } else if (!prefixcmp(placeholder
+ 1, "green")) {
784 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
786 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
787 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
789 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
790 strbuf_addstr(sb
, GIT_COLOR_RESET
);
794 case 'n': /* newline */
795 strbuf_addch(sb
, '\n');
798 /* %x00 == NUL, %x0a == LF, etc. */
799 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
801 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
803 strbuf_addch(sb
, (h1
<<4)|h2
);
808 if (placeholder
[1] == '(') {
809 unsigned long width
= 0, indent1
= 0, indent2
= 0;
811 const char *start
= placeholder
+ 2;
812 const char *end
= strchr(start
, ')');
816 width
= strtoul(start
, &next
, 10);
818 indent1
= strtoul(next
+ 1, &next
, 10);
820 indent2
= strtoul(next
+ 1,
827 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
828 return end
- placeholder
+ 1;
833 /* these depend on the commit */
834 if (!commit
->object
.parsed
)
835 parse_object(commit
->object
.sha1
);
837 switch (placeholder
[0]) {
838 case 'H': /* commit hash */
839 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
841 case 'h': /* abbreviated commit hash */
842 if (add_again(sb
, &c
->abbrev_commit_hash
))
844 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
845 c
->pretty_ctx
->abbrev
));
846 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
848 case 'T': /* tree hash */
849 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
851 case 't': /* abbreviated tree hash */
852 if (add_again(sb
, &c
->abbrev_tree_hash
))
854 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
855 c
->pretty_ctx
->abbrev
));
856 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
858 case 'P': /* parent hashes */
859 for (p
= commit
->parents
; p
; p
= p
->next
) {
860 if (p
!= commit
->parents
)
861 strbuf_addch(sb
, ' ');
862 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
865 case 'p': /* abbreviated parent hashes */
866 if (add_again(sb
, &c
->abbrev_parent_hashes
))
868 for (p
= commit
->parents
; p
; p
= p
->next
) {
869 if (p
!= commit
->parents
)
870 strbuf_addch(sb
, ' ');
871 strbuf_addstr(sb
, find_unique_abbrev(
872 p
->item
->object
.sha1
,
873 c
->pretty_ctx
->abbrev
));
875 c
->abbrev_parent_hashes
.len
= sb
->len
-
876 c
->abbrev_parent_hashes
.off
;
878 case 'm': /* left/right/bottom */
879 strbuf_addstr(sb
, get_revision_mark(NULL
, commit
));
882 format_decoration(sb
, commit
);
884 case 'g': /* reflog info */
885 switch(placeholder
[1]) {
886 case 'd': /* reflog selector */
888 if (c
->pretty_ctx
->reflog_info
)
889 get_reflog_selector(sb
,
890 c
->pretty_ctx
->reflog_info
,
891 c
->pretty_ctx
->date_mode
,
892 (placeholder
[1] == 'd'));
894 case 's': /* reflog message */
895 if (c
->pretty_ctx
->reflog_info
)
896 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
899 return 0; /* unknown %g placeholder */
901 if (c
->pretty_ctx
->show_notes
) {
902 format_display_notes(commit
->object
.sha1
, sb
,
903 get_log_output_encoding(), 0);
909 /* For the rest we have to parse the commit header. */
910 if (!c
->commit_header_parsed
)
911 parse_commit_header(c
);
913 switch (placeholder
[0]) {
914 case 'a': /* author ... */
915 return format_person_part(sb
, placeholder
[1],
916 msg
+ c
->author
.off
, c
->author
.len
,
917 c
->pretty_ctx
->date_mode
);
918 case 'c': /* committer ... */
919 return format_person_part(sb
, placeholder
[1],
920 msg
+ c
->committer
.off
, c
->committer
.len
,
921 c
->pretty_ctx
->date_mode
);
922 case 'e': /* encoding */
923 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
925 case 'B': /* raw body */
926 /* message_off is always left at the initial newline */
927 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
931 /* Now we need to parse the commit message. */
932 if (!c
->commit_message_parsed
)
933 parse_commit_message(c
);
935 switch (placeholder
[0]) {
936 case 's': /* subject */
937 format_subject(sb
, msg
+ c
->subject_off
, " ");
939 case 'f': /* sanitized subject */
940 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
943 strbuf_addstr(sb
, msg
+ c
->body_off
);
946 return 0; /* unknown placeholder */
949 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
956 ADD_LF_BEFORE_NON_EMPTY
,
958 ADD_SP_BEFORE_NON_EMPTY
961 switch (placeholder
[0]) {
963 magic
= DEL_LF_BEFORE_EMPTY
;
966 magic
= ADD_LF_BEFORE_NON_EMPTY
;
969 magic
= ADD_SP_BEFORE_NON_EMPTY
;
974 if (magic
!= NO_MAGIC
)
978 consumed
= format_commit_one(sb
, placeholder
, context
);
979 if (magic
== NO_MAGIC
)
982 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
983 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
984 strbuf_setlen(sb
, sb
->len
- 1);
985 } else if (orig_len
!= sb
->len
) {
986 if (magic
== ADD_LF_BEFORE_NON_EMPTY
)
987 strbuf_insert(sb
, orig_len
, "\n", 1);
988 else if (magic
== ADD_SP_BEFORE_NON_EMPTY
)
989 strbuf_insert(sb
, orig_len
, " ", 1);
994 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
997 struct userformat_want
*w
= context
;
999 if (*placeholder
== '+' || *placeholder
== '-' || *placeholder
== ' ')
1002 switch (*placeholder
) {
1010 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
1012 struct strbuf dummy
= STRBUF_INIT
;
1019 strbuf_expand(&dummy
, user_format
, userformat_want_item
, w
);
1020 strbuf_release(&dummy
);
1023 void format_commit_message(const struct commit
*commit
,
1024 const char *format
, struct strbuf
*sb
,
1025 const struct pretty_print_context
*pretty_ctx
)
1027 struct format_commit_context context
;
1028 static const char utf8
[] = "UTF-8";
1030 const char *output_enc
= pretty_ctx
->output_encoding
;
1032 memset(&context
, 0, sizeof(context
));
1033 context
.commit
= commit
;
1034 context
.pretty_ctx
= pretty_ctx
;
1035 context
.wrap_start
= sb
->len
;
1036 context
.message
= commit
->buffer
;
1038 enc
= get_header(commit
, "encoding");
1039 enc
= enc
? enc
: utf8
;
1040 if (strcmp(enc
, output_enc
))
1041 context
.message
= logmsg_reencode(commit
, output_enc
);
1044 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1045 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1047 if (context
.message
!= commit
->buffer
)
1048 free(context
.message
);
1051 static void pp_header(enum cmit_fmt fmt
,
1053 enum date_mode dmode
,
1054 const char *encoding
,
1055 const struct commit
*commit
,
1059 int parents_shown
= 0;
1062 const char *line
= *msg_p
;
1063 int linelen
= get_one_line(*msg_p
);
1073 if (fmt
== CMIT_FMT_RAW
) {
1074 strbuf_add(sb
, line
, linelen
);
1078 if (!memcmp(line
, "parent ", 7)) {
1080 die("bad parent line in commit");
1084 if (!parents_shown
) {
1085 struct commit_list
*parent
;
1087 for (parent
= commit
->parents
, num
= 0;
1089 parent
= parent
->next
, num
++)
1091 /* with enough slop */
1092 strbuf_grow(sb
, num
* 50 + 20);
1093 add_merge_info(fmt
, sb
, commit
, abbrev
);
1098 * MEDIUM == DEFAULT shows only author with dates.
1099 * FULL shows both authors but not dates.
1100 * FULLER shows both authors and dates.
1102 if (!memcmp(line
, "author ", 7)) {
1103 strbuf_grow(sb
, linelen
+ 80);
1104 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
1106 if (!memcmp(line
, "committer ", 10) &&
1107 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
1108 strbuf_grow(sb
, linelen
+ 80);
1109 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
1114 void pp_title_line(enum cmit_fmt fmt
,
1117 const char *subject
,
1118 const char *after_subject
,
1119 const char *encoding
,
1122 struct strbuf title
;
1124 strbuf_init(&title
, 80);
1125 *msg_p
= format_subject(&title
, *msg_p
, " ");
1127 strbuf_grow(sb
, title
.len
+ 1024);
1129 strbuf_addstr(sb
, subject
);
1130 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
1132 strbuf_addbuf(sb
, &title
);
1134 strbuf_addch(sb
, '\n');
1136 if (need_8bit_cte
> 0) {
1137 const char *header_fmt
=
1138 "MIME-Version: 1.0\n"
1139 "Content-Type: text/plain; charset=%s\n"
1140 "Content-Transfer-Encoding: 8bit\n";
1141 strbuf_addf(sb
, header_fmt
, encoding
);
1143 if (after_subject
) {
1144 strbuf_addstr(sb
, after_subject
);
1146 if (fmt
== CMIT_FMT_EMAIL
) {
1147 strbuf_addch(sb
, '\n');
1149 strbuf_release(&title
);
1152 void pp_remainder(enum cmit_fmt fmt
,
1159 const char *line
= *msg_p
;
1160 int linelen
= get_one_line(line
);
1166 if (is_empty_line(line
, &linelen
)) {
1169 if (fmt
== CMIT_FMT_SHORT
)
1174 strbuf_grow(sb
, linelen
+ indent
+ 20);
1176 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1177 strbuf_setlen(sb
, sb
->len
+ indent
);
1179 strbuf_add(sb
, line
, linelen
);
1180 strbuf_addch(sb
, '\n');
1184 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1186 const char *encoding
;
1188 encoding
= get_log_output_encoding();
1190 *encoding_p
= encoding
;
1191 return logmsg_reencode(commit
, encoding
);
1194 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
1196 const struct pretty_print_context
*context
)
1198 unsigned long beginning_of_body
;
1200 const char *msg
= commit
->buffer
;
1202 const char *encoding
;
1203 int need_8bit_cte
= context
->need_8bit_cte
;
1205 if (fmt
== CMIT_FMT_USERFORMAT
) {
1206 format_commit_message(commit
, user_format
, sb
, context
);
1210 reencoded
= reencode_commit_message(commit
, &encoding
);
1215 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1219 * We need to check and emit Content-type: to mark it
1220 * as 8-bit if we haven't done so.
1222 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1225 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1227 /* author could be non 7-bit ASCII but
1228 * the log may be so; skip over the
1229 * header part first.
1231 if (ch
== '\n' && msg
[i
+1] == '\n')
1234 else if (non_ascii(ch
)) {
1241 pp_header(fmt
, context
->abbrev
, context
->date_mode
, encoding
,
1243 if (fmt
!= CMIT_FMT_ONELINE
&& !context
->subject
) {
1244 strbuf_addch(sb
, '\n');
1247 /* Skip excess blank lines at the beginning of body, if any... */
1248 msg
= skip_empty_lines(msg
);
1250 /* These formats treat the title line specially. */
1251 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1252 pp_title_line(fmt
, &msg
, sb
, context
->subject
,
1253 context
->after_subject
, encoding
, need_8bit_cte
);
1255 beginning_of_body
= sb
->len
;
1256 if (fmt
!= CMIT_FMT_ONELINE
)
1257 pp_remainder(fmt
, &msg
, sb
, indent
);
1260 /* Make sure there is an EOLN for the non-oneline case */
1261 if (fmt
!= CMIT_FMT_ONELINE
)
1262 strbuf_addch(sb
, '\n');
1265 * The caller may append additional body text in e-mail
1266 * format. Make sure we did not strip the blank line
1267 * between the header and the body.
1269 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1270 strbuf_addch(sb
, '\n');
1272 if (context
->show_notes
)
1273 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1274 NOTES_SHOW_HEADER
| NOTES_INDENT
);