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