]>
git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-config.c
2 #include "string-list.h"
5 * This program exposes the C API of the configuration mechanism
6 * as a set of simple commands in order to facilitate testing.
8 * Reads stdin and prints result of command to stdout:
10 * get_value -> prints the value with highest priority for the entered key
12 * get_value_multi -> prints all values for the entered key in increasing order
15 * get_int -> print integer value for the entered key or die
17 * get_bool -> print bool value for the entered key or die
19 * get_string -> print string value for the entered key or die
21 * configset_get_value -> returns value with the highest priority for the entered key
22 * from a config_set constructed from files entered as arguments.
24 * configset_get_value_multi -> returns value_list for the entered key sorted in
25 * ascending order of priority from a config_set
26 * constructed from files entered as arguments.
28 * iterate -> iterate over all values using git_config(), and print some
33 * To print the value with highest priority for key "foo.bAr Baz.rock":
34 * test-config get_value "foo.bAr Baz.rock"
38 static int iterate_cb(const char *var
, const char *value
, void *data
)
45 printf("key=%s\n", var
);
46 printf("value=%s\n", value
? value
: "(null)");
47 printf("origin=%s\n", current_config_origin_type());
48 printf("name=%s\n", current_config_name());
53 int main(int argc
, char **argv
)
57 const struct string_list
*strptr
;
59 git_configset_init(&cs
);
62 fprintf(stderr
, "Please, provide a command name on the command-line\n");
64 } else if (argc
== 3 && !strcmp(argv
[1], "get_value")) {
65 if (!git_config_get_value(argv
[2], &v
)) {
72 printf("Value not found for \"%s\"\n", argv
[2]);
75 } else if (argc
== 3 && !strcmp(argv
[1], "get_value_multi")) {
76 strptr
= git_config_get_value_multi(argv
[2]);
78 for (i
= 0; i
< strptr
->nr
; i
++) {
79 v
= strptr
->items
[i
].string
;
87 printf("Value not found for \"%s\"\n", argv
[2]);
90 } else if (argc
== 3 && !strcmp(argv
[1], "get_int")) {
91 if (!git_config_get_int(argv
[2], &val
)) {
95 printf("Value not found for \"%s\"\n", argv
[2]);
98 } else if (argc
== 3 && !strcmp(argv
[1], "get_bool")) {
99 if (!git_config_get_bool(argv
[2], &val
)) {
103 printf("Value not found for \"%s\"\n", argv
[2]);
106 } else if (argc
== 3 && !strcmp(argv
[1], "get_string")) {
107 if (!git_config_get_string_const(argv
[2], &v
)) {
111 printf("Value not found for \"%s\"\n", argv
[2]);
114 } else if (!strcmp(argv
[1], "configset_get_value")) {
115 for (i
= 3; i
< argc
; i
++) {
117 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
118 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
122 if (!git_configset_get_value(&cs
, argv
[2], &v
)) {
129 printf("Value not found for \"%s\"\n", argv
[2]);
132 } else if (!strcmp(argv
[1], "configset_get_value_multi")) {
133 for (i
= 3; i
< argc
; i
++) {
135 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
136 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
140 strptr
= git_configset_get_value_multi(&cs
, argv
[2]);
142 for (i
= 0; i
< strptr
->nr
; i
++) {
143 v
= strptr
->items
[i
].string
;
151 printf("Value not found for \"%s\"\n", argv
[2]);
154 } else if (!strcmp(argv
[1], "iterate")) {
155 git_config(iterate_cb
, NULL
);
159 die("%s: Please check the syntax and the function name", argv
[0]);
162 git_configset_clear(&cs
);
166 git_configset_clear(&cs
);
170 git_configset_clear(&cs
);