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