Merge branch 'sh/mergetool-hideresolved'
[alt-git.git] / builtin / config.c
blobf71fa39b38eecd558f8d246b0a20cfa84fea00cc
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_("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_, void *cb)
212 if (show_origin || show_scope) {
213 struct strbuf buf = STRBUF_INIT;
214 if (show_scope)
215 show_config_scope(&buf);
216 if (show_origin)
217 show_config_origin(&buf);
218 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
219 fwrite(buf.buf, 1, buf.len, stdout);
220 strbuf_release(&buf);
222 if (!omit_values && value_)
223 printf("%s%c%s%c", key_, delim, value_, term);
224 else
225 printf("%s%c", key_, term);
226 return 0;
229 struct strbuf_list {
230 struct strbuf *items;
231 int nr;
232 int alloc;
235 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
237 if (show_scope)
238 show_config_scope(buf);
239 if (show_origin)
240 show_config_origin(buf);
241 if (show_keys)
242 strbuf_addstr(buf, key_);
243 if (!omit_values) {
244 if (show_keys)
245 strbuf_addch(buf, key_delim);
247 if (type == TYPE_INT)
248 strbuf_addf(buf, "%"PRId64,
249 git_config_int64(key_, value_ ? value_ : ""));
250 else if (type == TYPE_BOOL)
251 strbuf_addstr(buf, git_config_bool(key_, value_) ?
252 "true" : "false");
253 else if (type == TYPE_BOOL_OR_INT) {
254 int is_bool, v;
255 v = git_config_bool_or_int(key_, value_, &is_bool);
256 if (is_bool)
257 strbuf_addstr(buf, v ? "true" : "false");
258 else
259 strbuf_addf(buf, "%d", v);
260 } else if (type == TYPE_BOOL_OR_STR) {
261 int v = git_parse_maybe_bool(value_);
262 if (v < 0)
263 strbuf_addstr(buf, value_);
264 else
265 strbuf_addstr(buf, v ? "true" : "false");
266 } else if (type == TYPE_PATH) {
267 const char *v;
268 if (git_config_pathname(&v, key_, value_) < 0)
269 return -1;
270 strbuf_addstr(buf, v);
271 free((char *)v);
272 } else if (type == TYPE_EXPIRY_DATE) {
273 timestamp_t t;
274 if (git_config_expiry_date(&t, key_, value_) < 0)
275 return -1;
276 strbuf_addf(buf, "%"PRItime, t);
277 } else if (type == TYPE_COLOR) {
278 char v[COLOR_MAXLEN];
279 if (git_config_color(v, key_, value_) < 0)
280 return -1;
281 strbuf_addstr(buf, v);
282 } else if (value_) {
283 strbuf_addstr(buf, value_);
284 } else {
285 /* Just show the key name; back out delimiter */
286 if (show_keys)
287 strbuf_setlen(buf, buf->len - 1);
290 strbuf_addch(buf, term);
291 return 0;
294 static int collect_config(const char *key_, const char *value_, void *cb)
296 struct strbuf_list *values = cb;
298 if (!use_key_regexp && strcmp(key_, key))
299 return 0;
300 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
301 return 0;
302 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
303 return 0;
304 if (regexp != NULL &&
305 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
306 return 0;
308 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
309 strbuf_init(&values->items[values->nr], 0);
311 return format_config(&values->items[values->nr++], key_, value_);
314 static int get_value(const char *key_, const char *regex_, unsigned flags)
316 int ret = CONFIG_GENERIC_ERROR;
317 struct strbuf_list values = {NULL};
318 int i;
320 if (use_key_regexp) {
321 char *tl;
324 * NEEDSWORK: this naive pattern lowercasing obviously does not
325 * work for more complex patterns like "^[^.]*Foo.*bar".
326 * Perhaps we should deprecate this altogether someday.
329 key = xstrdup(key_);
330 for (tl = key + strlen(key) - 1;
331 tl >= key && *tl != '.';
332 tl--)
333 *tl = tolower(*tl);
334 for (tl = key; *tl && *tl != '.'; tl++)
335 *tl = tolower(*tl);
337 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
338 if (regcomp(key_regexp, key, REG_EXTENDED)) {
339 error(_("invalid key pattern: %s"), key_);
340 FREE_AND_NULL(key_regexp);
341 ret = CONFIG_INVALID_PATTERN;
342 goto free_strings;
344 } else {
345 if (git_config_parse_key(key_, &key, NULL)) {
346 ret = CONFIG_INVALID_KEY;
347 goto free_strings;
351 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
352 value_pattern = regex_;
353 else if (regex_) {
354 if (regex_[0] == '!') {
355 do_not_match = 1;
356 regex_++;
359 regexp = (regex_t*)xmalloc(sizeof(regex_t));
360 if (regcomp(regexp, regex_, REG_EXTENDED)) {
361 error(_("invalid pattern: %s"), regex_);
362 FREE_AND_NULL(regexp);
363 ret = CONFIG_INVALID_PATTERN;
364 goto free_strings;
368 config_with_options(collect_config, &values,
369 &given_config_source, &config_options);
371 if (!values.nr && default_value) {
372 struct strbuf *item;
373 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
374 item = &values.items[values.nr++];
375 strbuf_init(item, 0);
376 if (format_config(item, key_, default_value) < 0)
377 die(_("failed to format default config value: %s"),
378 default_value);
381 ret = !values.nr;
383 for (i = 0; i < values.nr; i++) {
384 struct strbuf *buf = values.items + i;
385 if (do_all || i == values.nr - 1)
386 fwrite(buf->buf, 1, buf->len, stdout);
387 strbuf_release(buf);
389 free(values.items);
391 free_strings:
392 free(key);
393 if (key_regexp) {
394 regfree(key_regexp);
395 free(key_regexp);
397 if (regexp) {
398 regfree(regexp);
399 free(regexp);
402 return ret;
405 static char *normalize_value(const char *key, const char *value)
407 if (!value)
408 return NULL;
410 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
412 * We don't do normalization for TYPE_PATH here: If
413 * the path is like ~/foobar/, we prefer to store
414 * "~/foobar/" in the config file, and to expand the ~
415 * when retrieving the value.
416 * Also don't do normalization for expiry dates.
418 return xstrdup(value);
419 if (type == TYPE_INT)
420 return xstrfmt("%"PRId64, git_config_int64(key, value));
421 if (type == TYPE_BOOL)
422 return xstrdup(git_config_bool(key, value) ? "true" : "false");
423 if (type == TYPE_BOOL_OR_INT) {
424 int is_bool, v;
425 v = git_config_bool_or_int(key, value, &is_bool);
426 if (!is_bool)
427 return xstrfmt("%d", v);
428 else
429 return xstrdup(v ? "true" : "false");
431 if (type == TYPE_BOOL_OR_STR) {
432 int v = git_parse_maybe_bool(value);
433 if (v < 0)
434 return xstrdup(value);
435 else
436 return xstrdup(v ? "true" : "false");
438 if (type == TYPE_COLOR) {
439 char v[COLOR_MAXLEN];
440 if (git_config_color(v, key, value))
441 die(_("cannot parse color '%s'"), value);
444 * The contents of `v` now contain an ANSI escape
445 * sequence, not suitable for including within a
446 * configuration file. Treat the above as a
447 * "sanity-check", and return the given value, which we
448 * know is representable as valid color code.
450 return xstrdup(value);
453 BUG("cannot normalize type %d", type);
456 static int get_color_found;
457 static const char *get_color_slot;
458 static const char *get_colorbool_slot;
459 static char parsed_color[COLOR_MAXLEN];
461 static int git_get_color_config(const char *var, const char *value, void *cb)
463 if (!strcmp(var, get_color_slot)) {
464 if (!value)
465 config_error_nonbool(var);
466 if (color_parse(value, parsed_color) < 0)
467 return -1;
468 get_color_found = 1;
470 return 0;
473 static void get_color(const char *var, const char *def_color)
475 get_color_slot = var;
476 get_color_found = 0;
477 parsed_color[0] = '\0';
478 config_with_options(git_get_color_config, NULL,
479 &given_config_source, &config_options);
481 if (!get_color_found && def_color) {
482 if (color_parse(def_color, parsed_color) < 0)
483 die(_("unable to parse default color value"));
486 fputs(parsed_color, stdout);
489 static int get_colorbool_found;
490 static int get_diff_color_found;
491 static int get_color_ui_found;
492 static int git_get_colorbool_config(const char *var, const char *value,
493 void *cb)
495 if (!strcmp(var, get_colorbool_slot))
496 get_colorbool_found = git_config_colorbool(var, value);
497 else if (!strcmp(var, "diff.color"))
498 get_diff_color_found = git_config_colorbool(var, value);
499 else if (!strcmp(var, "color.ui"))
500 get_color_ui_found = git_config_colorbool(var, value);
501 return 0;
504 static int get_colorbool(const char *var, int print)
506 get_colorbool_slot = var;
507 get_colorbool_found = -1;
508 get_diff_color_found = -1;
509 get_color_ui_found = -1;
510 config_with_options(git_get_colorbool_config, NULL,
511 &given_config_source, &config_options);
513 if (get_colorbool_found < 0) {
514 if (!strcmp(get_colorbool_slot, "color.diff"))
515 get_colorbool_found = get_diff_color_found;
516 if (get_colorbool_found < 0)
517 get_colorbool_found = get_color_ui_found;
520 if (get_colorbool_found < 0)
521 /* default value if none found in config */
522 get_colorbool_found = GIT_COLOR_AUTO;
524 get_colorbool_found = want_color(get_colorbool_found);
526 if (print) {
527 printf("%s\n", get_colorbool_found ? "true" : "false");
528 return 0;
529 } else
530 return get_colorbool_found ? 0 : 1;
533 static void check_write(void)
535 if (!given_config_source.file && !startup_info->have_repository)
536 die(_("not in a git directory"));
538 if (given_config_source.use_stdin)
539 die(_("writing to stdin is not supported"));
541 if (given_config_source.blob)
542 die(_("writing config blobs is not supported"));
545 struct urlmatch_current_candidate_value {
546 char value_is_null;
547 struct strbuf value;
550 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
552 struct string_list *values = cb;
553 struct string_list_item *item = string_list_insert(values, var);
554 struct urlmatch_current_candidate_value *matched = item->util;
556 if (!matched) {
557 matched = xmalloc(sizeof(*matched));
558 strbuf_init(&matched->value, 0);
559 item->util = matched;
560 } else {
561 strbuf_reset(&matched->value);
564 if (value) {
565 strbuf_addstr(&matched->value, value);
566 matched->value_is_null = 0;
567 } else {
568 matched->value_is_null = 1;
570 return 0;
573 static int get_urlmatch(const char *var, const char *url)
575 int ret;
576 char *section_tail;
577 struct string_list_item *item;
578 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
579 struct string_list values = STRING_LIST_INIT_DUP;
581 config.collect_fn = urlmatch_collect_fn;
582 config.cascade_fn = NULL;
583 config.cb = &values;
585 if (!url_normalize(url, &config.url))
586 die("%s", config.url.err);
588 config.section = xstrdup_tolower(var);
589 section_tail = strchr(config.section, '.');
590 if (section_tail) {
591 *section_tail = '\0';
592 config.key = section_tail + 1;
593 show_keys = 0;
594 } else {
595 config.key = NULL;
596 show_keys = 1;
599 config_with_options(urlmatch_config_entry, &config,
600 &given_config_source, &config_options);
602 ret = !values.nr;
604 for_each_string_list_item(item, &values) {
605 struct urlmatch_current_candidate_value *matched = item->util;
606 struct strbuf buf = STRBUF_INIT;
608 format_config(&buf, item->string,
609 matched->value_is_null ? NULL : matched->value.buf);
610 fwrite(buf.buf, 1, buf.len, stdout);
611 strbuf_release(&buf);
613 strbuf_release(&matched->value);
615 string_list_clear(&config.vars, 1);
616 string_list_clear(&values, 1);
617 free(config.url.url);
619 free((void *)config.section);
620 return ret;
623 static char *default_user_config(void)
625 struct strbuf buf = STRBUF_INIT;
626 strbuf_addf(&buf,
627 _("# This is Git's per-user configuration file.\n"
628 "[user]\n"
629 "# Please adapt and uncomment the following lines:\n"
630 "# name = %s\n"
631 "# email = %s\n"),
632 ident_default_name(),
633 ident_default_email());
634 return strbuf_detach(&buf, NULL);
637 int cmd_config(int argc, const char **argv, const char *prefix)
639 int nongit = !startup_info->have_repository;
640 char *value;
641 int flags = 0;
643 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
645 argc = parse_options(argc, argv, prefix, builtin_config_options,
646 builtin_config_usage,
647 PARSE_OPT_STOP_AT_NON_OPTION);
649 if (use_global_config + use_system_config + use_local_config +
650 use_worktree_config +
651 !!given_config_source.file + !!given_config_source.blob > 1) {
652 error(_("only one config file at a time"));
653 usage_builtin_config();
656 if (nongit) {
657 if (use_local_config)
658 die(_("--local can only be used inside a git repository"));
659 if (given_config_source.blob)
660 die(_("--blob can only be used inside a git repository"));
661 if (use_worktree_config)
662 die(_("--worktree can only be used inside a git repository"));
666 if (given_config_source.file &&
667 !strcmp(given_config_source.file, "-")) {
668 given_config_source.file = NULL;
669 given_config_source.use_stdin = 1;
670 given_config_source.scope = CONFIG_SCOPE_COMMAND;
673 if (use_global_config) {
674 char *user_config = expand_user_path("~/.gitconfig", 0);
675 char *xdg_config = xdg_config_home("config");
677 if (!user_config)
679 * It is unknown if HOME/.gitconfig exists, so
680 * we do not know if we should write to XDG
681 * location; error out even if XDG_CONFIG_HOME
682 * is set and points at a sane location.
684 die(_("$HOME not set"));
686 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
688 if (access_or_warn(user_config, R_OK, 0) &&
689 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
690 given_config_source.file = xdg_config;
691 free(user_config);
692 } else {
693 given_config_source.file = user_config;
694 free(xdg_config);
697 else if (use_system_config) {
698 given_config_source.file = git_etc_gitconfig();
699 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
700 } else if (use_local_config) {
701 given_config_source.file = git_pathdup("config");
702 given_config_source.scope = CONFIG_SCOPE_LOCAL;
703 } else if (use_worktree_config) {
704 struct worktree **worktrees = get_worktrees();
705 if (repository_format_worktree_config)
706 given_config_source.file = git_pathdup("config.worktree");
707 else if (worktrees[0] && worktrees[1])
708 die(_("--worktree cannot be used with multiple "
709 "working trees unless the config\n"
710 "extension worktreeConfig is enabled. "
711 "Please read \"CONFIGURATION FILE\"\n"
712 "section in \"git help worktree\" for details"));
713 else
714 given_config_source.file = git_pathdup("config");
715 given_config_source.scope = CONFIG_SCOPE_LOCAL;
716 free_worktrees(worktrees);
717 } else if (given_config_source.file) {
718 if (!is_absolute_path(given_config_source.file) && prefix)
719 given_config_source.file =
720 prefix_filename(prefix, given_config_source.file);
721 given_config_source.scope = CONFIG_SCOPE_COMMAND;
722 } else if (given_config_source.blob) {
723 given_config_source.scope = CONFIG_SCOPE_COMMAND;
727 if (respect_includes_opt == -1)
728 config_options.respect_includes = !given_config_source.file;
729 else
730 config_options.respect_includes = respect_includes_opt;
731 if (!nongit) {
732 config_options.commondir = get_git_common_dir();
733 config_options.git_dir = get_git_dir();
736 if (end_nul) {
737 term = '\0';
738 delim = '\n';
739 key_delim = '\n';
742 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
743 error(_("--get-color and variable type are incoherent"));
744 usage_builtin_config();
747 if (HAS_MULTI_BITS(actions)) {
748 error(_("only one action at a time"));
749 usage_builtin_config();
751 if (actions == 0)
752 switch (argc) {
753 case 1: actions = ACTION_GET; break;
754 case 2: actions = ACTION_SET; break;
755 case 3: actions = ACTION_SET_ALL; break;
756 default:
757 usage_builtin_config();
759 if (omit_values &&
760 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
761 error(_("--name-only is only applicable to --list or --get-regexp"));
762 usage_builtin_config();
765 if (show_origin && !(actions &
766 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
767 error(_("--show-origin is only applicable to --get, --get-all, "
768 "--get-regexp, and --list"));
769 usage_builtin_config();
772 if (default_value && !(actions & ACTION_GET)) {
773 error(_("--default is only applicable to --get"));
774 usage_builtin_config();
777 /* check usage of --fixed-value */
778 if (fixed_value) {
779 int allowed_usage = 0;
781 switch (actions) {
782 /* git config --get <name> <value-pattern> */
783 case ACTION_GET:
784 /* git config --get-all <name> <value-pattern> */
785 case ACTION_GET_ALL:
786 /* git config --get-regexp <name-pattern> <value-pattern> */
787 case ACTION_GET_REGEXP:
788 /* git config --unset <name> <value-pattern> */
789 case ACTION_UNSET:
790 /* git config --unset-all <name> <value-pattern> */
791 case ACTION_UNSET_ALL:
792 allowed_usage = argc > 1 && !!argv[1];
793 break;
795 /* git config <name> <value> <value-pattern> */
796 case ACTION_SET_ALL:
797 /* git config --replace-all <name> <value> <value-pattern> */
798 case ACTION_REPLACE_ALL:
799 allowed_usage = argc > 2 && !!argv[2];
800 break;
802 /* other options don't allow --fixed-value */
805 if (!allowed_usage) {
806 error(_("--fixed-value only applies with 'value-pattern'"));
807 usage_builtin_config();
810 flags |= CONFIG_FLAGS_FIXED_VALUE;
813 if (actions & PAGING_ACTIONS)
814 setup_auto_pager("config", 1);
816 if (actions == ACTION_LIST) {
817 check_argc(argc, 0, 0);
818 if (config_with_options(show_all_config, NULL,
819 &given_config_source,
820 &config_options) < 0) {
821 if (given_config_source.file)
822 die_errno(_("unable to read config file '%s'"),
823 given_config_source.file);
824 else
825 die(_("error processing config file(s)"));
828 else if (actions == ACTION_EDIT) {
829 char *config_file;
831 check_argc(argc, 0, 0);
832 if (!given_config_source.file && nongit)
833 die(_("not in a git directory"));
834 if (given_config_source.use_stdin)
835 die(_("editing stdin is not supported"));
836 if (given_config_source.blob)
837 die(_("editing blobs is not supported"));
838 git_config(git_default_config, NULL);
839 config_file = given_config_source.file ?
840 xstrdup(given_config_source.file) :
841 git_pathdup("config");
842 if (use_global_config) {
843 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
844 if (fd >= 0) {
845 char *content = default_user_config();
846 write_str_in_full(fd, content);
847 free(content);
848 close(fd);
850 else if (errno != EEXIST)
851 die_errno(_("cannot create configuration file %s"), config_file);
853 launch_editor(config_file, NULL, NULL);
854 free(config_file);
856 else if (actions == ACTION_SET) {
857 int ret;
858 check_write();
859 check_argc(argc, 2, 2);
860 value = normalize_value(argv[0], argv[1]);
861 UNLEAK(value);
862 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
863 if (ret == CONFIG_NOTHING_SET)
864 error(_("cannot overwrite multiple values with a single value\n"
865 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
866 return ret;
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 UNLEAK(value);
873 return git_config_set_multivar_in_file_gently(given_config_source.file,
874 argv[0], value, argv[2],
875 flags);
877 else if (actions == ACTION_ADD) {
878 check_write();
879 check_argc(argc, 2, 2);
880 value = normalize_value(argv[0], argv[1]);
881 UNLEAK(value);
882 return git_config_set_multivar_in_file_gently(given_config_source.file,
883 argv[0], value,
884 CONFIG_REGEX_NONE,
885 flags);
887 else if (actions == ACTION_REPLACE_ALL) {
888 check_write();
889 check_argc(argc, 2, 3);
890 value = normalize_value(argv[0], argv[1]);
891 UNLEAK(value);
892 return git_config_set_multivar_in_file_gently(given_config_source.file,
893 argv[0], value, argv[2],
894 flags | CONFIG_FLAGS_MULTI_REPLACE);
896 else if (actions == ACTION_GET) {
897 check_argc(argc, 1, 2);
898 return get_value(argv[0], argv[1], flags);
900 else if (actions == ACTION_GET_ALL) {
901 do_all = 1;
902 check_argc(argc, 1, 2);
903 return get_value(argv[0], argv[1], flags);
905 else if (actions == ACTION_GET_REGEXP) {
906 show_keys = 1;
907 use_key_regexp = 1;
908 do_all = 1;
909 check_argc(argc, 1, 2);
910 return get_value(argv[0], argv[1], flags);
912 else if (actions == ACTION_GET_URLMATCH) {
913 check_argc(argc, 2, 2);
914 return get_urlmatch(argv[0], argv[1]);
916 else if (actions == ACTION_UNSET) {
917 check_write();
918 check_argc(argc, 1, 2);
919 if (argc == 2)
920 return git_config_set_multivar_in_file_gently(given_config_source.file,
921 argv[0], NULL, argv[1],
922 flags);
923 else
924 return git_config_set_in_file_gently(given_config_source.file,
925 argv[0], NULL);
927 else if (actions == ACTION_UNSET_ALL) {
928 check_write();
929 check_argc(argc, 1, 2);
930 return git_config_set_multivar_in_file_gently(given_config_source.file,
931 argv[0], NULL, argv[1],
932 flags | CONFIG_FLAGS_MULTI_REPLACE);
934 else if (actions == ACTION_RENAME_SECTION) {
935 int ret;
936 check_write();
937 check_argc(argc, 2, 2);
938 ret = git_config_rename_section_in_file(given_config_source.file,
939 argv[0], argv[1]);
940 if (ret < 0)
941 return ret;
942 if (ret == 0)
943 die(_("no such section: %s"), argv[0]);
945 else if (actions == ACTION_REMOVE_SECTION) {
946 int ret;
947 check_write();
948 check_argc(argc, 1, 1);
949 ret = git_config_rename_section_in_file(given_config_source.file,
950 argv[0], NULL);
951 if (ret < 0)
952 return ret;
953 if (ret == 0)
954 die(_("no such section: %s"), argv[0]);
956 else if (actions == ACTION_GET_COLOR) {
957 check_argc(argc, 1, 2);
958 get_color(argv[0], argv[1]);
960 else if (actions == ACTION_GET_COLORBOOL) {
961 check_argc(argc, 1, 2);
962 if (argc == 2)
963 color_stdout_is_tty = git_config_bool("command line", argv[1]);
964 return get_colorbool(argv[0], argc == 2);
967 return 0;