blob: 11f0b9fb40bcedbfcd3111fa8076ce0d9e1eb20f [file] [log] [blame]
Christian Couder9385b5d2014-10-13 20:16:23 +02001#include "cache.h"
Christian Couderf0a90b42014-10-13 20:16:26 +02002#include "string-list.h"
Christian Couder85039fb2014-10-13 20:16:31 +02003#include "run-command.h"
Christian Couder61cfef42014-11-09 10:23:42 +01004#include "commit.h"
Tobias Klausere1f89862016-01-14 17:57:55 +01005#include "tempfile.h"
Christian Couderb1d78d72014-10-13 20:16:28 +02006#include "trailer.h"
Jonathan Tan8966a392016-10-20 14:39:47 -07007#include "list.h"
Christian Couder9385b5d2014-10-13 20:16:23 +02008/*
9 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
10 */
11
12enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
13enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
14 EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
15enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
16
17struct conf_info {
18 char *name;
19 char *key;
20 char *command;
21 enum action_where where;
22 enum action_if_exists if_exists;
23 enum action_if_missing if_missing;
24};
25
26static struct conf_info default_conf_info;
27
28struct trailer_item {
Jonathan Tan8966a392016-10-20 14:39:47 -070029 struct list_head list;
Jonathan Tan14624502016-10-21 10:55:01 -070030 /*
31 * If this is not a trailer line, the line is stored in value
32 * (excluding the terminating newline) and token is NULL.
33 */
Jonathan Tand65fd422016-10-14 10:37:58 -070034 char *token;
35 char *value;
Jonathan Tancc71b0d2016-10-20 14:39:49 -070036};
37
38struct arg_item {
39 struct list_head list;
40 char *token;
41 char *value;
Christian Couder9385b5d2014-10-13 20:16:23 +020042 struct conf_info conf;
43};
44
Jonathan Tan8966a392016-10-20 14:39:47 -070045static LIST_HEAD(conf_head);
Christian Couder9385b5d2014-10-13 20:16:23 +020046
47static char *separators = ":";
48
Jonathan Tane8c352c2016-11-02 10:29:19 -070049static int configured;
50
Christian Couder85039fb2014-10-13 20:16:31 +020051#define TRAILER_ARG_STRING "$ARG"
52
Jonathan Tan14624502016-10-21 10:55:01 -070053static const char *git_generated_prefixes[] = {
54 "Signed-off-by: ",
55 "(cherry picked from commit ",
56 NULL
57};
58
Jonathan Tan8966a392016-10-20 14:39:47 -070059/* Iterate over the elements of the list. */
60#define list_for_each_dir(pos, head, is_reverse) \
61 for (pos = is_reverse ? (head)->prev : (head)->next; \
62 pos != (head); \
63 pos = is_reverse ? pos->prev : pos->next)
64
Christian Couder9385b5d2014-10-13 20:16:23 +020065static int after_or_end(enum action_where where)
66{
67 return (where == WHERE_AFTER) || (where == WHERE_END);
68}
69
70/*
71 * Return the length of the string not including any final
72 * punctuation. E.g., the input "Signed-off-by:" would return
73 * 13, stripping the trailing punctuation but retaining
74 * internal punctuation.
75 */
76static size_t token_len_without_separator(const char *token, size_t len)
77{
78 while (len > 0 && !isalnum(token[len - 1]))
79 len--;
80 return len;
81}
82
Jonathan Tancc71b0d2016-10-20 14:39:49 -070083static int same_token(struct trailer_item *a, struct arg_item *b)
Christian Couder9385b5d2014-10-13 20:16:23 +020084{
Jonathan Tan14624502016-10-21 10:55:01 -070085 size_t a_len, b_len, min_len;
86
87 if (!a->token)
88 return 0;
89
90 a_len = token_len_without_separator(a->token, strlen(a->token));
91 b_len = token_len_without_separator(b->token, strlen(b->token));
92 min_len = (a_len > b_len) ? b_len : a_len;
Christian Couder9385b5d2014-10-13 20:16:23 +020093
94 return !strncasecmp(a->token, b->token, min_len);
95}
96
Jonathan Tancc71b0d2016-10-20 14:39:49 -070097static int same_value(struct trailer_item *a, struct arg_item *b)
Christian Couder9385b5d2014-10-13 20:16:23 +020098{
99 return !strcasecmp(a->value, b->value);
100}
101
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700102static int same_trailer(struct trailer_item *a, struct arg_item *b)
Christian Couder9385b5d2014-10-13 20:16:23 +0200103{
104 return same_token(a, b) && same_value(a, b);
105}
Christian Couder41038182014-10-13 20:16:24 +0200106
Jonathan Tan022349c2016-11-02 10:29:18 -0700107static inline int is_blank_line(const char *str)
Christian Couder2013d852014-10-13 20:16:27 +0200108{
109 const char *s = str;
Jonathan Tan022349c2016-11-02 10:29:18 -0700110 while (*s && *s != '\n' && isspace(*s))
Christian Couder2013d852014-10-13 20:16:27 +0200111 s++;
Jonathan Tan022349c2016-11-02 10:29:18 -0700112 return !*s || *s == '\n';
Christian Couder2013d852014-10-13 20:16:27 +0200113}
114
Christian Couder85039fb2014-10-13 20:16:31 +0200115static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
116{
117 const char *ptr = strstr(sb->buf, a);
118 if (ptr)
119 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
120}
121
Christian Couder41038182014-10-13 20:16:24 +0200122static void free_trailer_item(struct trailer_item *item)
123{
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700124 free(item->token);
125 free(item->value);
126 free(item);
127}
128
129static void free_arg_item(struct arg_item *item)
130{
Christian Couder41038182014-10-13 20:16:24 +0200131 free(item->conf.name);
132 free(item->conf.key);
133 free(item->conf.command);
Jonathan Tand65fd422016-10-14 10:37:58 -0700134 free(item->token);
135 free(item->value);
Christian Couder41038182014-10-13 20:16:24 +0200136 free(item);
137}
138
Christian Couderb1d78d72014-10-13 20:16:28 +0200139static char last_non_space_char(const char *s)
140{
141 int i;
142 for (i = strlen(s) - 1; i >= 0; i--)
143 if (!isspace(s[i]))
144 return s[i];
145 return '\0';
146}
147
Tobias Klauserd0d23442016-01-14 17:57:54 +0100148static void print_tok_val(FILE *outfile, const char *tok, const char *val)
Christian Couderb1d78d72014-10-13 20:16:28 +0200149{
Jonathan Tan14624502016-10-21 10:55:01 -0700150 char c;
151
152 if (!tok) {
153 fprintf(outfile, "%s\n", val);
154 return;
155 }
156
157 c = last_non_space_char(tok);
Christian Couderb1d78d72014-10-13 20:16:28 +0200158 if (!c)
159 return;
160 if (strchr(separators, c))
Tobias Klauserd0d23442016-01-14 17:57:54 +0100161 fprintf(outfile, "%s%s\n", tok, val);
Christian Couderb1d78d72014-10-13 20:16:28 +0200162 else
Tobias Klauserd0d23442016-01-14 17:57:54 +0100163 fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
Christian Couderb1d78d72014-10-13 20:16:28 +0200164}
165
Jonathan Tan8966a392016-10-20 14:39:47 -0700166static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
Christian Couderb1d78d72014-10-13 20:16:28 +0200167{
Jonathan Tan8966a392016-10-20 14:39:47 -0700168 struct list_head *pos;
Christian Couderb1d78d72014-10-13 20:16:28 +0200169 struct trailer_item *item;
Jonathan Tan8966a392016-10-20 14:39:47 -0700170 list_for_each(pos, head) {
171 item = list_entry(pos, struct trailer_item, list);
Christian Couderb1d78d72014-10-13 20:16:28 +0200172 if (!trim_empty || strlen(item->value) > 0)
Tobias Klauserd0d23442016-01-14 17:57:54 +0100173 print_tok_val(outfile, item->token, item->value);
Christian Couderb1d78d72014-10-13 20:16:28 +0200174 }
175}
176
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700177static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
Christian Couder41038182014-10-13 20:16:24 +0200178{
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700179 struct trailer_item *new = xcalloc(sizeof(*new), 1);
180 new->token = arg_tok->token;
181 new->value = arg_tok->value;
182 arg_tok->token = arg_tok->value = NULL;
183 free_arg_item(arg_tok);
184 return new;
Christian Couder41038182014-10-13 20:16:24 +0200185}
186
187static void add_arg_to_input_list(struct trailer_item *on_tok,
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700188 struct arg_item *arg_tok)
Christian Couder41038182014-10-13 20:16:24 +0200189{
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700190 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 Tan8966a392016-10-20 14:39:47 -0700194 else
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700195 list_add_tail(&to_add->list, &on_tok->list);
Christian Couder41038182014-10-13 20:16:24 +0200196}
197
198static int check_if_different(struct trailer_item *in_tok,
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700199 struct arg_item *arg_tok,
Jonathan Tan8966a392016-10-20 14:39:47 -0700200 int check_all,
201 struct list_head *head)
Christian Couder41038182014-10-13 20:16:24 +0200202{
203 enum action_where where = arg_tok->conf.where;
Jonathan Tan8966a392016-10-20 14:39:47 -0700204 struct list_head *next_head;
Christian Couder41038182014-10-13 20:16:24 +0200205 do {
Christian Couder41038182014-10-13 20:16:24 +0200206 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 Tan8966a392016-10-20 14:39:47 -0700212 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 Couder41038182014-10-13 20:16:24 +0200217 } while (check_all);
218 return 1;
219}
220
Jonathan Tand65fd422016-10-14 10:37:58 -0700221static char *apply_command(const char *command, const char *arg)
Christian Couder85039fb2014-10-13 20:16:31 +0200222{
223 struct strbuf cmd = STRBUF_INIT;
224 struct strbuf buf = STRBUF_INIT;
René Scharfeb2262932014-11-09 14:49:58 +0100225 struct child_process cp = CHILD_PROCESS_INIT;
Christian Couder85039fb2014-10-13 20:16:31 +0200226 const char *argv[] = {NULL, NULL};
Jonathan Tand65fd422016-10-14 10:37:58 -0700227 char *result;
Christian Couder85039fb2014-10-13 20:16:31 +0200228
229 strbuf_addstr(&cmd, command);
230 if (arg)
231 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
232
233 argv[0] = cmd.buf;
Christian Couder85039fb2014-10-13 20:16:31 +0200234 cp.argv = argv;
235 cp.env = local_repo_env;
236 cp.no_stdin = 1;
Christian Couder85039fb2014-10-13 20:16:31 +0200237 cp.use_shell = 1;
238
Jeff Kingc5eadca2015-03-22 23:54:00 -0400239 if (capture_command(&cp, &buf, 1024)) {
Nguyễn Thái Ngọc Duy13ad56f2016-02-27 13:42:10 +0700240 error(_("running trailer command '%s' failed"), cmd.buf);
Christian Couder85039fb2014-10-13 20:16:31 +0200241 strbuf_release(&buf);
242 result = xstrdup("");
Jeff Kingc5eadca2015-03-22 23:54:00 -0400243 } else {
244 strbuf_trim(&buf);
Christian Couder85039fb2014-10-13 20:16:31 +0200245 result = strbuf_detach(&buf, NULL);
Jeff Kingc5eadca2015-03-22 23:54:00 -0400246 }
Christian Couder85039fb2014-10-13 20:16:31 +0200247
248 strbuf_release(&cmd);
249 return result;
250}
251
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700252static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
Christian Couder85039fb2014-10-13 20:16:31 +0200253{
254 if (arg_tok->conf.command) {
255 const char *arg;
256 if (arg_tok->value && arg_tok->value[0]) {
257 arg = arg_tok->value;
258 } else {
259 if (in_tok && in_tok->value)
260 arg = xstrdup(in_tok->value);
261 else
262 arg = xstrdup("");
263 }
264 arg_tok->value = apply_command(arg_tok->conf.command, arg);
265 free((char *)arg);
266 }
267}
268
Christian Couder41038182014-10-13 20:16:24 +0200269static void apply_arg_if_exists(struct trailer_item *in_tok,
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700270 struct arg_item *arg_tok,
Christian Couder41038182014-10-13 20:16:24 +0200271 struct trailer_item *on_tok,
Jonathan Tan8966a392016-10-20 14:39:47 -0700272 struct list_head *head)
Christian Couder41038182014-10-13 20:16:24 +0200273{
274 switch (arg_tok->conf.if_exists) {
275 case EXISTS_DO_NOTHING:
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700276 free_arg_item(arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200277 break;
278 case EXISTS_REPLACE:
Christian Couder85039fb2014-10-13 20:16:31 +0200279 apply_item_command(in_tok, arg_tok);
Jonathan Tan8966a392016-10-20 14:39:47 -0700280 add_arg_to_input_list(on_tok, arg_tok);
281 list_del(&in_tok->list);
Christian Couder41038182014-10-13 20:16:24 +0200282 free_trailer_item(in_tok);
283 break;
284 case EXISTS_ADD:
Christian Couder85039fb2014-10-13 20:16:31 +0200285 apply_item_command(in_tok, arg_tok);
Jonathan Tan8966a392016-10-20 14:39:47 -0700286 add_arg_to_input_list(on_tok, arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200287 break;
288 case EXISTS_ADD_IF_DIFFERENT:
Christian Couder85039fb2014-10-13 20:16:31 +0200289 apply_item_command(in_tok, arg_tok);
Jonathan Tan8966a392016-10-20 14:39:47 -0700290 if (check_if_different(in_tok, arg_tok, 1, head))
291 add_arg_to_input_list(on_tok, arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200292 else
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700293 free_arg_item(arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200294 break;
295 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
Christian Couder85039fb2014-10-13 20:16:31 +0200296 apply_item_command(in_tok, arg_tok);
Jonathan Tan8966a392016-10-20 14:39:47 -0700297 if (check_if_different(on_tok, arg_tok, 0, head))
298 add_arg_to_input_list(on_tok, arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200299 else
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700300 free_arg_item(arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200301 break;
302 }
303}
304
Jonathan Tan8966a392016-10-20 14:39:47 -0700305static void apply_arg_if_missing(struct list_head *head,
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700306 struct arg_item *arg_tok)
Christian Couder41038182014-10-13 20:16:24 +0200307{
Christian Couder41038182014-10-13 20:16:24 +0200308 enum action_where where;
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700309 struct trailer_item *to_add;
Christian Couder41038182014-10-13 20:16:24 +0200310
311 switch (arg_tok->conf.if_missing) {
312 case MISSING_DO_NOTHING:
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700313 free_arg_item(arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200314 break;
315 case MISSING_ADD:
316 where = arg_tok->conf.where;
Christian Couder85039fb2014-10-13 20:16:31 +0200317 apply_item_command(NULL, arg_tok);
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700318 to_add = trailer_from_arg(arg_tok);
Jonathan Tan8966a392016-10-20 14:39:47 -0700319 if (after_or_end(where))
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700320 list_add_tail(&to_add->list, head);
Jonathan Tan8966a392016-10-20 14:39:47 -0700321 else
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700322 list_add(&to_add->list, head);
Christian Couder41038182014-10-13 20:16:24 +0200323 }
324}
325
Jonathan Tan8966a392016-10-20 14:39:47 -0700326static int find_same_and_apply_arg(struct list_head *head,
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700327 struct arg_item *arg_tok)
Christian Couder41038182014-10-13 20:16:24 +0200328{
Jonathan Tan8966a392016-10-20 14:39:47 -0700329 struct list_head *pos;
Christian Couder41038182014-10-13 20:16:24 +0200330 struct trailer_item *in_tok;
331 struct trailer_item *on_tok;
Christian Couder41038182014-10-13 20:16:24 +0200332
333 enum action_where where = arg_tok->conf.where;
334 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
335 int backwards = after_or_end(where);
Jonathan Tan8966a392016-10-20 14:39:47 -0700336 struct trailer_item *start_tok;
Christian Couder41038182014-10-13 20:16:24 +0200337
Jonathan Tan8966a392016-10-20 14:39:47 -0700338 if (list_empty(head))
339 return 0;
340
341 start_tok = list_entry(backwards ? head->prev : head->next,
342 struct trailer_item,
343 list);
344
345 list_for_each_dir(pos, head, backwards) {
346 in_tok = list_entry(pos, struct trailer_item, list);
Christian Couder41038182014-10-13 20:16:24 +0200347 if (!same_token(in_tok, arg_tok))
348 continue;
349 on_tok = middle ? in_tok : start_tok;
Jonathan Tan8966a392016-10-20 14:39:47 -0700350 apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
Christian Couder41038182014-10-13 20:16:24 +0200351 return 1;
352 }
353 return 0;
354}
355
Jonathan Tan8966a392016-10-20 14:39:47 -0700356static void process_trailers_lists(struct list_head *head,
357 struct list_head *arg_head)
Christian Couder41038182014-10-13 20:16:24 +0200358{
Jonathan Tan8966a392016-10-20 14:39:47 -0700359 struct list_head *pos, *p;
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700360 struct arg_item *arg_tok;
Christian Couder41038182014-10-13 20:16:24 +0200361
Jonathan Tan8966a392016-10-20 14:39:47 -0700362 list_for_each_safe(pos, p, arg_head) {
Christian Couder41038182014-10-13 20:16:24 +0200363 int applied = 0;
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700364 arg_tok = list_entry(pos, struct arg_item, list);
Christian Couder41038182014-10-13 20:16:24 +0200365
Jonathan Tan8966a392016-10-20 14:39:47 -0700366 list_del(pos);
Christian Couder41038182014-10-13 20:16:24 +0200367
Jonathan Tan8966a392016-10-20 14:39:47 -0700368 applied = find_same_and_apply_arg(head, arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200369
370 if (!applied)
Jonathan Tan8966a392016-10-20 14:39:47 -0700371 apply_arg_if_missing(head, arg_tok);
Christian Couder41038182014-10-13 20:16:24 +0200372 }
373}
Christian Couder46a06132014-10-13 20:16:25 +0200374
375static int set_where(struct conf_info *item, const char *value)
376{
377 if (!strcasecmp("after", value))
378 item->where = WHERE_AFTER;
379 else if (!strcasecmp("before", value))
380 item->where = WHERE_BEFORE;
381 else if (!strcasecmp("end", value))
382 item->where = WHERE_END;
383 else if (!strcasecmp("start", value))
384 item->where = WHERE_START;
385 else
386 return -1;
387 return 0;
388}
389
390static int set_if_exists(struct conf_info *item, const char *value)
391{
392 if (!strcasecmp("addIfDifferent", value))
393 item->if_exists = EXISTS_ADD_IF_DIFFERENT;
394 else if (!strcasecmp("addIfDifferentNeighbor", value))
395 item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
396 else if (!strcasecmp("add", value))
397 item->if_exists = EXISTS_ADD;
398 else if (!strcasecmp("replace", value))
399 item->if_exists = EXISTS_REPLACE;
400 else if (!strcasecmp("doNothing", value))
401 item->if_exists = EXISTS_DO_NOTHING;
402 else
403 return -1;
404 return 0;
405}
406
407static int set_if_missing(struct conf_info *item, const char *value)
408{
409 if (!strcasecmp("doNothing", value))
410 item->if_missing = MISSING_DO_NOTHING;
411 else if (!strcasecmp("add", value))
412 item->if_missing = MISSING_ADD;
413 else
414 return -1;
415 return 0;
416}
417
Jonathan Tand65fd422016-10-14 10:37:58 -0700418static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
Christian Couder46a06132014-10-13 20:16:25 +0200419{
420 *dst = *src;
Junio C Hamano13092a92016-10-12 11:20:23 -0700421 dst->name = xstrdup_or_null(src->name);
422 dst->key = xstrdup_or_null(src->key);
423 dst->command = xstrdup_or_null(src->command);
Christian Couder46a06132014-10-13 20:16:25 +0200424}
425
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700426static struct arg_item *get_conf_item(const char *name)
Christian Couder46a06132014-10-13 20:16:25 +0200427{
Jonathan Tan8966a392016-10-20 14:39:47 -0700428 struct list_head *pos;
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700429 struct arg_item *item;
Christian Couder46a06132014-10-13 20:16:25 +0200430
431 /* Look up item with same name */
Jonathan Tan8966a392016-10-20 14:39:47 -0700432 list_for_each(pos, &conf_head) {
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700433 item = list_entry(pos, struct arg_item, list);
Christian Couder46a06132014-10-13 20:16:25 +0200434 if (!strcasecmp(item->conf.name, name))
435 return item;
436 }
437
438 /* Item does not already exists, create it */
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700439 item = xcalloc(sizeof(*item), 1);
Christian Couder46a06132014-10-13 20:16:25 +0200440 duplicate_conf(&item->conf, &default_conf_info);
441 item->conf.name = xstrdup(name);
442
Jonathan Tan8966a392016-10-20 14:39:47 -0700443 list_add_tail(&item->list, &conf_head);
Christian Couder46a06132014-10-13 20:16:25 +0200444
445 return item;
446}
447
448enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
449 TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
450
451static struct {
452 const char *name;
453 enum trailer_info_type type;
454} trailer_config_items[] = {
455 { "key", TRAILER_KEY },
456 { "command", TRAILER_COMMAND },
457 { "where", TRAILER_WHERE },
458 { "ifexists", TRAILER_IF_EXISTS },
459 { "ifmissing", TRAILER_IF_MISSING }
460};
461
462static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
463{
464 const char *trailer_item, *variable_name;
465
466 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
467 return 0;
468
469 variable_name = strrchr(trailer_item, '.');
470 if (!variable_name) {
471 if (!strcmp(trailer_item, "where")) {
472 if (set_where(&default_conf_info, value) < 0)
473 warning(_("unknown value '%s' for key '%s'"),
474 value, conf_key);
475 } else if (!strcmp(trailer_item, "ifexists")) {
476 if (set_if_exists(&default_conf_info, value) < 0)
477 warning(_("unknown value '%s' for key '%s'"),
478 value, conf_key);
479 } else if (!strcmp(trailer_item, "ifmissing")) {
480 if (set_if_missing(&default_conf_info, value) < 0)
481 warning(_("unknown value '%s' for key '%s'"),
482 value, conf_key);
483 } else if (!strcmp(trailer_item, "separators")) {
484 separators = xstrdup(value);
485 }
486 }
487 return 0;
488}
489
490static int git_trailer_config(const char *conf_key, const char *value, void *cb)
491{
492 const char *trailer_item, *variable_name;
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700493 struct arg_item *item;
Christian Couder46a06132014-10-13 20:16:25 +0200494 struct conf_info *conf;
495 char *name = NULL;
496 enum trailer_info_type type;
497 int i;
498
499 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
500 return 0;
501
502 variable_name = strrchr(trailer_item, '.');
503 if (!variable_name)
504 return 0;
505
506 variable_name++;
507 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
508 if (strcmp(trailer_config_items[i].name, variable_name))
509 continue;
510 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
511 type = trailer_config_items[i].type;
512 break;
513 }
514
515 if (!name)
516 return 0;
517
518 item = get_conf_item(name);
519 conf = &item->conf;
520 free(name);
521
522 switch (type) {
523 case TRAILER_KEY:
524 if (conf->key)
525 warning(_("more than one %s"), conf_key);
526 conf->key = xstrdup(value);
527 break;
528 case TRAILER_COMMAND:
529 if (conf->command)
530 warning(_("more than one %s"), conf_key);
531 conf->command = xstrdup(value);
532 break;
533 case TRAILER_WHERE:
534 if (set_where(conf, value))
535 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
536 break;
537 case TRAILER_IF_EXISTS:
538 if (set_if_exists(conf, value))
539 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
540 break;
541 case TRAILER_IF_MISSING:
542 if (set_if_missing(conf, value))
543 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
544 break;
545 default:
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200546 die("BUG: trailer.c: unhandled type %d", type);
Christian Couder46a06132014-10-13 20:16:25 +0200547 }
548 return 0;
549}
Christian Couderf0a90b42014-10-13 20:16:26 +0200550
Jonathan Tane8c352c2016-11-02 10:29:19 -0700551static void ensure_configured(void)
552{
553 if (configured)
554 return;
555
556 /* Default config must be setup first */
557 git_config(git_trailer_default_config, NULL);
558 git_config(git_trailer_config, NULL);
559 configured = 1;
560}
561
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700562static const char *token_from_item(struct arg_item *item, char *tok)
Christian Couderf0a90b42014-10-13 20:16:26 +0200563{
564 if (item->conf.key)
565 return item->conf.key;
566 if (tok)
567 return tok;
568 return item->conf.name;
569}
570
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700571static int token_matches_item(const char *tok, struct arg_item *item, int tok_len)
Christian Couderf0a90b42014-10-13 20:16:26 +0200572{
573 if (!strncasecmp(tok, item->conf.name, tok_len))
574 return 1;
575 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
576}
577
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700578/*
Jonathan Tane4319562016-11-02 10:29:16 -0700579 * If the given line is of the form
580 * "<token><optional whitespace><separator>..." or "<separator>...", return the
581 * location of the separator. Otherwise, return -1. The optional whitespace
582 * is allowed there primarily to allow things like "Bug #43" where <token> is
583 * "Bug" and <separator> is "#".
584 *
585 * The separator-starts-line case (in which this function returns 0) is
586 * distinguished from the non-well-formed-line case (in which this function
587 * returns -1) because some callers of this function need such a distinction.
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700588 */
589static int find_separator(const char *line, const char *separators)
Christian Couderf0a90b42014-10-13 20:16:26 +0200590{
Jonathan Tane4319562016-11-02 10:29:16 -0700591 int whitespace_found = 0;
592 const char *c;
593 for (c = line; *c; c++) {
594 if (strchr(separators, *c))
595 return c - line;
596 if (!whitespace_found && (isalnum(*c) || *c == '-'))
597 continue;
598 if (c != line && (*c == ' ' || *c == '\t')) {
599 whitespace_found = 1;
600 continue;
601 }
602 break;
603 }
604 return -1;
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700605}
606
607/*
608 * Obtain the token, value, and conf from the given trailer.
609 *
610 * separator_pos must not be 0, since the token cannot be an empty string.
611 *
612 * If separator_pos is -1, interpret the whole trailer as a token.
613 */
614static void parse_trailer(struct strbuf *tok, struct strbuf *val,
615 const struct conf_info **conf, const char *trailer,
616 int separator_pos)
617{
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700618 struct arg_item *item;
Christian Couderf0a90b42014-10-13 20:16:26 +0200619 int tok_len;
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700620 struct list_head *pos;
Christian Couderf0a90b42014-10-13 20:16:26 +0200621
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700622 if (separator_pos != -1) {
623 strbuf_add(tok, trailer, separator_pos);
Christian Couderf0a90b42014-10-13 20:16:26 +0200624 strbuf_trim(tok);
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700625 strbuf_addstr(val, trailer + separator_pos + 1);
Christian Couderf0a90b42014-10-13 20:16:26 +0200626 strbuf_trim(val);
627 } else {
628 strbuf_addstr(tok, trailer);
629 strbuf_trim(tok);
630 }
Christian Couderf0a90b42014-10-13 20:16:26 +0200631
632 /* Lookup if the token matches something in the config */
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700633 tok_len = token_len_without_separator(tok->buf, tok->len);
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700634 if (conf)
635 *conf = &default_conf_info;
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700636 list_for_each(pos, &conf_head) {
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700637 item = list_entry(pos, struct arg_item, list);
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700638 if (token_matches_item(tok->buf, item, tok_len)) {
639 char *tok_buf = strbuf_detach(tok, NULL);
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700640 if (conf)
641 *conf = &item->conf;
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700642 strbuf_addstr(tok, token_from_item(item, tok_buf));
643 free(tok_buf);
644 break;
645 }
Christian Couderf0a90b42014-10-13 20:16:26 +0200646 }
647}
648
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700649static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
650 char *val)
Christian Couderf0a90b42014-10-13 20:16:26 +0200651{
Christian Couderf0a90b42014-10-13 20:16:26 +0200652 struct trailer_item *new = xcalloc(sizeof(*new), 1);
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700653 new->token = tok;
654 new->value = val;
Jonathan Tan8966a392016-10-20 14:39:47 -0700655 list_add_tail(&new->list, head);
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700656 return new;
Christian Couderf0a90b42014-10-13 20:16:26 +0200657}
658
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700659static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
660 const struct conf_info *conf)
661{
662 struct arg_item *new = xcalloc(sizeof(*new), 1);
663 new->token = tok;
664 new->value = val;
665 duplicate_conf(&new->conf, conf);
666 list_add_tail(&new->list, arg_head);
667}
668
Jonathan Tan8966a392016-10-20 14:39:47 -0700669static void process_command_line_args(struct list_head *arg_head,
670 struct string_list *trailers)
Christian Couderf0a90b42014-10-13 20:16:26 +0200671{
Christian Couderf0a90b42014-10-13 20:16:26 +0200672 struct string_list_item *tr;
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700673 struct arg_item *item;
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700674 struct strbuf tok = STRBUF_INIT;
675 struct strbuf val = STRBUF_INIT;
676 const struct conf_info *conf;
Jonathan Tan8966a392016-10-20 14:39:47 -0700677 struct list_head *pos;
Christian Couderf0a90b42014-10-13 20:16:26 +0200678
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700679 /*
680 * In command-line arguments, '=' is accepted (in addition to the
681 * separators that are defined).
682 */
683 char *cl_separators = xstrfmt("=%s", separators);
684
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700685 /* Add an arg item for each configured trailer with a command */
Jonathan Tan8966a392016-10-20 14:39:47 -0700686 list_for_each(pos, &conf_head) {
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700687 item = list_entry(pos, struct arg_item, list);
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700688 if (item->conf.command)
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700689 add_arg_item(arg_head,
690 xstrdup(token_from_item(item, NULL)),
691 xstrdup(""),
692 &item->conf);
Christian Couder85039fb2014-10-13 20:16:31 +0200693 }
694
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700695 /* Add an arg item for each trailer on the command line */
Christian Couderf0a90b42014-10-13 20:16:26 +0200696 for_each_string_list_item(tr, trailers) {
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700697 int separator_pos = find_separator(tr->string, cl_separators);
698 if (separator_pos == 0) {
699 struct strbuf sb = STRBUF_INIT;
700 strbuf_addstr(&sb, tr->string);
701 strbuf_trim(&sb);
702 error(_("empty trailer token in trailer '%.*s'"),
703 (int) sb.len, sb.buf);
704 strbuf_release(&sb);
705 } else {
706 parse_trailer(&tok, &val, &conf, tr->string,
707 separator_pos);
Jonathan Tancc71b0d2016-10-20 14:39:49 -0700708 add_arg_item(arg_head,
709 strbuf_detach(&tok, NULL),
710 strbuf_detach(&val, NULL),
711 conf);
Christian Couder2013d852014-10-13 20:16:27 +0200712 }
713 }
714
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700715 free(cl_separators);
Christian Couderf0a90b42014-10-13 20:16:26 +0200716}
Christian Couder2013d852014-10-13 20:16:27 +0200717
Jonathan Tan022349c2016-11-02 10:29:18 -0700718static void read_input_file(struct strbuf *sb, const char *file)
Christian Couder2013d852014-10-13 20:16:27 +0200719{
Christian Couder2013d852014-10-13 20:16:27 +0200720 if (file) {
Jonathan Tan022349c2016-11-02 10:29:18 -0700721 if (strbuf_read_file(sb, file, 0) < 0)
Christian Couder2013d852014-10-13 20:16:27 +0200722 die_errno(_("could not read input file '%s'"), file);
723 } else {
Jonathan Tan022349c2016-11-02 10:29:18 -0700724 if (strbuf_read(sb, fileno(stdin), 0) < 0)
Christian Couder2013d852014-10-13 20:16:27 +0200725 die_errno(_("could not read from stdin"));
726 }
Jonathan Tan022349c2016-11-02 10:29:18 -0700727}
Christian Couder2013d852014-10-13 20:16:27 +0200728
Jonathan Tan022349c2016-11-02 10:29:18 -0700729static const char *next_line(const char *str)
730{
731 const char *nl = strchrnul(str, '\n');
732 return nl + !!*nl;
Christian Couder2013d852014-10-13 20:16:27 +0200733}
734
735/*
Jonathan Tan022349c2016-11-02 10:29:18 -0700736 * Return the position of the start of the last line. If len is 0, return -1.
Christian Couder2013d852014-10-13 20:16:27 +0200737 */
Jonathan Tan022349c2016-11-02 10:29:18 -0700738static int last_line(const char *buf, size_t len)
Christian Couder2013d852014-10-13 20:16:27 +0200739{
740 int i;
Jonathan Tan022349c2016-11-02 10:29:18 -0700741 if (len == 0)
742 return -1;
743 if (len == 1)
744 return 0;
745 /*
746 * Skip the last character (in addition to the null terminator),
747 * because if the last character is a newline, it is considered as part
748 * of the last line anyway.
749 */
750 i = len - 2;
Christian Couder2013d852014-10-13 20:16:27 +0200751
Jonathan Tan022349c2016-11-02 10:29:18 -0700752 for (; i >= 0; i--) {
753 if (buf[i] == '\n')
754 return i + 1;
Christian Couder2013d852014-10-13 20:16:27 +0200755 }
Jonathan Tan022349c2016-11-02 10:29:18 -0700756 return 0;
Christian Couder2013d852014-10-13 20:16:27 +0200757}
758
759/*
Jonathan Tan022349c2016-11-02 10:29:18 -0700760 * Return the position of the start of the patch or the length of str if there
761 * is no patch in the message.
Christian Couder2013d852014-10-13 20:16:27 +0200762 */
Jonathan Tan022349c2016-11-02 10:29:18 -0700763static int find_patch_start(const char *str)
Christian Couder2013d852014-10-13 20:16:27 +0200764{
Jonathan Tan022349c2016-11-02 10:29:18 -0700765 const char *s;
766
767 for (s = str; *s; s = next_line(s)) {
768 if (starts_with(s, "---"))
769 return s - str;
770 }
771
772 return s - str;
773}
774
775/*
776 * Return the position of the first trailer line or len if there are no
777 * trailers.
778 */
779static int find_trailer_start(const char *buf, size_t len)
780{
781 const char *s;
782 int end_of_title, l, only_spaces = 1;
Jonathan Tan14624502016-10-21 10:55:01 -0700783 int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700784 /*
785 * Number of possible continuation lines encountered. This will be
786 * reset to 0 if we encounter a trailer (since those lines are to be
787 * considered continuations of that trailer), and added to
788 * non_trailer_lines if we encounter a non-trailer (since those lines
789 * are to be considered non-trailers).
790 */
791 int possible_continuation_lines = 0;
Christian Couder5c999952015-08-30 21:14:40 +0200792
793 /* The first paragraph is the title and cannot be trailers */
Jonathan Tan022349c2016-11-02 10:29:18 -0700794 for (s = buf; s < buf + len; s = next_line(s)) {
795 if (s[0] == comment_line_char)
Christian Couder5c999952015-08-30 21:14:40 +0200796 continue;
Jonathan Tan022349c2016-11-02 10:29:18 -0700797 if (is_blank_line(s))
Christian Couder5c999952015-08-30 21:14:40 +0200798 break;
799 }
Jonathan Tan022349c2016-11-02 10:29:18 -0700800 end_of_title = s - buf;
Christian Couder2013d852014-10-13 20:16:27 +0200801
802 /*
Jonathan Tan14624502016-10-21 10:55:01 -0700803 * Get the start of the trailers by looking starting from the end for a
804 * blank line before a set of non-blank lines that (i) are all
805 * trailers, or (ii) contains at least one Git-generated trailer and
806 * consists of at least 25% trailers.
Christian Couder2013d852014-10-13 20:16:27 +0200807 */
Jonathan Tan022349c2016-11-02 10:29:18 -0700808 for (l = last_line(buf, len);
809 l >= end_of_title;
810 l = last_line(buf, l)) {
811 const char *bol = buf + l;
Jonathan Tan14624502016-10-21 10:55:01 -0700812 const char **p;
813 int separator_pos;
814
Jonathan Tan022349c2016-11-02 10:29:18 -0700815 if (bol[0] == comment_line_char) {
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700816 non_trailer_lines += possible_continuation_lines;
817 possible_continuation_lines = 0;
Christian Couder2013d852014-10-13 20:16:27 +0200818 continue;
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700819 }
Jonathan Tan022349c2016-11-02 10:29:18 -0700820 if (is_blank_line(bol)) {
Christian Couder2013d852014-10-13 20:16:27 +0200821 if (only_spaces)
822 continue;
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700823 non_trailer_lines += possible_continuation_lines;
Jonathan Tan14624502016-10-21 10:55:01 -0700824 if (recognized_prefix &&
825 trailer_lines * 3 >= non_trailer_lines)
Jonathan Tan022349c2016-11-02 10:29:18 -0700826 return next_line(bol) - buf;
827 else if (trailer_lines && !non_trailer_lines)
828 return next_line(bol) - buf;
829 return len;
Christian Couder2013d852014-10-13 20:16:27 +0200830 }
Jonathan Tan14624502016-10-21 10:55:01 -0700831 only_spaces = 0;
832
833 for (p = git_generated_prefixes; *p; p++) {
Jonathan Tan022349c2016-11-02 10:29:18 -0700834 if (starts_with(bol, *p)) {
Jonathan Tan14624502016-10-21 10:55:01 -0700835 trailer_lines++;
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700836 possible_continuation_lines = 0;
Jonathan Tan14624502016-10-21 10:55:01 -0700837 recognized_prefix = 1;
838 goto continue_outer_loop;
839 }
Christian Couder2013d852014-10-13 20:16:27 +0200840 }
Jonathan Tan14624502016-10-21 10:55:01 -0700841
Jonathan Tan022349c2016-11-02 10:29:18 -0700842 separator_pos = find_separator(bol, separators);
843 if (separator_pos >= 1 && !isspace(bol[0])) {
Jonathan Tan14624502016-10-21 10:55:01 -0700844 struct list_head *pos;
845
846 trailer_lines++;
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700847 possible_continuation_lines = 0;
Jonathan Tan14624502016-10-21 10:55:01 -0700848 if (recognized_prefix)
849 continue;
850 list_for_each(pos, &conf_head) {
851 struct arg_item *item;
852 item = list_entry(pos, struct arg_item, list);
Jonathan Tan022349c2016-11-02 10:29:18 -0700853 if (token_matches_item(bol, item,
Jonathan Tan14624502016-10-21 10:55:01 -0700854 separator_pos)) {
855 recognized_prefix = 1;
856 break;
857 }
858 }
Jonathan Tan022349c2016-11-02 10:29:18 -0700859 } else if (isspace(bol[0]))
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700860 possible_continuation_lines++;
861 else {
Jonathan Tan14624502016-10-21 10:55:01 -0700862 non_trailer_lines++;
Jonathan Tan60ef86a2016-10-21 10:55:03 -0700863 non_trailer_lines += possible_continuation_lines;
864 possible_continuation_lines = 0;
865 }
Jonathan Tan14624502016-10-21 10:55:01 -0700866continue_outer_loop:
867 ;
Christian Couder2013d852014-10-13 20:16:27 +0200868 }
869
Jonathan Tan022349c2016-11-02 10:29:18 -0700870 return len;
Christian Couder2013d852014-10-13 20:16:27 +0200871}
872
Jonathan Tan022349c2016-11-02 10:29:18 -0700873/* Return the position of the end of the trailers. */
874static int find_trailer_end(const char *buf, size_t len)
Christian Couder61cfef42014-11-09 10:23:42 +0100875{
Jonathan Tan022349c2016-11-02 10:29:18 -0700876 return len - ignore_non_trailer(buf, len);
Christian Couder61cfef42014-11-09 10:23:42 +0100877}
878
Jonathan Tan022349c2016-11-02 10:29:18 -0700879static int ends_with_blank_line(const char *buf, size_t len)
Christian Couder2013d852014-10-13 20:16:27 +0200880{
Jonathan Tan022349c2016-11-02 10:29:18 -0700881 int ll = last_line(buf, len);
882 if (ll < 0)
883 return 0;
884 return is_blank_line(buf + ll);
Christian Couder2013d852014-10-13 20:16:27 +0200885}
886
Tobias Klauserd0d23442016-01-14 17:57:54 +0100887static int process_input_file(FILE *outfile,
Jonathan Tan022349c2016-11-02 10:29:18 -0700888 const char *str,
Jonathan Tan8966a392016-10-20 14:39:47 -0700889 struct list_head *head)
Christian Couder2013d852014-10-13 20:16:27 +0200890{
Jonathan Tane8c352c2016-11-02 10:29:19 -0700891 struct trailer_info info;
Jonathan Tan63ab3f32016-10-20 14:39:48 -0700892 struct strbuf tok = STRBUF_INIT;
893 struct strbuf val = STRBUF_INIT;
Jonathan Tane8c352c2016-11-02 10:29:19 -0700894 int i;
Christian Couder2013d852014-10-13 20:16:27 +0200895
Jonathan Tane8c352c2016-11-02 10:29:19 -0700896 trailer_info_get(&info, str);
Christian Couder2013d852014-10-13 20:16:27 +0200897
898 /* Print lines before the trailers as is */
Jonathan Tane8c352c2016-11-02 10:29:19 -0700899 fwrite(str, 1, info.trailer_start - str, outfile);
Christian Couder2013d852014-10-13 20:16:27 +0200900
Jonathan Tane8c352c2016-11-02 10:29:19 -0700901 if (!info.blank_line_before_trailer)
Tobias Klauserd0d23442016-01-14 17:57:54 +0100902 fprintf(outfile, "\n");
Christian Couder2013d852014-10-13 20:16:27 +0200903
Jonathan Tane8c352c2016-11-02 10:29:19 -0700904 for (i = 0; i < info.trailer_nr; i++) {
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700905 int separator_pos;
Jonathan Tane8c352c2016-11-02 10:29:19 -0700906 char *trailer = info.trailers[i];
907 if (trailer[0] == comment_line_char)
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700908 continue;
Jonathan Tane8c352c2016-11-02 10:29:19 -0700909 separator_pos = find_separator(trailer, separators);
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700910 if (separator_pos >= 1) {
Jonathan Tane8c352c2016-11-02 10:29:19 -0700911 parse_trailer(&tok, &val, NULL, trailer,
Jonathan Tanfdbf4512016-10-21 10:55:00 -0700912 separator_pos);
Jonathan Tane8c352c2016-11-02 10:29:19 -0700913 add_trailer_item(head,
914 strbuf_detach(&tok, NULL),
915 strbuf_detach(&val, NULL));
Jonathan Tan14624502016-10-21 10:55:01 -0700916 } else {
Jonathan Tane8c352c2016-11-02 10:29:19 -0700917 strbuf_addstr(&val, trailer);
Jonathan Tan14624502016-10-21 10:55:01 -0700918 strbuf_strip_suffix(&val, "\n");
919 add_trailer_item(head,
920 NULL,
921 strbuf_detach(&val, NULL));
Christian Couder28871032014-11-09 10:23:39 +0100922 }
Christian Couder2013d852014-10-13 20:16:27 +0200923 }
924
Jonathan Tane8c352c2016-11-02 10:29:19 -0700925 trailer_info_release(&info);
926
927 return info.trailer_end - str;
Christian Couder2013d852014-10-13 20:16:27 +0200928}
Christian Couderb1d78d72014-10-13 20:16:28 +0200929
Jonathan Tan8966a392016-10-20 14:39:47 -0700930static void free_all(struct list_head *head)
Christian Couderb1d78d72014-10-13 20:16:28 +0200931{
Jonathan Tan8966a392016-10-20 14:39:47 -0700932 struct list_head *pos, *p;
933 list_for_each_safe(pos, p, head) {
934 list_del(pos);
935 free_trailer_item(list_entry(pos, struct trailer_item, list));
Christian Couderb1d78d72014-10-13 20:16:28 +0200936 }
937}
938
Tobias Klausere1f89862016-01-14 17:57:55 +0100939static struct tempfile trailers_tempfile;
940
941static FILE *create_in_place_tempfile(const char *file)
942{
943 struct stat st;
944 struct strbuf template = STRBUF_INIT;
945 const char *tail;
946 FILE *outfile;
947
948 if (stat(file, &st))
949 die_errno(_("could not stat %s"), file);
950 if (!S_ISREG(st.st_mode))
951 die(_("file %s is not a regular file"), file);
952 if (!(st.st_mode & S_IWUSR))
953 die(_("file %s is not writable by user"), file);
954
955 /* Create temporary file in the same directory as the original */
956 tail = strrchr(file, '/');
957 if (tail != NULL)
958 strbuf_add(&template, file, tail - file + 1);
959 strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
960
961 xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
962 strbuf_release(&template);
963 outfile = fdopen_tempfile(&trailers_tempfile, "w");
964 if (!outfile)
965 die_errno(_("could not open temporary file"));
966
967 return outfile;
968}
969
970void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
Christian Couderb1d78d72014-10-13 20:16:28 +0200971{
Jonathan Tan8966a392016-10-20 14:39:47 -0700972 LIST_HEAD(head);
973 LIST_HEAD(arg_head);
Jonathan Tan022349c2016-11-02 10:29:18 -0700974 struct strbuf sb = STRBUF_INIT;
Christian Couder61cfef42014-11-09 10:23:42 +0100975 int trailer_end;
Tobias Klauserd0d23442016-01-14 17:57:54 +0100976 FILE *outfile = stdout;
Christian Couderb1d78d72014-10-13 20:16:28 +0200977
Jonathan Tane8c352c2016-11-02 10:29:19 -0700978 ensure_configured();
Christian Couderb1d78d72014-10-13 20:16:28 +0200979
Jonathan Tan022349c2016-11-02 10:29:18 -0700980 read_input_file(&sb, file);
Christian Couderb1d78d72014-10-13 20:16:28 +0200981
Tobias Klausere1f89862016-01-14 17:57:55 +0100982 if (in_place)
983 outfile = create_in_place_tempfile(file);
984
Christian Couderb1d78d72014-10-13 20:16:28 +0200985 /* Print the lines before the trailers */
Jonathan Tan022349c2016-11-02 10:29:18 -0700986 trailer_end = process_input_file(outfile, sb.buf, &head);
Christian Couderb1d78d72014-10-13 20:16:28 +0200987
Jonathan Tan8966a392016-10-20 14:39:47 -0700988 process_command_line_args(&arg_head, trailers);
Christian Couderb1d78d72014-10-13 20:16:28 +0200989
Jonathan Tan8966a392016-10-20 14:39:47 -0700990 process_trailers_lists(&head, &arg_head);
Christian Couderb1d78d72014-10-13 20:16:28 +0200991
Jonathan Tan8966a392016-10-20 14:39:47 -0700992 print_all(outfile, &head, trim_empty);
Christian Couderb1d78d72014-10-13 20:16:28 +0200993
Jonathan Tan8966a392016-10-20 14:39:47 -0700994 free_all(&head);
Christian Couderb1d78d72014-10-13 20:16:28 +0200995
996 /* Print the lines after the trailers as is */
Jonathan Tan022349c2016-11-02 10:29:18 -0700997 fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
Christian Couderb1d78d72014-10-13 20:16:28 +0200998
Tobias Klausere1f89862016-01-14 17:57:55 +0100999 if (in_place)
1000 if (rename_tempfile(&trailers_tempfile, file))
1001 die_errno(_("could not rename temporary file to %s"), file);
1002
Jonathan Tan022349c2016-11-02 10:29:18 -07001003 strbuf_release(&sb);
Christian Couderb1d78d72014-10-13 20:16:28 +02001004}
Jonathan Tane8c352c2016-11-02 10:29:19 -07001005
1006void trailer_info_get(struct trailer_info *info, const char *str)
1007{
1008 int patch_start, trailer_end, trailer_start;
1009 struct strbuf **trailer_lines, **ptr;
1010 char **trailer_strings = NULL;
1011 size_t nr = 0, alloc = 0;
1012 char **last = NULL;
1013
1014 ensure_configured();
1015
1016 patch_start = find_patch_start(str);
1017 trailer_end = find_trailer_end(str, patch_start);
1018 trailer_start = find_trailer_start(str, trailer_end);
1019
1020 trailer_lines = strbuf_split_buf(str + trailer_start,
1021 trailer_end - trailer_start,
1022 '\n',
1023 0);
1024 for (ptr = trailer_lines; *ptr; ptr++) {
1025 if (last && isspace((*ptr)->buf[0])) {
1026 struct strbuf sb = STRBUF_INIT;
1027 strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1028 strbuf_addbuf(&sb, *ptr);
1029 *last = strbuf_detach(&sb, NULL);
1030 continue;
1031 }
1032 ALLOC_GROW(trailer_strings, nr + 1, alloc);
1033 trailer_strings[nr] = strbuf_detach(*ptr, NULL);
1034 last = find_separator(trailer_strings[nr], separators) >= 1
1035 ? &trailer_strings[nr]
1036 : NULL;
1037 nr++;
1038 }
1039 strbuf_list_free(trailer_lines);
1040
1041 info->blank_line_before_trailer = ends_with_blank_line(str,
1042 trailer_start);
1043 info->trailer_start = str + trailer_start;
1044 info->trailer_end = str + trailer_end;
1045 info->trailers = trailer_strings;
1046 info->trailer_nr = nr;
1047}
1048
1049void trailer_info_release(struct trailer_info *info)
1050{
1051 int i;
1052 for (i = 0; i < info->trailer_nr; i++)
1053 free(info->trailers[i]);
1054 free(info->trailers);
1055}