6 #include "string-list.h"
11 #include "reflog-walk.h"
13 static char *user_format
;
14 static struct cmt_fmt_map
{
19 static size_t commit_formats_len
;
20 static struct cmt_fmt_map
*find_commit_format(const char *sought
);
22 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
25 user_format
= xstrdup(cp
);
27 rev
->use_terminator
= 1;
28 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
31 static void setup_commit_formats(void)
33 struct cmt_fmt_map builtin_formats
[] = {
34 { "raw", CMIT_FMT_RAW
, 0 },
35 { "medium", CMIT_FMT_MEDIUM
, 0 },
36 { "short", CMIT_FMT_SHORT
, 0 },
37 { "email", CMIT_FMT_EMAIL
, 0 },
38 { "fuller", CMIT_FMT_FULLER
, 0 },
39 { "full", CMIT_FMT_FULL
, 0 },
40 { "oneline", CMIT_FMT_ONELINE
, 1 }
42 commit_formats_len
= ARRAY_SIZE(builtin_formats
);
43 commit_formats
= xmalloc(commit_formats_len
*
44 sizeof(*builtin_formats
));
45 memcpy(commit_formats
, builtin_formats
,
46 sizeof(*builtin_formats
)*ARRAY_SIZE(builtin_formats
));
49 static struct cmt_fmt_map
*find_commit_format(const char *sought
)
51 struct cmt_fmt_map
*found
= NULL
;
52 size_t found_match_len
= 0;
56 setup_commit_formats();
58 for (i
= 0; i
< commit_formats_len
; i
++) {
61 if (prefixcmp(commit_formats
[i
].name
, sought
))
64 match_len
= strlen(commit_formats
[i
].name
);
65 if (found
== NULL
|| found_match_len
> match_len
) {
66 found
= &commit_formats
[i
];
67 found_match_len
= match_len
;
73 void get_commit_format(const char *arg
, struct rev_info
*rev
)
75 struct cmt_fmt_map
*commit_format
;
77 rev
->use_terminator
= 0;
79 rev
->commit_format
= CMIT_FMT_DEFAULT
;
82 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
83 save_user_format(rev
, strchr(arg
, ':') + 1, arg
[0] == 't');
87 if (strchr(arg
, '%')) {
88 save_user_format(rev
, arg
, 1);
92 commit_format
= find_commit_format(arg
);
94 die("invalid --pretty format: %s", arg
);
96 rev
->commit_format
= commit_format
->format
;
97 rev
->use_terminator
= commit_format
->is_tformat
;
101 * Generic support for pretty-printing the header
103 static int get_one_line(const char *msg
)
118 /* High bit set, or ISO-2022-INT */
119 static int non_ascii(int ch
)
121 return !isascii(ch
) || ch
== '\033';
124 int has_non_ascii(const char *s
)
129 while ((ch
= *s
++) != '\0') {
136 static int is_rfc2047_special(char ch
)
138 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
141 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
142 const char *encoding
)
146 for (i
= 0; i
< len
; i
++) {
150 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
153 strbuf_add(sb
, line
, len
);
157 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
158 strbuf_addf(sb
, "=?%s?q?", encoding
);
159 for (i
= last
= 0; i
< len
; i
++) {
160 unsigned ch
= line
[i
] & 0xFF;
162 * We encode ' ' using '=20' even though rfc2047
163 * allows using '_' for readability. Unfortunately,
164 * many programs do not understand this and just
165 * leave the underscore in place.
167 if (is_rfc2047_special(ch
) || ch
== ' ') {
168 strbuf_add(sb
, line
+ last
, i
- last
);
169 strbuf_addf(sb
, "=%02X", ch
);
173 strbuf_add(sb
, line
+ last
, len
- last
);
174 strbuf_addstr(sb
, "?=");
177 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
178 const char *line
, enum date_mode dmode
,
179 const char *encoding
)
186 if (fmt
== CMIT_FMT_ONELINE
)
188 date
= strchr(line
, '>');
191 namelen
= ++date
- line
;
192 time
= strtoul(date
, &date
, 10);
193 tz
= strtol(date
, NULL
, 10);
195 if (fmt
== CMIT_FMT_EMAIL
) {
196 char *name_tail
= strchr(line
, '<');
197 int display_name_length
;
200 while (line
< name_tail
&& isspace(name_tail
[-1]))
202 display_name_length
= name_tail
- line
;
203 strbuf_addstr(sb
, "From: ");
204 add_rfc2047(sb
, line
, display_name_length
, encoding
);
205 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
206 strbuf_addch(sb
, '\n');
208 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
209 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
213 case CMIT_FMT_MEDIUM
:
214 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
217 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
219 case CMIT_FMT_FULLER
:
220 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
228 static int is_empty_line(const char *line
, int *len_p
)
231 while (len
&& isspace(line
[len
-1]))
237 static const char *skip_empty_lines(const char *msg
)
240 int linelen
= get_one_line(msg
);
244 if (!is_empty_line(msg
, &ll
))
251 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
252 const struct commit
*commit
, int abbrev
)
254 struct commit_list
*parent
= commit
->parents
;
256 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
257 !parent
|| !parent
->next
)
260 strbuf_addstr(sb
, "Merge:");
263 struct commit
*p
= parent
->item
;
264 const char *hex
= NULL
;
266 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
268 hex
= sha1_to_hex(p
->object
.sha1
);
269 parent
= parent
->next
;
271 strbuf_addf(sb
, " %s", hex
);
273 strbuf_addch(sb
, '\n');
276 static char *get_header(const struct commit
*commit
, const char *key
)
278 int key_len
= strlen(key
);
279 const char *line
= commit
->buffer
;
282 const char *eol
= strchr(line
, '\n'), *next
;
287 eol
= line
+ strlen(line
);
291 if (eol
- line
> key_len
&&
292 !strncmp(line
, key
, key_len
) &&
293 line
[key_len
] == ' ') {
294 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
300 static char *replace_encoding_header(char *buf
, const char *encoding
)
302 struct strbuf tmp
= STRBUF_INIT
;
306 /* guess if there is an encoding header before a \n\n */
307 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
308 cp
= strchr(cp
, '\n');
309 if (!cp
|| *++cp
== '\n')
313 cp
= strchr(cp
, '\n');
315 return buf
; /* should not happen but be defensive */
316 len
= cp
+ 1 - (buf
+ start
);
318 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
319 if (is_encoding_utf8(encoding
)) {
320 /* we have re-coded to UTF-8; drop the header */
321 strbuf_remove(&tmp
, start
, len
);
323 /* just replaces XXXX in 'encoding XXXX\n' */
324 strbuf_splice(&tmp
, start
+ strlen("encoding "),
325 len
- strlen("encoding \n"),
326 encoding
, strlen(encoding
));
328 return strbuf_detach(&tmp
, NULL
);
331 static char *logmsg_reencode(const struct commit
*commit
,
332 const char *output_encoding
)
334 static const char *utf8
= "UTF-8";
335 const char *use_encoding
;
339 if (!*output_encoding
)
341 encoding
= get_header(commit
, "encoding");
342 use_encoding
= encoding
? encoding
: utf8
;
343 if (!strcmp(use_encoding
, output_encoding
))
344 if (encoding
) /* we'll strip encoding header later */
345 out
= xstrdup(commit
->buffer
);
347 return NULL
; /* nothing to do */
349 out
= reencode_string(commit
->buffer
,
350 output_encoding
, use_encoding
);
352 out
= replace_encoding_header(out
, output_encoding
);
358 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
360 static struct string_list
*mail_map
;
362 mail_map
= xcalloc(1, sizeof(*mail_map
));
363 read_mailmap(mail_map
, NULL
);
365 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
368 static size_t format_person_part(struct strbuf
*sb
, char part
,
369 const char *msg
, int len
, enum date_mode dmode
)
371 /* currently all placeholders have same length */
372 const int placeholder_len
= 2;
373 int start
, end
, tz
= 0;
374 unsigned long date
= 0;
376 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
377 char person_name
[1024];
378 char person_mail
[1024];
380 /* advance 'end' to point to email start delimiter */
381 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
385 * When end points at the '<' that we found, it should have
386 * matching '>' later, which means 'end' must be strictly
392 /* Seek for both name and email part */
395 while (name_end
> name_start
&& isspace(*(name_end
-1)))
397 mail_start
= msg
+end
+1;
398 mail_end
= mail_start
;
399 while (mail_end
< msg_end
&& *mail_end
!= '>')
401 if (mail_end
== msg_end
)
405 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
406 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
407 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
408 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
409 name_start
= person_name
;
410 name_end
= name_start
+ strlen(person_name
);
411 mail_start
= person_mail
;
412 mail_end
= mail_start
+ strlen(person_mail
);
414 if (part
== 'n' || part
== 'N') { /* name */
415 strbuf_add(sb
, name_start
, name_end
-name_start
);
416 return placeholder_len
;
418 if (part
== 'e' || part
== 'E') { /* email */
419 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
420 return placeholder_len
;
423 /* advance 'start' to point to date start delimiter */
424 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
428 date
= strtoul(msg
+ start
, &ep
, 10);
429 if (msg
+ start
== ep
)
432 if (part
== 't') { /* date, UNIX timestamp */
433 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
434 return placeholder_len
;
438 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
440 if (start
+ 1 < len
) {
441 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
442 if (msg
[start
] == '-')
448 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
449 return placeholder_len
;
450 case 'D': /* date, RFC2822 style */
451 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
452 return placeholder_len
;
453 case 'r': /* date, relative */
454 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
455 return placeholder_len
;
456 case 'i': /* date, ISO 8601 */
457 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
458 return placeholder_len
;
463 * bogus commit, 'sb' cannot be updated, but we still need to
464 * compute a valid return value.
466 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
467 || part
== 'D' || part
== 'r' || part
== 'i')
468 return placeholder_len
;
470 return 0; /* unknown placeholder */
478 struct format_commit_context
{
479 const struct commit
*commit
;
480 const struct pretty_print_context
*pretty_ctx
;
481 unsigned commit_header_parsed
:1;
482 unsigned commit_message_parsed
:1;
483 size_t width
, indent1
, indent2
;
485 /* These offsets are relative to the start of the commit message. */
487 struct chunk committer
;
488 struct chunk encoding
;
493 /* The following ones are relative to the result struct strbuf. */
494 struct chunk abbrev_commit_hash
;
495 struct chunk abbrev_tree_hash
;
496 struct chunk abbrev_parent_hashes
;
500 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
503 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
508 * We haven't seen this chunk before. Our caller is surely
509 * going to add it the hard way now. Remember the most likely
510 * start of the to-be-added chunk: the current end of the
513 chunk
->off
= sb
->len
;
517 static void parse_commit_header(struct format_commit_context
*context
)
519 const char *msg
= context
->commit
->buffer
;
522 for (i
= 0; msg
[i
]; i
++) {
524 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
529 } else if (!prefixcmp(msg
+ i
, "author ")) {
530 context
->author
.off
= i
+ 7;
531 context
->author
.len
= eol
- i
- 7;
532 } else if (!prefixcmp(msg
+ i
, "committer ")) {
533 context
->committer
.off
= i
+ 10;
534 context
->committer
.len
= eol
- i
- 10;
535 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
536 context
->encoding
.off
= i
+ 9;
537 context
->encoding
.len
= eol
- i
- 9;
541 context
->message_off
= i
;
542 context
->commit_header_parsed
= 1;
545 static int istitlechar(char c
)
547 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
548 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
551 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
554 size_t start_len
= sb
->len
;
557 for (; *msg
&& *msg
!= '\n'; msg
++) {
558 if (istitlechar(*msg
)) {
560 strbuf_addch(sb
, '-');
562 strbuf_addch(sb
, *msg
);
564 while (*(msg
+1) == '.')
570 /* trim any trailing '.' or '-' characters */
572 while (sb
->len
- trimlen
> start_len
&&
573 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
574 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
576 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
579 const char *format_subject(struct strbuf
*sb
, const char *msg
,
580 const char *line_separator
)
585 const char *line
= msg
;
586 int linelen
= get_one_line(line
);
589 if (!linelen
|| is_empty_line(line
, &linelen
))
594 strbuf_grow(sb
, linelen
+ 2);
596 strbuf_addstr(sb
, line_separator
);
597 strbuf_add(sb
, line
, linelen
);
603 static void parse_commit_message(struct format_commit_context
*c
)
605 const char *msg
= c
->commit
->buffer
+ c
->message_off
;
606 const char *start
= c
->commit
->buffer
;
608 msg
= skip_empty_lines(msg
);
609 c
->subject_off
= msg
- start
;
611 msg
= format_subject(NULL
, msg
, NULL
);
612 msg
= skip_empty_lines(msg
);
613 c
->body_off
= msg
- start
;
615 c
->commit_message_parsed
= 1;
618 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
620 struct name_decoration
*d
;
621 const char *prefix
= " (";
623 load_ref_decorations(DECORATE_SHORT_REFS
);
624 d
= lookup_decoration(&name_decoration
, &commit
->object
);
626 strbuf_addstr(sb
, prefix
);
628 strbuf_addstr(sb
, d
->name
);
631 if (prefix
[0] == ',')
632 strbuf_addch(sb
, ')');
635 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
636 size_t width
, size_t indent1
, size_t indent2
)
638 struct strbuf tmp
= STRBUF_INIT
;
641 strbuf_add(&tmp
, sb
->buf
, pos
);
642 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
643 (int) indent1
, (int) indent2
, (int) width
);
644 strbuf_swap(&tmp
, sb
);
645 strbuf_release(&tmp
);
648 static void rewrap_message_tail(struct strbuf
*sb
,
649 struct format_commit_context
*c
,
650 size_t new_width
, size_t new_indent1
,
653 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
654 c
->indent2
== new_indent2
)
656 if (c
->wrap_start
< sb
->len
)
657 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
658 c
->wrap_start
= sb
->len
;
659 c
->width
= new_width
;
660 c
->indent1
= new_indent1
;
661 c
->indent2
= new_indent2
;
664 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
667 struct format_commit_context
*c
= context
;
668 const struct commit
*commit
= c
->commit
;
669 const char *msg
= commit
->buffer
;
670 struct commit_list
*p
;
673 /* these are independent of the commit */
674 switch (placeholder
[0]) {
676 if (placeholder
[1] == '(') {
677 const char *end
= strchr(placeholder
+ 2, ')');
678 char color
[COLOR_MAXLEN
];
681 color_parse_mem(placeholder
+ 2,
682 end
- (placeholder
+ 2),
683 "--pretty format", color
);
684 strbuf_addstr(sb
, color
);
685 return end
- placeholder
+ 1;
687 if (!prefixcmp(placeholder
+ 1, "red")) {
688 strbuf_addstr(sb
, GIT_COLOR_RED
);
690 } else if (!prefixcmp(placeholder
+ 1, "green")) {
691 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
693 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
694 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
696 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
697 strbuf_addstr(sb
, GIT_COLOR_RESET
);
701 case 'n': /* newline */
702 strbuf_addch(sb
, '\n');
705 /* %x00 == NUL, %x0a == LF, etc. */
706 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
708 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
710 strbuf_addch(sb
, (h1
<<4)|h2
);
715 if (placeholder
[1] == '(') {
716 unsigned long width
= 0, indent1
= 0, indent2
= 0;
718 const char *start
= placeholder
+ 2;
719 const char *end
= strchr(start
, ')');
723 width
= strtoul(start
, &next
, 10);
725 indent1
= strtoul(next
+ 1, &next
, 10);
727 indent2
= strtoul(next
+ 1,
734 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
735 return end
- placeholder
+ 1;
740 /* these depend on the commit */
741 if (!commit
->object
.parsed
)
742 parse_object(commit
->object
.sha1
);
744 switch (placeholder
[0]) {
745 case 'H': /* commit hash */
746 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
748 case 'h': /* abbreviated commit hash */
749 if (add_again(sb
, &c
->abbrev_commit_hash
))
751 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
753 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
755 case 'T': /* tree hash */
756 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
758 case 't': /* abbreviated tree hash */
759 if (add_again(sb
, &c
->abbrev_tree_hash
))
761 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
763 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
765 case 'P': /* parent hashes */
766 for (p
= commit
->parents
; p
; p
= p
->next
) {
767 if (p
!= commit
->parents
)
768 strbuf_addch(sb
, ' ');
769 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
772 case 'p': /* abbreviated parent hashes */
773 if (add_again(sb
, &c
->abbrev_parent_hashes
))
775 for (p
= commit
->parents
; p
; p
= p
->next
) {
776 if (p
!= commit
->parents
)
777 strbuf_addch(sb
, ' ');
778 strbuf_addstr(sb
, find_unique_abbrev(
779 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
781 c
->abbrev_parent_hashes
.len
= sb
->len
-
782 c
->abbrev_parent_hashes
.off
;
784 case 'm': /* left/right/bottom */
785 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
787 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
792 format_decoration(sb
, commit
);
794 case 'g': /* reflog info */
795 switch(placeholder
[1]) {
796 case 'd': /* reflog selector */
798 if (c
->pretty_ctx
->reflog_info
)
799 get_reflog_selector(sb
,
800 c
->pretty_ctx
->reflog_info
,
801 c
->pretty_ctx
->date_mode
,
802 (placeholder
[1] == 'd'));
804 case 's': /* reflog message */
805 if (c
->pretty_ctx
->reflog_info
)
806 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
809 return 0; /* unknown %g placeholder */
811 if (c
->pretty_ctx
->show_notes
) {
812 format_display_notes(commit
->object
.sha1
, sb
,
813 git_log_output_encoding
? git_log_output_encoding
814 : git_commit_encoding
, 0);
820 /* For the rest we have to parse the commit header. */
821 if (!c
->commit_header_parsed
)
822 parse_commit_header(c
);
824 switch (placeholder
[0]) {
825 case 'a': /* author ... */
826 return format_person_part(sb
, placeholder
[1],
827 msg
+ c
->author
.off
, c
->author
.len
,
828 c
->pretty_ctx
->date_mode
);
829 case 'c': /* committer ... */
830 return format_person_part(sb
, placeholder
[1],
831 msg
+ c
->committer
.off
, c
->committer
.len
,
832 c
->pretty_ctx
->date_mode
);
833 case 'e': /* encoding */
834 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
838 /* Now we need to parse the commit message. */
839 if (!c
->commit_message_parsed
)
840 parse_commit_message(c
);
842 switch (placeholder
[0]) {
843 case 's': /* subject */
844 format_subject(sb
, msg
+ c
->subject_off
, " ");
846 case 'f': /* sanitized subject */
847 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
850 strbuf_addstr(sb
, msg
+ c
->body_off
);
853 return 0; /* unknown placeholder */
856 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
863 ADD_LF_BEFORE_NON_EMPTY
,
867 switch (placeholder
[0]) {
869 magic
= DEL_LF_BEFORE_EMPTY
;
872 magic
= ADD_LF_BEFORE_NON_EMPTY
;
877 if (magic
!= NO_MAGIC
)
881 consumed
= format_commit_one(sb
, placeholder
, context
);
882 if (magic
== NO_MAGIC
)
885 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
886 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
887 strbuf_setlen(sb
, sb
->len
- 1);
888 } else if ((orig_len
!= sb
->len
) && magic
== ADD_LF_BEFORE_NON_EMPTY
) {
889 strbuf_insert(sb
, orig_len
, "\n", 1);
894 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
897 struct userformat_want
*w
= context
;
899 if (*placeholder
== '+' || *placeholder
== '-')
902 switch (*placeholder
) {
910 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
912 struct strbuf dummy
= STRBUF_INIT
;
919 strbuf_expand(&dummy
, user_format
, userformat_want_item
, w
);
920 strbuf_release(&dummy
);
923 void format_commit_message(const struct commit
*commit
,
924 const char *format
, struct strbuf
*sb
,
925 const struct pretty_print_context
*pretty_ctx
)
927 struct format_commit_context context
;
929 memset(&context
, 0, sizeof(context
));
930 context
.commit
= commit
;
931 context
.pretty_ctx
= pretty_ctx
;
932 context
.wrap_start
= sb
->len
;
933 strbuf_expand(sb
, format
, format_commit_item
, &context
);
934 rewrap_message_tail(sb
, &context
, 0, 0, 0);
937 static void pp_header(enum cmit_fmt fmt
,
939 enum date_mode dmode
,
940 const char *encoding
,
941 const struct commit
*commit
,
945 int parents_shown
= 0;
948 const char *line
= *msg_p
;
949 int linelen
= get_one_line(*msg_p
);
959 if (fmt
== CMIT_FMT_RAW
) {
960 strbuf_add(sb
, line
, linelen
);
964 if (!memcmp(line
, "parent ", 7)) {
966 die("bad parent line in commit");
970 if (!parents_shown
) {
971 struct commit_list
*parent
;
973 for (parent
= commit
->parents
, num
= 0;
975 parent
= parent
->next
, num
++)
977 /* with enough slop */
978 strbuf_grow(sb
, num
* 50 + 20);
979 add_merge_info(fmt
, sb
, commit
, abbrev
);
984 * MEDIUM == DEFAULT shows only author with dates.
985 * FULL shows both authors but not dates.
986 * FULLER shows both authors and dates.
988 if (!memcmp(line
, "author ", 7)) {
989 strbuf_grow(sb
, linelen
+ 80);
990 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
992 if (!memcmp(line
, "committer ", 10) &&
993 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
994 strbuf_grow(sb
, linelen
+ 80);
995 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
1000 void pp_title_line(enum cmit_fmt fmt
,
1003 const char *subject
,
1004 const char *after_subject
,
1005 const char *encoding
,
1008 const char *line_separator
= (fmt
== CMIT_FMT_EMAIL
) ? "\n " : " ";
1009 struct strbuf title
;
1011 strbuf_init(&title
, 80);
1012 *msg_p
= format_subject(&title
, *msg_p
, line_separator
);
1014 strbuf_grow(sb
, title
.len
+ 1024);
1016 strbuf_addstr(sb
, subject
);
1017 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
1019 strbuf_addbuf(sb
, &title
);
1021 strbuf_addch(sb
, '\n');
1023 if (need_8bit_cte
> 0) {
1024 const char *header_fmt
=
1025 "MIME-Version: 1.0\n"
1026 "Content-Type: text/plain; charset=%s\n"
1027 "Content-Transfer-Encoding: 8bit\n";
1028 strbuf_addf(sb
, header_fmt
, encoding
);
1030 if (after_subject
) {
1031 strbuf_addstr(sb
, after_subject
);
1033 if (fmt
== CMIT_FMT_EMAIL
) {
1034 strbuf_addch(sb
, '\n');
1036 strbuf_release(&title
);
1039 void pp_remainder(enum cmit_fmt fmt
,
1046 const char *line
= *msg_p
;
1047 int linelen
= get_one_line(line
);
1053 if (is_empty_line(line
, &linelen
)) {
1056 if (fmt
== CMIT_FMT_SHORT
)
1061 strbuf_grow(sb
, linelen
+ indent
+ 20);
1063 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1064 strbuf_setlen(sb
, sb
->len
+ indent
);
1066 strbuf_add(sb
, line
, linelen
);
1067 strbuf_addch(sb
, '\n');
1071 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1073 const char *encoding
;
1075 encoding
= (git_log_output_encoding
1076 ? git_log_output_encoding
1077 : git_commit_encoding
);
1081 *encoding_p
= encoding
;
1082 return logmsg_reencode(commit
, encoding
);
1085 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
1087 const struct pretty_print_context
*context
)
1089 unsigned long beginning_of_body
;
1091 const char *msg
= commit
->buffer
;
1093 const char *encoding
;
1094 int need_8bit_cte
= context
->need_8bit_cte
;
1096 if (fmt
== CMIT_FMT_USERFORMAT
) {
1097 format_commit_message(commit
, user_format
, sb
, context
);
1101 reencoded
= reencode_commit_message(commit
, &encoding
);
1106 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1110 * We need to check and emit Content-type: to mark it
1111 * as 8-bit if we haven't done so.
1113 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1116 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1118 /* author could be non 7-bit ASCII but
1119 * the log may be so; skip over the
1120 * header part first.
1122 if (ch
== '\n' && msg
[i
+1] == '\n')
1125 else if (non_ascii(ch
)) {
1132 pp_header(fmt
, context
->abbrev
, context
->date_mode
, encoding
,
1134 if (fmt
!= CMIT_FMT_ONELINE
&& !context
->subject
) {
1135 strbuf_addch(sb
, '\n');
1138 /* Skip excess blank lines at the beginning of body, if any... */
1139 msg
= skip_empty_lines(msg
);
1141 /* These formats treat the title line specially. */
1142 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1143 pp_title_line(fmt
, &msg
, sb
, context
->subject
,
1144 context
->after_subject
, encoding
, need_8bit_cte
);
1146 beginning_of_body
= sb
->len
;
1147 if (fmt
!= CMIT_FMT_ONELINE
)
1148 pp_remainder(fmt
, &msg
, sb
, indent
);
1151 /* Make sure there is an EOLN for the non-oneline case */
1152 if (fmt
!= CMIT_FMT_ONELINE
)
1153 strbuf_addch(sb
, '\n');
1156 * The caller may append additional body text in e-mail
1157 * format. Make sure we did not strip the blank line
1158 * between the header and the body.
1160 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1161 strbuf_addch(sb
, '\n');
1163 if (context
->show_notes
)
1164 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1165 NOTES_SHOW_HEADER
| NOTES_INDENT
);