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_)
68 const char *home = getenv("HOME");
69 const char *global = (home ? mkpath("%s/.gitconfig", home) : NULL);
72 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
74 for (tl=key; *tl && *tl != '.'; ++tl)
78 key_regexp = (regex_t*)malloc(sizeof(regex_t));
79 if (regcomp(key_regexp, key, REG_EXTENDED)) {
80 fprintf(stderr, "Invalid key pattern: %s\n", key_);
86 if (regex_[0] == '!') {
91 regexp = (regex_t*)malloc(sizeof(regex_t));
92 if (regcomp(regexp, regex_, REG_EXTENDED)) {
93 fprintf(stderr, "Invalid pattern: %s\n", regex_);
99 git_config_from_file(show_config, global);
100 if (!git_ignore_local_config)
101 git_config_from_file(show_config, git_path("config"));
102 if (!do_all && !seen)
103 git_config_from_file(show_config, global);
114 return (seen == 1) ? 0 : 1;
117 int main(int argc, const char **argv)
120 setup_git_directory_gently(&nongit);
123 if (!strcmp(argv[1], "--int"))
125 else if (!strcmp(argv[1], "--bool"))
127 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
128 return git_config(show_all_config);
129 else if (!strcmp(argv[1], "--no-local"))
130 git_ignore_local_config = 1;
139 return get_value(argv[1], NULL);
141 if (!strcmp(argv[1], "--unset"))
142 return git_config_set(argv[2], NULL);
143 else if (!strcmp(argv[1], "--unset-all"))
144 return git_config_set_multivar(argv[2], NULL, NULL, 1);
145 else if (!strcmp(argv[1], "--get"))
146 return get_value(argv[2], NULL);
147 else if (!strcmp(argv[1], "--get-all")) {
149 return get_value(argv[2], NULL);
150 } else if (!strcmp(argv[1], "--get-regexp")) {
154 return get_value(argv[2], NULL);
157 return git_config_set(argv[1], argv[2]);
159 if (!strcmp(argv[1], "--unset"))
160 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
161 else if (!strcmp(argv[1], "--unset-all"))
162 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
163 else if (!strcmp(argv[1], "--get"))
164 return get_value(argv[2], argv[3]);
165 else if (!strcmp(argv[1], "--get-all")) {
167 return get_value(argv[2], argv[3]);
168 } else if (!strcmp(argv[1], "--get-regexp")) {
172 return get_value(argv[2], argv[3]);
173 } else if (!strcmp(argv[1], "--replace-all"))
175 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
178 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
180 if (!strcmp(argv[1], "--replace-all"))
181 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
184 usage(git_config_set_usage);