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 ]";
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
= config_exclusive_filename
;
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();
95 for (tl
=key
+strlen(key
)-1; tl
>= key
&& *tl
!= '.'; --tl
)
97 for (tl
=key
; *tl
&& *tl
!= '.'; ++tl
)
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_
);
109 if (regex_
[0] == '!') {
114 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
115 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
116 fprintf(stderr
, "Invalid pattern: %s\n", regex_
);
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
);
140 ret
= (seen
== 1) ? 0 : seen
> 1 ? 2 : 1;
148 static char *normalize_value(const char *key
, const char *value
)
156 normalized
= xstrdup(value
);
158 normalized
= xmalloc(64);
160 int v
= git_config_int(key
, value
);
161 sprintf(normalized
, "%d", v
);
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
) {
168 v
= git_config_bool_or_int(key
, value
, &is_bool
);
170 sprintf(normalized
, "%d", v
);
172 sprintf(normalized
, "%s", v
? "true" : "false");
179 static int get_color_found
;
180 static const char *get_color_slot
;
181 static const char *get_colorbool_slot
;
182 static char parsed_color
[COLOR_MAXLEN
];
184 static int git_get_color_config(const char *var
, const char *value
, void *cb
)
186 if (!strcmp(var
, get_color_slot
)) {
188 config_error_nonbool(var
);
189 color_parse(value
, var
, parsed_color
);
195 static void get_color(const char *def_color
)
198 parsed_color
[0] = '\0';
199 git_config(git_get_color_config
, NULL
);
201 if (!get_color_found
&& def_color
)
202 color_parse(def_color
, "command line", parsed_color
);
204 fputs(parsed_color
, stdout
);
207 static int stdout_is_tty
;
208 static int get_colorbool_found
;
209 static int get_diff_color_found
;
210 static int git_get_colorbool_config(const char *var
, const char *value
,
213 if (!strcmp(var
, get_colorbool_slot
)) {
214 get_colorbool_found
=
215 git_config_colorbool(var
, value
, stdout_is_tty
);
217 if (!strcmp(var
, "diff.color")) {
218 get_diff_color_found
=
219 git_config_colorbool(var
, value
, stdout_is_tty
);
221 if (!strcmp(var
, "color.ui")) {
222 git_use_color_default
= git_config_colorbool(var
, value
, stdout_is_tty
);
228 static int get_colorbool(int print
)
230 get_colorbool_found
= -1;
231 get_diff_color_found
= -1;
232 git_config(git_get_colorbool_config
, NULL
);
234 if (get_colorbool_found
< 0) {
235 if (!strcmp(get_colorbool_slot
, "color.diff"))
236 get_colorbool_found
= get_diff_color_found
;
237 if (get_colorbool_found
< 0)
238 get_colorbool_found
= git_use_color_default
;
242 printf("%s\n", get_colorbool_found
? "true" : "false");
245 return get_colorbool_found
? 0 : 1;
248 int cmd_config(int argc
, const char **argv
, const char *unused_prefix
)
252 const char *prefix
= setup_git_directory_gently(&nongit
);
254 config_exclusive_filename
= getenv(CONFIG_ENVIRONMENT
);
257 if (!strcmp(argv
[1], "--int"))
259 else if (!strcmp(argv
[1], "--bool"))
261 else if (!strcmp(argv
[1], "--bool-or-int"))
262 type
= T_BOOL_OR_INT
;
263 else if (!strcmp(argv
[1], "--list") || !strcmp(argv
[1], "-l")) {
265 usage(git_config_set_usage
);
266 if (git_config(show_all_config
, NULL
) < 0) {
267 if (config_exclusive_filename
)
268 die("unable to read config file %s: %s",
269 config_exclusive_filename
, strerror(errno
));
271 die("error processing config file(s)");
275 else if (!strcmp(argv
[1], "--global")) {
276 char *home
= getenv("HOME");
278 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
279 config_exclusive_filename
= user_config
;
281 die("$HOME not set");
284 else if (!strcmp(argv
[1], "--system"))
285 config_exclusive_filename
= git_etc_gitconfig();
286 else if (!strcmp(argv
[1], "--file") || !strcmp(argv
[1], "-f")) {
288 usage(git_config_set_usage
);
289 if (!is_absolute_path(argv
[2]) && prefix
)
290 config_exclusive_filename
= prefix_filename(prefix
,
294 config_exclusive_filename
= argv
[2];
298 else if (!strcmp(argv
[1], "--null") || !strcmp(argv
[1], "-z")) {
303 else if (!strcmp(argv
[1], "--rename-section")) {
306 usage(git_config_set_usage
);
307 ret
= git_config_rename_section(argv
[2], argv
[3]);
311 fprintf(stderr
, "No such section!\n");
316 else if (!strcmp(argv
[1], "--remove-section")) {
319 usage(git_config_set_usage
);
320 ret
= git_config_rename_section(argv
[2], NULL
);
324 fprintf(stderr
, "No such section!\n");
328 } else if (!strcmp(argv
[1], "--get-color")) {
329 if (argc
> 4 || argc
< 3)
330 usage(git_config_set_usage
);
331 get_color_slot
= argv
[2];
334 } else if (!strcmp(argv
[1], "--get-colorbool")) {
336 stdout_is_tty
= git_config_bool("command line", argv
[3]);
338 stdout_is_tty
= isatty(1);
340 usage(git_config_set_usage
);
341 get_colorbool_slot
= argv
[2];
342 return get_colorbool(argc
!= 3);
343 } else if (!strcmp(argv
[1], "--edit") || !strcmp(argv
[1], "-e")) {
345 usage(git_config_set_usage
);
346 git_config(git_default_config
, NULL
);
347 launch_editor(config_exclusive_filename
?
348 config_exclusive_filename
: git_path("config"),
359 return get_value(argv
[1], NULL
);
361 if (!strcmp(argv
[1], "--unset"))
362 return git_config_set(argv
[2], NULL
);
363 else if (!strcmp(argv
[1], "--unset-all"))
364 return git_config_set_multivar(argv
[2], NULL
, NULL
, 1);
365 else if (!strcmp(argv
[1], "--get"))
366 return get_value(argv
[2], NULL
);
367 else if (!strcmp(argv
[1], "--get-all")) {
369 return get_value(argv
[2], NULL
);
370 } else if (!strcmp(argv
[1], "--get-regexp")) {
374 return get_value(argv
[2], NULL
);
376 value
= normalize_value(argv
[1], argv
[2]);
377 return git_config_set(argv
[1], value
);
380 if (!strcmp(argv
[1], "--unset"))
381 return git_config_set_multivar(argv
[2], NULL
, argv
[3], 0);
382 else if (!strcmp(argv
[1], "--unset-all"))
383 return git_config_set_multivar(argv
[2], NULL
, argv
[3], 1);
384 else if (!strcmp(argv
[1], "--get"))
385 return get_value(argv
[2], argv
[3]);
386 else if (!strcmp(argv
[1], "--get-all")) {
388 return get_value(argv
[2], argv
[3]);
389 } else if (!strcmp(argv
[1], "--get-regexp")) {
393 return get_value(argv
[2], argv
[3]);
394 } else if (!strcmp(argv
[1], "--add")) {
395 value
= normalize_value(argv
[2], argv
[3]);
396 return git_config_set_multivar(argv
[2], value
, "^$", 0);
397 } else if (!strcmp(argv
[1], "--replace-all")) {
398 value
= normalize_value(argv
[2], argv
[3]);
399 return git_config_set_multivar(argv
[2], value
, NULL
, 1);
401 value
= normalize_value(argv
[1], argv
[2]);
402 return git_config_set_multivar(argv
[1], value
, argv
[3], 0);
405 if (!strcmp(argv
[1], "--replace-all")) {
406 value
= normalize_value(argv
[2], argv
[3]);
407 return git_config_set_multivar(argv
[2], value
, argv
[4], 1);
411 usage(git_config_set_usage
);