4 static const char git_config_set_usage[] =
5 "git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_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)))
46 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
47 else if (type == T_BOOL)
48 vptr = git_config_bool(key_, value_) ? "true" : "false";
50 vptr = value_?value_:"";
53 error("More than one value for the key %s: %s",
62 static int get_value(const char* key_, const char* regex_)
66 char *global = NULL, *repo_config = NULL;
69 local = getenv(CONFIG_ENVIRONMENT);
71 const char *home = getenv("HOME");
72 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
74 local = repo_config = xstrdup(git_path("config"));
76 global = xstrdup(mkpath("%s/.gitconfig", home));
80 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
82 for (tl=key; *tl && *tl != '.'; ++tl)
86 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
87 if (regcomp(key_regexp, key, REG_EXTENDED)) {
88 fprintf(stderr, "Invalid key pattern: %s\n", key_);
94 if (regex_[0] == '!') {
99 regexp = (regex_t*)xmalloc(sizeof(regex_t));
100 if (regcomp(regexp, regex_, REG_EXTENDED)) {
101 fprintf(stderr, "Invalid pattern: %s\n", regex_);
106 if (do_all && global)
107 git_config_from_file(show_config, global);
108 git_config_from_file(show_config, local);
109 if (!do_all && !seen && global)
110 git_config_from_file(show_config, global);
121 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
129 int cmd_repo_config(int argc, const char **argv, const char *prefix)
132 setup_git_directory_gently(&nongit);
135 if (!strcmp(argv[1], "--int"))
137 else if (!strcmp(argv[1], "--bool"))
139 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
140 return git_config(show_all_config);
141 else if (!strcmp(argv[1], "--global")) {
142 char *home = getenv("HOME");
144 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
145 setenv("GIT_CONFIG", user_config, 1);
148 die("$HOME not set");
150 } else if (!strcmp(argv[1], "--rename-section")) {
153 usage(git_config_set_usage);
154 ret = git_config_rename_section(argv[2], argv[3]);
158 fprintf(stderr, "No such section!\n");
170 return get_value(argv[1], NULL);
172 if (!strcmp(argv[1], "--unset"))
173 return git_config_set(argv[2], NULL);
174 else if (!strcmp(argv[1], "--unset-all"))
175 return git_config_set_multivar(argv[2], NULL, NULL, 1);
176 else if (!strcmp(argv[1], "--get"))
177 return get_value(argv[2], NULL);
178 else if (!strcmp(argv[1], "--get-all")) {
180 return get_value(argv[2], NULL);
181 } else if (!strcmp(argv[1], "--get-regexp")) {
185 return get_value(argv[2], NULL);
188 return git_config_set(argv[1], argv[2]);
190 if (!strcmp(argv[1], "--unset"))
191 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
192 else if (!strcmp(argv[1], "--unset-all"))
193 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
194 else if (!strcmp(argv[1], "--get"))
195 return get_value(argv[2], argv[3]);
196 else if (!strcmp(argv[1], "--get-all")) {
198 return get_value(argv[2], argv[3]);
199 } else if (!strcmp(argv[1], "--get-regexp")) {
203 return get_value(argv[2], argv[3]);
204 } else if (!strcmp(argv[1], "--add"))
205 return git_config_set_multivar(argv[2], argv[3], "^$", 0);
206 else if (!strcmp(argv[1], "--replace-all"))
208 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
211 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
213 if (!strcmp(argv[1], "--replace-all"))
214 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
217 usage(git_config_set_usage);