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