4 #include "string-list.h"
7 * This program exposes the C API of the configuration mechanism
8 * as a set of simple commands in order to facilitate testing.
10 * Reads stdin and prints result of command to stdout:
12 * get_value -> prints the value with highest priority for the entered key
14 * get_value_multi -> prints all values for the entered key in increasing order
17 * get_int -> print integer value for the entered key or die
19 * get_bool -> print bool value for the entered key or die
21 * get_string -> print string value for the entered key or die
23 * configset_get_value -> returns value with the highest priority for the entered key
24 * from a config_set constructed from files entered as arguments.
26 * configset_get_value_multi -> returns value_list for the entered key sorted in
27 * ascending order of priority from a config_set
28 * constructed from files entered as arguments.
30 * iterate -> iterate over all values using git_config(), and print some
35 * To print the value with highest priority for key "foo.bAr Baz.rock":
36 * test-tool config get_value "foo.bAr Baz.rock"
40 static int iterate_cb(const char *var, const char *value, void *data)
47 printf("key=%s\n", var);
48 printf("value=%s\n", value ? value : "(null)");
49 printf("origin=%s\n", current_config_origin_type());
50 printf("name=%s\n", current_config_name());
51 printf("lno=%d\n", current_config_line());
52 printf("scope=%s\n", config_scope_name(current_config_scope()));
57 static int early_config_cb(const char *var, const char *value, void *vdata)
59 const char *key = vdata;
61 if (!strcmp(key, var))
62 printf("%s\n", value);
67 int cmd__config(int argc, const char **argv)
71 const struct string_list *strptr;
74 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
75 read_early_config(early_config_cb, (void *)argv[2]);
79 setup_git_directory();
81 git_configset_init(&cs);
84 fprintf(stderr, "Please, provide a command name on the command-line\n");
86 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
87 if (!git_config_get_value(argv[2], &v)) {
94 printf("Value not found for \"%s\"\n", argv[2]);
97 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
98 strptr = git_config_get_value_multi(argv[2]);
100 for (i = 0; i < strptr->nr; i++) {
101 v = strptr->items[i].string;
109 printf("Value not found for \"%s\"\n", argv[2]);
112 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
113 if (!git_config_get_int(argv[2], &val)) {
117 printf("Value not found for \"%s\"\n", argv[2]);
120 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
121 if (!git_config_get_bool(argv[2], &val)) {
125 printf("Value not found for \"%s\"\n", argv[2]);
128 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
129 if (!git_config_get_string_tmp(argv[2], &v)) {
133 printf("Value not found for \"%s\"\n", argv[2]);
136 } else if (!strcmp(argv[1], "configset_get_value")) {
137 for (i = 3; i < argc; i++) {
139 if ((err = git_configset_add_file(&cs, argv[i]))) {
140 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
144 if (!git_configset_get_value(&cs, argv[2], &v)) {
151 printf("Value not found for \"%s\"\n", argv[2]);
154 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
155 for (i = 3; i < argc; i++) {
157 if ((err = git_configset_add_file(&cs, argv[i]))) {
158 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
162 strptr = git_configset_get_value_multi(&cs, argv[2]);
164 for (i = 0; i < strptr->nr; i++) {
165 v = strptr->items[i].string;
173 printf("Value not found for \"%s\"\n", argv[2]);
176 } else if (!strcmp(argv[1], "iterate")) {
177 git_config(iterate_cb, NULL);
181 die("%s: Please check the syntax and the function name", argv[0]);
184 git_configset_clear(&cs);
188 git_configset_clear(&cs);
192 git_configset_clear(&cs);