blob: 2f6b0ae6c1486660bb37442b991cb1140a9f2bb6 [file] [log] [blame]
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00003#include "commit.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00004#include "utf8.h"
5#include "diff.h"
6#include "revision.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01007#include "string-list.h"
Johannes Schindeline0cbc392008-07-12 00:28:18 +01008#include "mailmap.h"
René Scharfe3b3d4432008-09-04 23:40:03 +02009#include "log-tree.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +020010#include "notes.h"
Jeff Kingc0029222009-01-17 10:38:46 -050011#include "color.h"
Thomas Rast8f8f5472009-10-19 17:48:10 +020012#include "reflog-walk.h"
Junio C Hamanof6667c52011-10-21 21:06:02 -070013#include "gpg-interface.h"
Jacob Kellerd9f31fb2016-11-18 16:58:14 -080014#include "trailer.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000015
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000016static char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010017static struct cmt_fmt_map {
18 const char *name;
19 enum cmit_fmt format;
20 int is_tformat;
Junio C Hamano0893eec2016-03-29 15:49:24 -070021 int expand_tabs_in_log;
Will Palmer2d7671e2010-05-02 12:00:43 +010022 int is_alias;
23 const char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010024} *commit_formats;
Will Palmer80281842010-05-02 12:00:44 +010025static size_t builtin_formats_len;
Will Palmer40957892010-05-02 12:00:42 +010026static size_t commit_formats_len;
Will Palmer80281842010-05-02 12:00:44 +010027static size_t commit_formats_alloc;
Will Palmer40957892010-05-02 12:00:42 +010028static struct cmt_fmt_map *find_commit_format(const char *sought);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000029
Jeff Kingb9c7d6e2014-07-29 13:56:48 -040030int commit_format_is_empty(enum cmit_fmt fmt)
31{
32 return fmt == CMIT_FMT_USERFORMAT && !*user_format;
33}
34
Nanako Shiraishi36407542009-02-24 18:59:15 +090035static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
36{
37 free(user_format);
38 user_format = xstrdup(cp);
39 if (is_tformat)
40 rev->use_terminator = 1;
41 rev->commit_format = CMIT_FMT_USERFORMAT;
42}
43
Will Palmer80281842010-05-02 12:00:44 +010044static int git_pretty_formats_config(const char *var, const char *value, void *cb)
45{
46 struct cmt_fmt_map *commit_format = NULL;
47 const char *name;
48 const char *fmt;
49 int i;
50
Jeff King95b567c2014-06-18 15:48:29 -040051 if (!skip_prefix(var, "pretty.", &name))
Will Palmer80281842010-05-02 12:00:44 +010052 return 0;
53
Will Palmer80281842010-05-02 12:00:44 +010054 for (i = 0; i < builtin_formats_len; i++) {
55 if (!strcmp(commit_formats[i].name, name))
56 return 0;
57 }
58
59 for (i = builtin_formats_len; i < commit_formats_len; i++) {
60 if (!strcmp(commit_formats[i].name, name)) {
61 commit_format = &commit_formats[i];
62 break;
63 }
64 }
65
66 if (!commit_format) {
67 ALLOC_GROW(commit_formats, commit_formats_len+1,
68 commit_formats_alloc);
69 commit_format = &commit_formats[commit_formats_len];
Jonathan Nieder95a26182010-05-08 16:07:39 -050070 memset(commit_format, 0, sizeof(*commit_format));
Will Palmer80281842010-05-02 12:00:44 +010071 commit_formats_len++;
72 }
73
74 commit_format->name = xstrdup(name);
75 commit_format->format = CMIT_FMT_USERFORMAT;
Tanay Abhraa26bc612014-08-04 07:41:15 -070076 if (git_config_string(&fmt, var, value))
77 return -1;
78
René Scharfee3f1da92014-10-04 20:54:50 +020079 if (skip_prefix(fmt, "format:", &fmt))
80 commit_format->is_tformat = 0;
81 else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
Will Palmer80281842010-05-02 12:00:44 +010082 commit_format->is_tformat = 1;
83 else
84 commit_format->is_alias = 1;
85 commit_format->user_format = fmt;
86
87 return 0;
88}
89
Will Palmer40957892010-05-02 12:00:42 +010090static void setup_commit_formats(void)
91{
92 struct cmt_fmt_map builtin_formats[] = {
Junio C Hamano0893eec2016-03-29 15:49:24 -070093 { "raw", CMIT_FMT_RAW, 0, 0 },
Junio C Hamanofe37a9c2016-03-29 16:05:39 -070094 { "medium", CMIT_FMT_MEDIUM, 0, 8 },
Junio C Hamano0893eec2016-03-29 15:49:24 -070095 { "short", CMIT_FMT_SHORT, 0, 0 },
96 { "email", CMIT_FMT_EMAIL, 0, 0 },
Eric Wong9f23e042016-06-05 04:46:39 +000097 { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
Junio C Hamanofe37a9c2016-03-29 16:05:39 -070098 { "fuller", CMIT_FMT_FULLER, 0, 8 },
99 { "full", CMIT_FMT_FULL, 0, 8 },
Junio C Hamano0893eec2016-03-29 15:49:24 -0700100 { "oneline", CMIT_FMT_ONELINE, 1, 0 }
Will Palmer40957892010-05-02 12:00:42 +0100101 };
102 commit_formats_len = ARRAY_SIZE(builtin_formats);
Will Palmer80281842010-05-02 12:00:44 +0100103 builtin_formats_len = commit_formats_len;
104 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
Will Palmer40957892010-05-02 12:00:42 +0100105 memcpy(commit_formats, builtin_formats,
106 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
Will Palmer80281842010-05-02 12:00:44 +0100107
108 git_config(git_pretty_formats_config, NULL);
Will Palmer40957892010-05-02 12:00:42 +0100109}
110
Will Palmer2d7671e2010-05-02 12:00:43 +0100111static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
112 const char *original,
113 int num_redirections)
Will Palmer40957892010-05-02 12:00:42 +0100114{
115 struct cmt_fmt_map *found = NULL;
116 size_t found_match_len = 0;
117 int i;
118
Will Palmer2d7671e2010-05-02 12:00:43 +0100119 if (num_redirections >= commit_formats_len)
120 die("invalid --pretty format: "
121 "'%s' references an alias which points to itself",
122 original);
Will Palmer40957892010-05-02 12:00:42 +0100123
124 for (i = 0; i < commit_formats_len; i++) {
125 size_t match_len;
126
Christian Couder59556542013-11-30 21:55:40 +0100127 if (!starts_with(commit_formats[i].name, sought))
Will Palmer40957892010-05-02 12:00:42 +0100128 continue;
129
130 match_len = strlen(commit_formats[i].name);
131 if (found == NULL || found_match_len > match_len) {
132 found = &commit_formats[i];
133 found_match_len = match_len;
134 }
135 }
Will Palmer2d7671e2010-05-02 12:00:43 +0100136
137 if (found && found->is_alias) {
138 found = find_commit_format_recursive(found->user_format,
139 original,
140 num_redirections+1);
141 }
142
Will Palmer40957892010-05-02 12:00:42 +0100143 return found;
144}
145
Will Palmer2d7671e2010-05-02 12:00:43 +0100146static struct cmt_fmt_map *find_commit_format(const char *sought)
147{
148 if (!commit_formats)
149 setup_commit_formats();
150
151 return find_commit_format_recursive(sought, sought, 0);
152}
153
Junio C Hamano4da45be2008-04-07 17:11:34 -0700154void get_commit_format(const char *arg, struct rev_info *rev)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000155{
Will Palmer40957892010-05-02 12:00:42 +0100156 struct cmt_fmt_map *commit_format;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000157
Junio C Hamano4da45be2008-04-07 17:11:34 -0700158 rev->use_terminator = 0;
Jeff Kingc75e7ad2014-07-29 13:54:46 -0400159 if (!arg) {
Junio C Hamano4da45be2008-04-07 17:11:34 -0700160 rev->commit_format = CMIT_FMT_DEFAULT;
161 return;
162 }
René Scharfee3f1da92014-10-04 20:54:50 +0200163 if (skip_prefix(arg, "format:", &arg)) {
164 save_user_format(rev, arg, 0);
Junio C Hamano4da45be2008-04-07 17:11:34 -0700165 return;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000166 }
Will Palmer40957892010-05-02 12:00:42 +0100167
René Scharfee3f1da92014-10-04 20:54:50 +0200168 if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
Nanako Shiraishi36407542009-02-24 18:59:15 +0900169 save_user_format(rev, arg, 1);
170 return;
171 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000172
Will Palmer40957892010-05-02 12:00:42 +0100173 commit_format = find_commit_format(arg);
174 if (!commit_format)
175 die("invalid --pretty format: %s", arg);
176
177 rev->commit_format = commit_format->format;
178 rev->use_terminator = commit_format->is_tformat;
Junio C Hamano0893eec2016-03-29 15:49:24 -0700179 rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
Will Palmer80281842010-05-02 12:00:44 +0100180 if (commit_format->format == CMIT_FMT_USERFORMAT) {
181 save_user_format(rev, commit_format->user_format,
182 commit_format->is_tformat);
183 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000184}
185
186/*
187 * Generic support for pretty-printing the header
188 */
189static int get_one_line(const char *msg)
190{
191 int ret = 0;
192
193 for (;;) {
194 char c = *msg++;
195 if (!c)
196 break;
197 ret++;
198 if (c == '\n')
199 break;
200 }
201 return ret;
202}
203
204/* High bit set, or ISO-2022-INT */
Junio C Hamanocc571142010-01-11 22:23:35 -0800205static int non_ascii(int ch)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000206{
René Scharfec2e93642009-03-07 14:06:49 +0100207 return !isascii(ch) || ch == '\033';
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000208}
209
Johannes Schindelin28e9cf62009-08-10 18:22:18 +0200210int has_non_ascii(const char *s)
211{
212 int ch;
213 if (!s)
214 return 0;
215 while ((ch = *s++) != '\0') {
216 if (non_ascii(ch))
217 return 1;
218 }
219 return 0;
220}
221
Jeff King4d03c182011-04-08 18:40:36 -0400222static int is_rfc822_special(char ch)
223{
224 switch (ch) {
225 case '(':
226 case ')':
227 case '<':
228 case '>':
229 case '[':
230 case ']':
231 case ':':
232 case ';':
233 case '@':
234 case ',':
235 case '.':
236 case '"':
237 case '\\':
238 return 1;
239 default:
240 return 0;
241 }
242}
243
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200244static int needs_rfc822_quoting(const char *s, int len)
Jeff King4d03c182011-04-08 18:40:36 -0400245{
246 int i;
247 for (i = 0; i < len; i++)
248 if (is_rfc822_special(s[i]))
249 return 1;
250 return 0;
251}
252
Jan H. Schönherrf9b72042012-10-18 16:43:31 +0200253static int last_line_length(struct strbuf *sb)
254{
255 int i;
256
257 /* How many bytes are already used on the last line? */
258 for (i = sb->len - 1; i >= 0; i--)
259 if (sb->buf[i] == '\n')
260 break;
261 return sb->len - (i + 1);
262}
263
Jeff King4d03c182011-04-08 18:40:36 -0400264static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
265{
266 int i;
267
268 /* just a guess, we may have to also backslash-quote */
269 strbuf_grow(out, len + 2);
270
271 strbuf_addch(out, '"');
272 for (i = 0; i < len; i++) {
273 switch (s[i]) {
274 case '"':
275 case '\\':
276 strbuf_addch(out, '\\');
277 /* fall through */
278 default:
279 strbuf_addch(out, s[i]);
280 }
281 }
282 strbuf_addch(out, '"');
283}
284
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200285enum rfc2047_type {
286 RFC2047_SUBJECT,
Ronnie Sahlberg78273522014-07-02 11:24:05 -0700287 RFC2047_ADDRESS
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200288};
289
290static int is_rfc2047_special(char ch, enum rfc2047_type type)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000291{
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200292 /*
293 * rfc2047, section 4.2:
294 *
295 * 8-bit values which correspond to printable ASCII characters other
296 * than "=", "?", and "_" (underscore), MAY be represented as those
297 * characters. (But see section 5 for restrictions.) In
298 * particular, SPACE and TAB MUST NOT be represented as themselves
299 * within encoded words.
300 */
301
302 /*
303 * rule out non-ASCII characters and non-printable characters (the
304 * non-ASCII check should be redundant as isprint() is not localized
305 * and only knows about ASCII, but be defensive about that)
306 */
307 if (non_ascii(ch) || !isprint(ch))
Jan H. Schönherr94f6cdf2012-10-18 16:43:30 +0200308 return 1;
309
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200310 /*
311 * rule out special printable characters (' ' should be the only
312 * whitespace character considered printable, but be defensive and use
313 * isspace())
314 */
315 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
316 return 1;
317
318 /*
319 * rfc2047, section 5.3:
320 *
321 * As a replacement for a 'word' entity within a 'phrase', for example,
322 * one that precedes an address in a From, To, or Cc header. The ABNF
323 * definition for 'phrase' from RFC 822 thus becomes:
324 *
325 * phrase = 1*( encoded-word / word )
326 *
327 * In this case the set of characters that may be used in a "Q"-encoded
328 * 'encoded-word' is restricted to: <upper and lower case ASCII
329 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
330 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
331 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
332 * 'special' by 'linear-white-space'.
333 */
334
335 if (type != RFC2047_ADDRESS)
336 return 0;
337
338 /* '=' and '_' are special cases and have been checked above */
339 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000340}
341
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200342static int needs_rfc2047_encoding(const char *line, int len,
343 enum rfc2047_type type)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000344{
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500345 int i;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000346
347 for (i = 0; i < len; i++) {
348 int ch = line[i];
Jeff Kingc22e7de2011-02-23 04:59:18 -0500349 if (non_ascii(ch) || ch == '\n')
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200350 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000351 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200352 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000353 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000354
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200355 return 0;
356}
357
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400358static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200359 const char *encoding, enum rfc2047_type type)
360{
361 static const int max_encoded_length = 76; /* per rfc2047 */
362 int i;
363 int line_len = last_line_length(sb);
364
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000365 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
366 strbuf_addf(sb, "=?%s?q?", encoding);
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500367 line_len += strlen(encoding) + 5; /* 5 for =??q? */
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400368
369 while (len) {
370 /*
371 * RFC 2047, section 5 (3):
372 *
373 * Each 'encoded-word' MUST represent an integral number of
374 * characters. A multi-octet character may not be split across
375 * adjacent 'encoded- word's.
376 */
377 const unsigned char *p = (const unsigned char *)line;
378 int chrlen = mbs_chrlen(&line, &len, encoding);
379 int is_special = (chrlen > 1) || is_rfc2047_special(*p, type);
380
381 /* "=%02X" * chrlen, or the byte itself */
382 const char *encoded_fmt = is_special ? "=%02X" : "%c";
383 int encoded_len = is_special ? 3 * chrlen : 1;
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500384
Jan H. Schönherr94f6cdf2012-10-18 16:43:30 +0200385 /*
386 * According to RFC 2047, we could encode the special character
387 * ' ' (space) with '_' (underscore) for readability. But many
388 * programs do not understand this and just leave the
389 * underscore in place. Thus, we do nothing special here, which
390 * causes ' ' to be encoded as '=20', avoiding this problem.
391 */
392
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400393 if (line_len + encoded_len + 2 > max_encoded_length) {
394 /* It won't fit with trailing "?=" --- break the line */
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500395 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
396 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
397 }
398
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400399 for (i = 0; i < chrlen; i++)
400 strbuf_addf(sb, encoded_fmt, p[i]);
401 line_len += encoded_len;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000402 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000403 strbuf_addstr(sb, "?=");
404}
405
Jeff Kingd1053242014-05-01 21:07:22 -0400406const char *show_ident_date(const struct ident_split *ident,
Jeff Kinga5481a62015-06-25 12:55:02 -0400407 const struct date_mode *mode)
René Scharfe9dbe7c32013-04-17 20:33:35 +0200408{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200409 timestamp_t date = 0;
Jeff King3f419d42014-03-07 12:15:01 -0500410 long tz = 0;
René Scharfe9dbe7c32013-04-17 20:33:35 +0200411
412 if (ident->date_begin && ident->date_end)
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200413 date = parse_timestamp(ident->date_begin, NULL, 10);
Jeff King1dca1552014-02-24 02:46:37 -0500414 if (date_overflows(date))
415 date = 0;
416 else {
417 if (ident->tz_begin && ident->tz_end)
418 tz = strtol(ident->tz_begin, NULL, 10);
Jeff King3f419d42014-03-07 12:15:01 -0500419 if (tz >= INT_MAX || tz <= INT_MIN)
Jeff King1dca1552014-02-24 02:46:37 -0500420 tz = 0;
421 }
René Scharfe9dbe7c32013-04-17 20:33:35 +0200422 return show_date(date, tz, mode);
423}
424
Jeff King10f2fbf2013-07-03 03:07:48 -0400425void pp_user_info(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -0400426 const char *what, struct strbuf *sb,
427 const char *line, const char *encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000428{
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100429 struct ident_split ident;
René Scharfe9dbe7c32013-04-17 20:33:35 +0200430 char *line_end;
Antoine Pelissedffd3252013-01-05 22:26:42 +0100431 const char *mailbuf, *namebuf;
432 size_t namelen, maillen;
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200433 int max_length = 78; /* per rfc2822 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000434
Jeff King6bf13942011-05-26 18:27:49 -0400435 if (pp->fmt == CMIT_FMT_ONELINE)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000436 return;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100437
René Scharfe30e77bc2013-04-25 21:40:25 +0200438 line_end = strchrnul(line, '\n');
439 if (split_ident_line(&ident, line, line_end - line))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000440 return;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100441
Antoine Pelissedffd3252013-01-05 22:26:42 +0100442 mailbuf = ident.mail_begin;
443 maillen = ident.mail_end - ident.mail_begin;
444 namebuf = ident.name_begin;
445 namelen = ident.name_end - ident.name_begin;
446
447 if (pp->mailmap)
448 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
449
Eric Wong9f23e042016-06-05 04:46:39 +0000450 if (cmit_fmt_is_mail(pp->fmt)) {
Jeff King662cc302013-09-20 06:16:28 -0400451 if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
Jeff Kinga9080472013-07-03 03:08:22 -0400452 struct strbuf buf = STRBUF_INIT;
453
454 strbuf_addstr(&buf, "From: ");
455 strbuf_add(&buf, namebuf, namelen);
456 strbuf_addstr(&buf, " <");
457 strbuf_add(&buf, mailbuf, maillen);
458 strbuf_addstr(&buf, ">\n");
459 string_list_append(&pp->in_body_headers,
460 strbuf_detach(&buf, NULL));
461
462 mailbuf = pp->from_ident->mail_begin;
463 maillen = pp->from_ident->mail_end - mailbuf;
464 namebuf = pp->from_ident->name_begin;
465 namelen = pp->from_ident->name_end - namebuf;
466 }
467
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000468 strbuf_addstr(sb, "From: ");
René Scharfea0511b32013-04-25 21:43:56 +0200469 if (needs_rfc2047_encoding(namebuf, namelen, RFC2047_ADDRESS)) {
470 add_rfc2047(sb, namebuf, namelen,
Antoine Pelissedffd3252013-01-05 22:26:42 +0100471 encoding, RFC2047_ADDRESS);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200472 max_length = 76; /* per rfc2047 */
René Scharfea0511b32013-04-25 21:43:56 +0200473 } else if (needs_rfc822_quoting(namebuf, namelen)) {
Jeff King4d03c182011-04-08 18:40:36 -0400474 struct strbuf quoted = STRBUF_INIT;
René Scharfea0511b32013-04-25 21:43:56 +0200475 add_rfc822_quoted(&quoted, namebuf, namelen);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200476 strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
477 -6, 1, max_length);
Jeff King4d03c182011-04-08 18:40:36 -0400478 strbuf_release(&quoted);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200479 } else {
René Scharfea0511b32013-04-25 21:43:56 +0200480 strbuf_add_wrapped_bytes(sb, namebuf, namelen,
Antoine Pelissedffd3252013-01-05 22:26:42 +0100481 -6, 1, max_length);
Jeff King4d03c182011-04-08 18:40:36 -0400482 }
Antoine Pelissedffd3252013-01-05 22:26:42 +0100483
René Scharfe97a17e72013-04-25 21:41:57 +0200484 if (max_length <
485 last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
Jeff King990f6e32011-04-14 18:18:09 -0400486 strbuf_addch(sb, '\n');
René Scharfea0511b32013-04-25 21:43:56 +0200487 strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000488 } else {
René Scharfea0511b32013-04-25 21:43:56 +0200489 strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
490 (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
491 (int)namelen, namebuf, (int)maillen, mailbuf);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000492 }
Antoine Pelissedffd3252013-01-05 22:26:42 +0100493
Jeff King6bf13942011-05-26 18:27:49 -0400494 switch (pp->fmt) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000495 case CMIT_FMT_MEDIUM:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200496 strbuf_addf(sb, "Date: %s\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400497 show_ident_date(&ident, &pp->date_mode));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000498 break;
499 case CMIT_FMT_EMAIL:
Eric Wong9f23e042016-06-05 04:46:39 +0000500 case CMIT_FMT_MBOXRD:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200501 strbuf_addf(sb, "Date: %s\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400502 show_ident_date(&ident, DATE_MODE(RFC2822)));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000503 break;
504 case CMIT_FMT_FULLER:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200505 strbuf_addf(sb, "%sDate: %s\n", what,
Jeff Kinga5481a62015-06-25 12:55:02 -0400506 show_ident_date(&ident, &pp->date_mode));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000507 break;
508 default:
509 /* notin' */
510 break;
511 }
512}
513
Johannes Schindelin77356122016-06-22 22:20:16 +0200514static int is_blank_line(const char *line, int *len_p)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000515{
516 int len = *len_p;
Felipe Contreras35b2fa52013-10-31 03:25:42 -0600517 while (len && isspace(line[len - 1]))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000518 len--;
519 *len_p = len;
520 return !len;
521}
522
Johannes Schindelin77356122016-06-22 22:20:16 +0200523const char *skip_blank_lines(const char *msg)
René Scharfea0109662008-12-27 01:32:49 +0100524{
525 for (;;) {
526 int linelen = get_one_line(msg);
527 int ll = linelen;
528 if (!linelen)
529 break;
Johannes Schindelin77356122016-06-22 22:20:16 +0200530 if (!is_blank_line(msg, &ll))
René Scharfea0109662008-12-27 01:32:49 +0100531 break;
532 msg += linelen;
533 }
534 return msg;
535}
536
Jeff King6bf13942011-05-26 18:27:49 -0400537static void add_merge_info(const struct pretty_print_context *pp,
538 struct strbuf *sb, const struct commit *commit)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000539{
540 struct commit_list *parent = commit->parents;
541
Eric Wong9f23e042016-06-05 04:46:39 +0000542 if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000543 !parent || !parent->next)
544 return;
545
546 strbuf_addstr(sb, "Merge:");
547
548 while (parent) {
René Scharfea94bb682016-10-08 17:38:47 +0200549 struct object_id *oidp = &parent->item->object.oid;
550 strbuf_addch(sb, ' ');
Jeff King6bf13942011-05-26 18:27:49 -0400551 if (pp->abbrev)
René Scharfea94bb682016-10-08 17:38:47 +0200552 strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev);
553 else
554 strbuf_addstr(sb, oid_to_hex(oidp));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000555 parent = parent->next;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000556 }
557 strbuf_addch(sb, '\n');
558}
559
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400560static char *get_header(const char *msg, const char *key)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000561{
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400562 size_t len;
563 const char *v = find_commit_header(msg, key, &len);
564 return v ? xmemdupz(v, len) : NULL;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000565}
566
567static char *replace_encoding_header(char *buf, const char *encoding)
568{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500569 struct strbuf tmp = STRBUF_INIT;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000570 size_t start, len;
571 char *cp = buf;
572
573 /* guess if there is an encoding header before a \n\n */
René Scharfe68d6d6e2015-02-21 20:53:09 +0100574 while (!starts_with(cp, "encoding ")) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000575 cp = strchr(cp, '\n');
576 if (!cp || *++cp == '\n')
577 return buf;
578 }
579 start = cp - buf;
580 cp = strchr(cp, '\n');
581 if (!cp)
582 return buf; /* should not happen but be defensive */
583 len = cp + 1 - (buf + start);
584
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000585 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
586 if (is_encoding_utf8(encoding)) {
587 /* we have re-coded to UTF-8; drop the header */
588 strbuf_remove(&tmp, start, len);
589 } else {
590 /* just replaces XXXX in 'encoding XXXX\n' */
591 strbuf_splice(&tmp, start + strlen("encoding "),
592 len - strlen("encoding \n"),
593 encoding, strlen(encoding));
594 }
595 return strbuf_detach(&tmp, NULL);
596}
597
Jeff Kingb000c592014-06-10 17:39:30 -0400598const char *logmsg_reencode(const struct commit *commit,
599 char **commit_encoding,
600 const char *output_encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000601{
Brandon Casey330db182009-05-18 18:44:39 -0500602 static const char *utf8 = "UTF-8";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000603 const char *use_encoding;
604 char *encoding;
Jeff King8597ea32014-06-10 17:44:13 -0400605 const char *msg = get_commit_buffer(commit, NULL);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000606 char *out;
607
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000608 if (!output_encoding || !*output_encoding) {
609 if (commit_encoding)
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400610 *commit_encoding = get_header(msg, "encoding");
Jeff Kingdd0d3882013-01-26 04:44:06 -0500611 return msg;
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000612 }
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400613 encoding = get_header(msg, "encoding");
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000614 if (commit_encoding)
615 *commit_encoding = encoding;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000616 use_encoding = encoding ? encoding : utf8;
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500617 if (same_encoding(use_encoding, output_encoding)) {
618 /*
619 * No encoding work to be done. If we have no encoding header
620 * at all, then there's nothing to do, and we can return the
621 * message verbatim (whether newly allocated or not).
622 */
623 if (!encoding)
624 return msg;
625
626 /*
627 * Otherwise, we still want to munge the encoding header in the
628 * result, which will be done by modifying the buffer. If we
629 * are using a fresh copy, we can reuse it. But if we are using
Jeff Kingb66103c2014-06-10 17:41:39 -0400630 * the cached copy from get_commit_buffer, we need to duplicate it
631 * to avoid munging the cached copy.
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500632 */
Jeff King8597ea32014-06-10 17:44:13 -0400633 if (msg == get_cached_commit_buffer(commit, NULL))
Jeff Kingb66103c2014-06-10 17:41:39 -0400634 out = xstrdup(msg);
635 else
636 out = (char *)msg;
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500637 }
638 else {
639 /*
640 * There's actual encoding work to do. Do the reencoding, which
641 * still leaves the header to be replaced in the next step. At
642 * this point, we are done with msg. If we allocated a fresh
643 * copy, we can free it.
644 */
645 out = reencode_string(msg, output_encoding, use_encoding);
Jeff Kingb66103c2014-06-10 17:41:39 -0400646 if (out)
647 unuse_commit_buffer(commit, msg);
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500648 }
649
650 /*
651 * This replacement actually consumes the buffer we hand it, so we do
652 * not have to worry about freeing the old "out" here.
653 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000654 if (out)
655 out = replace_encoding_header(out, output_encoding);
656
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000657 if (!commit_encoding)
658 free(encoding);
Jeff Kingdd0d3882013-01-26 04:44:06 -0500659 /*
660 * If the re-encoding failed, out might be NULL here; in that
661 * case we just return the commit message verbatim.
662 */
663 return out ? out : msg;
664}
665
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100666static int mailmap_name(const char **email, size_t *email_len,
667 const char **name, size_t *name_len)
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100668{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100669 static struct string_list *mail_map;
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100670 if (!mail_map) {
671 mail_map = xcalloc(1, sizeof(*mail_map));
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100672 read_mailmap(mail_map, NULL);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100673 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100674 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100675}
676
Marco Costalbac3a670d2008-02-09 15:40:19 +0100677static size_t format_person_part(struct strbuf *sb, char part,
Jeff Kinga5481a62015-06-25 12:55:02 -0400678 const char *msg, int len,
679 const struct date_mode *dmode)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000680{
Marco Costalbac3a670d2008-02-09 15:40:19 +0100681 /* currently all placeholders have same length */
682 const int placeholder_len = 2;
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800683 struct ident_split s;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100684 const char *name, *mail;
685 size_t maillen, namelen;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000686
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800687 if (split_ident_line(&s, msg, len) < 0)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100688 goto skip;
689
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100690 name = s.name_begin;
691 namelen = s.name_end - s.name_begin;
692 mail = s.mail_begin;
693 maillen = s.mail_end - s.mail_begin;
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100694
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100695 if (part == 'N' || part == 'E') /* mailmap lookup */
696 mailmap_name(&mail, &maillen, &name, &namelen);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100697 if (part == 'n' || part == 'N') { /* name */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100698 strbuf_add(sb, name, namelen);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100699 return placeholder_len;
700 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100701 if (part == 'e' || part == 'E') { /* email */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100702 strbuf_add(sb, mail, maillen);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100703 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100704 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000705
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800706 if (!s.date_begin)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100707 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000708
René Scharfecde75e52007-11-09 01:49:42 +0100709 if (part == 't') { /* date, UNIX timestamp */
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800710 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100711 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100712 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000713
René Scharfecde75e52007-11-09 01:49:42 +0100714 switch (part) {
715 case 'd': /* date */
René Scharfe9dbe7c32013-04-17 20:33:35 +0200716 strbuf_addstr(sb, show_ident_date(&s, dmode));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100717 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100718 case 'D': /* date, RFC2822 style */
Jeff Kinga5481a62015-06-25 12:55:02 -0400719 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100720 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100721 case 'r': /* date, relative */
Jeff Kinga5481a62015-06-25 12:55:02 -0400722 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100723 return placeholder_len;
Beat Bolli466fb672014-08-29 18:58:42 +0200724 case 'i': /* date, ISO 8601-like */
Jeff Kinga5481a62015-06-25 12:55:02 -0400725 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100726 return placeholder_len;
Beat Bolli466fb672014-08-29 18:58:42 +0200727 case 'I': /* date, ISO 8601 strict */
Jeff Kinga5481a62015-06-25 12:55:02 -0400728 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
Beat Bolli466fb672014-08-29 18:58:42 +0200729 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100730 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100731
732skip:
733 /*
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800734 * reading from either a bogus commit, or a reflog entry with
735 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
736 * to compute a valid return value.
Marco Costalbac3a670d2008-02-09 15:40:19 +0100737 */
738 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
739 || part == 'D' || part == 'r' || part == 'i')
740 return placeholder_len;
741
742 return 0; /* unknown placeholder */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000743}
744
René Scharfef29d5952007-11-10 12:14:20 +0100745struct chunk {
746 size_t off;
747 size_t len;
748};
749
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000750enum flush_type {
751 no_flush,
752 flush_right,
753 flush_left,
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +1000754 flush_left_and_steal,
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000755 flush_both
756};
757
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000758enum trunc_type {
759 trunc_none,
760 trunc_left,
761 trunc_middle,
762 trunc_right
763};
764
René Scharfef29d5952007-11-10 12:14:20 +0100765struct format_commit_context {
766 const struct commit *commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200767 const struct pretty_print_context *pretty_ctx;
René Scharfef53bd742008-12-27 01:49:21 +0100768 unsigned commit_header_parsed:1;
769 unsigned commit_message_parsed:1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +0200770 struct signature_check signature_check;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000771 enum flush_type flush_type;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000772 enum trunc_type truncate;
Jeff Kingb000c592014-06-10 17:39:30 -0400773 const char *message;
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +1000774 char *commit_encoding;
René Scharfe02edd562009-10-17 23:04:19 +0200775 size_t width, indent1, indent2;
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +1000776 int auto_color;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000777 int padding;
René Scharfef29d5952007-11-10 12:14:20 +0100778
779 /* These offsets are relative to the start of the commit message. */
René Scharfef29d5952007-11-10 12:14:20 +0100780 struct chunk author;
781 struct chunk committer;
René Scharfef53bd742008-12-27 01:49:21 +0100782 size_t message_off;
783 size_t subject_off;
René Scharfef29d5952007-11-10 12:14:20 +0100784 size_t body_off;
René Scharfeb9c62322007-11-10 12:18:26 +0100785
786 /* The following ones are relative to the result struct strbuf. */
René Scharfe02edd562009-10-17 23:04:19 +0200787 size_t wrap_start;
René Scharfef29d5952007-11-10 12:14:20 +0100788};
789
790static void parse_commit_header(struct format_commit_context *context)
791{
Pat Notz177b29d2010-11-02 13:59:08 -0600792 const char *msg = context->message;
René Scharfef29d5952007-11-10 12:14:20 +0100793 int i;
René Scharfef29d5952007-11-10 12:14:20 +0100794
René Scharfef53bd742008-12-27 01:49:21 +0100795 for (i = 0; msg[i]; i++) {
René Scharfee3f1da92014-10-04 20:54:50 +0200796 const char *name;
René Scharfef29d5952007-11-10 12:14:20 +0100797 int eol;
798 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
799 ; /* do nothing */
800
René Scharfef29d5952007-11-10 12:14:20 +0100801 if (i == eol) {
René Scharfef53bd742008-12-27 01:49:21 +0100802 break;
René Scharfee3f1da92014-10-04 20:54:50 +0200803 } else if (skip_prefix(msg + i, "author ", &name)) {
804 context->author.off = name - msg;
805 context->author.len = msg + eol - name;
806 } else if (skip_prefix(msg + i, "committer ", &name)) {
807 context->committer.off = name - msg;
808 context->committer.len = msg + eol - name;
René Scharfef29d5952007-11-10 12:14:20 +0100809 }
810 i = eol;
811 }
René Scharfef53bd742008-12-27 01:49:21 +0100812 context->message_off = i;
René Scharfef29d5952007-11-10 12:14:20 +0100813 context->commit_header_parsed = 1;
814}
815
Stephen Boyd46d164b2009-03-22 19:14:01 -0700816static int istitlechar(char c)
817{
818 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
819 (c >= '0' && c <= '9') || c == '.' || c == '_';
820}
821
822static void format_sanitized_subject(struct strbuf *sb, const char *msg)
823{
824 size_t trimlen;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700825 size_t start_len = sb->len;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700826 int space = 2;
827
828 for (; *msg && *msg != '\n'; msg++) {
829 if (istitlechar(*msg)) {
830 if (space == 1)
831 strbuf_addch(sb, '-');
832 space = 0;
833 strbuf_addch(sb, *msg);
834 if (*msg == '.')
835 while (*(msg+1) == '.')
836 msg++;
837 } else
838 space |= 1;
839 }
840
841 /* trim any trailing '.' or '-' characters */
842 trimlen = 0;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700843 while (sb->len - trimlen > start_len &&
844 (sb->buf[sb->len - 1 - trimlen] == '.'
845 || sb->buf[sb->len - 1 - trimlen] == '-'))
Stephen Boyd46d164b2009-03-22 19:14:01 -0700846 trimlen++;
847 strbuf_remove(sb, sb->len - trimlen, trimlen);
848}
849
René Scharfecec08712009-01-06 21:41:06 +0100850const char *format_subject(struct strbuf *sb, const char *msg,
851 const char *line_separator)
René Scharfe88c44732008-12-27 01:39:35 +0100852{
853 int first = 1;
854
855 for (;;) {
856 const char *line = msg;
857 int linelen = get_one_line(line);
858
859 msg += linelen;
Johannes Schindelin77356122016-06-22 22:20:16 +0200860 if (!linelen || is_blank_line(line, &linelen))
René Scharfe88c44732008-12-27 01:39:35 +0100861 break;
862
René Scharfef53bd742008-12-27 01:49:21 +0100863 if (!sb)
864 continue;
René Scharfe88c44732008-12-27 01:39:35 +0100865 strbuf_grow(sb, linelen + 2);
866 if (!first)
867 strbuf_addstr(sb, line_separator);
868 strbuf_add(sb, line, linelen);
869 first = 0;
870 }
871 return msg;
872}
873
René Scharfef53bd742008-12-27 01:49:21 +0100874static void parse_commit_message(struct format_commit_context *c)
875{
Pat Notz177b29d2010-11-02 13:59:08 -0600876 const char *msg = c->message + c->message_off;
877 const char *start = c->message;
René Scharfef53bd742008-12-27 01:49:21 +0100878
Johannes Schindelin77356122016-06-22 22:20:16 +0200879 msg = skip_blank_lines(msg);
René Scharfef53bd742008-12-27 01:49:21 +0100880 c->subject_off = msg - start;
881
882 msg = format_subject(NULL, msg, NULL);
Johannes Schindelin77356122016-06-22 22:20:16 +0200883 msg = skip_blank_lines(msg);
René Scharfef53bd742008-12-27 01:49:21 +0100884 c->body_off = msg - start;
885
886 c->commit_message_parsed = 1;
887}
888
René Scharfe02edd562009-10-17 23:04:19 +0200889static void strbuf_wrap(struct strbuf *sb, size_t pos,
890 size_t width, size_t indent1, size_t indent2)
891{
892 struct strbuf tmp = STRBUF_INIT;
893
894 if (pos)
895 strbuf_add(&tmp, sb->buf, pos);
896 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
897 (int) indent1, (int) indent2, (int) width);
898 strbuf_swap(&tmp, sb);
899 strbuf_release(&tmp);
900}
901
902static void rewrap_message_tail(struct strbuf *sb,
903 struct format_commit_context *c,
904 size_t new_width, size_t new_indent1,
905 size_t new_indent2)
906{
907 if (c->width == new_width && c->indent1 == new_indent1 &&
908 c->indent2 == new_indent2)
909 return;
René Scharfe32ca4242009-11-08 02:04:21 +0100910 if (c->wrap_start < sb->len)
René Scharfe02edd562009-10-17 23:04:19 +0200911 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
912 c->wrap_start = sb->len;
913 c->width = new_width;
914 c->indent1 = new_indent1;
915 c->indent2 = new_indent2;
916}
917
Jeff Kingcd1957f2011-12-16 06:40:24 -0500918static int format_reflog_person(struct strbuf *sb,
919 char part,
920 struct reflog_walk_info *log,
Jeff Kinga5481a62015-06-25 12:55:02 -0400921 const struct date_mode *dmode)
Jeff Kingcd1957f2011-12-16 06:40:24 -0500922{
923 const char *ident;
924
925 if (!log)
926 return 2;
927
928 ident = get_reflog_ident(log);
929 if (!ident)
930 return 2;
931
932 return format_person_part(sb, part, ident, strlen(ident), dmode);
933}
934
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000935static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
936 const char *placeholder,
937 struct format_commit_context *c)
938{
René Scharfee3f1da92014-10-04 20:54:50 +0200939 const char *rest = placeholder;
Jeff King18fb7ff2017-07-13 11:08:46 -0400940 const char *basic_color = NULL;
René Scharfee3f1da92014-10-04 20:54:50 +0200941
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000942 if (placeholder[1] == '(') {
943 const char *begin = placeholder + 2;
944 const char *end = strchr(begin, ')');
945 char color[COLOR_MAXLEN];
946
947 if (!end)
948 return 0;
Jeff King18fb7ff2017-07-13 11:08:46 -0400949
René Scharfee3f1da92014-10-04 20:54:50 +0200950 if (skip_prefix(begin, "auto,", &begin)) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000951 if (!want_color(c->pretty_ctx->color))
952 return end - placeholder + 1;
Jeff King18fb7ff2017-07-13 11:08:46 -0400953 } else if (skip_prefix(begin, "always,", &begin)) {
954 /* nothing to do; we do not respect want_color at all */
955 } else {
956 /* the default is the same as "auto" */
957 if (!want_color(c->pretty_ctx->color))
958 return end - placeholder + 1;
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000959 }
Jeff King18fb7ff2017-07-13 11:08:46 -0400960
Jeff Kingf6c5a292014-10-07 15:33:09 -0400961 if (color_parse_mem(begin, end - begin, color) < 0)
962 die(_("unable to parse --pretty format"));
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000963 strbuf_addstr(sb, color);
964 return end - placeholder + 1;
965 }
Jeff King18fb7ff2017-07-13 11:08:46 -0400966
967 /*
968 * We handle things like "%C(red)" above; for historical reasons, there
969 * are a few colors that can be specified without parentheses (and
970 * they cannot support things like "auto" or "always" at all).
971 */
René Scharfee3f1da92014-10-04 20:54:50 +0200972 if (skip_prefix(placeholder + 1, "red", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -0400973 basic_color = GIT_COLOR_RED;
René Scharfee3f1da92014-10-04 20:54:50 +0200974 else if (skip_prefix(placeholder + 1, "green", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -0400975 basic_color = GIT_COLOR_GREEN;
René Scharfee3f1da92014-10-04 20:54:50 +0200976 else if (skip_prefix(placeholder + 1, "blue", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -0400977 basic_color = GIT_COLOR_BLUE;
René Scharfee3f1da92014-10-04 20:54:50 +0200978 else if (skip_prefix(placeholder + 1, "reset", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -0400979 basic_color = GIT_COLOR_RESET;
980
981 if (basic_color && want_color(c->pretty_ctx->color))
982 strbuf_addstr(sb, basic_color);
983
René Scharfee3f1da92014-10-04 20:54:50 +0200984 return rest - placeholder;
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000985}
986
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000987static size_t parse_padding_placeholder(struct strbuf *sb,
988 const char *placeholder,
989 struct format_commit_context *c)
990{
991 const char *ch = placeholder;
992 enum flush_type flush_type;
993 int to_column = 0;
994
995 switch (*ch++) {
996 case '<':
997 flush_type = flush_right;
998 break;
999 case '>':
1000 if (*ch == '<') {
1001 flush_type = flush_both;
1002 ch++;
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10001003 } else if (*ch == '>') {
1004 flush_type = flush_left_and_steal;
1005 ch++;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001006 } else
1007 flush_type = flush_left;
1008 break;
1009 default:
1010 return 0;
1011 }
1012
1013 /* the next value means "wide enough to that column" */
1014 if (*ch == '|') {
1015 to_column = 1;
1016 ch++;
1017 }
1018
1019 if (*ch == '(') {
1020 const char *start = ch + 1;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001021 const char *end = start + strcspn(start, ",)");
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001022 char *next;
1023 int width;
1024 if (!end || end == start)
1025 return 0;
Nguyễn Thái Ngọc Duy066790d2016-06-16 20:18:38 +07001026 width = strtol(start, &next, 10);
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001027 if (next == start || width == 0)
1028 return 0;
Nguyễn Thái Ngọc Duy066790d2016-06-16 20:18:38 +07001029 if (width < 0) {
1030 if (to_column)
1031 width += term_columns();
1032 if (width < 0)
1033 return 0;
1034 }
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001035 c->padding = to_column ? -width : width;
1036 c->flush_type = flush_type;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001037
1038 if (*end == ',') {
1039 start = end + 1;
1040 end = strchr(start, ')');
1041 if (!end || end == start)
1042 return 0;
Christian Couder59556542013-11-30 21:55:40 +01001043 if (starts_with(start, "trunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001044 c->truncate = trunc_right;
Christian Couder59556542013-11-30 21:55:40 +01001045 else if (starts_with(start, "ltrunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001046 c->truncate = trunc_left;
Christian Couder59556542013-11-30 21:55:40 +01001047 else if (starts_with(start, "mtrunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001048 c->truncate = trunc_middle;
1049 else
1050 return 0;
1051 } else
1052 c->truncate = trunc_none;
1053
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001054 return end - placeholder + 1;
1055 }
1056 return 0;
1057}
1058
Taylor Blau84ff0532017-10-01 09:18:47 -07001059static int match_placeholder_arg(const char *to_parse, const char *candidate,
1060 const char **end)
1061{
1062 const char *p;
1063
1064 if (!(skip_prefix(to_parse, candidate, &p)))
1065 return 0;
1066 if (*p == ',') {
1067 *end = p + 1;
1068 return 1;
1069 }
1070 if (*p == ')') {
1071 *end = p;
1072 return 1;
1073 }
1074 return 0;
1075}
1076
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001077static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1078 const char *placeholder,
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001079 void *context)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001080{
René Scharfef29d5952007-11-10 12:14:20 +01001081 struct format_commit_context *c = context;
1082 const struct commit *commit = c->commit;
Pat Notz177b29d2010-11-02 13:59:08 -06001083 const char *msg = c->message;
René Scharfef29d5952007-11-10 12:14:20 +01001084 struct commit_list *p;
Jeff King58311c62017-08-15 06:25:27 -04001085 const char *arg;
René Scharfed2330972016-09-03 17:59:20 +02001086 int ch;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001087
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001088 /* these are independent of the commit */
René Scharfecde75e52007-11-09 01:49:42 +01001089 switch (placeholder[0]) {
1090 case 'C':
Christian Couder59556542013-11-30 21:55:40 +01001091 if (starts_with(placeholder + 1, "(auto)")) {
Edward Thomsonb15a3e02016-05-26 22:46:10 -05001092 c->auto_color = want_color(c->pretty_ctx->color);
René Scharfe82b83da2016-09-29 20:13:05 +02001093 if (c->auto_color && sb->len)
René Scharfec99ad272016-09-17 20:25:24 +02001094 strbuf_addstr(sb, GIT_COLOR_RESET);
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001095 return 7; /* consumed 7 bytes, "C(auto)" */
1096 } else {
1097 int ret = parse_color(sb, placeholder, c);
1098 if (ret)
1099 c->auto_color = 0;
1100 /*
1101 * Otherwise, we decided to treat %C<unknown>
1102 * as a literal string, and the previous
1103 * %C(auto) is still valid.
1104 */
1105 return ret;
Jeff Kingc0029222009-01-17 10:38:46 -05001106 }
René Scharfecde75e52007-11-09 01:49:42 +01001107 case 'n': /* newline */
1108 strbuf_addch(sb, '\n');
Marco Costalbac3a670d2008-02-09 15:40:19 +01001109 return 1;
Govind Salinas42c8c742008-03-21 10:05:06 -05001110 case 'x':
1111 /* %x00 == NUL, %x0a == LF, etc. */
René Scharfed2330972016-09-03 17:59:20 +02001112 ch = hex2chr(placeholder + 1);
1113 if (ch < 0)
Govind Salinas42c8c742008-03-21 10:05:06 -05001114 return 0;
René Scharfed2330972016-09-03 17:59:20 +02001115 strbuf_addch(sb, ch);
1116 return 3;
René Scharfe02edd562009-10-17 23:04:19 +02001117 case 'w':
1118 if (placeholder[1] == '(') {
1119 unsigned long width = 0, indent1 = 0, indent2 = 0;
1120 char *next;
1121 const char *start = placeholder + 2;
1122 const char *end = strchr(start, ')');
1123 if (!end)
1124 return 0;
1125 if (end > start) {
1126 width = strtoul(start, &next, 10);
1127 if (*next == ',') {
1128 indent1 = strtoul(next + 1, &next, 10);
1129 if (*next == ',') {
1130 indent2 = strtoul(next + 1,
1131 &next, 10);
1132 }
1133 }
1134 if (*next != ')')
1135 return 0;
1136 }
1137 rewrap_message_tail(sb, c, width, indent1, indent2);
1138 return end - placeholder + 1;
1139 } else
1140 return 0;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001141
1142 case '<':
1143 case '>':
1144 return parse_padding_placeholder(sb, placeholder, c);
René Scharfecde75e52007-11-09 01:49:42 +01001145 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001146
1147 /* these depend on the commit */
1148 if (!commit->object.parsed)
brian m. carlsonc251c832017-05-06 22:10:38 +00001149 parse_object(&commit->object.oid);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001150
René Scharfecde75e52007-11-09 01:49:42 +01001151 switch (placeholder[0]) {
1152 case 'H': /* commit hash */
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001153 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001154 strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001155 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001156 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001157 case 'h': /* abbreviated commit hash */
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001158 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
René Scharfe1eb47f12016-08-06 17:41:01 +02001159 strbuf_add_unique_abbrev(sb, commit->object.oid.hash,
1160 c->pretty_ctx->abbrev);
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001161 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001162 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001163 case 'T': /* tree hash */
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001164 strbuf_addstr(sb, oid_to_hex(&commit->tree->object.oid));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001165 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001166 case 't': /* abbreviated tree hash */
René Scharfe1eb47f12016-08-06 17:41:01 +02001167 strbuf_add_unique_abbrev(sb, commit->tree->object.oid.hash,
1168 c->pretty_ctx->abbrev);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001169 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001170 case 'P': /* parent hashes */
1171 for (p = commit->parents; p; p = p->next) {
1172 if (p != commit->parents)
1173 strbuf_addch(sb, ' ');
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001174 strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
René Scharfecde75e52007-11-09 01:49:42 +01001175 }
Marco Costalbac3a670d2008-02-09 15:40:19 +01001176 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001177 case 'p': /* abbreviated parent hashes */
1178 for (p = commit->parents; p; p = p->next) {
1179 if (p != commit->parents)
1180 strbuf_addch(sb, ' ');
René Scharfe1eb47f12016-08-06 17:41:01 +02001181 strbuf_add_unique_abbrev(sb, p->item->object.oid.hash,
1182 c->pretty_ctx->abbrev);
René Scharfecde75e52007-11-09 01:49:42 +01001183 }
Marco Costalbac3a670d2008-02-09 15:40:19 +01001184 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001185 case 'm': /* left/right/bottom */
Michael J Gruber1df2d652011-03-07 13:31:39 +01001186 strbuf_addstr(sb, get_revision_mark(NULL, commit));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001187 return 1;
René Scharfe3b3d4432008-09-04 23:40:03 +02001188 case 'd':
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +10001189 load_ref_decorations(DECORATE_SHORT_REFS);
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001190 format_decorations(sb, commit, c->auto_color);
René Scharfe3b3d4432008-09-04 23:40:03 +02001191 return 1;
Harry Jeffery92710952014-09-18 21:53:53 +01001192 case 'D':
1193 load_ref_decorations(DECORATE_SHORT_REFS);
1194 format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1195 return 1;
Thomas Rast8f8f5472009-10-19 17:48:10 +02001196 case 'g': /* reflog info */
1197 switch(placeholder[1]) {
1198 case 'd': /* reflog selector */
1199 case 'D':
1200 if (c->pretty_ctx->reflog_info)
1201 get_reflog_selector(sb,
1202 c->pretty_ctx->reflog_info,
Jeff Kinga5481a62015-06-25 12:55:02 -04001203 &c->pretty_ctx->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -07001204 c->pretty_ctx->date_mode_explicit,
Thomas Rast8f8f5472009-10-19 17:48:10 +02001205 (placeholder[1] == 'd'));
1206 return 2;
1207 case 's': /* reflog message */
1208 if (c->pretty_ctx->reflog_info)
1209 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1210 return 2;
Jeff Kingcd1957f2011-12-16 06:40:24 -05001211 case 'n':
1212 case 'N':
1213 case 'e':
1214 case 'E':
1215 return format_reflog_person(sb,
1216 placeholder[1],
1217 c->pretty_ctx->reflog_info,
Jeff Kinga5481a62015-06-25 12:55:02 -04001218 &c->pretty_ctx->date_mode);
Thomas Rast8f8f5472009-10-19 17:48:10 +02001219 }
1220 return 0; /* unknown %g placeholder */
Johannes Schindelin8b208f02009-10-09 12:22:05 +02001221 case 'N':
Junio C Hamanoddf333f2012-10-17 18:51:47 -07001222 if (c->pretty_ctx->notes_message) {
1223 strbuf_addstr(sb, c->pretty_ctx->notes_message);
Johannes Gilger5b163602010-04-13 22:31:12 +02001224 return 1;
1225 }
1226 return 0;
René Scharfecde75e52007-11-09 01:49:42 +01001227 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001228
Junio C Hamanof6667c52011-10-21 21:06:02 -07001229 if (placeholder[0] == 'G') {
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001230 if (!c->signature_check.result)
1231 check_commit_signature(c->commit, &(c->signature_check));
Junio C Hamanof6667c52011-10-21 21:06:02 -07001232 switch (placeholder[1]) {
1233 case 'G':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001234 if (c->signature_check.gpg_output)
1235 strbuf_addstr(sb, c->signature_check.gpg_output);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001236 break;
1237 case '?':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001238 switch (c->signature_check.result) {
Junio C Hamanof6667c52011-10-21 21:06:02 -07001239 case 'G':
1240 case 'B':
Michael J Gruber661a1802016-10-12 15:04:15 +02001241 case 'E':
Sebastian Göttee290c4b2013-03-31 18:03:22 +02001242 case 'U':
1243 case 'N':
Michael J Gruber661a1802016-10-12 15:04:15 +02001244 case 'X':
1245 case 'Y':
1246 case 'R':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001247 strbuf_addch(sb, c->signature_check.result);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001248 }
1249 break;
1250 case 'S':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001251 if (c->signature_check.signer)
1252 strbuf_addstr(sb, c->signature_check.signer);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001253 break;
Michael J Gruber0174eea2013-02-14 17:04:46 +01001254 case 'K':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001255 if (c->signature_check.key)
1256 strbuf_addstr(sb, c->signature_check.key);
Michael J Gruber0174eea2013-02-14 17:04:46 +01001257 break;
Jeff Kingaa4b78d2014-06-16 20:07:07 -04001258 default:
1259 return 0;
Junio C Hamanof6667c52011-10-21 21:06:02 -07001260 }
1261 return 2;
1262 }
1263
1264
René Scharfecde75e52007-11-09 01:49:42 +01001265 /* For the rest we have to parse the commit header. */
René Scharfef29d5952007-11-10 12:14:20 +01001266 if (!c->commit_header_parsed)
1267 parse_commit_header(c);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001268
René Scharfef29d5952007-11-10 12:14:20 +01001269 switch (placeholder[0]) {
Marco Costalbac3a670d2008-02-09 15:40:19 +01001270 case 'a': /* author ... */
1271 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -04001272 msg + c->author.off, c->author.len,
Jeff Kinga5481a62015-06-25 12:55:02 -04001273 &c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001274 case 'c': /* committer ... */
1275 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -04001276 msg + c->committer.off, c->committer.len,
Jeff Kinga5481a62015-06-25 12:55:02 -04001277 &c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001278 case 'e': /* encoding */
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001279 if (c->commit_encoding)
1280 strbuf_addstr(sb, c->commit_encoding);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001281 return 1;
Eli Barzilay1367b122010-03-24 22:51:52 -04001282 case 'B': /* raw body */
1283 /* message_off is always left at the initial newline */
1284 strbuf_addstr(sb, msg + c->message_off + 1);
1285 return 1;
René Scharfef53bd742008-12-27 01:49:21 +01001286 }
1287
1288 /* Now we need to parse the commit message. */
1289 if (!c->commit_message_parsed)
1290 parse_commit_message(c);
1291
1292 switch (placeholder[0]) {
1293 case 's': /* subject */
1294 format_subject(sb, msg + c->subject_off, " ");
1295 return 1;
Stephen Boyd46d164b2009-03-22 19:14:01 -07001296 case 'f': /* sanitized subject */
1297 format_sanitized_subject(sb, msg + c->subject_off);
1298 return 1;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001299 case 'b': /* body */
René Scharfef29d5952007-11-10 12:14:20 +01001300 strbuf_addstr(sb, msg + c->body_off);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001301 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001302 }
Jacob Kellerd9f31fb2016-11-18 16:58:14 -08001303
Jeff King58311c62017-08-15 06:25:27 -04001304 if (skip_prefix(placeholder, "(trailers", &arg)) {
Jeff Kinga388b102017-08-15 06:23:56 -04001305 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
Taylor Blau84ff0532017-10-01 09:18:47 -07001306 if (*arg == ':') {
1307 arg++;
1308 for (;;) {
1309 if (match_placeholder_arg(arg, "only", &arg))
1310 opts.only_trailers = 1;
1311 else if (match_placeholder_arg(arg, "unfold", &arg))
1312 opts.unfold = 1;
1313 else
1314 break;
1315 }
Jeff King58311c62017-08-15 06:25:27 -04001316 }
1317 if (*arg == ')') {
1318 format_trailers_from_commit(sb, msg + c->subject_off, &opts);
1319 return arg - placeholder + 1;
1320 }
Jacob Kellerd9f31fb2016-11-18 16:58:14 -08001321 }
1322
Marco Costalbac3a670d2008-02-09 15:40:19 +01001323 return 0; /* unknown placeholder */
René Scharfecde75e52007-11-09 01:49:42 +01001324}
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001325
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001326static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1327 const char *placeholder,
1328 struct format_commit_context *c)
1329{
1330 struct strbuf local_sb = STRBUF_INIT;
1331 int total_consumed = 0, len, padding = c->padding;
1332 if (padding < 0) {
1333 const char *start = strrchr(sb->buf, '\n');
1334 int occupied;
1335 if (!start)
1336 start = sb->buf;
1337 occupied = utf8_strnwidth(start, -1, 1);
Josef Kufner3ad87c82016-06-16 20:18:37 +07001338 occupied += c->pretty_ctx->graph_width;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001339 padding = (-padding) - occupied;
1340 }
1341 while (1) {
1342 int modifier = *placeholder == 'C';
1343 int consumed = format_commit_one(&local_sb, placeholder, c);
1344 total_consumed += consumed;
1345
1346 if (!modifier)
1347 break;
1348
1349 placeholder += consumed;
1350 if (*placeholder != '%')
1351 break;
1352 placeholder++;
1353 total_consumed++;
1354 }
1355 len = utf8_strnwidth(local_sb.buf, -1, 1);
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10001356
1357 if (c->flush_type == flush_left_and_steal) {
1358 const char *ch = sb->buf + sb->len - 1;
1359 while (len > padding && ch > sb->buf) {
1360 const char *p;
1361 if (*ch == ' ') {
1362 ch--;
1363 padding++;
1364 continue;
1365 }
1366 /* check for trailing ansi sequences */
1367 if (*ch != 'm')
1368 break;
1369 p = ch - 1;
1370 while (ch - p < 10 && *p != '\033')
1371 p--;
1372 if (*p != '\033' ||
1373 ch + 1 - p != display_mode_esc_sequence_len(p))
1374 break;
1375 /*
1376 * got a good ansi sequence, put it back to
1377 * local_sb as we're cutting sb
1378 */
1379 strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1380 ch = p - 1;
1381 }
1382 strbuf_setlen(sb, ch + 1 - sb->buf);
1383 c->flush_type = flush_left;
1384 }
1385
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001386 if (len > padding) {
1387 switch (c->truncate) {
1388 case trunc_left:
1389 strbuf_utf8_replace(&local_sb,
1390 0, len - (padding - 2),
1391 "..");
1392 break;
1393 case trunc_middle:
1394 strbuf_utf8_replace(&local_sb,
1395 padding / 2 - 1,
1396 len - (padding - 2),
1397 "..");
1398 break;
1399 case trunc_right:
1400 strbuf_utf8_replace(&local_sb,
1401 padding - 2, len - (padding - 2),
1402 "..");
1403 break;
1404 case trunc_none:
1405 break;
1406 }
René Scharfee992d1e2014-07-10 10:52:21 +02001407 strbuf_addbuf(sb, &local_sb);
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001408 } else {
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001409 int sb_len = sb->len, offset = 0;
1410 if (c->flush_type == flush_left)
1411 offset = padding - len;
1412 else if (c->flush_type == flush_both)
1413 offset = (padding - len) / 2;
1414 /*
1415 * we calculate padding in columns, now
1416 * convert it back to chars
1417 */
1418 padding = padding - len + local_sb.len;
René Scharfe415792e2014-09-07 09:06:42 +02001419 strbuf_addchars(sb, ' ', padding);
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001420 memcpy(sb->buf + sb_len + offset, local_sb.buf,
1421 local_sb.len);
1422 }
1423 strbuf_release(&local_sb);
1424 c->flush_type = no_flush;
1425 return total_consumed;
1426}
1427
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001428static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1429 const char *placeholder,
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001430 void *context)
1431{
1432 int consumed;
1433 size_t orig_len;
1434 enum {
1435 NO_MAGIC,
1436 ADD_LF_BEFORE_NON_EMPTY,
1437 DEL_LF_BEFORE_EMPTY,
Junio C Hamano223a9232010-06-22 09:45:22 -07001438 ADD_SP_BEFORE_NON_EMPTY
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001439 } magic = NO_MAGIC;
1440
1441 switch (placeholder[0]) {
1442 case '-':
1443 magic = DEL_LF_BEFORE_EMPTY;
1444 break;
1445 case '+':
1446 magic = ADD_LF_BEFORE_NON_EMPTY;
1447 break;
Michael J Gruber7b881762010-06-14 18:12:29 +02001448 case ' ':
1449 magic = ADD_SP_BEFORE_NON_EMPTY;
1450 break;
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001451 default:
1452 break;
1453 }
1454 if (magic != NO_MAGIC)
1455 placeholder++;
1456
1457 orig_len = sb->len;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001458 if (((struct format_commit_context *)context)->flush_type != no_flush)
1459 consumed = format_and_pad_commit(sb, placeholder, context);
1460 else
1461 consumed = format_commit_one(sb, placeholder, context);
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001462 if (magic == NO_MAGIC)
1463 return consumed;
1464
1465 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1466 while (sb->len && sb->buf[sb->len - 1] == '\n')
1467 strbuf_setlen(sb, sb->len - 1);
Michael J Gruber7b881762010-06-14 18:12:29 +02001468 } else if (orig_len != sb->len) {
1469 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1470 strbuf_insert(sb, orig_len, "\n", 1);
1471 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1472 strbuf_insert(sb, orig_len, " ", 1);
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001473 }
1474 return consumed + 1;
1475}
1476
Johannes Gilger5b163602010-04-13 22:31:12 +02001477static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1478 void *context)
1479{
1480 struct userformat_want *w = context;
1481
Michael J Gruber7b881762010-06-14 18:12:29 +02001482 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
Johannes Gilger5b163602010-04-13 22:31:12 +02001483 placeholder++;
1484
1485 switch (*placeholder) {
1486 case 'N':
1487 w->notes = 1;
1488 break;
1489 }
1490 return 0;
1491}
1492
1493void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1494{
1495 struct strbuf dummy = STRBUF_INIT;
1496
1497 if (!fmt) {
1498 if (!user_format)
1499 return;
1500 fmt = user_format;
1501 }
Junio C Hamanoa6253d12011-05-25 12:23:44 -07001502 strbuf_expand(&dummy, fmt, userformat_want_item, w);
Johannes Gilger5b163602010-04-13 22:31:12 +02001503 strbuf_release(&dummy);
1504}
1505
René Scharfecde75e52007-11-09 01:49:42 +01001506void format_commit_message(const struct commit *commit,
Junio C Hamano7f98ebc2009-10-15 22:59:41 -07001507 const char *format, struct strbuf *sb,
Thomas Rastdd2e7942009-10-19 17:48:08 +02001508 const struct pretty_print_context *pretty_ctx)
René Scharfecde75e52007-11-09 01:49:42 +01001509{
René Scharfef29d5952007-11-10 12:14:20 +01001510 struct format_commit_context context;
Pat Notz177b29d2010-11-02 13:59:08 -06001511 const char *output_enc = pretty_ctx->output_encoding;
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001512 const char *utf8 = "UTF-8";
René Scharfef29d5952007-11-10 12:14:20 +01001513
1514 memset(&context, 0, sizeof(context));
1515 context.commit = commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +02001516 context.pretty_ctx = pretty_ctx;
René Scharfe02edd562009-10-17 23:04:19 +02001517 context.wrap_start = sb->len;
Alexey Shumkin7d509872014-05-21 17:20:07 +04001518 /*
1519 * convert a commit message to UTF-8 first
1520 * as far as 'format_commit_item' assumes it in UTF-8
1521 */
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001522 context.message = logmsg_reencode(commit,
1523 &context.commit_encoding,
Alexey Shumkin7d509872014-05-21 17:20:07 +04001524 utf8);
Pat Notz177b29d2010-11-02 13:59:08 -06001525
Marco Costalbac3a670d2008-02-09 15:40:19 +01001526 strbuf_expand(sb, format, format_commit_item, &context);
René Scharfe02edd562009-10-17 23:04:19 +02001527 rewrap_message_tail(sb, &context, 0, 0, 0);
Pat Notz177b29d2010-11-02 13:59:08 -06001528
Alexey Shumkin7d509872014-05-21 17:20:07 +04001529 /* then convert a commit message to an actual output encoding */
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001530 if (output_enc) {
1531 if (same_encoding(utf8, output_enc))
1532 output_enc = NULL;
1533 } else {
1534 if (context.commit_encoding &&
1535 !same_encoding(context.commit_encoding, utf8))
1536 output_enc = context.commit_encoding;
1537 }
1538
1539 if (output_enc) {
1540 int outsz;
1541 char *out = reencode_string_len(sb->buf, sb->len,
1542 output_enc, utf8, &outsz);
1543 if (out)
1544 strbuf_attach(sb, out, outsz, outsz + 1);
1545 }
1546
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001547 free(context.commit_encoding);
Jeff Kingb66103c2014-06-10 17:41:39 -04001548 unuse_commit_buffer(commit, context.message);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001549}
1550
Jeff King10f2fbf2013-07-03 03:07:48 -04001551static void pp_header(struct pretty_print_context *pp,
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001552 const char *encoding,
1553 const struct commit *commit,
1554 const char **msg_p,
1555 struct strbuf *sb)
1556{
1557 int parents_shown = 0;
1558
1559 for (;;) {
René Scharfee3f1da92014-10-04 20:54:50 +02001560 const char *name, *line = *msg_p;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001561 int linelen = get_one_line(*msg_p);
1562
1563 if (!linelen)
1564 return;
1565 *msg_p += linelen;
1566
1567 if (linelen == 1)
1568 /* End of header */
1569 return;
1570
Jeff King6bf13942011-05-26 18:27:49 -04001571 if (pp->fmt == CMIT_FMT_RAW) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001572 strbuf_add(sb, line, linelen);
1573 continue;
1574 }
1575
Christian Couder59556542013-11-30 21:55:40 +01001576 if (starts_with(line, "parent ")) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001577 if (linelen != 48)
1578 die("bad parent line in commit");
1579 continue;
1580 }
1581
1582 if (!parents_shown) {
René Scharfe4bbaa1e2014-07-17 01:52:09 +02001583 unsigned num = commit_list_count(commit->parents);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001584 /* with enough slop */
1585 strbuf_grow(sb, num * 50 + 20);
Jeff King6bf13942011-05-26 18:27:49 -04001586 add_merge_info(pp, sb, commit);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001587 parents_shown = 1;
1588 }
1589
1590 /*
1591 * MEDIUM == DEFAULT shows only author with dates.
1592 * FULL shows both authors but not dates.
1593 * FULLER shows both authors and dates.
1594 */
René Scharfee3f1da92014-10-04 20:54:50 +02001595 if (skip_prefix(line, "author ", &name)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001596 strbuf_grow(sb, linelen + 80);
René Scharfee3f1da92014-10-04 20:54:50 +02001597 pp_user_info(pp, "Author", sb, name, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001598 }
René Scharfee3f1da92014-10-04 20:54:50 +02001599 if (skip_prefix(line, "committer ", &name) &&
Jeff King6bf13942011-05-26 18:27:49 -04001600 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001601 strbuf_grow(sb, linelen + 80);
René Scharfee3f1da92014-10-04 20:54:50 +02001602 pp_user_info(pp, "Commit", sb, name, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001603 }
1604 }
1605}
1606
Jeff King10f2fbf2013-07-03 03:07:48 -04001607void pp_title_line(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001608 const char **msg_p,
1609 struct strbuf *sb,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001610 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -07001611 int need_8bit_cte)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001612{
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +02001613 static const int max_length = 78; /* per rfc2047 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001614 struct strbuf title;
1615
1616 strbuf_init(&title, 80);
Jeff King9553d2b2011-05-26 18:28:17 -04001617 *msg_p = format_subject(&title, *msg_p,
1618 pp->preserve_subject ? "\n" : " ");
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001619
1620 strbuf_grow(sb, title.len + 1024);
René Scharfe6d167fd2017-03-01 12:37:07 +01001621 if (pp->print_email_subject) {
1622 if (pp->rev)
1623 fmt_output_email_subject(sb, pp->rev);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +02001624 if (needs_rfc2047_encoding(title.buf, title.len, RFC2047_SUBJECT))
1625 add_rfc2047(sb, title.buf, title.len,
1626 encoding, RFC2047_SUBJECT);
1627 else
1628 strbuf_add_wrapped_bytes(sb, title.buf, title.len,
1629 -last_line_length(sb), 1, max_length);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001630 } else {
1631 strbuf_addbuf(sb, &title);
1632 }
1633 strbuf_addch(sb, '\n');
1634
Jeff Kinga9080472013-07-03 03:08:22 -04001635 if (need_8bit_cte == 0) {
1636 int i;
1637 for (i = 0; i < pp->in_body_headers.nr; i++) {
1638 if (has_non_ascii(pp->in_body_headers.items[i].string)) {
1639 need_8bit_cte = 1;
1640 break;
1641 }
1642 }
1643 }
1644
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001645 if (need_8bit_cte > 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001646 const char *header_fmt =
1647 "MIME-Version: 1.0\n"
1648 "Content-Type: text/plain; charset=%s\n"
1649 "Content-Transfer-Encoding: 8bit\n";
1650 strbuf_addf(sb, header_fmt, encoding);
1651 }
Jeff King6bf13942011-05-26 18:27:49 -04001652 if (pp->after_subject) {
1653 strbuf_addstr(sb, pp->after_subject);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001654 }
Eric Wong9f23e042016-06-05 04:46:39 +00001655 if (cmit_fmt_is_mail(pp->fmt)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001656 strbuf_addch(sb, '\n');
1657 }
Jeff Kinga9080472013-07-03 03:08:22 -04001658
1659 if (pp->in_body_headers.nr) {
1660 int i;
1661 for (i = 0; i < pp->in_body_headers.nr; i++) {
1662 strbuf_addstr(sb, pp->in_body_headers.items[i].string);
1663 free(pp->in_body_headers.items[i].string);
1664 }
1665 string_list_clear(&pp->in_body_headers, 0);
1666 strbuf_addch(sb, '\n');
1667 }
1668
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001669 strbuf_release(&title);
1670}
1671
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001672static int pp_utf8_width(const char *start, const char *end)
1673{
1674 int width = 0;
1675 size_t remain = end - start;
1676
1677 while (remain) {
1678 int n = utf8_width(&start, &remain);
1679 if (n < 0 || !start)
1680 return -1;
1681 width += n;
1682 }
1683 return width;
1684}
1685
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001686static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001687 const char *line, int linelen)
1688{
1689 const char *tab;
1690
1691 while ((tab = memchr(line, '\t', linelen)) != NULL) {
1692 int width = pp_utf8_width(line, tab);
1693
1694 /*
1695 * If it wasn't well-formed utf8, or it
1696 * had characters with badly defined
1697 * width (control characters etc), just
1698 * give up on trying to align things.
1699 */
1700 if (width < 0)
1701 break;
1702
1703 /* Output the data .. */
1704 strbuf_add(sb, line, tab - line);
1705
1706 /* .. and the de-tabified tab */
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001707 strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001708
1709 /* Skip over the printed part .. */
1710 linelen -= tab + 1 - line;
1711 line = tab + 1;
1712 }
1713
1714 /*
1715 * Print out everything after the last tab without
1716 * worrying about width - there's nothing more to
1717 * align.
1718 */
1719 strbuf_add(sb, line, linelen);
1720}
1721
1722/*
1723 * pp_handle_indent() prints out the intendation, and
1724 * the whole line (without the final newline), after
1725 * de-tabifying.
1726 */
1727static void pp_handle_indent(struct pretty_print_context *pp,
1728 struct strbuf *sb, int indent,
1729 const char *line, int linelen)
1730{
1731 strbuf_addchars(sb, ' ', indent);
1732 if (pp->expand_tabs_in_log)
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001733 strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001734 else
1735 strbuf_add(sb, line, linelen);
1736}
1737
Eric Wong9f23e042016-06-05 04:46:39 +00001738static int is_mboxrd_from(const char *line, int len)
1739{
1740 /*
1741 * a line matching /^From $/ here would only have len == 4
1742 * at this point because is_empty_line would've trimmed all
1743 * trailing space
1744 */
1745 return len > 4 && starts_with(line + strspn(line, ">"), "From ");
1746}
1747
Jeff King10f2fbf2013-07-03 03:07:48 -04001748void pp_remainder(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001749 const char **msg_p,
1750 struct strbuf *sb,
1751 int indent)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001752{
1753 int first = 1;
1754 for (;;) {
1755 const char *line = *msg_p;
1756 int linelen = get_one_line(line);
1757 *msg_p += linelen;
1758
1759 if (!linelen)
1760 break;
1761
Johannes Schindelin77356122016-06-22 22:20:16 +02001762 if (is_blank_line(line, &linelen)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001763 if (first)
1764 continue;
Jeff King6bf13942011-05-26 18:27:49 -04001765 if (pp->fmt == CMIT_FMT_SHORT)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001766 break;
1767 }
1768 first = 0;
1769
1770 strbuf_grow(sb, linelen + indent + 20);
René Scharfe415792e2014-09-07 09:06:42 +02001771 if (indent)
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001772 pp_handle_indent(pp, sb, indent, line, linelen);
Junio C Hamano0893eec2016-03-29 15:49:24 -07001773 else if (pp->expand_tabs_in_log)
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001774 strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
1775 line, linelen);
Eric Wong9f23e042016-06-05 04:46:39 +00001776 else {
1777 if (pp->fmt == CMIT_FMT_MBOXRD &&
1778 is_mboxrd_from(line, linelen))
1779 strbuf_addch(sb, '>');
1780
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001781 strbuf_add(sb, line, linelen);
Eric Wong9f23e042016-06-05 04:46:39 +00001782 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001783 strbuf_addch(sb, '\n');
1784 }
1785}
1786
Jeff King10f2fbf2013-07-03 03:07:48 -04001787void pretty_print_commit(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -04001788 const struct commit *commit,
1789 struct strbuf *sb)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001790{
1791 unsigned long beginning_of_body;
1792 int indent = 4;
Jeff Kingdd0d3882013-01-26 04:44:06 -05001793 const char *msg;
Jeff Kingb000c592014-06-10 17:39:30 -04001794 const char *reencoded;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001795 const char *encoding;
Jeff King6bf13942011-05-26 18:27:49 -04001796 int need_8bit_cte = pp->need_8bit_cte;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001797
Jeff King6bf13942011-05-26 18:27:49 -04001798 if (pp->fmt == CMIT_FMT_USERFORMAT) {
1799 format_commit_message(commit, user_format, sb, pp);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001800 return;
1801 }
1802
Junio C Hamanoe297cf52012-10-17 17:12:55 -07001803 encoding = get_log_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +10001804 msg = reencoded = logmsg_reencode(commit, NULL, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001805
Eric Wong9f23e042016-06-05 04:46:39 +00001806 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001807 indent = 0;
1808
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001809 /*
1810 * We need to check and emit Content-type: to mark it
1811 * as 8-bit if we haven't done so.
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001812 */
Eric Wong9f23e042016-06-05 04:46:39 +00001813 if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001814 int i, ch, in_body;
1815
1816 for (in_body = i = 0; (ch = msg[i]); i++) {
1817 if (!in_body) {
1818 /* author could be non 7-bit ASCII but
1819 * the log may be so; skip over the
1820 * header part first.
1821 */
1822 if (ch == '\n' && msg[i+1] == '\n')
1823 in_body = 1;
1824 }
1825 else if (non_ascii(ch)) {
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001826 need_8bit_cte = 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001827 break;
1828 }
1829 }
1830 }
1831
Jeff King6bf13942011-05-26 18:27:49 -04001832 pp_header(pp, encoding, commit, &msg, sb);
René Scharfe6d167fd2017-03-01 12:37:07 +01001833 if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001834 strbuf_addch(sb, '\n');
1835 }
1836
1837 /* Skip excess blank lines at the beginning of body, if any... */
Johannes Schindelin77356122016-06-22 22:20:16 +02001838 msg = skip_blank_lines(msg);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001839
1840 /* These formats treat the title line specially. */
Eric Wong9f23e042016-06-05 04:46:39 +00001841 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
Jeff King6bf13942011-05-26 18:27:49 -04001842 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001843
1844 beginning_of_body = sb->len;
Jeff King6bf13942011-05-26 18:27:49 -04001845 if (pp->fmt != CMIT_FMT_ONELINE)
1846 pp_remainder(pp, &msg, sb, indent);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001847 strbuf_rtrim(sb);
1848
1849 /* Make sure there is an EOLN for the non-oneline case */
Jeff King6bf13942011-05-26 18:27:49 -04001850 if (pp->fmt != CMIT_FMT_ONELINE)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001851 strbuf_addch(sb, '\n');
1852
1853 /*
1854 * The caller may append additional body text in e-mail
1855 * format. Make sure we did not strip the blank line
1856 * between the header and the body.
1857 */
Eric Wong9f23e042016-06-05 04:46:39 +00001858 if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001859 strbuf_addch(sb, '\n');
Johannes Schindelina97a7462009-10-09 12:21:57 +02001860
Jeff Kingb66103c2014-06-10 17:41:39 -04001861 unuse_commit_buffer(commit, reencoded);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001862}
Jeff King8b8a5372011-05-26 18:27:24 -04001863
1864void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
1865 struct strbuf *sb)
1866{
1867 struct pretty_print_context pp = {0};
Jeff King6bf13942011-05-26 18:27:49 -04001868 pp.fmt = fmt;
1869 pretty_print_commit(&pp, commit, sb);
Jeff King8b8a5372011-05-26 18:27:24 -04001870}