2014-10-14 02:16:23 +08:00
|
|
|
#include "cache.h"
|
2014-10-14 02:16:26 +08:00
|
|
|
#include "string-list.h"
|
2014-10-14 02:16:31 +08:00
|
|
|
#include "run-command.h"
|
2014-11-09 17:23:42 +08:00
|
|
|
#include "commit.h"
|
2016-01-15 00:57:55 +08:00
|
|
|
#include "tempfile.h"
|
2014-10-14 02:16:28 +08:00
|
|
|
#include "trailer.h"
|
2016-10-21 05:39:47 +08:00
|
|
|
#include "list.h"
|
2014-10-14 02:16:23 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
|
|
|
|
enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
|
|
|
|
EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
|
|
|
|
enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
|
|
|
|
|
|
|
|
struct conf_info {
|
|
|
|
char *name;
|
|
|
|
char *key;
|
|
|
|
char *command;
|
|
|
|
enum action_where where;
|
|
|
|
enum action_if_exists if_exists;
|
|
|
|
enum action_if_missing if_missing;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct conf_info default_conf_info;
|
|
|
|
|
|
|
|
struct trailer_item {
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head list;
|
2016-10-22 01:55:01 +08:00
|
|
|
/*
|
|
|
|
* If this is not a trailer line, the line is stored in value
|
|
|
|
* (excluding the terminating newline) and token is NULL.
|
|
|
|
*/
|
2016-10-15 01:37:58 +08:00
|
|
|
char *token;
|
|
|
|
char *value;
|
2016-10-21 05:39:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct arg_item {
|
|
|
|
struct list_head list;
|
|
|
|
char *token;
|
|
|
|
char *value;
|
2014-10-14 02:16:23 +08:00
|
|
|
struct conf_info conf;
|
|
|
|
};
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static LIST_HEAD(conf_head);
|
2014-10-14 02:16:23 +08:00
|
|
|
|
|
|
|
static char *separators = ":";
|
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
static int configured;
|
|
|
|
|
2014-10-14 02:16:31 +08:00
|
|
|
#define TRAILER_ARG_STRING "$ARG"
|
|
|
|
|
2016-10-22 01:55:01 +08:00
|
|
|
static const char *git_generated_prefixes[] = {
|
|
|
|
"Signed-off-by: ",
|
|
|
|
"(cherry picked from commit ",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
/* Iterate over the elements of the list. */
|
|
|
|
#define list_for_each_dir(pos, head, is_reverse) \
|
|
|
|
for (pos = is_reverse ? (head)->prev : (head)->next; \
|
|
|
|
pos != (head); \
|
|
|
|
pos = is_reverse ? pos->prev : pos->next)
|
|
|
|
|
2014-10-14 02:16:23 +08:00
|
|
|
static int after_or_end(enum action_where where)
|
|
|
|
{
|
|
|
|
return (where == WHERE_AFTER) || (where == WHERE_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the length of the string not including any final
|
|
|
|
* punctuation. E.g., the input "Signed-off-by:" would return
|
|
|
|
* 13, stripping the trailing punctuation but retaining
|
|
|
|
* internal punctuation.
|
|
|
|
*/
|
|
|
|
static size_t token_len_without_separator(const char *token, size_t len)
|
|
|
|
{
|
|
|
|
while (len > 0 && !isalnum(token[len - 1]))
|
|
|
|
len--;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static int same_token(struct trailer_item *a, struct arg_item *b)
|
2014-10-14 02:16:23 +08:00
|
|
|
{
|
2016-10-22 01:55:01 +08:00
|
|
|
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;
|
2014-10-14 02:16:23 +08:00
|
|
|
|
|
|
|
return !strncasecmp(a->token, b->token, min_len);
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static int same_value(struct trailer_item *a, struct arg_item *b)
|
2014-10-14 02:16:23 +08:00
|
|
|
{
|
|
|
|
return !strcasecmp(a->value, b->value);
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static int same_trailer(struct trailer_item *a, struct arg_item *b)
|
2014-10-14 02:16:23 +08:00
|
|
|
{
|
|
|
|
return same_token(a, b) && same_value(a, b);
|
|
|
|
}
|
2014-10-14 02:16:24 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
static inline int is_blank_line(const char *str)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
|
|
|
const char *s = str;
|
2016-11-03 01:29:18 +08:00
|
|
|
while (*s && *s != '\n' && isspace(*s))
|
2014-10-14 02:16:27 +08:00
|
|
|
s++;
|
2016-11-03 01:29:18 +08:00
|
|
|
return !*s || *s == '\n';
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
2014-10-14 02:16:31 +08:00
|
|
|
static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
|
|
|
|
{
|
|
|
|
const char *ptr = strstr(sb->buf, a);
|
|
|
|
if (ptr)
|
|
|
|
strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:16:24 +08:00
|
|
|
static void free_trailer_item(struct trailer_item *item)
|
2016-10-21 05:39:49 +08:00
|
|
|
{
|
|
|
|
free(item->token);
|
|
|
|
free(item->value);
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_arg_item(struct arg_item *item)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
|
|
|
free(item->conf.name);
|
|
|
|
free(item->conf.key);
|
|
|
|
free(item->conf.command);
|
2016-10-15 01:37:58 +08:00
|
|
|
free(item->token);
|
|
|
|
free(item->value);
|
2014-10-14 02:16:24 +08:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:16:28 +08:00
|
|
|
static char last_non_space_char(const char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = strlen(s) - 1; i >= 0; i--)
|
|
|
|
if (!isspace(s[i]))
|
|
|
|
return s[i];
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
|
2016-01-15 00:57:54 +08:00
|
|
|
static void print_tok_val(FILE *outfile, const char *tok, const char *val)
|
2014-10-14 02:16:28 +08:00
|
|
|
{
|
2016-10-22 01:55:01 +08:00
|
|
|
char c;
|
|
|
|
|
|
|
|
if (!tok) {
|
|
|
|
fprintf(outfile, "%s\n", val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = last_non_space_char(tok);
|
2014-10-14 02:16:28 +08:00
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
if (strchr(separators, c))
|
2016-01-15 00:57:54 +08:00
|
|
|
fprintf(outfile, "%s%s\n", tok, val);
|
2014-10-14 02:16:28 +08:00
|
|
|
else
|
2016-01-15 00:57:54 +08:00
|
|
|
fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
|
2014-10-14 02:16:28 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
|
2014-10-14 02:16:28 +08:00
|
|
|
{
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *pos;
|
2014-10-14 02:16:28 +08:00
|
|
|
struct trailer_item *item;
|
2016-10-21 05:39:47 +08:00
|
|
|
list_for_each(pos, head) {
|
|
|
|
item = list_entry(pos, struct trailer_item, list);
|
2014-10-14 02:16:28 +08:00
|
|
|
if (!trim_empty || strlen(item->value) > 0)
|
2016-01-15 00:57:54 +08:00
|
|
|
print_tok_val(outfile, item->token, item->value);
|
2014-10-14 02:16:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
2016-10-21 05:39:49 +08:00
|
|
|
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;
|
2014-10-14 02:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_arg_to_input_list(struct trailer_item *on_tok,
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *arg_tok)
|
2016-10-21 05:39:47 +08:00
|
|
|
{
|
2016-10-21 05:39:49 +08:00
|
|
|
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);
|
2016-10-21 05:39:47 +08:00
|
|
|
else
|
2016-10-21 05:39:49 +08:00
|
|
|
list_add_tail(&to_add->list, &on_tok->list);
|
2014-10-14 02:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int check_if_different(struct trailer_item *in_tok,
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *arg_tok,
|
2016-10-21 05:39:47 +08:00
|
|
|
int check_all,
|
|
|
|
struct list_head *head)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
|
|
|
enum action_where where = arg_tok->conf.where;
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *next_head;
|
2014-10-14 02:16:24 +08:00
|
|
|
do {
|
|
|
|
if (same_trailer(in_tok, arg_tok))
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* if we want to add a trailer after another one,
|
|
|
|
* we have to check those before this one
|
|
|
|
*/
|
2016-10-21 05:39:47 +08:00
|
|
|
next_head = after_or_end(where) ? in_tok->list.prev
|
|
|
|
: in_tok->list.next;
|
|
|
|
if (next_head == head)
|
|
|
|
break;
|
|
|
|
in_tok = list_entry(next_head, struct trailer_item, list);
|
2014-10-14 02:16:24 +08:00
|
|
|
} while (check_all);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:37:58 +08:00
|
|
|
static char *apply_command(const char *command, const char *arg)
|
2014-10-14 02:16:31 +08:00
|
|
|
{
|
|
|
|
struct strbuf cmd = STRBUF_INIT;
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2014-11-09 21:49:58 +08:00
|
|
|
struct child_process cp = CHILD_PROCESS_INIT;
|
2014-10-14 02:16:31 +08:00
|
|
|
const char *argv[] = {NULL, NULL};
|
2016-10-15 01:37:58 +08:00
|
|
|
char *result;
|
2014-10-14 02:16:31 +08:00
|
|
|
|
|
|
|
strbuf_addstr(&cmd, command);
|
|
|
|
if (arg)
|
|
|
|
strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
|
|
|
|
|
|
|
|
argv[0] = cmd.buf;
|
|
|
|
cp.argv = argv;
|
|
|
|
cp.env = local_repo_env;
|
|
|
|
cp.no_stdin = 1;
|
|
|
|
cp.use_shell = 1;
|
|
|
|
|
2015-03-23 11:54:00 +08:00
|
|
|
if (capture_command(&cp, &buf, 1024)) {
|
2016-02-27 14:42:10 +08:00
|
|
|
error(_("running trailer command '%s' failed"), cmd.buf);
|
2014-10-14 02:16:31 +08:00
|
|
|
strbuf_release(&buf);
|
|
|
|
result = xstrdup("");
|
2015-03-23 11:54:00 +08:00
|
|
|
} else {
|
|
|
|
strbuf_trim(&buf);
|
2014-10-14 02:16:31 +08:00
|
|
|
result = strbuf_detach(&buf, NULL);
|
2015-03-23 11:54:00 +08:00
|
|
|
}
|
2014-10-14 02:16:31 +08:00
|
|
|
|
|
|
|
strbuf_release(&cmd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
|
2014-10-14 02:16:31 +08:00
|
|
|
{
|
|
|
|
if (arg_tok->conf.command) {
|
|
|
|
const char *arg;
|
|
|
|
if (arg_tok->value && arg_tok->value[0]) {
|
|
|
|
arg = arg_tok->value;
|
|
|
|
} else {
|
|
|
|
if (in_tok && in_tok->value)
|
|
|
|
arg = xstrdup(in_tok->value);
|
|
|
|
else
|
|
|
|
arg = xstrdup("");
|
|
|
|
}
|
|
|
|
arg_tok->value = apply_command(arg_tok->conf.command, arg);
|
|
|
|
free((char *)arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:16:24 +08:00
|
|
|
static void apply_arg_if_exists(struct trailer_item *in_tok,
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *arg_tok,
|
2014-10-14 02:16:24 +08:00
|
|
|
struct trailer_item *on_tok,
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *head)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
|
|
|
switch (arg_tok->conf.if_exists) {
|
|
|
|
case EXISTS_DO_NOTHING:
|
2016-10-21 05:39:49 +08:00
|
|
|
free_arg_item(arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
break;
|
|
|
|
case EXISTS_REPLACE:
|
2014-10-14 02:16:31 +08:00
|
|
|
apply_item_command(in_tok, arg_tok);
|
2016-10-21 05:39:47 +08:00
|
|
|
add_arg_to_input_list(on_tok, arg_tok);
|
|
|
|
list_del(&in_tok->list);
|
2014-10-14 02:16:24 +08:00
|
|
|
free_trailer_item(in_tok);
|
|
|
|
break;
|
|
|
|
case EXISTS_ADD:
|
2014-10-14 02:16:31 +08:00
|
|
|
apply_item_command(in_tok, arg_tok);
|
2016-10-21 05:39:47 +08:00
|
|
|
add_arg_to_input_list(on_tok, arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
break;
|
|
|
|
case EXISTS_ADD_IF_DIFFERENT:
|
2014-10-14 02:16:31 +08:00
|
|
|
apply_item_command(in_tok, arg_tok);
|
2016-10-21 05:39:47 +08:00
|
|
|
if (check_if_different(in_tok, arg_tok, 1, head))
|
|
|
|
add_arg_to_input_list(on_tok, arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
else
|
2016-10-21 05:39:49 +08:00
|
|
|
free_arg_item(arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
break;
|
|
|
|
case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
|
2014-10-14 02:16:31 +08:00
|
|
|
apply_item_command(in_tok, arg_tok);
|
2016-10-21 05:39:47 +08:00
|
|
|
if (check_if_different(on_tok, arg_tok, 0, head))
|
|
|
|
add_arg_to_input_list(on_tok, arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
else
|
2016-10-21 05:39:49 +08:00
|
|
|
free_arg_item(arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static void apply_arg_if_missing(struct list_head *head,
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *arg_tok)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
|
|
|
enum action_where where;
|
2016-10-21 05:39:49 +08:00
|
|
|
struct trailer_item *to_add;
|
2014-10-14 02:16:24 +08:00
|
|
|
|
|
|
|
switch (arg_tok->conf.if_missing) {
|
|
|
|
case MISSING_DO_NOTHING:
|
2016-10-21 05:39:49 +08:00
|
|
|
free_arg_item(arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
break;
|
|
|
|
case MISSING_ADD:
|
|
|
|
where = arg_tok->conf.where;
|
2014-10-14 02:16:31 +08:00
|
|
|
apply_item_command(NULL, arg_tok);
|
2016-10-21 05:39:49 +08:00
|
|
|
to_add = trailer_from_arg(arg_tok);
|
2016-10-21 05:39:47 +08:00
|
|
|
if (after_or_end(where))
|
2016-10-21 05:39:49 +08:00
|
|
|
list_add_tail(&to_add->list, head);
|
2016-10-21 05:39:47 +08:00
|
|
|
else
|
2016-10-21 05:39:49 +08:00
|
|
|
list_add(&to_add->list, head);
|
2014-10-14 02:16:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static int find_same_and_apply_arg(struct list_head *head,
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *arg_tok)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *pos;
|
2014-10-14 02:16:24 +08:00
|
|
|
struct trailer_item *in_tok;
|
|
|
|
struct trailer_item *on_tok;
|
|
|
|
|
|
|
|
enum action_where where = arg_tok->conf.where;
|
|
|
|
int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
|
|
|
|
int backwards = after_or_end(where);
|
2016-10-21 05:39:47 +08:00
|
|
|
struct trailer_item *start_tok;
|
2014-10-14 02:16:24 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
if (list_empty(head))
|
|
|
|
return 0;
|
2014-10-14 02:16:24 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
start_tok = list_entry(backwards ? head->prev : head->next,
|
|
|
|
struct trailer_item,
|
|
|
|
list);
|
|
|
|
|
|
|
|
list_for_each_dir(pos, head, backwards) {
|
|
|
|
in_tok = list_entry(pos, struct trailer_item, list);
|
2014-10-14 02:16:24 +08:00
|
|
|
if (!same_token(in_tok, arg_tok))
|
|
|
|
continue;
|
|
|
|
on_tok = middle ? in_tok : start_tok;
|
2016-10-21 05:39:47 +08:00
|
|
|
apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
|
2014-10-14 02:16:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static void process_trailers_lists(struct list_head *head,
|
|
|
|
struct list_head *arg_head)
|
2014-10-14 02:16:24 +08:00
|
|
|
{
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *pos, *p;
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *arg_tok;
|
2014-10-14 02:16:24 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
list_for_each_safe(pos, p, arg_head) {
|
2014-10-14 02:16:24 +08:00
|
|
|
int applied = 0;
|
2016-10-21 05:39:49 +08:00
|
|
|
arg_tok = list_entry(pos, struct arg_item, list);
|
2014-10-14 02:16:24 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
list_del(pos);
|
2014-10-14 02:16:24 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
applied = find_same_and_apply_arg(head, arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
|
|
|
|
if (!applied)
|
2016-10-21 05:39:47 +08:00
|
|
|
apply_arg_if_missing(head, arg_tok);
|
2014-10-14 02:16:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-14 02:16:25 +08:00
|
|
|
|
|
|
|
static int set_where(struct conf_info *item, const char *value)
|
|
|
|
{
|
|
|
|
if (!strcasecmp("after", value))
|
|
|
|
item->where = WHERE_AFTER;
|
|
|
|
else if (!strcasecmp("before", value))
|
|
|
|
item->where = WHERE_BEFORE;
|
|
|
|
else if (!strcasecmp("end", value))
|
|
|
|
item->where = WHERE_END;
|
|
|
|
else if (!strcasecmp("start", value))
|
|
|
|
item->where = WHERE_START;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_if_exists(struct conf_info *item, const char *value)
|
|
|
|
{
|
|
|
|
if (!strcasecmp("addIfDifferent", value))
|
|
|
|
item->if_exists = EXISTS_ADD_IF_DIFFERENT;
|
|
|
|
else if (!strcasecmp("addIfDifferentNeighbor", value))
|
|
|
|
item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
|
|
|
|
else if (!strcasecmp("add", value))
|
|
|
|
item->if_exists = EXISTS_ADD;
|
|
|
|
else if (!strcasecmp("replace", value))
|
|
|
|
item->if_exists = EXISTS_REPLACE;
|
|
|
|
else if (!strcasecmp("doNothing", value))
|
|
|
|
item->if_exists = EXISTS_DO_NOTHING;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_if_missing(struct conf_info *item, const char *value)
|
|
|
|
{
|
|
|
|
if (!strcasecmp("doNothing", value))
|
|
|
|
item->if_missing = MISSING_DO_NOTHING;
|
|
|
|
else if (!strcasecmp("add", value))
|
|
|
|
item->if_missing = MISSING_ADD;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:37:58 +08:00
|
|
|
static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
|
2014-10-14 02:16:25 +08:00
|
|
|
{
|
|
|
|
*dst = *src;
|
2016-10-13 02:20:23 +08:00
|
|
|
dst->name = xstrdup_or_null(src->name);
|
|
|
|
dst->key = xstrdup_or_null(src->key);
|
|
|
|
dst->command = xstrdup_or_null(src->command);
|
2014-10-14 02:16:25 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static struct arg_item *get_conf_item(const char *name)
|
2014-10-14 02:16:25 +08:00
|
|
|
{
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *pos;
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *item;
|
2014-10-14 02:16:25 +08:00
|
|
|
|
|
|
|
/* Look up item with same name */
|
2016-10-21 05:39:47 +08:00
|
|
|
list_for_each(pos, &conf_head) {
|
2016-10-21 05:39:49 +08:00
|
|
|
item = list_entry(pos, struct arg_item, list);
|
2014-10-14 02:16:25 +08:00
|
|
|
if (!strcasecmp(item->conf.name, name))
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Item does not already exists, create it */
|
2016-10-21 05:39:49 +08:00
|
|
|
item = xcalloc(sizeof(*item), 1);
|
2014-10-14 02:16:25 +08:00
|
|
|
duplicate_conf(&item->conf, &default_conf_info);
|
|
|
|
item->conf.name = xstrdup(name);
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
list_add_tail(&item->list, &conf_head);
|
2014-10-14 02:16:25 +08:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
|
|
|
|
TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
enum trailer_info_type type;
|
|
|
|
} trailer_config_items[] = {
|
|
|
|
{ "key", TRAILER_KEY },
|
|
|
|
{ "command", TRAILER_COMMAND },
|
|
|
|
{ "where", TRAILER_WHERE },
|
|
|
|
{ "ifexists", TRAILER_IF_EXISTS },
|
|
|
|
{ "ifmissing", TRAILER_IF_MISSING }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
|
|
|
|
{
|
|
|
|
const char *trailer_item, *variable_name;
|
|
|
|
|
|
|
|
if (!skip_prefix(conf_key, "trailer.", &trailer_item))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
variable_name = strrchr(trailer_item, '.');
|
|
|
|
if (!variable_name) {
|
|
|
|
if (!strcmp(trailer_item, "where")) {
|
|
|
|
if (set_where(&default_conf_info, value) < 0)
|
|
|
|
warning(_("unknown value '%s' for key '%s'"),
|
|
|
|
value, conf_key);
|
|
|
|
} else if (!strcmp(trailer_item, "ifexists")) {
|
|
|
|
if (set_if_exists(&default_conf_info, value) < 0)
|
|
|
|
warning(_("unknown value '%s' for key '%s'"),
|
|
|
|
value, conf_key);
|
|
|
|
} else if (!strcmp(trailer_item, "ifmissing")) {
|
|
|
|
if (set_if_missing(&default_conf_info, value) < 0)
|
|
|
|
warning(_("unknown value '%s' for key '%s'"),
|
|
|
|
value, conf_key);
|
|
|
|
} else if (!strcmp(trailer_item, "separators")) {
|
|
|
|
separators = xstrdup(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int git_trailer_config(const char *conf_key, const char *value, void *cb)
|
|
|
|
{
|
|
|
|
const char *trailer_item, *variable_name;
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *item;
|
2014-10-14 02:16:25 +08:00
|
|
|
struct conf_info *conf;
|
|
|
|
char *name = NULL;
|
|
|
|
enum trailer_info_type type;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!skip_prefix(conf_key, "trailer.", &trailer_item))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
variable_name = strrchr(trailer_item, '.');
|
|
|
|
if (!variable_name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
variable_name++;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
|
|
|
|
if (strcmp(trailer_config_items[i].name, variable_name))
|
|
|
|
continue;
|
|
|
|
name = xstrndup(trailer_item, variable_name - trailer_item - 1);
|
|
|
|
type = trailer_config_items[i].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
item = get_conf_item(name);
|
|
|
|
conf = &item->conf;
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TRAILER_KEY:
|
|
|
|
if (conf->key)
|
|
|
|
warning(_("more than one %s"), conf_key);
|
|
|
|
conf->key = xstrdup(value);
|
|
|
|
break;
|
|
|
|
case TRAILER_COMMAND:
|
|
|
|
if (conf->command)
|
|
|
|
warning(_("more than one %s"), conf_key);
|
|
|
|
conf->command = xstrdup(value);
|
|
|
|
break;
|
|
|
|
case TRAILER_WHERE:
|
|
|
|
if (set_where(conf, value))
|
|
|
|
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
|
|
|
break;
|
|
|
|
case TRAILER_IF_EXISTS:
|
|
|
|
if (set_if_exists(conf, value))
|
|
|
|
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
|
|
|
break;
|
|
|
|
case TRAILER_IF_MISSING:
|
|
|
|
if (set_if_missing(conf, value))
|
|
|
|
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-27 00:05:50 +08:00
|
|
|
die("BUG: trailer.c: unhandled type %d", type);
|
2014-10-14 02:16:25 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-14 02:16:26 +08:00
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
static void ensure_configured(void)
|
|
|
|
{
|
|
|
|
if (configured)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Default config must be setup first */
|
|
|
|
git_config(git_trailer_default_config, NULL);
|
|
|
|
git_config(git_trailer_config, NULL);
|
|
|
|
configured = 1;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static const char *token_from_item(struct arg_item *item, char *tok)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
|
|
|
if (item->conf.key)
|
|
|
|
return item->conf.key;
|
|
|
|
if (tok)
|
|
|
|
return tok;
|
|
|
|
return item->conf.name;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static int token_matches_item(const char *tok, struct arg_item *item, int tok_len)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
|
|
|
if (!strncasecmp(tok, item->conf.name, tok_len))
|
|
|
|
return 1;
|
|
|
|
return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
|
|
|
|
}
|
|
|
|
|
2016-10-22 01:55:00 +08:00
|
|
|
/*
|
2016-11-03 01:29:16 +08:00
|
|
|
* If the given line is of the form
|
|
|
|
* "<token><optional whitespace><separator>..." or "<separator>...", return the
|
|
|
|
* location of the separator. Otherwise, return -1. The optional whitespace
|
|
|
|
* is allowed there primarily to allow things like "Bug #43" where <token> is
|
|
|
|
* "Bug" and <separator> is "#".
|
|
|
|
*
|
|
|
|
* The separator-starts-line case (in which this function returns 0) is
|
|
|
|
* distinguished from the non-well-formed-line case (in which this function
|
|
|
|
* returns -1) because some callers of this function need such a distinction.
|
2016-10-22 01:55:00 +08:00
|
|
|
*/
|
|
|
|
static int find_separator(const char *line, const char *separators)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
2016-11-03 01:29:16 +08:00
|
|
|
int whitespace_found = 0;
|
|
|
|
const char *c;
|
|
|
|
for (c = line; *c; c++) {
|
|
|
|
if (strchr(separators, *c))
|
|
|
|
return c - line;
|
|
|
|
if (!whitespace_found && (isalnum(*c) || *c == '-'))
|
|
|
|
continue;
|
|
|
|
if (c != line && (*c == ' ' || *c == '\t')) {
|
|
|
|
whitespace_found = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -1;
|
2016-10-22 01:55:00 +08:00
|
|
|
}
|
2014-10-14 02:16:26 +08:00
|
|
|
|
2016-10-22 01:55:00 +08:00
|
|
|
/*
|
|
|
|
* 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)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *item;
|
2016-10-21 05:39:48 +08:00
|
|
|
int tok_len;
|
|
|
|
struct list_head *pos;
|
2014-10-14 02:16:26 +08:00
|
|
|
|
2016-10-22 01:55:00 +08:00
|
|
|
if (separator_pos != -1) {
|
|
|
|
strbuf_add(tok, trailer, separator_pos);
|
2014-10-14 02:16:26 +08:00
|
|
|
strbuf_trim(tok);
|
2016-10-22 01:55:00 +08:00
|
|
|
strbuf_addstr(val, trailer + separator_pos + 1);
|
2014-10-14 02:16:26 +08:00
|
|
|
strbuf_trim(val);
|
|
|
|
} else {
|
|
|
|
strbuf_addstr(tok, trailer);
|
|
|
|
strbuf_trim(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup if the token matches something in the config */
|
2016-10-21 05:39:48 +08:00
|
|
|
tok_len = token_len_without_separator(tok->buf, tok->len);
|
2016-10-21 05:39:49 +08:00
|
|
|
if (conf)
|
|
|
|
*conf = &default_conf_info;
|
2016-10-21 05:39:47 +08:00
|
|
|
list_for_each(pos, &conf_head) {
|
2016-10-21 05:39:49 +08:00
|
|
|
item = list_entry(pos, struct arg_item, list);
|
2016-10-21 05:39:48 +08:00
|
|
|
if (token_matches_item(tok->buf, item, tok_len)) {
|
|
|
|
char *tok_buf = strbuf_detach(tok, NULL);
|
2016-10-21 05:39:49 +08:00
|
|
|
if (conf)
|
|
|
|
*conf = &item->conf;
|
2016-10-21 05:39:48 +08:00
|
|
|
strbuf_addstr(tok, token_from_item(item, tok_buf));
|
|
|
|
free(tok_buf);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 02:16:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 01:55:03 +08:00
|
|
|
static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
|
|
|
|
char *val)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
2016-10-21 05:39:48 +08:00
|
|
|
struct trailer_item *new = xcalloc(sizeof(*new), 1);
|
|
|
|
new->token = tok;
|
|
|
|
new->value = val;
|
2016-10-21 05:39:47 +08:00
|
|
|
list_add_tail(&new->list, head);
|
2016-10-22 01:55:03 +08:00
|
|
|
return new;
|
2014-10-14 02:16:26 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
|
|
|
|
const struct conf_info *conf)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
2016-10-21 05:39:49 +08:00
|
|
|
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);
|
2014-10-14 02:16:26 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static void process_command_line_args(struct list_head *arg_head,
|
|
|
|
struct string_list *trailers)
|
2014-10-14 02:16:26 +08:00
|
|
|
{
|
|
|
|
struct string_list_item *tr;
|
2016-10-21 05:39:49 +08:00
|
|
|
struct arg_item *item;
|
2016-10-21 05:39:48 +08:00
|
|
|
struct strbuf tok = STRBUF_INIT;
|
|
|
|
struct strbuf val = STRBUF_INIT;
|
|
|
|
const struct conf_info *conf;
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *pos;
|
2014-10-14 02:16:31 +08:00
|
|
|
|
2016-10-22 01:55:00 +08:00
|
|
|
/*
|
|
|
|
* In command-line arguments, '=' is accepted (in addition to the
|
|
|
|
* separators that are defined).
|
|
|
|
*/
|
|
|
|
char *cl_separators = xstrfmt("=%s", separators);
|
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
/* Add an arg item for each configured trailer with a command */
|
2016-10-21 05:39:47 +08:00
|
|
|
list_for_each(pos, &conf_head) {
|
2016-10-21 05:39:49 +08:00
|
|
|
item = list_entry(pos, struct arg_item, list);
|
2016-10-21 05:39:48 +08:00
|
|
|
if (item->conf.command)
|
2016-10-21 05:39:49 +08:00
|
|
|
add_arg_item(arg_head,
|
|
|
|
xstrdup(token_from_item(item, NULL)),
|
|
|
|
xstrdup(""),
|
|
|
|
&item->conf);
|
2014-10-14 02:16:31 +08:00
|
|
|
}
|
2014-10-14 02:16:26 +08:00
|
|
|
|
2016-10-21 05:39:49 +08:00
|
|
|
/* Add an arg item for each trailer on the command line */
|
2014-10-14 02:16:26 +08:00
|
|
|
for_each_string_list_item(tr, trailers) {
|
2016-10-22 01:55:00 +08:00
|
|
|
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);
|
2016-10-21 05:39:49 +08:00
|
|
|
add_arg_item(arg_head,
|
|
|
|
strbuf_detach(&tok, NULL),
|
|
|
|
strbuf_detach(&val, NULL),
|
|
|
|
conf);
|
2016-10-22 01:55:00 +08:00
|
|
|
}
|
2014-10-14 02:16:26 +08:00
|
|
|
}
|
|
|
|
|
2016-10-22 01:55:00 +08:00
|
|
|
free(cl_separators);
|
2014-10-14 02:16:26 +08:00
|
|
|
}
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
static void read_input_file(struct strbuf *sb, const char *file)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
|
|
|
if (file) {
|
2016-11-03 01:29:18 +08:00
|
|
|
if (strbuf_read_file(sb, file, 0) < 0)
|
2014-10-14 02:16:27 +08:00
|
|
|
die_errno(_("could not read input file '%s'"), file);
|
|
|
|
} else {
|
2016-11-03 01:29:18 +08:00
|
|
|
if (strbuf_read(sb, fileno(stdin), 0) < 0)
|
2014-10-14 02:16:27 +08:00
|
|
|
die_errno(_("could not read from stdin"));
|
|
|
|
}
|
2016-11-03 01:29:18 +08:00
|
|
|
}
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
static const char *next_line(const char *str)
|
|
|
|
{
|
|
|
|
const char *nl = strchrnul(str, '\n');
|
|
|
|
return nl + !!*nl;
|
|
|
|
}
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
/*
|
|
|
|
* Return the position of the start of the last line. If len is 0, return -1.
|
|
|
|
*/
|
|
|
|
static int last_line(const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (len == 0)
|
|
|
|
return -1;
|
|
|
|
if (len == 1)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Skip the last character (in addition to the null terminator),
|
|
|
|
* because if the last character is a newline, it is considered as part
|
|
|
|
* of the last line anyway.
|
|
|
|
*/
|
|
|
|
i = len - 2;
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
for (; i >= 0; i--) {
|
|
|
|
if (buf[i] == '\n')
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-11-03 01:29:18 +08:00
|
|
|
* Return the position of the start of the patch or the length of str if there
|
|
|
|
* is no patch in the message.
|
2014-10-14 02:16:27 +08:00
|
|
|
*/
|
2016-11-03 01:29:18 +08:00
|
|
|
static int find_patch_start(const char *str)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
2016-11-03 01:29:18 +08:00
|
|
|
const char *s;
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
for (s = str; *s; s = next_line(s)) {
|
|
|
|
if (starts_with(s, "---"))
|
|
|
|
return s - str;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
return s - str;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-11-03 01:29:18 +08:00
|
|
|
* Return the position of the first trailer line or len if there are no
|
|
|
|
* trailers.
|
2014-10-14 02:16:27 +08:00
|
|
|
*/
|
2016-11-03 01:29:18 +08:00
|
|
|
static int find_trailer_start(const char *buf, size_t len)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
2016-11-03 01:29:18 +08:00
|
|
|
const char *s;
|
|
|
|
int end_of_title, l, only_spaces = 1;
|
2016-10-22 01:55:01 +08:00
|
|
|
int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
|
2016-10-22 01:55:03 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
2015-08-31 03:14:40 +08:00
|
|
|
|
|
|
|
/* The first paragraph is the title and cannot be trailers */
|
2016-11-03 01:29:18 +08:00
|
|
|
for (s = buf; s < buf + len; s = next_line(s)) {
|
|
|
|
if (s[0] == comment_line_char)
|
2015-08-31 03:14:40 +08:00
|
|
|
continue;
|
2016-11-03 01:29:18 +08:00
|
|
|
if (is_blank_line(s))
|
2015-08-31 03:14:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-11-03 01:29:18 +08:00
|
|
|
end_of_title = s - buf;
|
2014-10-14 02:16:27 +08:00
|
|
|
|
|
|
|
/*
|
2016-10-22 01:55:01 +08:00
|
|
|
* 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.
|
2014-10-14 02:16:27 +08:00
|
|
|
*/
|
2016-11-03 01:29:18 +08:00
|
|
|
for (l = last_line(buf, len);
|
|
|
|
l >= end_of_title;
|
|
|
|
l = last_line(buf, l)) {
|
|
|
|
const char *bol = buf + l;
|
2016-10-22 01:55:01 +08:00
|
|
|
const char **p;
|
|
|
|
int separator_pos;
|
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
if (bol[0] == comment_line_char) {
|
2016-10-22 01:55:03 +08:00
|
|
|
non_trailer_lines += possible_continuation_lines;
|
|
|
|
possible_continuation_lines = 0;
|
2014-10-14 02:16:27 +08:00
|
|
|
continue;
|
2016-10-22 01:55:03 +08:00
|
|
|
}
|
2016-11-03 01:29:18 +08:00
|
|
|
if (is_blank_line(bol)) {
|
2014-10-14 02:16:27 +08:00
|
|
|
if (only_spaces)
|
|
|
|
continue;
|
2016-10-22 01:55:03 +08:00
|
|
|
non_trailer_lines += possible_continuation_lines;
|
2016-10-22 01:55:01 +08:00
|
|
|
if (recognized_prefix &&
|
|
|
|
trailer_lines * 3 >= non_trailer_lines)
|
2016-11-03 01:29:18 +08:00
|
|
|
return next_line(bol) - buf;
|
|
|
|
else if (trailer_lines && !non_trailer_lines)
|
|
|
|
return next_line(bol) - buf;
|
|
|
|
return len;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
2016-10-22 01:55:01 +08:00
|
|
|
only_spaces = 0;
|
|
|
|
|
|
|
|
for (p = git_generated_prefixes; *p; p++) {
|
2016-11-03 01:29:18 +08:00
|
|
|
if (starts_with(bol, *p)) {
|
2016-10-22 01:55:01 +08:00
|
|
|
trailer_lines++;
|
2016-10-22 01:55:03 +08:00
|
|
|
possible_continuation_lines = 0;
|
2016-10-22 01:55:01 +08:00
|
|
|
recognized_prefix = 1;
|
|
|
|
goto continue_outer_loop;
|
|
|
|
}
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
2016-10-22 01:55:01 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
separator_pos = find_separator(bol, separators);
|
|
|
|
if (separator_pos >= 1 && !isspace(bol[0])) {
|
2016-10-22 01:55:01 +08:00
|
|
|
struct list_head *pos;
|
|
|
|
|
|
|
|
trailer_lines++;
|
2016-10-22 01:55:03 +08:00
|
|
|
possible_continuation_lines = 0;
|
2016-10-22 01:55:01 +08:00
|
|
|
if (recognized_prefix)
|
|
|
|
continue;
|
|
|
|
list_for_each(pos, &conf_head) {
|
|
|
|
struct arg_item *item;
|
|
|
|
item = list_entry(pos, struct arg_item, list);
|
2016-11-03 01:29:18 +08:00
|
|
|
if (token_matches_item(bol, item,
|
2016-10-22 01:55:01 +08:00
|
|
|
separator_pos)) {
|
|
|
|
recognized_prefix = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 01:29:18 +08:00
|
|
|
} else if (isspace(bol[0]))
|
2016-10-22 01:55:03 +08:00
|
|
|
possible_continuation_lines++;
|
|
|
|
else {
|
2016-10-22 01:55:01 +08:00
|
|
|
non_trailer_lines++;
|
2016-10-22 01:55:03 +08:00
|
|
|
non_trailer_lines += possible_continuation_lines;
|
|
|
|
possible_continuation_lines = 0;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
2016-10-22 01:55:01 +08:00
|
|
|
continue_outer_loop:
|
|
|
|
;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
return len;
|
2014-11-09 17:23:42 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
/* Return the position of the end of the trailers. */
|
|
|
|
static int find_trailer_end(const char *buf, size_t len)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
2016-11-03 01:29:18 +08:00
|
|
|
return len - ignore_non_trailer(buf, len);
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
static int ends_with_blank_line(const char *buf, size_t len)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
2016-11-03 01:29:18 +08:00
|
|
|
int ll = last_line(buf, len);
|
|
|
|
if (ll < 0)
|
|
|
|
return 0;
|
|
|
|
return is_blank_line(buf + ll);
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 00:57:54 +08:00
|
|
|
static int process_input_file(FILE *outfile,
|
2016-11-03 01:29:18 +08:00
|
|
|
const char *str,
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *head)
|
2014-10-14 02:16:27 +08:00
|
|
|
{
|
2016-11-03 01:29:19 +08:00
|
|
|
struct trailer_info info;
|
2016-10-21 05:39:48 +08:00
|
|
|
struct strbuf tok = STRBUF_INIT;
|
|
|
|
struct strbuf val = STRBUF_INIT;
|
2016-11-03 01:29:19 +08:00
|
|
|
int i;
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
trailer_info_get(&info, str);
|
2014-10-14 02:16:27 +08:00
|
|
|
|
|
|
|
/* Print lines before the trailers as is */
|
2016-11-03 01:29:19 +08:00
|
|
|
fwrite(str, 1, info.trailer_start - str, outfile);
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
if (!info.blank_line_before_trailer)
|
2016-01-15 00:57:54 +08:00
|
|
|
fprintf(outfile, "\n");
|
2014-10-14 02:16:27 +08:00
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
for (i = 0; i < info.trailer_nr; i++) {
|
2016-10-22 01:55:00 +08:00
|
|
|
int separator_pos;
|
2016-11-03 01:29:19 +08:00
|
|
|
char *trailer = info.trailers[i];
|
|
|
|
if (trailer[0] == comment_line_char)
|
2016-10-22 01:55:03 +08:00
|
|
|
continue;
|
2016-11-03 01:29:19 +08:00
|
|
|
separator_pos = find_separator(trailer, separators);
|
2016-10-22 01:55:00 +08:00
|
|
|
if (separator_pos >= 1) {
|
2016-11-03 01:29:19 +08:00
|
|
|
parse_trailer(&tok, &val, NULL, trailer,
|
2016-10-22 01:55:00 +08:00
|
|
|
separator_pos);
|
2016-11-03 01:29:19 +08:00
|
|
|
add_trailer_item(head,
|
|
|
|
strbuf_detach(&tok, NULL),
|
|
|
|
strbuf_detach(&val, NULL));
|
2016-10-22 01:55:01 +08:00
|
|
|
} else {
|
2016-11-03 01:29:19 +08:00
|
|
|
strbuf_addstr(&val, trailer);
|
2016-10-22 01:55:01 +08:00
|
|
|
strbuf_strip_suffix(&val, "\n");
|
|
|
|
add_trailer_item(head,
|
|
|
|
NULL,
|
|
|
|
strbuf_detach(&val, NULL));
|
2014-11-09 17:23:39 +08:00
|
|
|
}
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
trailer_info_release(&info);
|
|
|
|
|
|
|
|
return info.trailer_end - str;
|
2014-10-14 02:16:27 +08:00
|
|
|
}
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
static void free_all(struct list_head *head)
|
2014-10-14 02:16:28 +08:00
|
|
|
{
|
2016-10-21 05:39:47 +08:00
|
|
|
struct list_head *pos, *p;
|
|
|
|
list_for_each_safe(pos, p, head) {
|
|
|
|
list_del(pos);
|
|
|
|
free_trailer_item(list_entry(pos, struct trailer_item, list));
|
2014-10-14 02:16:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 00:57:55 +08:00
|
|
|
static struct tempfile trailers_tempfile;
|
|
|
|
|
|
|
|
static FILE *create_in_place_tempfile(const char *file)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
struct strbuf template = STRBUF_INIT;
|
|
|
|
const char *tail;
|
|
|
|
FILE *outfile;
|
|
|
|
|
|
|
|
if (stat(file, &st))
|
|
|
|
die_errno(_("could not stat %s"), file);
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
die(_("file %s is not a regular file"), file);
|
|
|
|
if (!(st.st_mode & S_IWUSR))
|
|
|
|
die(_("file %s is not writable by user"), file);
|
|
|
|
|
|
|
|
/* Create temporary file in the same directory as the original */
|
|
|
|
tail = strrchr(file, '/');
|
|
|
|
if (tail != NULL)
|
|
|
|
strbuf_add(&template, file, tail - file + 1);
|
|
|
|
strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
|
|
|
|
|
|
|
|
xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
|
|
|
|
strbuf_release(&template);
|
|
|
|
outfile = fdopen_tempfile(&trailers_tempfile, "w");
|
|
|
|
if (!outfile)
|
|
|
|
die_errno(_("could not open temporary file"));
|
|
|
|
|
|
|
|
return outfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
|
2014-10-14 02:16:28 +08:00
|
|
|
{
|
2016-10-21 05:39:47 +08:00
|
|
|
LIST_HEAD(head);
|
|
|
|
LIST_HEAD(arg_head);
|
2016-11-03 01:29:18 +08:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2014-11-09 17:23:42 +08:00
|
|
|
int trailer_end;
|
2016-01-15 00:57:54 +08:00
|
|
|
FILE *outfile = stdout;
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-11-03 01:29:19 +08:00
|
|
|
ensure_configured();
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
read_input_file(&sb, file);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-01-15 00:57:55 +08:00
|
|
|
if (in_place)
|
|
|
|
outfile = create_in_place_tempfile(file);
|
|
|
|
|
2014-10-14 02:16:28 +08:00
|
|
|
/* Print the lines before the trailers */
|
2016-11-03 01:29:18 +08:00
|
|
|
trailer_end = process_input_file(outfile, sb.buf, &head);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
process_command_line_args(&arg_head, trailers);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
process_trailers_lists(&head, &arg_head);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
print_all(outfile, &head, trim_empty);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-10-21 05:39:47 +08:00
|
|
|
free_all(&head);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
|
|
|
/* Print the lines after the trailers as is */
|
2016-11-03 01:29:18 +08:00
|
|
|
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
|
2014-10-14 02:16:28 +08:00
|
|
|
|
2016-01-15 00:57:55 +08:00
|
|
|
if (in_place)
|
|
|
|
if (rename_tempfile(&trailers_tempfile, file))
|
|
|
|
die_errno(_("could not rename temporary file to %s"), file);
|
|
|
|
|
2016-11-03 01:29:18 +08:00
|
|
|
strbuf_release(&sb);
|
2014-10-14 02:16:28 +08:00
|
|
|
}
|
2016-11-03 01:29:19 +08:00
|
|
|
|
|
|
|
void trailer_info_get(struct trailer_info *info, const char *str)
|
|
|
|
{
|
|
|
|
int patch_start, trailer_end, trailer_start;
|
|
|
|
struct strbuf **trailer_lines, **ptr;
|
|
|
|
char **trailer_strings = NULL;
|
|
|
|
size_t nr = 0, alloc = 0;
|
|
|
|
char **last = NULL;
|
|
|
|
|
|
|
|
ensure_configured();
|
|
|
|
|
|
|
|
patch_start = find_patch_start(str);
|
|
|
|
trailer_end = find_trailer_end(str, patch_start);
|
|
|
|
trailer_start = find_trailer_start(str, trailer_end);
|
|
|
|
|
|
|
|
trailer_lines = strbuf_split_buf(str + trailer_start,
|
|
|
|
trailer_end - trailer_start,
|
|
|
|
'\n',
|
|
|
|
0);
|
|
|
|
for (ptr = trailer_lines; *ptr; ptr++) {
|
|
|
|
if (last && isspace((*ptr)->buf[0])) {
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
|
|
|
|
strbuf_addbuf(&sb, *ptr);
|
|
|
|
*last = strbuf_detach(&sb, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ALLOC_GROW(trailer_strings, nr + 1, alloc);
|
|
|
|
trailer_strings[nr] = strbuf_detach(*ptr, NULL);
|
|
|
|
last = find_separator(trailer_strings[nr], separators) >= 1
|
|
|
|
? &trailer_strings[nr]
|
|
|
|
: NULL;
|
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
strbuf_list_free(trailer_lines);
|
|
|
|
|
|
|
|
info->blank_line_before_trailer = ends_with_blank_line(str,
|
|
|
|
trailer_start);
|
|
|
|
info->trailer_start = str + trailer_start;
|
|
|
|
info->trailer_end = str + trailer_end;
|
|
|
|
info->trailers = trailer_strings;
|
|
|
|
info->trailer_nr = nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trailer_info_release(struct trailer_info *info)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < info->trailer_nr; i++)
|
|
|
|
free(info->trailers[i]);
|
|
|
|
free(info->trailers);
|
|
|
|
}
|