3 #include "interpolate.h"
8 static struct cmt_fmt_map
{
13 { "raw", 1, CMIT_FMT_RAW
},
14 { "medium", 1, CMIT_FMT_MEDIUM
},
15 { "short", 1, CMIT_FMT_SHORT
},
16 { "email", 1, CMIT_FMT_EMAIL
},
17 { "full", 5, CMIT_FMT_FULL
},
18 { "fuller", 5, CMIT_FMT_FULLER
},
19 { "oneline", 1, CMIT_FMT_ONELINE
},
20 { "format:", 7, CMIT_FMT_USERFORMAT
},
23 static char *user_format
;
25 enum cmit_fmt
get_commit_format(const char *arg
)
30 return CMIT_FMT_DEFAULT
;
33 if (!prefixcmp(arg
, "format:")) {
36 user_format
= xstrdup(arg
+ 7);
37 return CMIT_FMT_USERFORMAT
;
39 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
40 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
41 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
)))
45 die("invalid --pretty format: %s", arg
);
49 * Generic support for pretty-printing the header
51 static int get_one_line(const char *msg
)
66 /* High bit set, or ISO-2022-INT */
70 return ((ch
& 0x80) || (ch
== 0x1b));
73 static int is_rfc2047_special(char ch
)
75 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
78 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
83 for (i
= 0; i
< len
; i
++) {
87 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
90 strbuf_add(sb
, line
, len
);
94 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
95 strbuf_addf(sb
, "=?%s?q?", encoding
);
96 for (i
= last
= 0; i
< len
; i
++) {
97 unsigned ch
= line
[i
] & 0xFF;
99 * We encode ' ' using '=20' even though rfc2047
100 * allows using '_' for readability. Unfortunately,
101 * many programs do not understand this and just
102 * leave the underscore in place.
104 if (is_rfc2047_special(ch
) || ch
== ' ') {
105 strbuf_add(sb
, line
+ last
, i
- last
);
106 strbuf_addf(sb
, "=%02X", ch
);
110 strbuf_add(sb
, line
+ last
, len
- last
);
111 strbuf_addstr(sb
, "?=");
114 static void add_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
115 const char *line
, enum date_mode dmode
,
116 const char *encoding
)
122 const char *filler
= " ";
124 if (fmt
== CMIT_FMT_ONELINE
)
126 date
= strchr(line
, '>');
129 namelen
= ++date
- line
;
130 time
= strtoul(date
, &date
, 10);
131 tz
= strtol(date
, NULL
, 10);
133 if (fmt
== CMIT_FMT_EMAIL
) {
134 char *name_tail
= strchr(line
, '<');
135 int display_name_length
;
138 while (line
< name_tail
&& isspace(name_tail
[-1]))
140 display_name_length
= name_tail
- line
;
142 strbuf_addstr(sb
, "From: ");
143 add_rfc2047(sb
, line
, display_name_length
, encoding
);
144 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
145 strbuf_addch(sb
, '\n');
147 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
148 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
149 filler
, namelen
, line
);
152 case CMIT_FMT_MEDIUM
:
153 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
156 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
158 case CMIT_FMT_FULLER
:
159 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
167 static int is_empty_line(const char *line
, int *len_p
)
170 while (len
&& isspace(line
[len
-1]))
176 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
177 const struct commit
*commit
, int abbrev
)
179 struct commit_list
*parent
= commit
->parents
;
181 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
182 !parent
|| !parent
->next
)
185 strbuf_addstr(sb
, "Merge:");
188 struct commit
*p
= parent
->item
;
189 const char *hex
= NULL
;
192 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
194 hex
= sha1_to_hex(p
->object
.sha1
);
195 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
196 parent
= parent
->next
;
198 strbuf_addf(sb
, " %s%s", hex
, dots
);
200 strbuf_addch(sb
, '\n');
203 static char *get_header(const struct commit
*commit
, const char *key
)
205 int key_len
= strlen(key
);
206 const char *line
= commit
->buffer
;
209 const char *eol
= strchr(line
, '\n'), *next
;
214 eol
= line
+ strlen(line
);
218 if (eol
- line
> key_len
&&
219 !strncmp(line
, key
, key_len
) &&
220 line
[key_len
] == ' ') {
221 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
227 static char *replace_encoding_header(char *buf
, const char *encoding
)
233 /* guess if there is an encoding header before a \n\n */
234 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
235 cp
= strchr(cp
, '\n');
236 if (!cp
|| *++cp
== '\n')
240 cp
= strchr(cp
, '\n');
242 return buf
; /* should not happen but be defensive */
243 len
= cp
+ 1 - (buf
+ start
);
245 strbuf_init(&tmp
, 0);
246 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
247 if (is_encoding_utf8(encoding
)) {
248 /* we have re-coded to UTF-8; drop the header */
249 strbuf_remove(&tmp
, start
, len
);
251 /* just replaces XXXX in 'encoding XXXX\n' */
252 strbuf_splice(&tmp
, start
+ strlen("encoding "),
253 len
- strlen("encoding \n"),
254 encoding
, strlen(encoding
));
256 return strbuf_detach(&tmp
, NULL
);
259 static char *logmsg_reencode(const struct commit
*commit
,
260 const char *output_encoding
)
262 static const char *utf8
= "utf-8";
263 const char *use_encoding
;
267 if (!*output_encoding
)
269 encoding
= get_header(commit
, "encoding");
270 use_encoding
= encoding
? encoding
: utf8
;
271 if (!strcmp(use_encoding
, output_encoding
))
272 if (encoding
) /* we'll strip encoding header later */
273 out
= xstrdup(commit
->buffer
);
275 return NULL
; /* nothing to do */
277 out
= reencode_string(commit
->buffer
,
278 output_encoding
, use_encoding
);
280 out
= replace_encoding_header(out
, output_encoding
);
286 static void fill_person(struct interp
*table
, const char *msg
, int len
)
288 int start
, end
, tz
= 0;
293 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
296 while (end
> 0 && isspace(msg
[end
- 1]))
298 table
[0].value
= xmemdupz(msg
, end
);
304 for (end
= start
+ 1; end
< len
&& msg
[end
] != '>'; end
++)
310 table
[1].value
= xmemdupz(msg
+ start
, end
- start
);
313 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
317 date
= strtoul(msg
+ start
, &ep
, 10);
318 if (msg
+ start
== ep
)
321 table
[5].value
= xmemdupz(msg
+ start
, ep
- (msg
+ start
));
324 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
326 if (start
+ 1 < len
) {
327 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
328 if (msg
[start
] == '-')
332 interp_set_entry(table
, 2, show_date(date
, tz
, DATE_NORMAL
));
333 interp_set_entry(table
, 3, show_date(date
, tz
, DATE_RFC2822
));
334 interp_set_entry(table
, 4, show_date(date
, tz
, DATE_RELATIVE
));
335 interp_set_entry(table
, 6, show_date(date
, tz
, DATE_ISO8601
));
338 void format_commit_message(const struct commit
*commit
,
339 const void *format
, struct strbuf
*sb
)
341 struct interp table
[] = {
342 { "%H" }, /* commit hash */
343 { "%h" }, /* abbreviated commit hash */
344 { "%T" }, /* tree hash */
345 { "%t" }, /* abbreviated tree hash */
346 { "%P" }, /* parent hashes */
347 { "%p" }, /* abbreviated parent hashes */
348 { "%an" }, /* author name */
349 { "%ae" }, /* author email */
350 { "%ad" }, /* author date */
351 { "%aD" }, /* author date, RFC2822 style */
352 { "%ar" }, /* author date, relative */
353 { "%at" }, /* author date, UNIX timestamp */
354 { "%ai" }, /* author date, ISO 8601 */
355 { "%cn" }, /* committer name */
356 { "%ce" }, /* committer email */
357 { "%cd" }, /* committer date */
358 { "%cD" }, /* committer date, RFC2822 style */
359 { "%cr" }, /* committer date, relative */
360 { "%ct" }, /* committer date, UNIX timestamp */
361 { "%ci" }, /* committer date, ISO 8601 */
362 { "%e" }, /* encoding */
363 { "%s" }, /* subject */
365 { "%Cred" }, /* red */
366 { "%Cgreen" }, /* green */
367 { "%Cblue" }, /* blue */
368 { "%Creset" }, /* reset color */
369 { "%n" }, /* newline */
370 { "%m" }, /* left/right/bottom */
373 IHASH
= 0, IHASH_ABBREV
,
375 IPARENTS
, IPARENTS_ABBREV
,
376 IAUTHOR_NAME
, IAUTHOR_EMAIL
,
377 IAUTHOR_DATE
, IAUTHOR_DATE_RFC2822
, IAUTHOR_DATE_RELATIVE
,
378 IAUTHOR_TIMESTAMP
, IAUTHOR_ISO8601
,
379 ICOMMITTER_NAME
, ICOMMITTER_EMAIL
,
380 ICOMMITTER_DATE
, ICOMMITTER_DATE_RFC2822
,
381 ICOMMITTER_DATE_RELATIVE
, ICOMMITTER_TIMESTAMP
,
386 IRED
, IGREEN
, IBLUE
, IRESET_COLOR
,
390 struct commit_list
*p
;
394 enum { HEADER
, SUBJECT
, BODY
} state
;
395 const char *msg
= commit
->buffer
;
397 if (ILEFT_RIGHT
+ 1 != ARRAY_SIZE(table
))
398 die("invalid interp table!");
400 /* these are independent of the commit */
401 interp_set_entry(table
, IRED
, "\033[31m");
402 interp_set_entry(table
, IGREEN
, "\033[32m");
403 interp_set_entry(table
, IBLUE
, "\033[34m");
404 interp_set_entry(table
, IRESET_COLOR
, "\033[m");
405 interp_set_entry(table
, INEWLINE
, "\n");
407 /* these depend on the commit */
408 if (!commit
->object
.parsed
)
409 parse_object(commit
->object
.sha1
);
410 interp_set_entry(table
, IHASH
, sha1_to_hex(commit
->object
.sha1
));
411 interp_set_entry(table
, IHASH_ABBREV
,
412 find_unique_abbrev(commit
->object
.sha1
,
414 interp_set_entry(table
, ITREE
, sha1_to_hex(commit
->tree
->object
.sha1
));
415 interp_set_entry(table
, ITREE_ABBREV
,
416 find_unique_abbrev(commit
->tree
->object
.sha1
,
418 interp_set_entry(table
, ILEFT_RIGHT
,
419 (commit
->object
.flags
& BOUNDARY
)
421 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
426 for (i
= 0, p
= commit
->parents
;
427 p
&& i
< sizeof(parents
) - 1;
429 i
+= snprintf(parents
+ i
, sizeof(parents
) - i
- 1, " %s",
430 sha1_to_hex(p
->item
->object
.sha1
));
431 interp_set_entry(table
, IPARENTS
, parents
+ 1);
434 for (i
= 0, p
= commit
->parents
;
435 p
&& i
< sizeof(parents
) - 1;
437 i
+= snprintf(parents
+ i
, sizeof(parents
) - i
- 1, " %s",
438 find_unique_abbrev(p
->item
->object
.sha1
,
440 interp_set_entry(table
, IPARENTS_ABBREV
, parents
+ 1);
442 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
444 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
447 if (state
== SUBJECT
) {
448 table
[ISUBJECT
].value
= xmemdupz(msg
+ i
, eol
- i
);
453 /* strip empty lines */
454 while (msg
[eol
+ 1] == '\n')
456 } else if (!prefixcmp(msg
+ i
, "author "))
457 fill_person(table
+ IAUTHOR_NAME
,
458 msg
+ i
+ 7, eol
- i
- 7);
459 else if (!prefixcmp(msg
+ i
, "committer "))
460 fill_person(table
+ ICOMMITTER_NAME
,
461 msg
+ i
+ 10, eol
- i
- 10);
462 else if (!prefixcmp(msg
+ i
, "encoding "))
463 table
[IENCODING
].value
=
464 xmemdupz(msg
+ i
+ 9, eol
- i
- 9);
468 table
[IBODY
].value
= xstrdup(msg
+ i
);
470 len
= interpolate(sb
->buf
+ sb
->len
, strbuf_avail(sb
),
471 format
, table
, ARRAY_SIZE(table
));
472 if (len
> strbuf_avail(sb
)) {
473 strbuf_grow(sb
, len
);
474 interpolate(sb
->buf
+ sb
->len
, strbuf_avail(sb
) + 1,
475 format
, table
, ARRAY_SIZE(table
));
477 strbuf_setlen(sb
, sb
->len
+ len
);
478 interp_clear_table(table
, ARRAY_SIZE(table
));
481 static void pp_header(enum cmit_fmt fmt
,
483 enum date_mode dmode
,
484 const char *encoding
,
485 const struct commit
*commit
,
489 int parents_shown
= 0;
492 const char *line
= *msg_p
;
493 int linelen
= get_one_line(*msg_p
);
503 if (fmt
== CMIT_FMT_RAW
) {
504 strbuf_add(sb
, line
, linelen
);
508 if (!memcmp(line
, "parent ", 7)) {
510 die("bad parent line in commit");
514 if (!parents_shown
) {
515 struct commit_list
*parent
;
517 for (parent
= commit
->parents
, num
= 0;
519 parent
= parent
->next
, num
++)
521 /* with enough slop */
522 strbuf_grow(sb
, num
* 50 + 20);
523 add_merge_info(fmt
, sb
, commit
, abbrev
);
528 * MEDIUM == DEFAULT shows only author with dates.
529 * FULL shows both authors but not dates.
530 * FULLER shows both authors and dates.
532 if (!memcmp(line
, "author ", 7)) {
533 strbuf_grow(sb
, linelen
+ 80);
534 add_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
536 if (!memcmp(line
, "committer ", 10) &&
537 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
538 strbuf_grow(sb
, linelen
+ 80);
539 add_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
544 static void pp_title_line(enum cmit_fmt fmt
,
548 const char *after_subject
,
549 const char *encoding
,
554 strbuf_init(&title
, 80);
557 const char *line
= *msg_p
;
558 int linelen
= get_one_line(line
);
561 if (!linelen
|| is_empty_line(line
, &linelen
))
564 strbuf_grow(&title
, linelen
+ 2);
566 if (fmt
== CMIT_FMT_EMAIL
) {
567 strbuf_addch(&title
, '\n');
569 strbuf_addch(&title
, ' ');
571 strbuf_add(&title
, line
, linelen
);
574 strbuf_grow(sb
, title
.len
+ 1024);
576 strbuf_addstr(sb
, subject
);
577 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
579 strbuf_addbuf(sb
, &title
);
581 strbuf_addch(sb
, '\n');
583 if (plain_non_ascii
) {
584 const char *header_fmt
=
585 "MIME-Version: 1.0\n"
586 "Content-Type: text/plain; charset=%s\n"
587 "Content-Transfer-Encoding: 8bit\n";
588 strbuf_addf(sb
, header_fmt
, encoding
);
591 strbuf_addstr(sb
, after_subject
);
593 if (fmt
== CMIT_FMT_EMAIL
) {
594 strbuf_addch(sb
, '\n');
596 strbuf_release(&title
);
599 static void pp_remainder(enum cmit_fmt fmt
,
606 const char *line
= *msg_p
;
607 int linelen
= get_one_line(line
);
613 if (is_empty_line(line
, &linelen
)) {
616 if (fmt
== CMIT_FMT_SHORT
)
621 strbuf_grow(sb
, linelen
+ indent
+ 20);
623 memset(sb
->buf
+ sb
->len
, ' ', indent
);
624 strbuf_setlen(sb
, sb
->len
+ indent
);
626 strbuf_add(sb
, line
, linelen
);
627 strbuf_addch(sb
, '\n');
631 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
632 struct strbuf
*sb
, int abbrev
,
633 const char *subject
, const char *after_subject
,
634 enum date_mode dmode
, int plain_non_ascii
)
636 unsigned long beginning_of_body
;
638 const char *msg
= commit
->buffer
;
640 const char *encoding
;
642 if (fmt
== CMIT_FMT_USERFORMAT
) {
643 format_commit_message(commit
, user_format
, sb
);
647 encoding
= (git_log_output_encoding
648 ? git_log_output_encoding
649 : git_commit_encoding
);
652 reencoded
= logmsg_reencode(commit
, encoding
);
657 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
660 /* After-subject is used to pass in Content-Type: multipart
661 * MIME header; in that case we do not have to do the
662 * plaintext content type even if the commit message has
663 * non 7-bit ASCII character. Otherwise, check if we need
664 * to say this is not a 7-bit ASCII.
666 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
669 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
671 /* author could be non 7-bit ASCII but
672 * the log may be so; skip over the
675 if (ch
== '\n' && msg
[i
+1] == '\n')
678 else if (non_ascii(ch
)) {
685 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
686 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
687 strbuf_addch(sb
, '\n');
690 /* Skip excess blank lines at the beginning of body, if any... */
692 int linelen
= get_one_line(msg
);
696 if (!is_empty_line(msg
, &ll
))
701 /* These formats treat the title line specially. */
702 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
703 pp_title_line(fmt
, &msg
, sb
, subject
,
704 after_subject
, encoding
, plain_non_ascii
);
706 beginning_of_body
= sb
->len
;
707 if (fmt
!= CMIT_FMT_ONELINE
)
708 pp_remainder(fmt
, &msg
, sb
, indent
);
711 /* Make sure there is an EOLN for the non-oneline case */
712 if (fmt
!= CMIT_FMT_ONELINE
)
713 strbuf_addch(sb
, '\n');
716 * The caller may append additional body text in e-mail
717 * format. Make sure we did not strip the blank line
718 * between the header and the body.
720 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
721 strbuf_addch(sb
, '\n');