git config --get-colorbool
[git/git.git] / builtin-config.c
CommitLineData
e12c095a 1#include "builtin.h"
1b1e59c5 2#include "cache.h"
9ce03522 3#include "color.h"
1b1e59c5
JS
4
5static const char git_config_set_usage[] =
0f6f5a40 6"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default] | --get-colorbool name [stdout-is-tty]";
4ddba79d 7
96f1e58f
DR
8static char *key;
9static regex_t *key_regexp;
10static regex_t *regexp;
11static int show_keys;
12static int use_key_regexp;
13static int do_all;
14static int do_not_match;
15static int seen;
2275d502
FL
16static char delim = '=';
17static char key_delim = ' ';
18static char term = '\n';
7162dff3 19static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
4ddba79d 20
de791f15
PB
21static int show_all_config(const char *key_, const char *value_)
22{
23 if (value_)
2275d502 24 printf("%s%c%s%c", key_, delim, value_, term);
de791f15 25 else
2275d502 26 printf("%s%c", key_, term);
de791f15
PB
27 return 0;
28}
29
4ddba79d
JS
30static int show_config(const char* key_, const char* value_)
31{
e098c6f8
JH
32 char value[256];
33 const char *vptr = value;
8f5ff31f 34 int dup_error = 0;
e098c6f8 35
8f5ff31f
JS
36 if (!use_key_regexp && strcmp(key_, key))
37 return 0;
38 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
39 return 0;
40 if (regexp != NULL &&
9e4bbeb9 41 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
8f5ff31f
JS
42 return 0;
43
b69ba460
FL
44 if (show_keys) {
45 if (value_)
2275d502 46 printf("%s%c", key_, key_delim);
b69ba460
FL
47 else
48 printf("%s", key_);
49 }
8f5ff31f
JS
50 if (seen && !do_all)
51 dup_error = 1;
52 if (type == T_INT)
acb70149 53 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
8f5ff31f
JS
54 else if (type == T_BOOL)
55 vptr = git_config_bool(key_, value_) ? "true" : "false";
56 else
acb70149 57 vptr = value_?value_:"";
8f5ff31f
JS
58 seen++;
59 if (dup_error) {
60 error("More than one value for the key %s: %s",
61 key_, vptr);
4ddba79d 62 }
8f5ff31f 63 else
2275d502 64 printf("%s%c", vptr, term);
8f5ff31f 65
4ddba79d
JS
66 return 0;
67}
68
69static int get_value(const char* key_, const char* regex_)
70{
5f1a63e0 71 int ret = -1;
d14f7764 72 char *tl;
5f1a63e0 73 char *global = NULL, *repo_config = NULL;
32043c9f 74 const char *system_wide = NULL, *local;
5f1a63e0 75
d4ebc36c 76 local = getenv(CONFIG_ENVIRONMENT);
5f1a63e0
JS
77 if (!local) {
78 const char *home = getenv("HOME");
d4ebc36c 79 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
5f1a63e0 80 if (!local)
9befac47 81 local = repo_config = xstrdup(git_path("config"));
5f1a63e0 82 if (home)
9befac47 83 global = xstrdup(mkpath("%s/.gitconfig", home));
506b17b1 84 system_wide = git_etc_gitconfig();
5f1a63e0 85 }
4ddba79d 86
9befac47 87 key = xstrdup(key_);
d14f7764
LT
88 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
89 *tl = tolower(*tl);
90 for (tl=key; *tl && *tl != '.'; ++tl)
91 *tl = tolower(*tl);
4ddba79d 92
2fa9a0fb 93 if (use_key_regexp) {
2d7320d0 94 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 95 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 96 fprintf(stderr, "Invalid key pattern: %s\n", key_);
5f1a63e0 97 goto free_strings;
2fa9a0fb
JS
98 }
99 }
100
4ddba79d 101 if (regex_) {
f98d863d
JS
102 if (regex_[0] == '!') {
103 do_not_match = 1;
104 regex_++;
105 }
106
2d7320d0 107 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 108 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d 109 fprintf(stderr, "Invalid pattern: %s\n", regex_);
5f1a63e0 110 goto free_strings;
4ddba79d
JS
111 }
112 }
113
32043c9f
JS
114 if (do_all && system_wide)
115 git_config_from_file(show_config, system_wide);
5f1a63e0
JS
116 if (do_all && global)
117 git_config_from_file(show_config, global);
118 git_config_from_file(show_config, local);
119 if (!do_all && !seen && global)
120 git_config_from_file(show_config, global);
32043c9f
JS
121 if (!do_all && !seen && system_wide)
122 git_config_from_file(show_config, system_wide);
5f1a63e0 123
4ddba79d 124 free(key);
0a152171
AW
125 if (regexp) {
126 regfree(regexp);
127 free(regexp);
4ddba79d
JS
128 }
129
130 if (do_all)
5f1a63e0
JS
131 ret = !seen;
132 else
dc2613de 133 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
5f1a63e0
JS
134
135free_strings:
4cac42b1
JH
136 free(repo_config);
137 free(global);
5f1a63e0 138 return ret;
4ddba79d 139}
1b1e59c5 140
db1696b8
FL
141char *normalize_value(const char *key, const char *value)
142{
143 char *normalized;
144
145 if (!value)
146 return NULL;
147
148 if (type == T_RAW)
149 normalized = xstrdup(value);
150 else {
151 normalized = xmalloc(64);
152 if (type == T_INT) {
153 int v = git_config_int(key, value);
154 sprintf(normalized, "%d", v);
155 }
156 else if (type == T_BOOL)
157 sprintf(normalized, "%s",
158 git_config_bool(key, value) ? "true" : "false");
159 }
160
161 return normalized;
162}
163
9ce03522
JH
164static int get_color_found;
165static const char *get_color_slot;
166static char parsed_color[COLOR_MAXLEN];
167
168static int git_get_color_config(const char *var, const char *value)
169{
170 if (!strcmp(var, get_color_slot)) {
171 color_parse(value, var, parsed_color);
172 get_color_found = 1;
173 }
174 return 0;
175}
176
177static int get_color(int argc, const char **argv)
178{
179 /*
180 * grab the color setting for the given slot from the configuration,
181 * or parse the default value if missing, and return ANSI color
182 * escape sequence.
183 *
184 * e.g.
185 * git config --get-color color.diff.whitespace "blue reverse"
186 */
187 const char *def_color = NULL;
188
189 switch (argc) {
190 default:
191 usage(git_config_set_usage);
192 case 2:
193 def_color = argv[1];
194 /* fallthru */
195 case 1:
196 get_color_slot = argv[0];
197 break;
198 }
199
200 get_color_found = 0;
201 parsed_color[0] = '\0';
202 git_config(git_get_color_config);
203
204 if (!get_color_found && def_color)
205 color_parse(def_color, "command line", parsed_color);
206
207 fputs(parsed_color, stdout);
208 return 0;
209}
210
0f6f5a40
JH
211static int stdout_is_tty;
212static int get_colorbool_found;
213static int git_get_colorbool_config(const char *var, const char *value)
214{
215 if (!strcmp(var, get_color_slot))
216 get_colorbool_found =
217 git_config_colorbool(var, value, stdout_is_tty);
218 return 0;
219}
220
221static int get_colorbool(int argc, const char **argv)
222{
223 /*
224 * git config --get-colorbool <slot> [<stdout-is-tty>]
225 *
226 * returns "true" or "false" depending on how <slot>
227 * is configured.
228 */
229
230 if (argc == 2)
231 stdout_is_tty = git_config_bool("command line", argv[1]);
232 else if (argc == 1)
233 stdout_is_tty = isatty(1);
234 else
235 usage(git_config_set_usage);
236 get_colorbool_found = 0;
237 get_color_slot = argv[0];
238 git_config(git_get_colorbool_config);
239
240 if (argc == 1) {
241 return get_colorbool_found ? 0 : 1;
242 } else {
243 printf("%s\n", get_colorbool_found ? "true" : "false");
244 return 0;
245 }
246}
247
e0d10e1c 248int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 249{
4d599e6b 250 int nongit = 0;
db1696b8 251 char* value;
1ae14a6b 252 const char *file = setup_git_directory_gently(&nongit);
7162dff3
PB
253
254 while (1 < argc) {
255 if (!strcmp(argv[1], "--int"))
256 type = T_INT;
257 else if (!strcmp(argv[1], "--bool"))
258 type = T_BOOL;
a72c874e
FL
259 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
260 if (argc != 2)
261 usage(git_config_set_usage);
93a56c2c
GP
262 if (git_config(show_all_config) < 0 && file && errno)
263 die("unable to read config file %s: %s", file,
264 strerror(errno));
265 return 0;
a72c874e 266 }
34eb3340
SE
267 else if (!strcmp(argv[1], "--global")) {
268 char *home = getenv("HOME");
269 if (home) {
270 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
7627943a 271 setenv(CONFIG_ENVIRONMENT, user_config, 1);
34eb3340
SE
272 free(user_config);
273 } else {
274 die("$HOME not set");
275 }
32043c9f
JS
276 }
277 else if (!strcmp(argv[1], "--system"))
506b17b1 278 setenv(CONFIG_ENVIRONMENT, git_etc_gitconfig(), 1);
67d454fe
AR
279 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
280 if (argc < 3)
281 usage(git_config_set_usage);
1ae14a6b
GP
282 if (!is_absolute_path(argv[2]) && file)
283 file = prefix_filename(file, strlen(file),
284 argv[2]);
285 else
286 file = argv[2];
287 setenv(CONFIG_ENVIRONMENT, file, 1);
67d454fe
AR
288 argc--;
289 argv++;
290 }
2275d502
FL
291 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
292 term = '\0';
293 delim = '\n';
294 key_delim = '\n';
295 }
32043c9f 296 else if (!strcmp(argv[1], "--rename-section")) {
0667fcfb
JS
297 int ret;
298 if (argc != 4)
299 usage(git_config_set_usage);
300 ret = git_config_rename_section(argv[2], argv[3]);
301 if (ret < 0)
302 return ret;
303 if (ret == 0) {
304 fprintf(stderr, "No such section!\n");
305 return 1;
306 }
307 return 0;
32043c9f 308 }
118f8b24
PB
309 else if (!strcmp(argv[1], "--remove-section")) {
310 int ret;
311 if (argc != 3)
312 usage(git_config_set_usage);
313 ret = git_config_rename_section(argv[2], NULL);
314 if (ret < 0)
315 return ret;
316 if (ret == 0) {
317 fprintf(stderr, "No such section!\n");
318 return 1;
319 }
320 return 0;
9ce03522
JH
321 } else if (!strcmp(argv[1], "--get-color")) {
322 return get_color(argc-2, argv+2);
0f6f5a40
JH
323 } else if (!strcmp(argv[1], "--get-colorbool")) {
324 return get_colorbool(argc-2, argv+2);
9ce03522 325 } else
7162dff3
PB
326 break;
327 argc--;
328 argv++;
329 }
330
1b1e59c5
JS
331 switch (argc) {
332 case 2:
4ddba79d 333 return get_value(argv[1], NULL);
1b1e59c5
JS
334 case 3:
335 if (!strcmp(argv[1], "--unset"))
336 return git_config_set(argv[2], NULL);
4ddba79d
JS
337 else if (!strcmp(argv[1], "--unset-all"))
338 return git_config_set_multivar(argv[2], NULL, NULL, 1);
339 else if (!strcmp(argv[1], "--get"))
340 return get_value(argv[2], NULL);
341 else if (!strcmp(argv[1], "--get-all")) {
342 do_all = 1;
343 return get_value(argv[2], NULL);
2fa9a0fb
JS
344 } else if (!strcmp(argv[1], "--get-regexp")) {
345 show_keys = 1;
346 use_key_regexp = 1;
347 do_all = 1;
348 return get_value(argv[2], NULL);
db1696b8
FL
349 } else {
350 value = normalize_value(argv[1], argv[2]);
351 return git_config_set(argv[1], value);
352 }
1b1e59c5
JS
353 case 4:
354 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
355 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
356 else if (!strcmp(argv[1], "--unset-all"))
357 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
358 else if (!strcmp(argv[1], "--get"))
359 return get_value(argv[2], argv[3]);
360 else if (!strcmp(argv[1], "--get-all")) {
361 do_all = 1;
362 return get_value(argv[2], argv[3]);
2fa9a0fb
JS
363 } else if (!strcmp(argv[1], "--get-regexp")) {
364 show_keys = 1;
365 use_key_regexp = 1;
366 do_all = 1;
367 return get_value(argv[2], argv[3]);
db1696b8
FL
368 } else if (!strcmp(argv[1], "--add")) {
369 value = normalize_value(argv[2], argv[3]);
370 return git_config_set_multivar(argv[2], value, "^$", 0);
371 } else if (!strcmp(argv[1], "--replace-all")) {
372 value = normalize_value(argv[2], argv[3]);
373 return git_config_set_multivar(argv[2], value, NULL, 1);
374 } else {
375 value = normalize_value(argv[1], argv[2]);
376 return git_config_set_multivar(argv[1], value, argv[3], 0);
377 }
4ddba79d 378 case 5:
db1696b8
FL
379 if (!strcmp(argv[1], "--replace-all")) {
380 value = normalize_value(argv[2], argv[3]);
381 return git_config_set_multivar(argv[2], value, argv[4], 1);
382 }
4ddba79d 383 case 1:
1b1e59c5
JS
384 default:
385 usage(git_config_set_usage);
386 }
387 return 0;
388}