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("scope=%s\n", config_scope_name(current_config_scope()));
56 static int early_config_cb(const char *var, const char *value, void *vdata)
58 const char *key = vdata;
60 if (!strcmp(key, var))
61 printf("%s\n", value);
66 int cmd__config(int argc, const char **argv)
70 const struct string_list *strptr;
73 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
74 read_early_config(early_config_cb, (void *)argv[2]);
78 setup_git_directory();
80 git_configset_init(&cs);
83 fprintf(stderr, "Please, provide a command name on the command-line\n");
85 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
86 if (!git_config_get_value(argv[2], &v)) {
93 printf("Value not found for \"%s\"\n", argv[2]);
96 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
97 strptr = git_config_get_value_multi(argv[2]);
99 for (i = 0; i < strptr->nr; i++) {
100 v = strptr->items[i].string;
108 printf("Value not found for \"%s\"\n", argv[2]);
111 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
112 if (!git_config_get_int(argv[2], &val)) {
116 printf("Value not found for \"%s\"\n", argv[2]);
119 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
120 if (!git_config_get_bool(argv[2], &val)) {
124 printf("Value not found for \"%s\"\n", argv[2]);
127 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
128 if (!git_config_get_string_const(argv[2], &v)) {
132 printf("Value not found for \"%s\"\n", argv[2]);
135 } else if (!strcmp(argv[1], "configset_get_value")) {
136 for (i = 3; i < argc; i++) {
138 if ((err = git_configset_add_file(&cs, argv[i]))) {
139 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
143 if (!git_configset_get_value(&cs, argv[2], &v)) {
150 printf("Value not found for \"%s\"\n", argv[2]);
153 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
154 for (i = 3; i < argc; i++) {
156 if ((err = git_configset_add_file(&cs, argv[i]))) {
157 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
161 strptr = git_configset_get_value_multi(&cs, argv[2]);
163 for (i = 0; i < strptr->nr; i++) {
164 v = strptr->items[i].string;
172 printf("Value not found for \"%s\"\n", argv[2]);
175 } else if (!strcmp(argv[1], "iterate")) {
176 git_config(iterate_cb, NULL);
180 die("%s: Please check the syntax and the function name", argv[0]);
183 git_configset_clear(&cs);
187 git_configset_clear(&cs);
191 git_configset_clear(&cs);