6 #include "string-list.h"
11 #include "reflog-walk.h"
13 static char *user_format
;
15 static void save_user_format(struct rev_info
*rev
, const char *cp
, int is_tformat
)
18 user_format
= xstrdup(cp
);
20 rev
->use_terminator
= 1;
21 rev
->commit_format
= CMIT_FMT_USERFORMAT
;
24 void get_commit_format(const char *arg
, struct rev_info
*rev
)
27 static struct cmt_fmt_map
{
32 { "raw", 1, CMIT_FMT_RAW
},
33 { "medium", 1, CMIT_FMT_MEDIUM
},
34 { "short", 1, CMIT_FMT_SHORT
},
35 { "email", 1, CMIT_FMT_EMAIL
},
36 { "full", 5, CMIT_FMT_FULL
},
37 { "fuller", 5, CMIT_FMT_FULLER
},
38 { "oneline", 1, CMIT_FMT_ONELINE
},
41 rev
->use_terminator
= 0;
43 rev
->commit_format
= CMIT_FMT_DEFAULT
;
46 if (!prefixcmp(arg
, "format:") || !prefixcmp(arg
, "tformat:")) {
47 save_user_format(rev
, strchr(arg
, ':') + 1, arg
[0] == 't');
50 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
51 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
52 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
))) {
53 if (cmt_fmts
[i
].v
== CMIT_FMT_ONELINE
)
54 rev
->use_terminator
= 1;
55 rev
->commit_format
= cmt_fmts
[i
].v
;
59 if (strchr(arg
, '%')) {
60 save_user_format(rev
, arg
, 1);
64 die("invalid --pretty format: %s", arg
);
68 * Generic support for pretty-printing the header
70 static int get_one_line(const char *msg
)
85 /* High bit set, or ISO-2022-INT */
86 static int non_ascii(int ch
)
88 return !isascii(ch
) || ch
== '\033';
91 int has_non_ascii(const char *s
)
96 while ((ch
= *s
++) != '\0') {
103 static int is_rfc2047_special(char ch
)
105 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
108 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
109 const char *encoding
)
113 for (i
= 0; i
< len
; i
++) {
117 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
120 strbuf_add(sb
, line
, len
);
124 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
125 strbuf_addf(sb
, "=?%s?q?", encoding
);
126 for (i
= last
= 0; i
< len
; i
++) {
127 unsigned ch
= line
[i
] & 0xFF;
129 * We encode ' ' using '=20' even though rfc2047
130 * allows using '_' for readability. Unfortunately,
131 * many programs do not understand this and just
132 * leave the underscore in place.
134 if (is_rfc2047_special(ch
) || ch
== ' ') {
135 strbuf_add(sb
, line
+ last
, i
- last
);
136 strbuf_addf(sb
, "=%02X", ch
);
140 strbuf_add(sb
, line
+ last
, len
- last
);
141 strbuf_addstr(sb
, "?=");
144 void pp_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
145 const char *line
, enum date_mode dmode
,
146 const char *encoding
)
153 if (fmt
== CMIT_FMT_ONELINE
)
155 date
= strchr(line
, '>');
158 namelen
= ++date
- line
;
159 time
= strtoul(date
, &date
, 10);
160 tz
= strtol(date
, NULL
, 10);
162 if (fmt
== CMIT_FMT_EMAIL
) {
163 char *name_tail
= strchr(line
, '<');
164 int display_name_length
;
167 while (line
< name_tail
&& isspace(name_tail
[-1]))
169 display_name_length
= name_tail
- line
;
170 strbuf_addstr(sb
, "From: ");
171 add_rfc2047(sb
, line
, display_name_length
, encoding
);
172 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
173 strbuf_addch(sb
, '\n');
175 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
176 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
180 case CMIT_FMT_MEDIUM
:
181 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
184 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
186 case CMIT_FMT_FULLER
:
187 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
195 static int is_empty_line(const char *line
, int *len_p
)
198 while (len
&& isspace(line
[len
-1]))
204 static const char *skip_empty_lines(const char *msg
)
207 int linelen
= get_one_line(msg
);
211 if (!is_empty_line(msg
, &ll
))
218 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
219 const struct commit
*commit
, int abbrev
)
221 struct commit_list
*parent
= commit
->parents
;
223 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
224 !parent
|| !parent
->next
)
227 strbuf_addstr(sb
, "Merge:");
230 struct commit
*p
= parent
->item
;
231 const char *hex
= NULL
;
233 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
235 hex
= sha1_to_hex(p
->object
.sha1
);
236 parent
= parent
->next
;
238 strbuf_addf(sb
, " %s", hex
);
240 strbuf_addch(sb
, '\n');
243 static char *get_header(const struct commit
*commit
, const char *key
)
245 int key_len
= strlen(key
);
246 const char *line
= commit
->buffer
;
249 const char *eol
= strchr(line
, '\n'), *next
;
254 eol
= line
+ strlen(line
);
258 if (eol
- line
> key_len
&&
259 !strncmp(line
, key
, key_len
) &&
260 line
[key_len
] == ' ') {
261 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
267 static char *replace_encoding_header(char *buf
, const char *encoding
)
269 struct strbuf tmp
= STRBUF_INIT
;
273 /* guess if there is an encoding header before a \n\n */
274 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
275 cp
= strchr(cp
, '\n');
276 if (!cp
|| *++cp
== '\n')
280 cp
= strchr(cp
, '\n');
282 return buf
; /* should not happen but be defensive */
283 len
= cp
+ 1 - (buf
+ start
);
285 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
286 if (is_encoding_utf8(encoding
)) {
287 /* we have re-coded to UTF-8; drop the header */
288 strbuf_remove(&tmp
, start
, len
);
290 /* just replaces XXXX in 'encoding XXXX\n' */
291 strbuf_splice(&tmp
, start
+ strlen("encoding "),
292 len
- strlen("encoding \n"),
293 encoding
, strlen(encoding
));
295 return strbuf_detach(&tmp
, NULL
);
298 static char *logmsg_reencode(const struct commit
*commit
,
299 const char *output_encoding
)
301 static const char *utf8
= "UTF-8";
302 const char *use_encoding
;
306 if (!*output_encoding
)
308 encoding
= get_header(commit
, "encoding");
309 use_encoding
= encoding
? encoding
: utf8
;
310 if (!strcmp(use_encoding
, output_encoding
))
311 if (encoding
) /* we'll strip encoding header later */
312 out
= xstrdup(commit
->buffer
);
314 return NULL
; /* nothing to do */
316 out
= reencode_string(commit
->buffer
,
317 output_encoding
, use_encoding
);
319 out
= replace_encoding_header(out
, output_encoding
);
325 static int mailmap_name(char *email
, int email_len
, char *name
, int name_len
)
327 static struct string_list
*mail_map
;
329 mail_map
= xcalloc(1, sizeof(*mail_map
));
330 read_mailmap(mail_map
, NULL
);
332 return mail_map
->nr
&& map_user(mail_map
, email
, email_len
, name
, name_len
);
335 static size_t format_person_part(struct strbuf
*sb
, char part
,
336 const char *msg
, int len
, enum date_mode dmode
)
338 /* currently all placeholders have same length */
339 const int placeholder_len
= 2;
340 int start
, end
, tz
= 0;
341 unsigned long date
= 0;
343 const char *name_start
, *name_end
, *mail_start
, *mail_end
, *msg_end
= msg
+len
;
344 char person_name
[1024];
345 char person_mail
[1024];
347 /* advance 'end' to point to email start delimiter */
348 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
352 * When end points at the '<' that we found, it should have
353 * matching '>' later, which means 'end' must be strictly
359 /* Seek for both name and email part */
362 while (name_end
> name_start
&& isspace(*(name_end
-1)))
364 mail_start
= msg
+end
+1;
365 mail_end
= mail_start
;
366 while (mail_end
< msg_end
&& *mail_end
!= '>')
368 if (mail_end
== msg_end
)
372 if (part
== 'N' || part
== 'E') { /* mailmap lookup */
373 strlcpy(person_name
, name_start
, name_end
-name_start
+1);
374 strlcpy(person_mail
, mail_start
, mail_end
-mail_start
+1);
375 mailmap_name(person_mail
, sizeof(person_mail
), person_name
, sizeof(person_name
));
376 name_start
= person_name
;
377 name_end
= name_start
+ strlen(person_name
);
378 mail_start
= person_mail
;
379 mail_end
= mail_start
+ strlen(person_mail
);
381 if (part
== 'n' || part
== 'N') { /* name */
382 strbuf_add(sb
, name_start
, name_end
-name_start
);
383 return placeholder_len
;
385 if (part
== 'e' || part
== 'E') { /* email */
386 strbuf_add(sb
, mail_start
, mail_end
-mail_start
);
387 return placeholder_len
;
390 /* advance 'start' to point to date start delimiter */
391 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
395 date
= strtoul(msg
+ start
, &ep
, 10);
396 if (msg
+ start
== ep
)
399 if (part
== 't') { /* date, UNIX timestamp */
400 strbuf_add(sb
, msg
+ start
, ep
- (msg
+ start
));
401 return placeholder_len
;
405 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
407 if (start
+ 1 < len
) {
408 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
409 if (msg
[start
] == '-')
415 strbuf_addstr(sb
, show_date(date
, tz
, dmode
));
416 return placeholder_len
;
417 case 'D': /* date, RFC2822 style */
418 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RFC2822
));
419 return placeholder_len
;
420 case 'r': /* date, relative */
421 strbuf_addstr(sb
, show_date(date
, tz
, DATE_RELATIVE
));
422 return placeholder_len
;
423 case 'i': /* date, ISO 8601 */
424 strbuf_addstr(sb
, show_date(date
, tz
, DATE_ISO8601
));
425 return placeholder_len
;
430 * bogus commit, 'sb' cannot be updated, but we still need to
431 * compute a valid return value.
433 if (part
== 'n' || part
== 'e' || part
== 't' || part
== 'd'
434 || part
== 'D' || part
== 'r' || part
== 'i')
435 return placeholder_len
;
437 return 0; /* unknown placeholder */
445 struct format_commit_context
{
446 const struct commit
*commit
;
447 const struct pretty_print_context
*pretty_ctx
;
448 unsigned commit_header_parsed
:1;
449 unsigned commit_message_parsed
:1;
450 size_t width
, indent1
, indent2
;
452 /* These offsets are relative to the start of the commit message. */
454 struct chunk committer
;
455 struct chunk encoding
;
460 /* The following ones are relative to the result struct strbuf. */
461 struct chunk abbrev_commit_hash
;
462 struct chunk abbrev_tree_hash
;
463 struct chunk abbrev_parent_hashes
;
467 static int add_again(struct strbuf
*sb
, struct chunk
*chunk
)
470 strbuf_adddup(sb
, chunk
->off
, chunk
->len
);
475 * We haven't seen this chunk before. Our caller is surely
476 * going to add it the hard way now. Remember the most likely
477 * start of the to-be-added chunk: the current end of the
480 chunk
->off
= sb
->len
;
484 static void parse_commit_header(struct format_commit_context
*context
)
486 const char *msg
= context
->commit
->buffer
;
489 for (i
= 0; msg
[i
]; i
++) {
491 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
496 } else if (!prefixcmp(msg
+ i
, "author ")) {
497 context
->author
.off
= i
+ 7;
498 context
->author
.len
= eol
- i
- 7;
499 } else if (!prefixcmp(msg
+ i
, "committer ")) {
500 context
->committer
.off
= i
+ 10;
501 context
->committer
.len
= eol
- i
- 10;
502 } else if (!prefixcmp(msg
+ i
, "encoding ")) {
503 context
->encoding
.off
= i
+ 9;
504 context
->encoding
.len
= eol
- i
- 9;
508 context
->message_off
= i
;
509 context
->commit_header_parsed
= 1;
512 static int istitlechar(char c
)
514 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
515 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
518 static void format_sanitized_subject(struct strbuf
*sb
, const char *msg
)
521 size_t start_len
= sb
->len
;
524 for (; *msg
&& *msg
!= '\n'; msg
++) {
525 if (istitlechar(*msg
)) {
527 strbuf_addch(sb
, '-');
529 strbuf_addch(sb
, *msg
);
531 while (*(msg
+1) == '.')
537 /* trim any trailing '.' or '-' characters */
539 while (sb
->len
- trimlen
> start_len
&&
540 (sb
->buf
[sb
->len
- 1 - trimlen
] == '.'
541 || sb
->buf
[sb
->len
- 1 - trimlen
] == '-'))
543 strbuf_remove(sb
, sb
->len
- trimlen
, trimlen
);
546 const char *format_subject(struct strbuf
*sb
, const char *msg
,
547 const char *line_separator
)
552 const char *line
= msg
;
553 int linelen
= get_one_line(line
);
556 if (!linelen
|| is_empty_line(line
, &linelen
))
561 strbuf_grow(sb
, linelen
+ 2);
563 strbuf_addstr(sb
, line_separator
);
564 strbuf_add(sb
, line
, linelen
);
570 static void parse_commit_message(struct format_commit_context
*c
)
572 const char *msg
= c
->commit
->buffer
+ c
->message_off
;
573 const char *start
= c
->commit
->buffer
;
575 msg
= skip_empty_lines(msg
);
576 c
->subject_off
= msg
- start
;
578 msg
= format_subject(NULL
, msg
, NULL
);
579 msg
= skip_empty_lines(msg
);
580 c
->body_off
= msg
- start
;
582 c
->commit_message_parsed
= 1;
585 static void format_decoration(struct strbuf
*sb
, const struct commit
*commit
)
587 struct name_decoration
*d
;
588 const char *prefix
= " (";
590 load_ref_decorations(DECORATE_SHORT_REFS
);
591 d
= lookup_decoration(&name_decoration
, &commit
->object
);
593 strbuf_addstr(sb
, prefix
);
595 strbuf_addstr(sb
, d
->name
);
598 if (prefix
[0] == ',')
599 strbuf_addch(sb
, ')');
602 static void strbuf_wrap(struct strbuf
*sb
, size_t pos
,
603 size_t width
, size_t indent1
, size_t indent2
)
605 struct strbuf tmp
= STRBUF_INIT
;
608 strbuf_add(&tmp
, sb
->buf
, pos
);
609 strbuf_add_wrapped_text(&tmp
, sb
->buf
+ pos
,
610 (int) indent1
, (int) indent2
, (int) width
);
611 strbuf_swap(&tmp
, sb
);
612 strbuf_release(&tmp
);
615 static void rewrap_message_tail(struct strbuf
*sb
,
616 struct format_commit_context
*c
,
617 size_t new_width
, size_t new_indent1
,
620 if (c
->width
== new_width
&& c
->indent1
== new_indent1
&&
621 c
->indent2
== new_indent2
)
623 if (c
->wrap_start
< sb
->len
)
624 strbuf_wrap(sb
, c
->wrap_start
, c
->width
, c
->indent1
, c
->indent2
);
625 c
->wrap_start
= sb
->len
;
626 c
->width
= new_width
;
627 c
->indent1
= new_indent1
;
628 c
->indent2
= new_indent2
;
631 static size_t format_commit_one(struct strbuf
*sb
, const char *placeholder
,
634 struct format_commit_context
*c
= context
;
635 const struct commit
*commit
= c
->commit
;
636 const char *msg
= commit
->buffer
;
637 struct commit_list
*p
;
640 /* these are independent of the commit */
641 switch (placeholder
[0]) {
643 if (placeholder
[1] == '(') {
644 const char *end
= strchr(placeholder
+ 2, ')');
645 char color
[COLOR_MAXLEN
];
648 color_parse_mem(placeholder
+ 2,
649 end
- (placeholder
+ 2),
650 "--pretty format", color
);
651 strbuf_addstr(sb
, color
);
652 return end
- placeholder
+ 1;
654 if (!prefixcmp(placeholder
+ 1, "red")) {
655 strbuf_addstr(sb
, GIT_COLOR_RED
);
657 } else if (!prefixcmp(placeholder
+ 1, "green")) {
658 strbuf_addstr(sb
, GIT_COLOR_GREEN
);
660 } else if (!prefixcmp(placeholder
+ 1, "blue")) {
661 strbuf_addstr(sb
, GIT_COLOR_BLUE
);
663 } else if (!prefixcmp(placeholder
+ 1, "reset")) {
664 strbuf_addstr(sb
, GIT_COLOR_RESET
);
668 case 'n': /* newline */
669 strbuf_addch(sb
, '\n');
672 /* %x00 == NUL, %x0a == LF, etc. */
673 if (0 <= (h1
= hexval_table
[0xff & placeholder
[1]]) &&
675 0 <= (h2
= hexval_table
[0xff & placeholder
[2]]) &&
677 strbuf_addch(sb
, (h1
<<4)|h2
);
682 if (placeholder
[1] == '(') {
683 unsigned long width
= 0, indent1
= 0, indent2
= 0;
685 const char *start
= placeholder
+ 2;
686 const char *end
= strchr(start
, ')');
690 width
= strtoul(start
, &next
, 10);
692 indent1
= strtoul(next
+ 1, &next
, 10);
694 indent2
= strtoul(next
+ 1,
701 rewrap_message_tail(sb
, c
, width
, indent1
, indent2
);
702 return end
- placeholder
+ 1;
707 /* these depend on the commit */
708 if (!commit
->object
.parsed
)
709 parse_object(commit
->object
.sha1
);
711 switch (placeholder
[0]) {
712 case 'H': /* commit hash */
713 strbuf_addstr(sb
, sha1_to_hex(commit
->object
.sha1
));
715 case 'h': /* abbreviated commit hash */
716 if (add_again(sb
, &c
->abbrev_commit_hash
))
718 strbuf_addstr(sb
, find_unique_abbrev(commit
->object
.sha1
,
720 c
->abbrev_commit_hash
.len
= sb
->len
- c
->abbrev_commit_hash
.off
;
722 case 'T': /* tree hash */
723 strbuf_addstr(sb
, sha1_to_hex(commit
->tree
->object
.sha1
));
725 case 't': /* abbreviated tree hash */
726 if (add_again(sb
, &c
->abbrev_tree_hash
))
728 strbuf_addstr(sb
, find_unique_abbrev(commit
->tree
->object
.sha1
,
730 c
->abbrev_tree_hash
.len
= sb
->len
- c
->abbrev_tree_hash
.off
;
732 case 'P': /* parent hashes */
733 for (p
= commit
->parents
; p
; p
= p
->next
) {
734 if (p
!= commit
->parents
)
735 strbuf_addch(sb
, ' ');
736 strbuf_addstr(sb
, sha1_to_hex(p
->item
->object
.sha1
));
739 case 'p': /* abbreviated parent hashes */
740 if (add_again(sb
, &c
->abbrev_parent_hashes
))
742 for (p
= commit
->parents
; p
; p
= p
->next
) {
743 if (p
!= commit
->parents
)
744 strbuf_addch(sb
, ' ');
745 strbuf_addstr(sb
, find_unique_abbrev(
746 p
->item
->object
.sha1
, DEFAULT_ABBREV
));
748 c
->abbrev_parent_hashes
.len
= sb
->len
-
749 c
->abbrev_parent_hashes
.off
;
751 case 'm': /* left/right/bottom */
752 strbuf_addch(sb
, (commit
->object
.flags
& BOUNDARY
)
754 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
759 format_decoration(sb
, commit
);
761 case 'g': /* reflog info */
762 switch(placeholder
[1]) {
763 case 'd': /* reflog selector */
765 if (c
->pretty_ctx
->reflog_info
)
766 get_reflog_selector(sb
,
767 c
->pretty_ctx
->reflog_info
,
768 c
->pretty_ctx
->date_mode
,
769 (placeholder
[1] == 'd'));
771 case 's': /* reflog message */
772 if (c
->pretty_ctx
->reflog_info
)
773 get_reflog_message(sb
, c
->pretty_ctx
->reflog_info
);
776 return 0; /* unknown %g placeholder */
778 if (c
->pretty_ctx
->show_notes
) {
779 format_display_notes(commit
->object
.sha1
, sb
,
780 git_log_output_encoding
? git_log_output_encoding
781 : git_commit_encoding
, 0);
787 /* For the rest we have to parse the commit header. */
788 if (!c
->commit_header_parsed
)
789 parse_commit_header(c
);
791 switch (placeholder
[0]) {
792 case 'a': /* author ... */
793 return format_person_part(sb
, placeholder
[1],
794 msg
+ c
->author
.off
, c
->author
.len
,
795 c
->pretty_ctx
->date_mode
);
796 case 'c': /* committer ... */
797 return format_person_part(sb
, placeholder
[1],
798 msg
+ c
->committer
.off
, c
->committer
.len
,
799 c
->pretty_ctx
->date_mode
);
800 case 'e': /* encoding */
801 strbuf_add(sb
, msg
+ c
->encoding
.off
, c
->encoding
.len
);
803 case 'B': /* raw body */
804 /* message_off is always left at the initial newline */
805 strbuf_addstr(sb
, msg
+ c
->message_off
+ 1);
809 /* Now we need to parse the commit message. */
810 if (!c
->commit_message_parsed
)
811 parse_commit_message(c
);
813 switch (placeholder
[0]) {
814 case 's': /* subject */
815 format_subject(sb
, msg
+ c
->subject_off
, " ");
817 case 'f': /* sanitized subject */
818 format_sanitized_subject(sb
, msg
+ c
->subject_off
);
821 strbuf_addstr(sb
, msg
+ c
->body_off
);
824 return 0; /* unknown placeholder */
827 static size_t format_commit_item(struct strbuf
*sb
, const char *placeholder
,
834 ADD_LF_BEFORE_NON_EMPTY
,
838 switch (placeholder
[0]) {
840 magic
= DEL_LF_BEFORE_EMPTY
;
843 magic
= ADD_LF_BEFORE_NON_EMPTY
;
848 if (magic
!= NO_MAGIC
)
852 consumed
= format_commit_one(sb
, placeholder
, context
);
853 if (magic
== NO_MAGIC
)
856 if ((orig_len
== sb
->len
) && magic
== DEL_LF_BEFORE_EMPTY
) {
857 while (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n')
858 strbuf_setlen(sb
, sb
->len
- 1);
859 } else if ((orig_len
!= sb
->len
) && magic
== ADD_LF_BEFORE_NON_EMPTY
) {
860 strbuf_insert(sb
, orig_len
, "\n", 1);
865 static size_t userformat_want_item(struct strbuf
*sb
, const char *placeholder
,
868 struct userformat_want
*w
= context
;
870 if (*placeholder
== '+' || *placeholder
== '-')
873 switch (*placeholder
) {
881 void userformat_find_requirements(const char *fmt
, struct userformat_want
*w
)
883 struct strbuf dummy
= STRBUF_INIT
;
890 strbuf_expand(&dummy
, user_format
, userformat_want_item
, w
);
891 strbuf_release(&dummy
);
894 void format_commit_message(const struct commit
*commit
,
895 const char *format
, struct strbuf
*sb
,
896 const struct pretty_print_context
*pretty_ctx
)
898 struct format_commit_context context
;
900 memset(&context
, 0, sizeof(context
));
901 context
.commit
= commit
;
902 context
.pretty_ctx
= pretty_ctx
;
903 context
.wrap_start
= sb
->len
;
904 strbuf_expand(sb
, format
, format_commit_item
, &context
);
905 rewrap_message_tail(sb
, &context
, 0, 0, 0);
908 static void pp_header(enum cmit_fmt fmt
,
910 enum date_mode dmode
,
911 const char *encoding
,
912 const struct commit
*commit
,
916 int parents_shown
= 0;
919 const char *line
= *msg_p
;
920 int linelen
= get_one_line(*msg_p
);
930 if (fmt
== CMIT_FMT_RAW
) {
931 strbuf_add(sb
, line
, linelen
);
935 if (!memcmp(line
, "parent ", 7)) {
937 die("bad parent line in commit");
941 if (!parents_shown
) {
942 struct commit_list
*parent
;
944 for (parent
= commit
->parents
, num
= 0;
946 parent
= parent
->next
, num
++)
948 /* with enough slop */
949 strbuf_grow(sb
, num
* 50 + 20);
950 add_merge_info(fmt
, sb
, commit
, abbrev
);
955 * MEDIUM == DEFAULT shows only author with dates.
956 * FULL shows both authors but not dates.
957 * FULLER shows both authors and dates.
959 if (!memcmp(line
, "author ", 7)) {
960 strbuf_grow(sb
, linelen
+ 80);
961 pp_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
963 if (!memcmp(line
, "committer ", 10) &&
964 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
965 strbuf_grow(sb
, linelen
+ 80);
966 pp_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
971 void pp_title_line(enum cmit_fmt fmt
,
975 const char *after_subject
,
976 const char *encoding
,
979 const char *line_separator
= (fmt
== CMIT_FMT_EMAIL
) ? "\n " : " ";
982 strbuf_init(&title
, 80);
983 *msg_p
= format_subject(&title
, *msg_p
, line_separator
);
985 strbuf_grow(sb
, title
.len
+ 1024);
987 strbuf_addstr(sb
, subject
);
988 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
990 strbuf_addbuf(sb
, &title
);
992 strbuf_addch(sb
, '\n');
994 if (need_8bit_cte
> 0) {
995 const char *header_fmt
=
996 "MIME-Version: 1.0\n"
997 "Content-Type: text/plain; charset=%s\n"
998 "Content-Transfer-Encoding: 8bit\n";
999 strbuf_addf(sb
, header_fmt
, encoding
);
1001 if (after_subject
) {
1002 strbuf_addstr(sb
, after_subject
);
1004 if (fmt
== CMIT_FMT_EMAIL
) {
1005 strbuf_addch(sb
, '\n');
1007 strbuf_release(&title
);
1010 void pp_remainder(enum cmit_fmt fmt
,
1017 const char *line
= *msg_p
;
1018 int linelen
= get_one_line(line
);
1024 if (is_empty_line(line
, &linelen
)) {
1027 if (fmt
== CMIT_FMT_SHORT
)
1032 strbuf_grow(sb
, linelen
+ indent
+ 20);
1034 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1035 strbuf_setlen(sb
, sb
->len
+ indent
);
1037 strbuf_add(sb
, line
, linelen
);
1038 strbuf_addch(sb
, '\n');
1042 char *reencode_commit_message(const struct commit
*commit
, const char **encoding_p
)
1044 const char *encoding
;
1046 encoding
= (git_log_output_encoding
1047 ? git_log_output_encoding
1048 : git_commit_encoding
);
1052 *encoding_p
= encoding
;
1053 return logmsg_reencode(commit
, encoding
);
1056 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
1058 const struct pretty_print_context
*context
)
1060 unsigned long beginning_of_body
;
1062 const char *msg
= commit
->buffer
;
1064 const char *encoding
;
1065 int need_8bit_cte
= context
->need_8bit_cte
;
1067 if (fmt
== CMIT_FMT_USERFORMAT
) {
1068 format_commit_message(commit
, user_format
, sb
, context
);
1072 reencoded
= reencode_commit_message(commit
, &encoding
);
1077 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1081 * We need to check and emit Content-type: to mark it
1082 * as 8-bit if we haven't done so.
1084 if (fmt
== CMIT_FMT_EMAIL
&& need_8bit_cte
== 0) {
1087 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1089 /* author could be non 7-bit ASCII but
1090 * the log may be so; skip over the
1091 * header part first.
1093 if (ch
== '\n' && msg
[i
+1] == '\n')
1096 else if (non_ascii(ch
)) {
1103 pp_header(fmt
, context
->abbrev
, context
->date_mode
, encoding
,
1105 if (fmt
!= CMIT_FMT_ONELINE
&& !context
->subject
) {
1106 strbuf_addch(sb
, '\n');
1109 /* Skip excess blank lines at the beginning of body, if any... */
1110 msg
= skip_empty_lines(msg
);
1112 /* These formats treat the title line specially. */
1113 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1114 pp_title_line(fmt
, &msg
, sb
, context
->subject
,
1115 context
->after_subject
, encoding
, need_8bit_cte
);
1117 beginning_of_body
= sb
->len
;
1118 if (fmt
!= CMIT_FMT_ONELINE
)
1119 pp_remainder(fmt
, &msg
, sb
, indent
);
1122 /* Make sure there is an EOLN for the non-oneline case */
1123 if (fmt
!= CMIT_FMT_ONELINE
)
1124 strbuf_addch(sb
, '\n');
1127 * The caller may append additional body text in e-mail
1128 * format. Make sure we did not strip the blank line
1129 * between the header and the body.
1131 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1132 strbuf_addch(sb
, '\n');
1134 if (context
->show_notes
)
1135 format_display_notes(commit
->object
.sha1
, sb
, encoding
,
1136 NOTES_SHOW_HEADER
| NOTES_INDENT
);