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