rebase -i: squash should retain the authorship of the _first_ commit
[git/trast.git] / builtin-config.c
blob0a605e01aca6e1ab91fcfecd3929b8a853ff9f3d
1 #include "builtin.h"
2 #include "cache.h"
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";
7 static char *key;
8 static regex_t *key_regexp;
9 static regex_t *regexp;
10 static int show_keys;
11 static int use_key_regexp;
12 static int do_all;
13 static int do_not_match;
14 static int seen;
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_)
22 if (value_)
23 printf("%s%c%s%c", key_, delim, value_, term);
24 else
25 printf("%s%c", key_, term);
26 return 0;
29 static int show_config(const char* key_, const char* value_)
31 char value[256];
32 const char *vptr = value;
33 int dup_error = 0;
35 if (!use_key_regexp && strcmp(key_, key))
36 return 0;
37 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
38 return 0;
39 if (regexp != NULL &&
40 (do_not_match ^
41 regexec(regexp, (value_?value_:""), 0, NULL, 0)))
42 return 0;
44 if (show_keys) {
45 if (value_)
46 printf("%s%c", key_, key_delim);
47 else
48 printf("%s", key_);
50 if (seen && !do_all)
51 dup_error = 1;
52 if (type == T_INT)
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";
56 else
57 vptr = value_?value_:"";
58 seen++;
59 if (dup_error) {
60 error("More than one value for the key %s: %s",
61 key_, vptr);
63 else
64 printf("%s%c", vptr, term);
66 return 0;
69 static int get_value(const char* key_, const char* regex_)
71 int ret = -1;
72 char *tl;
73 char *global = NULL, *repo_config = NULL;
74 const char *system_wide = NULL, *local;
76 local = getenv(CONFIG_ENVIRONMENT);
77 if (!local) {
78 const char *home = getenv("HOME");
79 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
80 if (!local)
81 local = repo_config = xstrdup(git_path("config"));
82 if (home)
83 global = xstrdup(mkpath("%s/.gitconfig", home));
84 system_wide = ETC_GITCONFIG;
87 key = xstrdup(key_);
88 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
89 *tl = tolower(*tl);
90 for (tl=key; *tl && *tl != '.'; ++tl)
91 *tl = tolower(*tl);
93 if (use_key_regexp) {
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_);
97 goto free_strings;
101 if (regex_) {
102 if (regex_[0] == '!') {
103 do_not_match = 1;
104 regex_++;
107 regexp = (regex_t*)xmalloc(sizeof(regex_t));
108 if (regcomp(regexp, regex_, REG_EXTENDED)) {
109 fprintf(stderr, "Invalid pattern: %s\n", regex_);
110 goto free_strings;
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);
124 free(key);
125 if (regexp) {
126 regfree(regexp);
127 free(regexp);
130 if (do_all)
131 ret = !seen;
132 else
133 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
135 free_strings:
136 free(repo_config);
137 free(global);
138 return ret;
141 char *normalize_value(const char *key, const char *value)
143 char *normalized;
145 if (!value)
146 return NULL;
148 if (type == T_RAW)
149 normalized = xstrdup(value);
150 else {
151 normalized = xmalloc(64);
152 if (type == T_INT) {
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");
161 return normalized;
164 int cmd_config(int argc, const char **argv, const char *prefix)
166 int nongit = 0;
167 char* value;
168 setup_git_directory_gently(&nongit);
170 while (1 < argc) {
171 if (!strcmp(argv[1], "--int"))
172 type = T_INT;
173 else if (!strcmp(argv[1], "--bool"))
174 type = T_BOOL;
175 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
176 return git_config(show_all_config);
177 else if (!strcmp(argv[1], "--global")) {
178 char *home = getenv("HOME");
179 if (home) {
180 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
181 setenv(CONFIG_ENVIRONMENT, user_config, 1);
182 free(user_config);
183 } else {
184 die("$HOME not set");
187 else if (!strcmp(argv[1], "--system"))
188 setenv(CONFIG_ENVIRONMENT, ETC_GITCONFIG, 1);
189 else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
190 if (argc < 3)
191 usage(git_config_set_usage);
192 setenv(CONFIG_ENVIRONMENT, argv[2], 1);
193 argc--;
194 argv++;
196 else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
197 term = '\0';
198 delim = '\n';
199 key_delim = '\n';
201 else if (!strcmp(argv[1], "--rename-section")) {
202 int ret;
203 if (argc != 4)
204 usage(git_config_set_usage);
205 ret = git_config_rename_section(argv[2], argv[3]);
206 if (ret < 0)
207 return ret;
208 if (ret == 0) {
209 fprintf(stderr, "No such section!\n");
210 return 1;
212 return 0;
214 else if (!strcmp(argv[1], "--remove-section")) {
215 int ret;
216 if (argc != 3)
217 usage(git_config_set_usage);
218 ret = git_config_rename_section(argv[2], NULL);
219 if (ret < 0)
220 return ret;
221 if (ret == 0) {
222 fprintf(stderr, "No such section!\n");
223 return 1;
225 return 0;
227 else
228 break;
229 argc--;
230 argv++;
233 switch (argc) {
234 case 2:
235 return get_value(argv[1], NULL);
236 case 3:
237 if (!strcmp(argv[1], "--unset"))
238 return git_config_set(argv[2], NULL);
239 else if (!strcmp(argv[1], "--unset-all"))
240 return git_config_set_multivar(argv[2], NULL, NULL, 1);
241 else if (!strcmp(argv[1], "--get"))
242 return get_value(argv[2], NULL);
243 else if (!strcmp(argv[1], "--get-all")) {
244 do_all = 1;
245 return get_value(argv[2], NULL);
246 } else if (!strcmp(argv[1], "--get-regexp")) {
247 show_keys = 1;
248 use_key_regexp = 1;
249 do_all = 1;
250 return get_value(argv[2], NULL);
251 } else {
252 value = normalize_value(argv[1], argv[2]);
253 return git_config_set(argv[1], value);
255 case 4:
256 if (!strcmp(argv[1], "--unset"))
257 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
258 else if (!strcmp(argv[1], "--unset-all"))
259 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
260 else if (!strcmp(argv[1], "--get"))
261 return get_value(argv[2], argv[3]);
262 else if (!strcmp(argv[1], "--get-all")) {
263 do_all = 1;
264 return get_value(argv[2], argv[3]);
265 } else if (!strcmp(argv[1], "--get-regexp")) {
266 show_keys = 1;
267 use_key_regexp = 1;
268 do_all = 1;
269 return get_value(argv[2], argv[3]);
270 } else if (!strcmp(argv[1], "--add")) {
271 value = normalize_value(argv[2], argv[3]);
272 return git_config_set_multivar(argv[2], value, "^$", 0);
273 } else if (!strcmp(argv[1], "--replace-all")) {
274 value = normalize_value(argv[2], argv[3]);
275 return git_config_set_multivar(argv[2], value, NULL, 1);
276 } else {
277 value = normalize_value(argv[1], argv[2]);
278 return git_config_set_multivar(argv[1], value, argv[3], 0);
280 case 5:
281 if (!strcmp(argv[1], "--replace-all")) {
282 value = normalize_value(argv[2], argv[3]);
283 return git_config_set_multivar(argv[2], value, argv[4], 1);
285 case 1:
286 default:
287 usage(git_config_set_usage);
289 return 0;