]>
Commit | Line | Data |
---|---|---|
93fc05eb | 1 | #include "cache.h" |
36bf1958 | 2 | #include "alloc.h" |
b2141fc1 | 3 | #include "config.h" |
93fc05eb | 4 | #include "commit.h" |
f394e093 | 5 | #include "gettext.h" |
41771fa4 | 6 | #include "hex.h" |
93fc05eb JS |
7 | #include "utf8.h" |
8 | #include "diff.h" | |
9 | #include "revision.h" | |
c455c87c | 10 | #include "string-list.h" |
e0cbc397 | 11 | #include "mailmap.h" |
3b3d443f | 12 | #include "log-tree.h" |
a97a7468 | 13 | #include "notes.h" |
c002922a | 14 | #include "color.h" |
8f8f5476 | 15 | #include "reflog-walk.h" |
f6667c5e | 16 | #include "gpg-interface.h" |
d9f31fbf | 17 | #include "trailer.h" |
15ae82d5 | 18 | #include "run-command.h" |
93fc05eb | 19 | |
304a50ad PS |
20 | /* |
21 | * The limit for formatting directives, which enable the caller to append | |
22 | * arbitrarily many bytes to the formatted buffer. This includes padding | |
23 | * and wrapping formatters. | |
24 | */ | |
25 | #define FORMATTING_LIMIT (16 * 1024) | |
26 | ||
93fc05eb | 27 | static char *user_format; |
40957891 WP |
28 | static struct cmt_fmt_map { |
29 | const char *name; | |
30 | enum cmit_fmt format; | |
31 | int is_tformat; | |
0893eec8 | 32 | int expand_tabs_in_log; |
2d7671ef | 33 | int is_alias; |
618a8550 | 34 | enum date_mode_type default_date_mode_type; |
2d7671ef | 35 | const char *user_format; |
40957891 | 36 | } *commit_formats; |
8028184e | 37 | static size_t builtin_formats_len; |
40957891 | 38 | static size_t commit_formats_len; |
8028184e | 39 | static size_t commit_formats_alloc; |
40957891 | 40 | static struct cmt_fmt_map *find_commit_format(const char *sought); |
93fc05eb | 41 | |
b9c7d6e4 JK |
42 | int commit_format_is_empty(enum cmit_fmt fmt) |
43 | { | |
44 | return fmt == CMIT_FMT_USERFORMAT && !*user_format; | |
45 | } | |
46 | ||
36407548 NS |
47 | static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat) |
48 | { | |
49 | free(user_format); | |
50 | user_format = xstrdup(cp); | |
51 | if (is_tformat) | |
52 | rev->use_terminator = 1; | |
53 | rev->commit_format = CMIT_FMT_USERFORMAT; | |
54 | } | |
55 | ||
783a86c1 | 56 | static int git_pretty_formats_config(const char *var, const char *value, |
5cf88fd8 | 57 | void *cb UNUSED) |
93fc05eb | 58 | { |
8028184e WP |
59 | struct cmt_fmt_map *commit_format = NULL; |
60 | const char *name; | |
61 | const char *fmt; | |
93fc05eb | 62 | int i; |
8028184e | 63 | |
95b567c7 | 64 | if (!skip_prefix(var, "pretty.", &name)) |
8028184e WP |
65 | return 0; |
66 | ||
8028184e WP |
67 | for (i = 0; i < builtin_formats_len; i++) { |
68 | if (!strcmp(commit_formats[i].name, name)) | |
69 | return 0; | |
70 | } | |
71 | ||
72 | for (i = builtin_formats_len; i < commit_formats_len; i++) { | |
73 | if (!strcmp(commit_formats[i].name, name)) { | |
74 | commit_format = &commit_formats[i]; | |
75 | break; | |
76 | } | |
77 | } | |
78 | ||
79 | if (!commit_format) { | |
80 | ALLOC_GROW(commit_formats, commit_formats_len+1, | |
81 | commit_formats_alloc); | |
82 | commit_format = &commit_formats[commit_formats_len]; | |
95a2618f | 83 | memset(commit_format, 0, sizeof(*commit_format)); |
8028184e WP |
84 | commit_formats_len++; |
85 | } | |
86 | ||
87 | commit_format->name = xstrdup(name); | |
88 | commit_format->format = CMIT_FMT_USERFORMAT; | |
a26bc613 TA |
89 | if (git_config_string(&fmt, var, value)) |
90 | return -1; | |
91 | ||
e3f1da98 RS |
92 | if (skip_prefix(fmt, "format:", &fmt)) |
93 | commit_format->is_tformat = 0; | |
94 | else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%')) | |
8028184e WP |
95 | commit_format->is_tformat = 1; |
96 | else | |
97 | commit_format->is_alias = 1; | |
98 | commit_format->user_format = fmt; | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
40957891 | 103 | static void setup_commit_formats(void) |
93fc05eb | 104 | { |
40957891 | 105 | struct cmt_fmt_map builtin_formats[] = { |
0893eec8 | 106 | { "raw", CMIT_FMT_RAW, 0, 0 }, |
fe37a9c5 | 107 | { "medium", CMIT_FMT_MEDIUM, 0, 8 }, |
0893eec8 JH |
108 | { "short", CMIT_FMT_SHORT, 0, 0 }, |
109 | { "email", CMIT_FMT_EMAIL, 0, 0 }, | |
9f23e040 | 110 | { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 }, |
fe37a9c5 JH |
111 | { "fuller", CMIT_FMT_FULLER, 0, 8 }, |
112 | { "full", CMIT_FMT_FULL, 0, 8 }, | |
1f0fc1db DL |
113 | { "oneline", CMIT_FMT_ONELINE, 1, 0 }, |
114 | { "reference", CMIT_FMT_USERFORMAT, 1, 0, | |
115 | 0, DATE_SHORT, "%C(auto)%h (%s, %ad)" }, | |
5a59a230 NTND |
116 | /* |
117 | * Please update $__git_log_pretty_formats in | |
118 | * git-completion.bash when you add new formats. | |
119 | */ | |
4da45bef | 120 | }; |
40957891 | 121 | commit_formats_len = ARRAY_SIZE(builtin_formats); |
8028184e WP |
122 | builtin_formats_len = commit_formats_len; |
123 | ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc); | |
921d49be RS |
124 | COPY_ARRAY(commit_formats, builtin_formats, |
125 | ARRAY_SIZE(builtin_formats)); | |
8028184e WP |
126 | |
127 | git_config(git_pretty_formats_config, NULL); | |
40957891 WP |
128 | } |
129 | ||
2d7671ef WP |
130 | static struct cmt_fmt_map *find_commit_format_recursive(const char *sought, |
131 | const char *original, | |
132 | int num_redirections) | |
40957891 WP |
133 | { |
134 | struct cmt_fmt_map *found = NULL; | |
135 | size_t found_match_len = 0; | |
136 | int i; | |
137 | ||
2d7671ef WP |
138 | if (num_redirections >= commit_formats_len) |
139 | die("invalid --pretty format: " | |
140 | "'%s' references an alias which points to itself", | |
141 | original); | |
40957891 WP |
142 | |
143 | for (i = 0; i < commit_formats_len; i++) { | |
144 | size_t match_len; | |
145 | ||
59556548 | 146 | if (!starts_with(commit_formats[i].name, sought)) |
40957891 WP |
147 | continue; |
148 | ||
149 | match_len = strlen(commit_formats[i].name); | |
150 | if (found == NULL || found_match_len > match_len) { | |
151 | found = &commit_formats[i]; | |
152 | found_match_len = match_len; | |
153 | } | |
154 | } | |
2d7671ef WP |
155 | |
156 | if (found && found->is_alias) { | |
157 | found = find_commit_format_recursive(found->user_format, | |
158 | original, | |
159 | num_redirections+1); | |
160 | } | |
161 | ||
40957891 WP |
162 | return found; |
163 | } | |
164 | ||
2d7671ef WP |
165 | static struct cmt_fmt_map *find_commit_format(const char *sought) |
166 | { | |
167 | if (!commit_formats) | |
168 | setup_commit_formats(); | |
169 | ||
170 | return find_commit_format_recursive(sought, sought, 0); | |
171 | } | |
172 | ||
40957891 WP |
173 | void get_commit_format(const char *arg, struct rev_info *rev) |
174 | { | |
175 | struct cmt_fmt_map *commit_format; | |
93fc05eb | 176 | |
4da45bef | 177 | rev->use_terminator = 0; |
c75e7ad2 | 178 | if (!arg) { |
4da45bef JH |
179 | rev->commit_format = CMIT_FMT_DEFAULT; |
180 | return; | |
181 | } | |
e3f1da98 RS |
182 | if (skip_prefix(arg, "format:", &arg)) { |
183 | save_user_format(rev, arg, 0); | |
4da45bef | 184 | return; |
93fc05eb | 185 | } |
40957891 | 186 | |
e3f1da98 | 187 | if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) { |
36407548 NS |
188 | save_user_format(rev, arg, 1); |
189 | return; | |
190 | } | |
93fc05eb | 191 | |
40957891 WP |
192 | commit_format = find_commit_format(arg); |
193 | if (!commit_format) | |
194 | die("invalid --pretty format: %s", arg); | |
195 | ||
196 | rev->commit_format = commit_format->format; | |
197 | rev->use_terminator = commit_format->is_tformat; | |
0893eec8 | 198 | rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log; |
618a8550 DL |
199 | if (!rev->date_mode_explicit && commit_format->default_date_mode_type) |
200 | rev->date_mode.type = commit_format->default_date_mode_type; | |
8028184e WP |
201 | if (commit_format->format == CMIT_FMT_USERFORMAT) { |
202 | save_user_format(rev, commit_format->user_format, | |
203 | commit_format->is_tformat); | |
204 | } | |
93fc05eb JS |
205 | } |
206 | ||
207 | /* | |
208 | * Generic support for pretty-printing the header | |
209 | */ | |
210 | static int get_one_line(const char *msg) | |
211 | { | |
212 | int ret = 0; | |
213 | ||
214 | for (;;) { | |
215 | char c = *msg++; | |
216 | if (!c) | |
217 | break; | |
218 | ret++; | |
219 | if (c == '\n') | |
220 | break; | |
221 | } | |
222 | return ret; | |
223 | } | |
224 | ||
225 | /* High bit set, or ISO-2022-INT */ | |
cc571142 | 226 | static int non_ascii(int ch) |
93fc05eb | 227 | { |
c2e9364a | 228 | return !isascii(ch) || ch == '\033'; |
93fc05eb JS |
229 | } |
230 | ||
28e9cf65 JS |
231 | int has_non_ascii(const char *s) |
232 | { | |
233 | int ch; | |
234 | if (!s) | |
235 | return 0; | |
236 | while ((ch = *s++) != '\0') { | |
237 | if (non_ascii(ch)) | |
238 | return 1; | |
239 | } | |
240 | return 0; | |
241 | } | |
242 | ||
4d03c18a JK |
243 | static int is_rfc822_special(char ch) |
244 | { | |
245 | switch (ch) { | |
246 | case '(': | |
247 | case ')': | |
248 | case '<': | |
249 | case '>': | |
250 | case '[': | |
251 | case ']': | |
252 | case ':': | |
253 | case ';': | |
254 | case '@': | |
255 | case ',': | |
256 | case '.': | |
257 | case '"': | |
258 | case '\\': | |
259 | return 1; | |
260 | default: | |
261 | return 0; | |
262 | } | |
263 | } | |
264 | ||
41dd00ba | 265 | static int needs_rfc822_quoting(const char *s, int len) |
4d03c18a JK |
266 | { |
267 | int i; | |
268 | for (i = 0; i < len; i++) | |
269 | if (is_rfc822_special(s[i])) | |
270 | return 1; | |
271 | return 0; | |
272 | } | |
273 | ||
f9b7204b JS |
274 | static int last_line_length(struct strbuf *sb) |
275 | { | |
276 | int i; | |
277 | ||
278 | /* How many bytes are already used on the last line? */ | |
279 | for (i = sb->len - 1; i >= 0; i--) | |
280 | if (sb->buf[i] == '\n') | |
281 | break; | |
282 | return sb->len - (i + 1); | |
283 | } | |
284 | ||
4d03c18a JK |
285 | static void add_rfc822_quoted(struct strbuf *out, const char *s, int len) |
286 | { | |
287 | int i; | |
288 | ||
289 | /* just a guess, we may have to also backslash-quote */ | |
290 | strbuf_grow(out, len + 2); | |
291 | ||
292 | strbuf_addch(out, '"'); | |
293 | for (i = 0; i < len; i++) { | |
294 | switch (s[i]) { | |
295 | case '"': | |
296 | case '\\': | |
297 | strbuf_addch(out, '\\'); | |
298 | /* fall through */ | |
299 | default: | |
300 | strbuf_addch(out, s[i]); | |
301 | } | |
302 | } | |
303 | strbuf_addch(out, '"'); | |
304 | } | |
305 | ||
0fcec2ce JS |
306 | enum rfc2047_type { |
307 | RFC2047_SUBJECT, | |
78273520 | 308 | RFC2047_ADDRESS |
0fcec2ce JS |
309 | }; |
310 | ||
311 | static int is_rfc2047_special(char ch, enum rfc2047_type type) | |
93fc05eb | 312 | { |
0fcec2ce JS |
313 | /* |
314 | * rfc2047, section 4.2: | |
315 | * | |
316 | * 8-bit values which correspond to printable ASCII characters other | |
317 | * than "=", "?", and "_" (underscore), MAY be represented as those | |
318 | * characters. (But see section 5 for restrictions.) In | |
319 | * particular, SPACE and TAB MUST NOT be represented as themselves | |
320 | * within encoded words. | |
321 | */ | |
322 | ||
323 | /* | |
324 | * rule out non-ASCII characters and non-printable characters (the | |
325 | * non-ASCII check should be redundant as isprint() is not localized | |
326 | * and only knows about ASCII, but be defensive about that) | |
327 | */ | |
328 | if (non_ascii(ch) || !isprint(ch)) | |
329 | return 1; | |
330 | ||
331 | /* | |
332 | * rule out special printable characters (' ' should be the only | |
333 | * whitespace character considered printable, but be defensive and use | |
334 | * isspace()) | |
335 | */ | |
336 | if (isspace(ch) || ch == '=' || ch == '?' || ch == '_') | |
94f6cdf6 JS |
337 | return 1; |
338 | ||
0fcec2ce JS |
339 | /* |
340 | * rfc2047, section 5.3: | |
341 | * | |
342 | * As a replacement for a 'word' entity within a 'phrase', for example, | |
343 | * one that precedes an address in a From, To, or Cc header. The ABNF | |
344 | * definition for 'phrase' from RFC 822 thus becomes: | |
345 | * | |
346 | * phrase = 1*( encoded-word / word ) | |
347 | * | |
348 | * In this case the set of characters that may be used in a "Q"-encoded | |
349 | * 'encoded-word' is restricted to: <upper and lower case ASCII | |
350 | * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_" | |
351 | * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a | |
352 | * 'phrase' MUST be separated from any adjacent 'word', 'text' or | |
353 | * 'special' by 'linear-white-space'. | |
354 | */ | |
355 | ||
356 | if (type != RFC2047_ADDRESS) | |
357 | return 0; | |
358 | ||
359 | /* '=' and '_' are special cases and have been checked above */ | |
360 | return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/'); | |
93fc05eb JS |
361 | } |
362 | ||
da55ff3d | 363 | static int needs_rfc2047_encoding(const char *line, int len) |
93fc05eb | 364 | { |
a1f6baa5 | 365 | int i; |
93fc05eb JS |
366 | |
367 | for (i = 0; i < len; i++) { | |
368 | int ch = line[i]; | |
c22e7de3 | 369 | if (non_ascii(ch) || ch == '\n') |
41dd00ba | 370 | return 1; |
93fc05eb | 371 | if ((i + 1 < len) && (ch == '=' && line[i+1] == '?')) |
41dd00ba | 372 | return 1; |
93fc05eb | 373 | } |
93fc05eb | 374 | |
41dd00ba JS |
375 | return 0; |
376 | } | |
377 | ||
6cd3c053 | 378 | static void add_rfc2047(struct strbuf *sb, const char *line, size_t len, |
41dd00ba JS |
379 | const char *encoding, enum rfc2047_type type) |
380 | { | |
381 | static const int max_encoded_length = 76; /* per rfc2047 */ | |
382 | int i; | |
383 | int line_len = last_line_length(sb); | |
384 | ||
93fc05eb JS |
385 | strbuf_grow(sb, len * 3 + strlen(encoding) + 100); |
386 | strbuf_addf(sb, "=?%s?q?", encoding); | |
a1f6baa5 | 387 | line_len += strlen(encoding) + 5; /* 5 for =??q? */ |
6cd3c053 KS |
388 | |
389 | while (len) { | |
390 | /* | |
391 | * RFC 2047, section 5 (3): | |
392 | * | |
393 | * Each 'encoded-word' MUST represent an integral number of | |
394 | * characters. A multi-octet character may not be split across | |
395 | * adjacent 'encoded- word's. | |
396 | */ | |
397 | const unsigned char *p = (const unsigned char *)line; | |
398 | int chrlen = mbs_chrlen(&line, &len, encoding); | |
399 | int is_special = (chrlen > 1) || is_rfc2047_special(*p, type); | |
400 | ||
401 | /* "=%02X" * chrlen, or the byte itself */ | |
402 | const char *encoded_fmt = is_special ? "=%02X" : "%c"; | |
403 | int encoded_len = is_special ? 3 * chrlen : 1; | |
94f6cdf6 JS |
404 | |
405 | /* | |
406 | * According to RFC 2047, we could encode the special character | |
407 | * ' ' (space) with '_' (underscore) for readability. But many | |
408 | * programs do not understand this and just leave the | |
409 | * underscore in place. Thus, we do nothing special here, which | |
410 | * causes ' ' to be encoded as '=20', avoiding this problem. | |
411 | */ | |
a1f6baa5 | 412 | |
6cd3c053 KS |
413 | if (line_len + encoded_len + 2 > max_encoded_length) { |
414 | /* It won't fit with trailing "?=" --- break the line */ | |
a1f6baa5 JK |
415 | strbuf_addf(sb, "?=\n =?%s?q?", encoding); |
416 | line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */ | |
417 | } | |
418 | ||
6cd3c053 KS |
419 | for (i = 0; i < chrlen; i++) |
420 | strbuf_addf(sb, encoded_fmt, p[i]); | |
421 | line_len += encoded_len; | |
93fc05eb | 422 | } |
93fc05eb JS |
423 | strbuf_addstr(sb, "?="); |
424 | } | |
425 | ||
d1053246 | 426 | const char *show_ident_date(const struct ident_split *ident, |
a5481a6c | 427 | const struct date_mode *mode) |
9dbe7c3d | 428 | { |
dddbad72 | 429 | timestamp_t date = 0; |
3f419d45 | 430 | long tz = 0; |
9dbe7c3d RS |
431 | |
432 | if (ident->date_begin && ident->date_end) | |
1aeb7e75 | 433 | date = parse_timestamp(ident->date_begin, NULL, 10); |
1dca155f JK |
434 | if (date_overflows(date)) |
435 | date = 0; | |
436 | else { | |
437 | if (ident->tz_begin && ident->tz_end) | |
438 | tz = strtol(ident->tz_begin, NULL, 10); | |
3f419d45 | 439 | if (tz >= INT_MAX || tz <= INT_MIN) |
1dca155f JK |
440 | tz = 0; |
441 | } | |
9dbe7c3d RS |
442 | return show_date(date, tz, mode); |
443 | } | |
444 | ||
6a5c3379 HM |
445 | static inline void strbuf_add_with_color(struct strbuf *sb, const char *color, |
446 | const char *buf, size_t buflen) | |
447 | { | |
448 | strbuf_addstr(sb, color); | |
449 | strbuf_add(sb, buf, buflen); | |
450 | if (*color) | |
451 | strbuf_addstr(sb, GIT_COLOR_RESET); | |
452 | } | |
453 | ||
454 | static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt, | |
455 | const char *line, size_t linelen, | |
456 | int color, enum grep_context ctx, | |
457 | enum grep_header_field field) | |
458 | { | |
459 | const char *buf, *eol, *line_color, *match_color; | |
460 | regmatch_t match; | |
461 | int eflags = 0; | |
462 | ||
463 | buf = line; | |
464 | eol = buf + linelen; | |
465 | ||
466 | if (!opt || !want_color(color) || opt->invert) | |
467 | goto end; | |
468 | ||
469 | line_color = opt->colors[GREP_COLOR_SELECTED]; | |
470 | match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; | |
471 | ||
472 | while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) { | |
473 | if (match.rm_so == match.rm_eo) | |
474 | break; | |
475 | ||
476 | strbuf_add_with_color(sb, line_color, buf, match.rm_so); | |
477 | strbuf_add_with_color(sb, match_color, buf + match.rm_so, | |
478 | match.rm_eo - match.rm_so); | |
479 | buf += match.rm_eo; | |
480 | eflags = REG_NOTBOL; | |
481 | } | |
482 | ||
483 | if (eflags) | |
484 | strbuf_add_with_color(sb, line_color, buf, eol - buf); | |
485 | else { | |
486 | end: | |
487 | strbuf_add(sb, buf, eol - buf); | |
488 | } | |
489 | } | |
490 | ||
b84d0139 JH |
491 | static int use_in_body_from(const struct pretty_print_context *pp, |
492 | const struct ident_split *ident) | |
493 | { | |
34bc1b10 JH |
494 | if (pp->rev && pp->rev->force_in_body_from) |
495 | return 1; | |
b84d0139 JH |
496 | if (ident_cmp(pp->from_ident, ident)) |
497 | return 1; | |
498 | return 0; | |
499 | } | |
500 | ||
10f2fbff | 501 | void pp_user_info(struct pretty_print_context *pp, |
6bf13944 JK |
502 | const char *what, struct strbuf *sb, |
503 | const char *line, const char *encoding) | |
93fc05eb | 504 | { |
3c020bd5 | 505 | struct ident_split ident; |
9dbe7c3d | 506 | char *line_end; |
dffd325f AP |
507 | const char *mailbuf, *namebuf; |
508 | size_t namelen, maillen; | |
41dd00ba | 509 | int max_length = 78; /* per rfc2822 */ |
93fc05eb | 510 | |
6bf13944 | 511 | if (pp->fmt == CMIT_FMT_ONELINE) |
93fc05eb | 512 | return; |
3c020bd5 | 513 | |
30e77bcb RS |
514 | line_end = strchrnul(line, '\n'); |
515 | if (split_ident_line(&ident, line, line_end - line)) | |
93fc05eb | 516 | return; |
3c020bd5 | 517 | |
dffd325f AP |
518 | mailbuf = ident.mail_begin; |
519 | maillen = ident.mail_end - ident.mail_begin; | |
520 | namebuf = ident.name_begin; | |
521 | namelen = ident.name_end - ident.name_begin; | |
522 | ||
523 | if (pp->mailmap) | |
524 | map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen); | |
525 | ||
9f23e040 | 526 | if (cmit_fmt_is_mail(pp->fmt)) { |
b84d0139 | 527 | if (pp->from_ident && use_in_body_from(pp, &ident)) { |
a9080475 JK |
528 | struct strbuf buf = STRBUF_INIT; |
529 | ||
530 | strbuf_addstr(&buf, "From: "); | |
531 | strbuf_add(&buf, namebuf, namelen); | |
532 | strbuf_addstr(&buf, " <"); | |
533 | strbuf_add(&buf, mailbuf, maillen); | |
534 | strbuf_addstr(&buf, ">\n"); | |
535 | string_list_append(&pp->in_body_headers, | |
536 | strbuf_detach(&buf, NULL)); | |
537 | ||
538 | mailbuf = pp->from_ident->mail_begin; | |
539 | maillen = pp->from_ident->mail_end - mailbuf; | |
540 | namebuf = pp->from_ident->name_begin; | |
541 | namelen = pp->from_ident->name_end - namebuf; | |
542 | } | |
543 | ||
93fc05eb | 544 | strbuf_addstr(sb, "From: "); |
19d097e3 EB |
545 | if (pp->encode_email_headers && |
546 | needs_rfc2047_encoding(namebuf, namelen)) { | |
a0511b39 | 547 | add_rfc2047(sb, namebuf, namelen, |
dffd325f | 548 | encoding, RFC2047_ADDRESS); |
41dd00ba | 549 | max_length = 76; /* per rfc2047 */ |
a0511b39 | 550 | } else if (needs_rfc822_quoting(namebuf, namelen)) { |
4d03c18a | 551 | struct strbuf quoted = STRBUF_INIT; |
a0511b39 | 552 | add_rfc822_quoted("ed, namebuf, namelen); |
41dd00ba JS |
553 | strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len, |
554 | -6, 1, max_length); | |
4d03c18a | 555 | strbuf_release("ed); |
41dd00ba | 556 | } else { |
a0511b39 | 557 | strbuf_add_wrapped_bytes(sb, namebuf, namelen, |
dffd325f | 558 | -6, 1, max_length); |
4d03c18a | 559 | } |
dffd325f | 560 | |
97a17e77 RS |
561 | if (max_length < |
562 | last_line_length(sb) + strlen(" <") + maillen + strlen(">")) | |
563 | strbuf_addch(sb, '\n'); | |
a0511b39 | 564 | strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf); |
93fc05eb | 565 | } else { |
6a5c3379 HM |
566 | struct strbuf id = STRBUF_INIT; |
567 | enum grep_header_field field = GREP_HEADER_FIELD_MAX; | |
568 | struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL; | |
569 | ||
570 | if (!strcmp(what, "Author")) | |
571 | field = GREP_HEADER_AUTHOR; | |
572 | else if (!strcmp(what, "Commit")) | |
573 | field = GREP_HEADER_COMMITTER; | |
574 | ||
575 | strbuf_addf(sb, "%s: ", what); | |
576 | if (pp->fmt == CMIT_FMT_FULLER) | |
577 | strbuf_addchars(sb, ' ', 4); | |
578 | ||
579 | strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf, | |
580 | (int)maillen, mailbuf); | |
581 | ||
582 | append_line_with_color(sb, opt, id.buf, id.len, pp->color, | |
583 | GREP_CONTEXT_HEAD, field); | |
584 | strbuf_addch(sb, '\n'); | |
585 | strbuf_release(&id); | |
93fc05eb | 586 | } |
dffd325f | 587 | |
6bf13944 | 588 | switch (pp->fmt) { |
93fc05eb | 589 | case CMIT_FMT_MEDIUM: |
9dbe7c3d | 590 | strbuf_addf(sb, "Date: %s\n", |
a5481a6c | 591 | show_ident_date(&ident, &pp->date_mode)); |
93fc05eb JS |
592 | break; |
593 | case CMIT_FMT_EMAIL: | |
9f23e040 | 594 | case CMIT_FMT_MBOXRD: |
9dbe7c3d | 595 | strbuf_addf(sb, "Date: %s\n", |
a5481a6c | 596 | show_ident_date(&ident, DATE_MODE(RFC2822))); |
93fc05eb JS |
597 | break; |
598 | case CMIT_FMT_FULLER: | |
9dbe7c3d | 599 | strbuf_addf(sb, "%sDate: %s\n", what, |
a5481a6c | 600 | show_ident_date(&ident, &pp->date_mode)); |
93fc05eb JS |
601 | break; |
602 | default: | |
603 | /* notin' */ | |
604 | break; | |
605 | } | |
606 | } | |
607 | ||
77356122 | 608 | static int is_blank_line(const char *line, int *len_p) |
93fc05eb JS |
609 | { |
610 | int len = *len_p; | |
35b2fa5b | 611 | while (len && isspace(line[len - 1])) |
93fc05eb JS |
612 | len--; |
613 | *len_p = len; | |
614 | return !len; | |
615 | } | |
616 | ||
77356122 | 617 | const char *skip_blank_lines(const char *msg) |
a0109668 RS |
618 | { |
619 | for (;;) { | |
620 | int linelen = get_one_line(msg); | |
621 | int ll = linelen; | |
622 | if (!linelen) | |
623 | break; | |
77356122 | 624 | if (!is_blank_line(msg, &ll)) |
a0109668 RS |
625 | break; |
626 | msg += linelen; | |
627 | } | |
628 | return msg; | |
629 | } | |
630 | ||
6bf13944 JK |
631 | static void add_merge_info(const struct pretty_print_context *pp, |
632 | struct strbuf *sb, const struct commit *commit) | |
93fc05eb JS |
633 | { |
634 | struct commit_list *parent = commit->parents; | |
635 | ||
9f23e040 | 636 | if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) || |
93fc05eb JS |
637 | !parent || !parent->next) |
638 | return; | |
639 | ||
640 | strbuf_addstr(sb, "Merge:"); | |
641 | ||
642 | while (parent) { | |
a94bb683 RS |
643 | struct object_id *oidp = &parent->item->object.oid; |
644 | strbuf_addch(sb, ' '); | |
6bf13944 | 645 | if (pp->abbrev) |
30e677e0 | 646 | strbuf_add_unique_abbrev(sb, oidp, pp->abbrev); |
a94bb683 RS |
647 | else |
648 | strbuf_addstr(sb, oid_to_hex(oidp)); | |
93fc05eb | 649 | parent = parent->next; |
93fc05eb JS |
650 | } |
651 | strbuf_addch(sb, '\n'); | |
652 | } | |
653 | ||
fe6eb7f2 | 654 | static char *get_header(const char *msg, const char *key) |
93fc05eb | 655 | { |
fe6eb7f2 JK |
656 | size_t len; |
657 | const char *v = find_commit_header(msg, key, &len); | |
658 | return v ? xmemdupz(v, len) : NULL; | |
93fc05eb JS |
659 | } |
660 | ||
661 | static char *replace_encoding_header(char *buf, const char *encoding) | |
662 | { | |
f285a2d7 | 663 | struct strbuf tmp = STRBUF_INIT; |
93fc05eb JS |
664 | size_t start, len; |
665 | char *cp = buf; | |
666 | ||
667 | /* guess if there is an encoding header before a \n\n */ | |
68d6d6eb | 668 | while (!starts_with(cp, "encoding ")) { |
93fc05eb JS |
669 | cp = strchr(cp, '\n'); |
670 | if (!cp || *++cp == '\n') | |
671 | return buf; | |
672 | } | |
673 | start = cp - buf; | |
674 | cp = strchr(cp, '\n'); | |
675 | if (!cp) | |
676 | return buf; /* should not happen but be defensive */ | |
677 | len = cp + 1 - (buf + start); | |
678 | ||
93fc05eb JS |
679 | strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); |
680 | if (is_encoding_utf8(encoding)) { | |
681 | /* we have re-coded to UTF-8; drop the header */ | |
682 | strbuf_remove(&tmp, start, len); | |
683 | } else { | |
684 | /* just replaces XXXX in 'encoding XXXX\n' */ | |
685 | strbuf_splice(&tmp, start + strlen("encoding "), | |
686 | len - strlen("encoding \n"), | |
687 | encoding, strlen(encoding)); | |
688 | } | |
689 | return strbuf_detach(&tmp, NULL); | |
690 | } | |
691 | ||
424510ed SB |
692 | const char *repo_logmsg_reencode(struct repository *r, |
693 | const struct commit *commit, | |
694 | char **commit_encoding, | |
695 | const char *output_encoding) | |
93fc05eb | 696 | { |
330db18c | 697 | static const char *utf8 = "UTF-8"; |
93fc05eb JS |
698 | const char *use_encoding; |
699 | char *encoding; | |
424510ed | 700 | const char *msg = repo_get_commit_buffer(r, commit, NULL); |
93fc05eb JS |
701 | char *out; |
702 | ||
5a10d236 NTND |
703 | if (!output_encoding || !*output_encoding) { |
704 | if (commit_encoding) | |
fe6eb7f2 | 705 | *commit_encoding = get_header(msg, "encoding"); |
dd0d388c | 706 | return msg; |
5a10d236 | 707 | } |
fe6eb7f2 | 708 | encoding = get_header(msg, "encoding"); |
5a10d236 NTND |
709 | if (commit_encoding) |
710 | *commit_encoding = encoding; | |
93fc05eb | 711 | use_encoding = encoding ? encoding : utf8; |
be5c9fb9 JK |
712 | if (same_encoding(use_encoding, output_encoding)) { |
713 | /* | |
714 | * No encoding work to be done. If we have no encoding header | |
715 | * at all, then there's nothing to do, and we can return the | |
716 | * message verbatim (whether newly allocated or not). | |
717 | */ | |
718 | if (!encoding) | |
719 | return msg; | |
720 | ||
721 | /* | |
722 | * Otherwise, we still want to munge the encoding header in the | |
723 | * result, which will be done by modifying the buffer. If we | |
724 | * are using a fresh copy, we can reuse it. But if we are using | |
b66103c3 JK |
725 | * the cached copy from get_commit_buffer, we need to duplicate it |
726 | * to avoid munging the cached copy. | |
be5c9fb9 | 727 | */ |
424510ed | 728 | if (msg == get_cached_commit_buffer(r, commit, NULL)) |
b66103c3 JK |
729 | out = xstrdup(msg); |
730 | else | |
731 | out = (char *)msg; | |
be5c9fb9 JK |
732 | } |
733 | else { | |
734 | /* | |
735 | * There's actual encoding work to do. Do the reencoding, which | |
736 | * still leaves the header to be replaced in the next step. At | |
737 | * this point, we are done with msg. If we allocated a fresh | |
738 | * copy, we can free it. | |
739 | */ | |
740 | out = reencode_string(msg, output_encoding, use_encoding); | |
b66103c3 | 741 | if (out) |
424510ed | 742 | repo_unuse_commit_buffer(r, commit, msg); |
be5c9fb9 JK |
743 | } |
744 | ||
745 | /* | |
746 | * This replacement actually consumes the buffer we hand it, so we do | |
747 | * not have to worry about freeing the old "out" here. | |
748 | */ | |
93fc05eb JS |
749 | if (out) |
750 | out = replace_encoding_header(out, output_encoding); | |
751 | ||
5a10d236 NTND |
752 | if (!commit_encoding) |
753 | free(encoding); | |
dd0d388c JK |
754 | /* |
755 | * If the re-encoding failed, out might be NULL here; in that | |
756 | * case we just return the commit message verbatim. | |
757 | */ | |
0988e665 | 758 | return out ? out : msg; |
dd0d388c JK |
759 | } |
760 | ||
ea02ffa3 AP |
761 | static int mailmap_name(const char **email, size_t *email_len, |
762 | const char **name, size_t *name_len) | |
e0cbc397 | 763 | { |
c455c87c | 764 | static struct string_list *mail_map; |
e0cbc397 | 765 | if (!mail_map) { |
ca56dadb | 766 | CALLOC_ARRAY(mail_map, 1); |
4e168333 | 767 | read_mailmap(mail_map); |
e0cbc397 | 768 | } |
d20d654f | 769 | return mail_map->nr && map_user(mail_map, email, email_len, name, name_len); |
e0cbc397 JS |
770 | } |
771 | ||
c3a670de | 772 | static size_t format_person_part(struct strbuf *sb, char part, |
a5481a6c JK |
773 | const char *msg, int len, |
774 | const struct date_mode *dmode) | |
93fc05eb | 775 | { |
c3a670de MC |
776 | /* currently all placeholders have same length */ |
777 | const int placeholder_len = 2; | |
4b340cfa | 778 | struct ident_split s; |
ea02ffa3 AP |
779 | const char *name, *mail; |
780 | size_t maillen, namelen; | |
93fc05eb | 781 | |
4b340cfa | 782 | if (split_ident_line(&s, msg, len) < 0) |
c3a670de MC |
783 | goto skip; |
784 | ||
ea02ffa3 AP |
785 | name = s.name_begin; |
786 | namelen = s.name_end - s.name_begin; | |
787 | mail = s.mail_begin; | |
788 | maillen = s.mail_end - s.mail_begin; | |
789 | ||
d8b8217c | 790 | if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */ |
ea02ffa3 | 791 | mailmap_name(&mail, &maillen, &name, &namelen); |
e0cbc397 | 792 | if (part == 'n' || part == 'N') { /* name */ |
ea02ffa3 | 793 | strbuf_add(sb, name, namelen); |
c3a670de | 794 | return placeholder_len; |
cde75e59 | 795 | } |
d20d654f | 796 | if (part == 'e' || part == 'E') { /* email */ |
ea02ffa3 | 797 | strbuf_add(sb, mail, maillen); |
c3a670de | 798 | return placeholder_len; |
cde75e59 | 799 | } |
d8b8217c PB |
800 | if (part == 'l' || part == 'L') { /* local-part */ |
801 | const char *at = memchr(mail, '@', maillen); | |
802 | if (at) | |
803 | maillen = at - mail; | |
804 | strbuf_add(sb, mail, maillen); | |
805 | return placeholder_len; | |
806 | } | |
93fc05eb | 807 | |
4b340cfa | 808 | if (!s.date_begin) |
c3a670de | 809 | goto skip; |
93fc05eb | 810 | |
cde75e59 | 811 | if (part == 't') { /* date, UNIX timestamp */ |
4b340cfa | 812 | strbuf_add(sb, s.date_begin, s.date_end - s.date_begin); |
c3a670de | 813 | return placeholder_len; |
cde75e59 | 814 | } |
93fc05eb | 815 | |
cde75e59 RS |
816 | switch (part) { |
817 | case 'd': /* date */ | |
9dbe7c3d | 818 | strbuf_addstr(sb, show_ident_date(&s, dmode)); |
c3a670de | 819 | return placeholder_len; |
cde75e59 | 820 | case 'D': /* date, RFC2822 style */ |
a5481a6c | 821 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822))); |
c3a670de | 822 | return placeholder_len; |
cde75e59 | 823 | case 'r': /* date, relative */ |
a5481a6c | 824 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE))); |
c3a670de | 825 | return placeholder_len; |
466fb674 | 826 | case 'i': /* date, ISO 8601-like */ |
a5481a6c | 827 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601))); |
c3a670de | 828 | return placeholder_len; |
466fb674 | 829 | case 'I': /* date, ISO 8601 strict */ |
a5481a6c | 830 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT))); |
466fb674 | 831 | return placeholder_len; |
b722d456 ZH |
832 | case 'h': /* date, human */ |
833 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(HUMAN))); | |
834 | return placeholder_len; | |
0df62117 RS |
835 | case 's': |
836 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT))); | |
837 | return placeholder_len; | |
cde75e59 | 838 | } |
c3a670de MC |
839 | |
840 | skip: | |
841 | /* | |
4b340cfa JH |
842 | * reading from either a bogus commit, or a reflog entry with |
843 | * %gn, %ge, etc.; 'sb' cannot be updated, but we still need | |
844 | * to compute a valid return value. | |
c3a670de MC |
845 | */ |
846 | if (part == 'n' || part == 'e' || part == 't' || part == 'd' | |
847 | || part == 'D' || part == 'r' || part == 'i') | |
848 | return placeholder_len; | |
849 | ||
850 | return 0; /* unknown placeholder */ | |
93fc05eb JS |
851 | } |
852 | ||
f29d5958 RS |
853 | struct chunk { |
854 | size_t off; | |
855 | size_t len; | |
856 | }; | |
857 | ||
a5752342 NTND |
858 | enum flush_type { |
859 | no_flush, | |
860 | flush_right, | |
861 | flush_left, | |
1640632b | 862 | flush_left_and_steal, |
a5752342 NTND |
863 | flush_both |
864 | }; | |
865 | ||
a7f01c6b NTND |
866 | enum trunc_type { |
867 | trunc_none, | |
868 | trunc_left, | |
869 | trunc_middle, | |
870 | trunc_right | |
871 | }; | |
872 | ||
f29d5958 | 873 | struct format_commit_context { |
018b9deb | 874 | struct repository *repository; |
f29d5958 | 875 | const struct commit *commit; |
dd2e794a | 876 | const struct pretty_print_context *pretty_ctx; |
f53bd743 RS |
877 | unsigned commit_header_parsed:1; |
878 | unsigned commit_message_parsed:1; | |
ffb6d7d5 | 879 | struct signature_check signature_check; |
a5752342 | 880 | enum flush_type flush_type; |
a7f01c6b | 881 | enum trunc_type truncate; |
b000c59b | 882 | const char *message; |
0940a76d | 883 | char *commit_encoding; |
02edd56b | 884 | size_t width, indent1, indent2; |
a95f067e | 885 | int auto_color; |
a5752342 | 886 | int padding; |
f29d5958 RS |
887 | |
888 | /* These offsets are relative to the start of the commit message. */ | |
f29d5958 RS |
889 | struct chunk author; |
890 | struct chunk committer; | |
f53bd743 RS |
891 | size_t message_off; |
892 | size_t subject_off; | |
f29d5958 | 893 | size_t body_off; |
b9c62321 RS |
894 | |
895 | /* The following ones are relative to the result struct strbuf. */ | |
02edd56b | 896 | size_t wrap_start; |
f29d5958 RS |
897 | }; |
898 | ||
899 | static void parse_commit_header(struct format_commit_context *context) | |
93fc05eb | 900 | { |
177b29dc | 901 | const char *msg = context->message; |
93fc05eb | 902 | int i; |
f29d5958 | 903 | |
f53bd743 | 904 | for (i = 0; msg[i]; i++) { |
e3f1da98 | 905 | const char *name; |
f29d5958 RS |
906 | int eol; |
907 | for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) | |
908 | ; /* do nothing */ | |
909 | ||
f29d5958 | 910 | if (i == eol) { |
f53bd743 | 911 | break; |
e3f1da98 RS |
912 | } else if (skip_prefix(msg + i, "author ", &name)) { |
913 | context->author.off = name - msg; | |
914 | context->author.len = msg + eol - name; | |
915 | } else if (skip_prefix(msg + i, "committer ", &name)) { | |
916 | context->committer.off = name - msg; | |
917 | context->committer.len = msg + eol - name; | |
f29d5958 RS |
918 | } |
919 | i = eol; | |
920 | } | |
f53bd743 | 921 | context->message_off = i; |
f29d5958 RS |
922 | context->commit_header_parsed = 1; |
923 | } | |
924 | ||
46d164b0 SB |
925 | static int istitlechar(char c) |
926 | { | |
927 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
928 | (c >= '0' && c <= '9') || c == '.' || c == '_'; | |
929 | } | |
930 | ||
47d4676a | 931 | void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len) |
46d164b0 SB |
932 | { |
933 | size_t trimlen; | |
871d21d4 | 934 | size_t start_len = sb->len; |
46d164b0 | 935 | int space = 2; |
47d4676a | 936 | int i; |
46d164b0 | 937 | |
47d4676a HV |
938 | for (i = 0; i < len; i++) { |
939 | if (istitlechar(msg[i])) { | |
46d164b0 SB |
940 | if (space == 1) |
941 | strbuf_addch(sb, '-'); | |
942 | space = 0; | |
47d4676a HV |
943 | strbuf_addch(sb, msg[i]); |
944 | if (msg[i] == '.') | |
945 | while (msg[i+1] == '.') | |
946 | i++; | |
46d164b0 SB |
947 | } else |
948 | space |= 1; | |
949 | } | |
950 | ||
951 | /* trim any trailing '.' or '-' characters */ | |
952 | trimlen = 0; | |
871d21d4 SB |
953 | while (sb->len - trimlen > start_len && |
954 | (sb->buf[sb->len - 1 - trimlen] == '.' | |
955 | || sb->buf[sb->len - 1 - trimlen] == '-')) | |
46d164b0 SB |
956 | trimlen++; |
957 | strbuf_remove(sb, sb->len - trimlen, trimlen); | |
958 | } | |
959 | ||
cec08717 RS |
960 | const char *format_subject(struct strbuf *sb, const char *msg, |
961 | const char *line_separator) | |
88c44735 RS |
962 | { |
963 | int first = 1; | |
964 | ||
965 | for (;;) { | |
966 | const char *line = msg; | |
967 | int linelen = get_one_line(line); | |
968 | ||
969 | msg += linelen; | |
77356122 | 970 | if (!linelen || is_blank_line(line, &linelen)) |
88c44735 RS |
971 | break; |
972 | ||
f53bd743 RS |
973 | if (!sb) |
974 | continue; | |
88c44735 RS |
975 | strbuf_grow(sb, linelen + 2); |
976 | if (!first) | |
977 | strbuf_addstr(sb, line_separator); | |
978 | strbuf_add(sb, line, linelen); | |
979 | first = 0; | |
980 | } | |
981 | return msg; | |
982 | } | |
983 | ||
f53bd743 RS |
984 | static void parse_commit_message(struct format_commit_context *c) |
985 | { | |
177b29dc PN |
986 | const char *msg = c->message + c->message_off; |
987 | const char *start = c->message; | |
f53bd743 | 988 | |
77356122 | 989 | msg = skip_blank_lines(msg); |
f53bd743 RS |
990 | c->subject_off = msg - start; |
991 | ||
992 | msg = format_subject(NULL, msg, NULL); | |
77356122 | 993 | msg = skip_blank_lines(msg); |
f53bd743 RS |
994 | c->body_off = msg - start; |
995 | ||
996 | c->commit_message_parsed = 1; | |
997 | } | |
998 | ||
02edd56b RS |
999 | static void strbuf_wrap(struct strbuf *sb, size_t pos, |
1000 | size_t width, size_t indent1, size_t indent2) | |
1001 | { | |
1002 | struct strbuf tmp = STRBUF_INIT; | |
1003 | ||
1004 | if (pos) | |
1005 | strbuf_add(&tmp, sb->buf, pos); | |
1006 | strbuf_add_wrapped_text(&tmp, sb->buf + pos, | |
48050c42 PS |
1007 | cast_size_t_to_int(indent1), |
1008 | cast_size_t_to_int(indent2), | |
1009 | cast_size_t_to_int(width)); | |
02edd56b RS |
1010 | strbuf_swap(&tmp, sb); |
1011 | strbuf_release(&tmp); | |
1012 | } | |
1013 | ||
1014 | static void rewrap_message_tail(struct strbuf *sb, | |
1015 | struct format_commit_context *c, | |
1016 | size_t new_width, size_t new_indent1, | |
1017 | size_t new_indent2) | |
1018 | { | |
1019 | if (c->width == new_width && c->indent1 == new_indent1 && | |
1020 | c->indent2 == new_indent2) | |
1021 | return; | |
32ca4249 | 1022 | if (c->wrap_start < sb->len) |
02edd56b RS |
1023 | strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2); |
1024 | c->wrap_start = sb->len; | |
1025 | c->width = new_width; | |
1026 | c->indent1 = new_indent1; | |
1027 | c->indent2 = new_indent2; | |
1028 | } | |
1029 | ||
cd1957f5 JK |
1030 | static int format_reflog_person(struct strbuf *sb, |
1031 | char part, | |
1032 | struct reflog_walk_info *log, | |
a5481a6c | 1033 | const struct date_mode *dmode) |
cd1957f5 JK |
1034 | { |
1035 | const char *ident; | |
1036 | ||
1037 | if (!log) | |
1038 | return 2; | |
1039 | ||
1040 | ident = get_reflog_ident(log); | |
1041 | if (!ident) | |
1042 | return 2; | |
1043 | ||
1044 | return format_person_part(sb, part, ident, strlen(ident), dmode); | |
1045 | } | |
1046 | ||
fcabc2d9 NTND |
1047 | static size_t parse_color(struct strbuf *sb, /* in UTF-8 */ |
1048 | const char *placeholder, | |
1049 | struct format_commit_context *c) | |
1050 | { | |
e3f1da98 | 1051 | const char *rest = placeholder; |
18fb7ffc | 1052 | const char *basic_color = NULL; |
e3f1da98 | 1053 | |
fcabc2d9 NTND |
1054 | if (placeholder[1] == '(') { |
1055 | const char *begin = placeholder + 2; | |
1056 | const char *end = strchr(begin, ')'); | |
1057 | char color[COLOR_MAXLEN]; | |
1058 | ||
1059 | if (!end) | |
1060 | return 0; | |
18fb7ffc | 1061 | |
e3f1da98 | 1062 | if (skip_prefix(begin, "auto,", &begin)) { |
fcabc2d9 NTND |
1063 | if (!want_color(c->pretty_ctx->color)) |
1064 | return end - placeholder + 1; | |
18fb7ffc JK |
1065 | } else if (skip_prefix(begin, "always,", &begin)) { |
1066 | /* nothing to do; we do not respect want_color at all */ | |
1067 | } else { | |
1068 | /* the default is the same as "auto" */ | |
1069 | if (!want_color(c->pretty_ctx->color)) | |
1070 | return end - placeholder + 1; | |
fcabc2d9 | 1071 | } |
18fb7ffc | 1072 | |
f6c5a296 JK |
1073 | if (color_parse_mem(begin, end - begin, color) < 0) |
1074 | die(_("unable to parse --pretty format")); | |
fcabc2d9 NTND |
1075 | strbuf_addstr(sb, color); |
1076 | return end - placeholder + 1; | |
1077 | } | |
18fb7ffc JK |
1078 | |
1079 | /* | |
1080 | * We handle things like "%C(red)" above; for historical reasons, there | |
1081 | * are a few colors that can be specified without parentheses (and | |
1082 | * they cannot support things like "auto" or "always" at all). | |
1083 | */ | |
e3f1da98 | 1084 | if (skip_prefix(placeholder + 1, "red", &rest)) |
18fb7ffc | 1085 | basic_color = GIT_COLOR_RED; |
e3f1da98 | 1086 | else if (skip_prefix(placeholder + 1, "green", &rest)) |
18fb7ffc | 1087 | basic_color = GIT_COLOR_GREEN; |
e3f1da98 | 1088 | else if (skip_prefix(placeholder + 1, "blue", &rest)) |
18fb7ffc | 1089 | basic_color = GIT_COLOR_BLUE; |
e3f1da98 | 1090 | else if (skip_prefix(placeholder + 1, "reset", &rest)) |
18fb7ffc JK |
1091 | basic_color = GIT_COLOR_RESET; |
1092 | ||
1093 | if (basic_color && want_color(c->pretty_ctx->color)) | |
1094 | strbuf_addstr(sb, basic_color); | |
1095 | ||
e3f1da98 | 1096 | return rest - placeholder; |
fcabc2d9 NTND |
1097 | } |
1098 | ||
9a1180fc | 1099 | static size_t parse_padding_placeholder(const char *placeholder, |
a5752342 NTND |
1100 | struct format_commit_context *c) |
1101 | { | |
1102 | const char *ch = placeholder; | |
1103 | enum flush_type flush_type; | |
1104 | int to_column = 0; | |
1105 | ||
1106 | switch (*ch++) { | |
1107 | case '<': | |
1108 | flush_type = flush_right; | |
1109 | break; | |
1110 | case '>': | |
1111 | if (*ch == '<') { | |
1112 | flush_type = flush_both; | |
1113 | ch++; | |
1640632b NTND |
1114 | } else if (*ch == '>') { |
1115 | flush_type = flush_left_and_steal; | |
1116 | ch++; | |
a5752342 NTND |
1117 | } else |
1118 | flush_type = flush_left; | |
1119 | break; | |
1120 | default: | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | /* the next value means "wide enough to that column" */ | |
1125 | if (*ch == '|') { | |
1126 | to_column = 1; | |
1127 | ch++; | |
1128 | } | |
1129 | ||
1130 | if (*ch == '(') { | |
1131 | const char *start = ch + 1; | |
a7f01c6b | 1132 | const char *end = start + strcspn(start, ",)"); |
a5752342 NTND |
1133 | char *next; |
1134 | int width; | |
f6e0b9f3 | 1135 | if (!*end || end == start) |
a5752342 | 1136 | return 0; |
066790d7 | 1137 | width = strtol(start, &next, 10); |
304a50ad PS |
1138 | |
1139 | /* | |
1140 | * We need to limit the amount of padding, or otherwise this | |
1141 | * would allow the user to pad the buffer by arbitrarily many | |
1142 | * bytes and thus cause resource exhaustion. | |
1143 | */ | |
1144 | if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT) | |
1145 | return 0; | |
1146 | ||
a5752342 NTND |
1147 | if (next == start || width == 0) |
1148 | return 0; | |
066790d7 NTND |
1149 | if (width < 0) { |
1150 | if (to_column) | |
1151 | width += term_columns(); | |
1152 | if (width < 0) | |
1153 | return 0; | |
1154 | } | |
a5752342 NTND |
1155 | c->padding = to_column ? -width : width; |
1156 | c->flush_type = flush_type; | |
a7f01c6b NTND |
1157 | |
1158 | if (*end == ',') { | |
1159 | start = end + 1; | |
1160 | end = strchr(start, ')'); | |
1161 | if (!end || end == start) | |
1162 | return 0; | |
59556548 | 1163 | if (starts_with(start, "trunc)")) |
a7f01c6b | 1164 | c->truncate = trunc_right; |
59556548 | 1165 | else if (starts_with(start, "ltrunc)")) |
a7f01c6b | 1166 | c->truncate = trunc_left; |
59556548 | 1167 | else if (starts_with(start, "mtrunc)")) |
a7f01c6b NTND |
1168 | c->truncate = trunc_middle; |
1169 | else | |
1170 | return 0; | |
1171 | } else | |
1172 | c->truncate = trunc_none; | |
1173 | ||
a5752342 NTND |
1174 | return end - placeholder + 1; |
1175 | } | |
1176 | return 0; | |
1177 | } | |
1178 | ||
4f732e0f AW |
1179 | static int match_placeholder_arg_value(const char *to_parse, const char *candidate, |
1180 | const char **end, const char **valuestart, | |
1181 | size_t *valuelen) | |
84ff053d TB |
1182 | { |
1183 | const char *p; | |
1184 | ||
1185 | if (!(skip_prefix(to_parse, candidate, &p))) | |
1186 | return 0; | |
4f732e0f AW |
1187 | if (valuestart) { |
1188 | if (*p == '=') { | |
1189 | *valuestart = p + 1; | |
1190 | *valuelen = strcspn(*valuestart, ",)"); | |
1191 | p = *valuestart + *valuelen; | |
1192 | } else { | |
1193 | if (*p != ',' && *p != ')') | |
1194 | return 0; | |
1195 | *valuestart = NULL; | |
1196 | *valuelen = 0; | |
1197 | } | |
1198 | } | |
84ff053d TB |
1199 | if (*p == ',') { |
1200 | *end = p + 1; | |
1201 | return 1; | |
1202 | } | |
1203 | if (*p == ')') { | |
1204 | *end = p; | |
1205 | return 1; | |
1206 | } | |
1207 | return 0; | |
1208 | } | |
1209 | ||
4f732e0f AW |
1210 | static int match_placeholder_bool_arg(const char *to_parse, const char *candidate, |
1211 | const char **end, int *val) | |
1212 | { | |
1213 | const char *argval; | |
1214 | char *strval; | |
1215 | size_t arglen; | |
1216 | int v; | |
1217 | ||
1218 | if (!match_placeholder_arg_value(to_parse, candidate, end, &argval, &arglen)) | |
1219 | return 0; | |
1220 | ||
1221 | if (!argval) { | |
1222 | *val = 1; | |
1223 | return 1; | |
1224 | } | |
1225 | ||
1226 | strval = xstrndup(argval, arglen); | |
1227 | v = git_parse_maybe_bool(strval); | |
1228 | free(strval); | |
1229 | ||
1230 | if (v == -1) | |
1231 | return 0; | |
1232 | ||
1233 | *val = v; | |
1234 | ||
1235 | return 1; | |
1236 | } | |
1237 | ||
250bea0c AW |
1238 | static int format_trailer_match_cb(const struct strbuf *key, void *ud) |
1239 | { | |
1240 | const struct string_list *list = ud; | |
1241 | const struct string_list_item *item; | |
1242 | ||
1243 | for_each_string_list_item (item, list) { | |
1244 | if (key->len == (uintptr_t)item->util && | |
1245 | !strncasecmp(item->string, key->buf, key->len)) | |
1246 | return 1; | |
1247 | } | |
1248 | return 0; | |
1249 | } | |
1250 | ||
90563aed HV |
1251 | int format_set_trailers_options(struct process_trailer_options *opts, |
1252 | struct string_list *filter_list, | |
1253 | struct strbuf *sepbuf, | |
1254 | struct strbuf *kvsepbuf, | |
636a0aee HV |
1255 | const char **arg, |
1256 | char **invalid_arg) | |
90563aed HV |
1257 | { |
1258 | for (;;) { | |
1259 | const char *argval; | |
1260 | size_t arglen; | |
1261 | ||
636a0aee HV |
1262 | if (**arg == ')') |
1263 | break; | |
1264 | ||
90563aed HV |
1265 | if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) { |
1266 | uintptr_t len = arglen; | |
1267 | ||
1268 | if (!argval) | |
1269 | return -1; | |
1270 | ||
1271 | if (len && argval[len - 1] == ':') | |
1272 | len--; | |
1273 | string_list_append(filter_list, argval)->util = (char *)len; | |
1274 | ||
1275 | opts->filter = format_trailer_match_cb; | |
1276 | opts->filter_data = filter_list; | |
1277 | opts->only_trailers = 1; | |
1278 | } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) { | |
1279 | char *fmt; | |
1280 | ||
1281 | strbuf_reset(sepbuf); | |
1282 | fmt = xstrndup(argval, arglen); | |
1283 | strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL); | |
1284 | free(fmt); | |
1285 | opts->separator = sepbuf; | |
1286 | } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) { | |
1287 | char *fmt; | |
1288 | ||
1289 | strbuf_reset(kvsepbuf); | |
1290 | fmt = xstrndup(argval, arglen); | |
1291 | strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL); | |
1292 | free(fmt); | |
1293 | opts->key_value_separator = kvsepbuf; | |
1294 | } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) && | |
1295 | !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) && | |
1296 | !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) && | |
636a0aee HV |
1297 | !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) { |
1298 | if (invalid_arg) { | |
1299 | size_t len = strcspn(*arg, ",)"); | |
1300 | *invalid_arg = xstrndup(*arg, len); | |
1301 | } | |
1302 | return -1; | |
1303 | } | |
90563aed HV |
1304 | } |
1305 | return 0; | |
1306 | } | |
1307 | ||
b081547e RS |
1308 | static size_t parse_describe_args(const char *start, struct strvec *args) |
1309 | { | |
3c6eb4ec ES |
1310 | struct { |
1311 | char *name; | |
1312 | enum { | |
1d517cea | 1313 | DESCRIBE_ARG_BOOL, |
eccd97d0 | 1314 | DESCRIBE_ARG_INTEGER, |
3c6eb4ec ES |
1315 | DESCRIBE_ARG_STRING, |
1316 | } type; | |
1317 | } option[] = { | |
1d517cea | 1318 | { "tags", DESCRIBE_ARG_BOOL}, |
eccd97d0 | 1319 | { "abbrev", DESCRIBE_ARG_INTEGER }, |
3c6eb4ec ES |
1320 | { "exclude", DESCRIBE_ARG_STRING }, |
1321 | { "match", DESCRIBE_ARG_STRING }, | |
1322 | }; | |
b081547e RS |
1323 | const char *arg = start; |
1324 | ||
1325 | for (;;) { | |
3c6eb4ec | 1326 | int found = 0; |
b081547e RS |
1327 | const char *argval; |
1328 | size_t arglen = 0; | |
1d517cea | 1329 | int optval = 0; |
b081547e RS |
1330 | int i; |
1331 | ||
3c6eb4ec ES |
1332 | for (i = 0; !found && i < ARRAY_SIZE(option); i++) { |
1333 | switch (option[i].type) { | |
1d517cea ES |
1334 | case DESCRIBE_ARG_BOOL: |
1335 | if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) { | |
1336 | if (optval) | |
1337 | strvec_pushf(args, "--%s", option[i].name); | |
1338 | else | |
1339 | strvec_pushf(args, "--no-%s", option[i].name); | |
1340 | found = 1; | |
1341 | } | |
1342 | break; | |
eccd97d0 ES |
1343 | case DESCRIBE_ARG_INTEGER: |
1344 | if (match_placeholder_arg_value(arg, option[i].name, &arg, | |
1345 | &argval, &arglen)) { | |
1346 | char *endptr; | |
1347 | if (!arglen) | |
1348 | return 0; | |
1349 | strtol(argval, &endptr, 10); | |
1350 | if (endptr - argval != arglen) | |
1351 | return 0; | |
1352 | strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval); | |
1353 | found = 1; | |
1354 | } | |
1355 | break; | |
3c6eb4ec ES |
1356 | case DESCRIBE_ARG_STRING: |
1357 | if (match_placeholder_arg_value(arg, option[i].name, &arg, | |
1358 | &argval, &arglen)) { | |
1359 | if (!arglen) | |
1360 | return 0; | |
1361 | strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval); | |
1362 | found = 1; | |
1363 | } | |
b081547e RS |
1364 | break; |
1365 | } | |
1366 | } | |
3c6eb4ec | 1367 | if (!found) |
b081547e RS |
1368 | break; |
1369 | ||
b081547e RS |
1370 | } |
1371 | return arg - start; | |
1372 | } | |
1373 | ||
7e77df39 NTND |
1374 | static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ |
1375 | const char *placeholder, | |
9fa708da | 1376 | void *context) |
f29d5958 RS |
1377 | { |
1378 | struct format_commit_context *c = context; | |
1379 | const struct commit *commit = c->commit; | |
177b29dc | 1380 | const char *msg = c->message; |
f29d5958 | 1381 | struct commit_list *p; |
47d4676a | 1382 | const char *arg, *eol; |
fd2015b3 | 1383 | size_t res; |
ad6f028f | 1384 | char **slot; |
93fc05eb | 1385 | |
93fc05eb | 1386 | /* these are independent of the commit */ |
fd2015b3 AW |
1387 | res = strbuf_expand_literal_cb(sb, placeholder, NULL); |
1388 | if (res) | |
1389 | return res; | |
1390 | ||
cde75e59 RS |
1391 | switch (placeholder[0]) { |
1392 | case 'C': | |
59556548 | 1393 | if (starts_with(placeholder + 1, "(auto)")) { |
b15a3e00 | 1394 | c->auto_color = want_color(c->pretty_ctx->color); |
82b83da8 | 1395 | if (c->auto_color && sb->len) |
c99ad274 | 1396 | strbuf_addstr(sb, GIT_COLOR_RESET); |
a95f067e NTND |
1397 | return 7; /* consumed 7 bytes, "C(auto)" */ |
1398 | } else { | |
1399 | int ret = parse_color(sb, placeholder, c); | |
1400 | if (ret) | |
1401 | c->auto_color = 0; | |
1402 | /* | |
1403 | * Otherwise, we decided to treat %C<unknown> | |
1404 | * as a literal string, and the previous | |
1405 | * %C(auto) is still valid. | |
1406 | */ | |
1407 | return ret; | |
c002922a | 1408 | } |
02edd56b RS |
1409 | case 'w': |
1410 | if (placeholder[1] == '(') { | |
1411 | unsigned long width = 0, indent1 = 0, indent2 = 0; | |
1412 | char *next; | |
1413 | const char *start = placeholder + 2; | |
1414 | const char *end = strchr(start, ')'); | |
1415 | if (!end) | |
1416 | return 0; | |
1417 | if (end > start) { | |
1418 | width = strtoul(start, &next, 10); | |
1419 | if (*next == ',') { | |
1420 | indent1 = strtoul(next + 1, &next, 10); | |
1421 | if (*next == ',') { | |
1422 | indent2 = strtoul(next + 1, | |
1423 | &next, 10); | |
1424 | } | |
1425 | } | |
1426 | if (*next != ')') | |
1427 | return 0; | |
1428 | } | |
304a50ad PS |
1429 | |
1430 | /* | |
1431 | * We need to limit the format here as it allows the | |
1432 | * user to prepend arbitrarily many bytes to the buffer | |
1433 | * when rewrapping. | |
1434 | */ | |
1435 | if (width > FORMATTING_LIMIT || | |
1436 | indent1 > FORMATTING_LIMIT || | |
1437 | indent2 > FORMATTING_LIMIT) | |
1438 | return 0; | |
02edd56b RS |
1439 | rewrap_message_tail(sb, c, width, indent1, indent2); |
1440 | return end - placeholder + 1; | |
1441 | } else | |
1442 | return 0; | |
a5752342 NTND |
1443 | |
1444 | case '<': | |
1445 | case '>': | |
9a1180fc | 1446 | return parse_padding_placeholder(placeholder, c); |
cde75e59 | 1447 | } |
93fc05eb | 1448 | |
b081547e | 1449 | if (skip_prefix(placeholder, "(describe", &arg)) { |
15ae82d5 RS |
1450 | struct child_process cmd = CHILD_PROCESS_INIT; |
1451 | struct strbuf out = STRBUF_INIT; | |
1452 | struct strbuf err = STRBUF_INIT; | |
96099726 RS |
1453 | struct pretty_print_describe_status *describe_status; |
1454 | ||
1455 | describe_status = c->pretty_ctx->describe_status; | |
1456 | if (describe_status) { | |
1457 | if (!describe_status->max_invocations) | |
1458 | return 0; | |
1459 | describe_status->max_invocations--; | |
1460 | } | |
15ae82d5 RS |
1461 | |
1462 | cmd.git_cmd = 1; | |
1463 | strvec_push(&cmd.args, "describe"); | |
b081547e RS |
1464 | |
1465 | if (*arg == ':') { | |
1466 | arg++; | |
1467 | arg += parse_describe_args(arg, &cmd.args); | |
1468 | } | |
1469 | ||
1470 | if (*arg != ')') { | |
1471 | child_process_clear(&cmd); | |
1472 | return 0; | |
1473 | } | |
1474 | ||
15ae82d5 RS |
1475 | strvec_push(&cmd.args, oid_to_hex(&commit->object.oid)); |
1476 | pipe_command(&cmd, NULL, 0, &out, 0, &err, 0); | |
1477 | strbuf_rtrim(&out); | |
1478 | strbuf_addbuf(sb, &out); | |
1479 | strbuf_release(&out); | |
1480 | strbuf_release(&err); | |
b081547e | 1481 | return arg - placeholder + 1; |
15ae82d5 RS |
1482 | } |
1483 | ||
93fc05eb JS |
1484 | /* these depend on the commit */ |
1485 | if (!commit->object.parsed) | |
109cd76d | 1486 | parse_object(the_repository, &commit->object.oid); |
93fc05eb | 1487 | |
cde75e59 RS |
1488 | switch (placeholder[0]) { |
1489 | case 'H': /* commit hash */ | |
a95f067e | 1490 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT)); |
f2fd0760 | 1491 | strbuf_addstr(sb, oid_to_hex(&commit->object.oid)); |
a95f067e | 1492 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET)); |
c3a670de | 1493 | return 1; |
cde75e59 | 1494 | case 'h': /* abbreviated commit hash */ |
a95f067e | 1495 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT)); |
30e677e0 | 1496 | strbuf_add_unique_abbrev(sb, &commit->object.oid, |
1eb47f16 | 1497 | c->pretty_ctx->abbrev); |
a95f067e | 1498 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET)); |
c3a670de | 1499 | return 1; |
cde75e59 | 1500 | case 'T': /* tree hash */ |
2e27bd77 | 1501 | strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit))); |
c3a670de | 1502 | return 1; |
cde75e59 | 1503 | case 't': /* abbreviated tree hash */ |
2e27bd77 | 1504 | strbuf_add_unique_abbrev(sb, |
c89b6e13 | 1505 | get_commit_tree_oid(commit), |
1eb47f16 | 1506 | c->pretty_ctx->abbrev); |
c3a670de | 1507 | return 1; |
cde75e59 RS |
1508 | case 'P': /* parent hashes */ |
1509 | for (p = commit->parents; p; p = p->next) { | |
1510 | if (p != commit->parents) | |
1511 | strbuf_addch(sb, ' '); | |
f2fd0760 | 1512 | strbuf_addstr(sb, oid_to_hex(&p->item->object.oid)); |
cde75e59 | 1513 | } |
c3a670de | 1514 | return 1; |
cde75e59 RS |
1515 | case 'p': /* abbreviated parent hashes */ |
1516 | for (p = commit->parents; p; p = p->next) { | |
1517 | if (p != commit->parents) | |
1518 | strbuf_addch(sb, ' '); | |
30e677e0 | 1519 | strbuf_add_unique_abbrev(sb, &p->item->object.oid, |
1eb47f16 | 1520 | c->pretty_ctx->abbrev); |
cde75e59 | 1521 | } |
c3a670de | 1522 | return 1; |
cde75e59 | 1523 | case 'm': /* left/right/bottom */ |
1df2d656 | 1524 | strbuf_addstr(sb, get_revision_mark(NULL, commit)); |
c3a670de | 1525 | return 1; |
3b3d443f | 1526 | case 'd': |
a95f067e | 1527 | format_decorations(sb, commit, c->auto_color); |
3b3d443f | 1528 | return 1; |
9271095c | 1529 | case 'D': |
9271095c HJ |
1530 | format_decorations_extended(sb, commit, c->auto_color, "", ", ", ""); |
1531 | return 1; | |
ad6f028f IT |
1532 | case 'S': /* tag/branch like --source */ |
1533 | if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources)) | |
1534 | return 0; | |
1535 | slot = revision_sources_at(c->pretty_ctx->rev->sources, commit); | |
1536 | if (!(slot && *slot)) | |
1537 | return 0; | |
1538 | strbuf_addstr(sb, *slot); | |
1539 | return 1; | |
8f8f5476 TR |
1540 | case 'g': /* reflog info */ |
1541 | switch(placeholder[1]) { | |
1542 | case 'd': /* reflog selector */ | |
1543 | case 'D': | |
1544 | if (c->pretty_ctx->reflog_info) | |
1545 | get_reflog_selector(sb, | |
1546 | c->pretty_ctx->reflog_info, | |
a5481a6c | 1547 | &c->pretty_ctx->date_mode, |
55ccf85a | 1548 | c->pretty_ctx->date_mode_explicit, |
8f8f5476 TR |
1549 | (placeholder[1] == 'd')); |
1550 | return 2; | |
1551 | case 's': /* reflog message */ | |
1552 | if (c->pretty_ctx->reflog_info) | |
1553 | get_reflog_message(sb, c->pretty_ctx->reflog_info); | |
1554 | return 2; | |
cd1957f5 JK |
1555 | case 'n': |
1556 | case 'N': | |
1557 | case 'e': | |
1558 | case 'E': | |
1559 | return format_reflog_person(sb, | |
1560 | placeholder[1], | |
1561 | c->pretty_ctx->reflog_info, | |
a5481a6c | 1562 | &c->pretty_ctx->date_mode); |
8f8f5476 TR |
1563 | } |
1564 | return 0; /* unknown %g placeholder */ | |
8b208f02 | 1565 | case 'N': |
ddf333f6 JH |
1566 | if (c->pretty_ctx->notes_message) { |
1567 | strbuf_addstr(sb, c->pretty_ctx->notes_message); | |
5b163603 JG |
1568 | return 1; |
1569 | } | |
1570 | return 0; | |
cde75e59 RS |
1571 | } |
1572 | ||
f6667c5e | 1573 | if (placeholder[0] == 'G') { |
ffb6d7d5 SG |
1574 | if (!c->signature_check.result) |
1575 | check_commit_signature(c->commit, &(c->signature_check)); | |
f6667c5e JH |
1576 | switch (placeholder[1]) { |
1577 | case 'G': | |
b5726a5d FS |
1578 | if (c->signature_check.output) |
1579 | strbuf_addstr(sb, c->signature_check.output); | |
f6667c5e JH |
1580 | break; |
1581 | case '?': | |
ffb6d7d5 | 1582 | switch (c->signature_check.result) { |
f6667c5e | 1583 | case 'G': |
54887b46 HJI |
1584 | switch (c->signature_check.trust_level) { |
1585 | case TRUST_UNDEFINED: | |
1586 | case TRUST_NEVER: | |
1587 | strbuf_addch(sb, 'U'); | |
1588 | break; | |
1589 | default: | |
1590 | strbuf_addch(sb, 'G'); | |
1591 | break; | |
1592 | } | |
1593 | break; | |
f6667c5e | 1594 | case 'B': |
661a1806 | 1595 | case 'E': |
e290c4b9 | 1596 | case 'N': |
661a1806 MG |
1597 | case 'X': |
1598 | case 'Y': | |
1599 | case 'R': | |
ffb6d7d5 | 1600 | strbuf_addch(sb, c->signature_check.result); |
f6667c5e JH |
1601 | } |
1602 | break; | |
1603 | case 'S': | |
ffb6d7d5 SG |
1604 | if (c->signature_check.signer) |
1605 | strbuf_addstr(sb, c->signature_check.signer); | |
f6667c5e | 1606 | break; |
0174eeaa | 1607 | case 'K': |
ffb6d7d5 SG |
1608 | if (c->signature_check.key) |
1609 | strbuf_addstr(sb, c->signature_check.key); | |
0174eeaa | 1610 | break; |
3daaaabe MG |
1611 | case 'F': |
1612 | if (c->signature_check.fingerprint) | |
1613 | strbuf_addstr(sb, c->signature_check.fingerprint); | |
1614 | break; | |
4de9394d MG |
1615 | case 'P': |
1616 | if (c->signature_check.primary_key_fingerprint) | |
1617 | strbuf_addstr(sb, c->signature_check.primary_key_fingerprint); | |
1618 | break; | |
54887b46 | 1619 | case 'T': |
803978da | 1620 | strbuf_addstr(sb, gpg_trust_level_to_str(c->signature_check.trust_level)); |
54887b46 | 1621 | break; |
aa4b78d4 JK |
1622 | default: |
1623 | return 0; | |
f6667c5e JH |
1624 | } |
1625 | return 2; | |
1626 | } | |
1627 | ||
cde75e59 | 1628 | /* For the rest we have to parse the commit header. */ |
018b9deb JK |
1629 | if (!c->commit_header_parsed) { |
1630 | msg = c->message = | |
1631 | repo_logmsg_reencode(c->repository, commit, | |
1632 | &c->commit_encoding, "UTF-8"); | |
f29d5958 | 1633 | parse_commit_header(c); |
018b9deb | 1634 | } |
93fc05eb | 1635 | |
f29d5958 | 1636 | switch (placeholder[0]) { |
c3a670de MC |
1637 | case 'a': /* author ... */ |
1638 | return format_person_part(sb, placeholder[1], | |
d36f8679 | 1639 | msg + c->author.off, c->author.len, |
a5481a6c | 1640 | &c->pretty_ctx->date_mode); |
c3a670de MC |
1641 | case 'c': /* committer ... */ |
1642 | return format_person_part(sb, placeholder[1], | |
d36f8679 | 1643 | msg + c->committer.off, c->committer.len, |
a5481a6c | 1644 | &c->pretty_ctx->date_mode); |
c3a670de | 1645 | case 'e': /* encoding */ |
0940a76d NTND |
1646 | if (c->commit_encoding) |
1647 | strbuf_addstr(sb, c->commit_encoding); | |
c3a670de | 1648 | return 1; |
1367b12a EB |
1649 | case 'B': /* raw body */ |
1650 | /* message_off is always left at the initial newline */ | |
1651 | strbuf_addstr(sb, msg + c->message_off + 1); | |
1652 | return 1; | |
f53bd743 RS |
1653 | } |
1654 | ||
1655 | /* Now we need to parse the commit message. */ | |
1656 | if (!c->commit_message_parsed) | |
1657 | parse_commit_message(c); | |
1658 | ||
1659 | switch (placeholder[0]) { | |
1660 | case 's': /* subject */ | |
1661 | format_subject(sb, msg + c->subject_off, " "); | |
1662 | return 1; | |
46d164b0 | 1663 | case 'f': /* sanitized subject */ |
47d4676a HV |
1664 | eol = strchrnul(msg + c->subject_off, '\n'); |
1665 | format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off)); | |
46d164b0 | 1666 | return 1; |
c3a670de | 1667 | case 'b': /* body */ |
f29d5958 | 1668 | strbuf_addstr(sb, msg + c->body_off); |
c3a670de | 1669 | return 1; |
93fc05eb | 1670 | } |
d9f31fbf | 1671 | |
58311c66 | 1672 | if (skip_prefix(placeholder, "(trailers", &arg)) { |
a388b10f | 1673 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
250bea0c | 1674 | struct string_list filter_list = STRING_LIST_INIT_NODUP; |
0b691d86 | 1675 | struct strbuf sepbuf = STRBUF_INIT; |
058761f1 | 1676 | struct strbuf kvsepbuf = STRBUF_INIT; |
3e3f3478 | 1677 | size_t ret = 0; |
e5fba5d5 JK |
1678 | |
1679 | opts.no_divider = 1; | |
1680 | ||
84ff053d TB |
1681 | if (*arg == ':') { |
1682 | arg++; | |
636a0aee | 1683 | if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL)) |
90563aed | 1684 | goto trailer_out; |
58311c66 JK |
1685 | } |
1686 | if (*arg == ')') { | |
1687 | format_trailers_from_commit(sb, msg + c->subject_off, &opts); | |
3e3f3478 | 1688 | ret = arg - placeholder + 1; |
58311c66 | 1689 | } |
250bea0c AW |
1690 | trailer_out: |
1691 | string_list_clear(&filter_list, 0); | |
0b691d86 | 1692 | strbuf_release(&sepbuf); |
3e3f3478 | 1693 | return ret; |
d9f31fbf JK |
1694 | } |
1695 | ||
c3a670de | 1696 | return 0; /* unknown placeholder */ |
cde75e59 RS |
1697 | } |
1698 | ||
a5752342 NTND |
1699 | static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */ |
1700 | const char *placeholder, | |
1701 | struct format_commit_context *c) | |
1702 | { | |
1703 | struct strbuf local_sb = STRBUF_INIT; | |
81dc898d PS |
1704 | size_t total_consumed = 0; |
1705 | int len, padding = c->padding; | |
1706 | ||
a5752342 NTND |
1707 | if (padding < 0) { |
1708 | const char *start = strrchr(sb->buf, '\n'); | |
1709 | int occupied; | |
1710 | if (!start) | |
1711 | start = sb->buf; | |
522cc87f | 1712 | occupied = utf8_strnwidth(start, strlen(start), 1); |
3ad87c80 | 1713 | occupied += c->pretty_ctx->graph_width; |
a5752342 NTND |
1714 | padding = (-padding) - occupied; |
1715 | } | |
1716 | while (1) { | |
1717 | int modifier = *placeholder == 'C'; | |
81dc898d | 1718 | size_t consumed = format_commit_one(&local_sb, placeholder, c); |
a5752342 NTND |
1719 | total_consumed += consumed; |
1720 | ||
1721 | if (!modifier) | |
1722 | break; | |
1723 | ||
1724 | placeholder += consumed; | |
1725 | if (*placeholder != '%') | |
1726 | break; | |
1727 | placeholder++; | |
1728 | total_consumed++; | |
1729 | } | |
522cc87f | 1730 | len = utf8_strnwidth(local_sb.buf, local_sb.len, 1); |
1640632b NTND |
1731 | |
1732 | if (c->flush_type == flush_left_and_steal) { | |
1733 | const char *ch = sb->buf + sb->len - 1; | |
1734 | while (len > padding && ch > sb->buf) { | |
1735 | const char *p; | |
1736 | if (*ch == ' ') { | |
1737 | ch--; | |
1738 | padding++; | |
1739 | continue; | |
1740 | } | |
1741 | /* check for trailing ansi sequences */ | |
1742 | if (*ch != 'm') | |
1743 | break; | |
1744 | p = ch - 1; | |
b49f309a | 1745 | while (p > sb->buf && ch - p < 10 && *p != '\033') |
1640632b NTND |
1746 | p--; |
1747 | if (*p != '\033' || | |
1748 | ch + 1 - p != display_mode_esc_sequence_len(p)) | |
1749 | break; | |
1750 | /* | |
1751 | * got a good ansi sequence, put it back to | |
1752 | * local_sb as we're cutting sb | |
1753 | */ | |
1754 | strbuf_insert(&local_sb, 0, p, ch + 1 - p); | |
1755 | ch = p - 1; | |
1756 | } | |
1757 | strbuf_setlen(sb, ch + 1 - sb->buf); | |
1758 | c->flush_type = flush_left; | |
1759 | } | |
1760 | ||
a7f01c6b NTND |
1761 | if (len > padding) { |
1762 | switch (c->truncate) { | |
1763 | case trunc_left: | |
1764 | strbuf_utf8_replace(&local_sb, | |
1765 | 0, len - (padding - 2), | |
1766 | ".."); | |
1767 | break; | |
1768 | case trunc_middle: | |
1769 | strbuf_utf8_replace(&local_sb, | |
1770 | padding / 2 - 1, | |
1771 | len - (padding - 2), | |
1772 | ".."); | |
1773 | break; | |
1774 | case trunc_right: | |
1775 | strbuf_utf8_replace(&local_sb, | |
1776 | padding - 2, len - (padding - 2), | |
1777 | ".."); | |
1778 | break; | |
1779 | case trunc_none: | |
1780 | break; | |
1781 | } | |
e992d1eb | 1782 | strbuf_addbuf(sb, &local_sb); |
a7f01c6b | 1783 | } else { |
81dc898d | 1784 | size_t sb_len = sb->len, offset = 0; |
a5752342 NTND |
1785 | if (c->flush_type == flush_left) |
1786 | offset = padding - len; | |
1787 | else if (c->flush_type == flush_both) | |
1788 | offset = (padding - len) / 2; | |
1789 | /* | |
1790 | * we calculate padding in columns, now | |
1791 | * convert it back to chars | |
1792 | */ | |
1793 | padding = padding - len + local_sb.len; | |
415792ed | 1794 | strbuf_addchars(sb, ' ', padding); |
a5752342 NTND |
1795 | memcpy(sb->buf + sb_len + offset, local_sb.buf, |
1796 | local_sb.len); | |
1797 | } | |
1798 | strbuf_release(&local_sb); | |
1799 | c->flush_type = no_flush; | |
1800 | return total_consumed; | |
1801 | } | |
1802 | ||
7e77df39 NTND |
1803 | static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */ |
1804 | const char *placeholder, | |
9fa708da JH |
1805 | void *context) |
1806 | { | |
81dc898d | 1807 | size_t consumed, orig_len; |
9fa708da JH |
1808 | enum { |
1809 | NO_MAGIC, | |
1810 | ADD_LF_BEFORE_NON_EMPTY, | |
1811 | DEL_LF_BEFORE_EMPTY, | |
223a923c | 1812 | ADD_SP_BEFORE_NON_EMPTY |
9fa708da JH |
1813 | } magic = NO_MAGIC; |
1814 | ||
1815 | switch (placeholder[0]) { | |
1816 | case '-': | |
1817 | magic = DEL_LF_BEFORE_EMPTY; | |
1818 | break; | |
1819 | case '+': | |
1820 | magic = ADD_LF_BEFORE_NON_EMPTY; | |
1821 | break; | |
7b88176e MG |
1822 | case ' ': |
1823 | magic = ADD_SP_BEFORE_NON_EMPTY; | |
1824 | break; | |
9fa708da JH |
1825 | default: |
1826 | break; | |
1827 | } | |
1de69c0c | 1828 | if (magic != NO_MAGIC) { |
9fa708da JH |
1829 | placeholder++; |
1830 | ||
1de69c0c PS |
1831 | switch (placeholder[0]) { |
1832 | case 'w': | |
1833 | /* | |
1834 | * `%+w()` cannot ever expand to a non-empty string, | |
1835 | * and it potentially changes the layout of preceding | |
1836 | * contents. We're thus not able to handle the magic in | |
1837 | * this combination and refuse the pattern. | |
1838 | */ | |
1839 | return 0; | |
1840 | }; | |
1841 | } | |
1842 | ||
9fa708da | 1843 | orig_len = sb->len; |
a5752342 NTND |
1844 | if (((struct format_commit_context *)context)->flush_type != no_flush) |
1845 | consumed = format_and_pad_commit(sb, placeholder, context); | |
1846 | else | |
1847 | consumed = format_commit_one(sb, placeholder, context); | |
9fa708da JH |
1848 | if (magic == NO_MAGIC) |
1849 | return consumed; | |
1850 | ||
1851 | if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) { | |
1852 | while (sb->len && sb->buf[sb->len - 1] == '\n') | |
1853 | strbuf_setlen(sb, sb->len - 1); | |
7b88176e MG |
1854 | } else if (orig_len != sb->len) { |
1855 | if (magic == ADD_LF_BEFORE_NON_EMPTY) | |
a91cc7fa | 1856 | strbuf_insertstr(sb, orig_len, "\n"); |
7b88176e | 1857 | else if (magic == ADD_SP_BEFORE_NON_EMPTY) |
a91cc7fa | 1858 | strbuf_insertstr(sb, orig_len, " "); |
9fa708da JH |
1859 | } |
1860 | return consumed + 1; | |
1861 | } | |
1862 | ||
1bff8554 JK |
1863 | static size_t userformat_want_item(struct strbuf *sb UNUSED, |
1864 | const char *placeholder, | |
5b163603 JG |
1865 | void *context) |
1866 | { | |
1867 | struct userformat_want *w = context; | |
1868 | ||
7b88176e | 1869 | if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ') |
5b163603 JG |
1870 | placeholder++; |
1871 | ||
1872 | switch (*placeholder) { | |
1873 | case 'N': | |
1874 | w->notes = 1; | |
1875 | break; | |
ad6f028f IT |
1876 | case 'S': |
1877 | w->source = 1; | |
1878 | break; | |
b2086b51 JK |
1879 | case 'd': |
1880 | case 'D': | |
1881 | w->decorate = 1; | |
1882 | break; | |
5b163603 JG |
1883 | } |
1884 | return 0; | |
1885 | } | |
1886 | ||
1887 | void userformat_find_requirements(const char *fmt, struct userformat_want *w) | |
1888 | { | |
1889 | struct strbuf dummy = STRBUF_INIT; | |
1890 | ||
1891 | if (!fmt) { | |
1892 | if (!user_format) | |
1893 | return; | |
1894 | fmt = user_format; | |
1895 | } | |
a6253d10 | 1896 | strbuf_expand(&dummy, fmt, userformat_want_item, w); |
5b163603 JG |
1897 | strbuf_release(&dummy); |
1898 | } | |
1899 | ||
f54fbf5e SB |
1900 | void repo_format_commit_message(struct repository *r, |
1901 | const struct commit *commit, | |
1902 | const char *format, struct strbuf *sb, | |
1903 | const struct pretty_print_context *pretty_ctx) | |
cde75e59 | 1904 | { |
3e8ed3b9 | 1905 | struct format_commit_context context = { |
018b9deb | 1906 | .repository = r, |
3e8ed3b9 DL |
1907 | .commit = commit, |
1908 | .pretty_ctx = pretty_ctx, | |
1909 | .wrap_start = sb->len | |
1910 | }; | |
177b29dc | 1911 | const char *output_enc = pretty_ctx->output_encoding; |
7e77df39 | 1912 | const char *utf8 = "UTF-8"; |
f29d5958 | 1913 | |
c3a670de | 1914 | strbuf_expand(sb, format, format_commit_item, &context); |
02edd56b | 1915 | rewrap_message_tail(sb, &context, 0, 0, 0); |
177b29dc | 1916 | |
018b9deb JK |
1917 | /* |
1918 | * Convert output to an actual output encoding; note that | |
1919 | * format_commit_item() will always use UTF-8, so we don't | |
1920 | * have to bother if that's what the output wants. | |
1921 | */ | |
7e77df39 NTND |
1922 | if (output_enc) { |
1923 | if (same_encoding(utf8, output_enc)) | |
1924 | output_enc = NULL; | |
1925 | } else { | |
1926 | if (context.commit_encoding && | |
1927 | !same_encoding(context.commit_encoding, utf8)) | |
1928 | output_enc = context.commit_encoding; | |
1929 | } | |
1930 | ||
1931 | if (output_enc) { | |
c7d017d7 | 1932 | size_t outsz; |
7e77df39 NTND |
1933 | char *out = reencode_string_len(sb->buf, sb->len, |
1934 | output_enc, utf8, &outsz); | |
1935 | if (out) | |
1936 | strbuf_attach(sb, out, outsz, outsz + 1); | |
1937 | } | |
1938 | ||
0940a76d | 1939 | free(context.commit_encoding); |
f54fbf5e | 1940 | repo_unuse_commit_buffer(r, commit, context.message); |
93fc05eb JS |
1941 | } |
1942 | ||
10f2fbff | 1943 | static void pp_header(struct pretty_print_context *pp, |
93fc05eb JS |
1944 | const char *encoding, |
1945 | const struct commit *commit, | |
1946 | const char **msg_p, | |
1947 | struct strbuf *sb) | |
1948 | { | |
1949 | int parents_shown = 0; | |
1950 | ||
1951 | for (;;) { | |
e3f1da98 | 1952 | const char *name, *line = *msg_p; |
93fc05eb JS |
1953 | int linelen = get_one_line(*msg_p); |
1954 | ||
1955 | if (!linelen) | |
1956 | return; | |
1957 | *msg_p += linelen; | |
1958 | ||
1959 | if (linelen == 1) | |
1960 | /* End of header */ | |
1961 | return; | |
1962 | ||
6bf13944 | 1963 | if (pp->fmt == CMIT_FMT_RAW) { |
93fc05eb JS |
1964 | strbuf_add(sb, line, linelen); |
1965 | continue; | |
1966 | } | |
1967 | ||
59556548 | 1968 | if (starts_with(line, "parent ")) { |
580f0980 | 1969 | if (linelen != the_hash_algo->hexsz + 8) |
93fc05eb JS |
1970 | die("bad parent line in commit"); |
1971 | continue; | |
1972 | } | |
1973 | ||
1974 | if (!parents_shown) { | |
4bbaa1eb | 1975 | unsigned num = commit_list_count(commit->parents); |
93fc05eb | 1976 | /* with enough slop */ |
580f0980 | 1977 | strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20); |
6bf13944 | 1978 | add_merge_info(pp, sb, commit); |
93fc05eb JS |
1979 | parents_shown = 1; |
1980 | } | |
1981 | ||
1982 | /* | |
1983 | * MEDIUM == DEFAULT shows only author with dates. | |
1984 | * FULL shows both authors but not dates. | |
1985 | * FULLER shows both authors and dates. | |
1986 | */ | |
e3f1da98 | 1987 | if (skip_prefix(line, "author ", &name)) { |
93fc05eb | 1988 | strbuf_grow(sb, linelen + 80); |
e3f1da98 | 1989 | pp_user_info(pp, "Author", sb, name, encoding); |
93fc05eb | 1990 | } |
e3f1da98 | 1991 | if (skip_prefix(line, "committer ", &name) && |
6bf13944 | 1992 | (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { |
93fc05eb | 1993 | strbuf_grow(sb, linelen + 80); |
e3f1da98 | 1994 | pp_user_info(pp, "Commit", sb, name, encoding); |
93fc05eb JS |
1995 | } |
1996 | } | |
1997 | } | |
1998 | ||
10f2fbff | 1999 | void pp_title_line(struct pretty_print_context *pp, |
b02bd65f DB |
2000 | const char **msg_p, |
2001 | struct strbuf *sb, | |
b02bd65f | 2002 | const char *encoding, |
267123b4 | 2003 | int need_8bit_cte) |
93fc05eb | 2004 | { |
41dd00ba | 2005 | static const int max_length = 78; /* per rfc2047 */ |
93fc05eb JS |
2006 | struct strbuf title; |
2007 | ||
2008 | strbuf_init(&title, 80); | |
9553d2b2 JK |
2009 | *msg_p = format_subject(&title, *msg_p, |
2010 | pp->preserve_subject ? "\n" : " "); | |
93fc05eb JS |
2011 | |
2012 | strbuf_grow(sb, title.len + 1024); | |
6d167fd7 RS |
2013 | if (pp->print_email_subject) { |
2014 | if (pp->rev) | |
2015 | fmt_output_email_subject(sb, pp->rev); | |
19d097e3 EB |
2016 | if (pp->encode_email_headers && |
2017 | needs_rfc2047_encoding(title.buf, title.len)) | |
41dd00ba JS |
2018 | add_rfc2047(sb, title.buf, title.len, |
2019 | encoding, RFC2047_SUBJECT); | |
2020 | else | |
2021 | strbuf_add_wrapped_bytes(sb, title.buf, title.len, | |
2022 | -last_line_length(sb), 1, max_length); | |
93fc05eb JS |
2023 | } else { |
2024 | strbuf_addbuf(sb, &title); | |
2025 | } | |
2026 | strbuf_addch(sb, '\n'); | |
2027 | ||
a9080475 JK |
2028 | if (need_8bit_cte == 0) { |
2029 | int i; | |
2030 | for (i = 0; i < pp->in_body_headers.nr; i++) { | |
2031 | if (has_non_ascii(pp->in_body_headers.items[i].string)) { | |
2032 | need_8bit_cte = 1; | |
2033 | break; | |
2034 | } | |
2035 | } | |
2036 | } | |
2037 | ||
6bf4f1b4 | 2038 | if (need_8bit_cte > 0) { |
93fc05eb JS |
2039 | const char *header_fmt = |
2040 | "MIME-Version: 1.0\n" | |
2041 | "Content-Type: text/plain; charset=%s\n" | |
2042 | "Content-Transfer-Encoding: 8bit\n"; | |
2043 | strbuf_addf(sb, header_fmt, encoding); | |
2044 | } | |
6bf13944 JK |
2045 | if (pp->after_subject) { |
2046 | strbuf_addstr(sb, pp->after_subject); | |
93fc05eb | 2047 | } |
9f23e040 | 2048 | if (cmit_fmt_is_mail(pp->fmt)) { |
93fc05eb JS |
2049 | strbuf_addch(sb, '\n'); |
2050 | } | |
a9080475 JK |
2051 | |
2052 | if (pp->in_body_headers.nr) { | |
2053 | int i; | |
2054 | for (i = 0; i < pp->in_body_headers.nr; i++) { | |
2055 | strbuf_addstr(sb, pp->in_body_headers.items[i].string); | |
2056 | free(pp->in_body_headers.items[i].string); | |
2057 | } | |
2058 | string_list_clear(&pp->in_body_headers, 0); | |
2059 | strbuf_addch(sb, '\n'); | |
2060 | } | |
2061 | ||
93fc05eb JS |
2062 | strbuf_release(&title); |
2063 | } | |
2064 | ||
7cc13c71 LT |
2065 | static int pp_utf8_width(const char *start, const char *end) |
2066 | { | |
2067 | int width = 0; | |
2068 | size_t remain = end - start; | |
2069 | ||
2070 | while (remain) { | |
2071 | int n = utf8_width(&start, &remain); | |
2072 | if (n < 0 || !start) | |
2073 | return -1; | |
2074 | width += n; | |
2075 | } | |
2076 | return width; | |
2077 | } | |
2078 | ||
6a5c3379 HM |
2079 | static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt, |
2080 | int color, int tabwidth, const char *line, | |
2081 | int linelen) | |
7cc13c71 LT |
2082 | { |
2083 | const char *tab; | |
2084 | ||
2085 | while ((tab = memchr(line, '\t', linelen)) != NULL) { | |
2086 | int width = pp_utf8_width(line, tab); | |
2087 | ||
2088 | /* | |
2089 | * If it wasn't well-formed utf8, or it | |
2090 | * had characters with badly defined | |
2091 | * width (control characters etc), just | |
2092 | * give up on trying to align things. | |
2093 | */ | |
2094 | if (width < 0) | |
2095 | break; | |
2096 | ||
2097 | /* Output the data .. */ | |
6a5c3379 HM |
2098 | append_line_with_color(sb, opt, line, tab - line, color, |
2099 | GREP_CONTEXT_BODY, | |
2100 | GREP_HEADER_FIELD_MAX); | |
7cc13c71 LT |
2101 | |
2102 | /* .. and the de-tabified tab */ | |
fe37a9c5 | 2103 | strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth)); |
7cc13c71 LT |
2104 | |
2105 | /* Skip over the printed part .. */ | |
2106 | linelen -= tab + 1 - line; | |
2107 | line = tab + 1; | |
2108 | } | |
2109 | ||
2110 | /* | |
2111 | * Print out everything after the last tab without | |
2112 | * worrying about width - there's nothing more to | |
2113 | * align. | |
2114 | */ | |
6a5c3379 HM |
2115 | append_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY, |
2116 | GREP_HEADER_FIELD_MAX); | |
7cc13c71 LT |
2117 | } |
2118 | ||
2119 | /* | |
2120 | * pp_handle_indent() prints out the intendation, and | |
2121 | * the whole line (without the final newline), after | |
2122 | * de-tabifying. | |
2123 | */ | |
2124 | static void pp_handle_indent(struct pretty_print_context *pp, | |
2125 | struct strbuf *sb, int indent, | |
2126 | const char *line, int linelen) | |
2127 | { | |
6a5c3379 HM |
2128 | struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL; |
2129 | ||
7cc13c71 LT |
2130 | strbuf_addchars(sb, ' ', indent); |
2131 | if (pp->expand_tabs_in_log) | |
6a5c3379 HM |
2132 | strbuf_add_tabexpand(sb, opt, pp->color, pp->expand_tabs_in_log, |
2133 | line, linelen); | |
7cc13c71 | 2134 | else |
6a5c3379 HM |
2135 | append_line_with_color(sb, opt, line, linelen, pp->color, |
2136 | GREP_CONTEXT_BODY, | |
2137 | GREP_HEADER_FIELD_MAX); | |
7cc13c71 LT |
2138 | } |
2139 | ||
9f23e040 EW |
2140 | static int is_mboxrd_from(const char *line, int len) |
2141 | { | |
2142 | /* | |
2143 | * a line matching /^From $/ here would only have len == 4 | |
2144 | * at this point because is_empty_line would've trimmed all | |
2145 | * trailing space | |
2146 | */ | |
2147 | return len > 4 && starts_with(line + strspn(line, ">"), "From "); | |
2148 | } | |
2149 | ||
10f2fbff | 2150 | void pp_remainder(struct pretty_print_context *pp, |
b02bd65f DB |
2151 | const char **msg_p, |
2152 | struct strbuf *sb, | |
2153 | int indent) | |
93fc05eb | 2154 | { |
6a5c3379 | 2155 | struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL; |
93fc05eb | 2156 | int first = 1; |
6a5c3379 | 2157 | |
93fc05eb JS |
2158 | for (;;) { |
2159 | const char *line = *msg_p; | |
2160 | int linelen = get_one_line(line); | |
2161 | *msg_p += linelen; | |
2162 | ||
2163 | if (!linelen) | |
2164 | break; | |
2165 | ||
77356122 | 2166 | if (is_blank_line(line, &linelen)) { |
93fc05eb JS |
2167 | if (first) |
2168 | continue; | |
6bf13944 | 2169 | if (pp->fmt == CMIT_FMT_SHORT) |
93fc05eb JS |
2170 | break; |
2171 | } | |
2172 | first = 0; | |
2173 | ||
2174 | strbuf_grow(sb, linelen + indent + 20); | |
415792ed | 2175 | if (indent) |
7cc13c71 | 2176 | pp_handle_indent(pp, sb, indent, line, linelen); |
0893eec8 | 2177 | else if (pp->expand_tabs_in_log) |
6a5c3379 HM |
2178 | strbuf_add_tabexpand(sb, opt, pp->color, |
2179 | pp->expand_tabs_in_log, line, | |
2180 | linelen); | |
9f23e040 EW |
2181 | else { |
2182 | if (pp->fmt == CMIT_FMT_MBOXRD && | |
2183 | is_mboxrd_from(line, linelen)) | |
2184 | strbuf_addch(sb, '>'); | |
2185 | ||
6a5c3379 HM |
2186 | append_line_with_color(sb, opt, line, linelen, |
2187 | pp->color, GREP_CONTEXT_BODY, | |
2188 | GREP_HEADER_FIELD_MAX); | |
9f23e040 | 2189 | } |
93fc05eb JS |
2190 | strbuf_addch(sb, '\n'); |
2191 | } | |
2192 | } | |
2193 | ||
10f2fbff | 2194 | void pretty_print_commit(struct pretty_print_context *pp, |
6bf13944 JK |
2195 | const struct commit *commit, |
2196 | struct strbuf *sb) | |
93fc05eb JS |
2197 | { |
2198 | unsigned long beginning_of_body; | |
2199 | int indent = 4; | |
dd0d388c | 2200 | const char *msg; |
b000c59b | 2201 | const char *reencoded; |
93fc05eb | 2202 | const char *encoding; |
6bf13944 | 2203 | int need_8bit_cte = pp->need_8bit_cte; |
93fc05eb | 2204 | |
6bf13944 JK |
2205 | if (pp->fmt == CMIT_FMT_USERFORMAT) { |
2206 | format_commit_message(commit, user_format, sb, pp); | |
93fc05eb JS |
2207 | return; |
2208 | } | |
2209 | ||
e297cf5a | 2210 | encoding = get_log_output_encoding(); |
5a10d236 | 2211 | msg = reencoded = logmsg_reencode(commit, NULL, encoding); |
93fc05eb | 2212 | |
9f23e040 | 2213 | if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt)) |
93fc05eb JS |
2214 | indent = 0; |
2215 | ||
6bf4f1b4 JH |
2216 | /* |
2217 | * We need to check and emit Content-type: to mark it | |
2218 | * as 8-bit if we haven't done so. | |
93fc05eb | 2219 | */ |
9f23e040 | 2220 | if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) { |
93fc05eb JS |
2221 | int i, ch, in_body; |
2222 | ||
2223 | for (in_body = i = 0; (ch = msg[i]); i++) { | |
2224 | if (!in_body) { | |
2225 | /* author could be non 7-bit ASCII but | |
2226 | * the log may be so; skip over the | |
2227 | * header part first. | |
2228 | */ | |
2229 | if (ch == '\n' && msg[i+1] == '\n') | |
2230 | in_body = 1; | |
2231 | } | |
2232 | else if (non_ascii(ch)) { | |
6bf4f1b4 | 2233 | need_8bit_cte = 1; |
93fc05eb JS |
2234 | break; |
2235 | } | |
2236 | } | |
2237 | } | |
2238 | ||
6bf13944 | 2239 | pp_header(pp, encoding, commit, &msg, sb); |
6d167fd7 | 2240 | if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) { |
93fc05eb JS |
2241 | strbuf_addch(sb, '\n'); |
2242 | } | |
2243 | ||
2244 | /* Skip excess blank lines at the beginning of body, if any... */ | |
77356122 | 2245 | msg = skip_blank_lines(msg); |
93fc05eb JS |
2246 | |
2247 | /* These formats treat the title line specially. */ | |
9f23e040 | 2248 | if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt)) |
6bf13944 | 2249 | pp_title_line(pp, &msg, sb, encoding, need_8bit_cte); |
93fc05eb JS |
2250 | |
2251 | beginning_of_body = sb->len; | |
6bf13944 JK |
2252 | if (pp->fmt != CMIT_FMT_ONELINE) |
2253 | pp_remainder(pp, &msg, sb, indent); | |
93fc05eb JS |
2254 | strbuf_rtrim(sb); |
2255 | ||
2256 | /* Make sure there is an EOLN for the non-oneline case */ | |
6bf13944 | 2257 | if (pp->fmt != CMIT_FMT_ONELINE) |
93fc05eb JS |
2258 | strbuf_addch(sb, '\n'); |
2259 | ||
2260 | /* | |
2261 | * The caller may append additional body text in e-mail | |
2262 | * format. Make sure we did not strip the blank line | |
2263 | * between the header and the body. | |
2264 | */ | |
9f23e040 | 2265 | if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body) |
93fc05eb | 2266 | strbuf_addch(sb, '\n'); |
a97a7468 | 2267 | |
b66103c3 | 2268 | unuse_commit_buffer(commit, reencoded); |
93fc05eb | 2269 | } |
8b8a5374 JK |
2270 | |
2271 | void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit, | |
2272 | struct strbuf *sb) | |
2273 | { | |
2274 | struct pretty_print_context pp = {0}; | |
6bf13944 JK |
2275 | pp.fmt = fmt; |
2276 | pretty_print_commit(&pp, commit, sb); | |
8b8a5374 | 2277 | } |