1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-03 07:36:35 +02:00
git/t/helper/test-config.c
Ævar Arnfjörð Bjarmason b83efcecaf config API: add and use a "git_config_get()" family of functions
We already have the basic "git_config_get_value()" function and its
"repo_*" and "configset" siblings to get a given "key" and assign the
last key found to a provided "value".

But some callers don't care about that value, but just want to use the
return value of the "get_value()" function to check whether the key
exist (or another non-zero return value).

The immediate motivation for this is that a subsequent commit will
need to change all callers of the "*_get_value_multi()" family of
functions. In two cases here we (ab)used it to check whether we had
any values for the given key, but didn't care about the return value.

The rest of the callers here used various other config API functions
to do the same, all of which resolved to the same underlying functions
to provide the answer.

Some of these were using either git_config_get_string() or
git_config_get_string_tmp(), see fe4c750fb1 (submodule--helper: fix a
configure_added_submodule() leak, 2022-09-01) for a recent example. We
can now use a helper function that doesn't require a throwaway
variable.

We could have changed git_configset_get_value_multi() (and then
git_config_get_value() etc.) to accept a "NULL" as a "dest" for all
callers, but let's avoid changing the behavior of existing API
users. Having an "unused" value that we throw away internal to
config.c is cheap.

A "NULL as optional dest" pattern is also more fragile, as the intent
of the caller might be misinterpreted if he were to accidentally pass
"NULL", e.g. when "dest" is passed in from another function.

Another name for this function could have been
"*_config_key_exists()", as suggested in [1]. That would work for all
of these callers, and would currently be equivalent to this function,
as the git_configset_get_value() API normalizes all non-zero return
values to a "1".

But adding that API would set us up to lose information, as e.g. if
git_config_parse_key() in the underlying configset_find_element()
fails we'd like to return -1, not 1.

Let's change the underlying configset_find_element() function to
support this use-case, we'll make further use of it in a subsequent
commit where the git_configset_get_value_multi() function itself will
expose this new return value.

This still leaves various inconsistencies and clobbering or ignoring
of the return value in place. E.g here we're modifying
configset_add_value(), but ever since it was added in [2] we've been
ignoring its "int" return value, but as we're changing the
configset_find_element() it uses, let's have it faithfully ferry that
"ret" along.

Let's also use the "RESULT_MUST_BE_USED" macro introduced in [3] to
assert that we're checking the return value of
configset_find_element().

We're leaving the same change to configset_add_value() for some future
series. Once we start paying attention to its return value we'd need
to ferry it up as deep as do_config_from(), and would need to make
least read_{,very_}early_config() and git_protected_config() return an
"int" instead of "void". Let's leave that for now, and focus on
the *_get_*() functions.

1. 3c8687a73e (add `config_set` API for caching config-like files, 2014-07-28)
2. https://lore.kernel.org/git/xmqqczadkq9f.fsf@gitster.g/
3. 1e8697b5c4 (submodule--helper: check repo{_submodule,}_init()
   return values, 2022-09-01),

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-03-28 07:37:52 -07:00

217 lines
5.5 KiB
C

#include "test-tool.h"
#include "cache.h"
#include "config.h"
#include "string-list.h"
/*
* This program exposes the C API of the configuration mechanism
* as a set of simple commands in order to facilitate testing.
*
* Reads stdin and prints result of command to stdout:
*
* get_value -> prints the value with highest priority for the entered key
*
* get_value_multi -> prints all values for the entered key in increasing order
* of priority
*
* get -> print return value for the entered key
*
* get_int -> print integer value for the entered key or die
*
* get_bool -> print bool value for the entered key or die
*
* get_string -> print string value for the entered key or die
*
* configset_get_value -> returns value with the highest priority for the entered key
* from a config_set constructed from files entered as arguments.
*
* configset_get_value_multi -> returns value_list for the entered key sorted in
* ascending order of priority from a config_set
* constructed from files entered as arguments.
*
* iterate -> iterate over all values using git_config(), and print some
* data for each
*
* Examples:
*
* To print the value with highest priority for key "foo.bAr Baz.rock":
* test-tool config get_value "foo.bAr Baz.rock"
*
*/
static int iterate_cb(const char *var, const char *value, void *data UNUSED)
{
static int nr;
if (nr++)
putchar('\n');
printf("key=%s\n", var);
printf("value=%s\n", value ? value : "(null)");
printf("origin=%s\n", current_config_origin_type());
printf("name=%s\n", current_config_name());
printf("lno=%d\n", current_config_line());
printf("scope=%s\n", config_scope_name(current_config_scope()));
return 0;
}
static int early_config_cb(const char *var, const char *value, void *vdata)
{
const char *key = vdata;
if (!strcmp(key, var))
printf("%s\n", value);
return 0;
}
int cmd__config(int argc, const char **argv)
{
int i, val;
const char *v;
const struct string_list *strptr;
struct config_set cs;
if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
read_early_config(early_config_cb, (void *)argv[2]);
return 0;
}
setup_git_directory();
git_configset_init(&cs);
if (argc < 2) {
fprintf(stderr, "Please, provide a command name on the command-line\n");
goto exit1;
} else if (argc == 3 && !strcmp(argv[1], "get_value")) {
if (!git_config_get_value(argv[2], &v)) {
if (!v)
printf("(NULL)\n");
else
printf("%s\n", v);
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
strptr = git_config_get_value_multi(argv[2]);
if (strptr) {
for (i = 0; i < strptr->nr; i++) {
v = strptr->items[i].string;
if (!v)
printf("(NULL)\n");
else
printf("%s\n", v);
}
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (argc == 3 && !strcmp(argv[1], "get")) {
int ret;
if (!(ret = git_config_get(argv[2])))
goto exit0;
else if (ret == 1)
printf("Value not found for \"%s\"\n", argv[2]);
else if (ret == -CONFIG_INVALID_KEY)
printf("Key \"%s\" is invalid\n", argv[2]);
else if (ret == -CONFIG_NO_SECTION_OR_NAME)
printf("Key \"%s\" has no section\n", argv[2]);
else
/*
* A normal caller should just check "ret <
* 0", but for our own tests let's BUG() if
* our whitelist of git_config_parse_key()
* return values isn't exhaustive.
*/
BUG("Key \"%s\" has unknown return %d", argv[2], ret);
goto exit1;
} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
if (!git_config_get_int(argv[2], &val)) {
printf("%d\n", val);
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
if (!git_config_get_bool(argv[2], &val)) {
printf("%d\n", val);
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (argc == 3 && !strcmp(argv[1], "get_string")) {
if (!git_config_get_string_tmp(argv[2], &v)) {
printf("%s\n", v);
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (!strcmp(argv[1], "configset_get_value")) {
for (i = 3; i < argc; i++) {
int err;
if ((err = git_configset_add_file(&cs, argv[i]))) {
fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
goto exit2;
}
}
if (!git_configset_get_value(&cs, argv[2], &v)) {
if (!v)
printf("(NULL)\n");
else
printf("%s\n", v);
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (!strcmp(argv[1], "configset_get_value_multi")) {
for (i = 3; i < argc; i++) {
int err;
if ((err = git_configset_add_file(&cs, argv[i]))) {
fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
goto exit2;
}
}
strptr = git_configset_get_value_multi(&cs, argv[2]);
if (strptr) {
for (i = 0; i < strptr->nr; i++) {
v = strptr->items[i].string;
if (!v)
printf("(NULL)\n");
else
printf("%s\n", v);
}
goto exit0;
} else {
printf("Value not found for \"%s\"\n", argv[2]);
goto exit1;
}
} else if (!strcmp(argv[1], "iterate")) {
git_config(iterate_cb, NULL);
goto exit0;
}
die("%s: Please check the syntax and the function name", argv[0]);
exit0:
git_configset_clear(&cs);
return 0;
exit1:
git_configset_clear(&cs);
return 1;
exit2:
git_configset_clear(&cs);
return 2;
}