3 #include "string-list.h"
6 * This program exposes the C API of the configuration mechanism
7 * as a set of simple commands in order to facilitate testing.
9 * Reads stdin and prints result of command to stdout:
11 * get_value -> prints the value with highest priority for the entered key
13 * get_value_multi -> prints all values for the entered key in increasing order
16 * get_int -> print integer value for the entered key or die
18 * get_bool -> print bool value for the entered key or die
20 * get_string -> print string value for the entered key or die
22 * configset_get_value -> returns value with the highest priority for the entered key
23 * from a config_set constructed from files entered as arguments.
25 * configset_get_value_multi -> returns value_list for the entered key sorted in
26 * ascending order of priority from a config_set
27 * constructed from files entered as arguments.
29 * iterate -> iterate over all values using git_config(), and print some
34 * To print the value with highest priority for key "foo.bAr Baz.rock":
35 * test-config get_value "foo.bAr Baz.rock"
39 static const char *scope_name(enum config_scope scope)
42 case CONFIG_SCOPE_SYSTEM:
44 case CONFIG_SCOPE_GLOBAL:
46 case CONFIG_SCOPE_REPO:
48 case CONFIG_SCOPE_CMDLINE:
54 static int iterate_cb(const char *var, const char *value, void *data)
61 printf("key=%s\n", var);
62 printf("value=%s\n", value ? value : "(null)");
63 printf("origin=%s\n", current_config_origin_type());
64 printf("name=%s\n", current_config_name());
65 printf("scope=%s\n", scope_name(current_config_scope()));
70 static int early_config_cb(const char *var, const char *value, void *vdata)
72 const char *key = vdata;
74 if (!strcmp(key, var))
75 printf("%s\n", value);
80 int cmd_main(int argc, const char **argv)
84 const struct string_list *strptr;
87 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
88 read_early_config(early_config_cb, (void *)argv[2]);
92 setup_git_directory();
94 git_configset_init(&cs);
97 fprintf(stderr, "Please, provide a command name on the command-line\n");
99 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
100 if (!git_config_get_value(argv[2], &v)) {
107 printf("Value not found for \"%s\"\n", argv[2]);
110 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
111 strptr = git_config_get_value_multi(argv[2]);
113 for (i = 0; i < strptr->nr; i++) {
114 v = strptr->items[i].string;
122 printf("Value not found for \"%s\"\n", argv[2]);
125 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
126 if (!git_config_get_int(argv[2], &val)) {
130 printf("Value not found for \"%s\"\n", argv[2]);
133 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
134 if (!git_config_get_bool(argv[2], &val)) {
138 printf("Value not found for \"%s\"\n", argv[2]);
141 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
142 if (!git_config_get_string_const(argv[2], &v)) {
146 printf("Value not found for \"%s\"\n", argv[2]);
149 } else if (!strcmp(argv[1], "configset_get_value")) {
150 for (i = 3; i < argc; i++) {
152 if ((err = git_configset_add_file(&cs, argv[i]))) {
153 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
157 if (!git_configset_get_value(&cs, argv[2], &v)) {
164 printf("Value not found for \"%s\"\n", argv[2]);
167 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
168 for (i = 3; i < argc; i++) {
170 if ((err = git_configset_add_file(&cs, argv[i]))) {
171 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
175 strptr = git_configset_get_value_multi(&cs, argv[2]);
177 for (i = 0; i < strptr->nr; i++) {
178 v = strptr->items[i].string;
186 printf("Value not found for \"%s\"\n", argv[2]);
189 } else if (!strcmp(argv[1], "iterate")) {
190 git_config(iterate_cb, NULL);
194 die("%s: Please check the syntax and the function name", argv[0]);
197 git_configset_clear(&cs);
201 git_configset_clear(&cs);
205 git_configset_clear(&cs);