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