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