]>
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" |
a97a7468 | 9 | #include "notes.h" |
c002922a | 10 | #include "color.h" |
8f8f5476 | 11 | #include "reflog-walk.h" |
f6667c5e | 12 | #include "gpg-interface.h" |
93fc05eb | 13 | |
93fc05eb | 14 | static char *user_format; |
40957891 WP |
15 | static struct cmt_fmt_map { |
16 | const char *name; | |
17 | enum cmit_fmt format; | |
18 | int is_tformat; | |
2d7671ef WP |
19 | int is_alias; |
20 | const char *user_format; | |
40957891 | 21 | } *commit_formats; |
8028184e | 22 | static size_t builtin_formats_len; |
40957891 | 23 | static size_t commit_formats_len; |
8028184e | 24 | static size_t commit_formats_alloc; |
40957891 | 25 | static struct cmt_fmt_map *find_commit_format(const char *sought); |
93fc05eb | 26 | |
36407548 NS |
27 | static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat) |
28 | { | |
29 | free(user_format); | |
30 | user_format = xstrdup(cp); | |
31 | if (is_tformat) | |
32 | rev->use_terminator = 1; | |
33 | rev->commit_format = CMIT_FMT_USERFORMAT; | |
34 | } | |
35 | ||
8028184e | 36 | static int git_pretty_formats_config(const char *var, const char *value, void *cb) |
93fc05eb | 37 | { |
8028184e WP |
38 | struct cmt_fmt_map *commit_format = NULL; |
39 | const char *name; | |
40 | const char *fmt; | |
93fc05eb | 41 | int i; |
8028184e WP |
42 | |
43 | if (prefixcmp(var, "pretty.")) | |
44 | return 0; | |
45 | ||
46 | name = var + strlen("pretty."); | |
47 | for (i = 0; i < builtin_formats_len; i++) { | |
48 | if (!strcmp(commit_formats[i].name, name)) | |
49 | return 0; | |
50 | } | |
51 | ||
52 | for (i = builtin_formats_len; i < commit_formats_len; i++) { | |
53 | if (!strcmp(commit_formats[i].name, name)) { | |
54 | commit_format = &commit_formats[i]; | |
55 | break; | |
56 | } | |
57 | } | |
58 | ||
59 | if (!commit_format) { | |
60 | ALLOC_GROW(commit_formats, commit_formats_len+1, | |
61 | commit_formats_alloc); | |
62 | commit_format = &commit_formats[commit_formats_len]; | |
95a2618f | 63 | memset(commit_format, 0, sizeof(*commit_format)); |
8028184e WP |
64 | commit_formats_len++; |
65 | } | |
66 | ||
67 | commit_format->name = xstrdup(name); | |
68 | commit_format->format = CMIT_FMT_USERFORMAT; | |
69 | git_config_string(&fmt, var, value); | |
70 | if (!prefixcmp(fmt, "format:") || !prefixcmp(fmt, "tformat:")) { | |
71 | commit_format->is_tformat = fmt[0] == 't'; | |
72 | fmt = strchr(fmt, ':') + 1; | |
73 | } else if (strchr(fmt, '%')) | |
74 | commit_format->is_tformat = 1; | |
75 | else | |
76 | commit_format->is_alias = 1; | |
77 | commit_format->user_format = fmt; | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
40957891 | 82 | static void setup_commit_formats(void) |
93fc05eb | 83 | { |
40957891 WP |
84 | struct cmt_fmt_map builtin_formats[] = { |
85 | { "raw", CMIT_FMT_RAW, 0 }, | |
86 | { "medium", CMIT_FMT_MEDIUM, 0 }, | |
87 | { "short", CMIT_FMT_SHORT, 0 }, | |
88 | { "email", CMIT_FMT_EMAIL, 0 }, | |
89 | { "fuller", CMIT_FMT_FULLER, 0 }, | |
90 | { "full", CMIT_FMT_FULL, 0 }, | |
91 | { "oneline", CMIT_FMT_ONELINE, 1 } | |
4da45bef | 92 | }; |
40957891 | 93 | commit_formats_len = ARRAY_SIZE(builtin_formats); |
8028184e WP |
94 | builtin_formats_len = commit_formats_len; |
95 | ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc); | |
40957891 WP |
96 | memcpy(commit_formats, builtin_formats, |
97 | sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats)); | |
8028184e WP |
98 | |
99 | git_config(git_pretty_formats_config, NULL); | |
40957891 WP |
100 | } |
101 | ||
2d7671ef WP |
102 | static struct cmt_fmt_map *find_commit_format_recursive(const char *sought, |
103 | const char *original, | |
104 | int num_redirections) | |
40957891 WP |
105 | { |
106 | struct cmt_fmt_map *found = NULL; | |
107 | size_t found_match_len = 0; | |
108 | int i; | |
109 | ||
2d7671ef WP |
110 | if (num_redirections >= commit_formats_len) |
111 | die("invalid --pretty format: " | |
112 | "'%s' references an alias which points to itself", | |
113 | original); | |
40957891 WP |
114 | |
115 | for (i = 0; i < commit_formats_len; i++) { | |
116 | size_t match_len; | |
117 | ||
118 | if (prefixcmp(commit_formats[i].name, sought)) | |
119 | continue; | |
120 | ||
121 | match_len = strlen(commit_formats[i].name); | |
122 | if (found == NULL || found_match_len > match_len) { | |
123 | found = &commit_formats[i]; | |
124 | found_match_len = match_len; | |
125 | } | |
126 | } | |
2d7671ef WP |
127 | |
128 | if (found && found->is_alias) { | |
129 | found = find_commit_format_recursive(found->user_format, | |
130 | original, | |
131 | num_redirections+1); | |
132 | } | |
133 | ||
40957891 WP |
134 | return found; |
135 | } | |
136 | ||
2d7671ef WP |
137 | static struct cmt_fmt_map *find_commit_format(const char *sought) |
138 | { | |
139 | if (!commit_formats) | |
140 | setup_commit_formats(); | |
141 | ||
142 | return find_commit_format_recursive(sought, sought, 0); | |
143 | } | |
144 | ||
40957891 WP |
145 | void get_commit_format(const char *arg, struct rev_info *rev) |
146 | { | |
147 | struct cmt_fmt_map *commit_format; | |
93fc05eb | 148 | |
4da45bef JH |
149 | rev->use_terminator = 0; |
150 | if (!arg || !*arg) { | |
151 | rev->commit_format = CMIT_FMT_DEFAULT; | |
152 | return; | |
153 | } | |
4da45bef | 154 | if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) { |
36407548 | 155 | save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't'); |
4da45bef | 156 | return; |
93fc05eb | 157 | } |
40957891 | 158 | |
36407548 NS |
159 | if (strchr(arg, '%')) { |
160 | save_user_format(rev, arg, 1); | |
161 | return; | |
162 | } | |
93fc05eb | 163 | |
40957891 WP |
164 | commit_format = find_commit_format(arg); |
165 | if (!commit_format) | |
166 | die("invalid --pretty format: %s", arg); | |
167 | ||
168 | rev->commit_format = commit_format->format; | |
169 | rev->use_terminator = commit_format->is_tformat; | |
8028184e WP |
170 | if (commit_format->format == CMIT_FMT_USERFORMAT) { |
171 | save_user_format(rev, commit_format->user_format, | |
172 | commit_format->is_tformat); | |
173 | } | |
93fc05eb JS |
174 | } |
175 | ||
176 | /* | |
177 | * Generic support for pretty-printing the header | |
178 | */ | |
179 | static int get_one_line(const char *msg) | |
180 | { | |
181 | int ret = 0; | |
182 | ||
183 | for (;;) { | |
184 | char c = *msg++; | |
185 | if (!c) | |
186 | break; | |
187 | ret++; | |
188 | if (c == '\n') | |
189 | break; | |
190 | } | |
191 | return ret; | |
192 | } | |
193 | ||
194 | /* High bit set, or ISO-2022-INT */ | |
cc571142 | 195 | static int non_ascii(int ch) |
93fc05eb | 196 | { |
c2e9364a | 197 | return !isascii(ch) || ch == '\033'; |
93fc05eb JS |
198 | } |
199 | ||
28e9cf65 JS |
200 | int has_non_ascii(const char *s) |
201 | { | |
202 | int ch; | |
203 | if (!s) | |
204 | return 0; | |
205 | while ((ch = *s++) != '\0') { | |
206 | if (non_ascii(ch)) | |
207 | return 1; | |
208 | } | |
209 | return 0; | |
210 | } | |
211 | ||
4d03c18a JK |
212 | static int is_rfc822_special(char ch) |
213 | { | |
214 | switch (ch) { | |
215 | case '(': | |
216 | case ')': | |
217 | case '<': | |
218 | case '>': | |
219 | case '[': | |
220 | case ']': | |
221 | case ':': | |
222 | case ';': | |
223 | case '@': | |
224 | case ',': | |
225 | case '.': | |
226 | case '"': | |
227 | case '\\': | |
228 | return 1; | |
229 | default: | |
230 | return 0; | |
231 | } | |
232 | } | |
233 | ||
234 | static int has_rfc822_specials(const char *s, int len) | |
235 | { | |
236 | int i; | |
237 | for (i = 0; i < len; i++) | |
238 | if (is_rfc822_special(s[i])) | |
239 | return 1; | |
240 | return 0; | |
241 | } | |
242 | ||
f9b7204b JS |
243 | static int last_line_length(struct strbuf *sb) |
244 | { | |
245 | int i; | |
246 | ||
247 | /* How many bytes are already used on the last line? */ | |
248 | for (i = sb->len - 1; i >= 0; i--) | |
249 | if (sb->buf[i] == '\n') | |
250 | break; | |
251 | return sb->len - (i + 1); | |
252 | } | |
253 | ||
4d03c18a JK |
254 | static void add_rfc822_quoted(struct strbuf *out, const char *s, int len) |
255 | { | |
256 | int i; | |
257 | ||
258 | /* just a guess, we may have to also backslash-quote */ | |
259 | strbuf_grow(out, len + 2); | |
260 | ||
261 | strbuf_addch(out, '"'); | |
262 | for (i = 0; i < len; i++) { | |
263 | switch (s[i]) { | |
264 | case '"': | |
265 | case '\\': | |
266 | strbuf_addch(out, '\\'); | |
267 | /* fall through */ | |
268 | default: | |
269 | strbuf_addch(out, s[i]); | |
270 | } | |
271 | } | |
272 | strbuf_addch(out, '"'); | |
273 | } | |
274 | ||
93fc05eb JS |
275 | static int is_rfc2047_special(char ch) |
276 | { | |
94f6cdf6 JS |
277 | if (ch == ' ' || ch == '\n') |
278 | return 1; | |
279 | ||
93fc05eb JS |
280 | return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_')); |
281 | } | |
282 | ||
283 | static void add_rfc2047(struct strbuf *sb, const char *line, int len, | |
284 | const char *encoding) | |
285 | { | |
a1f6baa5 | 286 | static const int max_length = 78; /* per rfc2822 */ |
94f6cdf6 | 287 | static const int max_encoded_length = 76; /* per rfc2047 */ |
a1f6baa5 | 288 | int i; |
f9b7204b | 289 | int line_len = last_line_length(sb); |
93fc05eb JS |
290 | |
291 | for (i = 0; i < len; i++) { | |
292 | int ch = line[i]; | |
c22e7de3 | 293 | if (non_ascii(ch) || ch == '\n') |
93fc05eb JS |
294 | goto needquote; |
295 | if ((i + 1 < len) && (ch == '=' && line[i+1] == '?')) | |
296 | goto needquote; | |
297 | } | |
7a76e68a | 298 | strbuf_add_wrapped_bytes(sb, line, len, -line_len, 1, max_length); |
93fc05eb JS |
299 | return; |
300 | ||
301 | needquote: | |
302 | strbuf_grow(sb, len * 3 + strlen(encoding) + 100); | |
303 | strbuf_addf(sb, "=?%s?q?", encoding); | |
a1f6baa5 JK |
304 | line_len += strlen(encoding) + 5; /* 5 for =??q? */ |
305 | for (i = 0; i < len; i++) { | |
93fc05eb | 306 | unsigned ch = line[i] & 0xFF; |
94f6cdf6 JS |
307 | int is_special = is_rfc2047_special(ch); |
308 | ||
309 | /* | |
310 | * According to RFC 2047, we could encode the special character | |
311 | * ' ' (space) with '_' (underscore) for readability. But many | |
312 | * programs do not understand this and just leave the | |
313 | * underscore in place. Thus, we do nothing special here, which | |
314 | * causes ' ' to be encoded as '=20', avoiding this problem. | |
315 | */ | |
a1f6baa5 | 316 | |
94f6cdf6 | 317 | if (line_len + 2 + (is_special ? 3 : 1) > max_encoded_length) { |
a1f6baa5 JK |
318 | strbuf_addf(sb, "?=\n =?%s?q?", encoding); |
319 | line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */ | |
320 | } | |
321 | ||
94f6cdf6 | 322 | if (is_special) { |
93fc05eb | 323 | strbuf_addf(sb, "=%02X", ch); |
a1f6baa5 | 324 | line_len += 3; |
94f6cdf6 | 325 | } else { |
a1f6baa5 JK |
326 | strbuf_addch(sb, ch); |
327 | line_len++; | |
93fc05eb JS |
328 | } |
329 | } | |
93fc05eb JS |
330 | strbuf_addstr(sb, "?="); |
331 | } | |
332 | ||
6bf13944 JK |
333 | void pp_user_info(const struct pretty_print_context *pp, |
334 | const char *what, struct strbuf *sb, | |
335 | const char *line, const char *encoding) | |
93fc05eb JS |
336 | { |
337 | char *date; | |
338 | int namelen; | |
339 | unsigned long time; | |
340 | int tz; | |
93fc05eb | 341 | |
6bf13944 | 342 | if (pp->fmt == CMIT_FMT_ONELINE) |
93fc05eb JS |
343 | return; |
344 | date = strchr(line, '>'); | |
345 | if (!date) | |
346 | return; | |
347 | namelen = ++date - line; | |
348 | time = strtoul(date, &date, 10); | |
349 | tz = strtol(date, NULL, 10); | |
350 | ||
6bf13944 | 351 | if (pp->fmt == CMIT_FMT_EMAIL) { |
93fc05eb JS |
352 | char *name_tail = strchr(line, '<'); |
353 | int display_name_length; | |
354 | if (!name_tail) | |
355 | return; | |
356 | while (line < name_tail && isspace(name_tail[-1])) | |
357 | name_tail--; | |
358 | display_name_length = name_tail - line; | |
93fc05eb | 359 | strbuf_addstr(sb, "From: "); |
4d03c18a JK |
360 | if (!has_rfc822_specials(line, display_name_length)) { |
361 | add_rfc2047(sb, line, display_name_length, encoding); | |
362 | } else { | |
363 | struct strbuf quoted = STRBUF_INIT; | |
364 | add_rfc822_quoted("ed, line, display_name_length); | |
365 | add_rfc2047(sb, quoted.buf, quoted.len, encoding); | |
366 | strbuf_release("ed); | |
367 | } | |
f9b7204b | 368 | if (namelen - display_name_length + last_line_length(sb) > 78) { |
990f6e30 JK |
369 | strbuf_addch(sb, '\n'); |
370 | if (!isspace(name_tail[0])) | |
371 | strbuf_addch(sb, ' '); | |
372 | } | |
93fc05eb JS |
373 | strbuf_add(sb, name_tail, namelen - display_name_length); |
374 | strbuf_addch(sb, '\n'); | |
375 | } else { | |
376 | strbuf_addf(sb, "%s: %.*s%.*s\n", what, | |
6bf13944 | 377 | (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, |
8e76bf3f | 378 | " ", namelen, line); |
93fc05eb | 379 | } |
6bf13944 | 380 | switch (pp->fmt) { |
93fc05eb | 381 | case CMIT_FMT_MEDIUM: |
6bf13944 | 382 | strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode)); |
93fc05eb JS |
383 | break; |
384 | case CMIT_FMT_EMAIL: | |
385 | strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822)); | |
386 | break; | |
387 | case CMIT_FMT_FULLER: | |
6bf13944 | 388 | strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode)); |
93fc05eb JS |
389 | break; |
390 | default: | |
391 | /* notin' */ | |
392 | break; | |
393 | } | |
394 | } | |
395 | ||
396 | static int is_empty_line(const char *line, int *len_p) | |
397 | { | |
398 | int len = *len_p; | |
399 | while (len && isspace(line[len-1])) | |
400 | len--; | |
401 | *len_p = len; | |
402 | return !len; | |
403 | } | |
404 | ||
a0109668 RS |
405 | static const char *skip_empty_lines(const char *msg) |
406 | { | |
407 | for (;;) { | |
408 | int linelen = get_one_line(msg); | |
409 | int ll = linelen; | |
410 | if (!linelen) | |
411 | break; | |
412 | if (!is_empty_line(msg, &ll)) | |
413 | break; | |
414 | msg += linelen; | |
415 | } | |
416 | return msg; | |
417 | } | |
418 | ||
6bf13944 JK |
419 | static void add_merge_info(const struct pretty_print_context *pp, |
420 | struct strbuf *sb, const struct commit *commit) | |
93fc05eb JS |
421 | { |
422 | struct commit_list *parent = commit->parents; | |
423 | ||
6bf13944 | 424 | if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) || |
93fc05eb JS |
425 | !parent || !parent->next) |
426 | return; | |
427 | ||
428 | strbuf_addstr(sb, "Merge:"); | |
429 | ||
430 | while (parent) { | |
431 | struct commit *p = parent->item; | |
432 | const char *hex = NULL; | |
6bf13944 JK |
433 | if (pp->abbrev) |
434 | hex = find_unique_abbrev(p->object.sha1, pp->abbrev); | |
93fc05eb JS |
435 | if (!hex) |
436 | hex = sha1_to_hex(p->object.sha1); | |
93fc05eb JS |
437 | parent = parent->next; |
438 | ||
7fcda920 | 439 | strbuf_addf(sb, " %s", hex); |
93fc05eb JS |
440 | } |
441 | strbuf_addch(sb, '\n'); | |
442 | } | |
443 | ||
444 | static char *get_header(const struct commit *commit, const char *key) | |
445 | { | |
446 | int key_len = strlen(key); | |
447 | const char *line = commit->buffer; | |
448 | ||
a9c7a8a8 | 449 | while (line) { |
93fc05eb JS |
450 | const char *eol = strchr(line, '\n'), *next; |
451 | ||
452 | if (line == eol) | |
453 | return NULL; | |
454 | if (!eol) { | |
a9c7a8a8 JK |
455 | warning("malformed commit (header is missing newline): %s", |
456 | sha1_to_hex(commit->object.sha1)); | |
93fc05eb JS |
457 | eol = line + strlen(line); |
458 | next = NULL; | |
459 | } else | |
460 | next = eol + 1; | |
461 | if (eol - line > key_len && | |
462 | !strncmp(line, key, key_len) && | |
463 | line[key_len] == ' ') { | |
464 | return xmemdupz(line + key_len + 1, eol - line - key_len - 1); | |
465 | } | |
466 | line = next; | |
467 | } | |
a9c7a8a8 | 468 | return NULL; |
93fc05eb JS |
469 | } |
470 | ||
471 | static char *replace_encoding_header(char *buf, const char *encoding) | |
472 | { | |
f285a2d7 | 473 | struct strbuf tmp = STRBUF_INIT; |
93fc05eb JS |
474 | size_t start, len; |
475 | char *cp = buf; | |
476 | ||
477 | /* guess if there is an encoding header before a \n\n */ | |
478 | while (strncmp(cp, "encoding ", strlen("encoding "))) { | |
479 | cp = strchr(cp, '\n'); | |
480 | if (!cp || *++cp == '\n') | |
481 | return buf; | |
482 | } | |
483 | start = cp - buf; | |
484 | cp = strchr(cp, '\n'); | |
485 | if (!cp) | |
486 | return buf; /* should not happen but be defensive */ | |
487 | len = cp + 1 - (buf + start); | |
488 | ||
93fc05eb JS |
489 | strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); |
490 | if (is_encoding_utf8(encoding)) { | |
491 | /* we have re-coded to UTF-8; drop the header */ | |
492 | strbuf_remove(&tmp, start, len); | |
493 | } else { | |
494 | /* just replaces XXXX in 'encoding XXXX\n' */ | |
495 | strbuf_splice(&tmp, start + strlen("encoding "), | |
496 | len - strlen("encoding \n"), | |
497 | encoding, strlen(encoding)); | |
498 | } | |
499 | return strbuf_detach(&tmp, NULL); | |
500 | } | |
501 | ||
177b29dc PN |
502 | char *logmsg_reencode(const struct commit *commit, |
503 | const char *output_encoding) | |
93fc05eb | 504 | { |
330db18c | 505 | static const char *utf8 = "UTF-8"; |
93fc05eb JS |
506 | const char *use_encoding; |
507 | char *encoding; | |
508 | char *out; | |
509 | ||
510 | if (!*output_encoding) | |
511 | return NULL; | |
512 | encoding = get_header(commit, "encoding"); | |
513 | use_encoding = encoding ? encoding : utf8; | |
514 | if (!strcmp(use_encoding, output_encoding)) | |
515 | if (encoding) /* we'll strip encoding header later */ | |
516 | out = xstrdup(commit->buffer); | |
517 | else | |
518 | return NULL; /* nothing to do */ | |
519 | else | |
520 | out = reencode_string(commit->buffer, | |
521 | output_encoding, use_encoding); | |
522 | if (out) | |
523 | out = replace_encoding_header(out, output_encoding); | |
524 | ||
525 | free(encoding); | |
526 | return out; | |
527 | } | |
528 | ||
d20d654f | 529 | static int mailmap_name(char *email, int email_len, char *name, int name_len) |
e0cbc397 | 530 | { |
c455c87c | 531 | static struct string_list *mail_map; |
e0cbc397 JS |
532 | if (!mail_map) { |
533 | mail_map = xcalloc(1, sizeof(*mail_map)); | |
d551a488 | 534 | read_mailmap(mail_map, NULL); |
e0cbc397 | 535 | } |
d20d654f | 536 | return mail_map->nr && map_user(mail_map, email, email_len, name, name_len); |
e0cbc397 JS |
537 | } |
538 | ||
c3a670de | 539 | static size_t format_person_part(struct strbuf *sb, char part, |
d36f8679 | 540 | const char *msg, int len, enum date_mode dmode) |
93fc05eb | 541 | { |
c3a670de MC |
542 | /* currently all placeholders have same length */ |
543 | const int placeholder_len = 2; | |
4b340cfa | 544 | int tz; |
c3a670de | 545 | unsigned long date = 0; |
d20d654f MSO |
546 | char person_name[1024]; |
547 | char person_mail[1024]; | |
4b340cfa JH |
548 | struct ident_split s; |
549 | const char *name_start, *name_end, *mail_start, *mail_end; | |
93fc05eb | 550 | |
4b340cfa | 551 | if (split_ident_line(&s, msg, len) < 0) |
c3a670de MC |
552 | goto skip; |
553 | ||
4b340cfa JH |
554 | name_start = s.name_begin; |
555 | name_end = s.name_end; | |
556 | mail_start = s.mail_begin; | |
557 | mail_end = s.mail_end; | |
d20d654f MSO |
558 | |
559 | if (part == 'N' || part == 'E') { /* mailmap lookup */ | |
c9b4e9e5 JK |
560 | snprintf(person_name, sizeof(person_name), "%.*s", |
561 | (int)(name_end - name_start), name_start); | |
562 | snprintf(person_mail, sizeof(person_mail), "%.*s", | |
563 | (int)(mail_end - mail_start), mail_start); | |
d20d654f MSO |
564 | mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name)); |
565 | name_start = person_name; | |
566 | name_end = name_start + strlen(person_name); | |
567 | mail_start = person_mail; | |
568 | mail_end = mail_start + strlen(person_mail); | |
569 | } | |
e0cbc397 | 570 | if (part == 'n' || part == 'N') { /* name */ |
d20d654f | 571 | strbuf_add(sb, name_start, name_end-name_start); |
c3a670de | 572 | return placeholder_len; |
cde75e59 | 573 | } |
d20d654f MSO |
574 | if (part == 'e' || part == 'E') { /* email */ |
575 | strbuf_add(sb, mail_start, mail_end-mail_start); | |
c3a670de | 576 | return placeholder_len; |
cde75e59 | 577 | } |
93fc05eb | 578 | |
4b340cfa | 579 | if (!s.date_begin) |
c3a670de | 580 | goto skip; |
93fc05eb | 581 | |
4b340cfa JH |
582 | date = strtoul(s.date_begin, NULL, 10); |
583 | ||
cde75e59 | 584 | if (part == 't') { /* date, UNIX timestamp */ |
4b340cfa | 585 | strbuf_add(sb, s.date_begin, s.date_end - s.date_begin); |
c3a670de | 586 | return placeholder_len; |
cde75e59 | 587 | } |
93fc05eb JS |
588 | |
589 | /* parse tz */ | |
4b340cfa JH |
590 | tz = strtoul(s.tz_begin + 1, NULL, 10); |
591 | if (*s.tz_begin == '-') | |
592 | tz = -tz; | |
93fc05eb | 593 | |
cde75e59 RS |
594 | switch (part) { |
595 | case 'd': /* date */ | |
d36f8679 | 596 | strbuf_addstr(sb, show_date(date, tz, dmode)); |
c3a670de | 597 | return placeholder_len; |
cde75e59 RS |
598 | case 'D': /* date, RFC2822 style */ |
599 | strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822)); | |
c3a670de | 600 | return placeholder_len; |
cde75e59 RS |
601 | case 'r': /* date, relative */ |
602 | strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE)); | |
c3a670de | 603 | return placeholder_len; |
cde75e59 RS |
604 | case 'i': /* date, ISO 8601 */ |
605 | strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601)); | |
c3a670de | 606 | return placeholder_len; |
cde75e59 | 607 | } |
c3a670de MC |
608 | |
609 | skip: | |
610 | /* | |
4b340cfa JH |
611 | * reading from either a bogus commit, or a reflog entry with |
612 | * %gn, %ge, etc.; 'sb' cannot be updated, but we still need | |
613 | * to compute a valid return value. | |
c3a670de MC |
614 | */ |
615 | if (part == 'n' || part == 'e' || part == 't' || part == 'd' | |
616 | || part == 'D' || part == 'r' || part == 'i') | |
617 | return placeholder_len; | |
618 | ||
619 | return 0; /* unknown placeholder */ | |
93fc05eb JS |
620 | } |
621 | ||
f29d5958 RS |
622 | struct chunk { |
623 | size_t off; | |
624 | size_t len; | |
625 | }; | |
626 | ||
627 | struct format_commit_context { | |
628 | const struct commit *commit; | |
dd2e794a | 629 | const struct pretty_print_context *pretty_ctx; |
f53bd743 RS |
630 | unsigned commit_header_parsed:1; |
631 | unsigned commit_message_parsed:1; | |
f6667c5e JH |
632 | unsigned commit_signature_parsed:1; |
633 | struct { | |
634 | char *gpg_output; | |
635 | char good_bad; | |
636 | char *signer; | |
637 | } signature; | |
177b29dc | 638 | char *message; |
02edd56b | 639 | size_t width, indent1, indent2; |
f29d5958 RS |
640 | |
641 | /* These offsets are relative to the start of the commit message. */ | |
f29d5958 RS |
642 | struct chunk author; |
643 | struct chunk committer; | |
644 | struct chunk encoding; | |
f53bd743 RS |
645 | size_t message_off; |
646 | size_t subject_off; | |
f29d5958 | 647 | size_t body_off; |
b9c62321 RS |
648 | |
649 | /* The following ones are relative to the result struct strbuf. */ | |
650 | struct chunk abbrev_commit_hash; | |
651 | struct chunk abbrev_tree_hash; | |
652 | struct chunk abbrev_parent_hashes; | |
02edd56b | 653 | size_t wrap_start; |
f29d5958 RS |
654 | }; |
655 | ||
b9c62321 RS |
656 | static int add_again(struct strbuf *sb, struct chunk *chunk) |
657 | { | |
658 | if (chunk->len) { | |
659 | strbuf_adddup(sb, chunk->off, chunk->len); | |
660 | return 1; | |
661 | } | |
662 | ||
663 | /* | |
664 | * We haven't seen this chunk before. Our caller is surely | |
665 | * going to add it the hard way now. Remember the most likely | |
666 | * start of the to-be-added chunk: the current end of the | |
667 | * struct strbuf. | |
668 | */ | |
669 | chunk->off = sb->len; | |
670 | return 0; | |
671 | } | |
672 | ||
f29d5958 | 673 | static void parse_commit_header(struct format_commit_context *context) |
93fc05eb | 674 | { |
177b29dc | 675 | const char *msg = context->message; |
93fc05eb | 676 | int i; |
f29d5958 | 677 | |
f53bd743 | 678 | for (i = 0; msg[i]; i++) { |
f29d5958 RS |
679 | int eol; |
680 | for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) | |
681 | ; /* do nothing */ | |
682 | ||
f29d5958 | 683 | if (i == eol) { |
f53bd743 | 684 | break; |
f29d5958 RS |
685 | } else if (!prefixcmp(msg + i, "author ")) { |
686 | context->author.off = i + 7; | |
687 | context->author.len = eol - i - 7; | |
688 | } else if (!prefixcmp(msg + i, "committer ")) { | |
689 | context->committer.off = i + 10; | |
690 | context->committer.len = eol - i - 10; | |
691 | } else if (!prefixcmp(msg + i, "encoding ")) { | |
692 | context->encoding.off = i + 9; | |
693 | context->encoding.len = eol - i - 9; | |
694 | } | |
695 | i = eol; | |
696 | } | |
f53bd743 | 697 | context->message_off = i; |
f29d5958 RS |
698 | context->commit_header_parsed = 1; |
699 | } | |
700 | ||
46d164b0 SB |
701 | static int istitlechar(char c) |
702 | { | |
703 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
704 | (c >= '0' && c <= '9') || c == '.' || c == '_'; | |
705 | } | |
706 | ||
707 | static void format_sanitized_subject(struct strbuf *sb, const char *msg) | |
708 | { | |
709 | size_t trimlen; | |
871d21d4 | 710 | size_t start_len = sb->len; |
46d164b0 SB |
711 | int space = 2; |
712 | ||
713 | for (; *msg && *msg != '\n'; msg++) { | |
714 | if (istitlechar(*msg)) { | |
715 | if (space == 1) | |
716 | strbuf_addch(sb, '-'); | |
717 | space = 0; | |
718 | strbuf_addch(sb, *msg); | |
719 | if (*msg == '.') | |
720 | while (*(msg+1) == '.') | |
721 | msg++; | |
722 | } else | |
723 | space |= 1; | |
724 | } | |
725 | ||
726 | /* trim any trailing '.' or '-' characters */ | |
727 | trimlen = 0; | |
871d21d4 SB |
728 | while (sb->len - trimlen > start_len && |
729 | (sb->buf[sb->len - 1 - trimlen] == '.' | |
730 | || sb->buf[sb->len - 1 - trimlen] == '-')) | |
46d164b0 SB |
731 | trimlen++; |
732 | strbuf_remove(sb, sb->len - trimlen, trimlen); | |
733 | } | |
734 | ||
cec08717 RS |
735 | const char *format_subject(struct strbuf *sb, const char *msg, |
736 | const char *line_separator) | |
88c44735 RS |
737 | { |
738 | int first = 1; | |
739 | ||
740 | for (;;) { | |
741 | const char *line = msg; | |
742 | int linelen = get_one_line(line); | |
743 | ||
744 | msg += linelen; | |
745 | if (!linelen || is_empty_line(line, &linelen)) | |
746 | break; | |
747 | ||
f53bd743 RS |
748 | if (!sb) |
749 | continue; | |
88c44735 RS |
750 | strbuf_grow(sb, linelen + 2); |
751 | if (!first) | |
752 | strbuf_addstr(sb, line_separator); | |
753 | strbuf_add(sb, line, linelen); | |
754 | first = 0; | |
755 | } | |
756 | return msg; | |
757 | } | |
758 | ||
f53bd743 RS |
759 | static void parse_commit_message(struct format_commit_context *c) |
760 | { | |
177b29dc PN |
761 | const char *msg = c->message + c->message_off; |
762 | const char *start = c->message; | |
f53bd743 RS |
763 | |
764 | msg = skip_empty_lines(msg); | |
765 | c->subject_off = msg - start; | |
766 | ||
767 | msg = format_subject(NULL, msg, NULL); | |
768 | msg = skip_empty_lines(msg); | |
769 | c->body_off = msg - start; | |
770 | ||
771 | c->commit_message_parsed = 1; | |
772 | } | |
773 | ||
3b3d443f RS |
774 | static void format_decoration(struct strbuf *sb, const struct commit *commit) |
775 | { | |
776 | struct name_decoration *d; | |
777 | const char *prefix = " ("; | |
778 | ||
33e7018c | 779 | load_ref_decorations(DECORATE_SHORT_REFS); |
3b3d443f RS |
780 | d = lookup_decoration(&name_decoration, &commit->object); |
781 | while (d) { | |
782 | strbuf_addstr(sb, prefix); | |
783 | prefix = ", "; | |
784 | strbuf_addstr(sb, d->name); | |
785 | d = d->next; | |
786 | } | |
787 | if (prefix[0] == ',') | |
788 | strbuf_addch(sb, ')'); | |
789 | } | |
790 | ||
02edd56b RS |
791 | static void strbuf_wrap(struct strbuf *sb, size_t pos, |
792 | size_t width, size_t indent1, size_t indent2) | |
793 | { | |
794 | struct strbuf tmp = STRBUF_INIT; | |
795 | ||
796 | if (pos) | |
797 | strbuf_add(&tmp, sb->buf, pos); | |
798 | strbuf_add_wrapped_text(&tmp, sb->buf + pos, | |
799 | (int) indent1, (int) indent2, (int) width); | |
800 | strbuf_swap(&tmp, sb); | |
801 | strbuf_release(&tmp); | |
802 | } | |
803 | ||
804 | static void rewrap_message_tail(struct strbuf *sb, | |
805 | struct format_commit_context *c, | |
806 | size_t new_width, size_t new_indent1, | |
807 | size_t new_indent2) | |
808 | { | |
809 | if (c->width == new_width && c->indent1 == new_indent1 && | |
810 | c->indent2 == new_indent2) | |
811 | return; | |
32ca4249 | 812 | if (c->wrap_start < sb->len) |
02edd56b RS |
813 | strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2); |
814 | c->wrap_start = sb->len; | |
815 | c->width = new_width; | |
816 | c->indent1 = new_indent1; | |
817 | c->indent2 = new_indent2; | |
818 | } | |
819 | ||
f6667c5e JH |
820 | static struct { |
821 | char result; | |
822 | const char *check; | |
823 | } signature_check[] = { | |
824 | { 'G', ": Good signature from " }, | |
825 | { 'B', ": BAD signature from " }, | |
826 | }; | |
827 | ||
828 | static void parse_signature_lines(struct format_commit_context *ctx) | |
829 | { | |
830 | const char *buf = ctx->signature.gpg_output; | |
831 | int i; | |
832 | ||
833 | for (i = 0; i < ARRAY_SIZE(signature_check); i++) { | |
834 | const char *found = strstr(buf, signature_check[i].check); | |
835 | const char *next; | |
836 | if (!found) | |
837 | continue; | |
838 | ctx->signature.good_bad = signature_check[i].result; | |
839 | found += strlen(signature_check[i].check); | |
840 | next = strchrnul(found, '\n'); | |
841 | ctx->signature.signer = xmemdupz(found, next - found); | |
842 | break; | |
843 | } | |
844 | } | |
845 | ||
846 | static void parse_commit_signature(struct format_commit_context *ctx) | |
847 | { | |
848 | struct strbuf payload = STRBUF_INIT; | |
849 | struct strbuf signature = STRBUF_INIT; | |
850 | struct strbuf gpg_output = STRBUF_INIT; | |
851 | int status; | |
852 | ||
853 | ctx->commit_signature_parsed = 1; | |
854 | ||
855 | if (parse_signed_commit(ctx->commit->object.sha1, | |
856 | &payload, &signature) <= 0) | |
857 | goto out; | |
858 | status = verify_signed_buffer(payload.buf, payload.len, | |
859 | signature.buf, signature.len, | |
860 | &gpg_output); | |
861 | if (status && !gpg_output.len) | |
862 | goto out; | |
863 | ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL); | |
864 | parse_signature_lines(ctx); | |
865 | ||
866 | out: | |
867 | strbuf_release(&gpg_output); | |
868 | strbuf_release(&payload); | |
869 | strbuf_release(&signature); | |
870 | } | |
871 | ||
872 | ||
cd1957f5 JK |
873 | static int format_reflog_person(struct strbuf *sb, |
874 | char part, | |
875 | struct reflog_walk_info *log, | |
876 | enum date_mode dmode) | |
877 | { | |
878 | const char *ident; | |
879 | ||
880 | if (!log) | |
881 | return 2; | |
882 | ||
883 | ident = get_reflog_ident(log); | |
884 | if (!ident) | |
885 | return 2; | |
886 | ||
887 | return format_person_part(sb, part, ident, strlen(ident), dmode); | |
888 | } | |
889 | ||
9fa708da JH |
890 | static size_t format_commit_one(struct strbuf *sb, const char *placeholder, |
891 | void *context) | |
f29d5958 RS |
892 | { |
893 | struct format_commit_context *c = context; | |
894 | const struct commit *commit = c->commit; | |
177b29dc | 895 | const char *msg = c->message; |
f29d5958 | 896 | struct commit_list *p; |
42c8c74c | 897 | int h1, h2; |
93fc05eb | 898 | |
93fc05eb | 899 | /* these are independent of the commit */ |
cde75e59 RS |
900 | switch (placeholder[0]) { |
901 | case 'C': | |
c002922a JK |
902 | if (placeholder[1] == '(') { |
903 | const char *end = strchr(placeholder + 2, ')'); | |
904 | char color[COLOR_MAXLEN]; | |
905 | if (!end) | |
906 | return 0; | |
907 | color_parse_mem(placeholder + 2, | |
908 | end - (placeholder + 2), | |
909 | "--pretty format", color); | |
910 | strbuf_addstr(sb, color); | |
911 | return end - placeholder + 1; | |
912 | } | |
c3a670de | 913 | if (!prefixcmp(placeholder + 1, "red")) { |
dc6ebd4c | 914 | strbuf_addstr(sb, GIT_COLOR_RED); |
c3a670de MC |
915 | return 4; |
916 | } else if (!prefixcmp(placeholder + 1, "green")) { | |
dc6ebd4c | 917 | strbuf_addstr(sb, GIT_COLOR_GREEN); |
c3a670de MC |
918 | return 6; |
919 | } else if (!prefixcmp(placeholder + 1, "blue")) { | |
dc6ebd4c | 920 | strbuf_addstr(sb, GIT_COLOR_BLUE); |
c3a670de MC |
921 | return 5; |
922 | } else if (!prefixcmp(placeholder + 1, "reset")) { | |
dc6ebd4c | 923 | strbuf_addstr(sb, GIT_COLOR_RESET); |
c3a670de MC |
924 | return 6; |
925 | } else | |
926 | return 0; | |
cde75e59 RS |
927 | case 'n': /* newline */ |
928 | strbuf_addch(sb, '\n'); | |
c3a670de | 929 | return 1; |
42c8c74c GS |
930 | case 'x': |
931 | /* %x00 == NUL, %x0a == LF, etc. */ | |
932 | if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) && | |
933 | h1 <= 16 && | |
934 | 0 <= (h2 = hexval_table[0xff & placeholder[2]]) && | |
935 | h2 <= 16) { | |
936 | strbuf_addch(sb, (h1<<4)|h2); | |
937 | return 3; | |
938 | } else | |
939 | return 0; | |
02edd56b RS |
940 | case 'w': |
941 | if (placeholder[1] == '(') { | |
942 | unsigned long width = 0, indent1 = 0, indent2 = 0; | |
943 | char *next; | |
944 | const char *start = placeholder + 2; | |
945 | const char *end = strchr(start, ')'); | |
946 | if (!end) | |
947 | return 0; | |
948 | if (end > start) { | |
949 | width = strtoul(start, &next, 10); | |
950 | if (*next == ',') { | |
951 | indent1 = strtoul(next + 1, &next, 10); | |
952 | if (*next == ',') { | |
953 | indent2 = strtoul(next + 1, | |
954 | &next, 10); | |
955 | } | |
956 | } | |
957 | if (*next != ')') | |
958 | return 0; | |
959 | } | |
960 | rewrap_message_tail(sb, c, width, indent1, indent2); | |
961 | return end - placeholder + 1; | |
962 | } else | |
963 | return 0; | |
cde75e59 | 964 | } |
93fc05eb JS |
965 | |
966 | /* these depend on the commit */ | |
967 | if (!commit->object.parsed) | |
968 | parse_object(commit->object.sha1); | |
93fc05eb | 969 | |
cde75e59 RS |
970 | switch (placeholder[0]) { |
971 | case 'H': /* commit hash */ | |
972 | strbuf_addstr(sb, sha1_to_hex(commit->object.sha1)); | |
c3a670de | 973 | return 1; |
cde75e59 | 974 | case 'h': /* abbreviated commit hash */ |
b9c62321 | 975 | if (add_again(sb, &c->abbrev_commit_hash)) |
c3a670de | 976 | return 1; |
cde75e59 | 977 | strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1, |
c1977021 | 978 | c->pretty_ctx->abbrev)); |
b9c62321 | 979 | c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off; |
c3a670de | 980 | return 1; |
cde75e59 RS |
981 | case 'T': /* tree hash */ |
982 | strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1)); | |
c3a670de | 983 | return 1; |
cde75e59 | 984 | case 't': /* abbreviated tree hash */ |
b9c62321 | 985 | if (add_again(sb, &c->abbrev_tree_hash)) |
c3a670de | 986 | return 1; |
cde75e59 | 987 | strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1, |
c1977021 | 988 | c->pretty_ctx->abbrev)); |
b9c62321 | 989 | c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off; |
c3a670de | 990 | return 1; |
cde75e59 RS |
991 | case 'P': /* parent hashes */ |
992 | for (p = commit->parents; p; p = p->next) { | |
993 | if (p != commit->parents) | |
994 | strbuf_addch(sb, ' '); | |
995 | strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1)); | |
996 | } | |
c3a670de | 997 | return 1; |
cde75e59 | 998 | case 'p': /* abbreviated parent hashes */ |
b9c62321 | 999 | if (add_again(sb, &c->abbrev_parent_hashes)) |
c3a670de | 1000 | return 1; |
cde75e59 RS |
1001 | for (p = commit->parents; p; p = p->next) { |
1002 | if (p != commit->parents) | |
1003 | strbuf_addch(sb, ' '); | |
1004 | strbuf_addstr(sb, find_unique_abbrev( | |
c1977021 WP |
1005 | p->item->object.sha1, |
1006 | c->pretty_ctx->abbrev)); | |
cde75e59 | 1007 | } |
b9c62321 RS |
1008 | c->abbrev_parent_hashes.len = sb->len - |
1009 | c->abbrev_parent_hashes.off; | |
c3a670de | 1010 | return 1; |
cde75e59 | 1011 | case 'm': /* left/right/bottom */ |
1df2d656 | 1012 | strbuf_addstr(sb, get_revision_mark(NULL, commit)); |
c3a670de | 1013 | return 1; |
3b3d443f RS |
1014 | case 'd': |
1015 | format_decoration(sb, commit); | |
1016 | return 1; | |
8f8f5476 TR |
1017 | case 'g': /* reflog info */ |
1018 | switch(placeholder[1]) { | |
1019 | case 'd': /* reflog selector */ | |
1020 | case 'D': | |
1021 | if (c->pretty_ctx->reflog_info) | |
1022 | get_reflog_selector(sb, | |
1023 | c->pretty_ctx->reflog_info, | |
1024 | c->pretty_ctx->date_mode, | |
55ccf85a | 1025 | c->pretty_ctx->date_mode_explicit, |
8f8f5476 TR |
1026 | (placeholder[1] == 'd')); |
1027 | return 2; | |
1028 | case 's': /* reflog message */ | |
1029 | if (c->pretty_ctx->reflog_info) | |
1030 | get_reflog_message(sb, c->pretty_ctx->reflog_info); | |
1031 | return 2; | |
cd1957f5 JK |
1032 | case 'n': |
1033 | case 'N': | |
1034 | case 'e': | |
1035 | case 'E': | |
1036 | return format_reflog_person(sb, | |
1037 | placeholder[1], | |
1038 | c->pretty_ctx->reflog_info, | |
1039 | c->pretty_ctx->date_mode); | |
8f8f5476 TR |
1040 | } |
1041 | return 0; /* unknown %g placeholder */ | |
8b208f02 | 1042 | case 'N': |
5b163603 JG |
1043 | if (c->pretty_ctx->show_notes) { |
1044 | format_display_notes(commit->object.sha1, sb, | |
a6fa5992 | 1045 | get_log_output_encoding(), 0); |
5b163603 JG |
1046 | return 1; |
1047 | } | |
1048 | return 0; | |
cde75e59 RS |
1049 | } |
1050 | ||
f6667c5e JH |
1051 | if (placeholder[0] == 'G') { |
1052 | if (!c->commit_signature_parsed) | |
1053 | parse_commit_signature(c); | |
1054 | switch (placeholder[1]) { | |
1055 | case 'G': | |
1056 | if (c->signature.gpg_output) | |
1057 | strbuf_addstr(sb, c->signature.gpg_output); | |
1058 | break; | |
1059 | case '?': | |
1060 | switch (c->signature.good_bad) { | |
1061 | case 'G': | |
1062 | case 'B': | |
1063 | strbuf_addch(sb, c->signature.good_bad); | |
1064 | } | |
1065 | break; | |
1066 | case 'S': | |
1067 | if (c->signature.signer) | |
1068 | strbuf_addstr(sb, c->signature.signer); | |
1069 | break; | |
1070 | } | |
1071 | return 2; | |
1072 | } | |
1073 | ||
1074 | ||
cde75e59 | 1075 | /* For the rest we have to parse the commit header. */ |
f29d5958 RS |
1076 | if (!c->commit_header_parsed) |
1077 | parse_commit_header(c); | |
93fc05eb | 1078 | |
f29d5958 | 1079 | switch (placeholder[0]) { |
c3a670de MC |
1080 | case 'a': /* author ... */ |
1081 | return format_person_part(sb, placeholder[1], | |
d36f8679 | 1082 | msg + c->author.off, c->author.len, |
dd2e794a | 1083 | c->pretty_ctx->date_mode); |
c3a670de MC |
1084 | case 'c': /* committer ... */ |
1085 | return format_person_part(sb, placeholder[1], | |
d36f8679 | 1086 | msg + c->committer.off, c->committer.len, |
dd2e794a | 1087 | c->pretty_ctx->date_mode); |
c3a670de | 1088 | case 'e': /* encoding */ |
f29d5958 | 1089 | strbuf_add(sb, msg + c->encoding.off, c->encoding.len); |
c3a670de | 1090 | return 1; |
1367b12a EB |
1091 | case 'B': /* raw body */ |
1092 | /* message_off is always left at the initial newline */ | |
1093 | strbuf_addstr(sb, msg + c->message_off + 1); | |
1094 | return 1; | |
f53bd743 RS |
1095 | } |
1096 | ||
1097 | /* Now we need to parse the commit message. */ | |
1098 | if (!c->commit_message_parsed) | |
1099 | parse_commit_message(c); | |
1100 | ||
1101 | switch (placeholder[0]) { | |
1102 | case 's': /* subject */ | |
1103 | format_subject(sb, msg + c->subject_off, " "); | |
1104 | return 1; | |
46d164b0 SB |
1105 | case 'f': /* sanitized subject */ |
1106 | format_sanitized_subject(sb, msg + c->subject_off); | |
1107 | return 1; | |
c3a670de | 1108 | case 'b': /* body */ |
f29d5958 | 1109 | strbuf_addstr(sb, msg + c->body_off); |
c3a670de | 1110 | return 1; |
93fc05eb | 1111 | } |
c3a670de | 1112 | return 0; /* unknown placeholder */ |
cde75e59 RS |
1113 | } |
1114 | ||
9fa708da JH |
1115 | static size_t format_commit_item(struct strbuf *sb, const char *placeholder, |
1116 | void *context) | |
1117 | { | |
1118 | int consumed; | |
1119 | size_t orig_len; | |
1120 | enum { | |
1121 | NO_MAGIC, | |
1122 | ADD_LF_BEFORE_NON_EMPTY, | |
1123 | DEL_LF_BEFORE_EMPTY, | |
223a923c | 1124 | ADD_SP_BEFORE_NON_EMPTY |
9fa708da JH |
1125 | } magic = NO_MAGIC; |
1126 | ||
1127 | switch (placeholder[0]) { | |
1128 | case '-': | |
1129 | magic = DEL_LF_BEFORE_EMPTY; | |
1130 | break; | |
1131 | case '+': | |
1132 | magic = ADD_LF_BEFORE_NON_EMPTY; | |
1133 | break; | |
7b88176e MG |
1134 | case ' ': |
1135 | magic = ADD_SP_BEFORE_NON_EMPTY; | |
1136 | break; | |
9fa708da JH |
1137 | default: |
1138 | break; | |
1139 | } | |
1140 | if (magic != NO_MAGIC) | |
1141 | placeholder++; | |
1142 | ||
1143 | orig_len = sb->len; | |
1144 | consumed = format_commit_one(sb, placeholder, context); | |
1145 | if (magic == NO_MAGIC) | |
1146 | return consumed; | |
1147 | ||
1148 | if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) { | |
1149 | while (sb->len && sb->buf[sb->len - 1] == '\n') | |
1150 | strbuf_setlen(sb, sb->len - 1); | |
7b88176e MG |
1151 | } else if (orig_len != sb->len) { |
1152 | if (magic == ADD_LF_BEFORE_NON_EMPTY) | |
1153 | strbuf_insert(sb, orig_len, "\n", 1); | |
1154 | else if (magic == ADD_SP_BEFORE_NON_EMPTY) | |
1155 | strbuf_insert(sb, orig_len, " ", 1); | |
9fa708da JH |
1156 | } |
1157 | return consumed + 1; | |
1158 | } | |
1159 | ||
5b163603 JG |
1160 | static size_t userformat_want_item(struct strbuf *sb, const char *placeholder, |
1161 | void *context) | |
1162 | { | |
1163 | struct userformat_want *w = context; | |
1164 | ||
7b88176e | 1165 | if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ') |
5b163603 JG |
1166 | placeholder++; |
1167 | ||
1168 | switch (*placeholder) { | |
1169 | case 'N': | |
1170 | w->notes = 1; | |
1171 | break; | |
1172 | } | |
1173 | return 0; | |
1174 | } | |
1175 | ||
1176 | void userformat_find_requirements(const char *fmt, struct userformat_want *w) | |
1177 | { | |
1178 | struct strbuf dummy = STRBUF_INIT; | |
1179 | ||
1180 | if (!fmt) { | |
1181 | if (!user_format) | |
1182 | return; | |
1183 | fmt = user_format; | |
1184 | } | |
a6253d10 | 1185 | strbuf_expand(&dummy, fmt, userformat_want_item, w); |
5b163603 JG |
1186 | strbuf_release(&dummy); |
1187 | } | |
1188 | ||
cde75e59 | 1189 | void format_commit_message(const struct commit *commit, |
7f98ebc8 | 1190 | const char *format, struct strbuf *sb, |
dd2e794a | 1191 | const struct pretty_print_context *pretty_ctx) |
cde75e59 | 1192 | { |
f29d5958 | 1193 | struct format_commit_context context; |
177b29dc | 1194 | static const char utf8[] = "UTF-8"; |
177b29dc | 1195 | const char *output_enc = pretty_ctx->output_encoding; |
f29d5958 RS |
1196 | |
1197 | memset(&context, 0, sizeof(context)); | |
1198 | context.commit = commit; | |
dd2e794a | 1199 | context.pretty_ctx = pretty_ctx; |
02edd56b | 1200 | context.wrap_start = sb->len; |
177b29dc PN |
1201 | context.message = commit->buffer; |
1202 | if (output_enc) { | |
9cd7a92b | 1203 | char *enc = get_header(commit, "encoding"); |
1d5bd615 | 1204 | if (strcmp(enc ? enc : utf8, output_enc)) { |
177b29dc | 1205 | context.message = logmsg_reencode(commit, output_enc); |
1d5bd615 NTND |
1206 | if (!context.message) |
1207 | context.message = commit->buffer; | |
1208 | } | |
9cd7a92b | 1209 | free(enc); |
177b29dc PN |
1210 | } |
1211 | ||
c3a670de | 1212 | strbuf_expand(sb, format, format_commit_item, &context); |
02edd56b | 1213 | rewrap_message_tail(sb, &context, 0, 0, 0); |
177b29dc PN |
1214 | |
1215 | if (context.message != commit->buffer) | |
1216 | free(context.message); | |
f6667c5e JH |
1217 | free(context.signature.gpg_output); |
1218 | free(context.signature.signer); | |
93fc05eb JS |
1219 | } |
1220 | ||
6bf13944 | 1221 | static void pp_header(const struct pretty_print_context *pp, |
93fc05eb JS |
1222 | const char *encoding, |
1223 | const struct commit *commit, | |
1224 | const char **msg_p, | |
1225 | struct strbuf *sb) | |
1226 | { | |
1227 | int parents_shown = 0; | |
1228 | ||
1229 | for (;;) { | |
1230 | const char *line = *msg_p; | |
1231 | int linelen = get_one_line(*msg_p); | |
1232 | ||
1233 | if (!linelen) | |
1234 | return; | |
1235 | *msg_p += linelen; | |
1236 | ||
1237 | if (linelen == 1) | |
1238 | /* End of header */ | |
1239 | return; | |
1240 | ||
6bf13944 | 1241 | if (pp->fmt == CMIT_FMT_RAW) { |
93fc05eb JS |
1242 | strbuf_add(sb, line, linelen); |
1243 | continue; | |
1244 | } | |
1245 | ||
1246 | if (!memcmp(line, "parent ", 7)) { | |
1247 | if (linelen != 48) | |
1248 | die("bad parent line in commit"); | |
1249 | continue; | |
1250 | } | |
1251 | ||
1252 | if (!parents_shown) { | |
1253 | struct commit_list *parent; | |
1254 | int num; | |
1255 | for (parent = commit->parents, num = 0; | |
1256 | parent; | |
1257 | parent = parent->next, num++) | |
1258 | ; | |
1259 | /* with enough slop */ | |
1260 | strbuf_grow(sb, num * 50 + 20); | |
6bf13944 | 1261 | add_merge_info(pp, sb, commit); |
93fc05eb JS |
1262 | parents_shown = 1; |
1263 | } | |
1264 | ||
1265 | /* | |
1266 | * MEDIUM == DEFAULT shows only author with dates. | |
1267 | * FULL shows both authors but not dates. | |
1268 | * FULLER shows both authors and dates. | |
1269 | */ | |
1270 | if (!memcmp(line, "author ", 7)) { | |
1271 | strbuf_grow(sb, linelen + 80); | |
6bf13944 | 1272 | pp_user_info(pp, "Author", sb, line + 7, encoding); |
93fc05eb JS |
1273 | } |
1274 | if (!memcmp(line, "committer ", 10) && | |
6bf13944 | 1275 | (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { |
93fc05eb | 1276 | strbuf_grow(sb, linelen + 80); |
6bf13944 | 1277 | pp_user_info(pp, "Commit", sb, line + 10, encoding); |
93fc05eb JS |
1278 | } |
1279 | } | |
1280 | } | |
1281 | ||
6bf13944 | 1282 | void pp_title_line(const struct pretty_print_context *pp, |
b02bd65f DB |
1283 | const char **msg_p, |
1284 | struct strbuf *sb, | |
b02bd65f | 1285 | const char *encoding, |
267123b4 | 1286 | int need_8bit_cte) |
93fc05eb JS |
1287 | { |
1288 | struct strbuf title; | |
1289 | ||
1290 | strbuf_init(&title, 80); | |
9553d2b2 JK |
1291 | *msg_p = format_subject(&title, *msg_p, |
1292 | pp->preserve_subject ? "\n" : " "); | |
93fc05eb JS |
1293 | |
1294 | strbuf_grow(sb, title.len + 1024); | |
6bf13944 JK |
1295 | if (pp->subject) { |
1296 | strbuf_addstr(sb, pp->subject); | |
93fc05eb JS |
1297 | add_rfc2047(sb, title.buf, title.len, encoding); |
1298 | } else { | |
1299 | strbuf_addbuf(sb, &title); | |
1300 | } | |
1301 | strbuf_addch(sb, '\n'); | |
1302 | ||
6bf4f1b4 | 1303 | if (need_8bit_cte > 0) { |
93fc05eb JS |
1304 | const char *header_fmt = |
1305 | "MIME-Version: 1.0\n" | |
1306 | "Content-Type: text/plain; charset=%s\n" | |
1307 | "Content-Transfer-Encoding: 8bit\n"; | |
1308 | strbuf_addf(sb, header_fmt, encoding); | |
1309 | } | |
6bf13944 JK |
1310 | if (pp->after_subject) { |
1311 | strbuf_addstr(sb, pp->after_subject); | |
93fc05eb | 1312 | } |
6bf13944 | 1313 | if (pp->fmt == CMIT_FMT_EMAIL) { |
93fc05eb JS |
1314 | strbuf_addch(sb, '\n'); |
1315 | } | |
1316 | strbuf_release(&title); | |
1317 | } | |
1318 | ||
6bf13944 | 1319 | void pp_remainder(const struct pretty_print_context *pp, |
b02bd65f DB |
1320 | const char **msg_p, |
1321 | struct strbuf *sb, | |
1322 | int indent) | |
93fc05eb JS |
1323 | { |
1324 | int first = 1; | |
1325 | for (;;) { | |
1326 | const char *line = *msg_p; | |
1327 | int linelen = get_one_line(line); | |
1328 | *msg_p += linelen; | |
1329 | ||
1330 | if (!linelen) | |
1331 | break; | |
1332 | ||
1333 | if (is_empty_line(line, &linelen)) { | |
1334 | if (first) | |
1335 | continue; | |
6bf13944 | 1336 | if (pp->fmt == CMIT_FMT_SHORT) |
93fc05eb JS |
1337 | break; |
1338 | } | |
1339 | first = 0; | |
1340 | ||
1341 | strbuf_grow(sb, linelen + indent + 20); | |
1342 | if (indent) { | |
1343 | memset(sb->buf + sb->len, ' ', indent); | |
1344 | strbuf_setlen(sb, sb->len + indent); | |
1345 | } | |
1346 | strbuf_add(sb, line, linelen); | |
1347 | strbuf_addch(sb, '\n'); | |
1348 | } | |
1349 | } | |
1350 | ||
69cd8f63 AG |
1351 | char *reencode_commit_message(const struct commit *commit, const char **encoding_p) |
1352 | { | |
1353 | const char *encoding; | |
1354 | ||
a6fa5992 | 1355 | encoding = get_log_output_encoding(); |
69cd8f63 AG |
1356 | if (encoding_p) |
1357 | *encoding_p = encoding; | |
1358 | return logmsg_reencode(commit, encoding); | |
1359 | } | |
1360 | ||
6bf13944 JK |
1361 | void pretty_print_commit(const struct pretty_print_context *pp, |
1362 | const struct commit *commit, | |
1363 | struct strbuf *sb) | |
93fc05eb JS |
1364 | { |
1365 | unsigned long beginning_of_body; | |
1366 | int indent = 4; | |
1367 | const char *msg = commit->buffer; | |
1368 | char *reencoded; | |
1369 | const char *encoding; | |
6bf13944 | 1370 | int need_8bit_cte = pp->need_8bit_cte; |
93fc05eb | 1371 | |
6bf13944 JK |
1372 | if (pp->fmt == CMIT_FMT_USERFORMAT) { |
1373 | format_commit_message(commit, user_format, sb, pp); | |
93fc05eb JS |
1374 | return; |
1375 | } | |
1376 | ||
69cd8f63 | 1377 | reencoded = reencode_commit_message(commit, &encoding); |
93fc05eb JS |
1378 | if (reencoded) { |
1379 | msg = reencoded; | |
1380 | } | |
1381 | ||
6bf13944 | 1382 | if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL) |
93fc05eb JS |
1383 | indent = 0; |
1384 | ||
6bf4f1b4 JH |
1385 | /* |
1386 | * We need to check and emit Content-type: to mark it | |
1387 | * as 8-bit if we haven't done so. | |
93fc05eb | 1388 | */ |
6bf13944 | 1389 | if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) { |
93fc05eb JS |
1390 | int i, ch, in_body; |
1391 | ||
1392 | for (in_body = i = 0; (ch = msg[i]); i++) { | |
1393 | if (!in_body) { | |
1394 | /* author could be non 7-bit ASCII but | |
1395 | * the log may be so; skip over the | |
1396 | * header part first. | |
1397 | */ | |
1398 | if (ch == '\n' && msg[i+1] == '\n') | |
1399 | in_body = 1; | |
1400 | } | |
1401 | else if (non_ascii(ch)) { | |
6bf4f1b4 | 1402 | need_8bit_cte = 1; |
93fc05eb JS |
1403 | break; |
1404 | } | |
1405 | } | |
1406 | } | |
1407 | ||
6bf13944 JK |
1408 | pp_header(pp, encoding, commit, &msg, sb); |
1409 | if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) { | |
93fc05eb JS |
1410 | strbuf_addch(sb, '\n'); |
1411 | } | |
1412 | ||
1413 | /* Skip excess blank lines at the beginning of body, if any... */ | |
a0109668 | 1414 | msg = skip_empty_lines(msg); |
93fc05eb JS |
1415 | |
1416 | /* These formats treat the title line specially. */ | |
6bf13944 JK |
1417 | if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL) |
1418 | pp_title_line(pp, &msg, sb, encoding, need_8bit_cte); | |
93fc05eb JS |
1419 | |
1420 | beginning_of_body = sb->len; | |
6bf13944 JK |
1421 | if (pp->fmt != CMIT_FMT_ONELINE) |
1422 | pp_remainder(pp, &msg, sb, indent); | |
93fc05eb JS |
1423 | strbuf_rtrim(sb); |
1424 | ||
1425 | /* Make sure there is an EOLN for the non-oneline case */ | |
6bf13944 | 1426 | if (pp->fmt != CMIT_FMT_ONELINE) |
93fc05eb JS |
1427 | strbuf_addch(sb, '\n'); |
1428 | ||
1429 | /* | |
1430 | * The caller may append additional body text in e-mail | |
1431 | * format. Make sure we did not strip the blank line | |
1432 | * between the header and the body. | |
1433 | */ | |
6bf13944 | 1434 | if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body) |
93fc05eb | 1435 | strbuf_addch(sb, '\n'); |
a97a7468 | 1436 | |
6bf13944 | 1437 | if (pp->show_notes) |
894a9d33 TR |
1438 | format_display_notes(commit->object.sha1, sb, encoding, |
1439 | NOTES_SHOW_HEADER | NOTES_INDENT); | |
a97a7468 | 1440 | |
93fc05eb JS |
1441 | free(reencoded); |
1442 | } | |
8b8a5374 JK |
1443 | |
1444 | void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit, | |
1445 | struct strbuf *sb) | |
1446 | { | |
1447 | struct pretty_print_context pp = {0}; | |
6bf13944 JK |
1448 | pp.fmt = fmt; |
1449 | pretty_print_commit(&pp, commit, sb); | |
8b8a5374 | 1450 | } |