blob: af0f57ac831f7ed67883fc2d9125b72f1e90d7d7 [file] [log] [blame]
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001#include "cache.h"
2#include "commit.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00003#include "utf8.h"
4#include "diff.h"
5#include "revision.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Johannes Schindeline0cbc392008-07-12 00:28:18 +01007#include "mailmap.h"
René Scharfe3b3d4432008-09-04 23:40:03 +02008#include "log-tree.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +02009#include "notes.h"
Jeff Kingc0029222009-01-17 10:38:46 -050010#include "color.h"
Thomas Rast8f8f5472009-10-19 17:48:10 +020011#include "reflog-walk.h"
Junio C Hamanof6667c52011-10-21 21:06:02 -070012#include "gpg-interface.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000013
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000014static char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010015static struct cmt_fmt_map {
16 const char *name;
17 enum cmit_fmt format;
18 int is_tformat;
Will Palmer2d7671e2010-05-02 12:00:43 +010019 int is_alias;
20 const char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010021} *commit_formats;
Will Palmer80281842010-05-02 12:00:44 +010022static size_t builtin_formats_len;
Will Palmer40957892010-05-02 12:00:42 +010023static size_t commit_formats_len;
Will Palmer80281842010-05-02 12:00:44 +010024static size_t commit_formats_alloc;
Will Palmer40957892010-05-02 12:00:42 +010025static struct cmt_fmt_map *find_commit_format(const char *sought);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000026
Jeff Kingb9c7d6e2014-07-29 13:56:48 -040027int commit_format_is_empty(enum cmit_fmt fmt)
28{
29 return fmt == CMIT_FMT_USERFORMAT && !*user_format;
30}
31
Nanako Shiraishi36407542009-02-24 18:59:15 +090032static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
33{
34 free(user_format);
35 user_format = xstrdup(cp);
36 if (is_tformat)
37 rev->use_terminator = 1;
38 rev->commit_format = CMIT_FMT_USERFORMAT;
39}
40
Will Palmer80281842010-05-02 12:00:44 +010041static int git_pretty_formats_config(const char *var, const char *value, void *cb)
42{
43 struct cmt_fmt_map *commit_format = NULL;
44 const char *name;
45 const char *fmt;
46 int i;
47
Jeff King95b567c2014-06-18 15:48:29 -040048 if (!skip_prefix(var, "pretty.", &name))
Will Palmer80281842010-05-02 12:00:44 +010049 return 0;
50
Will Palmer80281842010-05-02 12:00:44 +010051 for (i = 0; i < builtin_formats_len; i++) {
52 if (!strcmp(commit_formats[i].name, name))
53 return 0;
54 }
55
56 for (i = builtin_formats_len; i < commit_formats_len; i++) {
57 if (!strcmp(commit_formats[i].name, name)) {
58 commit_format = &commit_formats[i];
59 break;
60 }
61 }
62
63 if (!commit_format) {
64 ALLOC_GROW(commit_formats, commit_formats_len+1,
65 commit_formats_alloc);
66 commit_format = &commit_formats[commit_formats_len];
Jonathan Nieder95a26182010-05-08 16:07:39 -050067 memset(commit_format, 0, sizeof(*commit_format));
Will Palmer80281842010-05-02 12:00:44 +010068 commit_formats_len++;
69 }
70
71 commit_format->name = xstrdup(name);
72 commit_format->format = CMIT_FMT_USERFORMAT;
73 git_config_string(&fmt, var, value);
Christian Couder59556542013-11-30 21:55:40 +010074 if (starts_with(fmt, "format:") || starts_with(fmt, "tformat:")) {
Will Palmer80281842010-05-02 12:00:44 +010075 commit_format->is_tformat = fmt[0] == 't';
76 fmt = strchr(fmt, ':') + 1;
77 } else if (strchr(fmt, '%'))
78 commit_format->is_tformat = 1;
79 else
80 commit_format->is_alias = 1;
81 commit_format->user_format = fmt;
82
83 return 0;
84}
85
Will Palmer40957892010-05-02 12:00:42 +010086static void setup_commit_formats(void)
87{
88 struct cmt_fmt_map builtin_formats[] = {
89 { "raw", CMIT_FMT_RAW, 0 },
90 { "medium", CMIT_FMT_MEDIUM, 0 },
91 { "short", CMIT_FMT_SHORT, 0 },
92 { "email", CMIT_FMT_EMAIL, 0 },
93 { "fuller", CMIT_FMT_FULLER, 0 },
94 { "full", CMIT_FMT_FULL, 0 },
95 { "oneline", CMIT_FMT_ONELINE, 1 }
96 };
97 commit_formats_len = ARRAY_SIZE(builtin_formats);
Will Palmer80281842010-05-02 12:00:44 +010098 builtin_formats_len = commit_formats_len;
99 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
Will Palmer40957892010-05-02 12:00:42 +0100100 memcpy(commit_formats, builtin_formats,
101 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
Will Palmer80281842010-05-02 12:00:44 +0100102
103 git_config(git_pretty_formats_config, NULL);
Will Palmer40957892010-05-02 12:00:42 +0100104}
105
Will Palmer2d7671e2010-05-02 12:00:43 +0100106static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
107 const char *original,
108 int num_redirections)
Will Palmer40957892010-05-02 12:00:42 +0100109{
110 struct cmt_fmt_map *found = NULL;
111 size_t found_match_len = 0;
112 int i;
113
Will Palmer2d7671e2010-05-02 12:00:43 +0100114 if (num_redirections >= commit_formats_len)
115 die("invalid --pretty format: "
116 "'%s' references an alias which points to itself",
117 original);
Will Palmer40957892010-05-02 12:00:42 +0100118
119 for (i = 0; i < commit_formats_len; i++) {
120 size_t match_len;
121
Christian Couder59556542013-11-30 21:55:40 +0100122 if (!starts_with(commit_formats[i].name, sought))
Will Palmer40957892010-05-02 12:00:42 +0100123 continue;
124
125 match_len = strlen(commit_formats[i].name);
126 if (found == NULL || found_match_len > match_len) {
127 found = &commit_formats[i];
128 found_match_len = match_len;
129 }
130 }
Will Palmer2d7671e2010-05-02 12:00:43 +0100131
132 if (found && found->is_alias) {
133 found = find_commit_format_recursive(found->user_format,
134 original,
135 num_redirections+1);
136 }
137
Will Palmer40957892010-05-02 12:00:42 +0100138 return found;
139}
140
Will Palmer2d7671e2010-05-02 12:00:43 +0100141static struct cmt_fmt_map *find_commit_format(const char *sought)
142{
143 if (!commit_formats)
144 setup_commit_formats();
145
146 return find_commit_format_recursive(sought, sought, 0);
147}
148
Junio C Hamano4da45be2008-04-07 17:11:34 -0700149void get_commit_format(const char *arg, struct rev_info *rev)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000150{
Will Palmer40957892010-05-02 12:00:42 +0100151 struct cmt_fmt_map *commit_format;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000152
Junio C Hamano4da45be2008-04-07 17:11:34 -0700153 rev->use_terminator = 0;
Jeff Kingc75e7ad2014-07-29 13:54:46 -0400154 if (!arg) {
Junio C Hamano4da45be2008-04-07 17:11:34 -0700155 rev->commit_format = CMIT_FMT_DEFAULT;
156 return;
157 }
Christian Couder59556542013-11-30 21:55:40 +0100158 if (starts_with(arg, "format:") || starts_with(arg, "tformat:")) {
Nanako Shiraishi36407542009-02-24 18:59:15 +0900159 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
Junio C Hamano4da45be2008-04-07 17:11:34 -0700160 return;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000161 }
Will Palmer40957892010-05-02 12:00:42 +0100162
Jeff Kingc75e7ad2014-07-29 13:54:46 -0400163 if (!*arg || strchr(arg, '%')) {
Nanako Shiraishi36407542009-02-24 18:59:15 +0900164 save_user_format(rev, arg, 1);
165 return;
166 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000167
Will Palmer40957892010-05-02 12:00:42 +0100168 commit_format = find_commit_format(arg);
169 if (!commit_format)
170 die("invalid --pretty format: %s", arg);
171
172 rev->commit_format = commit_format->format;
173 rev->use_terminator = commit_format->is_tformat;
Will Palmer80281842010-05-02 12:00:44 +0100174 if (commit_format->format == CMIT_FMT_USERFORMAT) {
175 save_user_format(rev, commit_format->user_format,
176 commit_format->is_tformat);
177 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000178}
179
180/*
181 * Generic support for pretty-printing the header
182 */
183static int get_one_line(const char *msg)
184{
185 int ret = 0;
186
187 for (;;) {
188 char c = *msg++;
189 if (!c)
190 break;
191 ret++;
192 if (c == '\n')
193 break;
194 }
195 return ret;
196}
197
198/* High bit set, or ISO-2022-INT */
Junio C Hamanocc571142010-01-11 22:23:35 -0800199static int non_ascii(int ch)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000200{
René Scharfec2e93642009-03-07 14:06:49 +0100201 return !isascii(ch) || ch == '\033';
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000202}
203
Johannes Schindelin28e9cf62009-08-10 18:22:18 +0200204int has_non_ascii(const char *s)
205{
206 int ch;
207 if (!s)
208 return 0;
209 while ((ch = *s++) != '\0') {
210 if (non_ascii(ch))
211 return 1;
212 }
213 return 0;
214}
215
Jeff King4d03c182011-04-08 18:40:36 -0400216static int is_rfc822_special(char ch)
217{
218 switch (ch) {
219 case '(':
220 case ')':
221 case '<':
222 case '>':
223 case '[':
224 case ']':
225 case ':':
226 case ';':
227 case '@':
228 case ',':
229 case '.':
230 case '"':
231 case '\\':
232 return 1;
233 default:
234 return 0;
235 }
236}
237
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200238static int needs_rfc822_quoting(const char *s, int len)
Jeff King4d03c182011-04-08 18:40:36 -0400239{
240 int i;
241 for (i = 0; i < len; i++)
242 if (is_rfc822_special(s[i]))
243 return 1;
244 return 0;
245}
246
Jan H. Schönherrf9b72042012-10-18 16:43:31 +0200247static int last_line_length(struct strbuf *sb)
248{
249 int i;
250
251 /* How many bytes are already used on the last line? */
252 for (i = sb->len - 1; i >= 0; i--)
253 if (sb->buf[i] == '\n')
254 break;
255 return sb->len - (i + 1);
256}
257
Jeff King4d03c182011-04-08 18:40:36 -0400258static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
259{
260 int i;
261
262 /* just a guess, we may have to also backslash-quote */
263 strbuf_grow(out, len + 2);
264
265 strbuf_addch(out, '"');
266 for (i = 0; i < len; i++) {
267 switch (s[i]) {
268 case '"':
269 case '\\':
270 strbuf_addch(out, '\\');
271 /* fall through */
272 default:
273 strbuf_addch(out, s[i]);
274 }
275 }
276 strbuf_addch(out, '"');
277}
278
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200279enum rfc2047_type {
280 RFC2047_SUBJECT,
Ronnie Sahlberg78273522014-07-02 11:24:05 -0700281 RFC2047_ADDRESS
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200282};
283
284static int is_rfc2047_special(char ch, enum rfc2047_type type)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000285{
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200286 /*
287 * rfc2047, section 4.2:
288 *
289 * 8-bit values which correspond to printable ASCII characters other
290 * than "=", "?", and "_" (underscore), MAY be represented as those
291 * characters. (But see section 5 for restrictions.) In
292 * particular, SPACE and TAB MUST NOT be represented as themselves
293 * within encoded words.
294 */
295
296 /*
297 * rule out non-ASCII characters and non-printable characters (the
298 * non-ASCII check should be redundant as isprint() is not localized
299 * and only knows about ASCII, but be defensive about that)
300 */
301 if (non_ascii(ch) || !isprint(ch))
Jan H. Schönherr94f6cdf2012-10-18 16:43:30 +0200302 return 1;
303
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200304 /*
305 * rule out special printable characters (' ' should be the only
306 * whitespace character considered printable, but be defensive and use
307 * isspace())
308 */
309 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
310 return 1;
311
312 /*
313 * rfc2047, section 5.3:
314 *
315 * As a replacement for a 'word' entity within a 'phrase', for example,
316 * one that precedes an address in a From, To, or Cc header. The ABNF
317 * definition for 'phrase' from RFC 822 thus becomes:
318 *
319 * phrase = 1*( encoded-word / word )
320 *
321 * In this case the set of characters that may be used in a "Q"-encoded
322 * 'encoded-word' is restricted to: <upper and lower case ASCII
323 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
324 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
325 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
326 * 'special' by 'linear-white-space'.
327 */
328
329 if (type != RFC2047_ADDRESS)
330 return 0;
331
332 /* '=' and '_' are special cases and have been checked above */
333 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000334}
335
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200336static int needs_rfc2047_encoding(const char *line, int len,
337 enum rfc2047_type type)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000338{
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500339 int i;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000340
341 for (i = 0; i < len; i++) {
342 int ch = line[i];
Jeff Kingc22e7de2011-02-23 04:59:18 -0500343 if (non_ascii(ch) || ch == '\n')
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200344 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000345 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200346 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000347 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000348
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200349 return 0;
350}
351
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400352static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200353 const char *encoding, enum rfc2047_type type)
354{
355 static const int max_encoded_length = 76; /* per rfc2047 */
356 int i;
357 int line_len = last_line_length(sb);
358
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000359 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
360 strbuf_addf(sb, "=?%s?q?", encoding);
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500361 line_len += strlen(encoding) + 5; /* 5 for =??q? */
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400362
363 while (len) {
364 /*
365 * RFC 2047, section 5 (3):
366 *
367 * Each 'encoded-word' MUST represent an integral number of
368 * characters. A multi-octet character may not be split across
369 * adjacent 'encoded- word's.
370 */
371 const unsigned char *p = (const unsigned char *)line;
372 int chrlen = mbs_chrlen(&line, &len, encoding);
373 int is_special = (chrlen > 1) || is_rfc2047_special(*p, type);
374
375 /* "=%02X" * chrlen, or the byte itself */
376 const char *encoded_fmt = is_special ? "=%02X" : "%c";
377 int encoded_len = is_special ? 3 * chrlen : 1;
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500378
Jan H. Schönherr94f6cdf2012-10-18 16:43:30 +0200379 /*
380 * According to RFC 2047, we could encode the special character
381 * ' ' (space) with '_' (underscore) for readability. But many
382 * programs do not understand this and just leave the
383 * underscore in place. Thus, we do nothing special here, which
384 * causes ' ' to be encoded as '=20', avoiding this problem.
385 */
386
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400387 if (line_len + encoded_len + 2 > max_encoded_length) {
388 /* It won't fit with trailing "?=" --- break the line */
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500389 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
390 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
391 }
392
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400393 for (i = 0; i < chrlen; i++)
394 strbuf_addf(sb, encoded_fmt, p[i]);
395 line_len += encoded_len;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000396 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000397 strbuf_addstr(sb, "?=");
398}
399
Jeff Kingd1053242014-05-01 21:07:22 -0400400const char *show_ident_date(const struct ident_split *ident,
401 enum date_mode mode)
René Scharfe9dbe7c32013-04-17 20:33:35 +0200402{
403 unsigned long date = 0;
Jeff King3f419d42014-03-07 12:15:01 -0500404 long tz = 0;
René Scharfe9dbe7c32013-04-17 20:33:35 +0200405
406 if (ident->date_begin && ident->date_end)
407 date = strtoul(ident->date_begin, NULL, 10);
Jeff King1dca1552014-02-24 02:46:37 -0500408 if (date_overflows(date))
409 date = 0;
410 else {
411 if (ident->tz_begin && ident->tz_end)
412 tz = strtol(ident->tz_begin, NULL, 10);
Jeff King3f419d42014-03-07 12:15:01 -0500413 if (tz >= INT_MAX || tz <= INT_MIN)
Jeff King1dca1552014-02-24 02:46:37 -0500414 tz = 0;
415 }
René Scharfe9dbe7c32013-04-17 20:33:35 +0200416 return show_date(date, tz, mode);
417}
418
Jeff King10f2fbf2013-07-03 03:07:48 -0400419void pp_user_info(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -0400420 const char *what, struct strbuf *sb,
421 const char *line, const char *encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000422{
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100423 struct ident_split ident;
René Scharfe9dbe7c32013-04-17 20:33:35 +0200424 char *line_end;
Antoine Pelissedffd3252013-01-05 22:26:42 +0100425 const char *mailbuf, *namebuf;
426 size_t namelen, maillen;
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200427 int max_length = 78; /* per rfc2822 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000428
Jeff King6bf13942011-05-26 18:27:49 -0400429 if (pp->fmt == CMIT_FMT_ONELINE)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000430 return;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100431
René Scharfe30e77bc2013-04-25 21:40:25 +0200432 line_end = strchrnul(line, '\n');
433 if (split_ident_line(&ident, line, line_end - line))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000434 return;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100435
Antoine Pelissedffd3252013-01-05 22:26:42 +0100436 mailbuf = ident.mail_begin;
437 maillen = ident.mail_end - ident.mail_begin;
438 namebuf = ident.name_begin;
439 namelen = ident.name_end - ident.name_begin;
440
441 if (pp->mailmap)
442 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
443
Jeff King6bf13942011-05-26 18:27:49 -0400444 if (pp->fmt == CMIT_FMT_EMAIL) {
Jeff King662cc302013-09-20 06:16:28 -0400445 if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
Jeff Kinga9080472013-07-03 03:08:22 -0400446 struct strbuf buf = STRBUF_INIT;
447
448 strbuf_addstr(&buf, "From: ");
449 strbuf_add(&buf, namebuf, namelen);
450 strbuf_addstr(&buf, " <");
451 strbuf_add(&buf, mailbuf, maillen);
452 strbuf_addstr(&buf, ">\n");
453 string_list_append(&pp->in_body_headers,
454 strbuf_detach(&buf, NULL));
455
456 mailbuf = pp->from_ident->mail_begin;
457 maillen = pp->from_ident->mail_end - mailbuf;
458 namebuf = pp->from_ident->name_begin;
459 namelen = pp->from_ident->name_end - namebuf;
460 }
461
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000462 strbuf_addstr(sb, "From: ");
René Scharfea0511b32013-04-25 21:43:56 +0200463 if (needs_rfc2047_encoding(namebuf, namelen, RFC2047_ADDRESS)) {
464 add_rfc2047(sb, namebuf, namelen,
Antoine Pelissedffd3252013-01-05 22:26:42 +0100465 encoding, RFC2047_ADDRESS);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200466 max_length = 76; /* per rfc2047 */
René Scharfea0511b32013-04-25 21:43:56 +0200467 } else if (needs_rfc822_quoting(namebuf, namelen)) {
Jeff King4d03c182011-04-08 18:40:36 -0400468 struct strbuf quoted = STRBUF_INIT;
René Scharfea0511b32013-04-25 21:43:56 +0200469 add_rfc822_quoted(&quoted, namebuf, namelen);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200470 strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
471 -6, 1, max_length);
Jeff King4d03c182011-04-08 18:40:36 -0400472 strbuf_release(&quoted);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200473 } else {
René Scharfea0511b32013-04-25 21:43:56 +0200474 strbuf_add_wrapped_bytes(sb, namebuf, namelen,
Antoine Pelissedffd3252013-01-05 22:26:42 +0100475 -6, 1, max_length);
Jeff King4d03c182011-04-08 18:40:36 -0400476 }
Antoine Pelissedffd3252013-01-05 22:26:42 +0100477
René Scharfe97a17e72013-04-25 21:41:57 +0200478 if (max_length <
479 last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
Jeff King990f6e32011-04-14 18:18:09 -0400480 strbuf_addch(sb, '\n');
René Scharfea0511b32013-04-25 21:43:56 +0200481 strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000482 } else {
René Scharfea0511b32013-04-25 21:43:56 +0200483 strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
484 (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
485 (int)namelen, namebuf, (int)maillen, mailbuf);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000486 }
Antoine Pelissedffd3252013-01-05 22:26:42 +0100487
Jeff King6bf13942011-05-26 18:27:49 -0400488 switch (pp->fmt) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000489 case CMIT_FMT_MEDIUM:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200490 strbuf_addf(sb, "Date: %s\n",
491 show_ident_date(&ident, pp->date_mode));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000492 break;
493 case CMIT_FMT_EMAIL:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200494 strbuf_addf(sb, "Date: %s\n",
495 show_ident_date(&ident, DATE_RFC2822));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000496 break;
497 case CMIT_FMT_FULLER:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200498 strbuf_addf(sb, "%sDate: %s\n", what,
499 show_ident_date(&ident, pp->date_mode));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000500 break;
501 default:
502 /* notin' */
503 break;
504 }
505}
506
507static int is_empty_line(const char *line, int *len_p)
508{
509 int len = *len_p;
Felipe Contreras35b2fa52013-10-31 03:25:42 -0600510 while (len && isspace(line[len - 1]))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000511 len--;
512 *len_p = len;
513 return !len;
514}
515
René Scharfea0109662008-12-27 01:32:49 +0100516static const char *skip_empty_lines(const char *msg)
517{
518 for (;;) {
519 int linelen = get_one_line(msg);
520 int ll = linelen;
521 if (!linelen)
522 break;
523 if (!is_empty_line(msg, &ll))
524 break;
525 msg += linelen;
526 }
527 return msg;
528}
529
Jeff King6bf13942011-05-26 18:27:49 -0400530static void add_merge_info(const struct pretty_print_context *pp,
531 struct strbuf *sb, const struct commit *commit)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000532{
533 struct commit_list *parent = commit->parents;
534
Jeff King6bf13942011-05-26 18:27:49 -0400535 if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000536 !parent || !parent->next)
537 return;
538
539 strbuf_addstr(sb, "Merge:");
540
541 while (parent) {
542 struct commit *p = parent->item;
543 const char *hex = NULL;
Jeff King6bf13942011-05-26 18:27:49 -0400544 if (pp->abbrev)
545 hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000546 if (!hex)
547 hex = sha1_to_hex(p->object.sha1);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000548 parent = parent->next;
549
Thomas Rast7fcda922009-02-13 23:10:41 +0100550 strbuf_addf(sb, " %s", hex);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000551 }
552 strbuf_addch(sb, '\n');
553}
554
Jeff Kingdd0d3882013-01-26 04:44:06 -0500555static char *get_header(const struct commit *commit, const char *msg,
556 const char *key)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000557{
558 int key_len = strlen(key);
Jeff Kingdd0d3882013-01-26 04:44:06 -0500559 const char *line = msg;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000560
Jeff Kinga9c7a8a2012-05-22 00:52:17 -0400561 while (line) {
Rohit Mani2c5495f2014-03-07 22:48:31 -0800562 const char *eol = strchrnul(line, '\n'), *next;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000563
564 if (line == eol)
565 return NULL;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800566 if (!*eol) {
Jeff Kinga9c7a8a2012-05-22 00:52:17 -0400567 warning("malformed commit (header is missing newline): %s",
568 sha1_to_hex(commit->object.sha1));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000569 next = NULL;
570 } else
571 next = eol + 1;
572 if (eol - line > key_len &&
573 !strncmp(line, key, key_len) &&
574 line[key_len] == ' ') {
575 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
576 }
577 line = next;
578 }
Jeff Kinga9c7a8a2012-05-22 00:52:17 -0400579 return NULL;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000580}
581
582static char *replace_encoding_header(char *buf, const char *encoding)
583{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500584 struct strbuf tmp = STRBUF_INIT;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000585 size_t start, len;
586 char *cp = buf;
587
588 /* guess if there is an encoding header before a \n\n */
René Scharfe68d6d6e2015-02-21 20:53:09 +0100589 while (!starts_with(cp, "encoding ")) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000590 cp = strchr(cp, '\n');
591 if (!cp || *++cp == '\n')
592 return buf;
593 }
594 start = cp - buf;
595 cp = strchr(cp, '\n');
596 if (!cp)
597 return buf; /* should not happen but be defensive */
598 len = cp + 1 - (buf + start);
599
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000600 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
601 if (is_encoding_utf8(encoding)) {
602 /* we have re-coded to UTF-8; drop the header */
603 strbuf_remove(&tmp, start, len);
604 } else {
605 /* just replaces XXXX in 'encoding XXXX\n' */
606 strbuf_splice(&tmp, start + strlen("encoding "),
607 len - strlen("encoding \n"),
608 encoding, strlen(encoding));
609 }
610 return strbuf_detach(&tmp, NULL);
611}
612
Jeff Kingb000c592014-06-10 17:39:30 -0400613const char *logmsg_reencode(const struct commit *commit,
614 char **commit_encoding,
615 const char *output_encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000616{
Brandon Casey330db182009-05-18 18:44:39 -0500617 static const char *utf8 = "UTF-8";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000618 const char *use_encoding;
619 char *encoding;
Jeff King8597ea32014-06-10 17:44:13 -0400620 const char *msg = get_commit_buffer(commit, NULL);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000621 char *out;
622
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000623 if (!output_encoding || !*output_encoding) {
624 if (commit_encoding)
625 *commit_encoding =
626 get_header(commit, msg, "encoding");
Jeff Kingdd0d3882013-01-26 04:44:06 -0500627 return msg;
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000628 }
Jeff Kingdd0d3882013-01-26 04:44:06 -0500629 encoding = get_header(commit, msg, "encoding");
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000630 if (commit_encoding)
631 *commit_encoding = encoding;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000632 use_encoding = encoding ? encoding : utf8;
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500633 if (same_encoding(use_encoding, output_encoding)) {
634 /*
635 * No encoding work to be done. If we have no encoding header
636 * at all, then there's nothing to do, and we can return the
637 * message verbatim (whether newly allocated or not).
638 */
639 if (!encoding)
640 return msg;
641
642 /*
643 * Otherwise, we still want to munge the encoding header in the
644 * result, which will be done by modifying the buffer. If we
645 * are using a fresh copy, we can reuse it. But if we are using
Jeff Kingb66103c2014-06-10 17:41:39 -0400646 * the cached copy from get_commit_buffer, we need to duplicate it
647 * to avoid munging the cached copy.
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500648 */
Jeff King8597ea32014-06-10 17:44:13 -0400649 if (msg == get_cached_commit_buffer(commit, NULL))
Jeff Kingb66103c2014-06-10 17:41:39 -0400650 out = xstrdup(msg);
651 else
652 out = (char *)msg;
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500653 }
654 else {
655 /*
656 * There's actual encoding work to do. Do the reencoding, which
657 * still leaves the header to be replaced in the next step. At
658 * this point, we are done with msg. If we allocated a fresh
659 * copy, we can free it.
660 */
661 out = reencode_string(msg, output_encoding, use_encoding);
Jeff Kingb66103c2014-06-10 17:41:39 -0400662 if (out)
663 unuse_commit_buffer(commit, msg);
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500664 }
665
666 /*
667 * This replacement actually consumes the buffer we hand it, so we do
668 * not have to worry about freeing the old "out" here.
669 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000670 if (out)
671 out = replace_encoding_header(out, output_encoding);
672
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000673 if (!commit_encoding)
674 free(encoding);
Jeff Kingdd0d3882013-01-26 04:44:06 -0500675 /*
676 * If the re-encoding failed, out might be NULL here; in that
677 * case we just return the commit message verbatim.
678 */
679 return out ? out : msg;
680}
681
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100682static int mailmap_name(const char **email, size_t *email_len,
683 const char **name, size_t *name_len)
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100684{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100685 static struct string_list *mail_map;
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100686 if (!mail_map) {
687 mail_map = xcalloc(1, sizeof(*mail_map));
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100688 read_mailmap(mail_map, NULL);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100689 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100690 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100691}
692
Marco Costalbac3a670d2008-02-09 15:40:19 +0100693static size_t format_person_part(struct strbuf *sb, char part,
Jeff Kingd36f8672008-08-28 20:54:59 -0400694 const char *msg, int len, enum date_mode dmode)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000695{
Marco Costalbac3a670d2008-02-09 15:40:19 +0100696 /* currently all placeholders have same length */
697 const int placeholder_len = 2;
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800698 struct ident_split s;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100699 const char *name, *mail;
700 size_t maillen, namelen;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000701
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800702 if (split_ident_line(&s, msg, len) < 0)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100703 goto skip;
704
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100705 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;
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100709
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100710 if (part == 'N' || part == 'E') /* mailmap lookup */
711 mailmap_name(&mail, &maillen, &name, &namelen);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100712 if (part == 'n' || part == 'N') { /* name */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100713 strbuf_add(sb, name, namelen);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100714 return placeholder_len;
715 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100716 if (part == 'e' || part == 'E') { /* email */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100717 strbuf_add(sb, mail, maillen);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100718 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100719 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000720
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800721 if (!s.date_begin)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100722 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000723
René Scharfecde75e52007-11-09 01:49:42 +0100724 if (part == 't') { /* date, UNIX timestamp */
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800725 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100726 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100727 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000728
René Scharfecde75e52007-11-09 01:49:42 +0100729 switch (part) {
730 case 'd': /* date */
René Scharfe9dbe7c32013-04-17 20:33:35 +0200731 strbuf_addstr(sb, show_ident_date(&s, dmode));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100732 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100733 case 'D': /* date, RFC2822 style */
René Scharfe9dbe7c32013-04-17 20:33:35 +0200734 strbuf_addstr(sb, show_ident_date(&s, DATE_RFC2822));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100735 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100736 case 'r': /* date, relative */
René Scharfe9dbe7c32013-04-17 20:33:35 +0200737 strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100738 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100739 case 'i': /* date, ISO 8601 */
René Scharfe9dbe7c32013-04-17 20:33:35 +0200740 strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100741 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100742 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100743
744skip:
745 /*
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800746 * reading from either a bogus commit, or a reflog entry with
747 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
748 * to compute a valid return value.
Marco Costalbac3a670d2008-02-09 15:40:19 +0100749 */
750 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
751 || part == 'D' || part == 'r' || part == 'i')
752 return placeholder_len;
753
754 return 0; /* unknown placeholder */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000755}
756
René Scharfef29d5952007-11-10 12:14:20 +0100757struct chunk {
758 size_t off;
759 size_t len;
760};
761
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000762enum flush_type {
763 no_flush,
764 flush_right,
765 flush_left,
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +1000766 flush_left_and_steal,
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000767 flush_both
768};
769
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000770enum trunc_type {
771 trunc_none,
772 trunc_left,
773 trunc_middle,
774 trunc_right
775};
776
René Scharfef29d5952007-11-10 12:14:20 +0100777struct format_commit_context {
778 const struct commit *commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200779 const struct pretty_print_context *pretty_ctx;
René Scharfef53bd742008-12-27 01:49:21 +0100780 unsigned commit_header_parsed:1;
781 unsigned commit_message_parsed:1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +0200782 struct signature_check signature_check;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000783 enum flush_type flush_type;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000784 enum trunc_type truncate;
Jeff Kingb000c592014-06-10 17:39:30 -0400785 const char *message;
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +1000786 char *commit_encoding;
René Scharfe02edd562009-10-17 23:04:19 +0200787 size_t width, indent1, indent2;
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +1000788 int auto_color;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000789 int padding;
René Scharfef29d5952007-11-10 12:14:20 +0100790
791 /* These offsets are relative to the start of the commit message. */
René Scharfef29d5952007-11-10 12:14:20 +0100792 struct chunk author;
793 struct chunk committer;
René Scharfef53bd742008-12-27 01:49:21 +0100794 size_t message_off;
795 size_t subject_off;
René Scharfef29d5952007-11-10 12:14:20 +0100796 size_t body_off;
René Scharfeb9c62322007-11-10 12:18:26 +0100797
798 /* The following ones are relative to the result struct strbuf. */
799 struct chunk abbrev_commit_hash;
800 struct chunk abbrev_tree_hash;
801 struct chunk abbrev_parent_hashes;
René Scharfe02edd562009-10-17 23:04:19 +0200802 size_t wrap_start;
René Scharfef29d5952007-11-10 12:14:20 +0100803};
804
René Scharfeb9c62322007-11-10 12:18:26 +0100805static int add_again(struct strbuf *sb, struct chunk *chunk)
806{
807 if (chunk->len) {
808 strbuf_adddup(sb, chunk->off, chunk->len);
809 return 1;
810 }
811
812 /*
813 * We haven't seen this chunk before. Our caller is surely
814 * going to add it the hard way now. Remember the most likely
815 * start of the to-be-added chunk: the current end of the
816 * struct strbuf.
817 */
818 chunk->off = sb->len;
819 return 0;
820}
821
René Scharfef29d5952007-11-10 12:14:20 +0100822static void parse_commit_header(struct format_commit_context *context)
823{
Pat Notz177b29d2010-11-02 13:59:08 -0600824 const char *msg = context->message;
René Scharfef29d5952007-11-10 12:14:20 +0100825 int i;
René Scharfef29d5952007-11-10 12:14:20 +0100826
René Scharfef53bd742008-12-27 01:49:21 +0100827 for (i = 0; msg[i]; i++) {
René Scharfef29d5952007-11-10 12:14:20 +0100828 int eol;
829 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
830 ; /* do nothing */
831
René Scharfef29d5952007-11-10 12:14:20 +0100832 if (i == eol) {
René Scharfef53bd742008-12-27 01:49:21 +0100833 break;
Christian Couder59556542013-11-30 21:55:40 +0100834 } else if (starts_with(msg + i, "author ")) {
René Scharfef29d5952007-11-10 12:14:20 +0100835 context->author.off = i + 7;
836 context->author.len = eol - i - 7;
Christian Couder59556542013-11-30 21:55:40 +0100837 } else if (starts_with(msg + i, "committer ")) {
René Scharfef29d5952007-11-10 12:14:20 +0100838 context->committer.off = i + 10;
839 context->committer.len = eol - i - 10;
René Scharfef29d5952007-11-10 12:14:20 +0100840 }
841 i = eol;
842 }
René Scharfef53bd742008-12-27 01:49:21 +0100843 context->message_off = i;
René Scharfef29d5952007-11-10 12:14:20 +0100844 context->commit_header_parsed = 1;
845}
846
Stephen Boyd46d164b2009-03-22 19:14:01 -0700847static int istitlechar(char c)
848{
849 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
850 (c >= '0' && c <= '9') || c == '.' || c == '_';
851}
852
853static void format_sanitized_subject(struct strbuf *sb, const char *msg)
854{
855 size_t trimlen;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700856 size_t start_len = sb->len;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700857 int space = 2;
858
859 for (; *msg && *msg != '\n'; msg++) {
860 if (istitlechar(*msg)) {
861 if (space == 1)
862 strbuf_addch(sb, '-');
863 space = 0;
864 strbuf_addch(sb, *msg);
865 if (*msg == '.')
866 while (*(msg+1) == '.')
867 msg++;
868 } else
869 space |= 1;
870 }
871
872 /* trim any trailing '.' or '-' characters */
873 trimlen = 0;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700874 while (sb->len - trimlen > start_len &&
875 (sb->buf[sb->len - 1 - trimlen] == '.'
876 || sb->buf[sb->len - 1 - trimlen] == '-'))
Stephen Boyd46d164b2009-03-22 19:14:01 -0700877 trimlen++;
878 strbuf_remove(sb, sb->len - trimlen, trimlen);
879}
880
René Scharfecec08712009-01-06 21:41:06 +0100881const char *format_subject(struct strbuf *sb, const char *msg,
882 const char *line_separator)
René Scharfe88c44732008-12-27 01:39:35 +0100883{
884 int first = 1;
885
886 for (;;) {
887 const char *line = msg;
888 int linelen = get_one_line(line);
889
890 msg += linelen;
891 if (!linelen || is_empty_line(line, &linelen))
892 break;
893
René Scharfef53bd742008-12-27 01:49:21 +0100894 if (!sb)
895 continue;
René Scharfe88c44732008-12-27 01:39:35 +0100896 strbuf_grow(sb, linelen + 2);
897 if (!first)
898 strbuf_addstr(sb, line_separator);
899 strbuf_add(sb, line, linelen);
900 first = 0;
901 }
902 return msg;
903}
904
René Scharfef53bd742008-12-27 01:49:21 +0100905static void parse_commit_message(struct format_commit_context *c)
906{
Pat Notz177b29d2010-11-02 13:59:08 -0600907 const char *msg = c->message + c->message_off;
908 const char *start = c->message;
René Scharfef53bd742008-12-27 01:49:21 +0100909
910 msg = skip_empty_lines(msg);
911 c->subject_off = msg - start;
912
913 msg = format_subject(NULL, msg, NULL);
914 msg = skip_empty_lines(msg);
915 c->body_off = msg - start;
916
917 c->commit_message_parsed = 1;
918}
919
René Scharfe02edd562009-10-17 23:04:19 +0200920static void strbuf_wrap(struct strbuf *sb, size_t pos,
921 size_t width, size_t indent1, size_t indent2)
922{
923 struct strbuf tmp = STRBUF_INIT;
924
925 if (pos)
926 strbuf_add(&tmp, sb->buf, pos);
927 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
928 (int) indent1, (int) indent2, (int) width);
929 strbuf_swap(&tmp, sb);
930 strbuf_release(&tmp);
931}
932
933static void rewrap_message_tail(struct strbuf *sb,
934 struct format_commit_context *c,
935 size_t new_width, size_t new_indent1,
936 size_t new_indent2)
937{
938 if (c->width == new_width && c->indent1 == new_indent1 &&
939 c->indent2 == new_indent2)
940 return;
René Scharfe32ca4242009-11-08 02:04:21 +0100941 if (c->wrap_start < sb->len)
René Scharfe02edd562009-10-17 23:04:19 +0200942 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
943 c->wrap_start = sb->len;
944 c->width = new_width;
945 c->indent1 = new_indent1;
946 c->indent2 = new_indent2;
947}
948
Jeff Kingcd1957f2011-12-16 06:40:24 -0500949static int format_reflog_person(struct strbuf *sb,
950 char part,
951 struct reflog_walk_info *log,
952 enum date_mode dmode)
953{
954 const char *ident;
955
956 if (!log)
957 return 2;
958
959 ident = get_reflog_ident(log);
960 if (!ident)
961 return 2;
962
963 return format_person_part(sb, part, ident, strlen(ident), dmode);
964}
965
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000966static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
967 const char *placeholder,
968 struct format_commit_context *c)
969{
970 if (placeholder[1] == '(') {
971 const char *begin = placeholder + 2;
972 const char *end = strchr(begin, ')');
973 char color[COLOR_MAXLEN];
974
975 if (!end)
976 return 0;
Christian Couder59556542013-11-30 21:55:40 +0100977 if (starts_with(begin, "auto,")) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000978 if (!want_color(c->pretty_ctx->color))
979 return end - placeholder + 1;
980 begin += 5;
981 }
982 color_parse_mem(begin,
983 end - begin,
984 "--pretty format", color);
985 strbuf_addstr(sb, color);
986 return end - placeholder + 1;
987 }
Christian Couder59556542013-11-30 21:55:40 +0100988 if (starts_with(placeholder + 1, "red")) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000989 strbuf_addstr(sb, GIT_COLOR_RED);
990 return 4;
Christian Couder59556542013-11-30 21:55:40 +0100991 } else if (starts_with(placeholder + 1, "green")) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000992 strbuf_addstr(sb, GIT_COLOR_GREEN);
993 return 6;
Christian Couder59556542013-11-30 21:55:40 +0100994 } else if (starts_with(placeholder + 1, "blue")) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000995 strbuf_addstr(sb, GIT_COLOR_BLUE);
996 return 5;
Christian Couder59556542013-11-30 21:55:40 +0100997 } else if (starts_with(placeholder + 1, "reset")) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000998 strbuf_addstr(sb, GIT_COLOR_RESET);
999 return 6;
1000 } else
1001 return 0;
1002}
1003
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001004static size_t parse_padding_placeholder(struct strbuf *sb,
1005 const char *placeholder,
1006 struct format_commit_context *c)
1007{
1008 const char *ch = placeholder;
1009 enum flush_type flush_type;
1010 int to_column = 0;
1011
1012 switch (*ch++) {
1013 case '<':
1014 flush_type = flush_right;
1015 break;
1016 case '>':
1017 if (*ch == '<') {
1018 flush_type = flush_both;
1019 ch++;
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10001020 } else if (*ch == '>') {
1021 flush_type = flush_left_and_steal;
1022 ch++;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001023 } else
1024 flush_type = flush_left;
1025 break;
1026 default:
1027 return 0;
1028 }
1029
1030 /* the next value means "wide enough to that column" */
1031 if (*ch == '|') {
1032 to_column = 1;
1033 ch++;
1034 }
1035
1036 if (*ch == '(') {
1037 const char *start = ch + 1;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001038 const char *end = start + strcspn(start, ",)");
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001039 char *next;
1040 int width;
1041 if (!end || end == start)
1042 return 0;
1043 width = strtoul(start, &next, 10);
1044 if (next == start || width == 0)
1045 return 0;
1046 c->padding = to_column ? -width : width;
1047 c->flush_type = flush_type;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001048
1049 if (*end == ',') {
1050 start = end + 1;
1051 end = strchr(start, ')');
1052 if (!end || end == start)
1053 return 0;
Christian Couder59556542013-11-30 21:55:40 +01001054 if (starts_with(start, "trunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001055 c->truncate = trunc_right;
Christian Couder59556542013-11-30 21:55:40 +01001056 else if (starts_with(start, "ltrunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001057 c->truncate = trunc_left;
Christian Couder59556542013-11-30 21:55:40 +01001058 else if (starts_with(start, "mtrunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001059 c->truncate = trunc_middle;
1060 else
1061 return 0;
1062 } else
1063 c->truncate = trunc_none;
1064
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001065 return end - placeholder + 1;
1066 }
1067 return 0;
1068}
1069
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001070static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1071 const char *placeholder,
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001072 void *context)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001073{
René Scharfef29d5952007-11-10 12:14:20 +01001074 struct format_commit_context *c = context;
1075 const struct commit *commit = c->commit;
Pat Notz177b29d2010-11-02 13:59:08 -06001076 const char *msg = c->message;
René Scharfef29d5952007-11-10 12:14:20 +01001077 struct commit_list *p;
Govind Salinas42c8c742008-03-21 10:05:06 -05001078 int h1, h2;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001079
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001080 /* these are independent of the commit */
René Scharfecde75e52007-11-09 01:49:42 +01001081 switch (placeholder[0]) {
1082 case 'C':
Christian Couder59556542013-11-30 21:55:40 +01001083 if (starts_with(placeholder + 1, "(auto)")) {
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001084 c->auto_color = 1;
1085 return 7; /* consumed 7 bytes, "C(auto)" */
1086 } else {
1087 int ret = parse_color(sb, placeholder, c);
1088 if (ret)
1089 c->auto_color = 0;
1090 /*
1091 * Otherwise, we decided to treat %C<unknown>
1092 * as a literal string, and the previous
1093 * %C(auto) is still valid.
1094 */
1095 return ret;
Jeff Kingc0029222009-01-17 10:38:46 -05001096 }
René Scharfecde75e52007-11-09 01:49:42 +01001097 case 'n': /* newline */
1098 strbuf_addch(sb, '\n');
Marco Costalbac3a670d2008-02-09 15:40:19 +01001099 return 1;
Govind Salinas42c8c742008-03-21 10:05:06 -05001100 case 'x':
1101 /* %x00 == NUL, %x0a == LF, etc. */
1102 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
1103 h1 <= 16 &&
1104 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
1105 h2 <= 16) {
1106 strbuf_addch(sb, (h1<<4)|h2);
1107 return 3;
1108 } else
1109 return 0;
René Scharfe02edd562009-10-17 23:04:19 +02001110 case 'w':
1111 if (placeholder[1] == '(') {
1112 unsigned long width = 0, indent1 = 0, indent2 = 0;
1113 char *next;
1114 const char *start = placeholder + 2;
1115 const char *end = strchr(start, ')');
1116 if (!end)
1117 return 0;
1118 if (end > start) {
1119 width = strtoul(start, &next, 10);
1120 if (*next == ',') {
1121 indent1 = strtoul(next + 1, &next, 10);
1122 if (*next == ',') {
1123 indent2 = strtoul(next + 1,
1124 &next, 10);
1125 }
1126 }
1127 if (*next != ')')
1128 return 0;
1129 }
1130 rewrap_message_tail(sb, c, width, indent1, indent2);
1131 return end - placeholder + 1;
1132 } else
1133 return 0;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001134
1135 case '<':
1136 case '>':
1137 return parse_padding_placeholder(sb, placeholder, c);
René Scharfecde75e52007-11-09 01:49:42 +01001138 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001139
1140 /* these depend on the commit */
1141 if (!commit->object.parsed)
1142 parse_object(commit->object.sha1);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001143
René Scharfecde75e52007-11-09 01:49:42 +01001144 switch (placeholder[0]) {
1145 case 'H': /* commit hash */
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001146 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
René Scharfecde75e52007-11-09 01:49:42 +01001147 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001148 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001149 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001150 case 'h': /* abbreviated commit hash */
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001151 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
1152 if (add_again(sb, &c->abbrev_commit_hash)) {
1153 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001154 return 1;
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001155 }
René Scharfecde75e52007-11-09 01:49:42 +01001156 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
Will Palmerc1977022010-05-03 22:18:57 -05001157 c->pretty_ctx->abbrev));
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001158 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
René Scharfeb9c62322007-11-10 12:18:26 +01001159 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001160 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001161 case 'T': /* tree hash */
1162 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001163 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001164 case 't': /* abbreviated tree hash */
René Scharfeb9c62322007-11-10 12:18:26 +01001165 if (add_again(sb, &c->abbrev_tree_hash))
Marco Costalbac3a670d2008-02-09 15:40:19 +01001166 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001167 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
Will Palmerc1977022010-05-03 22:18:57 -05001168 c->pretty_ctx->abbrev));
René Scharfeb9c62322007-11-10 12:18:26 +01001169 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001170 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001171 case 'P': /* parent hashes */
1172 for (p = commit->parents; p; p = p->next) {
1173 if (p != commit->parents)
1174 strbuf_addch(sb, ' ');
1175 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
1176 }
Marco Costalbac3a670d2008-02-09 15:40:19 +01001177 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001178 case 'p': /* abbreviated parent hashes */
René Scharfeb9c62322007-11-10 12:18:26 +01001179 if (add_again(sb, &c->abbrev_parent_hashes))
Marco Costalbac3a670d2008-02-09 15:40:19 +01001180 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001181 for (p = commit->parents; p; p = p->next) {
1182 if (p != commit->parents)
1183 strbuf_addch(sb, ' ');
1184 strbuf_addstr(sb, find_unique_abbrev(
Will Palmerc1977022010-05-03 22:18:57 -05001185 p->item->object.sha1,
1186 c->pretty_ctx->abbrev));
René Scharfecde75e52007-11-09 01:49:42 +01001187 }
René Scharfeb9c62322007-11-10 12:18:26 +01001188 c->abbrev_parent_hashes.len = sb->len -
1189 c->abbrev_parent_hashes.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001190 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001191 case 'm': /* left/right/bottom */
Michael J Gruber1df2d652011-03-07 13:31:39 +01001192 strbuf_addstr(sb, get_revision_mark(NULL, commit));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001193 return 1;
René Scharfe3b3d4432008-09-04 23:40:03 +02001194 case 'd':
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +10001195 load_ref_decorations(DECORATE_SHORT_REFS);
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001196 format_decorations(sb, commit, c->auto_color);
René Scharfe3b3d4432008-09-04 23:40:03 +02001197 return 1;
Thomas Rast8f8f5472009-10-19 17:48:10 +02001198 case 'g': /* reflog info */
1199 switch(placeholder[1]) {
1200 case 'd': /* reflog selector */
1201 case 'D':
1202 if (c->pretty_ctx->reflog_info)
1203 get_reflog_selector(sb,
1204 c->pretty_ctx->reflog_info,
1205 c->pretty_ctx->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -07001206 c->pretty_ctx->date_mode_explicit,
Thomas Rast8f8f5472009-10-19 17:48:10 +02001207 (placeholder[1] == 'd'));
1208 return 2;
1209 case 's': /* reflog message */
1210 if (c->pretty_ctx->reflog_info)
1211 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1212 return 2;
Jeff Kingcd1957f2011-12-16 06:40:24 -05001213 case 'n':
1214 case 'N':
1215 case 'e':
1216 case 'E':
1217 return format_reflog_person(sb,
1218 placeholder[1],
1219 c->pretty_ctx->reflog_info,
1220 c->pretty_ctx->date_mode);
Thomas Rast8f8f5472009-10-19 17:48:10 +02001221 }
1222 return 0; /* unknown %g placeholder */
Johannes Schindelin8b208f02009-10-09 12:22:05 +02001223 case 'N':
Junio C Hamanoddf333f2012-10-17 18:51:47 -07001224 if (c->pretty_ctx->notes_message) {
1225 strbuf_addstr(sb, c->pretty_ctx->notes_message);
Johannes Gilger5b163602010-04-13 22:31:12 +02001226 return 1;
1227 }
1228 return 0;
René Scharfecde75e52007-11-09 01:49:42 +01001229 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001230
Junio C Hamanof6667c52011-10-21 21:06:02 -07001231 if (placeholder[0] == 'G') {
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001232 if (!c->signature_check.result)
1233 check_commit_signature(c->commit, &(c->signature_check));
Junio C Hamanof6667c52011-10-21 21:06:02 -07001234 switch (placeholder[1]) {
1235 case 'G':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001236 if (c->signature_check.gpg_output)
1237 strbuf_addstr(sb, c->signature_check.gpg_output);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001238 break;
1239 case '?':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001240 switch (c->signature_check.result) {
Junio C Hamanof6667c52011-10-21 21:06:02 -07001241 case 'G':
1242 case 'B':
Sebastian Göttee290c4b2013-03-31 18:03:22 +02001243 case 'U':
1244 case 'N':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001245 strbuf_addch(sb, c->signature_check.result);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001246 }
1247 break;
1248 case 'S':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001249 if (c->signature_check.signer)
1250 strbuf_addstr(sb, c->signature_check.signer);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001251 break;
Michael J Gruber0174eea2013-02-14 17:04:46 +01001252 case 'K':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001253 if (c->signature_check.key)
1254 strbuf_addstr(sb, c->signature_check.key);
Michael J Gruber0174eea2013-02-14 17:04:46 +01001255 break;
Jeff Kingaa4b78d2014-06-16 20:07:07 -04001256 default:
1257 return 0;
Junio C Hamanof6667c52011-10-21 21:06:02 -07001258 }
1259 return 2;
1260 }
1261
1262
René Scharfecde75e52007-11-09 01:49:42 +01001263 /* For the rest we have to parse the commit header. */
René Scharfef29d5952007-11-10 12:14:20 +01001264 if (!c->commit_header_parsed)
1265 parse_commit_header(c);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001266
René Scharfef29d5952007-11-10 12:14:20 +01001267 switch (placeholder[0]) {
Marco Costalbac3a670d2008-02-09 15:40:19 +01001268 case 'a': /* author ... */
1269 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -04001270 msg + c->author.off, c->author.len,
Thomas Rastdd2e7942009-10-19 17:48:08 +02001271 c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001272 case 'c': /* committer ... */
1273 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -04001274 msg + c->committer.off, c->committer.len,
Thomas Rastdd2e7942009-10-19 17:48:08 +02001275 c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001276 case 'e': /* encoding */
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001277 if (c->commit_encoding)
1278 strbuf_addstr(sb, c->commit_encoding);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001279 return 1;
Eli Barzilay1367b122010-03-24 22:51:52 -04001280 case 'B': /* raw body */
1281 /* message_off is always left at the initial newline */
1282 strbuf_addstr(sb, msg + c->message_off + 1);
1283 return 1;
René Scharfef53bd742008-12-27 01:49:21 +01001284 }
1285
1286 /* Now we need to parse the commit message. */
1287 if (!c->commit_message_parsed)
1288 parse_commit_message(c);
1289
1290 switch (placeholder[0]) {
1291 case 's': /* subject */
1292 format_subject(sb, msg + c->subject_off, " ");
1293 return 1;
Stephen Boyd46d164b2009-03-22 19:14:01 -07001294 case 'f': /* sanitized subject */
1295 format_sanitized_subject(sb, msg + c->subject_off);
1296 return 1;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001297 case 'b': /* body */
René Scharfef29d5952007-11-10 12:14:20 +01001298 strbuf_addstr(sb, msg + c->body_off);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001299 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001300 }
Marco Costalbac3a670d2008-02-09 15:40:19 +01001301 return 0; /* unknown placeholder */
René Scharfecde75e52007-11-09 01:49:42 +01001302}
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001303
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001304static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1305 const char *placeholder,
1306 struct format_commit_context *c)
1307{
1308 struct strbuf local_sb = STRBUF_INIT;
1309 int total_consumed = 0, len, padding = c->padding;
1310 if (padding < 0) {
1311 const char *start = strrchr(sb->buf, '\n');
1312 int occupied;
1313 if (!start)
1314 start = sb->buf;
1315 occupied = utf8_strnwidth(start, -1, 1);
1316 padding = (-padding) - occupied;
1317 }
1318 while (1) {
1319 int modifier = *placeholder == 'C';
1320 int consumed = format_commit_one(&local_sb, placeholder, c);
1321 total_consumed += consumed;
1322
1323 if (!modifier)
1324 break;
1325
1326 placeholder += consumed;
1327 if (*placeholder != '%')
1328 break;
1329 placeholder++;
1330 total_consumed++;
1331 }
1332 len = utf8_strnwidth(local_sb.buf, -1, 1);
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10001333
1334 if (c->flush_type == flush_left_and_steal) {
1335 const char *ch = sb->buf + sb->len - 1;
1336 while (len > padding && ch > sb->buf) {
1337 const char *p;
1338 if (*ch == ' ') {
1339 ch--;
1340 padding++;
1341 continue;
1342 }
1343 /* check for trailing ansi sequences */
1344 if (*ch != 'm')
1345 break;
1346 p = ch - 1;
1347 while (ch - p < 10 && *p != '\033')
1348 p--;
1349 if (*p != '\033' ||
1350 ch + 1 - p != display_mode_esc_sequence_len(p))
1351 break;
1352 /*
1353 * got a good ansi sequence, put it back to
1354 * local_sb as we're cutting sb
1355 */
1356 strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1357 ch = p - 1;
1358 }
1359 strbuf_setlen(sb, ch + 1 - sb->buf);
1360 c->flush_type = flush_left;
1361 }
1362
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001363 if (len > padding) {
1364 switch (c->truncate) {
1365 case trunc_left:
1366 strbuf_utf8_replace(&local_sb,
1367 0, len - (padding - 2),
1368 "..");
1369 break;
1370 case trunc_middle:
1371 strbuf_utf8_replace(&local_sb,
1372 padding / 2 - 1,
1373 len - (padding - 2),
1374 "..");
1375 break;
1376 case trunc_right:
1377 strbuf_utf8_replace(&local_sb,
1378 padding - 2, len - (padding - 2),
1379 "..");
1380 break;
1381 case trunc_none:
1382 break;
1383 }
René Scharfee992d1e2014-07-10 10:52:21 +02001384 strbuf_addbuf(sb, &local_sb);
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001385 } else {
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001386 int sb_len = sb->len, offset = 0;
1387 if (c->flush_type == flush_left)
1388 offset = padding - len;
1389 else if (c->flush_type == flush_both)
1390 offset = (padding - len) / 2;
1391 /*
1392 * we calculate padding in columns, now
1393 * convert it back to chars
1394 */
1395 padding = padding - len + local_sb.len;
1396 strbuf_grow(sb, padding);
1397 strbuf_setlen(sb, sb_len + padding);
1398 memset(sb->buf + sb_len, ' ', sb->len - sb_len);
1399 memcpy(sb->buf + sb_len + offset, local_sb.buf,
1400 local_sb.len);
1401 }
1402 strbuf_release(&local_sb);
1403 c->flush_type = no_flush;
1404 return total_consumed;
1405}
1406
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001407static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1408 const char *placeholder,
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001409 void *context)
1410{
1411 int consumed;
1412 size_t orig_len;
1413 enum {
1414 NO_MAGIC,
1415 ADD_LF_BEFORE_NON_EMPTY,
1416 DEL_LF_BEFORE_EMPTY,
Junio C Hamano223a9232010-06-22 09:45:22 -07001417 ADD_SP_BEFORE_NON_EMPTY
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001418 } magic = NO_MAGIC;
1419
1420 switch (placeholder[0]) {
1421 case '-':
1422 magic = DEL_LF_BEFORE_EMPTY;
1423 break;
1424 case '+':
1425 magic = ADD_LF_BEFORE_NON_EMPTY;
1426 break;
Michael J Gruber7b881762010-06-14 18:12:29 +02001427 case ' ':
1428 magic = ADD_SP_BEFORE_NON_EMPTY;
1429 break;
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001430 default:
1431 break;
1432 }
1433 if (magic != NO_MAGIC)
1434 placeholder++;
1435
1436 orig_len = sb->len;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001437 if (((struct format_commit_context *)context)->flush_type != no_flush)
1438 consumed = format_and_pad_commit(sb, placeholder, context);
1439 else
1440 consumed = format_commit_one(sb, placeholder, context);
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001441 if (magic == NO_MAGIC)
1442 return consumed;
1443
1444 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1445 while (sb->len && sb->buf[sb->len - 1] == '\n')
1446 strbuf_setlen(sb, sb->len - 1);
Michael J Gruber7b881762010-06-14 18:12:29 +02001447 } else if (orig_len != sb->len) {
1448 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1449 strbuf_insert(sb, orig_len, "\n", 1);
1450 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1451 strbuf_insert(sb, orig_len, " ", 1);
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001452 }
1453 return consumed + 1;
1454}
1455
Johannes Gilger5b163602010-04-13 22:31:12 +02001456static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1457 void *context)
1458{
1459 struct userformat_want *w = context;
1460
Michael J Gruber7b881762010-06-14 18:12:29 +02001461 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
Johannes Gilger5b163602010-04-13 22:31:12 +02001462 placeholder++;
1463
1464 switch (*placeholder) {
1465 case 'N':
1466 w->notes = 1;
1467 break;
1468 }
1469 return 0;
1470}
1471
1472void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1473{
1474 struct strbuf dummy = STRBUF_INIT;
1475
1476 if (!fmt) {
1477 if (!user_format)
1478 return;
1479 fmt = user_format;
1480 }
Junio C Hamanoa6253d12011-05-25 12:23:44 -07001481 strbuf_expand(&dummy, fmt, userformat_want_item, w);
Johannes Gilger5b163602010-04-13 22:31:12 +02001482 strbuf_release(&dummy);
1483}
1484
René Scharfecde75e52007-11-09 01:49:42 +01001485void format_commit_message(const struct commit *commit,
Junio C Hamano7f98ebc2009-10-15 22:59:41 -07001486 const char *format, struct strbuf *sb,
Thomas Rastdd2e7942009-10-19 17:48:08 +02001487 const struct pretty_print_context *pretty_ctx)
René Scharfecde75e52007-11-09 01:49:42 +01001488{
René Scharfef29d5952007-11-10 12:14:20 +01001489 struct format_commit_context context;
Pat Notz177b29d2010-11-02 13:59:08 -06001490 const char *output_enc = pretty_ctx->output_encoding;
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001491 const char *utf8 = "UTF-8";
René Scharfef29d5952007-11-10 12:14:20 +01001492
1493 memset(&context, 0, sizeof(context));
1494 context.commit = commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +02001495 context.pretty_ctx = pretty_ctx;
René Scharfe02edd562009-10-17 23:04:19 +02001496 context.wrap_start = sb->len;
Alexey Shumkin7d509872014-05-21 17:20:07 +04001497 /*
1498 * convert a commit message to UTF-8 first
1499 * as far as 'format_commit_item' assumes it in UTF-8
1500 */
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001501 context.message = logmsg_reencode(commit,
1502 &context.commit_encoding,
Alexey Shumkin7d509872014-05-21 17:20:07 +04001503 utf8);
Pat Notz177b29d2010-11-02 13:59:08 -06001504
Marco Costalbac3a670d2008-02-09 15:40:19 +01001505 strbuf_expand(sb, format, format_commit_item, &context);
René Scharfe02edd562009-10-17 23:04:19 +02001506 rewrap_message_tail(sb, &context, 0, 0, 0);
Pat Notz177b29d2010-11-02 13:59:08 -06001507
Alexey Shumkin7d509872014-05-21 17:20:07 +04001508 /* then convert a commit message to an actual output encoding */
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001509 if (output_enc) {
1510 if (same_encoding(utf8, output_enc))
1511 output_enc = NULL;
1512 } else {
1513 if (context.commit_encoding &&
1514 !same_encoding(context.commit_encoding, utf8))
1515 output_enc = context.commit_encoding;
1516 }
1517
1518 if (output_enc) {
1519 int outsz;
1520 char *out = reencode_string_len(sb->buf, sb->len,
1521 output_enc, utf8, &outsz);
1522 if (out)
1523 strbuf_attach(sb, out, outsz, outsz + 1);
1524 }
1525
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001526 free(context.commit_encoding);
Jeff Kingb66103c2014-06-10 17:41:39 -04001527 unuse_commit_buffer(commit, context.message);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001528}
1529
Jeff King10f2fbf2013-07-03 03:07:48 -04001530static void pp_header(struct pretty_print_context *pp,
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001531 const char *encoding,
1532 const struct commit *commit,
1533 const char **msg_p,
1534 struct strbuf *sb)
1535{
1536 int parents_shown = 0;
1537
1538 for (;;) {
1539 const char *line = *msg_p;
1540 int linelen = get_one_line(*msg_p);
1541
1542 if (!linelen)
1543 return;
1544 *msg_p += linelen;
1545
1546 if (linelen == 1)
1547 /* End of header */
1548 return;
1549
Jeff King6bf13942011-05-26 18:27:49 -04001550 if (pp->fmt == CMIT_FMT_RAW) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001551 strbuf_add(sb, line, linelen);
1552 continue;
1553 }
1554
Christian Couder59556542013-11-30 21:55:40 +01001555 if (starts_with(line, "parent ")) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001556 if (linelen != 48)
1557 die("bad parent line in commit");
1558 continue;
1559 }
1560
1561 if (!parents_shown) {
René Scharfe4bbaa1e2014-07-17 01:52:09 +02001562 unsigned num = commit_list_count(commit->parents);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001563 /* with enough slop */
1564 strbuf_grow(sb, num * 50 + 20);
Jeff King6bf13942011-05-26 18:27:49 -04001565 add_merge_info(pp, sb, commit);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001566 parents_shown = 1;
1567 }
1568
1569 /*
1570 * MEDIUM == DEFAULT shows only author with dates.
1571 * FULL shows both authors but not dates.
1572 * FULLER shows both authors and dates.
1573 */
Christian Couder59556542013-11-30 21:55:40 +01001574 if (starts_with(line, "author ")) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001575 strbuf_grow(sb, linelen + 80);
Jeff King6bf13942011-05-26 18:27:49 -04001576 pp_user_info(pp, "Author", sb, line + 7, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001577 }
Christian Couder59556542013-11-30 21:55:40 +01001578 if (starts_with(line, "committer ") &&
Jeff King6bf13942011-05-26 18:27:49 -04001579 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001580 strbuf_grow(sb, linelen + 80);
Jeff King6bf13942011-05-26 18:27:49 -04001581 pp_user_info(pp, "Commit", sb, line + 10, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001582 }
1583 }
1584}
1585
Jeff King10f2fbf2013-07-03 03:07:48 -04001586void pp_title_line(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001587 const char **msg_p,
1588 struct strbuf *sb,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001589 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -07001590 int need_8bit_cte)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001591{
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +02001592 static const int max_length = 78; /* per rfc2047 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001593 struct strbuf title;
1594
1595 strbuf_init(&title, 80);
Jeff King9553d2b2011-05-26 18:28:17 -04001596 *msg_p = format_subject(&title, *msg_p,
1597 pp->preserve_subject ? "\n" : " ");
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001598
1599 strbuf_grow(sb, title.len + 1024);
Jeff King6bf13942011-05-26 18:27:49 -04001600 if (pp->subject) {
1601 strbuf_addstr(sb, pp->subject);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +02001602 if (needs_rfc2047_encoding(title.buf, title.len, RFC2047_SUBJECT))
1603 add_rfc2047(sb, title.buf, title.len,
1604 encoding, RFC2047_SUBJECT);
1605 else
1606 strbuf_add_wrapped_bytes(sb, title.buf, title.len,
1607 -last_line_length(sb), 1, max_length);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001608 } else {
1609 strbuf_addbuf(sb, &title);
1610 }
1611 strbuf_addch(sb, '\n');
1612
Jeff Kinga9080472013-07-03 03:08:22 -04001613 if (need_8bit_cte == 0) {
1614 int i;
1615 for (i = 0; i < pp->in_body_headers.nr; i++) {
1616 if (has_non_ascii(pp->in_body_headers.items[i].string)) {
1617 need_8bit_cte = 1;
1618 break;
1619 }
1620 }
1621 }
1622
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001623 if (need_8bit_cte > 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001624 const char *header_fmt =
1625 "MIME-Version: 1.0\n"
1626 "Content-Type: text/plain; charset=%s\n"
1627 "Content-Transfer-Encoding: 8bit\n";
1628 strbuf_addf(sb, header_fmt, encoding);
1629 }
Jeff King6bf13942011-05-26 18:27:49 -04001630 if (pp->after_subject) {
1631 strbuf_addstr(sb, pp->after_subject);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001632 }
Jeff King6bf13942011-05-26 18:27:49 -04001633 if (pp->fmt == CMIT_FMT_EMAIL) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001634 strbuf_addch(sb, '\n');
1635 }
Jeff Kinga9080472013-07-03 03:08:22 -04001636
1637 if (pp->in_body_headers.nr) {
1638 int i;
1639 for (i = 0; i < pp->in_body_headers.nr; i++) {
1640 strbuf_addstr(sb, pp->in_body_headers.items[i].string);
1641 free(pp->in_body_headers.items[i].string);
1642 }
1643 string_list_clear(&pp->in_body_headers, 0);
1644 strbuf_addch(sb, '\n');
1645 }
1646
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001647 strbuf_release(&title);
1648}
1649
Jeff King10f2fbf2013-07-03 03:07:48 -04001650void pp_remainder(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001651 const char **msg_p,
1652 struct strbuf *sb,
1653 int indent)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001654{
1655 int first = 1;
1656 for (;;) {
1657 const char *line = *msg_p;
1658 int linelen = get_one_line(line);
1659 *msg_p += linelen;
1660
1661 if (!linelen)
1662 break;
1663
1664 if (is_empty_line(line, &linelen)) {
1665 if (first)
1666 continue;
Jeff King6bf13942011-05-26 18:27:49 -04001667 if (pp->fmt == CMIT_FMT_SHORT)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001668 break;
1669 }
1670 first = 0;
1671
1672 strbuf_grow(sb, linelen + indent + 20);
1673 if (indent) {
1674 memset(sb->buf + sb->len, ' ', indent);
1675 strbuf_setlen(sb, sb->len + indent);
1676 }
1677 strbuf_add(sb, line, linelen);
1678 strbuf_addch(sb, '\n');
1679 }
1680}
1681
Jeff King10f2fbf2013-07-03 03:07:48 -04001682void pretty_print_commit(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -04001683 const struct commit *commit,
1684 struct strbuf *sb)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001685{
1686 unsigned long beginning_of_body;
1687 int indent = 4;
Jeff Kingdd0d3882013-01-26 04:44:06 -05001688 const char *msg;
Jeff Kingb000c592014-06-10 17:39:30 -04001689 const char *reencoded;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001690 const char *encoding;
Jeff King6bf13942011-05-26 18:27:49 -04001691 int need_8bit_cte = pp->need_8bit_cte;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001692
Jeff King6bf13942011-05-26 18:27:49 -04001693 if (pp->fmt == CMIT_FMT_USERFORMAT) {
1694 format_commit_message(commit, user_format, sb, pp);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001695 return;
1696 }
1697
Junio C Hamanoe297cf52012-10-17 17:12:55 -07001698 encoding = get_log_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +10001699 msg = reencoded = logmsg_reencode(commit, NULL, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001700
Jeff King6bf13942011-05-26 18:27:49 -04001701 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001702 indent = 0;
1703
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001704 /*
1705 * We need to check and emit Content-type: to mark it
1706 * as 8-bit if we haven't done so.
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001707 */
Jeff King6bf13942011-05-26 18:27:49 -04001708 if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001709 int i, ch, in_body;
1710
1711 for (in_body = i = 0; (ch = msg[i]); i++) {
1712 if (!in_body) {
1713 /* author could be non 7-bit ASCII but
1714 * the log may be so; skip over the
1715 * header part first.
1716 */
1717 if (ch == '\n' && msg[i+1] == '\n')
1718 in_body = 1;
1719 }
1720 else if (non_ascii(ch)) {
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001721 need_8bit_cte = 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001722 break;
1723 }
1724 }
1725 }
1726
Jeff King6bf13942011-05-26 18:27:49 -04001727 pp_header(pp, encoding, commit, &msg, sb);
1728 if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001729 strbuf_addch(sb, '\n');
1730 }
1731
1732 /* Skip excess blank lines at the beginning of body, if any... */
René Scharfea0109662008-12-27 01:32:49 +01001733 msg = skip_empty_lines(msg);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001734
1735 /* These formats treat the title line specially. */
Jeff King6bf13942011-05-26 18:27:49 -04001736 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1737 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001738
1739 beginning_of_body = sb->len;
Jeff King6bf13942011-05-26 18:27:49 -04001740 if (pp->fmt != CMIT_FMT_ONELINE)
1741 pp_remainder(pp, &msg, sb, indent);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001742 strbuf_rtrim(sb);
1743
1744 /* Make sure there is an EOLN for the non-oneline case */
Jeff King6bf13942011-05-26 18:27:49 -04001745 if (pp->fmt != CMIT_FMT_ONELINE)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001746 strbuf_addch(sb, '\n');
1747
1748 /*
1749 * The caller may append additional body text in e-mail
1750 * format. Make sure we did not strip the blank line
1751 * between the header and the body.
1752 */
Jeff King6bf13942011-05-26 18:27:49 -04001753 if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001754 strbuf_addch(sb, '\n');
Johannes Schindelina97a7462009-10-09 12:21:57 +02001755
Jeff Kingb66103c2014-06-10 17:41:39 -04001756 unuse_commit_buffer(commit, reencoded);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001757}
Jeff King8b8a5372011-05-26 18:27:24 -04001758
1759void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
1760 struct strbuf *sb)
1761{
1762 struct pretty_print_context pp = {0};
Jeff King6bf13942011-05-26 18:27:49 -04001763 pp.fmt = fmt;
1764 pretty_print_commit(&pp, commit, sb);
Jeff King8b8a5372011-05-26 18:27:24 -04001765}