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