config: extract function to parse config pairs

The function `git_config_parse_parameter` is responsible for parsing a `foo.bar=baz`-formatted configuration key, sanitizing the key and then processing it via the given callback function. Given that we're about to add a second user which is going to process keys which already has keys and values separated, this commit extracts a function `config_parse_pair` which only does the sanitization and processing part as a preparatory step. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jan 12, 2021 at 13:26 UTC b342ae61b3658ed4c062a27f8e46ea79c38d4197
1 file changed +17 -7
config.c
+17 -7
@@ -462,11 +462,26 @@ int git_config_key_is_valid(const char *key)
462 return !git_config_parse_key_1(key, NULL, NULL, 1);
463 }
464
465 +static int config_parse_pair(const char *key, const char *value,
466 + config_fn_t fn, void *data)
467 +{
468 + char *canonical_name;
469 + int ret;
470 +
471 + if (!strlen(key))
472 + return error(_("empty config key"));
473 + if (git_config_parse_key(key, &canonical_name, NULL))
474 + return -1;
475 +
476 + ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
477 + free(canonical_name);
478 + return ret;
479 +}
480 +
481 int git_config_parse_parameter(const char *text,
482 config_fn_t fn, void *data)
483 {
484 const char *value;
469 - char *canonical_name;
485 struct strbuf **pair;
486 int ret;
487
@@ -487,12 +502,7 @@ int git_config_parse_parameter(const char *text,
502 return error(_("bogus config parameter: %s"), text);
503 }
504
490 - if (git_config_parse_key(pair[0]->buf, &canonical_name, NULL)) {
491 - ret = -1;
492 - } else {
493 - ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
494 - free(canonical_name);
495 - }
505 + ret = config_parse_pair(pair[0]->buf, value, fn, data);
506 strbuf_list_free(pair);
507 return ret;
508 }