Commit d731f0ad authored by Jeff King's avatar Jeff King Committed by Junio C Hamano

convert some config callbacks to parse_config_key

These callers can drop some inline pointer arithmetic and
magic offset constants, making them more readable and less
error-prone (those constants had to match the lengths of
strings, but there is no automatic verification of that
fact).

The "ep" pointer (presumably for "end pointer"), which
points to the final key segment of the config variable, is
given the more standard name "key" to describe its function
rather than its derivation.
Signed-off-by: default avatarJeff King <[email protected]>
Reviewed-by: default avatarJonathan Nieder <[email protected]>
Signed-off-by: default avatarJunio C Hamano <[email protected]>
parent 785a0429
...@@ -457,7 +457,7 @@ static struct convert_driver { ...@@ -457,7 +457,7 @@ static struct convert_driver {
static int read_convert_config(const char *var, const char *value, void *cb) static int read_convert_config(const char *var, const char *value, void *cb)
{ {
const char *ep, *name; const char *key, *name;
int namelen; int namelen;
struct convert_driver *drv; struct convert_driver *drv;
...@@ -465,10 +465,8 @@ static int read_convert_config(const char *var, const char *value, void *cb) ...@@ -465,10 +465,8 @@ static int read_convert_config(const char *var, const char *value, void *cb)
* External conversion drivers are configured using * External conversion drivers are configured using
* "filter.<name>.variable". * "filter.<name>.variable".
*/ */
if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6) if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
return 0; return 0;
name = var + 7;
namelen = ep - name;
for (drv = user_convert; drv; drv = drv->next) for (drv = user_convert; drv; drv = drv->next)
if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
break; break;
...@@ -479,8 +477,6 @@ static int read_convert_config(const char *var, const char *value, void *cb) ...@@ -479,8 +477,6 @@ static int read_convert_config(const char *var, const char *value, void *cb)
user_convert_tail = &(drv->next); user_convert_tail = &(drv->next);
} }
ep++;
/* /*
* filter.<name>.smudge and filter.<name>.clean specifies * filter.<name>.smudge and filter.<name>.clean specifies
* the command line: * the command line:
...@@ -490,13 +486,13 @@ static int read_convert_config(const char *var, const char *value, void *cb) ...@@ -490,13 +486,13 @@ static int read_convert_config(const char *var, const char *value, void *cb)
* The command-line will not be interpolated in any way. * The command-line will not be interpolated in any way.
*/ */
if (!strcmp("smudge", ep)) if (!strcmp("smudge", key))
return git_config_string(&drv->smudge, var, value); return git_config_string(&drv->smudge, var, value);
if (!strcmp("clean", ep)) if (!strcmp("clean", key))
return git_config_string(&drv->clean, var, value); return git_config_string(&drv->clean, var, value);
if (!strcmp("required", ep)) { if (!strcmp("required", key)) {
drv->required = git_config_bool(var, value); drv->required = git_config_bool(var, value);
return 0; return 0;
} }
......
...@@ -222,7 +222,7 @@ static const char *default_ll_merge; ...@@ -222,7 +222,7 @@ static const char *default_ll_merge;
static int read_merge_config(const char *var, const char *value, void *cb) static int read_merge_config(const char *var, const char *value, void *cb)
{ {
struct ll_merge_driver *fn; struct ll_merge_driver *fn;
const char *ep, *name; const char *key, *name;
int namelen; int namelen;
if (!strcmp(var, "merge.default")) { if (!strcmp(var, "merge.default")) {
...@@ -236,15 +236,13 @@ static int read_merge_config(const char *var, const char *value, void *cb) ...@@ -236,15 +236,13 @@ static int read_merge_config(const char *var, const char *value, void *cb)
* especially, we do not want to look at variables such as * especially, we do not want to look at variables such as
* "merge.summary", "merge.tool", and "merge.verbosity". * "merge.summary", "merge.tool", and "merge.verbosity".
*/ */
if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5) if (parse_config_key(var, "merge", &name, &namelen, &key) < 0 || !name)
return 0; return 0;
/* /*
* Find existing one as we might be processing merge.<name>.var2 * Find existing one as we might be processing merge.<name>.var2
* after seeing merge.<name>.var1. * after seeing merge.<name>.var1.
*/ */
name = var + 6;
namelen = ep - name;
for (fn = ll_user_merge; fn; fn = fn->next) for (fn = ll_user_merge; fn; fn = fn->next)
if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
break; break;
...@@ -256,16 +254,14 @@ static int read_merge_config(const char *var, const char *value, void *cb) ...@@ -256,16 +254,14 @@ static int read_merge_config(const char *var, const char *value, void *cb)
ll_user_merge_tail = &(fn->next); ll_user_merge_tail = &(fn->next);
} }
ep++; if (!strcmp("name", key)) {
if (!strcmp("name", ep)) {
if (!value) if (!value)
return error("%s: lacks value", var); return error("%s: lacks value", var);
fn->description = xstrdup(value); fn->description = xstrdup(value);
return 0; return 0;
} }
if (!strcmp("driver", ep)) { if (!strcmp("driver", key)) {
if (!value) if (!value)
return error("%s: lacks value", var); return error("%s: lacks value", var);
/* /*
...@@ -289,7 +285,7 @@ static int read_merge_config(const char *var, const char *value, void *cb) ...@@ -289,7 +285,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
return 0; return 0;
} }
if (!strcmp("recursive", ep)) { if (!strcmp("recursive", key)) {
if (!value) if (!value)
return error("%s: lacks value", var); return error("%s: lacks value", var);
fn->recursive = xstrdup(value); fn->recursive = xstrdup(value);
......
...@@ -188,20 +188,13 @@ static struct userdiff_driver *parse_driver(const char *var, ...@@ -188,20 +188,13 @@ static struct userdiff_driver *parse_driver(const char *var,
const char *value, const char *type) const char *value, const char *type)
{ {
struct userdiff_driver *drv; struct userdiff_driver *drv;
const char *dot; const char *name, *key;
const char *name;
int namelen; int namelen;
if (prefixcmp(var, "diff.")) if (parse_config_key(var, "diff", &name, &namelen, &key) < 0 ||
return NULL; !name || strcmp(type, key))
dot = strrchr(var, '.');
if (dot == var + 4)
return NULL;
if (strcmp(type, dot+1))
return NULL; return NULL;
name = var + 5;
namelen = dot - name;
drv = userdiff_find_by_namelen(name, namelen); drv = userdiff_find_by_namelen(name, namelen);
if (!drv) { if (!drv) {
ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment