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