builtin/config: pull out function to handle config location
[alt-git.git] / builtin / config.c
blob8c7cd30cb4675bbdbcd27b48301006352aa71a33
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "color.h"
5 #include "editor.h"
6 #include "environment.h"
7 #include "repository.h"
8 #include "gettext.h"
9 #include "ident.h"
10 #include "parse-options.h"
11 #include "urlmatch.h"
12 #include "path.h"
13 #include "quote.h"
14 #include "setup.h"
15 #include "strbuf.h"
16 #include "worktree.h"
18 static const char *const builtin_config_usage[] = {
19 N_("git config [<options>]"),
20 NULL
23 static char *key;
24 static regex_t *key_regexp;
25 static const char *value_pattern;
26 static regex_t *regexp;
27 static int show_keys;
28 static int omit_values;
29 static int use_key_regexp;
30 static int do_all;
31 static int do_not_match;
32 static char delim = '=';
33 static char key_delim = ' ';
34 static char term = '\n';
36 static int use_global_config, use_system_config, use_local_config;
37 static int use_worktree_config;
38 static struct git_config_source given_config_source;
39 static int actions, type;
40 static char *default_value;
41 static int end_nul;
42 static int respect_includes_opt = -1;
43 static struct config_options config_options;
44 static int show_origin;
45 static int show_scope;
46 static int fixed_value;
47 static const char *comment_arg;
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 void check_argc(int argc, int min, int max)
140 if (argc >= min && argc <= max)
141 return;
142 if (min == max)
143 error(_("wrong number of arguments, should be %d"), min);
144 else
145 error(_("wrong number of arguments, should be from %d to %d"),
146 min, max);
147 usage_builtin_config();
150 static void show_config_origin(const struct key_value_info *kvi,
151 struct strbuf *buf)
153 const char term = end_nul ? '\0' : '\t';
155 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
156 strbuf_addch(buf, ':');
157 if (end_nul)
158 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
159 else
160 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
161 strbuf_addch(buf, term);
164 static void show_config_scope(const struct key_value_info *kvi,
165 struct strbuf *buf)
167 const char term = end_nul ? '\0' : '\t';
168 const char *scope = config_scope_name(kvi->scope);
170 strbuf_addstr(buf, N_(scope));
171 strbuf_addch(buf, term);
174 static int show_all_config(const char *key_, const char *value_,
175 const struct config_context *ctx,
176 void *cb UNUSED)
178 const struct key_value_info *kvi = ctx->kvi;
180 if (show_origin || show_scope) {
181 struct strbuf buf = STRBUF_INIT;
182 if (show_scope)
183 show_config_scope(kvi, &buf);
184 if (show_origin)
185 show_config_origin(kvi, &buf);
186 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
187 fwrite(buf.buf, 1, buf.len, stdout);
188 strbuf_release(&buf);
190 if (!omit_values && value_)
191 printf("%s%c%s%c", key_, delim, value_, term);
192 else
193 printf("%s%c", key_, term);
194 return 0;
197 struct strbuf_list {
198 struct strbuf *items;
199 int nr;
200 int alloc;
203 static int format_config(struct strbuf *buf, const char *key_,
204 const char *value_, const struct key_value_info *kvi)
206 if (show_scope)
207 show_config_scope(kvi, buf);
208 if (show_origin)
209 show_config_origin(kvi, buf);
210 if (show_keys)
211 strbuf_addstr(buf, key_);
212 if (!omit_values) {
213 if (show_keys)
214 strbuf_addch(buf, key_delim);
216 if (type == TYPE_INT)
217 strbuf_addf(buf, "%"PRId64,
218 git_config_int64(key_, value_ ? value_ : "", kvi));
219 else if (type == TYPE_BOOL)
220 strbuf_addstr(buf, git_config_bool(key_, value_) ?
221 "true" : "false");
222 else if (type == TYPE_BOOL_OR_INT) {
223 int is_bool, v;
224 v = git_config_bool_or_int(key_, value_, kvi,
225 &is_bool);
226 if (is_bool)
227 strbuf_addstr(buf, v ? "true" : "false");
228 else
229 strbuf_addf(buf, "%d", v);
230 } else if (type == TYPE_BOOL_OR_STR) {
231 int v = git_parse_maybe_bool(value_);
232 if (v < 0)
233 strbuf_addstr(buf, value_);
234 else
235 strbuf_addstr(buf, v ? "true" : "false");
236 } else if (type == TYPE_PATH) {
237 const char *v;
238 if (git_config_pathname(&v, key_, value_) < 0)
239 return -1;
240 strbuf_addstr(buf, v);
241 free((char *)v);
242 } else if (type == TYPE_EXPIRY_DATE) {
243 timestamp_t t;
244 if (git_config_expiry_date(&t, key_, value_) < 0)
245 return -1;
246 strbuf_addf(buf, "%"PRItime, t);
247 } else if (type == TYPE_COLOR) {
248 char v[COLOR_MAXLEN];
249 if (git_config_color(v, key_, value_) < 0)
250 return -1;
251 strbuf_addstr(buf, v);
252 } else if (value_) {
253 strbuf_addstr(buf, value_);
254 } else {
255 /* Just show the key name; back out delimiter */
256 if (show_keys)
257 strbuf_setlen(buf, buf->len - 1);
260 strbuf_addch(buf, term);
261 return 0;
264 static int collect_config(const char *key_, const char *value_,
265 const struct config_context *ctx, void *cb)
267 struct strbuf_list *values = cb;
268 const struct key_value_info *kvi = ctx->kvi;
270 if (!use_key_regexp && strcmp(key_, key))
271 return 0;
272 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
273 return 0;
274 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
275 return 0;
276 if (regexp != NULL &&
277 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
278 return 0;
280 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
281 strbuf_init(&values->items[values->nr], 0);
283 return format_config(&values->items[values->nr++], key_, value_, kvi);
286 static int get_value(const char *key_, const char *regex_, unsigned flags)
288 int ret = CONFIG_GENERIC_ERROR;
289 struct strbuf_list values = {NULL};
290 int i;
292 if (use_key_regexp) {
293 char *tl;
296 * NEEDSWORK: this naive pattern lowercasing obviously does not
297 * work for more complex patterns like "^[^.]*Foo.*bar".
298 * Perhaps we should deprecate this altogether someday.
301 key = xstrdup(key_);
302 for (tl = key + strlen(key) - 1;
303 tl >= key && *tl != '.';
304 tl--)
305 *tl = tolower(*tl);
306 for (tl = key; *tl && *tl != '.'; tl++)
307 *tl = tolower(*tl);
309 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
310 if (regcomp(key_regexp, key, REG_EXTENDED)) {
311 error(_("invalid key pattern: %s"), key_);
312 FREE_AND_NULL(key_regexp);
313 ret = CONFIG_INVALID_PATTERN;
314 goto free_strings;
316 } else {
317 if (git_config_parse_key(key_, &key, NULL)) {
318 ret = CONFIG_INVALID_KEY;
319 goto free_strings;
323 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
324 value_pattern = regex_;
325 else if (regex_) {
326 if (regex_[0] == '!') {
327 do_not_match = 1;
328 regex_++;
331 regexp = (regex_t*)xmalloc(sizeof(regex_t));
332 if (regcomp(regexp, regex_, REG_EXTENDED)) {
333 error(_("invalid pattern: %s"), regex_);
334 FREE_AND_NULL(regexp);
335 ret = CONFIG_INVALID_PATTERN;
336 goto free_strings;
340 config_with_options(collect_config, &values,
341 &given_config_source, the_repository,
342 &config_options);
344 if (!values.nr && default_value) {
345 struct key_value_info kvi = KVI_INIT;
346 struct strbuf *item;
348 kvi_from_param(&kvi);
349 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
350 item = &values.items[values.nr++];
351 strbuf_init(item, 0);
352 if (format_config(item, key_, default_value, &kvi) < 0)
353 die(_("failed to format default config value: %s"),
354 default_value);
357 ret = !values.nr;
359 for (i = 0; i < values.nr; i++) {
360 struct strbuf *buf = values.items + i;
361 if (do_all || i == values.nr - 1)
362 fwrite(buf->buf, 1, buf->len, stdout);
363 strbuf_release(buf);
365 free(values.items);
367 free_strings:
368 free(key);
369 if (key_regexp) {
370 regfree(key_regexp);
371 free(key_regexp);
373 if (regexp) {
374 regfree(regexp);
375 free(regexp);
378 return ret;
381 static char *normalize_value(const char *key, const char *value,
382 struct key_value_info *kvi)
384 if (!value)
385 return NULL;
387 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
389 * We don't do normalization for TYPE_PATH here: If
390 * the path is like ~/foobar/, we prefer to store
391 * "~/foobar/" in the config file, and to expand the ~
392 * when retrieving the value.
393 * Also don't do normalization for expiry dates.
395 return xstrdup(value);
396 if (type == TYPE_INT)
397 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
398 if (type == TYPE_BOOL)
399 return xstrdup(git_config_bool(key, value) ? "true" : "false");
400 if (type == TYPE_BOOL_OR_INT) {
401 int is_bool, v;
402 v = git_config_bool_or_int(key, value, kvi, &is_bool);
403 if (!is_bool)
404 return xstrfmt("%d", v);
405 else
406 return xstrdup(v ? "true" : "false");
408 if (type == TYPE_BOOL_OR_STR) {
409 int v = git_parse_maybe_bool(value);
410 if (v < 0)
411 return xstrdup(value);
412 else
413 return xstrdup(v ? "true" : "false");
415 if (type == TYPE_COLOR) {
416 char v[COLOR_MAXLEN];
417 if (git_config_color(v, key, value))
418 die(_("cannot parse color '%s'"), value);
421 * The contents of `v` now contain an ANSI escape
422 * sequence, not suitable for including within a
423 * configuration file. Treat the above as a
424 * "sanity-check", and return the given value, which we
425 * know is representable as valid color code.
427 return xstrdup(value);
430 BUG("cannot normalize type %d", type);
433 static int get_color_found;
434 static const char *get_color_slot;
435 static const char *get_colorbool_slot;
436 static char parsed_color[COLOR_MAXLEN];
438 static int git_get_color_config(const char *var, const char *value,
439 const struct config_context *ctx UNUSED,
440 void *cb UNUSED)
442 if (!strcmp(var, get_color_slot)) {
443 if (!value)
444 config_error_nonbool(var);
445 if (color_parse(value, parsed_color) < 0)
446 return -1;
447 get_color_found = 1;
449 return 0;
452 static void get_color(const char *var, const char *def_color)
454 get_color_slot = var;
455 get_color_found = 0;
456 parsed_color[0] = '\0';
457 config_with_options(git_get_color_config, NULL,
458 &given_config_source, the_repository,
459 &config_options);
461 if (!get_color_found && def_color) {
462 if (color_parse(def_color, parsed_color) < 0)
463 die(_("unable to parse default color value"));
466 fputs(parsed_color, stdout);
469 static int get_colorbool_found;
470 static int get_diff_color_found;
471 static int get_color_ui_found;
472 static int git_get_colorbool_config(const char *var, const char *value,
473 const struct config_context *ctx UNUSED,
474 void *data UNUSED)
476 if (!strcmp(var, get_colorbool_slot))
477 get_colorbool_found = git_config_colorbool(var, value);
478 else if (!strcmp(var, "diff.color"))
479 get_diff_color_found = git_config_colorbool(var, value);
480 else if (!strcmp(var, "color.ui"))
481 get_color_ui_found = git_config_colorbool(var, value);
482 return 0;
485 static int get_colorbool(const char *var, int print)
487 get_colorbool_slot = var;
488 get_colorbool_found = -1;
489 get_diff_color_found = -1;
490 get_color_ui_found = -1;
491 config_with_options(git_get_colorbool_config, NULL,
492 &given_config_source, the_repository,
493 &config_options);
495 if (get_colorbool_found < 0) {
496 if (!strcmp(get_colorbool_slot, "color.diff"))
497 get_colorbool_found = get_diff_color_found;
498 if (get_colorbool_found < 0)
499 get_colorbool_found = get_color_ui_found;
502 if (get_colorbool_found < 0)
503 /* default value if none found in config */
504 get_colorbool_found = GIT_COLOR_AUTO;
506 get_colorbool_found = want_color(get_colorbool_found);
508 if (print) {
509 printf("%s\n", get_colorbool_found ? "true" : "false");
510 return 0;
511 } else
512 return get_colorbool_found ? 0 : 1;
515 static void check_write(void)
517 if (!given_config_source.file && !startup_info->have_repository)
518 die(_("not in a git directory"));
520 if (given_config_source.use_stdin)
521 die(_("writing to stdin is not supported"));
523 if (given_config_source.blob)
524 die(_("writing config blobs is not supported"));
527 struct urlmatch_current_candidate_value {
528 char value_is_null;
529 struct strbuf value;
530 struct key_value_info kvi;
533 static int urlmatch_collect_fn(const char *var, const char *value,
534 const struct config_context *ctx,
535 void *cb)
537 struct string_list *values = cb;
538 struct string_list_item *item = string_list_insert(values, var);
539 struct urlmatch_current_candidate_value *matched = item->util;
540 const struct key_value_info *kvi = ctx->kvi;
542 if (!matched) {
543 matched = xmalloc(sizeof(*matched));
544 strbuf_init(&matched->value, 0);
545 item->util = matched;
546 } else {
547 strbuf_reset(&matched->value);
549 matched->kvi = *kvi;
551 if (value) {
552 strbuf_addstr(&matched->value, value);
553 matched->value_is_null = 0;
554 } else {
555 matched->value_is_null = 1;
557 return 0;
560 static int get_urlmatch(const char *var, const char *url)
562 int ret;
563 char *section_tail;
564 struct string_list_item *item;
565 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
566 struct string_list values = STRING_LIST_INIT_DUP;
568 config.collect_fn = urlmatch_collect_fn;
569 config.cascade_fn = NULL;
570 config.cb = &values;
572 if (!url_normalize(url, &config.url))
573 die("%s", config.url.err);
575 config.section = xstrdup_tolower(var);
576 section_tail = strchr(config.section, '.');
577 if (section_tail) {
578 *section_tail = '\0';
579 config.key = section_tail + 1;
580 show_keys = 0;
581 } else {
582 config.key = NULL;
583 show_keys = 1;
586 config_with_options(urlmatch_config_entry, &config,
587 &given_config_source, the_repository,
588 &config_options);
590 ret = !values.nr;
592 for_each_string_list_item(item, &values) {
593 struct urlmatch_current_candidate_value *matched = item->util;
594 struct strbuf buf = STRBUF_INIT;
596 format_config(&buf, item->string,
597 matched->value_is_null ? NULL : matched->value.buf,
598 &matched->kvi);
599 fwrite(buf.buf, 1, buf.len, stdout);
600 strbuf_release(&buf);
602 strbuf_release(&matched->value);
604 urlmatch_config_release(&config);
605 string_list_clear(&values, 1);
606 free(config.url.url);
608 free((void *)config.section);
609 return ret;
612 static char *default_user_config(void)
614 struct strbuf buf = STRBUF_INIT;
615 strbuf_addf(&buf,
616 _("# This is Git's per-user configuration file.\n"
617 "[user]\n"
618 "# Please adapt and uncomment the following lines:\n"
619 "# name = %s\n"
620 "# email = %s\n"),
621 ident_default_name(),
622 ident_default_email());
623 return strbuf_detach(&buf, NULL);
626 static void handle_config_location(const char *prefix)
628 if (use_global_config + use_system_config + use_local_config +
629 use_worktree_config +
630 !!given_config_source.file + !!given_config_source.blob > 1) {
631 error(_("only one config file at a time"));
632 usage_builtin_config();
635 if (!startup_info->have_repository) {
636 if (use_local_config)
637 die(_("--local can only be used inside a git repository"));
638 if (given_config_source.blob)
639 die(_("--blob can only be used inside a git repository"));
640 if (use_worktree_config)
641 die(_("--worktree can only be used inside a git repository"));
644 if (given_config_source.file &&
645 !strcmp(given_config_source.file, "-")) {
646 given_config_source.file = NULL;
647 given_config_source.use_stdin = 1;
648 given_config_source.scope = CONFIG_SCOPE_COMMAND;
651 if (use_global_config) {
652 given_config_source.file = git_global_config();
653 if (!given_config_source.file)
655 * It is unknown if HOME/.gitconfig exists, so
656 * we do not know if we should write to XDG
657 * location; error out even if XDG_CONFIG_HOME
658 * is set and points at a sane location.
660 die(_("$HOME not set"));
661 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
662 } else if (use_system_config) {
663 given_config_source.file = git_system_config();
664 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
665 } else if (use_local_config) {
666 given_config_source.file = git_pathdup("config");
667 given_config_source.scope = CONFIG_SCOPE_LOCAL;
668 } else if (use_worktree_config) {
669 struct worktree **worktrees = get_worktrees();
670 if (the_repository->repository_format_worktree_config)
671 given_config_source.file = git_pathdup("config.worktree");
672 else if (worktrees[0] && worktrees[1])
673 die(_("--worktree cannot be used with multiple "
674 "working trees unless the config\n"
675 "extension worktreeConfig is enabled. "
676 "Please read \"CONFIGURATION FILE\"\n"
677 "section in \"git help worktree\" for details"));
678 else
679 given_config_source.file = git_pathdup("config");
680 given_config_source.scope = CONFIG_SCOPE_LOCAL;
681 free_worktrees(worktrees);
682 } else if (given_config_source.file) {
683 if (!is_absolute_path(given_config_source.file) && prefix)
684 given_config_source.file =
685 prefix_filename(prefix, given_config_source.file);
686 given_config_source.scope = CONFIG_SCOPE_COMMAND;
687 } else if (given_config_source.blob) {
688 given_config_source.scope = CONFIG_SCOPE_COMMAND;
691 if (respect_includes_opt == -1)
692 config_options.respect_includes = !given_config_source.file;
693 else
694 config_options.respect_includes = respect_includes_opt;
695 if (startup_info->have_repository) {
696 config_options.commondir = get_git_common_dir();
697 config_options.git_dir = get_git_dir();
701 static struct option builtin_config_options[] = {
702 OPT_GROUP(N_("Config file location")),
703 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
704 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
705 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
706 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
707 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
708 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
709 OPT_GROUP(N_("Action")),
710 OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
711 OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
712 OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
713 OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
714 OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
715 OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
716 OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
717 OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
718 OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
719 OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
720 OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
721 OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
722 OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
723 OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
724 OPT_GROUP(N_("Type")),
725 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
726 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
727 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
728 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
729 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
730 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
731 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
732 OPT_GROUP(N_("Other")),
733 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
734 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
735 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
736 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
737 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
738 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
739 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
740 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
741 OPT_END(),
744 static NORETURN void usage_builtin_config(void)
746 usage_with_options(builtin_config_usage, builtin_config_options);
749 int cmd_config(int argc, const char **argv, const char *prefix)
751 char *value = NULL, *comment = NULL;
752 int flags = 0;
753 int ret = 0;
754 struct key_value_info default_kvi = KVI_INIT;
756 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
758 argc = parse_options(argc, argv, prefix, builtin_config_options,
759 builtin_config_usage,
760 PARSE_OPT_STOP_AT_NON_OPTION);
762 handle_config_location(prefix);
764 if (end_nul) {
765 term = '\0';
766 delim = '\n';
767 key_delim = '\n';
770 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
771 error(_("--get-color and variable type are incoherent"));
772 usage_builtin_config();
775 if (actions == 0)
776 switch (argc) {
777 case 1: actions = ACTION_GET; break;
778 case 2: actions = ACTION_SET; break;
779 case 3: actions = ACTION_SET_ALL; break;
780 default:
781 usage_builtin_config();
783 if (omit_values &&
784 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
785 error(_("--name-only is only applicable to --list or --get-regexp"));
786 usage_builtin_config();
789 if (show_origin && !(actions &
790 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
791 error(_("--show-origin is only applicable to --get, --get-all, "
792 "--get-regexp, and --list"));
793 usage_builtin_config();
796 if (default_value && !(actions & ACTION_GET)) {
797 error(_("--default is only applicable to --get"));
798 usage_builtin_config();
801 if (comment_arg &&
802 !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) {
803 error(_("--comment is only applicable to add/set/replace operations"));
804 usage_builtin_config();
807 /* check usage of --fixed-value */
808 if (fixed_value) {
809 int allowed_usage = 0;
811 switch (actions) {
812 /* git config --get <name> <value-pattern> */
813 case ACTION_GET:
814 /* git config --get-all <name> <value-pattern> */
815 case ACTION_GET_ALL:
816 /* git config --get-regexp <name-pattern> <value-pattern> */
817 case ACTION_GET_REGEXP:
818 /* git config --unset <name> <value-pattern> */
819 case ACTION_UNSET:
820 /* git config --unset-all <name> <value-pattern> */
821 case ACTION_UNSET_ALL:
822 allowed_usage = argc > 1 && !!argv[1];
823 break;
825 /* git config <name> <value> <value-pattern> */
826 case ACTION_SET_ALL:
827 /* git config --replace-all <name> <value> <value-pattern> */
828 case ACTION_REPLACE_ALL:
829 allowed_usage = argc > 2 && !!argv[2];
830 break;
832 /* other options don't allow --fixed-value */
835 if (!allowed_usage) {
836 error(_("--fixed-value only applies with 'value-pattern'"));
837 usage_builtin_config();
840 flags |= CONFIG_FLAGS_FIXED_VALUE;
843 comment = git_config_prepare_comment_string(comment_arg);
845 if (actions & PAGING_ACTIONS)
846 setup_auto_pager("config", 1);
848 if (actions == ACTION_LIST) {
849 check_argc(argc, 0, 0);
850 if (config_with_options(show_all_config, NULL,
851 &given_config_source, the_repository,
852 &config_options) < 0) {
853 if (given_config_source.file)
854 die_errno(_("unable to read config file '%s'"),
855 given_config_source.file);
856 else
857 die(_("error processing config file(s)"));
860 else if (actions == ACTION_EDIT) {
861 char *config_file;
863 check_argc(argc, 0, 0);
864 if (!given_config_source.file && !startup_info->have_repository)
865 die(_("not in a git directory"));
866 if (given_config_source.use_stdin)
867 die(_("editing stdin is not supported"));
868 if (given_config_source.blob)
869 die(_("editing blobs is not supported"));
870 git_config(git_default_config, NULL);
871 config_file = given_config_source.file ?
872 xstrdup(given_config_source.file) :
873 git_pathdup("config");
874 if (use_global_config) {
875 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
876 if (fd >= 0) {
877 char *content = default_user_config();
878 write_str_in_full(fd, content);
879 free(content);
880 close(fd);
882 else if (errno != EEXIST)
883 die_errno(_("cannot create configuration file %s"), config_file);
885 launch_editor(config_file, NULL, NULL);
886 free(config_file);
888 else if (actions == ACTION_SET) {
889 check_write();
890 check_argc(argc, 2, 2);
891 value = normalize_value(argv[0], argv[1], &default_kvi);
892 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], comment, value);
893 if (ret == CONFIG_NOTHING_SET)
894 error(_("cannot overwrite multiple values with a single value\n"
895 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
897 else if (actions == ACTION_SET_ALL) {
898 check_write();
899 check_argc(argc, 2, 3);
900 value = normalize_value(argv[0], argv[1], &default_kvi);
901 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
902 argv[0], value, argv[2],
903 comment, flags);
905 else if (actions == ACTION_ADD) {
906 check_write();
907 check_argc(argc, 2, 2);
908 value = normalize_value(argv[0], argv[1], &default_kvi);
909 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
910 argv[0], value,
911 CONFIG_REGEX_NONE,
912 comment, flags);
914 else if (actions == ACTION_REPLACE_ALL) {
915 check_write();
916 check_argc(argc, 2, 3);
917 value = normalize_value(argv[0], argv[1], &default_kvi);
918 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
919 argv[0], value, argv[2],
920 comment, flags | CONFIG_FLAGS_MULTI_REPLACE);
922 else if (actions == ACTION_GET) {
923 check_argc(argc, 1, 2);
924 return get_value(argv[0], argv[1], flags);
926 else if (actions == ACTION_GET_ALL) {
927 do_all = 1;
928 check_argc(argc, 1, 2);
929 return get_value(argv[0], argv[1], flags);
931 else if (actions == ACTION_GET_REGEXP) {
932 show_keys = 1;
933 use_key_regexp = 1;
934 do_all = 1;
935 check_argc(argc, 1, 2);
936 return get_value(argv[0], argv[1], flags);
938 else if (actions == ACTION_GET_URLMATCH) {
939 check_argc(argc, 2, 2);
940 return get_urlmatch(argv[0], argv[1]);
942 else if (actions == ACTION_UNSET) {
943 check_write();
944 check_argc(argc, 1, 2);
945 if (argc == 2)
946 return git_config_set_multivar_in_file_gently(given_config_source.file,
947 argv[0], NULL, argv[1],
948 NULL, flags);
949 else
950 return git_config_set_in_file_gently(given_config_source.file,
951 argv[0], NULL, NULL);
953 else if (actions == ACTION_UNSET_ALL) {
954 check_write();
955 check_argc(argc, 1, 2);
956 return git_config_set_multivar_in_file_gently(given_config_source.file,
957 argv[0], NULL, argv[1],
958 NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
960 else if (actions == ACTION_RENAME_SECTION) {
961 check_write();
962 check_argc(argc, 2, 2);
963 ret = git_config_rename_section_in_file(given_config_source.file,
964 argv[0], argv[1]);
965 if (ret < 0)
966 return ret;
967 else if (!ret)
968 die(_("no such section: %s"), argv[0]);
969 else
970 ret = 0;
972 else if (actions == ACTION_REMOVE_SECTION) {
973 check_write();
974 check_argc(argc, 1, 1);
975 ret = git_config_rename_section_in_file(given_config_source.file,
976 argv[0], NULL);
977 if (ret < 0)
978 return ret;
979 else if (!ret)
980 die(_("no such section: %s"), argv[0]);
981 else
982 ret = 0;
984 else if (actions == ACTION_GET_COLOR) {
985 check_argc(argc, 1, 2);
986 get_color(argv[0], argv[1]);
988 else if (actions == ACTION_GET_COLORBOOL) {
989 check_argc(argc, 1, 2);
990 if (argc == 2)
991 color_stdout_is_tty = git_config_bool("command line", argv[1]);
992 return get_colorbool(argv[0], argc == 2);
995 free(comment);
996 free(value);
997 return ret;