relnotes: redo the description of text=auto fix
[git/debian.git] / t / helper / test-config.c
blob3c6d08cd095152cab9abeb038c1b4f82440c55cd
1 #include "cache.h"
2 #include "string-list.h"
4 /*
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
13 * of priority
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
29 * data for each
31 * Examples:
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 const char *scope_name(enum config_scope scope)
40 switch (scope) {
41 case CONFIG_SCOPE_SYSTEM:
42 return "system";
43 case CONFIG_SCOPE_GLOBAL:
44 return "global";
45 case CONFIG_SCOPE_REPO:
46 return "repo";
47 case CONFIG_SCOPE_CMDLINE:
48 return "cmdline";
49 default:
50 return "unknown";
53 static int iterate_cb(const char *var, const char *value, void *data)
55 static int nr;
57 if (nr++)
58 putchar('\n');
60 printf("key=%s\n", var);
61 printf("value=%s\n", value ? value : "(null)");
62 printf("origin=%s\n", current_config_origin_type());
63 printf("name=%s\n", current_config_name());
64 printf("scope=%s\n", scope_name(current_config_scope()));
66 return 0;
69 int cmd_main(int argc, const char **argv)
71 int i, val;
72 const char *v;
73 const struct string_list *strptr;
74 struct config_set cs;
75 git_configset_init(&cs);
77 if (argc < 2) {
78 fprintf(stderr, "Please, provide a command name on the command-line\n");
79 goto exit1;
80 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
81 if (!git_config_get_value(argv[2], &v)) {
82 if (!v)
83 printf("(NULL)\n");
84 else
85 printf("%s\n", v);
86 goto exit0;
87 } else {
88 printf("Value not found for \"%s\"\n", argv[2]);
89 goto exit1;
91 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
92 strptr = git_config_get_value_multi(argv[2]);
93 if (strptr) {
94 for (i = 0; i < strptr->nr; i++) {
95 v = strptr->items[i].string;
96 if (!v)
97 printf("(NULL)\n");
98 else
99 printf("%s\n", v);
101 goto exit0;
102 } else {
103 printf("Value not found for \"%s\"\n", argv[2]);
104 goto exit1;
106 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
107 if (!git_config_get_int(argv[2], &val)) {
108 printf("%d\n", val);
109 goto exit0;
110 } else {
111 printf("Value not found for \"%s\"\n", argv[2]);
112 goto exit1;
114 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
115 if (!git_config_get_bool(argv[2], &val)) {
116 printf("%d\n", val);
117 goto exit0;
118 } else {
119 printf("Value not found for \"%s\"\n", argv[2]);
120 goto exit1;
122 } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
123 if (!git_config_get_string_const(argv[2], &v)) {
124 printf("%s\n", v);
125 goto exit0;
126 } else {
127 printf("Value not found for \"%s\"\n", argv[2]);
128 goto exit1;
130 } else if (!strcmp(argv[1], "configset_get_value")) {
131 for (i = 3; i < argc; i++) {
132 int err;
133 if ((err = git_configset_add_file(&cs, argv[i]))) {
134 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
135 goto exit2;
138 if (!git_configset_get_value(&cs, argv[2], &v)) {
139 if (!v)
140 printf("(NULL)\n");
141 else
142 printf("%s\n", v);
143 goto exit0;
144 } else {
145 printf("Value not found for \"%s\"\n", argv[2]);
146 goto exit1;
148 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
149 for (i = 3; i < argc; i++) {
150 int err;
151 if ((err = git_configset_add_file(&cs, argv[i]))) {
152 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
153 goto exit2;
156 strptr = git_configset_get_value_multi(&cs, argv[2]);
157 if (strptr) {
158 for (i = 0; i < strptr->nr; i++) {
159 v = strptr->items[i].string;
160 if (!v)
161 printf("(NULL)\n");
162 else
163 printf("%s\n", v);
165 goto exit0;
166 } else {
167 printf("Value not found for \"%s\"\n", argv[2]);
168 goto exit1;
170 } else if (!strcmp(argv[1], "iterate")) {
171 git_config(iterate_cb, NULL);
172 goto exit0;
175 die("%s: Please check the syntax and the function name", argv[0]);
177 exit0:
178 git_configset_clear(&cs);
179 return 0;
181 exit1:
182 git_configset_clear(&cs);
183 return 1;
185 exit2:
186 git_configset_clear(&cs);
187 return 2;