]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-config.c
rebase: allow overriding the maximal length of the generated labels
[thirdparty/git.git] / t / helper / test-config.c
1 #include "test-tool.h"
2 #include "config.h"
3 #include "setup.h"
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 *
17 * get -> print return value for the entered key
18 *
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 *
23 * get_string -> print string value for the entered key or die
24 *
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 *
32 * iterate -> iterate over all values using git_config(), and print some
33 * data for each
34 *
35 * git_config_int -> iterate over all values using git_config() and print the
36 * integer value for the entered key or die
37 *
38 * Examples:
39 *
40 * To print the value with highest priority for key "foo.bAr Baz.rock":
41 * test-tool config get_value "foo.bAr Baz.rock"
42 *
43 */
44
45 static int iterate_cb(const char *var, const char *value, void *data UNUSED)
46 {
47 static int nr;
48
49 if (nr++)
50 putchar('\n');
51
52 printf("key=%s\n", var);
53 printf("value=%s\n", value ? value : "(null)");
54 printf("origin=%s\n", current_config_origin_type());
55 printf("name=%s\n", current_config_name());
56 printf("lno=%d\n", current_config_line());
57 printf("scope=%s\n", config_scope_name(current_config_scope()));
58
59 return 0;
60 }
61
62 static int parse_int_cb(const char *var, const char *value, void *data)
63 {
64 const char *key_to_match = data;
65
66 if (!strcmp(key_to_match, var)) {
67 int parsed = git_config_int(value, value);
68 printf("%d\n", parsed);
69 }
70 return 0;
71 }
72
73 static int early_config_cb(const char *var, const char *value, void *vdata)
74 {
75 const char *key = vdata;
76
77 if (!strcmp(key, var))
78 printf("%s\n", value);
79
80 return 0;
81 }
82
83 int cmd__config(int argc, const char **argv)
84 {
85 int i, val;
86 const char *v;
87 const struct string_list *strptr;
88 struct config_set cs;
89
90 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
91 read_early_config(early_config_cb, (void *)argv[2]);
92 return 0;
93 }
94
95 setup_git_directory();
96
97 git_configset_init(&cs);
98
99 if (argc < 2) {
100 fprintf(stderr, "Please, provide a command name on the command-line\n");
101 goto exit1;
102 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
103 if (!git_config_get_value(argv[2], &v)) {
104 if (!v)
105 printf("(NULL)\n");
106 else
107 printf("%s\n", v);
108 goto exit0;
109 } else {
110 printf("Value not found for \"%s\"\n", argv[2]);
111 goto exit1;
112 }
113 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
114 if (!git_config_get_value_multi(argv[2], &strptr)) {
115 for (i = 0; i < strptr->nr; i++) {
116 v = strptr->items[i].string;
117 if (!v)
118 printf("(NULL)\n");
119 else
120 printf("%s\n", v);
121 }
122 goto exit0;
123 } else {
124 printf("Value not found for \"%s\"\n", argv[2]);
125 goto exit1;
126 }
127 } else if (argc == 3 && !strcmp(argv[1], "get")) {
128 int ret;
129
130 if (!(ret = git_config_get(argv[2])))
131 goto exit0;
132 else if (ret == 1)
133 printf("Value not found for \"%s\"\n", argv[2]);
134 else if (ret == -CONFIG_INVALID_KEY)
135 printf("Key \"%s\" is invalid\n", argv[2]);
136 else if (ret == -CONFIG_NO_SECTION_OR_NAME)
137 printf("Key \"%s\" has no section\n", argv[2]);
138 else
139 /*
140 * A normal caller should just check "ret <
141 * 0", but for our own tests let's BUG() if
142 * our whitelist of git_config_parse_key()
143 * return values isn't exhaustive.
144 */
145 BUG("Key \"%s\" has unknown return %d", argv[2], ret);
146 goto exit1;
147 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
148 if (!git_config_get_int(argv[2], &val)) {
149 printf("%d\n", val);
150 goto exit0;
151 } else {
152 printf("Value not found for \"%s\"\n", argv[2]);
153 goto exit1;
154 }
155 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
156 if (!git_config_get_bool(argv[2], &val)) {
157 printf("%d\n", val);
158 goto exit0;
159 } else {
160 printf("Value not found for \"%s\"\n", argv[2]);
161 goto exit1;
162 }
163 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
164 if (!git_config_get_string_tmp(argv[2], &v)) {
165 printf("%s\n", v);
166 goto exit0;
167 } else {
168 printf("Value not found for \"%s\"\n", argv[2]);
169 goto exit1;
170 }
171 } else if (!strcmp(argv[1], "configset_get_value")) {
172 for (i = 3; i < argc; i++) {
173 int err;
174 if ((err = git_configset_add_file(&cs, argv[i]))) {
175 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
176 goto exit2;
177 }
178 }
179 if (!git_configset_get_value(&cs, argv[2], &v)) {
180 if (!v)
181 printf("(NULL)\n");
182 else
183 printf("%s\n", v);
184 goto exit0;
185 } else {
186 printf("Value not found for \"%s\"\n", argv[2]);
187 goto exit1;
188 }
189 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
190 for (i = 3; i < argc; i++) {
191 int err;
192 if ((err = git_configset_add_file(&cs, argv[i]))) {
193 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
194 goto exit2;
195 }
196 }
197 if (!git_configset_get_value_multi(&cs, argv[2], &strptr)) {
198 for (i = 0; i < strptr->nr; i++) {
199 v = strptr->items[i].string;
200 if (!v)
201 printf("(NULL)\n");
202 else
203 printf("%s\n", v);
204 }
205 goto exit0;
206 } else {
207 printf("Value not found for \"%s\"\n", argv[2]);
208 goto exit1;
209 }
210 } else if (!strcmp(argv[1], "iterate")) {
211 git_config(iterate_cb, NULL);
212 goto exit0;
213 } else if (argc == 3 && !strcmp(argv[1], "git_config_int")) {
214 git_config(parse_int_cb, (void *) argv[2]);
215 goto exit0;
216 }
217
218 die("%s: Please check the syntax and the function name", argv[0]);
219
220 exit0:
221 git_configset_clear(&cs);
222 return 0;
223
224 exit1:
225 git_configset_clear(&cs);
226 return 1;
227
228 exit2:
229 git_configset_clear(&cs);
230 return 2;
231 }