]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/config.c
git-compat-util: move alloc macros to git-compat-util.h
[thirdparty/git.git] / builtin / config.c
CommitLineData
e12c095a 1#include "builtin.h"
0b027f6c 2#include "abspath.h"
b2141fc1 3#include "config.h"
9ce03522 4#include "color.h"
4e120823 5#include "editor.h"
32a8f510 6#include "environment.h"
3867f6d6 7#include "repository.h"
f394e093 8#include "gettext.h"
b5fa6081 9#include "ident.h"
d64ec16c 10#include "parse-options.h"
d4770964 11#include "urlmatch.h"
d1cbe1e6 12#include "path.h"
70bd879a 13#include "quote.h"
e38da487 14#include "setup.h"
88e4e183 15#include "strbuf.h"
58b284a2 16#include "worktree.h"
1b1e59c5 17
d64ec16c 18static const char *const builtin_config_usage[] = {
9c9b4f2f 19 N_("git config [<options>]"),
d64ec16c
FC
20 NULL
21};
4ddba79d 22
96f1e58f
DR
23static char *key;
24static regex_t *key_regexp;
3f1bae1d 25static const char *value_pattern;
96f1e58f
DR
26static regex_t *regexp;
27static int show_keys;
578625fa 28static int omit_values;
96f1e58f
DR
29static int use_key_regexp;
30static int do_all;
31static int do_not_match;
2275d502
FL
32static char delim = '=';
33static char key_delim = ' ';
34static char term = '\n';
4ddba79d 35
57210a67 36static int use_global_config, use_system_config, use_local_config;
58b284a2 37static int use_worktree_config;
c8985ce0 38static struct git_config_source given_config_source;
0a8950be 39static int actions, type;
eeaa24b9 40static char *default_value;
329e6ec3 41static int end_nul;
c48f4b37
NTND
42static int respect_includes_opt = -1;
43static struct config_options config_options;
70bd879a 44static int show_origin;
145d59f4 45static int show_scope;
fda43942 46static int fixed_value;
d64ec16c
FC
47
48#define ACTION_GET (1<<0)
49#define ACTION_GET_ALL (1<<1)
50#define ACTION_GET_REGEXP (1<<2)
51#define ACTION_REPLACE_ALL (1<<3)
52#define ACTION_ADD (1<<4)
53#define ACTION_UNSET (1<<5)
54#define ACTION_UNSET_ALL (1<<6)
55#define ACTION_RENAME_SECTION (1<<7)
56#define ACTION_REMOVE_SECTION (1<<8)
57#define ACTION_LIST (1<<9)
58#define ACTION_EDIT (1<<10)
59#define ACTION_SET (1<<11)
60#define ACTION_SET_ALL (1<<12)
61#define ACTION_GET_COLOR (1<<13)
62#define ACTION_GET_COLORBOOL (1<<14)
d4770964 63#define ACTION_GET_URLMATCH (1<<15)
d64ec16c 64
32888b8f
65/*
66 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
67 * one line of output and which should therefore be paged.
68 */
69#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
70 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
71
0a8950be
TB
72#define TYPE_BOOL 1
73#define TYPE_INT 2
74#define TYPE_BOOL_OR_INT 3
75#define TYPE_PATH 4
76#define TYPE_EXPIRY_DATE 5
63e2a0f8 77#define TYPE_COLOR 6
dbd8c09b 78#define TYPE_BOOL_OR_STR 7
16c1e939 79
fb0dc3ba
TB
80#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
81 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
82 PARSE_OPT_NONEG, option_parse_type, (i) }
83
6aaded55 84static NORETURN void usage_builtin_config(void);
fb0dc3ba
TB
85
86static int option_parse_type(const struct option *opt, const char *arg,
87 int unset)
88{
89 int new_type, *to_type;
90
91 if (unset) {
92 *((int *) opt->value) = 0;
93 return 0;
94 }
95
96 /*
97 * To support '--<type>' style flags, begin with new_type equal to
98 * opt->defval.
99 */
100 new_type = opt->defval;
101 if (!new_type) {
102 if (!strcmp(arg, "bool"))
103 new_type = TYPE_BOOL;
104 else if (!strcmp(arg, "int"))
105 new_type = TYPE_INT;
106 else if (!strcmp(arg, "bool-or-int"))
107 new_type = TYPE_BOOL_OR_INT;
dbd8c09b
LS
108 else if (!strcmp(arg, "bool-or-str"))
109 new_type = TYPE_BOOL_OR_STR;
fb0dc3ba
TB
110 else if (!strcmp(arg, "path"))
111 new_type = TYPE_PATH;
112 else if (!strcmp(arg, "expiry-date"))
113 new_type = TYPE_EXPIRY_DATE;
63e2a0f8
TB
114 else if (!strcmp(arg, "color"))
115 new_type = TYPE_COLOR;
fb0dc3ba
TB
116 else
117 die(_("unrecognized --type argument, %s"), arg);
118 }
119
120 to_type = opt->value;
121 if (*to_type && *to_type != new_type) {
122 /*
123 * Complain when there is a new type not equal to the old type.
124 * This allows for combinations like '--int --type=int' and
125 * '--type=int --type=int', but disallows ones like '--type=bool
126 * --int' and '--type=bool
127 * --type=int'.
128 */
1d28ff4c 129 error(_("only one type at a time"));
6aaded55 130 usage_builtin_config();
fb0dc3ba
TB
131 }
132 *to_type = new_type;
133
134 return 0;
135}
136
d64ec16c 137static struct option builtin_config_options[] = {
1bd31ce6 138 OPT_GROUP(N_("Config file location")),
21e047dc
SB
139 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
140 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
141 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
58b284a2 142 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
c8985ce0
KS
143 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
144 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
1bd31ce6 145 OPT_GROUP(N_("Action")),
247e2f82
DS
146 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
147 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
148 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
d4770964 149 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
247e2f82 150 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
f63cf8c9 151 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
247e2f82
DS
152 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
153 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
1bd31ce6
NTND
154 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
155 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
156 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
fda43942 157 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
f63cf8c9 158 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
d0e08d62
JK
159 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
160 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
1bd31ce6 161 OPT_GROUP(N_("Type")),
5445124f 162 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
fb0dc3ba
TB
163 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
164 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
165 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
dbd8c09b 166 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
fb0dc3ba
TB
167 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
168 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
1bd31ce6 169 OPT_GROUP(N_("Other")),
329e6ec3 170 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
578625fa 171 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
c48f4b37 172 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
70bd879a 173 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
145d59f4 174 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
eeaa24b9 175 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
d64ec16c
FC
176 OPT_END(),
177};
178
6aaded55
BB
179static NORETURN void usage_builtin_config(void)
180{
181 usage_with_options(builtin_config_usage, builtin_config_options);
182}
183
3b335762
NTND
184static void check_argc(int argc, int min, int max)
185{
d64ec16c
FC
186 if (argc >= min && argc <= max)
187 return;
1a07e59c 188 if (min == max)
1d28ff4c 189 error(_("wrong number of arguments, should be %d"), min);
1a07e59c 190 else
1d28ff4c 191 error(_("wrong number of arguments, should be from %d to %d"),
1a07e59c 192 min, max);
6aaded55 193 usage_builtin_config();
d64ec16c
FC
194}
195
70bd879a
LS
196static void show_config_origin(struct strbuf *buf)
197{
329e6ec3 198 const char term = end_nul ? '\0' : '\t';
70bd879a
LS
199
200 strbuf_addstr(buf, current_config_origin_type());
201 strbuf_addch(buf, ':');
329e6ec3 202 if (end_nul)
70bd879a
LS
203 strbuf_addstr(buf, current_config_name());
204 else
205 quote_c_style(current_config_name(), buf, NULL, 0);
206 strbuf_addch(buf, term);
207}
208
145d59f4
MR
209static void show_config_scope(struct strbuf *buf)
210{
211 const char term = end_nul ? '\0' : '\t';
212 const char *scope = config_scope_name(current_config_scope());
213
214 strbuf_addstr(buf, N_(scope));
215 strbuf_addch(buf, term);
216}
217
783a86c1 218static int show_all_config(const char *key_, const char *value_,
5cf88fd8 219 void *cb UNUSED)
de791f15 220{
145d59f4 221 if (show_origin || show_scope) {
70bd879a 222 struct strbuf buf = STRBUF_INIT;
145d59f4
MR
223 if (show_scope)
224 show_config_scope(&buf);
225 if (show_origin)
226 show_config_origin(&buf);
70bd879a
LS
227 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
228 fwrite(buf.buf, 1, buf.len, stdout);
229 strbuf_release(&buf);
230 }
578625fa 231 if (!omit_values && value_)
2275d502 232 printf("%s%c%s%c", key_, delim, value_, term);
de791f15 233 else
2275d502 234 printf("%s%c", key_, term);
de791f15
PB
235 return 0;
236}
237
7acdd6f0
JK
238struct strbuf_list {
239 struct strbuf *items;
240 int nr;
241 int alloc;
242};
243
d9b9169b 244static int format_config(struct strbuf *buf, const char *key_, const char *value_)
4ddba79d 245{
145d59f4
MR
246 if (show_scope)
247 show_config_scope(buf);
70bd879a
LS
248 if (show_origin)
249 show_config_origin(buf);
ebca2d49 250 if (show_keys)
7acdd6f0 251 strbuf_addstr(buf, key_);
ebca2d49 252 if (!omit_values) {
f2259877
JK
253 if (show_keys)
254 strbuf_addch(buf, key_delim);
ebca2d49 255
0a8950be 256 if (type == TYPE_INT)
f2259877
JK
257 strbuf_addf(buf, "%"PRId64,
258 git_config_int64(key_, value_ ? value_ : ""));
0a8950be 259 else if (type == TYPE_BOOL)
f2259877
JK
260 strbuf_addstr(buf, git_config_bool(key_, value_) ?
261 "true" : "false");
0a8950be 262 else if (type == TYPE_BOOL_OR_INT) {
ebca2d49
SG
263 int is_bool, v;
264 v = git_config_bool_or_int(key_, value_, &is_bool);
265 if (is_bool)
f2259877 266 strbuf_addstr(buf, v ? "true" : "false");
ebca2d49 267 else
f2259877 268 strbuf_addf(buf, "%d", v);
dbd8c09b
LS
269 } else if (type == TYPE_BOOL_OR_STR) {
270 int v = git_parse_maybe_bool(value_);
271 if (v < 0)
272 strbuf_addstr(buf, value_);
273 else
274 strbuf_addstr(buf, v ? "true" : "false");
0a8950be 275 } else if (type == TYPE_PATH) {
f2259877
JK
276 const char *v;
277 if (git_config_pathname(&v, key_, value_) < 0)
ebca2d49 278 return -1;
f2259877
JK
279 strbuf_addstr(buf, v);
280 free((char *)v);
0a8950be 281 } else if (type == TYPE_EXPIRY_DATE) {
5f967424
HM
282 timestamp_t t;
283 if (git_config_expiry_date(&t, key_, value_) < 0)
284 return -1;
285 strbuf_addf(buf, "%"PRItime, t);
63e2a0f8
TB
286 } else if (type == TYPE_COLOR) {
287 char v[COLOR_MAXLEN];
288 if (git_config_color(v, key_, value_) < 0)
289 return -1;
290 strbuf_addstr(buf, v);
ebca2d49 291 } else if (value_) {
f2259877 292 strbuf_addstr(buf, value_);
ebca2d49 293 } else {
f2259877
JK
294 /* Just show the key name; back out delimiter */
295 if (show_keys)
296 strbuf_setlen(buf, buf->len - 1);
ebca2d49 297 }
578625fa 298 }
00b347d3 299 strbuf_addch(buf, term);
4ddba79d
JS
300 return 0;
301}
302
d9b9169b
JH
303static int collect_config(const char *key_, const char *value_, void *cb)
304{
305 struct strbuf_list *values = cb;
306
307 if (!use_key_regexp && strcmp(key_, key))
308 return 0;
309 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
310 return 0;
3f1bae1d
DS
311 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
312 return 0;
d9b9169b
JH
313 if (regexp != NULL &&
314 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
315 return 0;
316
317 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
9f1429df 318 strbuf_init(&values->items[values->nr], 0);
d9b9169b
JH
319
320 return format_config(&values->items[values->nr++], key_, value_);
321}
322
3f1bae1d 323static int get_value(const char *key_, const char *regex_, unsigned flags)
4ddba79d 324{
9409c7a5 325 int ret = CONFIG_GENERIC_ERROR;
5ba1a8a7 326 struct strbuf_list values = {NULL};
7acdd6f0 327 int i;
4ddba79d 328
2fa9a0fb 329 if (use_key_regexp) {
b09c53a3
LP
330 char *tl;
331
332 /*
333 * NEEDSWORK: this naive pattern lowercasing obviously does not
334 * work for more complex patterns like "^[^.]*Foo.*bar".
335 * Perhaps we should deprecate this altogether someday.
336 */
337
338 key = xstrdup(key_);
339 for (tl = key + strlen(key) - 1;
340 tl >= key && *tl != '.';
341 tl--)
342 *tl = tolower(*tl);
343 for (tl = key; *tl && *tl != '.'; tl++)
344 *tl = tolower(*tl);
345
2d7320d0 346 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 347 if (regcomp(key_regexp, key, REG_EXTENDED)) {
1d28ff4c 348 error(_("invalid key pattern: %s"), key_);
6a83d902 349 FREE_AND_NULL(key_regexp);
9409c7a5 350 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 351 goto free_strings;
2fa9a0fb 352 }
b09c53a3 353 } else {
9409c7a5
JH
354 if (git_config_parse_key(key_, &key, NULL)) {
355 ret = CONFIG_INVALID_KEY;
b09c53a3 356 goto free_strings;
9409c7a5 357 }
2fa9a0fb
JS
358 }
359
3f1bae1d
DS
360 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
361 value_pattern = regex_;
362 else if (regex_) {
f98d863d
JS
363 if (regex_[0] == '!') {
364 do_not_match = 1;
365 regex_++;
366 }
367
2d7320d0 368 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 369 if (regcomp(regexp, regex_, REG_EXTENDED)) {
1d28ff4c 370 error(_("invalid pattern: %s"), regex_);
6a83d902 371 FREE_AND_NULL(regexp);
9409c7a5 372 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 373 goto free_strings;
4ddba79d
JS
374 }
375 }
376
dc8441fd 377 config_with_options(collect_config, &values,
9b6b06c1
VD
378 &given_config_source, the_repository,
379 &config_options);
5f1a63e0 380
eeaa24b9
TB
381 if (!values.nr && default_value) {
382 struct strbuf *item;
383 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
384 item = &values.items[values.nr++];
385 strbuf_init(item, 0);
386 if (format_config(item, key_, default_value) < 0)
387 die(_("failed to format default config value: %s"),
388 default_value);
389 }
390
00b347d3 391 ret = !values.nr;
9b25a0b5 392
7acdd6f0
JK
393 for (i = 0; i < values.nr; i++) {
394 struct strbuf *buf = values.items + i;
00b347d3
JK
395 if (do_all || i == values.nr - 1)
396 fwrite(buf->buf, 1, buf->len, stdout);
7acdd6f0
JK
397 strbuf_release(buf);
398 }
399 free(values.items);
5f1a63e0 400
97ed50f9 401free_strings:
4ddba79d 402 free(key);
35998c89
JK
403 if (key_regexp) {
404 regfree(key_regexp);
405 free(key_regexp);
406 }
0a152171
AW
407 if (regexp) {
408 regfree(regexp);
409 free(regexp);
4ddba79d
JS
410 }
411
5f1a63e0 412 return ret;
4ddba79d 413}
1b1e59c5 414
186458b1 415static char *normalize_value(const char *key, const char *value)
db1696b8 416{
db1696b8
FL
417 if (!value)
418 return NULL;
419
0a8950be 420 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
1349484e
MM
421 /*
422 * We don't do normalization for TYPE_PATH here: If
423 * the path is like ~/foobar/, we prefer to store
424 * "~/foobar/" in the config file, and to expand the ~
425 * when retrieving the value.
5f967424 426 * Also don't do normalization for expiry dates.
1349484e 427 */
3ec832c4 428 return xstrdup(value);
0a8950be 429 if (type == TYPE_INT)
3ec832c4 430 return xstrfmt("%"PRId64, git_config_int64(key, value));
0a8950be 431 if (type == TYPE_BOOL)
3ec832c4 432 return xstrdup(git_config_bool(key, value) ? "true" : "false");
0a8950be 433 if (type == TYPE_BOOL_OR_INT) {
3ec832c4
JK
434 int is_bool, v;
435 v = git_config_bool_or_int(key, value, &is_bool);
436 if (!is_bool)
437 return xstrfmt("%d", v);
438 else
439 return xstrdup(v ? "true" : "false");
db1696b8 440 }
dbd8c09b
LS
441 if (type == TYPE_BOOL_OR_STR) {
442 int v = git_parse_maybe_bool(value);
443 if (v < 0)
444 return xstrdup(value);
445 else
446 return xstrdup(v ? "true" : "false");
447 }
63e2a0f8
TB
448 if (type == TYPE_COLOR) {
449 char v[COLOR_MAXLEN];
450 if (git_config_color(v, key, value))
1d28ff4c 451 die(_("cannot parse color '%s'"), value);
63e2a0f8
TB
452
453 /*
454 * The contents of `v` now contain an ANSI escape
455 * sequence, not suitable for including within a
456 * configuration file. Treat the above as a
457 * "sanity-check", and return the given value, which we
458 * know is representable as valid color code.
459 */
460 return xstrdup(value);
461 }
db1696b8 462
50f08db5 463 BUG("cannot normalize type %d", type);
db1696b8
FL
464}
465
9ce03522
JH
466static int get_color_found;
467static const char *get_color_slot;
b408457f 468static const char *get_colorbool_slot;
9ce03522
JH
469static char parsed_color[COLOR_MAXLEN];
470
783a86c1 471static int git_get_color_config(const char *var, const char *value,
5cf88fd8 472 void *cb UNUSED)
9ce03522
JH
473{
474 if (!strcmp(var, get_color_slot)) {
f769982d
JH
475 if (!value)
476 config_error_nonbool(var);
f6c5a296
JK
477 if (color_parse(value, parsed_color) < 0)
478 return -1;
9ce03522
JH
479 get_color_found = 1;
480 }
481 return 0;
482}
483
d0e08d62 484static void get_color(const char *var, const char *def_color)
9ce03522 485{
d0e08d62 486 get_color_slot = var;
9ce03522
JH
487 get_color_found = 0;
488 parsed_color[0] = '\0';
dc8441fd 489 config_with_options(git_get_color_config, NULL,
9b6b06c1
VD
490 &given_config_source, the_repository,
491 &config_options);
9ce03522 492
f6c5a296
JK
493 if (!get_color_found && def_color) {
494 if (color_parse(def_color, parsed_color) < 0)
495 die(_("unable to parse default color value"));
496 }
9ce03522
JH
497
498 fputs(parsed_color, stdout);
9ce03522
JH
499}
500
0f6f5a40 501static int get_colorbool_found;
69243c2b 502static int get_diff_color_found;
c659f55b 503static int get_color_ui_found;
ef90d6d4 504static int git_get_colorbool_config(const char *var, const char *value,
5cf88fd8 505 void *data UNUSED)
0f6f5a40 506{
e269eb79
JK
507 if (!strcmp(var, get_colorbool_slot))
508 get_colorbool_found = git_config_colorbool(var, value);
509 else if (!strcmp(var, "diff.color"))
510 get_diff_color_found = git_config_colorbool(var, value);
511 else if (!strcmp(var, "color.ui"))
c659f55b 512 get_color_ui_found = git_config_colorbool(var, value);
0f6f5a40
JH
513 return 0;
514}
515
d0e08d62 516static int get_colorbool(const char *var, int print)
0f6f5a40 517{
d0e08d62 518 get_colorbool_slot = var;
69243c2b
JH
519 get_colorbool_found = -1;
520 get_diff_color_found = -1;
b8612b4d 521 get_color_ui_found = -1;
dc8441fd 522 config_with_options(git_get_colorbool_config, NULL,
9b6b06c1
VD
523 &given_config_source, the_repository,
524 &config_options);
0f6f5a40 525
69243c2b 526 if (get_colorbool_found < 0) {
b408457f 527 if (!strcmp(get_colorbool_slot, "color.diff"))
69243c2b
JH
528 get_colorbool_found = get_diff_color_found;
529 if (get_colorbool_found < 0)
c659f55b 530 get_colorbool_found = get_color_ui_found;
69243c2b
JH
531 }
532
b8612b4d
MM
533 if (get_colorbool_found < 0)
534 /* default value if none found in config */
4c7f1819 535 get_colorbool_found = GIT_COLOR_AUTO;
b8612b4d 536
daa0c3d9
JK
537 get_colorbool_found = want_color(get_colorbool_found);
538
0e854a28 539 if (print) {
0f6f5a40
JH
540 printf("%s\n", get_colorbool_found ? "true" : "false");
541 return 0;
0e854a28
FC
542 } else
543 return get_colorbool_found ? 0 : 1;
0f6f5a40
JH
544}
545
6aea9f0f 546static void check_write(void)
1bc88819 547{
638fa623 548 if (!given_config_source.file && !startup_info->have_repository)
1d28ff4c 549 die(_("not in a git directory"));
638fa623 550
3caec73b 551 if (given_config_source.use_stdin)
1d28ff4c 552 die(_("writing to stdin is not supported"));
3caec73b 553
c8985ce0 554 if (given_config_source.blob)
1d28ff4c 555 die(_("writing config blobs is not supported"));
1bc88819
HV
556}
557
d4770964
JH
558struct urlmatch_current_candidate_value {
559 char value_is_null;
560 struct strbuf value;
561};
562
563static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
564{
565 struct string_list *values = cb;
566 struct string_list_item *item = string_list_insert(values, var);
567 struct urlmatch_current_candidate_value *matched = item->util;
568
569 if (!matched) {
570 matched = xmalloc(sizeof(*matched));
571 strbuf_init(&matched->value, 0);
572 item->util = matched;
573 } else {
574 strbuf_reset(&matched->value);
575 }
576
577 if (value) {
578 strbuf_addstr(&matched->value, value);
579 matched->value_is_null = 0;
580 } else {
581 matched->value_is_null = 1;
582 }
583 return 0;
584}
585
d4770964
JH
586static int get_urlmatch(const char *var, const char *url)
587{
27b30be6 588 int ret;
d4770964
JH
589 char *section_tail;
590 struct string_list_item *item;
73ee449b 591 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
d4770964
JH
592 struct string_list values = STRING_LIST_INIT_DUP;
593
594 config.collect_fn = urlmatch_collect_fn;
595 config.cascade_fn = NULL;
596 config.cb = &values;
597
598 if (!url_normalize(url, &config.url))
6667a6ac 599 die("%s", config.url.err);
d4770964 600
88d5a6f6 601 config.section = xstrdup_tolower(var);
d4770964
JH
602 section_tail = strchr(config.section, '.');
603 if (section_tail) {
604 *section_tail = '\0';
605 config.key = section_tail + 1;
606 show_keys = 0;
607 } else {
608 config.key = NULL;
609 show_keys = 1;
610 }
611
dc8441fd 612 config_with_options(urlmatch_config_entry, &config,
9b6b06c1
VD
613 &given_config_source, the_repository,
614 &config_options);
d4770964 615
27b30be6
JK
616 ret = !values.nr;
617
d4770964
JH
618 for_each_string_list_item(item, &values) {
619 struct urlmatch_current_candidate_value *matched = item->util;
d4770964
JH
620 struct strbuf buf = STRBUF_INIT;
621
a92330d2 622 format_config(&buf, item->string,
d4770964
JH
623 matched->value_is_null ? NULL : matched->value.buf);
624 fwrite(buf.buf, 1, buf.len, stdout);
d4770964
JH
625 strbuf_release(&buf);
626
627 strbuf_release(&matched->value);
628 }
a41e8e74 629 urlmatch_config_release(&config);
d4770964
JH
630 string_list_clear(&values, 1);
631 free(config.url.url);
632
633 free((void *)config.section);
27b30be6 634 return ret;
d4770964
JH
635}
636
9830534e
MM
637static char *default_user_config(void)
638{
639 struct strbuf buf = STRBUF_INIT;
640 strbuf_addf(&buf,
641 _("# This is Git's per-user configuration file.\n"
7e110524 642 "[user]\n"
9830534e 643 "# Please adapt and uncomment the following lines:\n"
7e110524 644 "# name = %s\n"
9830534e
MM
645 "# email = %s\n"),
646 ident_default_name(),
647 ident_default_email());
648 return strbuf_detach(&buf, NULL);
649}
650
3ba7e6e2 651int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 652{
3ba7e6e2 653 int nongit = !startup_info->have_repository;
ac95f5d3 654 char *value = NULL;
c90702a1 655 int flags = 0;
ac95f5d3 656 int ret = 0;
7162dff3 657
423ff9be 658 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
dc871831 659
37782920
SB
660 argc = parse_options(argc, argv, prefix, builtin_config_options,
661 builtin_config_usage,
d64ec16c
FC
662 PARSE_OPT_STOP_AT_NON_OPTION);
663
1bc88819 664 if (use_global_config + use_system_config + use_local_config +
58b284a2 665 use_worktree_config +
c8985ce0 666 !!given_config_source.file + !!given_config_source.blob > 1) {
1d28ff4c 667 error(_("only one config file at a time"));
6aaded55 668 usage_builtin_config();
67052c9d
FC
669 }
670
378fe5fc
MT
671 if (nongit) {
672 if (use_local_config)
673 die(_("--local can only be used inside a git repository"));
674 if (given_config_source.blob)
675 die(_("--blob can only be used inside a git repository"));
676 if (use_worktree_config)
677 die(_("--worktree can only be used inside a git repository"));
25cd2919 678
378fe5fc 679 }
17b8a2d6 680
3caec73b
KS
681 if (given_config_source.file &&
682 !strcmp(given_config_source.file, "-")) {
683 given_config_source.file = NULL;
684 given_config_source.use_stdin = 1;
e37efa40 685 given_config_source.scope = CONFIG_SCOPE_COMMAND;
3caec73b
KS
686 }
687
d64ec16c 688 if (use_global_config) {
1e06eb9b 689 char *user_config, *xdg_config;
21cf3227 690
1e06eb9b 691 git_global_config(&user_config, &xdg_config);
e3ebc35b
MM
692 if (!user_config)
693 /*
694 * It is unknown if HOME/.gitconfig exists, so
695 * we do not know if we should write to XDG
696 * location; error out even if XDG_CONFIG_HOME
697 * is set and points at a sane location.
698 */
1d28ff4c 699 die(_("$HOME not set"));
e3ebc35b 700
e37efa40
MR
701 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
702
4698c8fe 703 if (access_or_warn(user_config, R_OK, 0) &&
6c6b08d2 704 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
c8985ce0 705 given_config_source.file = xdg_config;
6c6b08d2
JK
706 free(user_config);
707 } else {
c8985ce0 708 given_config_source.file = user_config;
6c6b08d2
JK
709 free(xdg_config);
710 }
d64ec16c 711 }
e37efa40 712 else if (use_system_config) {
c62a999c 713 given_config_source.file = git_system_config();
e37efa40
MR
714 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
715 } else if (use_local_config) {
c8985ce0 716 given_config_source.file = git_pathdup("config");
e37efa40
MR
717 given_config_source.scope = CONFIG_SCOPE_LOCAL;
718 } else if (use_worktree_config) {
03f2465b 719 struct worktree **worktrees = get_worktrees();
3867f6d6 720 if (the_repository->repository_format_worktree_config)
58b284a2
NTND
721 given_config_source.file = git_pathdup("config.worktree");
722 else if (worktrees[0] && worktrees[1])
723 die(_("--worktree cannot be used with multiple "
724 "working trees unless the config\n"
725 "extension worktreeConfig is enabled. "
726 "Please read \"CONFIGURATION FILE\"\n"
727 "section in \"git help worktree\" for details"));
728 else
729 given_config_source.file = git_pathdup("config");
e37efa40 730 given_config_source.scope = CONFIG_SCOPE_LOCAL;
58b284a2
NTND
731 free_worktrees(worktrees);
732 } else if (given_config_source.file) {
c8985ce0
KS
733 if (!is_absolute_path(given_config_source.file) && prefix)
734 given_config_source.file =
e4da43b1 735 prefix_filename(prefix, given_config_source.file);
e37efa40
MR
736 given_config_source.scope = CONFIG_SCOPE_COMMAND;
737 } else if (given_config_source.blob) {
738 given_config_source.scope = CONFIG_SCOPE_COMMAND;
d64ec16c
FC
739 }
740
e37efa40 741
c48f4b37
NTND
742 if (respect_includes_opt == -1)
743 config_options.respect_includes = !given_config_source.file;
744 else
745 config_options.respect_includes = respect_includes_opt;
dc8441fd
BW
746 if (!nongit) {
747 config_options.commondir = get_git_common_dir();
748 config_options.git_dir = get_git_dir();
749 }
9b25a0b5 750
329e6ec3 751 if (end_nul) {
d64ec16c
FC
752 term = '\0';
753 delim = '\n';
754 key_delim = '\n';
755 }
756
0a8950be 757 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
1d28ff4c 758 error(_("--get-color and variable type are incoherent"));
6aaded55 759 usage_builtin_config();
c2387358
FC
760 }
761
d64ec16c 762 if (HAS_MULTI_BITS(actions)) {
1d28ff4c 763 error(_("only one action at a time"));
6aaded55 764 usage_builtin_config();
d64ec16c
FC
765 }
766 if (actions == 0)
767 switch (argc) {
768 case 1: actions = ACTION_GET; break;
769 case 2: actions = ACTION_SET; break;
770 case 3: actions = ACTION_SET_ALL; break;
771 default:
6aaded55 772 usage_builtin_config();
db1696b8 773 }
578625fa
SG
774 if (omit_values &&
775 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
1d28ff4c 776 error(_("--name-only is only applicable to --list or --get-regexp"));
6aaded55 777 usage_builtin_config();
578625fa 778 }
70bd879a
LS
779
780 if (show_origin && !(actions &
781 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
1d28ff4c
NTND
782 error(_("--show-origin is only applicable to --get, --get-all, "
783 "--get-regexp, and --list"));
6aaded55 784 usage_builtin_config();
70bd879a
LS
785 }
786
eeaa24b9 787 if (default_value && !(actions & ACTION_GET)) {
1d28ff4c 788 error(_("--default is only applicable to --get"));
6aaded55 789 usage_builtin_config();
eeaa24b9
TB
790 }
791
fda43942
DS
792 /* check usage of --fixed-value */
793 if (fixed_value) {
794 int allowed_usage = 0;
795
796 switch (actions) {
797 /* git config --get <name> <value-pattern> */
798 case ACTION_GET:
799 /* git config --get-all <name> <value-pattern> */
800 case ACTION_GET_ALL:
801 /* git config --get-regexp <name-pattern> <value-pattern> */
802 case ACTION_GET_REGEXP:
803 /* git config --unset <name> <value-pattern> */
804 case ACTION_UNSET:
805 /* git config --unset-all <name> <value-pattern> */
806 case ACTION_UNSET_ALL:
807 allowed_usage = argc > 1 && !!argv[1];
808 break;
809
810 /* git config <name> <value> <value-pattern> */
811 case ACTION_SET_ALL:
812 /* git config --replace-all <name> <value> <value-pattern> */
813 case ACTION_REPLACE_ALL:
814 allowed_usage = argc > 2 && !!argv[2];
815 break;
816
817 /* other options don't allow --fixed-value */
818 }
819
820 if (!allowed_usage) {
821 error(_("--fixed-value only applies with 'value-pattern'"));
822 usage_builtin_config();
823 }
c90702a1
DS
824
825 flags |= CONFIG_FLAGS_FIXED_VALUE;
fda43942
DS
826 }
827
32888b8f 828 if (actions & PAGING_ACTIONS)
c0e9f5be 829 setup_auto_pager("config", 1);
32888b8f 830
d64ec16c 831 if (actions == ACTION_LIST) {
225a9caf 832 check_argc(argc, 0, 0);
dc8441fd 833 if (config_with_options(show_all_config, NULL,
9b6b06c1 834 &given_config_source, the_repository,
dc8441fd 835 &config_options) < 0) {
c8985ce0 836 if (given_config_source.file)
1d28ff4c 837 die_errno(_("unable to read config file '%s'"),
c8985ce0 838 given_config_source.file);
d64ec16c 839 else
1d28ff4c 840 die(_("error processing config file(s)"));
db1696b8 841 }
1b1e59c5 842 }
d64ec16c 843 else if (actions == ACTION_EDIT) {
3696a7c2
MH
844 char *config_file;
845
225a9caf 846 check_argc(argc, 0, 0);
c8985ce0 847 if (!given_config_source.file && nongit)
1d28ff4c 848 die(_("not in a git directory"));
3caec73b 849 if (given_config_source.use_stdin)
1d28ff4c 850 die(_("editing stdin is not supported"));
c8985ce0 851 if (given_config_source.blob)
1d28ff4c 852 die(_("editing blobs is not supported"));
d64ec16c 853 git_config(git_default_config, NULL);
d9c69644
JK
854 config_file = given_config_source.file ?
855 xstrdup(given_config_source.file) :
856 git_pathdup("config");
9830534e
MM
857 if (use_global_config) {
858 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
aabbd3f3 859 if (fd >= 0) {
9830534e
MM
860 char *content = default_user_config();
861 write_str_in_full(fd, content);
862 free(content);
863 close(fd);
864 }
865 else if (errno != EEXIST)
866 die_errno(_("cannot create configuration file %s"), config_file);
867 }
868 launch_editor(config_file, NULL, NULL);
3696a7c2 869 free(config_file);
d64ec16c
FC
870 }
871 else if (actions == ACTION_SET) {
6aea9f0f 872 check_write();
d64ec16c
FC
873 check_argc(argc, 2, 2);
874 value = normalize_value(argv[0], argv[1]);
30598ad0 875 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
5a2df368 876 if (ret == CONFIG_NOTHING_SET)
ccf63801
VA
877 error(_("cannot overwrite multiple values with a single value\n"
878 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
d64ec16c
FC
879 }
880 else if (actions == ACTION_SET_ALL) {
6aea9f0f 881 check_write();
d64ec16c
FC
882 check_argc(argc, 2, 3);
883 value = normalize_value(argv[0], argv[1]);
ac95f5d3
ÆAB
884 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
885 argv[0], value, argv[2],
886 flags);
d64ec16c
FC
887 }
888 else if (actions == ACTION_ADD) {
6aea9f0f 889 check_write();
d64ec16c
FC
890 check_argc(argc, 2, 2);
891 value = normalize_value(argv[0], argv[1]);
ac95f5d3
ÆAB
892 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
893 argv[0], value,
894 CONFIG_REGEX_NONE,
895 flags);
d64ec16c
FC
896 }
897 else if (actions == ACTION_REPLACE_ALL) {
6aea9f0f 898 check_write();
d64ec16c
FC
899 check_argc(argc, 2, 3);
900 value = normalize_value(argv[0], argv[1]);
ac95f5d3
ÆAB
901 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
902 argv[0], value, argv[2],
903 flags | CONFIG_FLAGS_MULTI_REPLACE);
d64ec16c
FC
904 }
905 else if (actions == ACTION_GET) {
906 check_argc(argc, 1, 2);
3f1bae1d 907 return get_value(argv[0], argv[1], flags);
d64ec16c
FC
908 }
909 else if (actions == ACTION_GET_ALL) {
910 do_all = 1;
911 check_argc(argc, 1, 2);
3f1bae1d 912 return get_value(argv[0], argv[1], flags);
d64ec16c
FC
913 }
914 else if (actions == ACTION_GET_REGEXP) {
915 show_keys = 1;
916 use_key_regexp = 1;
917 do_all = 1;
918 check_argc(argc, 1, 2);
3f1bae1d 919 return get_value(argv[0], argv[1], flags);
d64ec16c 920 }
d4770964
JH
921 else if (actions == ACTION_GET_URLMATCH) {
922 check_argc(argc, 2, 2);
923 return get_urlmatch(argv[0], argv[1]);
924 }
d64ec16c 925 else if (actions == ACTION_UNSET) {
6aea9f0f 926 check_write();
d64ec16c
FC
927 check_argc(argc, 1, 2);
928 if (argc == 2)
30598ad0 929 return git_config_set_multivar_in_file_gently(given_config_source.file,
c90702a1
DS
930 argv[0], NULL, argv[1],
931 flags);
d64ec16c 932 else
30598ad0
PS
933 return git_config_set_in_file_gently(given_config_source.file,
934 argv[0], NULL);
d64ec16c
FC
935 }
936 else if (actions == ACTION_UNSET_ALL) {
6aea9f0f 937 check_write();
d64ec16c 938 check_argc(argc, 1, 2);
30598ad0 939 return git_config_set_multivar_in_file_gently(given_config_source.file,
504ee129 940 argv[0], NULL, argv[1],
c90702a1 941 flags | CONFIG_FLAGS_MULTI_REPLACE);
d64ec16c
FC
942 }
943 else if (actions == ACTION_RENAME_SECTION) {
6aea9f0f 944 check_write();
d64ec16c 945 check_argc(argc, 2, 2);
c8985ce0 946 ret = git_config_rename_section_in_file(given_config_source.file,
270a3443 947 argv[0], argv[1]);
d64ec16c
FC
948 if (ret < 0)
949 return ret;
ac95f5d3 950 else if (!ret)
1d28ff4c 951 die(_("no such section: %s"), argv[0]);
ac95f5d3
ÆAB
952 else
953 ret = 0;
d64ec16c
FC
954 }
955 else if (actions == ACTION_REMOVE_SECTION) {
6aea9f0f 956 check_write();
d64ec16c 957 check_argc(argc, 1, 1);
c8985ce0 958 ret = git_config_rename_section_in_file(given_config_source.file,
270a3443 959 argv[0], NULL);
d64ec16c
FC
960 if (ret < 0)
961 return ret;
ac95f5d3 962 else if (!ret)
1d28ff4c 963 die(_("no such section: %s"), argv[0]);
ac95f5d3
ÆAB
964 else
965 ret = 0;
d64ec16c
FC
966 }
967 else if (actions == ACTION_GET_COLOR) {
d0e08d62
JK
968 check_argc(argc, 1, 2);
969 get_color(argv[0], argv[1]);
d64ec16c
FC
970 }
971 else if (actions == ACTION_GET_COLORBOOL) {
d0e08d62
JK
972 check_argc(argc, 1, 2);
973 if (argc == 2)
974 color_stdout_is_tty = git_config_bool("command line", argv[1]);
975 return get_colorbool(argv[0], argc == 2);
d64ec16c
FC
976 }
977
ac95f5d3
ÆAB
978 free(value);
979 return ret;
1b1e59c5 980}