]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-config.c
Merge branch 'es/add-doc-list-short-form-of-all-in-synopsis'
[thirdparty/git.git] / t / helper / test-config.c
CommitLineData
0e2678af 1#include "test-tool.h"
b2141fc1 2#include "config.h"
e38da487 3#include "setup.h"
4c715ebb
TA
4#include "string-list.h"
5
6/*
7 * This program exposes the C API of the configuration mechanism
8 * as a set of simple commands in order to facilitate testing.
9 *
10 * Reads stdin and prints result of command to stdout:
11 *
12 * get_value -> prints the value with highest priority for the entered key
13 *
14 * get_value_multi -> prints all values for the entered key in increasing order
15 * of priority
16 *
b83efcec
ÆAB
17 * get -> print return value for the entered key
18 *
4c715ebb
TA
19 * get_int -> print integer value for the entered key or die
20 *
21 * get_bool -> print bool value for the entered key or die
22 *
8a7b034d
TA
23 * get_string -> print string value for the entered key or die
24 *
4c715ebb
TA
25 * configset_get_value -> returns value with the highest priority for the entered key
26 * from a config_set constructed from files entered as arguments.
27 *
28 * configset_get_value_multi -> returns value_list for the entered key sorted in
29 * ascending order of priority from a config_set
30 * constructed from files entered as arguments.
31 *
0d44a2da
JK
32 * iterate -> iterate over all values using git_config(), and print some
33 * data for each
34 *
e2016508
GC
35 * git_config_int -> iterate over all values using git_config() and print the
36 * integer value for the entered key or die
37 *
4c715ebb
TA
38 * Examples:
39 *
40 * To print the value with highest priority for key "foo.bAr Baz.rock":
0e2678af 41 * test-tool config get_value "foo.bAr Baz.rock"
4c715ebb
TA
42 *
43 */
44
a4e7e317 45static int iterate_cb(const char *var, const char *value,
6021e1d1 46 const struct config_context *ctx,
a4e7e317 47 void *data UNUSED)
0d44a2da 48{
6021e1d1 49 const struct key_value_info *kvi = ctx->kvi;
0d44a2da
JK
50 static int nr;
51
52 if (nr++)
53 putchar('\n');
54
55 printf("key=%s\n", var);
56 printf("value=%s\n", value ? value : "(null)");
6021e1d1
GC
57 printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
58 printf("name=%s\n", kvi->filename ? kvi->filename : "");
59 printf("lno=%d\n", kvi->linenr);
60 printf("scope=%s\n", config_scope_name(kvi->scope));
0d44a2da
JK
61
62 return 0;
63}
4c715ebb 64
a4e7e317 65static int parse_int_cb(const char *var, const char *value,
8868b1eb 66 const struct config_context *ctx, void *data)
e2016508
GC
67{
68 const char *key_to_match = data;
69
70 if (!strcmp(key_to_match, var)) {
8868b1eb 71 int parsed = git_config_int(value, value, ctx->kvi);
e2016508
GC
72 printf("%d\n", parsed);
73 }
74 return 0;
75}
76
a4e7e317
GC
77static int early_config_cb(const char *var, const char *value,
78 const struct config_context *ctx UNUSED,
79 void *vdata)
1a6ec1eb
JS
80{
81 const char *key = vdata;
82
83 if (!strcmp(key, var))
84 printf("%s\n", value);
85
86 return 0;
87}
88
0e2678af 89int cmd__config(int argc, const char **argv)
4c715ebb
TA
90{
91 int i, val;
92 const char *v;
93 const struct string_list *strptr;
94 struct config_set cs;
25a6c280 95
1a6ec1eb
JS
96 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
97 read_early_config(early_config_cb, (void *)argv[2]);
98 return 0;
99 }
100
25a6c280
JK
101 setup_git_directory();
102
4c715ebb
TA
103 git_configset_init(&cs);
104
105 if (argc < 2) {
106 fprintf(stderr, "Please, provide a command name on the command-line\n");
107 goto exit1;
108 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
109 if (!git_config_get_value(argv[2], &v)) {
110 if (!v)
111 printf("(NULL)\n");
112 else
113 printf("%s\n", v);
114 goto exit0;
115 } else {
116 printf("Value not found for \"%s\"\n", argv[2]);
117 goto exit1;
118 }
119 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
a4286193 120 if (!git_config_get_value_multi(argv[2], &strptr)) {
4c715ebb
TA
121 for (i = 0; i < strptr->nr; i++) {
122 v = strptr->items[i].string;
123 if (!v)
124 printf("(NULL)\n");
125 else
126 printf("%s\n", v);
127 }
128 goto exit0;
129 } else {
130 printf("Value not found for \"%s\"\n", argv[2]);
131 goto exit1;
132 }
b83efcec
ÆAB
133 } else if (argc == 3 && !strcmp(argv[1], "get")) {
134 int ret;
135
136 if (!(ret = git_config_get(argv[2])))
137 goto exit0;
138 else if (ret == 1)
139 printf("Value not found for \"%s\"\n", argv[2]);
140 else if (ret == -CONFIG_INVALID_KEY)
141 printf("Key \"%s\" is invalid\n", argv[2]);
142 else if (ret == -CONFIG_NO_SECTION_OR_NAME)
143 printf("Key \"%s\" has no section\n", argv[2]);
144 else
145 /*
146 * A normal caller should just check "ret <
147 * 0", but for our own tests let's BUG() if
148 * our whitelist of git_config_parse_key()
149 * return values isn't exhaustive.
150 */
151 BUG("Key \"%s\" has unknown return %d", argv[2], ret);
152 goto exit1;
4c715ebb
TA
153 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
154 if (!git_config_get_int(argv[2], &val)) {
155 printf("%d\n", val);
156 goto exit0;
157 } else {
158 printf("Value not found for \"%s\"\n", argv[2]);
159 goto exit1;
160 }
161 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
162 if (!git_config_get_bool(argv[2], &val)) {
163 printf("%d\n", val);
164 goto exit0;
165 } else {
166 printf("Value not found for \"%s\"\n", argv[2]);
167 goto exit1;
168 }
8a7b034d 169 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
f1de981e 170 if (!git_config_get_string_tmp(argv[2], &v)) {
8a7b034d
TA
171 printf("%s\n", v);
172 goto exit0;
173 } else {
174 printf("Value not found for \"%s\"\n", argv[2]);
175 goto exit1;
176 }
4c715ebb
TA
177 } else if (!strcmp(argv[1], "configset_get_value")) {
178 for (i = 3; i < argc; i++) {
179 int err;
180 if ((err = git_configset_add_file(&cs, argv[i]))) {
181 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
182 goto exit2;
183 }
184 }
8868b1eb 185 if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
4c715ebb
TA
186 if (!v)
187 printf("(NULL)\n");
188 else
189 printf("%s\n", v);
190 goto exit0;
191 } else {
192 printf("Value not found for \"%s\"\n", argv[2]);
193 goto exit1;
194 }
195 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
196 for (i = 3; i < argc; i++) {
197 int err;
198 if ((err = git_configset_add_file(&cs, argv[i]))) {
199 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
200 goto exit2;
201 }
202 }
a4286193 203 if (!git_configset_get_value_multi(&cs, argv[2], &strptr)) {
4c715ebb
TA
204 for (i = 0; i < strptr->nr; i++) {
205 v = strptr->items[i].string;
206 if (!v)
207 printf("(NULL)\n");
208 else
209 printf("%s\n", v);
210 }
211 goto exit0;
212 } else {
213 printf("Value not found for \"%s\"\n", argv[2]);
214 goto exit1;
215 }
0d44a2da
JK
216 } else if (!strcmp(argv[1], "iterate")) {
217 git_config(iterate_cb, NULL);
218 goto exit0;
e2016508
GC
219 } else if (argc == 3 && !strcmp(argv[1], "git_config_int")) {
220 git_config(parse_int_cb, (void *) argv[2]);
221 goto exit0;
4c715ebb
TA
222 }
223
224 die("%s: Please check the syntax and the function name", argv[0]);
225
226exit0:
227 git_configset_clear(&cs);
228 return 0;
229
230exit1:
231 git_configset_clear(&cs);
232 return 1;
233
234exit2:
235 git_configset_clear(&cs);
236 return 2;
237}