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"));
83 global
= xstrdup(mkpath("%s/.gitconfig", home
));
84 system_wide
= git_etc_gitconfig();
88 for (tl
=key
+strlen(key
)-1; tl
>= key
&& *tl
!= '.'; --tl
)
90 for (tl
=key
; *tl
&& *tl
!= '.'; ++tl
)
94 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
95 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
96 fprintf(stderr
, "Invalid key pattern: %s\n", key_
);
102 if (regex_
[0] == '!') {
107 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
108 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
109 fprintf(stderr
, "Invalid pattern: %s\n", regex_
);
114 if (do_all
&& system_wide
)
115 git_config_from_file(show_config
, system_wide
);
116 if (do_all
&& global
)
117 git_config_from_file(show_config
, global
);
118 git_config_from_file(show_config
, local
);
119 if (!do_all
&& !seen
&& global
)
120 git_config_from_file(show_config
, global
);
121 if (!do_all
&& !seen
&& system_wide
)
122 git_config_from_file(show_config
, system_wide
);
133 ret
= (seen
== 1) ? 0 : seen
> 1 ? 2 : 1;
141 char *normalize_value(const char *key
, const char *value
)
149 normalized
= xstrdup(value
);
151 normalized
= xmalloc(64);
153 int v
= git_config_int(key
, value
);
154 sprintf(normalized
, "%d", v
);
156 else if (type
== T_BOOL
)
157 sprintf(normalized
, "%s",
158 git_config_bool(key
, value
) ? "true" : "false");
164 static int get_color_found
;
165 static const char *get_color_slot
;
166 static char parsed_color
[COLOR_MAXLEN
];
168 static int git_get_color_config(const char *var
, const char *value
)
170 if (!strcmp(var
, get_color_slot
)) {
171 color_parse(value
, var
, parsed_color
);
177 static int get_color(int argc
, const char **argv
)
180 * grab the color setting for the given slot from the configuration,
181 * or parse the default value if missing, and return ANSI color
185 * git config --get-color color.diff.whitespace "blue reverse"
187 const char *def_color
= NULL
;
191 usage(git_config_set_usage
);
196 get_color_slot
= argv
[0];
201 parsed_color
[0] = '\0';
202 git_config(git_get_color_config
);
204 if (!get_color_found
&& def_color
)
205 color_parse(def_color
, "command line", parsed_color
);
207 fputs(parsed_color
, stdout
);
211 static int stdout_is_tty
;
212 static int get_colorbool_found
;
213 static int get_diff_color_found
;
214 static int git_get_colorbool_config(const char *var
, const char *value
)
216 if (!strcmp(var
, get_color_slot
)) {
217 get_colorbool_found
=
218 git_config_colorbool(var
, value
, stdout_is_tty
);
220 if (!strcmp(var
, "diff.color")) {
221 get_diff_color_found
=
222 git_config_colorbool(var
, value
, stdout_is_tty
);
227 static int get_colorbool(int argc
, const char **argv
)
230 * git config --get-colorbool <slot> [<stdout-is-tty>]
232 * returns "true" or "false" depending on how <slot>
237 stdout_is_tty
= git_config_bool("command line", argv
[1]);
239 stdout_is_tty
= isatty(1);
241 usage(git_config_set_usage
);
242 get_colorbool_found
= -1;
243 get_diff_color_found
= -1;
244 get_color_slot
= argv
[0];
245 git_config(git_get_colorbool_config
);
247 if (get_colorbool_found
< 0) {
248 if (!strcmp(get_color_slot
, "color.diff"))
249 get_colorbool_found
= get_diff_color_found
;
250 if (get_colorbool_found
< 0)
251 get_colorbool_found
= 0;
255 return get_colorbool_found
? 0 : 1;
257 printf("%s\n", get_colorbool_found
? "true" : "false");
262 int cmd_config(int argc
, const char **argv
, const char *prefix
)
266 const char *file
= setup_git_directory_gently(&nongit
);
269 if (!strcmp(argv
[1], "--int"))
271 else if (!strcmp(argv
[1], "--bool"))
273 else if (!strcmp(argv
[1], "--list") || !strcmp(argv
[1], "-l")) {
275 usage(git_config_set_usage
);
276 if (git_config(show_all_config
) < 0 && file
&& errno
)
277 die("unable to read config file %s: %s", file
,
281 else if (!strcmp(argv
[1], "--global")) {
282 char *home
= getenv("HOME");
284 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
285 setenv(CONFIG_ENVIRONMENT
, user_config
, 1);
288 die("$HOME not set");
291 else if (!strcmp(argv
[1], "--system"))
292 setenv(CONFIG_ENVIRONMENT
, git_etc_gitconfig(), 1);
293 else if (!strcmp(argv
[1], "--file") || !strcmp(argv
[1], "-f")) {
295 usage(git_config_set_usage
);
296 if (!is_absolute_path(argv
[2]) && file
)
297 file
= prefix_filename(file
, strlen(file
),
301 setenv(CONFIG_ENVIRONMENT
, file
, 1);
305 else if (!strcmp(argv
[1], "--null") || !strcmp(argv
[1], "-z")) {
310 else if (!strcmp(argv
[1], "--rename-section")) {
313 usage(git_config_set_usage
);
314 ret
= git_config_rename_section(argv
[2], argv
[3]);
318 fprintf(stderr
, "No such section!\n");
323 else if (!strcmp(argv
[1], "--remove-section")) {
326 usage(git_config_set_usage
);
327 ret
= git_config_rename_section(argv
[2], NULL
);
331 fprintf(stderr
, "No such section!\n");
335 } else if (!strcmp(argv
[1], "--get-color")) {
336 return get_color(argc
-2, argv
+2);
337 } else if (!strcmp(argv
[1], "--get-colorbool")) {
338 return get_colorbool(argc
-2, argv
+2);
347 return get_value(argv
[1], NULL
);
349 if (!strcmp(argv
[1], "--unset"))
350 return git_config_set(argv
[2], NULL
);
351 else if (!strcmp(argv
[1], "--unset-all"))
352 return git_config_set_multivar(argv
[2], NULL
, NULL
, 1);
353 else if (!strcmp(argv
[1], "--get"))
354 return get_value(argv
[2], NULL
);
355 else if (!strcmp(argv
[1], "--get-all")) {
357 return get_value(argv
[2], NULL
);
358 } else if (!strcmp(argv
[1], "--get-regexp")) {
362 return get_value(argv
[2], NULL
);
364 value
= normalize_value(argv
[1], argv
[2]);
365 return git_config_set(argv
[1], value
);
368 if (!strcmp(argv
[1], "--unset"))
369 return git_config_set_multivar(argv
[2], NULL
, argv
[3], 0);
370 else if (!strcmp(argv
[1], "--unset-all"))
371 return git_config_set_multivar(argv
[2], NULL
, argv
[3], 1);
372 else if (!strcmp(argv
[1], "--get"))
373 return get_value(argv
[2], argv
[3]);
374 else if (!strcmp(argv
[1], "--get-all")) {
376 return get_value(argv
[2], argv
[3]);
377 } else if (!strcmp(argv
[1], "--get-regexp")) {
381 return get_value(argv
[2], argv
[3]);
382 } else if (!strcmp(argv
[1], "--add")) {
383 value
= normalize_value(argv
[2], argv
[3]);
384 return git_config_set_multivar(argv
[2], value
, "^$", 0);
385 } else if (!strcmp(argv
[1], "--replace-all")) {
386 value
= normalize_value(argv
[2], argv
[3]);
387 return git_config_set_multivar(argv
[2], value
, NULL
, 1);
389 value
= normalize_value(argv
[1], argv
[2]);
390 return git_config_set_multivar(argv
[1], value
, argv
[3], 0);
393 if (!strcmp(argv
[1], "--replace-all")) {
394 value
= normalize_value(argv
[2], argv
[3]);
395 return git_config_set_multivar(argv
[2], value
, argv
[4], 1);
399 usage(git_config_set_usage
);