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