]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-config.c
setup.h: move declarations for setup.c functions from cache.h
[thirdparty/git.git] / t / helper / test-config.c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "setup.h"
5 #include "string-list.h"
6
7 /*
8 * This program exposes the C API of the configuration mechanism
9 * as a set of simple commands in order to facilitate testing.
10 *
11 * Reads stdin and prints result of command to stdout:
12 *
13 * get_value -> prints the value with highest priority for the entered key
14 *
15 * get_value_multi -> prints all values for the entered key in increasing order
16 * of priority
17 *
18 * get_int -> print integer value for the entered key or die
19 *
20 * get_bool -> print bool value for the entered key or die
21 *
22 * get_string -> print string value for the entered key or die
23 *
24 * configset_get_value -> returns value with the highest priority for the entered key
25 * from a config_set constructed from files entered as arguments.
26 *
27 * configset_get_value_multi -> returns value_list for the entered key sorted in
28 * ascending order of priority from a config_set
29 * constructed from files entered as arguments.
30 *
31 * iterate -> iterate over all values using git_config(), and print some
32 * data for each
33 *
34 * Examples:
35 *
36 * To print the value with highest priority for key "foo.bAr Baz.rock":
37 * test-tool config get_value "foo.bAr Baz.rock"
38 *
39 */
40
41 static int iterate_cb(const char *var, const char *value, void *data UNUSED)
42 {
43 static int nr;
44
45 if (nr++)
46 putchar('\n');
47
48 printf("key=%s\n", var);
49 printf("value=%s\n", value ? value : "(null)");
50 printf("origin=%s\n", current_config_origin_type());
51 printf("name=%s\n", current_config_name());
52 printf("lno=%d\n", current_config_line());
53 printf("scope=%s\n", config_scope_name(current_config_scope()));
54
55 return 0;
56 }
57
58 static int early_config_cb(const char *var, const char *value, void *vdata)
59 {
60 const char *key = vdata;
61
62 if (!strcmp(key, var))
63 printf("%s\n", value);
64
65 return 0;
66 }
67
68 int cmd__config(int argc, const char **argv)
69 {
70 int i, val;
71 const char *v;
72 const struct string_list *strptr;
73 struct config_set cs;
74
75 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
76 read_early_config(early_config_cb, (void *)argv[2]);
77 return 0;
78 }
79
80 setup_git_directory();
81
82 git_configset_init(&cs);
83
84 if (argc < 2) {
85 fprintf(stderr, "Please, provide a command name on the command-line\n");
86 goto exit1;
87 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
88 if (!git_config_get_value(argv[2], &v)) {
89 if (!v)
90 printf("(NULL)\n");
91 else
92 printf("%s\n", v);
93 goto exit0;
94 } else {
95 printf("Value not found for \"%s\"\n", argv[2]);
96 goto exit1;
97 }
98 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
99 strptr = git_config_get_value_multi(argv[2]);
100 if (strptr) {
101 for (i = 0; i < strptr->nr; i++) {
102 v = strptr->items[i].string;
103 if (!v)
104 printf("(NULL)\n");
105 else
106 printf("%s\n", v);
107 }
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_int")) {
114 if (!git_config_get_int(argv[2], &val)) {
115 printf("%d\n", val);
116 goto exit0;
117 } else {
118 printf("Value not found for \"%s\"\n", argv[2]);
119 goto exit1;
120 }
121 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
122 if (!git_config_get_bool(argv[2], &val)) {
123 printf("%d\n", val);
124 goto exit0;
125 } else {
126 printf("Value not found for \"%s\"\n", argv[2]);
127 goto exit1;
128 }
129 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
130 if (!git_config_get_string_tmp(argv[2], &v)) {
131 printf("%s\n", v);
132 goto exit0;
133 } else {
134 printf("Value not found for \"%s\"\n", argv[2]);
135 goto exit1;
136 }
137 } else if (!strcmp(argv[1], "configset_get_value")) {
138 for (i = 3; i < argc; i++) {
139 int err;
140 if ((err = git_configset_add_file(&cs, argv[i]))) {
141 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
142 goto exit2;
143 }
144 }
145 if (!git_configset_get_value(&cs, argv[2], &v)) {
146 if (!v)
147 printf("(NULL)\n");
148 else
149 printf("%s\n", v);
150 goto exit0;
151 } else {
152 printf("Value not found for \"%s\"\n", argv[2]);
153 goto exit1;
154 }
155 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
156 for (i = 3; i < argc; i++) {
157 int err;
158 if ((err = git_configset_add_file(&cs, argv[i]))) {
159 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
160 goto exit2;
161 }
162 }
163 strptr = git_configset_get_value_multi(&cs, argv[2]);
164 if (strptr) {
165 for (i = 0; i < strptr->nr; i++) {
166 v = strptr->items[i].string;
167 if (!v)
168 printf("(NULL)\n");
169 else
170 printf("%s\n", v);
171 }
172 goto exit0;
173 } else {
174 printf("Value not found for \"%s\"\n", argv[2]);
175 goto exit1;
176 }
177 } else if (!strcmp(argv[1], "iterate")) {
178 git_config(iterate_cb, NULL);
179 goto exit0;
180 }
181
182 die("%s: Please check the syntax and the function name", argv[0]);
183
184 exit0:
185 git_configset_clear(&cs);
186 return 0;
187
188 exit1:
189 git_configset_clear(&cs);
190 return 1;
191
192 exit2:
193 git_configset_clear(&cs);
194 return 2;
195 }