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