t1300: add tests for missing keys
[alt-git.git] / builtin / config.c
blob060cf9f3e05e6718ae02923e132e4804dbdcb291
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 = NULL;
643 int flags = 0;
644 int ret = 0;
646 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
648 argc = parse_options(argc, argv, prefix, builtin_config_options,
649 builtin_config_usage,
650 PARSE_OPT_STOP_AT_NON_OPTION);
652 if (use_global_config + use_system_config + use_local_config +
653 use_worktree_config +
654 !!given_config_source.file + !!given_config_source.blob > 1) {
655 error(_("only one config file at a time"));
656 usage_builtin_config();
659 if (nongit) {
660 if (use_local_config)
661 die(_("--local can only be used inside a git repository"));
662 if (given_config_source.blob)
663 die(_("--blob can only be used inside a git repository"));
664 if (use_worktree_config)
665 die(_("--worktree can only be used inside a git repository"));
669 if (given_config_source.file &&
670 !strcmp(given_config_source.file, "-")) {
671 given_config_source.file = NULL;
672 given_config_source.use_stdin = 1;
673 given_config_source.scope = CONFIG_SCOPE_COMMAND;
676 if (use_global_config) {
677 char *user_config, *xdg_config;
679 git_global_config(&user_config, &xdg_config);
680 if (!user_config)
682 * It is unknown if HOME/.gitconfig exists, so
683 * we do not know if we should write to XDG
684 * location; error out even if XDG_CONFIG_HOME
685 * is set and points at a sane location.
687 die(_("$HOME not set"));
689 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
691 if (access_or_warn(user_config, R_OK, 0) &&
692 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
693 given_config_source.file = xdg_config;
694 free(user_config);
695 } else {
696 given_config_source.file = user_config;
697 free(xdg_config);
700 else if (use_system_config) {
701 given_config_source.file = git_system_config();
702 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
703 } else if (use_local_config) {
704 given_config_source.file = git_pathdup("config");
705 given_config_source.scope = CONFIG_SCOPE_LOCAL;
706 } else if (use_worktree_config) {
707 struct worktree **worktrees = get_worktrees();
708 if (repository_format_worktree_config)
709 given_config_source.file = git_pathdup("config.worktree");
710 else if (worktrees[0] && worktrees[1])
711 die(_("--worktree cannot be used with multiple "
712 "working trees unless the config\n"
713 "extension worktreeConfig is enabled. "
714 "Please read \"CONFIGURATION FILE\"\n"
715 "section in \"git help worktree\" for details"));
716 else
717 given_config_source.file = git_pathdup("config");
718 given_config_source.scope = CONFIG_SCOPE_LOCAL;
719 free_worktrees(worktrees);
720 } else if (given_config_source.file) {
721 if (!is_absolute_path(given_config_source.file) && prefix)
722 given_config_source.file =
723 prefix_filename(prefix, given_config_source.file);
724 given_config_source.scope = CONFIG_SCOPE_COMMAND;
725 } else if (given_config_source.blob) {
726 given_config_source.scope = CONFIG_SCOPE_COMMAND;
730 if (respect_includes_opt == -1)
731 config_options.respect_includes = !given_config_source.file;
732 else
733 config_options.respect_includes = respect_includes_opt;
734 if (!nongit) {
735 config_options.commondir = get_git_common_dir();
736 config_options.git_dir = get_git_dir();
739 if (end_nul) {
740 term = '\0';
741 delim = '\n';
742 key_delim = '\n';
745 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
746 error(_("--get-color and variable type are incoherent"));
747 usage_builtin_config();
750 if (HAS_MULTI_BITS(actions)) {
751 error(_("only one action at a time"));
752 usage_builtin_config();
754 if (actions == 0)
755 switch (argc) {
756 case 1: actions = ACTION_GET; break;
757 case 2: actions = ACTION_SET; break;
758 case 3: actions = ACTION_SET_ALL; break;
759 default:
760 usage_builtin_config();
762 if (omit_values &&
763 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
764 error(_("--name-only is only applicable to --list or --get-regexp"));
765 usage_builtin_config();
768 if (show_origin && !(actions &
769 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
770 error(_("--show-origin is only applicable to --get, --get-all, "
771 "--get-regexp, and --list"));
772 usage_builtin_config();
775 if (default_value && !(actions & ACTION_GET)) {
776 error(_("--default is only applicable to --get"));
777 usage_builtin_config();
780 /* check usage of --fixed-value */
781 if (fixed_value) {
782 int allowed_usage = 0;
784 switch (actions) {
785 /* git config --get <name> <value-pattern> */
786 case ACTION_GET:
787 /* git config --get-all <name> <value-pattern> */
788 case ACTION_GET_ALL:
789 /* git config --get-regexp <name-pattern> <value-pattern> */
790 case ACTION_GET_REGEXP:
791 /* git config --unset <name> <value-pattern> */
792 case ACTION_UNSET:
793 /* git config --unset-all <name> <value-pattern> */
794 case ACTION_UNSET_ALL:
795 allowed_usage = argc > 1 && !!argv[1];
796 break;
798 /* git config <name> <value> <value-pattern> */
799 case ACTION_SET_ALL:
800 /* git config --replace-all <name> <value> <value-pattern> */
801 case ACTION_REPLACE_ALL:
802 allowed_usage = argc > 2 && !!argv[2];
803 break;
805 /* other options don't allow --fixed-value */
808 if (!allowed_usage) {
809 error(_("--fixed-value only applies with 'value-pattern'"));
810 usage_builtin_config();
813 flags |= CONFIG_FLAGS_FIXED_VALUE;
816 if (actions & PAGING_ACTIONS)
817 setup_auto_pager("config", 1);
819 if (actions == ACTION_LIST) {
820 check_argc(argc, 0, 0);
821 if (config_with_options(show_all_config, NULL,
822 &given_config_source,
823 &config_options) < 0) {
824 if (given_config_source.file)
825 die_errno(_("unable to read config file '%s'"),
826 given_config_source.file);
827 else
828 die(_("error processing config file(s)"));
831 else if (actions == ACTION_EDIT) {
832 char *config_file;
834 check_argc(argc, 0, 0);
835 if (!given_config_source.file && nongit)
836 die(_("not in a git directory"));
837 if (given_config_source.use_stdin)
838 die(_("editing stdin is not supported"));
839 if (given_config_source.blob)
840 die(_("editing blobs is not supported"));
841 git_config(git_default_config, NULL);
842 config_file = given_config_source.file ?
843 xstrdup(given_config_source.file) :
844 git_pathdup("config");
845 if (use_global_config) {
846 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
847 if (fd >= 0) {
848 char *content = default_user_config();
849 write_str_in_full(fd, content);
850 free(content);
851 close(fd);
853 else if (errno != EEXIST)
854 die_errno(_("cannot create configuration file %s"), config_file);
856 launch_editor(config_file, NULL, NULL);
857 free(config_file);
859 else if (actions == ACTION_SET) {
860 check_write();
861 check_argc(argc, 2, 2);
862 value = normalize_value(argv[0], argv[1]);
863 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
864 if (ret == CONFIG_NOTHING_SET)
865 error(_("cannot overwrite multiple values with a single value\n"
866 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
868 else if (actions == ACTION_SET_ALL) {
869 check_write();
870 check_argc(argc, 2, 3);
871 value = normalize_value(argv[0], argv[1]);
872 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
873 argv[0], value, argv[2],
874 flags);
876 else if (actions == ACTION_ADD) {
877 check_write();
878 check_argc(argc, 2, 2);
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,
882 CONFIG_REGEX_NONE,
883 flags);
885 else if (actions == ACTION_REPLACE_ALL) {
886 check_write();
887 check_argc(argc, 2, 3);
888 value = normalize_value(argv[0], argv[1]);
889 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
890 argv[0], value, argv[2],
891 flags | CONFIG_FLAGS_MULTI_REPLACE);
893 else if (actions == ACTION_GET) {
894 check_argc(argc, 1, 2);
895 return get_value(argv[0], argv[1], flags);
897 else if (actions == ACTION_GET_ALL) {
898 do_all = 1;
899 check_argc(argc, 1, 2);
900 return get_value(argv[0], argv[1], flags);
902 else if (actions == ACTION_GET_REGEXP) {
903 show_keys = 1;
904 use_key_regexp = 1;
905 do_all = 1;
906 check_argc(argc, 1, 2);
907 return get_value(argv[0], argv[1], flags);
909 else if (actions == ACTION_GET_URLMATCH) {
910 check_argc(argc, 2, 2);
911 return get_urlmatch(argv[0], argv[1]);
913 else if (actions == ACTION_UNSET) {
914 check_write();
915 check_argc(argc, 1, 2);
916 if (argc == 2)
917 return git_config_set_multivar_in_file_gently(given_config_source.file,
918 argv[0], NULL, argv[1],
919 flags);
920 else
921 return git_config_set_in_file_gently(given_config_source.file,
922 argv[0], NULL);
924 else if (actions == ACTION_UNSET_ALL) {
925 check_write();
926 check_argc(argc, 1, 2);
927 return git_config_set_multivar_in_file_gently(given_config_source.file,
928 argv[0], NULL, argv[1],
929 flags | CONFIG_FLAGS_MULTI_REPLACE);
931 else if (actions == ACTION_RENAME_SECTION) {
932 check_write();
933 check_argc(argc, 2, 2);
934 ret = git_config_rename_section_in_file(given_config_source.file,
935 argv[0], argv[1]);
936 if (ret < 0)
937 return ret;
938 else if (!ret)
939 die(_("no such section: %s"), argv[0]);
940 else
941 ret = 0;
943 else if (actions == ACTION_REMOVE_SECTION) {
944 check_write();
945 check_argc(argc, 1, 1);
946 ret = git_config_rename_section_in_file(given_config_source.file,
947 argv[0], NULL);
948 if (ret < 0)
949 return ret;
950 else if (!ret)
951 die(_("no such section: %s"), argv[0]);
952 else
953 ret = 0;
955 else if (actions == ACTION_GET_COLOR) {
956 check_argc(argc, 1, 2);
957 get_color(argv[0], argv[1]);
959 else if (actions == ACTION_GET_COLORBOOL) {
960 check_argc(argc, 1, 2);
961 if (argc == 2)
962 color_stdout_is_tty = git_config_bool("command line", argv[1]);
963 return get_colorbool(argv[0], argc == 2);
966 free(value);
967 return ret;