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