t1700-split-index: drop unnecessary 'grep'
[git.git] / t / helper / test-config.c
blob214003d5b2f9bbe978d5aa4d9c9ab3f9b8a990da
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "string-list.h"
6 /*
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
15 * of priority
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
31 * data for each
33 * Examples:
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 const char *scope_name(enum config_scope scope)
42 switch (scope) {
43 case CONFIG_SCOPE_SYSTEM:
44 return "system";
45 case CONFIG_SCOPE_GLOBAL:
46 return "global";
47 case CONFIG_SCOPE_REPO:
48 return "repo";
49 case CONFIG_SCOPE_CMDLINE:
50 return "cmdline";
51 default:
52 return "unknown";
55 static int iterate_cb(const char *var, const char *value, void *data)
57 static int nr;
59 if (nr++)
60 putchar('\n');
62 printf("key=%s\n", var);
63 printf("value=%s\n", value ? value : "(null)");
64 printf("origin=%s\n", current_config_origin_type());
65 printf("name=%s\n", current_config_name());
66 printf("scope=%s\n", scope_name(current_config_scope()));
68 return 0;
71 static int early_config_cb(const char *var, const char *value, void *vdata)
73 const char *key = vdata;
75 if (!strcmp(key, var))
76 printf("%s\n", value);
78 return 0;
81 int cmd__config(int argc, const char **argv)
83 int i, val;
84 const char *v;
85 const struct string_list *strptr;
86 struct config_set cs;
88 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
89 read_early_config(early_config_cb, (void *)argv[2]);
90 return 0;
93 setup_git_directory();
95 git_configset_init(&cs);
97 if (argc < 2) {
98 fprintf(stderr, "Please, provide a command name on the command-line\n");
99 goto exit1;
100 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
101 if (!git_config_get_value(argv[2], &v)) {
102 if (!v)
103 printf("(NULL)\n");
104 else
105 printf("%s\n", v);
106 goto exit0;
107 } else {
108 printf("Value not found for \"%s\"\n", argv[2]);
109 goto exit1;
111 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
112 strptr = git_config_get_value_multi(argv[2]);
113 if (strptr) {
114 for (i = 0; i < strptr->nr; i++) {
115 v = strptr->items[i].string;
116 if (!v)
117 printf("(NULL)\n");
118 else
119 printf("%s\n", v);
121 goto exit0;
122 } else {
123 printf("Value not found for \"%s\"\n", argv[2]);
124 goto exit1;
126 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
127 if (!git_config_get_int(argv[2], &val)) {
128 printf("%d\n", val);
129 goto exit0;
130 } else {
131 printf("Value not found for \"%s\"\n", argv[2]);
132 goto exit1;
134 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
135 if (!git_config_get_bool(argv[2], &val)) {
136 printf("%d\n", val);
137 goto exit0;
138 } else {
139 printf("Value not found for \"%s\"\n", argv[2]);
140 goto exit1;
142 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
143 if (!git_config_get_string_const(argv[2], &v)) {
144 printf("%s\n", v);
145 goto exit0;
146 } else {
147 printf("Value not found for \"%s\"\n", argv[2]);
148 goto exit1;
150 } else if (!strcmp(argv[1], "configset_get_value")) {
151 for (i = 3; i < argc; i++) {
152 int err;
153 if ((err = git_configset_add_file(&cs, argv[i]))) {
154 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
155 goto exit2;
158 if (!git_configset_get_value(&cs, argv[2], &v)) {
159 if (!v)
160 printf("(NULL)\n");
161 else
162 printf("%s\n", v);
163 goto exit0;
164 } else {
165 printf("Value not found for \"%s\"\n", argv[2]);
166 goto exit1;
168 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
169 for (i = 3; i < argc; i++) {
170 int err;
171 if ((err = git_configset_add_file(&cs, argv[i]))) {
172 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
173 goto exit2;
176 strptr = git_configset_get_value_multi(&cs, argv[2]);
177 if (strptr) {
178 for (i = 0; i < strptr->nr; i++) {
179 v = strptr->items[i].string;
180 if (!v)
181 printf("(NULL)\n");
182 else
183 printf("%s\n", v);
185 goto exit0;
186 } else {
187 printf("Value not found for \"%s\"\n", argv[2]);
188 goto exit1;
190 } else if (!strcmp(argv[1], "iterate")) {
191 git_config(iterate_cb, NULL);
192 goto exit0;
195 die("%s: Please check the syntax and the function name", argv[0]);
197 exit0:
198 git_configset_clear(&cs);
199 return 0;
201 exit1:
202 git_configset_clear(&cs);
203 return 1;
205 exit2:
206 git_configset_clear(&cs);
207 return 2;