l10n: fr.po v2.11.0_rnd1
[gitweb.git] / trailer.c
index 0b3716f514299ca9137d2e452df5445c914a274a..f0ecde2d2f8b35c565ab176c76882a5fee8d8fb2 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -26,6 +26,16 @@ struct conf_info {
 static struct conf_info default_conf_info;
 
 struct trailer_item {
+       struct list_head list;
+       /*
+        * If this is not a trailer line, the line is stored in value
+        * (excluding the terminating newline) and token is NULL.
+        */
+       char *token;
+       char *value;
+};
+
+struct arg_item {
        struct list_head list;
        char *token;
        char *value;
@@ -38,6 +48,12 @@ static char *separators = ":";
 
 #define TRAILER_ARG_STRING "$ARG"
 
+static const char *git_generated_prefixes[] = {
+       "Signed-off-by: ",
+       "(cherry picked from commit ",
+       NULL
+};
+
 /* Iterate over the elements of the list. */
 #define list_for_each_dir(pos, head, is_reverse) \
        for (pos = is_reverse ? (head)->prev : (head)->next; \
@@ -62,21 +78,26 @@ static size_t token_len_without_separator(const char *token, size_t len)
        return len;
 }
 
-static int same_token(struct trailer_item *a, struct trailer_item *b)
+static int same_token(struct trailer_item *a, struct arg_item *b)
 {
-       size_t a_len = token_len_without_separator(a->token, strlen(a->token));
-       size_t b_len = token_len_without_separator(b->token, strlen(b->token));
-       size_t min_len = (a_len > b_len) ? b_len : a_len;
+       size_t a_len, b_len, min_len;
+
+       if (!a->token)
+               return 0;
+
+       a_len = token_len_without_separator(a->token, strlen(a->token));
+       b_len = token_len_without_separator(b->token, strlen(b->token));
+       min_len = (a_len > b_len) ? b_len : a_len;
 
        return !strncasecmp(a->token, b->token, min_len);
 }
 
-static int same_value(struct trailer_item *a, struct trailer_item *b)
+static int same_value(struct trailer_item *a, struct arg_item *b)
 {
        return !strcasecmp(a->value, b->value);
 }
 
-static int same_trailer(struct trailer_item *a, struct trailer_item *b)
+static int same_trailer(struct trailer_item *a, struct arg_item *b)
 {
        return same_token(a, b) && same_value(a, b);
 }
@@ -97,6 +118,13 @@ static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *
 }
 
 static void free_trailer_item(struct trailer_item *item)
+{
+       free(item->token);
+       free(item->value);
+       free(item);
+}
+
+static void free_arg_item(struct arg_item *item)
 {
        free(item->conf.name);
        free(item->conf.key);
@@ -117,7 +145,14 @@ static char last_non_space_char(const char *s)
 
 static void print_tok_val(FILE *outfile, const char *tok, const char *val)
 {
-       char c = last_non_space_char(tok);
+       char c;
+
+       if (!tok) {
+               fprintf(outfile, "%s\n", val);
+               return;
+       }
+
+       c = last_non_space_char(tok);
        if (!c)
                return;
        if (strchr(separators, c))
@@ -137,17 +172,29 @@ static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
        }
 }
 
+static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
+{
+       struct trailer_item *new = xcalloc(sizeof(*new), 1);
+       new->token = arg_tok->token;
+       new->value = arg_tok->value;
+       arg_tok->token = arg_tok->value = NULL;
+       free_arg_item(arg_tok);
+       return new;
+}
+
 static void add_arg_to_input_list(struct trailer_item *on_tok,
-                                 struct trailer_item *arg_tok)
+                                 struct arg_item *arg_tok)
 {
-       if (after_or_end(arg_tok->conf.where))
-               list_add(&arg_tok->list, &on_tok->list);
+       int aoe = after_or_end(arg_tok->conf.where);
+       struct trailer_item *to_add = trailer_from_arg(arg_tok);
+       if (aoe)
+               list_add(&to_add->list, &on_tok->list);
        else
-               list_add_tail(&arg_tok->list, &on_tok->list);
+               list_add_tail(&to_add->list, &on_tok->list);
 }
 
 static int check_if_different(struct trailer_item *in_tok,
-                             struct trailer_item *arg_tok,
+                             struct arg_item *arg_tok,
                              int check_all,
                              struct list_head *head)
 {
@@ -200,7 +247,7 @@ static char *apply_command(const char *command, const char *arg)
        return result;
 }
 
-static void apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
+static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
 {
        if (arg_tok->conf.command) {
                const char *arg;
@@ -218,13 +265,13 @@ static void apply_item_command(struct trailer_item *in_tok, struct trailer_item
 }
 
 static void apply_arg_if_exists(struct trailer_item *in_tok,
-                               struct trailer_item *arg_tok,
+                               struct arg_item *arg_tok,
                                struct trailer_item *on_tok,
                                struct list_head *head)
 {
        switch (arg_tok->conf.if_exists) {
        case EXISTS_DO_NOTHING:
-               free_trailer_item(arg_tok);
+               free_arg_item(arg_tok);
                break;
        case EXISTS_REPLACE:
                apply_item_command(in_tok, arg_tok);
@@ -241,39 +288,41 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
                if (check_if_different(in_tok, arg_tok, 1, head))
                        add_arg_to_input_list(on_tok, arg_tok);
                else
-                       free_trailer_item(arg_tok);
+                       free_arg_item(arg_tok);
                break;
        case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
                apply_item_command(in_tok, arg_tok);
                if (check_if_different(on_tok, arg_tok, 0, head))
                        add_arg_to_input_list(on_tok, arg_tok);
                else
-                       free_trailer_item(arg_tok);
+                       free_arg_item(arg_tok);
                break;
        }
 }
 
 static void apply_arg_if_missing(struct list_head *head,
-                                struct trailer_item *arg_tok)
+                                struct arg_item *arg_tok)
 {
        enum action_where where;
+       struct trailer_item *to_add;
 
        switch (arg_tok->conf.if_missing) {
        case MISSING_DO_NOTHING:
-               free_trailer_item(arg_tok);
+               free_arg_item(arg_tok);
                break;
        case MISSING_ADD:
                where = arg_tok->conf.where;
                apply_item_command(NULL, arg_tok);
+               to_add = trailer_from_arg(arg_tok);
                if (after_or_end(where))
-                       list_add_tail(&arg_tok->list, head);
+                       list_add_tail(&to_add->list, head);
                else
-                       list_add(&arg_tok->list, head);
+                       list_add(&to_add->list, head);
        }
 }
 
 static int find_same_and_apply_arg(struct list_head *head,
-                                  struct trailer_item *arg_tok)
+                                  struct arg_item *arg_tok)
 {
        struct list_head *pos;
        struct trailer_item *in_tok;
@@ -306,11 +355,11 @@ static void process_trailers_lists(struct list_head *head,
                                   struct list_head *arg_head)
 {
        struct list_head *pos, *p;
-       struct trailer_item *arg_tok;
+       struct arg_item *arg_tok;
 
        list_for_each_safe(pos, p, arg_head) {
                int applied = 0;
-               arg_tok = list_entry(pos, struct trailer_item, list);
+               arg_tok = list_entry(pos, struct arg_item, list);
 
                list_del(pos);
 
@@ -367,28 +416,25 @@ static int set_if_missing(struct conf_info *item, const char *value)
 static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
 {
        *dst = *src;
-       if (src->name)
-               dst->name = xstrdup(src->name);
-       if (src->key)
-               dst->key = xstrdup(src->key);
-       if (src->command)
-               dst->command = xstrdup(src->command);
+       dst->name = xstrdup_or_null(src->name);
+       dst->key = xstrdup_or_null(src->key);
+       dst->command = xstrdup_or_null(src->command);
 }
 
-static struct trailer_item *get_conf_item(const char *name)
+static struct arg_item *get_conf_item(const char *name)
 {
        struct list_head *pos;
-       struct trailer_item *item;
+       struct arg_item *item;
 
        /* Look up item with same name */
        list_for_each(pos, &conf_head) {
-               item = list_entry(pos, struct trailer_item, list);
+               item = list_entry(pos, struct arg_item, list);
                if (!strcasecmp(item->conf.name, name))
                        return item;
        }
 
        /* Item does not already exists, create it */
-       item = xcalloc(sizeof(struct trailer_item), 1);
+       item = xcalloc(sizeof(*item), 1);
        duplicate_conf(&item->conf, &default_conf_info);
        item->conf.name = xstrdup(name);
 
@@ -442,7 +488,7 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v
 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
 {
        const char *trailer_item, *variable_name;
-       struct trailer_item *item;
+       struct arg_item *item;
        struct conf_info *conf;
        char *name = NULL;
        enum trailer_info_type type;
@@ -500,33 +546,7 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
        return 0;
 }
 
-static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
-{
-       size_t len;
-       struct strbuf seps = STRBUF_INIT;
-       strbuf_addstr(&seps, separators);
-       strbuf_addch(&seps, '=');
-       len = strcspn(trailer, seps.buf);
-       strbuf_release(&seps);
-       if (len == 0) {
-               int l = strlen(trailer);
-               while (l > 0 && isspace(trailer[l - 1]))
-                       l--;
-               return error(_("empty trailer token in trailer '%.*s'"), l, trailer);
-       }
-       if (len < strlen(trailer)) {
-               strbuf_add(tok, trailer, len);
-               strbuf_trim(tok);
-               strbuf_addstr(val, trailer + len + 1);
-               strbuf_trim(val);
-       } else {
-               strbuf_addstr(tok, trailer);
-               strbuf_trim(tok);
-       }
-       return 0;
-}
-
-static const char *token_from_item(struct trailer_item *item, char *tok)
+static const char *token_from_item(struct arg_item *item, char *tok)
 {
        if (item->conf.key)
                return item->conf.key;
@@ -535,86 +555,134 @@ static const char *token_from_item(struct trailer_item *item, char *tok)
        return item->conf.name;
 }
 
-static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
-                                            char *tok, char *val)
-{
-       struct trailer_item *new = xcalloc(sizeof(*new), 1);
-       new->value = val ? val : xstrdup("");
-
-       if (conf_item) {
-               duplicate_conf(&new->conf, &conf_item->conf);
-               new->token = xstrdup(token_from_item(conf_item, tok));
-               free(tok);
-       } else {
-               duplicate_conf(&new->conf, &default_conf_info);
-               new->token = tok;
-       }
-
-       return new;
-}
-
-static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
+static int token_matches_item(const char *tok, struct arg_item *item, int tok_len)
 {
        if (!strncasecmp(tok, item->conf.name, tok_len))
                return 1;
        return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
 }
 
-static struct trailer_item *create_trailer_item(const char *string)
+/*
+ * Return the location of the first separator in line, or -1 if there is no
+ * separator.
+ */
+static int find_separator(const char *line, const char *separators)
 {
-       struct strbuf tok = STRBUF_INIT;
-       struct strbuf val = STRBUF_INIT;
-       struct trailer_item *item;
+       int loc = strcspn(line, separators);
+       if (!line[loc])
+               return -1;
+       return loc;
+}
+
+/*
+ * Obtain the token, value, and conf from the given trailer.
+ *
+ * separator_pos must not be 0, since the token cannot be an empty string.
+ *
+ * If separator_pos is -1, interpret the whole trailer as a token.
+ */
+static void parse_trailer(struct strbuf *tok, struct strbuf *val,
+                        const struct conf_info **conf, const char *trailer,
+                        int separator_pos)
+{
+       struct arg_item *item;
        int tok_len;
        struct list_head *pos;
 
-       if (parse_trailer(&tok, &val, string))
-               return NULL;
-
-       tok_len = token_len_without_separator(tok.buf, tok.len);
+       if (separator_pos != -1) {
+               strbuf_add(tok, trailer, separator_pos);
+               strbuf_trim(tok);
+               strbuf_addstr(val, trailer + separator_pos + 1);
+               strbuf_trim(val);
+       } else {
+               strbuf_addstr(tok, trailer);
+               strbuf_trim(tok);
+       }
 
        /* Lookup if the token matches something in the config */
+       tok_len = token_len_without_separator(tok->buf, tok->len);
+       if (conf)
+               *conf = &default_conf_info;
        list_for_each(pos, &conf_head) {
-               item = list_entry(pos, struct trailer_item, list);
-               if (token_matches_item(tok.buf, item, tok_len))
-                       return new_trailer_item(item,
-                                               strbuf_detach(&tok, NULL),
-                                               strbuf_detach(&val, NULL));
+               item = list_entry(pos, struct arg_item, list);
+               if (token_matches_item(tok->buf, item, tok_len)) {
+                       char *tok_buf = strbuf_detach(tok, NULL);
+                       if (conf)
+                               *conf = &item->conf;
+                       strbuf_addstr(tok, token_from_item(item, tok_buf));
+                       free(tok_buf);
+                       break;
+               }
        }
-
-       return new_trailer_item(NULL,
-                               strbuf_detach(&tok, NULL),
-                               strbuf_detach(&val, NULL));
 }
 
-static void add_trailer_item(struct list_head *head, struct trailer_item *new)
+static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
+                                            char *val)
 {
-       if (!new)
-               return;
+       struct trailer_item *new = xcalloc(sizeof(*new), 1);
+       new->token = tok;
+       new->value = val;
        list_add_tail(&new->list, head);
+       return new;
+}
+
+static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
+                        const struct conf_info *conf)
+{
+       struct arg_item *new = xcalloc(sizeof(*new), 1);
+       new->token = tok;
+       new->value = val;
+       duplicate_conf(&new->conf, conf);
+       list_add_tail(&new->list, arg_head);
 }
 
 static void process_command_line_args(struct list_head *arg_head,
                                      struct string_list *trailers)
 {
        struct string_list_item *tr;
-       struct trailer_item *item;
+       struct arg_item *item;
+       struct strbuf tok = STRBUF_INIT;
+       struct strbuf val = STRBUF_INIT;
+       const struct conf_info *conf;
        struct list_head *pos;
 
-       /* Add a trailer item for each configured trailer with a command */
+       /*
+        * In command-line arguments, '=' is accepted (in addition to the
+        * separators that are defined).
+        */
+       char *cl_separators = xstrfmt("=%s", separators);
+
+       /* Add an arg item for each configured trailer with a command */
        list_for_each(pos, &conf_head) {
-               item = list_entry(pos, struct trailer_item, list);
-               if (item->conf.command) {
-                       struct trailer_item *new = new_trailer_item(item, NULL, NULL);
-                       add_trailer_item(arg_head, new);
-               }
+               item = list_entry(pos, struct arg_item, list);
+               if (item->conf.command)
+                       add_arg_item(arg_head,
+                                    xstrdup(token_from_item(item, NULL)),
+                                    xstrdup(""),
+                                    &item->conf);
        }
 
-       /* Add a trailer item for each trailer on the command line */
+       /* Add an arg item for each trailer on the command line */
        for_each_string_list_item(tr, trailers) {
-               struct trailer_item *new = create_trailer_item(tr->string);
-               add_trailer_item(arg_head, new);
+               int separator_pos = find_separator(tr->string, cl_separators);
+               if (separator_pos == 0) {
+                       struct strbuf sb = STRBUF_INIT;
+                       strbuf_addstr(&sb, tr->string);
+                       strbuf_trim(&sb);
+                       error(_("empty trailer token in trailer '%.*s'"),
+                             (int) sb.len, sb.buf);
+                       strbuf_release(&sb);
+               } else {
+                       parse_trailer(&tok, &val, &conf, tr->string,
+                                     separator_pos);
+                       add_arg_item(arg_head,
+                                    strbuf_detach(&tok, NULL),
+                                    strbuf_detach(&val, NULL),
+                                    conf);
+               }
        }
+
+       free(cl_separators);
 }
 
 static struct strbuf **read_input_file(const char *file)
@@ -662,6 +730,15 @@ static int find_patch_start(struct strbuf **lines, int count)
 static int find_trailer_start(struct strbuf **lines, int count)
 {
        int start, end_of_title, only_spaces = 1;
+       int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
+       /*
+        * Number of possible continuation lines encountered. This will be
+        * reset to 0 if we encounter a trailer (since those lines are to be
+        * considered continuations of that trailer), and added to
+        * non_trailer_lines if we encounter a non-trailer (since those lines
+        * are to be considered non-trailers).
+        */
+       int possible_continuation_lines = 0;
 
        /* The first paragraph is the title and cannot be trailers */
        for (start = 0; start < count; start++) {
@@ -673,26 +750,71 @@ static int find_trailer_start(struct strbuf **lines, int count)
        end_of_title = start;
 
        /*
-        * Get the start of the trailers by looking starting from the end
-        * for a line with only spaces before lines with one separator.
+        * Get the start of the trailers by looking starting from the end for a
+        * blank line before a set of non-blank lines that (i) are all
+        * trailers, or (ii) contains at least one Git-generated trailer and
+        * consists of at least 25% trailers.
         */
        for (start = count - 1; start >= end_of_title; start--) {
-               if (lines[start]->buf[0] == comment_line_char)
+               const char **p;
+               int separator_pos;
+
+               if (lines[start]->buf[0] == comment_line_char) {
+                       non_trailer_lines += possible_continuation_lines;
+                       possible_continuation_lines = 0;
                        continue;
+               }
                if (contains_only_spaces(lines[start]->buf)) {
                        if (only_spaces)
                                continue;
-                       return start + 1;
+                       non_trailer_lines += possible_continuation_lines;
+                       if (recognized_prefix &&
+                           trailer_lines * 3 >= non_trailer_lines)
+                               return start + 1;
+                       if (trailer_lines && !non_trailer_lines)
+                               return start + 1;
+                       return count;
                }
-               if (strcspn(lines[start]->buf, separators) < lines[start]->len) {
-                       if (only_spaces)
-                               only_spaces = 0;
-                       continue;
+               only_spaces = 0;
+
+               for (p = git_generated_prefixes; *p; p++) {
+                       if (starts_with(lines[start]->buf, *p)) {
+                               trailer_lines++;
+                               possible_continuation_lines = 0;
+                               recognized_prefix = 1;
+                               goto continue_outer_loop;
+                       }
                }
-               return count;
+
+               separator_pos = find_separator(lines[start]->buf, separators);
+               if (separator_pos >= 1 && !isspace(lines[start]->buf[0])) {
+                       struct list_head *pos;
+
+                       trailer_lines++;
+                       possible_continuation_lines = 0;
+                       if (recognized_prefix)
+                               continue;
+                       list_for_each(pos, &conf_head) {
+                               struct arg_item *item;
+                               item = list_entry(pos, struct arg_item, list);
+                               if (token_matches_item(lines[start]->buf, item,
+                                                      separator_pos)) {
+                                       recognized_prefix = 1;
+                                       break;
+                               }
+                       }
+               } else if (isspace(lines[start]->buf[0]))
+                       possible_continuation_lines++;
+               else {
+                       non_trailer_lines++;
+                       non_trailer_lines += possible_continuation_lines;
+                       possible_continuation_lines = 0;
+               }
+continue_outer_loop:
+               ;
        }
 
-       return only_spaces ? count : 0;
+       return count;
 }
 
 /* Get the index of the end of the trailers */
@@ -734,6 +856,9 @@ static int process_input_file(FILE *outfile,
 {
        int count = 0;
        int patch_start, trailer_start, trailer_end, i;
+       struct strbuf tok = STRBUF_INIT;
+       struct strbuf val = STRBUF_INIT;
+       struct trailer_item *last = NULL;
 
        /* Get the line count */
        while (lines[count])
@@ -751,9 +876,31 @@ static int process_input_file(FILE *outfile,
 
        /* Parse trailer lines */
        for (i = trailer_start; i < trailer_end; i++) {
-               if (lines[i]->buf[0] != comment_line_char) {
-                       struct trailer_item *new = create_trailer_item(lines[i]->buf);
-                       add_trailer_item(head, new);
+               int separator_pos;
+               if (lines[i]->buf[0] == comment_line_char)
+                       continue;
+               if (last && isspace(lines[i]->buf[0])) {
+                       struct strbuf sb = STRBUF_INIT;
+                       strbuf_addf(&sb, "%s\n%s", last->value, lines[i]->buf);
+                       strbuf_strip_suffix(&sb, "\n");
+                       free(last->value);
+                       last->value = strbuf_detach(&sb, NULL);
+                       continue;
+               }
+               separator_pos = find_separator(lines[i]->buf, separators);
+               if (separator_pos >= 1) {
+                       parse_trailer(&tok, &val, NULL, lines[i]->buf,
+                                     separator_pos);
+                       last = add_trailer_item(head,
+                                               strbuf_detach(&tok, NULL),
+                                               strbuf_detach(&val, NULL));
+               } else {
+                       strbuf_addbuf(&val, lines[i]);
+                       strbuf_strip_suffix(&val, "\n");
+                       add_trailer_item(head,
+                                        NULL,
+                                        strbuf_detach(&val, NULL));
+                       last = NULL;
                }
        }