4 #include "parse-options.h"
7 static const char *const builtin_config_usage[] = {
8 N_("git config [<options>]"),
13 static regex_t *key_regexp;
14 static regex_t *regexp;
16 static int use_key_regexp;
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;
27 static int respect_includes = -1;
29 #define ACTION_GET (1<<0)
30 #define ACTION_GET_ALL (1<<1)
31 #define ACTION_GET_REGEXP (1<<2)
32 #define ACTION_REPLACE_ALL (1<<3)
33 #define ACTION_ADD (1<<4)
34 #define ACTION_UNSET (1<<5)
35 #define ACTION_UNSET_ALL (1<<6)
36 #define ACTION_RENAME_SECTION (1<<7)
37 #define ACTION_REMOVE_SECTION (1<<8)
38 #define ACTION_LIST (1<<9)
39 #define ACTION_EDIT (1<<10)
40 #define ACTION_SET (1<<11)
41 #define ACTION_SET_ALL (1<<12)
42 #define ACTION_GET_COLOR (1<<13)
43 #define ACTION_GET_COLORBOOL (1<<14)
44 #define ACTION_GET_URLMATCH (1<<15)
46 #define TYPE_BOOL (1<<0)
47 #define TYPE_INT (1<<1)
48 #define TYPE_BOOL_OR_INT (1<<2)
49 #define TYPE_PATH (1<<3)
51 static struct option builtin_config_options[] = {
52 OPT_GROUP(N_("Config file location")),
53 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
54 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
55 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
56 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
57 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
58 OPT_GROUP(N_("Action")),
59 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
60 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
61 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
62 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
63 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
64 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
65 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
66 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
67 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
68 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
69 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
70 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
71 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
72 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
73 OPT_GROUP(N_("Type")),
74 OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
75 OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
76 OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
77 OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
78 OPT_GROUP(N_("Other")),
79 OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
80 OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
84 static void check_argc(int argc, int min, int max) {
85 if (argc >= min && argc <= max)
87 error("wrong number of arguments");
88 usage_with_options(builtin_config_usage, builtin_config_options);
91 static int show_all_config(const char *key_, const char *value_, void *cb)
94 printf("%s%c%s%c", key_, delim, value_, term);
96 printf("%s%c", key_, term);
101 struct strbuf *items;
106 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
108 int must_free_vptr = 0;
109 int must_print_delim = 0;
111 const char *vptr = value;
116 strbuf_addstr(buf, key_);
117 must_print_delim = 1;
119 if (types == TYPE_INT)
120 sprintf(value, "%"PRId64,
121 git_config_int64(key_, value_ ? value_ : ""));
122 else if (types == TYPE_BOOL)
123 vptr = git_config_bool(key_, value_) ? "true" : "false";
124 else if (types == TYPE_BOOL_OR_INT) {
126 v = git_config_bool_or_int(key_, value_, &is_bool);
128 vptr = v ? "true" : "false";
130 sprintf(value, "%d", v);
131 } else if (types == TYPE_PATH) {
132 if (git_config_pathname(&vptr, key_, value_) < 0)
138 /* Just show the key name */
140 must_print_delim = 0;
143 if (must_print_delim)
144 strbuf_addch(buf, key_delim);
145 strbuf_addstr(buf, vptr);
146 strbuf_addch(buf, term);
153 static int collect_config(const char *key_, const char *value_, void *cb)
155 struct strbuf_list *values = cb;
157 if (!use_key_regexp && strcmp(key_, key))
159 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
161 if (regexp != NULL &&
162 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
165 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
167 return format_config(&values->items[values->nr++], key_, value_);
170 static int get_value(const char *key_, const char *regex_)
172 int ret = CONFIG_GENERIC_ERROR;
173 struct strbuf_list values = {NULL};
176 if (use_key_regexp) {
180 * NEEDSWORK: this naive pattern lowercasing obviously does not
181 * work for more complex patterns like "^[^.]*Foo.*bar".
182 * Perhaps we should deprecate this altogether someday.
186 for (tl = key + strlen(key) - 1;
187 tl >= key && *tl != '.';
190 for (tl = key; *tl && *tl != '.'; tl++)
193 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
194 if (regcomp(key_regexp, key, REG_EXTENDED)) {
195 fprintf(stderr, "Invalid key pattern: %s\n", key_);
198 ret = CONFIG_INVALID_PATTERN;
202 if (git_config_parse_key(key_, &key, NULL)) {
203 ret = CONFIG_INVALID_KEY;
209 if (regex_[0] == '!') {
214 regexp = (regex_t*)xmalloc(sizeof(regex_t));
215 if (regcomp(regexp, regex_, REG_EXTENDED)) {
216 fprintf(stderr, "Invalid pattern: %s\n", regex_);
219 ret = CONFIG_INVALID_PATTERN;
224 git_config_with_options(collect_config, &values,
225 &given_config_source, respect_includes);
229 for (i = 0; i < values.nr; i++) {
230 struct strbuf *buf = values.items + i;
231 if (do_all || i == values.nr - 1)
232 fwrite(buf->buf, 1, buf->len, stdout);
251 static char *normalize_value(const char *key, const char *value)
258 if (types == 0 || types == TYPE_PATH)
260 * We don't do normalization for TYPE_PATH here: If
261 * the path is like ~/foobar/, we prefer to store
262 * "~/foobar/" in the config file, and to expand the ~
263 * when retrieving the value.
265 normalized = xstrdup(value);
267 normalized = xmalloc(64);
268 if (types == TYPE_INT) {
269 int64_t v = git_config_int64(key, value);
270 sprintf(normalized, "%"PRId64, v);
272 else if (types == TYPE_BOOL)
273 sprintf(normalized, "%s",
274 git_config_bool(key, value) ? "true" : "false");
275 else if (types == TYPE_BOOL_OR_INT) {
277 v = git_config_bool_or_int(key, value, &is_bool);
279 sprintf(normalized, "%d", v);
281 sprintf(normalized, "%s", v ? "true" : "false");
288 static int get_color_found;
289 static const char *get_color_slot;
290 static const char *get_colorbool_slot;
291 static char parsed_color[COLOR_MAXLEN];
293 static int git_get_color_config(const char *var, const char *value, void *cb)
295 if (!strcmp(var, get_color_slot)) {
297 config_error_nonbool(var);
298 if (color_parse(value, parsed_color) < 0)
305 static void get_color(const char *var, const char *def_color)
307 get_color_slot = var;
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 if (color_parse(def_color, parsed_color) < 0)
315 die(_("unable to parse default color value"));
318 fputs(parsed_color, stdout);
321 static int get_colorbool_found;
322 static int get_diff_color_found;
323 static int get_color_ui_found;
324 static int git_get_colorbool_config(const char *var, const char *value,
327 if (!strcmp(var, get_colorbool_slot))
328 get_colorbool_found = git_config_colorbool(var, value);
329 else if (!strcmp(var, "diff.color"))
330 get_diff_color_found = git_config_colorbool(var, value);
331 else if (!strcmp(var, "color.ui"))
332 get_color_ui_found = git_config_colorbool(var, value);
336 static int get_colorbool(const char *var, int print)
338 get_colorbool_slot = var;
339 get_colorbool_found = -1;
340 get_diff_color_found = -1;
341 get_color_ui_found = -1;
342 git_config_with_options(git_get_colorbool_config, NULL,
343 &given_config_source, respect_includes);
345 if (get_colorbool_found < 0) {
346 if (!strcmp(get_colorbool_slot, "color.diff"))
347 get_colorbool_found = get_diff_color_found;
348 if (get_colorbool_found < 0)
349 get_colorbool_found = get_color_ui_found;
352 if (get_colorbool_found < 0)
353 /* default value if none found in config */
354 get_colorbool_found = GIT_COLOR_AUTO;
356 get_colorbool_found = want_color(get_colorbool_found);
359 printf("%s\n", get_colorbool_found ? "true" : "false");
362 return get_colorbool_found ? 0 : 1;
365 static void check_write(void)
367 if (given_config_source.use_stdin)
368 die("writing to stdin is not supported");
370 if (given_config_source.blob)
371 die("writing config blobs is not supported");
374 struct urlmatch_current_candidate_value {
379 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
381 struct string_list *values = cb;
382 struct string_list_item *item = string_list_insert(values, var);
383 struct urlmatch_current_candidate_value *matched = item->util;
386 matched = xmalloc(sizeof(*matched));
387 strbuf_init(&matched->value, 0);
388 item->util = matched;
390 strbuf_reset(&matched->value);
394 strbuf_addstr(&matched->value, value);
395 matched->value_is_null = 0;
397 matched->value_is_null = 1;
402 static int get_urlmatch(const char *var, const char *url)
405 struct string_list_item *item;
406 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
407 struct string_list values = STRING_LIST_INIT_DUP;
409 config.collect_fn = urlmatch_collect_fn;
410 config.cascade_fn = NULL;
413 if (!url_normalize(url, &config.url))
414 die("%s", config.url.err);
416 config.section = xstrdup_tolower(var);
417 section_tail = strchr(config.section, '.');
419 *section_tail = '\0';
420 config.key = section_tail + 1;
427 git_config_with_options(urlmatch_config_entry, &config,
428 &given_config_source, respect_includes);
430 for_each_string_list_item(item, &values) {
431 struct urlmatch_current_candidate_value *matched = item->util;
432 struct strbuf key = STRBUF_INIT;
433 struct strbuf buf = STRBUF_INIT;
435 strbuf_addstr(&key, item->string);
436 format_config(&buf, key.buf,
437 matched->value_is_null ? NULL : matched->value.buf);
438 fwrite(buf.buf, 1, buf.len, stdout);
439 strbuf_release(&key);
440 strbuf_release(&buf);
442 strbuf_release(&matched->value);
444 string_list_clear(&config.vars, 1);
445 string_list_clear(&values, 1);
446 free(config.url.url);
448 free((void *)config.section);
452 static char *default_user_config(void)
454 struct strbuf buf = STRBUF_INIT;
456 _("# This is Git's per-user configuration file.\n"
458 "# Please adapt and uncomment the following lines:\n"
461 ident_default_name(),
462 ident_default_email());
463 return strbuf_detach(&buf, NULL);
466 int cmd_config(int argc, const char **argv, const char *prefix)
468 int nongit = !startup_info->have_repository;
471 given_config_source.file = getenv(CONFIG_ENVIRONMENT);
473 argc = parse_options(argc, argv, prefix, builtin_config_options,
474 builtin_config_usage,
475 PARSE_OPT_STOP_AT_NON_OPTION);
477 if (use_global_config + use_system_config + use_local_config +
478 !!given_config_source.file + !!given_config_source.blob > 1) {
479 error("only one config file at a time.");
480 usage_with_options(builtin_config_usage, builtin_config_options);
483 if (given_config_source.file &&
484 !strcmp(given_config_source.file, "-")) {
485 given_config_source.file = NULL;
486 given_config_source.use_stdin = 1;
489 if (use_global_config) {
490 char *user_config = expand_user_path("~/.gitconfig");
491 char *xdg_config = xdg_config_home("config");
495 * It is unknown if HOME/.gitconfig exists, so
496 * we do not know if we should write to XDG
497 * location; error out even if XDG_CONFIG_HOME
498 * is set and points at a sane location.
500 die("$HOME not set");
502 if (access_or_warn(user_config, R_OK, 0) &&
503 xdg_config && !access_or_warn(xdg_config, R_OK, 0))
504 given_config_source.file = xdg_config;
506 given_config_source.file = user_config;
508 else if (use_system_config)
509 given_config_source.file = git_etc_gitconfig();
510 else if (use_local_config)
511 given_config_source.file = git_pathdup("config");
512 else if (given_config_source.file) {
513 if (!is_absolute_path(given_config_source.file) && prefix)
514 given_config_source.file =
515 xstrdup(prefix_filename(prefix,
517 given_config_source.file));
520 if (respect_includes == -1)
521 respect_includes = !given_config_source.file;
529 if (HAS_MULTI_BITS(types)) {
530 error("only one type at a time.");
531 usage_with_options(builtin_config_usage, builtin_config_options);
534 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
535 error("--get-color and variable type are incoherent");
536 usage_with_options(builtin_config_usage, builtin_config_options);
539 if (HAS_MULTI_BITS(actions)) {
540 error("only one action at a time.");
541 usage_with_options(builtin_config_usage, builtin_config_options);
545 case 1: actions = ACTION_GET; break;
546 case 2: actions = ACTION_SET; break;
547 case 3: actions = ACTION_SET_ALL; break;
549 usage_with_options(builtin_config_usage, builtin_config_options);
552 if (actions == ACTION_LIST) {
553 check_argc(argc, 0, 0);
554 if (git_config_with_options(show_all_config, NULL,
555 &given_config_source,
556 respect_includes) < 0) {
557 if (given_config_source.file)
558 die_errno("unable to read config file '%s'",
559 given_config_source.file);
561 die("error processing config file(s)");
564 else if (actions == ACTION_EDIT) {
567 check_argc(argc, 0, 0);
568 if (!given_config_source.file && nongit)
569 die("not in a git directory");
570 if (given_config_source.use_stdin)
571 die("editing stdin is not supported");
572 if (given_config_source.blob)
573 die("editing blobs is not supported");
574 git_config(git_default_config, NULL);
575 config_file = xstrdup(given_config_source.file ?
576 given_config_source.file : git_path("config"));
577 if (use_global_config) {
578 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
580 char *content = default_user_config();
581 write_str_in_full(fd, content);
585 else if (errno != EEXIST)
586 die_errno(_("cannot create configuration file %s"), config_file);
588 launch_editor(config_file, NULL, NULL);
591 else if (actions == ACTION_SET) {
594 check_argc(argc, 2, 2);
595 value = normalize_value(argv[0], argv[1]);
596 ret = git_config_set_in_file(given_config_source.file, argv[0], value);
597 if (ret == CONFIG_NOTHING_SET)
598 error("cannot overwrite multiple values with a single value\n"
599 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
602 else if (actions == ACTION_SET_ALL) {
604 check_argc(argc, 2, 3);
605 value = normalize_value(argv[0], argv[1]);
606 return git_config_set_multivar_in_file(given_config_source.file,
607 argv[0], value, argv[2], 0);
609 else if (actions == ACTION_ADD) {
611 check_argc(argc, 2, 2);
612 value = normalize_value(argv[0], argv[1]);
613 return git_config_set_multivar_in_file(given_config_source.file,
615 CONFIG_REGEX_NONE, 0);
617 else if (actions == ACTION_REPLACE_ALL) {
619 check_argc(argc, 2, 3);
620 value = normalize_value(argv[0], argv[1]);
621 return git_config_set_multivar_in_file(given_config_source.file,
622 argv[0], value, argv[2], 1);
624 else if (actions == ACTION_GET) {
625 check_argc(argc, 1, 2);
626 return get_value(argv[0], argv[1]);
628 else if (actions == ACTION_GET_ALL) {
630 check_argc(argc, 1, 2);
631 return get_value(argv[0], argv[1]);
633 else if (actions == ACTION_GET_REGEXP) {
637 check_argc(argc, 1, 2);
638 return get_value(argv[0], argv[1]);
640 else if (actions == ACTION_GET_URLMATCH) {
641 check_argc(argc, 2, 2);
642 return get_urlmatch(argv[0], argv[1]);
644 else if (actions == ACTION_UNSET) {
646 check_argc(argc, 1, 2);
648 return git_config_set_multivar_in_file(given_config_source.file,
649 argv[0], NULL, argv[1], 0);
651 return git_config_set_in_file(given_config_source.file,
654 else if (actions == ACTION_UNSET_ALL) {
656 check_argc(argc, 1, 2);
657 return git_config_set_multivar_in_file(given_config_source.file,
658 argv[0], NULL, argv[1], 1);
660 else if (actions == ACTION_RENAME_SECTION) {
663 check_argc(argc, 2, 2);
664 ret = git_config_rename_section_in_file(given_config_source.file,
669 die("No such section!");
671 else if (actions == ACTION_REMOVE_SECTION) {
674 check_argc(argc, 1, 1);
675 ret = git_config_rename_section_in_file(given_config_source.file,
680 die("No such section!");
682 else if (actions == ACTION_GET_COLOR) {
683 check_argc(argc, 1, 2);
684 get_color(argv[0], argv[1]);
686 else if (actions == ACTION_GET_COLORBOOL) {
687 check_argc(argc, 1, 2);
689 color_stdout_is_tty = git_config_bool("command line", argv[1]);
690 return get_colorbool(argv[0], argc == 2);