1#include "builtin.h"
2#include "cache.h"
3#include "color.h"
4#include "parse-options.h"
5
6static const char *const builtin_config_usage[] = {
7 N_("git config [options]"),
8 NULL
9};
10
11static char *key;
12static regex_t *key_regexp;
13static regex_t *regexp;
14static int show_keys;
15static int use_key_regexp;
16static int do_all;
17static int do_not_match;
18static char delim = '=';
19static char key_delim = ' ';
20static char term = '\n';
21
22static int use_global_config, use_system_config, use_local_config;
23static const char *given_config_file;
24static int actions, types;
25static const char *get_color_slot, *get_colorbool_slot;
26static int end_null;
27static int respect_includes = -1;
28
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
45#define TYPE_BOOL (1<<0)
46#define TYPE_INT (1<<1)
47#define TYPE_BOOL_OR_INT (1<<2)
48#define TYPE_PATH (1<<3)
49
50static struct option builtin_config_options[] = {
51 OPT_GROUP(N_("Config file location")),
52 OPT_BOOLEAN(0, "global", &use_global_config, N_("use global config file")),
53 OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")),
54 OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")),
55 OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
56 OPT_GROUP(N_("Action")),
57 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
58 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
59 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
60 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
61 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
62 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
63 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
64 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
65 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
66 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
67 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
68 OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
69 OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
70 OPT_GROUP(N_("Type")),
71 OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
72 OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
73 OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
74 OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
75 OPT_GROUP(N_("Other")),
76 OPT_BOOLEAN('z', "null", &end_null, N_("terminate values with NUL byte")),
77 OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
78 OPT_END(),
79};
80
81static void check_argc(int argc, int min, int max) {
82 if (argc >= min && argc <= max)
83 return;
84 error("wrong number of arguments");
85 usage_with_options(builtin_config_usage, builtin_config_options);
86}
87
88static int show_all_config(const char *key_, const char *value_, void *cb)
89{
90 if (value_)
91 printf("%s%c%s%c", key_, delim, value_, term);
92 else
93 printf("%s%c", key_, term);
94 return 0;
95}
96
97struct strbuf_list {
98 struct strbuf *items;
99 int nr;
100 int alloc;
101};
102
103static int collect_config(const char *key_, const char *value_, void *cb)
104{
105 struct strbuf_list *values = cb;
106 struct strbuf *buf;
107 char value[256];
108 const char *vptr = value;
109 int must_free_vptr = 0;
110 int dup_error = 0;
111 int must_print_delim = 0;
112
113 if (!use_key_regexp && strcmp(key_, key))
114 return 0;
115 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
116 return 0;
117 if (regexp != NULL &&
118 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
119 return 0;
120
121 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
122 buf = &values->items[values->nr++];
123 strbuf_init(buf, 0);
124
125 if (show_keys) {
126 strbuf_addstr(buf, key_);
127 must_print_delim = 1;
128 }
129 if (values->nr > 1 && !do_all)
130 dup_error = 1;
131 if (types == TYPE_INT)
132 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
133 else if (types == TYPE_BOOL)
134 vptr = git_config_bool(key_, value_) ? "true" : "false";
135 else if (types == TYPE_BOOL_OR_INT) {
136 int is_bool, v;
137 v = git_config_bool_or_int(key_, value_, &is_bool);
138 if (is_bool)
139 vptr = v ? "true" : "false";
140 else
141 sprintf(value, "%d", v);
142 } else if (types == TYPE_PATH) {
143 git_config_pathname(&vptr, key_, value_);
144 must_free_vptr = 1;
145 } else if (value_) {
146 vptr = value_;
147 } else {
148 /* Just show the key name */
149 vptr = "";
150 must_print_delim = 0;
151 }
152 if (dup_error) {
153 error("More than one value for the key %s: %s",
154 key_, vptr);
155 }
156 else {
157 if (must_print_delim)
158 strbuf_addch(buf, key_delim);
159 strbuf_addstr(buf, vptr);
160 strbuf_addch(buf, term);
161 }
162 if (must_free_vptr)
163 /* If vptr must be freed, it's a pointer to a
164 * dynamically allocated buffer, it's safe to cast to
165 * const.
166 */
167 free((char *)vptr);
168
169 return 0;
170}
171
172static int get_value(const char *key_, const char *regex_)
173{
174 int ret = CONFIG_GENERIC_ERROR;
175 char *global = NULL, *xdg = NULL, *repo_config = NULL;
176 const char *system_wide = NULL, *local;
177 struct config_include_data inc = CONFIG_INCLUDE_INIT;
178 config_fn_t fn;
179 void *data;
180 struct strbuf_list values = {0};
181 int i;
182
183 local = given_config_file;
184 if (!local) {
185 local = repo_config = git_pathdup("config");
186 if (git_config_system())
187 system_wide = git_etc_gitconfig();
188 home_config_paths(&global, &xdg, "config");
189 }
190
191 if (use_key_regexp) {
192 char *tl;
193
194 /*
195 * NEEDSWORK: this naive pattern lowercasing obviously does not
196 * work for more complex patterns like "^[^.]*Foo.*bar".
197 * Perhaps we should deprecate this altogether someday.
198 */
199
200 key = xstrdup(key_);
201 for (tl = key + strlen(key) - 1;
202 tl >= key && *tl != '.';
203 tl--)
204 *tl = tolower(*tl);
205 for (tl = key; *tl && *tl != '.'; tl++)
206 *tl = tolower(*tl);
207
208 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
209 if (regcomp(key_regexp, key, REG_EXTENDED)) {
210 fprintf(stderr, "Invalid key pattern: %s\n", key_);
211 free(key_regexp);
212 key_regexp = NULL;
213 ret = CONFIG_INVALID_PATTERN;
214 goto free_strings;
215 }
216 } else {
217 if (git_config_parse_key(key_, &key, NULL)) {
218 ret = CONFIG_INVALID_KEY;
219 goto free_strings;
220 }
221 }
222
223 if (regex_) {
224 if (regex_[0] == '!') {
225 do_not_match = 1;
226 regex_++;
227 }
228
229 regexp = (regex_t*)xmalloc(sizeof(regex_t));
230 if (regcomp(regexp, regex_, REG_EXTENDED)) {
231 fprintf(stderr, "Invalid pattern: %s\n", regex_);
232 free(regexp);
233 regexp = NULL;
234 ret = CONFIG_INVALID_PATTERN;
235 goto free_strings;
236 }
237 }
238
239 fn = collect_config;
240 data = &values;
241 if (respect_includes) {
242 inc.fn = fn;
243 inc.data = data;
244 fn = git_config_include;
245 data = &inc;
246 }
247
248 if (do_all && system_wide)
249 git_config_from_file(fn, system_wide, data);
250 if (do_all && xdg)
251 git_config_from_file(fn, xdg, data);
252 if (do_all && global)
253 git_config_from_file(fn, global, data);
254 if (do_all)
255 git_config_from_file(fn, local, data);
256 git_config_from_parameters(fn, data);
257 if (!do_all && !values.nr)
258 git_config_from_file(fn, local, data);
259 if (!do_all && !values.nr && global)
260 git_config_from_file(fn, global, data);
261 if (!do_all && !values.nr && xdg)
262 git_config_from_file(fn, xdg, data);
263 if (!do_all && !values.nr && system_wide)
264 git_config_from_file(fn, system_wide, data);
265
266 if (do_all)
267 ret = !values.nr;
268 else
269 ret = (values.nr == 1) ? 0 : values.nr > 1 ? 2 : 1;
270
271 for (i = 0; i < values.nr; i++) {
272 struct strbuf *buf = values.items + i;
273 fwrite(buf->buf, 1, buf->len, stdout);
274 strbuf_release(buf);
275 }
276 free(values.items);
277
278free_strings:
279 free(repo_config);
280 free(global);
281 free(xdg);
282 free(key);
283 if (key_regexp) {
284 regfree(key_regexp);
285 free(key_regexp);
286 }
287 if (regexp) {
288 regfree(regexp);
289 free(regexp);
290 }
291
292 return ret;
293}
294
295static char *normalize_value(const char *key, const char *value)
296{
297 char *normalized;
298
299 if (!value)
300 return NULL;
301
302 if (types == 0 || types == TYPE_PATH)
303 /*
304 * We don't do normalization for TYPE_PATH here: If
305 * the path is like ~/foobar/, we prefer to store
306 * "~/foobar/" in the config file, and to expand the ~
307 * when retrieving the value.
308 */
309 normalized = xstrdup(value);
310 else {
311 normalized = xmalloc(64);
312 if (types == TYPE_INT) {
313 int v = git_config_int(key, value);
314 sprintf(normalized, "%d", v);
315 }
316 else if (types == TYPE_BOOL)
317 sprintf(normalized, "%s",
318 git_config_bool(key, value) ? "true" : "false");
319 else if (types == TYPE_BOOL_OR_INT) {
320 int is_bool, v;
321 v = git_config_bool_or_int(key, value, &is_bool);
322 if (!is_bool)
323 sprintf(normalized, "%d", v);
324 else
325 sprintf(normalized, "%s", v ? "true" : "false");
326 }
327 }
328
329 return normalized;
330}
331
332static int get_color_found;
333static const char *get_color_slot;
334static const char *get_colorbool_slot;
335static char parsed_color[COLOR_MAXLEN];
336
337static int git_get_color_config(const char *var, const char *value, void *cb)
338{
339 if (!strcmp(var, get_color_slot)) {
340 if (!value)
341 config_error_nonbool(var);
342 color_parse(value, var, parsed_color);
343 get_color_found = 1;
344 }
345 return 0;
346}
347
348static void get_color(const char *def_color)
349{
350 get_color_found = 0;
351 parsed_color[0] = '\0';
352 git_config_with_options(git_get_color_config, NULL,
353 given_config_file, respect_includes);
354
355 if (!get_color_found && def_color)
356 color_parse(def_color, "command line", parsed_color);
357
358 fputs(parsed_color, stdout);
359}
360
361static int get_colorbool_found;
362static int get_diff_color_found;
363static int get_color_ui_found;
364static int git_get_colorbool_config(const char *var, const char *value,
365 void *cb)
366{
367 if (!strcmp(var, get_colorbool_slot))
368 get_colorbool_found = git_config_colorbool(var, value);
369 else if (!strcmp(var, "diff.color"))
370 get_diff_color_found = git_config_colorbool(var, value);
371 else if (!strcmp(var, "color.ui"))
372 get_color_ui_found = git_config_colorbool(var, value);
373 return 0;
374}
375
376static int get_colorbool(int print)
377{
378 get_colorbool_found = -1;
379 get_diff_color_found = -1;
380 git_config_with_options(git_get_colorbool_config, NULL,
381 given_config_file, respect_includes);
382
383 if (get_colorbool_found < 0) {
384 if (!strcmp(get_colorbool_slot, "color.diff"))
385 get_colorbool_found = get_diff_color_found;
386 if (get_colorbool_found < 0)
387 get_colorbool_found = get_color_ui_found;
388 }
389
390 get_colorbool_found = want_color(get_colorbool_found);
391
392 if (print) {
393 printf("%s\n", get_colorbool_found ? "true" : "false");
394 return 0;
395 } else
396 return get_colorbool_found ? 0 : 1;
397}
398
399int cmd_config(int argc, const char **argv, const char *prefix)
400{
401 int nongit = !startup_info->have_repository;
402 char *value;
403
404 given_config_file = getenv(CONFIG_ENVIRONMENT);
405
406 argc = parse_options(argc, argv, prefix, builtin_config_options,
407 builtin_config_usage,
408 PARSE_OPT_STOP_AT_NON_OPTION);
409
410 if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
411 error("only one config file at a time.");
412 usage_with_options(builtin_config_usage, builtin_config_options);
413 }
414
415 if (use_global_config) {
416 char *user_config = NULL;
417 char *xdg_config = NULL;
418
419 home_config_paths(&user_config, &xdg_config, "config");
420
421 if (!user_config)
422 /*
423 * It is unknown if HOME/.gitconfig exists, so
424 * we do not know if we should write to XDG
425 * location; error out even if XDG_CONFIG_HOME
426 * is set and points at a sane location.
427 */
428 die("$HOME not set");
429
430 if (access_or_warn(user_config, R_OK) &&
431 xdg_config && !access_or_warn(xdg_config, R_OK))
432 given_config_file = xdg_config;
433 else
434 given_config_file = user_config;
435 }
436 else if (use_system_config)
437 given_config_file = git_etc_gitconfig();
438 else if (use_local_config)
439 given_config_file = git_pathdup("config");
440 else if (given_config_file) {
441 if (!is_absolute_path(given_config_file) && prefix)
442 given_config_file =
443 xstrdup(prefix_filename(prefix,
444 strlen(prefix),
445 given_config_file));
446 }
447
448 if (respect_includes == -1)
449 respect_includes = !given_config_file;
450
451 if (end_null) {
452 term = '\0';
453 delim = '\n';
454 key_delim = '\n';
455 }
456
457 if (HAS_MULTI_BITS(types)) {
458 error("only one type at a time.");
459 usage_with_options(builtin_config_usage, builtin_config_options);
460 }
461
462 if (get_color_slot)
463 actions |= ACTION_GET_COLOR;
464 if (get_colorbool_slot)
465 actions |= ACTION_GET_COLORBOOL;
466
467 if ((get_color_slot || get_colorbool_slot) && types) {
468 error("--get-color and variable type are incoherent");
469 usage_with_options(builtin_config_usage, builtin_config_options);
470 }
471
472 if (HAS_MULTI_BITS(actions)) {
473 error("only one action at a time.");
474 usage_with_options(builtin_config_usage, builtin_config_options);
475 }
476 if (actions == 0)
477 switch (argc) {
478 case 1: actions = ACTION_GET; break;
479 case 2: actions = ACTION_SET; break;
480 case 3: actions = ACTION_SET_ALL; break;
481 default:
482 usage_with_options(builtin_config_usage, builtin_config_options);
483 }
484
485 if (actions == ACTION_LIST) {
486 check_argc(argc, 0, 0);
487 if (git_config_with_options(show_all_config, NULL,
488 given_config_file,
489 respect_includes) < 0) {
490 if (given_config_file)
491 die_errno("unable to read config file '%s'",
492 given_config_file);
493 else
494 die("error processing config file(s)");
495 }
496 }
497 else if (actions == ACTION_EDIT) {
498 check_argc(argc, 0, 0);
499 if (!given_config_file && nongit)
500 die("not in a git directory");
501 git_config(git_default_config, NULL);
502 launch_editor(given_config_file ?
503 given_config_file : git_path("config"),
504 NULL, NULL);
505 }
506 else if (actions == ACTION_SET) {
507 int ret;
508 check_argc(argc, 2, 2);
509 value = normalize_value(argv[0], argv[1]);
510 ret = git_config_set_in_file(given_config_file, argv[0], value);
511 if (ret == CONFIG_NOTHING_SET)
512 error("cannot overwrite multiple values with a single value\n"
513 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
514 return ret;
515 }
516 else if (actions == ACTION_SET_ALL) {
517 check_argc(argc, 2, 3);
518 value = normalize_value(argv[0], argv[1]);
519 return git_config_set_multivar_in_file(given_config_file,
520 argv[0], value, argv[2], 0);
521 }
522 else if (actions == ACTION_ADD) {
523 check_argc(argc, 2, 2);
524 value = normalize_value(argv[0], argv[1]);
525 return git_config_set_multivar_in_file(given_config_file,
526 argv[0], value, "^$", 0);
527 }
528 else if (actions == ACTION_REPLACE_ALL) {
529 check_argc(argc, 2, 3);
530 value = normalize_value(argv[0], argv[1]);
531 return git_config_set_multivar_in_file(given_config_file,
532 argv[0], value, argv[2], 1);
533 }
534 else if (actions == ACTION_GET) {
535 check_argc(argc, 1, 2);
536 return get_value(argv[0], argv[1]);
537 }
538 else if (actions == ACTION_GET_ALL) {
539 do_all = 1;
540 check_argc(argc, 1, 2);
541 return get_value(argv[0], argv[1]);
542 }
543 else if (actions == ACTION_GET_REGEXP) {
544 show_keys = 1;
545 use_key_regexp = 1;
546 do_all = 1;
547 check_argc(argc, 1, 2);
548 return get_value(argv[0], argv[1]);
549 }
550 else if (actions == ACTION_UNSET) {
551 check_argc(argc, 1, 2);
552 if (argc == 2)
553 return git_config_set_multivar_in_file(given_config_file,
554 argv[0], NULL, argv[1], 0);
555 else
556 return git_config_set_in_file(given_config_file,
557 argv[0], NULL);
558 }
559 else if (actions == ACTION_UNSET_ALL) {
560 check_argc(argc, 1, 2);
561 return git_config_set_multivar_in_file(given_config_file,
562 argv[0], NULL, argv[1], 1);
563 }
564 else if (actions == ACTION_RENAME_SECTION) {
565 int ret;
566 check_argc(argc, 2, 2);
567 ret = git_config_rename_section_in_file(given_config_file,
568 argv[0], argv[1]);
569 if (ret < 0)
570 return ret;
571 if (ret == 0)
572 die("No such section!");
573 }
574 else if (actions == ACTION_REMOVE_SECTION) {
575 int ret;
576 check_argc(argc, 1, 1);
577 ret = git_config_rename_section_in_file(given_config_file,
578 argv[0], NULL);
579 if (ret < 0)
580 return ret;
581 if (ret == 0)
582 die("No such section!");
583 }
584 else if (actions == ACTION_GET_COLOR) {
585 get_color(argv[0]);
586 }
587 else if (actions == ACTION_GET_COLORBOOL) {
588 if (argc == 1)
589 color_stdout_is_tty = git_config_bool("command line", argv[0]);
590 return get_colorbool(argc != 0);
591 }
592
593 return 0;
594}
595
596int cmd_repo_config(int argc, const char **argv, const char *prefix)
597{
598 fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
599 return cmd_config(argc, argv, prefix);
600}