config: Add new option to open an editor.
[git/git.git] / builtin-config.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "color.h"
4
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 ]";
7
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;
20
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 }
29
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;
35
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;
43
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);
73
74 return 0;
75 }
76
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;
83
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 }
93
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);
99
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 }
107
108 if (regex_) {
109 if (regex_[0] == '!') {
110 do_not_match = 1;
111 regex_++;
112 }
113
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 }
120
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);
130
131 free(key);
132 if (regexp) {
133 regfree(regexp);
134 free(regexp);
135 }
136
137 if (do_all)
138 ret = !seen;
139 else
140 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
141
142 free_strings:
143 free(repo_config);
144 free(global);
145 return ret;
146 }
147
148 static char *normalize_value(const char *key, const char *value)
149 {
150 char *normalized;
151
152 if (!value)
153 return NULL;
154
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 }
175
176 return normalized;
177 }
178
179 static int get_color_found;
180 static const char *get_color_slot;
181 static char parsed_color[COLOR_MAXLEN];
182
183 static int git_get_color_config(const char *var, const char *value, void *cb)
184 {
185 if (!strcmp(var, get_color_slot)) {
186 if (!value)
187 config_error_nonbool(var);
188 color_parse(value, var, parsed_color);
189 get_color_found = 1;
190 }
191 return 0;
192 }
193
194 static int get_color(int argc, const char **argv)
195 {
196 /*
197 * grab the color setting for the given slot from the configuration,
198 * or parse the default value if missing, and return ANSI color
199 * escape sequence.
200 *
201 * e.g.
202 * git config --get-color color.diff.whitespace "blue reverse"
203 */
204 const char *def_color = NULL;
205
206 switch (argc) {
207 default:
208 usage(git_config_set_usage);
209 case 2:
210 def_color = argv[1];
211 /* fallthru */
212 case 1:
213 get_color_slot = argv[0];
214 break;
215 }
216
217 get_color_found = 0;
218 parsed_color[0] = '\0';
219 git_config(git_get_color_config, NULL);
220
221 if (!get_color_found && def_color)
222 color_parse(def_color, "command line", parsed_color);
223
224 fputs(parsed_color, stdout);
225 return 0;
226 }
227
228 static int stdout_is_tty;
229 static int get_colorbool_found;
230 static int get_diff_color_found;
231 static int git_get_colorbool_config(const char *var, const char *value,
232 void *cb)
233 {
234 if (!strcmp(var, get_color_slot)) {
235 get_colorbool_found =
236 git_config_colorbool(var, value, stdout_is_tty);
237 }
238 if (!strcmp(var, "diff.color")) {
239 get_diff_color_found =
240 git_config_colorbool(var, value, stdout_is_tty);
241 }
242 if (!strcmp(var, "color.ui")) {
243 git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
244 return 0;
245 }
246 return 0;
247 }
248
249 static int get_colorbool(int argc, const char **argv)
250 {
251 /*
252 * git config --get-colorbool <slot> [<stdout-is-tty>]
253 *
254 * returns "true" or "false" depending on how <slot>
255 * is configured.
256 */
257
258 if (argc == 2)
259 stdout_is_tty = git_config_bool("command line", argv[1]);
260 else if (argc == 1)
261 stdout_is_tty = isatty(1);
262 else
263 usage(git_config_set_usage);
264 get_colorbool_found = -1;
265 get_diff_color_found = -1;
266 get_color_slot = argv[0];
267 git_config(git_get_colorbool_config, NULL);
268
269 if (get_colorbool_found < 0) {
270 if (!strcmp(get_color_slot, "color.diff"))
271 get_colorbool_found = get_diff_color_found;
272 if (get_colorbool_found < 0)
273 get_colorbool_found = git_use_color_default;
274 }
275
276 if (argc == 1) {
277 return get_colorbool_found ? 0 : 1;
278 } else {
279 printf("%s\n", get_colorbool_found ? "true" : "false");
280 return 0;
281 }
282 }
283
284 int cmd_config(int argc, const char **argv, const char *prefix)
285 {
286 int nongit;
287 char* value;
288 const char *file = setup_git_directory_gently(&nongit);
289
290 config_exclusive_filename = getenv(CONFIG_ENVIRONMENT);
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;
297 else if (!strcmp(argv[1], "--bool-or-int"))
298 type = T_BOOL_OR_INT;
299 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
300 if (argc != 2)
301 usage(git_config_set_usage);
302 if (git_config(show_all_config, NULL) < 0 &&
303 file && errno)
304 die("unable to read config file %s: %s", file,
305 strerror(errno));
306 return 0;
307 }
308 else if (!strcmp(argv[1], "--global")) {
309 char *home = getenv("HOME");
310 if (home) {
311 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
312 config_exclusive_filename = user_config;
313 } else {
314 die("$HOME not set");
315 }
316 }
317 else if (!strcmp(argv[1], "--system"))
318 config_exclusive_filename = git_etc_gitconfig();
319 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
320 if (argc < 3)
321 usage(git_config_set_usage);
322 if (!is_absolute_path(argv[2]) && file)
323 file = prefix_filename(file, strlen(file),
324 argv[2]);
325 else
326 file = argv[2];
327 config_exclusive_filename = file;
328 argc--;
329 argv++;
330 }
331 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
332 term = '\0';
333 delim = '\n';
334 key_delim = '\n';
335 }
336 else if (!strcmp(argv[1], "--rename-section")) {
337 int ret;
338 if (argc != 4)
339 usage(git_config_set_usage);
340 ret = git_config_rename_section(argv[2], argv[3]);
341 if (ret < 0)
342 return ret;
343 if (ret == 0) {
344 fprintf(stderr, "No such section!\n");
345 return 1;
346 }
347 return 0;
348 }
349 else if (!strcmp(argv[1], "--remove-section")) {
350 int ret;
351 if (argc != 3)
352 usage(git_config_set_usage);
353 ret = git_config_rename_section(argv[2], NULL);
354 if (ret < 0)
355 return ret;
356 if (ret == 0) {
357 fprintf(stderr, "No such section!\n");
358 return 1;
359 }
360 return 0;
361 } else if (!strcmp(argv[1], "--get-color")) {
362 return get_color(argc-2, argv+2);
363 } else if (!strcmp(argv[1], "--get-colorbool")) {
364 return get_colorbool(argc-2, argv+2);
365 } else if (!strcmp(argv[1], "--edit") || !strcmp(argv[1], "-e")) {
366 const char *config_filename;
367 if (argc != 2)
368 usage(git_config_set_usage);
369 if (config_exclusive_filename)
370 config_filename = config_exclusive_filename;
371 else
372 config_filename = git_path("config");
373 git_config(git_default_config, NULL);
374 launch_editor(config_filename, NULL, NULL);
375 return 0;
376 } else
377 break;
378 argc--;
379 argv++;
380 }
381
382 switch (argc) {
383 case 2:
384 return get_value(argv[1], NULL);
385 case 3:
386 if (!strcmp(argv[1], "--unset"))
387 return git_config_set(argv[2], NULL);
388 else if (!strcmp(argv[1], "--unset-all"))
389 return git_config_set_multivar(argv[2], NULL, NULL, 1);
390 else if (!strcmp(argv[1], "--get"))
391 return get_value(argv[2], NULL);
392 else if (!strcmp(argv[1], "--get-all")) {
393 do_all = 1;
394 return get_value(argv[2], NULL);
395 } else if (!strcmp(argv[1], "--get-regexp")) {
396 show_keys = 1;
397 use_key_regexp = 1;
398 do_all = 1;
399 return get_value(argv[2], NULL);
400 } else {
401 value = normalize_value(argv[1], argv[2]);
402 return git_config_set(argv[1], value);
403 }
404 case 4:
405 if (!strcmp(argv[1], "--unset"))
406 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
407 else if (!strcmp(argv[1], "--unset-all"))
408 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
409 else if (!strcmp(argv[1], "--get"))
410 return get_value(argv[2], argv[3]);
411 else if (!strcmp(argv[1], "--get-all")) {
412 do_all = 1;
413 return get_value(argv[2], argv[3]);
414 } else if (!strcmp(argv[1], "--get-regexp")) {
415 show_keys = 1;
416 use_key_regexp = 1;
417 do_all = 1;
418 return get_value(argv[2], argv[3]);
419 } else if (!strcmp(argv[1], "--add")) {
420 value = normalize_value(argv[2], argv[3]);
421 return git_config_set_multivar(argv[2], value, "^$", 0);
422 } else if (!strcmp(argv[1], "--replace-all")) {
423 value = normalize_value(argv[2], argv[3]);
424 return git_config_set_multivar(argv[2], value, NULL, 1);
425 } else {
426 value = normalize_value(argv[1], argv[2]);
427 return git_config_set_multivar(argv[1], value, argv[3], 0);
428 }
429 case 5:
430 if (!strcmp(argv[1], "--replace-all")) {
431 value = normalize_value(argv[2], argv[3]);
432 return git_config_set_multivar(argv[2], value, argv[4], 1);
433 }
434 case 1:
435 default:
436 usage(git_config_set_usage);
437 }
438 return 0;
439 }