Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 3 | #include "commit.h" |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 4 | #include "utf8.h" |
| 5 | #include "diff.h" |
| 6 | #include "revision.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 7 | #include "string-list.h" |
Johannes Schindelin | e0cbc39 | 2008-07-12 00:28:18 +0100 | [diff] [blame] | 8 | #include "mailmap.h" |
René Scharfe | 3b3d443 | 2008-09-04 23:40:03 +0200 | [diff] [blame] | 9 | #include "log-tree.h" |
Johannes Schindelin | a97a746 | 2009-10-09 12:21:57 +0200 | [diff] [blame] | 10 | #include "notes.h" |
Jeff King | c002922 | 2009-01-17 10:38:46 -0500 | [diff] [blame] | 11 | #include "color.h" |
Thomas Rast | 8f8f547 | 2009-10-19 17:48:10 +0200 | [diff] [blame] | 12 | #include "reflog-walk.h" |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 13 | #include "gpg-interface.h" |
Jacob Keller | d9f31fb | 2016-11-18 16:58:14 -0800 | [diff] [blame] | 14 | #include "trailer.h" |
René Scharfe | 15ae82d | 2021-02-14 11:04:34 +0100 | [diff] [blame] | 15 | #include "run-command.h" |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 16 | |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 17 | static char *user_format; |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 18 | static struct cmt_fmt_map { |
| 19 | const char *name; |
| 20 | enum cmit_fmt format; |
| 21 | int is_tformat; |
Junio C Hamano | 0893eec | 2016-03-29 15:49:24 -0700 | [diff] [blame] | 22 | int expand_tabs_in_log; |
Will Palmer | 2d7671e | 2010-05-02 12:00:43 +0100 | [diff] [blame] | 23 | int is_alias; |
Denton Liu | 618a855 | 2019-11-19 16:51:23 -0800 | [diff] [blame] | 24 | enum date_mode_type default_date_mode_type; |
Will Palmer | 2d7671e | 2010-05-02 12:00:43 +0100 | [diff] [blame] | 25 | const char *user_format; |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 26 | } *commit_formats; |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 27 | static size_t builtin_formats_len; |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 28 | static size_t commit_formats_len; |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 29 | static size_t commit_formats_alloc; |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 30 | static struct cmt_fmt_map *find_commit_format(const char *sought); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 31 | |
Jeff King | b9c7d6e | 2014-07-29 13:56:48 -0400 | [diff] [blame] | 32 | int commit_format_is_empty(enum cmit_fmt fmt) |
| 33 | { |
| 34 | return fmt == CMIT_FMT_USERFORMAT && !*user_format; |
| 35 | } |
| 36 | |
Nanako Shiraishi | 3640754 | 2009-02-24 18:59:15 +0900 | [diff] [blame] | 37 | static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat) |
| 38 | { |
| 39 | free(user_format); |
| 40 | user_format = xstrdup(cp); |
| 41 | if (is_tformat) |
| 42 | rev->use_terminator = 1; |
| 43 | rev->commit_format = CMIT_FMT_USERFORMAT; |
| 44 | } |
| 45 | |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 46 | static 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 King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 53 | if (!skip_prefix(var, "pretty.", &name)) |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 54 | return 0; |
| 55 | |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 56 | for (i = 0; i < builtin_formats_len; i++) { |
| 57 | if (!strcmp(commit_formats[i].name, name)) |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | for (i = builtin_formats_len; i < commit_formats_len; i++) { |
| 62 | if (!strcmp(commit_formats[i].name, name)) { |
| 63 | commit_format = &commit_formats[i]; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (!commit_format) { |
| 69 | ALLOC_GROW(commit_formats, commit_formats_len+1, |
| 70 | commit_formats_alloc); |
| 71 | commit_format = &commit_formats[commit_formats_len]; |
Jonathan Nieder | 95a2618 | 2010-05-08 16:07:39 -0500 | [diff] [blame] | 72 | memset(commit_format, 0, sizeof(*commit_format)); |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 73 | commit_formats_len++; |
| 74 | } |
| 75 | |
| 76 | commit_format->name = xstrdup(name); |
| 77 | commit_format->format = CMIT_FMT_USERFORMAT; |
Tanay Abhra | a26bc61 | 2014-08-04 07:41:15 -0700 | [diff] [blame] | 78 | if (git_config_string(&fmt, var, value)) |
| 79 | return -1; |
| 80 | |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 81 | if (skip_prefix(fmt, "format:", &fmt)) |
| 82 | commit_format->is_tformat = 0; |
| 83 | else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%')) |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 84 | commit_format->is_tformat = 1; |
| 85 | else |
| 86 | commit_format->is_alias = 1; |
| 87 | commit_format->user_format = fmt; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 92 | static void setup_commit_formats(void) |
| 93 | { |
| 94 | struct cmt_fmt_map builtin_formats[] = { |
Junio C Hamano | 0893eec | 2016-03-29 15:49:24 -0700 | [diff] [blame] | 95 | { "raw", CMIT_FMT_RAW, 0, 0 }, |
Junio C Hamano | fe37a9c | 2016-03-29 16:05:39 -0700 | [diff] [blame] | 96 | { "medium", CMIT_FMT_MEDIUM, 0, 8 }, |
Junio C Hamano | 0893eec | 2016-03-29 15:49:24 -0700 | [diff] [blame] | 97 | { "short", CMIT_FMT_SHORT, 0, 0 }, |
| 98 | { "email", CMIT_FMT_EMAIL, 0, 0 }, |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 99 | { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 }, |
Junio C Hamano | fe37a9c | 2016-03-29 16:05:39 -0700 | [diff] [blame] | 100 | { "fuller", CMIT_FMT_FULLER, 0, 8 }, |
| 101 | { "full", CMIT_FMT_FULL, 0, 8 }, |
Denton Liu | 1f0fc1d | 2019-11-19 16:51:25 -0800 | [diff] [blame] | 102 | { "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 Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 105 | /* |
| 106 | * Please update $__git_log_pretty_formats in |
| 107 | * git-completion.bash when you add new formats. |
| 108 | */ |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 109 | }; |
| 110 | commit_formats_len = ARRAY_SIZE(builtin_formats); |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 111 | builtin_formats_len = commit_formats_len; |
| 112 | ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc); |
René Scharfe | 921d49b | 2019-06-15 20:36:35 +0200 | [diff] [blame] | 113 | COPY_ARRAY(commit_formats, builtin_formats, |
| 114 | ARRAY_SIZE(builtin_formats)); |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 115 | |
| 116 | git_config(git_pretty_formats_config, NULL); |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Will Palmer | 2d7671e | 2010-05-02 12:00:43 +0100 | [diff] [blame] | 119 | static struct cmt_fmt_map *find_commit_format_recursive(const char *sought, |
| 120 | const char *original, |
| 121 | int num_redirections) |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 122 | { |
| 123 | struct cmt_fmt_map *found = NULL; |
| 124 | size_t found_match_len = 0; |
| 125 | int i; |
| 126 | |
Will Palmer | 2d7671e | 2010-05-02 12:00:43 +0100 | [diff] [blame] | 127 | if (num_redirections >= commit_formats_len) |
| 128 | die("invalid --pretty format: " |
| 129 | "'%s' references an alias which points to itself", |
| 130 | original); |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 131 | |
| 132 | for (i = 0; i < commit_formats_len; i++) { |
| 133 | size_t match_len; |
| 134 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 135 | if (!starts_with(commit_formats[i].name, sought)) |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 136 | continue; |
| 137 | |
| 138 | match_len = strlen(commit_formats[i].name); |
| 139 | if (found == NULL || found_match_len > match_len) { |
| 140 | found = &commit_formats[i]; |
| 141 | found_match_len = match_len; |
| 142 | } |
| 143 | } |
Will Palmer | 2d7671e | 2010-05-02 12:00:43 +0100 | [diff] [blame] | 144 | |
| 145 | if (found && found->is_alias) { |
| 146 | found = find_commit_format_recursive(found->user_format, |
| 147 | original, |
| 148 | num_redirections+1); |
| 149 | } |
| 150 | |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 151 | return found; |
| 152 | } |
| 153 | |
Will Palmer | 2d7671e | 2010-05-02 12:00:43 +0100 | [diff] [blame] | 154 | static struct cmt_fmt_map *find_commit_format(const char *sought) |
| 155 | { |
| 156 | if (!commit_formats) |
| 157 | setup_commit_formats(); |
| 158 | |
| 159 | return find_commit_format_recursive(sought, sought, 0); |
| 160 | } |
| 161 | |
Junio C Hamano | 4da45be | 2008-04-07 17:11:34 -0700 | [diff] [blame] | 162 | void get_commit_format(const char *arg, struct rev_info *rev) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 163 | { |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 164 | struct cmt_fmt_map *commit_format; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 165 | |
Junio C Hamano | 4da45be | 2008-04-07 17:11:34 -0700 | [diff] [blame] | 166 | rev->use_terminator = 0; |
Jeff King | c75e7ad | 2014-07-29 13:54:46 -0400 | [diff] [blame] | 167 | if (!arg) { |
Junio C Hamano | 4da45be | 2008-04-07 17:11:34 -0700 | [diff] [blame] | 168 | rev->commit_format = CMIT_FMT_DEFAULT; |
| 169 | return; |
| 170 | } |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 171 | if (skip_prefix(arg, "format:", &arg)) { |
| 172 | save_user_format(rev, arg, 0); |
Junio C Hamano | 4da45be | 2008-04-07 17:11:34 -0700 | [diff] [blame] | 173 | return; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 174 | } |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 175 | |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 176 | if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) { |
Nanako Shiraishi | 3640754 | 2009-02-24 18:59:15 +0900 | [diff] [blame] | 177 | save_user_format(rev, arg, 1); |
| 178 | return; |
| 179 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 180 | |
Will Palmer | 4095789 | 2010-05-02 12:00:42 +0100 | [diff] [blame] | 181 | commit_format = find_commit_format(arg); |
| 182 | if (!commit_format) |
| 183 | die("invalid --pretty format: %s", arg); |
| 184 | |
| 185 | rev->commit_format = commit_format->format; |
| 186 | rev->use_terminator = commit_format->is_tformat; |
Junio C Hamano | 0893eec | 2016-03-29 15:49:24 -0700 | [diff] [blame] | 187 | rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log; |
Denton Liu | 618a855 | 2019-11-19 16:51:23 -0800 | [diff] [blame] | 188 | if (!rev->date_mode_explicit && commit_format->default_date_mode_type) |
| 189 | rev->date_mode.type = commit_format->default_date_mode_type; |
Will Palmer | 8028184 | 2010-05-02 12:00:44 +0100 | [diff] [blame] | 190 | if (commit_format->format == CMIT_FMT_USERFORMAT) { |
| 191 | save_user_format(rev, commit_format->user_format, |
| 192 | commit_format->is_tformat); |
| 193 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Generic support for pretty-printing the header |
| 198 | */ |
| 199 | static int get_one_line(const char *msg) |
| 200 | { |
| 201 | int ret = 0; |
| 202 | |
| 203 | for (;;) { |
| 204 | char c = *msg++; |
| 205 | if (!c) |
| 206 | break; |
| 207 | ret++; |
| 208 | if (c == '\n') |
| 209 | break; |
| 210 | } |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /* High bit set, or ISO-2022-INT */ |
Junio C Hamano | cc57114 | 2010-01-11 22:23:35 -0800 | [diff] [blame] | 215 | static int non_ascii(int ch) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 216 | { |
René Scharfe | c2e9364 | 2009-03-07 14:06:49 +0100 | [diff] [blame] | 217 | return !isascii(ch) || ch == '\033'; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Johannes Schindelin | 28e9cf6 | 2009-08-10 18:22:18 +0200 | [diff] [blame] | 220 | int has_non_ascii(const char *s) |
| 221 | { |
| 222 | int ch; |
| 223 | if (!s) |
| 224 | return 0; |
| 225 | while ((ch = *s++) != '\0') { |
| 226 | if (non_ascii(ch)) |
| 227 | return 1; |
| 228 | } |
| 229 | return 0; |
| 230 | } |
| 231 | |
Jeff King | 4d03c18 | 2011-04-08 18:40:36 -0400 | [diff] [blame] | 232 | static int is_rfc822_special(char ch) |
| 233 | { |
| 234 | switch (ch) { |
| 235 | case '(': |
| 236 | case ')': |
| 237 | case '<': |
| 238 | case '>': |
| 239 | case '[': |
| 240 | case ']': |
| 241 | case ':': |
| 242 | case ';': |
| 243 | case '@': |
| 244 | case ',': |
| 245 | case '.': |
| 246 | case '"': |
| 247 | case '\\': |
| 248 | return 1; |
| 249 | default: |
| 250 | return 0; |
| 251 | } |
| 252 | } |
| 253 | |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 254 | static int needs_rfc822_quoting(const char *s, int len) |
Jeff King | 4d03c18 | 2011-04-08 18:40:36 -0400 | [diff] [blame] | 255 | { |
| 256 | int i; |
| 257 | for (i = 0; i < len; i++) |
| 258 | if (is_rfc822_special(s[i])) |
| 259 | return 1; |
| 260 | return 0; |
| 261 | } |
| 262 | |
Jan H. Schönherr | f9b7204 | 2012-10-18 16:43:31 +0200 | [diff] [blame] | 263 | static int last_line_length(struct strbuf *sb) |
| 264 | { |
| 265 | int i; |
| 266 | |
| 267 | /* How many bytes are already used on the last line? */ |
| 268 | for (i = sb->len - 1; i >= 0; i--) |
| 269 | if (sb->buf[i] == '\n') |
| 270 | break; |
| 271 | return sb->len - (i + 1); |
| 272 | } |
| 273 | |
Jeff King | 4d03c18 | 2011-04-08 18:40:36 -0400 | [diff] [blame] | 274 | static void add_rfc822_quoted(struct strbuf *out, const char *s, int len) |
| 275 | { |
| 276 | int i; |
| 277 | |
| 278 | /* just a guess, we may have to also backslash-quote */ |
| 279 | strbuf_grow(out, len + 2); |
| 280 | |
| 281 | strbuf_addch(out, '"'); |
| 282 | for (i = 0; i < len; i++) { |
| 283 | switch (s[i]) { |
| 284 | case '"': |
| 285 | case '\\': |
| 286 | strbuf_addch(out, '\\'); |
| 287 | /* fall through */ |
| 288 | default: |
| 289 | strbuf_addch(out, s[i]); |
| 290 | } |
| 291 | } |
| 292 | strbuf_addch(out, '"'); |
| 293 | } |
| 294 | |
Jan H. Schönherr | 0fcec2c | 2012-10-18 16:43:32 +0200 | [diff] [blame] | 295 | enum rfc2047_type { |
| 296 | RFC2047_SUBJECT, |
Ronnie Sahlberg | 7827352 | 2014-07-02 11:24:05 -0700 | [diff] [blame] | 297 | RFC2047_ADDRESS |
Jan H. Schönherr | 0fcec2c | 2012-10-18 16:43:32 +0200 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | static int is_rfc2047_special(char ch, enum rfc2047_type type) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 301 | { |
Jan H. Schönherr | 0fcec2c | 2012-10-18 16:43:32 +0200 | [diff] [blame] | 302 | /* |
| 303 | * rfc2047, section 4.2: |
| 304 | * |
| 305 | * 8-bit values which correspond to printable ASCII characters other |
| 306 | * than "=", "?", and "_" (underscore), MAY be represented as those |
| 307 | * characters. (But see section 5 for restrictions.) In |
| 308 | * particular, SPACE and TAB MUST NOT be represented as themselves |
| 309 | * within encoded words. |
| 310 | */ |
| 311 | |
| 312 | /* |
| 313 | * rule out non-ASCII characters and non-printable characters (the |
| 314 | * non-ASCII check should be redundant as isprint() is not localized |
| 315 | * and only knows about ASCII, but be defensive about that) |
| 316 | */ |
| 317 | if (non_ascii(ch) || !isprint(ch)) |
Jan H. Schönherr | 94f6cdf | 2012-10-18 16:43:30 +0200 | [diff] [blame] | 318 | return 1; |
| 319 | |
Jan H. Schönherr | 0fcec2c | 2012-10-18 16:43:32 +0200 | [diff] [blame] | 320 | /* |
| 321 | * rule out special printable characters (' ' should be the only |
| 322 | * whitespace character considered printable, but be defensive and use |
| 323 | * isspace()) |
| 324 | */ |
| 325 | if (isspace(ch) || ch == '=' || ch == '?' || ch == '_') |
| 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 Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Jeff King | da55ff3 | 2019-03-20 04:16:36 -0400 | [diff] [blame] | 352 | static int needs_rfc2047_encoding(const char *line, int len) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 353 | { |
Jeff King | a1f6baa | 2011-02-23 04:58:41 -0500 | [diff] [blame] | 354 | int i; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 355 | |
| 356 | for (i = 0; i < len; i++) { |
| 357 | int ch = line[i]; |
Jeff King | c22e7de | 2011-02-23 04:59:18 -0500 | [diff] [blame] | 358 | if (non_ascii(ch) || ch == '\n') |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 359 | return 1; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 360 | if ((i + 1 < len) && (ch == '=' && line[i+1] == '?')) |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 361 | return 1; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 362 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 363 | |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Kirill Smelkov | 6cd3c05 | 2013-03-07 14:55:07 +0400 | [diff] [blame] | 367 | static void add_rfc2047(struct strbuf *sb, const char *line, size_t len, |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 368 | const char *encoding, enum rfc2047_type type) |
| 369 | { |
| 370 | static const int max_encoded_length = 76; /* per rfc2047 */ |
| 371 | int i; |
| 372 | int line_len = last_line_length(sb); |
| 373 | |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 374 | strbuf_grow(sb, len * 3 + strlen(encoding) + 100); |
| 375 | strbuf_addf(sb, "=?%s?q?", encoding); |
Jeff King | a1f6baa | 2011-02-23 04:58:41 -0500 | [diff] [blame] | 376 | line_len += strlen(encoding) + 5; /* 5 for =??q? */ |
Kirill Smelkov | 6cd3c05 | 2013-03-07 14:55:07 +0400 | [diff] [blame] | 377 | |
| 378 | while (len) { |
| 379 | /* |
| 380 | * RFC 2047, section 5 (3): |
| 381 | * |
| 382 | * Each 'encoded-word' MUST represent an integral number of |
| 383 | * characters. A multi-octet character may not be split across |
| 384 | * adjacent 'encoded- word's. |
| 385 | */ |
| 386 | const unsigned char *p = (const unsigned char *)line; |
| 387 | int chrlen = mbs_chrlen(&line, &len, encoding); |
| 388 | int is_special = (chrlen > 1) || is_rfc2047_special(*p, type); |
| 389 | |
| 390 | /* "=%02X" * chrlen, or the byte itself */ |
| 391 | const char *encoded_fmt = is_special ? "=%02X" : "%c"; |
| 392 | int encoded_len = is_special ? 3 * chrlen : 1; |
Jeff King | a1f6baa | 2011-02-23 04:58:41 -0500 | [diff] [blame] | 393 | |
Jan H. Schönherr | 94f6cdf | 2012-10-18 16:43:30 +0200 | [diff] [blame] | 394 | /* |
| 395 | * According to RFC 2047, we could encode the special character |
| 396 | * ' ' (space) with '_' (underscore) for readability. But many |
| 397 | * programs do not understand this and just leave the |
| 398 | * underscore in place. Thus, we do nothing special here, which |
| 399 | * causes ' ' to be encoded as '=20', avoiding this problem. |
| 400 | */ |
| 401 | |
Kirill Smelkov | 6cd3c05 | 2013-03-07 14:55:07 +0400 | [diff] [blame] | 402 | if (line_len + encoded_len + 2 > max_encoded_length) { |
| 403 | /* It won't fit with trailing "?=" --- break the line */ |
Jeff King | a1f6baa | 2011-02-23 04:58:41 -0500 | [diff] [blame] | 404 | strbuf_addf(sb, "?=\n =?%s?q?", encoding); |
| 405 | line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */ |
| 406 | } |
| 407 | |
Kirill Smelkov | 6cd3c05 | 2013-03-07 14:55:07 +0400 | [diff] [blame] | 408 | for (i = 0; i < chrlen; i++) |
| 409 | strbuf_addf(sb, encoded_fmt, p[i]); |
| 410 | line_len += encoded_len; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 411 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 412 | strbuf_addstr(sb, "?="); |
| 413 | } |
| 414 | |
Jeff King | d105324 | 2014-05-01 21:07:22 -0400 | [diff] [blame] | 415 | const char *show_ident_date(const struct ident_split *ident, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 416 | const struct date_mode *mode) |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 417 | { |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 418 | timestamp_t date = 0; |
Jeff King | 3f419d4 | 2014-03-07 12:15:01 -0500 | [diff] [blame] | 419 | long tz = 0; |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 420 | |
| 421 | if (ident->date_begin && ident->date_end) |
Johannes Schindelin | 1aeb7e7 | 2017-04-21 12:45:44 +0200 | [diff] [blame] | 422 | date = parse_timestamp(ident->date_begin, NULL, 10); |
Jeff King | 1dca155 | 2014-02-24 02:46:37 -0500 | [diff] [blame] | 423 | if (date_overflows(date)) |
| 424 | date = 0; |
| 425 | else { |
| 426 | if (ident->tz_begin && ident->tz_end) |
| 427 | tz = strtol(ident->tz_begin, NULL, 10); |
Jeff King | 3f419d4 | 2014-03-07 12:15:01 -0500 | [diff] [blame] | 428 | if (tz >= INT_MAX || tz <= INT_MIN) |
Jeff King | 1dca155 | 2014-02-24 02:46:37 -0500 | [diff] [blame] | 429 | tz = 0; |
| 430 | } |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 431 | return show_date(date, tz, mode); |
| 432 | } |
| 433 | |
Jeff King | 10f2fbf | 2013-07-03 03:07:48 -0400 | [diff] [blame] | 434 | void pp_user_info(struct pretty_print_context *pp, |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 435 | const char *what, struct strbuf *sb, |
| 436 | const char *line, const char *encoding) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 437 | { |
Antoine Pelisse | 3c020bd | 2013-01-05 22:26:38 +0100 | [diff] [blame] | 438 | struct ident_split ident; |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 439 | char *line_end; |
Antoine Pelisse | dffd325 | 2013-01-05 22:26:42 +0100 | [diff] [blame] | 440 | const char *mailbuf, *namebuf; |
| 441 | size_t namelen, maillen; |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 442 | int max_length = 78; /* per rfc2822 */ |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 443 | |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 444 | if (pp->fmt == CMIT_FMT_ONELINE) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 445 | return; |
Antoine Pelisse | 3c020bd | 2013-01-05 22:26:38 +0100 | [diff] [blame] | 446 | |
René Scharfe | 30e77bc | 2013-04-25 21:40:25 +0200 | [diff] [blame] | 447 | line_end = strchrnul(line, '\n'); |
| 448 | if (split_ident_line(&ident, line, line_end - line)) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 449 | return; |
Antoine Pelisse | 3c020bd | 2013-01-05 22:26:38 +0100 | [diff] [blame] | 450 | |
Antoine Pelisse | dffd325 | 2013-01-05 22:26:42 +0100 | [diff] [blame] | 451 | mailbuf = ident.mail_begin; |
| 452 | maillen = ident.mail_end - ident.mail_begin; |
| 453 | namebuf = ident.name_begin; |
| 454 | namelen = ident.name_end - ident.name_begin; |
| 455 | |
| 456 | if (pp->mailmap) |
| 457 | map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen); |
| 458 | |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 459 | if (cmit_fmt_is_mail(pp->fmt)) { |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 460 | if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) { |
Jeff King | a908047 | 2013-07-03 03:08:22 -0400 | [diff] [blame] | 461 | struct strbuf buf = STRBUF_INIT; |
| 462 | |
| 463 | strbuf_addstr(&buf, "From: "); |
| 464 | strbuf_add(&buf, namebuf, namelen); |
| 465 | strbuf_addstr(&buf, " <"); |
| 466 | strbuf_add(&buf, mailbuf, maillen); |
| 467 | strbuf_addstr(&buf, ">\n"); |
| 468 | string_list_append(&pp->in_body_headers, |
| 469 | strbuf_detach(&buf, NULL)); |
| 470 | |
| 471 | mailbuf = pp->from_ident->mail_begin; |
| 472 | maillen = pp->from_ident->mail_end - mailbuf; |
| 473 | namebuf = pp->from_ident->name_begin; |
| 474 | namelen = pp->from_ident->name_end - namebuf; |
| 475 | } |
| 476 | |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 477 | strbuf_addstr(sb, "From: "); |
Emma Brooks | 19d097e | 2020-04-08 04:31:38 +0000 | [diff] [blame] | 478 | if (pp->encode_email_headers && |
| 479 | needs_rfc2047_encoding(namebuf, namelen)) { |
René Scharfe | a0511b3 | 2013-04-25 21:43:56 +0200 | [diff] [blame] | 480 | add_rfc2047(sb, namebuf, namelen, |
Antoine Pelisse | dffd325 | 2013-01-05 22:26:42 +0100 | [diff] [blame] | 481 | encoding, RFC2047_ADDRESS); |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 482 | max_length = 76; /* per rfc2047 */ |
René Scharfe | a0511b3 | 2013-04-25 21:43:56 +0200 | [diff] [blame] | 483 | } else if (needs_rfc822_quoting(namebuf, namelen)) { |
Jeff King | 4d03c18 | 2011-04-08 18:40:36 -0400 | [diff] [blame] | 484 | struct strbuf quoted = STRBUF_INIT; |
René Scharfe | a0511b3 | 2013-04-25 21:43:56 +0200 | [diff] [blame] | 485 | add_rfc822_quoted("ed, namebuf, namelen); |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 486 | strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len, |
| 487 | -6, 1, max_length); |
Jeff King | 4d03c18 | 2011-04-08 18:40:36 -0400 | [diff] [blame] | 488 | strbuf_release("ed); |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 489 | } else { |
René Scharfe | a0511b3 | 2013-04-25 21:43:56 +0200 | [diff] [blame] | 490 | strbuf_add_wrapped_bytes(sb, namebuf, namelen, |
Antoine Pelisse | dffd325 | 2013-01-05 22:26:42 +0100 | [diff] [blame] | 491 | -6, 1, max_length); |
Jeff King | 4d03c18 | 2011-04-08 18:40:36 -0400 | [diff] [blame] | 492 | } |
Antoine Pelisse | dffd325 | 2013-01-05 22:26:42 +0100 | [diff] [blame] | 493 | |
René Scharfe | 97a17e7 | 2013-04-25 21:41:57 +0200 | [diff] [blame] | 494 | if (max_length < |
| 495 | last_line_length(sb) + strlen(" <") + maillen + strlen(">")) |
Jeff King | 990f6e3 | 2011-04-14 18:18:09 -0400 | [diff] [blame] | 496 | strbuf_addch(sb, '\n'); |
René Scharfe | a0511b3 | 2013-04-25 21:43:56 +0200 | [diff] [blame] | 497 | strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 498 | } else { |
René Scharfe | a0511b3 | 2013-04-25 21:43:56 +0200 | [diff] [blame] | 499 | strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what, |
| 500 | (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ", |
| 501 | (int)namelen, namebuf, (int)maillen, mailbuf); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 502 | } |
Antoine Pelisse | dffd325 | 2013-01-05 22:26:42 +0100 | [diff] [blame] | 503 | |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 504 | switch (pp->fmt) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 505 | case CMIT_FMT_MEDIUM: |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 506 | strbuf_addf(sb, "Date: %s\n", |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 507 | show_ident_date(&ident, &pp->date_mode)); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 508 | break; |
| 509 | case CMIT_FMT_EMAIL: |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 510 | case CMIT_FMT_MBOXRD: |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 511 | strbuf_addf(sb, "Date: %s\n", |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 512 | show_ident_date(&ident, DATE_MODE(RFC2822))); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 513 | break; |
| 514 | case CMIT_FMT_FULLER: |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 515 | strbuf_addf(sb, "%sDate: %s\n", what, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 516 | show_ident_date(&ident, &pp->date_mode)); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 517 | break; |
| 518 | default: |
| 519 | /* notin' */ |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 524 | static int is_blank_line(const char *line, int *len_p) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 525 | { |
| 526 | int len = *len_p; |
Felipe Contreras | 35b2fa5 | 2013-10-31 03:25:42 -0600 | [diff] [blame] | 527 | while (len && isspace(line[len - 1])) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 528 | len--; |
| 529 | *len_p = len; |
| 530 | return !len; |
| 531 | } |
| 532 | |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 533 | const char *skip_blank_lines(const char *msg) |
René Scharfe | a010966 | 2008-12-27 01:32:49 +0100 | [diff] [blame] | 534 | { |
| 535 | for (;;) { |
| 536 | int linelen = get_one_line(msg); |
| 537 | int ll = linelen; |
| 538 | if (!linelen) |
| 539 | break; |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 540 | if (!is_blank_line(msg, &ll)) |
René Scharfe | a010966 | 2008-12-27 01:32:49 +0100 | [diff] [blame] | 541 | break; |
| 542 | msg += linelen; |
| 543 | } |
| 544 | return msg; |
| 545 | } |
| 546 | |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 547 | static void add_merge_info(const struct pretty_print_context *pp, |
| 548 | struct strbuf *sb, const struct commit *commit) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 549 | { |
| 550 | struct commit_list *parent = commit->parents; |
| 551 | |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 552 | if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) || |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 553 | !parent || !parent->next) |
| 554 | return; |
| 555 | |
| 556 | strbuf_addstr(sb, "Merge:"); |
| 557 | |
| 558 | while (parent) { |
René Scharfe | a94bb68 | 2016-10-08 17:38:47 +0200 | [diff] [blame] | 559 | struct object_id *oidp = &parent->item->object.oid; |
| 560 | strbuf_addch(sb, ' '); |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 561 | if (pp->abbrev) |
brian m. carlson | 30e677e | 2018-03-12 02:27:28 +0000 | [diff] [blame] | 562 | strbuf_add_unique_abbrev(sb, oidp, pp->abbrev); |
René Scharfe | a94bb68 | 2016-10-08 17:38:47 +0200 | [diff] [blame] | 563 | else |
| 564 | strbuf_addstr(sb, oid_to_hex(oidp)); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 565 | parent = parent->next; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 566 | } |
| 567 | strbuf_addch(sb, '\n'); |
| 568 | } |
| 569 | |
Jeff King | fe6eb7f | 2014-08-27 03:56:01 -0400 | [diff] [blame] | 570 | static char *get_header(const char *msg, const char *key) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 571 | { |
Jeff King | fe6eb7f | 2014-08-27 03:56:01 -0400 | [diff] [blame] | 572 | size_t len; |
| 573 | const char *v = find_commit_header(msg, key, &len); |
| 574 | return v ? xmemdupz(v, len) : NULL; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static char *replace_encoding_header(char *buf, const char *encoding) |
| 578 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 579 | struct strbuf tmp = STRBUF_INIT; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 580 | size_t start, len; |
| 581 | char *cp = buf; |
| 582 | |
| 583 | /* guess if there is an encoding header before a \n\n */ |
René Scharfe | 68d6d6e | 2015-02-21 20:53:09 +0100 | [diff] [blame] | 584 | while (!starts_with(cp, "encoding ")) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 585 | cp = strchr(cp, '\n'); |
| 586 | if (!cp || *++cp == '\n') |
| 587 | return buf; |
| 588 | } |
| 589 | start = cp - buf; |
| 590 | cp = strchr(cp, '\n'); |
| 591 | if (!cp) |
| 592 | return buf; /* should not happen but be defensive */ |
| 593 | len = cp + 1 - (buf + start); |
| 594 | |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 595 | strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); |
| 596 | if (is_encoding_utf8(encoding)) { |
| 597 | /* we have re-coded to UTF-8; drop the header */ |
| 598 | strbuf_remove(&tmp, start, len); |
| 599 | } else { |
| 600 | /* just replaces XXXX in 'encoding XXXX\n' */ |
| 601 | strbuf_splice(&tmp, start + strlen("encoding "), |
| 602 | len - strlen("encoding \n"), |
| 603 | encoding, strlen(encoding)); |
| 604 | } |
| 605 | return strbuf_detach(&tmp, NULL); |
| 606 | } |
| 607 | |
Stefan Beller | 424510e | 2018-11-13 16:12:59 -0800 | [diff] [blame] | 608 | const char *repo_logmsg_reencode(struct repository *r, |
| 609 | const struct commit *commit, |
| 610 | char **commit_encoding, |
| 611 | const char *output_encoding) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 612 | { |
Brandon Casey | 330db18 | 2009-05-18 18:44:39 -0500 | [diff] [blame] | 613 | static const char *utf8 = "UTF-8"; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 614 | const char *use_encoding; |
| 615 | char *encoding; |
Stefan Beller | 424510e | 2018-11-13 16:12:59 -0800 | [diff] [blame] | 616 | const char *msg = repo_get_commit_buffer(r, commit, NULL); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 617 | char *out; |
| 618 | |
Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-19 09:08:40 +1000 | [diff] [blame] | 619 | if (!output_encoding || !*output_encoding) { |
| 620 | if (commit_encoding) |
Jeff King | fe6eb7f | 2014-08-27 03:56:01 -0400 | [diff] [blame] | 621 | *commit_encoding = get_header(msg, "encoding"); |
Jeff King | dd0d388 | 2013-01-26 04:44:06 -0500 | [diff] [blame] | 622 | return msg; |
Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-19 09:08:40 +1000 | [diff] [blame] | 623 | } |
Jeff King | fe6eb7f | 2014-08-27 03:56:01 -0400 | [diff] [blame] | 624 | encoding = get_header(msg, "encoding"); |
Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-19 09:08:40 +1000 | [diff] [blame] | 625 | if (commit_encoding) |
| 626 | *commit_encoding = encoding; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 627 | use_encoding = encoding ? encoding : utf8; |
Jeff King | be5c9fb | 2013-01-26 04:44:28 -0500 | [diff] [blame] | 628 | if (same_encoding(use_encoding, output_encoding)) { |
| 629 | /* |
| 630 | * No encoding work to be done. If we have no encoding header |
| 631 | * at all, then there's nothing to do, and we can return the |
| 632 | * message verbatim (whether newly allocated or not). |
| 633 | */ |
| 634 | if (!encoding) |
| 635 | return msg; |
| 636 | |
| 637 | /* |
| 638 | * Otherwise, we still want to munge the encoding header in the |
| 639 | * result, which will be done by modifying the buffer. If we |
| 640 | * are using a fresh copy, we can reuse it. But if we are using |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 641 | * the cached copy from get_commit_buffer, we need to duplicate it |
| 642 | * to avoid munging the cached copy. |
Jeff King | be5c9fb | 2013-01-26 04:44:28 -0500 | [diff] [blame] | 643 | */ |
Stefan Beller | 424510e | 2018-11-13 16:12:59 -0800 | [diff] [blame] | 644 | if (msg == get_cached_commit_buffer(r, commit, NULL)) |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 645 | out = xstrdup(msg); |
| 646 | else |
| 647 | out = (char *)msg; |
Jeff King | be5c9fb | 2013-01-26 04:44:28 -0500 | [diff] [blame] | 648 | } |
| 649 | else { |
| 650 | /* |
| 651 | * There's actual encoding work to do. Do the reencoding, which |
| 652 | * still leaves the header to be replaced in the next step. At |
| 653 | * this point, we are done with msg. If we allocated a fresh |
| 654 | * copy, we can free it. |
| 655 | */ |
| 656 | out = reencode_string(msg, output_encoding, use_encoding); |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 657 | if (out) |
Stefan Beller | 424510e | 2018-11-13 16:12:59 -0800 | [diff] [blame] | 658 | repo_unuse_commit_buffer(r, commit, msg); |
Jeff King | be5c9fb | 2013-01-26 04:44:28 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * This replacement actually consumes the buffer we hand it, so we do |
| 663 | * not have to worry about freeing the old "out" here. |
| 664 | */ |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 665 | if (out) |
| 666 | out = replace_encoding_header(out, output_encoding); |
| 667 | |
Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-19 09:08:40 +1000 | [diff] [blame] | 668 | if (!commit_encoding) |
| 669 | free(encoding); |
Jeff King | dd0d388 | 2013-01-26 04:44:06 -0500 | [diff] [blame] | 670 | /* |
| 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 Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 677 | static int mailmap_name(const char **email, size_t *email_len, |
| 678 | const char **name, size_t *name_len) |
Johannes Schindelin | e0cbc39 | 2008-07-12 00:28:18 +0100 | [diff] [blame] | 679 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 680 | static struct string_list *mail_map; |
Johannes Schindelin | e0cbc39 | 2008-07-12 00:28:18 +0100 | [diff] [blame] | 681 | if (!mail_map) { |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 682 | CALLOC_ARRAY(mail_map, 1); |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 683 | read_mailmap(mail_map); |
Johannes Schindelin | e0cbc39 | 2008-07-12 00:28:18 +0100 | [diff] [blame] | 684 | } |
Marius Storm-Olsen | d20d654 | 2009-02-08 15:34:30 +0100 | [diff] [blame] | 685 | return mail_map->nr && map_user(mail_map, email, email_len, name, name_len); |
Johannes Schindelin | e0cbc39 | 2008-07-12 00:28:18 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 688 | static size_t format_person_part(struct strbuf *sb, char part, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 689 | const char *msg, int len, |
| 690 | const struct date_mode *dmode) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 691 | { |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 692 | /* currently all placeholders have same length */ |
| 693 | const int placeholder_len = 2; |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 694 | struct ident_split s; |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 695 | const char *name, *mail; |
| 696 | size_t maillen, namelen; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 697 | |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 698 | if (split_ident_line(&s, msg, len) < 0) |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 699 | goto skip; |
| 700 | |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 701 | 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-Olsen | d20d654 | 2009-02-08 15:34:30 +0100 | [diff] [blame] | 705 | |
Prarit Bhargava | d8b8217 | 2019-10-29 08:09:14 -0400 | [diff] [blame] | 706 | if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */ |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 707 | mailmap_name(&mail, &maillen, &name, &namelen); |
Johannes Schindelin | e0cbc39 | 2008-07-12 00:28:18 +0100 | [diff] [blame] | 708 | if (part == 'n' || part == 'N') { /* name */ |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 709 | strbuf_add(sb, name, namelen); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 710 | return placeholder_len; |
| 711 | } |
Marius Storm-Olsen | d20d654 | 2009-02-08 15:34:30 +0100 | [diff] [blame] | 712 | if (part == 'e' || part == 'E') { /* email */ |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 713 | strbuf_add(sb, mail, maillen); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 714 | return placeholder_len; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 715 | } |
Prarit Bhargava | d8b8217 | 2019-10-29 08:09:14 -0400 | [diff] [blame] | 716 | 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 Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 723 | |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 724 | if (!s.date_begin) |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 725 | goto skip; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 726 | |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 727 | if (part == 't') { /* date, UNIX timestamp */ |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 728 | strbuf_add(sb, s.date_begin, s.date_end - s.date_begin); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 729 | return placeholder_len; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 730 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 731 | |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 732 | switch (part) { |
| 733 | case 'd': /* date */ |
René Scharfe | 9dbe7c3 | 2013-04-17 20:33:35 +0200 | [diff] [blame] | 734 | strbuf_addstr(sb, show_ident_date(&s, dmode)); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 735 | return placeholder_len; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 736 | case 'D': /* date, RFC2822 style */ |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 737 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822))); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 738 | return placeholder_len; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 739 | case 'r': /* date, relative */ |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 740 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE))); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 741 | return placeholder_len; |
Beat Bolli | 466fb67 | 2014-08-29 18:58:42 +0200 | [diff] [blame] | 742 | case 'i': /* date, ISO 8601-like */ |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 743 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601))); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 744 | return placeholder_len; |
Beat Bolli | 466fb67 | 2014-08-29 18:58:42 +0200 | [diff] [blame] | 745 | case 'I': /* date, ISO 8601 strict */ |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 746 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT))); |
Beat Bolli | 466fb67 | 2014-08-29 18:58:42 +0200 | [diff] [blame] | 747 | return placeholder_len; |
ZheNing Hu | b722d45 | 2021-04-25 10:41:45 +0000 | [diff] [blame] | 748 | case 'h': /* date, human */ |
| 749 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(HUMAN))); |
| 750 | return placeholder_len; |
René Scharfe | 0df6211 | 2019-11-19 16:51:21 -0800 | [diff] [blame] | 751 | case 's': |
| 752 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT))); |
| 753 | return placeholder_len; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 754 | } |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 755 | |
| 756 | skip: |
| 757 | /* |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 758 | * 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 Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 761 | */ |
| 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 Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 767 | } |
| 768 | |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 769 | struct chunk { |
| 770 | size_t off; |
| 771 | size_t len; |
| 772 | }; |
| 773 | |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 774 | enum flush_type { |
| 775 | no_flush, |
| 776 | flush_right, |
| 777 | flush_left, |
Nguyễn Thái Ngọc Duy | 1640632 | 2013-04-19 09:08:52 +1000 | [diff] [blame] | 778 | flush_left_and_steal, |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 779 | flush_both |
| 780 | }; |
| 781 | |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 782 | enum trunc_type { |
| 783 | trunc_none, |
| 784 | trunc_left, |
| 785 | trunc_middle, |
| 786 | trunc_right |
| 787 | }; |
| 788 | |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 789 | struct format_commit_context { |
Jeff King | 018b9de | 2021-01-28 14:57:39 -0500 | [diff] [blame] | 790 | struct repository *repository; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 791 | const struct commit *commit; |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 792 | const struct pretty_print_context *pretty_ctx; |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 793 | unsigned commit_header_parsed:1; |
| 794 | unsigned commit_message_parsed:1; |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 795 | struct signature_check signature_check; |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 796 | enum flush_type flush_type; |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 797 | enum trunc_type truncate; |
Jeff King | b000c59 | 2014-06-10 17:39:30 -0400 | [diff] [blame] | 798 | const char *message; |
Nguyễn Thái Ngọc Duy | 0940a76 | 2013-04-19 09:08:41 +1000 | [diff] [blame] | 799 | char *commit_encoding; |
René Scharfe | 02edd56 | 2009-10-17 23:04:19 +0200 | [diff] [blame] | 800 | size_t width, indent1, indent2; |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 801 | int auto_color; |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 802 | int padding; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 803 | |
| 804 | /* These offsets are relative to the start of the commit message. */ |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 805 | struct chunk author; |
| 806 | struct chunk committer; |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 807 | size_t message_off; |
| 808 | size_t subject_off; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 809 | size_t body_off; |
René Scharfe | b9c6232 | 2007-11-10 12:18:26 +0100 | [diff] [blame] | 810 | |
| 811 | /* The following ones are relative to the result struct strbuf. */ |
René Scharfe | 02edd56 | 2009-10-17 23:04:19 +0200 | [diff] [blame] | 812 | size_t wrap_start; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 813 | }; |
| 814 | |
| 815 | static void parse_commit_header(struct format_commit_context *context) |
| 816 | { |
Pat Notz | 177b29d | 2010-11-02 13:59:08 -0600 | [diff] [blame] | 817 | const char *msg = context->message; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 818 | int i; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 819 | |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 820 | for (i = 0; msg[i]; i++) { |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 821 | const char *name; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 822 | int eol; |
| 823 | for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) |
| 824 | ; /* do nothing */ |
| 825 | |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 826 | if (i == eol) { |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 827 | break; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 828 | } 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é Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 834 | } |
| 835 | i = eol; |
| 836 | } |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 837 | context->message_off = i; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 838 | context->commit_header_parsed = 1; |
| 839 | } |
| 840 | |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 841 | static 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 Verma | 47d4676 | 2020-08-21 21:41:49 +0000 | [diff] [blame] | 847 | void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len) |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 848 | { |
| 849 | size_t trimlen; |
Stephen Boyd | 871d21d | 2009-03-31 16:24:38 -0700 | [diff] [blame] | 850 | size_t start_len = sb->len; |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 851 | int space = 2; |
Hariom Verma | 47d4676 | 2020-08-21 21:41:49 +0000 | [diff] [blame] | 852 | int i; |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 853 | |
Hariom Verma | 47d4676 | 2020-08-21 21:41:49 +0000 | [diff] [blame] | 854 | for (i = 0; i < len; i++) { |
| 855 | if (istitlechar(msg[i])) { |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 856 | if (space == 1) |
| 857 | strbuf_addch(sb, '-'); |
| 858 | space = 0; |
Hariom Verma | 47d4676 | 2020-08-21 21:41:49 +0000 | [diff] [blame] | 859 | strbuf_addch(sb, msg[i]); |
| 860 | if (msg[i] == '.') |
| 861 | while (msg[i+1] == '.') |
| 862 | i++; |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 863 | } else |
| 864 | space |= 1; |
| 865 | } |
| 866 | |
| 867 | /* trim any trailing '.' or '-' characters */ |
| 868 | trimlen = 0; |
Stephen Boyd | 871d21d | 2009-03-31 16:24:38 -0700 | [diff] [blame] | 869 | while (sb->len - trimlen > start_len && |
| 870 | (sb->buf[sb->len - 1 - trimlen] == '.' |
| 871 | || sb->buf[sb->len - 1 - trimlen] == '-')) |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 872 | trimlen++; |
| 873 | strbuf_remove(sb, sb->len - trimlen, trimlen); |
| 874 | } |
| 875 | |
René Scharfe | cec0871 | 2009-01-06 21:41:06 +0100 | [diff] [blame] | 876 | const char *format_subject(struct strbuf *sb, const char *msg, |
| 877 | const char *line_separator) |
René Scharfe | 88c4473 | 2008-12-27 01:39:35 +0100 | [diff] [blame] | 878 | { |
| 879 | int first = 1; |
| 880 | |
| 881 | for (;;) { |
| 882 | const char *line = msg; |
| 883 | int linelen = get_one_line(line); |
| 884 | |
| 885 | msg += linelen; |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 886 | if (!linelen || is_blank_line(line, &linelen)) |
René Scharfe | 88c4473 | 2008-12-27 01:39:35 +0100 | [diff] [blame] | 887 | break; |
| 888 | |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 889 | if (!sb) |
| 890 | continue; |
René Scharfe | 88c4473 | 2008-12-27 01:39:35 +0100 | [diff] [blame] | 891 | 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é Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 900 | static void parse_commit_message(struct format_commit_context *c) |
| 901 | { |
Pat Notz | 177b29d | 2010-11-02 13:59:08 -0600 | [diff] [blame] | 902 | const char *msg = c->message + c->message_off; |
| 903 | const char *start = c->message; |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 904 | |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 905 | msg = skip_blank_lines(msg); |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 906 | c->subject_off = msg - start; |
| 907 | |
| 908 | msg = format_subject(NULL, msg, NULL); |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 909 | msg = skip_blank_lines(msg); |
René Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 910 | c->body_off = msg - start; |
| 911 | |
| 912 | c->commit_message_parsed = 1; |
| 913 | } |
| 914 | |
René Scharfe | 02edd56 | 2009-10-17 23:04:19 +0200 | [diff] [blame] | 915 | static 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 | |
| 928 | static 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é Scharfe | 32ca424 | 2009-11-08 02:04:21 +0100 | [diff] [blame] | 936 | if (c->wrap_start < sb->len) |
René Scharfe | 02edd56 | 2009-10-17 23:04:19 +0200 | [diff] [blame] | 937 | 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 King | cd1957f | 2011-12-16 06:40:24 -0500 | [diff] [blame] | 944 | static int format_reflog_person(struct strbuf *sb, |
| 945 | char part, |
| 946 | struct reflog_walk_info *log, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 947 | const struct date_mode *dmode) |
Jeff King | cd1957f | 2011-12-16 06:40:24 -0500 | [diff] [blame] | 948 | { |
| 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 Duy | fcabc2d | 2013-04-19 09:08:48 +1000 | [diff] [blame] | 961 | static size_t parse_color(struct strbuf *sb, /* in UTF-8 */ |
| 962 | const char *placeholder, |
| 963 | struct format_commit_context *c) |
| 964 | { |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 965 | const char *rest = placeholder; |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 966 | const char *basic_color = NULL; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 967 | |
Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-19 09:08:48 +1000 | [diff] [blame] | 968 | 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 King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 975 | |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 976 | if (skip_prefix(begin, "auto,", &begin)) { |
Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-19 09:08:48 +1000 | [diff] [blame] | 977 | if (!want_color(c->pretty_ctx->color)) |
| 978 | return end - placeholder + 1; |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 979 | } 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 Duy | fcabc2d | 2013-04-19 09:08:48 +1000 | [diff] [blame] | 985 | } |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 986 | |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 987 | if (color_parse_mem(begin, end - begin, color) < 0) |
| 988 | die(_("unable to parse --pretty format")); |
Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-19 09:08:48 +1000 | [diff] [blame] | 989 | strbuf_addstr(sb, color); |
| 990 | return end - placeholder + 1; |
| 991 | } |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 992 | |
| 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é Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 998 | if (skip_prefix(placeholder + 1, "red", &rest)) |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 999 | basic_color = GIT_COLOR_RED; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1000 | else if (skip_prefix(placeholder + 1, "green", &rest)) |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 1001 | basic_color = GIT_COLOR_GREEN; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1002 | else if (skip_prefix(placeholder + 1, "blue", &rest)) |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 1003 | basic_color = GIT_COLOR_BLUE; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1004 | else if (skip_prefix(placeholder + 1, "reset", &rest)) |
Jeff King | 18fb7ff | 2017-07-13 11:08:46 -0400 | [diff] [blame] | 1005 | basic_color = GIT_COLOR_RESET; |
| 1006 | |
| 1007 | if (basic_color && want_color(c->pretty_ctx->color)) |
| 1008 | strbuf_addstr(sb, basic_color); |
| 1009 | |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1010 | return rest - placeholder; |
Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-19 09:08:48 +1000 | [diff] [blame] | 1011 | } |
| 1012 | |
Jeff King | 9a1180f | 2019-03-20 04:16:42 -0400 | [diff] [blame] | 1013 | static size_t parse_padding_placeholder(const char *placeholder, |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1014 | 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 Duy | 1640632 | 2013-04-19 09:08:52 +1000 | [diff] [blame] | 1028 | } else if (*ch == '>') { |
| 1029 | flush_type = flush_left_and_steal; |
| 1030 | ch++; |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1031 | } 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 Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1046 | const char *end = start + strcspn(start, ",)"); |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1047 | char *next; |
| 1048 | int width; |
| 1049 | if (!end || end == start) |
| 1050 | return 0; |
Nguyễn Thái Ngọc Duy | 066790d | 2016-06-16 20:18:38 +0700 | [diff] [blame] | 1051 | width = strtol(start, &next, 10); |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1052 | if (next == start || width == 0) |
| 1053 | return 0; |
Nguyễn Thái Ngọc Duy | 066790d | 2016-06-16 20:18:38 +0700 | [diff] [blame] | 1054 | 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 Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1060 | c->padding = to_column ? -width : width; |
| 1061 | c->flush_type = flush_type; |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1062 | |
| 1063 | if (*end == ',') { |
| 1064 | start = end + 1; |
| 1065 | end = strchr(start, ')'); |
| 1066 | if (!end || end == start) |
| 1067 | return 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1068 | if (starts_with(start, "trunc)")) |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1069 | c->truncate = trunc_right; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1070 | else if (starts_with(start, "ltrunc)")) |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1071 | c->truncate = trunc_left; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1072 | else if (starts_with(start, "mtrunc)")) |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1073 | c->truncate = trunc_middle; |
| 1074 | else |
| 1075 | return 0; |
| 1076 | } else |
| 1077 | c->truncate = trunc_none; |
| 1078 | |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1079 | return end - placeholder + 1; |
| 1080 | } |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
Anders Waldenborg | 4f732e0 | 2019-01-29 07:49:00 +0100 | [diff] [blame] | 1084 | static int match_placeholder_arg_value(const char *to_parse, const char *candidate, |
| 1085 | const char **end, const char **valuestart, |
| 1086 | size_t *valuelen) |
Taylor Blau | 84ff053 | 2017-10-01 09:18:47 -0700 | [diff] [blame] | 1087 | { |
| 1088 | const char *p; |
| 1089 | |
| 1090 | if (!(skip_prefix(to_parse, candidate, &p))) |
| 1091 | return 0; |
Anders Waldenborg | 4f732e0 | 2019-01-29 07:49:00 +0100 | [diff] [blame] | 1092 | 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 Blau | 84ff053 | 2017-10-01 09:18:47 -0700 | [diff] [blame] | 1104 | 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 Waldenborg | 4f732e0 | 2019-01-29 07:49:00 +0100 | [diff] [blame] | 1115 | static 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 Waldenborg | 250bea0 | 2019-01-28 22:33:34 +0100 | [diff] [blame] | 1143 | static 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 Verma | 90563ae | 2021-02-13 01:52:41 +0000 | [diff] [blame] | 1156 | int format_set_trailers_options(struct process_trailer_options *opts, |
| 1157 | struct string_list *filter_list, |
| 1158 | struct strbuf *sepbuf, |
| 1159 | struct strbuf *kvsepbuf, |
Hariom Verma | 636a0ae | 2021-02-13 01:52:42 +0000 | [diff] [blame] | 1160 | const char **arg, |
| 1161 | char **invalid_arg) |
Hariom Verma | 90563ae | 2021-02-13 01:52:41 +0000 | [diff] [blame] | 1162 | { |
| 1163 | for (;;) { |
| 1164 | const char *argval; |
| 1165 | size_t arglen; |
| 1166 | |
Hariom Verma | 636a0ae | 2021-02-13 01:52:42 +0000 | [diff] [blame] | 1167 | if (**arg == ')') |
| 1168 | break; |
| 1169 | |
Hariom Verma | 90563ae | 2021-02-13 01:52:41 +0000 | [diff] [blame] | 1170 | 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 Verma | 636a0ae | 2021-02-13 01:52:42 +0000 | [diff] [blame] | 1202 | !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 Verma | 90563ae | 2021-02-13 01:52:41 +0000 | [diff] [blame] | 1209 | } |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
René Scharfe | b081547 | 2021-02-14 11:10:57 +0100 | [diff] [blame] | 1213 | static 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 Duy | 7e77df3 | 2013-04-19 09:08:47 +1000 | [diff] [blame] | 1241 | static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ |
| 1242 | const char *placeholder, |
Junio C Hamano | 9fa708d | 2009-10-04 23:43:32 -0700 | [diff] [blame] | 1243 | void *context) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1244 | { |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 1245 | struct format_commit_context *c = context; |
| 1246 | const struct commit *commit = c->commit; |
Pat Notz | 177b29d | 2010-11-02 13:59:08 -0600 | [diff] [blame] | 1247 | const char *msg = c->message; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 1248 | struct commit_list *p; |
Hariom Verma | 47d4676 | 2020-08-21 21:41:49 +0000 | [diff] [blame] | 1249 | const char *arg, *eol; |
Anders Waldenborg | fd2015b | 2019-01-28 22:33:36 +0100 | [diff] [blame] | 1250 | size_t res; |
Issac Trotts | ad6f028 | 2019-01-10 22:30:46 -0800 | [diff] [blame] | 1251 | char **slot; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1252 | |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1253 | /* these are independent of the commit */ |
Anders Waldenborg | fd2015b | 2019-01-28 22:33:36 +0100 | [diff] [blame] | 1254 | res = strbuf_expand_literal_cb(sb, placeholder, NULL); |
| 1255 | if (res) |
| 1256 | return res; |
| 1257 | |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1258 | switch (placeholder[0]) { |
| 1259 | case 'C': |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1260 | if (starts_with(placeholder + 1, "(auto)")) { |
Edward Thomson | b15a3e0 | 2016-05-26 22:46:10 -0500 | [diff] [blame] | 1261 | c->auto_color = want_color(c->pretty_ctx->color); |
René Scharfe | 82b83da | 2016-09-29 20:13:05 +0200 | [diff] [blame] | 1262 | if (c->auto_color && sb->len) |
René Scharfe | c99ad27 | 2016-09-17 20:25:24 +0200 | [diff] [blame] | 1263 | strbuf_addstr(sb, GIT_COLOR_RESET); |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 1264 | 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 King | c002922 | 2009-01-17 10:38:46 -0500 | [diff] [blame] | 1275 | } |
René Scharfe | 02edd56 | 2009-10-17 23:04:19 +0200 | [diff] [blame] | 1276 | 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 Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1300 | |
| 1301 | case '<': |
| 1302 | case '>': |
Jeff King | 9a1180f | 2019-03-20 04:16:42 -0400 | [diff] [blame] | 1303 | return parse_padding_placeholder(placeholder, c); |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1304 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1305 | |
René Scharfe | b081547 | 2021-02-14 11:10:57 +0100 | [diff] [blame] | 1306 | if (skip_prefix(placeholder, "(describe", &arg)) { |
René Scharfe | 15ae82d | 2021-02-14 11:04:34 +0100 | [diff] [blame] | 1307 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 1308 | struct strbuf out = STRBUF_INIT; |
| 1309 | struct strbuf err = STRBUF_INIT; |
René Scharfe | 9609972 | 2021-02-28 12:22:47 +0100 | [diff] [blame] | 1310 | 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é Scharfe | 15ae82d | 2021-02-14 11:04:34 +0100 | [diff] [blame] | 1318 | |
| 1319 | cmd.git_cmd = 1; |
| 1320 | strvec_push(&cmd.args, "describe"); |
René Scharfe | b081547 | 2021-02-14 11:10:57 +0100 | [diff] [blame] | 1321 | |
| 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é Scharfe | 15ae82d | 2021-02-14 11:04:34 +0100 | [diff] [blame] | 1332 | 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é Scharfe | b081547 | 2021-02-14 11:10:57 +0100 | [diff] [blame] | 1338 | return arg - placeholder + 1; |
René Scharfe | 15ae82d | 2021-02-14 11:04:34 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1341 | /* these depend on the commit */ |
| 1342 | if (!commit->object.parsed) |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 1343 | parse_object(the_repository, &commit->object.oid); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1344 | |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1345 | switch (placeholder[0]) { |
| 1346 | case 'H': /* commit hash */ |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 1347 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT)); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1348 | strbuf_addstr(sb, oid_to_hex(&commit->object.oid)); |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 1349 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET)); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1350 | return 1; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1351 | case 'h': /* abbreviated commit hash */ |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 1352 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT)); |
brian m. carlson | 30e677e | 2018-03-12 02:27:28 +0000 | [diff] [blame] | 1353 | strbuf_add_unique_abbrev(sb, &commit->object.oid, |
René Scharfe | 1eb47f1 | 2016-08-06 17:41:01 +0200 | [diff] [blame] | 1354 | c->pretty_ctx->abbrev); |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 1355 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET)); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1356 | return 1; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1357 | case 'T': /* tree hash */ |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 1358 | strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit))); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1359 | return 1; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1360 | case 't': /* abbreviated tree hash */ |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 1361 | strbuf_add_unique_abbrev(sb, |
Junio C Hamano | c89b6e1 | 2018-05-23 14:38:13 +0900 | [diff] [blame] | 1362 | get_commit_tree_oid(commit), |
René Scharfe | 1eb47f1 | 2016-08-06 17:41:01 +0200 | [diff] [blame] | 1363 | c->pretty_ctx->abbrev); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1364 | return 1; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1365 | case 'P': /* parent hashes */ |
| 1366 | for (p = commit->parents; p; p = p->next) { |
| 1367 | if (p != commit->parents) |
| 1368 | strbuf_addch(sb, ' '); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1369 | strbuf_addstr(sb, oid_to_hex(&p->item->object.oid)); |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1370 | } |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1371 | return 1; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1372 | 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. carlson | 30e677e | 2018-03-12 02:27:28 +0000 | [diff] [blame] | 1376 | strbuf_add_unique_abbrev(sb, &p->item->object.oid, |
René Scharfe | 1eb47f1 | 2016-08-06 17:41:01 +0200 | [diff] [blame] | 1377 | c->pretty_ctx->abbrev); |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1378 | } |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1379 | return 1; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1380 | case 'm': /* left/right/bottom */ |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 1381 | strbuf_addstr(sb, get_revision_mark(NULL, commit)); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1382 | return 1; |
René Scharfe | 3b3d443 | 2008-09-04 23:40:03 +0200 | [diff] [blame] | 1383 | case 'd': |
Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-19 09:08:49 +1000 | [diff] [blame] | 1384 | format_decorations(sb, commit, c->auto_color); |
René Scharfe | 3b3d443 | 2008-09-04 23:40:03 +0200 | [diff] [blame] | 1385 | return 1; |
Harry Jeffery | 9271095 | 2014-09-18 21:53:53 +0100 | [diff] [blame] | 1386 | case 'D': |
Harry Jeffery | 9271095 | 2014-09-18 21:53:53 +0100 | [diff] [blame] | 1387 | format_decorations_extended(sb, commit, c->auto_color, "", ", ", ""); |
| 1388 | return 1; |
Issac Trotts | ad6f028 | 2019-01-10 22:30:46 -0800 | [diff] [blame] | 1389 | 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 Rast | 8f8f547 | 2009-10-19 17:48:10 +0200 | [diff] [blame] | 1397 | 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 King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1404 | &c->pretty_ctx->date_mode, |
Junio C Hamano | 55ccf85 | 2012-05-07 14:11:32 -0700 | [diff] [blame] | 1405 | c->pretty_ctx->date_mode_explicit, |
Thomas Rast | 8f8f547 | 2009-10-19 17:48:10 +0200 | [diff] [blame] | 1406 | (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 King | cd1957f | 2011-12-16 06:40:24 -0500 | [diff] [blame] | 1412 | 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 King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1419 | &c->pretty_ctx->date_mode); |
Thomas Rast | 8f8f547 | 2009-10-19 17:48:10 +0200 | [diff] [blame] | 1420 | } |
| 1421 | return 0; /* unknown %g placeholder */ |
Johannes Schindelin | 8b208f0 | 2009-10-09 12:22:05 +0200 | [diff] [blame] | 1422 | case 'N': |
Junio C Hamano | ddf333f | 2012-10-17 18:51:47 -0700 | [diff] [blame] | 1423 | if (c->pretty_ctx->notes_message) { |
| 1424 | strbuf_addstr(sb, c->pretty_ctx->notes_message); |
Johannes Gilger | 5b16360 | 2010-04-13 22:31:12 +0200 | [diff] [blame] | 1425 | return 1; |
| 1426 | } |
| 1427 | return 0; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1428 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1429 | |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1430 | if (placeholder[0] == 'G') { |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 1431 | if (!c->signature_check.result) |
| 1432 | check_commit_signature(c->commit, &(c->signature_check)); |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1433 | switch (placeholder[1]) { |
| 1434 | case 'G': |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 1435 | if (c->signature_check.gpg_output) |
| 1436 | strbuf_addstr(sb, c->signature_check.gpg_output); |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1437 | break; |
| 1438 | case '?': |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 1439 | switch (c->signature_check.result) { |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1440 | case 'G': |
Hans Jerry Illikainen | 54887b4 | 2019-12-27 13:55:57 +0000 | [diff] [blame] | 1441 | 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 Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1451 | case 'B': |
Michael J Gruber | 661a180 | 2016-10-12 15:04:15 +0200 | [diff] [blame] | 1452 | case 'E': |
Sebastian Götte | e290c4b | 2013-03-31 18:03:22 +0200 | [diff] [blame] | 1453 | case 'N': |
Michael J Gruber | 661a180 | 2016-10-12 15:04:15 +0200 | [diff] [blame] | 1454 | case 'X': |
| 1455 | case 'Y': |
| 1456 | case 'R': |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 1457 | strbuf_addch(sb, c->signature_check.result); |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1458 | } |
| 1459 | break; |
| 1460 | case 'S': |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 1461 | if (c->signature_check.signer) |
| 1462 | strbuf_addstr(sb, c->signature_check.signer); |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1463 | break; |
Michael J Gruber | 0174eea | 2013-02-14 17:04:46 +0100 | [diff] [blame] | 1464 | case 'K': |
Sebastian Götte | ffb6d7d | 2013-03-31 18:00:14 +0200 | [diff] [blame] | 1465 | if (c->signature_check.key) |
| 1466 | strbuf_addstr(sb, c->signature_check.key); |
Michael J Gruber | 0174eea | 2013-02-14 17:04:46 +0100 | [diff] [blame] | 1467 | break; |
Michał Górny | 3daaaab | 2018-10-22 18:38:20 +0200 | [diff] [blame] | 1468 | case 'F': |
| 1469 | if (c->signature_check.fingerprint) |
| 1470 | strbuf_addstr(sb, c->signature_check.fingerprint); |
| 1471 | break; |
Michał Górny | 4de9394 | 2018-10-22 18:38:21 +0200 | [diff] [blame] | 1472 | case 'P': |
| 1473 | if (c->signature_check.primary_key_fingerprint) |
| 1474 | strbuf_addstr(sb, c->signature_check.primary_key_fingerprint); |
| 1475 | break; |
Hans Jerry Illikainen | 54887b4 | 2019-12-27 13:55:57 +0000 | [diff] [blame] | 1476 | 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 King | aa4b78d | 2014-06-16 20:07:07 -0400 | [diff] [blame] | 1495 | default: |
| 1496 | return 0; |
Junio C Hamano | f6667c5 | 2011-10-21 21:06:02 -0700 | [diff] [blame] | 1497 | } |
| 1498 | return 2; |
| 1499 | } |
| 1500 | |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1501 | /* For the rest we have to parse the commit header. */ |
Jeff King | 018b9de | 2021-01-28 14:57:39 -0500 | [diff] [blame] | 1502 | if (!c->commit_header_parsed) { |
| 1503 | msg = c->message = |
| 1504 | repo_logmsg_reencode(c->repository, commit, |
| 1505 | &c->commit_encoding, "UTF-8"); |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 1506 | parse_commit_header(c); |
Jeff King | 018b9de | 2021-01-28 14:57:39 -0500 | [diff] [blame] | 1507 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1508 | |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 1509 | switch (placeholder[0]) { |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1510 | case 'a': /* author ... */ |
| 1511 | return format_person_part(sb, placeholder[1], |
Jeff King | d36f867 | 2008-08-28 20:54:59 -0400 | [diff] [blame] | 1512 | msg + c->author.off, c->author.len, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1513 | &c->pretty_ctx->date_mode); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1514 | case 'c': /* committer ... */ |
| 1515 | return format_person_part(sb, placeholder[1], |
Jeff King | d36f867 | 2008-08-28 20:54:59 -0400 | [diff] [blame] | 1516 | msg + c->committer.off, c->committer.len, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1517 | &c->pretty_ctx->date_mode); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1518 | case 'e': /* encoding */ |
Nguyễn Thái Ngọc Duy | 0940a76 | 2013-04-19 09:08:41 +1000 | [diff] [blame] | 1519 | if (c->commit_encoding) |
| 1520 | strbuf_addstr(sb, c->commit_encoding); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1521 | return 1; |
Eli Barzilay | 1367b12 | 2010-03-24 22:51:52 -0400 | [diff] [blame] | 1522 | 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é Scharfe | f53bd74 | 2008-12-27 01:49:21 +0100 | [diff] [blame] | 1526 | } |
| 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 Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 1536 | case 'f': /* sanitized subject */ |
Hariom Verma | 47d4676 | 2020-08-21 21:41:49 +0000 | [diff] [blame] | 1537 | eol = strchrnul(msg + c->subject_off, '\n'); |
| 1538 | format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off)); |
Stephen Boyd | 46d164b | 2009-03-22 19:14:01 -0700 | [diff] [blame] | 1539 | return 1; |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1540 | case 'b': /* body */ |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 1541 | strbuf_addstr(sb, msg + c->body_off); |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1542 | return 1; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1543 | } |
Jacob Keller | d9f31fb | 2016-11-18 16:58:14 -0800 | [diff] [blame] | 1544 | |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1545 | if (skip_prefix(placeholder, "(trailers", &arg)) { |
Jeff King | a388b10 | 2017-08-15 06:23:56 -0400 | [diff] [blame] | 1546 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
Anders Waldenborg | 250bea0 | 2019-01-28 22:33:34 +0100 | [diff] [blame] | 1547 | struct string_list filter_list = STRING_LIST_INIT_NODUP; |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1548 | struct strbuf sepbuf = STRBUF_INIT; |
Ævar Arnfjörð Bjarmason | 058761f | 2020-12-09 16:52:08 +0100 | [diff] [blame] | 1549 | struct strbuf kvsepbuf = STRBUF_INIT; |
Anders Waldenborg | 3e3f347 | 2019-01-28 22:33:33 +0100 | [diff] [blame] | 1550 | size_t ret = 0; |
Jeff King | e5fba5d | 2018-08-22 20:50:17 -0400 | [diff] [blame] | 1551 | |
| 1552 | opts.no_divider = 1; |
| 1553 | |
Taylor Blau | 84ff053 | 2017-10-01 09:18:47 -0700 | [diff] [blame] | 1554 | if (*arg == ':') { |
| 1555 | arg++; |
Hariom Verma | 636a0ae | 2021-02-13 01:52:42 +0000 | [diff] [blame] | 1556 | if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL)) |
Hariom Verma | 90563ae | 2021-02-13 01:52:41 +0000 | [diff] [blame] | 1557 | goto trailer_out; |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1558 | } |
| 1559 | if (*arg == ')') { |
| 1560 | format_trailers_from_commit(sb, msg + c->subject_off, &opts); |
Anders Waldenborg | 3e3f347 | 2019-01-28 22:33:33 +0100 | [diff] [blame] | 1561 | ret = arg - placeholder + 1; |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1562 | } |
Anders Waldenborg | 250bea0 | 2019-01-28 22:33:34 +0100 | [diff] [blame] | 1563 | trailer_out: |
| 1564 | string_list_clear(&filter_list, 0); |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1565 | strbuf_release(&sepbuf); |
Anders Waldenborg | 3e3f347 | 2019-01-28 22:33:33 +0100 | [diff] [blame] | 1566 | return ret; |
Jacob Keller | d9f31fb | 2016-11-18 16:58:14 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1569 | return 0; /* unknown placeholder */ |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1570 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1571 | |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1572 | static 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 Kufner | 3ad87c8 | 2016-06-16 20:18:37 +0700 | [diff] [blame] | 1584 | occupied += c->pretty_ctx->graph_width; |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1585 | 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 Duy | 1640632 | 2013-04-19 09:08:52 +1000 | [diff] [blame] | 1602 | |
| 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 Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1632 | 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é Scharfe | e992d1e | 2014-07-10 10:52:21 +0200 | [diff] [blame] | 1653 | strbuf_addbuf(sb, &local_sb); |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 1654 | } else { |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1655 | 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é Scharfe | 415792e | 2014-09-07 09:06:42 +0200 | [diff] [blame] | 1665 | strbuf_addchars(sb, ' ', padding); |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1666 | 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 Duy | 7e77df3 | 2013-04-19 09:08:47 +1000 | [diff] [blame] | 1674 | static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */ |
| 1675 | const char *placeholder, |
Junio C Hamano | 9fa708d | 2009-10-04 23:43:32 -0700 | [diff] [blame] | 1676 | 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 Hamano | 223a923 | 2010-06-22 09:45:22 -0700 | [diff] [blame] | 1684 | ADD_SP_BEFORE_NON_EMPTY |
Junio C Hamano | 9fa708d | 2009-10-04 23:43:32 -0700 | [diff] [blame] | 1685 | } 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 Gruber | 7b88176 | 2010-06-14 18:12:29 +0200 | [diff] [blame] | 1694 | case ' ': |
| 1695 | magic = ADD_SP_BEFORE_NON_EMPTY; |
| 1696 | break; |
Junio C Hamano | 9fa708d | 2009-10-04 23:43:32 -0700 | [diff] [blame] | 1697 | default: |
| 1698 | break; |
| 1699 | } |
| 1700 | if (magic != NO_MAGIC) |
| 1701 | placeholder++; |
| 1702 | |
| 1703 | orig_len = sb->len; |
Nguyễn Thái Ngọc Duy | a575234 | 2013-04-19 09:08:50 +1000 | [diff] [blame] | 1704 | 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 Hamano | 9fa708d | 2009-10-04 23:43:32 -0700 | [diff] [blame] | 1708 | 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 Gruber | 7b88176 | 2010-06-14 18:12:29 +0200 | [diff] [blame] | 1714 | } else if (orig_len != sb->len) { |
| 1715 | if (magic == ADD_LF_BEFORE_NON_EMPTY) |
René Scharfe | a91cc7f | 2020-02-09 14:44:23 +0100 | [diff] [blame] | 1716 | strbuf_insertstr(sb, orig_len, "\n"); |
Michael J Gruber | 7b88176 | 2010-06-14 18:12:29 +0200 | [diff] [blame] | 1717 | else if (magic == ADD_SP_BEFORE_NON_EMPTY) |
René Scharfe | a91cc7f | 2020-02-09 14:44:23 +0100 | [diff] [blame] | 1718 | strbuf_insertstr(sb, orig_len, " "); |
Junio C Hamano | 9fa708d | 2009-10-04 23:43:32 -0700 | [diff] [blame] | 1719 | } |
| 1720 | return consumed + 1; |
| 1721 | } |
| 1722 | |
Johannes Gilger | 5b16360 | 2010-04-13 22:31:12 +0200 | [diff] [blame] | 1723 | static size_t userformat_want_item(struct strbuf *sb, const char *placeholder, |
| 1724 | void *context) |
| 1725 | { |
| 1726 | struct userformat_want *w = context; |
| 1727 | |
Michael J Gruber | 7b88176 | 2010-06-14 18:12:29 +0200 | [diff] [blame] | 1728 | if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ') |
Johannes Gilger | 5b16360 | 2010-04-13 22:31:12 +0200 | [diff] [blame] | 1729 | placeholder++; |
| 1730 | |
| 1731 | switch (*placeholder) { |
| 1732 | case 'N': |
| 1733 | w->notes = 1; |
| 1734 | break; |
Issac Trotts | ad6f028 | 2019-01-10 22:30:46 -0800 | [diff] [blame] | 1735 | case 'S': |
| 1736 | w->source = 1; |
| 1737 | break; |
Johannes Gilger | 5b16360 | 2010-04-13 22:31:12 +0200 | [diff] [blame] | 1738 | } |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | void 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 Hamano | a6253d1 | 2011-05-25 12:23:44 -0700 | [diff] [blame] | 1751 | strbuf_expand(&dummy, fmt, userformat_want_item, w); |
Johannes Gilger | 5b16360 | 2010-04-13 22:31:12 +0200 | [diff] [blame] | 1752 | strbuf_release(&dummy); |
| 1753 | } |
| 1754 | |
Stefan Beller | f54fbf5 | 2018-11-13 16:13:00 -0800 | [diff] [blame] | 1755 | void 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é Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 1759 | { |
Denton Liu | 3e8ed3b | 2019-11-19 16:51:16 -0800 | [diff] [blame] | 1760 | struct format_commit_context context = { |
Jeff King | 018b9de | 2021-01-28 14:57:39 -0500 | [diff] [blame] | 1761 | .repository = r, |
Denton Liu | 3e8ed3b | 2019-11-19 16:51:16 -0800 | [diff] [blame] | 1762 | .commit = commit, |
| 1763 | .pretty_ctx = pretty_ctx, |
| 1764 | .wrap_start = sb->len |
| 1765 | }; |
Pat Notz | 177b29d | 2010-11-02 13:59:08 -0600 | [diff] [blame] | 1766 | const char *output_enc = pretty_ctx->output_encoding; |
Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-19 09:08:47 +1000 | [diff] [blame] | 1767 | const char *utf8 = "UTF-8"; |
René Scharfe | f29d595 | 2007-11-10 12:14:20 +0100 | [diff] [blame] | 1768 | |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 1769 | strbuf_expand(sb, format, format_commit_item, &context); |
René Scharfe | 02edd56 | 2009-10-17 23:04:19 +0200 | [diff] [blame] | 1770 | rewrap_message_tail(sb, &context, 0, 0, 0); |
Pat Notz | 177b29d | 2010-11-02 13:59:08 -0600 | [diff] [blame] | 1771 | |
Jeff King | 018b9de | 2021-01-28 14:57:39 -0500 | [diff] [blame] | 1772 | /* |
| 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 Duy | 7e77df3 | 2013-04-19 09:08:47 +1000 | [diff] [blame] | 1777 | 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 King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 1787 | size_t outsz; |
Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-19 09:08:47 +1000 | [diff] [blame] | 1788 | 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 Duy | 0940a76 | 2013-04-19 09:08:41 +1000 | [diff] [blame] | 1794 | free(context.commit_encoding); |
Stefan Beller | f54fbf5 | 2018-11-13 16:13:00 -0800 | [diff] [blame] | 1795 | repo_unuse_commit_buffer(r, commit, context.message); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Jeff King | 10f2fbf | 2013-07-03 03:07:48 -0400 | [diff] [blame] | 1798 | static void pp_header(struct pretty_print_context *pp, |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1799 | 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é Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1807 | const char *name, *line = *msg_p; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1808 | 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 King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 1818 | if (pp->fmt == CMIT_FMT_RAW) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1819 | strbuf_add(sb, line, linelen); |
| 1820 | continue; |
| 1821 | } |
| 1822 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1823 | if (starts_with(line, "parent ")) { |
brian m. carlson | 580f098 | 2018-07-16 01:28:08 +0000 | [diff] [blame] | 1824 | if (linelen != the_hash_algo->hexsz + 8) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1825 | die("bad parent line in commit"); |
| 1826 | continue; |
| 1827 | } |
| 1828 | |
| 1829 | if (!parents_shown) { |
René Scharfe | 4bbaa1e | 2014-07-17 01:52:09 +0200 | [diff] [blame] | 1830 | unsigned num = commit_list_count(commit->parents); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1831 | /* with enough slop */ |
brian m. carlson | 580f098 | 2018-07-16 01:28:08 +0000 | [diff] [blame] | 1832 | strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20); |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 1833 | add_merge_info(pp, sb, commit); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1834 | 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é Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1842 | if (skip_prefix(line, "author ", &name)) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1843 | strbuf_grow(sb, linelen + 80); |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1844 | pp_user_info(pp, "Author", sb, name, encoding); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1845 | } |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1846 | if (skip_prefix(line, "committer ", &name) && |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 1847 | (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1848 | strbuf_grow(sb, linelen + 80); |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1849 | pp_user_info(pp, "Commit", sb, name, encoding); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
Jeff King | 10f2fbf | 2013-07-03 03:07:48 -0400 | [diff] [blame] | 1854 | void pp_title_line(struct pretty_print_context *pp, |
Daniel Barkalow | b02bd65 | 2008-02-18 22:56:08 -0500 | [diff] [blame] | 1855 | const char **msg_p, |
| 1856 | struct strbuf *sb, |
Daniel Barkalow | b02bd65 | 2008-02-18 22:56:08 -0500 | [diff] [blame] | 1857 | const char *encoding, |
Junio C Hamano | 267123b | 2008-03-15 00:09:20 -0700 | [diff] [blame] | 1858 | int need_8bit_cte) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1859 | { |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 1860 | static const int max_length = 78; /* per rfc2047 */ |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1861 | struct strbuf title; |
| 1862 | |
| 1863 | strbuf_init(&title, 80); |
Jeff King | 9553d2b | 2011-05-26 18:28:17 -0400 | [diff] [blame] | 1864 | *msg_p = format_subject(&title, *msg_p, |
| 1865 | pp->preserve_subject ? "\n" : " "); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1866 | |
| 1867 | strbuf_grow(sb, title.len + 1024); |
René Scharfe | 6d167fd | 2017-03-01 12:37:07 +0100 | [diff] [blame] | 1868 | if (pp->print_email_subject) { |
| 1869 | if (pp->rev) |
| 1870 | fmt_output_email_subject(sb, pp->rev); |
Emma Brooks | 19d097e | 2020-04-08 04:31:38 +0000 | [diff] [blame] | 1871 | if (pp->encode_email_headers && |
| 1872 | needs_rfc2047_encoding(title.buf, title.len)) |
Jan H. Schönherr | 41dd00b | 2012-10-18 16:43:33 +0200 | [diff] [blame] | 1873 | 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 Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1878 | } else { |
| 1879 | strbuf_addbuf(sb, &title); |
| 1880 | } |
| 1881 | strbuf_addch(sb, '\n'); |
| 1882 | |
Jeff King | a908047 | 2013-07-03 03:08:22 -0400 | [diff] [blame] | 1883 | 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 Hamano | 6bf4f1b | 2008-03-14 17:10:09 -0700 | [diff] [blame] | 1893 | if (need_8bit_cte > 0) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1894 | 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 King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 1900 | if (pp->after_subject) { |
| 1901 | strbuf_addstr(sb, pp->after_subject); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1902 | } |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 1903 | if (cmit_fmt_is_mail(pp->fmt)) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1904 | strbuf_addch(sb, '\n'); |
| 1905 | } |
Jeff King | a908047 | 2013-07-03 03:08:22 -0400 | [diff] [blame] | 1906 | |
| 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 Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 1917 | strbuf_release(&title); |
| 1918 | } |
| 1919 | |
Linus Torvalds | 7cc13c7 | 2016-03-16 09:15:53 -0700 | [diff] [blame] | 1920 | static 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 Hamano | fe37a9c | 2016-03-29 16:05:39 -0700 | [diff] [blame] | 1934 | static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth, |
Linus Torvalds | 7cc13c7 | 2016-03-16 09:15:53 -0700 | [diff] [blame] | 1935 | 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 Hamano | fe37a9c | 2016-03-29 16:05:39 -0700 | [diff] [blame] | 1955 | strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth)); |
Linus Torvalds | 7cc13c7 | 2016-03-16 09:15:53 -0700 | [diff] [blame] | 1956 | |
| 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 | */ |
| 1975 | static 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 Hamano | fe37a9c | 2016-03-29 16:05:39 -0700 | [diff] [blame] | 1981 | strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen); |
Linus Torvalds | 7cc13c7 | 2016-03-16 09:15:53 -0700 | [diff] [blame] | 1982 | else |
| 1983 | strbuf_add(sb, line, linelen); |
| 1984 | } |
| 1985 | |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 1986 | static 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 King | 10f2fbf | 2013-07-03 03:07:48 -0400 | [diff] [blame] | 1996 | void pp_remainder(struct pretty_print_context *pp, |
Daniel Barkalow | b02bd65 | 2008-02-18 22:56:08 -0500 | [diff] [blame] | 1997 | const char **msg_p, |
| 1998 | struct strbuf *sb, |
| 1999 | int indent) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2000 | { |
| 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 Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 2010 | if (is_blank_line(line, &linelen)) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2011 | if (first) |
| 2012 | continue; |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2013 | if (pp->fmt == CMIT_FMT_SHORT) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2014 | break; |
| 2015 | } |
| 2016 | first = 0; |
| 2017 | |
| 2018 | strbuf_grow(sb, linelen + indent + 20); |
René Scharfe | 415792e | 2014-09-07 09:06:42 +0200 | [diff] [blame] | 2019 | if (indent) |
Linus Torvalds | 7cc13c7 | 2016-03-16 09:15:53 -0700 | [diff] [blame] | 2020 | pp_handle_indent(pp, sb, indent, line, linelen); |
Junio C Hamano | 0893eec | 2016-03-29 15:49:24 -0700 | [diff] [blame] | 2021 | else if (pp->expand_tabs_in_log) |
Junio C Hamano | fe37a9c | 2016-03-29 16:05:39 -0700 | [diff] [blame] | 2022 | strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, |
| 2023 | line, linelen); |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 2024 | else { |
| 2025 | if (pp->fmt == CMIT_FMT_MBOXRD && |
| 2026 | is_mboxrd_from(line, linelen)) |
| 2027 | strbuf_addch(sb, '>'); |
| 2028 | |
Linus Torvalds | 7cc13c7 | 2016-03-16 09:15:53 -0700 | [diff] [blame] | 2029 | strbuf_add(sb, line, linelen); |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 2030 | } |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2031 | strbuf_addch(sb, '\n'); |
| 2032 | } |
| 2033 | } |
| 2034 | |
Jeff King | 10f2fbf | 2013-07-03 03:07:48 -0400 | [diff] [blame] | 2035 | void pretty_print_commit(struct pretty_print_context *pp, |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2036 | const struct commit *commit, |
| 2037 | struct strbuf *sb) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2038 | { |
| 2039 | unsigned long beginning_of_body; |
| 2040 | int indent = 4; |
Jeff King | dd0d388 | 2013-01-26 04:44:06 -0500 | [diff] [blame] | 2041 | const char *msg; |
Jeff King | b000c59 | 2014-06-10 17:39:30 -0400 | [diff] [blame] | 2042 | const char *reencoded; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2043 | const char *encoding; |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2044 | int need_8bit_cte = pp->need_8bit_cte; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2045 | |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2046 | if (pp->fmt == CMIT_FMT_USERFORMAT) { |
| 2047 | format_commit_message(commit, user_format, sb, pp); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2048 | return; |
| 2049 | } |
| 2050 | |
Junio C Hamano | e297cf5 | 2012-10-17 17:12:55 -0700 | [diff] [blame] | 2051 | encoding = get_log_output_encoding(); |
Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-19 09:08:40 +1000 | [diff] [blame] | 2052 | msg = reencoded = logmsg_reencode(commit, NULL, encoding); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2053 | |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 2054 | if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt)) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2055 | indent = 0; |
| 2056 | |
Junio C Hamano | 6bf4f1b | 2008-03-14 17:10:09 -0700 | [diff] [blame] | 2057 | /* |
| 2058 | * We need to check and emit Content-type: to mark it |
| 2059 | * as 8-bit if we haven't done so. |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2060 | */ |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 2061 | if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2062 | 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 Hamano | 6bf4f1b | 2008-03-14 17:10:09 -0700 | [diff] [blame] | 2074 | need_8bit_cte = 1; |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2075 | break; |
| 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2080 | pp_header(pp, encoding, commit, &msg, sb); |
René Scharfe | 6d167fd | 2017-03-01 12:37:07 +0100 | [diff] [blame] | 2081 | if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) { |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2082 | strbuf_addch(sb, '\n'); |
| 2083 | } |
| 2084 | |
| 2085 | /* Skip excess blank lines at the beginning of body, if any... */ |
Johannes Schindelin | 7735612 | 2016-06-22 22:20:16 +0200 | [diff] [blame] | 2086 | msg = skip_blank_lines(msg); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2087 | |
| 2088 | /* These formats treat the title line specially. */ |
Eric Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 2089 | if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt)) |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2090 | pp_title_line(pp, &msg, sb, encoding, need_8bit_cte); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2091 | |
| 2092 | beginning_of_body = sb->len; |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2093 | if (pp->fmt != CMIT_FMT_ONELINE) |
| 2094 | pp_remainder(pp, &msg, sb, indent); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2095 | strbuf_rtrim(sb); |
| 2096 | |
| 2097 | /* Make sure there is an EOLN for the non-oneline case */ |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2098 | if (pp->fmt != CMIT_FMT_ONELINE) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2099 | 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 Wong | 9f23e04 | 2016-06-05 04:46:39 +0000 | [diff] [blame] | 2106 | if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body) |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2107 | strbuf_addch(sb, '\n'); |
Johannes Schindelin | a97a746 | 2009-10-09 12:21:57 +0200 | [diff] [blame] | 2108 | |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 2109 | unuse_commit_buffer(commit, reencoded); |
Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 +0000 | [diff] [blame] | 2110 | } |
Jeff King | 8b8a537 | 2011-05-26 18:27:24 -0400 | [diff] [blame] | 2111 | |
| 2112 | void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit, |
| 2113 | struct strbuf *sb) |
| 2114 | { |
| 2115 | struct pretty_print_context pp = {0}; |
Jeff King | 6bf1394 | 2011-05-26 18:27:49 -0400 | [diff] [blame] | 2116 | pp.fmt = fmt; |
| 2117 | pretty_print_commit(&pp, commit, sb); |
Jeff King | 8b8a537 | 2011-05-26 18:27:24 -0400 | [diff] [blame] | 2118 | } |