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