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_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
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 const char *scope_name(enum config_scope scope
)
43 case CONFIG_SCOPE_SYSTEM
:
45 case CONFIG_SCOPE_GLOBAL
:
47 case CONFIG_SCOPE_REPO
:
49 case CONFIG_SCOPE_CMDLINE
:
55 static int iterate_cb(const char *var
, const char *value
, void *data
)
62 printf("key=%s\n", var
);
63 printf("value=%s\n", value
? value
: "(null)");
64 printf("origin=%s\n", current_config_origin_type());
65 printf("name=%s\n", current_config_name());
66 printf("scope=%s\n", scope_name(current_config_scope()));
71 static int early_config_cb(const char *var
, const char *value
, void *vdata
)
73 const char *key
= vdata
;
75 if (!strcmp(key
, var
))
76 printf("%s\n", value
);
81 int cmd__config(int argc
, const char **argv
)
85 const struct string_list
*strptr
;
88 if (argc
== 3 && !strcmp(argv
[1], "read_early_config")) {
89 read_early_config(early_config_cb
, (void *)argv
[2]);
93 setup_git_directory();
95 git_configset_init(&cs
);
98 fprintf(stderr
, "Please, provide a command name on the command-line\n");
100 } else if (argc
== 3 && !strcmp(argv
[1], "get_value")) {
101 if (!git_config_get_value(argv
[2], &v
)) {
108 printf("Value not found for \"%s\"\n", argv
[2]);
111 } else if (argc
== 3 && !strcmp(argv
[1], "get_value_multi")) {
112 strptr
= git_config_get_value_multi(argv
[2]);
114 for (i
= 0; i
< strptr
->nr
; i
++) {
115 v
= strptr
->items
[i
].string
;
123 printf("Value not found for \"%s\"\n", argv
[2]);
126 } else if (argc
== 3 && !strcmp(argv
[1], "get_int")) {
127 if (!git_config_get_int(argv
[2], &val
)) {
131 printf("Value not found for \"%s\"\n", argv
[2]);
134 } else if (argc
== 3 && !strcmp(argv
[1], "get_bool")) {
135 if (!git_config_get_bool(argv
[2], &val
)) {
139 printf("Value not found for \"%s\"\n", argv
[2]);
142 } else if (argc
== 3 && !strcmp(argv
[1], "get_string")) {
143 if (!git_config_get_string_const(argv
[2], &v
)) {
147 printf("Value not found for \"%s\"\n", argv
[2]);
150 } else if (!strcmp(argv
[1], "configset_get_value")) {
151 for (i
= 3; i
< argc
; i
++) {
153 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
154 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
158 if (!git_configset_get_value(&cs
, argv
[2], &v
)) {
165 printf("Value not found for \"%s\"\n", argv
[2]);
168 } else if (!strcmp(argv
[1], "configset_get_value_multi")) {
169 for (i
= 3; i
< argc
; i
++) {
171 if ((err
= git_configset_add_file(&cs
, argv
[i
]))) {
172 fprintf(stderr
, "Error (%d) reading configuration file %s.\n", err
, argv
[i
]);
176 strptr
= git_configset_get_value_multi(&cs
, argv
[2]);
178 for (i
= 0; i
< strptr
->nr
; i
++) {
179 v
= strptr
->items
[i
].string
;
187 printf("Value not found for \"%s\"\n", argv
[2]);
190 } else if (!strcmp(argv
[1], "iterate")) {
191 git_config(iterate_cb
, NULL
);
195 die("%s: Please check the syntax and the function name", argv
[0]);
198 git_configset_clear(&cs
);
202 git_configset_clear(&cs
);
206 git_configset_clear(&cs
);