trailers: export action enums and corresponding lookup functions
Separate the mechanical changes out of the next patch. The functions are changed to take a pointer to enum, because struct conf_info is not going to be public. Set the default values explicitly in default_conf_info, since they are not anymore close to default_conf_info and it's not obvious which constant has value 0. With the next patches, in fact, the values will not be zero anymore! Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
5800c63717
commit
52fc319d4d
65
trailer.c
65
trailer.c
@ -10,18 +10,13 @@
|
|||||||
* Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
|
* 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 {
|
struct conf_info {
|
||||||
char *name;
|
char *name;
|
||||||
char *key;
|
char *key;
|
||||||
char *command;
|
char *command;
|
||||||
enum action_where where;
|
enum trailer_where where;
|
||||||
enum action_if_exists if_exists;
|
enum trailer_if_exists if_exists;
|
||||||
enum action_if_missing if_missing;
|
enum trailer_if_missing if_missing;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct conf_info default_conf_info;
|
static struct conf_info default_conf_info;
|
||||||
@ -63,7 +58,7 @@ static const char *git_generated_prefixes[] = {
|
|||||||
pos != (head); \
|
pos != (head); \
|
||||||
pos = is_reverse ? pos->prev : pos->next)
|
pos = is_reverse ? pos->prev : pos->next)
|
||||||
|
|
||||||
static int after_or_end(enum action_where where)
|
static int after_or_end(enum trailer_where where)
|
||||||
{
|
{
|
||||||
return (where == WHERE_AFTER) || (where == WHERE_END);
|
return (where == WHERE_AFTER) || (where == WHERE_END);
|
||||||
}
|
}
|
||||||
@ -201,7 +196,7 @@ static int check_if_different(struct trailer_item *in_tok,
|
|||||||
int check_all,
|
int check_all,
|
||||||
struct list_head *head)
|
struct list_head *head)
|
||||||
{
|
{
|
||||||
enum action_where where = arg_tok->conf.where;
|
enum trailer_where where = arg_tok->conf.where;
|
||||||
struct list_head *next_head;
|
struct list_head *next_head;
|
||||||
do {
|
do {
|
||||||
if (same_trailer(in_tok, arg_tok))
|
if (same_trailer(in_tok, arg_tok))
|
||||||
@ -306,7 +301,7 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
|
|||||||
static void apply_arg_if_missing(struct list_head *head,
|
static void apply_arg_if_missing(struct list_head *head,
|
||||||
struct arg_item *arg_tok)
|
struct arg_item *arg_tok)
|
||||||
{
|
{
|
||||||
enum action_where where;
|
enum trailer_where where;
|
||||||
struct trailer_item *to_add;
|
struct trailer_item *to_add;
|
||||||
|
|
||||||
switch (arg_tok->conf.if_missing) {
|
switch (arg_tok->conf.if_missing) {
|
||||||
@ -331,7 +326,7 @@ static int find_same_and_apply_arg(struct list_head *head,
|
|||||||
struct trailer_item *in_tok;
|
struct trailer_item *in_tok;
|
||||||
struct trailer_item *on_tok;
|
struct trailer_item *on_tok;
|
||||||
|
|
||||||
enum action_where where = arg_tok->conf.where;
|
enum trailer_where where = arg_tok->conf.where;
|
||||||
int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
|
int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
|
||||||
int backwards = after_or_end(where);
|
int backwards = after_or_end(where);
|
||||||
struct trailer_item *start_tok;
|
struct trailer_item *start_tok;
|
||||||
@ -373,44 +368,44 @@ static void process_trailers_lists(struct list_head *head,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int set_where(struct conf_info *item, const char *value)
|
int trailer_set_where(enum trailer_where *item, const char *value)
|
||||||
{
|
{
|
||||||
if (!strcasecmp("after", value))
|
if (!strcasecmp("after", value))
|
||||||
item->where = WHERE_AFTER;
|
*item = WHERE_AFTER;
|
||||||
else if (!strcasecmp("before", value))
|
else if (!strcasecmp("before", value))
|
||||||
item->where = WHERE_BEFORE;
|
*item = WHERE_BEFORE;
|
||||||
else if (!strcasecmp("end", value))
|
else if (!strcasecmp("end", value))
|
||||||
item->where = WHERE_END;
|
*item = WHERE_END;
|
||||||
else if (!strcasecmp("start", value))
|
else if (!strcasecmp("start", value))
|
||||||
item->where = WHERE_START;
|
*item = WHERE_START;
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int set_if_exists(struct conf_info *item, const char *value)
|
int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
|
||||||
{
|
{
|
||||||
if (!strcasecmp("addIfDifferent", value))
|
if (!strcasecmp("addIfDifferent", value))
|
||||||
item->if_exists = EXISTS_ADD_IF_DIFFERENT;
|
*item = EXISTS_ADD_IF_DIFFERENT;
|
||||||
else if (!strcasecmp("addIfDifferentNeighbor", value))
|
else if (!strcasecmp("addIfDifferentNeighbor", value))
|
||||||
item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
|
*item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
|
||||||
else if (!strcasecmp("add", value))
|
else if (!strcasecmp("add", value))
|
||||||
item->if_exists = EXISTS_ADD;
|
*item = EXISTS_ADD;
|
||||||
else if (!strcasecmp("replace", value))
|
else if (!strcasecmp("replace", value))
|
||||||
item->if_exists = EXISTS_REPLACE;
|
*item = EXISTS_REPLACE;
|
||||||
else if (!strcasecmp("doNothing", value))
|
else if (!strcasecmp("doNothing", value))
|
||||||
item->if_exists = EXISTS_DO_NOTHING;
|
*item = EXISTS_DO_NOTHING;
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int set_if_missing(struct conf_info *item, const char *value)
|
int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
|
||||||
{
|
{
|
||||||
if (!strcasecmp("doNothing", value))
|
if (!strcasecmp("doNothing", value))
|
||||||
item->if_missing = MISSING_DO_NOTHING;
|
*item = MISSING_DO_NOTHING;
|
||||||
else if (!strcasecmp("add", value))
|
else if (!strcasecmp("add", value))
|
||||||
item->if_missing = MISSING_ADD;
|
*item = MISSING_ADD;
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
@ -470,15 +465,18 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v
|
|||||||
variable_name = strrchr(trailer_item, '.');
|
variable_name = strrchr(trailer_item, '.');
|
||||||
if (!variable_name) {
|
if (!variable_name) {
|
||||||
if (!strcmp(trailer_item, "where")) {
|
if (!strcmp(trailer_item, "where")) {
|
||||||
if (set_where(&default_conf_info, value) < 0)
|
if (trailer_set_where(&default_conf_info.where,
|
||||||
|
value) < 0)
|
||||||
warning(_("unknown value '%s' for key '%s'"),
|
warning(_("unknown value '%s' for key '%s'"),
|
||||||
value, conf_key);
|
value, conf_key);
|
||||||
} else if (!strcmp(trailer_item, "ifexists")) {
|
} else if (!strcmp(trailer_item, "ifexists")) {
|
||||||
if (set_if_exists(&default_conf_info, value) < 0)
|
if (trailer_set_if_exists(&default_conf_info.if_exists,
|
||||||
|
value) < 0)
|
||||||
warning(_("unknown value '%s' for key '%s'"),
|
warning(_("unknown value '%s' for key '%s'"),
|
||||||
value, conf_key);
|
value, conf_key);
|
||||||
} else if (!strcmp(trailer_item, "ifmissing")) {
|
} else if (!strcmp(trailer_item, "ifmissing")) {
|
||||||
if (set_if_missing(&default_conf_info, value) < 0)
|
if (trailer_set_if_missing(&default_conf_info.if_missing,
|
||||||
|
value) < 0)
|
||||||
warning(_("unknown value '%s' for key '%s'"),
|
warning(_("unknown value '%s' for key '%s'"),
|
||||||
value, conf_key);
|
value, conf_key);
|
||||||
} else if (!strcmp(trailer_item, "separators")) {
|
} else if (!strcmp(trailer_item, "separators")) {
|
||||||
@ -532,15 +530,15 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
|
|||||||
conf->command = xstrdup(value);
|
conf->command = xstrdup(value);
|
||||||
break;
|
break;
|
||||||
case TRAILER_WHERE:
|
case TRAILER_WHERE:
|
||||||
if (set_where(conf, value))
|
if (trailer_set_where(&conf->where, value))
|
||||||
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
||||||
break;
|
break;
|
||||||
case TRAILER_IF_EXISTS:
|
case TRAILER_IF_EXISTS:
|
||||||
if (set_if_exists(conf, value))
|
if (trailer_set_if_exists(&conf->if_exists, value))
|
||||||
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
||||||
break;
|
break;
|
||||||
case TRAILER_IF_MISSING:
|
case TRAILER_IF_MISSING:
|
||||||
if (set_if_missing(conf, value))
|
if (trailer_set_if_missing(&conf->if_missing, value))
|
||||||
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -555,6 +553,9 @@ static void ensure_configured(void)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* Default config must be setup first */
|
/* Default config must be setup first */
|
||||||
|
default_conf_info.where = WHERE_END;
|
||||||
|
default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
|
||||||
|
default_conf_info.if_missing = MISSING_ADD;
|
||||||
git_config(git_trailer_default_config, NULL);
|
git_config(git_trailer_default_config, NULL);
|
||||||
git_config(git_trailer_config, NULL);
|
git_config(git_trailer_config, NULL);
|
||||||
configured = 1;
|
configured = 1;
|
||||||
|
22
trailer.h
22
trailer.h
@ -1,6 +1,28 @@
|
|||||||
#ifndef TRAILER_H
|
#ifndef TRAILER_H
|
||||||
#define TRAILER_H
|
#define TRAILER_H
|
||||||
|
|
||||||
|
enum trailer_where {
|
||||||
|
WHERE_END,
|
||||||
|
WHERE_AFTER,
|
||||||
|
WHERE_BEFORE,
|
||||||
|
WHERE_START
|
||||||
|
};
|
||||||
|
enum trailer_if_exists {
|
||||||
|
EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
|
||||||
|
EXISTS_ADD_IF_DIFFERENT,
|
||||||
|
EXISTS_ADD,
|
||||||
|
EXISTS_REPLACE,
|
||||||
|
EXISTS_DO_NOTHING
|
||||||
|
};
|
||||||
|
enum trailer_if_missing {
|
||||||
|
MISSING_ADD,
|
||||||
|
MISSING_DO_NOTHING
|
||||||
|
};
|
||||||
|
|
||||||
|
int trailer_set_where(enum trailer_where *item, const char *value);
|
||||||
|
int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
|
||||||
|
int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
|
||||||
|
|
||||||
struct trailer_info {
|
struct trailer_info {
|
||||||
/*
|
/*
|
||||||
* True if there is a blank line before the location pointed to by
|
* True if there is a blank line before the location pointed to by
|
||||||
|
Reference in New Issue
Block a user