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