]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/config.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[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")),
5445124f 154 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
fb0dc3ba
TB
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
783a86c1 210static int show_all_config(const char *key_, const char *value_,
5cf88fd8 211 void *cb UNUSED)
de791f15 212{
145d59f4 213 if (show_origin || show_scope) {
70bd879a 214 struct strbuf buf = STRBUF_INIT;
145d59f4
MR
215 if (show_scope)
216 show_config_scope(&buf);
217 if (show_origin)
218 show_config_origin(&buf);
70bd879a
LS
219 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
220 fwrite(buf.buf, 1, buf.len, stdout);
221 strbuf_release(&buf);
222 }
578625fa 223 if (!omit_values && value_)
2275d502 224 printf("%s%c%s%c", key_, delim, value_, term);
de791f15 225 else
2275d502 226 printf("%s%c", key_, term);
de791f15
PB
227 return 0;
228}
229
7acdd6f0
JK
230struct strbuf_list {
231 struct strbuf *items;
232 int nr;
233 int alloc;
234};
235
d9b9169b 236static int format_config(struct strbuf *buf, const char *key_, const char *value_)
4ddba79d 237{
145d59f4
MR
238 if (show_scope)
239 show_config_scope(buf);
70bd879a
LS
240 if (show_origin)
241 show_config_origin(buf);
ebca2d49 242 if (show_keys)
7acdd6f0 243 strbuf_addstr(buf, key_);
ebca2d49 244 if (!omit_values) {
f2259877
JK
245 if (show_keys)
246 strbuf_addch(buf, key_delim);
ebca2d49 247
0a8950be 248 if (type == TYPE_INT)
f2259877
JK
249 strbuf_addf(buf, "%"PRId64,
250 git_config_int64(key_, value_ ? value_ : ""));
0a8950be 251 else if (type == TYPE_BOOL)
f2259877
JK
252 strbuf_addstr(buf, git_config_bool(key_, value_) ?
253 "true" : "false");
0a8950be 254 else if (type == TYPE_BOOL_OR_INT) {
ebca2d49
SG
255 int is_bool, v;
256 v = git_config_bool_or_int(key_, value_, &is_bool);
257 if (is_bool)
f2259877 258 strbuf_addstr(buf, v ? "true" : "false");
ebca2d49 259 else
f2259877 260 strbuf_addf(buf, "%d", v);
dbd8c09b
LS
261 } else if (type == TYPE_BOOL_OR_STR) {
262 int v = git_parse_maybe_bool(value_);
263 if (v < 0)
264 strbuf_addstr(buf, value_);
265 else
266 strbuf_addstr(buf, v ? "true" : "false");
0a8950be 267 } else if (type == TYPE_PATH) {
f2259877
JK
268 const char *v;
269 if (git_config_pathname(&v, key_, value_) < 0)
ebca2d49 270 return -1;
f2259877
JK
271 strbuf_addstr(buf, v);
272 free((char *)v);
0a8950be 273 } else if (type == TYPE_EXPIRY_DATE) {
5f967424
HM
274 timestamp_t t;
275 if (git_config_expiry_date(&t, key_, value_) < 0)
276 return -1;
277 strbuf_addf(buf, "%"PRItime, t);
63e2a0f8
TB
278 } else if (type == TYPE_COLOR) {
279 char v[COLOR_MAXLEN];
280 if (git_config_color(v, key_, value_) < 0)
281 return -1;
282 strbuf_addstr(buf, v);
ebca2d49 283 } else if (value_) {
f2259877 284 strbuf_addstr(buf, value_);
ebca2d49 285 } else {
f2259877
JK
286 /* Just show the key name; back out delimiter */
287 if (show_keys)
288 strbuf_setlen(buf, buf->len - 1);
ebca2d49 289 }
578625fa 290 }
00b347d3 291 strbuf_addch(buf, term);
4ddba79d
JS
292 return 0;
293}
294
d9b9169b
JH
295static int collect_config(const char *key_, const char *value_, void *cb)
296{
297 struct strbuf_list *values = cb;
298
299 if (!use_key_regexp && strcmp(key_, key))
300 return 0;
301 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
302 return 0;
3f1bae1d
DS
303 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
304 return 0;
d9b9169b
JH
305 if (regexp != NULL &&
306 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
307 return 0;
308
309 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
9f1429df 310 strbuf_init(&values->items[values->nr], 0);
d9b9169b
JH
311
312 return format_config(&values->items[values->nr++], key_, value_);
313}
314
3f1bae1d 315static int get_value(const char *key_, const char *regex_, unsigned flags)
4ddba79d 316{
9409c7a5 317 int ret = CONFIG_GENERIC_ERROR;
5ba1a8a7 318 struct strbuf_list values = {NULL};
7acdd6f0 319 int i;
4ddba79d 320
2fa9a0fb 321 if (use_key_regexp) {
b09c53a3
LP
322 char *tl;
323
324 /*
325 * NEEDSWORK: this naive pattern lowercasing obviously does not
326 * work for more complex patterns like "^[^.]*Foo.*bar".
327 * Perhaps we should deprecate this altogether someday.
328 */
329
330 key = xstrdup(key_);
331 for (tl = key + strlen(key) - 1;
332 tl >= key && *tl != '.';
333 tl--)
334 *tl = tolower(*tl);
335 for (tl = key; *tl && *tl != '.'; tl++)
336 *tl = tolower(*tl);
337
2d7320d0 338 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 339 if (regcomp(key_regexp, key, REG_EXTENDED)) {
1d28ff4c 340 error(_("invalid key pattern: %s"), key_);
6a83d902 341 FREE_AND_NULL(key_regexp);
9409c7a5 342 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 343 goto free_strings;
2fa9a0fb 344 }
b09c53a3 345 } else {
9409c7a5
JH
346 if (git_config_parse_key(key_, &key, NULL)) {
347 ret = CONFIG_INVALID_KEY;
b09c53a3 348 goto free_strings;
9409c7a5 349 }
2fa9a0fb
JS
350 }
351
3f1bae1d
DS
352 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
353 value_pattern = regex_;
354 else if (regex_) {
f98d863d
JS
355 if (regex_[0] == '!') {
356 do_not_match = 1;
357 regex_++;
358 }
359
2d7320d0 360 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 361 if (regcomp(regexp, regex_, REG_EXTENDED)) {
1d28ff4c 362 error(_("invalid pattern: %s"), regex_);
6a83d902 363 FREE_AND_NULL(regexp);
9409c7a5 364 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 365 goto free_strings;
4ddba79d
JS
366 }
367 }
368
dc8441fd
BW
369 config_with_options(collect_config, &values,
370 &given_config_source, &config_options);
5f1a63e0 371
eeaa24b9
TB
372 if (!values.nr && default_value) {
373 struct strbuf *item;
374 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
375 item = &values.items[values.nr++];
376 strbuf_init(item, 0);
377 if (format_config(item, key_, default_value) < 0)
378 die(_("failed to format default config value: %s"),
379 default_value);
380 }
381
00b347d3 382 ret = !values.nr;
9b25a0b5 383
7acdd6f0
JK
384 for (i = 0; i < values.nr; i++) {
385 struct strbuf *buf = values.items + i;
00b347d3
JK
386 if (do_all || i == values.nr - 1)
387 fwrite(buf->buf, 1, buf->len, stdout);
7acdd6f0
JK
388 strbuf_release(buf);
389 }
390 free(values.items);
5f1a63e0 391
97ed50f9 392free_strings:
4ddba79d 393 free(key);
35998c89
JK
394 if (key_regexp) {
395 regfree(key_regexp);
396 free(key_regexp);
397 }
0a152171
AW
398 if (regexp) {
399 regfree(regexp);
400 free(regexp);
4ddba79d
JS
401 }
402
5f1a63e0 403 return ret;
4ddba79d 404}
1b1e59c5 405
186458b1 406static char *normalize_value(const char *key, const char *value)
db1696b8 407{
db1696b8
FL
408 if (!value)
409 return NULL;
410
0a8950be 411 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
1349484e
MM
412 /*
413 * We don't do normalization for TYPE_PATH here: If
414 * the path is like ~/foobar/, we prefer to store
415 * "~/foobar/" in the config file, and to expand the ~
416 * when retrieving the value.
5f967424 417 * Also don't do normalization for expiry dates.
1349484e 418 */
3ec832c4 419 return xstrdup(value);
0a8950be 420 if (type == TYPE_INT)
3ec832c4 421 return xstrfmt("%"PRId64, git_config_int64(key, value));
0a8950be 422 if (type == TYPE_BOOL)
3ec832c4 423 return xstrdup(git_config_bool(key, value) ? "true" : "false");
0a8950be 424 if (type == TYPE_BOOL_OR_INT) {
3ec832c4
JK
425 int is_bool, v;
426 v = git_config_bool_or_int(key, value, &is_bool);
427 if (!is_bool)
428 return xstrfmt("%d", v);
429 else
430 return xstrdup(v ? "true" : "false");
db1696b8 431 }
dbd8c09b
LS
432 if (type == TYPE_BOOL_OR_STR) {
433 int v = git_parse_maybe_bool(value);
434 if (v < 0)
435 return xstrdup(value);
436 else
437 return xstrdup(v ? "true" : "false");
438 }
63e2a0f8
TB
439 if (type == TYPE_COLOR) {
440 char v[COLOR_MAXLEN];
441 if (git_config_color(v, key, value))
1d28ff4c 442 die(_("cannot parse color '%s'"), value);
63e2a0f8
TB
443
444 /*
445 * The contents of `v` now contain an ANSI escape
446 * sequence, not suitable for including within a
447 * configuration file. Treat the above as a
448 * "sanity-check", and return the given value, which we
449 * know is representable as valid color code.
450 */
451 return xstrdup(value);
452 }
db1696b8 453
50f08db5 454 BUG("cannot normalize type %d", type);
db1696b8
FL
455}
456
9ce03522
JH
457static int get_color_found;
458static const char *get_color_slot;
b408457f 459static const char *get_colorbool_slot;
9ce03522
JH
460static char parsed_color[COLOR_MAXLEN];
461
783a86c1 462static int git_get_color_config(const char *var, const char *value,
5cf88fd8 463 void *cb UNUSED)
9ce03522
JH
464{
465 if (!strcmp(var, get_color_slot)) {
f769982d
JH
466 if (!value)
467 config_error_nonbool(var);
f6c5a296
JK
468 if (color_parse(value, parsed_color) < 0)
469 return -1;
9ce03522
JH
470 get_color_found = 1;
471 }
472 return 0;
473}
474
d0e08d62 475static void get_color(const char *var, const char *def_color)
9ce03522 476{
d0e08d62 477 get_color_slot = var;
9ce03522
JH
478 get_color_found = 0;
479 parsed_color[0] = '\0';
dc8441fd
BW
480 config_with_options(git_get_color_config, NULL,
481 &given_config_source, &config_options);
9ce03522 482
f6c5a296
JK
483 if (!get_color_found && def_color) {
484 if (color_parse(def_color, parsed_color) < 0)
485 die(_("unable to parse default color value"));
486 }
9ce03522
JH
487
488 fputs(parsed_color, stdout);
9ce03522
JH
489}
490
0f6f5a40 491static int get_colorbool_found;
69243c2b 492static int get_diff_color_found;
c659f55b 493static int get_color_ui_found;
ef90d6d4 494static int git_get_colorbool_config(const char *var, const char *value,
5cf88fd8 495 void *data UNUSED)
0f6f5a40 496{
e269eb79
JK
497 if (!strcmp(var, get_colorbool_slot))
498 get_colorbool_found = git_config_colorbool(var, value);
499 else if (!strcmp(var, "diff.color"))
500 get_diff_color_found = git_config_colorbool(var, value);
501 else if (!strcmp(var, "color.ui"))
c659f55b 502 get_color_ui_found = git_config_colorbool(var, value);
0f6f5a40
JH
503 return 0;
504}
505
d0e08d62 506static int get_colorbool(const char *var, int print)
0f6f5a40 507{
d0e08d62 508 get_colorbool_slot = var;
69243c2b
JH
509 get_colorbool_found = -1;
510 get_diff_color_found = -1;
b8612b4d 511 get_color_ui_found = -1;
dc8441fd
BW
512 config_with_options(git_get_colorbool_config, NULL,
513 &given_config_source, &config_options);
0f6f5a40 514
69243c2b 515 if (get_colorbool_found < 0) {
b408457f 516 if (!strcmp(get_colorbool_slot, "color.diff"))
69243c2b
JH
517 get_colorbool_found = get_diff_color_found;
518 if (get_colorbool_found < 0)
c659f55b 519 get_colorbool_found = get_color_ui_found;
69243c2b
JH
520 }
521
b8612b4d
MM
522 if (get_colorbool_found < 0)
523 /* default value if none found in config */
4c7f1819 524 get_colorbool_found = GIT_COLOR_AUTO;
b8612b4d 525
daa0c3d9
JK
526 get_colorbool_found = want_color(get_colorbool_found);
527
0e854a28 528 if (print) {
0f6f5a40
JH
529 printf("%s\n", get_colorbool_found ? "true" : "false");
530 return 0;
0e854a28
FC
531 } else
532 return get_colorbool_found ? 0 : 1;
0f6f5a40
JH
533}
534
6aea9f0f 535static void check_write(void)
1bc88819 536{
638fa623 537 if (!given_config_source.file && !startup_info->have_repository)
1d28ff4c 538 die(_("not in a git directory"));
638fa623 539
3caec73b 540 if (given_config_source.use_stdin)
1d28ff4c 541 die(_("writing to stdin is not supported"));
3caec73b 542
c8985ce0 543 if (given_config_source.blob)
1d28ff4c 544 die(_("writing config blobs is not supported"));
1bc88819
HV
545}
546
d4770964
JH
547struct urlmatch_current_candidate_value {
548 char value_is_null;
549 struct strbuf value;
550};
551
552static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
553{
554 struct string_list *values = cb;
555 struct string_list_item *item = string_list_insert(values, var);
556 struct urlmatch_current_candidate_value *matched = item->util;
557
558 if (!matched) {
559 matched = xmalloc(sizeof(*matched));
560 strbuf_init(&matched->value, 0);
561 item->util = matched;
562 } else {
563 strbuf_reset(&matched->value);
564 }
565
566 if (value) {
567 strbuf_addstr(&matched->value, value);
568 matched->value_is_null = 0;
569 } else {
570 matched->value_is_null = 1;
571 }
572 return 0;
573}
574
d4770964
JH
575static int get_urlmatch(const char *var, const char *url)
576{
27b30be6 577 int ret;
d4770964
JH
578 char *section_tail;
579 struct string_list_item *item;
73ee449b 580 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
d4770964
JH
581 struct string_list values = STRING_LIST_INIT_DUP;
582
583 config.collect_fn = urlmatch_collect_fn;
584 config.cascade_fn = NULL;
585 config.cb = &values;
586
587 if (!url_normalize(url, &config.url))
6667a6ac 588 die("%s", config.url.err);
d4770964 589
88d5a6f6 590 config.section = xstrdup_tolower(var);
d4770964
JH
591 section_tail = strchr(config.section, '.');
592 if (section_tail) {
593 *section_tail = '\0';
594 config.key = section_tail + 1;
595 show_keys = 0;
596 } else {
597 config.key = NULL;
598 show_keys = 1;
599 }
600
dc8441fd
BW
601 config_with_options(urlmatch_config_entry, &config,
602 &given_config_source, &config_options);
d4770964 603
27b30be6
JK
604 ret = !values.nr;
605
d4770964
JH
606 for_each_string_list_item(item, &values) {
607 struct urlmatch_current_candidate_value *matched = item->util;
d4770964
JH
608 struct strbuf buf = STRBUF_INIT;
609
a92330d2 610 format_config(&buf, item->string,
d4770964
JH
611 matched->value_is_null ? NULL : matched->value.buf);
612 fwrite(buf.buf, 1, buf.len, stdout);
d4770964
JH
613 strbuf_release(&buf);
614
615 strbuf_release(&matched->value);
616 }
a41e8e74 617 urlmatch_config_release(&config);
d4770964
JH
618 string_list_clear(&values, 1);
619 free(config.url.url);
620
621 free((void *)config.section);
27b30be6 622 return ret;
d4770964
JH
623}
624
9830534e
MM
625static char *default_user_config(void)
626{
627 struct strbuf buf = STRBUF_INIT;
628 strbuf_addf(&buf,
629 _("# This is Git's per-user configuration file.\n"
7e110524 630 "[user]\n"
9830534e 631 "# Please adapt and uncomment the following lines:\n"
7e110524 632 "# name = %s\n"
9830534e
MM
633 "# email = %s\n"),
634 ident_default_name(),
635 ident_default_email());
636 return strbuf_detach(&buf, NULL);
637}
638
3ba7e6e2 639int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 640{
3ba7e6e2 641 int nongit = !startup_info->have_repository;
4b951b7e 642 char *value;
c90702a1 643 int flags = 0;
7162dff3 644
423ff9be 645 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
dc871831 646
37782920
SB
647 argc = parse_options(argc, argv, prefix, builtin_config_options,
648 builtin_config_usage,
d64ec16c
FC
649 PARSE_OPT_STOP_AT_NON_OPTION);
650
1bc88819 651 if (use_global_config + use_system_config + use_local_config +
58b284a2 652 use_worktree_config +
c8985ce0 653 !!given_config_source.file + !!given_config_source.blob > 1) {
1d28ff4c 654 error(_("only one config file at a time"));
6aaded55 655 usage_builtin_config();
67052c9d
FC
656 }
657
378fe5fc
MT
658 if (nongit) {
659 if (use_local_config)
660 die(_("--local can only be used inside a git repository"));
661 if (given_config_source.blob)
662 die(_("--blob can only be used inside a git repository"));
663 if (use_worktree_config)
664 die(_("--worktree can only be used inside a git repository"));
25cd2919 665
378fe5fc 666 }
17b8a2d6 667
3caec73b
KS
668 if (given_config_source.file &&
669 !strcmp(given_config_source.file, "-")) {
670 given_config_source.file = NULL;
671 given_config_source.use_stdin = 1;
e37efa40 672 given_config_source.scope = CONFIG_SCOPE_COMMAND;
3caec73b
KS
673 }
674
d64ec16c 675 if (use_global_config) {
1e06eb9b 676 char *user_config, *xdg_config;
21cf3227 677
1e06eb9b 678 git_global_config(&user_config, &xdg_config);
e3ebc35b
MM
679 if (!user_config)
680 /*
681 * It is unknown if HOME/.gitconfig exists, so
682 * we do not know if we should write to XDG
683 * location; error out even if XDG_CONFIG_HOME
684 * is set and points at a sane location.
685 */
1d28ff4c 686 die(_("$HOME not set"));
e3ebc35b 687
e37efa40
MR
688 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
689
4698c8fe 690 if (access_or_warn(user_config, R_OK, 0) &&
6c6b08d2 691 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
c8985ce0 692 given_config_source.file = xdg_config;
6c6b08d2
JK
693 free(user_config);
694 } else {
c8985ce0 695 given_config_source.file = user_config;
6c6b08d2
JK
696 free(xdg_config);
697 }
d64ec16c 698 }
e37efa40 699 else if (use_system_config) {
c62a999c 700 given_config_source.file = git_system_config();
e37efa40
MR
701 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
702 } else if (use_local_config) {
c8985ce0 703 given_config_source.file = git_pathdup("config");
e37efa40
MR
704 given_config_source.scope = CONFIG_SCOPE_LOCAL;
705 } else if (use_worktree_config) {
03f2465b 706 struct worktree **worktrees = get_worktrees();
58b284a2
NTND
707 if (repository_format_worktree_config)
708 given_config_source.file = git_pathdup("config.worktree");
709 else if (worktrees[0] && worktrees[1])
710 die(_("--worktree cannot be used with multiple "
711 "working trees unless the config\n"
712 "extension worktreeConfig is enabled. "
713 "Please read \"CONFIGURATION FILE\"\n"
714 "section in \"git help worktree\" for details"));
715 else
716 given_config_source.file = git_pathdup("config");
e37efa40 717 given_config_source.scope = CONFIG_SCOPE_LOCAL;
58b284a2
NTND
718 free_worktrees(worktrees);
719 } else if (given_config_source.file) {
c8985ce0
KS
720 if (!is_absolute_path(given_config_source.file) && prefix)
721 given_config_source.file =
e4da43b1 722 prefix_filename(prefix, given_config_source.file);
e37efa40
MR
723 given_config_source.scope = CONFIG_SCOPE_COMMAND;
724 } else if (given_config_source.blob) {
725 given_config_source.scope = CONFIG_SCOPE_COMMAND;
d64ec16c
FC
726 }
727
e37efa40 728
c48f4b37
NTND
729 if (respect_includes_opt == -1)
730 config_options.respect_includes = !given_config_source.file;
731 else
732 config_options.respect_includes = respect_includes_opt;
dc8441fd
BW
733 if (!nongit) {
734 config_options.commondir = get_git_common_dir();
735 config_options.git_dir = get_git_dir();
736 }
9b25a0b5 737
329e6ec3 738 if (end_nul) {
d64ec16c
FC
739 term = '\0';
740 delim = '\n';
741 key_delim = '\n';
742 }
743
0a8950be 744 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
1d28ff4c 745 error(_("--get-color and variable type are incoherent"));
6aaded55 746 usage_builtin_config();
c2387358
FC
747 }
748
d64ec16c 749 if (HAS_MULTI_BITS(actions)) {
1d28ff4c 750 error(_("only one action at a time"));
6aaded55 751 usage_builtin_config();
d64ec16c
FC
752 }
753 if (actions == 0)
754 switch (argc) {
755 case 1: actions = ACTION_GET; break;
756 case 2: actions = ACTION_SET; break;
757 case 3: actions = ACTION_SET_ALL; break;
758 default:
6aaded55 759 usage_builtin_config();
db1696b8 760 }
578625fa
SG
761 if (omit_values &&
762 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
1d28ff4c 763 error(_("--name-only is only applicable to --list or --get-regexp"));
6aaded55 764 usage_builtin_config();
578625fa 765 }
70bd879a
LS
766
767 if (show_origin && !(actions &
768 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
1d28ff4c
NTND
769 error(_("--show-origin is only applicable to --get, --get-all, "
770 "--get-regexp, and --list"));
6aaded55 771 usage_builtin_config();
70bd879a
LS
772 }
773
eeaa24b9 774 if (default_value && !(actions & ACTION_GET)) {
1d28ff4c 775 error(_("--default is only applicable to --get"));
6aaded55 776 usage_builtin_config();
eeaa24b9
TB
777 }
778
fda43942
DS
779 /* check usage of --fixed-value */
780 if (fixed_value) {
781 int allowed_usage = 0;
782
783 switch (actions) {
784 /* git config --get <name> <value-pattern> */
785 case ACTION_GET:
786 /* git config --get-all <name> <value-pattern> */
787 case ACTION_GET_ALL:
788 /* git config --get-regexp <name-pattern> <value-pattern> */
789 case ACTION_GET_REGEXP:
790 /* git config --unset <name> <value-pattern> */
791 case ACTION_UNSET:
792 /* git config --unset-all <name> <value-pattern> */
793 case ACTION_UNSET_ALL:
794 allowed_usage = argc > 1 && !!argv[1];
795 break;
796
797 /* git config <name> <value> <value-pattern> */
798 case ACTION_SET_ALL:
799 /* git config --replace-all <name> <value> <value-pattern> */
800 case ACTION_REPLACE_ALL:
801 allowed_usage = argc > 2 && !!argv[2];
802 break;
803
804 /* other options don't allow --fixed-value */
805 }
806
807 if (!allowed_usage) {
808 error(_("--fixed-value only applies with 'value-pattern'"));
809 usage_builtin_config();
810 }
c90702a1
DS
811
812 flags |= CONFIG_FLAGS_FIXED_VALUE;
fda43942
DS
813 }
814
32888b8f 815 if (actions & PAGING_ACTIONS)
c0e9f5be 816 setup_auto_pager("config", 1);
32888b8f 817
d64ec16c 818 if (actions == ACTION_LIST) {
225a9caf 819 check_argc(argc, 0, 0);
dc8441fd
BW
820 if (config_with_options(show_all_config, NULL,
821 &given_config_source,
822 &config_options) < 0) {
c8985ce0 823 if (given_config_source.file)
1d28ff4c 824 die_errno(_("unable to read config file '%s'"),
c8985ce0 825 given_config_source.file);
d64ec16c 826 else
1d28ff4c 827 die(_("error processing config file(s)"));
db1696b8 828 }
1b1e59c5 829 }
d64ec16c 830 else if (actions == ACTION_EDIT) {
3696a7c2
MH
831 char *config_file;
832
225a9caf 833 check_argc(argc, 0, 0);
c8985ce0 834 if (!given_config_source.file && nongit)
1d28ff4c 835 die(_("not in a git directory"));
3caec73b 836 if (given_config_source.use_stdin)
1d28ff4c 837 die(_("editing stdin is not supported"));
c8985ce0 838 if (given_config_source.blob)
1d28ff4c 839 die(_("editing blobs is not supported"));
d64ec16c 840 git_config(git_default_config, NULL);
d9c69644
JK
841 config_file = given_config_source.file ?
842 xstrdup(given_config_source.file) :
843 git_pathdup("config");
9830534e
MM
844 if (use_global_config) {
845 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
aabbd3f3 846 if (fd >= 0) {
9830534e
MM
847 char *content = default_user_config();
848 write_str_in_full(fd, content);
849 free(content);
850 close(fd);
851 }
852 else if (errno != EEXIST)
853 die_errno(_("cannot create configuration file %s"), config_file);
854 }
855 launch_editor(config_file, NULL, NULL);
3696a7c2 856 free(config_file);
d64ec16c
FC
857 }
858 else if (actions == ACTION_SET) {
5a2df368 859 int ret;
6aea9f0f 860 check_write();
d64ec16c
FC
861 check_argc(argc, 2, 2);
862 value = normalize_value(argv[0], argv[1]);
0e5bba53 863 UNLEAK(value);
30598ad0 864 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
5a2df368 865 if (ret == CONFIG_NOTHING_SET)
ccf63801
VA
866 error(_("cannot overwrite multiple values with a single value\n"
867 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
5a2df368 868 return ret;
d64ec16c
FC
869 }
870 else if (actions == ACTION_SET_ALL) {
6aea9f0f 871 check_write();
d64ec16c
FC
872 check_argc(argc, 2, 3);
873 value = normalize_value(argv[0], argv[1]);
0e5bba53 874 UNLEAK(value);
30598ad0 875 return git_config_set_multivar_in_file_gently(given_config_source.file,
c90702a1
DS
876 argv[0], value, argv[2],
877 flags);
d64ec16c
FC
878 }
879 else if (actions == ACTION_ADD) {
6aea9f0f 880 check_write();
d64ec16c
FC
881 check_argc(argc, 2, 2);
882 value = normalize_value(argv[0], argv[1]);
0e5bba53 883 UNLEAK(value);
30598ad0
PS
884 return git_config_set_multivar_in_file_gently(given_config_source.file,
885 argv[0], value,
c90702a1
DS
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]);
0e5bba53 893 UNLEAK(value);
30598ad0 894 return git_config_set_multivar_in_file_gently(given_config_source.file,
504ee129 895 argv[0], value, argv[2],
c90702a1 896 flags | CONFIG_FLAGS_MULTI_REPLACE);
d64ec16c
FC
897 }
898 else if (actions == ACTION_GET) {
899 check_argc(argc, 1, 2);
3f1bae1d 900 return get_value(argv[0], argv[1], flags);
d64ec16c
FC
901 }
902 else if (actions == ACTION_GET_ALL) {
903 do_all = 1;
904 check_argc(argc, 1, 2);
3f1bae1d 905 return get_value(argv[0], argv[1], flags);
d64ec16c
FC
906 }
907 else if (actions == ACTION_GET_REGEXP) {
908 show_keys = 1;
909 use_key_regexp = 1;
910 do_all = 1;
911 check_argc(argc, 1, 2);
3f1bae1d 912 return get_value(argv[0], argv[1], flags);
d64ec16c 913 }
d4770964
JH
914 else if (actions == ACTION_GET_URLMATCH) {
915 check_argc(argc, 2, 2);
916 return get_urlmatch(argv[0], argv[1]);
917 }
d64ec16c 918 else if (actions == ACTION_UNSET) {
6aea9f0f 919 check_write();
d64ec16c
FC
920 check_argc(argc, 1, 2);
921 if (argc == 2)
30598ad0 922 return git_config_set_multivar_in_file_gently(given_config_source.file,
c90702a1
DS
923 argv[0], NULL, argv[1],
924 flags);
d64ec16c 925 else
30598ad0
PS
926 return git_config_set_in_file_gently(given_config_source.file,
927 argv[0], NULL);
d64ec16c
FC
928 }
929 else if (actions == ACTION_UNSET_ALL) {
6aea9f0f 930 check_write();
d64ec16c 931 check_argc(argc, 1, 2);
30598ad0 932 return git_config_set_multivar_in_file_gently(given_config_source.file,
504ee129 933 argv[0], NULL, argv[1],
c90702a1 934 flags | CONFIG_FLAGS_MULTI_REPLACE);
d64ec16c
FC
935 }
936 else if (actions == ACTION_RENAME_SECTION) {
937 int ret;
6aea9f0f 938 check_write();
d64ec16c 939 check_argc(argc, 2, 2);
c8985ce0 940 ret = git_config_rename_section_in_file(given_config_source.file,
270a3443 941 argv[0], argv[1]);
d64ec16c
FC
942 if (ret < 0)
943 return ret;
944 if (ret == 0)
1d28ff4c 945 die(_("no such section: %s"), argv[0]);
d64ec16c
FC
946 }
947 else if (actions == ACTION_REMOVE_SECTION) {
948 int ret;
6aea9f0f 949 check_write();
d64ec16c 950 check_argc(argc, 1, 1);
c8985ce0 951 ret = git_config_rename_section_in_file(given_config_source.file,
270a3443 952 argv[0], NULL);
d64ec16c
FC
953 if (ret < 0)
954 return ret;
955 if (ret == 0)
1d28ff4c 956 die(_("no such section: %s"), argv[0]);
d64ec16c
FC
957 }
958 else if (actions == ACTION_GET_COLOR) {
d0e08d62
JK
959 check_argc(argc, 1, 2);
960 get_color(argv[0], argv[1]);
d64ec16c
FC
961 }
962 else if (actions == ACTION_GET_COLORBOOL) {
d0e08d62
JK
963 check_argc(argc, 1, 2);
964 if (argc == 2)
965 color_stdout_is_tty = git_config_bool("command line", argv[1]);
966 return get_colorbool(argv[0], argc == 2);
d64ec16c
FC
967 }
968
1b1e59c5
JS
969 return 0;
970}