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 const char *given_config_file;
25 static int actions, types;
26 static const char *get_color_slot, *get_colorbool_slot;
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_BOOLEAN(0, "global", &use_global_config, N_("use global config file")),
55 OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")),
56 OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")),
57 OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
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_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
72 OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
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_BOOLEAN('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, "%d", git_config_int(key_, value_ ? value_ : ""));
121 else if (types == TYPE_BOOL)
122 vptr = git_config_bool(key_, value_) ? "true" : "false";
123 else if (types == TYPE_BOOL_OR_INT) {
125 v = git_config_bool_or_int(key_, value_, &is_bool);
127 vptr = v ? "true" : "false";
129 sprintf(value, "%d", v);
130 } else if (types == TYPE_PATH) {
131 if (git_config_pathname(&vptr, key_, value_) < 0)
137 /* Just show the key name */
139 must_print_delim = 0;
142 if (must_print_delim)
143 strbuf_addch(buf, key_delim);
144 strbuf_addstr(buf, vptr);
145 strbuf_addch(buf, term);
152 static int collect_config(const char *key_, const char *value_, void *cb)
154 struct strbuf_list *values = cb;
156 if (!use_key_regexp && strcmp(key_, key))
158 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
160 if (regexp != NULL &&
161 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
164 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
166 return format_config(&values->items[values->nr++], key_, value_);
169 static int get_value(const char *key_, const char *regex_)
171 int ret = CONFIG_GENERIC_ERROR;
172 struct strbuf_list values = {NULL};
175 if (use_key_regexp) {
179 * NEEDSWORK: this naive pattern lowercasing obviously does not
180 * work for more complex patterns like "^[^.]*Foo.*bar".
181 * Perhaps we should deprecate this altogether someday.
185 for (tl = key + strlen(key) - 1;
186 tl >= key && *tl != '.';
189 for (tl = key; *tl && *tl != '.'; tl++)
192 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
193 if (regcomp(key_regexp, key, REG_EXTENDED)) {
194 fprintf(stderr, "Invalid key pattern: %s\n", key_);
197 ret = CONFIG_INVALID_PATTERN;
201 if (git_config_parse_key(key_, &key, NULL)) {
202 ret = CONFIG_INVALID_KEY;
208 if (regex_[0] == '!') {
213 regexp = (regex_t*)xmalloc(sizeof(regex_t));
214 if (regcomp(regexp, regex_, REG_EXTENDED)) {
215 fprintf(stderr, "Invalid pattern: %s\n", regex_);
218 ret = CONFIG_INVALID_PATTERN;
223 git_config_with_options(collect_config, &values,
224 given_config_file, respect_includes);
228 for (i = 0; i < values.nr; i++) {
229 struct strbuf *buf = values.items + i;
230 if (do_all || i == values.nr - 1)
231 fwrite(buf->buf, 1, buf->len, stdout);
250 static char *normalize_value(const char *key, const char *value)
257 if (types == 0 || types == TYPE_PATH)
259 * We don't do normalization for TYPE_PATH here: If
260 * the path is like ~/foobar/, we prefer to store
261 * "~/foobar/" in the config file, and to expand the ~
262 * when retrieving the value.
264 normalized = xstrdup(value);
266 normalized = xmalloc(64);
267 if (types == TYPE_INT) {
268 int v = git_config_int(key, value);
269 sprintf(normalized, "%d", v);
271 else if (types == TYPE_BOOL)
272 sprintf(normalized, "%s",
273 git_config_bool(key, value) ? "true" : "false");
274 else if (types == TYPE_BOOL_OR_INT) {
276 v = git_config_bool_or_int(key, value, &is_bool);
278 sprintf(normalized, "%d", v);
280 sprintf(normalized, "%s", v ? "true" : "false");
287 static int get_color_found;
288 static const char *get_color_slot;
289 static const char *get_colorbool_slot;
290 static char parsed_color[COLOR_MAXLEN];
292 static int git_get_color_config(const char *var, const char *value, void *cb)
294 if (!strcmp(var, get_color_slot)) {
296 config_error_nonbool(var);
297 color_parse(value, var, parsed_color);
303 static void get_color(const char *def_color)
306 parsed_color[0] = '\0';
307 git_config_with_options(git_get_color_config, NULL,
308 given_config_file, respect_includes);
310 if (!get_color_found && def_color)
311 color_parse(def_color, "command line", parsed_color);
313 fputs(parsed_color, stdout);
316 static int get_colorbool_found;
317 static int get_diff_color_found;
318 static int get_color_ui_found;
319 static int git_get_colorbool_config(const char *var, const char *value,
322 if (!strcmp(var, get_colorbool_slot))
323 get_colorbool_found = git_config_colorbool(var, value);
324 else if (!strcmp(var, "diff.color"))
325 get_diff_color_found = git_config_colorbool(var, value);
326 else if (!strcmp(var, "color.ui"))
327 get_color_ui_found = git_config_colorbool(var, value);
331 static int get_colorbool(int print)
333 get_colorbool_found = -1;
334 get_diff_color_found = -1;
335 git_config_with_options(git_get_colorbool_config, NULL,
336 given_config_file, respect_includes);
338 if (get_colorbool_found < 0) {
339 if (!strcmp(get_colorbool_slot, "color.diff"))
340 get_colorbool_found = get_diff_color_found;
341 if (get_colorbool_found < 0)
342 get_colorbool_found = get_color_ui_found;
345 get_colorbool_found = want_color(get_colorbool_found);
348 printf("%s\n", get_colorbool_found ? "true" : "false");
351 return get_colorbool_found ? 0 : 1;
354 struct urlmatch_current_candidate_value {
359 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
361 struct string_list *values = cb;
362 struct string_list_item *item = string_list_insert(values, var);
363 struct urlmatch_current_candidate_value *matched = item->util;
366 matched = xmalloc(sizeof(*matched));
367 strbuf_init(&matched->value, 0);
368 item->util = matched;
370 strbuf_reset(&matched->value);
374 strbuf_addstr(&matched->value, value);
375 matched->value_is_null = 0;
377 matched->value_is_null = 1;
382 static char *dup_downcase(const char *string)
387 len = strlen(string);
388 result = xmalloc(len + 1);
389 for (i = 0; i < len; i++)
390 result[i] = tolower(string[i]);
395 static int get_urlmatch(const char *var, const char *url)
398 struct string_list_item *item;
399 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
400 struct string_list values = STRING_LIST_INIT_DUP;
402 config.collect_fn = urlmatch_collect_fn;
403 config.cascade_fn = NULL;
406 if (!url_normalize(url, &config.url))
407 die("%s", config.url.err);
409 config.section = dup_downcase(var);
410 section_tail = strchr(config.section, '.');
412 *section_tail = '\0';
413 config.key = section_tail + 1;
420 git_config_with_options(urlmatch_config_entry, &config,
421 given_config_file, respect_includes);
423 for_each_string_list_item(item, &values) {
424 struct urlmatch_current_candidate_value *matched = item->util;
425 struct strbuf key = STRBUF_INIT;
426 struct strbuf buf = STRBUF_INIT;
428 strbuf_addstr(&key, item->string);
429 format_config(&buf, key.buf,
430 matched->value_is_null ? NULL : matched->value.buf);
431 fwrite(buf.buf, 1, buf.len, stdout);
432 strbuf_release(&key);
433 strbuf_release(&buf);
435 strbuf_release(&matched->value);
437 string_list_clear(&config.vars, 1);
438 string_list_clear(&values, 1);
439 free(config.url.url);
441 free((void *)config.section);
445 int cmd_config(int argc, const char **argv, const char *prefix)
447 int nongit = !startup_info->have_repository;
450 given_config_file = getenv(CONFIG_ENVIRONMENT);
452 argc = parse_options(argc, argv, prefix, builtin_config_options,
453 builtin_config_usage,
454 PARSE_OPT_STOP_AT_NON_OPTION);
456 if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
457 error("only one config file at a time.");
458 usage_with_options(builtin_config_usage, builtin_config_options);
461 if (use_global_config) {
462 char *user_config = NULL;
463 char *xdg_config = NULL;
465 home_config_paths(&user_config, &xdg_config, "config");
469 * It is unknown if HOME/.gitconfig exists, so
470 * we do not know if we should write to XDG
471 * location; error out even if XDG_CONFIG_HOME
472 * is set and points at a sane location.
474 die("$HOME not set");
476 if (access_or_warn(user_config, R_OK) &&
477 xdg_config && !access_or_warn(xdg_config, R_OK))
478 given_config_file = xdg_config;
480 given_config_file = user_config;
482 else if (use_system_config)
483 given_config_file = git_etc_gitconfig();
484 else if (use_local_config)
485 given_config_file = git_pathdup("config");
486 else if (given_config_file) {
487 if (!is_absolute_path(given_config_file) && prefix)
489 xstrdup(prefix_filename(prefix,
494 if (respect_includes == -1)
495 respect_includes = !given_config_file;
503 if (HAS_MULTI_BITS(types)) {
504 error("only one type at a time.");
505 usage_with_options(builtin_config_usage, builtin_config_options);
509 actions |= ACTION_GET_COLOR;
510 if (get_colorbool_slot)
511 actions |= ACTION_GET_COLORBOOL;
513 if ((get_color_slot || get_colorbool_slot) && types) {
514 error("--get-color and variable type are incoherent");
515 usage_with_options(builtin_config_usage, builtin_config_options);
518 if (HAS_MULTI_BITS(actions)) {
519 error("only one action at a time.");
520 usage_with_options(builtin_config_usage, builtin_config_options);
524 case 1: actions = ACTION_GET; break;
525 case 2: actions = ACTION_SET; break;
526 case 3: actions = ACTION_SET_ALL; break;
528 usage_with_options(builtin_config_usage, builtin_config_options);
531 if (actions == ACTION_LIST) {
532 check_argc(argc, 0, 0);
533 if (git_config_with_options(show_all_config, NULL,
535 respect_includes) < 0) {
536 if (given_config_file)
537 die_errno("unable to read config file '%s'",
540 die("error processing config file(s)");
543 else if (actions == ACTION_EDIT) {
544 check_argc(argc, 0, 0);
545 if (!given_config_file && nongit)
546 die("not in a git directory");
547 git_config(git_default_config, NULL);
548 launch_editor(given_config_file ?
549 given_config_file : git_path("config"),
552 else if (actions == ACTION_SET) {
554 check_argc(argc, 2, 2);
555 value = normalize_value(argv[0], argv[1]);
556 ret = git_config_set_in_file(given_config_file, argv[0], value);
557 if (ret == CONFIG_NOTHING_SET)
558 error("cannot overwrite multiple values with a single value\n"
559 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
562 else if (actions == ACTION_SET_ALL) {
563 check_argc(argc, 2, 3);
564 value = normalize_value(argv[0], argv[1]);
565 return git_config_set_multivar_in_file(given_config_file,
566 argv[0], value, argv[2], 0);
568 else if (actions == ACTION_ADD) {
569 check_argc(argc, 2, 2);
570 value = normalize_value(argv[0], argv[1]);
571 return git_config_set_multivar_in_file(given_config_file,
572 argv[0], value, "^$", 0);
574 else if (actions == ACTION_REPLACE_ALL) {
575 check_argc(argc, 2, 3);
576 value = normalize_value(argv[0], argv[1]);
577 return git_config_set_multivar_in_file(given_config_file,
578 argv[0], value, argv[2], 1);
580 else if (actions == ACTION_GET) {
581 check_argc(argc, 1, 2);
582 return get_value(argv[0], argv[1]);
584 else if (actions == ACTION_GET_ALL) {
586 check_argc(argc, 1, 2);
587 return get_value(argv[0], argv[1]);
589 else if (actions == ACTION_GET_REGEXP) {
593 check_argc(argc, 1, 2);
594 return get_value(argv[0], argv[1]);
596 else if (actions == ACTION_GET_URLMATCH) {
597 check_argc(argc, 2, 2);
598 return get_urlmatch(argv[0], argv[1]);
600 else if (actions == ACTION_UNSET) {
601 check_argc(argc, 1, 2);
603 return git_config_set_multivar_in_file(given_config_file,
604 argv[0], NULL, argv[1], 0);
606 return git_config_set_in_file(given_config_file,
609 else if (actions == ACTION_UNSET_ALL) {
610 check_argc(argc, 1, 2);
611 return git_config_set_multivar_in_file(given_config_file,
612 argv[0], NULL, argv[1], 1);
614 else if (actions == ACTION_RENAME_SECTION) {
616 check_argc(argc, 2, 2);
617 ret = git_config_rename_section_in_file(given_config_file,
622 die("No such section!");
624 else if (actions == ACTION_REMOVE_SECTION) {
626 check_argc(argc, 1, 1);
627 ret = git_config_rename_section_in_file(given_config_file,
632 die("No such section!");
634 else if (actions == ACTION_GET_COLOR) {
637 else if (actions == ACTION_GET_COLORBOOL) {
639 color_stdout_is_tty = git_config_bool("command line", argv[0]);
640 return get_colorbool(argc != 0);
646 int cmd_repo_config(int argc, const char **argv, const char *prefix)
648 fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
649 return cmd_config(argc, argv, prefix);