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