1#include "builtin.h"
2#include "cache.h"
3#include "color.h"
4#include "parse-options.h"
5#include "urlmatch.h"
6
7static const char *const builtin_config_usage[] = {
8 N_("git config [<options>]"),
9 NULL
10};
11
12static char *key;
13static regex_t *key_regexp;
14static regex_t *regexp;
15static int show_keys;
16static int omit_values;
17static int use_key_regexp;
18static int do_all;
19static int do_not_match;
20static char delim = '=';
21static char key_delim = ' ';
22static char term = '\n';
23
24static int use_global_config, use_system_config, use_local_config;
25static struct git_config_source given_config_source;
26static int actions, types;
27static const char *get_color_slot, *get_colorbool_slot;
28static int end_null;
29static int respect_includes = -1;
30
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)
47
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)
52
53static 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(),
85};
86
87static 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);
92}
93
94static int show_all_config(const char *key_, const char *value_, void *cb)
95{
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;
101}
102
103struct strbuf_list {
104 struct strbuf *items;
105 int nr;
106 int alloc;
107};
108
109static int format_config(struct strbuf *buf, const char *key_, const char *value_)
110{
111 if (show_keys)
112 strbuf_addstr(buf, key_);
113 if (!omit_values) {
114 if (show_keys)
115 strbuf_addch(buf, key_delim);
116
117 if (types == TYPE_INT)
118 strbuf_addf(buf, "%"PRId64,
119 git_config_int64(key_, value_ ? value_ : ""));
120 else if (types == TYPE_BOOL)
121 strbuf_addstr(buf, git_config_bool(key_, value_) ?
122 "true" : "false");
123 else if (types == TYPE_BOOL_OR_INT) {
124 int is_bool, v;
125 v = git_config_bool_or_int(key_, value_, &is_bool);
126 if (is_bool)
127 strbuf_addstr(buf, v ? "true" : "false");
128 else
129 strbuf_addf(buf, "%d", v);
130 } else if (types == TYPE_PATH) {
131 const char *v;
132 if (git_config_pathname(&v, key_, value_) < 0)
133 return -1;
134 strbuf_addstr(buf, v);
135 free((char *)v);
136 } else if (value_) {
137 strbuf_addstr(buf, value_);
138 } else {
139 /* Just show the key name; back out delimiter */
140 if (show_keys)
141 strbuf_setlen(buf, buf->len - 1);
142 }
143 }
144 strbuf_addch(buf, term);
145 return 0;
146}
147
148static int collect_config(const char *key_, const char *value_, void *cb)
149{
150 struct strbuf_list *values = cb;
151
152 if (!use_key_regexp && strcmp(key_, key))
153 return 0;
154 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
155 return 0;
156 if (regexp != NULL &&
157 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
158 return 0;
159
160 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
161 strbuf_init(&values->items[values->nr], 0);
162
163 return format_config(&values->items[values->nr++], key_, value_);
164}
165
166static int get_value(const char *key_, const char *regex_)
167{
168 int ret = CONFIG_GENERIC_ERROR;
169 struct strbuf_list values = {NULL};
170 int i;
171
172 if (use_key_regexp) {
173 char *tl;
174
175 /*
176 * NEEDSWORK: this naive pattern lowercasing obviously does not
177 * work for more complex patterns like "^[^.]*Foo.*bar".
178 * Perhaps we should deprecate this altogether someday.
179 */
180
181 key = xstrdup(key_);
182 for (tl = key + strlen(key) - 1;
183 tl >= key && *tl != '.';
184 tl--)
185 *tl = tolower(*tl);
186 for (tl = key; *tl && *tl != '.'; tl++)
187 *tl = tolower(*tl);
188
189 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
190 if (regcomp(key_regexp, key, REG_EXTENDED)) {
191 error("invalid key pattern: %s", key_);
192 free(key_regexp);
193 key_regexp = NULL;
194 ret = CONFIG_INVALID_PATTERN;
195 goto free_strings;
196 }
197 } else {
198 if (git_config_parse_key(key_, &key, NULL)) {
199 ret = CONFIG_INVALID_KEY;
200 goto free_strings;
201 }
202 }
203
204 if (regex_) {
205 if (regex_[0] == '!') {
206 do_not_match = 1;
207 regex_++;
208 }
209
210 regexp = (regex_t*)xmalloc(sizeof(regex_t));
211 if (regcomp(regexp, regex_, REG_EXTENDED)) {
212 error("invalid pattern: %s", regex_);
213 free(regexp);
214 regexp = NULL;
215 ret = CONFIG_INVALID_PATTERN;
216 goto free_strings;
217 }
218 }
219
220 git_config_with_options(collect_config, &values,
221 &given_config_source, respect_includes);
222
223 ret = !values.nr;
224
225 for (i = 0; i < values.nr; i++) {
226 struct strbuf *buf = values.items + i;
227 if (do_all || i == values.nr - 1)
228 fwrite(buf->buf, 1, buf->len, stdout);
229 strbuf_release(buf);
230 }
231 free(values.items);
232
233free_strings:
234 free(key);
235 if (key_regexp) {
236 regfree(key_regexp);
237 free(key_regexp);
238 }
239 if (regexp) {
240 regfree(regexp);
241 free(regexp);
242 }
243
244 return ret;
245}
246
247static char *normalize_value(const char *key, const char *value)
248{
249 char *normalized;
250
251 if (!value)
252 return NULL;
253
254 if (types == 0 || types == TYPE_PATH)
255 /*
256 * We don't do normalization for TYPE_PATH here: If
257 * the path is like ~/foobar/, we prefer to store
258 * "~/foobar/" in the config file, and to expand the ~
259 * when retrieving the value.
260 */
261 normalized = xstrdup(value);
262 else {
263 normalized = xmalloc(64);
264 if (types == TYPE_INT) {
265 int64_t v = git_config_int64(key, value);
266 sprintf(normalized, "%"PRId64, v);
267 }
268 else if (types == TYPE_BOOL)
269 sprintf(normalized, "%s",
270 git_config_bool(key, value) ? "true" : "false");
271 else if (types == TYPE_BOOL_OR_INT) {
272 int is_bool, v;
273 v = git_config_bool_or_int(key, value, &is_bool);
274 if (!is_bool)
275 sprintf(normalized, "%d", v);
276 else
277 sprintf(normalized, "%s", v ? "true" : "false");
278 }
279 }
280
281 return normalized;
282}
283
284static int get_color_found;
285static const char *get_color_slot;
286static const char *get_colorbool_slot;
287static char parsed_color[COLOR_MAXLEN];
288
289static int git_get_color_config(const char *var, const char *value, void *cb)
290{
291 if (!strcmp(var, get_color_slot)) {
292 if (!value)
293 config_error_nonbool(var);
294 if (color_parse(value, parsed_color) < 0)
295 return -1;
296 get_color_found = 1;
297 }
298 return 0;
299}
300
301static void get_color(const char *var, const char *def_color)
302{
303 get_color_slot = var;
304 get_color_found = 0;
305 parsed_color[0] = '\0';
306 git_config_with_options(git_get_color_config, NULL,
307 &given_config_source, respect_includes);
308
309 if (!get_color_found && def_color) {
310 if (color_parse(def_color, parsed_color) < 0)
311 die(_("unable to parse default color value"));
312 }
313
314 fputs(parsed_color, stdout);
315}
316
317static int get_colorbool_found;
318static int get_diff_color_found;
319static int get_color_ui_found;
320static int git_get_colorbool_config(const char *var, const char *value,
321 void *cb)
322{
323 if (!strcmp(var, get_colorbool_slot))
324 get_colorbool_found = git_config_colorbool(var, value);
325 else if (!strcmp(var, "diff.color"))
326 get_diff_color_found = git_config_colorbool(var, value);
327 else if (!strcmp(var, "color.ui"))
328 get_color_ui_found = git_config_colorbool(var, value);
329 return 0;
330}
331
332static int get_colorbool(const char *var, int print)
333{
334 get_colorbool_slot = var;
335 get_colorbool_found = -1;
336 get_diff_color_found = -1;
337 get_color_ui_found = -1;
338 git_config_with_options(git_get_colorbool_config, NULL,
339 &given_config_source, respect_includes);
340
341 if (get_colorbool_found < 0) {
342 if (!strcmp(get_colorbool_slot, "color.diff"))
343 get_colorbool_found = get_diff_color_found;
344 if (get_colorbool_found < 0)
345 get_colorbool_found = get_color_ui_found;
346 }
347
348 if (get_colorbool_found < 0)
349 /* default value if none found in config */
350 get_colorbool_found = GIT_COLOR_AUTO;
351
352 get_colorbool_found = want_color(get_colorbool_found);
353
354 if (print) {
355 printf("%s\n", get_colorbool_found ? "true" : "false");
356 return 0;
357 } else
358 return get_colorbool_found ? 0 : 1;
359}
360
361static void check_write(void)
362{
363 if (given_config_source.use_stdin)
364 die("writing to stdin is not supported");
365
366 if (given_config_source.blob)
367 die("writing config blobs is not supported");
368}
369
370struct urlmatch_current_candidate_value {
371 char value_is_null;
372 struct strbuf value;
373};
374
375static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
376{
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;
380
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);
387 }
388
389 if (value) {
390 strbuf_addstr(&matched->value, value);
391 matched->value_is_null = 0;
392 } else {
393 matched->value_is_null = 1;
394 }
395 return 0;
396}
397
398static int get_urlmatch(const char *var, const char *url)
399{
400 char *section_tail;
401 struct string_list_item *item;
402 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
403 struct string_list values = STRING_LIST_INIT_DUP;
404
405 config.collect_fn = urlmatch_collect_fn;
406 config.cascade_fn = NULL;
407 config.cb = &values;
408
409 if (!url_normalize(url, &config.url))
410 die("%s", config.url.err);
411
412 config.section = xstrdup_tolower(var);
413 section_tail = strchr(config.section, '.');
414 if (section_tail) {
415 *section_tail = '\0';
416 config.key = section_tail + 1;
417 show_keys = 0;
418 } else {
419 config.key = NULL;
420 show_keys = 1;
421 }
422
423 git_config_with_options(urlmatch_config_entry, &config,
424 &given_config_source, respect_includes);
425
426 for_each_string_list_item(item, &values) {
427 struct urlmatch_current_candidate_value *matched = item->util;
428 struct strbuf buf = STRBUF_INIT;
429
430 format_config(&buf, item->string,
431 matched->value_is_null ? NULL : matched->value.buf);
432 fwrite(buf.buf, 1, buf.len, stdout);
433 strbuf_release(&buf);
434
435 strbuf_release(&matched->value);
436 }
437 string_list_clear(&config.vars, 1);
438 string_list_clear(&values, 1);
439 free(config.url.url);
440
441 free((void *)config.section);
442 return 0;
443}
444
445static char *default_user_config(void)
446{
447 struct strbuf buf = STRBUF_INIT;
448 strbuf_addf(&buf,
449 _("# This is Git's per-user configuration file.\n"
450 "[user]\n"
451 "# Please adapt and uncomment the following lines:\n"
452 "# name = %s\n"
453 "# email = %s\n"),
454 ident_default_name(),
455 ident_default_email());
456 return strbuf_detach(&buf, NULL);
457}
458
459int cmd_config(int argc, const char **argv, const char *prefix)
460{
461 int nongit = !startup_info->have_repository;
462 char *value;
463
464 given_config_source.file = getenv(CONFIG_ENVIRONMENT);
465
466 argc = parse_options(argc, argv, prefix, builtin_config_options,
467 builtin_config_usage,
468 PARSE_OPT_STOP_AT_NON_OPTION);
469
470 if (use_global_config + use_system_config + use_local_config +
471 !!given_config_source.file + !!given_config_source.blob > 1) {
472 error("only one config file at a time.");
473 usage_with_options(builtin_config_usage, builtin_config_options);
474 }
475
476 if (given_config_source.file &&
477 !strcmp(given_config_source.file, "-")) {
478 given_config_source.file = NULL;
479 given_config_source.use_stdin = 1;
480 }
481
482 if (use_global_config) {
483 char *user_config = expand_user_path("~/.gitconfig");
484 char *xdg_config = xdg_config_home("config");
485
486 if (!user_config)
487 /*
488 * It is unknown if HOME/.gitconfig exists, so
489 * we do not know if we should write to XDG
490 * location; error out even if XDG_CONFIG_HOME
491 * is set and points at a sane location.
492 */
493 die("$HOME not set");
494
495 if (access_or_warn(user_config, R_OK, 0) &&
496 xdg_config && !access_or_warn(xdg_config, R_OK, 0))
497 given_config_source.file = xdg_config;
498 else
499 given_config_source.file = user_config;
500 }
501 else if (use_system_config)
502 given_config_source.file = git_etc_gitconfig();
503 else if (use_local_config)
504 given_config_source.file = git_pathdup("config");
505 else if (given_config_source.file) {
506 if (!is_absolute_path(given_config_source.file) && prefix)
507 given_config_source.file =
508 xstrdup(prefix_filename(prefix,
509 strlen(prefix),
510 given_config_source.file));
511 }
512
513 if (respect_includes == -1)
514 respect_includes = !given_config_source.file;
515
516 if (end_null) {
517 term = '\0';
518 delim = '\n';
519 key_delim = '\n';
520 }
521
522 if (HAS_MULTI_BITS(types)) {
523 error("only one type at a time.");
524 usage_with_options(builtin_config_usage, builtin_config_options);
525 }
526
527 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
528 error("--get-color and variable type are incoherent");
529 usage_with_options(builtin_config_usage, builtin_config_options);
530 }
531
532 if (HAS_MULTI_BITS(actions)) {
533 error("only one action at a time.");
534 usage_with_options(builtin_config_usage, builtin_config_options);
535 }
536 if (actions == 0)
537 switch (argc) {
538 case 1: actions = ACTION_GET; break;
539 case 2: actions = ACTION_SET; break;
540 case 3: actions = ACTION_SET_ALL; break;
541 default:
542 usage_with_options(builtin_config_usage, builtin_config_options);
543 }
544 if (omit_values &&
545 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
546 error("--name-only is only applicable to --list or --get-regexp");
547 usage_with_options(builtin_config_usage, builtin_config_options);
548 }
549 if (actions == ACTION_LIST) {
550 check_argc(argc, 0, 0);
551 if (git_config_with_options(show_all_config, NULL,
552 &given_config_source,
553 respect_includes) < 0) {
554 if (given_config_source.file)
555 die_errno("unable to read config file '%s'",
556 given_config_source.file);
557 else
558 die("error processing config file(s)");
559 }
560 }
561 else if (actions == ACTION_EDIT) {
562 char *config_file;
563
564 check_argc(argc, 0, 0);
565 if (!given_config_source.file && nongit)
566 die("not in a git directory");
567 if (given_config_source.use_stdin)
568 die("editing stdin is not supported");
569 if (given_config_source.blob)
570 die("editing blobs is not supported");
571 git_config(git_default_config, NULL);
572 config_file = xstrdup(given_config_source.file ?
573 given_config_source.file : git_path("config"));
574 if (use_global_config) {
575 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
576 if (fd) {
577 char *content = default_user_config();
578 write_str_in_full(fd, content);
579 free(content);
580 close(fd);
581 }
582 else if (errno != EEXIST)
583 die_errno(_("cannot create configuration file %s"), config_file);
584 }
585 launch_editor(config_file, NULL, NULL);
586 free(config_file);
587 }
588 else if (actions == ACTION_SET) {
589 int ret;
590 check_write();
591 check_argc(argc, 2, 2);
592 value = normalize_value(argv[0], argv[1]);
593 ret = git_config_set_in_file(given_config_source.file, argv[0], value);
594 if (ret == CONFIG_NOTHING_SET)
595 error("cannot overwrite multiple values with a single value\n"
596 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
597 return ret;
598 }
599 else if (actions == ACTION_SET_ALL) {
600 check_write();
601 check_argc(argc, 2, 3);
602 value = normalize_value(argv[0], argv[1]);
603 return git_config_set_multivar_in_file(given_config_source.file,
604 argv[0], value, argv[2], 0);
605 }
606 else if (actions == ACTION_ADD) {
607 check_write();
608 check_argc(argc, 2, 2);
609 value = normalize_value(argv[0], argv[1]);
610 return git_config_set_multivar_in_file(given_config_source.file,
611 argv[0], value,
612 CONFIG_REGEX_NONE, 0);
613 }
614 else if (actions == ACTION_REPLACE_ALL) {
615 check_write();
616 check_argc(argc, 2, 3);
617 value = normalize_value(argv[0], argv[1]);
618 return git_config_set_multivar_in_file(given_config_source.file,
619 argv[0], value, argv[2], 1);
620 }
621 else if (actions == ACTION_GET) {
622 check_argc(argc, 1, 2);
623 return get_value(argv[0], argv[1]);
624 }
625 else if (actions == ACTION_GET_ALL) {
626 do_all = 1;
627 check_argc(argc, 1, 2);
628 return get_value(argv[0], argv[1]);
629 }
630 else if (actions == ACTION_GET_REGEXP) {
631 show_keys = 1;
632 use_key_regexp = 1;
633 do_all = 1;
634 check_argc(argc, 1, 2);
635 return get_value(argv[0], argv[1]);
636 }
637 else if (actions == ACTION_GET_URLMATCH) {
638 check_argc(argc, 2, 2);
639 return get_urlmatch(argv[0], argv[1]);
640 }
641 else if (actions == ACTION_UNSET) {
642 check_write();
643 check_argc(argc, 1, 2);
644 if (argc == 2)
645 return git_config_set_multivar_in_file(given_config_source.file,
646 argv[0], NULL, argv[1], 0);
647 else
648 return git_config_set_in_file(given_config_source.file,
649 argv[0], NULL);
650 }
651 else if (actions == ACTION_UNSET_ALL) {
652 check_write();
653 check_argc(argc, 1, 2);
654 return git_config_set_multivar_in_file(given_config_source.file,
655 argv[0], NULL, argv[1], 1);
656 }
657 else if (actions == ACTION_RENAME_SECTION) {
658 int ret;
659 check_write();
660 check_argc(argc, 2, 2);
661 ret = git_config_rename_section_in_file(given_config_source.file,
662 argv[0], argv[1]);
663 if (ret < 0)
664 return ret;
665 if (ret == 0)
666 die("No such section!");
667 }
668 else if (actions == ACTION_REMOVE_SECTION) {
669 int ret;
670 check_write();
671 check_argc(argc, 1, 1);
672 ret = git_config_rename_section_in_file(given_config_source.file,
673 argv[0], NULL);
674 if (ret < 0)
675 return ret;
676 if (ret == 0)
677 die("No such section!");
678 }
679 else if (actions == ACTION_GET_COLOR) {
680 check_argc(argc, 1, 2);
681 get_color(argv[0], argv[1]);
682 }
683 else if (actions == ACTION_GET_COLORBOOL) {
684 check_argc(argc, 1, 2);
685 if (argc == 2)
686 color_stdout_is_tty = git_config_bool("command line", argv[1]);
687 return get_colorbool(argv[0], argc == 2);
688 }
689
690 return 0;
691}