userdiff: drop parse_driver function
When we parse userdiff config, we generally assume that diff.name.key will affect the "key" value of the "name" driver. However, without confirming that the key is a valid userdiff key, we may accidentally conflict with the ancient "diff.color.*" namespace. The current code is careful not to even create a driver struct if we do not see a key that is known by the diff-driver code. However, this carefulness is unnecessary; the default driver with no keys set behaves exactly the same as having no driver at all. We can simply set up the driver struct as soon as we see we have a config key that looks like a driver. This makes the code a bit more readable. Signed-off-by: Jeff King <peff@peff.net> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
d731f0ade1
commit
0a5987fe5e
50
userdiff.c
50
userdiff.c
@ -184,28 +184,6 @@ static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct userdiff_driver *parse_driver(const char *var,
|
||||
const char *value, const char *type)
|
||||
{
|
||||
struct userdiff_driver *drv;
|
||||
const char *name, *key;
|
||||
int namelen;
|
||||
|
||||
if (parse_config_key(var, "diff", &name, &namelen, &key) < 0 ||
|
||||
!name || strcmp(type, key))
|
||||
return NULL;
|
||||
|
||||
drv = userdiff_find_by_namelen(name, namelen);
|
||||
if (!drv) {
|
||||
ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
|
||||
drv = &drivers[ndrivers++];
|
||||
memset(drv, 0, sizeof(*drv));
|
||||
drv->name = xmemdupz(name, namelen);
|
||||
drv->binary = -1;
|
||||
}
|
||||
return drv;
|
||||
}
|
||||
|
||||
static int parse_funcname(struct userdiff_funcname *f, const char *k,
|
||||
const char *v, int cflags)
|
||||
{
|
||||
@ -233,20 +211,34 @@ static int parse_bool(int *b, const char *k, const char *v)
|
||||
int userdiff_config(const char *k, const char *v)
|
||||
{
|
||||
struct userdiff_driver *drv;
|
||||
const char *name, *type;
|
||||
int namelen;
|
||||
|
||||
if ((drv = parse_driver(k, v, "funcname")))
|
||||
if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
|
||||
return 0;
|
||||
|
||||
drv = userdiff_find_by_namelen(name, namelen);
|
||||
if (!drv) {
|
||||
ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
|
||||
drv = &drivers[ndrivers++];
|
||||
memset(drv, 0, sizeof(*drv));
|
||||
drv->name = xmemdupz(name, namelen);
|
||||
drv->binary = -1;
|
||||
}
|
||||
|
||||
if (!strcmp(type, "funcname"))
|
||||
return parse_funcname(&drv->funcname, k, v, 0);
|
||||
if ((drv = parse_driver(k, v, "xfuncname")))
|
||||
if (!strcmp(type, "xfuncname"))
|
||||
return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
|
||||
if ((drv = parse_driver(k, v, "binary")))
|
||||
if (!strcmp(type, "binary"))
|
||||
return parse_tristate(&drv->binary, k, v);
|
||||
if ((drv = parse_driver(k, v, "command")))
|
||||
if (!strcmp(type, "command"))
|
||||
return git_config_string(&drv->external, k, v);
|
||||
if ((drv = parse_driver(k, v, "textconv")))
|
||||
if (!strcmp(type, "textconv"))
|
||||
return git_config_string(&drv->textconv, k, v);
|
||||
if ((drv = parse_driver(k, v, "cachetextconv")))
|
||||
if (!strcmp(type, "cachetextconv"))
|
||||
return parse_bool(&drv->textconv_want_cache, k, v);
|
||||
if ((drv = parse_driver(k, v, "wordregex")))
|
||||
if (!strcmp(type, "wordregex"))
|
||||
return git_config_string(&drv->word_regex, k, v);
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user