]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/config.c
Merge branch 'fc/t6030-bisect-reset-removes-auxiliary-files'
[thirdparty/git.git] / builtin / config.c
CommitLineData
e12c095a 1#include "builtin.h"
1b1e59c5 2#include "cache.h"
b2141fc1 3#include "config.h"
9ce03522 4#include "color.h"
d64ec16c 5#include "parse-options.h"
d4770964 6#include "urlmatch.h"
70bd879a 7#include "quote.h"
58b284a2 8#include "worktree.h"
1b1e59c5 9
d64ec16c 10static const char *const builtin_config_usage[] = {
9c9b4f2f 11 N_("git config [<options>]"),
d64ec16c
FC
12 NULL
13};
4ddba79d 14
96f1e58f
DR
15static char *key;
16static regex_t *key_regexp;
3f1bae1d 17static const char *value_pattern;
96f1e58f
DR
18static regex_t *regexp;
19static int show_keys;
578625fa 20static int omit_values;
96f1e58f
DR
21static int use_key_regexp;
22static int do_all;
23static int do_not_match;
2275d502
FL
24static char delim = '=';
25static char key_delim = ' ';
26static char term = '\n';
4ddba79d 27
57210a67 28static int use_global_config, use_system_config, use_local_config;
58b284a2 29static int use_worktree_config;
c8985ce0 30static struct git_config_source given_config_source;
0a8950be 31static int actions, type;
eeaa24b9 32static char *default_value;
329e6ec3 33static int end_nul;
c48f4b37
NTND
34static int respect_includes_opt = -1;
35static struct config_options config_options;
70bd879a 36static int show_origin;
145d59f4 37static int show_scope;
fda43942 38static int fixed_value;
d64ec16c
FC
39
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)
d4770964 55#define ACTION_GET_URLMATCH (1<<15)
d64ec16c 56
32888b8f
57/*
58 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
59 * one line of output and which should therefore be paged.
60 */
61#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
62 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
63
0a8950be
TB
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
63e2a0f8 69#define TYPE_COLOR 6
dbd8c09b 70#define TYPE_BOOL_OR_STR 7
16c1e939 71
fb0dc3ba
TB
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) }
75
6aaded55 76static NORETURN void usage_builtin_config(void);
fb0dc3ba
TB
77
78static int option_parse_type(const struct option *opt, const char *arg,
79 int unset)
80{
81 int new_type, *to_type;
82
83 if (unset) {
84 *((int *) opt->value) = 0;
85 return 0;
86 }
87
88 /*
89 * To support '--<type>' style flags, begin with new_type equal to
90 * opt->defval.
91 */
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;
dbd8c09b
LS
100 else if (!strcmp(arg, "bool-or-str"))
101 new_type = TYPE_BOOL_OR_STR;
fb0dc3ba
TB
102 else if (!strcmp(arg, "path"))
103 new_type = TYPE_PATH;
104 else if (!strcmp(arg, "expiry-date"))
105 new_type = TYPE_EXPIRY_DATE;
63e2a0f8
TB
106 else if (!strcmp(arg, "color"))
107 new_type = TYPE_COLOR;
fb0dc3ba
TB
108 else
109 die(_("unrecognized --type argument, %s"), arg);
110 }
111
112 to_type = opt->value;
113 if (*to_type && *to_type != new_type) {
114 /*
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'.
120 */
1d28ff4c 121 error(_("only one type at a time"));
6aaded55 122 usage_builtin_config();
fb0dc3ba
TB
123 }
124 *to_type = new_type;
125
126 return 0;
127}
128
d64ec16c 129static struct option builtin_config_options[] = {
1bd31ce6 130 OPT_GROUP(N_("Config file location")),
21e047dc
SB
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")),
58b284a2 134 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
c8985ce0
KS
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")),
1bd31ce6 137 OPT_GROUP(N_("Action")),
247e2f82
DS
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),
d4770964 141 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
247e2f82 142 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
f63cf8c9 143 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
247e2f82
DS
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),
1bd31ce6
NTND
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),
fda43942 149 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
f63cf8c9 150 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
d0e08d62
JK
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),
1bd31ce6 153 OPT_GROUP(N_("Type")),
fb0dc3ba
TB
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),
dbd8c09b 158 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
fb0dc3ba
TB
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),
1bd31ce6 161 OPT_GROUP(N_("Other")),
329e6ec3 162 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
578625fa 163 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
c48f4b37 164 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
70bd879a 165 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
145d59f4 166 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
eeaa24b9 167 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
d64ec16c
FC
168 OPT_END(),
169};
170
6aaded55
BB
171static NORETURN void usage_builtin_config(void)
172{
173 usage_with_options(builtin_config_usage, builtin_config_options);
174}
175
3b335762
NTND
176static void check_argc(int argc, int min, int max)
177{
d64ec16c
FC
178 if (argc >= min && argc <= max)
179 return;
1a07e59c 180 if (min == max)
1d28ff4c 181 error(_("wrong number of arguments, should be %d"), min);
1a07e59c 182 else
1d28ff4c 183 error(_("wrong number of arguments, should be from %d to %d"),
1a07e59c 184 min, max);
6aaded55 185 usage_builtin_config();
d64ec16c
FC
186}
187
70bd879a
LS
188static void show_config_origin(struct strbuf *buf)
189{
329e6ec3 190 const char term = end_nul ? '\0' : '\t';
70bd879a
LS
191
192 strbuf_addstr(buf, current_config_origin_type());
193 strbuf_addch(buf, ':');
329e6ec3 194 if (end_nul)
70bd879a
LS
195 strbuf_addstr(buf, current_config_name());
196 else
197 quote_c_style(current_config_name(), buf, NULL, 0);
198 strbuf_addch(buf, term);
199}
200
145d59f4
MR
201static void show_config_scope(struct strbuf *buf)
202{
203 const char term = end_nul ? '\0' : '\t';
204 const char *scope = config_scope_name(current_config_scope());
205
206 strbuf_addstr(buf, N_(scope));
207 strbuf_addch(buf, term);
208}
209
ef90d6d4 210static int show_all_config(const char *key_, const char *value_, void *cb)
de791f15 211{
145d59f4 212 if (show_origin || show_scope) {
70bd879a 213 struct strbuf buf = STRBUF_INIT;
145d59f4
MR
214 if (show_scope)
215 show_config_scope(&buf);
216 if (show_origin)
217 show_config_origin(&buf);
70bd879a
LS
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);
221 }
578625fa 222 if (!omit_values && value_)
2275d502 223 printf("%s%c%s%c", key_, delim, value_, term);
de791f15 224 else
2275d502 225 printf("%s%c", key_, term);
de791f15
PB
226 return 0;
227}
228
7acdd6f0
JK
229struct strbuf_list {
230 struct strbuf *items;
231 int nr;
232 int alloc;
233};
234
d9b9169b 235static int format_config(struct strbuf *buf, const char *key_, const char *value_)
4ddba79d 236{
145d59f4
MR
237 if (show_scope)
238 show_config_scope(buf);
70bd879a
LS
239 if (show_origin)
240 show_config_origin(buf);
ebca2d49 241 if (show_keys)
7acdd6f0 242 strbuf_addstr(buf, key_);
ebca2d49 243 if (!omit_values) {
f2259877
JK
244 if (show_keys)
245 strbuf_addch(buf, key_delim);
ebca2d49 246
0a8950be 247 if (type == TYPE_INT)
f2259877
JK
248 strbuf_addf(buf, "%"PRId64,
249 git_config_int64(key_, value_ ? value_ : ""));
0a8950be 250 else if (type == TYPE_BOOL)
f2259877
JK
251 strbuf_addstr(buf, git_config_bool(key_, value_) ?
252 "true" : "false");
0a8950be 253 else if (type == TYPE_BOOL_OR_INT) {
ebca2d49
SG
254 int is_bool, v;
255 v = git_config_bool_or_int(key_, value_, &is_bool);
256 if (is_bool)
f2259877 257 strbuf_addstr(buf, v ? "true" : "false");
ebca2d49 258 else
f2259877 259 strbuf_addf(buf, "%d", v);
dbd8c09b
LS
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");
0a8950be 266 } else if (type == TYPE_PATH) {
f2259877
JK
267 const char *v;
268 if (git_config_pathname(&v, key_, value_) < 0)
ebca2d49 269 return -1;
f2259877
JK
270 strbuf_addstr(buf, v);
271 free((char *)v);
0a8950be 272 } else if (type == TYPE_EXPIRY_DATE) {
5f967424
HM
273 timestamp_t t;
274 if (git_config_expiry_date(&t, key_, value_) < 0)
275 return -1;
276 strbuf_addf(buf, "%"PRItime, t);
63e2a0f8
TB
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);
ebca2d49 282 } else if (value_) {
f2259877 283 strbuf_addstr(buf, value_);
ebca2d49 284 } else {
f2259877
JK
285 /* Just show the key name; back out delimiter */
286 if (show_keys)
287 strbuf_setlen(buf, buf->len - 1);
ebca2d49 288 }
578625fa 289 }
00b347d3 290 strbuf_addch(buf, term);
4ddba79d
JS
291 return 0;
292}
293
d9b9169b
JH
294static int collect_config(const char *key_, const char *value_, void *cb)
295{
296 struct strbuf_list *values = cb;
297
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;
3f1bae1d
DS
302 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
303 return 0;
d9b9169b
JH
304 if (regexp != NULL &&
305 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
306 return 0;
307
308 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
9f1429df 309 strbuf_init(&values->items[values->nr], 0);
d9b9169b
JH
310
311 return format_config(&values->items[values->nr++], key_, value_);
312}
313
3f1bae1d 314static int get_value(const char *key_, const char *regex_, unsigned flags)
4ddba79d 315{
9409c7a5 316 int ret = CONFIG_GENERIC_ERROR;
5ba1a8a7 317 struct strbuf_list values = {NULL};
7acdd6f0 318 int i;
4ddba79d 319
2fa9a0fb 320 if (use_key_regexp) {
b09c53a3
LP
321 char *tl;
322
323 /*
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.
327 */
328
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);
336
2d7320d0 337 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 338 if (regcomp(key_regexp, key, REG_EXTENDED)) {
1d28ff4c 339 error(_("invalid key pattern: %s"), key_);
6a83d902 340 FREE_AND_NULL(key_regexp);
9409c7a5 341 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 342 goto free_strings;
2fa9a0fb 343 }
b09c53a3 344 } else {
9409c7a5
JH
345 if (git_config_parse_key(key_, &key, NULL)) {
346 ret = CONFIG_INVALID_KEY;
b09c53a3 347 goto free_strings;
9409c7a5 348 }
2fa9a0fb
JS
349 }
350
3f1bae1d
DS
351 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
352 value_pattern = regex_;
353 else if (regex_) {
f98d863d
JS
354 if (regex_[0] == '!') {
355 do_not_match = 1;
356 regex_++;
357 }
358
2d7320d0 359 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 360 if (regcomp(regexp, regex_, REG_EXTENDED)) {
1d28ff4c 361 error(_("invalid pattern: %s"), regex_);
6a83d902 362 FREE_AND_NULL(regexp);
9409c7a5 363 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 364 goto free_strings;
4ddba79d
JS
365 }
366 }
367
dc8441fd
BW
368 config_with_options(collect_config, &values,
369 &given_config_source, &config_options);
5f1a63e0 370
eeaa24b9
TB
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);
379 }
380
00b347d3 381 ret = !values.nr;
9b25a0b5 382
7acdd6f0
JK
383 for (i = 0; i < values.nr; i++) {
384 struct strbuf *buf = values.items + i;
00b347d3
JK
385 if (do_all || i == values.nr - 1)
386 fwrite(buf->buf, 1, buf->len, stdout);
7acdd6f0
JK
387 strbuf_release(buf);
388 }
389 free(values.items);
5f1a63e0 390
97ed50f9 391free_strings:
4ddba79d 392 free(key);
35998c89
JK
393 if (key_regexp) {
394 regfree(key_regexp);
395 free(key_regexp);
396 }
0a152171
AW
397 if (regexp) {
398 regfree(regexp);
399 free(regexp);
4ddba79d
JS
400 }
401
5f1a63e0 402 return ret;
4ddba79d 403}
1b1e59c5 404
186458b1 405static char *normalize_value(const char *key, const char *value)
db1696b8 406{
db1696b8
FL
407 if (!value)
408 return NULL;
409
0a8950be 410 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
1349484e
MM
411 /*
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.
5f967424 416 * Also don't do normalization for expiry dates.
1349484e 417 */
3ec832c4 418 return xstrdup(value);
0a8950be 419 if (type == TYPE_INT)
3ec832c4 420 return xstrfmt("%"PRId64, git_config_int64(key, value));
0a8950be 421 if (type == TYPE_BOOL)
3ec832c4 422 return xstrdup(git_config_bool(key, value) ? "true" : "false");
0a8950be 423 if (type == TYPE_BOOL_OR_INT) {
3ec832c4
JK
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");
db1696b8 430 }
dbd8c09b
LS
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");
437 }
63e2a0f8
TB
438 if (type == TYPE_COLOR) {
439 char v[COLOR_MAXLEN];
440 if (git_config_color(v, key, value))
1d28ff4c 441 die(_("cannot parse color '%s'"), value);
63e2a0f8
TB
442
443 /*
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.
449 */
450 return xstrdup(value);
451 }
db1696b8 452
50f08db5 453 BUG("cannot normalize type %d", type);
db1696b8
FL
454}
455
9ce03522
JH
456static int get_color_found;
457static const char *get_color_slot;
b408457f 458static const char *get_colorbool_slot;
9ce03522
JH
459static char parsed_color[COLOR_MAXLEN];
460
ef90d6d4 461static int git_get_color_config(const char *var, const char *value, void *cb)
9ce03522
JH
462{
463 if (!strcmp(var, get_color_slot)) {
f769982d
JH
464 if (!value)
465 config_error_nonbool(var);
f6c5a296
JK
466 if (color_parse(value, parsed_color) < 0)
467 return -1;
9ce03522
JH
468 get_color_found = 1;
469 }
470 return 0;
471}
472
d0e08d62 473static void get_color(const char *var, const char *def_color)
9ce03522 474{
d0e08d62 475 get_color_slot = var;
9ce03522
JH
476 get_color_found = 0;
477 parsed_color[0] = '\0';
dc8441fd
BW
478 config_with_options(git_get_color_config, NULL,
479 &given_config_source, &config_options);
9ce03522 480
f6c5a296
JK
481 if (!get_color_found && def_color) {
482 if (color_parse(def_color, parsed_color) < 0)
483 die(_("unable to parse default color value"));
484 }
9ce03522
JH
485
486 fputs(parsed_color, stdout);
9ce03522
JH
487}
488
0f6f5a40 489static int get_colorbool_found;
69243c2b 490static int get_diff_color_found;
c659f55b 491static int get_color_ui_found;
ef90d6d4
JS
492static int git_get_colorbool_config(const char *var, const char *value,
493 void *cb)
0f6f5a40 494{
e269eb79
JK
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"))
c659f55b 500 get_color_ui_found = git_config_colorbool(var, value);
0f6f5a40
JH
501 return 0;
502}
503
d0e08d62 504static int get_colorbool(const char *var, int print)
0f6f5a40 505{
d0e08d62 506 get_colorbool_slot = var;
69243c2b
JH
507 get_colorbool_found = -1;
508 get_diff_color_found = -1;
b8612b4d 509 get_color_ui_found = -1;
dc8441fd
BW
510 config_with_options(git_get_colorbool_config, NULL,
511 &given_config_source, &config_options);
0f6f5a40 512
69243c2b 513 if (get_colorbool_found < 0) {
b408457f 514 if (!strcmp(get_colorbool_slot, "color.diff"))
69243c2b
JH
515 get_colorbool_found = get_diff_color_found;
516 if (get_colorbool_found < 0)
c659f55b 517 get_colorbool_found = get_color_ui_found;
69243c2b
JH
518 }
519
b8612b4d
MM
520 if (get_colorbool_found < 0)
521 /* default value if none found in config */
4c7f1819 522 get_colorbool_found = GIT_COLOR_AUTO;
b8612b4d 523
daa0c3d9
JK
524 get_colorbool_found = want_color(get_colorbool_found);
525
0e854a28 526 if (print) {
0f6f5a40
JH
527 printf("%s\n", get_colorbool_found ? "true" : "false");
528 return 0;
0e854a28
FC
529 } else
530 return get_colorbool_found ? 0 : 1;
0f6f5a40
JH
531}
532
6aea9f0f 533static void check_write(void)
1bc88819 534{
638fa623 535 if (!given_config_source.file && !startup_info->have_repository)
1d28ff4c 536 die(_("not in a git directory"));
638fa623 537
3caec73b 538 if (given_config_source.use_stdin)
1d28ff4c 539 die(_("writing to stdin is not supported"));
3caec73b 540
c8985ce0 541 if (given_config_source.blob)
1d28ff4c 542 die(_("writing config blobs is not supported"));
1bc88819
HV
543}
544
d4770964
JH
545struct urlmatch_current_candidate_value {
546 char value_is_null;
547 struct strbuf value;
548};
549
550static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
551{
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;
555
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);
562 }
563
564 if (value) {
565 strbuf_addstr(&matched->value, value);
566 matched->value_is_null = 0;
567 } else {
568 matched->value_is_null = 1;
569 }
570 return 0;
571}
572
d4770964
JH
573static int get_urlmatch(const char *var, const char *url)
574{
27b30be6 575 int ret;
d4770964
JH
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;
580
581 config.collect_fn = urlmatch_collect_fn;
582 config.cascade_fn = NULL;
583 config.cb = &values;
584
585 if (!url_normalize(url, &config.url))
6667a6ac 586 die("%s", config.url.err);
d4770964 587
88d5a6f6 588 config.section = xstrdup_tolower(var);
d4770964
JH
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;
597 }
598
dc8441fd
BW
599 config_with_options(urlmatch_config_entry, &config,
600 &given_config_source, &config_options);
d4770964 601
27b30be6
JK
602 ret = !values.nr;
603
d4770964
JH
604 for_each_string_list_item(item, &values) {
605 struct urlmatch_current_candidate_value *matched = item->util;
d4770964
JH
606 struct strbuf buf = STRBUF_INIT;
607
a92330d2 608 format_config(&buf, item->string,
d4770964
JH
609 matched->value_is_null ? NULL : matched->value.buf);
610 fwrite(buf.buf, 1, buf.len, stdout);
d4770964
JH
611 strbuf_release(&buf);
612
613 strbuf_release(&matched->value);
614 }
615 string_list_clear(&config.vars, 1);
616 string_list_clear(&values, 1);
617 free(config.url.url);
618
619 free((void *)config.section);
27b30be6 620 return ret;
d4770964
JH
621}
622
9830534e
MM
623static char *default_user_config(void)
624{
625 struct strbuf buf = STRBUF_INIT;
626 strbuf_addf(&buf,
627 _("# This is Git's per-user configuration file.\n"
7e110524 628 "[user]\n"
9830534e 629 "# Please adapt and uncomment the following lines:\n"
7e110524 630 "# name = %s\n"
9830534e
MM
631 "# email = %s\n"),
632 ident_default_name(),
633 ident_default_email());
634 return strbuf_detach(&buf, NULL);
635}
636
3ba7e6e2 637int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 638{
3ba7e6e2 639 int nongit = !startup_info->have_repository;
4b951b7e 640 char *value;
c90702a1 641 int flags = 0;
7162dff3 642
423ff9be 643 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
dc871831 644
37782920
SB
645 argc = parse_options(argc, argv, prefix, builtin_config_options,
646 builtin_config_usage,
d64ec16c
FC
647 PARSE_OPT_STOP_AT_NON_OPTION);
648
1bc88819 649 if (use_global_config + use_system_config + use_local_config +
58b284a2 650 use_worktree_config +
c8985ce0 651 !!given_config_source.file + !!given_config_source.blob > 1) {
1d28ff4c 652 error(_("only one config file at a time"));
6aaded55 653 usage_builtin_config();
67052c9d
FC
654 }
655
378fe5fc
MT
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"));
25cd2919 663
378fe5fc 664 }
17b8a2d6 665
3caec73b
KS
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;
e37efa40 670 given_config_source.scope = CONFIG_SCOPE_COMMAND;
3caec73b
KS
671 }
672
d64ec16c 673 if (use_global_config) {
4aad2f16 674 char *user_config = expand_user_path("~/.gitconfig", 0);
509adc33 675 char *xdg_config = xdg_config_home("config");
21cf3227 676
e3ebc35b
MM
677 if (!user_config)
678 /*
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.
683 */
1d28ff4c 684 die(_("$HOME not set"));
e3ebc35b 685
e37efa40
MR
686 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
687
4698c8fe 688 if (access_or_warn(user_config, R_OK, 0) &&
6c6b08d2 689 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
c8985ce0 690 given_config_source.file = xdg_config;
6c6b08d2
JK
691 free(user_config);
692 } else {
c8985ce0 693 given_config_source.file = user_config;
6c6b08d2
JK
694 free(xdg_config);
695 }
d64ec16c 696 }
e37efa40 697 else if (use_system_config) {
c8985ce0 698 given_config_source.file = git_etc_gitconfig();
e37efa40
MR
699 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
700 } else if (use_local_config) {
c8985ce0 701 given_config_source.file = git_pathdup("config");
e37efa40
MR
702 given_config_source.scope = CONFIG_SCOPE_LOCAL;
703 } else if (use_worktree_config) {
03f2465b 704 struct worktree **worktrees = get_worktrees();
58b284a2
NTND
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");
e37efa40 715 given_config_source.scope = CONFIG_SCOPE_LOCAL;
58b284a2
NTND
716 free_worktrees(worktrees);
717 } else if (given_config_source.file) {
c8985ce0
KS
718 if (!is_absolute_path(given_config_source.file) && prefix)
719 given_config_source.file =
e4da43b1 720 prefix_filename(prefix, given_config_source.file);
e37efa40
MR
721 given_config_source.scope = CONFIG_SCOPE_COMMAND;
722 } else if (given_config_source.blob) {
723 given_config_source.scope = CONFIG_SCOPE_COMMAND;
d64ec16c
FC
724 }
725
e37efa40 726
c48f4b37
NTND
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;
dc8441fd
BW
731 if (!nongit) {
732 config_options.commondir = get_git_common_dir();
733 config_options.git_dir = get_git_dir();
734 }
9b25a0b5 735
329e6ec3 736 if (end_nul) {
d64ec16c
FC
737 term = '\0';
738 delim = '\n';
739 key_delim = '\n';
740 }
741
0a8950be 742 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
1d28ff4c 743 error(_("--get-color and variable type are incoherent"));
6aaded55 744 usage_builtin_config();
c2387358
FC
745 }
746
d64ec16c 747 if (HAS_MULTI_BITS(actions)) {
1d28ff4c 748 error(_("only one action at a time"));
6aaded55 749 usage_builtin_config();
d64ec16c
FC
750 }
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:
6aaded55 757 usage_builtin_config();
db1696b8 758 }
578625fa
SG
759 if (omit_values &&
760 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
1d28ff4c 761 error(_("--name-only is only applicable to --list or --get-regexp"));
6aaded55 762 usage_builtin_config();
578625fa 763 }
70bd879a
LS
764
765 if (show_origin && !(actions &
766 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
1d28ff4c
NTND
767 error(_("--show-origin is only applicable to --get, --get-all, "
768 "--get-regexp, and --list"));
6aaded55 769 usage_builtin_config();
70bd879a
LS
770 }
771
eeaa24b9 772 if (default_value && !(actions & ACTION_GET)) {
1d28ff4c 773 error(_("--default is only applicable to --get"));
6aaded55 774 usage_builtin_config();
eeaa24b9
TB
775 }
776
fda43942
DS
777 /* check usage of --fixed-value */
778 if (fixed_value) {
779 int allowed_usage = 0;
780
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;
794
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;
801
802 /* other options don't allow --fixed-value */
803 }
804
805 if (!allowed_usage) {
806 error(_("--fixed-value only applies with 'value-pattern'"));
807 usage_builtin_config();
808 }
c90702a1
DS
809
810 flags |= CONFIG_FLAGS_FIXED_VALUE;
fda43942
DS
811 }
812
32888b8f 813 if (actions & PAGING_ACTIONS)
c0e9f5be 814 setup_auto_pager("config", 1);
32888b8f 815
d64ec16c 816 if (actions == ACTION_LIST) {
225a9caf 817 check_argc(argc, 0, 0);
dc8441fd
BW
818 if (config_with_options(show_all_config, NULL,
819 &given_config_source,
820 &config_options) < 0) {
c8985ce0 821 if (given_config_source.file)
1d28ff4c 822 die_errno(_("unable to read config file '%s'"),
c8985ce0 823 given_config_source.file);
d64ec16c 824 else
1d28ff4c 825 die(_("error processing config file(s)"));
db1696b8 826 }
1b1e59c5 827 }
d64ec16c 828 else if (actions == ACTION_EDIT) {
3696a7c2
MH
829 char *config_file;
830
225a9caf 831 check_argc(argc, 0, 0);
c8985ce0 832 if (!given_config_source.file && nongit)
1d28ff4c 833 die(_("not in a git directory"));
3caec73b 834 if (given_config_source.use_stdin)
1d28ff4c 835 die(_("editing stdin is not supported"));
c8985ce0 836 if (given_config_source.blob)
1d28ff4c 837 die(_("editing blobs is not supported"));
d64ec16c 838 git_config(git_default_config, NULL);
d9c69644
JK
839 config_file = given_config_source.file ?
840 xstrdup(given_config_source.file) :
841 git_pathdup("config");
9830534e
MM
842 if (use_global_config) {
843 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
aabbd3f3 844 if (fd >= 0) {
9830534e
MM
845 char *content = default_user_config();
846 write_str_in_full(fd, content);
847 free(content);
848 close(fd);
849 }
850 else if (errno != EEXIST)
851 die_errno(_("cannot create configuration file %s"), config_file);
852 }
853 launch_editor(config_file, NULL, NULL);
3696a7c2 854 free(config_file);
d64ec16c
FC
855 }
856 else if (actions == ACTION_SET) {
5a2df368 857 int ret;
6aea9f0f 858 check_write();
d64ec16c
FC
859 check_argc(argc, 2, 2);
860 value = normalize_value(argv[0], argv[1]);
0e5bba53 861 UNLEAK(value);
30598ad0 862 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
5a2df368 863 if (ret == CONFIG_NOTHING_SET)
ccf63801
VA
864 error(_("cannot overwrite multiple values with a single value\n"
865 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
5a2df368 866 return ret;
d64ec16c
FC
867 }
868 else if (actions == ACTION_SET_ALL) {
6aea9f0f 869 check_write();
d64ec16c
FC
870 check_argc(argc, 2, 3);
871 value = normalize_value(argv[0], argv[1]);
0e5bba53 872 UNLEAK(value);
30598ad0 873 return git_config_set_multivar_in_file_gently(given_config_source.file,
c90702a1
DS
874 argv[0], value, argv[2],
875 flags);
d64ec16c
FC
876 }
877 else if (actions == ACTION_ADD) {
6aea9f0f 878 check_write();
d64ec16c
FC
879 check_argc(argc, 2, 2);
880 value = normalize_value(argv[0], argv[1]);
0e5bba53 881 UNLEAK(value);
30598ad0
PS
882 return git_config_set_multivar_in_file_gently(given_config_source.file,
883 argv[0], value,
c90702a1
DS
884 CONFIG_REGEX_NONE,
885 flags);
d64ec16c
FC
886 }
887 else if (actions == ACTION_REPLACE_ALL) {
6aea9f0f 888 check_write();
d64ec16c
FC
889 check_argc(argc, 2, 3);
890 value = normalize_value(argv[0], argv[1]);
0e5bba53 891 UNLEAK(value);
30598ad0 892 return git_config_set_multivar_in_file_gently(given_config_source.file,
504ee129 893 argv[0], value, argv[2],
c90702a1 894 flags | CONFIG_FLAGS_MULTI_REPLACE);
d64ec16c
FC
895 }
896 else if (actions == ACTION_GET) {
897 check_argc(argc, 1, 2);
3f1bae1d 898 return get_value(argv[0], argv[1], flags);
d64ec16c
FC
899 }
900 else if (actions == ACTION_GET_ALL) {
901 do_all = 1;
902 check_argc(argc, 1, 2);
3f1bae1d 903 return get_value(argv[0], argv[1], flags);
d64ec16c
FC
904 }
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);
3f1bae1d 910 return get_value(argv[0], argv[1], flags);
d64ec16c 911 }
d4770964
JH
912 else if (actions == ACTION_GET_URLMATCH) {
913 check_argc(argc, 2, 2);
914 return get_urlmatch(argv[0], argv[1]);
915 }
d64ec16c 916 else if (actions == ACTION_UNSET) {
6aea9f0f 917 check_write();
d64ec16c
FC
918 check_argc(argc, 1, 2);
919 if (argc == 2)
30598ad0 920 return git_config_set_multivar_in_file_gently(given_config_source.file,
c90702a1
DS
921 argv[0], NULL, argv[1],
922 flags);
d64ec16c 923 else
30598ad0
PS
924 return git_config_set_in_file_gently(given_config_source.file,
925 argv[0], NULL);
d64ec16c
FC
926 }
927 else if (actions == ACTION_UNSET_ALL) {
6aea9f0f 928 check_write();
d64ec16c 929 check_argc(argc, 1, 2);
30598ad0 930 return git_config_set_multivar_in_file_gently(given_config_source.file,
504ee129 931 argv[0], NULL, argv[1],
c90702a1 932 flags | CONFIG_FLAGS_MULTI_REPLACE);
d64ec16c
FC
933 }
934 else if (actions == ACTION_RENAME_SECTION) {
935 int ret;
6aea9f0f 936 check_write();
d64ec16c 937 check_argc(argc, 2, 2);
c8985ce0 938 ret = git_config_rename_section_in_file(given_config_source.file,
270a3443 939 argv[0], argv[1]);
d64ec16c
FC
940 if (ret < 0)
941 return ret;
942 if (ret == 0)
1d28ff4c 943 die(_("no such section: %s"), argv[0]);
d64ec16c
FC
944 }
945 else if (actions == ACTION_REMOVE_SECTION) {
946 int ret;
6aea9f0f 947 check_write();
d64ec16c 948 check_argc(argc, 1, 1);
c8985ce0 949 ret = git_config_rename_section_in_file(given_config_source.file,
270a3443 950 argv[0], NULL);
d64ec16c
FC
951 if (ret < 0)
952 return ret;
953 if (ret == 0)
1d28ff4c 954 die(_("no such section: %s"), argv[0]);
d64ec16c
FC
955 }
956 else if (actions == ACTION_GET_COLOR) {
d0e08d62
JK
957 check_argc(argc, 1, 2);
958 get_color(argv[0], argv[1]);
d64ec16c
FC
959 }
960 else if (actions == ACTION_GET_COLORBOOL) {
d0e08d62
JK
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);
d64ec16c
FC
965 }
966
1b1e59c5
JS
967 return 0;
968}