Merge branch 'js/test-lint-pathname'
[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         git_configset_init(&cs);
76
77         if (argc < 2) {
78                 fprintf(stderr, "Please, provide a command name on the command-line\n");
79                 goto exit1;
80         } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
81                 if (!git_config_get_value(argv[2], &v)) {
82                         if (!v)
83                                 printf("(NULL)\n");
84                         else
85                                 printf("%s\n", v);
86                         goto exit0;
87                 } else {
88                         printf("Value not found for \"%s\"\n", argv[2]);
89                         goto exit1;
90                 }
91         } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
92                 strptr = git_config_get_value_multi(argv[2]);
93                 if (strptr) {
94                         for (i = 0; i < strptr->nr; i++) {
95                                 v = strptr->items[i].string;
96                                 if (!v)
97                                         printf("(NULL)\n");
98                                 else
99                                         printf("%s\n", v);
100                         }
101                         goto exit0;
102                 } else {
103                         printf("Value not found for \"%s\"\n", argv[2]);
104                         goto exit1;
105                 }
106         } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
107                 if (!git_config_get_int(argv[2], &val)) {
108                         printf("%d\n", val);
109                         goto exit0;
110                 } else {
111                         printf("Value not found for \"%s\"\n", argv[2]);
112                         goto exit1;
113                 }
114         } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
115                 if (!git_config_get_bool(argv[2], &val)) {
116                         printf("%d\n", val);
117                         goto exit0;
118                 } else {
119                         printf("Value not found for \"%s\"\n", argv[2]);
120                         goto exit1;
121                 }
122         } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
123                 if (!git_config_get_string_const(argv[2], &v)) {
124                         printf("%s\n", v);
125                         goto exit0;
126                 } else {
127                         printf("Value not found for \"%s\"\n", argv[2]);
128                         goto exit1;
129                 }
130         } else if (!strcmp(argv[1], "configset_get_value")) {
131                 for (i = 3; i < argc; i++) {
132                         int err;
133                         if ((err = git_configset_add_file(&cs, argv[i]))) {
134                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
135                                 goto exit2;
136                         }
137                 }
138                 if (!git_configset_get_value(&cs, argv[2], &v)) {
139                         if (!v)
140                                 printf("(NULL)\n");
141                         else
142                                 printf("%s\n", v);
143                         goto exit0;
144                 } else {
145                         printf("Value not found for \"%s\"\n", argv[2]);
146                         goto exit1;
147                 }
148         } else if (!strcmp(argv[1], "configset_get_value_multi")) {
149                 for (i = 3; i < argc; i++) {
150                         int err;
151                         if ((err = git_configset_add_file(&cs, argv[i]))) {
152                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
153                                 goto exit2;
154                         }
155                 }
156                 strptr = git_configset_get_value_multi(&cs, argv[2]);
157                 if (strptr) {
158                         for (i = 0; i < strptr->nr; i++) {
159                                 v = strptr->items[i].string;
160                                 if (!v)
161                                         printf("(NULL)\n");
162                                 else
163                                         printf("%s\n", v);
164                         }
165                         goto exit0;
166                 } else {
167                         printf("Value not found for \"%s\"\n", argv[2]);
168                         goto exit1;
169                 }
170         } else if (!strcmp(argv[1], "iterate")) {
171                 git_config(iterate_cb, NULL);
172                 goto exit0;
173         }
174
175         die("%s: Please check the syntax and the function name", argv[0]);
176
177 exit0:
178         git_configset_clear(&cs);
179         return 0;
180
181 exit1:
182         git_configset_clear(&cs);
183         return 1;
184
185 exit2:
186         git_configset_clear(&cs);
187         return 2;
188 }