]>
git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-config.c
5 #include "string-list.h"
8 * This program exposes the C API of the configuration mechanism
9 * as a set of simple commands in order to facilitate testing.
11 * Reads stdin and prints result of command to stdout:
13 * get_value -> prints the value with highest priority for the entered key
15 * get_value_multi -> prints all values for the entered key in increasing order
18 * get_int -> print integer value for the entered key or die
20 * get_bool -> print bool value for the entered key or die
22 * get_string -> print string value for the entered key or die
24 * configset_get_value -> returns value with the highest priority for the entered key
25 * from a config_set constructed from files entered as arguments.
27 * configset_get_value_multi -> returns value_list for the entered key sorted in
28 * ascending order of priority from a config_set
29 * constructed from files entered as arguments.
31 * iterate -> iterate over all values using git_config(), and print some
36 * To print the value with highest priority for key "foo.bAr Baz.rock":
37 * test-tool config get_value "foo.bAr Baz.rock"
41 static int iterate_cb(const char *var
, const char *value
, void *data UNUSED
)
48 printf("key=%s\n", var
);
49 printf("value=%s\n", value
? value
: "(null)");
50 printf("origin=%s\n", current_config_origin_type());
51 printf("name=%s\n", current_config_name());
52 printf("lno=%d\n", current_config_line());
53 printf("scope=%s\n", config_scope_name(current_config_scope()));
58 static int early_config_cb(const char *var
, const char *value
, void *vdata
)
60 const char *key
= vdata
;
62 if (!strcmp(key
, var
))
63 printf("%s\n", value
);
68 int cmd__config(int argc
, const char **argv
)
72 const struct string_list
*strptr
;
75 if (argc
== 3 && !strcmp(argv
[1], "read_early_config")) {
76 read_early_config(early_config_cb
, (void *)argv
[2]);
80 setup_git_directory();
82 git_configset_init(&cs
);
85 fprintf(stderr
, "Please, provide a command name on the command-line\n");
87 } else if (argc
== 3 && !strcmp(argv
[1], "get_value")) {
88 if (!git_config_get_value(argv
[2], &v
)) {
95 printf("Value not found for \"%s\"\n", argv
[2]);
98 } else if (argc
== 3 && !strcmp(argv
[1], "get_value_multi")) {
99 strptr
= git_config_get_value_multi(argv
[2]);
101 for (i
= 0; i
< strptr
->nr
; i
++) {
102 v
= strptr
->items
[i
].string
;
110 printf("Value not found for \"%s\"\n", argv
[2]);
113 } else if (argc
== 3 && !strcmp(argv
[1], "get_int")) {
114 if (!git_config_get_int(argv
[2], &val
)) {
118 printf("Value not found for \"%s\"\n", argv
[2]);
121 } else if (argc
== 3 && !strcmp(argv
[1], "get_bool")) {
122 if (!git_config_get_bool(argv
[2], &val
)) {
126 printf("Value not found for \"%s\"\n", argv
[2]);
129 } else if (argc
== 3 && !strcmp(argv
[1], "get_string")) {
130 if (!git_config_get_string_tmp(argv
[2], &v
)) {
134 printf("Value not found for \"%s\"\n", argv
[2]);
137 } else if (!strcmp(argv
[1], "configset_get_value")) {
138 for (i
= 3; i
< argc
; i
++) {
140 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
141 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
145 if (!git_configset_get_value(&cs
, argv
[2], &v
)) {
152 printf("Value not found for \"%s\"\n", argv
[2]);
155 } else if (!strcmp(argv
[1], "configset_get_value_multi")) {
156 for (i
= 3; i
< argc
; i
++) {
158 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
159 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
163 strptr
= git_configset_get_value_multi(&cs
, argv
[2]);
165 for (i
= 0; i
< strptr
->nr
; i
++) {
166 v
= strptr
->items
[i
].string
;
174 printf("Value not found for \"%s\"\n", argv
[2]);
177 } else if (!strcmp(argv
[1], "iterate")) {
178 git_config(iterate_cb
, NULL
);
182 die("%s: Please check the syntax and the function name", argv
[0]);
185 git_configset_clear(&cs
);
189 git_configset_clear(&cs
);
193 git_configset_clear(&cs
);