Fourth batch
[git/raj.git] / t / helper / test-config.c
blob234c722b485e1ecdf9f00ba56d119c8364a6f949
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "string-list.h"
6 /*
7 * This program exposes the C API of the configuration mechanism
8 * as a set of simple commands in order to facilitate testing.
10 * Reads stdin and prints result of command to stdout:
12 * get_value -> prints the value with highest priority for the entered key
14 * get_value_multi -> prints all values for the entered key in increasing order
15 * of priority
17 * get_int -> print integer value for the entered key or die
19 * get_bool -> print bool value for the entered key or die
21 * get_string -> print string value for the entered key or die
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.
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.
30 * iterate -> iterate over all values using git_config(), and print some
31 * data for each
33 * Examples:
35 * To print the value with highest priority for key "foo.bAr Baz.rock":
36 * test-tool config get_value "foo.bAr Baz.rock"
40 static int iterate_cb(const char *var, const char *value, void *data)
42 static int nr;
44 if (nr++)
45 putchar('\n');
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()));
54 return 0;
57 static int early_config_cb(const char *var, const char *value, void *vdata)
59 const char *key = vdata;
61 if (!strcmp(key, var))
62 printf("%s\n", value);
64 return 0;
67 int cmd__config(int argc, const char **argv)
69 int i, val;
70 const char *v;
71 const struct string_list *strptr;
72 struct config_set cs;
74 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
75 read_early_config(early_config_cb, (void *)argv[2]);
76 return 0;
79 setup_git_directory();
81 git_configset_init(&cs);
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;
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);
107 goto exit0;
108 } else {
109 printf("Value not found for \"%s\"\n", argv[2]);
110 goto exit1;
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;
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;
128 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
129 if (!git_config_get_string_const(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;
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;
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;
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;
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);
171 goto exit0;
172 } else {
173 printf("Value not found for \"%s\"\n", argv[2]);
174 goto exit1;
176 } else if (!strcmp(argv[1], "iterate")) {
177 git_config(iterate_cb, NULL);
178 goto exit0;
181 die("%s: Please check the syntax and the function name", argv[0]);
183 exit0:
184 git_configset_clear(&cs);
185 return 0;
187 exit1:
188 git_configset_clear(&cs);
189 return 1;
191 exit2:
192 git_configset_clear(&cs);
193 return 2;