git config: reorganize get_color*
[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[] =
3bec8ff9 6"git config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int | --bool-or-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] | --edit | -e ]";
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';
c35b0b58 19static enum { T_RAW, T_INT, T_BOOL, T_BOOL_OR_INT } type = T_RAW;
4ddba79d 20
ef90d6d4 21static int show_all_config(const char *key_, const char *value_, void *cb)
de791f15
PB
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
4b951b7e 30static int show_config(const char *key_, const char *value_, void *cb)
4ddba79d 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";
c35b0b58
JH
56 else if (type == T_BOOL_OR_INT) {
57 int is_bool, v;
58 v = git_config_bool_or_int(key_, value_, &is_bool);
59 if (is_bool)
60 vptr = v ? "true" : "false";
61 else
62 sprintf(value, "%d", v);
63 }
8f5ff31f 64 else
acb70149 65 vptr = value_?value_:"";
8f5ff31f
JS
66 seen++;
67 if (dup_error) {
68 error("More than one value for the key %s: %s",
69 key_, vptr);
4ddba79d 70 }
8f5ff31f 71 else
2275d502 72 printf("%s%c", vptr, term);
8f5ff31f 73
4ddba79d
JS
74 return 0;
75}
76
4b951b7e 77static int get_value(const char *key_, const char *regex_)
4ddba79d 78{
5f1a63e0 79 int ret = -1;
d14f7764 80 char *tl;
5f1a63e0 81 char *global = NULL, *repo_config = NULL;
32043c9f 82 const char *system_wide = NULL, *local;
5f1a63e0 83
dc871831 84 local = config_exclusive_filename;
5f1a63e0
JS
85 if (!local) {
86 const char *home = getenv("HOME");
a4f34cbb 87 local = repo_config = git_pathdup("config");
ab88c363 88 if (git_config_global() && home)
9befac47 89 global = xstrdup(mkpath("%s/.gitconfig", home));
ab88c363
JK
90 if (git_config_system())
91 system_wide = git_etc_gitconfig();
5f1a63e0 92 }
4ddba79d 93
9befac47 94 key = xstrdup(key_);
d14f7764
LT
95 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
96 *tl = tolower(*tl);
97 for (tl=key; *tl && *tl != '.'; ++tl)
98 *tl = tolower(*tl);
4ddba79d 99
2fa9a0fb 100 if (use_key_regexp) {
2d7320d0 101 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 102 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 103 fprintf(stderr, "Invalid key pattern: %s\n", key_);
5f1a63e0 104 goto free_strings;
2fa9a0fb
JS
105 }
106 }
107
4ddba79d 108 if (regex_) {
f98d863d
JS
109 if (regex_[0] == '!') {
110 do_not_match = 1;
111 regex_++;
112 }
113
2d7320d0 114 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 115 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d 116 fprintf(stderr, "Invalid pattern: %s\n", regex_);
5f1a63e0 117 goto free_strings;
4ddba79d
JS
118 }
119 }
120
32043c9f 121 if (do_all && system_wide)
ef90d6d4 122 git_config_from_file(show_config, system_wide, NULL);
5f1a63e0 123 if (do_all && global)
ef90d6d4
JS
124 git_config_from_file(show_config, global, NULL);
125 git_config_from_file(show_config, local, NULL);
5f1a63e0 126 if (!do_all && !seen && global)
ef90d6d4 127 git_config_from_file(show_config, global, NULL);
32043c9f 128 if (!do_all && !seen && system_wide)
ef90d6d4 129 git_config_from_file(show_config, system_wide, NULL);
5f1a63e0 130
4ddba79d 131 free(key);
0a152171
AW
132 if (regexp) {
133 regfree(regexp);
134 free(regexp);
4ddba79d
JS
135 }
136
137 if (do_all)
5f1a63e0
JS
138 ret = !seen;
139 else
dc2613de 140 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
5f1a63e0
JS
141
142free_strings:
4cac42b1
JH
143 free(repo_config);
144 free(global);
5f1a63e0 145 return ret;
4ddba79d 146}
1b1e59c5 147
186458b1 148static char *normalize_value(const char *key, const char *value)
db1696b8
FL
149{
150 char *normalized;
151
152 if (!value)
153 return NULL;
154
155 if (type == T_RAW)
156 normalized = xstrdup(value);
157 else {
158 normalized = xmalloc(64);
159 if (type == T_INT) {
160 int v = git_config_int(key, value);
161 sprintf(normalized, "%d", v);
162 }
163 else if (type == T_BOOL)
164 sprintf(normalized, "%s",
165 git_config_bool(key, value) ? "true" : "false");
c35b0b58
JH
166 else if (type == T_BOOL_OR_INT) {
167 int is_bool, v;
168 v = git_config_bool_or_int(key, value, &is_bool);
169 if (!is_bool)
170 sprintf(normalized, "%d", v);
171 else
172 sprintf(normalized, "%s", v ? "true" : "false");
173 }
db1696b8
FL
174 }
175
176 return normalized;
177}
178
9ce03522
JH
179static int get_color_found;
180static const char *get_color_slot;
b408457f 181static const char *get_colorbool_slot;
9ce03522
JH
182static char parsed_color[COLOR_MAXLEN];
183
ef90d6d4 184static int git_get_color_config(const char *var, const char *value, void *cb)
9ce03522
JH
185{
186 if (!strcmp(var, get_color_slot)) {
f769982d
JH
187 if (!value)
188 config_error_nonbool(var);
9ce03522
JH
189 color_parse(value, var, parsed_color);
190 get_color_found = 1;
191 }
192 return 0;
193}
194
0e854a28 195static void get_color(const char *def_color)
9ce03522 196{
9ce03522
JH
197 get_color_found = 0;
198 parsed_color[0] = '\0';
ef90d6d4 199 git_config(git_get_color_config, NULL);
9ce03522
JH
200
201 if (!get_color_found && def_color)
202 color_parse(def_color, "command line", parsed_color);
203
204 fputs(parsed_color, stdout);
9ce03522
JH
205}
206
0f6f5a40
JH
207static int stdout_is_tty;
208static int get_colorbool_found;
69243c2b 209static int get_diff_color_found;
ef90d6d4
JS
210static int git_get_colorbool_config(const char *var, const char *value,
211 void *cb)
0f6f5a40 212{
b408457f 213 if (!strcmp(var, get_colorbool_slot)) {
0f6f5a40
JH
214 get_colorbool_found =
215 git_config_colorbool(var, value, stdout_is_tty);
69243c2b
JH
216 }
217 if (!strcmp(var, "diff.color")) {
218 get_diff_color_found =
219 git_config_colorbool(var, value, stdout_is_tty);
220 }
4d4f5ba3
MK
221 if (!strcmp(var, "color.ui")) {
222 git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
223 return 0;
224 }
0f6f5a40
JH
225 return 0;
226}
227
0e854a28 228static int get_colorbool(int print)
0f6f5a40 229{
69243c2b
JH
230 get_colorbool_found = -1;
231 get_diff_color_found = -1;
ef90d6d4 232 git_config(git_get_colorbool_config, NULL);
0f6f5a40 233
69243c2b 234 if (get_colorbool_found < 0) {
b408457f 235 if (!strcmp(get_colorbool_slot, "color.diff"))
69243c2b
JH
236 get_colorbool_found = get_diff_color_found;
237 if (get_colorbool_found < 0)
4d4f5ba3 238 get_colorbool_found = git_use_color_default;
69243c2b
JH
239 }
240
0e854a28 241 if (print) {
0f6f5a40
JH
242 printf("%s\n", get_colorbool_found ? "true" : "false");
243 return 0;
0e854a28
FC
244 } else
245 return get_colorbool_found ? 0 : 1;
0f6f5a40
JH
246}
247
b408457f 248int cmd_config(int argc, const char **argv, const char *unused_prefix)
1b1e59c5 249{
af05d679 250 int nongit;
4b951b7e 251 char *value;
b408457f 252 const char *prefix = setup_git_directory_gently(&nongit);
7162dff3 253
dc871831
DB
254 config_exclusive_filename = getenv(CONFIG_ENVIRONMENT);
255
7162dff3
PB
256 while (1 < argc) {
257 if (!strcmp(argv[1], "--int"))
258 type = T_INT;
259 else if (!strcmp(argv[1], "--bool"))
260 type = T_BOOL;
c35b0b58
JH
261 else if (!strcmp(argv[1], "--bool-or-int"))
262 type = T_BOOL_OR_INT;
a72c874e
FL
263 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
264 if (argc != 2)
265 usage(git_config_set_usage);
b408457f
FC
266 if (git_config(show_all_config, NULL) < 0) {
267 if (config_exclusive_filename)
268 die("unable to read config file %s: %s",
269 config_exclusive_filename, strerror(errno));
270 else
271 die("error processing config file(s)");
272 }
93a56c2c 273 return 0;
a72c874e 274 }
34eb3340
SE
275 else if (!strcmp(argv[1], "--global")) {
276 char *home = getenv("HOME");
277 if (home) {
278 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
dc871831 279 config_exclusive_filename = user_config;
34eb3340
SE
280 } else {
281 die("$HOME not set");
282 }
32043c9f
JS
283 }
284 else if (!strcmp(argv[1], "--system"))
dc871831 285 config_exclusive_filename = git_etc_gitconfig();
67d454fe
AR
286 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
287 if (argc < 3)
288 usage(git_config_set_usage);
b408457f
FC
289 if (!is_absolute_path(argv[2]) && prefix)
290 config_exclusive_filename = prefix_filename(prefix,
291 strlen(prefix),
292 argv[2]);
1ae14a6b 293 else
b408457f 294 config_exclusive_filename = argv[2];
67d454fe
AR
295 argc--;
296 argv++;
297 }
2275d502
FL
298 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
299 term = '\0';
300 delim = '\n';
301 key_delim = '\n';
302 }
32043c9f 303 else if (!strcmp(argv[1], "--rename-section")) {
0667fcfb
JS
304 int ret;
305 if (argc != 4)
306 usage(git_config_set_usage);
307 ret = git_config_rename_section(argv[2], argv[3]);
308 if (ret < 0)
309 return ret;
310 if (ret == 0) {
311 fprintf(stderr, "No such section!\n");
312 return 1;
313 }
314 return 0;
32043c9f 315 }
118f8b24
PB
316 else if (!strcmp(argv[1], "--remove-section")) {
317 int ret;
318 if (argc != 3)
319 usage(git_config_set_usage);
320 ret = git_config_rename_section(argv[2], NULL);
321 if (ret < 0)
322 return ret;
323 if (ret == 0) {
324 fprintf(stderr, "No such section!\n");
325 return 1;
326 }
327 return 0;
9ce03522 328 } else if (!strcmp(argv[1], "--get-color")) {
0e854a28
FC
329 if (argc > 4 || argc < 3)
330 usage(git_config_set_usage);
331 get_color_slot = argv[2];
332 get_color(argv[3]);
333 return 0;
0f6f5a40 334 } else if (!strcmp(argv[1], "--get-colorbool")) {
0e854a28
FC
335 if (argc == 4)
336 stdout_is_tty = git_config_bool("command line", argv[3]);
337 else if (argc == 3)
338 stdout_is_tty = isatty(1);
339 else
340 usage(git_config_set_usage);
341 get_colorbool_slot = argv[2];
342 return get_colorbool(argc != 3);
3bec8ff9 343 } else if (!strcmp(argv[1], "--edit") || !strcmp(argv[1], "-e")) {
3bec8ff9
FC
344 if (argc != 2)
345 usage(git_config_set_usage);
3bec8ff9 346 git_config(git_default_config, NULL);
414f2e53
JS
347 launch_editor(config_exclusive_filename ?
348 config_exclusive_filename : git_path("config"),
349 NULL, NULL);
3bec8ff9 350 return 0;
9ce03522 351 } else
7162dff3
PB
352 break;
353 argc--;
354 argv++;
355 }
356
1b1e59c5
JS
357 switch (argc) {
358 case 2:
4ddba79d 359 return get_value(argv[1], NULL);
1b1e59c5
JS
360 case 3:
361 if (!strcmp(argv[1], "--unset"))
362 return git_config_set(argv[2], NULL);
4ddba79d
JS
363 else if (!strcmp(argv[1], "--unset-all"))
364 return git_config_set_multivar(argv[2], NULL, NULL, 1);
365 else if (!strcmp(argv[1], "--get"))
366 return get_value(argv[2], NULL);
367 else if (!strcmp(argv[1], "--get-all")) {
368 do_all = 1;
369 return get_value(argv[2], NULL);
2fa9a0fb
JS
370 } else if (!strcmp(argv[1], "--get-regexp")) {
371 show_keys = 1;
372 use_key_regexp = 1;
373 do_all = 1;
374 return get_value(argv[2], NULL);
db1696b8
FL
375 } else {
376 value = normalize_value(argv[1], argv[2]);
377 return git_config_set(argv[1], value);
378 }
1b1e59c5
JS
379 case 4:
380 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
381 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
382 else if (!strcmp(argv[1], "--unset-all"))
383 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
384 else if (!strcmp(argv[1], "--get"))
385 return get_value(argv[2], argv[3]);
386 else if (!strcmp(argv[1], "--get-all")) {
387 do_all = 1;
388 return get_value(argv[2], argv[3]);
2fa9a0fb
JS
389 } else if (!strcmp(argv[1], "--get-regexp")) {
390 show_keys = 1;
391 use_key_regexp = 1;
392 do_all = 1;
393 return get_value(argv[2], argv[3]);
db1696b8
FL
394 } else if (!strcmp(argv[1], "--add")) {
395 value = normalize_value(argv[2], argv[3]);
396 return git_config_set_multivar(argv[2], value, "^$", 0);
397 } else if (!strcmp(argv[1], "--replace-all")) {
398 value = normalize_value(argv[2], argv[3]);
399 return git_config_set_multivar(argv[2], value, NULL, 1);
400 } else {
401 value = normalize_value(argv[1], argv[2]);
402 return git_config_set_multivar(argv[1], value, argv[3], 0);
403 }
4ddba79d 404 case 5:
db1696b8
FL
405 if (!strcmp(argv[1], "--replace-all")) {
406 value = normalize_value(argv[2], argv[3]);
407 return git_config_set_multivar(argv[2], value, argv[4], 1);
408 }
4ddba79d 409 case 1:
1b1e59c5
JS
410 default:
411 usage(git_config_set_usage);
412 }
413 return 0;
414}