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