4 static const char git_config_set_usage[] =
5 "git-config [ --global | --system ] [ --bool | --int ] [--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 enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
17 static int show_all_config(const char *key_, const char *value_)
20 printf("%s=%s\n", key_, value_);
26 static int show_config(const char* key_, const char* value_)
29 const char *vptr = value;
32 if (!use_key_regexp && strcmp(key_, key))
34 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
38 regexec(regexp, (value_?value_:""), 0, NULL, 0)))
50 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
51 else if (type == T_BOOL)
52 vptr = git_config_bool(key_, value_) ? "true" : "false";
54 vptr = value_?value_:"";
57 error("More than one value for the key %s: %s",
66 static int get_value(const char* key_, const char* regex_)
70 char *global = NULL, *repo_config = NULL;
71 const char *system_wide = NULL, *local;
73 local = getenv(CONFIG_ENVIRONMENT);
75 const char *home = getenv("HOME");
76 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
78 local = repo_config = xstrdup(git_path("config"));
80 global = xstrdup(mkpath("%s/.gitconfig", home));
81 system_wide = ETC_GITCONFIG;
85 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
87 for (tl=key; *tl && *tl != '.'; ++tl)
91 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
92 if (regcomp(key_regexp, key, REG_EXTENDED)) {
93 fprintf(stderr, "Invalid key pattern: %s\n", key_);
99 if (regex_[0] == '!') {
104 regexp = (regex_t*)xmalloc(sizeof(regex_t));
105 if (regcomp(regexp, regex_, REG_EXTENDED)) {
106 fprintf(stderr, "Invalid pattern: %s\n", regex_);
111 if (do_all && system_wide)
112 git_config_from_file(show_config, system_wide);
113 if (do_all && global)
114 git_config_from_file(show_config, global);
115 git_config_from_file(show_config, local);
116 if (!do_all && !seen && global)
117 git_config_from_file(show_config, global);
118 if (!do_all && !seen && system_wide)
119 git_config_from_file(show_config, system_wide);
130 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
138 int cmd_config(int argc, const char **argv, const char *prefix)
141 setup_git_directory_gently(&nongit);
144 if (!strcmp(argv[1], "--int"))
146 else if (!strcmp(argv[1], "--bool"))
148 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
149 return git_config(show_all_config);
150 else if (!strcmp(argv[1], "--global")) {
151 char *home = getenv("HOME");
153 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
154 setenv("GIT_CONFIG", user_config, 1);
157 die("$HOME not set");
160 else if (!strcmp(argv[1], "--system"))
161 setenv("GIT_CONFIG", ETC_GITCONFIG, 1);
162 else if (!strcmp(argv[1], "--rename-section")) {
165 usage(git_config_set_usage);
166 ret = git_config_rename_section(argv[2], argv[3]);
170 fprintf(stderr, "No such section!\n");
175 else if (!strcmp(argv[1], "--remove-section")) {
178 usage(git_config_set_usage);
179 ret = git_config_rename_section(argv[2], NULL);
183 fprintf(stderr, "No such section!\n");
196 return get_value(argv[1], NULL);
198 if (!strcmp(argv[1], "--unset"))
199 return git_config_set(argv[2], NULL);
200 else if (!strcmp(argv[1], "--unset-all"))
201 return git_config_set_multivar(argv[2], NULL, NULL, 1);
202 else if (!strcmp(argv[1], "--get"))
203 return get_value(argv[2], NULL);
204 else if (!strcmp(argv[1], "--get-all")) {
206 return get_value(argv[2], NULL);
207 } else if (!strcmp(argv[1], "--get-regexp")) {
211 return get_value(argv[2], NULL);
214 return git_config_set(argv[1], argv[2]);
216 if (!strcmp(argv[1], "--unset"))
217 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
218 else if (!strcmp(argv[1], "--unset-all"))
219 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
220 else if (!strcmp(argv[1], "--get"))
221 return get_value(argv[2], argv[3]);
222 else if (!strcmp(argv[1], "--get-all")) {
224 return get_value(argv[2], argv[3]);
225 } else if (!strcmp(argv[1], "--get-regexp")) {
229 return get_value(argv[2], argv[3]);
230 } else if (!strcmp(argv[1], "--add"))
231 return git_config_set_multivar(argv[2], argv[3], "^$", 0);
232 else if (!strcmp(argv[1], "--replace-all"))
234 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
237 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
239 if (!strcmp(argv[1], "--replace-all"))
240 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
243 usage(git_config_set_usage);