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