2014-07-28 18:10:39 +08:00
|
|
|
#include "cache.h"
|
2017-06-15 02:07:36 +08:00
|
|
|
#include "config.h"
|
2014-07-28 18:10:39 +08:00
|
|
|
#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_int -> print integer value for the entered key or die
|
|
|
|
*
|
|
|
|
* get_bool -> print bool value for the entered key or die
|
|
|
|
*
|
2014-08-07 19:59:19 +08:00
|
|
|
* get_string -> print string value for the entered key or die
|
|
|
|
*
|
2014-07-28 18:10:39 +08:00
|
|
|
* 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.
|
|
|
|
*
|
2016-05-27 08:32:23 +08:00
|
|
|
* iterate -> iterate over all values using git_config(), and print some
|
|
|
|
* data for each
|
|
|
|
*
|
2014-07-28 18:10:39 +08:00
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* To print the value with highest priority for key "foo.bAr Baz.rock":
|
|
|
|
* test-config get_value "foo.bAr Baz.rock"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
config: add a notion of "scope"
A config callback passed to git_config() doesn't know very
much about the context in which it sees a variable. It can
ask whether the variable comes from a file, and get the file
name. But without analyzing the filename (which is hard to
do accurately), it cannot tell whether it is in system-level
config, user-level config, or repo-specific config.
Generally this doesn't matter; the point of not passing this
to the callback is that it should treat the config the same
no matter where it comes from. But some programs, like
upload-pack, are a special case: we should be able to run
them in an untrusted repository, which means we cannot use
any "dangerous" config from the repository config file (but
it is OK to use it from system or user config).
This patch teaches the config code to record the "scope" of
each variable, and make it available inside config
callbacks, similar to how we give access to the filename.
The scope is the starting source for a particular parsing
operation, and remains the same even if we include other
files (so a .git/config which includes another file will
remain CONFIG_SCOPE_REPO, as it would be similarly
untrusted).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-05-19 06:44:23 +08:00
|
|
|
static const char *scope_name(enum config_scope scope)
|
|
|
|
{
|
|
|
|
switch (scope) {
|
|
|
|
case CONFIG_SCOPE_SYSTEM:
|
|
|
|
return "system";
|
|
|
|
case CONFIG_SCOPE_GLOBAL:
|
|
|
|
return "global";
|
|
|
|
case CONFIG_SCOPE_REPO:
|
|
|
|
return "repo";
|
|
|
|
case CONFIG_SCOPE_CMDLINE:
|
|
|
|
return "cmdline";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 08:32:23 +08:00
|
|
|
static int iterate_cb(const char *var, const char *value, void *data)
|
|
|
|
{
|
|
|
|
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());
|
config: add a notion of "scope"
A config callback passed to git_config() doesn't know very
much about the context in which it sees a variable. It can
ask whether the variable comes from a file, and get the file
name. But without analyzing the filename (which is hard to
do accurately), it cannot tell whether it is in system-level
config, user-level config, or repo-specific config.
Generally this doesn't matter; the point of not passing this
to the callback is that it should treat the config the same
no matter where it comes from. But some programs, like
upload-pack, are a special case: we should be able to run
them in an untrusted repository, which means we cannot use
any "dangerous" config from the repository config file (but
it is OK to use it from system or user config).
This patch teaches the config code to record the "scope" of
each variable, and make it available inside config
callbacks, similar to how we give access to the filename.
The scope is the starting source for a particular parsing
operation, and remains the same even if we include other
files (so a .git/config which includes another file will
remain CONFIG_SCOPE_REPO, as it would be similarly
untrusted).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-05-19 06:44:23 +08:00
|
|
|
printf("scope=%s\n", scope_name(current_config_scope()));
|
2016-05-27 08:32:23 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-28 18:10:39 +08:00
|
|
|
|
2017-03-14 04:11:17 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
add an extra level of indirection to main()
There are certain startup tasks that we expect every git
process to do. In some cases this is just to improve the
quality of the program (e.g., setting up gettext()). In
others it is a requirement for using certain functions in
libgit.a (e.g., system_path() expects that you have called
git_extract_argv0_path()).
Most commands are builtins and are covered by the git.c
version of main(). However, there are still a few external
commands that use their own main(). Each of these has to
remember to include the correct startup sequence, and we are
not always consistent.
Rather than just fix the inconsistencies, let's make this
harder to get wrong by providing a common main() that can
run this standard startup.
We basically have two options to do this:
- the compat/mingw.h file already does something like this by
adding a #define that replaces the definition of main with a
wrapper that calls mingw_startup().
The upside is that the code in each program doesn't need
to be changed at all; it's rewritten on the fly by the
preprocessor.
The downside is that it may make debugging of the startup
sequence a bit more confusing, as the preprocessor is
quietly inserting new code.
- the builtin functions are all of the form cmd_foo(),
and git.c's main() calls them.
This is much more explicit, which may make things more
obvious to somebody reading the code. It's also more
flexible (because of course we have to figure out _which_
cmd_foo() to call).
The downside is that each of the builtins must define
cmd_foo(), instead of just main().
This patch chooses the latter option, preferring the more
explicit approach, even though it is more invasive. We
introduce a new file common-main.c, with the "real" main. It
expects to call cmd_main() from whatever other objects it is
linked against.
We link common-main.o against anything that links against
libgit.a, since we know that such programs will need to do
this setup. Note that common-main.o can't actually go inside
libgit.a, as the linker would not pick up its main()
function automatically (it has no callers).
The rest of the patch is just adjusting all of the various
external programs (mostly in t/helper) to use cmd_main().
I've provided a global declaration for cmd_main(), which
means that all of the programs also need to match its
signature. In particular, many functions need to switch to
"const char **" instead of "char **" for argv. This effect
ripples out to a few other variables and functions, as well.
This makes the patch even more invasive, but the end result
is much better. We should be treating argv strings as const
anyway, and now all programs conform to the same signature
(which also matches the way builtins are defined).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-07-01 13:58:58 +08:00
|
|
|
int cmd_main(int argc, const char **argv)
|
2014-07-28 18:10:39 +08:00
|
|
|
{
|
|
|
|
int i, val;
|
|
|
|
const char *v;
|
|
|
|
const struct string_list *strptr;
|
|
|
|
struct config_set cs;
|
2016-09-13 11:24:10 +08:00
|
|
|
|
2017-03-14 04:11:17 +08:00
|
|
|
if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
|
|
|
|
read_early_config(early_config_cb, (void *)argv[2]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:24:10 +08:00
|
|
|
setup_git_directory();
|
|
|
|
|
2014-07-28 18:10:39 +08:00
|
|
|
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")) {
|
|
|
|
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;
|
|
|
|
}
|
2014-08-07 19:59:19 +08:00
|
|
|
} else if (argc == 3 && !strcmp(argv[1], "get_string")) {
|
|
|
|
if (!git_config_get_string_const(argv[2], &v)) {
|
|
|
|
printf("%s\n", v);
|
|
|
|
goto exit0;
|
|
|
|
} else {
|
|
|
|
printf("Value not found for \"%s\"\n", argv[2]);
|
|
|
|
goto exit1;
|
|
|
|
}
|
2014-07-28 18:10:39 +08:00
|
|
|
} 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;
|
|
|
|
}
|
2016-05-27 08:32:23 +08:00
|
|
|
} else if (!strcmp(argv[1], "iterate")) {
|
|
|
|
git_config(iterate_cb, NULL);
|
|
|
|
goto exit0;
|
2014-07-28 18:10:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|