Merge branch 'sb/blame-msg-i18n'
[git] / 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  * configset_get_value -> returns value with the highest priority for the entered key
20  *                      from a config_set constructed from files entered as arguments.
21  *
22  * configset_get_value_multi -> returns value_list for the entered key sorted in
23  *                              ascending order of priority from a config_set
24  *                              constructed from files entered as arguments.
25  *
26  * Examples:
27  *
28  * To print the value with highest priority for key "foo.bAr Baz.rock":
29  *      test-config get_value "foo.bAr Baz.rock"
30  *
31  */
32
33
34 int main(int argc, char **argv)
35 {
36         int i, val;
37         const char *v;
38         const struct string_list *strptr;
39         struct config_set cs;
40         git_configset_init(&cs);
41
42         if (argc < 2) {
43                 fprintf(stderr, "Please, provide a command name on the command-line\n");
44                 goto exit1;
45         } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
46                 if (!git_config_get_value(argv[2], &v)) {
47                         if (!v)
48                                 printf("(NULL)\n");
49                         else
50                                 printf("%s\n", v);
51                         goto exit0;
52                 } else {
53                         printf("Value not found for \"%s\"\n", argv[2]);
54                         goto exit1;
55                 }
56         } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
57                 strptr = git_config_get_value_multi(argv[2]);
58                 if (strptr) {
59                         for (i = 0; i < strptr->nr; i++) {
60                                 v = strptr->items[i].string;
61                                 if (!v)
62                                         printf("(NULL)\n");
63                                 else
64                                         printf("%s\n", v);
65                         }
66                         goto exit0;
67                 } else {
68                         printf("Value not found for \"%s\"\n", argv[2]);
69                         goto exit1;
70                 }
71         } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
72                 if (!git_config_get_int(argv[2], &val)) {
73                         printf("%d\n", val);
74                         goto exit0;
75                 } else {
76                         printf("Value not found for \"%s\"\n", argv[2]);
77                         goto exit1;
78                 }
79         } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
80                 if (!git_config_get_bool(argv[2], &val)) {
81                         printf("%d\n", val);
82                         goto exit0;
83                 } else {
84                         printf("Value not found for \"%s\"\n", argv[2]);
85                         goto exit1;
86                 }
87         } else if (!strcmp(argv[1], "configset_get_value")) {
88                 for (i = 3; i < argc; i++) {
89                         int err;
90                         if ((err = git_configset_add_file(&cs, argv[i]))) {
91                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
92                                 goto exit2;
93                         }
94                 }
95                 if (!git_configset_get_value(&cs, argv[2], &v)) {
96                         if (!v)
97                                 printf("(NULL)\n");
98                         else
99                                 printf("%s\n", v);
100                         goto exit0;
101                 } else {
102                         printf("Value not found for \"%s\"\n", argv[2]);
103                         goto exit1;
104                 }
105         } else if (!strcmp(argv[1], "configset_get_value_multi")) {
106                 for (i = 3; i < argc; i++) {
107                         int err;
108                         if ((err = git_configset_add_file(&cs, argv[i]))) {
109                                 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
110                                 goto exit2;
111                         }
112                 }
113                 strptr = git_configset_get_value_multi(&cs, argv[2]);
114                 if (strptr) {
115                         for (i = 0; i < strptr->nr; i++) {
116                                 v = strptr->items[i].string;
117                                 if (!v)
118                                         printf("(NULL)\n");
119                                 else
120                                         printf("%s\n", v);
121                         }
122                         goto exit0;
123                 } else {
124                         printf("Value not found for \"%s\"\n", argv[2]);
125                         goto exit1;
126                 }
127         }
128
129         die("%s: Please check the syntax and the function name", argv[0]);
130
131 exit0:
132         git_configset_clear(&cs);
133         return 0;
134
135 exit1:
136         git_configset_clear(&cs);
137         return 1;
138
139 exit2:
140         git_configset_clear(&cs);
141         return 2;
142 }