1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-13 12:16:10 +02:00
git/t/helper/test-config.c
Glen Choo a4e7e317f8 config: add ctx arg to config_fn_t
Add a new "const struct config_context *ctx" arg to config_fn_t to hold
additional information about the config iteration operation.
config_context has a "struct key_value_info kvi" member that holds
metadata about the config source being read (e.g. what kind of config
source it is, the filename, etc). In this series, we're only interested
in .kvi, so we could have just used "struct key_value_info" as an arg,
but config_context makes it possible to add/adjust members in the future
without changing the config_fn_t signature. We could also consider other
ways of organizing the args (e.g. moving the config name and value into
config_context or key_value_info), but in my experiments, the
incremental benefit doesn't justify the added complexity (e.g. a
config_fn_t will sometimes invoke another config_fn_t but with a
different config value).

In subsequent commits, the .kvi member will replace the global "struct
config_reader" in config.c, making config iteration a global-free
operation. It requires much more work for the machinery to provide
meaningful values of .kvi, so for now, merely change the signature and
call sites, pass NULL as a placeholder value, and don't rely on the arg
in any meaningful way.

Most of the changes are performed by
contrib/coccinelle/config_fn_ctx.pending.cocci, which, for every
config_fn_t:

- Modifies the signature to accept "const struct config_context *ctx"
- Passes "ctx" to any inner config_fn_t, if needed
- Adds UNUSED attributes to "ctx", if needed

Most config_fn_t instances are easily identified by seeing if they are
called by the various config functions. Most of the remaining ones are
manually named in the .cocci patch. Manual cleanups are still needed,
but the majority of it is trivial; it's either adjusting config_fn_t
that the .cocci patch didn't catch, or adding forward declarations of
"struct config_context ctx" to make the signatures make sense.

The non-trivial changes are in cases where we are invoking a config_fn_t
outside of config machinery, and we now need to decide what value of
"ctx" to pass. These cases are:

- trace2/tr2_cfg.c:tr2_cfg_set_fl()

  This is indirectly called by git_config_set() so that the trace2
  machinery can notice the new config values and update its settings
  using the tr2 config parsing function, i.e. tr2_cfg_cb().

- builtin/checkout.c:checkout_main()

  This calls git_xmerge_config() as a shorthand for parsing a CLI arg.
  This might be worth refactoring away in the future, since
  git_xmerge_config() can call git_default_config(), which can do much
  more than just parsing.

Handle them by creating a KVI_INIT macro that initializes "struct
key_value_info" to a reasonable default, and use that to construct the
"ctx" arg.

Signed-off-by: Glen Choo <chooglen@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-06-28 14:06:39 -07:00

237 lines
6.1 KiB
C

#include "test-tool.h"
#include "config.h"
#include "setup.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
*
* git_config_int -> iterate over all values using git_config() and print the
* integer value for the entered key or die
*
* 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,
const struct config_context *ctx UNUSED,
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 parse_int_cb(const char *var, const char *value,
const struct config_context *ctx UNUSED, void *data)
{
const char *key_to_match = data;
if (!strcmp(key_to_match, var)) {
int parsed = git_config_int(value, value);
printf("%d\n", parsed);
}
return 0;
}
static int early_config_cb(const char *var, const char *value,
const struct config_context *ctx UNUSED,
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")) {
if (!git_config_get_value_multi(argv[2], &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;
}
}
if (!git_configset_get_value_multi(&cs, argv[2], &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;
} else if (argc == 3 && !strcmp(argv[1], "git_config_int")) {
git_config(parse_int_cb, (void *) argv[2]);
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;
}