rerere: mention caveat about unmatched conflict markers
[git.git] / builtin / config.c
blobb29d26dede79b9a5c46bdc4f951b22e7b88310b3
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "color.h"
5 #include "parse-options.h"
6 #include "urlmatch.h"
7 #include "quote.h"
9 static const char *const builtin_config_usage[] = {
10 N_("git config [<options>]"),
11 NULL
14 static char *key;
15 static regex_t *key_regexp;
16 static regex_t *regexp;
17 static int show_keys;
18 static int omit_values;
19 static int use_key_regexp;
20 static int do_all;
21 static int do_not_match;
22 static char delim = '=';
23 static char key_delim = ' ';
24 static char term = '\n';
26 static int use_global_config, use_system_config, use_local_config;
27 static struct git_config_source given_config_source;
28 static int actions, type;
29 static char *default_value;
30 static int end_null;
31 static int respect_includes_opt = -1;
32 static struct config_options config_options;
33 static int show_origin;
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)
50 #define ACTION_GET_URLMATCH (1<<15)
53 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
54 * one line of output and which should therefore be paged.
56 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
57 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
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
64 #define TYPE_COLOR 6
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) }
70 static struct option builtin_config_options[];
72 static int option_parse_type(const struct option *opt, const char *arg,
73 int unset)
75 int new_type, *to_type;
77 if (unset) {
78 *((int *) opt->value) = 0;
79 return 0;
83 * To support '--<type>' style flags, begin with new_type equal to
84 * opt->defval.
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;
98 else if (!strcmp(arg, "color"))
99 new_type = TYPE_COLOR;
100 else
101 die(_("unrecognized --type argument, %s"), arg);
104 to_type = opt->value;
105 if (*to_type && *to_type != new_type) {
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'.
113 error("only one type at a time.");
114 usage_with_options(builtin_config_usage,
115 builtin_config_options);
117 *to_type = new_type;
119 return 0;
122 static struct option builtin_config_options[] = {
123 OPT_GROUP(N_("Config file location")),
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")),
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")),
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),
133 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
134 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
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),
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),
141 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
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),
144 OPT_GROUP(N_("Type")),
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),
151 OPT_GROUP(N_("Other")),
152 OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
153 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
154 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
155 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
156 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
157 OPT_END(),
160 static 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);
167 static void show_config_origin(struct strbuf *buf)
169 const char term = end_null ? '\0' : '\t';
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);
180 static int show_all_config(const char *key_, const char *value_, void *cb)
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);
189 if (!omit_values && value_)
190 printf("%s%c%s%c", key_, delim, value_, term);
191 else
192 printf("%s%c", key_, term);
193 return 0;
196 struct strbuf_list {
197 struct strbuf *items;
198 int nr;
199 int alloc;
202 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
204 if (show_origin)
205 show_config_origin(buf);
206 if (show_keys)
207 strbuf_addstr(buf, key_);
208 if (!omit_values) {
209 if (show_keys)
210 strbuf_addch(buf, key_delim);
212 if (type == TYPE_INT)
213 strbuf_addf(buf, "%"PRId64,
214 git_config_int64(key_, value_ ? value_ : ""));
215 else if (type == TYPE_BOOL)
216 strbuf_addstr(buf, git_config_bool(key_, value_) ?
217 "true" : "false");
218 else if (type == TYPE_BOOL_OR_INT) {
219 int is_bool, v;
220 v = git_config_bool_or_int(key_, value_, &is_bool);
221 if (is_bool)
222 strbuf_addstr(buf, v ? "true" : "false");
223 else
224 strbuf_addf(buf, "%d", v);
225 } else if (type == TYPE_PATH) {
226 const char *v;
227 if (git_config_pathname(&v, key_, value_) < 0)
228 return -1;
229 strbuf_addstr(buf, v);
230 free((char *)v);
231 } else if (type == TYPE_EXPIRY_DATE) {
232 timestamp_t t;
233 if (git_config_expiry_date(&t, key_, value_) < 0)
234 return -1;
235 strbuf_addf(buf, "%"PRItime, t);
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);
241 } else if (value_) {
242 strbuf_addstr(buf, value_);
243 } else {
244 /* Just show the key name; back out delimiter */
245 if (show_keys)
246 strbuf_setlen(buf, buf->len - 1);
249 strbuf_addch(buf, term);
250 return 0;
253 static int collect_config(const char *key_, const char *value_, void *cb)
255 struct strbuf_list *values = cb;
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;
265 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
266 strbuf_init(&values->items[values->nr], 0);
268 return format_config(&values->items[values->nr++], key_, value_);
271 static int get_value(const char *key_, const char *regex_)
273 int ret = CONFIG_GENERIC_ERROR;
274 struct strbuf_list values = {NULL};
275 int i;
277 if (use_key_regexp) {
278 char *tl;
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.
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);
294 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
295 if (regcomp(key_regexp, key, REG_EXTENDED)) {
296 error("invalid key pattern: %s", key_);
297 FREE_AND_NULL(key_regexp);
298 ret = CONFIG_INVALID_PATTERN;
299 goto free_strings;
301 } else {
302 if (git_config_parse_key(key_, &key, NULL)) {
303 ret = CONFIG_INVALID_KEY;
304 goto free_strings;
308 if (regex_) {
309 if (regex_[0] == '!') {
310 do_not_match = 1;
311 regex_++;
314 regexp = (regex_t*)xmalloc(sizeof(regex_t));
315 if (regcomp(regexp, regex_, REG_EXTENDED)) {
316 error("invalid pattern: %s", regex_);
317 FREE_AND_NULL(regexp);
318 ret = CONFIG_INVALID_PATTERN;
319 goto free_strings;
323 config_with_options(collect_config, &values,
324 &given_config_source, &config_options);
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);
336 ret = !values.nr;
338 for (i = 0; i < values.nr; i++) {
339 struct strbuf *buf = values.items + i;
340 if (do_all || i == values.nr - 1)
341 fwrite(buf->buf, 1, buf->len, stdout);
342 strbuf_release(buf);
344 free(values.items);
346 free_strings:
347 free(key);
348 if (key_regexp) {
349 regfree(key_regexp);
350 free(key_regexp);
352 if (regexp) {
353 regfree(regexp);
354 free(regexp);
357 return ret;
360 static char *normalize_value(const char *key, const char *value)
362 if (!value)
363 return NULL;
365 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
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.
371 * Also don't do normalization for expiry dates.
373 return xstrdup(value);
374 if (type == TYPE_INT)
375 return xstrfmt("%"PRId64, git_config_int64(key, value));
376 if (type == TYPE_BOOL)
377 return xstrdup(git_config_bool(key, value) ? "true" : "false");
378 if (type == TYPE_BOOL_OR_INT) {
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");
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);
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.
398 return xstrdup(value);
401 BUG("cannot normalize type %d", type);
404 static int get_color_found;
405 static const char *get_color_slot;
406 static const char *get_colorbool_slot;
407 static char parsed_color[COLOR_MAXLEN];
409 static int git_get_color_config(const char *var, const char *value, void *cb)
411 if (!strcmp(var, get_color_slot)) {
412 if (!value)
413 config_error_nonbool(var);
414 if (color_parse(value, parsed_color) < 0)
415 return -1;
416 get_color_found = 1;
418 return 0;
421 static void get_color(const char *var, const char *def_color)
423 get_color_slot = var;
424 get_color_found = 0;
425 parsed_color[0] = '\0';
426 config_with_options(git_get_color_config, NULL,
427 &given_config_source, &config_options);
429 if (!get_color_found && def_color) {
430 if (color_parse(def_color, parsed_color) < 0)
431 die(_("unable to parse default color value"));
434 fputs(parsed_color, stdout);
437 static int get_colorbool_found;
438 static int get_diff_color_found;
439 static int get_color_ui_found;
440 static int git_get_colorbool_config(const char *var, const char *value,
441 void *cb)
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"))
448 get_color_ui_found = git_config_colorbool(var, value);
449 return 0;
452 static int get_colorbool(const char *var, int print)
454 get_colorbool_slot = var;
455 get_colorbool_found = -1;
456 get_diff_color_found = -1;
457 get_color_ui_found = -1;
458 config_with_options(git_get_colorbool_config, NULL,
459 &given_config_source, &config_options);
461 if (get_colorbool_found < 0) {
462 if (!strcmp(get_colorbool_slot, "color.diff"))
463 get_colorbool_found = get_diff_color_found;
464 if (get_colorbool_found < 0)
465 get_colorbool_found = get_color_ui_found;
468 if (get_colorbool_found < 0)
469 /* default value if none found in config */
470 get_colorbool_found = GIT_COLOR_AUTO;
472 get_colorbool_found = want_color(get_colorbool_found);
474 if (print) {
475 printf("%s\n", get_colorbool_found ? "true" : "false");
476 return 0;
477 } else
478 return get_colorbool_found ? 0 : 1;
481 static void check_write(void)
483 if (!given_config_source.file && !startup_info->have_repository)
484 die("not in a git directory");
486 if (given_config_source.use_stdin)
487 die("writing to stdin is not supported");
489 if (given_config_source.blob)
490 die("writing config blobs is not supported");
493 struct urlmatch_current_candidate_value {
494 char value_is_null;
495 struct strbuf value;
498 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
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;
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);
512 if (value) {
513 strbuf_addstr(&matched->value, value);
514 matched->value_is_null = 0;
515 } else {
516 matched->value_is_null = 1;
518 return 0;
521 static int get_urlmatch(const char *var, const char *url)
523 int ret;
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;
529 config.collect_fn = urlmatch_collect_fn;
530 config.cascade_fn = NULL;
531 config.cb = &values;
533 if (!url_normalize(url, &config.url))
534 die("%s", config.url.err);
536 config.section = xstrdup_tolower(var);
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;
547 config_with_options(urlmatch_config_entry, &config,
548 &given_config_source, &config_options);
550 ret = !values.nr;
552 for_each_string_list_item(item, &values) {
553 struct urlmatch_current_candidate_value *matched = item->util;
554 struct strbuf buf = STRBUF_INIT;
556 format_config(&buf, item->string,
557 matched->value_is_null ? NULL : matched->value.buf);
558 fwrite(buf.buf, 1, buf.len, stdout);
559 strbuf_release(&buf);
561 strbuf_release(&matched->value);
563 string_list_clear(&config.vars, 1);
564 string_list_clear(&values, 1);
565 free(config.url.url);
567 free((void *)config.section);
568 return ret;
571 static char *default_user_config(void)
573 struct strbuf buf = STRBUF_INIT;
574 strbuf_addf(&buf,
575 _("# This is Git's per-user configuration file.\n"
576 "[user]\n"
577 "# Please adapt and uncomment the following lines:\n"
578 "# name = %s\n"
579 "# email = %s\n"),
580 ident_default_name(),
581 ident_default_email());
582 return strbuf_detach(&buf, NULL);
585 int cmd_config(int argc, const char **argv, const char *prefix)
587 int nongit = !startup_info->have_repository;
588 char *value;
590 given_config_source.file = getenv(CONFIG_ENVIRONMENT);
592 argc = parse_options(argc, argv, prefix, builtin_config_options,
593 builtin_config_usage,
594 PARSE_OPT_STOP_AT_NON_OPTION);
596 if (use_global_config + use_system_config + use_local_config +
597 !!given_config_source.file + !!given_config_source.blob > 1) {
598 error("only one config file at a time.");
599 usage_with_options(builtin_config_usage, builtin_config_options);
602 if (use_local_config && nongit)
603 die(_("--local can only be used inside a git repository"));
605 if (given_config_source.blob && nongit)
606 die(_("--blob can only be used inside a git repository"));
608 if (given_config_source.file &&
609 !strcmp(given_config_source.file, "-")) {
610 given_config_source.file = NULL;
611 given_config_source.use_stdin = 1;
614 if (use_global_config) {
615 char *user_config = expand_user_path("~/.gitconfig", 0);
616 char *xdg_config = xdg_config_home("config");
618 if (!user_config)
620 * It is unknown if HOME/.gitconfig exists, so
621 * we do not know if we should write to XDG
622 * location; error out even if XDG_CONFIG_HOME
623 * is set and points at a sane location.
625 die("$HOME not set");
627 if (access_or_warn(user_config, R_OK, 0) &&
628 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
629 given_config_source.file = xdg_config;
630 free(user_config);
631 } else {
632 given_config_source.file = user_config;
633 free(xdg_config);
636 else if (use_system_config)
637 given_config_source.file = git_etc_gitconfig();
638 else if (use_local_config)
639 given_config_source.file = git_pathdup("config");
640 else if (given_config_source.file) {
641 if (!is_absolute_path(given_config_source.file) && prefix)
642 given_config_source.file =
643 prefix_filename(prefix, given_config_source.file);
646 if (respect_includes_opt == -1)
647 config_options.respect_includes = !given_config_source.file;
648 else
649 config_options.respect_includes = respect_includes_opt;
650 if (!nongit) {
651 config_options.commondir = get_git_common_dir();
652 config_options.git_dir = get_git_dir();
655 if (end_null) {
656 term = '\0';
657 delim = '\n';
658 key_delim = '\n';
661 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
662 error("--get-color and variable type are incoherent");
663 usage_with_options(builtin_config_usage, builtin_config_options);
666 if (HAS_MULTI_BITS(actions)) {
667 error("only one action at a time.");
668 usage_with_options(builtin_config_usage, builtin_config_options);
670 if (actions == 0)
671 switch (argc) {
672 case 1: actions = ACTION_GET; break;
673 case 2: actions = ACTION_SET; break;
674 case 3: actions = ACTION_SET_ALL; break;
675 default:
676 usage_with_options(builtin_config_usage, builtin_config_options);
678 if (omit_values &&
679 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
680 error("--name-only is only applicable to --list or --get-regexp");
681 usage_with_options(builtin_config_usage, builtin_config_options);
684 if (show_origin && !(actions &
685 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
686 error("--show-origin is only applicable to --get, --get-all, "
687 "--get-regexp, and --list.");
688 usage_with_options(builtin_config_usage, builtin_config_options);
691 if (default_value && !(actions & ACTION_GET)) {
692 error("--default is only applicable to --get");
693 usage_with_options(builtin_config_usage,
694 builtin_config_options);
697 if (actions & PAGING_ACTIONS)
698 setup_auto_pager("config", 1);
700 if (actions == ACTION_LIST) {
701 check_argc(argc, 0, 0);
702 if (config_with_options(show_all_config, NULL,
703 &given_config_source,
704 &config_options) < 0) {
705 if (given_config_source.file)
706 die_errno("unable to read config file '%s'",
707 given_config_source.file);
708 else
709 die("error processing config file(s)");
712 else if (actions == ACTION_EDIT) {
713 char *config_file;
715 check_argc(argc, 0, 0);
716 if (!given_config_source.file && nongit)
717 die("not in a git directory");
718 if (given_config_source.use_stdin)
719 die("editing stdin is not supported");
720 if (given_config_source.blob)
721 die("editing blobs is not supported");
722 git_config(git_default_config, NULL);
723 config_file = given_config_source.file ?
724 xstrdup(given_config_source.file) :
725 git_pathdup("config");
726 if (use_global_config) {
727 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
728 if (fd >= 0) {
729 char *content = default_user_config();
730 write_str_in_full(fd, content);
731 free(content);
732 close(fd);
734 else if (errno != EEXIST)
735 die_errno(_("cannot create configuration file %s"), config_file);
737 launch_editor(config_file, NULL, NULL);
738 free(config_file);
740 else if (actions == ACTION_SET) {
741 int ret;
742 check_write();
743 check_argc(argc, 2, 2);
744 value = normalize_value(argv[0], argv[1]);
745 UNLEAK(value);
746 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
747 if (ret == CONFIG_NOTHING_SET)
748 error(_("cannot overwrite multiple values with a single value\n"
749 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
750 return ret;
752 else if (actions == ACTION_SET_ALL) {
753 check_write();
754 check_argc(argc, 2, 3);
755 value = normalize_value(argv[0], argv[1]);
756 UNLEAK(value);
757 return git_config_set_multivar_in_file_gently(given_config_source.file,
758 argv[0], value, argv[2], 0);
760 else if (actions == ACTION_ADD) {
761 check_write();
762 check_argc(argc, 2, 2);
763 value = normalize_value(argv[0], argv[1]);
764 UNLEAK(value);
765 return git_config_set_multivar_in_file_gently(given_config_source.file,
766 argv[0], value,
767 CONFIG_REGEX_NONE, 0);
769 else if (actions == ACTION_REPLACE_ALL) {
770 check_write();
771 check_argc(argc, 2, 3);
772 value = normalize_value(argv[0], argv[1]);
773 UNLEAK(value);
774 return git_config_set_multivar_in_file_gently(given_config_source.file,
775 argv[0], value, argv[2], 1);
777 else if (actions == ACTION_GET) {
778 check_argc(argc, 1, 2);
779 return get_value(argv[0], argv[1]);
781 else if (actions == ACTION_GET_ALL) {
782 do_all = 1;
783 check_argc(argc, 1, 2);
784 return get_value(argv[0], argv[1]);
786 else if (actions == ACTION_GET_REGEXP) {
787 show_keys = 1;
788 use_key_regexp = 1;
789 do_all = 1;
790 check_argc(argc, 1, 2);
791 return get_value(argv[0], argv[1]);
793 else if (actions == ACTION_GET_URLMATCH) {
794 check_argc(argc, 2, 2);
795 return get_urlmatch(argv[0], argv[1]);
797 else if (actions == ACTION_UNSET) {
798 check_write();
799 check_argc(argc, 1, 2);
800 if (argc == 2)
801 return git_config_set_multivar_in_file_gently(given_config_source.file,
802 argv[0], NULL, argv[1], 0);
803 else
804 return git_config_set_in_file_gently(given_config_source.file,
805 argv[0], NULL);
807 else if (actions == ACTION_UNSET_ALL) {
808 check_write();
809 check_argc(argc, 1, 2);
810 return git_config_set_multivar_in_file_gently(given_config_source.file,
811 argv[0], NULL, argv[1], 1);
813 else if (actions == ACTION_RENAME_SECTION) {
814 int ret;
815 check_write();
816 check_argc(argc, 2, 2);
817 ret = git_config_rename_section_in_file(given_config_source.file,
818 argv[0], argv[1]);
819 if (ret < 0)
820 return ret;
821 if (ret == 0)
822 die("No such section!");
824 else if (actions == ACTION_REMOVE_SECTION) {
825 int ret;
826 check_write();
827 check_argc(argc, 1, 1);
828 ret = git_config_rename_section_in_file(given_config_source.file,
829 argv[0], NULL);
830 if (ret < 0)
831 return ret;
832 if (ret == 0)
833 die("No such section!");
835 else if (actions == ACTION_GET_COLOR) {
836 check_argc(argc, 1, 2);
837 get_color(argv[0], argv[1]);
839 else if (actions == ACTION_GET_COLORBOOL) {
840 check_argc(argc, 1, 2);
841 if (argc == 2)
842 color_stdout_is_tty = git_config_bool("command line", argv[1]);
843 return get_colorbool(argv[0], argc == 2);
846 return 0;