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