4 static const char git_config_set_usage[] =
5 "git-repo-config [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
7 static char* key = NULL;
8 static regex_t* key_regexp = NULL;
9 static regex_t* regexp = NULL;
10 static int show_keys = 0;
11 static int use_key_regexp = 0;
12 static int do_all = 0;
13 static int do_not_match = 0;
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;
35 if (!use_key_regexp && strcmp(key_, key))
37 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
41 regexec(regexp, value_, 0, NULL, 0)))
49 sprintf(value, "%d", git_config_int(key_, value_));
50 else if (type == T_BOOL)
51 vptr = git_config_bool(key_, value_) ? "true" : "false";
56 error("More than one value for the key %s: %s",
65 static int get_value(const char* key_, const char* regex_)
69 char *global = NULL, *repo_config = NULL;
72 local = getenv("GIT_CONFIG");
74 const char *home = getenv("HOME");
75 local = getenv("GIT_CONFIG_LOCAL");
79 local = repo_config = strdup(git_path("config"));
81 global = strdup(mkpath("%s/.gitconfig", home));
85 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
87 for (tl=key; *tl && *tl != '.'; ++tl)
91 key_regexp = (regex_t*)malloc(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*)malloc(sizeof(regex_t));
105 if (regcomp(regexp, regex_, REG_EXTENDED)) {
106 fprintf(stderr, "Invalid pattern: %s\n", regex_);
111 if (do_all && global)
112 git_config_from_file(show_config, global);
113 git_config_from_file(show_config, local);
114 if (!do_all && !seen && global)
115 git_config_from_file(show_config, global);
126 ret = (seen == 1) ? 0 : 1;
136 int main(int argc, const char **argv)
139 setup_git_directory_gently(&nongit);
142 if (!strcmp(argv[1], "--int"))
144 else if (!strcmp(argv[1], "--bool"))
146 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
147 return git_config(show_all_config);
156 return get_value(argv[1], NULL);
158 if (!strcmp(argv[1], "--unset"))
159 return git_config_set(argv[2], NULL);
160 else if (!strcmp(argv[1], "--unset-all"))
161 return git_config_set_multivar(argv[2], NULL, NULL, 1);
162 else if (!strcmp(argv[1], "--get"))
163 return get_value(argv[2], NULL);
164 else if (!strcmp(argv[1], "--get-all")) {
166 return get_value(argv[2], NULL);
167 } else if (!strcmp(argv[1], "--get-regexp")) {
171 return get_value(argv[2], NULL);
174 return git_config_set(argv[1], argv[2]);
176 if (!strcmp(argv[1], "--unset"))
177 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
178 else if (!strcmp(argv[1], "--unset-all"))
179 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
180 else if (!strcmp(argv[1], "--get"))
181 return get_value(argv[2], argv[3]);
182 else if (!strcmp(argv[1], "--get-all")) {
184 return get_value(argv[2], argv[3]);
185 } else if (!strcmp(argv[1], "--get-regexp")) {
189 return get_value(argv[2], argv[3]);
190 } else if (!strcmp(argv[1], "--replace-all"))
192 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
195 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
197 if (!strcmp(argv[1], "--replace-all"))
198 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
201 usage(git_config_set_usage);