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