4 #include "string-list.h"
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
17 * get -> print return value for the entered key
19 * get_int -> print integer value for the entered key or die
21 * get_bool -> print bool value for the entered key or die
23 * get_string -> print string value for the entered key or die
25 * configset_get_value -> returns value with the highest priority for the entered key
26 * from a config_set constructed from files entered as arguments.
28 * configset_get_value_multi -> returns value_list for the entered key sorted in
29 * ascending order of priority from a config_set
30 * constructed from files entered as arguments.
32 * iterate -> iterate over all values using git_config(), and print some
35 * git_config_int -> iterate over all values using git_config() and print the
36 * integer value for the entered key or die
40 * To print the value with highest priority for key "foo.bAr Baz.rock":
41 * test-tool config get_value "foo.bAr Baz.rock"
45 static int iterate_cb(const char *var
, const char *value
, void *data UNUSED
)
52 printf("key=%s\n", var
);
53 printf("value=%s\n", value
? value
: "(null)");
54 printf("origin=%s\n", current_config_origin_type());
55 printf("name=%s\n", current_config_name());
56 printf("lno=%d\n", current_config_line());
57 printf("scope=%s\n", config_scope_name(current_config_scope()));
62 static int parse_int_cb(const char *var
, const char *value
, void *data
)
64 const char *key_to_match
= data
;
66 if (!strcmp(key_to_match
, var
)) {
67 int parsed
= git_config_int(value
, value
);
68 printf("%d\n", parsed
);
73 static int early_config_cb(const char *var
, const char *value
, void *vdata
)
75 const char *key
= vdata
;
77 if (!strcmp(key
, var
))
78 printf("%s\n", value
);
83 int cmd__config(int argc
, const char **argv
)
87 const struct string_list
*strptr
;
90 if (argc
== 3 && !strcmp(argv
[1], "read_early_config")) {
91 read_early_config(early_config_cb
, (void *)argv
[2]);
95 setup_git_directory();
97 git_configset_init(&cs
);
100 fprintf(stderr
, "Please, provide a command name on the command-line\n");
102 } else if (argc
== 3 && !strcmp(argv
[1], "get_value")) {
103 if (!git_config_get_value(argv
[2], &v
)) {
110 printf("Value not found for \"%s\"\n", argv
[2]);
113 } else if (argc
== 3 && !strcmp(argv
[1], "get_value_multi")) {
114 if (!git_config_get_value_multi(argv
[2], &strptr
)) {
115 for (i
= 0; i
< strptr
->nr
; i
++) {
116 v
= strptr
->items
[i
].string
;
124 printf("Value not found for \"%s\"\n", argv
[2]);
127 } else if (argc
== 3 && !strcmp(argv
[1], "get")) {
130 if (!(ret
= git_config_get(argv
[2])))
133 printf("Value not found for \"%s\"\n", argv
[2]);
134 else if (ret
== -CONFIG_INVALID_KEY
)
135 printf("Key \"%s\" is invalid\n", argv
[2]);
136 else if (ret
== -CONFIG_NO_SECTION_OR_NAME
)
137 printf("Key \"%s\" has no section\n", argv
[2]);
140 * A normal caller should just check "ret <
141 * 0", but for our own tests let's BUG() if
142 * our whitelist of git_config_parse_key()
143 * return values isn't exhaustive.
145 BUG("Key \"%s\" has unknown return %d", argv
[2], ret
);
147 } else if (argc
== 3 && !strcmp(argv
[1], "get_int")) {
148 if (!git_config_get_int(argv
[2], &val
)) {
152 printf("Value not found for \"%s\"\n", argv
[2]);
155 } else if (argc
== 3 && !strcmp(argv
[1], "get_bool")) {
156 if (!git_config_get_bool(argv
[2], &val
)) {
160 printf("Value not found for \"%s\"\n", argv
[2]);
163 } else if (argc
== 3 && !strcmp(argv
[1], "get_string")) {
164 if (!git_config_get_string_tmp(argv
[2], &v
)) {
168 printf("Value not found for \"%s\"\n", argv
[2]);
171 } else if (!strcmp(argv
[1], "configset_get_value")) {
172 for (i
= 3; i
< argc
; i
++) {
174 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
175 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
179 if (!git_configset_get_value(&cs
, argv
[2], &v
)) {
186 printf("Value not found for \"%s\"\n", argv
[2]);
189 } else if (!strcmp(argv
[1], "configset_get_value_multi")) {
190 for (i
= 3; i
< argc
; i
++) {
192 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
193 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
197 if (!git_configset_get_value_multi(&cs
, argv
[2], &strptr
)) {
198 for (i
= 0; i
< strptr
->nr
; i
++) {
199 v
= strptr
->items
[i
].string
;
207 printf("Value not found for \"%s\"\n", argv
[2]);
210 } else if (!strcmp(argv
[1], "iterate")) {
211 git_config(iterate_cb
, NULL
);
213 } else if (argc
== 3 && !strcmp(argv
[1], "git_config_int")) {
214 git_config(parse_int_cb
, (void *) argv
[2]);
218 die("%s: Please check the syntax and the function name", argv
[0]);
221 git_configset_clear(&cs
);
225 git_configset_clear(&cs
);
229 git_configset_clear(&cs
);