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