git maintenance: avoid console window in scheduled tasks on Windows
[git.git] / builtin / config.c
blobff2fe8ef1257f71dd1ae777cb75b8ea308fae97a
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "alloc.h"
4 #include "config.h"
5 #include "color.h"
6 #include "editor.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "ident.h"
10 #include "parse-options.h"
11 #include "urlmatch.h"
12 #include "path.h"
13 #include "quote.h"
14 #include "setup.h"
15 #include "worktree.h"
16 #include "wrapper.h"
18 static const char *const builtin_config_usage[] = {
19 N_("git config [<options>]"),
20 NULL
23 static char *key;
24 static regex_t *key_regexp;
25 static const char *value_pattern;
26 static regex_t *regexp;
27 static int show_keys;
28 static int omit_values;
29 static int use_key_regexp;
30 static int do_all;
31 static int do_not_match;
32 static char delim = '=';
33 static char key_delim = ' ';
34 static char term = '\n';
36 static int use_global_config, use_system_config, use_local_config;
37 static int use_worktree_config;
38 static struct git_config_source given_config_source;
39 static int actions, type;
40 static char *default_value;
41 static int end_nul;
42 static int respect_includes_opt = -1;
43 static struct config_options config_options;
44 static int show_origin;
45 static int show_scope;
46 static int fixed_value;
48 #define ACTION_GET (1<<0)
49 #define ACTION_GET_ALL (1<<1)
50 #define ACTION_GET_REGEXP (1<<2)
51 #define ACTION_REPLACE_ALL (1<<3)
52 #define ACTION_ADD (1<<4)
53 #define ACTION_UNSET (1<<5)
54 #define ACTION_UNSET_ALL (1<<6)
55 #define ACTION_RENAME_SECTION (1<<7)
56 #define ACTION_REMOVE_SECTION (1<<8)
57 #define ACTION_LIST (1<<9)
58 #define ACTION_EDIT (1<<10)
59 #define ACTION_SET (1<<11)
60 #define ACTION_SET_ALL (1<<12)
61 #define ACTION_GET_COLOR (1<<13)
62 #define ACTION_GET_COLORBOOL (1<<14)
63 #define ACTION_GET_URLMATCH (1<<15)
66 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
67 * one line of output and which should therefore be paged.
69 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
70 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
72 #define TYPE_BOOL 1
73 #define TYPE_INT 2
74 #define TYPE_BOOL_OR_INT 3
75 #define TYPE_PATH 4
76 #define TYPE_EXPIRY_DATE 5
77 #define TYPE_COLOR 6
78 #define TYPE_BOOL_OR_STR 7
80 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
81 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
82 PARSE_OPT_NONEG, option_parse_type, (i) }
84 static NORETURN void usage_builtin_config(void);
86 static int option_parse_type(const struct option *opt, const char *arg,
87 int unset)
89 int new_type, *to_type;
91 if (unset) {
92 *((int *) opt->value) = 0;
93 return 0;
97 * To support '--<type>' style flags, begin with new_type equal to
98 * opt->defval.
100 new_type = opt->defval;
101 if (!new_type) {
102 if (!strcmp(arg, "bool"))
103 new_type = TYPE_BOOL;
104 else if (!strcmp(arg, "int"))
105 new_type = TYPE_INT;
106 else if (!strcmp(arg, "bool-or-int"))
107 new_type = TYPE_BOOL_OR_INT;
108 else if (!strcmp(arg, "bool-or-str"))
109 new_type = TYPE_BOOL_OR_STR;
110 else if (!strcmp(arg, "path"))
111 new_type = TYPE_PATH;
112 else if (!strcmp(arg, "expiry-date"))
113 new_type = TYPE_EXPIRY_DATE;
114 else if (!strcmp(arg, "color"))
115 new_type = TYPE_COLOR;
116 else
117 die(_("unrecognized --type argument, %s"), arg);
120 to_type = opt->value;
121 if (*to_type && *to_type != new_type) {
123 * Complain when there is a new type not equal to the old type.
124 * This allows for combinations like '--int --type=int' and
125 * '--type=int --type=int', but disallows ones like '--type=bool
126 * --int' and '--type=bool
127 * --type=int'.
129 error(_("only one type at a time"));
130 usage_builtin_config();
132 *to_type = new_type;
134 return 0;
137 static struct option builtin_config_options[] = {
138 OPT_GROUP(N_("Config file location")),
139 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
140 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
141 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
142 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
143 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
144 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
145 OPT_GROUP(N_("Action")),
146 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
147 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
148 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
149 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
150 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
151 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
152 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
153 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
154 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
155 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
156 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
157 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
158 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
159 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
160 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
161 OPT_GROUP(N_("Type")),
162 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
163 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
164 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
165 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
166 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
167 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
168 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
169 OPT_GROUP(N_("Other")),
170 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
171 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
172 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
173 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
174 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
175 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
176 OPT_END(),
179 static NORETURN void usage_builtin_config(void)
181 usage_with_options(builtin_config_usage, builtin_config_options);
184 static void check_argc(int argc, int min, int max)
186 if (argc >= min && argc <= max)
187 return;
188 if (min == max)
189 error(_("wrong number of arguments, should be %d"), min);
190 else
191 error(_("wrong number of arguments, should be from %d to %d"),
192 min, max);
193 usage_builtin_config();
196 static void show_config_origin(struct strbuf *buf)
198 const char term = end_nul ? '\0' : '\t';
200 strbuf_addstr(buf, current_config_origin_type());
201 strbuf_addch(buf, ':');
202 if (end_nul)
203 strbuf_addstr(buf, current_config_name());
204 else
205 quote_c_style(current_config_name(), buf, NULL, 0);
206 strbuf_addch(buf, term);
209 static void show_config_scope(struct strbuf *buf)
211 const char term = end_nul ? '\0' : '\t';
212 const char *scope = config_scope_name(current_config_scope());
214 strbuf_addstr(buf, N_(scope));
215 strbuf_addch(buf, term);
218 static int show_all_config(const char *key_, const char *value_,
219 void *cb UNUSED)
221 if (show_origin || show_scope) {
222 struct strbuf buf = STRBUF_INIT;
223 if (show_scope)
224 show_config_scope(&buf);
225 if (show_origin)
226 show_config_origin(&buf);
227 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
228 fwrite(buf.buf, 1, buf.len, stdout);
229 strbuf_release(&buf);
231 if (!omit_values && value_)
232 printf("%s%c%s%c", key_, delim, value_, term);
233 else
234 printf("%s%c", key_, term);
235 return 0;
238 struct strbuf_list {
239 struct strbuf *items;
240 int nr;
241 int alloc;
244 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
246 if (show_scope)
247 show_config_scope(buf);
248 if (show_origin)
249 show_config_origin(buf);
250 if (show_keys)
251 strbuf_addstr(buf, key_);
252 if (!omit_values) {
253 if (show_keys)
254 strbuf_addch(buf, key_delim);
256 if (type == TYPE_INT)
257 strbuf_addf(buf, "%"PRId64,
258 git_config_int64(key_, value_ ? value_ : ""));
259 else if (type == TYPE_BOOL)
260 strbuf_addstr(buf, git_config_bool(key_, value_) ?
261 "true" : "false");
262 else if (type == TYPE_BOOL_OR_INT) {
263 int is_bool, v;
264 v = git_config_bool_or_int(key_, value_, &is_bool);
265 if (is_bool)
266 strbuf_addstr(buf, v ? "true" : "false");
267 else
268 strbuf_addf(buf, "%d", v);
269 } else if (type == TYPE_BOOL_OR_STR) {
270 int v = git_parse_maybe_bool(value_);
271 if (v < 0)
272 strbuf_addstr(buf, value_);
273 else
274 strbuf_addstr(buf, v ? "true" : "false");
275 } else if (type == TYPE_PATH) {
276 const char *v;
277 if (git_config_pathname(&v, key_, value_) < 0)
278 return -1;
279 strbuf_addstr(buf, v);
280 free((char *)v);
281 } else if (type == TYPE_EXPIRY_DATE) {
282 timestamp_t t;
283 if (git_config_expiry_date(&t, key_, value_) < 0)
284 return -1;
285 strbuf_addf(buf, "%"PRItime, t);
286 } else if (type == TYPE_COLOR) {
287 char v[COLOR_MAXLEN];
288 if (git_config_color(v, key_, value_) < 0)
289 return -1;
290 strbuf_addstr(buf, v);
291 } else if (value_) {
292 strbuf_addstr(buf, value_);
293 } else {
294 /* Just show the key name; back out delimiter */
295 if (show_keys)
296 strbuf_setlen(buf, buf->len - 1);
299 strbuf_addch(buf, term);
300 return 0;
303 static int collect_config(const char *key_, const char *value_, void *cb)
305 struct strbuf_list *values = cb;
307 if (!use_key_regexp && strcmp(key_, key))
308 return 0;
309 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
310 return 0;
311 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
312 return 0;
313 if (regexp != NULL &&
314 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
315 return 0;
317 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
318 strbuf_init(&values->items[values->nr], 0);
320 return format_config(&values->items[values->nr++], key_, value_);
323 static int get_value(const char *key_, const char *regex_, unsigned flags)
325 int ret = CONFIG_GENERIC_ERROR;
326 struct strbuf_list values = {NULL};
327 int i;
329 if (use_key_regexp) {
330 char *tl;
333 * NEEDSWORK: this naive pattern lowercasing obviously does not
334 * work for more complex patterns like "^[^.]*Foo.*bar".
335 * Perhaps we should deprecate this altogether someday.
338 key = xstrdup(key_);
339 for (tl = key + strlen(key) - 1;
340 tl >= key && *tl != '.';
341 tl--)
342 *tl = tolower(*tl);
343 for (tl = key; *tl && *tl != '.'; tl++)
344 *tl = tolower(*tl);
346 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
347 if (regcomp(key_regexp, key, REG_EXTENDED)) {
348 error(_("invalid key pattern: %s"), key_);
349 FREE_AND_NULL(key_regexp);
350 ret = CONFIG_INVALID_PATTERN;
351 goto free_strings;
353 } else {
354 if (git_config_parse_key(key_, &key, NULL)) {
355 ret = CONFIG_INVALID_KEY;
356 goto free_strings;
360 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
361 value_pattern = regex_;
362 else if (regex_) {
363 if (regex_[0] == '!') {
364 do_not_match = 1;
365 regex_++;
368 regexp = (regex_t*)xmalloc(sizeof(regex_t));
369 if (regcomp(regexp, regex_, REG_EXTENDED)) {
370 error(_("invalid pattern: %s"), regex_);
371 FREE_AND_NULL(regexp);
372 ret = CONFIG_INVALID_PATTERN;
373 goto free_strings;
377 config_with_options(collect_config, &values,
378 &given_config_source, &config_options);
380 if (!values.nr && default_value) {
381 struct strbuf *item;
382 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
383 item = &values.items[values.nr++];
384 strbuf_init(item, 0);
385 if (format_config(item, key_, default_value) < 0)
386 die(_("failed to format default config value: %s"),
387 default_value);
390 ret = !values.nr;
392 for (i = 0; i < values.nr; i++) {
393 struct strbuf *buf = values.items + i;
394 if (do_all || i == values.nr - 1)
395 fwrite(buf->buf, 1, buf->len, stdout);
396 strbuf_release(buf);
398 free(values.items);
400 free_strings:
401 free(key);
402 if (key_regexp) {
403 regfree(key_regexp);
404 free(key_regexp);
406 if (regexp) {
407 regfree(regexp);
408 free(regexp);
411 return ret;
414 static char *normalize_value(const char *key, const char *value)
416 if (!value)
417 return NULL;
419 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
421 * We don't do normalization for TYPE_PATH here: If
422 * the path is like ~/foobar/, we prefer to store
423 * "~/foobar/" in the config file, and to expand the ~
424 * when retrieving the value.
425 * Also don't do normalization for expiry dates.
427 return xstrdup(value);
428 if (type == TYPE_INT)
429 return xstrfmt("%"PRId64, git_config_int64(key, value));
430 if (type == TYPE_BOOL)
431 return xstrdup(git_config_bool(key, value) ? "true" : "false");
432 if (type == TYPE_BOOL_OR_INT) {
433 int is_bool, v;
434 v = git_config_bool_or_int(key, value, &is_bool);
435 if (!is_bool)
436 return xstrfmt("%d", v);
437 else
438 return xstrdup(v ? "true" : "false");
440 if (type == TYPE_BOOL_OR_STR) {
441 int v = git_parse_maybe_bool(value);
442 if (v < 0)
443 return xstrdup(value);
444 else
445 return xstrdup(v ? "true" : "false");
447 if (type == TYPE_COLOR) {
448 char v[COLOR_MAXLEN];
449 if (git_config_color(v, key, value))
450 die(_("cannot parse color '%s'"), value);
453 * The contents of `v` now contain an ANSI escape
454 * sequence, not suitable for including within a
455 * configuration file. Treat the above as a
456 * "sanity-check", and return the given value, which we
457 * know is representable as valid color code.
459 return xstrdup(value);
462 BUG("cannot normalize type %d", type);
465 static int get_color_found;
466 static const char *get_color_slot;
467 static const char *get_colorbool_slot;
468 static char parsed_color[COLOR_MAXLEN];
470 static int git_get_color_config(const char *var, const char *value,
471 void *cb UNUSED)
473 if (!strcmp(var, get_color_slot)) {
474 if (!value)
475 config_error_nonbool(var);
476 if (color_parse(value, parsed_color) < 0)
477 return -1;
478 get_color_found = 1;
480 return 0;
483 static void get_color(const char *var, const char *def_color)
485 get_color_slot = var;
486 get_color_found = 0;
487 parsed_color[0] = '\0';
488 config_with_options(git_get_color_config, NULL,
489 &given_config_source, &config_options);
491 if (!get_color_found && def_color) {
492 if (color_parse(def_color, parsed_color) < 0)
493 die(_("unable to parse default color value"));
496 fputs(parsed_color, stdout);
499 static int get_colorbool_found;
500 static int get_diff_color_found;
501 static int get_color_ui_found;
502 static int git_get_colorbool_config(const char *var, const char *value,
503 void *data UNUSED)
505 if (!strcmp(var, get_colorbool_slot))
506 get_colorbool_found = git_config_colorbool(var, value);
507 else if (!strcmp(var, "diff.color"))
508 get_diff_color_found = git_config_colorbool(var, value);
509 else if (!strcmp(var, "color.ui"))
510 get_color_ui_found = git_config_colorbool(var, value);
511 return 0;
514 static int get_colorbool(const char *var, int print)
516 get_colorbool_slot = var;
517 get_colorbool_found = -1;
518 get_diff_color_found = -1;
519 get_color_ui_found = -1;
520 config_with_options(git_get_colorbool_config, NULL,
521 &given_config_source, &config_options);
523 if (get_colorbool_found < 0) {
524 if (!strcmp(get_colorbool_slot, "color.diff"))
525 get_colorbool_found = get_diff_color_found;
526 if (get_colorbool_found < 0)
527 get_colorbool_found = get_color_ui_found;
530 if (get_colorbool_found < 0)
531 /* default value if none found in config */
532 get_colorbool_found = GIT_COLOR_AUTO;
534 get_colorbool_found = want_color(get_colorbool_found);
536 if (print) {
537 printf("%s\n", get_colorbool_found ? "true" : "false");
538 return 0;
539 } else
540 return get_colorbool_found ? 0 : 1;
543 static void check_write(void)
545 if (!given_config_source.file && !startup_info->have_repository)
546 die(_("not in a git directory"));
548 if (given_config_source.use_stdin)
549 die(_("writing to stdin is not supported"));
551 if (given_config_source.blob)
552 die(_("writing config blobs is not supported"));
555 struct urlmatch_current_candidate_value {
556 char value_is_null;
557 struct strbuf value;
560 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
562 struct string_list *values = cb;
563 struct string_list_item *item = string_list_insert(values, var);
564 struct urlmatch_current_candidate_value *matched = item->util;
566 if (!matched) {
567 matched = xmalloc(sizeof(*matched));
568 strbuf_init(&matched->value, 0);
569 item->util = matched;
570 } else {
571 strbuf_reset(&matched->value);
574 if (value) {
575 strbuf_addstr(&matched->value, value);
576 matched->value_is_null = 0;
577 } else {
578 matched->value_is_null = 1;
580 return 0;
583 static int get_urlmatch(const char *var, const char *url)
585 int ret;
586 char *section_tail;
587 struct string_list_item *item;
588 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
589 struct string_list values = STRING_LIST_INIT_DUP;
591 config.collect_fn = urlmatch_collect_fn;
592 config.cascade_fn = NULL;
593 config.cb = &values;
595 if (!url_normalize(url, &config.url))
596 die("%s", config.url.err);
598 config.section = xstrdup_tolower(var);
599 section_tail = strchr(config.section, '.');
600 if (section_tail) {
601 *section_tail = '\0';
602 config.key = section_tail + 1;
603 show_keys = 0;
604 } else {
605 config.key = NULL;
606 show_keys = 1;
609 config_with_options(urlmatch_config_entry, &config,
610 &given_config_source, &config_options);
612 ret = !values.nr;
614 for_each_string_list_item(item, &values) {
615 struct urlmatch_current_candidate_value *matched = item->util;
616 struct strbuf buf = STRBUF_INIT;
618 format_config(&buf, item->string,
619 matched->value_is_null ? NULL : matched->value.buf);
620 fwrite(buf.buf, 1, buf.len, stdout);
621 strbuf_release(&buf);
623 strbuf_release(&matched->value);
625 urlmatch_config_release(&config);
626 string_list_clear(&values, 1);
627 free(config.url.url);
629 free((void *)config.section);
630 return ret;
633 static char *default_user_config(void)
635 struct strbuf buf = STRBUF_INIT;
636 strbuf_addf(&buf,
637 _("# This is Git's per-user configuration file.\n"
638 "[user]\n"
639 "# Please adapt and uncomment the following lines:\n"
640 "# name = %s\n"
641 "# email = %s\n"),
642 ident_default_name(),
643 ident_default_email());
644 return strbuf_detach(&buf, NULL);
647 int cmd_config(int argc, const char **argv, const char *prefix)
649 int nongit = !startup_info->have_repository;
650 char *value = NULL;
651 int flags = 0;
652 int ret = 0;
654 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
656 argc = parse_options(argc, argv, prefix, builtin_config_options,
657 builtin_config_usage,
658 PARSE_OPT_STOP_AT_NON_OPTION);
660 if (use_global_config + use_system_config + use_local_config +
661 use_worktree_config +
662 !!given_config_source.file + !!given_config_source.blob > 1) {
663 error(_("only one config file at a time"));
664 usage_builtin_config();
667 if (nongit) {
668 if (use_local_config)
669 die(_("--local can only be used inside a git repository"));
670 if (given_config_source.blob)
671 die(_("--blob can only be used inside a git repository"));
672 if (use_worktree_config)
673 die(_("--worktree can only be used inside a git repository"));
677 if (given_config_source.file &&
678 !strcmp(given_config_source.file, "-")) {
679 given_config_source.file = NULL;
680 given_config_source.use_stdin = 1;
681 given_config_source.scope = CONFIG_SCOPE_COMMAND;
684 if (use_global_config) {
685 char *user_config, *xdg_config;
687 git_global_config(&user_config, &xdg_config);
688 if (!user_config)
690 * It is unknown if HOME/.gitconfig exists, so
691 * we do not know if we should write to XDG
692 * location; error out even if XDG_CONFIG_HOME
693 * is set and points at a sane location.
695 die(_("$HOME not set"));
697 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
699 if (access_or_warn(user_config, R_OK, 0) &&
700 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
701 given_config_source.file = xdg_config;
702 free(user_config);
703 } else {
704 given_config_source.file = user_config;
705 free(xdg_config);
708 else if (use_system_config) {
709 given_config_source.file = git_system_config();
710 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
711 } else if (use_local_config) {
712 given_config_source.file = git_pathdup("config");
713 given_config_source.scope = CONFIG_SCOPE_LOCAL;
714 } else if (use_worktree_config) {
715 struct worktree **worktrees = get_worktrees();
716 if (repository_format_worktree_config)
717 given_config_source.file = git_pathdup("config.worktree");
718 else if (worktrees[0] && worktrees[1])
719 die(_("--worktree cannot be used with multiple "
720 "working trees unless the config\n"
721 "extension worktreeConfig is enabled. "
722 "Please read \"CONFIGURATION FILE\"\n"
723 "section in \"git help worktree\" for details"));
724 else
725 given_config_source.file = git_pathdup("config");
726 given_config_source.scope = CONFIG_SCOPE_LOCAL;
727 free_worktrees(worktrees);
728 } else if (given_config_source.file) {
729 if (!is_absolute_path(given_config_source.file) && prefix)
730 given_config_source.file =
731 prefix_filename(prefix, given_config_source.file);
732 given_config_source.scope = CONFIG_SCOPE_COMMAND;
733 } else if (given_config_source.blob) {
734 given_config_source.scope = CONFIG_SCOPE_COMMAND;
738 if (respect_includes_opt == -1)
739 config_options.respect_includes = !given_config_source.file;
740 else
741 config_options.respect_includes = respect_includes_opt;
742 if (!nongit) {
743 config_options.commondir = get_git_common_dir();
744 config_options.git_dir = get_git_dir();
747 if (end_nul) {
748 term = '\0';
749 delim = '\n';
750 key_delim = '\n';
753 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
754 error(_("--get-color and variable type are incoherent"));
755 usage_builtin_config();
758 if (HAS_MULTI_BITS(actions)) {
759 error(_("only one action at a time"));
760 usage_builtin_config();
762 if (actions == 0)
763 switch (argc) {
764 case 1: actions = ACTION_GET; break;
765 case 2: actions = ACTION_SET; break;
766 case 3: actions = ACTION_SET_ALL; break;
767 default:
768 usage_builtin_config();
770 if (omit_values &&
771 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
772 error(_("--name-only is only applicable to --list or --get-regexp"));
773 usage_builtin_config();
776 if (show_origin && !(actions &
777 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
778 error(_("--show-origin is only applicable to --get, --get-all, "
779 "--get-regexp, and --list"));
780 usage_builtin_config();
783 if (default_value && !(actions & ACTION_GET)) {
784 error(_("--default is only applicable to --get"));
785 usage_builtin_config();
788 /* check usage of --fixed-value */
789 if (fixed_value) {
790 int allowed_usage = 0;
792 switch (actions) {
793 /* git config --get <name> <value-pattern> */
794 case ACTION_GET:
795 /* git config --get-all <name> <value-pattern> */
796 case ACTION_GET_ALL:
797 /* git config --get-regexp <name-pattern> <value-pattern> */
798 case ACTION_GET_REGEXP:
799 /* git config --unset <name> <value-pattern> */
800 case ACTION_UNSET:
801 /* git config --unset-all <name> <value-pattern> */
802 case ACTION_UNSET_ALL:
803 allowed_usage = argc > 1 && !!argv[1];
804 break;
806 /* git config <name> <value> <value-pattern> */
807 case ACTION_SET_ALL:
808 /* git config --replace-all <name> <value> <value-pattern> */
809 case ACTION_REPLACE_ALL:
810 allowed_usage = argc > 2 && !!argv[2];
811 break;
813 /* other options don't allow --fixed-value */
816 if (!allowed_usage) {
817 error(_("--fixed-value only applies with 'value-pattern'"));
818 usage_builtin_config();
821 flags |= CONFIG_FLAGS_FIXED_VALUE;
824 if (actions & PAGING_ACTIONS)
825 setup_auto_pager("config", 1);
827 if (actions == ACTION_LIST) {
828 check_argc(argc, 0, 0);
829 if (config_with_options(show_all_config, NULL,
830 &given_config_source,
831 &config_options) < 0) {
832 if (given_config_source.file)
833 die_errno(_("unable to read config file '%s'"),
834 given_config_source.file);
835 else
836 die(_("error processing config file(s)"));
839 else if (actions == ACTION_EDIT) {
840 char *config_file;
842 check_argc(argc, 0, 0);
843 if (!given_config_source.file && nongit)
844 die(_("not in a git directory"));
845 if (given_config_source.use_stdin)
846 die(_("editing stdin is not supported"));
847 if (given_config_source.blob)
848 die(_("editing blobs is not supported"));
849 git_config(git_default_config, NULL);
850 config_file = given_config_source.file ?
851 xstrdup(given_config_source.file) :
852 git_pathdup("config");
853 if (use_global_config) {
854 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
855 if (fd >= 0) {
856 char *content = default_user_config();
857 write_str_in_full(fd, content);
858 free(content);
859 close(fd);
861 else if (errno != EEXIST)
862 die_errno(_("cannot create configuration file %s"), config_file);
864 launch_editor(config_file, NULL, NULL);
865 free(config_file);
867 else if (actions == ACTION_SET) {
868 check_write();
869 check_argc(argc, 2, 2);
870 value = normalize_value(argv[0], argv[1]);
871 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
872 if (ret == CONFIG_NOTHING_SET)
873 error(_("cannot overwrite multiple values with a single value\n"
874 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
876 else if (actions == ACTION_SET_ALL) {
877 check_write();
878 check_argc(argc, 2, 3);
879 value = normalize_value(argv[0], argv[1]);
880 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
881 argv[0], value, argv[2],
882 flags);
884 else if (actions == ACTION_ADD) {
885 check_write();
886 check_argc(argc, 2, 2);
887 value = normalize_value(argv[0], argv[1]);
888 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
889 argv[0], value,
890 CONFIG_REGEX_NONE,
891 flags);
893 else if (actions == ACTION_REPLACE_ALL) {
894 check_write();
895 check_argc(argc, 2, 3);
896 value = normalize_value(argv[0], argv[1]);
897 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
898 argv[0], value, argv[2],
899 flags | CONFIG_FLAGS_MULTI_REPLACE);
901 else if (actions == ACTION_GET) {
902 check_argc(argc, 1, 2);
903 return get_value(argv[0], argv[1], flags);
905 else if (actions == ACTION_GET_ALL) {
906 do_all = 1;
907 check_argc(argc, 1, 2);
908 return get_value(argv[0], argv[1], flags);
910 else if (actions == ACTION_GET_REGEXP) {
911 show_keys = 1;
912 use_key_regexp = 1;
913 do_all = 1;
914 check_argc(argc, 1, 2);
915 return get_value(argv[0], argv[1], flags);
917 else if (actions == ACTION_GET_URLMATCH) {
918 check_argc(argc, 2, 2);
919 return get_urlmatch(argv[0], argv[1]);
921 else if (actions == ACTION_UNSET) {
922 check_write();
923 check_argc(argc, 1, 2);
924 if (argc == 2)
925 return git_config_set_multivar_in_file_gently(given_config_source.file,
926 argv[0], NULL, argv[1],
927 flags);
928 else
929 return git_config_set_in_file_gently(given_config_source.file,
930 argv[0], NULL);
932 else if (actions == ACTION_UNSET_ALL) {
933 check_write();
934 check_argc(argc, 1, 2);
935 return git_config_set_multivar_in_file_gently(given_config_source.file,
936 argv[0], NULL, argv[1],
937 flags | CONFIG_FLAGS_MULTI_REPLACE);
939 else if (actions == ACTION_RENAME_SECTION) {
940 check_write();
941 check_argc(argc, 2, 2);
942 ret = git_config_rename_section_in_file(given_config_source.file,
943 argv[0], argv[1]);
944 if (ret < 0)
945 return ret;
946 else if (!ret)
947 die(_("no such section: %s"), argv[0]);
948 else
949 ret = 0;
951 else if (actions == ACTION_REMOVE_SECTION) {
952 check_write();
953 check_argc(argc, 1, 1);
954 ret = git_config_rename_section_in_file(given_config_source.file,
955 argv[0], NULL);
956 if (ret < 0)
957 return ret;
958 else if (!ret)
959 die(_("no such section: %s"), argv[0]);
960 else
961 ret = 0;
963 else if (actions == ACTION_GET_COLOR) {
964 check_argc(argc, 1, 2);
965 get_color(argv[0], argv[1]);
967 else if (actions == ACTION_GET_COLORBOOL) {
968 check_argc(argc, 1, 2);
969 if (argc == 2)
970 color_stdout_is_tty = git_config_bool("command line", argv[1]);
971 return get_colorbool(argv[0], argc == 2);
974 free(value);
975 return ret;