t: make many tests depend less on the refs being files
[git/git-svn.git] / t / helper / test-config.c
blob1a7b8bd3d650fe1111c77115d2c68644b6ac9edb
1 #include "cache.h"
2 #include "config.h"
3 #include "string-list.h"
5 /*
6 * This program exposes the C API of the configuration mechanism
7 * as a set of simple commands in order to facilitate testing.
9 * Reads stdin and prints result of command to stdout:
11 * get_value -> prints the value with highest priority for the entered key
13 * get_value_multi -> prints all values for the entered key in increasing order
14 * of priority
16 * get_int -> print integer value for the entered key or die
18 * get_bool -> print bool value for the entered key or die
20 * get_string -> print string value for the entered key or die
22 * configset_get_value -> returns value with the highest priority for the entered key
23 * from a config_set constructed from files entered as arguments.
25 * configset_get_value_multi -> returns value_list for the entered key sorted in
26 * ascending order of priority from a config_set
27 * constructed from files entered as arguments.
29 * iterate -> iterate over all values using git_config(), and print some
30 * data for each
32 * Examples:
34 * To print the value with highest priority for key "foo.bAr Baz.rock":
35 * test-config get_value "foo.bAr Baz.rock"
39 static const char *scope_name(enum config_scope scope)
41 switch (scope) {
42 case CONFIG_SCOPE_SYSTEM:
43 return "system";
44 case CONFIG_SCOPE_GLOBAL:
45 return "global";
46 case CONFIG_SCOPE_REPO:
47 return "repo";
48 case CONFIG_SCOPE_CMDLINE:
49 return "cmdline";
50 default:
51 return "unknown";
54 static int iterate_cb(const char *var, const char *value, void *data)
56 static int nr;
58 if (nr++)
59 putchar('\n');
61 printf("key=%s\n", var);
62 printf("value=%s\n", value ? value : "(null)");
63 printf("origin=%s\n", current_config_origin_type());
64 printf("name=%s\n", current_config_name());
65 printf("scope=%s\n", scope_name(current_config_scope()));
67 return 0;
70 static int early_config_cb(const char *var, const char *value, void *vdata)
72 const char *key = vdata;
74 if (!strcmp(key, var))
75 printf("%s\n", value);
77 return 0;
80 int cmd_main(int argc, const char **argv)
82 int i, val;
83 const char *v;
84 const struct string_list *strptr;
85 struct config_set cs;
87 if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
88 read_early_config(early_config_cb, (void *)argv[2]);
89 return 0;
92 setup_git_directory();
94 git_configset_init(&cs);
96 if (argc < 2) {
97 fprintf(stderr, "Please, provide a command name on the command-line\n");
98 goto exit1;
99 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
100 if (!git_config_get_value(argv[2], &v)) {
101 if (!v)
102 printf("(NULL)\n");
103 else
104 printf("%s\n", v);
105 goto exit0;
106 } else {
107 printf("Value not found for \"%s\"\n", argv[2]);
108 goto exit1;
110 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
111 strptr = git_config_get_value_multi(argv[2]);
112 if (strptr) {
113 for (i = 0; i < strptr->nr; i++) {
114 v = strptr->items[i].string;
115 if (!v)
116 printf("(NULL)\n");
117 else
118 printf("%s\n", v);
120 goto exit0;
121 } else {
122 printf("Value not found for \"%s\"\n", argv[2]);
123 goto exit1;
125 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
126 if (!git_config_get_int(argv[2], &val)) {
127 printf("%d\n", val);
128 goto exit0;
129 } else {
130 printf("Value not found for \"%s\"\n", argv[2]);
131 goto exit1;
133 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
134 if (!git_config_get_bool(argv[2], &val)) {
135 printf("%d\n", val);
136 goto exit0;
137 } else {
138 printf("Value not found for \"%s\"\n", argv[2]);
139 goto exit1;
141 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
142 if (!git_config_get_string_const(argv[2], &v)) {
143 printf("%s\n", v);
144 goto exit0;
145 } else {
146 printf("Value not found for \"%s\"\n", argv[2]);
147 goto exit1;
149 } else if (!strcmp(argv[1], "configset_get_value")) {
150 for (i = 3; i < argc; i++) {
151 int err;
152 if ((err = git_configset_add_file(&cs, argv[i]))) {
153 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
154 goto exit2;
157 if (!git_configset_get_value(&cs, argv[2], &v)) {
158 if (!v)
159 printf("(NULL)\n");
160 else
161 printf("%s\n", v);
162 goto exit0;
163 } else {
164 printf("Value not found for \"%s\"\n", argv[2]);
165 goto exit1;
167 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
168 for (i = 3; i < argc; i++) {
169 int err;
170 if ((err = git_configset_add_file(&cs, argv[i]))) {
171 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
172 goto exit2;
175 strptr = git_configset_get_value_multi(&cs, argv[2]);
176 if (strptr) {
177 for (i = 0; i < strptr->nr; i++) {
178 v = strptr->items[i].string;
179 if (!v)
180 printf("(NULL)\n");
181 else
182 printf("%s\n", v);
184 goto exit0;
185 } else {
186 printf("Value not found for \"%s\"\n", argv[2]);
187 goto exit1;
189 } else if (!strcmp(argv[1], "iterate")) {
190 git_config(iterate_cb, NULL);
191 goto exit0;
194 die("%s: Please check the syntax and the function name", argv[0]);
196 exit0:
197 git_configset_clear(&cs);
198 return 0;
200 exit1:
201 git_configset_clear(&cs);
202 return 1;
204 exit2:
205 git_configset_clear(&cs);
206 return 2;