]>
Commit | Line | Data |
---|---|---|
e12c095a | 1 | #include "builtin.h" |
1b1e59c5 JS |
2 | #include "cache.h" |
3 | ||
4 | static const char git_config_set_usage[] = | |
2275d502 | 5 | "git-config [ --global | --system ] [ --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"; |
4ddba79d | 6 | |
96f1e58f DR |
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; | |
2275d502 FL |
15 | static char delim = '='; |
16 | static char key_delim = ' '; | |
17 | static char term = '\n'; | |
7162dff3 | 18 | static enum { T_RAW, T_INT, T_BOOL } type = T_RAW; |
4ddba79d | 19 | |
de791f15 PB |
20 | static int show_all_config(const char *key_, const char *value_) |
21 | { | |
22 | if (value_) | |
2275d502 | 23 | printf("%s%c%s%c", key_, delim, value_, term); |
de791f15 | 24 | else |
2275d502 | 25 | printf("%s%c", key_, term); |
de791f15 PB |
26 | return 0; |
27 | } | |
28 | ||
4ddba79d JS |
29 | static int show_config(const char* key_, const char* value_) |
30 | { | |
e098c6f8 JH |
31 | char value[256]; |
32 | const char *vptr = value; | |
8f5ff31f | 33 | int dup_error = 0; |
e098c6f8 | 34 | |
8f5ff31f JS |
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 && | |
f98d863d | 40 | (do_not_match ^ |
acb70149 | 41 | regexec(regexp, (value_?value_:""), 0, NULL, 0))) |
8f5ff31f JS |
42 | return 0; |
43 | ||
b69ba460 FL |
44 | if (show_keys) { |
45 | if (value_) | |
2275d502 | 46 | printf("%s%c", key_, key_delim); |
b69ba460 FL |
47 | else |
48 | printf("%s", key_); | |
49 | } | |
8f5ff31f JS |
50 | if (seen && !do_all) |
51 | dup_error = 1; | |
52 | if (type == T_INT) | |
acb70149 | 53 | sprintf(value, "%d", git_config_int(key_, value_?value_:"")); |
8f5ff31f JS |
54 | else if (type == T_BOOL) |
55 | vptr = git_config_bool(key_, value_) ? "true" : "false"; | |
56 | else | |
acb70149 | 57 | vptr = value_?value_:""; |
8f5ff31f JS |
58 | seen++; |
59 | if (dup_error) { | |
60 | error("More than one value for the key %s: %s", | |
61 | key_, vptr); | |
4ddba79d | 62 | } |
8f5ff31f | 63 | else |
2275d502 | 64 | printf("%s%c", vptr, term); |
8f5ff31f | 65 | |
4ddba79d JS |
66 | return 0; |
67 | } | |
68 | ||
69 | static int get_value(const char* key_, const char* regex_) | |
70 | { | |
5f1a63e0 | 71 | int ret = -1; |
d14f7764 | 72 | char *tl; |
5f1a63e0 | 73 | char *global = NULL, *repo_config = NULL; |
32043c9f | 74 | const char *system_wide = NULL, *local; |
5f1a63e0 | 75 | |
d4ebc36c | 76 | local = getenv(CONFIG_ENVIRONMENT); |
5f1a63e0 JS |
77 | if (!local) { |
78 | const char *home = getenv("HOME"); | |
d4ebc36c | 79 | local = getenv(CONFIG_LOCAL_ENVIRONMENT); |
5f1a63e0 | 80 | if (!local) |
9befac47 | 81 | local = repo_config = xstrdup(git_path("config")); |
5f1a63e0 | 82 | if (home) |
9befac47 | 83 | global = xstrdup(mkpath("%s/.gitconfig", home)); |
32043c9f | 84 | system_wide = ETC_GITCONFIG; |
5f1a63e0 | 85 | } |
4ddba79d | 86 | |
9befac47 | 87 | key = xstrdup(key_); |
d14f7764 LT |
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); | |
4ddba79d | 92 | |
2fa9a0fb | 93 | if (use_key_regexp) { |
2d7320d0 | 94 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
2fa9a0fb | 95 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
e098c6f8 | 96 | fprintf(stderr, "Invalid key pattern: %s\n", key_); |
5f1a63e0 | 97 | goto free_strings; |
2fa9a0fb JS |
98 | } |
99 | } | |
100 | ||
4ddba79d | 101 | if (regex_) { |
f98d863d JS |
102 | if (regex_[0] == '!') { |
103 | do_not_match = 1; | |
104 | regex_++; | |
105 | } | |
106 | ||
2d7320d0 | 107 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
0a152171 | 108 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
4ddba79d | 109 | fprintf(stderr, "Invalid pattern: %s\n", regex_); |
5f1a63e0 | 110 | goto free_strings; |
4ddba79d JS |
111 | } |
112 | } | |
113 | ||
32043c9f JS |
114 | if (do_all && system_wide) |
115 | git_config_from_file(show_config, system_wide); | |
5f1a63e0 JS |
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); | |
32043c9f JS |
121 | if (!do_all && !seen && system_wide) |
122 | git_config_from_file(show_config, system_wide); | |
5f1a63e0 | 123 | |
4ddba79d | 124 | free(key); |
0a152171 AW |
125 | if (regexp) { |
126 | regfree(regexp); | |
127 | free(regexp); | |
4ddba79d JS |
128 | } |
129 | ||
130 | if (do_all) | |
5f1a63e0 JS |
131 | ret = !seen; |
132 | else | |
dc2613de | 133 | ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1; |
5f1a63e0 JS |
134 | |
135 | free_strings: | |
4cac42b1 JH |
136 | free(repo_config); |
137 | free(global); | |
5f1a63e0 | 138 | return ret; |
4ddba79d | 139 | } |
1b1e59c5 | 140 | |
e0d10e1c | 141 | int cmd_config(int argc, const char **argv, const char *prefix) |
1b1e59c5 | 142 | { |
4d599e6b LT |
143 | int nongit = 0; |
144 | setup_git_directory_gently(&nongit); | |
7162dff3 PB |
145 | |
146 | while (1 < argc) { | |
147 | if (!strcmp(argv[1], "--int")) | |
148 | type = T_INT; | |
149 | else if (!strcmp(argv[1], "--bool")) | |
150 | type = T_BOOL; | |
cfa24e18 JS |
151 | else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) |
152 | return git_config(show_all_config); | |
34eb3340 SE |
153 | else if (!strcmp(argv[1], "--global")) { |
154 | char *home = getenv("HOME"); | |
155 | if (home) { | |
156 | char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); | |
157 | setenv("GIT_CONFIG", user_config, 1); | |
158 | free(user_config); | |
159 | } else { | |
160 | die("$HOME not set"); | |
161 | } | |
32043c9f JS |
162 | } |
163 | else if (!strcmp(argv[1], "--system")) | |
164 | setenv("GIT_CONFIG", ETC_GITCONFIG, 1); | |
2275d502 FL |
165 | else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) { |
166 | term = '\0'; | |
167 | delim = '\n'; | |
168 | key_delim = '\n'; | |
169 | } | |
32043c9f | 170 | else if (!strcmp(argv[1], "--rename-section")) { |
0667fcfb JS |
171 | int ret; |
172 | if (argc != 4) | |
173 | usage(git_config_set_usage); | |
174 | ret = git_config_rename_section(argv[2], argv[3]); | |
175 | if (ret < 0) | |
176 | return ret; | |
177 | if (ret == 0) { | |
178 | fprintf(stderr, "No such section!\n"); | |
179 | return 1; | |
180 | } | |
181 | return 0; | |
32043c9f | 182 | } |
118f8b24 PB |
183 | else if (!strcmp(argv[1], "--remove-section")) { |
184 | int ret; | |
185 | if (argc != 3) | |
186 | usage(git_config_set_usage); | |
187 | ret = git_config_rename_section(argv[2], NULL); | |
188 | if (ret < 0) | |
189 | return ret; | |
190 | if (ret == 0) { | |
191 | fprintf(stderr, "No such section!\n"); | |
192 | return 1; | |
193 | } | |
194 | return 0; | |
195 | } | |
32043c9f | 196 | else |
7162dff3 PB |
197 | break; |
198 | argc--; | |
199 | argv++; | |
200 | } | |
201 | ||
1b1e59c5 JS |
202 | switch (argc) { |
203 | case 2: | |
4ddba79d | 204 | return get_value(argv[1], NULL); |
1b1e59c5 JS |
205 | case 3: |
206 | if (!strcmp(argv[1], "--unset")) | |
207 | return git_config_set(argv[2], NULL); | |
4ddba79d JS |
208 | else if (!strcmp(argv[1], "--unset-all")) |
209 | return git_config_set_multivar(argv[2], NULL, NULL, 1); | |
210 | else if (!strcmp(argv[1], "--get")) | |
211 | return get_value(argv[2], NULL); | |
212 | else if (!strcmp(argv[1], "--get-all")) { | |
213 | do_all = 1; | |
214 | return get_value(argv[2], NULL); | |
2fa9a0fb JS |
215 | } else if (!strcmp(argv[1], "--get-regexp")) { |
216 | show_keys = 1; | |
217 | use_key_regexp = 1; | |
218 | do_all = 1; | |
219 | return get_value(argv[2], NULL); | |
4ddba79d JS |
220 | } else |
221 | ||
1b1e59c5 JS |
222 | return git_config_set(argv[1], argv[2]); |
223 | case 4: | |
224 | if (!strcmp(argv[1], "--unset")) | |
4ddba79d JS |
225 | return git_config_set_multivar(argv[2], NULL, argv[3], 0); |
226 | else if (!strcmp(argv[1], "--unset-all")) | |
227 | return git_config_set_multivar(argv[2], NULL, argv[3], 1); | |
228 | else if (!strcmp(argv[1], "--get")) | |
229 | return get_value(argv[2], argv[3]); | |
230 | else if (!strcmp(argv[1], "--get-all")) { | |
231 | do_all = 1; | |
232 | return get_value(argv[2], argv[3]); | |
2fa9a0fb JS |
233 | } else if (!strcmp(argv[1], "--get-regexp")) { |
234 | show_keys = 1; | |
235 | use_key_regexp = 1; | |
236 | do_all = 1; | |
237 | return get_value(argv[2], argv[3]); | |
89c4afe0 BG |
238 | } else if (!strcmp(argv[1], "--add")) |
239 | return git_config_set_multivar(argv[2], argv[3], "^$", 0); | |
240 | else if (!strcmp(argv[1], "--replace-all")) | |
4ddba79d JS |
241 | |
242 | return git_config_set_multivar(argv[2], argv[3], NULL, 1); | |
1b1e59c5 | 243 | else |
4ddba79d JS |
244 | |
245 | return git_config_set_multivar(argv[1], argv[2], argv[3], 0); | |
246 | case 5: | |
247 | if (!strcmp(argv[1], "--replace-all")) | |
248 | return git_config_set_multivar(argv[2], argv[3], argv[4], 1); | |
249 | case 1: | |
1b1e59c5 JS |
250 | default: |
251 | usage(git_config_set_usage); | |
252 | } | |
253 | return 0; | |
254 | } |