Provide git_config with a callback-data parameter
[git/git.git] / builtin-config.c
CommitLineData
e12c095a 1#include "builtin.h"
1b1e59c5 2#include "cache.h"
9ce03522 3#include "color.h"
1b1e59c5
JS
4
5static const char git_config_set_usage[] =
c35b0b58 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]";
4ddba79d 7
96f1e58f
DR
8static char *key;
9static regex_t *key_regexp;
10static regex_t *regexp;
11static int show_keys;
12static int use_key_regexp;
13static int do_all;
14static int do_not_match;
15static int seen;
2275d502
FL
16static char delim = '=';
17static char key_delim = ' ';
18static char term = '\n';
c35b0b58 19static enum { T_RAW, T_INT, T_BOOL, T_BOOL_OR_INT } type = T_RAW;
4ddba79d 20
ef90d6d4 21static int show_all_config(const char *key_, const char *value_, void *cb)
de791f15
PB
22{
23 if (value_)
2275d502 24 printf("%s%c%s%c", key_, delim, value_, term);
de791f15 25 else
2275d502 26 printf("%s%c", key_, term);
de791f15
PB
27 return 0;
28}
29
ef90d6d4 30static int show_config(const char* key_, const char* value_, void *cb)
4ddba79d 31{
e098c6f8
JH
32 char value[256];
33 const char *vptr = value;
8f5ff31f 34 int dup_error = 0;
e098c6f8 35
8f5ff31f
JS
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 &&
9e4bbeb9 41 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
8f5ff31f
JS
42 return 0;
43
b69ba460
FL
44 if (show_keys) {
45 if (value_)
2275d502 46 printf("%s%c", key_, key_delim);
b69ba460
FL
47 else
48 printf("%s", key_);
49 }
8f5ff31f
JS
50 if (seen && !do_all)
51 dup_error = 1;
52 if (type == T_INT)
acb70149 53 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
8f5ff31f
JS
54 else if (type == T_BOOL)
55 vptr = git_config_bool(key_, value_) ? "true" : "false";
c35b0b58
JH
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 }
8f5ff31f 64 else
acb70149 65 vptr = value_?value_:"";
8f5ff31f
JS
66 seen++;
67 if (dup_error) {
68 error("More than one value for the key %s: %s",
69 key_, vptr);
4ddba79d 70 }
8f5ff31f 71 else
2275d502 72 printf("%s%c", vptr, term);
8f5ff31f 73
4ddba79d
JS
74 return 0;
75}
76
77static int get_value(const char* key_, const char* regex_)
78{
5f1a63e0 79 int ret = -1;
d14f7764 80 char *tl;
5f1a63e0 81 char *global = NULL, *repo_config = NULL;
32043c9f 82 const char *system_wide = NULL, *local;
5f1a63e0 83
d4ebc36c 84 local = getenv(CONFIG_ENVIRONMENT);
5f1a63e0
JS
85 if (!local) {
86 const char *home = getenv("HOME");
d4ebc36c 87 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
5f1a63e0 88 if (!local)
9befac47 89 local = repo_config = xstrdup(git_path("config"));
ab88c363 90 if (git_config_global() && home)
9befac47 91 global = xstrdup(mkpath("%s/.gitconfig", home));
ab88c363
JK
92 if (git_config_system())
93 system_wide = git_etc_gitconfig();
5f1a63e0 94 }
4ddba79d 95
9befac47 96 key = xstrdup(key_);
d14f7764
LT
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);
4ddba79d 101
2fa9a0fb 102 if (use_key_regexp) {
2d7320d0 103 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 104 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 105 fprintf(stderr, "Invalid key pattern: %s\n", key_);
5f1a63e0 106 goto free_strings;
2fa9a0fb
JS
107 }
108 }
109
4ddba79d 110 if (regex_) {
f98d863d
JS
111 if (regex_[0] == '!') {
112 do_not_match = 1;
113 regex_++;
114 }
115
2d7320d0 116 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 117 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d 118 fprintf(stderr, "Invalid pattern: %s\n", regex_);
5f1a63e0 119 goto free_strings;
4ddba79d
JS
120 }
121 }
122
32043c9f 123 if (do_all && system_wide)
ef90d6d4 124 git_config_from_file(show_config, system_wide, NULL);
5f1a63e0 125 if (do_all && global)
ef90d6d4
JS
126 git_config_from_file(show_config, global, NULL);
127 git_config_from_file(show_config, local, NULL);
5f1a63e0 128 if (!do_all && !seen && global)
ef90d6d4 129 git_config_from_file(show_config, global, NULL);
32043c9f 130 if (!do_all && !seen && system_wide)
ef90d6d4 131 git_config_from_file(show_config, system_wide, NULL);
5f1a63e0 132
4ddba79d 133 free(key);
0a152171
AW
134 if (regexp) {
135 regfree(regexp);
136 free(regexp);
4ddba79d
JS
137 }
138
139 if (do_all)
5f1a63e0
JS
140 ret = !seen;
141 else
dc2613de 142 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
5f1a63e0
JS
143
144free_strings:
4cac42b1
JH
145 free(repo_config);
146 free(global);
5f1a63e0 147 return ret;
4ddba79d 148}
1b1e59c5 149
db1696b8
FL
150char *normalize_value(const char *key, const char *value)
151{
152 char *normalized;
153
154 if (!value)
155 return NULL;
156
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);
164 }
165 else if (type == T_BOOL)
166 sprintf(normalized, "%s",
167 git_config_bool(key, value) ? "true" : "false");
c35b0b58
JH
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");
175 }
db1696b8
FL
176 }
177
178 return normalized;
179}
180
9ce03522
JH
181static int get_color_found;
182static const char *get_color_slot;
183static char parsed_color[COLOR_MAXLEN];
184
ef90d6d4 185static int git_get_color_config(const char *var, const char *value, void *cb)
9ce03522
JH
186{
187 if (!strcmp(var, get_color_slot)) {
f769982d
JH
188 if (!value)
189 config_error_nonbool(var);
9ce03522
JH
190 color_parse(value, var, parsed_color);
191 get_color_found = 1;
192 }
193 return 0;
194}
195
196static int get_color(int argc, const char **argv)
197{
198 /*
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.
202 *
203 * e.g.
204 * git config --get-color color.diff.whitespace "blue reverse"
205 */
206 const char *def_color = NULL;
207
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;
217 }
218
219 get_color_found = 0;
220 parsed_color[0] = '\0';
ef90d6d4 221 git_config(git_get_color_config, NULL);
9ce03522
JH
222
223 if (!get_color_found && def_color)
224 color_parse(def_color, "command line", parsed_color);
225
226 fputs(parsed_color, stdout);
227 return 0;
228}
229
0f6f5a40
JH
230static int stdout_is_tty;
231static int get_colorbool_found;
69243c2b 232static int get_diff_color_found;
ef90d6d4
JS
233static int git_get_colorbool_config(const char *var, const char *value,
234 void *cb)
0f6f5a40 235{
69243c2b 236 if (!strcmp(var, get_color_slot)) {
0f6f5a40
JH
237 get_colorbool_found =
238 git_config_colorbool(var, value, stdout_is_tty);
69243c2b
JH
239 }
240 if (!strcmp(var, "diff.color")) {
241 get_diff_color_found =
242 git_config_colorbool(var, value, stdout_is_tty);
243 }
4d4f5ba3
MK
244 if (!strcmp(var, "color.ui")) {
245 git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
246 return 0;
247 }
0f6f5a40
JH
248 return 0;
249}
250
251static int get_colorbool(int argc, const char **argv)
252{
253 /*
254 * git config --get-colorbool <slot> [<stdout-is-tty>]
255 *
256 * returns "true" or "false" depending on how <slot>
257 * is configured.
258 */
259
260 if (argc == 2)
261 stdout_is_tty = git_config_bool("command line", argv[1]);
262 else if (argc == 1)
263 stdout_is_tty = isatty(1);
264 else
265 usage(git_config_set_usage);
69243c2b
JH
266 get_colorbool_found = -1;
267 get_diff_color_found = -1;
0f6f5a40 268 get_color_slot = argv[0];
ef90d6d4 269 git_config(git_get_colorbool_config, NULL);
0f6f5a40 270
69243c2b
JH
271 if (get_colorbool_found < 0) {
272 if (!strcmp(get_color_slot, "color.diff"))
273 get_colorbool_found = get_diff_color_found;
274 if (get_colorbool_found < 0)
4d4f5ba3 275 get_colorbool_found = git_use_color_default;
69243c2b
JH
276 }
277
0f6f5a40
JH
278 if (argc == 1) {
279 return get_colorbool_found ? 0 : 1;
280 } else {
281 printf("%s\n", get_colorbool_found ? "true" : "false");
282 return 0;
283 }
284}
285
e0d10e1c 286int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 287{
af05d679 288 int nongit;
db1696b8 289 char* value;
1ae14a6b 290 const char *file = setup_git_directory_gently(&nongit);
7162dff3
PB
291
292 while (1 < argc) {
293 if (!strcmp(argv[1], "--int"))
294 type = T_INT;
295 else if (!strcmp(argv[1], "--bool"))
296 type = T_BOOL;
c35b0b58
JH
297 else if (!strcmp(argv[1], "--bool-or-int"))
298 type = T_BOOL_OR_INT;
a72c874e
FL
299 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
300 if (argc != 2)
301 usage(git_config_set_usage);
ef90d6d4
JS
302 if (git_config(show_all_config, NULL) < 0 &&
303 file && errno)
93a56c2c
GP
304 die("unable to read config file %s: %s", file,
305 strerror(errno));
306 return 0;
a72c874e 307 }
34eb3340
SE
308 else if (!strcmp(argv[1], "--global")) {
309 char *home = getenv("HOME");
310 if (home) {
311 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
7627943a 312 setenv(CONFIG_ENVIRONMENT, user_config, 1);
34eb3340
SE
313 free(user_config);
314 } else {
315 die("$HOME not set");
316 }
32043c9f
JS
317 }
318 else if (!strcmp(argv[1], "--system"))
506b17b1 319 setenv(CONFIG_ENVIRONMENT, git_etc_gitconfig(), 1);
67d454fe
AR
320 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
321 if (argc < 3)
322 usage(git_config_set_usage);
1ae14a6b
GP
323 if (!is_absolute_path(argv[2]) && file)
324 file = prefix_filename(file, strlen(file),
325 argv[2]);
326 else
327 file = argv[2];
328 setenv(CONFIG_ENVIRONMENT, file, 1);
67d454fe
AR
329 argc--;
330 argv++;
331 }
2275d502
FL
332 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
333 term = '\0';
334 delim = '\n';
335 key_delim = '\n';
336 }
32043c9f 337 else if (!strcmp(argv[1], "--rename-section")) {
0667fcfb
JS
338 int ret;
339 if (argc != 4)
340 usage(git_config_set_usage);
341 ret = git_config_rename_section(argv[2], argv[3]);
342 if (ret < 0)
343 return ret;
344 if (ret == 0) {
345 fprintf(stderr, "No such section!\n");
346 return 1;
347 }
348 return 0;
32043c9f 349 }
118f8b24
PB
350 else if (!strcmp(argv[1], "--remove-section")) {
351 int ret;
352 if (argc != 3)
353 usage(git_config_set_usage);
354 ret = git_config_rename_section(argv[2], NULL);
355 if (ret < 0)
356 return ret;
357 if (ret == 0) {
358 fprintf(stderr, "No such section!\n");
359 return 1;
360 }
361 return 0;
9ce03522
JH
362 } else if (!strcmp(argv[1], "--get-color")) {
363 return get_color(argc-2, argv+2);
0f6f5a40
JH
364 } else if (!strcmp(argv[1], "--get-colorbool")) {
365 return get_colorbool(argc-2, argv+2);
9ce03522 366 } else
7162dff3
PB
367 break;
368 argc--;
369 argv++;
370 }
371
1b1e59c5
JS
372 switch (argc) {
373 case 2:
4ddba79d 374 return get_value(argv[1], NULL);
1b1e59c5
JS
375 case 3:
376 if (!strcmp(argv[1], "--unset"))
377 return git_config_set(argv[2], NULL);
4ddba79d
JS
378 else if (!strcmp(argv[1], "--unset-all"))
379 return git_config_set_multivar(argv[2], NULL, NULL, 1);
380 else if (!strcmp(argv[1], "--get"))
381 return get_value(argv[2], NULL);
382 else if (!strcmp(argv[1], "--get-all")) {
383 do_all = 1;
384 return get_value(argv[2], NULL);
2fa9a0fb
JS
385 } else if (!strcmp(argv[1], "--get-regexp")) {
386 show_keys = 1;
387 use_key_regexp = 1;
388 do_all = 1;
389 return get_value(argv[2], NULL);
db1696b8
FL
390 } else {
391 value = normalize_value(argv[1], argv[2]);
392 return git_config_set(argv[1], value);
393 }
1b1e59c5
JS
394 case 4:
395 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
396 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
397 else if (!strcmp(argv[1], "--unset-all"))
398 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
399 else if (!strcmp(argv[1], "--get"))
400 return get_value(argv[2], argv[3]);
401 else if (!strcmp(argv[1], "--get-all")) {
402 do_all = 1;
403 return get_value(argv[2], argv[3]);
2fa9a0fb
JS
404 } else if (!strcmp(argv[1], "--get-regexp")) {
405 show_keys = 1;
406 use_key_regexp = 1;
407 do_all = 1;
408 return get_value(argv[2], argv[3]);
db1696b8
FL
409 } else if (!strcmp(argv[1], "--add")) {
410 value = normalize_value(argv[2], argv[3]);
411 return git_config_set_multivar(argv[2], value, "^$", 0);
412 } else if (!strcmp(argv[1], "--replace-all")) {
413 value = normalize_value(argv[2], argv[3]);
414 return git_config_set_multivar(argv[2], value, NULL, 1);
415 } else {
416 value = normalize_value(argv[1], argv[2]);
417 return git_config_set_multivar(argv[1], value, argv[3], 0);
418 }
4ddba79d 419 case 5:
db1696b8
FL
420 if (!strcmp(argv[1], "--replace-all")) {
421 value = normalize_value(argv[2], argv[3]);
422 return git_config_set_multivar(argv[2], value, argv[4], 1);
423 }
4ddba79d 424 case 1:
1b1e59c5
JS
425 default:
426 usage(git_config_set_usage);
427 }
428 return 0;
429}