blob: b1ecd039cef29ecc77edd894c500dfadf49058c8 [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"
René Scharfe15ae82d2021-02-14 11:04:34 +010015#include "run-command.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000016
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000017static char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010018static struct cmt_fmt_map {
19 const char *name;
20 enum cmit_fmt format;
21 int is_tformat;
Junio C Hamano0893eec2016-03-29 15:49:24 -070022 int expand_tabs_in_log;
Will Palmer2d7671e2010-05-02 12:00:43 +010023 int is_alias;
Denton Liu618a8552019-11-19 16:51:23 -080024 enum date_mode_type default_date_mode_type;
Will Palmer2d7671e2010-05-02 12:00:43 +010025 const char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010026} *commit_formats;
Will Palmer80281842010-05-02 12:00:44 +010027static size_t builtin_formats_len;
Will Palmer40957892010-05-02 12:00:42 +010028static size_t commit_formats_len;
Will Palmer80281842010-05-02 12:00:44 +010029static size_t commit_formats_alloc;
Will Palmer40957892010-05-02 12:00:42 +010030static struct cmt_fmt_map *find_commit_format(const char *sought);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000031
Jeff Kingb9c7d6e2014-07-29 13:56:48 -040032int commit_format_is_empty(enum cmit_fmt fmt)
33{
34 return fmt == CMIT_FMT_USERFORMAT && !*user_format;
35}
36
Nanako Shiraishi36407542009-02-24 18:59:15 +090037static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
38{
39 free(user_format);
40 user_format = xstrdup(cp);
41 if (is_tformat)
42 rev->use_terminator = 1;
43 rev->commit_format = CMIT_FMT_USERFORMAT;
44}
45
Will Palmer80281842010-05-02 12:00:44 +010046static int git_pretty_formats_config(const char *var, const char *value, void *cb)
47{
48 struct cmt_fmt_map *commit_format = NULL;
49 const char *name;
50 const char *fmt;
51 int i;
52
Jeff King95b567c2014-06-18 15:48:29 -040053 if (!skip_prefix(var, "pretty.", &name))
Will Palmer80281842010-05-02 12:00:44 +010054 return 0;
55
Will Palmer80281842010-05-02 12:00:44 +010056 for (i = 0; i < builtin_formats_len; i++) {
57 if (!strcmp(commit_formats[i].name, name))
58 return 0;
59 }
60
61 for (i = builtin_formats_len; i < commit_formats_len; i++) {
62 if (!strcmp(commit_formats[i].name, name)) {
63 commit_format = &commit_formats[i];
64 break;
65 }
66 }
67
68 if (!commit_format) {
69 ALLOC_GROW(commit_formats, commit_formats_len+1,
70 commit_formats_alloc);
71 commit_format = &commit_formats[commit_formats_len];
Jonathan Nieder95a26182010-05-08 16:07:39 -050072 memset(commit_format, 0, sizeof(*commit_format));
Will Palmer80281842010-05-02 12:00:44 +010073 commit_formats_len++;
74 }
75
76 commit_format->name = xstrdup(name);
77 commit_format->format = CMIT_FMT_USERFORMAT;
Tanay Abhraa26bc612014-08-04 07:41:15 -070078 if (git_config_string(&fmt, var, value))
79 return -1;
80
René Scharfee3f1da92014-10-04 20:54:50 +020081 if (skip_prefix(fmt, "format:", &fmt))
82 commit_format->is_tformat = 0;
83 else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
Will Palmer80281842010-05-02 12:00:44 +010084 commit_format->is_tformat = 1;
85 else
86 commit_format->is_alias = 1;
87 commit_format->user_format = fmt;
88
89 return 0;
90}
91
Will Palmer40957892010-05-02 12:00:42 +010092static void setup_commit_formats(void)
93{
94 struct cmt_fmt_map builtin_formats[] = {
Junio C Hamano0893eec2016-03-29 15:49:24 -070095 { "raw", CMIT_FMT_RAW, 0, 0 },
Junio C Hamanofe37a9c2016-03-29 16:05:39 -070096 { "medium", CMIT_FMT_MEDIUM, 0, 8 },
Junio C Hamano0893eec2016-03-29 15:49:24 -070097 { "short", CMIT_FMT_SHORT, 0, 0 },
98 { "email", CMIT_FMT_EMAIL, 0, 0 },
Eric Wong9f23e042016-06-05 04:46:39 +000099 { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
Junio C Hamanofe37a9c2016-03-29 16:05:39 -0700100 { "fuller", CMIT_FMT_FULLER, 0, 8 },
101 { "full", CMIT_FMT_FULL, 0, 8 },
Denton Liu1f0fc1d2019-11-19 16:51:25 -0800102 { "oneline", CMIT_FMT_ONELINE, 1, 0 },
103 { "reference", CMIT_FMT_USERFORMAT, 1, 0,
104 0, DATE_SHORT, "%C(auto)%h (%s, %ad)" },
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700105 /*
106 * Please update $__git_log_pretty_formats in
107 * git-completion.bash when you add new formats.
108 */
Will Palmer40957892010-05-02 12:00:42 +0100109 };
110 commit_formats_len = ARRAY_SIZE(builtin_formats);
Will Palmer80281842010-05-02 12:00:44 +0100111 builtin_formats_len = commit_formats_len;
112 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
René Scharfe921d49b2019-06-15 20:36:35 +0200113 COPY_ARRAY(commit_formats, builtin_formats,
114 ARRAY_SIZE(builtin_formats));
Will Palmer80281842010-05-02 12:00:44 +0100115
116 git_config(git_pretty_formats_config, NULL);
Will Palmer40957892010-05-02 12:00:42 +0100117}
118
Will Palmer2d7671e2010-05-02 12:00:43 +0100119static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
120 const char *original,
121 int num_redirections)
Will Palmer40957892010-05-02 12:00:42 +0100122{
123 struct cmt_fmt_map *found = NULL;
124 size_t found_match_len = 0;
125 int i;
126
Will Palmer2d7671e2010-05-02 12:00:43 +0100127 if (num_redirections >= commit_formats_len)
128 die("invalid --pretty format: "
129 "'%s' references an alias which points to itself",
130 original);
Will Palmer40957892010-05-02 12:00:42 +0100131
132 for (i = 0; i < commit_formats_len; i++) {
133 size_t match_len;
134
Christian Couder59556542013-11-30 21:55:40 +0100135 if (!starts_with(commit_formats[i].name, sought))
Will Palmer40957892010-05-02 12:00:42 +0100136 continue;
137
138 match_len = strlen(commit_formats[i].name);
139 if (found == NULL || found_match_len > match_len) {
140 found = &commit_formats[i];
141 found_match_len = match_len;
142 }
143 }
Will Palmer2d7671e2010-05-02 12:00:43 +0100144
145 if (found && found->is_alias) {
146 found = find_commit_format_recursive(found->user_format,
147 original,
148 num_redirections+1);
149 }
150
Will Palmer40957892010-05-02 12:00:42 +0100151 return found;
152}
153
Will Palmer2d7671e2010-05-02 12:00:43 +0100154static struct cmt_fmt_map *find_commit_format(const char *sought)
155{
156 if (!commit_formats)
157 setup_commit_formats();
158
159 return find_commit_format_recursive(sought, sought, 0);
160}
161
Junio C Hamano4da45be2008-04-07 17:11:34 -0700162void get_commit_format(const char *arg, struct rev_info *rev)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000163{
Will Palmer40957892010-05-02 12:00:42 +0100164 struct cmt_fmt_map *commit_format;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000165
Junio C Hamano4da45be2008-04-07 17:11:34 -0700166 rev->use_terminator = 0;
Jeff Kingc75e7ad2014-07-29 13:54:46 -0400167 if (!arg) {
Junio C Hamano4da45be2008-04-07 17:11:34 -0700168 rev->commit_format = CMIT_FMT_DEFAULT;
169 return;
170 }
René Scharfee3f1da92014-10-04 20:54:50 +0200171 if (skip_prefix(arg, "format:", &arg)) {
172 save_user_format(rev, arg, 0);
Junio C Hamano4da45be2008-04-07 17:11:34 -0700173 return;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000174 }
Will Palmer40957892010-05-02 12:00:42 +0100175
René Scharfee3f1da92014-10-04 20:54:50 +0200176 if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
Nanako Shiraishi36407542009-02-24 18:59:15 +0900177 save_user_format(rev, arg, 1);
178 return;
179 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000180
Will Palmer40957892010-05-02 12:00:42 +0100181 commit_format = find_commit_format(arg);
182 if (!commit_format)
183 die("invalid --pretty format: %s", arg);
184
185 rev->commit_format = commit_format->format;
186 rev->use_terminator = commit_format->is_tformat;
Junio C Hamano0893eec2016-03-29 15:49:24 -0700187 rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
Denton Liu618a8552019-11-19 16:51:23 -0800188 if (!rev->date_mode_explicit && commit_format->default_date_mode_type)
189 rev->date_mode.type = commit_format->default_date_mode_type;
Will Palmer80281842010-05-02 12:00:44 +0100190 if (commit_format->format == CMIT_FMT_USERFORMAT) {
191 save_user_format(rev, commit_format->user_format,
192 commit_format->is_tformat);
193 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000194}
195
196/*
197 * Generic support for pretty-printing the header
198 */
199static int get_one_line(const char *msg)
200{
201 int ret = 0;
202
203 for (;;) {
204 char c = *msg++;
205 if (!c)
206 break;
207 ret++;
208 if (c == '\n')
209 break;
210 }
211 return ret;
212}
213
214/* High bit set, or ISO-2022-INT */
Junio C Hamanocc571142010-01-11 22:23:35 -0800215static int non_ascii(int ch)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000216{
René Scharfec2e93642009-03-07 14:06:49 +0100217 return !isascii(ch) || ch == '\033';
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000218}
219
Johannes Schindelin28e9cf62009-08-10 18:22:18 +0200220int has_non_ascii(const char *s)
221{
222 int ch;
223 if (!s)
224 return 0;
225 while ((ch = *s++) != '\0') {
226 if (non_ascii(ch))
227 return 1;
228 }
229 return 0;
230}
231
Jeff King4d03c182011-04-08 18:40:36 -0400232static int is_rfc822_special(char ch)
233{
234 switch (ch) {
235 case '(':
236 case ')':
237 case '<':
238 case '>':
239 case '[':
240 case ']':
241 case ':':
242 case ';':
243 case '@':
244 case ',':
245 case '.':
246 case '"':
247 case '\\':
248 return 1;
249 default:
250 return 0;
251 }
252}
253
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200254static int needs_rfc822_quoting(const char *s, int len)
Jeff King4d03c182011-04-08 18:40:36 -0400255{
256 int i;
257 for (i = 0; i < len; i++)
258 if (is_rfc822_special(s[i]))
259 return 1;
260 return 0;
261}
262
Jan H. Schönherrf9b72042012-10-18 16:43:31 +0200263static int last_line_length(struct strbuf *sb)
264{
265 int i;
266
267 /* How many bytes are already used on the last line? */
268 for (i = sb->len - 1; i >= 0; i--)
269 if (sb->buf[i] == '\n')
270 break;
271 return sb->len - (i + 1);
272}
273
Jeff King4d03c182011-04-08 18:40:36 -0400274static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
275{
276 int i;
277
278 /* just a guess, we may have to also backslash-quote */
279 strbuf_grow(out, len + 2);
280
281 strbuf_addch(out, '"');
282 for (i = 0; i < len; i++) {
283 switch (s[i]) {
284 case '"':
285 case '\\':
286 strbuf_addch(out, '\\');
287 /* fall through */
288 default:
289 strbuf_addch(out, s[i]);
290 }
291 }
292 strbuf_addch(out, '"');
293}
294
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200295enum rfc2047_type {
296 RFC2047_SUBJECT,
Ronnie Sahlberg78273522014-07-02 11:24:05 -0700297 RFC2047_ADDRESS
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200298};
299
300static int is_rfc2047_special(char ch, enum rfc2047_type type)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000301{
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200302 /*
303 * rfc2047, section 4.2:
304 *
305 * 8-bit values which correspond to printable ASCII characters other
306 * than "=", "?", and "_" (underscore), MAY be represented as those
307 * characters. (But see section 5 for restrictions.) In
308 * particular, SPACE and TAB MUST NOT be represented as themselves
309 * within encoded words.
310 */
311
312 /*
313 * rule out non-ASCII characters and non-printable characters (the
314 * non-ASCII check should be redundant as isprint() is not localized
315 * and only knows about ASCII, but be defensive about that)
316 */
317 if (non_ascii(ch) || !isprint(ch))
Jan H. Schönherr94f6cdf2012-10-18 16:43:30 +0200318 return 1;
319
Jan H. Schönherr0fcec2c2012-10-18 16:43:32 +0200320 /*
321 * rule out special printable characters (' ' should be the only
322 * whitespace character considered printable, but be defensive and use
323 * isspace())
324 */
325 if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
326 return 1;
327
328 /*
329 * rfc2047, section 5.3:
330 *
331 * As a replacement for a 'word' entity within a 'phrase', for example,
332 * one that precedes an address in a From, To, or Cc header. The ABNF
333 * definition for 'phrase' from RFC 822 thus becomes:
334 *
335 * phrase = 1*( encoded-word / word )
336 *
337 * In this case the set of characters that may be used in a "Q"-encoded
338 * 'encoded-word' is restricted to: <upper and lower case ASCII
339 * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
340 * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
341 * 'phrase' MUST be separated from any adjacent 'word', 'text' or
342 * 'special' by 'linear-white-space'.
343 */
344
345 if (type != RFC2047_ADDRESS)
346 return 0;
347
348 /* '=' and '_' are special cases and have been checked above */
349 return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000350}
351
Jeff Kingda55ff32019-03-20 04:16:36 -0400352static int needs_rfc2047_encoding(const char *line, int len)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000353{
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500354 int i;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000355
356 for (i = 0; i < len; i++) {
357 int ch = line[i];
Jeff Kingc22e7de2011-02-23 04:59:18 -0500358 if (non_ascii(ch) || ch == '\n')
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200359 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000360 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200361 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000362 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000363
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200364 return 0;
365}
366
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400367static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200368 const char *encoding, enum rfc2047_type type)
369{
370 static const int max_encoded_length = 76; /* per rfc2047 */
371 int i;
372 int line_len = last_line_length(sb);
373
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000374 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
375 strbuf_addf(sb, "=?%s?q?", encoding);
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500376 line_len += strlen(encoding) + 5; /* 5 for =??q? */
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400377
378 while (len) {
379 /*
380 * RFC 2047, section 5 (3):
381 *
382 * Each 'encoded-word' MUST represent an integral number of
383 * characters. A multi-octet character may not be split across
384 * adjacent 'encoded- word's.
385 */
386 const unsigned char *p = (const unsigned char *)line;
387 int chrlen = mbs_chrlen(&line, &len, encoding);
388 int is_special = (chrlen > 1) || is_rfc2047_special(*p, type);
389
390 /* "=%02X" * chrlen, or the byte itself */
391 const char *encoded_fmt = is_special ? "=%02X" : "%c";
392 int encoded_len = is_special ? 3 * chrlen : 1;
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500393
Jan H. Schönherr94f6cdf2012-10-18 16:43:30 +0200394 /*
395 * According to RFC 2047, we could encode the special character
396 * ' ' (space) with '_' (underscore) for readability. But many
397 * programs do not understand this and just leave the
398 * underscore in place. Thus, we do nothing special here, which
399 * causes ' ' to be encoded as '=20', avoiding this problem.
400 */
401
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400402 if (line_len + encoded_len + 2 > max_encoded_length) {
403 /* It won't fit with trailing "?=" --- break the line */
Jeff Kinga1f6baa2011-02-23 04:58:41 -0500404 strbuf_addf(sb, "?=\n =?%s?q?", encoding);
405 line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
406 }
407
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400408 for (i = 0; i < chrlen; i++)
409 strbuf_addf(sb, encoded_fmt, p[i]);
410 line_len += encoded_len;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000411 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000412 strbuf_addstr(sb, "?=");
413}
414
Jeff Kingd1053242014-05-01 21:07:22 -0400415const char *show_ident_date(const struct ident_split *ident,
Jeff Kinga5481a62015-06-25 12:55:02 -0400416 const struct date_mode *mode)
René Scharfe9dbe7c32013-04-17 20:33:35 +0200417{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200418 timestamp_t date = 0;
Jeff King3f419d42014-03-07 12:15:01 -0500419 long tz = 0;
René Scharfe9dbe7c32013-04-17 20:33:35 +0200420
421 if (ident->date_begin && ident->date_end)
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200422 date = parse_timestamp(ident->date_begin, NULL, 10);
Jeff King1dca1552014-02-24 02:46:37 -0500423 if (date_overflows(date))
424 date = 0;
425 else {
426 if (ident->tz_begin && ident->tz_end)
427 tz = strtol(ident->tz_begin, NULL, 10);
Jeff King3f419d42014-03-07 12:15:01 -0500428 if (tz >= INT_MAX || tz <= INT_MIN)
Jeff King1dca1552014-02-24 02:46:37 -0500429 tz = 0;
430 }
René Scharfe9dbe7c32013-04-17 20:33:35 +0200431 return show_date(date, tz, mode);
432}
433
Jeff King10f2fbf2013-07-03 03:07:48 -0400434void pp_user_info(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -0400435 const char *what, struct strbuf *sb,
436 const char *line, const char *encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000437{
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100438 struct ident_split ident;
René Scharfe9dbe7c32013-04-17 20:33:35 +0200439 char *line_end;
Antoine Pelissedffd3252013-01-05 22:26:42 +0100440 const char *mailbuf, *namebuf;
441 size_t namelen, maillen;
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200442 int max_length = 78; /* per rfc2822 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000443
Jeff King6bf13942011-05-26 18:27:49 -0400444 if (pp->fmt == CMIT_FMT_ONELINE)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000445 return;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100446
René Scharfe30e77bc2013-04-25 21:40:25 +0200447 line_end = strchrnul(line, '\n');
448 if (split_ident_line(&ident, line, line_end - line))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000449 return;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +0100450
Antoine Pelissedffd3252013-01-05 22:26:42 +0100451 mailbuf = ident.mail_begin;
452 maillen = ident.mail_end - ident.mail_begin;
453 namebuf = ident.name_begin;
454 namelen = ident.name_end - ident.name_begin;
455
456 if (pp->mailmap)
457 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
458
Eric Wong9f23e042016-06-05 04:46:39 +0000459 if (cmit_fmt_is_mail(pp->fmt)) {
Jeff King662cc302013-09-20 06:16:28 -0400460 if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
Jeff Kinga9080472013-07-03 03:08:22 -0400461 struct strbuf buf = STRBUF_INIT;
462
463 strbuf_addstr(&buf, "From: ");
464 strbuf_add(&buf, namebuf, namelen);
465 strbuf_addstr(&buf, " <");
466 strbuf_add(&buf, mailbuf, maillen);
467 strbuf_addstr(&buf, ">\n");
468 string_list_append(&pp->in_body_headers,
469 strbuf_detach(&buf, NULL));
470
471 mailbuf = pp->from_ident->mail_begin;
472 maillen = pp->from_ident->mail_end - mailbuf;
473 namebuf = pp->from_ident->name_begin;
474 namelen = pp->from_ident->name_end - namebuf;
475 }
476
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000477 strbuf_addstr(sb, "From: ");
Emma Brooks19d097e2020-04-08 04:31:38 +0000478 if (pp->encode_email_headers &&
479 needs_rfc2047_encoding(namebuf, namelen)) {
René Scharfea0511b32013-04-25 21:43:56 +0200480 add_rfc2047(sb, namebuf, namelen,
Antoine Pelissedffd3252013-01-05 22:26:42 +0100481 encoding, RFC2047_ADDRESS);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200482 max_length = 76; /* per rfc2047 */
René Scharfea0511b32013-04-25 21:43:56 +0200483 } else if (needs_rfc822_quoting(namebuf, namelen)) {
Jeff King4d03c182011-04-08 18:40:36 -0400484 struct strbuf quoted = STRBUF_INIT;
René Scharfea0511b32013-04-25 21:43:56 +0200485 add_rfc822_quoted(&quoted, namebuf, namelen);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200486 strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
487 -6, 1, max_length);
Jeff King4d03c182011-04-08 18:40:36 -0400488 strbuf_release(&quoted);
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +0200489 } else {
René Scharfea0511b32013-04-25 21:43:56 +0200490 strbuf_add_wrapped_bytes(sb, namebuf, namelen,
Antoine Pelissedffd3252013-01-05 22:26:42 +0100491 -6, 1, max_length);
Jeff King4d03c182011-04-08 18:40:36 -0400492 }
Antoine Pelissedffd3252013-01-05 22:26:42 +0100493
René Scharfe97a17e72013-04-25 21:41:57 +0200494 if (max_length <
495 last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
Jeff King990f6e32011-04-14 18:18:09 -0400496 strbuf_addch(sb, '\n');
René Scharfea0511b32013-04-25 21:43:56 +0200497 strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000498 } else {
René Scharfea0511b32013-04-25 21:43:56 +0200499 strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
500 (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
501 (int)namelen, namebuf, (int)maillen, mailbuf);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000502 }
Antoine Pelissedffd3252013-01-05 22:26:42 +0100503
Jeff King6bf13942011-05-26 18:27:49 -0400504 switch (pp->fmt) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000505 case CMIT_FMT_MEDIUM:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200506 strbuf_addf(sb, "Date: %s\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400507 show_ident_date(&ident, &pp->date_mode));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000508 break;
509 case CMIT_FMT_EMAIL:
Eric Wong9f23e042016-06-05 04:46:39 +0000510 case CMIT_FMT_MBOXRD:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200511 strbuf_addf(sb, "Date: %s\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400512 show_ident_date(&ident, DATE_MODE(RFC2822)));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000513 break;
514 case CMIT_FMT_FULLER:
René Scharfe9dbe7c32013-04-17 20:33:35 +0200515 strbuf_addf(sb, "%sDate: %s\n", what,
Jeff Kinga5481a62015-06-25 12:55:02 -0400516 show_ident_date(&ident, &pp->date_mode));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000517 break;
518 default:
519 /* notin' */
520 break;
521 }
522}
523
Johannes Schindelin77356122016-06-22 22:20:16 +0200524static int is_blank_line(const char *line, int *len_p)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000525{
526 int len = *len_p;
Felipe Contreras35b2fa52013-10-31 03:25:42 -0600527 while (len && isspace(line[len - 1]))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000528 len--;
529 *len_p = len;
530 return !len;
531}
532
Johannes Schindelin77356122016-06-22 22:20:16 +0200533const char *skip_blank_lines(const char *msg)
René Scharfea0109662008-12-27 01:32:49 +0100534{
535 for (;;) {
536 int linelen = get_one_line(msg);
537 int ll = linelen;
538 if (!linelen)
539 break;
Johannes Schindelin77356122016-06-22 22:20:16 +0200540 if (!is_blank_line(msg, &ll))
René Scharfea0109662008-12-27 01:32:49 +0100541 break;
542 msg += linelen;
543 }
544 return msg;
545}
546
Jeff King6bf13942011-05-26 18:27:49 -0400547static void add_merge_info(const struct pretty_print_context *pp,
548 struct strbuf *sb, const struct commit *commit)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000549{
550 struct commit_list *parent = commit->parents;
551
Eric Wong9f23e042016-06-05 04:46:39 +0000552 if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000553 !parent || !parent->next)
554 return;
555
556 strbuf_addstr(sb, "Merge:");
557
558 while (parent) {
René Scharfea94bb682016-10-08 17:38:47 +0200559 struct object_id *oidp = &parent->item->object.oid;
560 strbuf_addch(sb, ' ');
Jeff King6bf13942011-05-26 18:27:49 -0400561 if (pp->abbrev)
brian m. carlson30e677e2018-03-12 02:27:28 +0000562 strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
René Scharfea94bb682016-10-08 17:38:47 +0200563 else
564 strbuf_addstr(sb, oid_to_hex(oidp));
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000565 parent = parent->next;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000566 }
567 strbuf_addch(sb, '\n');
568}
569
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400570static char *get_header(const char *msg, const char *key)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000571{
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400572 size_t len;
573 const char *v = find_commit_header(msg, key, &len);
574 return v ? xmemdupz(v, len) : NULL;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000575}
576
577static char *replace_encoding_header(char *buf, const char *encoding)
578{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500579 struct strbuf tmp = STRBUF_INIT;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000580 size_t start, len;
581 char *cp = buf;
582
583 /* guess if there is an encoding header before a \n\n */
René Scharfe68d6d6e2015-02-21 20:53:09 +0100584 while (!starts_with(cp, "encoding ")) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000585 cp = strchr(cp, '\n');
586 if (!cp || *++cp == '\n')
587 return buf;
588 }
589 start = cp - buf;
590 cp = strchr(cp, '\n');
591 if (!cp)
592 return buf; /* should not happen but be defensive */
593 len = cp + 1 - (buf + start);
594
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000595 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
596 if (is_encoding_utf8(encoding)) {
597 /* we have re-coded to UTF-8; drop the header */
598 strbuf_remove(&tmp, start, len);
599 } else {
600 /* just replaces XXXX in 'encoding XXXX\n' */
601 strbuf_splice(&tmp, start + strlen("encoding "),
602 len - strlen("encoding \n"),
603 encoding, strlen(encoding));
604 }
605 return strbuf_detach(&tmp, NULL);
606}
607
Stefan Beller424510e2018-11-13 16:12:59 -0800608const char *repo_logmsg_reencode(struct repository *r,
609 const struct commit *commit,
610 char **commit_encoding,
611 const char *output_encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000612{
Brandon Casey330db182009-05-18 18:44:39 -0500613 static const char *utf8 = "UTF-8";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000614 const char *use_encoding;
615 char *encoding;
Stefan Beller424510e2018-11-13 16:12:59 -0800616 const char *msg = repo_get_commit_buffer(r, commit, NULL);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000617 char *out;
618
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000619 if (!output_encoding || !*output_encoding) {
620 if (commit_encoding)
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400621 *commit_encoding = get_header(msg, "encoding");
Jeff Kingdd0d3882013-01-26 04:44:06 -0500622 return msg;
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000623 }
Jeff Kingfe6eb7f2014-08-27 03:56:01 -0400624 encoding = get_header(msg, "encoding");
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000625 if (commit_encoding)
626 *commit_encoding = encoding;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000627 use_encoding = encoding ? encoding : utf8;
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500628 if (same_encoding(use_encoding, output_encoding)) {
629 /*
630 * No encoding work to be done. If we have no encoding header
631 * at all, then there's nothing to do, and we can return the
632 * message verbatim (whether newly allocated or not).
633 */
634 if (!encoding)
635 return msg;
636
637 /*
638 * Otherwise, we still want to munge the encoding header in the
639 * result, which will be done by modifying the buffer. If we
640 * are using a fresh copy, we can reuse it. But if we are using
Jeff Kingb66103c2014-06-10 17:41:39 -0400641 * the cached copy from get_commit_buffer, we need to duplicate it
642 * to avoid munging the cached copy.
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500643 */
Stefan Beller424510e2018-11-13 16:12:59 -0800644 if (msg == get_cached_commit_buffer(r, commit, NULL))
Jeff Kingb66103c2014-06-10 17:41:39 -0400645 out = xstrdup(msg);
646 else
647 out = (char *)msg;
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500648 }
649 else {
650 /*
651 * There's actual encoding work to do. Do the reencoding, which
652 * still leaves the header to be replaced in the next step. At
653 * this point, we are done with msg. If we allocated a fresh
654 * copy, we can free it.
655 */
656 out = reencode_string(msg, output_encoding, use_encoding);
Jeff Kingb66103c2014-06-10 17:41:39 -0400657 if (out)
Stefan Beller424510e2018-11-13 16:12:59 -0800658 repo_unuse_commit_buffer(r, commit, msg);
Jeff Kingbe5c9fb2013-01-26 04:44:28 -0500659 }
660
661 /*
662 * This replacement actually consumes the buffer we hand it, so we do
663 * not have to worry about freeing the old "out" here.
664 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000665 if (out)
666 out = replace_encoding_header(out, output_encoding);
667
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +1000668 if (!commit_encoding)
669 free(encoding);
Jeff Kingdd0d3882013-01-26 04:44:06 -0500670 /*
671 * If the re-encoding failed, out might be NULL here; in that
672 * case we just return the commit message verbatim.
673 */
674 return out ? out : msg;
675}
676
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100677static int mailmap_name(const char **email, size_t *email_len,
678 const char **name, size_t *name_len)
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100679{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100680 static struct string_list *mail_map;
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100681 if (!mail_map) {
René Scharfeca56dad2021-03-13 17:17:22 +0100682 CALLOC_ARRAY(mail_map, 1);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100683 read_mailmap(mail_map);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100684 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100685 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100686}
687
Marco Costalbac3a670d2008-02-09 15:40:19 +0100688static size_t format_person_part(struct strbuf *sb, char part,
Jeff Kinga5481a62015-06-25 12:55:02 -0400689 const char *msg, int len,
690 const struct date_mode *dmode)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000691{
Marco Costalbac3a670d2008-02-09 15:40:19 +0100692 /* currently all placeholders have same length */
693 const int placeholder_len = 2;
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800694 struct ident_split s;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100695 const char *name, *mail;
696 size_t maillen, namelen;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000697
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800698 if (split_ident_line(&s, msg, len) < 0)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100699 goto skip;
700
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100701 name = s.name_begin;
702 namelen = s.name_end - s.name_begin;
703 mail = s.mail_begin;
704 maillen = s.mail_end - s.mail_begin;
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100705
Prarit Bhargavad8b82172019-10-29 08:09:14 -0400706 if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100707 mailmap_name(&mail, &maillen, &name, &namelen);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100708 if (part == 'n' || part == 'N') { /* name */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100709 strbuf_add(sb, name, namelen);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100710 return placeholder_len;
711 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100712 if (part == 'e' || part == 'E') { /* email */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100713 strbuf_add(sb, mail, maillen);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100714 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100715 }
Prarit Bhargavad8b82172019-10-29 08:09:14 -0400716 if (part == 'l' || part == 'L') { /* local-part */
717 const char *at = memchr(mail, '@', maillen);
718 if (at)
719 maillen = at - mail;
720 strbuf_add(sb, mail, maillen);
721 return placeholder_len;
722 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000723
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800724 if (!s.date_begin)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100725 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000726
René Scharfecde75e52007-11-09 01:49:42 +0100727 if (part == 't') { /* date, UNIX timestamp */
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800728 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100729 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100730 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000731
René Scharfecde75e52007-11-09 01:49:42 +0100732 switch (part) {
733 case 'd': /* date */
René Scharfe9dbe7c32013-04-17 20:33:35 +0200734 strbuf_addstr(sb, show_ident_date(&s, dmode));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100735 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100736 case 'D': /* date, RFC2822 style */
Jeff Kinga5481a62015-06-25 12:55:02 -0400737 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100738 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100739 case 'r': /* date, relative */
Jeff Kinga5481a62015-06-25 12:55:02 -0400740 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100741 return placeholder_len;
Beat Bolli466fb672014-08-29 18:58:42 +0200742 case 'i': /* date, ISO 8601-like */
Jeff Kinga5481a62015-06-25 12:55:02 -0400743 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100744 return placeholder_len;
Beat Bolli466fb672014-08-29 18:58:42 +0200745 case 'I': /* date, ISO 8601 strict */
Jeff Kinga5481a62015-06-25 12:55:02 -0400746 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
Beat Bolli466fb672014-08-29 18:58:42 +0200747 return placeholder_len;
ZheNing Hub722d452021-04-25 10:41:45 +0000748 case 'h': /* date, human */
749 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(HUMAN)));
750 return placeholder_len;
René Scharfe0df62112019-11-19 16:51:21 -0800751 case 's':
752 strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT)));
753 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100754 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100755
756skip:
757 /*
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800758 * reading from either a bogus commit, or a reflog entry with
759 * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
760 * to compute a valid return value.
Marco Costalbac3a670d2008-02-09 15:40:19 +0100761 */
762 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
763 || part == 'D' || part == 'r' || part == 'i')
764 return placeholder_len;
765
766 return 0; /* unknown placeholder */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000767}
768
René Scharfef29d5952007-11-10 12:14:20 +0100769struct chunk {
770 size_t off;
771 size_t len;
772};
773
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000774enum flush_type {
775 no_flush,
776 flush_right,
777 flush_left,
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +1000778 flush_left_and_steal,
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000779 flush_both
780};
781
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000782enum trunc_type {
783 trunc_none,
784 trunc_left,
785 trunc_middle,
786 trunc_right
787};
788
René Scharfef29d5952007-11-10 12:14:20 +0100789struct format_commit_context {
Jeff King018b9de2021-01-28 14:57:39 -0500790 struct repository *repository;
René Scharfef29d5952007-11-10 12:14:20 +0100791 const struct commit *commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200792 const struct pretty_print_context *pretty_ctx;
René Scharfef53bd742008-12-27 01:49:21 +0100793 unsigned commit_header_parsed:1;
794 unsigned commit_message_parsed:1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +0200795 struct signature_check signature_check;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000796 enum flush_type flush_type;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000797 enum trunc_type truncate;
Jeff Kingb000c592014-06-10 17:39:30 -0400798 const char *message;
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +1000799 char *commit_encoding;
René Scharfe02edd562009-10-17 23:04:19 +0200800 size_t width, indent1, indent2;
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +1000801 int auto_color;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +1000802 int padding;
René Scharfef29d5952007-11-10 12:14:20 +0100803
804 /* These offsets are relative to the start of the commit message. */
René Scharfef29d5952007-11-10 12:14:20 +0100805 struct chunk author;
806 struct chunk committer;
René Scharfef53bd742008-12-27 01:49:21 +0100807 size_t message_off;
808 size_t subject_off;
René Scharfef29d5952007-11-10 12:14:20 +0100809 size_t body_off;
René Scharfeb9c62322007-11-10 12:18:26 +0100810
811 /* The following ones are relative to the result struct strbuf. */
René Scharfe02edd562009-10-17 23:04:19 +0200812 size_t wrap_start;
René Scharfef29d5952007-11-10 12:14:20 +0100813};
814
815static void parse_commit_header(struct format_commit_context *context)
816{
Pat Notz177b29d2010-11-02 13:59:08 -0600817 const char *msg = context->message;
René Scharfef29d5952007-11-10 12:14:20 +0100818 int i;
René Scharfef29d5952007-11-10 12:14:20 +0100819
René Scharfef53bd742008-12-27 01:49:21 +0100820 for (i = 0; msg[i]; i++) {
René Scharfee3f1da92014-10-04 20:54:50 +0200821 const char *name;
René Scharfef29d5952007-11-10 12:14:20 +0100822 int eol;
823 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
824 ; /* do nothing */
825
René Scharfef29d5952007-11-10 12:14:20 +0100826 if (i == eol) {
René Scharfef53bd742008-12-27 01:49:21 +0100827 break;
René Scharfee3f1da92014-10-04 20:54:50 +0200828 } else if (skip_prefix(msg + i, "author ", &name)) {
829 context->author.off = name - msg;
830 context->author.len = msg + eol - name;
831 } else if (skip_prefix(msg + i, "committer ", &name)) {
832 context->committer.off = name - msg;
833 context->committer.len = msg + eol - name;
René Scharfef29d5952007-11-10 12:14:20 +0100834 }
835 i = eol;
836 }
René Scharfef53bd742008-12-27 01:49:21 +0100837 context->message_off = i;
René Scharfef29d5952007-11-10 12:14:20 +0100838 context->commit_header_parsed = 1;
839}
840
Stephen Boyd46d164b2009-03-22 19:14:01 -0700841static int istitlechar(char c)
842{
843 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
844 (c >= '0' && c <= '9') || c == '.' || c == '_';
845}
846
Hariom Verma47d46762020-08-21 21:41:49 +0000847void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
Stephen Boyd46d164b2009-03-22 19:14:01 -0700848{
849 size_t trimlen;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700850 size_t start_len = sb->len;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700851 int space = 2;
Hariom Verma47d46762020-08-21 21:41:49 +0000852 int i;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700853
Hariom Verma47d46762020-08-21 21:41:49 +0000854 for (i = 0; i < len; i++) {
855 if (istitlechar(msg[i])) {
Stephen Boyd46d164b2009-03-22 19:14:01 -0700856 if (space == 1)
857 strbuf_addch(sb, '-');
858 space = 0;
Hariom Verma47d46762020-08-21 21:41:49 +0000859 strbuf_addch(sb, msg[i]);
860 if (msg[i] == '.')
861 while (msg[i+1] == '.')
862 i++;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700863 } else
864 space |= 1;
865 }
866
867 /* trim any trailing '.' or '-' characters */
868 trimlen = 0;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700869 while (sb->len - trimlen > start_len &&
870 (sb->buf[sb->len - 1 - trimlen] == '.'
871 || sb->buf[sb->len - 1 - trimlen] == '-'))
Stephen Boyd46d164b2009-03-22 19:14:01 -0700872 trimlen++;
873 strbuf_remove(sb, sb->len - trimlen, trimlen);
874}
875
René Scharfecec08712009-01-06 21:41:06 +0100876const char *format_subject(struct strbuf *sb, const char *msg,
877 const char *line_separator)
René Scharfe88c44732008-12-27 01:39:35 +0100878{
879 int first = 1;
880
881 for (;;) {
882 const char *line = msg;
883 int linelen = get_one_line(line);
884
885 msg += linelen;
Johannes Schindelin77356122016-06-22 22:20:16 +0200886 if (!linelen || is_blank_line(line, &linelen))
René Scharfe88c44732008-12-27 01:39:35 +0100887 break;
888
René Scharfef53bd742008-12-27 01:49:21 +0100889 if (!sb)
890 continue;
René Scharfe88c44732008-12-27 01:39:35 +0100891 strbuf_grow(sb, linelen + 2);
892 if (!first)
893 strbuf_addstr(sb, line_separator);
894 strbuf_add(sb, line, linelen);
895 first = 0;
896 }
897 return msg;
898}
899
René Scharfef53bd742008-12-27 01:49:21 +0100900static void parse_commit_message(struct format_commit_context *c)
901{
Pat Notz177b29d2010-11-02 13:59:08 -0600902 const char *msg = c->message + c->message_off;
903 const char *start = c->message;
René Scharfef53bd742008-12-27 01:49:21 +0100904
Johannes Schindelin77356122016-06-22 22:20:16 +0200905 msg = skip_blank_lines(msg);
René Scharfef53bd742008-12-27 01:49:21 +0100906 c->subject_off = msg - start;
907
908 msg = format_subject(NULL, msg, NULL);
Johannes Schindelin77356122016-06-22 22:20:16 +0200909 msg = skip_blank_lines(msg);
René Scharfef53bd742008-12-27 01:49:21 +0100910 c->body_off = msg - start;
911
912 c->commit_message_parsed = 1;
913}
914
René Scharfe02edd562009-10-17 23:04:19 +0200915static void strbuf_wrap(struct strbuf *sb, size_t pos,
916 size_t width, size_t indent1, size_t indent2)
917{
918 struct strbuf tmp = STRBUF_INIT;
919
920 if (pos)
921 strbuf_add(&tmp, sb->buf, pos);
922 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
923 (int) indent1, (int) indent2, (int) width);
924 strbuf_swap(&tmp, sb);
925 strbuf_release(&tmp);
926}
927
928static void rewrap_message_tail(struct strbuf *sb,
929 struct format_commit_context *c,
930 size_t new_width, size_t new_indent1,
931 size_t new_indent2)
932{
933 if (c->width == new_width && c->indent1 == new_indent1 &&
934 c->indent2 == new_indent2)
935 return;
René Scharfe32ca4242009-11-08 02:04:21 +0100936 if (c->wrap_start < sb->len)
René Scharfe02edd562009-10-17 23:04:19 +0200937 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
938 c->wrap_start = sb->len;
939 c->width = new_width;
940 c->indent1 = new_indent1;
941 c->indent2 = new_indent2;
942}
943
Jeff Kingcd1957f2011-12-16 06:40:24 -0500944static int format_reflog_person(struct strbuf *sb,
945 char part,
946 struct reflog_walk_info *log,
Jeff Kinga5481a62015-06-25 12:55:02 -0400947 const struct date_mode *dmode)
Jeff Kingcd1957f2011-12-16 06:40:24 -0500948{
949 const char *ident;
950
951 if (!log)
952 return 2;
953
954 ident = get_reflog_ident(log);
955 if (!ident)
956 return 2;
957
958 return format_person_part(sb, part, ident, strlen(ident), dmode);
959}
960
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000961static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
962 const char *placeholder,
963 struct format_commit_context *c)
964{
René Scharfee3f1da92014-10-04 20:54:50 +0200965 const char *rest = placeholder;
Jeff King18fb7ff2017-07-13 11:08:46 -0400966 const char *basic_color = NULL;
René Scharfee3f1da92014-10-04 20:54:50 +0200967
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000968 if (placeholder[1] == '(') {
969 const char *begin = placeholder + 2;
970 const char *end = strchr(begin, ')');
971 char color[COLOR_MAXLEN];
972
973 if (!end)
974 return 0;
Jeff King18fb7ff2017-07-13 11:08:46 -0400975
René Scharfee3f1da92014-10-04 20:54:50 +0200976 if (skip_prefix(begin, "auto,", &begin)) {
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000977 if (!want_color(c->pretty_ctx->color))
978 return end - placeholder + 1;
Jeff King18fb7ff2017-07-13 11:08:46 -0400979 } else if (skip_prefix(begin, "always,", &begin)) {
980 /* nothing to do; we do not respect want_color at all */
981 } else {
982 /* the default is the same as "auto" */
983 if (!want_color(c->pretty_ctx->color))
984 return end - placeholder + 1;
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000985 }
Jeff King18fb7ff2017-07-13 11:08:46 -0400986
Jeff Kingf6c5a292014-10-07 15:33:09 -0400987 if (color_parse_mem(begin, end - begin, color) < 0)
988 die(_("unable to parse --pretty format"));
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +1000989 strbuf_addstr(sb, color);
990 return end - placeholder + 1;
991 }
Jeff King18fb7ff2017-07-13 11:08:46 -0400992
993 /*
994 * We handle things like "%C(red)" above; for historical reasons, there
995 * are a few colors that can be specified without parentheses (and
996 * they cannot support things like "auto" or "always" at all).
997 */
René Scharfee3f1da92014-10-04 20:54:50 +0200998 if (skip_prefix(placeholder + 1, "red", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -0400999 basic_color = GIT_COLOR_RED;
René Scharfee3f1da92014-10-04 20:54:50 +02001000 else if (skip_prefix(placeholder + 1, "green", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -04001001 basic_color = GIT_COLOR_GREEN;
René Scharfee3f1da92014-10-04 20:54:50 +02001002 else if (skip_prefix(placeholder + 1, "blue", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -04001003 basic_color = GIT_COLOR_BLUE;
René Scharfee3f1da92014-10-04 20:54:50 +02001004 else if (skip_prefix(placeholder + 1, "reset", &rest))
Jeff King18fb7ff2017-07-13 11:08:46 -04001005 basic_color = GIT_COLOR_RESET;
1006
1007 if (basic_color && want_color(c->pretty_ctx->color))
1008 strbuf_addstr(sb, basic_color);
1009
René Scharfee3f1da92014-10-04 20:54:50 +02001010 return rest - placeholder;
Nguyễn Thái Ngọc Duyfcabc2d2013-04-19 09:08:48 +10001011}
1012
Jeff King9a1180f2019-03-20 04:16:42 -04001013static size_t parse_padding_placeholder(const char *placeholder,
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001014 struct format_commit_context *c)
1015{
1016 const char *ch = placeholder;
1017 enum flush_type flush_type;
1018 int to_column = 0;
1019
1020 switch (*ch++) {
1021 case '<':
1022 flush_type = flush_right;
1023 break;
1024 case '>':
1025 if (*ch == '<') {
1026 flush_type = flush_both;
1027 ch++;
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10001028 } else if (*ch == '>') {
1029 flush_type = flush_left_and_steal;
1030 ch++;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001031 } else
1032 flush_type = flush_left;
1033 break;
1034 default:
1035 return 0;
1036 }
1037
1038 /* the next value means "wide enough to that column" */
1039 if (*ch == '|') {
1040 to_column = 1;
1041 ch++;
1042 }
1043
1044 if (*ch == '(') {
1045 const char *start = ch + 1;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001046 const char *end = start + strcspn(start, ",)");
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001047 char *next;
1048 int width;
1049 if (!end || end == start)
1050 return 0;
Nguyễn Thái Ngọc Duy066790d2016-06-16 20:18:38 +07001051 width = strtol(start, &next, 10);
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001052 if (next == start || width == 0)
1053 return 0;
Nguyễn Thái Ngọc Duy066790d2016-06-16 20:18:38 +07001054 if (width < 0) {
1055 if (to_column)
1056 width += term_columns();
1057 if (width < 0)
1058 return 0;
1059 }
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001060 c->padding = to_column ? -width : width;
1061 c->flush_type = flush_type;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001062
1063 if (*end == ',') {
1064 start = end + 1;
1065 end = strchr(start, ')');
1066 if (!end || end == start)
1067 return 0;
Christian Couder59556542013-11-30 21:55:40 +01001068 if (starts_with(start, "trunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001069 c->truncate = trunc_right;
Christian Couder59556542013-11-30 21:55:40 +01001070 else if (starts_with(start, "ltrunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001071 c->truncate = trunc_left;
Christian Couder59556542013-11-30 21:55:40 +01001072 else if (starts_with(start, "mtrunc)"))
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001073 c->truncate = trunc_middle;
1074 else
1075 return 0;
1076 } else
1077 c->truncate = trunc_none;
1078
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001079 return end - placeholder + 1;
1080 }
1081 return 0;
1082}
1083
Anders Waldenborg4f732e02019-01-29 07:49:00 +01001084static int match_placeholder_arg_value(const char *to_parse, const char *candidate,
1085 const char **end, const char **valuestart,
1086 size_t *valuelen)
Taylor Blau84ff0532017-10-01 09:18:47 -07001087{
1088 const char *p;
1089
1090 if (!(skip_prefix(to_parse, candidate, &p)))
1091 return 0;
Anders Waldenborg4f732e02019-01-29 07:49:00 +01001092 if (valuestart) {
1093 if (*p == '=') {
1094 *valuestart = p + 1;
1095 *valuelen = strcspn(*valuestart, ",)");
1096 p = *valuestart + *valuelen;
1097 } else {
1098 if (*p != ',' && *p != ')')
1099 return 0;
1100 *valuestart = NULL;
1101 *valuelen = 0;
1102 }
1103 }
Taylor Blau84ff0532017-10-01 09:18:47 -07001104 if (*p == ',') {
1105 *end = p + 1;
1106 return 1;
1107 }
1108 if (*p == ')') {
1109 *end = p;
1110 return 1;
1111 }
1112 return 0;
1113}
1114
Anders Waldenborg4f732e02019-01-29 07:49:00 +01001115static int match_placeholder_bool_arg(const char *to_parse, const char *candidate,
1116 const char **end, int *val)
1117{
1118 const char *argval;
1119 char *strval;
1120 size_t arglen;
1121 int v;
1122
1123 if (!match_placeholder_arg_value(to_parse, candidate, end, &argval, &arglen))
1124 return 0;
1125
1126 if (!argval) {
1127 *val = 1;
1128 return 1;
1129 }
1130
1131 strval = xstrndup(argval, arglen);
1132 v = git_parse_maybe_bool(strval);
1133 free(strval);
1134
1135 if (v == -1)
1136 return 0;
1137
1138 *val = v;
1139
1140 return 1;
1141}
1142
Anders Waldenborg250bea02019-01-28 22:33:34 +01001143static int format_trailer_match_cb(const struct strbuf *key, void *ud)
1144{
1145 const struct string_list *list = ud;
1146 const struct string_list_item *item;
1147
1148 for_each_string_list_item (item, list) {
1149 if (key->len == (uintptr_t)item->util &&
1150 !strncasecmp(item->string, key->buf, key->len))
1151 return 1;
1152 }
1153 return 0;
1154}
1155
Hariom Verma90563ae2021-02-13 01:52:41 +00001156int format_set_trailers_options(struct process_trailer_options *opts,
1157 struct string_list *filter_list,
1158 struct strbuf *sepbuf,
1159 struct strbuf *kvsepbuf,
Hariom Verma636a0ae2021-02-13 01:52:42 +00001160 const char **arg,
1161 char **invalid_arg)
Hariom Verma90563ae2021-02-13 01:52:41 +00001162{
1163 for (;;) {
1164 const char *argval;
1165 size_t arglen;
1166
Hariom Verma636a0ae2021-02-13 01:52:42 +00001167 if (**arg == ')')
1168 break;
1169
Hariom Verma90563ae2021-02-13 01:52:41 +00001170 if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) {
1171 uintptr_t len = arglen;
1172
1173 if (!argval)
1174 return -1;
1175
1176 if (len && argval[len - 1] == ':')
1177 len--;
1178 string_list_append(filter_list, argval)->util = (char *)len;
1179
1180 opts->filter = format_trailer_match_cb;
1181 opts->filter_data = filter_list;
1182 opts->only_trailers = 1;
1183 } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
1184 char *fmt;
1185
1186 strbuf_reset(sepbuf);
1187 fmt = xstrndup(argval, arglen);
1188 strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL);
1189 free(fmt);
1190 opts->separator = sepbuf;
1191 } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
1192 char *fmt;
1193
1194 strbuf_reset(kvsepbuf);
1195 fmt = xstrndup(argval, arglen);
1196 strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL);
1197 free(fmt);
1198 opts->key_value_separator = kvsepbuf;
1199 } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
1200 !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
1201 !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
Hariom Verma636a0ae2021-02-13 01:52:42 +00001202 !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) {
1203 if (invalid_arg) {
1204 size_t len = strcspn(*arg, ",)");
1205 *invalid_arg = xstrndup(*arg, len);
1206 }
1207 return -1;
1208 }
Hariom Verma90563ae2021-02-13 01:52:41 +00001209 }
1210 return 0;
1211}
1212
René Scharfeb0815472021-02-14 11:10:57 +01001213static size_t parse_describe_args(const char *start, struct strvec *args)
1214{
1215 const char *options[] = { "match", "exclude" };
1216 const char *arg = start;
1217
1218 for (;;) {
1219 const char *matched = NULL;
1220 const char *argval;
1221 size_t arglen = 0;
1222 int i;
1223
1224 for (i = 0; i < ARRAY_SIZE(options); i++) {
1225 if (match_placeholder_arg_value(arg, options[i], &arg,
1226 &argval, &arglen)) {
1227 matched = options[i];
1228 break;
1229 }
1230 }
1231 if (!matched)
1232 break;
1233
1234 if (!arglen)
1235 return 0;
1236 strvec_pushf(args, "--%s=%.*s", matched, (int)arglen, argval);
1237 }
1238 return arg - start;
1239}
1240
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001241static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1242 const char *placeholder,
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001243 void *context)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001244{
René Scharfef29d5952007-11-10 12:14:20 +01001245 struct format_commit_context *c = context;
1246 const struct commit *commit = c->commit;
Pat Notz177b29d2010-11-02 13:59:08 -06001247 const char *msg = c->message;
René Scharfef29d5952007-11-10 12:14:20 +01001248 struct commit_list *p;
Hariom Verma47d46762020-08-21 21:41:49 +00001249 const char *arg, *eol;
Anders Waldenborgfd2015b2019-01-28 22:33:36 +01001250 size_t res;
Issac Trottsad6f0282019-01-10 22:30:46 -08001251 char **slot;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001252
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001253 /* these are independent of the commit */
Anders Waldenborgfd2015b2019-01-28 22:33:36 +01001254 res = strbuf_expand_literal_cb(sb, placeholder, NULL);
1255 if (res)
1256 return res;
1257
René Scharfecde75e52007-11-09 01:49:42 +01001258 switch (placeholder[0]) {
1259 case 'C':
Christian Couder59556542013-11-30 21:55:40 +01001260 if (starts_with(placeholder + 1, "(auto)")) {
Edward Thomsonb15a3e02016-05-26 22:46:10 -05001261 c->auto_color = want_color(c->pretty_ctx->color);
René Scharfe82b83da2016-09-29 20:13:05 +02001262 if (c->auto_color && sb->len)
René Scharfec99ad272016-09-17 20:25:24 +02001263 strbuf_addstr(sb, GIT_COLOR_RESET);
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001264 return 7; /* consumed 7 bytes, "C(auto)" */
1265 } else {
1266 int ret = parse_color(sb, placeholder, c);
1267 if (ret)
1268 c->auto_color = 0;
1269 /*
1270 * Otherwise, we decided to treat %C<unknown>
1271 * as a literal string, and the previous
1272 * %C(auto) is still valid.
1273 */
1274 return ret;
Jeff Kingc0029222009-01-17 10:38:46 -05001275 }
René Scharfe02edd562009-10-17 23:04:19 +02001276 case 'w':
1277 if (placeholder[1] == '(') {
1278 unsigned long width = 0, indent1 = 0, indent2 = 0;
1279 char *next;
1280 const char *start = placeholder + 2;
1281 const char *end = strchr(start, ')');
1282 if (!end)
1283 return 0;
1284 if (end > start) {
1285 width = strtoul(start, &next, 10);
1286 if (*next == ',') {
1287 indent1 = strtoul(next + 1, &next, 10);
1288 if (*next == ',') {
1289 indent2 = strtoul(next + 1,
1290 &next, 10);
1291 }
1292 }
1293 if (*next != ')')
1294 return 0;
1295 }
1296 rewrap_message_tail(sb, c, width, indent1, indent2);
1297 return end - placeholder + 1;
1298 } else
1299 return 0;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001300
1301 case '<':
1302 case '>':
Jeff King9a1180f2019-03-20 04:16:42 -04001303 return parse_padding_placeholder(placeholder, c);
René Scharfecde75e52007-11-09 01:49:42 +01001304 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001305
René Scharfeb0815472021-02-14 11:10:57 +01001306 if (skip_prefix(placeholder, "(describe", &arg)) {
René Scharfe15ae82d2021-02-14 11:04:34 +01001307 struct child_process cmd = CHILD_PROCESS_INIT;
1308 struct strbuf out = STRBUF_INIT;
1309 struct strbuf err = STRBUF_INIT;
René Scharfe96099722021-02-28 12:22:47 +01001310 struct pretty_print_describe_status *describe_status;
1311
1312 describe_status = c->pretty_ctx->describe_status;
1313 if (describe_status) {
1314 if (!describe_status->max_invocations)
1315 return 0;
1316 describe_status->max_invocations--;
1317 }
René Scharfe15ae82d2021-02-14 11:04:34 +01001318
1319 cmd.git_cmd = 1;
1320 strvec_push(&cmd.args, "describe");
René Scharfeb0815472021-02-14 11:10:57 +01001321
1322 if (*arg == ':') {
1323 arg++;
1324 arg += parse_describe_args(arg, &cmd.args);
1325 }
1326
1327 if (*arg != ')') {
1328 child_process_clear(&cmd);
1329 return 0;
1330 }
1331
René Scharfe15ae82d2021-02-14 11:04:34 +01001332 strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1333 pipe_command(&cmd, NULL, 0, &out, 0, &err, 0);
1334 strbuf_rtrim(&out);
1335 strbuf_addbuf(sb, &out);
1336 strbuf_release(&out);
1337 strbuf_release(&err);
René Scharfeb0815472021-02-14 11:10:57 +01001338 return arg - placeholder + 1;
René Scharfe15ae82d2021-02-14 11:04:34 +01001339 }
1340
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001341 /* these depend on the commit */
1342 if (!commit->object.parsed)
Stefan Beller109cd762018-06-28 18:21:51 -07001343 parse_object(the_repository, &commit->object.oid);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001344
René Scharfecde75e52007-11-09 01:49:42 +01001345 switch (placeholder[0]) {
1346 case 'H': /* commit hash */
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001347 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001348 strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001349 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001350 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001351 case 'h': /* abbreviated commit hash */
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001352 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
brian m. carlson30e677e2018-03-12 02:27:28 +00001353 strbuf_add_unique_abbrev(sb, &commit->object.oid,
René Scharfe1eb47f12016-08-06 17:41:01 +02001354 c->pretty_ctx->abbrev);
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001355 strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001356 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001357 case 'T': /* tree hash */
Derrick Stolee2e27bd72018-04-06 19:09:38 +00001358 strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit)));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001359 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001360 case 't': /* abbreviated tree hash */
Derrick Stolee2e27bd72018-04-06 19:09:38 +00001361 strbuf_add_unique_abbrev(sb,
Junio C Hamanoc89b6e12018-05-23 14:38:13 +09001362 get_commit_tree_oid(commit),
René Scharfe1eb47f12016-08-06 17:41:01 +02001363 c->pretty_ctx->abbrev);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001364 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001365 case 'P': /* parent hashes */
1366 for (p = commit->parents; p; p = p->next) {
1367 if (p != commit->parents)
1368 strbuf_addch(sb, ' ');
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001369 strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
René Scharfecde75e52007-11-09 01:49:42 +01001370 }
Marco Costalbac3a670d2008-02-09 15:40:19 +01001371 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001372 case 'p': /* abbreviated parent hashes */
1373 for (p = commit->parents; p; p = p->next) {
1374 if (p != commit->parents)
1375 strbuf_addch(sb, ' ');
brian m. carlson30e677e2018-03-12 02:27:28 +00001376 strbuf_add_unique_abbrev(sb, &p->item->object.oid,
René Scharfe1eb47f12016-08-06 17:41:01 +02001377 c->pretty_ctx->abbrev);
René Scharfecde75e52007-11-09 01:49:42 +01001378 }
Marco Costalbac3a670d2008-02-09 15:40:19 +01001379 return 1;
René Scharfecde75e52007-11-09 01:49:42 +01001380 case 'm': /* left/right/bottom */
Michael J Gruber1df2d652011-03-07 13:31:39 +01001381 strbuf_addstr(sb, get_revision_mark(NULL, commit));
Marco Costalbac3a670d2008-02-09 15:40:19 +01001382 return 1;
René Scharfe3b3d4432008-09-04 23:40:03 +02001383 case 'd':
Nguyễn Thái Ngọc Duya95f0672013-04-19 09:08:49 +10001384 format_decorations(sb, commit, c->auto_color);
René Scharfe3b3d4432008-09-04 23:40:03 +02001385 return 1;
Harry Jeffery92710952014-09-18 21:53:53 +01001386 case 'D':
Harry Jeffery92710952014-09-18 21:53:53 +01001387 format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1388 return 1;
Issac Trottsad6f0282019-01-10 22:30:46 -08001389 case 'S': /* tag/branch like --source */
1390 if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
1391 return 0;
1392 slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);
1393 if (!(slot && *slot))
1394 return 0;
1395 strbuf_addstr(sb, *slot);
1396 return 1;
Thomas Rast8f8f5472009-10-19 17:48:10 +02001397 case 'g': /* reflog info */
1398 switch(placeholder[1]) {
1399 case 'd': /* reflog selector */
1400 case 'D':
1401 if (c->pretty_ctx->reflog_info)
1402 get_reflog_selector(sb,
1403 c->pretty_ctx->reflog_info,
Jeff Kinga5481a62015-06-25 12:55:02 -04001404 &c->pretty_ctx->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -07001405 c->pretty_ctx->date_mode_explicit,
Thomas Rast8f8f5472009-10-19 17:48:10 +02001406 (placeholder[1] == 'd'));
1407 return 2;
1408 case 's': /* reflog message */
1409 if (c->pretty_ctx->reflog_info)
1410 get_reflog_message(sb, c->pretty_ctx->reflog_info);
1411 return 2;
Jeff Kingcd1957f2011-12-16 06:40:24 -05001412 case 'n':
1413 case 'N':
1414 case 'e':
1415 case 'E':
1416 return format_reflog_person(sb,
1417 placeholder[1],
1418 c->pretty_ctx->reflog_info,
Jeff Kinga5481a62015-06-25 12:55:02 -04001419 &c->pretty_ctx->date_mode);
Thomas Rast8f8f5472009-10-19 17:48:10 +02001420 }
1421 return 0; /* unknown %g placeholder */
Johannes Schindelin8b208f02009-10-09 12:22:05 +02001422 case 'N':
Junio C Hamanoddf333f2012-10-17 18:51:47 -07001423 if (c->pretty_ctx->notes_message) {
1424 strbuf_addstr(sb, c->pretty_ctx->notes_message);
Johannes Gilger5b163602010-04-13 22:31:12 +02001425 return 1;
1426 }
1427 return 0;
René Scharfecde75e52007-11-09 01:49:42 +01001428 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001429
Junio C Hamanof6667c52011-10-21 21:06:02 -07001430 if (placeholder[0] == 'G') {
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001431 if (!c->signature_check.result)
1432 check_commit_signature(c->commit, &(c->signature_check));
Junio C Hamanof6667c52011-10-21 21:06:02 -07001433 switch (placeholder[1]) {
1434 case 'G':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001435 if (c->signature_check.gpg_output)
1436 strbuf_addstr(sb, c->signature_check.gpg_output);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001437 break;
1438 case '?':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001439 switch (c->signature_check.result) {
Junio C Hamanof6667c52011-10-21 21:06:02 -07001440 case 'G':
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001441 switch (c->signature_check.trust_level) {
1442 case TRUST_UNDEFINED:
1443 case TRUST_NEVER:
1444 strbuf_addch(sb, 'U');
1445 break;
1446 default:
1447 strbuf_addch(sb, 'G');
1448 break;
1449 }
1450 break;
Junio C Hamanof6667c52011-10-21 21:06:02 -07001451 case 'B':
Michael J Gruber661a1802016-10-12 15:04:15 +02001452 case 'E':
Sebastian Göttee290c4b2013-03-31 18:03:22 +02001453 case 'N':
Michael J Gruber661a1802016-10-12 15:04:15 +02001454 case 'X':
1455 case 'Y':
1456 case 'R':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001457 strbuf_addch(sb, c->signature_check.result);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001458 }
1459 break;
1460 case 'S':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001461 if (c->signature_check.signer)
1462 strbuf_addstr(sb, c->signature_check.signer);
Junio C Hamanof6667c52011-10-21 21:06:02 -07001463 break;
Michael J Gruber0174eea2013-02-14 17:04:46 +01001464 case 'K':
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001465 if (c->signature_check.key)
1466 strbuf_addstr(sb, c->signature_check.key);
Michael J Gruber0174eea2013-02-14 17:04:46 +01001467 break;
Michał Górny3daaaab2018-10-22 18:38:20 +02001468 case 'F':
1469 if (c->signature_check.fingerprint)
1470 strbuf_addstr(sb, c->signature_check.fingerprint);
1471 break;
Michał Górny4de93942018-10-22 18:38:21 +02001472 case 'P':
1473 if (c->signature_check.primary_key_fingerprint)
1474 strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
1475 break;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001476 case 'T':
1477 switch (c->signature_check.trust_level) {
1478 case TRUST_UNDEFINED:
1479 strbuf_addstr(sb, "undefined");
1480 break;
1481 case TRUST_NEVER:
1482 strbuf_addstr(sb, "never");
1483 break;
1484 case TRUST_MARGINAL:
1485 strbuf_addstr(sb, "marginal");
1486 break;
1487 case TRUST_FULLY:
1488 strbuf_addstr(sb, "fully");
1489 break;
1490 case TRUST_ULTIMATE:
1491 strbuf_addstr(sb, "ultimate");
1492 break;
1493 }
1494 break;
Jeff Kingaa4b78d2014-06-16 20:07:07 -04001495 default:
1496 return 0;
Junio C Hamanof6667c52011-10-21 21:06:02 -07001497 }
1498 return 2;
1499 }
1500
René Scharfecde75e52007-11-09 01:49:42 +01001501 /* For the rest we have to parse the commit header. */
Jeff King018b9de2021-01-28 14:57:39 -05001502 if (!c->commit_header_parsed) {
1503 msg = c->message =
1504 repo_logmsg_reencode(c->repository, commit,
1505 &c->commit_encoding, "UTF-8");
René Scharfef29d5952007-11-10 12:14:20 +01001506 parse_commit_header(c);
Jeff King018b9de2021-01-28 14:57:39 -05001507 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001508
René Scharfef29d5952007-11-10 12:14:20 +01001509 switch (placeholder[0]) {
Marco Costalbac3a670d2008-02-09 15:40:19 +01001510 case 'a': /* author ... */
1511 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -04001512 msg + c->author.off, c->author.len,
Jeff Kinga5481a62015-06-25 12:55:02 -04001513 &c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001514 case 'c': /* committer ... */
1515 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -04001516 msg + c->committer.off, c->committer.len,
Jeff Kinga5481a62015-06-25 12:55:02 -04001517 &c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001518 case 'e': /* encoding */
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001519 if (c->commit_encoding)
1520 strbuf_addstr(sb, c->commit_encoding);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001521 return 1;
Eli Barzilay1367b122010-03-24 22:51:52 -04001522 case 'B': /* raw body */
1523 /* message_off is always left at the initial newline */
1524 strbuf_addstr(sb, msg + c->message_off + 1);
1525 return 1;
René Scharfef53bd742008-12-27 01:49:21 +01001526 }
1527
1528 /* Now we need to parse the commit message. */
1529 if (!c->commit_message_parsed)
1530 parse_commit_message(c);
1531
1532 switch (placeholder[0]) {
1533 case 's': /* subject */
1534 format_subject(sb, msg + c->subject_off, " ");
1535 return 1;
Stephen Boyd46d164b2009-03-22 19:14:01 -07001536 case 'f': /* sanitized subject */
Hariom Verma47d46762020-08-21 21:41:49 +00001537 eol = strchrnul(msg + c->subject_off, '\n');
1538 format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
Stephen Boyd46d164b2009-03-22 19:14:01 -07001539 return 1;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001540 case 'b': /* body */
René Scharfef29d5952007-11-10 12:14:20 +01001541 strbuf_addstr(sb, msg + c->body_off);
Marco Costalbac3a670d2008-02-09 15:40:19 +01001542 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001543 }
Jacob Kellerd9f31fb2016-11-18 16:58:14 -08001544
Jeff King58311c62017-08-15 06:25:27 -04001545 if (skip_prefix(placeholder, "(trailers", &arg)) {
Jeff Kinga388b102017-08-15 06:23:56 -04001546 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
Anders Waldenborg250bea02019-01-28 22:33:34 +01001547 struct string_list filter_list = STRING_LIST_INIT_NODUP;
Anders Waldenborg0b691d82019-01-28 22:33:37 +01001548 struct strbuf sepbuf = STRBUF_INIT;
Ævar Arnfjörð Bjarmason058761f2020-12-09 16:52:08 +01001549 struct strbuf kvsepbuf = STRBUF_INIT;
Anders Waldenborg3e3f3472019-01-28 22:33:33 +01001550 size_t ret = 0;
Jeff Kinge5fba5d2018-08-22 20:50:17 -04001551
1552 opts.no_divider = 1;
1553
Taylor Blau84ff0532017-10-01 09:18:47 -07001554 if (*arg == ':') {
1555 arg++;
Hariom Verma636a0ae2021-02-13 01:52:42 +00001556 if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL))
Hariom Verma90563ae2021-02-13 01:52:41 +00001557 goto trailer_out;
Jeff King58311c62017-08-15 06:25:27 -04001558 }
1559 if (*arg == ')') {
1560 format_trailers_from_commit(sb, msg + c->subject_off, &opts);
Anders Waldenborg3e3f3472019-01-28 22:33:33 +01001561 ret = arg - placeholder + 1;
Jeff King58311c62017-08-15 06:25:27 -04001562 }
Anders Waldenborg250bea02019-01-28 22:33:34 +01001563 trailer_out:
1564 string_list_clear(&filter_list, 0);
Anders Waldenborg0b691d82019-01-28 22:33:37 +01001565 strbuf_release(&sepbuf);
Anders Waldenborg3e3f3472019-01-28 22:33:33 +01001566 return ret;
Jacob Kellerd9f31fb2016-11-18 16:58:14 -08001567 }
1568
Marco Costalbac3a670d2008-02-09 15:40:19 +01001569 return 0; /* unknown placeholder */
René Scharfecde75e52007-11-09 01:49:42 +01001570}
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001571
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001572static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1573 const char *placeholder,
1574 struct format_commit_context *c)
1575{
1576 struct strbuf local_sb = STRBUF_INIT;
1577 int total_consumed = 0, len, padding = c->padding;
1578 if (padding < 0) {
1579 const char *start = strrchr(sb->buf, '\n');
1580 int occupied;
1581 if (!start)
1582 start = sb->buf;
1583 occupied = utf8_strnwidth(start, -1, 1);
Josef Kufner3ad87c82016-06-16 20:18:37 +07001584 occupied += c->pretty_ctx->graph_width;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001585 padding = (-padding) - occupied;
1586 }
1587 while (1) {
1588 int modifier = *placeholder == 'C';
1589 int consumed = format_commit_one(&local_sb, placeholder, c);
1590 total_consumed += consumed;
1591
1592 if (!modifier)
1593 break;
1594
1595 placeholder += consumed;
1596 if (*placeholder != '%')
1597 break;
1598 placeholder++;
1599 total_consumed++;
1600 }
1601 len = utf8_strnwidth(local_sb.buf, -1, 1);
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10001602
1603 if (c->flush_type == flush_left_and_steal) {
1604 const char *ch = sb->buf + sb->len - 1;
1605 while (len > padding && ch > sb->buf) {
1606 const char *p;
1607 if (*ch == ' ') {
1608 ch--;
1609 padding++;
1610 continue;
1611 }
1612 /* check for trailing ansi sequences */
1613 if (*ch != 'm')
1614 break;
1615 p = ch - 1;
1616 while (ch - p < 10 && *p != '\033')
1617 p--;
1618 if (*p != '\033' ||
1619 ch + 1 - p != display_mode_esc_sequence_len(p))
1620 break;
1621 /*
1622 * got a good ansi sequence, put it back to
1623 * local_sb as we're cutting sb
1624 */
1625 strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1626 ch = p - 1;
1627 }
1628 strbuf_setlen(sb, ch + 1 - sb->buf);
1629 c->flush_type = flush_left;
1630 }
1631
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001632 if (len > padding) {
1633 switch (c->truncate) {
1634 case trunc_left:
1635 strbuf_utf8_replace(&local_sb,
1636 0, len - (padding - 2),
1637 "..");
1638 break;
1639 case trunc_middle:
1640 strbuf_utf8_replace(&local_sb,
1641 padding / 2 - 1,
1642 len - (padding - 2),
1643 "..");
1644 break;
1645 case trunc_right:
1646 strbuf_utf8_replace(&local_sb,
1647 padding - 2, len - (padding - 2),
1648 "..");
1649 break;
1650 case trunc_none:
1651 break;
1652 }
René Scharfee992d1e2014-07-10 10:52:21 +02001653 strbuf_addbuf(sb, &local_sb);
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +10001654 } else {
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001655 int sb_len = sb->len, offset = 0;
1656 if (c->flush_type == flush_left)
1657 offset = padding - len;
1658 else if (c->flush_type == flush_both)
1659 offset = (padding - len) / 2;
1660 /*
1661 * we calculate padding in columns, now
1662 * convert it back to chars
1663 */
1664 padding = padding - len + local_sb.len;
René Scharfe415792e2014-09-07 09:06:42 +02001665 strbuf_addchars(sb, ' ', padding);
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001666 memcpy(sb->buf + sb_len + offset, local_sb.buf,
1667 local_sb.len);
1668 }
1669 strbuf_release(&local_sb);
1670 c->flush_type = no_flush;
1671 return total_consumed;
1672}
1673
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001674static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1675 const char *placeholder,
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001676 void *context)
1677{
1678 int consumed;
1679 size_t orig_len;
1680 enum {
1681 NO_MAGIC,
1682 ADD_LF_BEFORE_NON_EMPTY,
1683 DEL_LF_BEFORE_EMPTY,
Junio C Hamano223a9232010-06-22 09:45:22 -07001684 ADD_SP_BEFORE_NON_EMPTY
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001685 } magic = NO_MAGIC;
1686
1687 switch (placeholder[0]) {
1688 case '-':
1689 magic = DEL_LF_BEFORE_EMPTY;
1690 break;
1691 case '+':
1692 magic = ADD_LF_BEFORE_NON_EMPTY;
1693 break;
Michael J Gruber7b881762010-06-14 18:12:29 +02001694 case ' ':
1695 magic = ADD_SP_BEFORE_NON_EMPTY;
1696 break;
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001697 default:
1698 break;
1699 }
1700 if (magic != NO_MAGIC)
1701 placeholder++;
1702
1703 orig_len = sb->len;
Nguyễn Thái Ngọc Duya5752342013-04-19 09:08:50 +10001704 if (((struct format_commit_context *)context)->flush_type != no_flush)
1705 consumed = format_and_pad_commit(sb, placeholder, context);
1706 else
1707 consumed = format_commit_one(sb, placeholder, context);
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001708 if (magic == NO_MAGIC)
1709 return consumed;
1710
1711 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1712 while (sb->len && sb->buf[sb->len - 1] == '\n')
1713 strbuf_setlen(sb, sb->len - 1);
Michael J Gruber7b881762010-06-14 18:12:29 +02001714 } else if (orig_len != sb->len) {
1715 if (magic == ADD_LF_BEFORE_NON_EMPTY)
René Scharfea91cc7f2020-02-09 14:44:23 +01001716 strbuf_insertstr(sb, orig_len, "\n");
Michael J Gruber7b881762010-06-14 18:12:29 +02001717 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
René Scharfea91cc7f2020-02-09 14:44:23 +01001718 strbuf_insertstr(sb, orig_len, " ");
Junio C Hamano9fa708d2009-10-04 23:43:32 -07001719 }
1720 return consumed + 1;
1721}
1722
Johannes Gilger5b163602010-04-13 22:31:12 +02001723static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1724 void *context)
1725{
1726 struct userformat_want *w = context;
1727
Michael J Gruber7b881762010-06-14 18:12:29 +02001728 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
Johannes Gilger5b163602010-04-13 22:31:12 +02001729 placeholder++;
1730
1731 switch (*placeholder) {
1732 case 'N':
1733 w->notes = 1;
1734 break;
Issac Trottsad6f0282019-01-10 22:30:46 -08001735 case 'S':
1736 w->source = 1;
1737 break;
Johannes Gilger5b163602010-04-13 22:31:12 +02001738 }
1739 return 0;
1740}
1741
1742void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1743{
1744 struct strbuf dummy = STRBUF_INIT;
1745
1746 if (!fmt) {
1747 if (!user_format)
1748 return;
1749 fmt = user_format;
1750 }
Junio C Hamanoa6253d12011-05-25 12:23:44 -07001751 strbuf_expand(&dummy, fmt, userformat_want_item, w);
Johannes Gilger5b163602010-04-13 22:31:12 +02001752 strbuf_release(&dummy);
1753}
1754
Stefan Bellerf54fbf52018-11-13 16:13:00 -08001755void repo_format_commit_message(struct repository *r,
1756 const struct commit *commit,
1757 const char *format, struct strbuf *sb,
1758 const struct pretty_print_context *pretty_ctx)
René Scharfecde75e52007-11-09 01:49:42 +01001759{
Denton Liu3e8ed3b2019-11-19 16:51:16 -08001760 struct format_commit_context context = {
Jeff King018b9de2021-01-28 14:57:39 -05001761 .repository = r,
Denton Liu3e8ed3b2019-11-19 16:51:16 -08001762 .commit = commit,
1763 .pretty_ctx = pretty_ctx,
1764 .wrap_start = sb->len
1765 };
Pat Notz177b29d2010-11-02 13:59:08 -06001766 const char *output_enc = pretty_ctx->output_encoding;
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001767 const char *utf8 = "UTF-8";
René Scharfef29d5952007-11-10 12:14:20 +01001768
Marco Costalbac3a670d2008-02-09 15:40:19 +01001769 strbuf_expand(sb, format, format_commit_item, &context);
René Scharfe02edd562009-10-17 23:04:19 +02001770 rewrap_message_tail(sb, &context, 0, 0, 0);
Pat Notz177b29d2010-11-02 13:59:08 -06001771
Jeff King018b9de2021-01-28 14:57:39 -05001772 /*
1773 * Convert output to an actual output encoding; note that
1774 * format_commit_item() will always use UTF-8, so we don't
1775 * have to bother if that's what the output wants.
1776 */
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001777 if (output_enc) {
1778 if (same_encoding(utf8, output_enc))
1779 output_enc = NULL;
1780 } else {
1781 if (context.commit_encoding &&
1782 !same_encoding(context.commit_encoding, utf8))
1783 output_enc = context.commit_encoding;
1784 }
1785
1786 if (output_enc) {
Jeff Kingc7d017d2018-07-24 06:50:33 -04001787 size_t outsz;
Nguyễn Thái Ngọc Duy7e77df32013-04-19 09:08:47 +10001788 char *out = reencode_string_len(sb->buf, sb->len,
1789 output_enc, utf8, &outsz);
1790 if (out)
1791 strbuf_attach(sb, out, outsz, outsz + 1);
1792 }
1793
Nguyễn Thái Ngọc Duy0940a762013-04-19 09:08:41 +10001794 free(context.commit_encoding);
Stefan Bellerf54fbf52018-11-13 16:13:00 -08001795 repo_unuse_commit_buffer(r, commit, context.message);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001796}
1797
Jeff King10f2fbf2013-07-03 03:07:48 -04001798static void pp_header(struct pretty_print_context *pp,
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001799 const char *encoding,
1800 const struct commit *commit,
1801 const char **msg_p,
1802 struct strbuf *sb)
1803{
1804 int parents_shown = 0;
1805
1806 for (;;) {
René Scharfee3f1da92014-10-04 20:54:50 +02001807 const char *name, *line = *msg_p;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001808 int linelen = get_one_line(*msg_p);
1809
1810 if (!linelen)
1811 return;
1812 *msg_p += linelen;
1813
1814 if (linelen == 1)
1815 /* End of header */
1816 return;
1817
Jeff King6bf13942011-05-26 18:27:49 -04001818 if (pp->fmt == CMIT_FMT_RAW) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001819 strbuf_add(sb, line, linelen);
1820 continue;
1821 }
1822
Christian Couder59556542013-11-30 21:55:40 +01001823 if (starts_with(line, "parent ")) {
brian m. carlson580f0982018-07-16 01:28:08 +00001824 if (linelen != the_hash_algo->hexsz + 8)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001825 die("bad parent line in commit");
1826 continue;
1827 }
1828
1829 if (!parents_shown) {
René Scharfe4bbaa1e2014-07-17 01:52:09 +02001830 unsigned num = commit_list_count(commit->parents);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001831 /* with enough slop */
brian m. carlson580f0982018-07-16 01:28:08 +00001832 strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20);
Jeff King6bf13942011-05-26 18:27:49 -04001833 add_merge_info(pp, sb, commit);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001834 parents_shown = 1;
1835 }
1836
1837 /*
1838 * MEDIUM == DEFAULT shows only author with dates.
1839 * FULL shows both authors but not dates.
1840 * FULLER shows both authors and dates.
1841 */
René Scharfee3f1da92014-10-04 20:54:50 +02001842 if (skip_prefix(line, "author ", &name)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001843 strbuf_grow(sb, linelen + 80);
René Scharfee3f1da92014-10-04 20:54:50 +02001844 pp_user_info(pp, "Author", sb, name, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001845 }
René Scharfee3f1da92014-10-04 20:54:50 +02001846 if (skip_prefix(line, "committer ", &name) &&
Jeff King6bf13942011-05-26 18:27:49 -04001847 (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001848 strbuf_grow(sb, linelen + 80);
René Scharfee3f1da92014-10-04 20:54:50 +02001849 pp_user_info(pp, "Commit", sb, name, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001850 }
1851 }
1852}
1853
Jeff King10f2fbf2013-07-03 03:07:48 -04001854void pp_title_line(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001855 const char **msg_p,
1856 struct strbuf *sb,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001857 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -07001858 int need_8bit_cte)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001859{
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +02001860 static const int max_length = 78; /* per rfc2047 */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001861 struct strbuf title;
1862
1863 strbuf_init(&title, 80);
Jeff King9553d2b2011-05-26 18:28:17 -04001864 *msg_p = format_subject(&title, *msg_p,
1865 pp->preserve_subject ? "\n" : " ");
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001866
1867 strbuf_grow(sb, title.len + 1024);
René Scharfe6d167fd2017-03-01 12:37:07 +01001868 if (pp->print_email_subject) {
1869 if (pp->rev)
1870 fmt_output_email_subject(sb, pp->rev);
Emma Brooks19d097e2020-04-08 04:31:38 +00001871 if (pp->encode_email_headers &&
1872 needs_rfc2047_encoding(title.buf, title.len))
Jan H. Schönherr41dd00b2012-10-18 16:43:33 +02001873 add_rfc2047(sb, title.buf, title.len,
1874 encoding, RFC2047_SUBJECT);
1875 else
1876 strbuf_add_wrapped_bytes(sb, title.buf, title.len,
1877 -last_line_length(sb), 1, max_length);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001878 } else {
1879 strbuf_addbuf(sb, &title);
1880 }
1881 strbuf_addch(sb, '\n');
1882
Jeff Kinga9080472013-07-03 03:08:22 -04001883 if (need_8bit_cte == 0) {
1884 int i;
1885 for (i = 0; i < pp->in_body_headers.nr; i++) {
1886 if (has_non_ascii(pp->in_body_headers.items[i].string)) {
1887 need_8bit_cte = 1;
1888 break;
1889 }
1890 }
1891 }
1892
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001893 if (need_8bit_cte > 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001894 const char *header_fmt =
1895 "MIME-Version: 1.0\n"
1896 "Content-Type: text/plain; charset=%s\n"
1897 "Content-Transfer-Encoding: 8bit\n";
1898 strbuf_addf(sb, header_fmt, encoding);
1899 }
Jeff King6bf13942011-05-26 18:27:49 -04001900 if (pp->after_subject) {
1901 strbuf_addstr(sb, pp->after_subject);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001902 }
Eric Wong9f23e042016-06-05 04:46:39 +00001903 if (cmit_fmt_is_mail(pp->fmt)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001904 strbuf_addch(sb, '\n');
1905 }
Jeff Kinga9080472013-07-03 03:08:22 -04001906
1907 if (pp->in_body_headers.nr) {
1908 int i;
1909 for (i = 0; i < pp->in_body_headers.nr; i++) {
1910 strbuf_addstr(sb, pp->in_body_headers.items[i].string);
1911 free(pp->in_body_headers.items[i].string);
1912 }
1913 string_list_clear(&pp->in_body_headers, 0);
1914 strbuf_addch(sb, '\n');
1915 }
1916
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001917 strbuf_release(&title);
1918}
1919
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001920static int pp_utf8_width(const char *start, const char *end)
1921{
1922 int width = 0;
1923 size_t remain = end - start;
1924
1925 while (remain) {
1926 int n = utf8_width(&start, &remain);
1927 if (n < 0 || !start)
1928 return -1;
1929 width += n;
1930 }
1931 return width;
1932}
1933
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001934static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001935 const char *line, int linelen)
1936{
1937 const char *tab;
1938
1939 while ((tab = memchr(line, '\t', linelen)) != NULL) {
1940 int width = pp_utf8_width(line, tab);
1941
1942 /*
1943 * If it wasn't well-formed utf8, or it
1944 * had characters with badly defined
1945 * width (control characters etc), just
1946 * give up on trying to align things.
1947 */
1948 if (width < 0)
1949 break;
1950
1951 /* Output the data .. */
1952 strbuf_add(sb, line, tab - line);
1953
1954 /* .. and the de-tabified tab */
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001955 strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001956
1957 /* Skip over the printed part .. */
1958 linelen -= tab + 1 - line;
1959 line = tab + 1;
1960 }
1961
1962 /*
1963 * Print out everything after the last tab without
1964 * worrying about width - there's nothing more to
1965 * align.
1966 */
1967 strbuf_add(sb, line, linelen);
1968}
1969
1970/*
1971 * pp_handle_indent() prints out the intendation, and
1972 * the whole line (without the final newline), after
1973 * de-tabifying.
1974 */
1975static void pp_handle_indent(struct pretty_print_context *pp,
1976 struct strbuf *sb, int indent,
1977 const char *line, int linelen)
1978{
1979 strbuf_addchars(sb, ' ', indent);
1980 if (pp->expand_tabs_in_log)
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07001981 strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
Linus Torvalds7cc13c72016-03-16 09:15:53 -07001982 else
1983 strbuf_add(sb, line, linelen);
1984}
1985
Eric Wong9f23e042016-06-05 04:46:39 +00001986static int is_mboxrd_from(const char *line, int len)
1987{
1988 /*
1989 * a line matching /^From $/ here would only have len == 4
1990 * at this point because is_empty_line would've trimmed all
1991 * trailing space
1992 */
1993 return len > 4 && starts_with(line + strspn(line, ">"), "From ");
1994}
1995
Jeff King10f2fbf2013-07-03 03:07:48 -04001996void pp_remainder(struct pretty_print_context *pp,
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001997 const char **msg_p,
1998 struct strbuf *sb,
1999 int indent)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002000{
2001 int first = 1;
2002 for (;;) {
2003 const char *line = *msg_p;
2004 int linelen = get_one_line(line);
2005 *msg_p += linelen;
2006
2007 if (!linelen)
2008 break;
2009
Johannes Schindelin77356122016-06-22 22:20:16 +02002010 if (is_blank_line(line, &linelen)) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002011 if (first)
2012 continue;
Jeff King6bf13942011-05-26 18:27:49 -04002013 if (pp->fmt == CMIT_FMT_SHORT)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002014 break;
2015 }
2016 first = 0;
2017
2018 strbuf_grow(sb, linelen + indent + 20);
René Scharfe415792e2014-09-07 09:06:42 +02002019 if (indent)
Linus Torvalds7cc13c72016-03-16 09:15:53 -07002020 pp_handle_indent(pp, sb, indent, line, linelen);
Junio C Hamano0893eec2016-03-29 15:49:24 -07002021 else if (pp->expand_tabs_in_log)
Junio C Hamanofe37a9c2016-03-29 16:05:39 -07002022 strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
2023 line, linelen);
Eric Wong9f23e042016-06-05 04:46:39 +00002024 else {
2025 if (pp->fmt == CMIT_FMT_MBOXRD &&
2026 is_mboxrd_from(line, linelen))
2027 strbuf_addch(sb, '>');
2028
Linus Torvalds7cc13c72016-03-16 09:15:53 -07002029 strbuf_add(sb, line, linelen);
Eric Wong9f23e042016-06-05 04:46:39 +00002030 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002031 strbuf_addch(sb, '\n');
2032 }
2033}
2034
Jeff King10f2fbf2013-07-03 03:07:48 -04002035void pretty_print_commit(struct pretty_print_context *pp,
Jeff King6bf13942011-05-26 18:27:49 -04002036 const struct commit *commit,
2037 struct strbuf *sb)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002038{
2039 unsigned long beginning_of_body;
2040 int indent = 4;
Jeff Kingdd0d3882013-01-26 04:44:06 -05002041 const char *msg;
Jeff Kingb000c592014-06-10 17:39:30 -04002042 const char *reencoded;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002043 const char *encoding;
Jeff King6bf13942011-05-26 18:27:49 -04002044 int need_8bit_cte = pp->need_8bit_cte;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002045
Jeff King6bf13942011-05-26 18:27:49 -04002046 if (pp->fmt == CMIT_FMT_USERFORMAT) {
2047 format_commit_message(commit, user_format, sb, pp);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002048 return;
2049 }
2050
Junio C Hamanoe297cf52012-10-17 17:12:55 -07002051 encoding = get_log_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +10002052 msg = reencoded = logmsg_reencode(commit, NULL, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002053
Eric Wong9f23e042016-06-05 04:46:39 +00002054 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002055 indent = 0;
2056
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07002057 /*
2058 * We need to check and emit Content-type: to mark it
2059 * as 8-bit if we haven't done so.
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002060 */
Eric Wong9f23e042016-06-05 04:46:39 +00002061 if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002062 int i, ch, in_body;
2063
2064 for (in_body = i = 0; (ch = msg[i]); i++) {
2065 if (!in_body) {
2066 /* author could be non 7-bit ASCII but
2067 * the log may be so; skip over the
2068 * header part first.
2069 */
2070 if (ch == '\n' && msg[i+1] == '\n')
2071 in_body = 1;
2072 }
2073 else if (non_ascii(ch)) {
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07002074 need_8bit_cte = 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002075 break;
2076 }
2077 }
2078 }
2079
Jeff King6bf13942011-05-26 18:27:49 -04002080 pp_header(pp, encoding, commit, &msg, sb);
René Scharfe6d167fd2017-03-01 12:37:07 +01002081 if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002082 strbuf_addch(sb, '\n');
2083 }
2084
2085 /* Skip excess blank lines at the beginning of body, if any... */
Johannes Schindelin77356122016-06-22 22:20:16 +02002086 msg = skip_blank_lines(msg);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002087
2088 /* These formats treat the title line specially. */
Eric Wong9f23e042016-06-05 04:46:39 +00002089 if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
Jeff King6bf13942011-05-26 18:27:49 -04002090 pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002091
2092 beginning_of_body = sb->len;
Jeff King6bf13942011-05-26 18:27:49 -04002093 if (pp->fmt != CMIT_FMT_ONELINE)
2094 pp_remainder(pp, &msg, sb, indent);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002095 strbuf_rtrim(sb);
2096
2097 /* Make sure there is an EOLN for the non-oneline case */
Jeff King6bf13942011-05-26 18:27:49 -04002098 if (pp->fmt != CMIT_FMT_ONELINE)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002099 strbuf_addch(sb, '\n');
2100
2101 /*
2102 * The caller may append additional body text in e-mail
2103 * format. Make sure we did not strip the blank line
2104 * between the header and the body.
2105 */
Eric Wong9f23e042016-06-05 04:46:39 +00002106 if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002107 strbuf_addch(sb, '\n');
Johannes Schindelina97a7462009-10-09 12:21:57 +02002108
Jeff Kingb66103c2014-06-10 17:41:39 -04002109 unuse_commit_buffer(commit, reencoded);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00002110}
Jeff King8b8a5372011-05-26 18:27:24 -04002111
2112void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
2113 struct strbuf *sb)
2114{
2115 struct pretty_print_context pp = {0};
Jeff King6bf13942011-05-26 18:27:49 -04002116 pp.fmt = fmt;
2117 pretty_print_commit(&pp, commit, sb);
Jeff King8b8a5372011-05-26 18:27:24 -04002118}