diff.h: remove unnecessary include of object.h
[git/debian.git] / builtin / config.c
blob49d832d40931c8a895e0381a995581dfd936b038
1 #include "builtin.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "color.h"
5 #include "ident.h"
6 #include "parse-options.h"
7 #include "urlmatch.h"
8 #include "quote.h"
9 #include "worktree.h"
11 static const char *const builtin_config_usage[] = {
12 N_("git config [<options>]"),
13 NULL
16 static char *key;
17 static regex_t *key_regexp;
18 static const char *value_pattern;
19 static regex_t *regexp;
20 static int show_keys;
21 static int omit_values;
22 static int use_key_regexp;
23 static int do_all;
24 static int do_not_match;
25 static char delim = '=';
26 static char key_delim = ' ';
27 static char term = '\n';
29 static int use_global_config, use_system_config, use_local_config;
30 static int use_worktree_config;
31 static struct git_config_source given_config_source;
32 static int actions, type;
33 static char *default_value;
34 static int end_nul;
35 static int respect_includes_opt = -1;
36 static struct config_options config_options;
37 static int show_origin;
38 static int show_scope;
39 static int fixed_value;
41 #define ACTION_GET (1<<0)
42 #define ACTION_GET_ALL (1<<1)
43 #define ACTION_GET_REGEXP (1<<2)
44 #define ACTION_REPLACE_ALL (1<<3)
45 #define ACTION_ADD (1<<4)
46 #define ACTION_UNSET (1<<5)
47 #define ACTION_UNSET_ALL (1<<6)
48 #define ACTION_RENAME_SECTION (1<<7)
49 #define ACTION_REMOVE_SECTION (1<<8)
50 #define ACTION_LIST (1<<9)
51 #define ACTION_EDIT (1<<10)
52 #define ACTION_SET (1<<11)
53 #define ACTION_SET_ALL (1<<12)
54 #define ACTION_GET_COLOR (1<<13)
55 #define ACTION_GET_COLORBOOL (1<<14)
56 #define ACTION_GET_URLMATCH (1<<15)
59 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
60 * one line of output and which should therefore be paged.
62 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
63 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
65 #define TYPE_BOOL 1
66 #define TYPE_INT 2
67 #define TYPE_BOOL_OR_INT 3
68 #define TYPE_PATH 4
69 #define TYPE_EXPIRY_DATE 5
70 #define TYPE_COLOR 6
71 #define TYPE_BOOL_OR_STR 7
73 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
74 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
75 PARSE_OPT_NONEG, option_parse_type, (i) }
77 static NORETURN void usage_builtin_config(void);
79 static int option_parse_type(const struct option *opt, const char *arg,
80 int unset)
82 int new_type, *to_type;
84 if (unset) {
85 *((int *) opt->value) = 0;
86 return 0;
90 * To support '--<type>' style flags, begin with new_type equal to
91 * opt->defval.
93 new_type = opt->defval;
94 if (!new_type) {
95 if (!strcmp(arg, "bool"))
96 new_type = TYPE_BOOL;
97 else if (!strcmp(arg, "int"))
98 new_type = TYPE_INT;
99 else if (!strcmp(arg, "bool-or-int"))
100 new_type = TYPE_BOOL_OR_INT;
101 else if (!strcmp(arg, "bool-or-str"))
102 new_type = TYPE_BOOL_OR_STR;
103 else if (!strcmp(arg, "path"))
104 new_type = TYPE_PATH;
105 else if (!strcmp(arg, "expiry-date"))
106 new_type = TYPE_EXPIRY_DATE;
107 else if (!strcmp(arg, "color"))
108 new_type = TYPE_COLOR;
109 else
110 die(_("unrecognized --type argument, %s"), arg);
113 to_type = opt->value;
114 if (*to_type && *to_type != new_type) {
116 * Complain when there is a new type not equal to the old type.
117 * This allows for combinations like '--int --type=int' and
118 * '--type=int --type=int', but disallows ones like '--type=bool
119 * --int' and '--type=bool
120 * --type=int'.
122 error(_("only one type at a time"));
123 usage_builtin_config();
125 *to_type = new_type;
127 return 0;
130 static struct option builtin_config_options[] = {
131 OPT_GROUP(N_("Config file location")),
132 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
133 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
134 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
135 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
136 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
137 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
138 OPT_GROUP(N_("Action")),
139 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
140 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
141 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
142 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
143 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
144 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
145 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
146 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
147 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
148 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
149 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
150 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
151 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
152 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
153 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
154 OPT_GROUP(N_("Type")),
155 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
156 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
157 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
158 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
159 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
160 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
161 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
162 OPT_GROUP(N_("Other")),
163 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
164 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
165 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
166 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
167 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
168 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
169 OPT_END(),
172 static NORETURN void usage_builtin_config(void)
174 usage_with_options(builtin_config_usage, builtin_config_options);
177 static void check_argc(int argc, int min, int max)
179 if (argc >= min && argc <= max)
180 return;
181 if (min == max)
182 error(_("wrong number of arguments, should be %d"), min);
183 else
184 error(_("wrong number of arguments, should be from %d to %d"),
185 min, max);
186 usage_builtin_config();
189 static void show_config_origin(struct strbuf *buf)
191 const char term = end_nul ? '\0' : '\t';
193 strbuf_addstr(buf, current_config_origin_type());
194 strbuf_addch(buf, ':');
195 if (end_nul)
196 strbuf_addstr(buf, current_config_name());
197 else
198 quote_c_style(current_config_name(), buf, NULL, 0);
199 strbuf_addch(buf, term);
202 static void show_config_scope(struct strbuf *buf)
204 const char term = end_nul ? '\0' : '\t';
205 const char *scope = config_scope_name(current_config_scope());
207 strbuf_addstr(buf, N_(scope));
208 strbuf_addch(buf, term);
211 static int show_all_config(const char *key_, const char *value_,
212 void *cb UNUSED)
214 if (show_origin || show_scope) {
215 struct strbuf buf = STRBUF_INIT;
216 if (show_scope)
217 show_config_scope(&buf);
218 if (show_origin)
219 show_config_origin(&buf);
220 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
221 fwrite(buf.buf, 1, buf.len, stdout);
222 strbuf_release(&buf);
224 if (!omit_values && value_)
225 printf("%s%c%s%c", key_, delim, value_, term);
226 else
227 printf("%s%c", key_, term);
228 return 0;
231 struct strbuf_list {
232 struct strbuf *items;
233 int nr;
234 int alloc;
237 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
239 if (show_scope)
240 show_config_scope(buf);
241 if (show_origin)
242 show_config_origin(buf);
243 if (show_keys)
244 strbuf_addstr(buf, key_);
245 if (!omit_values) {
246 if (show_keys)
247 strbuf_addch(buf, key_delim);
249 if (type == TYPE_INT)
250 strbuf_addf(buf, "%"PRId64,
251 git_config_int64(key_, value_ ? value_ : ""));
252 else if (type == TYPE_BOOL)
253 strbuf_addstr(buf, git_config_bool(key_, value_) ?
254 "true" : "false");
255 else if (type == TYPE_BOOL_OR_INT) {
256 int is_bool, v;
257 v = git_config_bool_or_int(key_, value_, &is_bool);
258 if (is_bool)
259 strbuf_addstr(buf, v ? "true" : "false");
260 else
261 strbuf_addf(buf, "%d", v);
262 } else if (type == TYPE_BOOL_OR_STR) {
263 int v = git_parse_maybe_bool(value_);
264 if (v < 0)
265 strbuf_addstr(buf, value_);
266 else
267 strbuf_addstr(buf, v ? "true" : "false");
268 } else if (type == TYPE_PATH) {
269 const char *v;
270 if (git_config_pathname(&v, key_, value_) < 0)
271 return -1;
272 strbuf_addstr(buf, v);
273 free((char *)v);
274 } else if (type == TYPE_EXPIRY_DATE) {
275 timestamp_t t;
276 if (git_config_expiry_date(&t, key_, value_) < 0)
277 return -1;
278 strbuf_addf(buf, "%"PRItime, t);
279 } else if (type == TYPE_COLOR) {
280 char v[COLOR_MAXLEN];
281 if (git_config_color(v, key_, value_) < 0)
282 return -1;
283 strbuf_addstr(buf, v);
284 } else if (value_) {
285 strbuf_addstr(buf, value_);
286 } else {
287 /* Just show the key name; back out delimiter */
288 if (show_keys)
289 strbuf_setlen(buf, buf->len - 1);
292 strbuf_addch(buf, term);
293 return 0;
296 static int collect_config(const char *key_, const char *value_, void *cb)
298 struct strbuf_list *values = cb;
300 if (!use_key_regexp && strcmp(key_, key))
301 return 0;
302 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
303 return 0;
304 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
305 return 0;
306 if (regexp != NULL &&
307 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
308 return 0;
310 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
311 strbuf_init(&values->items[values->nr], 0);
313 return format_config(&values->items[values->nr++], key_, value_);
316 static int get_value(const char *key_, const char *regex_, unsigned flags)
318 int ret = CONFIG_GENERIC_ERROR;
319 struct strbuf_list values = {NULL};
320 int i;
322 if (use_key_regexp) {
323 char *tl;
326 * NEEDSWORK: this naive pattern lowercasing obviously does not
327 * work for more complex patterns like "^[^.]*Foo.*bar".
328 * Perhaps we should deprecate this altogether someday.
331 key = xstrdup(key_);
332 for (tl = key + strlen(key) - 1;
333 tl >= key && *tl != '.';
334 tl--)
335 *tl = tolower(*tl);
336 for (tl = key; *tl && *tl != '.'; tl++)
337 *tl = tolower(*tl);
339 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
340 if (regcomp(key_regexp, key, REG_EXTENDED)) {
341 error(_("invalid key pattern: %s"), key_);
342 FREE_AND_NULL(key_regexp);
343 ret = CONFIG_INVALID_PATTERN;
344 goto free_strings;
346 } else {
347 if (git_config_parse_key(key_, &key, NULL)) {
348 ret = CONFIG_INVALID_KEY;
349 goto free_strings;
353 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
354 value_pattern = regex_;
355 else if (regex_) {
356 if (regex_[0] == '!') {
357 do_not_match = 1;
358 regex_++;
361 regexp = (regex_t*)xmalloc(sizeof(regex_t));
362 if (regcomp(regexp, regex_, REG_EXTENDED)) {
363 error(_("invalid pattern: %s"), regex_);
364 FREE_AND_NULL(regexp);
365 ret = CONFIG_INVALID_PATTERN;
366 goto free_strings;
370 config_with_options(collect_config, &values,
371 &given_config_source, &config_options);
373 if (!values.nr && default_value) {
374 struct strbuf *item;
375 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
376 item = &values.items[values.nr++];
377 strbuf_init(item, 0);
378 if (format_config(item, key_, default_value) < 0)
379 die(_("failed to format default config value: %s"),
380 default_value);
383 ret = !values.nr;
385 for (i = 0; i < values.nr; i++) {
386 struct strbuf *buf = values.items + i;
387 if (do_all || i == values.nr - 1)
388 fwrite(buf->buf, 1, buf->len, stdout);
389 strbuf_release(buf);
391 free(values.items);
393 free_strings:
394 free(key);
395 if (key_regexp) {
396 regfree(key_regexp);
397 free(key_regexp);
399 if (regexp) {
400 regfree(regexp);
401 free(regexp);
404 return ret;
407 static char *normalize_value(const char *key, const char *value)
409 if (!value)
410 return NULL;
412 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
414 * We don't do normalization for TYPE_PATH here: If
415 * the path is like ~/foobar/, we prefer to store
416 * "~/foobar/" in the config file, and to expand the ~
417 * when retrieving the value.
418 * Also don't do normalization for expiry dates.
420 return xstrdup(value);
421 if (type == TYPE_INT)
422 return xstrfmt("%"PRId64, git_config_int64(key, value));
423 if (type == TYPE_BOOL)
424 return xstrdup(git_config_bool(key, value) ? "true" : "false");
425 if (type == TYPE_BOOL_OR_INT) {
426 int is_bool, v;
427 v = git_config_bool_or_int(key, value, &is_bool);
428 if (!is_bool)
429 return xstrfmt("%d", v);
430 else
431 return xstrdup(v ? "true" : "false");
433 if (type == TYPE_BOOL_OR_STR) {
434 int v = git_parse_maybe_bool(value);
435 if (v < 0)
436 return xstrdup(value);
437 else
438 return xstrdup(v ? "true" : "false");
440 if (type == TYPE_COLOR) {
441 char v[COLOR_MAXLEN];
442 if (git_config_color(v, key, value))
443 die(_("cannot parse color '%s'"), value);
446 * The contents of `v` now contain an ANSI escape
447 * sequence, not suitable for including within a
448 * configuration file. Treat the above as a
449 * "sanity-check", and return the given value, which we
450 * know is representable as valid color code.
452 return xstrdup(value);
455 BUG("cannot normalize type %d", type);
458 static int get_color_found;
459 static const char *get_color_slot;
460 static const char *get_colorbool_slot;
461 static char parsed_color[COLOR_MAXLEN];
463 static int git_get_color_config(const char *var, const char *value,
464 void *cb UNUSED)
466 if (!strcmp(var, get_color_slot)) {
467 if (!value)
468 config_error_nonbool(var);
469 if (color_parse(value, parsed_color) < 0)
470 return -1;
471 get_color_found = 1;
473 return 0;
476 static void get_color(const char *var, const char *def_color)
478 get_color_slot = var;
479 get_color_found = 0;
480 parsed_color[0] = '\0';
481 config_with_options(git_get_color_config, NULL,
482 &given_config_source, &config_options);
484 if (!get_color_found && def_color) {
485 if (color_parse(def_color, parsed_color) < 0)
486 die(_("unable to parse default color value"));
489 fputs(parsed_color, stdout);
492 static int get_colorbool_found;
493 static int get_diff_color_found;
494 static int get_color_ui_found;
495 static int git_get_colorbool_config(const char *var, const char *value,
496 void *data UNUSED)
498 if (!strcmp(var, get_colorbool_slot))
499 get_colorbool_found = git_config_colorbool(var, value);
500 else if (!strcmp(var, "diff.color"))
501 get_diff_color_found = git_config_colorbool(var, value);
502 else if (!strcmp(var, "color.ui"))
503 get_color_ui_found = git_config_colorbool(var, value);
504 return 0;
507 static int get_colorbool(const char *var, int print)
509 get_colorbool_slot = var;
510 get_colorbool_found = -1;
511 get_diff_color_found = -1;
512 get_color_ui_found = -1;
513 config_with_options(git_get_colorbool_config, NULL,
514 &given_config_source, &config_options);
516 if (get_colorbool_found < 0) {
517 if (!strcmp(get_colorbool_slot, "color.diff"))
518 get_colorbool_found = get_diff_color_found;
519 if (get_colorbool_found < 0)
520 get_colorbool_found = get_color_ui_found;
523 if (get_colorbool_found < 0)
524 /* default value if none found in config */
525 get_colorbool_found = GIT_COLOR_AUTO;
527 get_colorbool_found = want_color(get_colorbool_found);
529 if (print) {
530 printf("%s\n", get_colorbool_found ? "true" : "false");
531 return 0;
532 } else
533 return get_colorbool_found ? 0 : 1;
536 static void check_write(void)
538 if (!given_config_source.file && !startup_info->have_repository)
539 die(_("not in a git directory"));
541 if (given_config_source.use_stdin)
542 die(_("writing to stdin is not supported"));
544 if (given_config_source.blob)
545 die(_("writing config blobs is not supported"));
548 struct urlmatch_current_candidate_value {
549 char value_is_null;
550 struct strbuf value;
553 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
555 struct string_list *values = cb;
556 struct string_list_item *item = string_list_insert(values, var);
557 struct urlmatch_current_candidate_value *matched = item->util;
559 if (!matched) {
560 matched = xmalloc(sizeof(*matched));
561 strbuf_init(&matched->value, 0);
562 item->util = matched;
563 } else {
564 strbuf_reset(&matched->value);
567 if (value) {
568 strbuf_addstr(&matched->value, value);
569 matched->value_is_null = 0;
570 } else {
571 matched->value_is_null = 1;
573 return 0;
576 static int get_urlmatch(const char *var, const char *url)
578 int ret;
579 char *section_tail;
580 struct string_list_item *item;
581 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
582 struct string_list values = STRING_LIST_INIT_DUP;
584 config.collect_fn = urlmatch_collect_fn;
585 config.cascade_fn = NULL;
586 config.cb = &values;
588 if (!url_normalize(url, &config.url))
589 die("%s", config.url.err);
591 config.section = xstrdup_tolower(var);
592 section_tail = strchr(config.section, '.');
593 if (section_tail) {
594 *section_tail = '\0';
595 config.key = section_tail + 1;
596 show_keys = 0;
597 } else {
598 config.key = NULL;
599 show_keys = 1;
602 config_with_options(urlmatch_config_entry, &config,
603 &given_config_source, &config_options);
605 ret = !values.nr;
607 for_each_string_list_item(item, &values) {
608 struct urlmatch_current_candidate_value *matched = item->util;
609 struct strbuf buf = STRBUF_INIT;
611 format_config(&buf, item->string,
612 matched->value_is_null ? NULL : matched->value.buf);
613 fwrite(buf.buf, 1, buf.len, stdout);
614 strbuf_release(&buf);
616 strbuf_release(&matched->value);
618 urlmatch_config_release(&config);
619 string_list_clear(&values, 1);
620 free(config.url.url);
622 free((void *)config.section);
623 return ret;
626 static char *default_user_config(void)
628 struct strbuf buf = STRBUF_INIT;
629 strbuf_addf(&buf,
630 _("# This is Git's per-user configuration file.\n"
631 "[user]\n"
632 "# Please adapt and uncomment the following lines:\n"
633 "# name = %s\n"
634 "# email = %s\n"),
635 ident_default_name(),
636 ident_default_email());
637 return strbuf_detach(&buf, NULL);
640 int cmd_config(int argc, const char **argv, const char *prefix)
642 int nongit = !startup_info->have_repository;
643 char *value = NULL;
644 int flags = 0;
645 int ret = 0;
647 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
649 argc = parse_options(argc, argv, prefix, builtin_config_options,
650 builtin_config_usage,
651 PARSE_OPT_STOP_AT_NON_OPTION);
653 if (use_global_config + use_system_config + use_local_config +
654 use_worktree_config +
655 !!given_config_source.file + !!given_config_source.blob > 1) {
656 error(_("only one config file at a time"));
657 usage_builtin_config();
660 if (nongit) {
661 if (use_local_config)
662 die(_("--local can only be used inside a git repository"));
663 if (given_config_source.blob)
664 die(_("--blob can only be used inside a git repository"));
665 if (use_worktree_config)
666 die(_("--worktree can only be used inside a git repository"));
670 if (given_config_source.file &&
671 !strcmp(given_config_source.file, "-")) {
672 given_config_source.file = NULL;
673 given_config_source.use_stdin = 1;
674 given_config_source.scope = CONFIG_SCOPE_COMMAND;
677 if (use_global_config) {
678 char *user_config, *xdg_config;
680 git_global_config(&user_config, &xdg_config);
681 if (!user_config)
683 * It is unknown if HOME/.gitconfig exists, so
684 * we do not know if we should write to XDG
685 * location; error out even if XDG_CONFIG_HOME
686 * is set and points at a sane location.
688 die(_("$HOME not set"));
690 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
692 if (access_or_warn(user_config, R_OK, 0) &&
693 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
694 given_config_source.file = xdg_config;
695 free(user_config);
696 } else {
697 given_config_source.file = user_config;
698 free(xdg_config);
701 else if (use_system_config) {
702 given_config_source.file = git_system_config();
703 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
704 } else if (use_local_config) {
705 given_config_source.file = git_pathdup("config");
706 given_config_source.scope = CONFIG_SCOPE_LOCAL;
707 } else if (use_worktree_config) {
708 struct worktree **worktrees = get_worktrees();
709 if (repository_format_worktree_config)
710 given_config_source.file = git_pathdup("config.worktree");
711 else if (worktrees[0] && worktrees[1])
712 die(_("--worktree cannot be used with multiple "
713 "working trees unless the config\n"
714 "extension worktreeConfig is enabled. "
715 "Please read \"CONFIGURATION FILE\"\n"
716 "section in \"git help worktree\" for details"));
717 else
718 given_config_source.file = git_pathdup("config");
719 given_config_source.scope = CONFIG_SCOPE_LOCAL;
720 free_worktrees(worktrees);
721 } else if (given_config_source.file) {
722 if (!is_absolute_path(given_config_source.file) && prefix)
723 given_config_source.file =
724 prefix_filename(prefix, given_config_source.file);
725 given_config_source.scope = CONFIG_SCOPE_COMMAND;
726 } else if (given_config_source.blob) {
727 given_config_source.scope = CONFIG_SCOPE_COMMAND;
731 if (respect_includes_opt == -1)
732 config_options.respect_includes = !given_config_source.file;
733 else
734 config_options.respect_includes = respect_includes_opt;
735 if (!nongit) {
736 config_options.commondir = get_git_common_dir();
737 config_options.git_dir = get_git_dir();
740 if (end_nul) {
741 term = '\0';
742 delim = '\n';
743 key_delim = '\n';
746 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
747 error(_("--get-color and variable type are incoherent"));
748 usage_builtin_config();
751 if (HAS_MULTI_BITS(actions)) {
752 error(_("only one action at a time"));
753 usage_builtin_config();
755 if (actions == 0)
756 switch (argc) {
757 case 1: actions = ACTION_GET; break;
758 case 2: actions = ACTION_SET; break;
759 case 3: actions = ACTION_SET_ALL; break;
760 default:
761 usage_builtin_config();
763 if (omit_values &&
764 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
765 error(_("--name-only is only applicable to --list or --get-regexp"));
766 usage_builtin_config();
769 if (show_origin && !(actions &
770 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
771 error(_("--show-origin is only applicable to --get, --get-all, "
772 "--get-regexp, and --list"));
773 usage_builtin_config();
776 if (default_value && !(actions & ACTION_GET)) {
777 error(_("--default is only applicable to --get"));
778 usage_builtin_config();
781 /* check usage of --fixed-value */
782 if (fixed_value) {
783 int allowed_usage = 0;
785 switch (actions) {
786 /* git config --get <name> <value-pattern> */
787 case ACTION_GET:
788 /* git config --get-all <name> <value-pattern> */
789 case ACTION_GET_ALL:
790 /* git config --get-regexp <name-pattern> <value-pattern> */
791 case ACTION_GET_REGEXP:
792 /* git config --unset <name> <value-pattern> */
793 case ACTION_UNSET:
794 /* git config --unset-all <name> <value-pattern> */
795 case ACTION_UNSET_ALL:
796 allowed_usage = argc > 1 && !!argv[1];
797 break;
799 /* git config <name> <value> <value-pattern> */
800 case ACTION_SET_ALL:
801 /* git config --replace-all <name> <value> <value-pattern> */
802 case ACTION_REPLACE_ALL:
803 allowed_usage = argc > 2 && !!argv[2];
804 break;
806 /* other options don't allow --fixed-value */
809 if (!allowed_usage) {
810 error(_("--fixed-value only applies with 'value-pattern'"));
811 usage_builtin_config();
814 flags |= CONFIG_FLAGS_FIXED_VALUE;
817 if (actions & PAGING_ACTIONS)
818 setup_auto_pager("config", 1);
820 if (actions == ACTION_LIST) {
821 check_argc(argc, 0, 0);
822 if (config_with_options(show_all_config, NULL,
823 &given_config_source,
824 &config_options) < 0) {
825 if (given_config_source.file)
826 die_errno(_("unable to read config file '%s'"),
827 given_config_source.file);
828 else
829 die(_("error processing config file(s)"));
832 else if (actions == ACTION_EDIT) {
833 char *config_file;
835 check_argc(argc, 0, 0);
836 if (!given_config_source.file && nongit)
837 die(_("not in a git directory"));
838 if (given_config_source.use_stdin)
839 die(_("editing stdin is not supported"));
840 if (given_config_source.blob)
841 die(_("editing blobs is not supported"));
842 git_config(git_default_config, NULL);
843 config_file = given_config_source.file ?
844 xstrdup(given_config_source.file) :
845 git_pathdup("config");
846 if (use_global_config) {
847 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
848 if (fd >= 0) {
849 char *content = default_user_config();
850 write_str_in_full(fd, content);
851 free(content);
852 close(fd);
854 else if (errno != EEXIST)
855 die_errno(_("cannot create configuration file %s"), config_file);
857 launch_editor(config_file, NULL, NULL);
858 free(config_file);
860 else if (actions == ACTION_SET) {
861 check_write();
862 check_argc(argc, 2, 2);
863 value = normalize_value(argv[0], argv[1]);
864 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
865 if (ret == CONFIG_NOTHING_SET)
866 error(_("cannot overwrite multiple values with a single value\n"
867 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
869 else if (actions == ACTION_SET_ALL) {
870 check_write();
871 check_argc(argc, 2, 3);
872 value = normalize_value(argv[0], argv[1]);
873 ret = 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 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
882 argv[0], value,
883 CONFIG_REGEX_NONE,
884 flags);
886 else if (actions == ACTION_REPLACE_ALL) {
887 check_write();
888 check_argc(argc, 2, 3);
889 value = normalize_value(argv[0], argv[1]);
890 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
891 argv[0], value, argv[2],
892 flags | CONFIG_FLAGS_MULTI_REPLACE);
894 else if (actions == ACTION_GET) {
895 check_argc(argc, 1, 2);
896 return get_value(argv[0], argv[1], flags);
898 else if (actions == ACTION_GET_ALL) {
899 do_all = 1;
900 check_argc(argc, 1, 2);
901 return get_value(argv[0], argv[1], flags);
903 else if (actions == ACTION_GET_REGEXP) {
904 show_keys = 1;
905 use_key_regexp = 1;
906 do_all = 1;
907 check_argc(argc, 1, 2);
908 return get_value(argv[0], argv[1], flags);
910 else if (actions == ACTION_GET_URLMATCH) {
911 check_argc(argc, 2, 2);
912 return get_urlmatch(argv[0], argv[1]);
914 else if (actions == ACTION_UNSET) {
915 check_write();
916 check_argc(argc, 1, 2);
917 if (argc == 2)
918 return git_config_set_multivar_in_file_gently(given_config_source.file,
919 argv[0], NULL, argv[1],
920 flags);
921 else
922 return git_config_set_in_file_gently(given_config_source.file,
923 argv[0], NULL);
925 else if (actions == ACTION_UNSET_ALL) {
926 check_write();
927 check_argc(argc, 1, 2);
928 return git_config_set_multivar_in_file_gently(given_config_source.file,
929 argv[0], NULL, argv[1],
930 flags | CONFIG_FLAGS_MULTI_REPLACE);
932 else if (actions == ACTION_RENAME_SECTION) {
933 check_write();
934 check_argc(argc, 2, 2);
935 ret = git_config_rename_section_in_file(given_config_source.file,
936 argv[0], argv[1]);
937 if (ret < 0)
938 return ret;
939 else if (!ret)
940 die(_("no such section: %s"), argv[0]);
941 else
942 ret = 0;
944 else if (actions == ACTION_REMOVE_SECTION) {
945 check_write();
946 check_argc(argc, 1, 1);
947 ret = git_config_rename_section_in_file(given_config_source.file,
948 argv[0], NULL);
949 if (ret < 0)
950 return ret;
951 else if (!ret)
952 die(_("no such section: %s"), argv[0]);
953 else
954 ret = 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 free(value);
968 return ret;