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