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