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