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