config: fix parsing of "git config --get-color some.key -1"
[git.git] / builtin / config.c
blob84b8e1cfa5515c8a0c1dc5c22c5c1bb708847532
1 #include "builtin.h"
2 #include "cache.h"
3 #include "color.h"
4 #include "parse-options.h"
5 #include "urlmatch.h"
7 static const char *const builtin_config_usage[] = {
8 N_("git config [options]"),
9 NULL
12 static char *key;
13 static regex_t *key_regexp;
14 static regex_t *regexp;
15 static int show_keys;
16 static int use_key_regexp;
17 static int do_all;
18 static int do_not_match;
19 static char delim = '=';
20 static char key_delim = ' ';
21 static char term = '\n';
23 static int use_global_config, use_system_config, use_local_config;
24 static struct git_config_source given_config_source;
25 static int actions, types;
26 static const char *get_color_slot, *get_colorbool_slot;
27 static int end_null;
28 static int respect_includes = -1;
30 #define ACTION_GET (1<<0)
31 #define ACTION_GET_ALL (1<<1)
32 #define ACTION_GET_REGEXP (1<<2)
33 #define ACTION_REPLACE_ALL (1<<3)
34 #define ACTION_ADD (1<<4)
35 #define ACTION_UNSET (1<<5)
36 #define ACTION_UNSET_ALL (1<<6)
37 #define ACTION_RENAME_SECTION (1<<7)
38 #define ACTION_REMOVE_SECTION (1<<8)
39 #define ACTION_LIST (1<<9)
40 #define ACTION_EDIT (1<<10)
41 #define ACTION_SET (1<<11)
42 #define ACTION_SET_ALL (1<<12)
43 #define ACTION_GET_COLOR (1<<13)
44 #define ACTION_GET_COLORBOOL (1<<14)
45 #define ACTION_GET_URLMATCH (1<<15)
47 #define TYPE_BOOL (1<<0)
48 #define TYPE_INT (1<<1)
49 #define TYPE_BOOL_OR_INT (1<<2)
50 #define TYPE_PATH (1<<3)
52 static struct option builtin_config_options[] = {
53 OPT_GROUP(N_("Config file location")),
54 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
55 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
56 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
57 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
58 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
59 OPT_GROUP(N_("Action")),
60 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
61 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
62 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
63 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
64 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
65 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
66 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
67 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
68 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
69 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
70 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
71 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
72 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
73 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
74 OPT_GROUP(N_("Type")),
75 OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
76 OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
77 OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
78 OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
79 OPT_GROUP(N_("Other")),
80 OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
81 OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
82 OPT_END(),
85 static void check_argc(int argc, int min, int max) {
86 if (argc >= min && argc <= max)
87 return;
88 error("wrong number of arguments");
89 usage_with_options(builtin_config_usage, builtin_config_options);
92 static int show_all_config(const char *key_, const char *value_, void *cb)
94 if (value_)
95 printf("%s%c%s%c", key_, delim, value_, term);
96 else
97 printf("%s%c", key_, term);
98 return 0;
101 struct strbuf_list {
102 struct strbuf *items;
103 int nr;
104 int alloc;
107 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
109 int must_free_vptr = 0;
110 int must_print_delim = 0;
111 char value[256];
112 const char *vptr = value;
114 strbuf_init(buf, 0);
116 if (show_keys) {
117 strbuf_addstr(buf, key_);
118 must_print_delim = 1;
120 if (types == TYPE_INT)
121 sprintf(value, "%"PRId64,
122 git_config_int64(key_, value_ ? value_ : ""));
123 else if (types == TYPE_BOOL)
124 vptr = git_config_bool(key_, value_) ? "true" : "false";
125 else if (types == TYPE_BOOL_OR_INT) {
126 int is_bool, v;
127 v = git_config_bool_or_int(key_, value_, &is_bool);
128 if (is_bool)
129 vptr = v ? "true" : "false";
130 else
131 sprintf(value, "%d", v);
132 } else if (types == TYPE_PATH) {
133 if (git_config_pathname(&vptr, key_, value_) < 0)
134 return -1;
135 must_free_vptr = 1;
136 } else if (value_) {
137 vptr = value_;
138 } else {
139 /* Just show the key name */
140 vptr = "";
141 must_print_delim = 0;
144 if (must_print_delim)
145 strbuf_addch(buf, key_delim);
146 strbuf_addstr(buf, vptr);
147 strbuf_addch(buf, term);
149 if (must_free_vptr)
150 free((char *)vptr);
151 return 0;
154 static int collect_config(const char *key_, const char *value_, void *cb)
156 struct strbuf_list *values = cb;
158 if (!use_key_regexp && strcmp(key_, key))
159 return 0;
160 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
161 return 0;
162 if (regexp != NULL &&
163 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
164 return 0;
166 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
168 return format_config(&values->items[values->nr++], key_, value_);
171 static int get_value(const char *key_, const char *regex_)
173 int ret = CONFIG_GENERIC_ERROR;
174 struct strbuf_list values = {NULL};
175 int i;
177 if (use_key_regexp) {
178 char *tl;
181 * NEEDSWORK: this naive pattern lowercasing obviously does not
182 * work for more complex patterns like "^[^.]*Foo.*bar".
183 * Perhaps we should deprecate this altogether someday.
186 key = xstrdup(key_);
187 for (tl = key + strlen(key) - 1;
188 tl >= key && *tl != '.';
189 tl--)
190 *tl = tolower(*tl);
191 for (tl = key; *tl && *tl != '.'; tl++)
192 *tl = tolower(*tl);
194 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
195 if (regcomp(key_regexp, key, REG_EXTENDED)) {
196 fprintf(stderr, "Invalid key pattern: %s\n", key_);
197 free(key_regexp);
198 key_regexp = NULL;
199 ret = CONFIG_INVALID_PATTERN;
200 goto free_strings;
202 } else {
203 if (git_config_parse_key(key_, &key, NULL)) {
204 ret = CONFIG_INVALID_KEY;
205 goto free_strings;
209 if (regex_) {
210 if (regex_[0] == '!') {
211 do_not_match = 1;
212 regex_++;
215 regexp = (regex_t*)xmalloc(sizeof(regex_t));
216 if (regcomp(regexp, regex_, REG_EXTENDED)) {
217 fprintf(stderr, "Invalid pattern: %s\n", regex_);
218 free(regexp);
219 regexp = NULL;
220 ret = CONFIG_INVALID_PATTERN;
221 goto free_strings;
225 git_config_with_options(collect_config, &values,
226 &given_config_source, respect_includes);
228 ret = !values.nr;
230 for (i = 0; i < values.nr; i++) {
231 struct strbuf *buf = values.items + i;
232 if (do_all || i == values.nr - 1)
233 fwrite(buf->buf, 1, buf->len, stdout);
234 strbuf_release(buf);
236 free(values.items);
238 free_strings:
239 free(key);
240 if (key_regexp) {
241 regfree(key_regexp);
242 free(key_regexp);
244 if (regexp) {
245 regfree(regexp);
246 free(regexp);
249 return ret;
252 static char *normalize_value(const char *key, const char *value)
254 char *normalized;
256 if (!value)
257 return NULL;
259 if (types == 0 || types == TYPE_PATH)
261 * We don't do normalization for TYPE_PATH here: If
262 * the path is like ~/foobar/, we prefer to store
263 * "~/foobar/" in the config file, and to expand the ~
264 * when retrieving the value.
266 normalized = xstrdup(value);
267 else {
268 normalized = xmalloc(64);
269 if (types == TYPE_INT) {
270 int64_t v = git_config_int64(key, value);
271 sprintf(normalized, "%"PRId64, v);
273 else if (types == TYPE_BOOL)
274 sprintf(normalized, "%s",
275 git_config_bool(key, value) ? "true" : "false");
276 else if (types == TYPE_BOOL_OR_INT) {
277 int is_bool, v;
278 v = git_config_bool_or_int(key, value, &is_bool);
279 if (!is_bool)
280 sprintf(normalized, "%d", v);
281 else
282 sprintf(normalized, "%s", v ? "true" : "false");
286 return normalized;
289 static int get_color_found;
290 static const char *get_color_slot;
291 static const char *get_colorbool_slot;
292 static char parsed_color[COLOR_MAXLEN];
294 static int git_get_color_config(const char *var, const char *value, void *cb)
296 if (!strcmp(var, get_color_slot)) {
297 if (!value)
298 config_error_nonbool(var);
299 color_parse(value, var, parsed_color);
300 get_color_found = 1;
302 return 0;
305 static void get_color(const char *var, const char *def_color)
307 get_color_slot = var;
308 get_color_found = 0;
309 parsed_color[0] = '\0';
310 git_config_with_options(git_get_color_config, NULL,
311 &given_config_source, respect_includes);
313 if (!get_color_found && def_color)
314 color_parse(def_color, "command line", parsed_color);
316 fputs(parsed_color, stdout);
319 static int get_colorbool_found;
320 static int get_diff_color_found;
321 static int get_color_ui_found;
322 static int git_get_colorbool_config(const char *var, const char *value,
323 void *cb)
325 if (!strcmp(var, get_colorbool_slot))
326 get_colorbool_found = git_config_colorbool(var, value);
327 else if (!strcmp(var, "diff.color"))
328 get_diff_color_found = git_config_colorbool(var, value);
329 else if (!strcmp(var, "color.ui"))
330 get_color_ui_found = git_config_colorbool(var, value);
331 return 0;
334 static int get_colorbool(const char *var, int print)
336 get_colorbool_slot = var;
337 get_colorbool_found = -1;
338 get_diff_color_found = -1;
339 get_color_ui_found = -1;
340 git_config_with_options(git_get_colorbool_config, NULL,
341 &given_config_source, respect_includes);
343 if (get_colorbool_found < 0) {
344 if (!strcmp(get_colorbool_slot, "color.diff"))
345 get_colorbool_found = get_diff_color_found;
346 if (get_colorbool_found < 0)
347 get_colorbool_found = get_color_ui_found;
350 if (get_colorbool_found < 0)
351 /* default value if none found in config */
352 get_colorbool_found = GIT_COLOR_AUTO;
354 get_colorbool_found = want_color(get_colorbool_found);
356 if (print) {
357 printf("%s\n", get_colorbool_found ? "true" : "false");
358 return 0;
359 } else
360 return get_colorbool_found ? 0 : 1;
363 static void check_write(void)
365 if (given_config_source.use_stdin)
366 die("writing to stdin is not supported");
368 if (given_config_source.blob)
369 die("writing config blobs is not supported");
372 struct urlmatch_current_candidate_value {
373 char value_is_null;
374 struct strbuf value;
377 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
379 struct string_list *values = cb;
380 struct string_list_item *item = string_list_insert(values, var);
381 struct urlmatch_current_candidate_value *matched = item->util;
383 if (!matched) {
384 matched = xmalloc(sizeof(*matched));
385 strbuf_init(&matched->value, 0);
386 item->util = matched;
387 } else {
388 strbuf_reset(&matched->value);
391 if (value) {
392 strbuf_addstr(&matched->value, value);
393 matched->value_is_null = 0;
394 } else {
395 matched->value_is_null = 1;
397 return 0;
400 static int get_urlmatch(const char *var, const char *url)
402 char *section_tail;
403 struct string_list_item *item;
404 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
405 struct string_list values = STRING_LIST_INIT_DUP;
407 config.collect_fn = urlmatch_collect_fn;
408 config.cascade_fn = NULL;
409 config.cb = &values;
411 if (!url_normalize(url, &config.url))
412 die("%s", config.url.err);
414 config.section = xstrdup_tolower(var);
415 section_tail = strchr(config.section, '.');
416 if (section_tail) {
417 *section_tail = '\0';
418 config.key = section_tail + 1;
419 show_keys = 0;
420 } else {
421 config.key = NULL;
422 show_keys = 1;
425 git_config_with_options(urlmatch_config_entry, &config,
426 &given_config_source, respect_includes);
428 for_each_string_list_item(item, &values) {
429 struct urlmatch_current_candidate_value *matched = item->util;
430 struct strbuf key = STRBUF_INIT;
431 struct strbuf buf = STRBUF_INIT;
433 strbuf_addstr(&key, item->string);
434 format_config(&buf, key.buf,
435 matched->value_is_null ? NULL : matched->value.buf);
436 fwrite(buf.buf, 1, buf.len, stdout);
437 strbuf_release(&key);
438 strbuf_release(&buf);
440 strbuf_release(&matched->value);
442 string_list_clear(&config.vars, 1);
443 string_list_clear(&values, 1);
444 free(config.url.url);
446 free((void *)config.section);
447 return 0;
450 int cmd_config(int argc, const char **argv, const char *prefix)
452 int nongit = !startup_info->have_repository;
453 char *value;
455 given_config_source.file = getenv(CONFIG_ENVIRONMENT);
457 argc = parse_options(argc, argv, prefix, builtin_config_options,
458 builtin_config_usage,
459 PARSE_OPT_STOP_AT_NON_OPTION);
461 if (use_global_config + use_system_config + use_local_config +
462 !!given_config_source.file + !!given_config_source.blob > 1) {
463 error("only one config file at a time.");
464 usage_with_options(builtin_config_usage, builtin_config_options);
467 if (given_config_source.file &&
468 !strcmp(given_config_source.file, "-")) {
469 given_config_source.file = NULL;
470 given_config_source.use_stdin = 1;
473 if (use_global_config) {
474 char *user_config = NULL;
475 char *xdg_config = NULL;
477 home_config_paths(&user_config, &xdg_config, "config");
479 if (!user_config)
481 * It is unknown if HOME/.gitconfig exists, so
482 * we do not know if we should write to XDG
483 * location; error out even if XDG_CONFIG_HOME
484 * is set and points at a sane location.
486 die("$HOME not set");
488 if (access_or_warn(user_config, R_OK, 0) &&
489 xdg_config && !access_or_warn(xdg_config, R_OK, 0))
490 given_config_source.file = xdg_config;
491 else
492 given_config_source.file = user_config;
494 else if (use_system_config)
495 given_config_source.file = git_etc_gitconfig();
496 else if (use_local_config)
497 given_config_source.file = git_pathdup("config");
498 else if (given_config_source.file) {
499 if (!is_absolute_path(given_config_source.file) && prefix)
500 given_config_source.file =
501 xstrdup(prefix_filename(prefix,
502 strlen(prefix),
503 given_config_source.file));
506 if (respect_includes == -1)
507 respect_includes = !given_config_source.file;
509 if (end_null) {
510 term = '\0';
511 delim = '\n';
512 key_delim = '\n';
515 if (HAS_MULTI_BITS(types)) {
516 error("only one type at a time.");
517 usage_with_options(builtin_config_usage, builtin_config_options);
520 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
521 error("--get-color and variable type are incoherent");
522 usage_with_options(builtin_config_usage, builtin_config_options);
525 if (HAS_MULTI_BITS(actions)) {
526 error("only one action at a time.");
527 usage_with_options(builtin_config_usage, builtin_config_options);
529 if (actions == 0)
530 switch (argc) {
531 case 1: actions = ACTION_GET; break;
532 case 2: actions = ACTION_SET; break;
533 case 3: actions = ACTION_SET_ALL; break;
534 default:
535 usage_with_options(builtin_config_usage, builtin_config_options);
538 if (actions == ACTION_LIST) {
539 check_argc(argc, 0, 0);
540 if (git_config_with_options(show_all_config, NULL,
541 &given_config_source,
542 respect_includes) < 0) {
543 if (given_config_source.file)
544 die_errno("unable to read config file '%s'",
545 given_config_source.file);
546 else
547 die("error processing config file(s)");
550 else if (actions == ACTION_EDIT) {
551 check_argc(argc, 0, 0);
552 if (!given_config_source.file && nongit)
553 die("not in a git directory");
554 if (given_config_source.use_stdin)
555 die("editing stdin is not supported");
556 if (given_config_source.blob)
557 die("editing blobs is not supported");
558 git_config(git_default_config, NULL);
559 launch_editor(given_config_source.file ?
560 given_config_source.file : git_path("config"),
561 NULL, NULL);
563 else if (actions == ACTION_SET) {
564 int ret;
565 check_write();
566 check_argc(argc, 2, 2);
567 value = normalize_value(argv[0], argv[1]);
568 ret = git_config_set_in_file(given_config_source.file, argv[0], value);
569 if (ret == CONFIG_NOTHING_SET)
570 error("cannot overwrite multiple values with a single value\n"
571 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
572 return ret;
574 else if (actions == ACTION_SET_ALL) {
575 check_write();
576 check_argc(argc, 2, 3);
577 value = normalize_value(argv[0], argv[1]);
578 return git_config_set_multivar_in_file(given_config_source.file,
579 argv[0], value, argv[2], 0);
581 else if (actions == ACTION_ADD) {
582 check_write();
583 check_argc(argc, 2, 2);
584 value = normalize_value(argv[0], argv[1]);
585 return git_config_set_multivar_in_file(given_config_source.file,
586 argv[0], value,
587 CONFIG_REGEX_NONE, 0);
589 else if (actions == ACTION_REPLACE_ALL) {
590 check_write();
591 check_argc(argc, 2, 3);
592 value = normalize_value(argv[0], argv[1]);
593 return git_config_set_multivar_in_file(given_config_source.file,
594 argv[0], value, argv[2], 1);
596 else if (actions == ACTION_GET) {
597 check_argc(argc, 1, 2);
598 return get_value(argv[0], argv[1]);
600 else if (actions == ACTION_GET_ALL) {
601 do_all = 1;
602 check_argc(argc, 1, 2);
603 return get_value(argv[0], argv[1]);
605 else if (actions == ACTION_GET_REGEXP) {
606 show_keys = 1;
607 use_key_regexp = 1;
608 do_all = 1;
609 check_argc(argc, 1, 2);
610 return get_value(argv[0], argv[1]);
612 else if (actions == ACTION_GET_URLMATCH) {
613 check_argc(argc, 2, 2);
614 return get_urlmatch(argv[0], argv[1]);
616 else if (actions == ACTION_UNSET) {
617 check_write();
618 check_argc(argc, 1, 2);
619 if (argc == 2)
620 return git_config_set_multivar_in_file(given_config_source.file,
621 argv[0], NULL, argv[1], 0);
622 else
623 return git_config_set_in_file(given_config_source.file,
624 argv[0], NULL);
626 else if (actions == ACTION_UNSET_ALL) {
627 check_write();
628 check_argc(argc, 1, 2);
629 return git_config_set_multivar_in_file(given_config_source.file,
630 argv[0], NULL, argv[1], 1);
632 else if (actions == ACTION_RENAME_SECTION) {
633 int ret;
634 check_write();
635 check_argc(argc, 2, 2);
636 ret = git_config_rename_section_in_file(given_config_source.file,
637 argv[0], argv[1]);
638 if (ret < 0)
639 return ret;
640 if (ret == 0)
641 die("No such section!");
643 else if (actions == ACTION_REMOVE_SECTION) {
644 int ret;
645 check_write();
646 check_argc(argc, 1, 1);
647 ret = git_config_rename_section_in_file(given_config_source.file,
648 argv[0], NULL);
649 if (ret < 0)
650 return ret;
651 if (ret == 0)
652 die("No such section!");
654 else if (actions == ACTION_GET_COLOR) {
655 check_argc(argc, 1, 2);
656 get_color(argv[0], argv[1]);
658 else if (actions == ACTION_GET_COLORBOOL) {
659 check_argc(argc, 1, 2);
660 if (argc == 2)
661 color_stdout_is_tty = git_config_bool("command line", argv[1]);
662 return get_colorbool(argv[0], argc == 2);
665 return 0;