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