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