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