config: add '--name-only' option to list only variable names
[alt-git.git] / builtin / config.c
blob631db458ec12b87d115bcbcf26caadf9137962f4
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 omit_values;
17 static int use_key_regexp;
18 static int do_all;
19 static int do_not_match;
20 static char delim = '=';
21 static char key_delim = ' ';
22 static char term = '\n';
24 static int use_global_config, use_system_config, use_local_config;
25 static struct git_config_source given_config_source;
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_source.file, N_("file"), N_("use given config file")),
59 OPT_STRING(0, "blob", &given_config_source.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_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
74 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
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, "name-only", &omit_values, N_("show variable names only")),
83 OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
84 OPT_END(),
87 static void check_argc(int argc, int min, int max) {
88 if (argc >= min && argc <= max)
89 return;
90 error("wrong number of arguments");
91 usage_with_options(builtin_config_usage, builtin_config_options);
94 static int show_all_config(const char *key_, const char *value_, void *cb)
96 if (!omit_values && value_)
97 printf("%s%c%s%c", key_, delim, value_, term);
98 else
99 printf("%s%c", key_, term);
100 return 0;
103 struct strbuf_list {
104 struct strbuf *items;
105 int nr;
106 int alloc;
109 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
111 int must_free_vptr = 0;
112 int must_print_delim = 0;
113 char value[256];
114 const char *vptr = value;
116 strbuf_init(buf, 0);
118 if (show_keys) {
119 strbuf_addstr(buf, key_);
120 must_print_delim = 1;
122 if (omit_values) {
123 strbuf_addch(buf, term);
124 return 0;
126 if (types == TYPE_INT)
127 sprintf(value, "%"PRId64,
128 git_config_int64(key_, value_ ? value_ : ""));
129 else if (types == TYPE_BOOL)
130 vptr = git_config_bool(key_, value_) ? "true" : "false";
131 else if (types == TYPE_BOOL_OR_INT) {
132 int is_bool, v;
133 v = git_config_bool_or_int(key_, value_, &is_bool);
134 if (is_bool)
135 vptr = v ? "true" : "false";
136 else
137 sprintf(value, "%d", v);
138 } else if (types == TYPE_PATH) {
139 if (git_config_pathname(&vptr, key_, value_) < 0)
140 return -1;
141 must_free_vptr = 1;
142 } else if (value_) {
143 vptr = value_;
144 } else {
145 /* Just show the key name */
146 vptr = "";
147 must_print_delim = 0;
150 if (must_print_delim)
151 strbuf_addch(buf, key_delim);
152 strbuf_addstr(buf, vptr);
153 strbuf_addch(buf, term);
155 if (must_free_vptr)
156 free((char *)vptr);
157 return 0;
160 static int collect_config(const char *key_, const char *value_, void *cb)
162 struct strbuf_list *values = cb;
164 if (!use_key_regexp && strcmp(key_, key))
165 return 0;
166 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
167 return 0;
168 if (regexp != NULL &&
169 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
170 return 0;
172 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
174 return format_config(&values->items[values->nr++], key_, value_);
177 static int get_value(const char *key_, const char *regex_)
179 int ret = CONFIG_GENERIC_ERROR;
180 struct strbuf_list values = {NULL};
181 int i;
183 if (use_key_regexp) {
184 char *tl;
187 * NEEDSWORK: this naive pattern lowercasing obviously does not
188 * work for more complex patterns like "^[^.]*Foo.*bar".
189 * Perhaps we should deprecate this altogether someday.
192 key = xstrdup(key_);
193 for (tl = key + strlen(key) - 1;
194 tl >= key && *tl != '.';
195 tl--)
196 *tl = tolower(*tl);
197 for (tl = key; *tl && *tl != '.'; tl++)
198 *tl = tolower(*tl);
200 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
201 if (regcomp(key_regexp, key, REG_EXTENDED)) {
202 error("invalid key pattern: %s", key_);
203 free(key_regexp);
204 key_regexp = NULL;
205 ret = CONFIG_INVALID_PATTERN;
206 goto free_strings;
208 } else {
209 if (git_config_parse_key(key_, &key, NULL)) {
210 ret = CONFIG_INVALID_KEY;
211 goto free_strings;
215 if (regex_) {
216 if (regex_[0] == '!') {
217 do_not_match = 1;
218 regex_++;
221 regexp = (regex_t*)xmalloc(sizeof(regex_t));
222 if (regcomp(regexp, regex_, REG_EXTENDED)) {
223 error("invalid pattern: %s", regex_);
224 free(regexp);
225 regexp = NULL;
226 ret = CONFIG_INVALID_PATTERN;
227 goto free_strings;
231 git_config_with_options(collect_config, &values,
232 &given_config_source, respect_includes);
234 ret = !values.nr;
236 for (i = 0; i < values.nr; i++) {
237 struct strbuf *buf = values.items + i;
238 if (do_all || i == values.nr - 1)
239 fwrite(buf->buf, 1, buf->len, stdout);
240 strbuf_release(buf);
242 free(values.items);
244 free_strings:
245 free(key);
246 if (key_regexp) {
247 regfree(key_regexp);
248 free(key_regexp);
250 if (regexp) {
251 regfree(regexp);
252 free(regexp);
255 return ret;
258 static char *normalize_value(const char *key, const char *value)
260 char *normalized;
262 if (!value)
263 return NULL;
265 if (types == 0 || types == TYPE_PATH)
267 * We don't do normalization for TYPE_PATH here: If
268 * the path is like ~/foobar/, we prefer to store
269 * "~/foobar/" in the config file, and to expand the ~
270 * when retrieving the value.
272 normalized = xstrdup(value);
273 else {
274 normalized = xmalloc(64);
275 if (types == TYPE_INT) {
276 int64_t v = git_config_int64(key, value);
277 sprintf(normalized, "%"PRId64, v);
279 else if (types == TYPE_BOOL)
280 sprintf(normalized, "%s",
281 git_config_bool(key, value) ? "true" : "false");
282 else if (types == TYPE_BOOL_OR_INT) {
283 int is_bool, v;
284 v = git_config_bool_or_int(key, value, &is_bool);
285 if (!is_bool)
286 sprintf(normalized, "%d", v);
287 else
288 sprintf(normalized, "%s", v ? "true" : "false");
292 return normalized;
295 static int get_color_found;
296 static const char *get_color_slot;
297 static const char *get_colorbool_slot;
298 static char parsed_color[COLOR_MAXLEN];
300 static int git_get_color_config(const char *var, const char *value, void *cb)
302 if (!strcmp(var, get_color_slot)) {
303 if (!value)
304 config_error_nonbool(var);
305 if (color_parse(value, parsed_color) < 0)
306 return -1;
307 get_color_found = 1;
309 return 0;
312 static void get_color(const char *var, const char *def_color)
314 get_color_slot = var;
315 get_color_found = 0;
316 parsed_color[0] = '\0';
317 git_config_with_options(git_get_color_config, NULL,
318 &given_config_source, respect_includes);
320 if (!get_color_found && def_color) {
321 if (color_parse(def_color, parsed_color) < 0)
322 die(_("unable to parse default color value"));
325 fputs(parsed_color, stdout);
328 static int get_colorbool_found;
329 static int get_diff_color_found;
330 static int get_color_ui_found;
331 static int git_get_colorbool_config(const char *var, const char *value,
332 void *cb)
334 if (!strcmp(var, get_colorbool_slot))
335 get_colorbool_found = git_config_colorbool(var, value);
336 else if (!strcmp(var, "diff.color"))
337 get_diff_color_found = git_config_colorbool(var, value);
338 else if (!strcmp(var, "color.ui"))
339 get_color_ui_found = git_config_colorbool(var, value);
340 return 0;
343 static int get_colorbool(const char *var, int print)
345 get_colorbool_slot = var;
346 get_colorbool_found = -1;
347 get_diff_color_found = -1;
348 get_color_ui_found = -1;
349 git_config_with_options(git_get_colorbool_config, NULL,
350 &given_config_source, respect_includes);
352 if (get_colorbool_found < 0) {
353 if (!strcmp(get_colorbool_slot, "color.diff"))
354 get_colorbool_found = get_diff_color_found;
355 if (get_colorbool_found < 0)
356 get_colorbool_found = get_color_ui_found;
359 if (get_colorbool_found < 0)
360 /* default value if none found in config */
361 get_colorbool_found = GIT_COLOR_AUTO;
363 get_colorbool_found = want_color(get_colorbool_found);
365 if (print) {
366 printf("%s\n", get_colorbool_found ? "true" : "false");
367 return 0;
368 } else
369 return get_colorbool_found ? 0 : 1;
372 static void check_write(void)
374 if (given_config_source.use_stdin)
375 die("writing to stdin is not supported");
377 if (given_config_source.blob)
378 die("writing config blobs is not supported");
381 struct urlmatch_current_candidate_value {
382 char value_is_null;
383 struct strbuf value;
386 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
388 struct string_list *values = cb;
389 struct string_list_item *item = string_list_insert(values, var);
390 struct urlmatch_current_candidate_value *matched = item->util;
392 if (!matched) {
393 matched = xmalloc(sizeof(*matched));
394 strbuf_init(&matched->value, 0);
395 item->util = matched;
396 } else {
397 strbuf_reset(&matched->value);
400 if (value) {
401 strbuf_addstr(&matched->value, value);
402 matched->value_is_null = 0;
403 } else {
404 matched->value_is_null = 1;
406 return 0;
409 static int get_urlmatch(const char *var, const char *url)
411 char *section_tail;
412 struct string_list_item *item;
413 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
414 struct string_list values = STRING_LIST_INIT_DUP;
416 config.collect_fn = urlmatch_collect_fn;
417 config.cascade_fn = NULL;
418 config.cb = &values;
420 if (!url_normalize(url, &config.url))
421 die("%s", config.url.err);
423 config.section = xstrdup_tolower(var);
424 section_tail = strchr(config.section, '.');
425 if (section_tail) {
426 *section_tail = '\0';
427 config.key = section_tail + 1;
428 show_keys = 0;
429 } else {
430 config.key = NULL;
431 show_keys = 1;
434 git_config_with_options(urlmatch_config_entry, &config,
435 &given_config_source, respect_includes);
437 for_each_string_list_item(item, &values) {
438 struct urlmatch_current_candidate_value *matched = item->util;
439 struct strbuf key = STRBUF_INIT;
440 struct strbuf buf = STRBUF_INIT;
442 strbuf_addstr(&key, item->string);
443 format_config(&buf, key.buf,
444 matched->value_is_null ? NULL : matched->value.buf);
445 fwrite(buf.buf, 1, buf.len, stdout);
446 strbuf_release(&key);
447 strbuf_release(&buf);
449 strbuf_release(&matched->value);
451 string_list_clear(&config.vars, 1);
452 string_list_clear(&values, 1);
453 free(config.url.url);
455 free((void *)config.section);
456 return 0;
459 static char *default_user_config(void)
461 struct strbuf buf = STRBUF_INIT;
462 strbuf_addf(&buf,
463 _("# This is Git's per-user configuration file.\n"
464 "[user]\n"
465 "# Please adapt and uncomment the following lines:\n"
466 "# name = %s\n"
467 "# email = %s\n"),
468 ident_default_name(),
469 ident_default_email());
470 return strbuf_detach(&buf, NULL);
473 int cmd_config(int argc, const char **argv, const char *prefix)
475 int nongit = !startup_info->have_repository;
476 char *value;
478 given_config_source.file = getenv(CONFIG_ENVIRONMENT);
480 argc = parse_options(argc, argv, prefix, builtin_config_options,
481 builtin_config_usage,
482 PARSE_OPT_STOP_AT_NON_OPTION);
484 if (use_global_config + use_system_config + use_local_config +
485 !!given_config_source.file + !!given_config_source.blob > 1) {
486 error("only one config file at a time.");
487 usage_with_options(builtin_config_usage, builtin_config_options);
490 if (given_config_source.file &&
491 !strcmp(given_config_source.file, "-")) {
492 given_config_source.file = NULL;
493 given_config_source.use_stdin = 1;
496 if (use_global_config) {
497 char *user_config = expand_user_path("~/.gitconfig");
498 char *xdg_config = xdg_config_home("config");
500 if (!user_config)
502 * It is unknown if HOME/.gitconfig exists, so
503 * we do not know if we should write to XDG
504 * location; error out even if XDG_CONFIG_HOME
505 * is set and points at a sane location.
507 die("$HOME not set");
509 if (access_or_warn(user_config, R_OK, 0) &&
510 xdg_config && !access_or_warn(xdg_config, R_OK, 0))
511 given_config_source.file = xdg_config;
512 else
513 given_config_source.file = user_config;
515 else if (use_system_config)
516 given_config_source.file = git_etc_gitconfig();
517 else if (use_local_config)
518 given_config_source.file = git_pathdup("config");
519 else if (given_config_source.file) {
520 if (!is_absolute_path(given_config_source.file) && prefix)
521 given_config_source.file =
522 xstrdup(prefix_filename(prefix,
523 strlen(prefix),
524 given_config_source.file));
527 if (respect_includes == -1)
528 respect_includes = !given_config_source.file;
530 if (end_null) {
531 term = '\0';
532 delim = '\n';
533 key_delim = '\n';
536 if (HAS_MULTI_BITS(types)) {
537 error("only one type at a time.");
538 usage_with_options(builtin_config_usage, builtin_config_options);
541 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
542 error("--get-color and variable type are incoherent");
543 usage_with_options(builtin_config_usage, builtin_config_options);
546 if (HAS_MULTI_BITS(actions)) {
547 error("only one action at a time.");
548 usage_with_options(builtin_config_usage, builtin_config_options);
550 if (actions == 0)
551 switch (argc) {
552 case 1: actions = ACTION_GET; break;
553 case 2: actions = ACTION_SET; break;
554 case 3: actions = ACTION_SET_ALL; break;
555 default:
556 usage_with_options(builtin_config_usage, builtin_config_options);
558 if (omit_values &&
559 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
560 error("--name-only is only applicable to --list or --get-regexp");
561 usage_with_options(builtin_config_usage, builtin_config_options);
563 if (actions == ACTION_LIST) {
564 check_argc(argc, 0, 0);
565 if (git_config_with_options(show_all_config, NULL,
566 &given_config_source,
567 respect_includes) < 0) {
568 if (given_config_source.file)
569 die_errno("unable to read config file '%s'",
570 given_config_source.file);
571 else
572 die("error processing config file(s)");
575 else if (actions == ACTION_EDIT) {
576 char *config_file;
578 check_argc(argc, 0, 0);
579 if (!given_config_source.file && nongit)
580 die("not in a git directory");
581 if (given_config_source.use_stdin)
582 die("editing stdin is not supported");
583 if (given_config_source.blob)
584 die("editing blobs is not supported");
585 git_config(git_default_config, NULL);
586 config_file = xstrdup(given_config_source.file ?
587 given_config_source.file : git_path("config"));
588 if (use_global_config) {
589 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
590 if (fd) {
591 char *content = default_user_config();
592 write_str_in_full(fd, content);
593 free(content);
594 close(fd);
596 else if (errno != EEXIST)
597 die_errno(_("cannot create configuration file %s"), config_file);
599 launch_editor(config_file, NULL, NULL);
600 free(config_file);
602 else if (actions == ACTION_SET) {
603 int ret;
604 check_write();
605 check_argc(argc, 2, 2);
606 value = normalize_value(argv[0], argv[1]);
607 ret = git_config_set_in_file(given_config_source.file, argv[0], value);
608 if (ret == CONFIG_NOTHING_SET)
609 error("cannot overwrite multiple values with a single value\n"
610 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
611 return ret;
613 else if (actions == ACTION_SET_ALL) {
614 check_write();
615 check_argc(argc, 2, 3);
616 value = normalize_value(argv[0], argv[1]);
617 return git_config_set_multivar_in_file(given_config_source.file,
618 argv[0], value, argv[2], 0);
620 else if (actions == ACTION_ADD) {
621 check_write();
622 check_argc(argc, 2, 2);
623 value = normalize_value(argv[0], argv[1]);
624 return git_config_set_multivar_in_file(given_config_source.file,
625 argv[0], value,
626 CONFIG_REGEX_NONE, 0);
628 else if (actions == ACTION_REPLACE_ALL) {
629 check_write();
630 check_argc(argc, 2, 3);
631 value = normalize_value(argv[0], argv[1]);
632 return git_config_set_multivar_in_file(given_config_source.file,
633 argv[0], value, argv[2], 1);
635 else if (actions == ACTION_GET) {
636 check_argc(argc, 1, 2);
637 return get_value(argv[0], argv[1]);
639 else if (actions == ACTION_GET_ALL) {
640 do_all = 1;
641 check_argc(argc, 1, 2);
642 return get_value(argv[0], argv[1]);
644 else if (actions == ACTION_GET_REGEXP) {
645 show_keys = 1;
646 use_key_regexp = 1;
647 do_all = 1;
648 check_argc(argc, 1, 2);
649 return get_value(argv[0], argv[1]);
651 else if (actions == ACTION_GET_URLMATCH) {
652 check_argc(argc, 2, 2);
653 return get_urlmatch(argv[0], argv[1]);
655 else if (actions == ACTION_UNSET) {
656 check_write();
657 check_argc(argc, 1, 2);
658 if (argc == 2)
659 return git_config_set_multivar_in_file(given_config_source.file,
660 argv[0], NULL, argv[1], 0);
661 else
662 return git_config_set_in_file(given_config_source.file,
663 argv[0], NULL);
665 else if (actions == ACTION_UNSET_ALL) {
666 check_write();
667 check_argc(argc, 1, 2);
668 return git_config_set_multivar_in_file(given_config_source.file,
669 argv[0], NULL, argv[1], 1);
671 else if (actions == ACTION_RENAME_SECTION) {
672 int ret;
673 check_write();
674 check_argc(argc, 2, 2);
675 ret = git_config_rename_section_in_file(given_config_source.file,
676 argv[0], argv[1]);
677 if (ret < 0)
678 return ret;
679 if (ret == 0)
680 die("No such section!");
682 else if (actions == ACTION_REMOVE_SECTION) {
683 int ret;
684 check_write();
685 check_argc(argc, 1, 1);
686 ret = git_config_rename_section_in_file(given_config_source.file,
687 argv[0], NULL);
688 if (ret < 0)
689 return ret;
690 if (ret == 0)
691 die("No such section!");
693 else if (actions == ACTION_GET_COLOR) {
694 check_argc(argc, 1, 2);
695 get_color(argv[0], argv[1]);
697 else if (actions == ACTION_GET_COLORBOOL) {
698 check_argc(argc, 1, 2);
699 if (argc == 2)
700 color_stdout_is_tty = git_config_bool("command line", argv[1]);
701 return get_colorbool(argv[0], argc == 2);
704 return 0;