Code

git-bisect: make "start", "good" and "skip" succeed or fail atomically
[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 ] [ -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]";
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 } type = T_RAW;
21 static int show_all_config(const char *key_, const char *value_)
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_)
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
57                 vptr = value_?value_:"";
58         seen++;
59         if (dup_error) {
60                 error("More than one value for the key %s: %s",
61                                 key_, vptr);
62         }
63         else
64                 printf("%s%c", vptr, term);
66         return 0;
67 }
69 static int get_value(const char* key_, const char* regex_)
70 {
71         int ret = -1;
72         char *tl;
73         char *global = NULL, *repo_config = NULL;
74         const char *system_wide = NULL, *local;
76         local = getenv(CONFIG_ENVIRONMENT);
77         if (!local) {
78                 const char *home = getenv("HOME");
79                 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
80                 if (!local)
81                         local = repo_config = xstrdup(git_path("config"));
82                 if (home)
83                         global = xstrdup(mkpath("%s/.gitconfig", home));
84                 system_wide = git_etc_gitconfig();
85         }
87         key = xstrdup(key_);
88         for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
89                 *tl = tolower(*tl);
90         for (tl=key; *tl && *tl != '.'; ++tl)
91                 *tl = tolower(*tl);
93         if (use_key_regexp) {
94                 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
95                 if (regcomp(key_regexp, key, REG_EXTENDED)) {
96                         fprintf(stderr, "Invalid key pattern: %s\n", key_);
97                         goto free_strings;
98                 }
99         }
101         if (regex_) {
102                 if (regex_[0] == '!') {
103                         do_not_match = 1;
104                         regex_++;
105                 }
107                 regexp = (regex_t*)xmalloc(sizeof(regex_t));
108                 if (regcomp(regexp, regex_, REG_EXTENDED)) {
109                         fprintf(stderr, "Invalid pattern: %s\n", regex_);
110                         goto free_strings;
111                 }
112         }
114         if (do_all && system_wide)
115                 git_config_from_file(show_config, system_wide);
116         if (do_all && global)
117                 git_config_from_file(show_config, global);
118         git_config_from_file(show_config, local);
119         if (!do_all && !seen && global)
120                 git_config_from_file(show_config, global);
121         if (!do_all && !seen && system_wide)
122                 git_config_from_file(show_config, system_wide);
124         free(key);
125         if (regexp) {
126                 regfree(regexp);
127                 free(regexp);
128         }
130         if (do_all)
131                 ret = !seen;
132         else
133                 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
135 free_strings:
136         free(repo_config);
137         free(global);
138         return ret;
141 char *normalize_value(const char *key, const char *value)
143         char *normalized;
145         if (!value)
146                 return NULL;
148         if (type == T_RAW)
149                 normalized = xstrdup(value);
150         else {
151                 normalized = xmalloc(64);
152                 if (type == T_INT) {
153                         int v = git_config_int(key, value);
154                         sprintf(normalized, "%d", v);
155                 }
156                 else if (type == T_BOOL)
157                         sprintf(normalized, "%s",
158                                 git_config_bool(key, value) ? "true" : "false");
159         }
161         return normalized;
164 static int get_color_found;
165 static const char *get_color_slot;
166 static char parsed_color[COLOR_MAXLEN];
168 static int git_get_color_config(const char *var, const char *value)
170         if (!strcmp(var, get_color_slot)) {
171                 if (!value)
172                         config_error_nonbool(var);
173                 color_parse(value, var, parsed_color);
174                 get_color_found = 1;
175         }
176         return 0;
179 static int get_color(int argc, const char **argv)
181         /*
182          * grab the color setting for the given slot from the configuration,
183          * or parse the default value if missing, and return ANSI color
184          * escape sequence.
185          *
186          * e.g.
187          * git config --get-color color.diff.whitespace "blue reverse"
188          */
189         const char *def_color = NULL;
191         switch (argc) {
192         default:
193                 usage(git_config_set_usage);
194         case 2:
195                 def_color = argv[1];
196                 /* fallthru */
197         case 1:
198                 get_color_slot = argv[0];
199                 break;
200         }
202         get_color_found = 0;
203         parsed_color[0] = '\0';
204         git_config(git_get_color_config);
206         if (!get_color_found && def_color)
207                 color_parse(def_color, "command line", parsed_color);
209         fputs(parsed_color, stdout);
210         return 0;
213 static int stdout_is_tty;
214 static int get_colorbool_found;
215 static int get_diff_color_found;
216 static int git_get_colorbool_config(const char *var, const char *value)
218         if (!strcmp(var, get_color_slot)) {
219                 get_colorbool_found =
220                         git_config_colorbool(var, value, stdout_is_tty);
221         }
222         if (!strcmp(var, "diff.color")) {
223                 get_diff_color_found =
224                         git_config_colorbool(var, value, stdout_is_tty);
225         }
226         return 0;
229 static int get_colorbool(int argc, const char **argv)
231         /*
232          * git config --get-colorbool <slot> [<stdout-is-tty>]
233          *
234          * returns "true" or "false" depending on how <slot>
235          * is configured.
236          */
238         if (argc == 2)
239                 stdout_is_tty = git_config_bool("command line", argv[1]);
240         else if (argc == 1)
241                 stdout_is_tty = isatty(1);
242         else
243                 usage(git_config_set_usage);
244         get_colorbool_found = -1;
245         get_diff_color_found = -1;
246         get_color_slot = argv[0];
247         git_config(git_get_colorbool_config);
249         if (get_colorbool_found < 0) {
250                 if (!strcmp(get_color_slot, "color.diff"))
251                         get_colorbool_found = get_diff_color_found;
252                 if (get_colorbool_found < 0)
253                         get_colorbool_found = 0;
254         }
256         if (argc == 1) {
257                 return get_colorbool_found ? 0 : 1;
258         } else {
259                 printf("%s\n", get_colorbool_found ? "true" : "false");
260                 return 0;
261         }
264 int cmd_config(int argc, const char **argv, const char *prefix)
266         int nongit = 0;
267         char* value;
268         const char *file = setup_git_directory_gently(&nongit);
270         while (1 < argc) {
271                 if (!strcmp(argv[1], "--int"))
272                         type = T_INT;
273                 else if (!strcmp(argv[1], "--bool"))
274                         type = T_BOOL;
275                 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
276                         if (argc != 2)
277                                 usage(git_config_set_usage);
278                         if (git_config(show_all_config) < 0 && file && errno)
279                                 die("unable to read config file %s: %s", file,
280                                     strerror(errno));
281                         return 0;
282                 }
283                 else if (!strcmp(argv[1], "--global")) {
284                         char *home = getenv("HOME");
285                         if (home) {
286                                 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
287                                 setenv(CONFIG_ENVIRONMENT, user_config, 1);
288                                 free(user_config);
289                         } else {
290                                 die("$HOME not set");
291                         }
292                 }
293                 else if (!strcmp(argv[1], "--system"))
294                         setenv(CONFIG_ENVIRONMENT, git_etc_gitconfig(), 1);
295                 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
296                         if (argc < 3)
297                                 usage(git_config_set_usage);
298                         if (!is_absolute_path(argv[2]) && file)
299                                 file = prefix_filename(file, strlen(file),
300                                                        argv[2]);
301                         else
302                                 file = argv[2];
303                         setenv(CONFIG_ENVIRONMENT, file, 1);
304                         argc--;
305                         argv++;
306                 }
307                 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
308                         term = '\0';
309                         delim = '\n';
310                         key_delim = '\n';
311                 }
312                 else if (!strcmp(argv[1], "--rename-section")) {
313                         int ret;
314                         if (argc != 4)
315                                 usage(git_config_set_usage);
316                         ret = git_config_rename_section(argv[2], argv[3]);
317                         if (ret < 0)
318                                 return ret;
319                         if (ret == 0) {
320                                 fprintf(stderr, "No such section!\n");
321                                 return 1;
322                         }
323                         return 0;
324                 }
325                 else if (!strcmp(argv[1], "--remove-section")) {
326                         int ret;
327                         if (argc != 3)
328                                 usage(git_config_set_usage);
329                         ret = git_config_rename_section(argv[2], NULL);
330                         if (ret < 0)
331                                 return ret;
332                         if (ret == 0) {
333                                 fprintf(stderr, "No such section!\n");
334                                 return 1;
335                         }
336                         return 0;
337                 } else if (!strcmp(argv[1], "--get-color")) {
338                         return get_color(argc-2, argv+2);
339                 } else if (!strcmp(argv[1], "--get-colorbool")) {
340                         return get_colorbool(argc-2, argv+2);
341                 } else
342                         break;
343                 argc--;
344                 argv++;
345         }
347         switch (argc) {
348         case 2:
349                 return get_value(argv[1], NULL);
350         case 3:
351                 if (!strcmp(argv[1], "--unset"))
352                         return git_config_set(argv[2], NULL);
353                 else if (!strcmp(argv[1], "--unset-all"))
354                         return git_config_set_multivar(argv[2], NULL, NULL, 1);
355                 else if (!strcmp(argv[1], "--get"))
356                         return get_value(argv[2], NULL);
357                 else if (!strcmp(argv[1], "--get-all")) {
358                         do_all = 1;
359                         return get_value(argv[2], NULL);
360                 } else if (!strcmp(argv[1], "--get-regexp")) {
361                         show_keys = 1;
362                         use_key_regexp = 1;
363                         do_all = 1;
364                         return get_value(argv[2], NULL);
365                 } else {
366                         value = normalize_value(argv[1], argv[2]);
367                         return git_config_set(argv[1], value);
368                 }
369         case 4:
370                 if (!strcmp(argv[1], "--unset"))
371                         return git_config_set_multivar(argv[2], NULL, argv[3], 0);
372                 else if (!strcmp(argv[1], "--unset-all"))
373                         return git_config_set_multivar(argv[2], NULL, argv[3], 1);
374                 else if (!strcmp(argv[1], "--get"))
375                         return get_value(argv[2], argv[3]);
376                 else if (!strcmp(argv[1], "--get-all")) {
377                         do_all = 1;
378                         return get_value(argv[2], argv[3]);
379                 } else if (!strcmp(argv[1], "--get-regexp")) {
380                         show_keys = 1;
381                         use_key_regexp = 1;
382                         do_all = 1;
383                         return get_value(argv[2], argv[3]);
384                 } else if (!strcmp(argv[1], "--add")) {
385                         value = normalize_value(argv[2], argv[3]);
386                         return git_config_set_multivar(argv[2], value, "^$", 0);
387                 } else if (!strcmp(argv[1], "--replace-all")) {
388                         value = normalize_value(argv[2], argv[3]);
389                         return git_config_set_multivar(argv[2], value, NULL, 1);
390                 } else {
391                         value = normalize_value(argv[1], argv[2]);
392                         return git_config_set_multivar(argv[1], value, argv[3], 0);
393                 }
394         case 5:
395                 if (!strcmp(argv[1], "--replace-all")) {
396                         value = normalize_value(argv[2], argv[3]);
397                         return git_config_set_multivar(argv[2], value, argv[4], 1);
398                 }
399         case 1:
400         default:
401                 usage(git_config_set_usage);
402         }
403         return 0;