contacts: fix to work in subdirectories
[git.git] / builtin / config.c
blobfc8d8820cbd2c0fcddb69aeb3fa258cf87e96814
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 const char *given_config_file;
25 static const char *given_config_blob;
26 static int actions, types;
27 static const char *get_color_slot, *get_colorbool_slot;
28 static int end_null;
29 static int respect_includes = -1;
31 #define ACTION_GET (1<<0)
32 #define ACTION_GET_ALL (1<<1)
33 #define ACTION_GET_REGEXP (1<<2)
34 #define ACTION_REPLACE_ALL (1<<3)
35 #define ACTION_ADD (1<<4)
36 #define ACTION_UNSET (1<<5)
37 #define ACTION_UNSET_ALL (1<<6)
38 #define ACTION_RENAME_SECTION (1<<7)
39 #define ACTION_REMOVE_SECTION (1<<8)
40 #define ACTION_LIST (1<<9)
41 #define ACTION_EDIT (1<<10)
42 #define ACTION_SET (1<<11)
43 #define ACTION_SET_ALL (1<<12)
44 #define ACTION_GET_COLOR (1<<13)
45 #define ACTION_GET_COLORBOOL (1<<14)
46 #define ACTION_GET_URLMATCH (1<<15)
48 #define TYPE_BOOL (1<<0)
49 #define TYPE_INT (1<<1)
50 #define TYPE_BOOL_OR_INT (1<<2)
51 #define TYPE_PATH (1<<3)
53 static struct option builtin_config_options[] = {
54 OPT_GROUP(N_("Config file location")),
55 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
56 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
57 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
58 OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
59 OPT_STRING(0, "blob", &given_config_blob, N_("blob-id"), N_("read config from given blob object")),
60 OPT_GROUP(N_("Action")),
61 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
62 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
63 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
64 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
65 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
66 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
67 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
68 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
69 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
70 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
71 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
72 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
73 OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
74 OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
75 OPT_GROUP(N_("Type")),
76 OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
77 OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
78 OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
79 OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
80 OPT_GROUP(N_("Other")),
81 OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
82 OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
83 OPT_END(),
86 static void check_argc(int argc, int min, int max) {
87 if (argc >= min && argc <= max)
88 return;
89 error("wrong number of arguments");
90 usage_with_options(builtin_config_usage, builtin_config_options);
93 static int show_all_config(const char *key_, const char *value_, void *cb)
95 if (value_)
96 printf("%s%c%s%c", key_, delim, value_, term);
97 else
98 printf("%s%c", key_, term);
99 return 0;
102 struct strbuf_list {
103 struct strbuf *items;
104 int nr;
105 int alloc;
108 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
110 int must_free_vptr = 0;
111 int must_print_delim = 0;
112 char value[256];
113 const char *vptr = value;
115 strbuf_init(buf, 0);
117 if (show_keys) {
118 strbuf_addstr(buf, key_);
119 must_print_delim = 1;
121 if (types == TYPE_INT)
122 sprintf(value, "%d", git_config_int(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_file, given_config_blob,
227 respect_includes);
229 ret = !values.nr;
231 for (i = 0; i < values.nr; i++) {
232 struct strbuf *buf = values.items + i;
233 if (do_all || i == values.nr - 1)
234 fwrite(buf->buf, 1, buf->len, stdout);
235 strbuf_release(buf);
237 free(values.items);
239 free_strings:
240 free(key);
241 if (key_regexp) {
242 regfree(key_regexp);
243 free(key_regexp);
245 if (regexp) {
246 regfree(regexp);
247 free(regexp);
250 return ret;
253 static char *normalize_value(const char *key, const char *value)
255 char *normalized;
257 if (!value)
258 return NULL;
260 if (types == 0 || types == TYPE_PATH)
262 * We don't do normalization for TYPE_PATH here: If
263 * the path is like ~/foobar/, we prefer to store
264 * "~/foobar/" in the config file, and to expand the ~
265 * when retrieving the value.
267 normalized = xstrdup(value);
268 else {
269 normalized = xmalloc(64);
270 if (types == TYPE_INT) {
271 int v = git_config_int(key, value);
272 sprintf(normalized, "%d", v);
274 else if (types == TYPE_BOOL)
275 sprintf(normalized, "%s",
276 git_config_bool(key, value) ? "true" : "false");
277 else if (types == TYPE_BOOL_OR_INT) {
278 int is_bool, v;
279 v = git_config_bool_or_int(key, value, &is_bool);
280 if (!is_bool)
281 sprintf(normalized, "%d", v);
282 else
283 sprintf(normalized, "%s", v ? "true" : "false");
287 return normalized;
290 static int get_color_found;
291 static const char *get_color_slot;
292 static const char *get_colorbool_slot;
293 static char parsed_color[COLOR_MAXLEN];
295 static int git_get_color_config(const char *var, const char *value, void *cb)
297 if (!strcmp(var, get_color_slot)) {
298 if (!value)
299 config_error_nonbool(var);
300 color_parse(value, var, parsed_color);
301 get_color_found = 1;
303 return 0;
306 static void get_color(const char *def_color)
308 get_color_found = 0;
309 parsed_color[0] = '\0';
310 git_config_with_options(git_get_color_config, NULL,
311 given_config_file, given_config_blob,
312 respect_includes);
314 if (!get_color_found && def_color)
315 color_parse(def_color, "command line", parsed_color);
317 fputs(parsed_color, stdout);
320 static int get_colorbool_found;
321 static int get_diff_color_found;
322 static int get_color_ui_found;
323 static int git_get_colorbool_config(const char *var, const char *value,
324 void *cb)
326 if (!strcmp(var, get_colorbool_slot))
327 get_colorbool_found = git_config_colorbool(var, value);
328 else if (!strcmp(var, "diff.color"))
329 get_diff_color_found = git_config_colorbool(var, value);
330 else if (!strcmp(var, "color.ui"))
331 get_color_ui_found = git_config_colorbool(var, value);
332 return 0;
335 static int get_colorbool(int print)
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_file, given_config_blob,
342 respect_includes);
344 if (get_colorbool_found < 0) {
345 if (!strcmp(get_colorbool_slot, "color.diff"))
346 get_colorbool_found = get_diff_color_found;
347 if (get_colorbool_found < 0)
348 get_colorbool_found = get_color_ui_found;
351 if (get_colorbool_found < 0)
352 /* default value if none found in config */
353 get_colorbool_found = GIT_COLOR_AUTO;
355 get_colorbool_found = want_color(get_colorbool_found);
357 if (print) {
358 printf("%s\n", get_colorbool_found ? "true" : "false");
359 return 0;
360 } else
361 return get_colorbool_found ? 0 : 1;
364 static void check_blob_write(void)
366 if (given_config_blob)
367 die("writing config blobs is not supported");
370 struct urlmatch_current_candidate_value {
371 char value_is_null;
372 struct strbuf value;
375 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
377 struct string_list *values = cb;
378 struct string_list_item *item = string_list_insert(values, var);
379 struct urlmatch_current_candidate_value *matched = item->util;
381 if (!matched) {
382 matched = xmalloc(sizeof(*matched));
383 strbuf_init(&matched->value, 0);
384 item->util = matched;
385 } else {
386 strbuf_reset(&matched->value);
389 if (value) {
390 strbuf_addstr(&matched->value, value);
391 matched->value_is_null = 0;
392 } else {
393 matched->value_is_null = 1;
395 return 0;
398 static char *dup_downcase(const char *string)
400 char *result;
401 size_t len, i;
403 len = strlen(string);
404 result = xmalloc(len + 1);
405 for (i = 0; i < len; i++)
406 result[i] = tolower(string[i]);
407 result[i] = '\0';
408 return result;
411 static int get_urlmatch(const char *var, const char *url)
413 char *section_tail;
414 struct string_list_item *item;
415 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
416 struct string_list values = STRING_LIST_INIT_DUP;
418 config.collect_fn = urlmatch_collect_fn;
419 config.cascade_fn = NULL;
420 config.cb = &values;
422 if (!url_normalize(url, &config.url))
423 die("%s", config.url.err);
425 config.section = dup_downcase(var);
426 section_tail = strchr(config.section, '.');
427 if (section_tail) {
428 *section_tail = '\0';
429 config.key = section_tail + 1;
430 show_keys = 0;
431 } else {
432 config.key = NULL;
433 show_keys = 1;
436 git_config_with_options(urlmatch_config_entry, &config,
437 given_config_file, NULL, respect_includes);
439 for_each_string_list_item(item, &values) {
440 struct urlmatch_current_candidate_value *matched = item->util;
441 struct strbuf key = STRBUF_INIT;
442 struct strbuf buf = STRBUF_INIT;
444 strbuf_addstr(&key, item->string);
445 format_config(&buf, key.buf,
446 matched->value_is_null ? NULL : matched->value.buf);
447 fwrite(buf.buf, 1, buf.len, stdout);
448 strbuf_release(&key);
449 strbuf_release(&buf);
451 strbuf_release(&matched->value);
453 string_list_clear(&config.vars, 1);
454 string_list_clear(&values, 1);
455 free(config.url.url);
457 free((void *)config.section);
458 return 0;
461 int cmd_config(int argc, const char **argv, const char *prefix)
463 int nongit = !startup_info->have_repository;
464 char *value;
466 given_config_file = getenv(CONFIG_ENVIRONMENT);
468 argc = parse_options(argc, argv, prefix, builtin_config_options,
469 builtin_config_usage,
470 PARSE_OPT_STOP_AT_NON_OPTION);
472 if (use_global_config + use_system_config + use_local_config +
473 !!given_config_file + !!given_config_blob > 1) {
474 error("only one config file at a time.");
475 usage_with_options(builtin_config_usage, builtin_config_options);
478 if (use_global_config) {
479 char *user_config = NULL;
480 char *xdg_config = NULL;
482 home_config_paths(&user_config, &xdg_config, "config");
484 if (!user_config)
486 * It is unknown if HOME/.gitconfig exists, so
487 * we do not know if we should write to XDG
488 * location; error out even if XDG_CONFIG_HOME
489 * is set and points at a sane location.
491 die("$HOME not set");
493 if (access_or_warn(user_config, R_OK, 0) &&
494 xdg_config && !access_or_warn(xdg_config, R_OK, 0))
495 given_config_file = xdg_config;
496 else
497 given_config_file = user_config;
499 else if (use_system_config)
500 given_config_file = git_etc_gitconfig();
501 else if (use_local_config)
502 given_config_file = git_pathdup("config");
503 else if (given_config_file) {
504 if (!is_absolute_path(given_config_file) && prefix)
505 given_config_file =
506 xstrdup(prefix_filename(prefix,
507 strlen(prefix),
508 given_config_file));
511 if (respect_includes == -1)
512 respect_includes = !given_config_file;
514 if (end_null) {
515 term = '\0';
516 delim = '\n';
517 key_delim = '\n';
520 if (HAS_MULTI_BITS(types)) {
521 error("only one type at a time.");
522 usage_with_options(builtin_config_usage, builtin_config_options);
525 if (get_color_slot)
526 actions |= ACTION_GET_COLOR;
527 if (get_colorbool_slot)
528 actions |= ACTION_GET_COLORBOOL;
530 if ((get_color_slot || get_colorbool_slot) && types) {
531 error("--get-color and variable type are incoherent");
532 usage_with_options(builtin_config_usage, builtin_config_options);
535 if (HAS_MULTI_BITS(actions)) {
536 error("only one action at a time.");
537 usage_with_options(builtin_config_usage, builtin_config_options);
539 if (actions == 0)
540 switch (argc) {
541 case 1: actions = ACTION_GET; break;
542 case 2: actions = ACTION_SET; break;
543 case 3: actions = ACTION_SET_ALL; break;
544 default:
545 usage_with_options(builtin_config_usage, builtin_config_options);
548 if (actions == ACTION_LIST) {
549 check_argc(argc, 0, 0);
550 if (git_config_with_options(show_all_config, NULL,
551 given_config_file,
552 given_config_blob,
553 respect_includes) < 0) {
554 if (given_config_file)
555 die_errno("unable to read config file '%s'",
556 given_config_file);
557 else
558 die("error processing config file(s)");
561 else if (actions == ACTION_EDIT) {
562 check_argc(argc, 0, 0);
563 if (!given_config_file && nongit)
564 die("not in a git directory");
565 if (given_config_blob)
566 die("editing blobs is not supported");
567 git_config(git_default_config, NULL);
568 launch_editor(given_config_file ?
569 given_config_file : git_path("config"),
570 NULL, NULL);
572 else if (actions == ACTION_SET) {
573 int ret;
574 check_blob_write();
575 check_argc(argc, 2, 2);
576 value = normalize_value(argv[0], argv[1]);
577 ret = git_config_set_in_file(given_config_file, argv[0], value);
578 if (ret == CONFIG_NOTHING_SET)
579 error("cannot overwrite multiple values with a single value\n"
580 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
581 return ret;
583 else if (actions == ACTION_SET_ALL) {
584 check_blob_write();
585 check_argc(argc, 2, 3);
586 value = normalize_value(argv[0], argv[1]);
587 return git_config_set_multivar_in_file(given_config_file,
588 argv[0], value, argv[2], 0);
590 else if (actions == ACTION_ADD) {
591 check_blob_write();
592 check_argc(argc, 2, 2);
593 value = normalize_value(argv[0], argv[1]);
594 return git_config_set_multivar_in_file(given_config_file,
595 argv[0], value, "^$", 0);
597 else if (actions == ACTION_REPLACE_ALL) {
598 check_blob_write();
599 check_argc(argc, 2, 3);
600 value = normalize_value(argv[0], argv[1]);
601 return git_config_set_multivar_in_file(given_config_file,
602 argv[0], value, argv[2], 1);
604 else if (actions == ACTION_GET) {
605 check_argc(argc, 1, 2);
606 return get_value(argv[0], argv[1]);
608 else if (actions == ACTION_GET_ALL) {
609 do_all = 1;
610 check_argc(argc, 1, 2);
611 return get_value(argv[0], argv[1]);
613 else if (actions == ACTION_GET_REGEXP) {
614 show_keys = 1;
615 use_key_regexp = 1;
616 do_all = 1;
617 check_argc(argc, 1, 2);
618 return get_value(argv[0], argv[1]);
620 else if (actions == ACTION_GET_URLMATCH) {
621 check_argc(argc, 2, 2);
622 return get_urlmatch(argv[0], argv[1]);
624 else if (actions == ACTION_UNSET) {
625 check_blob_write();
626 check_argc(argc, 1, 2);
627 if (argc == 2)
628 return git_config_set_multivar_in_file(given_config_file,
629 argv[0], NULL, argv[1], 0);
630 else
631 return git_config_set_in_file(given_config_file,
632 argv[0], NULL);
634 else if (actions == ACTION_UNSET_ALL) {
635 check_blob_write();
636 check_argc(argc, 1, 2);
637 return git_config_set_multivar_in_file(given_config_file,
638 argv[0], NULL, argv[1], 1);
640 else if (actions == ACTION_RENAME_SECTION) {
641 int ret;
642 check_blob_write();
643 check_argc(argc, 2, 2);
644 ret = git_config_rename_section_in_file(given_config_file,
645 argv[0], argv[1]);
646 if (ret < 0)
647 return ret;
648 if (ret == 0)
649 die("No such section!");
651 else if (actions == ACTION_REMOVE_SECTION) {
652 int ret;
653 check_blob_write();
654 check_argc(argc, 1, 1);
655 ret = git_config_rename_section_in_file(given_config_file,
656 argv[0], NULL);
657 if (ret < 0)
658 return ret;
659 if (ret == 0)
660 die("No such section!");
662 else if (actions == ACTION_GET_COLOR) {
663 get_color(argv[0]);
665 else if (actions == ACTION_GET_COLORBOOL) {
666 if (argc == 1)
667 color_stdout_is_tty = git_config_bool("command line", argv[0]);
668 return get_colorbool(argc != 0);
671 return 0;
674 int cmd_repo_config(int argc, const char **argv, const char *prefix)
676 fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
677 return cmd_config(argc, argv, prefix);