clone_submodule: avoid using `access()` on directories
[git/gitster.git] / builtin / config.c
blob753e5fac297e08763317adcab2fefe386ea68421
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "color.h"
5 #include "parse-options.h"
6 #include "urlmatch.h"
7 #include "quote.h"
8 #include "worktree.h"
10 static const char *const builtin_config_usage[] = {
11 N_("git config [<options>]"),
12 NULL
15 static char *key;
16 static regex_t *key_regexp;
17 static const char *value_pattern;
18 static regex_t *regexp;
19 static int show_keys;
20 static int omit_values;
21 static int use_key_regexp;
22 static int do_all;
23 static int do_not_match;
24 static char delim = '=';
25 static char key_delim = ' ';
26 static char term = '\n';
28 static int use_global_config, use_system_config, use_local_config;
29 static int use_worktree_config;
30 static struct git_config_source given_config_source;
31 static int actions, type;
32 static char *default_value;
33 static int end_nul;
34 static int respect_includes_opt = -1;
35 static struct config_options config_options;
36 static int show_origin;
37 static int show_scope;
38 static int fixed_value;
40 #define ACTION_GET (1<<0)
41 #define ACTION_GET_ALL (1<<1)
42 #define ACTION_GET_REGEXP (1<<2)
43 #define ACTION_REPLACE_ALL (1<<3)
44 #define ACTION_ADD (1<<4)
45 #define ACTION_UNSET (1<<5)
46 #define ACTION_UNSET_ALL (1<<6)
47 #define ACTION_RENAME_SECTION (1<<7)
48 #define ACTION_REMOVE_SECTION (1<<8)
49 #define ACTION_LIST (1<<9)
50 #define ACTION_EDIT (1<<10)
51 #define ACTION_SET (1<<11)
52 #define ACTION_SET_ALL (1<<12)
53 #define ACTION_GET_COLOR (1<<13)
54 #define ACTION_GET_COLORBOOL (1<<14)
55 #define ACTION_GET_URLMATCH (1<<15)
58 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
59 * one line of output and which should therefore be paged.
61 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
62 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
64 #define TYPE_BOOL 1
65 #define TYPE_INT 2
66 #define TYPE_BOOL_OR_INT 3
67 #define TYPE_PATH 4
68 #define TYPE_EXPIRY_DATE 5
69 #define TYPE_COLOR 6
70 #define TYPE_BOOL_OR_STR 7
72 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
73 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
74 PARSE_OPT_NONEG, option_parse_type, (i) }
76 static NORETURN void usage_builtin_config(void);
78 static int option_parse_type(const struct option *opt, const char *arg,
79 int unset)
81 int new_type, *to_type;
83 if (unset) {
84 *((int *) opt->value) = 0;
85 return 0;
89 * To support '--<type>' style flags, begin with new_type equal to
90 * opt->defval.
92 new_type = opt->defval;
93 if (!new_type) {
94 if (!strcmp(arg, "bool"))
95 new_type = TYPE_BOOL;
96 else if (!strcmp(arg, "int"))
97 new_type = TYPE_INT;
98 else if (!strcmp(arg, "bool-or-int"))
99 new_type = TYPE_BOOL_OR_INT;
100 else if (!strcmp(arg, "bool-or-str"))
101 new_type = TYPE_BOOL_OR_STR;
102 else if (!strcmp(arg, "path"))
103 new_type = TYPE_PATH;
104 else if (!strcmp(arg, "expiry-date"))
105 new_type = TYPE_EXPIRY_DATE;
106 else if (!strcmp(arg, "color"))
107 new_type = TYPE_COLOR;
108 else
109 die(_("unrecognized --type argument, %s"), arg);
112 to_type = opt->value;
113 if (*to_type && *to_type != new_type) {
115 * Complain when there is a new type not equal to the old type.
116 * This allows for combinations like '--int --type=int' and
117 * '--type=int --type=int', but disallows ones like '--type=bool
118 * --int' and '--type=bool
119 * --type=int'.
121 error(_("only one type at a time"));
122 usage_builtin_config();
124 *to_type = new_type;
126 return 0;
129 static struct option builtin_config_options[] = {
130 OPT_GROUP(N_("Config file location")),
131 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
132 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
133 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
134 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
135 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
136 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
137 OPT_GROUP(N_("Action")),
138 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
139 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
140 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
141 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
142 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
143 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
144 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
145 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
146 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
147 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
148 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
149 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
150 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
151 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
152 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
153 OPT_GROUP(N_("Type")),
154 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
155 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
156 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
157 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
158 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
159 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
160 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
161 OPT_GROUP(N_("Other")),
162 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
163 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
164 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
165 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
166 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
167 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
168 OPT_END(),
171 static NORETURN void usage_builtin_config(void)
173 usage_with_options(builtin_config_usage, builtin_config_options);
176 static void check_argc(int argc, int min, int max)
178 if (argc >= min && argc <= max)
179 return;
180 if (min == max)
181 error(_("wrong number of arguments, should be %d"), min);
182 else
183 error(_("wrong number of arguments, should be from %d to %d"),
184 min, max);
185 usage_builtin_config();
188 static void show_config_origin(struct strbuf *buf)
190 const char term = end_nul ? '\0' : '\t';
192 strbuf_addstr(buf, current_config_origin_type());
193 strbuf_addch(buf, ':');
194 if (end_nul)
195 strbuf_addstr(buf, current_config_name());
196 else
197 quote_c_style(current_config_name(), buf, NULL, 0);
198 strbuf_addch(buf, term);
201 static void show_config_scope(struct strbuf *buf)
203 const char term = end_nul ? '\0' : '\t';
204 const char *scope = config_scope_name(current_config_scope());
206 strbuf_addstr(buf, N_(scope));
207 strbuf_addch(buf, term);
210 static int show_all_config(const char *key_, const char *value_,
211 void *cb UNUSED)
213 if (show_origin || show_scope) {
214 struct strbuf buf = STRBUF_INIT;
215 if (show_scope)
216 show_config_scope(&buf);
217 if (show_origin)
218 show_config_origin(&buf);
219 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
220 fwrite(buf.buf, 1, buf.len, stdout);
221 strbuf_release(&buf);
223 if (!omit_values && value_)
224 printf("%s%c%s%c", key_, delim, value_, term);
225 else
226 printf("%s%c", key_, term);
227 return 0;
230 struct strbuf_list {
231 struct strbuf *items;
232 int nr;
233 int alloc;
236 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
238 if (show_scope)
239 show_config_scope(buf);
240 if (show_origin)
241 show_config_origin(buf);
242 if (show_keys)
243 strbuf_addstr(buf, key_);
244 if (!omit_values) {
245 if (show_keys)
246 strbuf_addch(buf, key_delim);
248 if (type == TYPE_INT)
249 strbuf_addf(buf, "%"PRId64,
250 git_config_int64(key_, value_ ? value_ : ""));
251 else if (type == TYPE_BOOL)
252 strbuf_addstr(buf, git_config_bool(key_, value_) ?
253 "true" : "false");
254 else if (type == TYPE_BOOL_OR_INT) {
255 int is_bool, v;
256 v = git_config_bool_or_int(key_, value_, &is_bool);
257 if (is_bool)
258 strbuf_addstr(buf, v ? "true" : "false");
259 else
260 strbuf_addf(buf, "%d", v);
261 } else if (type == TYPE_BOOL_OR_STR) {
262 int v = git_parse_maybe_bool(value_);
263 if (v < 0)
264 strbuf_addstr(buf, value_);
265 else
266 strbuf_addstr(buf, v ? "true" : "false");
267 } else if (type == TYPE_PATH) {
268 const char *v;
269 if (git_config_pathname(&v, key_, value_) < 0)
270 return -1;
271 strbuf_addstr(buf, v);
272 free((char *)v);
273 } else if (type == TYPE_EXPIRY_DATE) {
274 timestamp_t t;
275 if (git_config_expiry_date(&t, key_, value_) < 0)
276 return -1;
277 strbuf_addf(buf, "%"PRItime, t);
278 } else if (type == TYPE_COLOR) {
279 char v[COLOR_MAXLEN];
280 if (git_config_color(v, key_, value_) < 0)
281 return -1;
282 strbuf_addstr(buf, v);
283 } else if (value_) {
284 strbuf_addstr(buf, value_);
285 } else {
286 /* Just show the key name; back out delimiter */
287 if (show_keys)
288 strbuf_setlen(buf, buf->len - 1);
291 strbuf_addch(buf, term);
292 return 0;
295 static int collect_config(const char *key_, const char *value_, void *cb)
297 struct strbuf_list *values = cb;
299 if (!use_key_regexp && strcmp(key_, key))
300 return 0;
301 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
302 return 0;
303 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
304 return 0;
305 if (regexp != NULL &&
306 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
307 return 0;
309 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
310 strbuf_init(&values->items[values->nr], 0);
312 return format_config(&values->items[values->nr++], key_, value_);
315 static int get_value(const char *key_, const char *regex_, unsigned flags)
317 int ret = CONFIG_GENERIC_ERROR;
318 struct strbuf_list values = {NULL};
319 int i;
321 if (use_key_regexp) {
322 char *tl;
325 * NEEDSWORK: this naive pattern lowercasing obviously does not
326 * work for more complex patterns like "^[^.]*Foo.*bar".
327 * Perhaps we should deprecate this altogether someday.
330 key = xstrdup(key_);
331 for (tl = key + strlen(key) - 1;
332 tl >= key && *tl != '.';
333 tl--)
334 *tl = tolower(*tl);
335 for (tl = key; *tl && *tl != '.'; tl++)
336 *tl = tolower(*tl);
338 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
339 if (regcomp(key_regexp, key, REG_EXTENDED)) {
340 error(_("invalid key pattern: %s"), key_);
341 FREE_AND_NULL(key_regexp);
342 ret = CONFIG_INVALID_PATTERN;
343 goto free_strings;
345 } else {
346 if (git_config_parse_key(key_, &key, NULL)) {
347 ret = CONFIG_INVALID_KEY;
348 goto free_strings;
352 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
353 value_pattern = regex_;
354 else if (regex_) {
355 if (regex_[0] == '!') {
356 do_not_match = 1;
357 regex_++;
360 regexp = (regex_t*)xmalloc(sizeof(regex_t));
361 if (regcomp(regexp, regex_, REG_EXTENDED)) {
362 error(_("invalid pattern: %s"), regex_);
363 FREE_AND_NULL(regexp);
364 ret = CONFIG_INVALID_PATTERN;
365 goto free_strings;
369 config_with_options(collect_config, &values,
370 &given_config_source, &config_options);
372 if (!values.nr && default_value) {
373 struct strbuf *item;
374 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
375 item = &values.items[values.nr++];
376 strbuf_init(item, 0);
377 if (format_config(item, key_, default_value) < 0)
378 die(_("failed to format default config value: %s"),
379 default_value);
382 ret = !values.nr;
384 for (i = 0; i < values.nr; i++) {
385 struct strbuf *buf = values.items + i;
386 if (do_all || i == values.nr - 1)
387 fwrite(buf->buf, 1, buf->len, stdout);
388 strbuf_release(buf);
390 free(values.items);
392 free_strings:
393 free(key);
394 if (key_regexp) {
395 regfree(key_regexp);
396 free(key_regexp);
398 if (regexp) {
399 regfree(regexp);
400 free(regexp);
403 return ret;
406 static char *normalize_value(const char *key, const char *value)
408 if (!value)
409 return NULL;
411 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
413 * We don't do normalization for TYPE_PATH here: If
414 * the path is like ~/foobar/, we prefer to store
415 * "~/foobar/" in the config file, and to expand the ~
416 * when retrieving the value.
417 * Also don't do normalization for expiry dates.
419 return xstrdup(value);
420 if (type == TYPE_INT)
421 return xstrfmt("%"PRId64, git_config_int64(key, value));
422 if (type == TYPE_BOOL)
423 return xstrdup(git_config_bool(key, value) ? "true" : "false");
424 if (type == TYPE_BOOL_OR_INT) {
425 int is_bool, v;
426 v = git_config_bool_or_int(key, value, &is_bool);
427 if (!is_bool)
428 return xstrfmt("%d", v);
429 else
430 return xstrdup(v ? "true" : "false");
432 if (type == TYPE_BOOL_OR_STR) {
433 int v = git_parse_maybe_bool(value);
434 if (v < 0)
435 return xstrdup(value);
436 else
437 return xstrdup(v ? "true" : "false");
439 if (type == TYPE_COLOR) {
440 char v[COLOR_MAXLEN];
441 if (git_config_color(v, key, value))
442 die(_("cannot parse color '%s'"), value);
445 * The contents of `v` now contain an ANSI escape
446 * sequence, not suitable for including within a
447 * configuration file. Treat the above as a
448 * "sanity-check", and return the given value, which we
449 * know is representable as valid color code.
451 return xstrdup(value);
454 BUG("cannot normalize type %d", type);
457 static int get_color_found;
458 static const char *get_color_slot;
459 static const char *get_colorbool_slot;
460 static char parsed_color[COLOR_MAXLEN];
462 static int git_get_color_config(const char *var, const char *value,
463 void *cb UNUSED)
465 if (!strcmp(var, get_color_slot)) {
466 if (!value)
467 config_error_nonbool(var);
468 if (color_parse(value, parsed_color) < 0)
469 return -1;
470 get_color_found = 1;
472 return 0;
475 static void get_color(const char *var, const char *def_color)
477 get_color_slot = var;
478 get_color_found = 0;
479 parsed_color[0] = '\0';
480 config_with_options(git_get_color_config, NULL,
481 &given_config_source, &config_options);
483 if (!get_color_found && def_color) {
484 if (color_parse(def_color, parsed_color) < 0)
485 die(_("unable to parse default color value"));
488 fputs(parsed_color, stdout);
491 static int get_colorbool_found;
492 static int get_diff_color_found;
493 static int get_color_ui_found;
494 static int git_get_colorbool_config(const char *var, const char *value,
495 void *data UNUSED)
497 if (!strcmp(var, get_colorbool_slot))
498 get_colorbool_found = git_config_colorbool(var, value);
499 else if (!strcmp(var, "diff.color"))
500 get_diff_color_found = git_config_colorbool(var, value);
501 else if (!strcmp(var, "color.ui"))
502 get_color_ui_found = git_config_colorbool(var, value);
503 return 0;
506 static int get_colorbool(const char *var, int print)
508 get_colorbool_slot = var;
509 get_colorbool_found = -1;
510 get_diff_color_found = -1;
511 get_color_ui_found = -1;
512 config_with_options(git_get_colorbool_config, NULL,
513 &given_config_source, &config_options);
515 if (get_colorbool_found < 0) {
516 if (!strcmp(get_colorbool_slot, "color.diff"))
517 get_colorbool_found = get_diff_color_found;
518 if (get_colorbool_found < 0)
519 get_colorbool_found = get_color_ui_found;
522 if (get_colorbool_found < 0)
523 /* default value if none found in config */
524 get_colorbool_found = GIT_COLOR_AUTO;
526 get_colorbool_found = want_color(get_colorbool_found);
528 if (print) {
529 printf("%s\n", get_colorbool_found ? "true" : "false");
530 return 0;
531 } else
532 return get_colorbool_found ? 0 : 1;
535 static void check_write(void)
537 if (!given_config_source.file && !startup_info->have_repository)
538 die(_("not in a git directory"));
540 if (given_config_source.use_stdin)
541 die(_("writing to stdin is not supported"));
543 if (given_config_source.blob)
544 die(_("writing config blobs is not supported"));
547 struct urlmatch_current_candidate_value {
548 char value_is_null;
549 struct strbuf value;
552 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
554 struct string_list *values = cb;
555 struct string_list_item *item = string_list_insert(values, var);
556 struct urlmatch_current_candidate_value *matched = item->util;
558 if (!matched) {
559 matched = xmalloc(sizeof(*matched));
560 strbuf_init(&matched->value, 0);
561 item->util = matched;
562 } else {
563 strbuf_reset(&matched->value);
566 if (value) {
567 strbuf_addstr(&matched->value, value);
568 matched->value_is_null = 0;
569 } else {
570 matched->value_is_null = 1;
572 return 0;
575 static int get_urlmatch(const char *var, const char *url)
577 int ret;
578 char *section_tail;
579 struct string_list_item *item;
580 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
581 struct string_list values = STRING_LIST_INIT_DUP;
583 config.collect_fn = urlmatch_collect_fn;
584 config.cascade_fn = NULL;
585 config.cb = &values;
587 if (!url_normalize(url, &config.url))
588 die("%s", config.url.err);
590 config.section = xstrdup_tolower(var);
591 section_tail = strchr(config.section, '.');
592 if (section_tail) {
593 *section_tail = '\0';
594 config.key = section_tail + 1;
595 show_keys = 0;
596 } else {
597 config.key = NULL;
598 show_keys = 1;
601 config_with_options(urlmatch_config_entry, &config,
602 &given_config_source, &config_options);
604 ret = !values.nr;
606 for_each_string_list_item(item, &values) {
607 struct urlmatch_current_candidate_value *matched = item->util;
608 struct strbuf buf = STRBUF_INIT;
610 format_config(&buf, item->string,
611 matched->value_is_null ? NULL : matched->value.buf);
612 fwrite(buf.buf, 1, buf.len, stdout);
613 strbuf_release(&buf);
615 strbuf_release(&matched->value);
617 urlmatch_config_release(&config);
618 string_list_clear(&values, 1);
619 free(config.url.url);
621 free((void *)config.section);
622 return ret;
625 static char *default_user_config(void)
627 struct strbuf buf = STRBUF_INIT;
628 strbuf_addf(&buf,
629 _("# This is Git's per-user configuration file.\n"
630 "[user]\n"
631 "# Please adapt and uncomment the following lines:\n"
632 "# name = %s\n"
633 "# email = %s\n"),
634 ident_default_name(),
635 ident_default_email());
636 return strbuf_detach(&buf, NULL);
639 int cmd_config(int argc, const char **argv, const char *prefix)
641 int nongit = !startup_info->have_repository;
642 char *value;
643 int flags = 0;
645 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
647 argc = parse_options(argc, argv, prefix, builtin_config_options,
648 builtin_config_usage,
649 PARSE_OPT_STOP_AT_NON_OPTION);
651 if (use_global_config + use_system_config + use_local_config +
652 use_worktree_config +
653 !!given_config_source.file + !!given_config_source.blob > 1) {
654 error(_("only one config file at a time"));
655 usage_builtin_config();
658 if (nongit) {
659 if (use_local_config)
660 die(_("--local can only be used inside a git repository"));
661 if (given_config_source.blob)
662 die(_("--blob can only be used inside a git repository"));
663 if (use_worktree_config)
664 die(_("--worktree can only be used inside a git repository"));
668 if (given_config_source.file &&
669 !strcmp(given_config_source.file, "-")) {
670 given_config_source.file = NULL;
671 given_config_source.use_stdin = 1;
672 given_config_source.scope = CONFIG_SCOPE_COMMAND;
675 if (use_global_config) {
676 char *user_config, *xdg_config;
678 git_global_config(&user_config, &xdg_config);
679 if (!user_config)
681 * It is unknown if HOME/.gitconfig exists, so
682 * we do not know if we should write to XDG
683 * location; error out even if XDG_CONFIG_HOME
684 * is set and points at a sane location.
686 die(_("$HOME not set"));
688 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
690 if (access_or_warn(user_config, R_OK, 0) &&
691 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
692 given_config_source.file = xdg_config;
693 free(user_config);
694 } else {
695 given_config_source.file = user_config;
696 free(xdg_config);
699 else if (use_system_config) {
700 given_config_source.file = git_system_config();
701 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
702 } else if (use_local_config) {
703 given_config_source.file = git_pathdup("config");
704 given_config_source.scope = CONFIG_SCOPE_LOCAL;
705 } else if (use_worktree_config) {
706 struct worktree **worktrees = get_worktrees();
707 if (repository_format_worktree_config)
708 given_config_source.file = git_pathdup("config.worktree");
709 else if (worktrees[0] && worktrees[1])
710 die(_("--worktree cannot be used with multiple "
711 "working trees unless the config\n"
712 "extension worktreeConfig is enabled. "
713 "Please read \"CONFIGURATION FILE\"\n"
714 "section in \"git help worktree\" for details"));
715 else
716 given_config_source.file = git_pathdup("config");
717 given_config_source.scope = CONFIG_SCOPE_LOCAL;
718 free_worktrees(worktrees);
719 } else if (given_config_source.file) {
720 if (!is_absolute_path(given_config_source.file) && prefix)
721 given_config_source.file =
722 prefix_filename(prefix, given_config_source.file);
723 given_config_source.scope = CONFIG_SCOPE_COMMAND;
724 } else if (given_config_source.blob) {
725 given_config_source.scope = CONFIG_SCOPE_COMMAND;
729 if (respect_includes_opt == -1)
730 config_options.respect_includes = !given_config_source.file;
731 else
732 config_options.respect_includes = respect_includes_opt;
733 if (!nongit) {
734 config_options.commondir = get_git_common_dir();
735 config_options.git_dir = get_git_dir();
738 if (end_nul) {
739 term = '\0';
740 delim = '\n';
741 key_delim = '\n';
744 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
745 error(_("--get-color and variable type are incoherent"));
746 usage_builtin_config();
749 if (HAS_MULTI_BITS(actions)) {
750 error(_("only one action at a time"));
751 usage_builtin_config();
753 if (actions == 0)
754 switch (argc) {
755 case 1: actions = ACTION_GET; break;
756 case 2: actions = ACTION_SET; break;
757 case 3: actions = ACTION_SET_ALL; break;
758 default:
759 usage_builtin_config();
761 if (omit_values &&
762 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
763 error(_("--name-only is only applicable to --list or --get-regexp"));
764 usage_builtin_config();
767 if (show_origin && !(actions &
768 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
769 error(_("--show-origin is only applicable to --get, --get-all, "
770 "--get-regexp, and --list"));
771 usage_builtin_config();
774 if (default_value && !(actions & ACTION_GET)) {
775 error(_("--default is only applicable to --get"));
776 usage_builtin_config();
779 /* check usage of --fixed-value */
780 if (fixed_value) {
781 int allowed_usage = 0;
783 switch (actions) {
784 /* git config --get <name> <value-pattern> */
785 case ACTION_GET:
786 /* git config --get-all <name> <value-pattern> */
787 case ACTION_GET_ALL:
788 /* git config --get-regexp <name-pattern> <value-pattern> */
789 case ACTION_GET_REGEXP:
790 /* git config --unset <name> <value-pattern> */
791 case ACTION_UNSET:
792 /* git config --unset-all <name> <value-pattern> */
793 case ACTION_UNSET_ALL:
794 allowed_usage = argc > 1 && !!argv[1];
795 break;
797 /* git config <name> <value> <value-pattern> */
798 case ACTION_SET_ALL:
799 /* git config --replace-all <name> <value> <value-pattern> */
800 case ACTION_REPLACE_ALL:
801 allowed_usage = argc > 2 && !!argv[2];
802 break;
804 /* other options don't allow --fixed-value */
807 if (!allowed_usage) {
808 error(_("--fixed-value only applies with 'value-pattern'"));
809 usage_builtin_config();
812 flags |= CONFIG_FLAGS_FIXED_VALUE;
815 if (actions & PAGING_ACTIONS)
816 setup_auto_pager("config", 1);
818 if (actions == ACTION_LIST) {
819 check_argc(argc, 0, 0);
820 if (config_with_options(show_all_config, NULL,
821 &given_config_source,
822 &config_options) < 0) {
823 if (given_config_source.file)
824 die_errno(_("unable to read config file '%s'"),
825 given_config_source.file);
826 else
827 die(_("error processing config file(s)"));
830 else if (actions == ACTION_EDIT) {
831 char *config_file;
833 check_argc(argc, 0, 0);
834 if (!given_config_source.file && nongit)
835 die(_("not in a git directory"));
836 if (given_config_source.use_stdin)
837 die(_("editing stdin is not supported"));
838 if (given_config_source.blob)
839 die(_("editing blobs is not supported"));
840 git_config(git_default_config, NULL);
841 config_file = given_config_source.file ?
842 xstrdup(given_config_source.file) :
843 git_pathdup("config");
844 if (use_global_config) {
845 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
846 if (fd >= 0) {
847 char *content = default_user_config();
848 write_str_in_full(fd, content);
849 free(content);
850 close(fd);
852 else if (errno != EEXIST)
853 die_errno(_("cannot create configuration file %s"), config_file);
855 launch_editor(config_file, NULL, NULL);
856 free(config_file);
858 else if (actions == ACTION_SET) {
859 int ret;
860 check_write();
861 check_argc(argc, 2, 2);
862 value = normalize_value(argv[0], argv[1]);
863 UNLEAK(value);
864 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
865 if (ret == CONFIG_NOTHING_SET)
866 error(_("cannot overwrite multiple values with a single value\n"
867 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
868 return ret;
870 else if (actions == ACTION_SET_ALL) {
871 check_write();
872 check_argc(argc, 2, 3);
873 value = normalize_value(argv[0], argv[1]);
874 UNLEAK(value);
875 return git_config_set_multivar_in_file_gently(given_config_source.file,
876 argv[0], value, argv[2],
877 flags);
879 else if (actions == ACTION_ADD) {
880 check_write();
881 check_argc(argc, 2, 2);
882 value = normalize_value(argv[0], argv[1]);
883 UNLEAK(value);
884 return git_config_set_multivar_in_file_gently(given_config_source.file,
885 argv[0], value,
886 CONFIG_REGEX_NONE,
887 flags);
889 else if (actions == ACTION_REPLACE_ALL) {
890 check_write();
891 check_argc(argc, 2, 3);
892 value = normalize_value(argv[0], argv[1]);
893 UNLEAK(value);
894 return git_config_set_multivar_in_file_gently(given_config_source.file,
895 argv[0], value, argv[2],
896 flags | CONFIG_FLAGS_MULTI_REPLACE);
898 else if (actions == ACTION_GET) {
899 check_argc(argc, 1, 2);
900 return get_value(argv[0], argv[1], flags);
902 else if (actions == ACTION_GET_ALL) {
903 do_all = 1;
904 check_argc(argc, 1, 2);
905 return get_value(argv[0], argv[1], flags);
907 else if (actions == ACTION_GET_REGEXP) {
908 show_keys = 1;
909 use_key_regexp = 1;
910 do_all = 1;
911 check_argc(argc, 1, 2);
912 return get_value(argv[0], argv[1], flags);
914 else if (actions == ACTION_GET_URLMATCH) {
915 check_argc(argc, 2, 2);
916 return get_urlmatch(argv[0], argv[1]);
918 else if (actions == ACTION_UNSET) {
919 check_write();
920 check_argc(argc, 1, 2);
921 if (argc == 2)
922 return git_config_set_multivar_in_file_gently(given_config_source.file,
923 argv[0], NULL, argv[1],
924 flags);
925 else
926 return git_config_set_in_file_gently(given_config_source.file,
927 argv[0], NULL);
929 else if (actions == ACTION_UNSET_ALL) {
930 check_write();
931 check_argc(argc, 1, 2);
932 return git_config_set_multivar_in_file_gently(given_config_source.file,
933 argv[0], NULL, argv[1],
934 flags | CONFIG_FLAGS_MULTI_REPLACE);
936 else if (actions == ACTION_RENAME_SECTION) {
937 int ret;
938 check_write();
939 check_argc(argc, 2, 2);
940 ret = git_config_rename_section_in_file(given_config_source.file,
941 argv[0], argv[1]);
942 if (ret < 0)
943 return ret;
944 if (ret == 0)
945 die(_("no such section: %s"), argv[0]);
947 else if (actions == ACTION_REMOVE_SECTION) {
948 int ret;
949 check_write();
950 check_argc(argc, 1, 1);
951 ret = git_config_rename_section_in_file(given_config_source.file,
952 argv[0], NULL);
953 if (ret < 0)
954 return ret;
955 if (ret == 0)
956 die(_("no such section: %s"), argv[0]);
958 else if (actions == ACTION_GET_COLOR) {
959 check_argc(argc, 1, 2);
960 get_color(argv[0], argv[1]);
962 else if (actions == ACTION_GET_COLORBOOL) {
963 check_argc(argc, 1, 2);
964 if (argc == 2)
965 color_stdout_is_tty = git_config_bool("command line", argv[1]);
966 return get_colorbool(argv[0], argc == 2);
969 return 0;