Merge branch 'maint'
[git/dscho.git] / builtin-config.c
blob10447a704f7b3a1fb3b3053463a50ae2609a5684
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]";
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_)
23 if (value_)
24 printf("%s%c%s%c", key_, delim, value_, term);
25 else
26 printf("%s%c", key_, term);
27 return 0;
30 static int show_config(const char* key_, const char* value_)
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_);
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);
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);
71 else
72 printf("%s%c", vptr, term);
74 return 0;
77 static int get_value(const char* key_, const char* regex_)
79 int ret = -1;
80 char *tl;
81 char *global = NULL, *repo_config = NULL;
82 const char *system_wide = NULL, *local;
84 local = getenv(CONFIG_ENVIRONMENT);
85 if (!local) {
86 const char *home = getenv("HOME");
87 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
88 if (!local)
89 local = repo_config = xstrdup(git_path("config"));
90 if (git_config_global() && home)
91 global = xstrdup(mkpath("%s/.gitconfig", home));
92 if (git_config_system())
93 system_wide = git_etc_gitconfig();
96 key = xstrdup(key_);
97 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
98 *tl = tolower(*tl);
99 for (tl=key; *tl && *tl != '.'; ++tl)
100 *tl = tolower(*tl);
102 if (use_key_regexp) {
103 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
104 if (regcomp(key_regexp, key, REG_EXTENDED)) {
105 fprintf(stderr, "Invalid key pattern: %s\n", key_);
106 goto free_strings;
110 if (regex_) {
111 if (regex_[0] == '!') {
112 do_not_match = 1;
113 regex_++;
116 regexp = (regex_t*)xmalloc(sizeof(regex_t));
117 if (regcomp(regexp, regex_, REG_EXTENDED)) {
118 fprintf(stderr, "Invalid pattern: %s\n", regex_);
119 goto free_strings;
123 if (do_all && system_wide)
124 git_config_from_file(show_config, system_wide);
125 if (do_all && global)
126 git_config_from_file(show_config, global);
127 git_config_from_file(show_config, local);
128 if (!do_all && !seen && global)
129 git_config_from_file(show_config, global);
130 if (!do_all && !seen && system_wide)
131 git_config_from_file(show_config, system_wide);
133 free(key);
134 if (regexp) {
135 regfree(regexp);
136 free(regexp);
139 if (do_all)
140 ret = !seen;
141 else
142 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
144 free_strings:
145 free(repo_config);
146 free(global);
147 return ret;
150 char *normalize_value(const char *key, const char *value)
152 char *normalized;
154 if (!value)
155 return NULL;
157 if (type == T_RAW)
158 normalized = xstrdup(value);
159 else {
160 normalized = xmalloc(64);
161 if (type == T_INT) {
162 int v = git_config_int(key, value);
163 sprintf(normalized, "%d", v);
165 else if (type == T_BOOL)
166 sprintf(normalized, "%s",
167 git_config_bool(key, value) ? "true" : "false");
168 else if (type == T_BOOL_OR_INT) {
169 int is_bool, v;
170 v = git_config_bool_or_int(key, value, &is_bool);
171 if (!is_bool)
172 sprintf(normalized, "%d", v);
173 else
174 sprintf(normalized, "%s", v ? "true" : "false");
178 return normalized;
181 static int get_color_found;
182 static const char *get_color_slot;
183 static char parsed_color[COLOR_MAXLEN];
185 static int git_get_color_config(const char *var, const char *value)
187 if (!strcmp(var, get_color_slot)) {
188 if (!value)
189 config_error_nonbool(var);
190 color_parse(value, var, parsed_color);
191 get_color_found = 1;
193 return 0;
196 static int get_color(int argc, const char **argv)
199 * grab the color setting for the given slot from the configuration,
200 * or parse the default value if missing, and return ANSI color
201 * escape sequence.
203 * e.g.
204 * git config --get-color color.diff.whitespace "blue reverse"
206 const char *def_color = NULL;
208 switch (argc) {
209 default:
210 usage(git_config_set_usage);
211 case 2:
212 def_color = argv[1];
213 /* fallthru */
214 case 1:
215 get_color_slot = argv[0];
216 break;
219 get_color_found = 0;
220 parsed_color[0] = '\0';
221 git_config(git_get_color_config);
223 if (!get_color_found && def_color)
224 color_parse(def_color, "command line", parsed_color);
226 fputs(parsed_color, stdout);
227 return 0;
230 static int stdout_is_tty;
231 static int get_colorbool_found;
232 static int get_diff_color_found;
233 static int git_get_colorbool_config(const char *var, const char *value)
235 if (!strcmp(var, get_color_slot)) {
236 get_colorbool_found =
237 git_config_colorbool(var, value, stdout_is_tty);
239 if (!strcmp(var, "diff.color")) {
240 get_diff_color_found =
241 git_config_colorbool(var, value, stdout_is_tty);
243 return 0;
246 static int get_colorbool(int argc, const char **argv)
249 * git config --get-colorbool <slot> [<stdout-is-tty>]
251 * returns "true" or "false" depending on how <slot>
252 * is configured.
255 if (argc == 2)
256 stdout_is_tty = git_config_bool("command line", argv[1]);
257 else if (argc == 1)
258 stdout_is_tty = isatty(1);
259 else
260 usage(git_config_set_usage);
261 get_colorbool_found = -1;
262 get_diff_color_found = -1;
263 get_color_slot = argv[0];
264 git_config(git_get_colorbool_config);
266 if (get_colorbool_found < 0) {
267 if (!strcmp(get_color_slot, "color.diff"))
268 get_colorbool_found = get_diff_color_found;
269 if (get_colorbool_found < 0)
270 get_colorbool_found = 0;
273 if (argc == 1) {
274 return get_colorbool_found ? 0 : 1;
275 } else {
276 printf("%s\n", get_colorbool_found ? "true" : "false");
277 return 0;
281 int cmd_config(int argc, const char **argv, const char *prefix)
283 int nongit;
284 char* value;
285 const char *file = setup_git_directory_gently(&nongit);
287 while (1 < argc) {
288 if (!strcmp(argv[1], "--int"))
289 type = T_INT;
290 else if (!strcmp(argv[1], "--bool"))
291 type = T_BOOL;
292 else if (!strcmp(argv[1], "--bool-or-int"))
293 type = T_BOOL_OR_INT;
294 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
295 if (argc != 2)
296 usage(git_config_set_usage);
297 if (git_config(show_all_config) < 0 && file && errno)
298 die("unable to read config file %s: %s", file,
299 strerror(errno));
300 return 0;
302 else if (!strcmp(argv[1], "--global")) {
303 char *home = getenv("HOME");
304 if (home) {
305 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
306 setenv(CONFIG_ENVIRONMENT, user_config, 1);
307 free(user_config);
308 } else {
309 die("$HOME not set");
312 else if (!strcmp(argv[1], "--system"))
313 setenv(CONFIG_ENVIRONMENT, git_etc_gitconfig(), 1);
314 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
315 if (argc < 3)
316 usage(git_config_set_usage);
317 if (!is_absolute_path(argv[2]) && file)
318 file = prefix_filename(file, strlen(file),
319 argv[2]);
320 else
321 file = argv[2];
322 setenv(CONFIG_ENVIRONMENT, file, 1);
323 argc--;
324 argv++;
326 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
327 term = '\0';
328 delim = '\n';
329 key_delim = '\n';
331 else if (!strcmp(argv[1], "--rename-section")) {
332 int ret;
333 if (argc != 4)
334 usage(git_config_set_usage);
335 ret = git_config_rename_section(argv[2], argv[3]);
336 if (ret < 0)
337 return ret;
338 if (ret == 0) {
339 fprintf(stderr, "No such section!\n");
340 return 1;
342 return 0;
344 else if (!strcmp(argv[1], "--remove-section")) {
345 int ret;
346 if (argc != 3)
347 usage(git_config_set_usage);
348 ret = git_config_rename_section(argv[2], NULL);
349 if (ret < 0)
350 return ret;
351 if (ret == 0) {
352 fprintf(stderr, "No such section!\n");
353 return 1;
355 return 0;
356 } else if (!strcmp(argv[1], "--get-color")) {
357 return get_color(argc-2, argv+2);
358 } else if (!strcmp(argv[1], "--get-colorbool")) {
359 return get_colorbool(argc-2, argv+2);
360 } else
361 break;
362 argc--;
363 argv++;
366 switch (argc) {
367 case 2:
368 return get_value(argv[1], NULL);
369 case 3:
370 if (!strcmp(argv[1], "--unset"))
371 return git_config_set(argv[2], NULL);
372 else if (!strcmp(argv[1], "--unset-all"))
373 return git_config_set_multivar(argv[2], NULL, NULL, 1);
374 else if (!strcmp(argv[1], "--get"))
375 return get_value(argv[2], NULL);
376 else if (!strcmp(argv[1], "--get-all")) {
377 do_all = 1;
378 return get_value(argv[2], NULL);
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], NULL);
384 } else {
385 value = normalize_value(argv[1], argv[2]);
386 return git_config_set(argv[1], value);
388 case 4:
389 if (!strcmp(argv[1], "--unset"))
390 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
391 else if (!strcmp(argv[1], "--unset-all"))
392 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
393 else if (!strcmp(argv[1], "--get"))
394 return get_value(argv[2], argv[3]);
395 else if (!strcmp(argv[1], "--get-all")) {
396 do_all = 1;
397 return get_value(argv[2], argv[3]);
398 } else if (!strcmp(argv[1], "--get-regexp")) {
399 show_keys = 1;
400 use_key_regexp = 1;
401 do_all = 1;
402 return get_value(argv[2], argv[3]);
403 } else if (!strcmp(argv[1], "--add")) {
404 value = normalize_value(argv[2], argv[3]);
405 return git_config_set_multivar(argv[2], value, "^$", 0);
406 } else if (!strcmp(argv[1], "--replace-all")) {
407 value = normalize_value(argv[2], argv[3]);
408 return git_config_set_multivar(argv[2], value, NULL, 1);
409 } else {
410 value = normalize_value(argv[1], argv[2]);
411 return git_config_set_multivar(argv[1], value, argv[3], 0);
413 case 5:
414 if (!strcmp(argv[1], "--replace-all")) {
415 value = normalize_value(argv[2], argv[3]);
416 return git_config_set_multivar(argv[2], value, argv[4], 1);
418 case 1:
419 default:
420 usage(git_config_set_usage);
422 return 0;