Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 3 | #include "string-list.h" |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 4 | #include "run-command.h" |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 5 | #include "commit.h" |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 6 | #include "tempfile.h" |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 7 | #include "trailer.h" |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 8 | #include "list.h" |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 9 | /* |
| 10 | * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org> |
| 11 | */ |
| 12 | |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 13 | struct conf_info { |
| 14 | char *name; |
| 15 | char *key; |
| 16 | char *command; |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 17 | char *cmd; |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 18 | enum trailer_where where; |
| 19 | enum trailer_if_exists if_exists; |
| 20 | enum trailer_if_missing if_missing; |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | static struct conf_info default_conf_info; |
| 24 | |
| 25 | struct trailer_item { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 26 | struct list_head list; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 27 | /* |
| 28 | * If this is not a trailer line, the line is stored in value |
| 29 | * (excluding the terminating newline) and token is NULL. |
| 30 | */ |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 31 | char *token; |
| 32 | char *value; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct arg_item { |
| 36 | struct list_head list; |
| 37 | char *token; |
| 38 | char *value; |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 39 | struct conf_info conf; |
| 40 | }; |
| 41 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 42 | static LIST_HEAD(conf_head); |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 43 | |
| 44 | static char *separators = ":"; |
| 45 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 46 | static int configured; |
| 47 | |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 48 | #define TRAILER_ARG_STRING "$ARG" |
| 49 | |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 50 | static const char *git_generated_prefixes[] = { |
| 51 | "Signed-off-by: ", |
| 52 | "(cherry picked from commit ", |
| 53 | NULL |
| 54 | }; |
| 55 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 56 | /* Iterate over the elements of the list. */ |
| 57 | #define list_for_each_dir(pos, head, is_reverse) \ |
| 58 | for (pos = is_reverse ? (head)->prev : (head)->next; \ |
| 59 | pos != (head); \ |
| 60 | pos = is_reverse ? pos->prev : pos->next) |
| 61 | |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 62 | static int after_or_end(enum trailer_where where) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 63 | { |
| 64 | return (where == WHERE_AFTER) || (where == WHERE_END); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Return the length of the string not including any final |
| 69 | * punctuation. E.g., the input "Signed-off-by:" would return |
| 70 | * 13, stripping the trailing punctuation but retaining |
| 71 | * internal punctuation. |
| 72 | */ |
| 73 | static size_t token_len_without_separator(const char *token, size_t len) |
| 74 | { |
| 75 | while (len > 0 && !isalnum(token[len - 1])) |
| 76 | len--; |
| 77 | return len; |
| 78 | } |
| 79 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 80 | static int same_token(struct trailer_item *a, struct arg_item *b) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 81 | { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 82 | size_t a_len, b_len, min_len; |
| 83 | |
| 84 | if (!a->token) |
| 85 | return 0; |
| 86 | |
| 87 | a_len = token_len_without_separator(a->token, strlen(a->token)); |
| 88 | b_len = token_len_without_separator(b->token, strlen(b->token)); |
| 89 | min_len = (a_len > b_len) ? b_len : a_len; |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 90 | |
| 91 | return !strncasecmp(a->token, b->token, min_len); |
| 92 | } |
| 93 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 94 | static int same_value(struct trailer_item *a, struct arg_item *b) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 95 | { |
| 96 | return !strcasecmp(a->value, b->value); |
| 97 | } |
| 98 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 99 | static int same_trailer(struct trailer_item *a, struct arg_item *b) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 100 | { |
| 101 | return same_token(a, b) && same_value(a, b); |
| 102 | } |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 103 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 104 | static inline int is_blank_line(const char *str) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 105 | { |
| 106 | const char *s = str; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 107 | while (*s && *s != '\n' && isspace(*s)) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 108 | s++; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 109 | return !*s || *s == '\n'; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 112 | static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b) |
| 113 | { |
| 114 | const char *ptr = strstr(sb->buf, a); |
| 115 | if (ptr) |
| 116 | strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b)); |
| 117 | } |
| 118 | |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 119 | static void free_trailer_item(struct trailer_item *item) |
| 120 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 121 | free(item->token); |
| 122 | free(item->value); |
| 123 | free(item); |
| 124 | } |
| 125 | |
| 126 | static void free_arg_item(struct arg_item *item) |
| 127 | { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 128 | free(item->conf.name); |
| 129 | free(item->conf.key); |
| 130 | free(item->conf.command); |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 131 | free(item->conf.cmd); |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 132 | free(item->token); |
| 133 | free(item->value); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 134 | free(item); |
| 135 | } |
| 136 | |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 137 | static char last_non_space_char(const char *s) |
| 138 | { |
| 139 | int i; |
| 140 | for (i = strlen(s) - 1; i >= 0; i--) |
| 141 | if (!isspace(s[i])) |
| 142 | return s[i]; |
| 143 | return '\0'; |
| 144 | } |
| 145 | |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 146 | static void print_tok_val(FILE *outfile, const char *tok, const char *val) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 147 | { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 148 | char c; |
| 149 | |
| 150 | if (!tok) { |
| 151 | fprintf(outfile, "%s\n", val); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | c = last_non_space_char(tok); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 156 | if (!c) |
| 157 | return; |
| 158 | if (strchr(separators, c)) |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 159 | fprintf(outfile, "%s%s\n", tok, val); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 160 | else |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 161 | fprintf(outfile, "%s%c %s\n", tok, separators[0], val); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 164 | static void print_all(FILE *outfile, struct list_head *head, |
| 165 | const struct process_trailer_options *opts) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 166 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 167 | struct list_head *pos; |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 168 | struct trailer_item *item; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 169 | list_for_each(pos, head) { |
| 170 | item = list_entry(pos, struct trailer_item, list); |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 171 | if ((!opts->trim_empty || strlen(item->value) > 0) && |
| 172 | (!opts->only_trailers || item->token)) |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 173 | print_tok_val(outfile, item->token, item->value); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 177 | static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 178 | { |
René Scharfe | 241b5d3 | 2021-03-06 12:26:19 +0100 | [diff] [blame] | 179 | struct trailer_item *new_item = xcalloc(1, sizeof(*new_item)); |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 180 | new_item->token = arg_tok->token; |
| 181 | new_item->value = arg_tok->value; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 182 | arg_tok->token = arg_tok->value = NULL; |
| 183 | free_arg_item(arg_tok); |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 184 | return new_item; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void add_arg_to_input_list(struct trailer_item *on_tok, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 188 | struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 189 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 190 | int aoe = after_or_end(arg_tok->conf.where); |
| 191 | struct trailer_item *to_add = trailer_from_arg(arg_tok); |
| 192 | if (aoe) |
| 193 | list_add(&to_add->list, &on_tok->list); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 194 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 195 | list_add_tail(&to_add->list, &on_tok->list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int check_if_different(struct trailer_item *in_tok, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 199 | struct arg_item *arg_tok, |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 200 | int check_all, |
| 201 | struct list_head *head) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 202 | { |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 203 | enum trailer_where where = arg_tok->conf.where; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 204 | struct list_head *next_head; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 205 | do { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 206 | if (same_trailer(in_tok, arg_tok)) |
| 207 | return 0; |
| 208 | /* |
| 209 | * if we want to add a trailer after another one, |
| 210 | * we have to check those before this one |
| 211 | */ |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 212 | next_head = after_or_end(where) ? in_tok->list.prev |
| 213 | : in_tok->list.next; |
| 214 | if (next_head == head) |
| 215 | break; |
| 216 | in_tok = list_entry(next_head, struct trailer_item, list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 217 | } while (check_all); |
| 218 | return 1; |
| 219 | } |
| 220 | |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 221 | static char *apply_command(struct conf_info *conf, const char *arg) |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 222 | { |
| 223 | struct strbuf cmd = STRBUF_INIT; |
| 224 | struct strbuf buf = STRBUF_INIT; |
René Scharfe | b226293 | 2014-11-09 14:49:58 +0100 | [diff] [blame] | 225 | struct child_process cp = CHILD_PROCESS_INIT; |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 226 | char *result; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 227 | |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 228 | if (conf->cmd) { |
| 229 | strbuf_addstr(&cmd, conf->cmd); |
| 230 | strvec_push(&cp.args, cmd.buf); |
| 231 | if (arg) |
| 232 | strvec_push(&cp.args, arg); |
| 233 | } else if (conf->command) { |
| 234 | strbuf_addstr(&cmd, conf->command); |
| 235 | if (arg) |
| 236 | strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); |
| 237 | strvec_push(&cp.args, cmd.buf); |
| 238 | } |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 239 | strvec_pushv(&cp.env, (const char **)local_repo_env); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 240 | cp.no_stdin = 1; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 241 | cp.use_shell = 1; |
| 242 | |
Jeff King | c5eadca | 2015-03-22 23:54:00 -0400 | [diff] [blame] | 243 | if (capture_command(&cp, &buf, 1024)) { |
Nguyễn Thái Ngọc Duy | 13ad56f | 2016-02-27 13:42:10 +0700 | [diff] [blame] | 244 | error(_("running trailer command '%s' failed"), cmd.buf); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 245 | strbuf_release(&buf); |
| 246 | result = xstrdup(""); |
Jeff King | c5eadca | 2015-03-22 23:54:00 -0400 | [diff] [blame] | 247 | } else { |
| 248 | strbuf_trim(&buf); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 249 | result = strbuf_detach(&buf, NULL); |
Jeff King | c5eadca | 2015-03-22 23:54:00 -0400 | [diff] [blame] | 250 | } |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 251 | |
| 252 | strbuf_release(&cmd); |
| 253 | return result; |
| 254 | } |
| 255 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 256 | static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok) |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 257 | { |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 258 | if (arg_tok->conf.command || arg_tok->conf.cmd) { |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 259 | const char *arg; |
| 260 | if (arg_tok->value && arg_tok->value[0]) { |
| 261 | arg = arg_tok->value; |
| 262 | } else { |
| 263 | if (in_tok && in_tok->value) |
| 264 | arg = xstrdup(in_tok->value); |
| 265 | else |
| 266 | arg = xstrdup(""); |
| 267 | } |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 268 | arg_tok->value = apply_command(&arg_tok->conf, arg); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 269 | free((char *)arg); |
| 270 | } |
| 271 | } |
| 272 | |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 273 | static void apply_arg_if_exists(struct trailer_item *in_tok, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 274 | struct arg_item *arg_tok, |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 275 | struct trailer_item *on_tok, |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 276 | struct list_head *head) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 277 | { |
| 278 | switch (arg_tok->conf.if_exists) { |
| 279 | case EXISTS_DO_NOTHING: |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 280 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 281 | break; |
| 282 | case EXISTS_REPLACE: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 283 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 284 | add_arg_to_input_list(on_tok, arg_tok); |
| 285 | list_del(&in_tok->list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 286 | free_trailer_item(in_tok); |
| 287 | break; |
| 288 | case EXISTS_ADD: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 289 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 290 | add_arg_to_input_list(on_tok, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 291 | break; |
| 292 | case EXISTS_ADD_IF_DIFFERENT: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 293 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 294 | if (check_if_different(in_tok, arg_tok, 1, head)) |
| 295 | add_arg_to_input_list(on_tok, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 296 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 297 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 298 | break; |
| 299 | case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 300 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 301 | if (check_if_different(on_tok, arg_tok, 0, head)) |
| 302 | add_arg_to_input_list(on_tok, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 303 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 304 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 305 | break; |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 306 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 307 | BUG("trailer.c: unhandled value %d", |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 308 | arg_tok->conf.if_exists); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 312 | static void apply_arg_if_missing(struct list_head *head, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 313 | struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 314 | { |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 315 | enum trailer_where where; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 316 | struct trailer_item *to_add; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 317 | |
| 318 | switch (arg_tok->conf.if_missing) { |
| 319 | case MISSING_DO_NOTHING: |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 320 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 321 | break; |
| 322 | case MISSING_ADD: |
| 323 | where = arg_tok->conf.where; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 324 | apply_item_command(NULL, arg_tok); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 325 | to_add = trailer_from_arg(arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 326 | if (after_or_end(where)) |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 327 | list_add_tail(&to_add->list, head); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 328 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 329 | list_add(&to_add->list, head); |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 330 | break; |
| 331 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 332 | BUG("trailer.c: unhandled value %d", |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 333 | arg_tok->conf.if_missing); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 337 | static int find_same_and_apply_arg(struct list_head *head, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 338 | struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 339 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 340 | struct list_head *pos; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 341 | struct trailer_item *in_tok; |
| 342 | struct trailer_item *on_tok; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 343 | |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 344 | enum trailer_where where = arg_tok->conf.where; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 345 | int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE); |
| 346 | int backwards = after_or_end(where); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 347 | struct trailer_item *start_tok; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 348 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 349 | if (list_empty(head)) |
| 350 | return 0; |
| 351 | |
| 352 | start_tok = list_entry(backwards ? head->prev : head->next, |
| 353 | struct trailer_item, |
| 354 | list); |
| 355 | |
| 356 | list_for_each_dir(pos, head, backwards) { |
| 357 | in_tok = list_entry(pos, struct trailer_item, list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 358 | if (!same_token(in_tok, arg_tok)) |
| 359 | continue; |
| 360 | on_tok = middle ? in_tok : start_tok; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 361 | apply_arg_if_exists(in_tok, arg_tok, on_tok, head); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 362 | return 1; |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 367 | static void process_trailers_lists(struct list_head *head, |
| 368 | struct list_head *arg_head) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 369 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 370 | struct list_head *pos, *p; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 371 | struct arg_item *arg_tok; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 372 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 373 | list_for_each_safe(pos, p, arg_head) { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 374 | int applied = 0; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 375 | arg_tok = list_entry(pos, struct arg_item, list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 376 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 377 | list_del(pos); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 378 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 379 | applied = find_same_and_apply_arg(head, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 380 | |
| 381 | if (!applied) |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 382 | apply_arg_if_missing(head, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 383 | } |
| 384 | } |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 385 | |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 386 | int trailer_set_where(enum trailer_where *item, const char *value) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 387 | { |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 388 | if (!value) |
| 389 | *item = WHERE_DEFAULT; |
| 390 | else if (!strcasecmp("after", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 391 | *item = WHERE_AFTER; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 392 | else if (!strcasecmp("before", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 393 | *item = WHERE_BEFORE; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 394 | else if (!strcasecmp("end", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 395 | *item = WHERE_END; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 396 | else if (!strcasecmp("start", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 397 | *item = WHERE_START; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 398 | else |
| 399 | return -1; |
| 400 | return 0; |
| 401 | } |
| 402 | |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 403 | int trailer_set_if_exists(enum trailer_if_exists *item, const char *value) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 404 | { |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 405 | if (!value) |
| 406 | *item = EXISTS_DEFAULT; |
| 407 | else if (!strcasecmp("addIfDifferent", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 408 | *item = EXISTS_ADD_IF_DIFFERENT; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 409 | else if (!strcasecmp("addIfDifferentNeighbor", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 410 | *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 411 | else if (!strcasecmp("add", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 412 | *item = EXISTS_ADD; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 413 | else if (!strcasecmp("replace", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 414 | *item = EXISTS_REPLACE; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 415 | else if (!strcasecmp("doNothing", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 416 | *item = EXISTS_DO_NOTHING; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 417 | else |
| 418 | return -1; |
| 419 | return 0; |
| 420 | } |
| 421 | |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 422 | int trailer_set_if_missing(enum trailer_if_missing *item, const char *value) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 423 | { |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 424 | if (!value) |
| 425 | *item = MISSING_DEFAULT; |
| 426 | else if (!strcasecmp("doNothing", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 427 | *item = MISSING_DO_NOTHING; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 428 | else if (!strcasecmp("add", value)) |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 429 | *item = MISSING_ADD; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 430 | else |
| 431 | return -1; |
| 432 | return 0; |
| 433 | } |
| 434 | |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 435 | static void duplicate_conf(struct conf_info *dst, const struct conf_info *src) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 436 | { |
| 437 | *dst = *src; |
Junio C Hamano | 13092a9 | 2016-10-12 11:20:23 -0700 | [diff] [blame] | 438 | dst->name = xstrdup_or_null(src->name); |
| 439 | dst->key = xstrdup_or_null(src->key); |
| 440 | dst->command = xstrdup_or_null(src->command); |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 441 | dst->cmd = xstrdup_or_null(src->cmd); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 444 | static struct arg_item *get_conf_item(const char *name) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 445 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 446 | struct list_head *pos; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 447 | struct arg_item *item; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 448 | |
| 449 | /* Look up item with same name */ |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 450 | list_for_each(pos, &conf_head) { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 451 | item = list_entry(pos, struct arg_item, list); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 452 | if (!strcasecmp(item->conf.name, name)) |
| 453 | return item; |
| 454 | } |
| 455 | |
| 456 | /* Item does not already exists, create it */ |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 457 | CALLOC_ARRAY(item, 1); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 458 | duplicate_conf(&item->conf, &default_conf_info); |
| 459 | item->conf.name = xstrdup(name); |
| 460 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 461 | list_add_tail(&item->list, &conf_head); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 462 | |
| 463 | return item; |
| 464 | } |
| 465 | |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 466 | enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_CMD, |
| 467 | TRAILER_WHERE, TRAILER_IF_EXISTS, TRAILER_IF_MISSING }; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 468 | |
| 469 | static struct { |
| 470 | const char *name; |
| 471 | enum trailer_info_type type; |
| 472 | } trailer_config_items[] = { |
| 473 | { "key", TRAILER_KEY }, |
| 474 | { "command", TRAILER_COMMAND }, |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 475 | { "cmd", TRAILER_CMD }, |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 476 | { "where", TRAILER_WHERE }, |
| 477 | { "ifexists", TRAILER_IF_EXISTS }, |
| 478 | { "ifmissing", TRAILER_IF_MISSING } |
| 479 | }; |
| 480 | |
| 481 | static int git_trailer_default_config(const char *conf_key, const char *value, void *cb) |
| 482 | { |
| 483 | const char *trailer_item, *variable_name; |
| 484 | |
| 485 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) |
| 486 | return 0; |
| 487 | |
| 488 | variable_name = strrchr(trailer_item, '.'); |
| 489 | if (!variable_name) { |
| 490 | if (!strcmp(trailer_item, "where")) { |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 491 | if (trailer_set_where(&default_conf_info.where, |
| 492 | value) < 0) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 493 | warning(_("unknown value '%s' for key '%s'"), |
| 494 | value, conf_key); |
| 495 | } else if (!strcmp(trailer_item, "ifexists")) { |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 496 | if (trailer_set_if_exists(&default_conf_info.if_exists, |
| 497 | value) < 0) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 498 | warning(_("unknown value '%s' for key '%s'"), |
| 499 | value, conf_key); |
| 500 | } else if (!strcmp(trailer_item, "ifmissing")) { |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 501 | if (trailer_set_if_missing(&default_conf_info.if_missing, |
| 502 | value) < 0) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 503 | warning(_("unknown value '%s' for key '%s'"), |
| 504 | value, conf_key); |
| 505 | } else if (!strcmp(trailer_item, "separators")) { |
| 506 | separators = xstrdup(value); |
| 507 | } |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int git_trailer_config(const char *conf_key, const char *value, void *cb) |
| 513 | { |
| 514 | const char *trailer_item, *variable_name; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 515 | struct arg_item *item; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 516 | struct conf_info *conf; |
| 517 | char *name = NULL; |
| 518 | enum trailer_info_type type; |
| 519 | int i; |
| 520 | |
| 521 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) |
| 522 | return 0; |
| 523 | |
| 524 | variable_name = strrchr(trailer_item, '.'); |
| 525 | if (!variable_name) |
| 526 | return 0; |
| 527 | |
| 528 | variable_name++; |
| 529 | for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) { |
| 530 | if (strcmp(trailer_config_items[i].name, variable_name)) |
| 531 | continue; |
| 532 | name = xstrndup(trailer_item, variable_name - trailer_item - 1); |
| 533 | type = trailer_config_items[i].type; |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | if (!name) |
| 538 | return 0; |
| 539 | |
| 540 | item = get_conf_item(name); |
| 541 | conf = &item->conf; |
| 542 | free(name); |
| 543 | |
| 544 | switch (type) { |
| 545 | case TRAILER_KEY: |
| 546 | if (conf->key) |
| 547 | warning(_("more than one %s"), conf_key); |
| 548 | conf->key = xstrdup(value); |
| 549 | break; |
| 550 | case TRAILER_COMMAND: |
| 551 | if (conf->command) |
| 552 | warning(_("more than one %s"), conf_key); |
| 553 | conf->command = xstrdup(value); |
| 554 | break; |
ZheNing Hu | c364b7e | 2021-05-03 15:41:05 +0000 | [diff] [blame] | 555 | case TRAILER_CMD: |
| 556 | if (conf->cmd) |
| 557 | warning(_("more than one %s"), conf_key); |
| 558 | conf->cmd = xstrdup(value); |
| 559 | break; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 560 | case TRAILER_WHERE: |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 561 | if (trailer_set_where(&conf->where, value)) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 562 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
| 563 | break; |
| 564 | case TRAILER_IF_EXISTS: |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 565 | if (trailer_set_if_exists(&conf->if_exists, value)) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 566 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
| 567 | break; |
| 568 | case TRAILER_IF_MISSING: |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 569 | if (trailer_set_if_missing(&conf->if_missing, value)) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 570 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
| 571 | break; |
| 572 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 573 | BUG("trailer.c: unhandled type %d", type); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 574 | } |
| 575 | return 0; |
| 576 | } |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 577 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 578 | static void ensure_configured(void) |
| 579 | { |
| 580 | if (configured) |
| 581 | return; |
| 582 | |
| 583 | /* Default config must be setup first */ |
Paolo Bonzini | 52fc319 | 2017-07-24 10:22:43 +0200 | [diff] [blame] | 584 | default_conf_info.where = WHERE_END; |
| 585 | default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; |
| 586 | default_conf_info.if_missing = MISSING_ADD; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 587 | git_config(git_trailer_default_config, NULL); |
| 588 | git_config(git_trailer_config, NULL); |
| 589 | configured = 1; |
| 590 | } |
| 591 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 592 | static const char *token_from_item(struct arg_item *item, char *tok) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 593 | { |
| 594 | if (item->conf.key) |
| 595 | return item->conf.key; |
| 596 | if (tok) |
| 597 | return tok; |
| 598 | return item->conf.name; |
| 599 | } |
| 600 | |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 601 | static int token_matches_item(const char *tok, struct arg_item *item, size_t tok_len) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 602 | { |
| 603 | if (!strncasecmp(tok, item->conf.name, tok_len)) |
| 604 | return 1; |
| 605 | return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0; |
| 606 | } |
| 607 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 608 | /* |
Jonathan Tan | e431956 | 2016-11-02 10:29:16 -0700 | [diff] [blame] | 609 | * If the given line is of the form |
| 610 | * "<token><optional whitespace><separator>..." or "<separator>...", return the |
| 611 | * location of the separator. Otherwise, return -1. The optional whitespace |
| 612 | * is allowed there primarily to allow things like "Bug #43" where <token> is |
| 613 | * "Bug" and <separator> is "#". |
| 614 | * |
| 615 | * The separator-starts-line case (in which this function returns 0) is |
| 616 | * distinguished from the non-well-formed-line case (in which this function |
| 617 | * returns -1) because some callers of this function need such a distinction. |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 618 | */ |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 619 | static ssize_t find_separator(const char *line, const char *separators) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 620 | { |
Jonathan Tan | e431956 | 2016-11-02 10:29:16 -0700 | [diff] [blame] | 621 | int whitespace_found = 0; |
| 622 | const char *c; |
| 623 | for (c = line; *c; c++) { |
| 624 | if (strchr(separators, *c)) |
| 625 | return c - line; |
| 626 | if (!whitespace_found && (isalnum(*c) || *c == '-')) |
| 627 | continue; |
| 628 | if (c != line && (*c == ' ' || *c == '\t')) { |
| 629 | whitespace_found = 1; |
| 630 | continue; |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | return -1; |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Obtain the token, value, and conf from the given trailer. |
| 639 | * |
| 640 | * separator_pos must not be 0, since the token cannot be an empty string. |
| 641 | * |
| 642 | * If separator_pos is -1, interpret the whole trailer as a token. |
| 643 | */ |
| 644 | static void parse_trailer(struct strbuf *tok, struct strbuf *val, |
| 645 | const struct conf_info **conf, const char *trailer, |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 646 | ssize_t separator_pos) |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 647 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 648 | struct arg_item *item; |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 649 | size_t tok_len; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 650 | struct list_head *pos; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 651 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 652 | if (separator_pos != -1) { |
| 653 | strbuf_add(tok, trailer, separator_pos); |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 654 | strbuf_trim(tok); |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 655 | strbuf_addstr(val, trailer + separator_pos + 1); |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 656 | strbuf_trim(val); |
| 657 | } else { |
| 658 | strbuf_addstr(tok, trailer); |
| 659 | strbuf_trim(tok); |
| 660 | } |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 661 | |
| 662 | /* Lookup if the token matches something in the config */ |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 663 | tok_len = token_len_without_separator(tok->buf, tok->len); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 664 | if (conf) |
| 665 | *conf = &default_conf_info; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 666 | list_for_each(pos, &conf_head) { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 667 | item = list_entry(pos, struct arg_item, list); |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 668 | if (token_matches_item(tok->buf, item, tok_len)) { |
| 669 | char *tok_buf = strbuf_detach(tok, NULL); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 670 | if (conf) |
| 671 | *conf = &item->conf; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 672 | strbuf_addstr(tok, token_from_item(item, tok_buf)); |
| 673 | free(tok_buf); |
| 674 | break; |
| 675 | } |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 679 | static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, |
| 680 | char *val) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 681 | { |
René Scharfe | 241b5d3 | 2021-03-06 12:26:19 +0100 | [diff] [blame] | 682 | struct trailer_item *new_item = xcalloc(1, sizeof(*new_item)); |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 683 | new_item->token = tok; |
| 684 | new_item->value = val; |
| 685 | list_add_tail(&new_item->list, head); |
| 686 | return new_item; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 689 | static void add_arg_item(struct list_head *arg_head, char *tok, char *val, |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 690 | const struct conf_info *conf, |
| 691 | const struct new_trailer_item *new_trailer_item) |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 692 | { |
René Scharfe | 241b5d3 | 2021-03-06 12:26:19 +0100 | [diff] [blame] | 693 | struct arg_item *new_item = xcalloc(1, sizeof(*new_item)); |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 694 | new_item->token = tok; |
| 695 | new_item->value = val; |
| 696 | duplicate_conf(&new_item->conf, conf); |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 697 | if (new_trailer_item) { |
| 698 | if (new_trailer_item->where != WHERE_DEFAULT) |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 699 | new_item->conf.where = new_trailer_item->where; |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 700 | if (new_trailer_item->if_exists != EXISTS_DEFAULT) |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 701 | new_item->conf.if_exists = new_trailer_item->if_exists; |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 702 | if (new_trailer_item->if_missing != MISSING_DEFAULT) |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 703 | new_item->conf.if_missing = new_trailer_item->if_missing; |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 704 | } |
Brandon Williams | 1765132 | 2018-02-14 10:59:50 -0800 | [diff] [blame] | 705 | list_add_tail(&new_item->list, arg_head); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 708 | static void process_command_line_args(struct list_head *arg_head, |
Paolo Bonzini | 51166b8 | 2017-08-01 11:03:31 +0200 | [diff] [blame] | 709 | struct list_head *new_trailer_head) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 710 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 711 | struct arg_item *item; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 712 | struct strbuf tok = STRBUF_INIT; |
| 713 | struct strbuf val = STRBUF_INIT; |
| 714 | const struct conf_info *conf; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 715 | struct list_head *pos; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 716 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 717 | /* |
| 718 | * In command-line arguments, '=' is accepted (in addition to the |
| 719 | * separators that are defined). |
| 720 | */ |
| 721 | char *cl_separators = xstrfmt("=%s", separators); |
| 722 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 723 | /* Add an arg item for each configured trailer with a command */ |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 724 | list_for_each(pos, &conf_head) { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 725 | item = list_entry(pos, struct arg_item, list); |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 726 | if (item->conf.command) |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 727 | add_arg_item(arg_head, |
| 728 | xstrdup(token_from_item(item, NULL)), |
| 729 | xstrdup(""), |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 730 | &item->conf, NULL); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 733 | /* Add an arg item for each trailer on the command line */ |
Paolo Bonzini | 51166b8 | 2017-08-01 11:03:31 +0200 | [diff] [blame] | 734 | list_for_each(pos, new_trailer_head) { |
| 735 | struct new_trailer_item *tr = |
| 736 | list_entry(pos, struct new_trailer_item, list); |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 737 | ssize_t separator_pos = find_separator(tr->text, cl_separators); |
Paolo Bonzini | 51166b8 | 2017-08-01 11:03:31 +0200 | [diff] [blame] | 738 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 739 | if (separator_pos == 0) { |
| 740 | struct strbuf sb = STRBUF_INIT; |
Paolo Bonzini | 51166b8 | 2017-08-01 11:03:31 +0200 | [diff] [blame] | 741 | strbuf_addstr(&sb, tr->text); |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 742 | strbuf_trim(&sb); |
| 743 | error(_("empty trailer token in trailer '%.*s'"), |
| 744 | (int) sb.len, sb.buf); |
| 745 | strbuf_release(&sb); |
| 746 | } else { |
Paolo Bonzini | 51166b8 | 2017-08-01 11:03:31 +0200 | [diff] [blame] | 747 | parse_trailer(&tok, &val, &conf, tr->text, |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 748 | separator_pos); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 749 | add_arg_item(arg_head, |
| 750 | strbuf_detach(&tok, NULL), |
| 751 | strbuf_detach(&val, NULL), |
Paolo Bonzini | 0ea5292 | 2017-08-01 11:03:32 +0200 | [diff] [blame] | 752 | conf, tr); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 756 | free(cl_separators); |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 757 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 758 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 759 | static void read_input_file(struct strbuf *sb, const char *file) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 760 | { |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 761 | if (file) { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 762 | if (strbuf_read_file(sb, file, 0) < 0) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 763 | die_errno(_("could not read input file '%s'"), file); |
| 764 | } else { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 765 | if (strbuf_read(sb, fileno(stdin), 0) < 0) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 766 | die_errno(_("could not read from stdin")); |
| 767 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 768 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 769 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 770 | static const char *next_line(const char *str) |
| 771 | { |
| 772 | const char *nl = strchrnul(str, '\n'); |
| 773 | return nl + !!*nl; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 777 | * Return the position of the start of the last line. If len is 0, return -1. |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 778 | */ |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 779 | static ssize_t last_line(const char *buf, size_t len) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 780 | { |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 781 | ssize_t i; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 782 | if (len == 0) |
| 783 | return -1; |
| 784 | if (len == 1) |
| 785 | return 0; |
| 786 | /* |
| 787 | * Skip the last character (in addition to the null terminator), |
| 788 | * because if the last character is a newline, it is considered as part |
| 789 | * of the last line anyway. |
| 790 | */ |
| 791 | i = len - 2; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 792 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 793 | for (; i >= 0; i--) { |
| 794 | if (buf[i] == '\n') |
| 795 | return i + 1; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 796 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 797 | return 0; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 801 | * Return the position of the start of the patch or the length of str if there |
| 802 | * is no patch in the message. |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 803 | */ |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 804 | static size_t find_patch_start(const char *str) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 805 | { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 806 | const char *s; |
| 807 | |
| 808 | for (s = str; *s; s = next_line(s)) { |
Jeff King | c188668 | 2018-08-22 20:48:21 -0400 | [diff] [blame] | 809 | const char *v; |
| 810 | |
| 811 | if (skip_prefix(s, "---", &v) && isspace(*v)) |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 812 | return s - str; |
| 813 | } |
| 814 | |
| 815 | return s - str; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Return the position of the first trailer line or len if there are no |
| 820 | * trailers. |
| 821 | */ |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 822 | static size_t find_trailer_start(const char *buf, size_t len) |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 823 | { |
| 824 | const char *s; |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 825 | ssize_t end_of_title, l; |
| 826 | int only_spaces = 1; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 827 | int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 828 | /* |
| 829 | * Number of possible continuation lines encountered. This will be |
| 830 | * reset to 0 if we encounter a trailer (since those lines are to be |
| 831 | * considered continuations of that trailer), and added to |
| 832 | * non_trailer_lines if we encounter a non-trailer (since those lines |
| 833 | * are to be considered non-trailers). |
| 834 | */ |
| 835 | int possible_continuation_lines = 0; |
Christian Couder | 5c99995 | 2015-08-30 21:14:40 +0200 | [diff] [blame] | 836 | |
| 837 | /* The first paragraph is the title and cannot be trailers */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 838 | for (s = buf; s < buf + len; s = next_line(s)) { |
| 839 | if (s[0] == comment_line_char) |
Christian Couder | 5c99995 | 2015-08-30 21:14:40 +0200 | [diff] [blame] | 840 | continue; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 841 | if (is_blank_line(s)) |
Christian Couder | 5c99995 | 2015-08-30 21:14:40 +0200 | [diff] [blame] | 842 | break; |
| 843 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 844 | end_of_title = s - buf; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 845 | |
| 846 | /* |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 847 | * Get the start of the trailers by looking starting from the end for a |
| 848 | * blank line before a set of non-blank lines that (i) are all |
| 849 | * trailers, or (ii) contains at least one Git-generated trailer and |
| 850 | * consists of at least 25% trailers. |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 851 | */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 852 | for (l = last_line(buf, len); |
| 853 | l >= end_of_title; |
| 854 | l = last_line(buf, l)) { |
| 855 | const char *bol = buf + l; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 856 | const char **p; |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 857 | ssize_t separator_pos; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 858 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 859 | if (bol[0] == comment_line_char) { |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 860 | non_trailer_lines += possible_continuation_lines; |
| 861 | possible_continuation_lines = 0; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 862 | continue; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 863 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 864 | if (is_blank_line(bol)) { |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 865 | if (only_spaces) |
| 866 | continue; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 867 | non_trailer_lines += possible_continuation_lines; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 868 | if (recognized_prefix && |
| 869 | trailer_lines * 3 >= non_trailer_lines) |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 870 | return next_line(bol) - buf; |
| 871 | else if (trailer_lines && !non_trailer_lines) |
| 872 | return next_line(bol) - buf; |
| 873 | return len; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 874 | } |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 875 | only_spaces = 0; |
| 876 | |
| 877 | for (p = git_generated_prefixes; *p; p++) { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 878 | if (starts_with(bol, *p)) { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 879 | trailer_lines++; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 880 | possible_continuation_lines = 0; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 881 | recognized_prefix = 1; |
| 882 | goto continue_outer_loop; |
| 883 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 884 | } |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 885 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 886 | separator_pos = find_separator(bol, separators); |
| 887 | if (separator_pos >= 1 && !isspace(bol[0])) { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 888 | struct list_head *pos; |
| 889 | |
| 890 | trailer_lines++; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 891 | possible_continuation_lines = 0; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 892 | if (recognized_prefix) |
| 893 | continue; |
| 894 | list_for_each(pos, &conf_head) { |
| 895 | struct arg_item *item; |
| 896 | item = list_entry(pos, struct arg_item, list); |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 897 | if (token_matches_item(bol, item, |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 898 | separator_pos)) { |
| 899 | recognized_prefix = 1; |
| 900 | break; |
| 901 | } |
| 902 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 903 | } else if (isspace(bol[0])) |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 904 | possible_continuation_lines++; |
| 905 | else { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 906 | non_trailer_lines++; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 907 | non_trailer_lines += possible_continuation_lines; |
| 908 | possible_continuation_lines = 0; |
| 909 | } |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 910 | continue_outer_loop: |
| 911 | ; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 912 | } |
| 913 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 914 | return len; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 917 | /* Return the position of the end of the trailers. */ |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 918 | static size_t find_trailer_end(const char *buf, size_t len) |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 919 | { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 920 | return len - ignore_non_trailer(buf, len); |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 923 | static int ends_with_blank_line(const char *buf, size_t len) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 924 | { |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 925 | ssize_t ll = last_line(buf, len); |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 926 | if (ll < 0) |
| 927 | return 0; |
| 928 | return is_blank_line(buf + ll); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Jeff King | 0000239 | 2017-08-15 06:23:29 -0400 | [diff] [blame] | 931 | static void unfold_value(struct strbuf *val) |
| 932 | { |
| 933 | struct strbuf out = STRBUF_INIT; |
| 934 | size_t i; |
| 935 | |
| 936 | strbuf_grow(&out, val->len); |
| 937 | i = 0; |
| 938 | while (i < val->len) { |
| 939 | char c = val->buf[i++]; |
| 940 | if (c == '\n') { |
| 941 | /* Collapse continuation down to a single space. */ |
| 942 | while (i < val->len && isspace(val->buf[i])) |
| 943 | i++; |
| 944 | strbuf_addch(&out, ' '); |
| 945 | } else { |
| 946 | strbuf_addch(&out, c); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* Empty lines may have left us with whitespace cruft at the edges */ |
| 951 | strbuf_trim(&out); |
| 952 | |
| 953 | /* output goes back to val as if we modified it in-place */ |
| 954 | strbuf_swap(&out, val); |
| 955 | strbuf_release(&out); |
| 956 | } |
| 957 | |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 958 | static size_t process_input_file(FILE *outfile, |
| 959 | const char *str, |
| 960 | struct list_head *head, |
| 961 | const struct process_trailer_options *opts) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 962 | { |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 963 | struct trailer_info info; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 964 | struct strbuf tok = STRBUF_INIT; |
| 965 | struct strbuf val = STRBUF_INIT; |
Jeff King | a3b636e | 2018-08-22 20:45:44 -0400 | [diff] [blame] | 966 | size_t i; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 967 | |
Jeff King | 00a21f5 | 2018-08-22 20:46:23 -0400 | [diff] [blame] | 968 | trailer_info_get(&info, str, opts); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 969 | |
| 970 | /* Print lines before the trailers as is */ |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 971 | if (!opts->only_trailers) |
| 972 | fwrite(str, 1, info.trailer_start - str, outfile); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 973 | |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 974 | if (!opts->only_trailers && !info.blank_line_before_trailer) |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 975 | fprintf(outfile, "\n"); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 976 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 977 | for (i = 0; i < info.trailer_nr; i++) { |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 978 | int separator_pos; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 979 | char *trailer = info.trailers[i]; |
| 980 | if (trailer[0] == comment_line_char) |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 981 | continue; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 982 | separator_pos = find_separator(trailer, separators); |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 983 | if (separator_pos >= 1) { |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 984 | parse_trailer(&tok, &val, NULL, trailer, |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 985 | separator_pos); |
Jeff King | 0000239 | 2017-08-15 06:23:29 -0400 | [diff] [blame] | 986 | if (opts->unfold) |
| 987 | unfold_value(&val); |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 988 | add_trailer_item(head, |
| 989 | strbuf_detach(&tok, NULL), |
| 990 | strbuf_detach(&val, NULL)); |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 991 | } else if (!opts->only_trailers) { |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 992 | strbuf_addstr(&val, trailer); |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 993 | strbuf_strip_suffix(&val, "\n"); |
| 994 | add_trailer_item(head, |
| 995 | NULL, |
| 996 | strbuf_detach(&val, NULL)); |
Christian Couder | 2887103 | 2014-11-09 10:23:39 +0100 | [diff] [blame] | 997 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1000 | trailer_info_release(&info); |
| 1001 | |
| 1002 | return info.trailer_end - str; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 1003 | } |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1004 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 1005 | static void free_all(struct list_head *head) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1006 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 1007 | struct list_head *pos, *p; |
| 1008 | list_for_each_safe(pos, p, head) { |
| 1009 | list_del(pos); |
| 1010 | free_trailer_item(list_entry(pos, struct trailer_item, list)); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 1014 | static struct tempfile *trailers_tempfile; |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 1015 | |
| 1016 | static FILE *create_in_place_tempfile(const char *file) |
| 1017 | { |
| 1018 | struct stat st; |
Brandon Williams | 31c2c7a | 2018-02-14 10:59:58 -0800 | [diff] [blame] | 1019 | struct strbuf filename_template = STRBUF_INIT; |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 1020 | const char *tail; |
| 1021 | FILE *outfile; |
| 1022 | |
| 1023 | if (stat(file, &st)) |
| 1024 | die_errno(_("could not stat %s"), file); |
| 1025 | if (!S_ISREG(st.st_mode)) |
| 1026 | die(_("file %s is not a regular file"), file); |
| 1027 | if (!(st.st_mode & S_IWUSR)) |
| 1028 | die(_("file %s is not writable by user"), file); |
| 1029 | |
| 1030 | /* Create temporary file in the same directory as the original */ |
| 1031 | tail = strrchr(file, '/'); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 1032 | if (tail) |
Brandon Williams | 31c2c7a | 2018-02-14 10:59:58 -0800 | [diff] [blame] | 1033 | strbuf_add(&filename_template, file, tail - file + 1); |
| 1034 | strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX"); |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 1035 | |
Brandon Williams | 31c2c7a | 2018-02-14 10:59:58 -0800 | [diff] [blame] | 1036 | trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode); |
| 1037 | strbuf_release(&filename_template); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 1038 | outfile = fdopen_tempfile(trailers_tempfile, "w"); |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 1039 | if (!outfile) |
| 1040 | die_errno(_("could not open temporary file")); |
| 1041 | |
| 1042 | return outfile; |
| 1043 | } |
| 1044 | |
Jeff King | 8abc898 | 2017-08-10 14:03:58 -0400 | [diff] [blame] | 1045 | void process_trailers(const char *file, |
| 1046 | const struct process_trailer_options *opts, |
Paolo Bonzini | 51166b8 | 2017-08-01 11:03:31 +0200 | [diff] [blame] | 1047 | struct list_head *new_trailer_head) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1048 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 1049 | LIST_HEAD(head); |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 1050 | struct strbuf sb = STRBUF_INIT; |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 1051 | size_t trailer_end; |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 1052 | FILE *outfile = stdout; |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1053 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1054 | ensure_configured(); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1055 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 1056 | read_input_file(&sb, file); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1057 | |
Jeff King | 8abc898 | 2017-08-10 14:03:58 -0400 | [diff] [blame] | 1058 | if (opts->in_place) |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 1059 | outfile = create_in_place_tempfile(file); |
| 1060 | |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1061 | /* Print the lines before the trailers */ |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 1062 | trailer_end = process_input_file(outfile, sb.buf, &head, opts); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1063 | |
Jeff King | fdbdb64 | 2017-08-15 06:23:25 -0400 | [diff] [blame] | 1064 | if (!opts->only_input) { |
| 1065 | LIST_HEAD(arg_head); |
Junio C Hamano | 06cf4f2 | 2017-08-26 22:55:04 -0700 | [diff] [blame] | 1066 | process_command_line_args(&arg_head, new_trailer_head); |
Jeff King | fdbdb64 | 2017-08-15 06:23:25 -0400 | [diff] [blame] | 1067 | process_trailers_lists(&head, &arg_head); |
| 1068 | } |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1069 | |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 1070 | print_all(outfile, &head, opts); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1071 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 1072 | free_all(&head); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1073 | |
| 1074 | /* Print the lines after the trailers as is */ |
Jeff King | 56c493e | 2017-08-15 06:23:21 -0400 | [diff] [blame] | 1075 | if (!opts->only_trailers) |
| 1076 | fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1077 | |
Jeff King | 8abc898 | 2017-08-10 14:03:58 -0400 | [diff] [blame] | 1078 | if (opts->in_place) |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 1079 | if (rename_tempfile(&trailers_tempfile, file)) |
| 1080 | die_errno(_("could not rename temporary file to %s"), file); |
| 1081 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 1082 | strbuf_release(&sb); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1083 | } |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1084 | |
Jeff King | 00a21f5 | 2018-08-22 20:46:23 -0400 | [diff] [blame] | 1085 | void trailer_info_get(struct trailer_info *info, const char *str, |
| 1086 | const struct process_trailer_options *opts) |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1087 | { |
| 1088 | int patch_start, trailer_end, trailer_start; |
| 1089 | struct strbuf **trailer_lines, **ptr; |
| 1090 | char **trailer_strings = NULL; |
| 1091 | size_t nr = 0, alloc = 0; |
| 1092 | char **last = NULL; |
| 1093 | |
| 1094 | ensure_configured(); |
| 1095 | |
Jeff King | 1688c9a | 2018-08-22 20:49:56 -0400 | [diff] [blame] | 1096 | if (opts->no_divider) |
| 1097 | patch_start = strlen(str); |
| 1098 | else |
| 1099 | patch_start = find_patch_start(str); |
| 1100 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1101 | trailer_end = find_trailer_end(str, patch_start); |
| 1102 | trailer_start = find_trailer_start(str, trailer_end); |
| 1103 | |
| 1104 | trailer_lines = strbuf_split_buf(str + trailer_start, |
| 1105 | trailer_end - trailer_start, |
| 1106 | '\n', |
| 1107 | 0); |
| 1108 | for (ptr = trailer_lines; *ptr; ptr++) { |
| 1109 | if (last && isspace((*ptr)->buf[0])) { |
| 1110 | struct strbuf sb = STRBUF_INIT; |
| 1111 | strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); |
| 1112 | strbuf_addbuf(&sb, *ptr); |
| 1113 | *last = strbuf_detach(&sb, NULL); |
| 1114 | continue; |
| 1115 | } |
| 1116 | ALLOC_GROW(trailer_strings, nr + 1, alloc); |
| 1117 | trailer_strings[nr] = strbuf_detach(*ptr, NULL); |
| 1118 | last = find_separator(trailer_strings[nr], separators) >= 1 |
| 1119 | ? &trailer_strings[nr] |
| 1120 | : NULL; |
| 1121 | nr++; |
| 1122 | } |
| 1123 | strbuf_list_free(trailer_lines); |
| 1124 | |
| 1125 | info->blank_line_before_trailer = ends_with_blank_line(str, |
| 1126 | trailer_start); |
| 1127 | info->trailer_start = str + trailer_start; |
| 1128 | info->trailer_end = str + trailer_end; |
| 1129 | info->trailers = trailer_strings; |
| 1130 | info->trailer_nr = nr; |
| 1131 | } |
| 1132 | |
| 1133 | void trailer_info_release(struct trailer_info *info) |
| 1134 | { |
Jeff King | a3b636e | 2018-08-22 20:45:44 -0400 | [diff] [blame] | 1135 | size_t i; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1136 | for (i = 0; i < info->trailer_nr; i++) |
| 1137 | free(info->trailers[i]); |
| 1138 | free(info->trailers); |
| 1139 | } |
Jeff King | a388b10 | 2017-08-15 06:23:56 -0400 | [diff] [blame] | 1140 | |
| 1141 | static void format_trailer_info(struct strbuf *out, |
| 1142 | const struct trailer_info *info, |
| 1143 | const struct process_trailer_options *opts) |
| 1144 | { |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1145 | size_t origlen = out->len; |
Jeff King | a3b636e | 2018-08-22 20:45:44 -0400 | [diff] [blame] | 1146 | size_t i; |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1147 | |
| 1148 | /* If we want the whole block untouched, we can take the fast path. */ |
Ævar Arnfjörð Bjarmason | 8b966a0 | 2020-12-09 16:52:06 +0100 | [diff] [blame] | 1149 | if (!opts->only_trailers && !opts->unfold && !opts->filter && |
Ævar Arnfjörð Bjarmason | 058761f | 2020-12-09 16:52:08 +0100 | [diff] [blame] | 1150 | !opts->separator && !opts->key_only && !opts->value_only && |
| 1151 | !opts->key_value_separator) { |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1152 | strbuf_add(out, info->trailer_start, |
| 1153 | info->trailer_end - info->trailer_start); |
| 1154 | return; |
| 1155 | } |
| 1156 | |
| 1157 | for (i = 0; i < info->trailer_nr; i++) { |
| 1158 | char *trailer = info->trailers[i]; |
Jeff King | 0d2db00 | 2018-08-22 20:44:38 -0400 | [diff] [blame] | 1159 | ssize_t separator_pos = find_separator(trailer, separators); |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1160 | |
| 1161 | if (separator_pos >= 1) { |
| 1162 | struct strbuf tok = STRBUF_INIT; |
| 1163 | struct strbuf val = STRBUF_INIT; |
| 1164 | |
| 1165 | parse_trailer(&tok, &val, NULL, trailer, separator_pos); |
Anders Waldenborg | 250bea0 | 2019-01-28 22:33:34 +0100 | [diff] [blame] | 1166 | if (!opts->filter || opts->filter(&tok, opts->filter_data)) { |
| 1167 | if (opts->unfold) |
| 1168 | unfold_value(&val); |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1169 | |
| 1170 | if (opts->separator && out->len != origlen) |
| 1171 | strbuf_addbuf(out, opts->separator); |
Anders Waldenborg | d9b936d | 2019-01-28 22:33:35 +0100 | [diff] [blame] | 1172 | if (!opts->value_only) |
Ævar Arnfjörð Bjarmason | 9d87d5a | 2020-12-09 16:52:07 +0100 | [diff] [blame] | 1173 | strbuf_addbuf(out, &tok); |
Ævar Arnfjörð Bjarmason | 058761f | 2020-12-09 16:52:08 +0100 | [diff] [blame] | 1174 | if (!opts->key_only && !opts->value_only) { |
| 1175 | if (opts->key_value_separator) |
| 1176 | strbuf_addbuf(out, opts->key_value_separator); |
| 1177 | else |
| 1178 | strbuf_addstr(out, ": "); |
| 1179 | } |
Ævar Arnfjörð Bjarmason | 9d87d5a | 2020-12-09 16:52:07 +0100 | [diff] [blame] | 1180 | if (!opts->key_only) |
| 1181 | strbuf_addbuf(out, &val); |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1182 | if (!opts->separator) |
| 1183 | strbuf_addch(out, '\n'); |
Anders Waldenborg | 250bea0 | 2019-01-28 22:33:34 +0100 | [diff] [blame] | 1184 | } |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1185 | strbuf_release(&tok); |
| 1186 | strbuf_release(&val); |
| 1187 | |
| 1188 | } else if (!opts->only_trailers) { |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1189 | if (opts->separator && out->len != origlen) { |
| 1190 | strbuf_addbuf(out, opts->separator); |
| 1191 | } |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1192 | strbuf_addstr(out, trailer); |
Anders Waldenborg | 0b691d8 | 2019-01-28 22:33:37 +0100 | [diff] [blame] | 1193 | if (opts->separator) { |
| 1194 | strbuf_rtrim(out); |
| 1195 | } |
Jeff King | 58311c6 | 2017-08-15 06:25:27 -0400 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Jeff King | a388b10 | 2017-08-15 06:23:56 -0400 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | void format_trailers_from_commit(struct strbuf *out, const char *msg, |
| 1202 | const struct process_trailer_options *opts) |
| 1203 | { |
| 1204 | struct trailer_info info; |
| 1205 | |
Jeff King | 00a21f5 | 2018-08-22 20:46:23 -0400 | [diff] [blame] | 1206 | trailer_info_get(&info, msg, opts); |
Jeff King | a388b10 | 2017-08-15 06:23:56 -0400 | [diff] [blame] | 1207 | format_trailer_info(out, &info, opts); |
| 1208 | trailer_info_release(&info); |
| 1209 | } |
Jeff King | f0939a0 | 2020-09-27 04:40:01 -0400 | [diff] [blame] | 1210 | |
| 1211 | void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) |
| 1212 | { |
| 1213 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
| 1214 | strbuf_init(&iter->key, 0); |
| 1215 | strbuf_init(&iter->val, 0); |
| 1216 | opts.no_divider = 1; |
| 1217 | trailer_info_get(&iter->info, msg, &opts); |
| 1218 | iter->cur = 0; |
| 1219 | } |
| 1220 | |
| 1221 | int trailer_iterator_advance(struct trailer_iterator *iter) |
| 1222 | { |
| 1223 | while (iter->cur < iter->info.trailer_nr) { |
| 1224 | char *trailer = iter->info.trailers[iter->cur++]; |
| 1225 | int separator_pos = find_separator(trailer, separators); |
| 1226 | |
| 1227 | if (separator_pos < 1) |
| 1228 | continue; /* not a real trailer */ |
| 1229 | |
| 1230 | strbuf_reset(&iter->key); |
| 1231 | strbuf_reset(&iter->val); |
| 1232 | parse_trailer(&iter->key, &iter->val, NULL, |
| 1233 | trailer, separator_pos); |
| 1234 | unfold_value(&iter->val); |
| 1235 | return 1; |
| 1236 | } |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | void trailer_iterator_release(struct trailer_iterator *iter) |
| 1241 | { |
| 1242 | trailer_info_release(&iter->info); |
| 1243 | strbuf_release(&iter->val); |
| 1244 | strbuf_release(&iter->key); |
| 1245 | } |