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