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]";
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
, T_BOOL_OR_INT
} type
= T_RAW
;
21 static int show_all_config(const char *key_
, const char *value_
, void *cb
)
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_
, void *cb
)
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";
56 else if (type
== T_BOOL_OR_INT
) {
58 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
60 vptr
= v
? "true" : "false";
62 sprintf(value
, "%d", v
);
65 vptr
= value_
?value_
:"";
68 error("More than one value for the key %s: %s",
72 printf("%s%c", vptr
, term
);
77 static int get_value(const char* key_
, const char* regex_
)
81 char *global
= NULL
, *repo_config
= NULL
;
82 const char *system_wide
= NULL
, *local
;
84 local
= getenv(CONFIG_ENVIRONMENT
);
86 const char *home
= getenv("HOME");
87 local
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
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();
97 for (tl
=key
+strlen(key
)-1; tl
>= key
&& *tl
!= '.'; --tl
)
99 for (tl
=key
; *tl
&& *tl
!= '.'; ++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_
);
111 if (regex_
[0] == '!') {
116 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
117 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
118 fprintf(stderr
, "Invalid pattern: %s\n", regex_
);
123 if (do_all
&& system_wide
)
124 git_config_from_file(show_config
, system_wide
, NULL
);
125 if (do_all
&& global
)
126 git_config_from_file(show_config
, global
, NULL
);
127 git_config_from_file(show_config
, local
, NULL
);
128 if (!do_all
&& !seen
&& global
)
129 git_config_from_file(show_config
, global
, NULL
);
130 if (!do_all
&& !seen
&& system_wide
)
131 git_config_from_file(show_config
, system_wide
, NULL
);
142 ret
= (seen
== 1) ? 0 : seen
> 1 ? 2 : 1;
150 char *normalize_value(const char *key
, const char *value
)
158 normalized
= xstrdup(value
);
160 normalized
= xmalloc(64);
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
) {
170 v
= git_config_bool_or_int(key
, value
, &is_bool
);
172 sprintf(normalized
, "%d", v
);
174 sprintf(normalized
, "%s", v
? "true" : "false");
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
, void *cb
)
187 if (!strcmp(var
, get_color_slot
)) {
189 config_error_nonbool(var
);
190 color_parse(value
, var
, parsed_color
);
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
204 * git config --get-color color.diff.whitespace "blue reverse"
206 const char *def_color
= NULL
;
210 usage(git_config_set_usage
);
215 get_color_slot
= argv
[0];
220 parsed_color
[0] = '\0';
221 git_config(git_get_color_config
, NULL
);
223 if (!get_color_found
&& def_color
)
224 color_parse(def_color
, "command line", parsed_color
);
226 fputs(parsed_color
, stdout
);
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
,
236 if (!strcmp(var
, get_color_slot
)) {
237 get_colorbool_found
=
238 git_config_colorbool(var
, value
, stdout_is_tty
);
240 if (!strcmp(var
, "diff.color")) {
241 get_diff_color_found
=
242 git_config_colorbool(var
, value
, stdout_is_tty
);
244 if (!strcmp(var
, "color.ui")) {
245 git_use_color_default
= git_config_colorbool(var
, value
, stdout_is_tty
);
251 static int get_colorbool(int argc
, const char **argv
)
254 * git config --get-colorbool <slot> [<stdout-is-tty>]
256 * returns "true" or "false" depending on how <slot>
261 stdout_is_tty
= git_config_bool("command line", argv
[1]);
263 stdout_is_tty
= isatty(1);
265 usage(git_config_set_usage
);
266 get_colorbool_found
= -1;
267 get_diff_color_found
= -1;
268 get_color_slot
= argv
[0];
269 git_config(git_get_colorbool_config
, NULL
);
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)
275 get_colorbool_found
= git_use_color_default
;
279 return get_colorbool_found
? 0 : 1;
281 printf("%s\n", get_colorbool_found
? "true" : "false");
286 int cmd_config(int argc
, const char **argv
, const char *prefix
)
290 const char *file
= setup_git_directory_gently(&nongit
);
293 if (!strcmp(argv
[1], "--int"))
295 else if (!strcmp(argv
[1], "--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")) {
301 usage(git_config_set_usage
);
302 if (git_config(show_all_config
, NULL
) < 0 &&
304 die("unable to read config file %s: %s", file
,
308 else if (!strcmp(argv
[1], "--global")) {
309 char *home
= getenv("HOME");
311 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
312 setenv(CONFIG_ENVIRONMENT
, user_config
, 1);
315 die("$HOME not set");
318 else if (!strcmp(argv
[1], "--system"))
319 setenv(CONFIG_ENVIRONMENT
, git_etc_gitconfig(), 1);
320 else if (!strcmp(argv
[1], "--file") || !strcmp(argv
[1], "-f")) {
322 usage(git_config_set_usage
);
323 if (!is_absolute_path(argv
[2]) && file
)
324 file
= prefix_filename(file
, strlen(file
),
328 setenv(CONFIG_ENVIRONMENT
, file
, 1);
332 else if (!strcmp(argv
[1], "--null") || !strcmp(argv
[1], "-z")) {
337 else if (!strcmp(argv
[1], "--rename-section")) {
340 usage(git_config_set_usage
);
341 ret
= git_config_rename_section(argv
[2], argv
[3]);
345 fprintf(stderr
, "No such section!\n");
350 else if (!strcmp(argv
[1], "--remove-section")) {
353 usage(git_config_set_usage
);
354 ret
= git_config_rename_section(argv
[2], NULL
);
358 fprintf(stderr
, "No such section!\n");
362 } else if (!strcmp(argv
[1], "--get-color")) {
363 return get_color(argc
-2, argv
+2);
364 } else if (!strcmp(argv
[1], "--get-colorbool")) {
365 return get_colorbool(argc
-2, argv
+2);
374 return get_value(argv
[1], NULL
);
376 if (!strcmp(argv
[1], "--unset"))
377 return git_config_set(argv
[2], NULL
);
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")) {
384 return get_value(argv
[2], NULL
);
385 } else if (!strcmp(argv
[1], "--get-regexp")) {
389 return get_value(argv
[2], NULL
);
391 value
= normalize_value(argv
[1], argv
[2]);
392 return git_config_set(argv
[1], value
);
395 if (!strcmp(argv
[1], "--unset"))
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")) {
403 return get_value(argv
[2], argv
[3]);
404 } else if (!strcmp(argv
[1], "--get-regexp")) {
408 return get_value(argv
[2], argv
[3]);
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);
416 value
= normalize_value(argv
[1], argv
[2]);
417 return git_config_set_multivar(argv
[1], value
, argv
[3], 0);
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);
426 usage(git_config_set_usage
);