]> git.ipfire.org Git - thirdparty/git.git/blame - pretty.c
Start preparing release notes for 1.5.6.3
[thirdparty/git.git] / pretty.c
CommitLineData
93fc05eb
JS
1#include "cache.h"
2#include "commit.h"
93fc05eb
JS
3#include "utf8.h"
4#include "diff.h"
5#include "revision.h"
6
93fc05eb
JS
7static char *user_format;
8
4da45bef 9void get_commit_format(const char *arg, struct rev_info *rev)
93fc05eb
JS
10{
11 int i;
4da45bef
JH
12 static struct cmt_fmt_map {
13 const char *n;
14 size_t cmp_len;
15 enum cmit_fmt v;
16 } cmt_fmts[] = {
17 { "raw", 1, CMIT_FMT_RAW },
18 { "medium", 1, CMIT_FMT_MEDIUM },
19 { "short", 1, CMIT_FMT_SHORT },
20 { "email", 1, CMIT_FMT_EMAIL },
21 { "full", 5, CMIT_FMT_FULL },
22 { "fuller", 5, CMIT_FMT_FULLER },
23 { "oneline", 1, CMIT_FMT_ONELINE },
24 };
93fc05eb 25
4da45bef
JH
26 rev->use_terminator = 0;
27 if (!arg || !*arg) {
28 rev->commit_format = CMIT_FMT_DEFAULT;
29 return;
30 }
4da45bef
JH
31 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
32 const char *cp = strchr(arg, ':') + 1;
8e0f7003 33 free(user_format);
4da45bef
JH
34 user_format = xstrdup(cp);
35 if (arg[0] == 't')
36 rev->use_terminator = 1;
37 rev->commit_format = CMIT_FMT_USERFORMAT;
38 return;
93fc05eb
JS
39 }
40 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
41 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
4da45bef
JH
42 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
43 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
44 rev->use_terminator = 1;
45 rev->commit_format = cmt_fmts[i].v;
46 return;
47 }
93fc05eb
JS
48 }
49
50 die("invalid --pretty format: %s", arg);
51}
52
53/*
54 * Generic support for pretty-printing the header
55 */
56static int get_one_line(const char *msg)
57{
58 int ret = 0;
59
60 for (;;) {
61 char c = *msg++;
62 if (!c)
63 break;
64 ret++;
65 if (c == '\n')
66 break;
67 }
68 return ret;
69}
70
71/* High bit set, or ISO-2022-INT */
72int non_ascii(int ch)
73{
74 ch = (ch & 0xff);
75 return ((ch & 0x80) || (ch == 0x1b));
76}
77
78static int is_rfc2047_special(char ch)
79{
80 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
81}
82
83static void add_rfc2047(struct strbuf *sb, const char *line, int len,
84 const char *encoding)
85{
86 int i, last;
87
88 for (i = 0; i < len; i++) {
89 int ch = line[i];
90 if (non_ascii(ch))
91 goto needquote;
92 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
93 goto needquote;
94 }
95 strbuf_add(sb, line, len);
96 return;
97
98needquote:
99 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
100 strbuf_addf(sb, "=?%s?q?", encoding);
101 for (i = last = 0; i < len; i++) {
102 unsigned ch = line[i] & 0xFF;
103 /*
104 * We encode ' ' using '=20' even though rfc2047
105 * allows using '_' for readability. Unfortunately,
106 * many programs do not understand this and just
107 * leave the underscore in place.
108 */
109 if (is_rfc2047_special(ch) || ch == ' ') {
110 strbuf_add(sb, line + last, i - last);
111 strbuf_addf(sb, "=%02X", ch);
112 last = i + 1;
113 }
114 }
115 strbuf_add(sb, line + last, len - last);
116 strbuf_addstr(sb, "?=");
117}
118
b02bd65f
DB
119void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
120 const char *line, enum date_mode dmode,
121 const char *encoding)
93fc05eb
JS
122{
123 char *date;
124 int namelen;
125 unsigned long time;
126 int tz;
127 const char *filler = " ";
128
129 if (fmt == CMIT_FMT_ONELINE)
130 return;
131 date = strchr(line, '>');
132 if (!date)
133 return;
134 namelen = ++date - line;
135 time = strtoul(date, &date, 10);
136 tz = strtol(date, NULL, 10);
137
138 if (fmt == CMIT_FMT_EMAIL) {
139 char *name_tail = strchr(line, '<');
140 int display_name_length;
141 if (!name_tail)
142 return;
143 while (line < name_tail && isspace(name_tail[-1]))
144 name_tail--;
145 display_name_length = name_tail - line;
146 filler = "";
147 strbuf_addstr(sb, "From: ");
148 add_rfc2047(sb, line, display_name_length, encoding);
149 strbuf_add(sb, name_tail, namelen - display_name_length);
150 strbuf_addch(sb, '\n');
151 } else {
152 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
153 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
154 filler, namelen, line);
155 }
156 switch (fmt) {
157 case CMIT_FMT_MEDIUM:
158 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
159 break;
160 case CMIT_FMT_EMAIL:
161 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
162 break;
163 case CMIT_FMT_FULLER:
164 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
165 break;
166 default:
167 /* notin' */
168 break;
169 }
170}
171
172static int is_empty_line(const char *line, int *len_p)
173{
174 int len = *len_p;
175 while (len && isspace(line[len-1]))
176 len--;
177 *len_p = len;
178 return !len;
179}
180
181static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
182 const struct commit *commit, int abbrev)
183{
184 struct commit_list *parent = commit->parents;
185
186 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
187 !parent || !parent->next)
188 return;
189
190 strbuf_addstr(sb, "Merge:");
191
192 while (parent) {
193 struct commit *p = parent->item;
194 const char *hex = NULL;
195 const char *dots;
196 if (abbrev)
197 hex = find_unique_abbrev(p->object.sha1, abbrev);
198 if (!hex)
199 hex = sha1_to_hex(p->object.sha1);
200 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
201 parent = parent->next;
202
203 strbuf_addf(sb, " %s%s", hex, dots);
204 }
205 strbuf_addch(sb, '\n');
206}
207
208static char *get_header(const struct commit *commit, const char *key)
209{
210 int key_len = strlen(key);
211 const char *line = commit->buffer;
212
213 for (;;) {
214 const char *eol = strchr(line, '\n'), *next;
215
216 if (line == eol)
217 return NULL;
218 if (!eol) {
219 eol = line + strlen(line);
220 next = NULL;
221 } else
222 next = eol + 1;
223 if (eol - line > key_len &&
224 !strncmp(line, key, key_len) &&
225 line[key_len] == ' ') {
226 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
227 }
228 line = next;
229 }
230}
231
232static char *replace_encoding_header(char *buf, const char *encoding)
233{
234 struct strbuf tmp;
235 size_t start, len;
236 char *cp = buf;
237
238 /* guess if there is an encoding header before a \n\n */
239 while (strncmp(cp, "encoding ", strlen("encoding "))) {
240 cp = strchr(cp, '\n');
241 if (!cp || *++cp == '\n')
242 return buf;
243 }
244 start = cp - buf;
245 cp = strchr(cp, '\n');
246 if (!cp)
247 return buf; /* should not happen but be defensive */
248 len = cp + 1 - (buf + start);
249
250 strbuf_init(&tmp, 0);
251 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
252 if (is_encoding_utf8(encoding)) {
253 /* we have re-coded to UTF-8; drop the header */
254 strbuf_remove(&tmp, start, len);
255 } else {
256 /* just replaces XXXX in 'encoding XXXX\n' */
257 strbuf_splice(&tmp, start + strlen("encoding "),
258 len - strlen("encoding \n"),
259 encoding, strlen(encoding));
260 }
261 return strbuf_detach(&tmp, NULL);
262}
263
264static char *logmsg_reencode(const struct commit *commit,
265 const char *output_encoding)
266{
267 static const char *utf8 = "utf-8";
268 const char *use_encoding;
269 char *encoding;
270 char *out;
271
272 if (!*output_encoding)
273 return NULL;
274 encoding = get_header(commit, "encoding");
275 use_encoding = encoding ? encoding : utf8;
276 if (!strcmp(use_encoding, output_encoding))
277 if (encoding) /* we'll strip encoding header later */
278 out = xstrdup(commit->buffer);
279 else
280 return NULL; /* nothing to do */
281 else
282 out = reencode_string(commit->buffer,
283 output_encoding, use_encoding);
284 if (out)
285 out = replace_encoding_header(out, output_encoding);
286
287 free(encoding);
288 return out;
289}
290
c3a670de 291static size_t format_person_part(struct strbuf *sb, char part,
cde75e59 292 const char *msg, int len)
93fc05eb 293{
c3a670de
MC
294 /* currently all placeholders have same length */
295 const int placeholder_len = 2;
93fc05eb 296 int start, end, tz = 0;
c3a670de 297 unsigned long date = 0;
93fc05eb
JS
298 char *ep;
299
c3a670de 300 /* advance 'end' to point to email start delimiter */
93fc05eb
JS
301 for (end = 0; end < len && msg[end] != '<'; end++)
302 ; /* do nothing */
c3a670de 303
f7ab5c79 304 /*
c3a670de
MC
305 * When end points at the '<' that we found, it should have
306 * matching '>' later, which means 'end' must be strictly
307 * below len - 1.
f7ab5c79 308 */
c3a670de
MC
309 if (end >= len - 2)
310 goto skip;
311
cde75e59 312 if (part == 'n') { /* name */
c3a670de
MC
313 while (end > 0 && isspace(msg[end - 1]))
314 end--;
cde75e59 315 strbuf_add(sb, msg, end);
c3a670de 316 return placeholder_len;
cde75e59 317 }
c3a670de 318 start = ++end; /* save email start position */
93fc05eb 319
c3a670de
MC
320 /* advance 'end' to point to email end delimiter */
321 for ( ; end < len && msg[end] != '>'; end++)
93fc05eb
JS
322 ; /* do nothing */
323
324 if (end >= len)
c3a670de 325 goto skip;
93fc05eb 326
cde75e59
RS
327 if (part == 'e') { /* email */
328 strbuf_add(sb, msg + start, end - start);
c3a670de 329 return placeholder_len;
cde75e59 330 }
93fc05eb 331
c3a670de 332 /* advance 'start' to point to date start delimiter */
93fc05eb
JS
333 for (start = end + 1; start < len && isspace(msg[start]); start++)
334 ; /* do nothing */
335 if (start >= len)
c3a670de 336 goto skip;
93fc05eb
JS
337 date = strtoul(msg + start, &ep, 10);
338 if (msg + start == ep)
c3a670de 339 goto skip;
93fc05eb 340
cde75e59
RS
341 if (part == 't') { /* date, UNIX timestamp */
342 strbuf_add(sb, msg + start, ep - (msg + start));
c3a670de 343 return placeholder_len;
cde75e59 344 }
93fc05eb
JS
345
346 /* parse tz */
347 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
348 ; /* do nothing */
349 if (start + 1 < len) {
350 tz = strtoul(msg + start + 1, NULL, 10);
351 if (msg[start] == '-')
352 tz = -tz;
353 }
354
cde75e59
RS
355 switch (part) {
356 case 'd': /* date */
357 strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
c3a670de 358 return placeholder_len;
cde75e59
RS
359 case 'D': /* date, RFC2822 style */
360 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
c3a670de 361 return placeholder_len;
cde75e59
RS
362 case 'r': /* date, relative */
363 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
c3a670de 364 return placeholder_len;
cde75e59
RS
365 case 'i': /* date, ISO 8601 */
366 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
c3a670de 367 return placeholder_len;
cde75e59 368 }
c3a670de
MC
369
370skip:
371 /*
372 * bogus commit, 'sb' cannot be updated, but we still need to
373 * compute a valid return value.
374 */
375 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
376 || part == 'D' || part == 'r' || part == 'i')
377 return placeholder_len;
378
379 return 0; /* unknown placeholder */
93fc05eb
JS
380}
381
f29d5958
RS
382struct chunk {
383 size_t off;
384 size_t len;
385};
386
387struct format_commit_context {
388 const struct commit *commit;
389
390 /* These offsets are relative to the start of the commit message. */
391 int commit_header_parsed;
392 struct chunk subject;
393 struct chunk author;
394 struct chunk committer;
395 struct chunk encoding;
396 size_t body_off;
b9c62321
RS
397
398 /* The following ones are relative to the result struct strbuf. */
399 struct chunk abbrev_commit_hash;
400 struct chunk abbrev_tree_hash;
401 struct chunk abbrev_parent_hashes;
f29d5958
RS
402};
403
b9c62321
RS
404static int add_again(struct strbuf *sb, struct chunk *chunk)
405{
406 if (chunk->len) {
407 strbuf_adddup(sb, chunk->off, chunk->len);
408 return 1;
409 }
410
411 /*
412 * We haven't seen this chunk before. Our caller is surely
413 * going to add it the hard way now. Remember the most likely
414 * start of the to-be-added chunk: the current end of the
415 * struct strbuf.
416 */
417 chunk->off = sb->len;
418 return 0;
419}
420
f29d5958 421static void parse_commit_header(struct format_commit_context *context)
93fc05eb 422{
f29d5958 423 const char *msg = context->commit->buffer;
93fc05eb
JS
424 int i;
425 enum { HEADER, SUBJECT, BODY } state;
f29d5958
RS
426
427 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
428 int eol;
429 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
430 ; /* do nothing */
431
432 if (state == SUBJECT) {
433 context->subject.off = i;
434 context->subject.len = eol - i;
435 i = eol;
436 }
437 if (i == eol) {
438 state++;
439 /* strip empty lines */
7ed0988a 440 while (msg[eol] == '\n' && msg[eol + 1] == '\n')
f29d5958
RS
441 eol++;
442 } else if (!prefixcmp(msg + i, "author ")) {
443 context->author.off = i + 7;
444 context->author.len = eol - i - 7;
445 } else if (!prefixcmp(msg + i, "committer ")) {
446 context->committer.off = i + 10;
447 context->committer.len = eol - i - 10;
448 } else if (!prefixcmp(msg + i, "encoding ")) {
449 context->encoding.off = i + 9;
450 context->encoding.len = eol - i - 9;
451 }
452 i = eol;
7ed0988a
RS
453 if (!msg[i])
454 break;
f29d5958
RS
455 }
456 context->body_off = i;
457 context->commit_header_parsed = 1;
458}
459
c3a670de 460static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
f29d5958
RS
461 void *context)
462{
463 struct format_commit_context *c = context;
464 const struct commit *commit = c->commit;
93fc05eb 465 const char *msg = commit->buffer;
f29d5958 466 struct commit_list *p;
42c8c74c 467 int h1, h2;
93fc05eb 468
93fc05eb 469 /* these are independent of the commit */
cde75e59
RS
470 switch (placeholder[0]) {
471 case 'C':
c3a670de 472 if (!prefixcmp(placeholder + 1, "red")) {
cde75e59 473 strbuf_addstr(sb, "\033[31m");
c3a670de
MC
474 return 4;
475 } else if (!prefixcmp(placeholder + 1, "green")) {
cde75e59 476 strbuf_addstr(sb, "\033[32m");
c3a670de
MC
477 return 6;
478 } else if (!prefixcmp(placeholder + 1, "blue")) {
cde75e59 479 strbuf_addstr(sb, "\033[34m");
c3a670de
MC
480 return 5;
481 } else if (!prefixcmp(placeholder + 1, "reset")) {
cde75e59 482 strbuf_addstr(sb, "\033[m");
c3a670de
MC
483 return 6;
484 } else
485 return 0;
cde75e59
RS
486 case 'n': /* newline */
487 strbuf_addch(sb, '\n');
c3a670de 488 return 1;
42c8c74c
GS
489 case 'x':
490 /* %x00 == NUL, %x0a == LF, etc. */
491 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
492 h1 <= 16 &&
493 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
494 h2 <= 16) {
495 strbuf_addch(sb, (h1<<4)|h2);
496 return 3;
497 } else
498 return 0;
cde75e59 499 }
93fc05eb
JS
500
501 /* these depend on the commit */
502 if (!commit->object.parsed)
503 parse_object(commit->object.sha1);
93fc05eb 504
cde75e59
RS
505 switch (placeholder[0]) {
506 case 'H': /* commit hash */
507 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
c3a670de 508 return 1;
cde75e59 509 case 'h': /* abbreviated commit hash */
b9c62321 510 if (add_again(sb, &c->abbrev_commit_hash))
c3a670de 511 return 1;
cde75e59
RS
512 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
513 DEFAULT_ABBREV));
b9c62321 514 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
c3a670de 515 return 1;
cde75e59
RS
516 case 'T': /* tree hash */
517 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
c3a670de 518 return 1;
cde75e59 519 case 't': /* abbreviated tree hash */
b9c62321 520 if (add_again(sb, &c->abbrev_tree_hash))
c3a670de 521 return 1;
cde75e59
RS
522 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
523 DEFAULT_ABBREV));
b9c62321 524 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
c3a670de 525 return 1;
cde75e59
RS
526 case 'P': /* parent hashes */
527 for (p = commit->parents; p; p = p->next) {
528 if (p != commit->parents)
529 strbuf_addch(sb, ' ');
530 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
531 }
c3a670de 532 return 1;
cde75e59 533 case 'p': /* abbreviated parent hashes */
b9c62321 534 if (add_again(sb, &c->abbrev_parent_hashes))
c3a670de 535 return 1;
cde75e59
RS
536 for (p = commit->parents; p; p = p->next) {
537 if (p != commit->parents)
538 strbuf_addch(sb, ' ');
539 strbuf_addstr(sb, find_unique_abbrev(
540 p->item->object.sha1, DEFAULT_ABBREV));
541 }
b9c62321
RS
542 c->abbrev_parent_hashes.len = sb->len -
543 c->abbrev_parent_hashes.off;
c3a670de 544 return 1;
cde75e59
RS
545 case 'm': /* left/right/bottom */
546 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
547 ? '-'
548 : (commit->object.flags & SYMMETRIC_LEFT)
549 ? '<'
550 : '>');
c3a670de 551 return 1;
cde75e59
RS
552 }
553
554 /* For the rest we have to parse the commit header. */
f29d5958
RS
555 if (!c->commit_header_parsed)
556 parse_commit_header(c);
93fc05eb 557
f29d5958 558 switch (placeholder[0]) {
c3a670de 559 case 's': /* subject */
f29d5958 560 strbuf_add(sb, msg + c->subject.off, c->subject.len);
c3a670de
MC
561 return 1;
562 case 'a': /* author ... */
563 return format_person_part(sb, placeholder[1],
f29d5958 564 msg + c->author.off, c->author.len);
c3a670de
MC
565 case 'c': /* committer ... */
566 return format_person_part(sb, placeholder[1],
f29d5958 567 msg + c->committer.off, c->committer.len);
c3a670de 568 case 'e': /* encoding */
f29d5958 569 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
c3a670de
MC
570 return 1;
571 case 'b': /* body */
f29d5958 572 strbuf_addstr(sb, msg + c->body_off);
c3a670de 573 return 1;
93fc05eb 574 }
c3a670de 575 return 0; /* unknown placeholder */
cde75e59
RS
576}
577
578void format_commit_message(const struct commit *commit,
579 const void *format, struct strbuf *sb)
580{
f29d5958
RS
581 struct format_commit_context context;
582
583 memset(&context, 0, sizeof(context));
584 context.commit = commit;
c3a670de 585 strbuf_expand(sb, format, format_commit_item, &context);
93fc05eb
JS
586}
587
588static void pp_header(enum cmit_fmt fmt,
589 int abbrev,
590 enum date_mode dmode,
591 const char *encoding,
592 const struct commit *commit,
593 const char **msg_p,
594 struct strbuf *sb)
595{
596 int parents_shown = 0;
597
598 for (;;) {
599 const char *line = *msg_p;
600 int linelen = get_one_line(*msg_p);
601
602 if (!linelen)
603 return;
604 *msg_p += linelen;
605
606 if (linelen == 1)
607 /* End of header */
608 return;
609
610 if (fmt == CMIT_FMT_RAW) {
611 strbuf_add(sb, line, linelen);
612 continue;
613 }
614
615 if (!memcmp(line, "parent ", 7)) {
616 if (linelen != 48)
617 die("bad parent line in commit");
618 continue;
619 }
620
621 if (!parents_shown) {
622 struct commit_list *parent;
623 int num;
624 for (parent = commit->parents, num = 0;
625 parent;
626 parent = parent->next, num++)
627 ;
628 /* with enough slop */
629 strbuf_grow(sb, num * 50 + 20);
630 add_merge_info(fmt, sb, commit, abbrev);
631 parents_shown = 1;
632 }
633
634 /*
635 * MEDIUM == DEFAULT shows only author with dates.
636 * FULL shows both authors but not dates.
637 * FULLER shows both authors and dates.
638 */
639 if (!memcmp(line, "author ", 7)) {
640 strbuf_grow(sb, linelen + 80);
b02bd65f 641 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
93fc05eb
JS
642 }
643 if (!memcmp(line, "committer ", 10) &&
644 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
645 strbuf_grow(sb, linelen + 80);
b02bd65f 646 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
93fc05eb
JS
647 }
648 }
649}
650
b02bd65f
DB
651void pp_title_line(enum cmit_fmt fmt,
652 const char **msg_p,
653 struct strbuf *sb,
654 const char *subject,
655 const char *after_subject,
656 const char *encoding,
267123b4 657 int need_8bit_cte)
93fc05eb
JS
658{
659 struct strbuf title;
660
661 strbuf_init(&title, 80);
662
663 for (;;) {
664 const char *line = *msg_p;
665 int linelen = get_one_line(line);
666
667 *msg_p += linelen;
668 if (!linelen || is_empty_line(line, &linelen))
669 break;
670
671 strbuf_grow(&title, linelen + 2);
672 if (title.len) {
673 if (fmt == CMIT_FMT_EMAIL) {
674 strbuf_addch(&title, '\n');
675 }
676 strbuf_addch(&title, ' ');
677 }
678 strbuf_add(&title, line, linelen);
679 }
680
681 strbuf_grow(sb, title.len + 1024);
682 if (subject) {
683 strbuf_addstr(sb, subject);
684 add_rfc2047(sb, title.buf, title.len, encoding);
685 } else {
686 strbuf_addbuf(sb, &title);
687 }
688 strbuf_addch(sb, '\n');
689
6bf4f1b4 690 if (need_8bit_cte > 0) {
93fc05eb
JS
691 const char *header_fmt =
692 "MIME-Version: 1.0\n"
693 "Content-Type: text/plain; charset=%s\n"
694 "Content-Transfer-Encoding: 8bit\n";
695 strbuf_addf(sb, header_fmt, encoding);
696 }
697 if (after_subject) {
698 strbuf_addstr(sb, after_subject);
699 }
700 if (fmt == CMIT_FMT_EMAIL) {
701 strbuf_addch(sb, '\n');
702 }
703 strbuf_release(&title);
704}
705
b02bd65f
DB
706void pp_remainder(enum cmit_fmt fmt,
707 const char **msg_p,
708 struct strbuf *sb,
709 int indent)
93fc05eb
JS
710{
711 int first = 1;
712 for (;;) {
713 const char *line = *msg_p;
714 int linelen = get_one_line(line);
715 *msg_p += linelen;
716
717 if (!linelen)
718 break;
719
720 if (is_empty_line(line, &linelen)) {
721 if (first)
722 continue;
723 if (fmt == CMIT_FMT_SHORT)
724 break;
725 }
726 first = 0;
727
728 strbuf_grow(sb, linelen + indent + 20);
729 if (indent) {
730 memset(sb->buf + sb->len, ' ', indent);
731 strbuf_setlen(sb, sb->len + indent);
732 }
733 strbuf_add(sb, line, linelen);
734 strbuf_addch(sb, '\n');
735 }
736}
737
738void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
6bf4f1b4
JH
739 struct strbuf *sb, int abbrev,
740 const char *subject, const char *after_subject,
741 enum date_mode dmode, int need_8bit_cte)
93fc05eb
JS
742{
743 unsigned long beginning_of_body;
744 int indent = 4;
745 const char *msg = commit->buffer;
746 char *reencoded;
747 const char *encoding;
748
749 if (fmt == CMIT_FMT_USERFORMAT) {
750 format_commit_message(commit, user_format, sb);
751 return;
752 }
753
754 encoding = (git_log_output_encoding
755 ? git_log_output_encoding
756 : git_commit_encoding);
757 if (!encoding)
758 encoding = "utf-8";
759 reencoded = logmsg_reencode(commit, encoding);
760 if (reencoded) {
761 msg = reencoded;
762 }
763
764 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
765 indent = 0;
766
6bf4f1b4
JH
767 /*
768 * We need to check and emit Content-type: to mark it
769 * as 8-bit if we haven't done so.
93fc05eb 770 */
6bf4f1b4 771 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
93fc05eb
JS
772 int i, ch, in_body;
773
774 for (in_body = i = 0; (ch = msg[i]); i++) {
775 if (!in_body) {
776 /* author could be non 7-bit ASCII but
777 * the log may be so; skip over the
778 * header part first.
779 */
780 if (ch == '\n' && msg[i+1] == '\n')
781 in_body = 1;
782 }
783 else if (non_ascii(ch)) {
6bf4f1b4 784 need_8bit_cte = 1;
93fc05eb
JS
785 break;
786 }
787 }
788 }
789
790 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
791 if (fmt != CMIT_FMT_ONELINE && !subject) {
792 strbuf_addch(sb, '\n');
793 }
794
795 /* Skip excess blank lines at the beginning of body, if any... */
796 for (;;) {
797 int linelen = get_one_line(msg);
798 int ll = linelen;
799 if (!linelen)
800 break;
801 if (!is_empty_line(msg, &ll))
802 break;
803 msg += linelen;
804 }
805
806 /* These formats treat the title line specially. */
807 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
808 pp_title_line(fmt, &msg, sb, subject,
6bf4f1b4 809 after_subject, encoding, need_8bit_cte);
93fc05eb
JS
810
811 beginning_of_body = sb->len;
812 if (fmt != CMIT_FMT_ONELINE)
813 pp_remainder(fmt, &msg, sb, indent);
814 strbuf_rtrim(sb);
815
816 /* Make sure there is an EOLN for the non-oneline case */
817 if (fmt != CMIT_FMT_ONELINE)
818 strbuf_addch(sb, '\n');
819
820 /*
821 * The caller may append additional body text in e-mail
822 * format. Make sure we did not strip the blank line
823 * between the header and the body.
824 */
825 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
826 strbuf_addch(sb, '\n');
827 free(reencoded);
828}