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
9static const char *const builtin_config_usage[] = {
10 N_("git config [<options>]"),
11 NULL
12};
13
14static char *key;
15static regex_t *key_regexp;
16static regex_t *regexp;
17static int show_keys;
18static int omit_values;
19static int use_key_regexp;
20static int do_all;
21static int do_not_match;
22static char delim = '=';
23static char key_delim = ' ';
24static char term = '\n';
25
26static int use_global_config, use_system_config, use_local_config;
27static struct git_config_source given_config_source;
28static int actions, type;
29static char *default_value;
30static int end_null;
31static int respect_includes_opt = -1;
32static struct config_options config_options;
33static int show_origin;
34
35#define ACTION_GET (1<<0)
36#define ACTION_GET_ALL (1<<1)
37#define ACTION_GET_REGEXP (1<<2)
38#define ACTION_REPLACE_ALL (1<<3)
39#define ACTION_ADD (1<<4)
40#define ACTION_UNSET (1<<5)
41#define ACTION_UNSET_ALL (1<<6)
42#define ACTION_RENAME_SECTION (1<<7)
43#define ACTION_REMOVE_SECTION (1<<8)
44#define ACTION_LIST (1<<9)
45#define ACTION_EDIT (1<<10)
46#define ACTION_SET (1<<11)
47#define ACTION_SET_ALL (1<<12)
48#define ACTION_GET_COLOR (1<<13)
49#define ACTION_GET_COLORBOOL (1<<14)
50#define ACTION_GET_URLMATCH (1<<15)
51
52/*
53 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
54 * one line of output and which should therefore be paged.
55 */
56#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
57 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
58
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
65
66#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
67 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
68 PARSE_OPT_NONEG, option_parse_type, (i) }
69
70static struct option builtin_config_options[];
71
72static int option_parse_type(const struct option *opt, const char *arg,
73 int unset)
74{
75 int new_type, *to_type;
76
77 if (unset) {
78 *((int *) opt->value) = 0;
79 return 0;
80 }
81
82 /*
83 * To support '--<type>' style flags, begin with new_type equal to
84 * opt->defval.
85 */
86 new_type = opt->defval;
87 if (!new_type) {
88 if (!strcmp(arg, "bool"))
89 new_type = TYPE_BOOL;
90 else if (!strcmp(arg, "int"))
91 new_type = TYPE_INT;
92 else if (!strcmp(arg, "bool-or-int"))
93 new_type = TYPE_BOOL_OR_INT;
94 else if (!strcmp(arg, "path"))
95 new_type = TYPE_PATH;
96 else if (!strcmp(arg, "expiry-date"))
97 new_type = TYPE_EXPIRY_DATE;
98 else if (!strcmp(arg, "color"))
99 new_type = TYPE_COLOR;
100 else
101 die(_("unrecognized --type argument, %s"), arg);
102 }
103
104 to_type = opt->value;
105 if (*to_type && *to_type != new_type) {
106 /*
107 * Complain when there is a new type not equal to the old type.
108 * This allows for combinations like '--int --type=int' and
109 * '--type=int --type=int', but disallows ones like '--type=bool
110 * --int' and '--type=bool
111 * --type=int'.
112 */
113 error("only one type at a time.");
114 usage_with_options(builtin_config_usage,
115 builtin_config_options);
116 }
117 *to_type = new_type;
118
119 return 0;
120}
121
122static 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(),
158};
159
160static void check_argc(int argc, int min, int max) {
161 if (argc >= min && argc <= max)
162 return;
163 error("wrong number of arguments");
164 usage_with_options(builtin_config_usage, builtin_config_options);
165}
166
167static void show_config_origin(struct strbuf *buf)
168{
169 const char term = end_null ? '\0' : '\t';
170
171 strbuf_addstr(buf, current_config_origin_type());
172 strbuf_addch(buf, ':');
173 if (end_null)
174 strbuf_addstr(buf, current_config_name());
175 else
176 quote_c_style(current_config_name(), buf, NULL, 0);
177 strbuf_addch(buf, term);
178}
179
180static int show_all_config(const char *key_, const char *value_, void *cb)
181{
182 if (show_origin) {
183 struct strbuf buf = STRBUF_INIT;
184 show_config_origin(&buf);
185 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
186 fwrite(buf.buf, 1, buf.len, stdout);
187 strbuf_release(&buf);
188 }
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;
194}
195
196struct strbuf_list {
197 struct strbuf *items;
198 int nr;
199 int alloc;
200};
201
202static int format_config(struct strbuf *buf, const char *key_, const char *value_)
203{
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);
211
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);
247 }
248 }
249 strbuf_addch(buf, term);
250 return 0;
251}
252
253static int collect_config(const char *key_, const char *value_, void *cb)
254{
255 struct strbuf_list *values = cb;
256
257 if (!use_key_regexp && strcmp(key_, key))
258 return 0;
259 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
260 return 0;
261 if (regexp != NULL &&
262 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
263 return 0;
264
265 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
266 strbuf_init(&values->items[values->nr], 0);
267
268 return format_config(&values->items[values->nr++], key_, value_);
269}
270
271static int get_value(const char *key_, const char *regex_)
272{
273 int ret = CONFIG_GENERIC_ERROR;
274 struct strbuf_list values = {NULL};
275 int i;
276
277 if (use_key_regexp) {
278 char *tl;
279
280 /*
281 * NEEDSWORK: this naive pattern lowercasing obviously does not
282 * work for more complex patterns like "^[^.]*Foo.*bar".
283 * Perhaps we should deprecate this altogether someday.
284 */
285
286 key = xstrdup(key_);
287 for (tl = key + strlen(key) - 1;
288 tl >= key && *tl != '.';
289 tl--)
290 *tl = tolower(*tl);
291 for (tl = key; *tl && *tl != '.'; tl++)
292 *tl = tolower(*tl);
293
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;
300 }
301 } else {
302 if (git_config_parse_key(key_, &key, NULL)) {
303 ret = CONFIG_INVALID_KEY;
304 goto free_strings;
305 }
306 }
307
308 if (regex_) {
309 if (regex_[0] == '!') {
310 do_not_match = 1;
311 regex_++;
312 }
313
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;
320 }
321 }
322
323 config_with_options(collect_config, &values,
324 &given_config_source, &config_options);
325
326 if (!values.nr && default_value) {
327 struct strbuf *item;
328 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
329 item = &values.items[values.nr++];
330 strbuf_init(item, 0);
331 if (format_config(item, key_, default_value) < 0)
332 die(_("failed to format default config value: %s"),
333 default_value);
334 }
335
336 ret = !values.nr;
337
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);
343 }
344 free(values.items);
345
346free_strings:
347 free(key);
348 if (key_regexp) {
349 regfree(key_regexp);
350 free(key_regexp);
351 }
352 if (regexp) {
353 regfree(regexp);
354 free(regexp);
355 }
356
357 return ret;
358}
359
360static char *normalize_value(const char *key, const char *value)
361{
362 if (!value)
363 return NULL;
364
365 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
366 /*
367 * We don't do normalization for TYPE_PATH here: If
368 * the path is like ~/foobar/, we prefer to store
369 * "~/foobar/" in the config file, and to expand the ~
370 * when retrieving the value.
371 * Also don't do normalization for expiry dates.
372 */
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");
385 }
386 if (type == TYPE_COLOR) {
387 char v[COLOR_MAXLEN];
388 if (git_config_color(v, key, value))
389 die("cannot parse color '%s'", value);
390
391 /*
392 * The contents of `v` now contain an ANSI escape
393 * sequence, not suitable for including within a
394 * configuration file. Treat the above as a
395 * "sanity-check", and return the given value, which we
396 * know is representable as valid color code.
397 */
398 return xstrdup(value);
399 }
400
401 BUG("cannot normalize type %d", type);
402}
403
404static int get_color_found;
405static const char *get_color_slot;
406static const char *get_colorbool_slot;
407static char parsed_color[COLOR_MAXLEN];
408
409static int git_get_color_config(const char *var, const char *value, void *cb)
410{
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;
417 }
418 return 0;
419}
420
421static void get_color(const char *var, const char *def_color)
422{
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);
428
429 if (!get_color_found && def_color) {
430 if (color_parse(def_color, parsed_color) < 0)
431 die(_("unable to parse default color value"));
432 }
433
434 fputs(parsed_color, stdout);
435}
436
437static int get_colorbool_found;
438static int get_diff_color_found;
439static int get_color_ui_found;
440static int git_get_colorbool_config(const char *var, const char *value,
441 void *cb)
442{
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;
450}
451
452static int get_colorbool(const char *var, int print)
453{
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);
460
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;
466 }
467
468 if (get_colorbool_found < 0)
469 /* default value if none found in config */
470 get_colorbool_found = GIT_COLOR_AUTO;
471
472 get_colorbool_found = want_color(get_colorbool_found);
473
474 if (print) {
475 printf("%s\n", get_colorbool_found ? "true" : "false");
476 return 0;
477 } else
478 return get_colorbool_found ? 0 : 1;
479}
480
481static void check_write(void)
482{
483 if (!given_config_source.file && !startup_info->have_repository)
484 die("not in a git directory");
485
486 if (given_config_source.use_stdin)
487 die("writing to stdin is not supported");
488
489 if (given_config_source.blob)
490 die("writing config blobs is not supported");
491}
492
493struct urlmatch_current_candidate_value {
494 char value_is_null;
495 struct strbuf value;
496};
497
498static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
499{
500 struct string_list *values = cb;
501 struct string_list_item *item = string_list_insert(values, var);
502 struct urlmatch_current_candidate_value *matched = item->util;
503
504 if (!matched) {
505 matched = xmalloc(sizeof(*matched));
506 strbuf_init(&matched->value, 0);
507 item->util = matched;
508 } else {
509 strbuf_reset(&matched->value);
510 }
511
512 if (value) {
513 strbuf_addstr(&matched->value, value);
514 matched->value_is_null = 0;
515 } else {
516 matched->value_is_null = 1;
517 }
518 return 0;
519}
520
521static int get_urlmatch(const char *var, const char *url)
522{
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;
528
529 config.collect_fn = urlmatch_collect_fn;
530 config.cascade_fn = NULL;
531 config.cb = &values;
532
533 if (!url_normalize(url, &config.url))
534 die("%s", config.url.err);
535
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;
545 }
546
547 config_with_options(urlmatch_config_entry, &config,
548 &given_config_source, &config_options);
549
550 ret = !values.nr;
551
552 for_each_string_list_item(item, &values) {
553 struct urlmatch_current_candidate_value *matched = item->util;
554 struct strbuf buf = STRBUF_INIT;
555
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);
560
561 strbuf_release(&matched->value);
562 }
563 string_list_clear(&config.vars, 1);
564 string_list_clear(&values, 1);
565 free(config.url.url);
566
567 free((void *)config.section);
568 return ret;
569}
570
571static char *default_user_config(void)
572{
573 struct strbuf buf = STRBUF_INIT;
574 strbuf_addf(&buf,
575 _("# This is Git's per-user configuration file.\n"
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);
583}
584
585int cmd_config(int argc, const char **argv, const char *prefix)
586{
587 int nongit = !startup_info->have_repository;
588 char *value;
589
590 given_config_source.file = getenv(CONFIG_ENVIRONMENT);
591
592 argc = parse_options(argc, argv, prefix, builtin_config_options,
593 builtin_config_usage,
594 PARSE_OPT_STOP_AT_NON_OPTION);
595
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);
600 }
601
602 if (use_local_config && nongit)
603 die(_("--local can only be used inside a git repository"));
604
605 if (given_config_source.blob && nongit)
606 die(_("--blob can only be used inside a git repository"));
607
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;
612 }
613
614 if (use_global_config) {
615 char *user_config = expand_user_path("~/.gitconfig", 0);
616 char *xdg_config = xdg_config_home("config");
617
618 if (!user_config)
619 /*
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.
624 */
625 die("$HOME not set");
626
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);
634 }
635 }
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);
644 }
645
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();
653 }
654
655 if (end_null) {
656 term = '\0';
657 delim = '\n';
658 key_delim = '\n';
659 }
660
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);
664 }
665
666 if (HAS_MULTI_BITS(actions)) {
667 error("only one action at a time.");
668 usage_with_options(builtin_config_usage, builtin_config_options);
669 }
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);
677 }
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);
682 }
683
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);
689 }
690
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);
695 }
696
697 if (actions & PAGING_ACTIONS)
698 setup_auto_pager("config", 1);
699
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)");
710 }
711 }
712 else if (actions == ACTION_EDIT) {
713 char *config_file;
714
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);
733 }
734 else if (errno != EEXIST)
735 die_errno(_("cannot create configuration file %s"), config_file);
736 }
737 launch_editor(config_file, NULL, NULL);
738 free(config_file);
739 }
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;
751 }
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);
759 }
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);
768 }
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);
776 }
777 else if (actions == ACTION_GET) {
778 check_argc(argc, 1, 2);
779 return get_value(argv[0], argv[1]);
780 }
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]);
785 }
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]);
792 }
793 else if (actions == ACTION_GET_URLMATCH) {
794 check_argc(argc, 2, 2);
795 return get_urlmatch(argv[0], argv[1]);
796 }
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);
806 }
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);
812 }
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!");
823 }
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!");
834 }
835 else if (actions == ACTION_GET_COLOR) {
836 check_argc(argc, 1, 2);
837 get_color(argv[0], argv[1]);
838 }
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);
844 }
845
846 return 0;
847}