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]";
9 static regex_t *key_regexp;
10 static regex_t *regexp;
12 static int use_key_regexp;
14 static int do_not_match;
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_)
24 printf("%s%c%s%c", key_, delim, value_, term);
26 printf("%s%c", key_, term);
30 static int show_config(const char* key_, const char* value_)
33 const char *vptr = value;
36 if (!use_key_regexp && strcmp(key_, key))
38 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
41 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
46 printf("%s%c", key_, key_delim);
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";
57 vptr = value_?value_:"";
60 error("More than one value for the key %s: %s",
64 printf("%s%c", vptr, term);
69 static int get_value(const char* key_, const char* regex_)
73 char *global = NULL, *repo_config = NULL;
74 const char *system_wide = NULL, *local;
76 local = getenv(CONFIG_ENVIRONMENT);
78 const char *home = getenv("HOME");
79 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
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();
89 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
91 for (tl=key; *tl && *tl != '.'; ++tl)
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_);
103 if (regex_[0] == '!') {
108 regexp = (regex_t*)xmalloc(sizeof(regex_t));
109 if (regcomp(regexp, regex_, REG_EXTENDED)) {
110 fprintf(stderr, "Invalid pattern: %s\n", regex_);
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);
134 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
142 char *normalize_value(const char *key, const char *value)
150 normalized = xstrdup(value);
152 normalized = xmalloc(64);
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");
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)) {
173 config_error_nonbool(var);
174 color_parse(value, var, parsed_color);
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
188 * git config --get-color color.diff.whitespace "blue reverse"
190 const char *def_color = NULL;
194 usage(git_config_set_usage);
199 get_color_slot = argv[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);
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 if (!strcmp(var, "color.ui")) {
228 git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
234 static int get_colorbool(int argc, const char **argv)
237 * git config --get-colorbool <slot> [<stdout-is-tty>]
239 * returns "true" or "false" depending on how <slot>
244 stdout_is_tty = git_config_bool("command line", argv[1]);
246 stdout_is_tty = isatty(1);
248 usage(git_config_set_usage);
249 get_colorbool_found = -1;
250 get_diff_color_found = -1;
251 get_color_slot = argv[0];
252 git_config(git_get_colorbool_config);
254 if (get_colorbool_found < 0) {
255 if (!strcmp(get_color_slot, "color.diff"))
256 get_colorbool_found = get_diff_color_found;
257 if (get_colorbool_found < 0)
258 get_colorbool_found = git_use_color_default;
262 return get_colorbool_found ? 0 : 1;
264 printf("%s\n", get_colorbool_found ? "true" : "false");
269 int cmd_config(int argc, const char **argv, const char *prefix)
273 const char *file = setup_git_directory_gently(&nongit);
276 if (!strcmp(argv[1], "--int"))
278 else if (!strcmp(argv[1], "--bool"))
280 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
282 usage(git_config_set_usage);
283 if (git_config(show_all_config) < 0 && file && errno)
284 die("unable to read config file %s: %s", file,
288 else if (!strcmp(argv[1], "--global")) {
289 char *home = getenv("HOME");
291 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
292 setenv(CONFIG_ENVIRONMENT, user_config, 1);
295 die("$HOME not set");
298 else if (!strcmp(argv[1], "--system"))
299 setenv(CONFIG_ENVIRONMENT, git_etc_gitconfig(), 1);
300 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
302 usage(git_config_set_usage);
303 if (!is_absolute_path(argv[2]) && file)
304 file = prefix_filename(file, strlen(file),
308 setenv(CONFIG_ENVIRONMENT, file, 1);
312 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
317 else if (!strcmp(argv[1], "--rename-section")) {
320 usage(git_config_set_usage);
321 ret = git_config_rename_section(argv[2], argv[3]);
325 fprintf(stderr, "No such section!\n");
330 else if (!strcmp(argv[1], "--remove-section")) {
333 usage(git_config_set_usage);
334 ret = git_config_rename_section(argv[2], NULL);
338 fprintf(stderr, "No such section!\n");
342 } else if (!strcmp(argv[1], "--get-color")) {
343 return get_color(argc-2, argv+2);
344 } else if (!strcmp(argv[1], "--get-colorbool")) {
345 return get_colorbool(argc-2, argv+2);
354 return get_value(argv[1], NULL);
356 if (!strcmp(argv[1], "--unset"))
357 return git_config_set(argv[2], NULL);
358 else if (!strcmp(argv[1], "--unset-all"))
359 return git_config_set_multivar(argv[2], NULL, NULL, 1);
360 else if (!strcmp(argv[1], "--get"))
361 return get_value(argv[2], NULL);
362 else if (!strcmp(argv[1], "--get-all")) {
364 return get_value(argv[2], NULL);
365 } else if (!strcmp(argv[1], "--get-regexp")) {
369 return get_value(argv[2], NULL);
371 value = normalize_value(argv[1], argv[2]);
372 return git_config_set(argv[1], value);
375 if (!strcmp(argv[1], "--unset"))
376 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
377 else if (!strcmp(argv[1], "--unset-all"))
378 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
379 else if (!strcmp(argv[1], "--get"))
380 return get_value(argv[2], argv[3]);
381 else if (!strcmp(argv[1], "--get-all")) {
383 return get_value(argv[2], argv[3]);
384 } else if (!strcmp(argv[1], "--get-regexp")) {
388 return get_value(argv[2], argv[3]);
389 } else if (!strcmp(argv[1], "--add")) {
390 value = normalize_value(argv[2], argv[3]);
391 return git_config_set_multivar(argv[2], value, "^$", 0);
392 } else if (!strcmp(argv[1], "--replace-all")) {
393 value = normalize_value(argv[2], argv[3]);
394 return git_config_set_multivar(argv[2], value, NULL, 1);
396 value = normalize_value(argv[1], argv[2]);
397 return git_config_set_multivar(argv[1], value, argv[3], 0);
400 if (!strcmp(argv[1], "--replace-all")) {
401 value = normalize_value(argv[2], argv[3]);
402 return git_config_set_multivar(argv[2], value, argv[4], 1);
406 usage(git_config_set_usage);