Merge branch 'jk/commit-use-no-divider-with-interpret-trailers'
[alt-git.git] / builtin / config.c
blobd40fddb042a6cc34000f0135b1ccb60f284a3e18
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 "repository.h"
9 #include "gettext.h"
10 #include "ident.h"
11 #include "parse-options.h"
12 #include "urlmatch.h"
13 #include "path.h"
14 #include "quote.h"
15 #include "setup.h"
16 #include "worktree.h"
17 #include "wrapper.h"
19 static const char *const builtin_config_usage[] = {
20 N_("git config [<options>]"),
21 NULL
24 static char *key;
25 static regex_t *key_regexp;
26 static const char *value_pattern;
27 static regex_t *regexp;
28 static int show_keys;
29 static int omit_values;
30 static int use_key_regexp;
31 static int do_all;
32 static int do_not_match;
33 static char delim = '=';
34 static char key_delim = ' ';
35 static char term = '\n';
37 static int use_global_config, use_system_config, use_local_config;
38 static int use_worktree_config;
39 static struct git_config_source given_config_source;
40 static int actions, type;
41 static char *default_value;
42 static int end_nul;
43 static int respect_includes_opt = -1;
44 static struct config_options config_options;
45 static int show_origin;
46 static int show_scope;
47 static int fixed_value;
49 #define ACTION_GET (1<<0)
50 #define ACTION_GET_ALL (1<<1)
51 #define ACTION_GET_REGEXP (1<<2)
52 #define ACTION_REPLACE_ALL (1<<3)
53 #define ACTION_ADD (1<<4)
54 #define ACTION_UNSET (1<<5)
55 #define ACTION_UNSET_ALL (1<<6)
56 #define ACTION_RENAME_SECTION (1<<7)
57 #define ACTION_REMOVE_SECTION (1<<8)
58 #define ACTION_LIST (1<<9)
59 #define ACTION_EDIT (1<<10)
60 #define ACTION_SET (1<<11)
61 #define ACTION_SET_ALL (1<<12)
62 #define ACTION_GET_COLOR (1<<13)
63 #define ACTION_GET_COLORBOOL (1<<14)
64 #define ACTION_GET_URLMATCH (1<<15)
67 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
68 * one line of output and which should therefore be paged.
70 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
71 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
73 #define TYPE_BOOL 1
74 #define TYPE_INT 2
75 #define TYPE_BOOL_OR_INT 3
76 #define TYPE_PATH 4
77 #define TYPE_EXPIRY_DATE 5
78 #define TYPE_COLOR 6
79 #define TYPE_BOOL_OR_STR 7
81 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
82 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
83 PARSE_OPT_NONEG, option_parse_type, (i) }
85 static NORETURN void usage_builtin_config(void);
87 static int option_parse_type(const struct option *opt, const char *arg,
88 int unset)
90 int new_type, *to_type;
92 if (unset) {
93 *((int *) opt->value) = 0;
94 return 0;
98 * To support '--<type>' style flags, begin with new_type equal to
99 * opt->defval.
101 new_type = opt->defval;
102 if (!new_type) {
103 if (!strcmp(arg, "bool"))
104 new_type = TYPE_BOOL;
105 else if (!strcmp(arg, "int"))
106 new_type = TYPE_INT;
107 else if (!strcmp(arg, "bool-or-int"))
108 new_type = TYPE_BOOL_OR_INT;
109 else if (!strcmp(arg, "bool-or-str"))
110 new_type = TYPE_BOOL_OR_STR;
111 else if (!strcmp(arg, "path"))
112 new_type = TYPE_PATH;
113 else if (!strcmp(arg, "expiry-date"))
114 new_type = TYPE_EXPIRY_DATE;
115 else if (!strcmp(arg, "color"))
116 new_type = TYPE_COLOR;
117 else
118 die(_("unrecognized --type argument, %s"), arg);
121 to_type = opt->value;
122 if (*to_type && *to_type != new_type) {
124 * Complain when there is a new type not equal to the old type.
125 * This allows for combinations like '--int --type=int' and
126 * '--type=int --type=int', but disallows ones like '--type=bool
127 * --int' and '--type=bool
128 * --type=int'.
130 error(_("only one type at a time"));
131 usage_builtin_config();
133 *to_type = new_type;
135 return 0;
138 static struct option builtin_config_options[] = {
139 OPT_GROUP(N_("Config file location")),
140 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
141 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
142 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
143 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
144 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
145 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
146 OPT_GROUP(N_("Action")),
147 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
148 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
149 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
150 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
151 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
152 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
153 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
154 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
155 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
156 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
157 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
158 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
159 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
160 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
161 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
162 OPT_GROUP(N_("Type")),
163 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
164 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
165 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
166 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
167 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
168 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
169 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
170 OPT_GROUP(N_("Other")),
171 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
172 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
173 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
174 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
175 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
176 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
177 OPT_END(),
180 static NORETURN void usage_builtin_config(void)
182 usage_with_options(builtin_config_usage, builtin_config_options);
185 static void check_argc(int argc, int min, int max)
187 if (argc >= min && argc <= max)
188 return;
189 if (min == max)
190 error(_("wrong number of arguments, should be %d"), min);
191 else
192 error(_("wrong number of arguments, should be from %d to %d"),
193 min, max);
194 usage_builtin_config();
197 static void show_config_origin(struct strbuf *buf)
199 const char term = end_nul ? '\0' : '\t';
201 strbuf_addstr(buf, current_config_origin_type());
202 strbuf_addch(buf, ':');
203 if (end_nul)
204 strbuf_addstr(buf, current_config_name());
205 else
206 quote_c_style(current_config_name(), buf, NULL, 0);
207 strbuf_addch(buf, term);
210 static void show_config_scope(struct strbuf *buf)
212 const char term = end_nul ? '\0' : '\t';
213 const char *scope = config_scope_name(current_config_scope());
215 strbuf_addstr(buf, N_(scope));
216 strbuf_addch(buf, term);
219 static int show_all_config(const char *key_, const char *value_,
220 void *cb UNUSED)
222 if (show_origin || show_scope) {
223 struct strbuf buf = STRBUF_INIT;
224 if (show_scope)
225 show_config_scope(&buf);
226 if (show_origin)
227 show_config_origin(&buf);
228 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
229 fwrite(buf.buf, 1, buf.len, stdout);
230 strbuf_release(&buf);
232 if (!omit_values && value_)
233 printf("%s%c%s%c", key_, delim, value_, term);
234 else
235 printf("%s%c", key_, term);
236 return 0;
239 struct strbuf_list {
240 struct strbuf *items;
241 int nr;
242 int alloc;
245 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
247 if (show_scope)
248 show_config_scope(buf);
249 if (show_origin)
250 show_config_origin(buf);
251 if (show_keys)
252 strbuf_addstr(buf, key_);
253 if (!omit_values) {
254 if (show_keys)
255 strbuf_addch(buf, key_delim);
257 if (type == TYPE_INT)
258 strbuf_addf(buf, "%"PRId64,
259 git_config_int64(key_, value_ ? value_ : ""));
260 else if (type == TYPE_BOOL)
261 strbuf_addstr(buf, git_config_bool(key_, value_) ?
262 "true" : "false");
263 else if (type == TYPE_BOOL_OR_INT) {
264 int is_bool, v;
265 v = git_config_bool_or_int(key_, value_, &is_bool);
266 if (is_bool)
267 strbuf_addstr(buf, v ? "true" : "false");
268 else
269 strbuf_addf(buf, "%d", v);
270 } else if (type == TYPE_BOOL_OR_STR) {
271 int v = git_parse_maybe_bool(value_);
272 if (v < 0)
273 strbuf_addstr(buf, value_);
274 else
275 strbuf_addstr(buf, v ? "true" : "false");
276 } else if (type == TYPE_PATH) {
277 const char *v;
278 if (git_config_pathname(&v, key_, value_) < 0)
279 return -1;
280 strbuf_addstr(buf, v);
281 free((char *)v);
282 } else if (type == TYPE_EXPIRY_DATE) {
283 timestamp_t t;
284 if (git_config_expiry_date(&t, key_, value_) < 0)
285 return -1;
286 strbuf_addf(buf, "%"PRItime, t);
287 } else if (type == TYPE_COLOR) {
288 char v[COLOR_MAXLEN];
289 if (git_config_color(v, key_, value_) < 0)
290 return -1;
291 strbuf_addstr(buf, v);
292 } else if (value_) {
293 strbuf_addstr(buf, value_);
294 } else {
295 /* Just show the key name; back out delimiter */
296 if (show_keys)
297 strbuf_setlen(buf, buf->len - 1);
300 strbuf_addch(buf, term);
301 return 0;
304 static int collect_config(const char *key_, const char *value_, void *cb)
306 struct strbuf_list *values = cb;
308 if (!use_key_regexp && strcmp(key_, key))
309 return 0;
310 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
311 return 0;
312 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
313 return 0;
314 if (regexp != NULL &&
315 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
316 return 0;
318 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
319 strbuf_init(&values->items[values->nr], 0);
321 return format_config(&values->items[values->nr++], key_, value_);
324 static int get_value(const char *key_, const char *regex_, unsigned flags)
326 int ret = CONFIG_GENERIC_ERROR;
327 struct strbuf_list values = {NULL};
328 int i;
330 if (use_key_regexp) {
331 char *tl;
334 * NEEDSWORK: this naive pattern lowercasing obviously does not
335 * work for more complex patterns like "^[^.]*Foo.*bar".
336 * Perhaps we should deprecate this altogether someday.
339 key = xstrdup(key_);
340 for (tl = key + strlen(key) - 1;
341 tl >= key && *tl != '.';
342 tl--)
343 *tl = tolower(*tl);
344 for (tl = key; *tl && *tl != '.'; tl++)
345 *tl = tolower(*tl);
347 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
348 if (regcomp(key_regexp, key, REG_EXTENDED)) {
349 error(_("invalid key pattern: %s"), key_);
350 FREE_AND_NULL(key_regexp);
351 ret = CONFIG_INVALID_PATTERN;
352 goto free_strings;
354 } else {
355 if (git_config_parse_key(key_, &key, NULL)) {
356 ret = CONFIG_INVALID_KEY;
357 goto free_strings;
361 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
362 value_pattern = regex_;
363 else if (regex_) {
364 if (regex_[0] == '!') {
365 do_not_match = 1;
366 regex_++;
369 regexp = (regex_t*)xmalloc(sizeof(regex_t));
370 if (regcomp(regexp, regex_, REG_EXTENDED)) {
371 error(_("invalid pattern: %s"), regex_);
372 FREE_AND_NULL(regexp);
373 ret = CONFIG_INVALID_PATTERN;
374 goto free_strings;
378 config_with_options(collect_config, &values,
379 &given_config_source, the_repository,
380 &config_options);
382 if (!values.nr && default_value) {
383 struct strbuf *item;
384 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
385 item = &values.items[values.nr++];
386 strbuf_init(item, 0);
387 if (format_config(item, key_, default_value) < 0)
388 die(_("failed to format default config value: %s"),
389 default_value);
392 ret = !values.nr;
394 for (i = 0; i < values.nr; i++) {
395 struct strbuf *buf = values.items + i;
396 if (do_all || i == values.nr - 1)
397 fwrite(buf->buf, 1, buf->len, stdout);
398 strbuf_release(buf);
400 free(values.items);
402 free_strings:
403 free(key);
404 if (key_regexp) {
405 regfree(key_regexp);
406 free(key_regexp);
408 if (regexp) {
409 regfree(regexp);
410 free(regexp);
413 return ret;
416 static char *normalize_value(const char *key, const char *value)
418 if (!value)
419 return NULL;
421 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
423 * We don't do normalization for TYPE_PATH here: If
424 * the path is like ~/foobar/, we prefer to store
425 * "~/foobar/" in the config file, and to expand the ~
426 * when retrieving the value.
427 * Also don't do normalization for expiry dates.
429 return xstrdup(value);
430 if (type == TYPE_INT)
431 return xstrfmt("%"PRId64, git_config_int64(key, value));
432 if (type == TYPE_BOOL)
433 return xstrdup(git_config_bool(key, value) ? "true" : "false");
434 if (type == TYPE_BOOL_OR_INT) {
435 int is_bool, v;
436 v = git_config_bool_or_int(key, value, &is_bool);
437 if (!is_bool)
438 return xstrfmt("%d", v);
439 else
440 return xstrdup(v ? "true" : "false");
442 if (type == TYPE_BOOL_OR_STR) {
443 int v = git_parse_maybe_bool(value);
444 if (v < 0)
445 return xstrdup(value);
446 else
447 return xstrdup(v ? "true" : "false");
449 if (type == TYPE_COLOR) {
450 char v[COLOR_MAXLEN];
451 if (git_config_color(v, key, value))
452 die(_("cannot parse color '%s'"), value);
455 * The contents of `v` now contain an ANSI escape
456 * sequence, not suitable for including within a
457 * configuration file. Treat the above as a
458 * "sanity-check", and return the given value, which we
459 * know is representable as valid color code.
461 return xstrdup(value);
464 BUG("cannot normalize type %d", type);
467 static int get_color_found;
468 static const char *get_color_slot;
469 static const char *get_colorbool_slot;
470 static char parsed_color[COLOR_MAXLEN];
472 static int git_get_color_config(const char *var, const char *value,
473 void *cb UNUSED)
475 if (!strcmp(var, get_color_slot)) {
476 if (!value)
477 config_error_nonbool(var);
478 if (color_parse(value, parsed_color) < 0)
479 return -1;
480 get_color_found = 1;
482 return 0;
485 static void get_color(const char *var, const char *def_color)
487 get_color_slot = var;
488 get_color_found = 0;
489 parsed_color[0] = '\0';
490 config_with_options(git_get_color_config, NULL,
491 &given_config_source, the_repository,
492 &config_options);
494 if (!get_color_found && def_color) {
495 if (color_parse(def_color, parsed_color) < 0)
496 die(_("unable to parse default color value"));
499 fputs(parsed_color, stdout);
502 static int get_colorbool_found;
503 static int get_diff_color_found;
504 static int get_color_ui_found;
505 static int git_get_colorbool_config(const char *var, const char *value,
506 void *data UNUSED)
508 if (!strcmp(var, get_colorbool_slot))
509 get_colorbool_found = git_config_colorbool(var, value);
510 else if (!strcmp(var, "diff.color"))
511 get_diff_color_found = git_config_colorbool(var, value);
512 else if (!strcmp(var, "color.ui"))
513 get_color_ui_found = git_config_colorbool(var, value);
514 return 0;
517 static int get_colorbool(const char *var, int print)
519 get_colorbool_slot = var;
520 get_colorbool_found = -1;
521 get_diff_color_found = -1;
522 get_color_ui_found = -1;
523 config_with_options(git_get_colorbool_config, NULL,
524 &given_config_source, the_repository,
525 &config_options);
527 if (get_colorbool_found < 0) {
528 if (!strcmp(get_colorbool_slot, "color.diff"))
529 get_colorbool_found = get_diff_color_found;
530 if (get_colorbool_found < 0)
531 get_colorbool_found = get_color_ui_found;
534 if (get_colorbool_found < 0)
535 /* default value if none found in config */
536 get_colorbool_found = GIT_COLOR_AUTO;
538 get_colorbool_found = want_color(get_colorbool_found);
540 if (print) {
541 printf("%s\n", get_colorbool_found ? "true" : "false");
542 return 0;
543 } else
544 return get_colorbool_found ? 0 : 1;
547 static void check_write(void)
549 if (!given_config_source.file && !startup_info->have_repository)
550 die(_("not in a git directory"));
552 if (given_config_source.use_stdin)
553 die(_("writing to stdin is not supported"));
555 if (given_config_source.blob)
556 die(_("writing config blobs is not supported"));
559 struct urlmatch_current_candidate_value {
560 char value_is_null;
561 struct strbuf value;
564 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
566 struct string_list *values = cb;
567 struct string_list_item *item = string_list_insert(values, var);
568 struct urlmatch_current_candidate_value *matched = item->util;
570 if (!matched) {
571 matched = xmalloc(sizeof(*matched));
572 strbuf_init(&matched->value, 0);
573 item->util = matched;
574 } else {
575 strbuf_reset(&matched->value);
578 if (value) {
579 strbuf_addstr(&matched->value, value);
580 matched->value_is_null = 0;
581 } else {
582 matched->value_is_null = 1;
584 return 0;
587 static int get_urlmatch(const char *var, const char *url)
589 int ret;
590 char *section_tail;
591 struct string_list_item *item;
592 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
593 struct string_list values = STRING_LIST_INIT_DUP;
595 config.collect_fn = urlmatch_collect_fn;
596 config.cascade_fn = NULL;
597 config.cb = &values;
599 if (!url_normalize(url, &config.url))
600 die("%s", config.url.err);
602 config.section = xstrdup_tolower(var);
603 section_tail = strchr(config.section, '.');
604 if (section_tail) {
605 *section_tail = '\0';
606 config.key = section_tail + 1;
607 show_keys = 0;
608 } else {
609 config.key = NULL;
610 show_keys = 1;
613 config_with_options(urlmatch_config_entry, &config,
614 &given_config_source, the_repository,
615 &config_options);
617 ret = !values.nr;
619 for_each_string_list_item(item, &values) {
620 struct urlmatch_current_candidate_value *matched = item->util;
621 struct strbuf buf = STRBUF_INIT;
623 format_config(&buf, item->string,
624 matched->value_is_null ? NULL : matched->value.buf);
625 fwrite(buf.buf, 1, buf.len, stdout);
626 strbuf_release(&buf);
628 strbuf_release(&matched->value);
630 urlmatch_config_release(&config);
631 string_list_clear(&values, 1);
632 free(config.url.url);
634 free((void *)config.section);
635 return ret;
638 static char *default_user_config(void)
640 struct strbuf buf = STRBUF_INIT;
641 strbuf_addf(&buf,
642 _("# This is Git's per-user configuration file.\n"
643 "[user]\n"
644 "# Please adapt and uncomment the following lines:\n"
645 "# name = %s\n"
646 "# email = %s\n"),
647 ident_default_name(),
648 ident_default_email());
649 return strbuf_detach(&buf, NULL);
652 int cmd_config(int argc, const char **argv, const char *prefix)
654 int nongit = !startup_info->have_repository;
655 char *value = NULL;
656 int flags = 0;
657 int ret = 0;
659 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
661 argc = parse_options(argc, argv, prefix, builtin_config_options,
662 builtin_config_usage,
663 PARSE_OPT_STOP_AT_NON_OPTION);
665 if (use_global_config + use_system_config + use_local_config +
666 use_worktree_config +
667 !!given_config_source.file + !!given_config_source.blob > 1) {
668 error(_("only one config file at a time"));
669 usage_builtin_config();
672 if (nongit) {
673 if (use_local_config)
674 die(_("--local can only be used inside a git repository"));
675 if (given_config_source.blob)
676 die(_("--blob can only be used inside a git repository"));
677 if (use_worktree_config)
678 die(_("--worktree can only be used inside a git repository"));
682 if (given_config_source.file &&
683 !strcmp(given_config_source.file, "-")) {
684 given_config_source.file = NULL;
685 given_config_source.use_stdin = 1;
686 given_config_source.scope = CONFIG_SCOPE_COMMAND;
689 if (use_global_config) {
690 char *user_config, *xdg_config;
692 git_global_config(&user_config, &xdg_config);
693 if (!user_config)
695 * It is unknown if HOME/.gitconfig exists, so
696 * we do not know if we should write to XDG
697 * location; error out even if XDG_CONFIG_HOME
698 * is set and points at a sane location.
700 die(_("$HOME not set"));
702 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
704 if (access_or_warn(user_config, R_OK, 0) &&
705 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
706 given_config_source.file = xdg_config;
707 free(user_config);
708 } else {
709 given_config_source.file = user_config;
710 free(xdg_config);
713 else if (use_system_config) {
714 given_config_source.file = git_system_config();
715 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
716 } else if (use_local_config) {
717 given_config_source.file = git_pathdup("config");
718 given_config_source.scope = CONFIG_SCOPE_LOCAL;
719 } else if (use_worktree_config) {
720 struct worktree **worktrees = get_worktrees();
721 if (the_repository->repository_format_worktree_config)
722 given_config_source.file = git_pathdup("config.worktree");
723 else if (worktrees[0] && worktrees[1])
724 die(_("--worktree cannot be used with multiple "
725 "working trees unless the config\n"
726 "extension worktreeConfig is enabled. "
727 "Please read \"CONFIGURATION FILE\"\n"
728 "section in \"git help worktree\" for details"));
729 else
730 given_config_source.file = git_pathdup("config");
731 given_config_source.scope = CONFIG_SCOPE_LOCAL;
732 free_worktrees(worktrees);
733 } else if (given_config_source.file) {
734 if (!is_absolute_path(given_config_source.file) && prefix)
735 given_config_source.file =
736 prefix_filename(prefix, given_config_source.file);
737 given_config_source.scope = CONFIG_SCOPE_COMMAND;
738 } else if (given_config_source.blob) {
739 given_config_source.scope = CONFIG_SCOPE_COMMAND;
743 if (respect_includes_opt == -1)
744 config_options.respect_includes = !given_config_source.file;
745 else
746 config_options.respect_includes = respect_includes_opt;
747 if (!nongit) {
748 config_options.commondir = get_git_common_dir();
749 config_options.git_dir = get_git_dir();
752 if (end_nul) {
753 term = '\0';
754 delim = '\n';
755 key_delim = '\n';
758 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
759 error(_("--get-color and variable type are incoherent"));
760 usage_builtin_config();
763 if (HAS_MULTI_BITS(actions)) {
764 error(_("only one action at a time"));
765 usage_builtin_config();
767 if (actions == 0)
768 switch (argc) {
769 case 1: actions = ACTION_GET; break;
770 case 2: actions = ACTION_SET; break;
771 case 3: actions = ACTION_SET_ALL; break;
772 default:
773 usage_builtin_config();
775 if (omit_values &&
776 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
777 error(_("--name-only is only applicable to --list or --get-regexp"));
778 usage_builtin_config();
781 if (show_origin && !(actions &
782 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
783 error(_("--show-origin is only applicable to --get, --get-all, "
784 "--get-regexp, and --list"));
785 usage_builtin_config();
788 if (default_value && !(actions & ACTION_GET)) {
789 error(_("--default is only applicable to --get"));
790 usage_builtin_config();
793 /* check usage of --fixed-value */
794 if (fixed_value) {
795 int allowed_usage = 0;
797 switch (actions) {
798 /* git config --get <name> <value-pattern> */
799 case ACTION_GET:
800 /* git config --get-all <name> <value-pattern> */
801 case ACTION_GET_ALL:
802 /* git config --get-regexp <name-pattern> <value-pattern> */
803 case ACTION_GET_REGEXP:
804 /* git config --unset <name> <value-pattern> */
805 case ACTION_UNSET:
806 /* git config --unset-all <name> <value-pattern> */
807 case ACTION_UNSET_ALL:
808 allowed_usage = argc > 1 && !!argv[1];
809 break;
811 /* git config <name> <value> <value-pattern> */
812 case ACTION_SET_ALL:
813 /* git config --replace-all <name> <value> <value-pattern> */
814 case ACTION_REPLACE_ALL:
815 allowed_usage = argc > 2 && !!argv[2];
816 break;
818 /* other options don't allow --fixed-value */
821 if (!allowed_usage) {
822 error(_("--fixed-value only applies with 'value-pattern'"));
823 usage_builtin_config();
826 flags |= CONFIG_FLAGS_FIXED_VALUE;
829 if (actions & PAGING_ACTIONS)
830 setup_auto_pager("config", 1);
832 if (actions == ACTION_LIST) {
833 check_argc(argc, 0, 0);
834 if (config_with_options(show_all_config, NULL,
835 &given_config_source, the_repository,
836 &config_options) < 0) {
837 if (given_config_source.file)
838 die_errno(_("unable to read config file '%s'"),
839 given_config_source.file);
840 else
841 die(_("error processing config file(s)"));
844 else if (actions == ACTION_EDIT) {
845 char *config_file;
847 check_argc(argc, 0, 0);
848 if (!given_config_source.file && nongit)
849 die(_("not in a git directory"));
850 if (given_config_source.use_stdin)
851 die(_("editing stdin is not supported"));
852 if (given_config_source.blob)
853 die(_("editing blobs is not supported"));
854 git_config(git_default_config, NULL);
855 config_file = given_config_source.file ?
856 xstrdup(given_config_source.file) :
857 git_pathdup("config");
858 if (use_global_config) {
859 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
860 if (fd >= 0) {
861 char *content = default_user_config();
862 write_str_in_full(fd, content);
863 free(content);
864 close(fd);
866 else if (errno != EEXIST)
867 die_errno(_("cannot create configuration file %s"), config_file);
869 launch_editor(config_file, NULL, NULL);
870 free(config_file);
872 else if (actions == ACTION_SET) {
873 check_write();
874 check_argc(argc, 2, 2);
875 value = normalize_value(argv[0], argv[1]);
876 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
877 if (ret == CONFIG_NOTHING_SET)
878 error(_("cannot overwrite multiple values with a single value\n"
879 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
881 else if (actions == ACTION_SET_ALL) {
882 check_write();
883 check_argc(argc, 2, 3);
884 value = normalize_value(argv[0], argv[1]);
885 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
886 argv[0], value, argv[2],
887 flags);
889 else if (actions == ACTION_ADD) {
890 check_write();
891 check_argc(argc, 2, 2);
892 value = normalize_value(argv[0], argv[1]);
893 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
894 argv[0], value,
895 CONFIG_REGEX_NONE,
896 flags);
898 else if (actions == ACTION_REPLACE_ALL) {
899 check_write();
900 check_argc(argc, 2, 3);
901 value = normalize_value(argv[0], argv[1]);
902 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
903 argv[0], value, argv[2],
904 flags | CONFIG_FLAGS_MULTI_REPLACE);
906 else if (actions == ACTION_GET) {
907 check_argc(argc, 1, 2);
908 return get_value(argv[0], argv[1], flags);
910 else if (actions == ACTION_GET_ALL) {
911 do_all = 1;
912 check_argc(argc, 1, 2);
913 return get_value(argv[0], argv[1], flags);
915 else if (actions == ACTION_GET_REGEXP) {
916 show_keys = 1;
917 use_key_regexp = 1;
918 do_all = 1;
919 check_argc(argc, 1, 2);
920 return get_value(argv[0], argv[1], flags);
922 else if (actions == ACTION_GET_URLMATCH) {
923 check_argc(argc, 2, 2);
924 return get_urlmatch(argv[0], argv[1]);
926 else if (actions == ACTION_UNSET) {
927 check_write();
928 check_argc(argc, 1, 2);
929 if (argc == 2)
930 return git_config_set_multivar_in_file_gently(given_config_source.file,
931 argv[0], NULL, argv[1],
932 flags);
933 else
934 return git_config_set_in_file_gently(given_config_source.file,
935 argv[0], NULL);
937 else if (actions == ACTION_UNSET_ALL) {
938 check_write();
939 check_argc(argc, 1, 2);
940 return git_config_set_multivar_in_file_gently(given_config_source.file,
941 argv[0], NULL, argv[1],
942 flags | CONFIG_FLAGS_MULTI_REPLACE);
944 else if (actions == ACTION_RENAME_SECTION) {
945 check_write();
946 check_argc(argc, 2, 2);
947 ret = git_config_rename_section_in_file(given_config_source.file,
948 argv[0], argv[1]);
949 if (ret < 0)
950 return ret;
951 else if (!ret)
952 die(_("no such section: %s"), argv[0]);
953 else
954 ret = 0;
956 else if (actions == ACTION_REMOVE_SECTION) {
957 check_write();
958 check_argc(argc, 1, 1);
959 ret = git_config_rename_section_in_file(given_config_source.file,
960 argv[0], NULL);
961 if (ret < 0)
962 return ret;
963 else if (!ret)
964 die(_("no such section: %s"), argv[0]);
965 else
966 ret = 0;
968 else if (actions == ACTION_GET_COLOR) {
969 check_argc(argc, 1, 2);
970 get_color(argv[0], argv[1]);
972 else if (actions == ACTION_GET_COLORBOOL) {
973 check_argc(argc, 1, 2);
974 if (argc == 2)
975 color_stdout_is_tty = git_config_bool("command line", argv[1]);
976 return get_colorbool(argv[0], argc == 2);
979 free(value);
980 return ret;