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