| Elijah Newren | 77f091e | 2023-04-11 07:42:00 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| Brandon Williams | b2141fc | 2017-06-14 18:07:36 | [diff] [blame] | 2 | #include "config.h" |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 3 | #include "commit.h" |
| Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 | [diff] [blame] | 4 | #include "environment.h" |
| Elijah Newren | f394e09 | 2023-03-21 06:25:54 | [diff] [blame] | 5 | #include "gettext.h" |
| Elijah Newren | df6e874 | 2023-05-16 06:34:00 | [diff] [blame] | 6 | #include "hash.h" |
| Elijah Newren | 41771fa | 2023-02-24 00:09:27 | [diff] [blame] | 7 | #include "hex.h" |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 8 | #include "utf8.h" |
| 9 | #include "diff.h" |
| Elijah Newren | ca4eed7 | 2023-04-11 07:41:59 | [diff] [blame] | 10 | #include "pager.h" |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 11 | #include "revision.h" |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 12 | #include "string-list.h" |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 13 | #include "mailmap.h" |
| René Scharfe | 3b3d443 | 2008-09-04 21:40:03 | [diff] [blame] | 14 | #include "log-tree.h" |
| Johannes Schindelin | a97a746 | 2009-10-09 10:21:57 | [diff] [blame] | 15 | #include "notes.h" |
| Jeff King | c002922 | 2009-01-17 15:38:46 | [diff] [blame] | 16 | #include "color.h" |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 17 | #include "reflog-walk.h" |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 18 | #include "gpg-interface.h" |
| Jacob Keller | d9f31fb | 2016-11-19 00:58:14 | [diff] [blame] | 19 | #include "trailer.h" |
| René Scharfe | 15ae82d | 2021-02-14 10:04:34 | [diff] [blame] | 20 | #include "run-command.h" |
| Calvin Wan | f940185 | 2023-06-06 19:48:41 | [diff] [blame] | 21 | #include "object-name.h" |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 22 | |
| Patrick Steinhardt | 304a50a | 2022-12-01 14:47:23 | [diff] [blame] | 23 | /* |
| 24 | * The limit for formatting directives, which enable the caller to append |
| 25 | * arbitrarily many bytes to the formatted buffer. This includes padding |
| 26 | * and wrapping formatters. |
| 27 | */ |
| 28 | #define FORMATTING_LIMIT (16 * 1024) |
| 29 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 30 | static char *user_format; |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 31 | static struct cmt_fmt_map { |
| 32 | const char *name; |
| 33 | enum cmit_fmt format; |
| 34 | int is_tformat; |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 35 | int expand_tabs_in_log; |
| Will Palmer | 2d7671e | 2010-05-02 11:00:43 | [diff] [blame] | 36 | int is_alias; |
| Denton Liu | 618a855 | 2019-11-20 00:51:23 | [diff] [blame] | 37 | enum date_mode_type default_date_mode_type; |
| Will Palmer | 2d7671e | 2010-05-02 11:00:43 | [diff] [blame] | 38 | const char *user_format; |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 39 | } *commit_formats; |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 40 | static size_t builtin_formats_len; |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 41 | static size_t commit_formats_len; |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 42 | static size_t commit_formats_alloc; |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 43 | static struct cmt_fmt_map *find_commit_format(const char *sought); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 44 | |
| Jeff King | b9c7d6e | 2014-07-29 17:56:48 | [diff] [blame] | 45 | int commit_format_is_empty(enum cmit_fmt fmt) |
| 46 | { |
| 47 | return fmt == CMIT_FMT_USERFORMAT && !*user_format; |
| 48 | } |
| 49 | |
| Nanako Shiraishi | 3640754 | 2009-02-24 09:59:15 | [diff] [blame] | 50 | static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat) |
| 51 | { |
| 52 | free(user_format); |
| 53 | user_format = xstrdup(cp); |
| 54 | if (is_tformat) |
| 55 | rev->use_terminator = 1; |
| 56 | rev->commit_format = CMIT_FMT_USERFORMAT; |
| 57 | } |
| 58 | |
| Jeff King | 783a86c | 2022-08-19 10:08:44 | [diff] [blame] | 59 | static int git_pretty_formats_config(const char *var, const char *value, |
| Glen Choo | a4e7e31 | 2023-06-28 19:26:22 | [diff] [blame] | 60 | const struct config_context *ctx UNUSED, |
| Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 17:09:48 | [diff] [blame] | 61 | void *cb UNUSED) |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 62 | { |
| 63 | struct cmt_fmt_map *commit_format = NULL; |
| 64 | const char *name; |
| 65 | const char *fmt; |
| 66 | int i; |
| 67 | |
| Jeff King | 95b567c | 2014-06-18 19:48:29 | [diff] [blame] | 68 | if (!skip_prefix(var, "pretty.", &name)) |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 69 | return 0; |
| 70 | |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 71 | for (i = 0; i < builtin_formats_len; i++) { |
| 72 | if (!strcmp(commit_formats[i].name, name)) |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | for (i = builtin_formats_len; i < commit_formats_len; i++) { |
| 77 | if (!strcmp(commit_formats[i].name, name)) { |
| 78 | commit_format = &commit_formats[i]; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (!commit_format) { |
| 84 | ALLOC_GROW(commit_formats, commit_formats_len+1, |
| 85 | commit_formats_alloc); |
| 86 | commit_format = &commit_formats[commit_formats_len]; |
| Jonathan Nieder | 95a2618 | 2010-05-08 21:07:39 | [diff] [blame] | 87 | memset(commit_format, 0, sizeof(*commit_format)); |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 88 | commit_formats_len++; |
| 89 | } |
| 90 | |
| 91 | commit_format->name = xstrdup(name); |
| 92 | commit_format->format = CMIT_FMT_USERFORMAT; |
| Tanay Abhra | a26bc61 | 2014-08-04 14:41:15 | [diff] [blame] | 93 | if (git_config_string(&fmt, var, value)) |
| 94 | return -1; |
| 95 | |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 96 | if (skip_prefix(fmt, "format:", &fmt)) |
| 97 | commit_format->is_tformat = 0; |
| 98 | else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%')) |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 99 | commit_format->is_tformat = 1; |
| 100 | else |
| 101 | commit_format->is_alias = 1; |
| 102 | commit_format->user_format = fmt; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 107 | static void setup_commit_formats(void) |
| 108 | { |
| 109 | struct cmt_fmt_map builtin_formats[] = { |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 110 | { "raw", CMIT_FMT_RAW, 0, 0 }, |
| Junio C Hamano | fe37a9c | 2016-03-29 23:05:39 | [diff] [blame] | 111 | { "medium", CMIT_FMT_MEDIUM, 0, 8 }, |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 112 | { "short", CMIT_FMT_SHORT, 0, 0 }, |
| 113 | { "email", CMIT_FMT_EMAIL, 0, 0 }, |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 114 | { "mboxrd", CMIT_FMT_MBOXRD, 0, 0 }, |
| Junio C Hamano | fe37a9c | 2016-03-29 23:05:39 | [diff] [blame] | 115 | { "fuller", CMIT_FMT_FULLER, 0, 8 }, |
| 116 | { "full", CMIT_FMT_FULL, 0, 8 }, |
| Denton Liu | 1f0fc1d | 2019-11-20 00:51:25 | [diff] [blame] | 117 | { "oneline", CMIT_FMT_ONELINE, 1, 0 }, |
| 118 | { "reference", CMIT_FMT_USERFORMAT, 1, 0, |
| 119 | 0, DATE_SHORT, "%C(auto)%h (%s, %ad)" }, |
| Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 11:24:41 | [diff] [blame] | 120 | /* |
| 121 | * Please update $__git_log_pretty_formats in |
| 122 | * git-completion.bash when you add new formats. |
| 123 | */ |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 124 | }; |
| 125 | commit_formats_len = ARRAY_SIZE(builtin_formats); |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 126 | builtin_formats_len = commit_formats_len; |
| 127 | ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc); |
| René Scharfe | 921d49b | 2019-06-15 18:36:35 | [diff] [blame] | 128 | COPY_ARRAY(commit_formats, builtin_formats, |
| 129 | ARRAY_SIZE(builtin_formats)); |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 130 | |
| 131 | git_config(git_pretty_formats_config, NULL); |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 132 | } |
| 133 | |
| Will Palmer | 2d7671e | 2010-05-02 11:00:43 | [diff] [blame] | 134 | static struct cmt_fmt_map *find_commit_format_recursive(const char *sought, |
| 135 | const char *original, |
| 136 | int num_redirections) |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 137 | { |
| 138 | struct cmt_fmt_map *found = NULL; |
| 139 | size_t found_match_len = 0; |
| 140 | int i; |
| 141 | |
| Will Palmer | 2d7671e | 2010-05-02 11:00:43 | [diff] [blame] | 142 | if (num_redirections >= commit_formats_len) |
| 143 | die("invalid --pretty format: " |
| 144 | "'%s' references an alias which points to itself", |
| 145 | original); |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 146 | |
| 147 | for (i = 0; i < commit_formats_len; i++) { |
| 148 | size_t match_len; |
| 149 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 150 | if (!starts_with(commit_formats[i].name, sought)) |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 151 | continue; |
| 152 | |
| 153 | match_len = strlen(commit_formats[i].name); |
| 154 | if (found == NULL || found_match_len > match_len) { |
| 155 | found = &commit_formats[i]; |
| 156 | found_match_len = match_len; |
| 157 | } |
| 158 | } |
| Will Palmer | 2d7671e | 2010-05-02 11:00:43 | [diff] [blame] | 159 | |
| 160 | if (found && found->is_alias) { |
| 161 | found = find_commit_format_recursive(found->user_format, |
| 162 | original, |
| 163 | num_redirections+1); |
| 164 | } |
| 165 | |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 166 | return found; |
| 167 | } |
| 168 | |
| Will Palmer | 2d7671e | 2010-05-02 11:00:43 | [diff] [blame] | 169 | static struct cmt_fmt_map *find_commit_format(const char *sought) |
| 170 | { |
| 171 | if (!commit_formats) |
| 172 | setup_commit_formats(); |
| 173 | |
| 174 | return find_commit_format_recursive(sought, sought, 0); |
| 175 | } |
| 176 | |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 177 | void get_commit_format(const char *arg, struct rev_info *rev) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 178 | { |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 179 | struct cmt_fmt_map *commit_format; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 180 | |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 181 | rev->use_terminator = 0; |
| Jeff King | c75e7ad | 2014-07-29 17:54:46 | [diff] [blame] | 182 | if (!arg) { |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 183 | rev->commit_format = CMIT_FMT_DEFAULT; |
| 184 | return; |
| 185 | } |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 186 | if (skip_prefix(arg, "format:", &arg)) { |
| 187 | save_user_format(rev, arg, 0); |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 188 | return; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 189 | } |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 190 | |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 191 | if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) { |
| Nanako Shiraishi | 3640754 | 2009-02-24 09:59:15 | [diff] [blame] | 192 | save_user_format(rev, arg, 1); |
| 193 | return; |
| 194 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 195 | |
| Will Palmer | 4095789 | 2010-05-02 11:00:42 | [diff] [blame] | 196 | commit_format = find_commit_format(arg); |
| 197 | if (!commit_format) |
| 198 | die("invalid --pretty format: %s", arg); |
| 199 | |
| 200 | rev->commit_format = commit_format->format; |
| 201 | rev->use_terminator = commit_format->is_tformat; |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 202 | rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log; |
| Denton Liu | 618a855 | 2019-11-20 00:51:23 | [diff] [blame] | 203 | if (!rev->date_mode_explicit && commit_format->default_date_mode_type) |
| 204 | rev->date_mode.type = commit_format->default_date_mode_type; |
| Will Palmer | 8028184 | 2010-05-02 11:00:44 | [diff] [blame] | 205 | if (commit_format->format == CMIT_FMT_USERFORMAT) { |
| 206 | save_user_format(rev, commit_format->user_format, |
| 207 | commit_format->is_tformat); |
| 208 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Generic support for pretty-printing the header |
| 213 | */ |
| 214 | static int get_one_line(const char *msg) |
| 215 | { |
| 216 | int ret = 0; |
| 217 | |
| 218 | for (;;) { |
| 219 | char c = *msg++; |
| 220 | if (!c) |
| 221 | break; |
| 222 | ret++; |
| 223 | if (c == '\n') |
| 224 | break; |
| 225 | } |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | /* High bit set, or ISO-2022-INT */ |
| Junio C Hamano | cc57114 | 2010-01-12 06:23:35 | [diff] [blame] | 230 | static int non_ascii(int ch) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 231 | { |
| René Scharfe | c2e9364 | 2009-03-07 13:06:49 | [diff] [blame] | 232 | return !isascii(ch) || ch == '\033'; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 233 | } |
| 234 | |
| Johannes Schindelin | 28e9cf6 | 2009-08-10 16:22:18 | [diff] [blame] | 235 | int has_non_ascii(const char *s) |
| 236 | { |
| 237 | int ch; |
| 238 | if (!s) |
| 239 | return 0; |
| 240 | while ((ch = *s++) != '\0') { |
| 241 | if (non_ascii(ch)) |
| 242 | return 1; |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| Jeff King | 4d03c18 | 2011-04-08 22:40:36 | [diff] [blame] | 247 | static int is_rfc822_special(char ch) |
| 248 | { |
| 249 | switch (ch) { |
| 250 | case '(': |
| 251 | case ')': |
| 252 | case '<': |
| 253 | case '>': |
| 254 | case '[': |
| 255 | case ']': |
| 256 | case ':': |
| 257 | case ';': |
| 258 | case '@': |
| 259 | case ',': |
| 260 | case '.': |
| 261 | case '"': |
| 262 | case '\\': |
| 263 | return 1; |
| 264 | default: |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 269 | static int needs_rfc822_quoting(const char *s, int len) |
| Jeff King | 4d03c18 | 2011-04-08 22:40:36 | [diff] [blame] | 270 | { |
| 271 | int i; |
| 272 | for (i = 0; i < len; i++) |
| 273 | if (is_rfc822_special(s[i])) |
| 274 | return 1; |
| 275 | return 0; |
| 276 | } |
| 277 | |
| Jan H. Schönherr | f9b7204 | 2012-10-18 14:43:31 | [diff] [blame] | 278 | static int last_line_length(struct strbuf *sb) |
| 279 | { |
| 280 | int i; |
| 281 | |
| 282 | /* How many bytes are already used on the last line? */ |
| 283 | for (i = sb->len - 1; i >= 0; i--) |
| 284 | if (sb->buf[i] == '\n') |
| 285 | break; |
| 286 | return sb->len - (i + 1); |
| 287 | } |
| 288 | |
| Jeff King | 4d03c18 | 2011-04-08 22:40:36 | [diff] [blame] | 289 | static void add_rfc822_quoted(struct strbuf *out, const char *s, int len) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | /* just a guess, we may have to also backslash-quote */ |
| 294 | strbuf_grow(out, len + 2); |
| 295 | |
| 296 | strbuf_addch(out, '"'); |
| 297 | for (i = 0; i < len; i++) { |
| 298 | switch (s[i]) { |
| 299 | case '"': |
| 300 | case '\\': |
| 301 | strbuf_addch(out, '\\'); |
| 302 | /* fall through */ |
| 303 | default: |
| 304 | strbuf_addch(out, s[i]); |
| 305 | } |
| 306 | } |
| 307 | strbuf_addch(out, '"'); |
| 308 | } |
| 309 | |
| Jan H. Schönherr | 0fcec2c | 2012-10-18 14:43:32 | [diff] [blame] | 310 | enum rfc2047_type { |
| 311 | RFC2047_SUBJECT, |
| Ronnie Sahlberg | 7827352 | 2014-07-02 18:24:05 | [diff] [blame] | 312 | RFC2047_ADDRESS |
| Jan H. Schönherr | 0fcec2c | 2012-10-18 14:43:32 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | static int is_rfc2047_special(char ch, enum rfc2047_type type) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 316 | { |
| Jan H. Schönherr | 0fcec2c | 2012-10-18 14:43:32 | [diff] [blame] | 317 | /* |
| 318 | * rfc2047, section 4.2: |
| 319 | * |
| 320 | * 8-bit values which correspond to printable ASCII characters other |
| 321 | * than "=", "?", and "_" (underscore), MAY be represented as those |
| 322 | * characters. (But see section 5 for restrictions.) In |
| 323 | * particular, SPACE and TAB MUST NOT be represented as themselves |
| 324 | * within encoded words. |
| 325 | */ |
| 326 | |
| 327 | /* |
| 328 | * rule out non-ASCII characters and non-printable characters (the |
| 329 | * non-ASCII check should be redundant as isprint() is not localized |
| 330 | * and only knows about ASCII, but be defensive about that) |
| 331 | */ |
| 332 | if (non_ascii(ch) || !isprint(ch)) |
| Jan H. Schönherr | 94f6cdf | 2012-10-18 14:43:30 | [diff] [blame] | 333 | return 1; |
| 334 | |
| Jan H. Schönherr | 0fcec2c | 2012-10-18 14:43:32 | [diff] [blame] | 335 | /* |
| 336 | * rule out special printable characters (' ' should be the only |
| 337 | * whitespace character considered printable, but be defensive and use |
| 338 | * isspace()) |
| 339 | */ |
| 340 | if (isspace(ch) || ch == '=' || ch == '?' || ch == '_') |
| 341 | return 1; |
| 342 | |
| 343 | /* |
| 344 | * rfc2047, section 5.3: |
| 345 | * |
| 346 | * As a replacement for a 'word' entity within a 'phrase', for example, |
| 347 | * one that precedes an address in a From, To, or Cc header. The ABNF |
| 348 | * definition for 'phrase' from RFC 822 thus becomes: |
| 349 | * |
| 350 | * phrase = 1*( encoded-word / word ) |
| 351 | * |
| 352 | * In this case the set of characters that may be used in a "Q"-encoded |
| 353 | * 'encoded-word' is restricted to: <upper and lower case ASCII |
| 354 | * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_" |
| 355 | * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a |
| 356 | * 'phrase' MUST be separated from any adjacent 'word', 'text' or |
| 357 | * 'special' by 'linear-white-space'. |
| 358 | */ |
| 359 | |
| 360 | if (type != RFC2047_ADDRESS) |
| 361 | return 0; |
| 362 | |
| 363 | /* '=' and '_' are special cases and have been checked above */ |
| 364 | return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/'); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 365 | } |
| 366 | |
| Jeff King | da55ff3 | 2019-03-20 08:16:36 | [diff] [blame] | 367 | static int needs_rfc2047_encoding(const char *line, int len) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 368 | { |
| Jeff King | a1f6baa | 2011-02-23 09:58:41 | [diff] [blame] | 369 | int i; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 370 | |
| 371 | for (i = 0; i < len; i++) { |
| 372 | int ch = line[i]; |
| Jeff King | c22e7de | 2011-02-23 09:59:18 | [diff] [blame] | 373 | if (non_ascii(ch) || ch == '\n') |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 374 | return 1; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 375 | if ((i + 1 < len) && (ch == '=' && line[i+1] == '?')) |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 376 | return 1; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 377 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 378 | |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
| Kirill Smelkov | 6cd3c05 | 2013-03-07 10:55:07 | [diff] [blame] | 382 | static void add_rfc2047(struct strbuf *sb, const char *line, size_t len, |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 383 | const char *encoding, enum rfc2047_type type) |
| 384 | { |
| 385 | static const int max_encoded_length = 76; /* per rfc2047 */ |
| 386 | int i; |
| 387 | int line_len = last_line_length(sb); |
| 388 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 389 | strbuf_grow(sb, len * 3 + strlen(encoding) + 100); |
| 390 | strbuf_addf(sb, "=?%s?q?", encoding); |
| Jeff King | a1f6baa | 2011-02-23 09:58:41 | [diff] [blame] | 391 | line_len += strlen(encoding) + 5; /* 5 for =??q? */ |
| Kirill Smelkov | 6cd3c05 | 2013-03-07 10:55:07 | [diff] [blame] | 392 | |
| 393 | while (len) { |
| 394 | /* |
| 395 | * RFC 2047, section 5 (3): |
| 396 | * |
| 397 | * Each 'encoded-word' MUST represent an integral number of |
| 398 | * characters. A multi-octet character may not be split across |
| 399 | * adjacent 'encoded- word's. |
| 400 | */ |
| 401 | const unsigned char *p = (const unsigned char *)line; |
| 402 | int chrlen = mbs_chrlen(&line, &len, encoding); |
| 403 | int is_special = (chrlen > 1) || is_rfc2047_special(*p, type); |
| 404 | |
| 405 | /* "=%02X" * chrlen, or the byte itself */ |
| 406 | const char *encoded_fmt = is_special ? "=%02X" : "%c"; |
| 407 | int encoded_len = is_special ? 3 * chrlen : 1; |
| Jeff King | a1f6baa | 2011-02-23 09:58:41 | [diff] [blame] | 408 | |
| Jan H. Schönherr | 94f6cdf | 2012-10-18 14:43:30 | [diff] [blame] | 409 | /* |
| 410 | * According to RFC 2047, we could encode the special character |
| 411 | * ' ' (space) with '_' (underscore) for readability. But many |
| 412 | * programs do not understand this and just leave the |
| 413 | * underscore in place. Thus, we do nothing special here, which |
| 414 | * causes ' ' to be encoded as '=20', avoiding this problem. |
| 415 | */ |
| 416 | |
| Kirill Smelkov | 6cd3c05 | 2013-03-07 10:55:07 | [diff] [blame] | 417 | if (line_len + encoded_len + 2 > max_encoded_length) { |
| 418 | /* It won't fit with trailing "?=" --- break the line */ |
| Jeff King | a1f6baa | 2011-02-23 09:58:41 | [diff] [blame] | 419 | strbuf_addf(sb, "?=\n =?%s?q?", encoding); |
| 420 | line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */ |
| 421 | } |
| 422 | |
| Kirill Smelkov | 6cd3c05 | 2013-03-07 10:55:07 | [diff] [blame] | 423 | for (i = 0; i < chrlen; i++) |
| 424 | strbuf_addf(sb, encoded_fmt, p[i]); |
| 425 | line_len += encoded_len; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 426 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 427 | strbuf_addstr(sb, "?="); |
| 428 | } |
| 429 | |
| Jeff King | d105324 | 2014-05-02 01:07:22 | [diff] [blame] | 430 | const char *show_ident_date(const struct ident_split *ident, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 431 | const struct date_mode *mode) |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 432 | { |
| Johannes Schindelin | dddbad7 | 2017-04-26 19:29:31 | [diff] [blame] | 433 | timestamp_t date = 0; |
| Jeff King | 3f419d4 | 2014-03-07 17:15:01 | [diff] [blame] | 434 | long tz = 0; |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 435 | |
| 436 | if (ident->date_begin && ident->date_end) |
| Johannes Schindelin | 1aeb7e7 | 2017-04-21 10:45:44 | [diff] [blame] | 437 | date = parse_timestamp(ident->date_begin, NULL, 10); |
| Jeff King | 1dca155 | 2014-02-24 07:46:37 | [diff] [blame] | 438 | if (date_overflows(date)) |
| 439 | date = 0; |
| 440 | else { |
| 441 | if (ident->tz_begin && ident->tz_end) |
| 442 | tz = strtol(ident->tz_begin, NULL, 10); |
| Jeff King | 3f419d4 | 2014-03-07 17:15:01 | [diff] [blame] | 443 | if (tz >= INT_MAX || tz <= INT_MIN) |
| Jeff King | 1dca155 | 2014-02-24 07:46:37 | [diff] [blame] | 444 | tz = 0; |
| 445 | } |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 446 | return show_date(date, tz, mode); |
| 447 | } |
| 448 | |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 449 | static inline void strbuf_add_with_color(struct strbuf *sb, const char *color, |
| 450 | const char *buf, size_t buflen) |
| 451 | { |
| 452 | strbuf_addstr(sb, color); |
| 453 | strbuf_add(sb, buf, buflen); |
| 454 | if (*color) |
| 455 | strbuf_addstr(sb, GIT_COLOR_RESET); |
| 456 | } |
| 457 | |
| 458 | static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt, |
| 459 | const char *line, size_t linelen, |
| 460 | int color, enum grep_context ctx, |
| 461 | enum grep_header_field field) |
| 462 | { |
| 463 | const char *buf, *eol, *line_color, *match_color; |
| 464 | regmatch_t match; |
| 465 | int eflags = 0; |
| 466 | |
| 467 | buf = line; |
| 468 | eol = buf + linelen; |
| 469 | |
| 470 | if (!opt || !want_color(color) || opt->invert) |
| 471 | goto end; |
| 472 | |
| 473 | line_color = opt->colors[GREP_COLOR_SELECTED]; |
| 474 | match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; |
| 475 | |
| 476 | while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) { |
| 477 | if (match.rm_so == match.rm_eo) |
| 478 | break; |
| 479 | |
| 480 | strbuf_add_with_color(sb, line_color, buf, match.rm_so); |
| 481 | strbuf_add_with_color(sb, match_color, buf + match.rm_so, |
| 482 | match.rm_eo - match.rm_so); |
| 483 | buf += match.rm_eo; |
| 484 | eflags = REG_NOTBOL; |
| 485 | } |
| 486 | |
| 487 | if (eflags) |
| 488 | strbuf_add_with_color(sb, line_color, buf, eol - buf); |
| 489 | else { |
| 490 | end: |
| 491 | strbuf_add(sb, buf, eol - buf); |
| 492 | } |
| 493 | } |
| 494 | |
| Junio C Hamano | b84d013 | 2022-08-29 21:38:35 | [diff] [blame] | 495 | static int use_in_body_from(const struct pretty_print_context *pp, |
| 496 | const struct ident_split *ident) |
| 497 | { |
| Junio C Hamano | 34bc1b1 | 2022-08-29 21:38:36 | [diff] [blame] | 498 | if (pp->rev && pp->rev->force_in_body_from) |
| 499 | return 1; |
| Junio C Hamano | b84d013 | 2022-08-29 21:38:35 | [diff] [blame] | 500 | if (ident_cmp(pp->from_ident, ident)) |
| 501 | return 1; |
| 502 | return 0; |
| 503 | } |
| 504 | |
| Jeff King | 10f2fbf | 2013-07-03 07:07:48 | [diff] [blame] | 505 | void pp_user_info(struct pretty_print_context *pp, |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 506 | const char *what, struct strbuf *sb, |
| 507 | const char *line, const char *encoding) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 508 | { |
| Antoine Pelisse | 3c020bd | 2013-01-05 21:26:38 | [diff] [blame] | 509 | struct ident_split ident; |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 510 | char *line_end; |
| Antoine Pelisse | dffd325 | 2013-01-05 21:26:42 | [diff] [blame] | 511 | const char *mailbuf, *namebuf; |
| 512 | size_t namelen, maillen; |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 513 | int max_length = 78; /* per rfc2822 */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 514 | |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 515 | if (pp->fmt == CMIT_FMT_ONELINE) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 516 | return; |
| Antoine Pelisse | 3c020bd | 2013-01-05 21:26:38 | [diff] [blame] | 517 | |
| René Scharfe | 30e77bc | 2013-04-25 19:40:25 | [diff] [blame] | 518 | line_end = strchrnul(line, '\n'); |
| 519 | if (split_ident_line(&ident, line, line_end - line)) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 520 | return; |
| Antoine Pelisse | 3c020bd | 2013-01-05 21:26:38 | [diff] [blame] | 521 | |
| Antoine Pelisse | dffd325 | 2013-01-05 21:26:42 | [diff] [blame] | 522 | mailbuf = ident.mail_begin; |
| 523 | maillen = ident.mail_end - ident.mail_begin; |
| 524 | namebuf = ident.name_begin; |
| 525 | namelen = ident.name_end - ident.name_begin; |
| 526 | |
| 527 | if (pp->mailmap) |
| 528 | map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen); |
| 529 | |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 530 | if (cmit_fmt_is_mail(pp->fmt)) { |
| Junio C Hamano | b84d013 | 2022-08-29 21:38:35 | [diff] [blame] | 531 | if (pp->from_ident && use_in_body_from(pp, &ident)) { |
| Jeff King | a908047 | 2013-07-03 07:08:22 | [diff] [blame] | 532 | struct strbuf buf = STRBUF_INIT; |
| 533 | |
| 534 | strbuf_addstr(&buf, "From: "); |
| 535 | strbuf_add(&buf, namebuf, namelen); |
| 536 | strbuf_addstr(&buf, " <"); |
| 537 | strbuf_add(&buf, mailbuf, maillen); |
| 538 | strbuf_addstr(&buf, ">\n"); |
| 539 | string_list_append(&pp->in_body_headers, |
| 540 | strbuf_detach(&buf, NULL)); |
| 541 | |
| 542 | mailbuf = pp->from_ident->mail_begin; |
| 543 | maillen = pp->from_ident->mail_end - mailbuf; |
| 544 | namebuf = pp->from_ident->name_begin; |
| 545 | namelen = pp->from_ident->name_end - namebuf; |
| 546 | } |
| 547 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 548 | strbuf_addstr(sb, "From: "); |
| Emma Brooks | 19d097e | 2020-04-08 04:31:38 | [diff] [blame] | 549 | if (pp->encode_email_headers && |
| 550 | needs_rfc2047_encoding(namebuf, namelen)) { |
| René Scharfe | a0511b3 | 2013-04-25 19:43:56 | [diff] [blame] | 551 | add_rfc2047(sb, namebuf, namelen, |
| Antoine Pelisse | dffd325 | 2013-01-05 21:26:42 | [diff] [blame] | 552 | encoding, RFC2047_ADDRESS); |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 553 | max_length = 76; /* per rfc2047 */ |
| René Scharfe | a0511b3 | 2013-04-25 19:43:56 | [diff] [blame] | 554 | } else if (needs_rfc822_quoting(namebuf, namelen)) { |
| Jeff King | 4d03c18 | 2011-04-08 22:40:36 | [diff] [blame] | 555 | struct strbuf quoted = STRBUF_INIT; |
| René Scharfe | a0511b3 | 2013-04-25 19:43:56 | [diff] [blame] | 556 | add_rfc822_quoted("ed, namebuf, namelen); |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 557 | strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len, |
| 558 | -6, 1, max_length); |
| Jeff King | 4d03c18 | 2011-04-08 22:40:36 | [diff] [blame] | 559 | strbuf_release("ed); |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 560 | } else { |
| René Scharfe | a0511b3 | 2013-04-25 19:43:56 | [diff] [blame] | 561 | strbuf_add_wrapped_bytes(sb, namebuf, namelen, |
| Antoine Pelisse | dffd325 | 2013-01-05 21:26:42 | [diff] [blame] | 562 | -6, 1, max_length); |
| Jeff King | 4d03c18 | 2011-04-08 22:40:36 | [diff] [blame] | 563 | } |
| Antoine Pelisse | dffd325 | 2013-01-05 21:26:42 | [diff] [blame] | 564 | |
| René Scharfe | 97a17e7 | 2013-04-25 19:41:57 | [diff] [blame] | 565 | if (max_length < |
| 566 | last_line_length(sb) + strlen(" <") + maillen + strlen(">")) |
| Jeff King | 990f6e3 | 2011-04-14 22:18:09 | [diff] [blame] | 567 | strbuf_addch(sb, '\n'); |
| René Scharfe | a0511b3 | 2013-04-25 19:43:56 | [diff] [blame] | 568 | strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 569 | } else { |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 570 | struct strbuf id = STRBUF_INIT; |
| 571 | enum grep_header_field field = GREP_HEADER_FIELD_MAX; |
| 572 | struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL; |
| 573 | |
| 574 | if (!strcmp(what, "Author")) |
| 575 | field = GREP_HEADER_AUTHOR; |
| 576 | else if (!strcmp(what, "Commit")) |
| 577 | field = GREP_HEADER_COMMITTER; |
| 578 | |
| 579 | strbuf_addf(sb, "%s: ", what); |
| 580 | if (pp->fmt == CMIT_FMT_FULLER) |
| 581 | strbuf_addchars(sb, ' ', 4); |
| 582 | |
| 583 | strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf, |
| 584 | (int)maillen, mailbuf); |
| 585 | |
| 586 | append_line_with_color(sb, opt, id.buf, id.len, pp->color, |
| 587 | GREP_CONTEXT_HEAD, field); |
| 588 | strbuf_addch(sb, '\n'); |
| 589 | strbuf_release(&id); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 590 | } |
| Antoine Pelisse | dffd325 | 2013-01-05 21:26:42 | [diff] [blame] | 591 | |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 592 | switch (pp->fmt) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 593 | case CMIT_FMT_MEDIUM: |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 594 | strbuf_addf(sb, "Date: %s\n", |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 595 | show_ident_date(&ident, &pp->date_mode)); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 596 | break; |
| 597 | case CMIT_FMT_EMAIL: |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 598 | case CMIT_FMT_MBOXRD: |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 599 | strbuf_addf(sb, "Date: %s\n", |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 600 | show_ident_date(&ident, DATE_MODE(RFC2822))); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 601 | break; |
| 602 | case CMIT_FMT_FULLER: |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 603 | strbuf_addf(sb, "%sDate: %s\n", what, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 604 | show_ident_date(&ident, &pp->date_mode)); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 605 | break; |
| 606 | default: |
| 607 | /* notin' */ |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 612 | static int is_blank_line(const char *line, int *len_p) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 613 | { |
| 614 | int len = *len_p; |
| Felipe Contreras | 35b2fa5 | 2013-10-31 09:25:42 | [diff] [blame] | 615 | while (len && isspace(line[len - 1])) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 616 | len--; |
| 617 | *len_p = len; |
| 618 | return !len; |
| 619 | } |
| 620 | |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 621 | const char *skip_blank_lines(const char *msg) |
| René Scharfe | a010966 | 2008-12-27 00:32:49 | [diff] [blame] | 622 | { |
| 623 | for (;;) { |
| 624 | int linelen = get_one_line(msg); |
| 625 | int ll = linelen; |
| 626 | if (!linelen) |
| 627 | break; |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 628 | if (!is_blank_line(msg, &ll)) |
| René Scharfe | a010966 | 2008-12-27 00:32:49 | [diff] [blame] | 629 | break; |
| 630 | msg += linelen; |
| 631 | } |
| 632 | return msg; |
| 633 | } |
| 634 | |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 635 | static void add_merge_info(const struct pretty_print_context *pp, |
| 636 | struct strbuf *sb, const struct commit *commit) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 637 | { |
| 638 | struct commit_list *parent = commit->parents; |
| 639 | |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 640 | if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) || |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 641 | !parent || !parent->next) |
| 642 | return; |
| 643 | |
| 644 | strbuf_addstr(sb, "Merge:"); |
| 645 | |
| 646 | while (parent) { |
| René Scharfe | a94bb68 | 2016-10-08 15:38:47 | [diff] [blame] | 647 | struct object_id *oidp = &parent->item->object.oid; |
| 648 | strbuf_addch(sb, ' '); |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 649 | if (pp->abbrev) |
| brian m. carlson | 30e677e | 2018-03-12 02:27:28 | [diff] [blame] | 650 | strbuf_add_unique_abbrev(sb, oidp, pp->abbrev); |
| René Scharfe | a94bb68 | 2016-10-08 15:38:47 | [diff] [blame] | 651 | else |
| 652 | strbuf_addstr(sb, oid_to_hex(oidp)); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 653 | parent = parent->next; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 654 | } |
| 655 | strbuf_addch(sb, '\n'); |
| 656 | } |
| 657 | |
| Jeff King | fe6eb7f | 2014-08-27 07:56:01 | [diff] [blame] | 658 | static char *get_header(const char *msg, const char *key) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 659 | { |
| Jeff King | fe6eb7f | 2014-08-27 07:56:01 | [diff] [blame] | 660 | size_t len; |
| 661 | const char *v = find_commit_header(msg, key, &len); |
| 662 | return v ? xmemdupz(v, len) : NULL; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | static char *replace_encoding_header(char *buf, const char *encoding) |
| 666 | { |
| Brandon Casey | f285a2d | 2008-10-09 19:12:12 | [diff] [blame] | 667 | struct strbuf tmp = STRBUF_INIT; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 668 | size_t start, len; |
| 669 | char *cp = buf; |
| 670 | |
| 671 | /* guess if there is an encoding header before a \n\n */ |
| René Scharfe | 68d6d6e | 2015-02-21 19:53:09 | [diff] [blame] | 672 | while (!starts_with(cp, "encoding ")) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 673 | cp = strchr(cp, '\n'); |
| 674 | if (!cp || *++cp == '\n') |
| 675 | return buf; |
| 676 | } |
| 677 | start = cp - buf; |
| 678 | cp = strchr(cp, '\n'); |
| 679 | if (!cp) |
| 680 | return buf; /* should not happen but be defensive */ |
| 681 | len = cp + 1 - (buf + start); |
| 682 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 683 | strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); |
| 684 | if (is_encoding_utf8(encoding)) { |
| 685 | /* we have re-coded to UTF-8; drop the header */ |
| 686 | strbuf_remove(&tmp, start, len); |
| 687 | } else { |
| 688 | /* just replaces XXXX in 'encoding XXXX\n' */ |
| 689 | strbuf_splice(&tmp, start + strlen("encoding "), |
| 690 | len - strlen("encoding \n"), |
| 691 | encoding, strlen(encoding)); |
| 692 | } |
| 693 | return strbuf_detach(&tmp, NULL); |
| 694 | } |
| 695 | |
| Stefan Beller | 424510e | 2018-11-14 00:12:59 | [diff] [blame] | 696 | const char *repo_logmsg_reencode(struct repository *r, |
| 697 | const struct commit *commit, |
| 698 | char **commit_encoding, |
| 699 | const char *output_encoding) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 700 | { |
| Brandon Casey | 330db18 | 2009-05-18 23:44:39 | [diff] [blame] | 701 | static const char *utf8 = "UTF-8"; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 702 | const char *use_encoding; |
| 703 | char *encoding; |
| Stefan Beller | 424510e | 2018-11-14 00:12:59 | [diff] [blame] | 704 | const char *msg = repo_get_commit_buffer(r, commit, NULL); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 705 | char *out; |
| 706 | |
| Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-18 23:08:40 | [diff] [blame] | 707 | if (!output_encoding || !*output_encoding) { |
| 708 | if (commit_encoding) |
| Jeff King | fe6eb7f | 2014-08-27 07:56:01 | [diff] [blame] | 709 | *commit_encoding = get_header(msg, "encoding"); |
| Jeff King | dd0d388 | 2013-01-26 09:44:06 | [diff] [blame] | 710 | return msg; |
| Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-18 23:08:40 | [diff] [blame] | 711 | } |
| Jeff King | fe6eb7f | 2014-08-27 07:56:01 | [diff] [blame] | 712 | encoding = get_header(msg, "encoding"); |
| Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-18 23:08:40 | [diff] [blame] | 713 | if (commit_encoding) |
| 714 | *commit_encoding = encoding; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 715 | use_encoding = encoding ? encoding : utf8; |
| Jeff King | be5c9fb | 2013-01-26 09:44:28 | [diff] [blame] | 716 | if (same_encoding(use_encoding, output_encoding)) { |
| 717 | /* |
| 718 | * No encoding work to be done. If we have no encoding header |
| 719 | * at all, then there's nothing to do, and we can return the |
| 720 | * message verbatim (whether newly allocated or not). |
| 721 | */ |
| 722 | if (!encoding) |
| 723 | return msg; |
| 724 | |
| 725 | /* |
| 726 | * Otherwise, we still want to munge the encoding header in the |
| 727 | * result, which will be done by modifying the buffer. If we |
| 728 | * are using a fresh copy, we can reuse it. But if we are using |
| Ævar Arnfjörð Bjarmason | c7c33f5 | 2023-03-28 13:58:57 | [diff] [blame] | 729 | * the cached copy from repo_get_commit_buffer, we need to duplicate it |
| Jeff King | b66103c | 2014-06-10 21:41:39 | [diff] [blame] | 730 | * to avoid munging the cached copy. |
| Jeff King | be5c9fb | 2013-01-26 09:44:28 | [diff] [blame] | 731 | */ |
| Stefan Beller | 424510e | 2018-11-14 00:12:59 | [diff] [blame] | 732 | if (msg == get_cached_commit_buffer(r, commit, NULL)) |
| Jeff King | b66103c | 2014-06-10 21:41:39 | [diff] [blame] | 733 | out = xstrdup(msg); |
| 734 | else |
| 735 | out = (char *)msg; |
| Jeff King | be5c9fb | 2013-01-26 09:44:28 | [diff] [blame] | 736 | } |
| 737 | else { |
| 738 | /* |
| 739 | * There's actual encoding work to do. Do the reencoding, which |
| 740 | * still leaves the header to be replaced in the next step. At |
| 741 | * this point, we are done with msg. If we allocated a fresh |
| 742 | * copy, we can free it. |
| 743 | */ |
| 744 | out = reencode_string(msg, output_encoding, use_encoding); |
| Jeff King | b66103c | 2014-06-10 21:41:39 | [diff] [blame] | 745 | if (out) |
| Stefan Beller | 424510e | 2018-11-14 00:12:59 | [diff] [blame] | 746 | repo_unuse_commit_buffer(r, commit, msg); |
| Jeff King | be5c9fb | 2013-01-26 09:44:28 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* |
| 750 | * This replacement actually consumes the buffer we hand it, so we do |
| 751 | * not have to worry about freeing the old "out" here. |
| 752 | */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 753 | if (out) |
| 754 | out = replace_encoding_header(out, output_encoding); |
| 755 | |
| Nguyễn Thái Ngọc Duy | 5a10d23 | 2013-04-18 23:08:40 | [diff] [blame] | 756 | if (!commit_encoding) |
| 757 | free(encoding); |
| Jeff King | dd0d388 | 2013-01-26 09:44:06 | [diff] [blame] | 758 | /* |
| 759 | * If the re-encoding failed, out might be NULL here; in that |
| 760 | * case we just return the commit message verbatim. |
| 761 | */ |
| Junio C Hamano | 0988e66 | 2021-10-29 20:48:58 | [diff] [blame] | 762 | return out ? out : msg; |
| Jeff King | dd0d388 | 2013-01-26 09:44:06 | [diff] [blame] | 763 | } |
| 764 | |
| Antoine Pelisse | ea02ffa | 2013-01-05 21:26:40 | [diff] [blame] | 765 | static int mailmap_name(const char **email, size_t *email_len, |
| 766 | const char **name, size_t *name_len) |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 767 | { |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 768 | static struct string_list *mail_map; |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 769 | if (!mail_map) { |
| René Scharfe | ca56dad | 2021-03-13 16:17:22 | [diff] [blame] | 770 | CALLOC_ARRAY(mail_map, 1); |
| Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 20:18:06 | [diff] [blame] | 771 | read_mailmap(mail_map); |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 772 | } |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 773 | return mail_map->nr && map_user(mail_map, email, email_len, name, name_len); |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 774 | } |
| 775 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 776 | static size_t format_person_part(struct strbuf *sb, char part, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 777 | const char *msg, int len, |
| 778 | const struct date_mode *dmode) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 779 | { |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 780 | /* currently all placeholders have same length */ |
| 781 | const int placeholder_len = 2; |
| Junio C Hamano | 4b340cf | 2012-03-11 09:25:43 | [diff] [blame] | 782 | struct ident_split s; |
| Antoine Pelisse | ea02ffa | 2013-01-05 21:26:40 | [diff] [blame] | 783 | const char *name, *mail; |
| 784 | size_t maillen, namelen; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 785 | |
| Junio C Hamano | 4b340cf | 2012-03-11 09:25:43 | [diff] [blame] | 786 | if (split_ident_line(&s, msg, len) < 0) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 787 | goto skip; |
| 788 | |
| Antoine Pelisse | ea02ffa | 2013-01-05 21:26:40 | [diff] [blame] | 789 | name = s.name_begin; |
| 790 | namelen = s.name_end - s.name_begin; |
| 791 | mail = s.mail_begin; |
| 792 | maillen = s.mail_end - s.mail_begin; |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 793 | |
| Prarit Bhargava | d8b8217 | 2019-10-29 12:09:14 | [diff] [blame] | 794 | if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */ |
| Antoine Pelisse | ea02ffa | 2013-01-05 21:26:40 | [diff] [blame] | 795 | mailmap_name(&mail, &maillen, &name, &namelen); |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 796 | if (part == 'n' || part == 'N') { /* name */ |
| Antoine Pelisse | ea02ffa | 2013-01-05 21:26:40 | [diff] [blame] | 797 | strbuf_add(sb, name, namelen); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 798 | return placeholder_len; |
| 799 | } |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 800 | if (part == 'e' || part == 'E') { /* email */ |
| Antoine Pelisse | ea02ffa | 2013-01-05 21:26:40 | [diff] [blame] | 801 | strbuf_add(sb, mail, maillen); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 802 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 803 | } |
| Prarit Bhargava | d8b8217 | 2019-10-29 12:09:14 | [diff] [blame] | 804 | if (part == 'l' || part == 'L') { /* local-part */ |
| 805 | const char *at = memchr(mail, '@', maillen); |
| 806 | if (at) |
| 807 | maillen = at - mail; |
| 808 | strbuf_add(sb, mail, maillen); |
| 809 | return placeholder_len; |
| 810 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 811 | |
| Junio C Hamano | 4b340cf | 2012-03-11 09:25:43 | [diff] [blame] | 812 | if (!s.date_begin) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 813 | goto skip; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 814 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 815 | if (part == 't') { /* date, UNIX timestamp */ |
| Junio C Hamano | 4b340cf | 2012-03-11 09:25:43 | [diff] [blame] | 816 | strbuf_add(sb, s.date_begin, s.date_end - s.date_begin); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 817 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 818 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 819 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 820 | switch (part) { |
| 821 | case 'd': /* date */ |
| René Scharfe | 9dbe7c3 | 2013-04-17 18:33:35 | [diff] [blame] | 822 | strbuf_addstr(sb, show_ident_date(&s, dmode)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 823 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 824 | case 'D': /* date, RFC2822 style */ |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 825 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822))); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 826 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 827 | case 'r': /* date, relative */ |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 828 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE))); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 829 | return placeholder_len; |
| Beat Bolli | 466fb67 | 2014-08-29 16:58:42 | [diff] [blame] | 830 | case 'i': /* date, ISO 8601-like */ |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 831 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601))); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 832 | return placeholder_len; |
| Beat Bolli | 466fb67 | 2014-08-29 16:58:42 | [diff] [blame] | 833 | case 'I': /* date, ISO 8601 strict */ |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 834 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT))); |
| Beat Bolli | 466fb67 | 2014-08-29 16:58:42 | [diff] [blame] | 835 | return placeholder_len; |
| ZheNing Hu | b722d45 | 2021-04-25 10:41:45 | [diff] [blame] | 836 | case 'h': /* date, human */ |
| 837 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(HUMAN))); |
| 838 | return placeholder_len; |
| René Scharfe | 0df6211 | 2019-11-20 00:51:21 | [diff] [blame] | 839 | case 's': |
| 840 | strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT))); |
| 841 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 842 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 843 | |
| 844 | skip: |
| 845 | /* |
| Junio C Hamano | 4b340cf | 2012-03-11 09:25:43 | [diff] [blame] | 846 | * reading from either a bogus commit, or a reflog entry with |
| 847 | * %gn, %ge, etc.; 'sb' cannot be updated, but we still need |
| 848 | * to compute a valid return value. |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 849 | */ |
| 850 | if (part == 'n' || part == 'e' || part == 't' || part == 'd' |
| 851 | || part == 'D' || part == 'r' || part == 'i') |
| 852 | return placeholder_len; |
| 853 | |
| 854 | return 0; /* unknown placeholder */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 855 | } |
| 856 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 857 | struct chunk { |
| 858 | size_t off; |
| 859 | size_t len; |
| 860 | }; |
| 861 | |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 862 | enum flush_type { |
| 863 | no_flush, |
| 864 | flush_right, |
| 865 | flush_left, |
| Nguyễn Thái Ngọc Duy | 1640632 | 2013-04-18 23:08:52 | [diff] [blame] | 866 | flush_left_and_steal, |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 867 | flush_both |
| 868 | }; |
| 869 | |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 870 | enum trunc_type { |
| 871 | trunc_none, |
| 872 | trunc_left, |
| 873 | trunc_middle, |
| 874 | trunc_right |
| 875 | }; |
| 876 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 877 | struct format_commit_context { |
| Jeff King | 018b9de | 2021-01-28 19:57:39 | [diff] [blame] | 878 | struct repository *repository; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 879 | const struct commit *commit; |
| Thomas Rast | dd2e794 | 2009-10-19 15:48:08 | [diff] [blame] | 880 | const struct pretty_print_context *pretty_ctx; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 881 | unsigned commit_header_parsed:1; |
| 882 | unsigned commit_message_parsed:1; |
| Sebastian Götte | ffb6d7d | 2013-03-31 16:00:14 | [diff] [blame] | 883 | struct signature_check signature_check; |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 884 | enum flush_type flush_type; |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 885 | enum trunc_type truncate; |
| Jeff King | b000c59 | 2014-06-10 21:39:30 | [diff] [blame] | 886 | const char *message; |
| Nguyễn Thái Ngọc Duy | 0940a76 | 2013-04-18 23:08:41 | [diff] [blame] | 887 | char *commit_encoding; |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 888 | size_t width, indent1, indent2; |
| Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-18 23:08:49 | [diff] [blame] | 889 | int auto_color; |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 890 | int padding; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 891 | |
| 892 | /* These offsets are relative to the start of the commit message. */ |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 893 | struct chunk author; |
| 894 | struct chunk committer; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 895 | size_t message_off; |
| 896 | size_t subject_off; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 897 | size_t body_off; |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 898 | |
| 899 | /* The following ones are relative to the result struct strbuf. */ |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 900 | size_t wrap_start; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 901 | }; |
| 902 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 903 | static void parse_commit_header(struct format_commit_context *context) |
| 904 | { |
| Pat Notz | 177b29d | 2010-11-02 19:59:08 | [diff] [blame] | 905 | const char *msg = context->message; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 906 | int i; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 907 | |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 908 | for (i = 0; msg[i]; i++) { |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 909 | const char *name; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 910 | int eol; |
| 911 | for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) |
| 912 | ; /* do nothing */ |
| 913 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 914 | if (i == eol) { |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 915 | break; |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 916 | } else if (skip_prefix(msg + i, "author ", &name)) { |
| 917 | context->author.off = name - msg; |
| 918 | context->author.len = msg + eol - name; |
| 919 | } else if (skip_prefix(msg + i, "committer ", &name)) { |
| 920 | context->committer.off = name - msg; |
| 921 | context->committer.len = msg + eol - name; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 922 | } |
| 923 | i = eol; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 924 | } |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 925 | context->message_off = i; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 926 | context->commit_header_parsed = 1; |
| 927 | } |
| 928 | |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 929 | static int istitlechar(char c) |
| 930 | { |
| 931 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 932 | (c >= '0' && c <= '9') || c == '.' || c == '_'; |
| 933 | } |
| 934 | |
| Hariom Verma | 47d4676 | 2020-08-21 21:41:49 | [diff] [blame] | 935 | void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len) |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 936 | { |
| 937 | size_t trimlen; |
| Stephen Boyd | 871d21d | 2009-03-31 23:24:38 | [diff] [blame] | 938 | size_t start_len = sb->len; |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 939 | int space = 2; |
| Hariom Verma | 47d4676 | 2020-08-21 21:41:49 | [diff] [blame] | 940 | int i; |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 941 | |
| Hariom Verma | 47d4676 | 2020-08-21 21:41:49 | [diff] [blame] | 942 | for (i = 0; i < len; i++) { |
| 943 | if (istitlechar(msg[i])) { |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 944 | if (space == 1) |
| 945 | strbuf_addch(sb, '-'); |
| 946 | space = 0; |
| Hariom Verma | 47d4676 | 2020-08-21 21:41:49 | [diff] [blame] | 947 | strbuf_addch(sb, msg[i]); |
| 948 | if (msg[i] == '.') |
| 949 | while (msg[i+1] == '.') |
| 950 | i++; |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 951 | } else |
| 952 | space |= 1; |
| 953 | } |
| 954 | |
| 955 | /* trim any trailing '.' or '-' characters */ |
| 956 | trimlen = 0; |
| Stephen Boyd | 871d21d | 2009-03-31 23:24:38 | [diff] [blame] | 957 | while (sb->len - trimlen > start_len && |
| 958 | (sb->buf[sb->len - 1 - trimlen] == '.' |
| 959 | || sb->buf[sb->len - 1 - trimlen] == '-')) |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 960 | trimlen++; |
| 961 | strbuf_remove(sb, sb->len - trimlen, trimlen); |
| 962 | } |
| 963 | |
| René Scharfe | cec0871 | 2009-01-06 20:41:06 | [diff] [blame] | 964 | const char *format_subject(struct strbuf *sb, const char *msg, |
| 965 | const char *line_separator) |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 966 | { |
| 967 | int first = 1; |
| 968 | |
| 969 | for (;;) { |
| 970 | const char *line = msg; |
| 971 | int linelen = get_one_line(line); |
| 972 | |
| 973 | msg += linelen; |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 974 | if (!linelen || is_blank_line(line, &linelen)) |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 975 | break; |
| 976 | |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 977 | if (!sb) |
| 978 | continue; |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 979 | strbuf_grow(sb, linelen + 2); |
| 980 | if (!first) |
| 981 | strbuf_addstr(sb, line_separator); |
| 982 | strbuf_add(sb, line, linelen); |
| 983 | first = 0; |
| 984 | } |
| 985 | return msg; |
| 986 | } |
| 987 | |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 988 | static void parse_commit_message(struct format_commit_context *c) |
| 989 | { |
| Pat Notz | 177b29d | 2010-11-02 19:59:08 | [diff] [blame] | 990 | const char *msg = c->message + c->message_off; |
| 991 | const char *start = c->message; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 992 | |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 993 | msg = skip_blank_lines(msg); |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 994 | c->subject_off = msg - start; |
| 995 | |
| 996 | msg = format_subject(NULL, msg, NULL); |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 997 | msg = skip_blank_lines(msg); |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 998 | c->body_off = msg - start; |
| 999 | |
| 1000 | c->commit_message_parsed = 1; |
| 1001 | } |
| 1002 | |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 1003 | static void strbuf_wrap(struct strbuf *sb, size_t pos, |
| 1004 | size_t width, size_t indent1, size_t indent2) |
| 1005 | { |
| 1006 | struct strbuf tmp = STRBUF_INIT; |
| 1007 | |
| 1008 | if (pos) |
| 1009 | strbuf_add(&tmp, sb->buf, pos); |
| 1010 | strbuf_add_wrapped_text(&tmp, sb->buf + pos, |
| Patrick Steinhardt | 48050c4 | 2022-12-01 14:46:49 | [diff] [blame] | 1011 | cast_size_t_to_int(indent1), |
| 1012 | cast_size_t_to_int(indent2), |
| 1013 | cast_size_t_to_int(width)); |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 1014 | strbuf_swap(&tmp, sb); |
| 1015 | strbuf_release(&tmp); |
| 1016 | } |
| 1017 | |
| 1018 | static void rewrap_message_tail(struct strbuf *sb, |
| 1019 | struct format_commit_context *c, |
| 1020 | size_t new_width, size_t new_indent1, |
| 1021 | size_t new_indent2) |
| 1022 | { |
| 1023 | if (c->width == new_width && c->indent1 == new_indent1 && |
| 1024 | c->indent2 == new_indent2) |
| 1025 | return; |
| René Scharfe | 32ca424 | 2009-11-08 01:04:21 | [diff] [blame] | 1026 | if (c->wrap_start < sb->len) |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 1027 | strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2); |
| 1028 | c->wrap_start = sb->len; |
| 1029 | c->width = new_width; |
| 1030 | c->indent1 = new_indent1; |
| 1031 | c->indent2 = new_indent2; |
| 1032 | } |
| 1033 | |
| Jeff King | cd1957f | 2011-12-16 11:40:24 | [diff] [blame] | 1034 | static int format_reflog_person(struct strbuf *sb, |
| 1035 | char part, |
| 1036 | struct reflog_walk_info *log, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 1037 | const struct date_mode *dmode) |
| Jeff King | cd1957f | 2011-12-16 11:40:24 | [diff] [blame] | 1038 | { |
| 1039 | const char *ident; |
| 1040 | |
| 1041 | if (!log) |
| 1042 | return 2; |
| 1043 | |
| 1044 | ident = get_reflog_ident(log); |
| 1045 | if (!ident) |
| 1046 | return 2; |
| 1047 | |
| 1048 | return format_person_part(sb, part, ident, strlen(ident), dmode); |
| 1049 | } |
| 1050 | |
| Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-18 23:08:48 | [diff] [blame] | 1051 | static size_t parse_color(struct strbuf *sb, /* in UTF-8 */ |
| 1052 | const char *placeholder, |
| 1053 | struct format_commit_context *c) |
| 1054 | { |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1055 | const char *rest = placeholder; |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1056 | const char *basic_color = NULL; |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1057 | |
| Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-18 23:08:48 | [diff] [blame] | 1058 | if (placeholder[1] == '(') { |
| 1059 | const char *begin = placeholder + 2; |
| 1060 | const char *end = strchr(begin, ')'); |
| 1061 | char color[COLOR_MAXLEN]; |
| 1062 | |
| 1063 | if (!end) |
| 1064 | return 0; |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1065 | |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1066 | if (skip_prefix(begin, "auto,", &begin)) { |
| Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-18 23:08:48 | [diff] [blame] | 1067 | if (!want_color(c->pretty_ctx->color)) |
| 1068 | return end - placeholder + 1; |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1069 | } else if (skip_prefix(begin, "always,", &begin)) { |
| 1070 | /* nothing to do; we do not respect want_color at all */ |
| 1071 | } else { |
| 1072 | /* the default is the same as "auto" */ |
| 1073 | if (!want_color(c->pretty_ctx->color)) |
| 1074 | return end - placeholder + 1; |
| Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-18 23:08:48 | [diff] [blame] | 1075 | } |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1076 | |
| Jeff King | f6c5a29 | 2014-10-07 19:33:09 | [diff] [blame] | 1077 | if (color_parse_mem(begin, end - begin, color) < 0) |
| 1078 | die(_("unable to parse --pretty format")); |
| Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-18 23:08:48 | [diff] [blame] | 1079 | strbuf_addstr(sb, color); |
| 1080 | return end - placeholder + 1; |
| 1081 | } |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * We handle things like "%C(red)" above; for historical reasons, there |
| 1085 | * are a few colors that can be specified without parentheses (and |
| 1086 | * they cannot support things like "auto" or "always" at all). |
| 1087 | */ |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1088 | if (skip_prefix(placeholder + 1, "red", &rest)) |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1089 | basic_color = GIT_COLOR_RED; |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1090 | else if (skip_prefix(placeholder + 1, "green", &rest)) |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1091 | basic_color = GIT_COLOR_GREEN; |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1092 | else if (skip_prefix(placeholder + 1, "blue", &rest)) |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1093 | basic_color = GIT_COLOR_BLUE; |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1094 | else if (skip_prefix(placeholder + 1, "reset", &rest)) |
| Jeff King | 18fb7ff | 2017-07-13 15:08:46 | [diff] [blame] | 1095 | basic_color = GIT_COLOR_RESET; |
| 1096 | |
| 1097 | if (basic_color && want_color(c->pretty_ctx->color)) |
| 1098 | strbuf_addstr(sb, basic_color); |
| 1099 | |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 1100 | return rest - placeholder; |
| Nguyễn Thái Ngọc Duy | fcabc2d | 2013-04-18 23:08:48 | [diff] [blame] | 1101 | } |
| 1102 | |
| Jeff King | 9a1180f | 2019-03-20 08:16:42 | [diff] [blame] | 1103 | static size_t parse_padding_placeholder(const char *placeholder, |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1104 | struct format_commit_context *c) |
| 1105 | { |
| 1106 | const char *ch = placeholder; |
| 1107 | enum flush_type flush_type; |
| 1108 | int to_column = 0; |
| 1109 | |
| 1110 | switch (*ch++) { |
| 1111 | case '<': |
| 1112 | flush_type = flush_right; |
| 1113 | break; |
| 1114 | case '>': |
| 1115 | if (*ch == '<') { |
| 1116 | flush_type = flush_both; |
| 1117 | ch++; |
| Nguyễn Thái Ngọc Duy | 1640632 | 2013-04-18 23:08:52 | [diff] [blame] | 1118 | } else if (*ch == '>') { |
| 1119 | flush_type = flush_left_and_steal; |
| 1120 | ch++; |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1121 | } else |
| 1122 | flush_type = flush_left; |
| 1123 | break; |
| 1124 | default: |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | /* the next value means "wide enough to that column" */ |
| 1129 | if (*ch == '|') { |
| 1130 | to_column = 1; |
| 1131 | ch++; |
| 1132 | } |
| 1133 | |
| 1134 | if (*ch == '(') { |
| 1135 | const char *start = ch + 1; |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1136 | const char *end = start + strcspn(start, ",)"); |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1137 | char *next; |
| 1138 | int width; |
| Patrick Steinhardt | f6e0b9f | 2022-12-01 14:46:34 | [diff] [blame] | 1139 | if (!*end || end == start) |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1140 | return 0; |
| Nguyễn Thái Ngọc Duy | 066790d | 2016-06-16 13:18:38 | [diff] [blame] | 1141 | width = strtol(start, &next, 10); |
| Patrick Steinhardt | 304a50a | 2022-12-01 14:47:23 | [diff] [blame] | 1142 | |
| 1143 | /* |
| 1144 | * We need to limit the amount of padding, or otherwise this |
| 1145 | * would allow the user to pad the buffer by arbitrarily many |
| 1146 | * bytes and thus cause resource exhaustion. |
| 1147 | */ |
| 1148 | if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT) |
| 1149 | return 0; |
| 1150 | |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1151 | if (next == start || width == 0) |
| 1152 | return 0; |
| Nguyễn Thái Ngọc Duy | 066790d | 2016-06-16 13:18:38 | [diff] [blame] | 1153 | if (width < 0) { |
| 1154 | if (to_column) |
| 1155 | width += term_columns(); |
| 1156 | if (width < 0) |
| 1157 | return 0; |
| 1158 | } |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1159 | c->padding = to_column ? -width : width; |
| 1160 | c->flush_type = flush_type; |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1161 | |
| 1162 | if (*end == ',') { |
| 1163 | start = end + 1; |
| 1164 | end = strchr(start, ')'); |
| 1165 | if (!end || end == start) |
| 1166 | return 0; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1167 | if (starts_with(start, "trunc)")) |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1168 | c->truncate = trunc_right; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1169 | else if (starts_with(start, "ltrunc)")) |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1170 | c->truncate = trunc_left; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1171 | else if (starts_with(start, "mtrunc)")) |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1172 | c->truncate = trunc_middle; |
| 1173 | else |
| 1174 | return 0; |
| 1175 | } else |
| 1176 | c->truncate = trunc_none; |
| 1177 | |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1178 | return end - placeholder + 1; |
| 1179 | } |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| Anders Waldenborg | 4f732e0 | 2019-01-29 06:49:00 | [diff] [blame] | 1183 | static int match_placeholder_arg_value(const char *to_parse, const char *candidate, |
| 1184 | const char **end, const char **valuestart, |
| 1185 | size_t *valuelen) |
| Taylor Blau | 84ff053 | 2017-10-01 16:18:47 | [diff] [blame] | 1186 | { |
| 1187 | const char *p; |
| 1188 | |
| 1189 | if (!(skip_prefix(to_parse, candidate, &p))) |
| 1190 | return 0; |
| Anders Waldenborg | 4f732e0 | 2019-01-29 06:49:00 | [diff] [blame] | 1191 | if (valuestart) { |
| 1192 | if (*p == '=') { |
| 1193 | *valuestart = p + 1; |
| 1194 | *valuelen = strcspn(*valuestart, ",)"); |
| 1195 | p = *valuestart + *valuelen; |
| 1196 | } else { |
| 1197 | if (*p != ',' && *p != ')') |
| 1198 | return 0; |
| 1199 | *valuestart = NULL; |
| 1200 | *valuelen = 0; |
| 1201 | } |
| 1202 | } |
| Taylor Blau | 84ff053 | 2017-10-01 16:18:47 | [diff] [blame] | 1203 | if (*p == ',') { |
| 1204 | *end = p + 1; |
| 1205 | return 1; |
| 1206 | } |
| 1207 | if (*p == ')') { |
| 1208 | *end = p; |
| 1209 | return 1; |
| 1210 | } |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| Anders Waldenborg | 4f732e0 | 2019-01-29 06:49:00 | [diff] [blame] | 1214 | static int match_placeholder_bool_arg(const char *to_parse, const char *candidate, |
| 1215 | const char **end, int *val) |
| 1216 | { |
| 1217 | const char *argval; |
| 1218 | char *strval; |
| 1219 | size_t arglen; |
| 1220 | int v; |
| 1221 | |
| 1222 | if (!match_placeholder_arg_value(to_parse, candidate, end, &argval, &arglen)) |
| 1223 | return 0; |
| 1224 | |
| 1225 | if (!argval) { |
| 1226 | *val = 1; |
| 1227 | return 1; |
| 1228 | } |
| 1229 | |
| 1230 | strval = xstrndup(argval, arglen); |
| 1231 | v = git_parse_maybe_bool(strval); |
| 1232 | free(strval); |
| 1233 | |
| 1234 | if (v == -1) |
| 1235 | return 0; |
| 1236 | |
| 1237 | *val = v; |
| 1238 | |
| 1239 | return 1; |
| 1240 | } |
| 1241 | |
| Anders Waldenborg | 250bea0 | 2019-01-28 21:33:34 | [diff] [blame] | 1242 | static int format_trailer_match_cb(const struct strbuf *key, void *ud) |
| 1243 | { |
| 1244 | const struct string_list *list = ud; |
| 1245 | const struct string_list_item *item; |
| 1246 | |
| 1247 | for_each_string_list_item (item, list) { |
| 1248 | if (key->len == (uintptr_t)item->util && |
| 1249 | !strncasecmp(item->string, key->buf, key->len)) |
| 1250 | return 1; |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1255 | static struct strbuf *expand_string_arg(struct strbuf *sb, |
| 1256 | const char *argval, size_t arglen) |
| René Scharfe | 3c3d0c4 | 2023-06-17 20:40:57 | [diff] [blame] | 1257 | { |
| 1258 | char *fmt = xstrndup(argval, arglen); |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1259 | const char *format = fmt; |
| René Scharfe | 3c3d0c4 | 2023-06-17 20:40:57 | [diff] [blame] | 1260 | |
| 1261 | strbuf_reset(sb); |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1262 | while (strbuf_expand_step(sb, &format)) { |
| 1263 | size_t len; |
| 1264 | |
| 1265 | if (skip_prefix(format, "%", &format)) |
| 1266 | strbuf_addch(sb, '%'); |
| René Scharfe | 4416b86 | 2023-06-17 20:44:00 | [diff] [blame] | 1267 | else if ((len = strbuf_expand_literal(sb, format))) |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1268 | format += len; |
| 1269 | else |
| 1270 | strbuf_addch(sb, '%'); |
| 1271 | } |
| René Scharfe | 3c3d0c4 | 2023-06-17 20:40:57 | [diff] [blame] | 1272 | free(fmt); |
| 1273 | return sb; |
| 1274 | } |
| 1275 | |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1276 | int format_set_trailers_options(struct process_trailer_options *opts, |
| 1277 | struct string_list *filter_list, |
| 1278 | struct strbuf *sepbuf, |
| 1279 | struct strbuf *kvsepbuf, |
| Hariom Verma | 636a0ae | 2021-02-13 01:52:42 | [diff] [blame] | 1280 | const char **arg, |
| 1281 | char **invalid_arg) |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1282 | { |
| 1283 | for (;;) { |
| 1284 | const char *argval; |
| 1285 | size_t arglen; |
| 1286 | |
| Hariom Verma | 636a0ae | 2021-02-13 01:52:42 | [diff] [blame] | 1287 | if (**arg == ')') |
| 1288 | break; |
| 1289 | |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1290 | if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) { |
| 1291 | uintptr_t len = arglen; |
| 1292 | |
| 1293 | if (!argval) |
| 1294 | return -1; |
| 1295 | |
| 1296 | if (len && argval[len - 1] == ':') |
| 1297 | len--; |
| 1298 | string_list_append(filter_list, argval)->util = (char *)len; |
| 1299 | |
| 1300 | opts->filter = format_trailer_match_cb; |
| 1301 | opts->filter_data = filter_list; |
| 1302 | opts->only_trailers = 1; |
| 1303 | } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) { |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1304 | opts->separator = expand_string_arg(sepbuf, argval, arglen); |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1305 | } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) { |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1306 | opts->key_value_separator = expand_string_arg(kvsepbuf, argval, arglen); |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1307 | } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) && |
| 1308 | !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) && |
| 1309 | !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) && |
| Hariom Verma | 636a0ae | 2021-02-13 01:52:42 | [diff] [blame] | 1310 | !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) { |
| 1311 | if (invalid_arg) { |
| 1312 | size_t len = strcspn(*arg, ",)"); |
| 1313 | *invalid_arg = xstrndup(*arg, len); |
| 1314 | } |
| 1315 | return -1; |
| 1316 | } |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1317 | } |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1321 | static size_t parse_describe_args(const char *start, struct strvec *args) |
| 1322 | { |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1323 | struct { |
| 1324 | char *name; |
| 1325 | enum { |
| Eli Schwartz | 1d517ce | 2021-10-31 17:15:09 | [diff] [blame] | 1326 | DESCRIBE_ARG_BOOL, |
| Eli Schwartz | eccd97d | 2021-10-31 17:15:10 | [diff] [blame] | 1327 | DESCRIBE_ARG_INTEGER, |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1328 | DESCRIBE_ARG_STRING, |
| 1329 | } type; |
| 1330 | } option[] = { |
| Eli Schwartz | 1d517ce | 2021-10-31 17:15:09 | [diff] [blame] | 1331 | { "tags", DESCRIBE_ARG_BOOL}, |
| Eli Schwartz | eccd97d | 2021-10-31 17:15:10 | [diff] [blame] | 1332 | { "abbrev", DESCRIBE_ARG_INTEGER }, |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1333 | { "exclude", DESCRIBE_ARG_STRING }, |
| 1334 | { "match", DESCRIBE_ARG_STRING }, |
| 1335 | }; |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1336 | const char *arg = start; |
| 1337 | |
| 1338 | for (;;) { |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1339 | int found = 0; |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1340 | const char *argval; |
| 1341 | size_t arglen = 0; |
| Eli Schwartz | 1d517ce | 2021-10-31 17:15:09 | [diff] [blame] | 1342 | int optval = 0; |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1343 | int i; |
| 1344 | |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1345 | for (i = 0; !found && i < ARRAY_SIZE(option); i++) { |
| 1346 | switch (option[i].type) { |
| Eli Schwartz | 1d517ce | 2021-10-31 17:15:09 | [diff] [blame] | 1347 | case DESCRIBE_ARG_BOOL: |
| 1348 | if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) { |
| 1349 | if (optval) |
| 1350 | strvec_pushf(args, "--%s", option[i].name); |
| 1351 | else |
| 1352 | strvec_pushf(args, "--no-%s", option[i].name); |
| 1353 | found = 1; |
| 1354 | } |
| 1355 | break; |
| Eli Schwartz | eccd97d | 2021-10-31 17:15:10 | [diff] [blame] | 1356 | case DESCRIBE_ARG_INTEGER: |
| 1357 | if (match_placeholder_arg_value(arg, option[i].name, &arg, |
| 1358 | &argval, &arglen)) { |
| 1359 | char *endptr; |
| 1360 | if (!arglen) |
| 1361 | return 0; |
| 1362 | strtol(argval, &endptr, 10); |
| 1363 | if (endptr - argval != arglen) |
| 1364 | return 0; |
| 1365 | strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval); |
| 1366 | found = 1; |
| 1367 | } |
| 1368 | break; |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1369 | case DESCRIBE_ARG_STRING: |
| 1370 | if (match_placeholder_arg_value(arg, option[i].name, &arg, |
| 1371 | &argval, &arglen)) { |
| 1372 | if (!arglen) |
| 1373 | return 0; |
| 1374 | strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval); |
| 1375 | found = 1; |
| 1376 | } |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1377 | break; |
| 1378 | } |
| 1379 | } |
| Eli Schwartz | 3c6eb4e | 2021-10-31 17:15:08 | [diff] [blame] | 1380 | if (!found) |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1381 | break; |
| 1382 | |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1383 | } |
| 1384 | return arg - start; |
| 1385 | } |
| 1386 | |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1387 | |
| 1388 | static int parse_decoration_option(const char **arg, |
| 1389 | const char *name, |
| 1390 | char **opt) |
| 1391 | { |
| 1392 | const char *argval; |
| 1393 | size_t arglen; |
| 1394 | |
| 1395 | if (match_placeholder_arg_value(*arg, name, arg, &argval, &arglen)) { |
| 1396 | struct strbuf sb = STRBUF_INIT; |
| 1397 | |
| 1398 | expand_string_arg(&sb, argval, arglen); |
| 1399 | *opt = strbuf_detach(&sb, NULL); |
| 1400 | return 1; |
| 1401 | } |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | static void parse_decoration_options(const char **arg, |
| 1406 | struct decoration_options *opts) |
| 1407 | { |
| 1408 | while (parse_decoration_option(arg, "prefix", &opts->prefix) || |
| 1409 | parse_decoration_option(arg, "suffix", &opts->suffix) || |
| Andy Koppe | f1f8a25 | 2023-08-20 18:50:08 | [diff] [blame] | 1410 | parse_decoration_option(arg, "separator", &opts->separator) || |
| 1411 | parse_decoration_option(arg, "pointer", &opts->pointer) || |
| 1412 | parse_decoration_option(arg, "tag", &opts->tag)) |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1413 | ; |
| 1414 | } |
| 1415 | |
| 1416 | static void free_decoration_options(const struct decoration_options *opts) |
| 1417 | { |
| 1418 | free(opts->prefix); |
| 1419 | free(opts->suffix); |
| 1420 | free(opts->separator); |
| Andy Koppe | f1f8a25 | 2023-08-20 18:50:08 | [diff] [blame] | 1421 | free(opts->pointer); |
| 1422 | free(opts->tag); |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1423 | } |
| 1424 | |
| Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-18 23:08:47 | [diff] [blame] | 1425 | static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ |
| 1426 | const char *placeholder, |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1427 | void *context) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1428 | { |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 1429 | struct format_commit_context *c = context; |
| 1430 | const struct commit *commit = c->commit; |
| Pat Notz | 177b29d | 2010-11-02 19:59:08 | [diff] [blame] | 1431 | const char *msg = c->message; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 1432 | struct commit_list *p; |
| Hariom Verma | 47d4676 | 2020-08-21 21:41:49 | [diff] [blame] | 1433 | const char *arg, *eol; |
| Anders Waldenborg | fd2015b | 2019-01-28 21:33:36 | [diff] [blame] | 1434 | size_t res; |
| Issac Trotts | ad6f028 | 2019-01-11 06:30:46 | [diff] [blame] | 1435 | char **slot; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1436 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1437 | /* these are independent of the commit */ |
| René Scharfe | 4416b86 | 2023-06-17 20:44:00 | [diff] [blame] | 1438 | res = strbuf_expand_literal(sb, placeholder); |
| Anders Waldenborg | fd2015b | 2019-01-28 21:33:36 | [diff] [blame] | 1439 | if (res) |
| 1440 | return res; |
| 1441 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1442 | switch (placeholder[0]) { |
| 1443 | case 'C': |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1444 | if (starts_with(placeholder + 1, "(auto)")) { |
| Edward Thomson | b15a3e0 | 2016-05-27 03:46:10 | [diff] [blame] | 1445 | c->auto_color = want_color(c->pretty_ctx->color); |
| René Scharfe | 82b83da | 2016-09-29 18:13:05 | [diff] [blame] | 1446 | if (c->auto_color && sb->len) |
| René Scharfe | c99ad27 | 2016-09-17 18:25:24 | [diff] [blame] | 1447 | strbuf_addstr(sb, GIT_COLOR_RESET); |
| Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-18 23:08:49 | [diff] [blame] | 1448 | return 7; /* consumed 7 bytes, "C(auto)" */ |
| 1449 | } else { |
| 1450 | int ret = parse_color(sb, placeholder, c); |
| 1451 | if (ret) |
| 1452 | c->auto_color = 0; |
| 1453 | /* |
| 1454 | * Otherwise, we decided to treat %C<unknown> |
| 1455 | * as a literal string, and the previous |
| 1456 | * %C(auto) is still valid. |
| 1457 | */ |
| 1458 | return ret; |
| Jeff King | c002922 | 2009-01-17 15:38:46 | [diff] [blame] | 1459 | } |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 1460 | case 'w': |
| 1461 | if (placeholder[1] == '(') { |
| 1462 | unsigned long width = 0, indent1 = 0, indent2 = 0; |
| 1463 | char *next; |
| 1464 | const char *start = placeholder + 2; |
| 1465 | const char *end = strchr(start, ')'); |
| 1466 | if (!end) |
| 1467 | return 0; |
| 1468 | if (end > start) { |
| 1469 | width = strtoul(start, &next, 10); |
| 1470 | if (*next == ',') { |
| 1471 | indent1 = strtoul(next + 1, &next, 10); |
| 1472 | if (*next == ',') { |
| 1473 | indent2 = strtoul(next + 1, |
| 1474 | &next, 10); |
| 1475 | } |
| 1476 | } |
| 1477 | if (*next != ')') |
| 1478 | return 0; |
| 1479 | } |
| Patrick Steinhardt | 304a50a | 2022-12-01 14:47:23 | [diff] [blame] | 1480 | |
| 1481 | /* |
| 1482 | * We need to limit the format here as it allows the |
| 1483 | * user to prepend arbitrarily many bytes to the buffer |
| 1484 | * when rewrapping. |
| 1485 | */ |
| 1486 | if (width > FORMATTING_LIMIT || |
| 1487 | indent1 > FORMATTING_LIMIT || |
| 1488 | indent2 > FORMATTING_LIMIT) |
| 1489 | return 0; |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 1490 | rewrap_message_tail(sb, c, width, indent1, indent2); |
| 1491 | return end - placeholder + 1; |
| 1492 | } else |
| 1493 | return 0; |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1494 | |
| 1495 | case '<': |
| 1496 | case '>': |
| Jeff King | 9a1180f | 2019-03-20 08:16:42 | [diff] [blame] | 1497 | return parse_padding_placeholder(placeholder, c); |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1498 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1499 | |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1500 | if (skip_prefix(placeholder, "(describe", &arg)) { |
| René Scharfe | 15ae82d | 2021-02-14 10:04:34 | [diff] [blame] | 1501 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 1502 | struct strbuf out = STRBUF_INIT; |
| 1503 | struct strbuf err = STRBUF_INIT; |
| René Scharfe | 9609972 | 2021-02-28 11:22:47 | [diff] [blame] | 1504 | struct pretty_print_describe_status *describe_status; |
| 1505 | |
| 1506 | describe_status = c->pretty_ctx->describe_status; |
| 1507 | if (describe_status) { |
| 1508 | if (!describe_status->max_invocations) |
| 1509 | return 0; |
| 1510 | describe_status->max_invocations--; |
| 1511 | } |
| René Scharfe | 15ae82d | 2021-02-14 10:04:34 | [diff] [blame] | 1512 | |
| 1513 | cmd.git_cmd = 1; |
| 1514 | strvec_push(&cmd.args, "describe"); |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1515 | |
| 1516 | if (*arg == ':') { |
| 1517 | arg++; |
| 1518 | arg += parse_describe_args(arg, &cmd.args); |
| 1519 | } |
| 1520 | |
| 1521 | if (*arg != ')') { |
| 1522 | child_process_clear(&cmd); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| René Scharfe | 15ae82d | 2021-02-14 10:04:34 | [diff] [blame] | 1526 | strvec_push(&cmd.args, oid_to_hex(&commit->object.oid)); |
| 1527 | pipe_command(&cmd, NULL, 0, &out, 0, &err, 0); |
| 1528 | strbuf_rtrim(&out); |
| 1529 | strbuf_addbuf(sb, &out); |
| 1530 | strbuf_release(&out); |
| 1531 | strbuf_release(&err); |
| René Scharfe | b081547 | 2021-02-14 10:10:57 | [diff] [blame] | 1532 | return arg - placeholder + 1; |
| René Scharfe | 15ae82d | 2021-02-14 10:04:34 | [diff] [blame] | 1533 | } |
| 1534 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1535 | /* these depend on the commit */ |
| 1536 | if (!commit->object.parsed) |
| Stefan Beller | 109cd76 | 2018-06-29 01:21:51 | [diff] [blame] | 1537 | parse_object(the_repository, &commit->object.oid); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1538 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1539 | switch (placeholder[0]) { |
| 1540 | case 'H': /* commit hash */ |
| Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-18 23:08:49 | [diff] [blame] | 1541 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT)); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1542 | strbuf_addstr(sb, oid_to_hex(&commit->object.oid)); |
| Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-18 23:08:49 | [diff] [blame] | 1543 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1544 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1545 | case 'h': /* abbreviated commit hash */ |
| Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-18 23:08:49 | [diff] [blame] | 1546 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT)); |
| brian m. carlson | 30e677e | 2018-03-12 02:27:28 | [diff] [blame] | 1547 | strbuf_add_unique_abbrev(sb, &commit->object.oid, |
| René Scharfe | 1eb47f1 | 2016-08-06 15:41:01 | [diff] [blame] | 1548 | c->pretty_ctx->abbrev); |
| Nguyễn Thái Ngọc Duy | a95f067 | 2013-04-18 23:08:49 | [diff] [blame] | 1549 | strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1550 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1551 | case 'T': /* tree hash */ |
| Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 | [diff] [blame] | 1552 | strbuf_addstr(sb, oid_to_hex(get_commit_tree_oid(commit))); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1553 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1554 | case 't': /* abbreviated tree hash */ |
| Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 | [diff] [blame] | 1555 | strbuf_add_unique_abbrev(sb, |
| Junio C Hamano | c89b6e1 | 2018-05-23 05:38:13 | [diff] [blame] | 1556 | get_commit_tree_oid(commit), |
| René Scharfe | 1eb47f1 | 2016-08-06 15:41:01 | [diff] [blame] | 1557 | c->pretty_ctx->abbrev); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1558 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1559 | case 'P': /* parent hashes */ |
| 1560 | for (p = commit->parents; p; p = p->next) { |
| 1561 | if (p != commit->parents) |
| 1562 | strbuf_addch(sb, ' '); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1563 | strbuf_addstr(sb, oid_to_hex(&p->item->object.oid)); |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1564 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1565 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1566 | case 'p': /* abbreviated parent hashes */ |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1567 | for (p = commit->parents; p; p = p->next) { |
| 1568 | if (p != commit->parents) |
| 1569 | strbuf_addch(sb, ' '); |
| brian m. carlson | 30e677e | 2018-03-12 02:27:28 | [diff] [blame] | 1570 | strbuf_add_unique_abbrev(sb, &p->item->object.oid, |
| René Scharfe | 1eb47f1 | 2016-08-06 15:41:01 | [diff] [blame] | 1571 | c->pretty_ctx->abbrev); |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1572 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1573 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1574 | case 'm': /* left/right/bottom */ |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 1575 | strbuf_addstr(sb, get_revision_mark(NULL, commit)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1576 | return 1; |
| René Scharfe | 3b3d443 | 2008-09-04 21:40:03 | [diff] [blame] | 1577 | case 'd': |
| Andy Koppe | a3883a6 | 2023-08-20 18:50:04 | [diff] [blame] | 1578 | format_decorations(sb, commit, c->auto_color, NULL); |
| René Scharfe | 3b3d443 | 2008-09-04 21:40:03 | [diff] [blame] | 1579 | return 1; |
| Harry Jeffery | 9271095 | 2014-09-18 20:53:53 | [diff] [blame] | 1580 | case 'D': |
| Andy Koppe | a3883a6 | 2023-08-20 18:50:04 | [diff] [blame] | 1581 | { |
| 1582 | const struct decoration_options opts = { |
| 1583 | .prefix = "", |
| 1584 | .suffix = "" |
| 1585 | }; |
| 1586 | |
| 1587 | format_decorations(sb, commit, c->auto_color, &opts); |
| 1588 | return 1; |
| 1589 | } |
| Issac Trotts | ad6f028 | 2019-01-11 06:30:46 | [diff] [blame] | 1590 | case 'S': /* tag/branch like --source */ |
| 1591 | if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources)) |
| 1592 | return 0; |
| 1593 | slot = revision_sources_at(c->pretty_ctx->rev->sources, commit); |
| 1594 | if (!(slot && *slot)) |
| 1595 | return 0; |
| 1596 | strbuf_addstr(sb, *slot); |
| 1597 | return 1; |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 1598 | case 'g': /* reflog info */ |
| 1599 | switch(placeholder[1]) { |
| 1600 | case 'd': /* reflog selector */ |
| 1601 | case 'D': |
| 1602 | if (c->pretty_ctx->reflog_info) |
| 1603 | get_reflog_selector(sb, |
| 1604 | c->pretty_ctx->reflog_info, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 1605 | &c->pretty_ctx->date_mode, |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 1606 | c->pretty_ctx->date_mode_explicit, |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 1607 | (placeholder[1] == 'd')); |
| 1608 | return 2; |
| 1609 | case 's': /* reflog message */ |
| 1610 | if (c->pretty_ctx->reflog_info) |
| 1611 | get_reflog_message(sb, c->pretty_ctx->reflog_info); |
| 1612 | return 2; |
| Jeff King | cd1957f | 2011-12-16 11:40:24 | [diff] [blame] | 1613 | case 'n': |
| 1614 | case 'N': |
| 1615 | case 'e': |
| 1616 | case 'E': |
| 1617 | return format_reflog_person(sb, |
| 1618 | placeholder[1], |
| 1619 | c->pretty_ctx->reflog_info, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 1620 | &c->pretty_ctx->date_mode); |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 1621 | } |
| 1622 | return 0; /* unknown %g placeholder */ |
| Johannes Schindelin | 8b208f0 | 2009-10-09 10:22:05 | [diff] [blame] | 1623 | case 'N': |
| Junio C Hamano | ddf333f | 2012-10-18 01:51:47 | [diff] [blame] | 1624 | if (c->pretty_ctx->notes_message) { |
| 1625 | strbuf_addstr(sb, c->pretty_ctx->notes_message); |
| Johannes Gilger | 5b16360 | 2010-04-13 20:31:12 | [diff] [blame] | 1626 | return 1; |
| 1627 | } |
| 1628 | return 0; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1629 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1630 | |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1631 | if (placeholder[0] == 'G') { |
| Sebastian Götte | ffb6d7d | 2013-03-31 16:00:14 | [diff] [blame] | 1632 | if (!c->signature_check.result) |
| 1633 | check_commit_signature(c->commit, &(c->signature_check)); |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1634 | switch (placeholder[1]) { |
| 1635 | case 'G': |
| Fabian Stelzer | b5726a5 | 2021-09-10 20:07:34 | [diff] [blame] | 1636 | if (c->signature_check.output) |
| 1637 | strbuf_addstr(sb, c->signature_check.output); |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1638 | break; |
| 1639 | case '?': |
| Sebastian Götte | ffb6d7d | 2013-03-31 16:00:14 | [diff] [blame] | 1640 | switch (c->signature_check.result) { |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1641 | case 'G': |
| Hans Jerry Illikainen | 54887b4 | 2019-12-27 13:55:57 | [diff] [blame] | 1642 | switch (c->signature_check.trust_level) { |
| 1643 | case TRUST_UNDEFINED: |
| 1644 | case TRUST_NEVER: |
| 1645 | strbuf_addch(sb, 'U'); |
| 1646 | break; |
| 1647 | default: |
| 1648 | strbuf_addch(sb, 'G'); |
| 1649 | break; |
| 1650 | } |
| 1651 | break; |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1652 | case 'B': |
| Michael J Gruber | 661a180 | 2016-10-12 13:04:15 | [diff] [blame] | 1653 | case 'E': |
| Sebastian Götte | e290c4b | 2013-03-31 16:03:22 | [diff] [blame] | 1654 | case 'N': |
| Michael J Gruber | 661a180 | 2016-10-12 13:04:15 | [diff] [blame] | 1655 | case 'X': |
| 1656 | case 'Y': |
| 1657 | case 'R': |
| Sebastian Götte | ffb6d7d | 2013-03-31 16:00:14 | [diff] [blame] | 1658 | strbuf_addch(sb, c->signature_check.result); |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1659 | } |
| 1660 | break; |
| 1661 | case 'S': |
| Sebastian Götte | ffb6d7d | 2013-03-31 16:00:14 | [diff] [blame] | 1662 | if (c->signature_check.signer) |
| 1663 | strbuf_addstr(sb, c->signature_check.signer); |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1664 | break; |
| Michael J Gruber | 0174eea | 2013-02-14 16:04:46 | [diff] [blame] | 1665 | case 'K': |
| Sebastian Götte | ffb6d7d | 2013-03-31 16:00:14 | [diff] [blame] | 1666 | if (c->signature_check.key) |
| 1667 | strbuf_addstr(sb, c->signature_check.key); |
| Michael J Gruber | 0174eea | 2013-02-14 16:04:46 | [diff] [blame] | 1668 | break; |
| Michał Górny | 3daaaab | 2018-10-22 16:38:20 | [diff] [blame] | 1669 | case 'F': |
| 1670 | if (c->signature_check.fingerprint) |
| 1671 | strbuf_addstr(sb, c->signature_check.fingerprint); |
| 1672 | break; |
| Michał Górny | 4de9394 | 2018-10-22 16:38:21 | [diff] [blame] | 1673 | case 'P': |
| 1674 | if (c->signature_check.primary_key_fingerprint) |
| 1675 | strbuf_addstr(sb, c->signature_check.primary_key_fingerprint); |
| 1676 | break; |
| Hans Jerry Illikainen | 54887b4 | 2019-12-27 13:55:57 | [diff] [blame] | 1677 | case 'T': |
| Jaydeep Das | 803978d | 2022-07-11 05:00:50 | [diff] [blame] | 1678 | strbuf_addstr(sb, gpg_trust_level_to_str(c->signature_check.trust_level)); |
| Hans Jerry Illikainen | 54887b4 | 2019-12-27 13:55:57 | [diff] [blame] | 1679 | break; |
| Jeff King | aa4b78d | 2014-06-17 00:07:07 | [diff] [blame] | 1680 | default: |
| 1681 | return 0; |
| Junio C Hamano | f6667c5 | 2011-10-22 04:06:02 | [diff] [blame] | 1682 | } |
| 1683 | return 2; |
| 1684 | } |
| 1685 | |
| Andy Koppe | a58dd83 | 2023-08-20 18:50:07 | [diff] [blame] | 1686 | if (skip_prefix(placeholder, "(decorate", &arg)) { |
| 1687 | struct decoration_options opts = { NULL }; |
| 1688 | size_t ret = 0; |
| 1689 | |
| 1690 | if (*arg == ':') { |
| 1691 | arg++; |
| 1692 | parse_decoration_options(&arg, &opts); |
| 1693 | } |
| 1694 | if (*arg == ')') { |
| 1695 | format_decorations(sb, commit, c->auto_color, &opts); |
| 1696 | ret = arg - placeholder + 1; |
| 1697 | } |
| 1698 | |
| 1699 | free_decoration_options(&opts); |
| 1700 | return ret; |
| 1701 | } |
| 1702 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1703 | /* For the rest we have to parse the commit header. */ |
| Jeff King | 018b9de | 2021-01-28 19:57:39 | [diff] [blame] | 1704 | if (!c->commit_header_parsed) { |
| 1705 | msg = c->message = |
| 1706 | repo_logmsg_reencode(c->repository, commit, |
| 1707 | &c->commit_encoding, "UTF-8"); |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 1708 | parse_commit_header(c); |
| Jeff King | 018b9de | 2021-01-28 19:57:39 | [diff] [blame] | 1709 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1710 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 1711 | switch (placeholder[0]) { |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1712 | case 'a': /* author ... */ |
| 1713 | return format_person_part(sb, placeholder[1], |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 1714 | msg + c->author.off, c->author.len, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 1715 | &c->pretty_ctx->date_mode); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1716 | case 'c': /* committer ... */ |
| 1717 | return format_person_part(sb, placeholder[1], |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 1718 | msg + c->committer.off, c->committer.len, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 1719 | &c->pretty_ctx->date_mode); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1720 | case 'e': /* encoding */ |
| Nguyễn Thái Ngọc Duy | 0940a76 | 2013-04-18 23:08:41 | [diff] [blame] | 1721 | if (c->commit_encoding) |
| 1722 | strbuf_addstr(sb, c->commit_encoding); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1723 | return 1; |
| Eli Barzilay | 1367b12 | 2010-03-25 02:51:52 | [diff] [blame] | 1724 | case 'B': /* raw body */ |
| 1725 | /* message_off is always left at the initial newline */ |
| 1726 | strbuf_addstr(sb, msg + c->message_off + 1); |
| 1727 | return 1; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | /* Now we need to parse the commit message. */ |
| 1731 | if (!c->commit_message_parsed) |
| 1732 | parse_commit_message(c); |
| 1733 | |
| 1734 | switch (placeholder[0]) { |
| 1735 | case 's': /* subject */ |
| 1736 | format_subject(sb, msg + c->subject_off, " "); |
| 1737 | return 1; |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 1738 | case 'f': /* sanitized subject */ |
| Hariom Verma | 47d4676 | 2020-08-21 21:41:49 | [diff] [blame] | 1739 | eol = strchrnul(msg + c->subject_off, '\n'); |
| 1740 | format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off)); |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 1741 | return 1; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1742 | case 'b': /* body */ |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 1743 | strbuf_addstr(sb, msg + c->body_off); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1744 | return 1; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1745 | } |
| Jacob Keller | d9f31fb | 2016-11-19 00:58:14 | [diff] [blame] | 1746 | |
| Jeff King | 58311c6 | 2017-08-15 10:25:27 | [diff] [blame] | 1747 | if (skip_prefix(placeholder, "(trailers", &arg)) { |
| Jeff King | a388b10 | 2017-08-15 10:23:56 | [diff] [blame] | 1748 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
| Anders Waldenborg | 250bea0 | 2019-01-28 21:33:34 | [diff] [blame] | 1749 | struct string_list filter_list = STRING_LIST_INIT_NODUP; |
| Anders Waldenborg | 0b691d8 | 2019-01-28 21:33:37 | [diff] [blame] | 1750 | struct strbuf sepbuf = STRBUF_INIT; |
| Ævar Arnfjörð Bjarmason | 058761f | 2020-12-09 15:52:08 | [diff] [blame] | 1751 | struct strbuf kvsepbuf = STRBUF_INIT; |
| Anders Waldenborg | 3e3f347 | 2019-01-28 21:33:33 | [diff] [blame] | 1752 | size_t ret = 0; |
| Jeff King | e5fba5d | 2018-08-23 00:50:17 | [diff] [blame] | 1753 | |
| 1754 | opts.no_divider = 1; |
| 1755 | |
| Taylor Blau | 84ff053 | 2017-10-01 16:18:47 | [diff] [blame] | 1756 | if (*arg == ':') { |
| 1757 | arg++; |
| Hariom Verma | 636a0ae | 2021-02-13 01:52:42 | [diff] [blame] | 1758 | if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL)) |
| Hariom Verma | 90563ae | 2021-02-13 01:52:41 | [diff] [blame] | 1759 | goto trailer_out; |
| Jeff King | 58311c6 | 2017-08-15 10:25:27 | [diff] [blame] | 1760 | } |
| 1761 | if (*arg == ')') { |
| 1762 | format_trailers_from_commit(sb, msg + c->subject_off, &opts); |
| Anders Waldenborg | 3e3f347 | 2019-01-28 21:33:33 | [diff] [blame] | 1763 | ret = arg - placeholder + 1; |
| Jeff King | 58311c6 | 2017-08-15 10:25:27 | [diff] [blame] | 1764 | } |
| Anders Waldenborg | 250bea0 | 2019-01-28 21:33:34 | [diff] [blame] | 1765 | trailer_out: |
| 1766 | string_list_clear(&filter_list, 0); |
| Anders Waldenborg | 0b691d8 | 2019-01-28 21:33:37 | [diff] [blame] | 1767 | strbuf_release(&sepbuf); |
| Anders Waldenborg | 3e3f347 | 2019-01-28 21:33:33 | [diff] [blame] | 1768 | return ret; |
| Jacob Keller | d9f31fb | 2016-11-19 00:58:14 | [diff] [blame] | 1769 | } |
| 1770 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 1771 | return 0; /* unknown placeholder */ |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1772 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1773 | |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1774 | static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */ |
| 1775 | const char *placeholder, |
| 1776 | struct format_commit_context *c) |
| 1777 | { |
| 1778 | struct strbuf local_sb = STRBUF_INIT; |
| Patrick Steinhardt | 81dc898 | 2022-12-01 14:46:25 | [diff] [blame] | 1779 | size_t total_consumed = 0; |
| 1780 | int len, padding = c->padding; |
| 1781 | |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1782 | if (padding < 0) { |
| 1783 | const char *start = strrchr(sb->buf, '\n'); |
| 1784 | int occupied; |
| 1785 | if (!start) |
| 1786 | start = sb->buf; |
| Patrick Steinhardt | 522cc87 | 2022-12-01 14:46:53 | [diff] [blame] | 1787 | occupied = utf8_strnwidth(start, strlen(start), 1); |
| Josef Kufner | 3ad87c8 | 2016-06-16 13:18:37 | [diff] [blame] | 1788 | occupied += c->pretty_ctx->graph_width; |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1789 | padding = (-padding) - occupied; |
| 1790 | } |
| 1791 | while (1) { |
| 1792 | int modifier = *placeholder == 'C'; |
| Patrick Steinhardt | 81dc898 | 2022-12-01 14:46:25 | [diff] [blame] | 1793 | size_t consumed = format_commit_one(&local_sb, placeholder, c); |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1794 | total_consumed += consumed; |
| 1795 | |
| 1796 | if (!modifier) |
| 1797 | break; |
| 1798 | |
| 1799 | placeholder += consumed; |
| 1800 | if (*placeholder != '%') |
| 1801 | break; |
| 1802 | placeholder++; |
| 1803 | total_consumed++; |
| 1804 | } |
| Patrick Steinhardt | 522cc87 | 2022-12-01 14:46:53 | [diff] [blame] | 1805 | len = utf8_strnwidth(local_sb.buf, local_sb.len, 1); |
| Nguyễn Thái Ngọc Duy | 1640632 | 2013-04-18 23:08:52 | [diff] [blame] | 1806 | |
| 1807 | if (c->flush_type == flush_left_and_steal) { |
| 1808 | const char *ch = sb->buf + sb->len - 1; |
| 1809 | while (len > padding && ch > sb->buf) { |
| 1810 | const char *p; |
| 1811 | if (*ch == ' ') { |
| 1812 | ch--; |
| 1813 | padding++; |
| 1814 | continue; |
| 1815 | } |
| 1816 | /* check for trailing ansi sequences */ |
| 1817 | if (*ch != 'm') |
| 1818 | break; |
| 1819 | p = ch - 1; |
| Patrick Steinhardt | b49f309 | 2022-12-01 14:46:30 | [diff] [blame] | 1820 | while (p > sb->buf && ch - p < 10 && *p != '\033') |
| Nguyễn Thái Ngọc Duy | 1640632 | 2013-04-18 23:08:52 | [diff] [blame] | 1821 | p--; |
| 1822 | if (*p != '\033' || |
| 1823 | ch + 1 - p != display_mode_esc_sequence_len(p)) |
| 1824 | break; |
| 1825 | /* |
| 1826 | * got a good ansi sequence, put it back to |
| 1827 | * local_sb as we're cutting sb |
| 1828 | */ |
| 1829 | strbuf_insert(&local_sb, 0, p, ch + 1 - p); |
| 1830 | ch = p - 1; |
| 1831 | } |
| 1832 | strbuf_setlen(sb, ch + 1 - sb->buf); |
| 1833 | c->flush_type = flush_left; |
| 1834 | } |
| 1835 | |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1836 | if (len > padding) { |
| 1837 | switch (c->truncate) { |
| 1838 | case trunc_left: |
| 1839 | strbuf_utf8_replace(&local_sb, |
| 1840 | 0, len - (padding - 2), |
| 1841 | ".."); |
| 1842 | break; |
| 1843 | case trunc_middle: |
| 1844 | strbuf_utf8_replace(&local_sb, |
| 1845 | padding / 2 - 1, |
| 1846 | len - (padding - 2), |
| 1847 | ".."); |
| 1848 | break; |
| 1849 | case trunc_right: |
| 1850 | strbuf_utf8_replace(&local_sb, |
| 1851 | padding - 2, len - (padding - 2), |
| 1852 | ".."); |
| 1853 | break; |
| 1854 | case trunc_none: |
| 1855 | break; |
| 1856 | } |
| René Scharfe | e992d1e | 2014-07-10 08:52:21 | [diff] [blame] | 1857 | strbuf_addbuf(sb, &local_sb); |
| Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-18 23:08:51 | [diff] [blame] | 1858 | } else { |
| Patrick Steinhardt | 81dc898 | 2022-12-01 14:46:25 | [diff] [blame] | 1859 | size_t sb_len = sb->len, offset = 0; |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1860 | if (c->flush_type == flush_left) |
| 1861 | offset = padding - len; |
| 1862 | else if (c->flush_type == flush_both) |
| 1863 | offset = (padding - len) / 2; |
| 1864 | /* |
| 1865 | * we calculate padding in columns, now |
| 1866 | * convert it back to chars |
| 1867 | */ |
| 1868 | padding = padding - len + local_sb.len; |
| René Scharfe | 415792e | 2014-09-07 07:06:42 | [diff] [blame] | 1869 | strbuf_addchars(sb, ' ', padding); |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1870 | memcpy(sb->buf + sb_len + offset, local_sb.buf, |
| 1871 | local_sb.len); |
| 1872 | } |
| 1873 | strbuf_release(&local_sb); |
| 1874 | c->flush_type = no_flush; |
| 1875 | return total_consumed; |
| 1876 | } |
| 1877 | |
| Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-18 23:08:47 | [diff] [blame] | 1878 | static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */ |
| 1879 | const char *placeholder, |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1880 | struct format_commit_context *context) |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1881 | { |
| Patrick Steinhardt | 81dc898 | 2022-12-01 14:46:25 | [diff] [blame] | 1882 | size_t consumed, orig_len; |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1883 | enum { |
| 1884 | NO_MAGIC, |
| 1885 | ADD_LF_BEFORE_NON_EMPTY, |
| 1886 | DEL_LF_BEFORE_EMPTY, |
| Junio C Hamano | 223a923 | 2010-06-22 16:45:22 | [diff] [blame] | 1887 | ADD_SP_BEFORE_NON_EMPTY |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1888 | } magic = NO_MAGIC; |
| 1889 | |
| 1890 | switch (placeholder[0]) { |
| 1891 | case '-': |
| 1892 | magic = DEL_LF_BEFORE_EMPTY; |
| 1893 | break; |
| 1894 | case '+': |
| 1895 | magic = ADD_LF_BEFORE_NON_EMPTY; |
| 1896 | break; |
| Michael J Gruber | 7b88176 | 2010-06-14 16:12:29 | [diff] [blame] | 1897 | case ' ': |
| 1898 | magic = ADD_SP_BEFORE_NON_EMPTY; |
| 1899 | break; |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1900 | default: |
| 1901 | break; |
| 1902 | } |
| Patrick Steinhardt | 1de69c0 | 2022-12-01 14:46:39 | [diff] [blame] | 1903 | if (magic != NO_MAGIC) { |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1904 | placeholder++; |
| 1905 | |
| Patrick Steinhardt | 1de69c0 | 2022-12-01 14:46:39 | [diff] [blame] | 1906 | switch (placeholder[0]) { |
| 1907 | case 'w': |
| 1908 | /* |
| 1909 | * `%+w()` cannot ever expand to a non-empty string, |
| 1910 | * and it potentially changes the layout of preceding |
| 1911 | * contents. We're thus not able to handle the magic in |
| 1912 | * this combination and refuse the pattern. |
| 1913 | */ |
| 1914 | return 0; |
| 1915 | }; |
| 1916 | } |
| 1917 | |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1918 | orig_len = sb->len; |
| René Scharfe | 1dd14e8 | 2023-07-07 18:46:26 | [diff] [blame] | 1919 | if (context->flush_type == no_flush) |
| Nguyễn Thái Ngọc Duy | a575234 | 2013-04-18 23:08:50 | [diff] [blame] | 1920 | consumed = format_commit_one(sb, placeholder, context); |
| René Scharfe | 1dd14e8 | 2023-07-07 18:46:26 | [diff] [blame] | 1921 | else |
| 1922 | consumed = format_and_pad_commit(sb, placeholder, context); |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1923 | if (magic == NO_MAGIC) |
| 1924 | return consumed; |
| 1925 | |
| 1926 | if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) { |
| 1927 | while (sb->len && sb->buf[sb->len - 1] == '\n') |
| 1928 | strbuf_setlen(sb, sb->len - 1); |
| Michael J Gruber | 7b88176 | 2010-06-14 16:12:29 | [diff] [blame] | 1929 | } else if (orig_len != sb->len) { |
| 1930 | if (magic == ADD_LF_BEFORE_NON_EMPTY) |
| René Scharfe | a91cc7f | 2020-02-09 13:44:23 | [diff] [blame] | 1931 | strbuf_insertstr(sb, orig_len, "\n"); |
| Michael J Gruber | 7b88176 | 2010-06-14 16:12:29 | [diff] [blame] | 1932 | else if (magic == ADD_SP_BEFORE_NON_EMPTY) |
| René Scharfe | a91cc7f | 2020-02-09 13:44:23 | [diff] [blame] | 1933 | strbuf_insertstr(sb, orig_len, " "); |
| Junio C Hamano | 9fa708d | 2009-10-05 06:43:32 | [diff] [blame] | 1934 | } |
| 1935 | return consumed + 1; |
| 1936 | } |
| 1937 | |
| Johannes Gilger | 5b16360 | 2010-04-13 20:31:12 | [diff] [blame] | 1938 | void userformat_find_requirements(const char *fmt, struct userformat_want *w) |
| 1939 | { |
| Johannes Gilger | 5b16360 | 2010-04-13 20:31:12 | [diff] [blame] | 1940 | if (!fmt) { |
| 1941 | if (!user_format) |
| 1942 | return; |
| 1943 | fmt = user_format; |
| 1944 | } |
| René Scharfe | bd19ee9 | 2023-07-07 19:08:33 | [diff] [blame] | 1945 | while ((fmt = strchr(fmt, '%'))) { |
| 1946 | fmt++; |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1947 | if (skip_prefix(fmt, "%", &fmt)) |
| 1948 | continue; |
| 1949 | |
| 1950 | if (*fmt == '+' || *fmt == '-' || *fmt == ' ') |
| 1951 | fmt++; |
| 1952 | |
| 1953 | switch (*fmt) { |
| 1954 | case 'N': |
| 1955 | w->notes = 1; |
| 1956 | break; |
| 1957 | case 'S': |
| 1958 | w->source = 1; |
| 1959 | break; |
| 1960 | case 'd': |
| 1961 | case 'D': |
| 1962 | w->decorate = 1; |
| 1963 | break; |
| Andy Koppe | 2b09d16 | 2023-10-08 20:23:07 | [diff] [blame] | 1964 | case '(': |
| 1965 | if (starts_with(fmt + 1, "decorate")) |
| 1966 | w->decorate = 1; |
| 1967 | break; |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1968 | } |
| 1969 | } |
| Johannes Gilger | 5b16360 | 2010-04-13 20:31:12 | [diff] [blame] | 1970 | } |
| 1971 | |
| Stefan Beller | f54fbf5 | 2018-11-14 00:13:00 | [diff] [blame] | 1972 | void repo_format_commit_message(struct repository *r, |
| 1973 | const struct commit *commit, |
| 1974 | const char *format, struct strbuf *sb, |
| 1975 | const struct pretty_print_context *pretty_ctx) |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 1976 | { |
| Denton Liu | 3e8ed3b | 2019-11-20 00:51:16 | [diff] [blame] | 1977 | struct format_commit_context context = { |
| Jeff King | 018b9de | 2021-01-28 19:57:39 | [diff] [blame] | 1978 | .repository = r, |
| Denton Liu | 3e8ed3b | 2019-11-20 00:51:16 | [diff] [blame] | 1979 | .commit = commit, |
| 1980 | .pretty_ctx = pretty_ctx, |
| 1981 | .wrap_start = sb->len |
| 1982 | }; |
| Pat Notz | 177b29d | 2010-11-02 19:59:08 | [diff] [blame] | 1983 | const char *output_enc = pretty_ctx->output_encoding; |
| Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-18 23:08:47 | [diff] [blame] | 1984 | const char *utf8 = "UTF-8"; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 1985 | |
| René Scharfe | 6f1e2d5 | 2023-06-17 20:43:17 | [diff] [blame] | 1986 | while (strbuf_expand_step(sb, &format)) { |
| 1987 | size_t len; |
| 1988 | |
| 1989 | if (skip_prefix(format, "%", &format)) |
| 1990 | strbuf_addch(sb, '%'); |
| 1991 | else if ((len = format_commit_item(sb, format, &context))) |
| 1992 | format += len; |
| 1993 | else |
| 1994 | strbuf_addch(sb, '%'); |
| 1995 | } |
| René Scharfe | 02edd56 | 2009-10-17 21:04:19 | [diff] [blame] | 1996 | rewrap_message_tail(sb, &context, 0, 0, 0); |
| Pat Notz | 177b29d | 2010-11-02 19:59:08 | [diff] [blame] | 1997 | |
| Jeff King | 018b9de | 2021-01-28 19:57:39 | [diff] [blame] | 1998 | /* |
| 1999 | * Convert output to an actual output encoding; note that |
| 2000 | * format_commit_item() will always use UTF-8, so we don't |
| 2001 | * have to bother if that's what the output wants. |
| 2002 | */ |
| Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-18 23:08:47 | [diff] [blame] | 2003 | if (output_enc) { |
| 2004 | if (same_encoding(utf8, output_enc)) |
| 2005 | output_enc = NULL; |
| 2006 | } else { |
| 2007 | if (context.commit_encoding && |
| 2008 | !same_encoding(context.commit_encoding, utf8)) |
| 2009 | output_enc = context.commit_encoding; |
| 2010 | } |
| 2011 | |
| 2012 | if (output_enc) { |
| Jeff King | c7d017d | 2018-07-24 10:50:33 | [diff] [blame] | 2013 | size_t outsz; |
| Nguyễn Thái Ngọc Duy | 7e77df3 | 2013-04-18 23:08:47 | [diff] [blame] | 2014 | char *out = reencode_string_len(sb->buf, sb->len, |
| 2015 | output_enc, utf8, &outsz); |
| 2016 | if (out) |
| 2017 | strbuf_attach(sb, out, outsz, outsz + 1); |
| 2018 | } |
| 2019 | |
| Nguyễn Thái Ngọc Duy | 0940a76 | 2013-04-18 23:08:41 | [diff] [blame] | 2020 | free(context.commit_encoding); |
| Stefan Beller | f54fbf5 | 2018-11-14 00:13:00 | [diff] [blame] | 2021 | repo_unuse_commit_buffer(r, commit, context.message); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2022 | } |
| 2023 | |
| Jeff King | 10f2fbf | 2013-07-03 07:07:48 | [diff] [blame] | 2024 | static void pp_header(struct pretty_print_context *pp, |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2025 | const char *encoding, |
| 2026 | const struct commit *commit, |
| 2027 | const char **msg_p, |
| 2028 | struct strbuf *sb) |
| 2029 | { |
| 2030 | int parents_shown = 0; |
| 2031 | |
| 2032 | for (;;) { |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 2033 | const char *name, *line = *msg_p; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2034 | int linelen = get_one_line(*msg_p); |
| 2035 | |
| 2036 | if (!linelen) |
| 2037 | return; |
| 2038 | *msg_p += linelen; |
| 2039 | |
| 2040 | if (linelen == 1) |
| 2041 | /* End of header */ |
| 2042 | return; |
| 2043 | |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2044 | if (pp->fmt == CMIT_FMT_RAW) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2045 | strbuf_add(sb, line, linelen); |
| 2046 | continue; |
| 2047 | } |
| 2048 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2049 | if (starts_with(line, "parent ")) { |
| brian m. carlson | 580f098 | 2018-07-16 01:28:08 | [diff] [blame] | 2050 | if (linelen != the_hash_algo->hexsz + 8) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2051 | die("bad parent line in commit"); |
| 2052 | continue; |
| 2053 | } |
| 2054 | |
| 2055 | if (!parents_shown) { |
| René Scharfe | 4bbaa1e | 2014-07-16 23:52:09 | [diff] [blame] | 2056 | unsigned num = commit_list_count(commit->parents); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2057 | /* with enough slop */ |
| brian m. carlson | 580f098 | 2018-07-16 01:28:08 | [diff] [blame] | 2058 | strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20); |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2059 | add_merge_info(pp, sb, commit); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2060 | parents_shown = 1; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * MEDIUM == DEFAULT shows only author with dates. |
| 2065 | * FULL shows both authors but not dates. |
| 2066 | * FULLER shows both authors and dates. |
| 2067 | */ |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 2068 | if (skip_prefix(line, "author ", &name)) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2069 | strbuf_grow(sb, linelen + 80); |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 2070 | pp_user_info(pp, "Author", sb, name, encoding); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2071 | } |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 2072 | if (skip_prefix(line, "committer ", &name) && |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2073 | (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2074 | strbuf_grow(sb, linelen + 80); |
| René Scharfe | e3f1da9 | 2014-10-04 18:54:50 | [diff] [blame] | 2075 | pp_user_info(pp, "Commit", sb, name, encoding); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
| Jeff King | 10f2fbf | 2013-07-03 07:07:48 | [diff] [blame] | 2080 | void pp_title_line(struct pretty_print_context *pp, |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 2081 | const char **msg_p, |
| 2082 | struct strbuf *sb, |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 2083 | const char *encoding, |
| Junio C Hamano | 267123b | 2008-03-15 07:09:20 | [diff] [blame] | 2084 | int need_8bit_cte) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2085 | { |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 2086 | static const int max_length = 78; /* per rfc2047 */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2087 | struct strbuf title; |
| 2088 | |
| 2089 | strbuf_init(&title, 80); |
| Jeff King | 9553d2b | 2011-05-26 22:28:17 | [diff] [blame] | 2090 | *msg_p = format_subject(&title, *msg_p, |
| 2091 | pp->preserve_subject ? "\n" : " "); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2092 | |
| 2093 | strbuf_grow(sb, title.len + 1024); |
| René Scharfe | 6d167fd | 2017-03-01 11:37:07 | [diff] [blame] | 2094 | if (pp->print_email_subject) { |
| 2095 | if (pp->rev) |
| 2096 | fmt_output_email_subject(sb, pp->rev); |
| Emma Brooks | 19d097e | 2020-04-08 04:31:38 | [diff] [blame] | 2097 | if (pp->encode_email_headers && |
| 2098 | needs_rfc2047_encoding(title.buf, title.len)) |
| Jan H. Schönherr | 41dd00b | 2012-10-18 14:43:33 | [diff] [blame] | 2099 | add_rfc2047(sb, title.buf, title.len, |
| 2100 | encoding, RFC2047_SUBJECT); |
| 2101 | else |
| 2102 | strbuf_add_wrapped_bytes(sb, title.buf, title.len, |
| 2103 | -last_line_length(sb), 1, max_length); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2104 | } else { |
| 2105 | strbuf_addbuf(sb, &title); |
| 2106 | } |
| 2107 | strbuf_addch(sb, '\n'); |
| 2108 | |
| Jeff King | a908047 | 2013-07-03 07:08:22 | [diff] [blame] | 2109 | if (need_8bit_cte == 0) { |
| 2110 | int i; |
| 2111 | for (i = 0; i < pp->in_body_headers.nr; i++) { |
| 2112 | if (has_non_ascii(pp->in_body_headers.items[i].string)) { |
| 2113 | need_8bit_cte = 1; |
| 2114 | break; |
| 2115 | } |
| 2116 | } |
| 2117 | } |
| 2118 | |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 2119 | if (need_8bit_cte > 0) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2120 | const char *header_fmt = |
| 2121 | "MIME-Version: 1.0\n" |
| 2122 | "Content-Type: text/plain; charset=%s\n" |
| 2123 | "Content-Transfer-Encoding: 8bit\n"; |
| 2124 | strbuf_addf(sb, header_fmt, encoding); |
| 2125 | } |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2126 | if (pp->after_subject) { |
| 2127 | strbuf_addstr(sb, pp->after_subject); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2128 | } |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2129 | if (cmit_fmt_is_mail(pp->fmt)) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2130 | strbuf_addch(sb, '\n'); |
| 2131 | } |
| Jeff King | a908047 | 2013-07-03 07:08:22 | [diff] [blame] | 2132 | |
| 2133 | if (pp->in_body_headers.nr) { |
| 2134 | int i; |
| 2135 | for (i = 0; i < pp->in_body_headers.nr; i++) { |
| 2136 | strbuf_addstr(sb, pp->in_body_headers.items[i].string); |
| 2137 | free(pp->in_body_headers.items[i].string); |
| 2138 | } |
| 2139 | string_list_clear(&pp->in_body_headers, 0); |
| 2140 | strbuf_addch(sb, '\n'); |
| 2141 | } |
| 2142 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2143 | strbuf_release(&title); |
| 2144 | } |
| 2145 | |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2146 | static int pp_utf8_width(const char *start, const char *end) |
| 2147 | { |
| 2148 | int width = 0; |
| 2149 | size_t remain = end - start; |
| 2150 | |
| 2151 | while (remain) { |
| 2152 | int n = utf8_width(&start, &remain); |
| 2153 | if (n < 0 || !start) |
| 2154 | return -1; |
| 2155 | width += n; |
| 2156 | } |
| 2157 | return width; |
| 2158 | } |
| 2159 | |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2160 | static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt, |
| 2161 | int color, int tabwidth, const char *line, |
| 2162 | int linelen) |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2163 | { |
| 2164 | const char *tab; |
| 2165 | |
| 2166 | while ((tab = memchr(line, '\t', linelen)) != NULL) { |
| 2167 | int width = pp_utf8_width(line, tab); |
| 2168 | |
| 2169 | /* |
| 2170 | * If it wasn't well-formed utf8, or it |
| 2171 | * had characters with badly defined |
| 2172 | * width (control characters etc), just |
| 2173 | * give up on trying to align things. |
| 2174 | */ |
| 2175 | if (width < 0) |
| 2176 | break; |
| 2177 | |
| 2178 | /* Output the data .. */ |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2179 | append_line_with_color(sb, opt, line, tab - line, color, |
| 2180 | GREP_CONTEXT_BODY, |
| 2181 | GREP_HEADER_FIELD_MAX); |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2182 | |
| 2183 | /* .. and the de-tabified tab */ |
| Junio C Hamano | fe37a9c | 2016-03-29 23:05:39 | [diff] [blame] | 2184 | strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth)); |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2185 | |
| 2186 | /* Skip over the printed part .. */ |
| 2187 | linelen -= tab + 1 - line; |
| 2188 | line = tab + 1; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Print out everything after the last tab without |
| 2193 | * worrying about width - there's nothing more to |
| 2194 | * align. |
| 2195 | */ |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2196 | append_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY, |
| 2197 | GREP_HEADER_FIELD_MAX); |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | /* |
| 2201 | * pp_handle_indent() prints out the intendation, and |
| 2202 | * the whole line (without the final newline), after |
| 2203 | * de-tabifying. |
| 2204 | */ |
| 2205 | static void pp_handle_indent(struct pretty_print_context *pp, |
| 2206 | struct strbuf *sb, int indent, |
| 2207 | const char *line, int linelen) |
| 2208 | { |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2209 | struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL; |
| 2210 | |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2211 | strbuf_addchars(sb, ' ', indent); |
| 2212 | if (pp->expand_tabs_in_log) |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2213 | strbuf_add_tabexpand(sb, opt, pp->color, pp->expand_tabs_in_log, |
| 2214 | line, linelen); |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2215 | else |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2216 | append_line_with_color(sb, opt, line, linelen, pp->color, |
| 2217 | GREP_CONTEXT_BODY, |
| 2218 | GREP_HEADER_FIELD_MAX); |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2219 | } |
| 2220 | |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2221 | static int is_mboxrd_from(const char *line, int len) |
| 2222 | { |
| 2223 | /* |
| 2224 | * a line matching /^From $/ here would only have len == 4 |
| 2225 | * at this point because is_empty_line would've trimmed all |
| 2226 | * trailing space |
| 2227 | */ |
| 2228 | return len > 4 && starts_with(line + strspn(line, ">"), "From "); |
| 2229 | } |
| 2230 | |
| Jeff King | 10f2fbf | 2013-07-03 07:07:48 | [diff] [blame] | 2231 | void pp_remainder(struct pretty_print_context *pp, |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 2232 | const char **msg_p, |
| 2233 | struct strbuf *sb, |
| 2234 | int indent) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2235 | { |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2236 | struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2237 | int first = 1; |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2238 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2239 | for (;;) { |
| 2240 | const char *line = *msg_p; |
| 2241 | int linelen = get_one_line(line); |
| 2242 | *msg_p += linelen; |
| 2243 | |
| 2244 | if (!linelen) |
| 2245 | break; |
| 2246 | |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 2247 | if (is_blank_line(line, &linelen)) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2248 | if (first) |
| 2249 | continue; |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2250 | if (pp->fmt == CMIT_FMT_SHORT) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2251 | break; |
| 2252 | } |
| 2253 | first = 0; |
| 2254 | |
| 2255 | strbuf_grow(sb, linelen + indent + 20); |
| René Scharfe | 415792e | 2014-09-07 07:06:42 | [diff] [blame] | 2256 | if (indent) |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2257 | pp_handle_indent(pp, sb, indent, line, linelen); |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 2258 | else if (pp->expand_tabs_in_log) |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2259 | strbuf_add_tabexpand(sb, opt, pp->color, |
| 2260 | pp->expand_tabs_in_log, line, |
| 2261 | linelen); |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2262 | else { |
| 2263 | if (pp->fmt == CMIT_FMT_MBOXRD && |
| 2264 | is_mboxrd_from(line, linelen)) |
| 2265 | strbuf_addch(sb, '>'); |
| 2266 | |
| Hamza Mahfooz | 6a5c337 | 2021-10-07 20:31:47 | [diff] [blame] | 2267 | append_line_with_color(sb, opt, line, linelen, |
| 2268 | pp->color, GREP_CONTEXT_BODY, |
| 2269 | GREP_HEADER_FIELD_MAX); |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2270 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2271 | strbuf_addch(sb, '\n'); |
| 2272 | } |
| 2273 | } |
| 2274 | |
| Jeff King | 10f2fbf | 2013-07-03 07:07:48 | [diff] [blame] | 2275 | void pretty_print_commit(struct pretty_print_context *pp, |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2276 | const struct commit *commit, |
| 2277 | struct strbuf *sb) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2278 | { |
| 2279 | unsigned long beginning_of_body; |
| 2280 | int indent = 4; |
| Jeff King | dd0d388 | 2013-01-26 09:44:06 | [diff] [blame] | 2281 | const char *msg; |
| Jeff King | b000c59 | 2014-06-10 21:39:30 | [diff] [blame] | 2282 | const char *reencoded; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2283 | const char *encoding; |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2284 | int need_8bit_cte = pp->need_8bit_cte; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2285 | |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2286 | if (pp->fmt == CMIT_FMT_USERFORMAT) { |
| Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 13:58:51 | [diff] [blame] | 2287 | repo_format_commit_message(the_repository, commit, |
| 2288 | user_format, sb, pp); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2289 | return; |
| 2290 | } |
| 2291 | |
| Junio C Hamano | e297cf5 | 2012-10-18 00:12:55 | [diff] [blame] | 2292 | encoding = get_log_output_encoding(); |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 2293 | msg = reencoded = repo_logmsg_reencode(the_repository, commit, NULL, |
| 2294 | encoding); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2295 | |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2296 | if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt)) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2297 | indent = 0; |
| 2298 | |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 2299 | /* |
| 2300 | * We need to check and emit Content-type: to mark it |
| 2301 | * as 8-bit if we haven't done so. |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2302 | */ |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2303 | if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2304 | int i, ch, in_body; |
| 2305 | |
| 2306 | for (in_body = i = 0; (ch = msg[i]); i++) { |
| 2307 | if (!in_body) { |
| 2308 | /* author could be non 7-bit ASCII but |
| 2309 | * the log may be so; skip over the |
| 2310 | * header part first. |
| 2311 | */ |
| 2312 | if (ch == '\n' && msg[i+1] == '\n') |
| 2313 | in_body = 1; |
| 2314 | } |
| 2315 | else if (non_ascii(ch)) { |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 2316 | need_8bit_cte = 1; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2317 | break; |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2322 | pp_header(pp, encoding, commit, &msg, sb); |
| René Scharfe | 6d167fd | 2017-03-01 11:37:07 | [diff] [blame] | 2323 | if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2324 | strbuf_addch(sb, '\n'); |
| 2325 | } |
| 2326 | |
| 2327 | /* Skip excess blank lines at the beginning of body, if any... */ |
| Johannes Schindelin | 7735612 | 2016-06-22 20:20:16 | [diff] [blame] | 2328 | msg = skip_blank_lines(msg); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2329 | |
| 2330 | /* These formats treat the title line specially. */ |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2331 | if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt)) |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2332 | pp_title_line(pp, &msg, sb, encoding, need_8bit_cte); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2333 | |
| 2334 | beginning_of_body = sb->len; |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2335 | if (pp->fmt != CMIT_FMT_ONELINE) |
| 2336 | pp_remainder(pp, &msg, sb, indent); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2337 | strbuf_rtrim(sb); |
| 2338 | |
| 2339 | /* Make sure there is an EOLN for the non-oneline case */ |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2340 | if (pp->fmt != CMIT_FMT_ONELINE) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2341 | strbuf_addch(sb, '\n'); |
| 2342 | |
| 2343 | /* |
| 2344 | * The caller may append additional body text in e-mail |
| 2345 | * format. Make sure we did not strip the blank line |
| 2346 | * between the header and the body. |
| 2347 | */ |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 2348 | if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2349 | strbuf_addch(sb, '\n'); |
| Johannes Schindelin | a97a746 | 2009-10-09 10:21:57 | [diff] [blame] | 2350 | |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 2351 | repo_unuse_commit_buffer(the_repository, commit, reencoded); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 2352 | } |
| Jeff King | 8b8a537 | 2011-05-26 22:27:24 | [diff] [blame] | 2353 | |
| 2354 | void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit, |
| 2355 | struct strbuf *sb) |
| 2356 | { |
| 2357 | struct pretty_print_context pp = {0}; |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 2358 | pp.fmt = fmt; |
| 2359 | pretty_print_commit(&pp, commit, sb); |
| Jeff King | 8b8a537 | 2011-05-26 22:27:24 | [diff] [blame] | 2360 | } |