do not die when error in config parsing of buf occurs
[git] / builtin / config.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "color.h"
4 #include "parse-options.h"
5
6 static const char *const builtin_config_usage[] = {
7         N_("git config [options]"),
8         NULL
9 };
10
11 static char *key;
12 static regex_t *key_regexp;
13 static regex_t *regexp;
14 static int show_keys;
15 static int use_key_regexp;
16 static int do_all;
17 static int do_not_match;
18 static char delim = '=';
19 static char key_delim = ' ';
20 static char term = '\n';
21
22 static int use_global_config, use_system_config, use_local_config;
23 static const char *given_config_file;
24 static const char *given_config_blob;
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
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_BOOLEAN(0, "global", &use_global_config, N_("use global config file")),
54         OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")),
55         OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")),
56         OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
57         OPT_STRING(0, "blob", &given_config_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, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
63         OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
64         OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
65         OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
66         OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
67         OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
68         OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
69         OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
70         OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
71         OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
72         OPT_GROUP(N_("Type")),
73         OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
74         OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
75         OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
76         OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
77         OPT_GROUP(N_("Other")),
78         OPT_BOOLEAN('z', "null", &end_null, N_("terminate values with NUL byte")),
79         OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
80         OPT_END(),
81 };
82
83 static void check_argc(int argc, int min, int max) {
84         if (argc >= min && argc <= max)
85                 return;
86         error("wrong number of arguments");
87         usage_with_options(builtin_config_usage, builtin_config_options);
88 }
89
90 static int show_all_config(const char *key_, const char *value_, void *cb)
91 {
92         if (value_)
93                 printf("%s%c%s%c", key_, delim, value_, term);
94         else
95                 printf("%s%c", key_, term);
96         return 0;
97 }
98
99 struct strbuf_list {
100         struct strbuf *items;
101         int nr;
102         int alloc;
103 };
104
105 static int collect_config(const char *key_, const char *value_, void *cb)
106 {
107         struct strbuf_list *values = cb;
108         struct strbuf *buf;
109         char value[256];
110         const char *vptr = value;
111         int must_free_vptr = 0;
112         int must_print_delim = 0;
113
114         if (!use_key_regexp && strcmp(key_, key))
115                 return 0;
116         if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
117                 return 0;
118         if (regexp != NULL &&
119             (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
120                 return 0;
121
122         ALLOC_GROW(values->items, values->nr + 1, values->alloc);
123         buf = &values->items[values->nr++];
124         strbuf_init(buf, 0);
125
126         if (show_keys) {
127                 strbuf_addstr(buf, key_);
128                 must_print_delim = 1;
129         }
130         if (types == TYPE_INT)
131                 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
132         else if (types == TYPE_BOOL)
133                 vptr = git_config_bool(key_, value_) ? "true" : "false";
134         else if (types == TYPE_BOOL_OR_INT) {
135                 int is_bool, v;
136                 v = git_config_bool_or_int(key_, value_, &is_bool);
137                 if (is_bool)
138                         vptr = v ? "true" : "false";
139                 else
140                         sprintf(value, "%d", v);
141         } else if (types == TYPE_PATH) {
142                 if (git_config_pathname(&vptr, key_, value_) < 0)
143                         return -1;
144                 must_free_vptr = 1;
145         } else if (value_) {
146                 vptr = value_;
147         } else {
148                 /* Just show the key name */
149                 vptr = "";
150                 must_print_delim = 0;
151         }
152
153         if (must_print_delim)
154                 strbuf_addch(buf, key_delim);
155         strbuf_addstr(buf, vptr);
156         strbuf_addch(buf, term);
157
158         if (must_free_vptr)
159                 /* If vptr must be freed, it's a pointer to a
160                  * dynamically allocated buffer, it's safe to cast to
161                  * const.
162                 */
163                 free((char *)vptr);
164
165         return 0;
166 }
167
168 static int get_value(const char *key_, const char *regex_)
169 {
170         int ret = CONFIG_GENERIC_ERROR;
171         struct strbuf_list values = {NULL};
172         int i;
173
174         if (use_key_regexp) {
175                 char *tl;
176
177                 /*
178                  * NEEDSWORK: this naive pattern lowercasing obviously does not
179                  * work for more complex patterns like "^[^.]*Foo.*bar".
180                  * Perhaps we should deprecate this altogether someday.
181                  */
182
183                 key = xstrdup(key_);
184                 for (tl = key + strlen(key) - 1;
185                      tl >= key && *tl != '.';
186                      tl--)
187                         *tl = tolower(*tl);
188                 for (tl = key; *tl && *tl != '.'; tl++)
189                         *tl = tolower(*tl);
190
191                 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
192                 if (regcomp(key_regexp, key, REG_EXTENDED)) {
193                         fprintf(stderr, "Invalid key pattern: %s\n", key_);
194                         free(key_regexp);
195                         key_regexp = NULL;
196                         ret = CONFIG_INVALID_PATTERN;
197                         goto free_strings;
198                 }
199         } else {
200                 if (git_config_parse_key(key_, &key, NULL)) {
201                         ret = CONFIG_INVALID_KEY;
202                         goto free_strings;
203                 }
204         }
205
206         if (regex_) {
207                 if (regex_[0] == '!') {
208                         do_not_match = 1;
209                         regex_++;
210                 }
211
212                 regexp = (regex_t*)xmalloc(sizeof(regex_t));
213                 if (regcomp(regexp, regex_, REG_EXTENDED)) {
214                         fprintf(stderr, "Invalid pattern: %s\n", regex_);
215                         free(regexp);
216                         regexp = NULL;
217                         ret = CONFIG_INVALID_PATTERN;
218                         goto free_strings;
219                 }
220         }
221
222         git_config_with_options(collect_config, &values,
223                                 given_config_file, given_config_blob,
224                                 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, given_config_blob,
309                                 respect_includes);
310
311         if (!get_color_found && def_color)
312                 color_parse(def_color, "command line", parsed_color);
313
314         fputs(parsed_color, stdout);
315 }
316
317 static int get_colorbool_found;
318 static int get_diff_color_found;
319 static int get_color_ui_found;
320 static int git_get_colorbool_config(const char *var, const char *value,
321                 void *cb)
322 {
323         if (!strcmp(var, get_colorbool_slot))
324                 get_colorbool_found = git_config_colorbool(var, value);
325         else if (!strcmp(var, "diff.color"))
326                 get_diff_color_found = git_config_colorbool(var, value);
327         else if (!strcmp(var, "color.ui"))
328                 get_color_ui_found = git_config_colorbool(var, value);
329         return 0;
330 }
331
332 static int get_colorbool(int print)
333 {
334         get_colorbool_found = -1;
335         get_diff_color_found = -1;
336         git_config_with_options(git_get_colorbool_config, NULL,
337                                 given_config_file, given_config_blob,
338                                 respect_includes);
339
340         if (get_colorbool_found < 0) {
341                 if (!strcmp(get_colorbool_slot, "color.diff"))
342                         get_colorbool_found = get_diff_color_found;
343                 if (get_colorbool_found < 0)
344                         get_colorbool_found = get_color_ui_found;
345         }
346
347         get_colorbool_found = want_color(get_colorbool_found);
348
349         if (print) {
350                 printf("%s\n", get_colorbool_found ? "true" : "false");
351                 return 0;
352         } else
353                 return get_colorbool_found ? 0 : 1;
354 }
355
356 static void check_blob_write(void)
357 {
358         if (given_config_blob)
359                 die("writing config blobs is not supported");
360 }
361
362 int cmd_config(int argc, const char **argv, const char *prefix)
363 {
364         int nongit = !startup_info->have_repository;
365         char *value;
366
367         given_config_file = getenv(CONFIG_ENVIRONMENT);
368
369         argc = parse_options(argc, argv, prefix, builtin_config_options,
370                              builtin_config_usage,
371                              PARSE_OPT_STOP_AT_NON_OPTION);
372
373         if (use_global_config + use_system_config + use_local_config +
374             !!given_config_file + !!given_config_blob > 1) {
375                 error("only one config file at a time.");
376                 usage_with_options(builtin_config_usage, builtin_config_options);
377         }
378
379         if (use_global_config) {
380                 char *user_config = NULL;
381                 char *xdg_config = NULL;
382
383                 home_config_paths(&user_config, &xdg_config, "config");
384
385                 if (!user_config)
386                         /*
387                          * It is unknown if HOME/.gitconfig exists, so
388                          * we do not know if we should write to XDG
389                          * location; error out even if XDG_CONFIG_HOME
390                          * is set and points at a sane location.
391                          */
392                         die("$HOME not set");
393
394                 if (access_or_warn(user_config, R_OK) &&
395                     xdg_config && !access_or_warn(xdg_config, R_OK))
396                         given_config_file = xdg_config;
397                 else
398                         given_config_file = user_config;
399         }
400         else if (use_system_config)
401                 given_config_file = git_etc_gitconfig();
402         else if (use_local_config)
403                 given_config_file = git_pathdup("config");
404         else if (given_config_file) {
405                 if (!is_absolute_path(given_config_file) && prefix)
406                         given_config_file =
407                                 xstrdup(prefix_filename(prefix,
408                                                         strlen(prefix),
409                                                         given_config_file));
410         }
411
412         if (respect_includes == -1)
413                 respect_includes = !given_config_file;
414
415         if (end_null) {
416                 term = '\0';
417                 delim = '\n';
418                 key_delim = '\n';
419         }
420
421         if (HAS_MULTI_BITS(types)) {
422                 error("only one type at a time.");
423                 usage_with_options(builtin_config_usage, builtin_config_options);
424         }
425
426         if (get_color_slot)
427             actions |= ACTION_GET_COLOR;
428         if (get_colorbool_slot)
429             actions |= ACTION_GET_COLORBOOL;
430
431         if ((get_color_slot || get_colorbool_slot) && types) {
432                 error("--get-color and variable type are incoherent");
433                 usage_with_options(builtin_config_usage, builtin_config_options);
434         }
435
436         if (HAS_MULTI_BITS(actions)) {
437                 error("only one action at a time.");
438                 usage_with_options(builtin_config_usage, builtin_config_options);
439         }
440         if (actions == 0)
441                 switch (argc) {
442                 case 1: actions = ACTION_GET; break;
443                 case 2: actions = ACTION_SET; break;
444                 case 3: actions = ACTION_SET_ALL; break;
445                 default:
446                         usage_with_options(builtin_config_usage, builtin_config_options);
447                 }
448
449         if (actions == ACTION_LIST) {
450                 check_argc(argc, 0, 0);
451                 if (git_config_with_options(show_all_config, NULL,
452                                             given_config_file,
453                                             given_config_blob,
454                                             respect_includes) < 0) {
455                         if (given_config_file)
456                                 die_errno("unable to read config file '%s'",
457                                           given_config_file);
458                         else
459                                 die("error processing config file(s)");
460                 }
461         }
462         else if (actions == ACTION_EDIT) {
463                 check_argc(argc, 0, 0);
464                 if (!given_config_file && nongit)
465                         die("not in a git directory");
466                 if (given_config_blob)
467                         die("editing blobs is not supported");
468                 git_config(git_default_config, NULL);
469                 launch_editor(given_config_file ?
470                               given_config_file : git_path("config"),
471                               NULL, NULL);
472         }
473         else if (actions == ACTION_SET) {
474                 int ret;
475                 check_blob_write();
476                 check_argc(argc, 2, 2);
477                 value = normalize_value(argv[0], argv[1]);
478                 ret = git_config_set_in_file(given_config_file, argv[0], value);
479                 if (ret == CONFIG_NOTHING_SET)
480                         error("cannot overwrite multiple values with a single value\n"
481                         "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
482                 return ret;
483         }
484         else if (actions == ACTION_SET_ALL) {
485                 check_blob_write();
486                 check_argc(argc, 2, 3);
487                 value = normalize_value(argv[0], argv[1]);
488                 return git_config_set_multivar_in_file(given_config_file,
489                                                        argv[0], value, argv[2], 0);
490         }
491         else if (actions == ACTION_ADD) {
492                 check_blob_write();
493                 check_argc(argc, 2, 2);
494                 value = normalize_value(argv[0], argv[1]);
495                 return git_config_set_multivar_in_file(given_config_file,
496                                                        argv[0], value, "^$", 0);
497         }
498         else if (actions == ACTION_REPLACE_ALL) {
499                 check_blob_write();
500                 check_argc(argc, 2, 3);
501                 value = normalize_value(argv[0], argv[1]);
502                 return git_config_set_multivar_in_file(given_config_file,
503                                                        argv[0], value, argv[2], 1);
504         }
505         else if (actions == ACTION_GET) {
506                 check_argc(argc, 1, 2);
507                 return get_value(argv[0], argv[1]);
508         }
509         else if (actions == ACTION_GET_ALL) {
510                 do_all = 1;
511                 check_argc(argc, 1, 2);
512                 return get_value(argv[0], argv[1]);
513         }
514         else if (actions == ACTION_GET_REGEXP) {
515                 show_keys = 1;
516                 use_key_regexp = 1;
517                 do_all = 1;
518                 check_argc(argc, 1, 2);
519                 return get_value(argv[0], argv[1]);
520         }
521         else if (actions == ACTION_UNSET) {
522                 check_blob_write();
523                 check_argc(argc, 1, 2);
524                 if (argc == 2)
525                         return git_config_set_multivar_in_file(given_config_file,
526                                                                argv[0], NULL, argv[1], 0);
527                 else
528                         return git_config_set_in_file(given_config_file,
529                                                       argv[0], NULL);
530         }
531         else if (actions == ACTION_UNSET_ALL) {
532                 check_blob_write();
533                 check_argc(argc, 1, 2);
534                 return git_config_set_multivar_in_file(given_config_file,
535                                                        argv[0], NULL, argv[1], 1);
536         }
537         else if (actions == ACTION_RENAME_SECTION) {
538                 int ret;
539                 check_blob_write();
540                 check_argc(argc, 2, 2);
541                 ret = git_config_rename_section_in_file(given_config_file,
542                                                         argv[0], argv[1]);
543                 if (ret < 0)
544                         return ret;
545                 if (ret == 0)
546                         die("No such section!");
547         }
548         else if (actions == ACTION_REMOVE_SECTION) {
549                 int ret;
550                 check_blob_write();
551                 check_argc(argc, 1, 1);
552                 ret = git_config_rename_section_in_file(given_config_file,
553                                                         argv[0], NULL);
554                 if (ret < 0)
555                         return ret;
556                 if (ret == 0)
557                         die("No such section!");
558         }
559         else if (actions == ACTION_GET_COLOR) {
560                 get_color(argv[0]);
561         }
562         else if (actions == ACTION_GET_COLORBOOL) {
563                 if (argc == 1)
564                         color_stdout_is_tty = git_config_bool("command line", argv[0]);
565                 return get_colorbool(argc != 0);
566         }
567
568         return 0;
569 }
570
571 int cmd_repo_config(int argc, const char **argv, const char *prefix)
572 {
573         fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
574         return cmd_config(argc, argv, prefix);
575 }