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