Merge branch 'master' of github.com:git-l10n/git-po
[git/raj.git] / builtin / config.c
blobee4aef6a3555765a9ed74263d68c765ba8431f85
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"
8 #include "worktree.h"
10 static const char *const builtin_config_usage[] = {
11 N_("git config [<options>]"),
12 NULL
15 static char *key;
16 static regex_t *key_regexp;
17 static regex_t *regexp;
18 static int show_keys;
19 static int omit_values;
20 static int use_key_regexp;
21 static int do_all;
22 static int do_not_match;
23 static char delim = '=';
24 static char key_delim = ' ';
25 static char term = '\n';
27 static int use_global_config, use_system_config, use_local_config;
28 static int use_worktree_config;
29 static struct git_config_source given_config_source;
30 static int actions, type;
31 static char *default_value;
32 static int end_nul;
33 static int respect_includes_opt = -1;
34 static struct config_options config_options;
35 static int show_origin;
36 static int show_scope;
38 #define ACTION_GET (1<<0)
39 #define ACTION_GET_ALL (1<<1)
40 #define ACTION_GET_REGEXP (1<<2)
41 #define ACTION_REPLACE_ALL (1<<3)
42 #define ACTION_ADD (1<<4)
43 #define ACTION_UNSET (1<<5)
44 #define ACTION_UNSET_ALL (1<<6)
45 #define ACTION_RENAME_SECTION (1<<7)
46 #define ACTION_REMOVE_SECTION (1<<8)
47 #define ACTION_LIST (1<<9)
48 #define ACTION_EDIT (1<<10)
49 #define ACTION_SET (1<<11)
50 #define ACTION_SET_ALL (1<<12)
51 #define ACTION_GET_COLOR (1<<13)
52 #define ACTION_GET_COLORBOOL (1<<14)
53 #define ACTION_GET_URLMATCH (1<<15)
56 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
57 * one line of output and which should therefore be paged.
59 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
60 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
62 #define TYPE_BOOL 1
63 #define TYPE_INT 2
64 #define TYPE_BOOL_OR_INT 3
65 #define TYPE_PATH 4
66 #define TYPE_EXPIRY_DATE 5
67 #define TYPE_COLOR 6
69 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
70 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
71 PARSE_OPT_NONEG, option_parse_type, (i) }
73 static NORETURN void usage_builtin_config(void);
75 static int option_parse_type(const struct option *opt, const char *arg,
76 int unset)
78 int new_type, *to_type;
80 if (unset) {
81 *((int *) opt->value) = 0;
82 return 0;
86 * To support '--<type>' style flags, begin with new_type equal to
87 * opt->defval.
89 new_type = opt->defval;
90 if (!new_type) {
91 if (!strcmp(arg, "bool"))
92 new_type = TYPE_BOOL;
93 else if (!strcmp(arg, "int"))
94 new_type = TYPE_INT;
95 else if (!strcmp(arg, "bool-or-int"))
96 new_type = TYPE_BOOL_OR_INT;
97 else if (!strcmp(arg, "path"))
98 new_type = TYPE_PATH;
99 else if (!strcmp(arg, "expiry-date"))
100 new_type = TYPE_EXPIRY_DATE;
101 else if (!strcmp(arg, "color"))
102 new_type = TYPE_COLOR;
103 else
104 die(_("unrecognized --type argument, %s"), arg);
107 to_type = opt->value;
108 if (*to_type && *to_type != new_type) {
110 * Complain when there is a new type not equal to the old type.
111 * This allows for combinations like '--int --type=int' and
112 * '--type=int --type=int', but disallows ones like '--type=bool
113 * --int' and '--type=bool
114 * --type=int'.
116 error(_("only one type at a time"));
117 usage_builtin_config();
119 *to_type = new_type;
121 return 0;
124 static struct option builtin_config_options[] = {
125 OPT_GROUP(N_("Config file location")),
126 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
127 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
128 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
129 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
130 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
131 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
132 OPT_GROUP(N_("Action")),
133 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
134 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
135 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
136 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
137 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
138 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
139 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
140 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
141 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
142 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
143 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
144 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
145 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
146 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
147 OPT_GROUP(N_("Type")),
148 OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type),
149 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
150 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
151 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
152 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
153 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
154 OPT_GROUP(N_("Other")),
155 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
156 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
157 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
158 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
159 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
160 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
161 OPT_END(),
164 static NORETURN void usage_builtin_config(void)
166 usage_with_options(builtin_config_usage, builtin_config_options);
169 static void check_argc(int argc, int min, int max)
171 if (argc >= min && argc <= max)
172 return;
173 if (min == max)
174 error(_("wrong number of arguments, should be %d"), min);
175 else
176 error(_("wrong number of arguments, should be from %d to %d"),
177 min, max);
178 usage_builtin_config();
181 static void show_config_origin(struct strbuf *buf)
183 const char term = end_nul ? '\0' : '\t';
185 strbuf_addstr(buf, current_config_origin_type());
186 strbuf_addch(buf, ':');
187 if (end_nul)
188 strbuf_addstr(buf, current_config_name());
189 else
190 quote_c_style(current_config_name(), buf, NULL, 0);
191 strbuf_addch(buf, term);
194 static void show_config_scope(struct strbuf *buf)
196 const char term = end_nul ? '\0' : '\t';
197 const char *scope = config_scope_name(current_config_scope());
199 strbuf_addstr(buf, N_(scope));
200 strbuf_addch(buf, term);
203 static int show_all_config(const char *key_, const char *value_, void *cb)
205 if (show_origin || show_scope) {
206 struct strbuf buf = STRBUF_INIT;
207 if (show_scope)
208 show_config_scope(&buf);
209 if (show_origin)
210 show_config_origin(&buf);
211 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
212 fwrite(buf.buf, 1, buf.len, stdout);
213 strbuf_release(&buf);
215 if (!omit_values && value_)
216 printf("%s%c%s%c", key_, delim, value_, term);
217 else
218 printf("%s%c", key_, term);
219 return 0;
222 struct strbuf_list {
223 struct strbuf *items;
224 int nr;
225 int alloc;
228 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
230 if (show_scope)
231 show_config_scope(buf);
232 if (show_origin)
233 show_config_origin(buf);
234 if (show_keys)
235 strbuf_addstr(buf, key_);
236 if (!omit_values) {
237 if (show_keys)
238 strbuf_addch(buf, key_delim);
240 if (type == TYPE_INT)
241 strbuf_addf(buf, "%"PRId64,
242 git_config_int64(key_, value_ ? value_ : ""));
243 else if (type == TYPE_BOOL)
244 strbuf_addstr(buf, git_config_bool(key_, value_) ?
245 "true" : "false");
246 else if (type == TYPE_BOOL_OR_INT) {
247 int is_bool, v;
248 v = git_config_bool_or_int(key_, value_, &is_bool);
249 if (is_bool)
250 strbuf_addstr(buf, v ? "true" : "false");
251 else
252 strbuf_addf(buf, "%d", v);
253 } else if (type == TYPE_PATH) {
254 const char *v;
255 if (git_config_pathname(&v, key_, value_) < 0)
256 return -1;
257 strbuf_addstr(buf, v);
258 free((char *)v);
259 } else if (type == TYPE_EXPIRY_DATE) {
260 timestamp_t t;
261 if (git_config_expiry_date(&t, key_, value_) < 0)
262 return -1;
263 strbuf_addf(buf, "%"PRItime, t);
264 } else if (type == TYPE_COLOR) {
265 char v[COLOR_MAXLEN];
266 if (git_config_color(v, key_, value_) < 0)
267 return -1;
268 strbuf_addstr(buf, v);
269 } else if (value_) {
270 strbuf_addstr(buf, value_);
271 } else {
272 /* Just show the key name; back out delimiter */
273 if (show_keys)
274 strbuf_setlen(buf, buf->len - 1);
277 strbuf_addch(buf, term);
278 return 0;
281 static int collect_config(const char *key_, const char *value_, void *cb)
283 struct strbuf_list *values = cb;
285 if (!use_key_regexp && strcmp(key_, key))
286 return 0;
287 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
288 return 0;
289 if (regexp != NULL &&
290 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
291 return 0;
293 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
294 strbuf_init(&values->items[values->nr], 0);
296 return format_config(&values->items[values->nr++], key_, value_);
299 static int get_value(const char *key_, const char *regex_)
301 int ret = CONFIG_GENERIC_ERROR;
302 struct strbuf_list values = {NULL};
303 int i;
305 if (use_key_regexp) {
306 char *tl;
309 * NEEDSWORK: this naive pattern lowercasing obviously does not
310 * work for more complex patterns like "^[^.]*Foo.*bar".
311 * Perhaps we should deprecate this altogether someday.
314 key = xstrdup(key_);
315 for (tl = key + strlen(key) - 1;
316 tl >= key && *tl != '.';
317 tl--)
318 *tl = tolower(*tl);
319 for (tl = key; *tl && *tl != '.'; tl++)
320 *tl = tolower(*tl);
322 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
323 if (regcomp(key_regexp, key, REG_EXTENDED)) {
324 error(_("invalid key pattern: %s"), key_);
325 FREE_AND_NULL(key_regexp);
326 ret = CONFIG_INVALID_PATTERN;
327 goto free_strings;
329 } else {
330 if (git_config_parse_key(key_, &key, NULL)) {
331 ret = CONFIG_INVALID_KEY;
332 goto free_strings;
336 if (regex_) {
337 if (regex_[0] == '!') {
338 do_not_match = 1;
339 regex_++;
342 regexp = (regex_t*)xmalloc(sizeof(regex_t));
343 if (regcomp(regexp, regex_, REG_EXTENDED)) {
344 error(_("invalid pattern: %s"), regex_);
345 FREE_AND_NULL(regexp);
346 ret = CONFIG_INVALID_PATTERN;
347 goto free_strings;
351 config_with_options(collect_config, &values,
352 &given_config_source, &config_options);
354 if (!values.nr && default_value) {
355 struct strbuf *item;
356 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
357 item = &values.items[values.nr++];
358 strbuf_init(item, 0);
359 if (format_config(item, key_, default_value) < 0)
360 die(_("failed to format default config value: %s"),
361 default_value);
364 ret = !values.nr;
366 for (i = 0; i < values.nr; i++) {
367 struct strbuf *buf = values.items + i;
368 if (do_all || i == values.nr - 1)
369 fwrite(buf->buf, 1, buf->len, stdout);
370 strbuf_release(buf);
372 free(values.items);
374 free_strings:
375 free(key);
376 if (key_regexp) {
377 regfree(key_regexp);
378 free(key_regexp);
380 if (regexp) {
381 regfree(regexp);
382 free(regexp);
385 return ret;
388 static char *normalize_value(const char *key, const char *value)
390 if (!value)
391 return NULL;
393 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
395 * We don't do normalization for TYPE_PATH here: If
396 * the path is like ~/foobar/, we prefer to store
397 * "~/foobar/" in the config file, and to expand the ~
398 * when retrieving the value.
399 * Also don't do normalization for expiry dates.
401 return xstrdup(value);
402 if (type == TYPE_INT)
403 return xstrfmt("%"PRId64, git_config_int64(key, value));
404 if (type == TYPE_BOOL)
405 return xstrdup(git_config_bool(key, value) ? "true" : "false");
406 if (type == TYPE_BOOL_OR_INT) {
407 int is_bool, v;
408 v = git_config_bool_or_int(key, value, &is_bool);
409 if (!is_bool)
410 return xstrfmt("%d", v);
411 else
412 return xstrdup(v ? "true" : "false");
414 if (type == TYPE_COLOR) {
415 char v[COLOR_MAXLEN];
416 if (git_config_color(v, key, value))
417 die(_("cannot parse color '%s'"), value);
420 * The contents of `v` now contain an ANSI escape
421 * sequence, not suitable for including within a
422 * configuration file. Treat the above as a
423 * "sanity-check", and return the given value, which we
424 * know is representable as valid color code.
426 return xstrdup(value);
429 BUG("cannot normalize type %d", type);
432 static int get_color_found;
433 static const char *get_color_slot;
434 static const char *get_colorbool_slot;
435 static char parsed_color[COLOR_MAXLEN];
437 static int git_get_color_config(const char *var, const char *value, void *cb)
439 if (!strcmp(var, get_color_slot)) {
440 if (!value)
441 config_error_nonbool(var);
442 if (color_parse(value, parsed_color) < 0)
443 return -1;
444 get_color_found = 1;
446 return 0;
449 static void get_color(const char *var, const char *def_color)
451 get_color_slot = var;
452 get_color_found = 0;
453 parsed_color[0] = '\0';
454 config_with_options(git_get_color_config, NULL,
455 &given_config_source, &config_options);
457 if (!get_color_found && def_color) {
458 if (color_parse(def_color, parsed_color) < 0)
459 die(_("unable to parse default color value"));
462 fputs(parsed_color, stdout);
465 static int get_colorbool_found;
466 static int get_diff_color_found;
467 static int get_color_ui_found;
468 static int git_get_colorbool_config(const char *var, const char *value,
469 void *cb)
471 if (!strcmp(var, get_colorbool_slot))
472 get_colorbool_found = git_config_colorbool(var, value);
473 else if (!strcmp(var, "diff.color"))
474 get_diff_color_found = git_config_colorbool(var, value);
475 else if (!strcmp(var, "color.ui"))
476 get_color_ui_found = git_config_colorbool(var, value);
477 return 0;
480 static int get_colorbool(const char *var, int print)
482 get_colorbool_slot = var;
483 get_colorbool_found = -1;
484 get_diff_color_found = -1;
485 get_color_ui_found = -1;
486 config_with_options(git_get_colorbool_config, NULL,
487 &given_config_source, &config_options);
489 if (get_colorbool_found < 0) {
490 if (!strcmp(get_colorbool_slot, "color.diff"))
491 get_colorbool_found = get_diff_color_found;
492 if (get_colorbool_found < 0)
493 get_colorbool_found = get_color_ui_found;
496 if (get_colorbool_found < 0)
497 /* default value if none found in config */
498 get_colorbool_found = GIT_COLOR_AUTO;
500 get_colorbool_found = want_color(get_colorbool_found);
502 if (print) {
503 printf("%s\n", get_colorbool_found ? "true" : "false");
504 return 0;
505 } else
506 return get_colorbool_found ? 0 : 1;
509 static void check_write(void)
511 if (!given_config_source.file && !startup_info->have_repository)
512 die(_("not in a git directory"));
514 if (given_config_source.use_stdin)
515 die(_("writing to stdin is not supported"));
517 if (given_config_source.blob)
518 die(_("writing config blobs is not supported"));
521 struct urlmatch_current_candidate_value {
522 char value_is_null;
523 struct strbuf value;
526 static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
528 struct string_list *values = cb;
529 struct string_list_item *item = string_list_insert(values, var);
530 struct urlmatch_current_candidate_value *matched = item->util;
532 if (!matched) {
533 matched = xmalloc(sizeof(*matched));
534 strbuf_init(&matched->value, 0);
535 item->util = matched;
536 } else {
537 strbuf_reset(&matched->value);
540 if (value) {
541 strbuf_addstr(&matched->value, value);
542 matched->value_is_null = 0;
543 } else {
544 matched->value_is_null = 1;
546 return 0;
549 static int get_urlmatch(const char *var, const char *url)
551 int ret;
552 char *section_tail;
553 struct string_list_item *item;
554 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
555 struct string_list values = STRING_LIST_INIT_DUP;
557 config.collect_fn = urlmatch_collect_fn;
558 config.cascade_fn = NULL;
559 config.cb = &values;
561 if (!url_normalize(url, &config.url))
562 die("%s", config.url.err);
564 config.section = xstrdup_tolower(var);
565 section_tail = strchr(config.section, '.');
566 if (section_tail) {
567 *section_tail = '\0';
568 config.key = section_tail + 1;
569 show_keys = 0;
570 } else {
571 config.key = NULL;
572 show_keys = 1;
575 config_with_options(urlmatch_config_entry, &config,
576 &given_config_source, &config_options);
578 ret = !values.nr;
580 for_each_string_list_item(item, &values) {
581 struct urlmatch_current_candidate_value *matched = item->util;
582 struct strbuf buf = STRBUF_INIT;
584 format_config(&buf, item->string,
585 matched->value_is_null ? NULL : matched->value.buf);
586 fwrite(buf.buf, 1, buf.len, stdout);
587 strbuf_release(&buf);
589 strbuf_release(&matched->value);
591 string_list_clear(&config.vars, 1);
592 string_list_clear(&values, 1);
593 free(config.url.url);
595 free((void *)config.section);
596 return ret;
599 static char *default_user_config(void)
601 struct strbuf buf = STRBUF_INIT;
602 strbuf_addf(&buf,
603 _("# This is Git's per-user configuration file.\n"
604 "[user]\n"
605 "# Please adapt and uncomment the following lines:\n"
606 "# name = %s\n"
607 "# email = %s\n"),
608 ident_default_name(),
609 ident_default_email());
610 return strbuf_detach(&buf, NULL);
613 int cmd_config(int argc, const char **argv, const char *prefix)
615 int nongit = !startup_info->have_repository;
616 char *value;
618 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
620 argc = parse_options(argc, argv, prefix, builtin_config_options,
621 builtin_config_usage,
622 PARSE_OPT_STOP_AT_NON_OPTION);
624 if (use_global_config + use_system_config + use_local_config +
625 use_worktree_config +
626 !!given_config_source.file + !!given_config_source.blob > 1) {
627 error(_("only one config file at a time"));
628 usage_builtin_config();
631 if (use_local_config && nongit)
632 die(_("--local can only be used inside a git repository"));
634 if (given_config_source.blob && nongit)
635 die(_("--blob can only be used inside a git repository"));
637 if (given_config_source.file &&
638 !strcmp(given_config_source.file, "-")) {
639 given_config_source.file = NULL;
640 given_config_source.use_stdin = 1;
641 given_config_source.scope = CONFIG_SCOPE_COMMAND;
644 if (use_global_config) {
645 char *user_config = expand_user_path("~/.gitconfig", 0);
646 char *xdg_config = xdg_config_home("config");
648 if (!user_config)
650 * It is unknown if HOME/.gitconfig exists, so
651 * we do not know if we should write to XDG
652 * location; error out even if XDG_CONFIG_HOME
653 * is set and points at a sane location.
655 die(_("$HOME not set"));
657 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
659 if (access_or_warn(user_config, R_OK, 0) &&
660 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
661 given_config_source.file = xdg_config;
662 free(user_config);
663 } else {
664 given_config_source.file = user_config;
665 free(xdg_config);
668 else if (use_system_config) {
669 given_config_source.file = git_etc_gitconfig();
670 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
671 } else if (use_local_config) {
672 given_config_source.file = git_pathdup("config");
673 given_config_source.scope = CONFIG_SCOPE_LOCAL;
674 } else if (use_worktree_config) {
675 struct worktree **worktrees = get_worktrees(0);
676 if (repository_format_worktree_config)
677 given_config_source.file = git_pathdup("config.worktree");
678 else if (worktrees[0] && worktrees[1])
679 die(_("--worktree cannot be used with multiple "
680 "working trees unless the config\n"
681 "extension worktreeConfig is enabled. "
682 "Please read \"CONFIGURATION FILE\"\n"
683 "section in \"git help worktree\" for details"));
684 else
685 given_config_source.file = git_pathdup("config");
686 given_config_source.scope = CONFIG_SCOPE_LOCAL;
687 free_worktrees(worktrees);
688 } else if (given_config_source.file) {
689 if (!is_absolute_path(given_config_source.file) && prefix)
690 given_config_source.file =
691 prefix_filename(prefix, given_config_source.file);
692 given_config_source.scope = CONFIG_SCOPE_COMMAND;
693 } else if (given_config_source.blob) {
694 given_config_source.scope = CONFIG_SCOPE_COMMAND;
698 if (respect_includes_opt == -1)
699 config_options.respect_includes = !given_config_source.file;
700 else
701 config_options.respect_includes = respect_includes_opt;
702 if (!nongit) {
703 config_options.commondir = get_git_common_dir();
704 config_options.git_dir = get_git_dir();
707 if (end_nul) {
708 term = '\0';
709 delim = '\n';
710 key_delim = '\n';
713 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
714 error(_("--get-color and variable type are incoherent"));
715 usage_builtin_config();
718 if (HAS_MULTI_BITS(actions)) {
719 error(_("only one action at a time"));
720 usage_builtin_config();
722 if (actions == 0)
723 switch (argc) {
724 case 1: actions = ACTION_GET; break;
725 case 2: actions = ACTION_SET; break;
726 case 3: actions = ACTION_SET_ALL; break;
727 default:
728 usage_builtin_config();
730 if (omit_values &&
731 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
732 error(_("--name-only is only applicable to --list or --get-regexp"));
733 usage_builtin_config();
736 if (show_origin && !(actions &
737 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
738 error(_("--show-origin is only applicable to --get, --get-all, "
739 "--get-regexp, and --list"));
740 usage_builtin_config();
743 if (default_value && !(actions & ACTION_GET)) {
744 error(_("--default is only applicable to --get"));
745 usage_builtin_config();
748 if (actions & PAGING_ACTIONS)
749 setup_auto_pager("config", 1);
751 if (actions == ACTION_LIST) {
752 check_argc(argc, 0, 0);
753 if (config_with_options(show_all_config, NULL,
754 &given_config_source,
755 &config_options) < 0) {
756 if (given_config_source.file)
757 die_errno(_("unable to read config file '%s'"),
758 given_config_source.file);
759 else
760 die(_("error processing config file(s)"));
763 else if (actions == ACTION_EDIT) {
764 char *config_file;
766 check_argc(argc, 0, 0);
767 if (!given_config_source.file && nongit)
768 die(_("not in a git directory"));
769 if (given_config_source.use_stdin)
770 die(_("editing stdin is not supported"));
771 if (given_config_source.blob)
772 die(_("editing blobs is not supported"));
773 git_config(git_default_config, NULL);
774 config_file = given_config_source.file ?
775 xstrdup(given_config_source.file) :
776 git_pathdup("config");
777 if (use_global_config) {
778 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
779 if (fd >= 0) {
780 char *content = default_user_config();
781 write_str_in_full(fd, content);
782 free(content);
783 close(fd);
785 else if (errno != EEXIST)
786 die_errno(_("cannot create configuration file %s"), config_file);
788 launch_editor(config_file, NULL, NULL);
789 free(config_file);
791 else if (actions == ACTION_SET) {
792 int ret;
793 check_write();
794 check_argc(argc, 2, 2);
795 value = normalize_value(argv[0], argv[1]);
796 UNLEAK(value);
797 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
798 if (ret == CONFIG_NOTHING_SET)
799 error(_("cannot overwrite multiple values with a single value\n"
800 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
801 return ret;
803 else if (actions == ACTION_SET_ALL) {
804 check_write();
805 check_argc(argc, 2, 3);
806 value = normalize_value(argv[0], argv[1]);
807 UNLEAK(value);
808 return git_config_set_multivar_in_file_gently(given_config_source.file,
809 argv[0], value, argv[2], 0);
811 else if (actions == ACTION_ADD) {
812 check_write();
813 check_argc(argc, 2, 2);
814 value = normalize_value(argv[0], argv[1]);
815 UNLEAK(value);
816 return git_config_set_multivar_in_file_gently(given_config_source.file,
817 argv[0], value,
818 CONFIG_REGEX_NONE, 0);
820 else if (actions == ACTION_REPLACE_ALL) {
821 check_write();
822 check_argc(argc, 2, 3);
823 value = normalize_value(argv[0], argv[1]);
824 UNLEAK(value);
825 return git_config_set_multivar_in_file_gently(given_config_source.file,
826 argv[0], value, argv[2], 1);
828 else if (actions == ACTION_GET) {
829 check_argc(argc, 1, 2);
830 return get_value(argv[0], argv[1]);
832 else if (actions == ACTION_GET_ALL) {
833 do_all = 1;
834 check_argc(argc, 1, 2);
835 return get_value(argv[0], argv[1]);
837 else if (actions == ACTION_GET_REGEXP) {
838 show_keys = 1;
839 use_key_regexp = 1;
840 do_all = 1;
841 check_argc(argc, 1, 2);
842 return get_value(argv[0], argv[1]);
844 else if (actions == ACTION_GET_URLMATCH) {
845 check_argc(argc, 2, 2);
846 return get_urlmatch(argv[0], argv[1]);
848 else if (actions == ACTION_UNSET) {
849 check_write();
850 check_argc(argc, 1, 2);
851 if (argc == 2)
852 return git_config_set_multivar_in_file_gently(given_config_source.file,
853 argv[0], NULL, argv[1], 0);
854 else
855 return git_config_set_in_file_gently(given_config_source.file,
856 argv[0], NULL);
858 else if (actions == ACTION_UNSET_ALL) {
859 check_write();
860 check_argc(argc, 1, 2);
861 return git_config_set_multivar_in_file_gently(given_config_source.file,
862 argv[0], NULL, argv[1], 1);
864 else if (actions == ACTION_RENAME_SECTION) {
865 int ret;
866 check_write();
867 check_argc(argc, 2, 2);
868 ret = git_config_rename_section_in_file(given_config_source.file,
869 argv[0], argv[1]);
870 if (ret < 0)
871 return ret;
872 if (ret == 0)
873 die(_("no such section: %s"), argv[0]);
875 else if (actions == ACTION_REMOVE_SECTION) {
876 int ret;
877 check_write();
878 check_argc(argc, 1, 1);
879 ret = git_config_rename_section_in_file(given_config_source.file,
880 argv[0], NULL);
881 if (ret < 0)
882 return ret;
883 if (ret == 0)
884 die(_("no such section: %s"), argv[0]);
886 else if (actions == ACTION_GET_COLOR) {
887 check_argc(argc, 1, 2);
888 get_color(argv[0], argv[1]);
890 else if (actions == ACTION_GET_COLORBOOL) {
891 check_argc(argc, 1, 2);
892 if (argc == 2)
893 color_stdout_is_tty = git_config_bool("command line", argv[1]);
894 return get_colorbool(argv[0], argc == 2);
897 return 0;