Merge branch 'js/fsck-name-object' into 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 static int early_config_cb(const char *var, const char *value, void *vdata)
70 {
71         const char *key = vdata;
72
73         if (!strcmp(key, var))
74                 printf("%s\n", value);
75
76         return 0;
77 }
78
79 int cmd_main(int argc, const char **argv)
80 {
81         int i, val;
82         const char *v;
83         const struct string_list *strptr;
84         struct config_set cs;
85
86         if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
87                 read_early_config(early_config_cb, (void *)argv[2]);
88                 return 0;
89         }
90
91         setup_git_directory();
92
93         git_configset_init(&cs);
94
95         if (argc < 2) {
96                 fprintf(stderr, "Please, provide a command name on the command-line\n");
97                 goto exit1;
98         } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
99                 if (!git_config_get_value(argv[2], &v)) {
100                         if (!v)
101                                 printf("(NULL)\n");
102                         else
103                                 printf("%s\n", v);
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_value_multi")) {
110                 strptr = git_config_get_value_multi(argv[2]);
111                 if (strptr) {
112                         for (i = 0; i < strptr->nr; i++) {
113                                 v = strptr->items[i].string;
114                                 if (!v)
115                                         printf("(NULL)\n");
116                                 else
117                                         printf("%s\n", v);
118                         }
119                         goto exit0;
120                 } else {
121                         printf("Value not found for \"%s\"\n", argv[2]);
122                         goto exit1;
123                 }
124         } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
125                 if (!git_config_get_int(argv[2], &val)) {
126                         printf("%d\n", val);
127                         goto exit0;
128                 } else {
129                         printf("Value not found for \"%s\"\n", argv[2]);
130                         goto exit1;
131                 }
132         } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
133                 if (!git_config_get_bool(argv[2], &val)) {
134                         printf("%d\n", val);
135                         goto exit0;
136                 } else {
137                         printf("Value not found for \"%s\"\n", argv[2]);
138                         goto exit1;
139                 }
140         } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
141                 if (!git_config_get_string_const(argv[2], &v)) {
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")) {
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                 if (!git_configset_get_value(&cs, argv[2], &v)) {
157                         if (!v)
158                                 printf("(NULL)\n");
159                         else
160                                 printf("%s\n", v);
161                         goto exit0;
162                 } else {
163                         printf("Value not found for \"%s\"\n", argv[2]);
164                         goto exit1;
165                 }
166         } else if (!strcmp(argv[1], "configset_get_value_multi")) {
167                 for (i = 3; i < argc; i++) {
168                         int err;
169                         if ((err = git_configset_add_file(&cs, argv[i]))) {
170                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
171                                 goto exit2;
172                         }
173                 }
174                 strptr = git_configset_get_value_multi(&cs, argv[2]);
175                 if (strptr) {
176                         for (i = 0; i < strptr->nr; i++) {
177                                 v = strptr->items[i].string;
178                                 if (!v)
179                                         printf("(NULL)\n");
180                                 else
181                                         printf("%s\n", v);
182                         }
183                         goto exit0;
184                 } else {
185                         printf("Value not found for \"%s\"\n", argv[2]);
186                         goto exit1;
187                 }
188         } else if (!strcmp(argv[1], "iterate")) {
189                 git_config(iterate_cb, NULL);
190                 goto exit0;
191         }
192
193         die("%s: Please check the syntax and the function name", argv[0]);
194
195 exit0:
196         git_configset_clear(&cs);
197         return 0;
198
199 exit1:
200         git_configset_clear(&cs);
201         return 1;
202
203 exit2:
204         git_configset_clear(&cs);
205         return 2;
206 }