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