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
)
221 for (i
= 0; i
< len
; i
++) {
225 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
228 strbuf_add(sb
, line
, len
);
232 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
233 strbuf_addf(sb
, "=?%s?q?", encoding
);
234 for (i
= last
= 0; i
< len
; i
++) {
235 unsigned ch
= line
[i
] & 0xFF;
237 * We encode ' ' using '=20' even though rfc2047
238 * allows using '_' for readability. Unfortunately,
239 * many programs do not understand this and just
240 * leave the underscore in place.
242 if (is_rfc2047_special(ch
) || ch
== ' ') {
243 strbuf_add(sb
, line
+ last
, i
- last
);
244 strbuf_addf(sb
, "=%02X", ch
);
248 strbuf_add(sb
, line
+ last
, len
- last
);
249 strbuf_addstr(sb
, "?=");
252 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
253 const char *line
, enum date_mode dmode
,
254 const char *encoding
)
261 if (fmt
== CMIT_FMT_ONELINE
)
263 date
= strchr(line
, '>');
266 namelen
= ++date
- line
;
267 time
= strtoul(date
, &date
, 10);
268 tz
= strtol(date
, NULL
, 10);
270 if (fmt
== CMIT_FMT_EMAIL
) {
271 char *name_tail
= strchr(line
, '<');
272 int display_name_length
;
275 while (line
< name_tail
&& isspace(name_tail
[-1]))
277 display_name_length
= name_tail
- line
;
278 strbuf_addstr(sb
, "From: ");
279 add_rfc2047(sb
, line
, display_name_length
, encoding
);
280 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
281 strbuf_addch(sb
, '\n');
283 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
284 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
288 case CMIT_FMT_MEDIUM
:
289 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
292 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
294 case CMIT_FMT_FULLER
:
295 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
303 static int is_empty_line(const char *line
, int *len_p
)
306 while (len
&& isspace(line
[len
-1]))
312 static const char *skip_empty_lines(const char *msg
)
315 int linelen
= get_one_line(msg
);
319 if (!is_empty_line(msg
, &ll
))
326 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
327 const struct commit
*commit
, int abbrev
)
329 struct commit_list
*parent
= commit
->parents
;
331 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
332 !parent
|| !parent
->next
)
335 strbuf_addstr(sb
, "Merge:");
338 struct commit
*p
= parent
->item
;
339 const char *hex
= NULL
;
341 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
343 hex
= sha1_to_hex(p
->object
.sha1
);
344 parent
= parent
->next
;
346 strbuf_addf(sb
, " %s", hex
);
348 strbuf_addch(sb
, '\n');
351 static char *get_header(const struct commit
*commit
, const char *key
)
353 int key_len
= strlen(key
);
354 const char *line
= commit
->buffer
;
357 const char *eol
= strchr(line
, '\n'), *next
;
362 eol
= line
+ strlen(line
);
366 if (eol
- line
> key_len
&&
367 !strncmp(line
, key
, key_len
) &&
368 line
[key_len
] == ' ') {
369 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
375 static char *replace_encoding_header(char *buf
, const char *encoding
)
377 struct strbuf tmp
= STRBUF_INIT
;
381 /* guess if there is an encoding header before a \n\n */
382 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
383 cp
= strchr(cp
, '\n');
384 if (!cp
|| *++cp
== '\n')
388 cp
= strchr(cp
, '\n');
390 return buf
; /* should not happen but be defensive */
391 len
= cp
+ 1 - (buf
+ start
);
393 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
394 if (is_encoding_utf8(encoding
)) {
395 /* we have re-coded to UTF-8; drop the header */
396 strbuf_remove(&tmp
, start
, len
);
398 /* just replaces XXXX in 'encoding XXXX\n' */
399 strbuf_splice(&tmp
, start
+ strlen("encoding "),
400 len
- strlen("encoding \n"),
401 encoding
, strlen(encoding
));
403 return strbuf_detach(&tmp
, NULL
);
406 static char *logmsg_reencode(const struct commit
*commit
,
407 const char *output_encoding
)
409 static const char *utf8
= "UTF-8";
410 const char *use_encoding
;
414 if (!*output_encoding
)
416 encoding
= get_header(commit
, "encoding");
417 use_encoding
= encoding
? encoding
: utf8
;
418 if (!strcmp(use_encoding
, output_encoding
))
419 if (encoding
) /* we'll strip encoding header later */
420 out
= xstrdup(commit
->buffer
);
422 return NULL
; /* nothing to do */
424 out
= reencode_string(commit
->buffer
,
425 output_encoding
, use_encoding
);
427 out
= replace_encoding_header(out
, output_encoding
);
433 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
435 static struct string_list
*mail_map
;
437 mail_map
= xcalloc(1, sizeof(*mail_map
));
438 read_mailmap(mail_map
, NULL
);
440 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
443 static size_t format_person_part(struct strbuf
*sb
, char part
,
444 const char *msg
, int len
, enum date_mode dmode
)
446 /* currently all placeholders have same length */
447 const int placeholder_len
= 2;
448 int start
, end
, tz
= 0;
449 unsigned long date
= 0;
451 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
452 char person_name
[1024];
453 char person_mail
[1024];
455 /* advance 'end' to point to email start delimiter */
456 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
460 * When end points at the '<' that we found, it should have
461 * matching '>' later, which means 'end' must be strictly
467 /* Seek for both name and email part */
470 while (name_end
> name_start
&& isspace(*(name_end
-1)))
472 mail_start
= msg
+end
+1;
473 mail_end
= mail_start
;
474 while (mail_end
< msg_end
&& *mail_end
!= '>')
476 if (mail_end
== msg_end
)
480 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
481 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
482 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
483 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
484 name_start
= person_name
;
485 name_end
= name_start
+ strlen(person_name
);
486 mail_start
= person_mail
;
487 mail_end
= mail_start
+ strlen(person_mail
);
489 if (part
== 'n' || part
== 'N') { /* name */
490 strbuf_add(sb
, name_start
, name_end
-name_start
);
491 return placeholder_len
;
493 if (part
== 'e' || part
== 'E') { /* email */
494 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
495 return placeholder_len
;
498 /* advance 'start' to point to date start delimiter */
499 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
503 date
= strtoul(msg
+ start
, &ep
, 10);
504 if (msg
+ start
== ep
)
507 if (part
== 't') { /* date, UNIX timestamp */
508 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
509 return placeholder_len
;
513 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
515 if (start
+ 1 < len
) {
516 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
517 if (msg
[start
] == '-')
523 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
524 return placeholder_len
;
525 case 'D': /* date, RFC2822 style */
526 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
527 return placeholder_len
;
528 case 'r': /* date, relative */
529 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
530 return placeholder_len
;
531 case 'i': /* date, ISO 8601 */
532 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
533 return placeholder_len
;
538 * bogus commit, 'sb' cannot be updated, but we still need to
539 * compute a valid return value.
541 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
542 || part
== 'D' || part
== 'r' || part
== 'i')
543 return placeholder_len
;
545 return 0; /* unknown placeholder */
553 struct format_commit_context
{
554 const struct commit
*commit
;
555 const struct pretty_print_context
*pretty_ctx
;
556 unsigned commit_header_parsed
:1;
557 unsigned commit_message_parsed
:1;
558 size_t width
, indent1
, indent2
;
560 /* These offsets are relative to the start of the commit message. */
562 struct chunk committer
;
563 struct chunk encoding
;
568 /* The following ones are relative to the result struct strbuf. */
569 struct chunk abbrev_commit_hash
;
570 struct chunk abbrev_tree_hash
;
571 struct chunk abbrev_parent_hashes
;
575 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
578 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
583 * We haven't seen this chunk before. Our caller is surely
584 * going to add it the hard way now. Remember the most likely
585 * start of the to-be-added chunk: the current end of the
588 chunk
->off
= sb
->len
;
592 static void parse_commit_header(struct format_commit_context
*context
)
594 const char *msg
= context
->commit
->buffer
;
597 for (i
= 0; msg
[i
]; i
++) {
599 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
604 } else if (!prefixcmp(msg
+ i
, "author ")) {
605 context
->author
.off
= i
+ 7;
606 context
->author
.len
= eol
- i
- 7;
607 } else if (!prefixcmp(msg
+ i
, "committer ")) {
608 context
->committer
.off
= i
+ 10;
609 context
->committer
.len
= eol
- i
- 10;
610 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
611 context
->encoding
.off
= i
+ 9;
612 context
->encoding
.len
= eol
- i
- 9;
616 context
->message_off
= i
;
617 context
->commit_header_parsed
= 1;
620 static int istitlechar(char c
)
622 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
623 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
626 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
629 size_t start_len
= sb
->len
;
632 for (; *msg
&& *msg
!= '\n'; msg
++) {
633 if (istitlechar(*msg
)) {
635 strbuf_addch(sb
, '-');
637 strbuf_addch(sb
, *msg
);
639 while (*(msg
+1) == '.')
645 /* trim any trailing '.' or '-' characters */
647 while (sb
->len
- trimlen
> start_len
&&
648 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
649 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
651 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
654 const char *format_subject(struct strbuf
*sb
, const char *msg
,
655 const char *line_separator
)
660 const char *line
= msg
;
661 int linelen
= get_one_line(line
);
664 if (!linelen
|| is_empty_line(line
, &linelen
))
669 strbuf_grow(sb
, linelen
+ 2);
671 strbuf_addstr(sb
, line_separator
);
672 strbuf_add(sb
, line
, linelen
);
678 static void parse_commit_message(struct format_commit_context
*c
)
680 const char *msg
= c
->commit
->buffer
+ c
->message_off
;
681 const char *start
= c
->commit
->buffer
;
683 msg
= skip_empty_lines(msg
);
684 c
->subject_off
= msg
- start
;
686 msg
= format_subject(NULL
, msg
, NULL
);
687 msg
= skip_empty_lines(msg
);
688 c
->body_off
= msg
- start
;
690 c
->commit_message_parsed
= 1;
693 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
695 struct name_decoration
*d
;
696 const char *prefix
= " (";
698 load_ref_decorations(DECORATE_SHORT_REFS
);
699 d
= lookup_decoration(&name_decoration
, &commit
->object
);
701 strbuf_addstr(sb
, prefix
);
703 strbuf_addstr(sb
, d
->name
);
706 if (prefix
[0] == ',')
707 strbuf_addch(sb
, ')');
710 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
711 size_t width
, size_t indent1
, size_t indent2
)
713 struct strbuf tmp
= STRBUF_INIT
;
716 strbuf_add(&tmp
, sb
->buf
, pos
);
717 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
718 (int) indent1
, (int) indent2
, (int) width
);
719 strbuf_swap(&tmp
, sb
);
720 strbuf_release(&tmp
);
723 static void rewrap_message_tail(struct strbuf
*sb
,
724 struct format_commit_context
*c
,
725 size_t new_width
, size_t new_indent1
,
728 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
729 c
->indent2
== new_indent2
)
731 if (c
->wrap_start
< sb
->len
)
732 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
733 c
->wrap_start
= sb
->len
;
734 c
->width
= new_width
;
735 c
->indent1
= new_indent1
;
736 c
->indent2
= new_indent2
;
739 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
742 struct format_commit_context
*c
= context
;
743 const struct commit
*commit
= c
->commit
;
744 const char *msg
= commit
->buffer
;
745 struct commit_list
*p
;
748 /* these are independent of the commit */
749 switch (placeholder
[0]) {
751 if (placeholder
[1] == '(') {
752 const char *end
= strchr(placeholder
+ 2, ')');
753 char color
[COLOR_MAXLEN
];
756 color_parse_mem(placeholder
+ 2,
757 end
- (placeholder
+ 2),
758 "--pretty format", color
);
759 strbuf_addstr(sb
, color
);
760 return end
- placeholder
+ 1;
762 if (!prefixcmp(placeholder
+ 1, "red")) {
763 strbuf_addstr(sb
, GIT_COLOR_RED
);
765 } else if (!prefixcmp(placeholder
+ 1, "green")) {
766 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
768 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
769 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
771 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
772 strbuf_addstr(sb
, GIT_COLOR_RESET
);
776 case 'n': /* newline */
777 strbuf_addch(sb
, '\n');
780 /* %x00 == NUL, %x0a == LF, etc. */
781 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
783 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
785 strbuf_addch(sb
, (h1
<<4)|h2
);
790 if (placeholder
[1] == '(') {
791 unsigned long width
= 0, indent1
= 0, indent2
= 0;
793 const char *start
= placeholder
+ 2;
794 const char *end
= strchr(start
, ')');
798 width
= strtoul(start
, &next
, 10);
800 indent1
= strtoul(next
+ 1, &next
, 10);
802 indent2
= strtoul(next
+ 1,
809 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
810 return end
- placeholder
+ 1;
815 /* these depend on the commit */
816 if (!commit
->object
.parsed
)
817 parse_object(commit
->object
.sha1
);
819 switch (placeholder
[0]) {
820 case 'H': /* commit hash */
821 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
823 case 'h': /* abbreviated commit hash */
824 if (add_again(sb
, &c
->abbrev_commit_hash
))
826 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
827 c
->pretty_ctx
->abbrev
));
828 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
830 case 'T': /* tree hash */
831 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
833 case 't': /* abbreviated tree hash */
834 if (add_again(sb
, &c
->abbrev_tree_hash
))
836 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
837 c
->pretty_ctx
->abbrev
));
838 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
840 case 'P': /* parent hashes */
841 for (p
= commit
->parents
; p
; p
= p
->next
) {
842 if (p
!= commit
->parents
)
843 strbuf_addch(sb
, ' ');
844 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
847 case 'p': /* abbreviated parent hashes */
848 if (add_again(sb
, &c
->abbrev_parent_hashes
))
850 for (p
= commit
->parents
; p
; p
= p
->next
) {
851 if (p
!= commit
->parents
)
852 strbuf_addch(sb
, ' ');
853 strbuf_addstr(sb
, find_unique_abbrev(
854 p
->item
->object
.sha1
,
855 c
->pretty_ctx
->abbrev
));
857 c
->abbrev_parent_hashes
.len
= sb
->len
-
858 c
->abbrev_parent_hashes
.off
;
860 case 'm': /* left/right/bottom */
861 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
863 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
868 format_decoration(sb
, commit
);
870 case 'g': /* reflog info */
871 switch(placeholder
[1]) {
872 case 'd': /* reflog selector */
874 if (c
->pretty_ctx
->reflog_info
)
875 get_reflog_selector(sb
,
876 c
->pretty_ctx
->reflog_info
,
877 c
->pretty_ctx
->date_mode
,
878 (placeholder
[1] == 'd'));
880 case 's': /* reflog message */
881 if (c
->pretty_ctx
->reflog_info
)
882 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
885 return 0; /* unknown %g placeholder */
887 if (c
->pretty_ctx
->show_notes
) {
888 format_display_notes(commit
->object
.sha1
, sb
,
889 git_log_output_encoding
? git_log_output_encoding
890 : git_commit_encoding
, 0);
896 /* For the rest we have to parse the commit header. */
897 if (!c
->commit_header_parsed
)
898 parse_commit_header(c
);
900 switch (placeholder
[0]) {
901 case 'a': /* author ... */
902 return format_person_part(sb
, placeholder
[1],
903 msg
+ c
->author
.off
, c
->author
.len
,
904 c
->pretty_ctx
->date_mode
);
905 case 'c': /* committer ... */
906 return format_person_part(sb
, placeholder
[1],
907 msg
+ c
->committer
.off
, c
->committer
.len
,
908 c
->pretty_ctx
->date_mode
);
909 case 'e': /* encoding */
910 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
912 case 'B': /* raw body */
913 /* message_off is always left at the initial newline */
914 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
918 /* Now we need to parse the commit message. */
919 if (!c
->commit_message_parsed
)
920 parse_commit_message(c
);
922 switch (placeholder
[0]) {
923 case 's': /* subject */
924 format_subject(sb
, msg
+ c
->subject_off
, " ");
926 case 'f': /* sanitized subject */
927 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
930 strbuf_addstr(sb
, msg
+ c
->body_off
);
933 return 0; /* unknown placeholder */
936 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
943 ADD_LF_BEFORE_NON_EMPTY
,
947 switch (placeholder
[0]) {
949 magic
= DEL_LF_BEFORE_EMPTY
;
952 magic
= ADD_LF_BEFORE_NON_EMPTY
;
957 if (magic
!= NO_MAGIC
)
961 consumed
= format_commit_one(sb
, placeholder
, context
);
962 if (magic
== NO_MAGIC
)
965 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
966 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
967 strbuf_setlen(sb
, sb
->len
- 1);
968 } else if ((orig_len
!= sb
->len
) && magic
== ADD_LF_BEFORE_NON_EMPTY
) {
969 strbuf_insert(sb
, orig_len
, "\n", 1);
974 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
977 struct userformat_want
*w
= context
;
979 if (*placeholder
== '+' || *placeholder
== '-')
982 switch (*placeholder
) {
990 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
992 struct strbuf dummy
= STRBUF_INIT
;
999 strbuf_expand(&dummy
, user_format
, userformat_want_item
, w
);
1000 strbuf_release(&dummy
);
1003 void format_commit_message(const struct commit
*commit
,
1004 const char *format
, struct strbuf
*sb
,
1005 const struct pretty_print_context
*pretty_ctx
)
1007 struct format_commit_context context
;
1009 memset(&context
, 0, sizeof(context
));
1010 context
.commit
= commit
;
1011 context
.pretty_ctx
= pretty_ctx
;
1012 context
.wrap_start
= sb
->len
;
1013 strbuf_expand(sb
, format
, format_commit_item
, &context
);
1014 rewrap_message_tail(sb
, &context
, 0, 0, 0);
1017 static void pp_header(enum cmit_fmt fmt
,
1019 enum date_mode dmode
,
1020 const char *encoding
,
1021 const struct commit
*commit
,
1025 int parents_shown
= 0;
1028 const char *line
= *msg_p
;
1029 int linelen
= get_one_line(*msg_p
);
1039 if (fmt
== CMIT_FMT_RAW
) {
1040 strbuf_add(sb
, line
, linelen
);
1044 if (!memcmp(line
, "parent ", 7)) {
1046 die("bad parent line in commit");
1050 if (!parents_shown
) {
1051 struct commit_list
*parent
;
1053 for (parent
= commit
->parents
, num
= 0;
1055 parent
= parent
->next
, num
++)
1057 /* with enough slop */
1058 strbuf_grow(sb
, num
* 50 + 20);
1059 add_merge_info(fmt
, sb
, commit
, abbrev
);
1064 * MEDIUM == DEFAULT shows only author with dates.
1065 * FULL shows both authors but not dates.
1066 * FULLER shows both authors and dates.
1068 if (!memcmp(line
, "author ", 7)) {
1069 strbuf_grow(sb
, linelen
+ 80);
1070 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
1072 if (!memcmp(line
, "committer ", 10) &&
1073 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
1074 strbuf_grow(sb
, linelen
+ 80);
1075 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
1080 void pp_title_line(enum cmit_fmt fmt
,
1083 const char *subject
,
1084 const char *after_subject
,
1085 const char *encoding
,
1088 const char *line_separator
= (fmt
== CMIT_FMT_EMAIL
) ? "\n " : " ";
1089 struct strbuf title
;
1091 strbuf_init(&title
, 80);
1092 *msg_p
= format_subject(&title
, *msg_p
, line_separator
);
1094 strbuf_grow(sb
, title
.len
+ 1024);
1096 strbuf_addstr(sb
, subject
);
1097 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
1099 strbuf_addbuf(sb
, &title
);
1101 strbuf_addch(sb
, '\n');
1103 if (need_8bit_cte
> 0) {
1104 const char *header_fmt
=
1105 "MIME-Version: 1.0\n"
1106 "Content-Type: text/plain; charset=%s\n"
1107 "Content-Transfer-Encoding: 8bit\n";
1108 strbuf_addf(sb
, header_fmt
, encoding
);
1110 if (after_subject
) {
1111 strbuf_addstr(sb
, after_subject
);
1113 if (fmt
== CMIT_FMT_EMAIL
) {
1114 strbuf_addch(sb
, '\n');
1116 strbuf_release(&title
);
1119 void pp_remainder(enum cmit_fmt fmt
,
1126 const char *line
= *msg_p
;
1127 int linelen
= get_one_line(line
);
1133 if (is_empty_line(line
, &linelen
)) {
1136 if (fmt
== CMIT_FMT_SHORT
)
1141 strbuf_grow(sb
, linelen
+ indent
+ 20);
1143 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1144 strbuf_setlen(sb
, sb
->len
+ indent
);
1146 strbuf_add(sb
, line
, linelen
);
1147 strbuf_addch(sb
, '\n');
1151 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1153 const char *encoding
;
1155 encoding
= (git_log_output_encoding
1156 ? git_log_output_encoding
1157 : git_commit_encoding
);
1161 *encoding_p
= encoding
;
1162 return logmsg_reencode(commit
, encoding
);
1165 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
1167 const struct pretty_print_context
*context
)
1169 unsigned long beginning_of_body
;
1171 const char *msg
= commit
->buffer
;
1173 const char *encoding
;
1174 int need_8bit_cte
= context
->need_8bit_cte
;
1176 if (fmt
== CMIT_FMT_USERFORMAT
) {
1177 format_commit_message(commit
, user_format
, sb
, context
);
1181 reencoded
= reencode_commit_message(commit
, &encoding
);
1186 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1190 * We need to check and emit Content-type: to mark it
1191 * as 8-bit if we haven't done so.
1193 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1196 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1198 /* author could be non 7-bit ASCII but
1199 * the log may be so; skip over the
1200 * header part first.
1202 if (ch
== '\n' && msg
[i
+1] == '\n')
1205 else if (non_ascii(ch
)) {
1212 pp_header(fmt
, context
->abbrev
, context
->date_mode
, encoding
,
1214 if (fmt
!= CMIT_FMT_ONELINE
&& !context
->subject
) {
1215 strbuf_addch(sb
, '\n');
1218 /* Skip excess blank lines at the beginning of body, if any... */
1219 msg
= skip_empty_lines(msg
);
1221 /* These formats treat the title line specially. */
1222 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1223 pp_title_line(fmt
, &msg
, sb
, context
->subject
,
1224 context
->after_subject
, encoding
, need_8bit_cte
);
1226 beginning_of_body
= sb
->len
;
1227 if (fmt
!= CMIT_FMT_ONELINE
)
1228 pp_remainder(fmt
, &msg
, sb
, indent
);
1231 /* Make sure there is an EOLN for the non-oneline case */
1232 if (fmt
!= CMIT_FMT_ONELINE
)
1233 strbuf_addch(sb
, '\n');
1236 * The caller may append additional body text in e-mail
1237 * format. Make sure we did not strip the blank line
1238 * between the header and the body.
1240 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1241 strbuf_addch(sb
, '\n');
1243 if (context
->show_notes
)
1244 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1245 NOTES_SHOW_HEADER
| NOTES_INDENT
);