4 static const char git_config_set_usage
[] =
5 "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";
8 static regex_t
*key_regexp
;
9 static regex_t
*regexp
;
11 static int use_key_regexp
;
13 static int do_not_match
;
15 static char delim
= '=';
16 static char key_delim
= ' ';
17 static char term
= '\n';
18 static enum { T_RAW
, T_INT
, T_BOOL
} type
= T_RAW
;
20 static int show_all_config(const char *key_
, const char *value_
)
23 printf("%s%c%s%c", key_
, delim
, value_
, term
);
25 printf("%s%c", key_
, term
);
29 static int show_config(const char* key_
, const char* value_
)
32 const char *vptr
= value
;
35 if (!use_key_regexp
&& strcmp(key_
, key
))
37 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
41 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
= 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 int cmd_config(int argc
, const char **argv
, const char *prefix
)
168 setup_git_directory_gently(&nongit
);
171 if (!strcmp(argv
[1], "--int"))
173 else if (!strcmp(argv
[1], "--bool"))
175 else if (!strcmp(argv
[1], "--list") || !strcmp(argv
[1], "-l")) {
177 usage(git_config_set_usage
);
178 return git_config(show_all_config
);
180 else if (!strcmp(argv
[1], "--global")) {
181 char *home
= getenv("HOME");
183 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
184 setenv(CONFIG_ENVIRONMENT
, user_config
, 1);
187 die("$HOME not set");
190 else if (!strcmp(argv
[1], "--system"))
191 setenv(CONFIG_ENVIRONMENT
, ETC_GITCONFIG
, 1);
192 else if (!strcmp(argv
[1], "--file") || !strcmp(argv
[1], "-f")) {
194 usage(git_config_set_usage
);
195 setenv(CONFIG_ENVIRONMENT
, argv
[2], 1);
199 else if (!strcmp(argv
[1], "--null") || !strcmp(argv
[1], "-z")) {
204 else if (!strcmp(argv
[1], "--rename-section")) {
207 usage(git_config_set_usage
);
208 ret
= git_config_rename_section(argv
[2], argv
[3]);
212 fprintf(stderr
, "No such section!\n");
217 else if (!strcmp(argv
[1], "--remove-section")) {
220 usage(git_config_set_usage
);
221 ret
= git_config_rename_section(argv
[2], NULL
);
225 fprintf(stderr
, "No such section!\n");
238 return get_value(argv
[1], NULL
);
240 if (!strcmp(argv
[1], "--unset"))
241 return git_config_set(argv
[2], NULL
);
242 else if (!strcmp(argv
[1], "--unset-all"))
243 return git_config_set_multivar(argv
[2], NULL
, NULL
, 1);
244 else if (!strcmp(argv
[1], "--get"))
245 return get_value(argv
[2], NULL
);
246 else if (!strcmp(argv
[1], "--get-all")) {
248 return get_value(argv
[2], NULL
);
249 } else if (!strcmp(argv
[1], "--get-regexp")) {
253 return get_value(argv
[2], NULL
);
255 value
= normalize_value(argv
[1], argv
[2]);
256 return git_config_set(argv
[1], value
);
259 if (!strcmp(argv
[1], "--unset"))
260 return git_config_set_multivar(argv
[2], NULL
, argv
[3], 0);
261 else if (!strcmp(argv
[1], "--unset-all"))
262 return git_config_set_multivar(argv
[2], NULL
, argv
[3], 1);
263 else if (!strcmp(argv
[1], "--get"))
264 return get_value(argv
[2], argv
[3]);
265 else if (!strcmp(argv
[1], "--get-all")) {
267 return get_value(argv
[2], argv
[3]);
268 } else if (!strcmp(argv
[1], "--get-regexp")) {
272 return get_value(argv
[2], argv
[3]);
273 } else if (!strcmp(argv
[1], "--add")) {
274 value
= normalize_value(argv
[2], argv
[3]);
275 return git_config_set_multivar(argv
[2], value
, "^$", 0);
276 } else if (!strcmp(argv
[1], "--replace-all")) {
277 value
= normalize_value(argv
[2], argv
[3]);
278 return git_config_set_multivar(argv
[2], value
, NULL
, 1);
280 value
= normalize_value(argv
[1], argv
[2]);
281 return git_config_set_multivar(argv
[1], value
, argv
[3], 0);
284 if (!strcmp(argv
[1], "--replace-all")) {
285 value
= normalize_value(argv
[2], argv
[3]);
286 return git_config_set_multivar(argv
[2], value
, argv
[4], 1);
290 usage(git_config_set_usage
);