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