t7701: make annotated tag unreachable
[git/debian.git] / builtin / config.c
blob9401f1e5e3b21d44386f623e0037f8385041d2a4
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 "quote.h"
13 #include "setup.h"
14 #include "worktree.h"
15 #include "wrapper.h"
17 static const char *const builtin_config_usage[] = {
18 N_("git config [<options>]"),
19 NULL
22 static char *key;
23 static regex_t *key_regexp;
24 static const char *value_pattern;
25 static regex_t *regexp;
26 static int show_keys;
27 static int omit_values;
28 static int use_key_regexp;
29 static int do_all;
30 static int do_not_match;
31 static char delim = '=';
32 static char key_delim = ' ';
33 static char term = '\n';
35 static int use_global_config, use_system_config, use_local_config;
36 static int use_worktree_config;
37 static struct git_config_source given_config_source;
38 static int actions, type;
39 static char *default_value;
40 static int end_nul;
41 static int respect_includes_opt = -1;
42 static struct config_options config_options;
43 static int show_origin;
44 static int show_scope;
45 static int fixed_value;
47 #define ACTION_GET (1<<0)
48 #define ACTION_GET_ALL (1<<1)
49 #define ACTION_GET_REGEXP (1<<2)
50 #define ACTION_REPLACE_ALL (1<<3)
51 #define ACTION_ADD (1<<4)
52 #define ACTION_UNSET (1<<5)
53 #define ACTION_UNSET_ALL (1<<6)
54 #define ACTION_RENAME_SECTION (1<<7)
55 #define ACTION_REMOVE_SECTION (1<<8)
56 #define ACTION_LIST (1<<9)
57 #define ACTION_EDIT (1<<10)
58 #define ACTION_SET (1<<11)
59 #define ACTION_SET_ALL (1<<12)
60 #define ACTION_GET_COLOR (1<<13)
61 #define ACTION_GET_COLORBOOL (1<<14)
62 #define ACTION_GET_URLMATCH (1<<15)
65 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
66 * one line of output and which should therefore be paged.
68 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
69 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
71 #define TYPE_BOOL 1
72 #define TYPE_INT 2
73 #define TYPE_BOOL_OR_INT 3
74 #define TYPE_PATH 4
75 #define TYPE_EXPIRY_DATE 5
76 #define TYPE_COLOR 6
77 #define TYPE_BOOL_OR_STR 7
79 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
80 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
81 PARSE_OPT_NONEG, option_parse_type, (i) }
83 static NORETURN void usage_builtin_config(void);
85 static int option_parse_type(const struct option *opt, const char *arg,
86 int unset)
88 int new_type, *to_type;
90 if (unset) {
91 *((int *) opt->value) = 0;
92 return 0;
96 * To support '--<type>' style flags, begin with new_type equal to
97 * opt->defval.
99 new_type = opt->defval;
100 if (!new_type) {
101 if (!strcmp(arg, "bool"))
102 new_type = TYPE_BOOL;
103 else if (!strcmp(arg, "int"))
104 new_type = TYPE_INT;
105 else if (!strcmp(arg, "bool-or-int"))
106 new_type = TYPE_BOOL_OR_INT;
107 else if (!strcmp(arg, "bool-or-str"))
108 new_type = TYPE_BOOL_OR_STR;
109 else if (!strcmp(arg, "path"))
110 new_type = TYPE_PATH;
111 else if (!strcmp(arg, "expiry-date"))
112 new_type = TYPE_EXPIRY_DATE;
113 else if (!strcmp(arg, "color"))
114 new_type = TYPE_COLOR;
115 else
116 die(_("unrecognized --type argument, %s"), arg);
119 to_type = opt->value;
120 if (*to_type && *to_type != new_type) {
122 * Complain when there is a new type not equal to the old type.
123 * This allows for combinations like '--int --type=int' and
124 * '--type=int --type=int', but disallows ones like '--type=bool
125 * --int' and '--type=bool
126 * --type=int'.
128 error(_("only one type at a time"));
129 usage_builtin_config();
131 *to_type = new_type;
133 return 0;
136 static struct option builtin_config_options[] = {
137 OPT_GROUP(N_("Config file location")),
138 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
139 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
140 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
141 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
142 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
143 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
144 OPT_GROUP(N_("Action")),
145 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
146 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
147 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
148 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
149 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
150 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
151 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
152 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
153 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
154 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
155 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
156 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
157 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
158 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
159 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
160 OPT_GROUP(N_("Type")),
161 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
162 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
163 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
164 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
165 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
166 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
167 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
168 OPT_GROUP(N_("Other")),
169 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
170 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
171 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
172 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
173 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
174 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
175 OPT_END(),
178 static NORETURN void usage_builtin_config(void)
180 usage_with_options(builtin_config_usage, builtin_config_options);
183 static void check_argc(int argc, int min, int max)
185 if (argc >= min && argc <= max)
186 return;
187 if (min == max)
188 error(_("wrong number of arguments, should be %d"), min);
189 else
190 error(_("wrong number of arguments, should be from %d to %d"),
191 min, max);
192 usage_builtin_config();
195 static void show_config_origin(struct strbuf *buf)
197 const char term = end_nul ? '\0' : '\t';
199 strbuf_addstr(buf, current_config_origin_type());
200 strbuf_addch(buf, ':');
201 if (end_nul)
202 strbuf_addstr(buf, current_config_name());
203 else
204 quote_c_style(current_config_name(), buf, NULL, 0);
205 strbuf_addch(buf, term);
208 static void show_config_scope(struct strbuf *buf)
210 const char term = end_nul ? '\0' : '\t';
211 const char *scope = config_scope_name(current_config_scope());
213 strbuf_addstr(buf, N_(scope));
214 strbuf_addch(buf, term);
217 static int show_all_config(const char *key_, const char *value_,
218 void *cb UNUSED)
220 if (show_origin || show_scope) {
221 struct strbuf buf = STRBUF_INIT;
222 if (show_scope)
223 show_config_scope(&buf);
224 if (show_origin)
225 show_config_origin(&buf);
226 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
227 fwrite(buf.buf, 1, buf.len, stdout);
228 strbuf_release(&buf);
230 if (!omit_values && value_)
231 printf("%s%c%s%c", key_, delim, value_, term);
232 else
233 printf("%s%c", key_, term);
234 return 0;
237 struct strbuf_list {
238 struct strbuf *items;
239 int nr;
240 int alloc;
243 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
245 if (show_scope)
246 show_config_scope(buf);
247 if (show_origin)
248 show_config_origin(buf);
249 if (show_keys)
250 strbuf_addstr(buf, key_);
251 if (!omit_values) {
252 if (show_keys)
253 strbuf_addch(buf, key_delim);
255 if (type == TYPE_INT)
256 strbuf_addf(buf, "%"PRId64,
257 git_config_int64(key_, value_ ? value_ : ""));
258 else if (type == TYPE_BOOL)
259 strbuf_addstr(buf, git_config_bool(key_, value_) ?
260 "true" : "false");
261 else if (type == TYPE_BOOL_OR_INT) {
262 int is_bool, v;
263 v = git_config_bool_or_int(key_, value_, &is_bool);
264 if (is_bool)
265 strbuf_addstr(buf, v ? "true" : "false");
266 else
267 strbuf_addf(buf, "%d", v);
268 } else if (type == TYPE_BOOL_OR_STR) {
269 int v = git_parse_maybe_bool(value_);
270 if (v < 0)
271 strbuf_addstr(buf, value_);
272 else
273 strbuf_addstr(buf, v ? "true" : "false");
274 } else if (type == TYPE_PATH) {
275 const char *v;
276 if (git_config_pathname(&v, key_, value_) < 0)
277 return -1;
278 strbuf_addstr(buf, v);
279 free((char *)v);
280 } else if (type == TYPE_EXPIRY_DATE) {
281 timestamp_t t;
282 if (git_config_expiry_date(&t, key_, value_) < 0)
283 return -1;
284 strbuf_addf(buf, "%"PRItime, t);
285 } else if (type == TYPE_COLOR) {
286 char v[COLOR_MAXLEN];
287 if (git_config_color(v, key_, value_) < 0)
288 return -1;
289 strbuf_addstr(buf, v);
290 } else if (value_) {
291 strbuf_addstr(buf, value_);
292 } else {
293 /* Just show the key name; back out delimiter */
294 if (show_keys)
295 strbuf_setlen(buf, buf->len - 1);
298 strbuf_addch(buf, term);
299 return 0;
302 static int collect_config(const char *key_, const char *value_, void *cb)
304 struct strbuf_list *values = cb;
306 if (!use_key_regexp && strcmp(key_, key))
307 return 0;
308 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
309 return 0;
310 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
311 return 0;
312 if (regexp != NULL &&
313 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
314 return 0;
316 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
317 strbuf_init(&values->items[values->nr], 0);
319 return format_config(&values->items[values->nr++], key_, value_);
322 static int get_value(const char *key_, const char *regex_, unsigned flags)
324 int ret = CONFIG_GENERIC_ERROR;
325 struct strbuf_list values = {NULL};
326 int i;
328 if (use_key_regexp) {
329 char *tl;
332 * NEEDSWORK: this naive pattern lowercasing obviously does not
333 * work for more complex patterns like "^[^.]*Foo.*bar".
334 * Perhaps we should deprecate this altogether someday.
337 key = xstrdup(key_);
338 for (tl = key + strlen(key) - 1;
339 tl >= key && *tl != '.';
340 tl--)
341 *tl = tolower(*tl);
342 for (tl = key; *tl && *tl != '.'; tl++)
343 *tl = tolower(*tl);
345 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
346 if (regcomp(key_regexp, key, REG_EXTENDED)) {
347 error(_("invalid key pattern: %s"), key_);
348 FREE_AND_NULL(key_regexp);
349 ret = CONFIG_INVALID_PATTERN;
350 goto free_strings;
352 } else {
353 if (git_config_parse_key(key_, &key, NULL)) {
354 ret = CONFIG_INVALID_KEY;
355 goto free_strings;
359 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
360 value_pattern = regex_;
361 else if (regex_) {
362 if (regex_[0] == '!') {
363 do_not_match = 1;
364 regex_++;
367 regexp = (regex_t*)xmalloc(sizeof(regex_t));
368 if (regcomp(regexp, regex_, REG_EXTENDED)) {
369 error(_("invalid pattern: %s"), regex_);
370 FREE_AND_NULL(regexp);
371 ret = CONFIG_INVALID_PATTERN;
372 goto free_strings;
376 config_with_options(collect_config, &values,
377 &given_config_source, &config_options);
379 if (!values.nr && default_value) {
380 struct strbuf *item;
381 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
382 item = &values.items[values.nr++];
383 strbuf_init(item, 0);
384 if (format_config(item, key_, default_value) < 0)
385 die(_("failed to format default config value: %s"),
386 default_value);
389 ret = !values.nr;
391 for (i = 0; i < values.nr; i++) {
392 struct strbuf *buf = values.items + i;
393 if (do_all || i == values.nr - 1)
394 fwrite(buf->buf, 1, buf->len, stdout);
395 strbuf_release(buf);
397 free(values.items);
399 free_strings:
400 free(key);
401 if (key_regexp) {
402 regfree(key_regexp);
403 free(key_regexp);
405 if (regexp) {
406 regfree(regexp);
407 free(regexp);
410 return ret;
413 static char *normalize_value(const char *key, const char *value)
415 if (!value)
416 return NULL;
418 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
420 * We don't do normalization for TYPE_PATH here: If
421 * the path is like ~/foobar/, we prefer to store
422 * "~/foobar/" in the config file, and to expand the ~
423 * when retrieving the value.
424 * Also don't do normalization for expiry dates.
426 return xstrdup(value);
427 if (type == TYPE_INT)
428 return xstrfmt("%"PRId64, git_config_int64(key, value));
429 if (type == TYPE_BOOL)
430 return xstrdup(git_config_bool(key, value) ? "true" : "false");
431 if (type == TYPE_BOOL_OR_INT) {
432 int is_bool, v;
433 v = git_config_bool_or_int(key, value, &is_bool);
434 if (!is_bool)
435 return xstrfmt("%d", v);
436 else
437 return xstrdup(v ? "true" : "false");
439 if (type == TYPE_BOOL_OR_STR) {
440 int v = git_parse_maybe_bool(value);
441 if (v < 0)
442 return xstrdup(value);
443 else
444 return xstrdup(v ? "true" : "false");
446 if (type == TYPE_COLOR) {
447 char v[COLOR_MAXLEN];
448 if (git_config_color(v, key, value))
449 die(_("cannot parse color '%s'"), value);
452 * The contents of `v` now contain an ANSI escape
453 * sequence, not suitable for including within a
454 * configuration file. Treat the above as a
455 * "sanity-check", and return the given value, which we
456 * know is representable as valid color code.
458 return xstrdup(value);
461 BUG("cannot normalize type %d", type);
464 static int get_color_found;
465 static const char *get_color_slot;
466 static const char *get_colorbool_slot;
467 static char parsed_color[COLOR_MAXLEN];
469 static int git_get_color_config(const char *var, const char *value,
470 void *cb UNUSED)
472 if (!strcmp(var, get_color_slot)) {
473 if (!value)
474 config_error_nonbool(var);
475 if (color_parse(value, parsed_color) < 0)
476 return -1;
477 get_color_found = 1;
479 return 0;
482 static void get_color(const char *var, const char *def_color)
484 get_color_slot = var;
485 get_color_found = 0;
486 parsed_color[0] = '\0';
487 config_with_options(git_get_color_config, NULL,
488 &given_config_source, &config_options);
490 if (!get_color_found && def_color) {
491 if (color_parse(def_color, parsed_color) < 0)
492 die(_("unable to parse default color value"));
495 fputs(parsed_color, stdout);
498 static int get_colorbool_found;
499 static int get_diff_color_found;
500 static int get_color_ui_found;
501 static int git_get_colorbool_config(const char *var, const char *value,
502 void *data UNUSED)
504 if (!strcmp(var, get_colorbool_slot))
505 get_colorbool_found = git_config_colorbool(var, value);
506 else if (!strcmp(var, "diff.color"))
507 get_diff_color_found = git_config_colorbool(var, value);
508 else if (!strcmp(var, "color.ui"))
509 get_color_ui_found = git_config_colorbool(var, value);
510 return 0;
513 static int get_colorbool(const char *var, int print)
515 get_colorbool_slot = var;
516 get_colorbool_found = -1;
517 get_diff_color_found = -1;
518 get_color_ui_found = -1;
519 config_with_options(git_get_colorbool_config, NULL,
520 &given_config_source, &config_options);
522 if (get_colorbool_found < 0) {
523 if (!strcmp(get_colorbool_slot, "color.diff"))
524 get_colorbool_found = get_diff_color_found;
525 if (get_colorbool_found < 0)
526 get_colorbool_found = get_color_ui_found;
529 if (get_colorbool_found < 0)
530 /* default value if none found in config */
531 get_colorbool_found = GIT_COLOR_AUTO;
533 get_colorbool_found = want_color(get_colorbool_found);
535 if (print) {
536 printf("%s\n", get_colorbool_found ? "true" : "false");
537 return 0;
538 } else
539 return get_colorbool_found ? 0 : 1;
542 static void check_write(void)
544 if (!given_config_source.file && !startup_info->have_repository)
545 die(_("not in a git directory"));
547 if (given_config_source.use_stdin)
548 die(_("writing to stdin is not supported"));
550 if (given_config_source.blob)
551 die(_("writing config blobs is not supported"));
554 struct urlmatch_current_candidate_value {
555 char value_is_null;
556 struct strbuf value;
559 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
561 struct string_list *values = cb;
562 struct string_list_item *item = string_list_insert(values, var);
563 struct urlmatch_current_candidate_value *matched = item->util;
565 if (!matched) {
566 matched = xmalloc(sizeof(*matched));
567 strbuf_init(&matched->value, 0);
568 item->util = matched;
569 } else {
570 strbuf_reset(&matched->value);
573 if (value) {
574 strbuf_addstr(&matched->value, value);
575 matched->value_is_null = 0;
576 } else {
577 matched->value_is_null = 1;
579 return 0;
582 static int get_urlmatch(const char *var, const char *url)
584 int ret;
585 char *section_tail;
586 struct string_list_item *item;
587 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
588 struct string_list values = STRING_LIST_INIT_DUP;
590 config.collect_fn = urlmatch_collect_fn;
591 config.cascade_fn = NULL;
592 config.cb = &values;
594 if (!url_normalize(url, &config.url))
595 die("%s", config.url.err);
597 config.section = xstrdup_tolower(var);
598 section_tail = strchr(config.section, '.');
599 if (section_tail) {
600 *section_tail = '\0';
601 config.key = section_tail + 1;
602 show_keys = 0;
603 } else {
604 config.key = NULL;
605 show_keys = 1;
608 config_with_options(urlmatch_config_entry, &config,
609 &given_config_source, &config_options);
611 ret = !values.nr;
613 for_each_string_list_item(item, &values) {
614 struct urlmatch_current_candidate_value *matched = item->util;
615 struct strbuf buf = STRBUF_INIT;
617 format_config(&buf, item->string,
618 matched->value_is_null ? NULL : matched->value.buf);
619 fwrite(buf.buf, 1, buf.len, stdout);
620 strbuf_release(&buf);
622 strbuf_release(&matched->value);
624 urlmatch_config_release(&config);
625 string_list_clear(&values, 1);
626 free(config.url.url);
628 free((void *)config.section);
629 return ret;
632 static char *default_user_config(void)
634 struct strbuf buf = STRBUF_INIT;
635 strbuf_addf(&buf,
636 _("# This is Git's per-user configuration file.\n"
637 "[user]\n"
638 "# Please adapt and uncomment the following lines:\n"
639 "# name = %s\n"
640 "# email = %s\n"),
641 ident_default_name(),
642 ident_default_email());
643 return strbuf_detach(&buf, NULL);
646 int cmd_config(int argc, const char **argv, const char *prefix)
648 int nongit = !startup_info->have_repository;
649 char *value = NULL;
650 int flags = 0;
651 int ret = 0;
653 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
655 argc = parse_options(argc, argv, prefix, builtin_config_options,
656 builtin_config_usage,
657 PARSE_OPT_STOP_AT_NON_OPTION);
659 if (use_global_config + use_system_config + use_local_config +
660 use_worktree_config +
661 !!given_config_source.file + !!given_config_source.blob > 1) {
662 error(_("only one config file at a time"));
663 usage_builtin_config();
666 if (nongit) {
667 if (use_local_config)
668 die(_("--local can only be used inside a git repository"));
669 if (given_config_source.blob)
670 die(_("--blob can only be used inside a git repository"));
671 if (use_worktree_config)
672 die(_("--worktree can only be used inside a git repository"));
676 if (given_config_source.file &&
677 !strcmp(given_config_source.file, "-")) {
678 given_config_source.file = NULL;
679 given_config_source.use_stdin = 1;
680 given_config_source.scope = CONFIG_SCOPE_COMMAND;
683 if (use_global_config) {
684 char *user_config, *xdg_config;
686 git_global_config(&user_config, &xdg_config);
687 if (!user_config)
689 * It is unknown if HOME/.gitconfig exists, so
690 * we do not know if we should write to XDG
691 * location; error out even if XDG_CONFIG_HOME
692 * is set and points at a sane location.
694 die(_("$HOME not set"));
696 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
698 if (access_or_warn(user_config, R_OK, 0) &&
699 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
700 given_config_source.file = xdg_config;
701 free(user_config);
702 } else {
703 given_config_source.file = user_config;
704 free(xdg_config);
707 else if (use_system_config) {
708 given_config_source.file = git_system_config();
709 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
710 } else if (use_local_config) {
711 given_config_source.file = git_pathdup("config");
712 given_config_source.scope = CONFIG_SCOPE_LOCAL;
713 } else if (use_worktree_config) {
714 struct worktree **worktrees = get_worktrees();
715 if (repository_format_worktree_config)
716 given_config_source.file = git_pathdup("config.worktree");
717 else if (worktrees[0] && worktrees[1])
718 die(_("--worktree cannot be used with multiple "
719 "working trees unless the config\n"
720 "extension worktreeConfig is enabled. "
721 "Please read \"CONFIGURATION FILE\"\n"
722 "section in \"git help worktree\" for details"));
723 else
724 given_config_source.file = git_pathdup("config");
725 given_config_source.scope = CONFIG_SCOPE_LOCAL;
726 free_worktrees(worktrees);
727 } else if (given_config_source.file) {
728 if (!is_absolute_path(given_config_source.file) && prefix)
729 given_config_source.file =
730 prefix_filename(prefix, given_config_source.file);
731 given_config_source.scope = CONFIG_SCOPE_COMMAND;
732 } else if (given_config_source.blob) {
733 given_config_source.scope = CONFIG_SCOPE_COMMAND;
737 if (respect_includes_opt == -1)
738 config_options.respect_includes = !given_config_source.file;
739 else
740 config_options.respect_includes = respect_includes_opt;
741 if (!nongit) {
742 config_options.commondir = get_git_common_dir();
743 config_options.git_dir = get_git_dir();
746 if (end_nul) {
747 term = '\0';
748 delim = '\n';
749 key_delim = '\n';
752 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
753 error(_("--get-color and variable type are incoherent"));
754 usage_builtin_config();
757 if (HAS_MULTI_BITS(actions)) {
758 error(_("only one action at a time"));
759 usage_builtin_config();
761 if (actions == 0)
762 switch (argc) {
763 case 1: actions = ACTION_GET; break;
764 case 2: actions = ACTION_SET; break;
765 case 3: actions = ACTION_SET_ALL; break;
766 default:
767 usage_builtin_config();
769 if (omit_values &&
770 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
771 error(_("--name-only is only applicable to --list or --get-regexp"));
772 usage_builtin_config();
775 if (show_origin && !(actions &
776 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
777 error(_("--show-origin is only applicable to --get, --get-all, "
778 "--get-regexp, and --list"));
779 usage_builtin_config();
782 if (default_value && !(actions & ACTION_GET)) {
783 error(_("--default is only applicable to --get"));
784 usage_builtin_config();
787 /* check usage of --fixed-value */
788 if (fixed_value) {
789 int allowed_usage = 0;
791 switch (actions) {
792 /* git config --get <name> <value-pattern> */
793 case ACTION_GET:
794 /* git config --get-all <name> <value-pattern> */
795 case ACTION_GET_ALL:
796 /* git config --get-regexp <name-pattern> <value-pattern> */
797 case ACTION_GET_REGEXP:
798 /* git config --unset <name> <value-pattern> */
799 case ACTION_UNSET:
800 /* git config --unset-all <name> <value-pattern> */
801 case ACTION_UNSET_ALL:
802 allowed_usage = argc > 1 && !!argv[1];
803 break;
805 /* git config <name> <value> <value-pattern> */
806 case ACTION_SET_ALL:
807 /* git config --replace-all <name> <value> <value-pattern> */
808 case ACTION_REPLACE_ALL:
809 allowed_usage = argc > 2 && !!argv[2];
810 break;
812 /* other options don't allow --fixed-value */
815 if (!allowed_usage) {
816 error(_("--fixed-value only applies with 'value-pattern'"));
817 usage_builtin_config();
820 flags |= CONFIG_FLAGS_FIXED_VALUE;
823 if (actions & PAGING_ACTIONS)
824 setup_auto_pager("config", 1);
826 if (actions == ACTION_LIST) {
827 check_argc(argc, 0, 0);
828 if (config_with_options(show_all_config, NULL,
829 &given_config_source,
830 &config_options) < 0) {
831 if (given_config_source.file)
832 die_errno(_("unable to read config file '%s'"),
833 given_config_source.file);
834 else
835 die(_("error processing config file(s)"));
838 else if (actions == ACTION_EDIT) {
839 char *config_file;
841 check_argc(argc, 0, 0);
842 if (!given_config_source.file && nongit)
843 die(_("not in a git directory"));
844 if (given_config_source.use_stdin)
845 die(_("editing stdin is not supported"));
846 if (given_config_source.blob)
847 die(_("editing blobs is not supported"));
848 git_config(git_default_config, NULL);
849 config_file = given_config_source.file ?
850 xstrdup(given_config_source.file) :
851 git_pathdup("config");
852 if (use_global_config) {
853 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
854 if (fd >= 0) {
855 char *content = default_user_config();
856 write_str_in_full(fd, content);
857 free(content);
858 close(fd);
860 else if (errno != EEXIST)
861 die_errno(_("cannot create configuration file %s"), config_file);
863 launch_editor(config_file, NULL, NULL);
864 free(config_file);
866 else if (actions == ACTION_SET) {
867 check_write();
868 check_argc(argc, 2, 2);
869 value = normalize_value(argv[0], argv[1]);
870 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
871 if (ret == CONFIG_NOTHING_SET)
872 error(_("cannot overwrite multiple values with a single value\n"
873 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
875 else if (actions == ACTION_SET_ALL) {
876 check_write();
877 check_argc(argc, 2, 3);
878 value = normalize_value(argv[0], argv[1]);
879 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
880 argv[0], value, argv[2],
881 flags);
883 else if (actions == ACTION_ADD) {
884 check_write();
885 check_argc(argc, 2, 2);
886 value = normalize_value(argv[0], argv[1]);
887 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
888 argv[0], value,
889 CONFIG_REGEX_NONE,
890 flags);
892 else if (actions == ACTION_REPLACE_ALL) {
893 check_write();
894 check_argc(argc, 2, 3);
895 value = normalize_value(argv[0], argv[1]);
896 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
897 argv[0], value, argv[2],
898 flags | CONFIG_FLAGS_MULTI_REPLACE);
900 else if (actions == ACTION_GET) {
901 check_argc(argc, 1, 2);
902 return get_value(argv[0], argv[1], flags);
904 else if (actions == ACTION_GET_ALL) {
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_REGEXP) {
910 show_keys = 1;
911 use_key_regexp = 1;
912 do_all = 1;
913 check_argc(argc, 1, 2);
914 return get_value(argv[0], argv[1], flags);
916 else if (actions == ACTION_GET_URLMATCH) {
917 check_argc(argc, 2, 2);
918 return get_urlmatch(argv[0], argv[1]);
920 else if (actions == ACTION_UNSET) {
921 check_write();
922 check_argc(argc, 1, 2);
923 if (argc == 2)
924 return git_config_set_multivar_in_file_gently(given_config_source.file,
925 argv[0], NULL, argv[1],
926 flags);
927 else
928 return git_config_set_in_file_gently(given_config_source.file,
929 argv[0], NULL);
931 else if (actions == ACTION_UNSET_ALL) {
932 check_write();
933 check_argc(argc, 1, 2);
934 return git_config_set_multivar_in_file_gently(given_config_source.file,
935 argv[0], NULL, argv[1],
936 flags | CONFIG_FLAGS_MULTI_REPLACE);
938 else if (actions == ACTION_RENAME_SECTION) {
939 check_write();
940 check_argc(argc, 2, 2);
941 ret = git_config_rename_section_in_file(given_config_source.file,
942 argv[0], argv[1]);
943 if (ret < 0)
944 return ret;
945 else if (!ret)
946 die(_("no such section: %s"), argv[0]);
947 else
948 ret = 0;
950 else if (actions == ACTION_REMOVE_SECTION) {
951 check_write();
952 check_argc(argc, 1, 1);
953 ret = git_config_rename_section_in_file(given_config_source.file,
954 argv[0], NULL);
955 if (ret < 0)
956 return ret;
957 else if (!ret)
958 die(_("no such section: %s"), argv[0]);
959 else
960 ret = 0;
962 else if (actions == ACTION_GET_COLOR) {
963 check_argc(argc, 1, 2);
964 get_color(argv[0], argv[1]);
966 else if (actions == ACTION_GET_COLORBOOL) {
967 check_argc(argc, 1, 2);
968 if (argc == 2)
969 color_stdout_is_tty = git_config_bool("command line", argv[1]);
970 return get_colorbool(argv[0], argc == 2);
973 free(value);
974 return ret;