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