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