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