Code

git config: trivial rename in preparation for parseopt
[git.git] / builtin-config.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "color.h"
5 static const char git_config_set_usage[] =
6 "git config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int | --bool-or-int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default] | --get-colorbool name [stdout-is-tty] | --edit | -e ]";
8 static char *key;
9 static regex_t *key_regexp;
10 static regex_t *regexp;
11 static int show_keys;
12 static int use_key_regexp;
13 static int do_all;
14 static int do_not_match;
15 static int seen;
16 static char delim = '=';
17 static char key_delim = ' ';
18 static char term = '\n';
19 static enum { T_RAW, T_INT, T_BOOL, T_BOOL_OR_INT } type = T_RAW;
21 static int show_all_config(const char *key_, const char *value_, void *cb)
22 {
23         if (value_)
24                 printf("%s%c%s%c", key_, delim, value_, term);
25         else
26                 printf("%s%c", key_, term);
27         return 0;
28 }
30 static int show_config(const char *key_, const char *value_, void *cb)
31 {
32         char value[256];
33         const char *vptr = value;
34         int dup_error = 0;
36         if (!use_key_regexp && strcmp(key_, key))
37                 return 0;
38         if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
39                 return 0;
40         if (regexp != NULL &&
41             (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
42                 return 0;
44         if (show_keys) {
45                 if (value_)
46                         printf("%s%c", key_, key_delim);
47                 else
48                         printf("%s", key_);
49         }
50         if (seen && !do_all)
51                 dup_error = 1;
52         if (type == T_INT)
53                 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
54         else if (type == T_BOOL)
55                 vptr = git_config_bool(key_, value_) ? "true" : "false";
56         else if (type == T_BOOL_OR_INT) {
57                 int is_bool, v;
58                 v = git_config_bool_or_int(key_, value_, &is_bool);
59                 if (is_bool)
60                         vptr = v ? "true" : "false";
61                 else
62                         sprintf(value, "%d", v);
63         }
64         else
65                 vptr = value_?value_:"";
66         seen++;
67         if (dup_error) {
68                 error("More than one value for the key %s: %s",
69                                 key_, vptr);
70         }
71         else
72                 printf("%s%c", vptr, term);
74         return 0;
75 }
77 static int get_value(const char *key_, const char *regex_)
78 {
79         int ret = -1;
80         char *tl;
81         char *global = NULL, *repo_config = NULL;
82         const char *system_wide = NULL, *local;
84         local = config_exclusive_filename;
85         if (!local) {
86                 const char *home = getenv("HOME");
87                 local = repo_config = git_pathdup("config");
88                 if (git_config_global() && home)
89                         global = xstrdup(mkpath("%s/.gitconfig", home));
90                 if (git_config_system())
91                         system_wide = git_etc_gitconfig();
92         }
94         key = xstrdup(key_);
95         for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
96                 *tl = tolower(*tl);
97         for (tl=key; *tl && *tl != '.'; ++tl)
98                 *tl = tolower(*tl);
100         if (use_key_regexp) {
101                 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
102                 if (regcomp(key_regexp, key, REG_EXTENDED)) {
103                         fprintf(stderr, "Invalid key pattern: %s\n", key_);
104                         goto free_strings;
105                 }
106         }
108         if (regex_) {
109                 if (regex_[0] == '!') {
110                         do_not_match = 1;
111                         regex_++;
112                 }
114                 regexp = (regex_t*)xmalloc(sizeof(regex_t));
115                 if (regcomp(regexp, regex_, REG_EXTENDED)) {
116                         fprintf(stderr, "Invalid pattern: %s\n", regex_);
117                         goto free_strings;
118                 }
119         }
121         if (do_all && system_wide)
122                 git_config_from_file(show_config, system_wide, NULL);
123         if (do_all && global)
124                 git_config_from_file(show_config, global, NULL);
125         git_config_from_file(show_config, local, NULL);
126         if (!do_all && !seen && global)
127                 git_config_from_file(show_config, global, NULL);
128         if (!do_all && !seen && system_wide)
129                 git_config_from_file(show_config, system_wide, NULL);
131         free(key);
132         if (regexp) {
133                 regfree(regexp);
134                 free(regexp);
135         }
137         if (do_all)
138                 ret = !seen;
139         else
140                 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
142 free_strings:
143         free(repo_config);
144         free(global);
145         return ret;
148 static char *normalize_value(const char *key, const char *value)
150         char *normalized;
152         if (!value)
153                 return NULL;
155         if (type == T_RAW)
156                 normalized = xstrdup(value);
157         else {
158                 normalized = xmalloc(64);
159                 if (type == T_INT) {
160                         int v = git_config_int(key, value);
161                         sprintf(normalized, "%d", v);
162                 }
163                 else if (type == T_BOOL)
164                         sprintf(normalized, "%s",
165                                 git_config_bool(key, value) ? "true" : "false");
166                 else if (type == T_BOOL_OR_INT) {
167                         int is_bool, v;
168                         v = git_config_bool_or_int(key, value, &is_bool);
169                         if (!is_bool)
170                                 sprintf(normalized, "%d", v);
171                         else
172                                 sprintf(normalized, "%s", v ? "true" : "false");
173                 }
174         }
176         return normalized;
179 static int get_color_found;
180 static const char *get_color_slot;
181 static const char *get_colorbool_slot;
182 static char parsed_color[COLOR_MAXLEN];
184 static int git_get_color_config(const char *var, const char *value, void *cb)
186         if (!strcmp(var, get_color_slot)) {
187                 if (!value)
188                         config_error_nonbool(var);
189                 color_parse(value, var, parsed_color);
190                 get_color_found = 1;
191         }
192         return 0;
195 static int get_color(int argc, const char **argv)
197         /*
198          * grab the color setting for the given slot from the configuration,
199          * or parse the default value if missing, and return ANSI color
200          * escape sequence.
201          *
202          * e.g.
203          * git config --get-color color.diff.whitespace "blue reverse"
204          */
205         const char *def_color = NULL;
207         switch (argc) {
208         default:
209                 usage(git_config_set_usage);
210         case 2:
211                 def_color = argv[1];
212                 /* fallthru */
213         case 1:
214                 get_color_slot = argv[0];
215                 break;
216         }
218         get_color_found = 0;
219         parsed_color[0] = '\0';
220         git_config(git_get_color_config, NULL);
222         if (!get_color_found && def_color)
223                 color_parse(def_color, "command line", parsed_color);
225         fputs(parsed_color, stdout);
226         return 0;
229 static int stdout_is_tty;
230 static int get_colorbool_found;
231 static int get_diff_color_found;
232 static int git_get_colorbool_config(const char *var, const char *value,
233                 void *cb)
235         if (!strcmp(var, get_colorbool_slot)) {
236                 get_colorbool_found =
237                         git_config_colorbool(var, value, stdout_is_tty);
238         }
239         if (!strcmp(var, "diff.color")) {
240                 get_diff_color_found =
241                         git_config_colorbool(var, value, stdout_is_tty);
242         }
243         if (!strcmp(var, "color.ui")) {
244                 git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
245                 return 0;
246         }
247         return 0;
250 static int get_colorbool(int argc, const char **argv)
252         /*
253          * git config --get-colorbool <slot> [<stdout-is-tty>]
254          *
255          * returns "true" or "false" depending on how <slot>
256          * is configured.
257          */
259         if (argc == 2)
260                 stdout_is_tty = git_config_bool("command line", argv[1]);
261         else if (argc == 1)
262                 stdout_is_tty = isatty(1);
263         else
264                 usage(git_config_set_usage);
265         get_colorbool_found = -1;
266         get_diff_color_found = -1;
267         get_colorbool_slot = argv[0];
268         git_config(git_get_colorbool_config, NULL);
270         if (get_colorbool_found < 0) {
271                 if (!strcmp(get_colorbool_slot, "color.diff"))
272                         get_colorbool_found = get_diff_color_found;
273                 if (get_colorbool_found < 0)
274                         get_colorbool_found = git_use_color_default;
275         }
277         if (argc == 1) {
278                 return get_colorbool_found ? 0 : 1;
279         } else {
280                 printf("%s\n", get_colorbool_found ? "true" : "false");
281                 return 0;
282         }
285 int cmd_config(int argc, const char **argv, const char *unused_prefix)
287         int nongit;
288         char *value;
289         const char *prefix = setup_git_directory_gently(&nongit);
291         config_exclusive_filename = getenv(CONFIG_ENVIRONMENT);
293         while (1 < argc) {
294                 if (!strcmp(argv[1], "--int"))
295                         type = T_INT;
296                 else if (!strcmp(argv[1], "--bool"))
297                         type = T_BOOL;
298                 else if (!strcmp(argv[1], "--bool-or-int"))
299                         type = T_BOOL_OR_INT;
300                 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
301                         if (argc != 2)
302                                 usage(git_config_set_usage);
303                         if (git_config(show_all_config, NULL) < 0) {
304                                 if (config_exclusive_filename)
305                                         die("unable to read config file %s: %s",
306                                             config_exclusive_filename, strerror(errno));
307                                 else
308                                         die("error processing config file(s)");
309                         }
310                         return 0;
311                 }
312                 else if (!strcmp(argv[1], "--global")) {
313                         char *home = getenv("HOME");
314                         if (home) {
315                                 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
316                                 config_exclusive_filename = user_config;
317                         } else {
318                                 die("$HOME not set");
319                         }
320                 }
321                 else if (!strcmp(argv[1], "--system"))
322                         config_exclusive_filename = git_etc_gitconfig();
323                 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
324                         if (argc < 3)
325                                 usage(git_config_set_usage);
326                         if (!is_absolute_path(argv[2]) && prefix)
327                                 config_exclusive_filename = prefix_filename(prefix,
328                                                                             strlen(prefix),
329                                                                             argv[2]);
330                         else
331                                 config_exclusive_filename = argv[2];
332                         argc--;
333                         argv++;
334                 }
335                 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
336                         term = '\0';
337                         delim = '\n';
338                         key_delim = '\n';
339                 }
340                 else if (!strcmp(argv[1], "--rename-section")) {
341                         int ret;
342                         if (argc != 4)
343                                 usage(git_config_set_usage);
344                         ret = git_config_rename_section(argv[2], argv[3]);
345                         if (ret < 0)
346                                 return ret;
347                         if (ret == 0) {
348                                 fprintf(stderr, "No such section!\n");
349                                 return 1;
350                         }
351                         return 0;
352                 }
353                 else if (!strcmp(argv[1], "--remove-section")) {
354                         int ret;
355                         if (argc != 3)
356                                 usage(git_config_set_usage);
357                         ret = git_config_rename_section(argv[2], NULL);
358                         if (ret < 0)
359                                 return ret;
360                         if (ret == 0) {
361                                 fprintf(stderr, "No such section!\n");
362                                 return 1;
363                         }
364                         return 0;
365                 } else if (!strcmp(argv[1], "--get-color")) {
366                         return get_color(argc-2, argv+2);
367                 } else if (!strcmp(argv[1], "--get-colorbool")) {
368                         return get_colorbool(argc-2, argv+2);
369                 } else if (!strcmp(argv[1], "--edit") || !strcmp(argv[1], "-e")) {
370                         if (argc != 2)
371                                 usage(git_config_set_usage);
372                         git_config(git_default_config, NULL);
373                         launch_editor(config_exclusive_filename ?
374                                       config_exclusive_filename : git_path("config"),
375                                       NULL, NULL);
376                         return 0;
377                 } else
378                         break;
379                 argc--;
380                 argv++;
381         }
383         switch (argc) {
384         case 2:
385                 return get_value(argv[1], NULL);
386         case 3:
387                 if (!strcmp(argv[1], "--unset"))
388                         return git_config_set(argv[2], NULL);
389                 else if (!strcmp(argv[1], "--unset-all"))
390                         return git_config_set_multivar(argv[2], NULL, NULL, 1);
391                 else if (!strcmp(argv[1], "--get"))
392                         return get_value(argv[2], NULL);
393                 else if (!strcmp(argv[1], "--get-all")) {
394                         do_all = 1;
395                         return get_value(argv[2], NULL);
396                 } else if (!strcmp(argv[1], "--get-regexp")) {
397                         show_keys = 1;
398                         use_key_regexp = 1;
399                         do_all = 1;
400                         return get_value(argv[2], NULL);
401                 } else {
402                         value = normalize_value(argv[1], argv[2]);
403                         return git_config_set(argv[1], value);
404                 }
405         case 4:
406                 if (!strcmp(argv[1], "--unset"))
407                         return git_config_set_multivar(argv[2], NULL, argv[3], 0);
408                 else if (!strcmp(argv[1], "--unset-all"))
409                         return git_config_set_multivar(argv[2], NULL, argv[3], 1);
410                 else if (!strcmp(argv[1], "--get"))
411                         return get_value(argv[2], argv[3]);
412                 else if (!strcmp(argv[1], "--get-all")) {
413                         do_all = 1;
414                         return get_value(argv[2], argv[3]);
415                 } else if (!strcmp(argv[1], "--get-regexp")) {
416                         show_keys = 1;
417                         use_key_regexp = 1;
418                         do_all = 1;
419                         return get_value(argv[2], argv[3]);
420                 } else if (!strcmp(argv[1], "--add")) {
421                         value = normalize_value(argv[2], argv[3]);
422                         return git_config_set_multivar(argv[2], value, "^$", 0);
423                 } else if (!strcmp(argv[1], "--replace-all")) {
424                         value = normalize_value(argv[2], argv[3]);
425                         return git_config_set_multivar(argv[2], value, NULL, 1);
426                 } else {
427                         value = normalize_value(argv[1], argv[2]);
428                         return git_config_set_multivar(argv[1], value, argv[3], 0);
429                 }
430         case 5:
431                 if (!strcmp(argv[1], "--replace-all")) {
432                         value = normalize_value(argv[2], argv[3]);
433                         return git_config_set_multivar(argv[2], value, argv[4], 1);
434                 }
435         case 1:
436         default:
437                 usage(git_config_set_usage);
438         }
439         return 0;