]>
Commit | Line | Data |
---|---|---|
e12c095a | 1 | #include "builtin.h" |
1b1e59c5 | 2 | #include "cache.h" |
9ce03522 | 3 | #include "color.h" |
d64ec16c | 4 | #include "parse-options.h" |
d4770964 | 5 | #include "urlmatch.h" |
70bd879a | 6 | #include "quote.h" |
1b1e59c5 | 7 | |
d64ec16c | 8 | static const char *const builtin_config_usage[] = { |
9c9b4f2f | 9 | N_("git config [<options>]"), |
d64ec16c FC |
10 | NULL |
11 | }; | |
4ddba79d | 12 | |
96f1e58f DR |
13 | static char *key; |
14 | static regex_t *key_regexp; | |
15 | static regex_t *regexp; | |
16 | static int show_keys; | |
578625fa | 17 | static int omit_values; |
96f1e58f DR |
18 | static int use_key_regexp; |
19 | static int do_all; | |
20 | static int do_not_match; | |
2275d502 FL |
21 | static char delim = '='; |
22 | static char key_delim = ' '; | |
23 | static char term = '\n'; | |
4ddba79d | 24 | |
57210a67 | 25 | static int use_global_config, use_system_config, use_local_config; |
c8985ce0 | 26 | static struct git_config_source given_config_source; |
16c1e939 | 27 | static int actions, types; |
d64ec16c | 28 | static int end_null; |
c48f4b37 NTND |
29 | static int respect_includes_opt = -1; |
30 | static struct config_options config_options; | |
70bd879a | 31 | static int show_origin; |
d64ec16c FC |
32 | |
33 | #define ACTION_GET (1<<0) | |
34 | #define ACTION_GET_ALL (1<<1) | |
35 | #define ACTION_GET_REGEXP (1<<2) | |
36 | #define ACTION_REPLACE_ALL (1<<3) | |
37 | #define ACTION_ADD (1<<4) | |
38 | #define ACTION_UNSET (1<<5) | |
39 | #define ACTION_UNSET_ALL (1<<6) | |
40 | #define ACTION_RENAME_SECTION (1<<7) | |
41 | #define ACTION_REMOVE_SECTION (1<<8) | |
42 | #define ACTION_LIST (1<<9) | |
43 | #define ACTION_EDIT (1<<10) | |
44 | #define ACTION_SET (1<<11) | |
45 | #define ACTION_SET_ALL (1<<12) | |
46 | #define ACTION_GET_COLOR (1<<13) | |
47 | #define ACTION_GET_COLORBOOL (1<<14) | |
d4770964 | 48 | #define ACTION_GET_URLMATCH (1<<15) |
d64ec16c | 49 | |
16c1e939 FC |
50 | #define TYPE_BOOL (1<<0) |
51 | #define TYPE_INT (1<<1) | |
52 | #define TYPE_BOOL_OR_INT (1<<2) | |
1349484e | 53 | #define TYPE_PATH (1<<3) |
16c1e939 | 54 | |
d64ec16c | 55 | static struct option builtin_config_options[] = { |
1bd31ce6 | 56 | OPT_GROUP(N_("Config file location")), |
21e047dc SB |
57 | OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), |
58 | OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), | |
59 | OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), | |
c8985ce0 KS |
60 | OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), |
61 | OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")), | |
1bd31ce6 NTND |
62 | OPT_GROUP(N_("Action")), |
63 | OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), | |
64 | OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL), | |
65 | OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP), | |
d4770964 | 66 | OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), |
1bd31ce6 | 67 | OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL), |
f63cf8c9 NTND |
68 | OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), |
69 | OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET), | |
70 | OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), | |
1bd31ce6 NTND |
71 | OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), |
72 | OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), | |
73 | OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), | |
f63cf8c9 | 74 | OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), |
d0e08d62 JK |
75 | OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR), |
76 | OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL), | |
1bd31ce6 NTND |
77 | OPT_GROUP(N_("Type")), |
78 | OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL), | |
79 | OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT), | |
80 | OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), | |
81 | OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH), | |
82 | OPT_GROUP(N_("Other")), | |
d5d09d47 | 83 | OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")), |
578625fa | 84 | OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")), |
c48f4b37 | 85 | OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")), |
70bd879a | 86 | OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")), |
d64ec16c FC |
87 | OPT_END(), |
88 | }; | |
89 | ||
90 | static void check_argc(int argc, int min, int max) { | |
91 | if (argc >= min && argc <= max) | |
92 | return; | |
93 | error("wrong number of arguments"); | |
94 | usage_with_options(builtin_config_usage, builtin_config_options); | |
95 | } | |
96 | ||
70bd879a LS |
97 | static void show_config_origin(struct strbuf *buf) |
98 | { | |
99 | const char term = end_null ? '\0' : '\t'; | |
100 | ||
101 | strbuf_addstr(buf, current_config_origin_type()); | |
102 | strbuf_addch(buf, ':'); | |
103 | if (end_null) | |
104 | strbuf_addstr(buf, current_config_name()); | |
105 | else | |
106 | quote_c_style(current_config_name(), buf, NULL, 0); | |
107 | strbuf_addch(buf, term); | |
108 | } | |
109 | ||
ef90d6d4 | 110 | static int show_all_config(const char *key_, const char *value_, void *cb) |
de791f15 | 111 | { |
70bd879a LS |
112 | if (show_origin) { |
113 | struct strbuf buf = STRBUF_INIT; | |
114 | show_config_origin(&buf); | |
115 | /* Use fwrite as "buf" can contain \0's if "end_null" is set. */ | |
116 | fwrite(buf.buf, 1, buf.len, stdout); | |
117 | strbuf_release(&buf); | |
118 | } | |
578625fa | 119 | if (!omit_values && value_) |
2275d502 | 120 | printf("%s%c%s%c", key_, delim, value_, term); |
de791f15 | 121 | else |
2275d502 | 122 | printf("%s%c", key_, term); |
de791f15 PB |
123 | return 0; |
124 | } | |
125 | ||
7acdd6f0 JK |
126 | struct strbuf_list { |
127 | struct strbuf *items; | |
128 | int nr; | |
129 | int alloc; | |
130 | }; | |
131 | ||
d9b9169b | 132 | static int format_config(struct strbuf *buf, const char *key_, const char *value_) |
4ddba79d | 133 | { |
70bd879a LS |
134 | if (show_origin) |
135 | show_config_origin(buf); | |
ebca2d49 | 136 | if (show_keys) |
7acdd6f0 | 137 | strbuf_addstr(buf, key_); |
ebca2d49 | 138 | if (!omit_values) { |
f2259877 JK |
139 | if (show_keys) |
140 | strbuf_addch(buf, key_delim); | |
ebca2d49 SG |
141 | |
142 | if (types == TYPE_INT) | |
f2259877 JK |
143 | strbuf_addf(buf, "%"PRId64, |
144 | git_config_int64(key_, value_ ? value_ : "")); | |
ebca2d49 | 145 | else if (types == TYPE_BOOL) |
f2259877 JK |
146 | strbuf_addstr(buf, git_config_bool(key_, value_) ? |
147 | "true" : "false"); | |
ebca2d49 SG |
148 | else if (types == TYPE_BOOL_OR_INT) { |
149 | int is_bool, v; | |
150 | v = git_config_bool_or_int(key_, value_, &is_bool); | |
151 | if (is_bool) | |
f2259877 | 152 | strbuf_addstr(buf, v ? "true" : "false"); |
ebca2d49 | 153 | else |
f2259877 | 154 | strbuf_addf(buf, "%d", v); |
ebca2d49 | 155 | } else if (types == TYPE_PATH) { |
f2259877 JK |
156 | const char *v; |
157 | if (git_config_pathname(&v, key_, value_) < 0) | |
ebca2d49 | 158 | return -1; |
f2259877 JK |
159 | strbuf_addstr(buf, v); |
160 | free((char *)v); | |
ebca2d49 | 161 | } else if (value_) { |
f2259877 | 162 | strbuf_addstr(buf, value_); |
ebca2d49 | 163 | } else { |
f2259877 JK |
164 | /* Just show the key name; back out delimiter */ |
165 | if (show_keys) | |
166 | strbuf_setlen(buf, buf->len - 1); | |
ebca2d49 | 167 | } |
578625fa | 168 | } |
00b347d3 | 169 | strbuf_addch(buf, term); |
4ddba79d JS |
170 | return 0; |
171 | } | |
172 | ||
d9b9169b JH |
173 | static int collect_config(const char *key_, const char *value_, void *cb) |
174 | { | |
175 | struct strbuf_list *values = cb; | |
176 | ||
177 | if (!use_key_regexp && strcmp(key_, key)) | |
178 | return 0; | |
179 | if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) | |
180 | return 0; | |
181 | if (regexp != NULL && | |
182 | (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) | |
183 | return 0; | |
184 | ||
185 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); | |
9f1429df | 186 | strbuf_init(&values->items[values->nr], 0); |
d9b9169b JH |
187 | |
188 | return format_config(&values->items[values->nr++], key_, value_); | |
189 | } | |
190 | ||
4b951b7e | 191 | static int get_value(const char *key_, const char *regex_) |
4ddba79d | 192 | { |
9409c7a5 | 193 | int ret = CONFIG_GENERIC_ERROR; |
5ba1a8a7 | 194 | struct strbuf_list values = {NULL}; |
7acdd6f0 | 195 | int i; |
4ddba79d | 196 | |
2fa9a0fb | 197 | if (use_key_regexp) { |
b09c53a3 LP |
198 | char *tl; |
199 | ||
200 | /* | |
201 | * NEEDSWORK: this naive pattern lowercasing obviously does not | |
202 | * work for more complex patterns like "^[^.]*Foo.*bar". | |
203 | * Perhaps we should deprecate this altogether someday. | |
204 | */ | |
205 | ||
206 | key = xstrdup(key_); | |
207 | for (tl = key + strlen(key) - 1; | |
208 | tl >= key && *tl != '.'; | |
209 | tl--) | |
210 | *tl = tolower(*tl); | |
211 | for (tl = key; *tl && *tl != '.'; tl++) | |
212 | *tl = tolower(*tl); | |
213 | ||
2d7320d0 | 214 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
2fa9a0fb | 215 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
95d62121 | 216 | error("invalid key pattern: %s", key_); |
97ed50f9 JK |
217 | free(key_regexp); |
218 | key_regexp = NULL; | |
9409c7a5 | 219 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 220 | goto free_strings; |
2fa9a0fb | 221 | } |
b09c53a3 | 222 | } else { |
9409c7a5 JH |
223 | if (git_config_parse_key(key_, &key, NULL)) { |
224 | ret = CONFIG_INVALID_KEY; | |
b09c53a3 | 225 | goto free_strings; |
9409c7a5 | 226 | } |
2fa9a0fb JS |
227 | } |
228 | ||
4ddba79d | 229 | if (regex_) { |
f98d863d JS |
230 | if (regex_[0] == '!') { |
231 | do_not_match = 1; | |
232 | regex_++; | |
233 | } | |
234 | ||
2d7320d0 | 235 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
0a152171 | 236 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
95d62121 | 237 | error("invalid pattern: %s", regex_); |
97ed50f9 JK |
238 | free(regexp); |
239 | regexp = NULL; | |
9409c7a5 | 240 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 241 | goto free_strings; |
4ddba79d JS |
242 | } |
243 | } | |
244 | ||
e8955898 | 245 | git_config_with_options(collect_config, &values, |
c48f4b37 | 246 | &given_config_source, &config_options); |
5f1a63e0 | 247 | |
00b347d3 | 248 | ret = !values.nr; |
9b25a0b5 | 249 | |
7acdd6f0 JK |
250 | for (i = 0; i < values.nr; i++) { |
251 | struct strbuf *buf = values.items + i; | |
00b347d3 JK |
252 | if (do_all || i == values.nr - 1) |
253 | fwrite(buf->buf, 1, buf->len, stdout); | |
7acdd6f0 JK |
254 | strbuf_release(buf); |
255 | } | |
256 | free(values.items); | |
5f1a63e0 | 257 | |
97ed50f9 | 258 | free_strings: |
4ddba79d | 259 | free(key); |
35998c89 JK |
260 | if (key_regexp) { |
261 | regfree(key_regexp); | |
262 | free(key_regexp); | |
263 | } | |
0a152171 AW |
264 | if (regexp) { |
265 | regfree(regexp); | |
266 | free(regexp); | |
4ddba79d JS |
267 | } |
268 | ||
5f1a63e0 | 269 | return ret; |
4ddba79d | 270 | } |
1b1e59c5 | 271 | |
186458b1 | 272 | static char *normalize_value(const char *key, const char *value) |
db1696b8 | 273 | { |
db1696b8 FL |
274 | if (!value) |
275 | return NULL; | |
276 | ||
1349484e MM |
277 | if (types == 0 || types == TYPE_PATH) |
278 | /* | |
279 | * We don't do normalization for TYPE_PATH here: If | |
280 | * the path is like ~/foobar/, we prefer to store | |
281 | * "~/foobar/" in the config file, and to expand the ~ | |
282 | * when retrieving the value. | |
283 | */ | |
3ec832c4 JK |
284 | return xstrdup(value); |
285 | if (types == TYPE_INT) | |
286 | return xstrfmt("%"PRId64, git_config_int64(key, value)); | |
287 | if (types == TYPE_BOOL) | |
288 | return xstrdup(git_config_bool(key, value) ? "true" : "false"); | |
289 | if (types == TYPE_BOOL_OR_INT) { | |
290 | int is_bool, v; | |
291 | v = git_config_bool_or_int(key, value, &is_bool); | |
292 | if (!is_bool) | |
293 | return xstrfmt("%d", v); | |
294 | else | |
295 | return xstrdup(v ? "true" : "false"); | |
db1696b8 FL |
296 | } |
297 | ||
3ec832c4 | 298 | die("BUG: cannot normalize type %d", types); |
db1696b8 FL |
299 | } |
300 | ||
9ce03522 JH |
301 | static int get_color_found; |
302 | static const char *get_color_slot; | |
b408457f | 303 | static const char *get_colorbool_slot; |
9ce03522 JH |
304 | static char parsed_color[COLOR_MAXLEN]; |
305 | ||
ef90d6d4 | 306 | static int git_get_color_config(const char *var, const char *value, void *cb) |
9ce03522 JH |
307 | { |
308 | if (!strcmp(var, get_color_slot)) { | |
f769982d JH |
309 | if (!value) |
310 | config_error_nonbool(var); | |
f6c5a296 JK |
311 | if (color_parse(value, parsed_color) < 0) |
312 | return -1; | |
9ce03522 JH |
313 | get_color_found = 1; |
314 | } | |
315 | return 0; | |
316 | } | |
317 | ||
d0e08d62 | 318 | static void get_color(const char *var, const char *def_color) |
9ce03522 | 319 | { |
d0e08d62 | 320 | get_color_slot = var; |
9ce03522 JH |
321 | get_color_found = 0; |
322 | parsed_color[0] = '\0'; | |
270a3443 | 323 | git_config_with_options(git_get_color_config, NULL, |
c48f4b37 | 324 | &given_config_source, &config_options); |
9ce03522 | 325 | |
f6c5a296 JK |
326 | if (!get_color_found && def_color) { |
327 | if (color_parse(def_color, parsed_color) < 0) | |
328 | die(_("unable to parse default color value")); | |
329 | } | |
9ce03522 JH |
330 | |
331 | fputs(parsed_color, stdout); | |
9ce03522 JH |
332 | } |
333 | ||
0f6f5a40 | 334 | static int get_colorbool_found; |
69243c2b | 335 | static int get_diff_color_found; |
c659f55b | 336 | static int get_color_ui_found; |
ef90d6d4 JS |
337 | static int git_get_colorbool_config(const char *var, const char *value, |
338 | void *cb) | |
0f6f5a40 | 339 | { |
e269eb79 JK |
340 | if (!strcmp(var, get_colorbool_slot)) |
341 | get_colorbool_found = git_config_colorbool(var, value); | |
342 | else if (!strcmp(var, "diff.color")) | |
343 | get_diff_color_found = git_config_colorbool(var, value); | |
344 | else if (!strcmp(var, "color.ui")) | |
c659f55b | 345 | get_color_ui_found = git_config_colorbool(var, value); |
0f6f5a40 JH |
346 | return 0; |
347 | } | |
348 | ||
d0e08d62 | 349 | static int get_colorbool(const char *var, int print) |
0f6f5a40 | 350 | { |
d0e08d62 | 351 | get_colorbool_slot = var; |
69243c2b JH |
352 | get_colorbool_found = -1; |
353 | get_diff_color_found = -1; | |
b8612b4d | 354 | get_color_ui_found = -1; |
270a3443 | 355 | git_config_with_options(git_get_colorbool_config, NULL, |
c48f4b37 | 356 | &given_config_source, &config_options); |
0f6f5a40 | 357 | |
69243c2b | 358 | if (get_colorbool_found < 0) { |
b408457f | 359 | if (!strcmp(get_colorbool_slot, "color.diff")) |
69243c2b JH |
360 | get_colorbool_found = get_diff_color_found; |
361 | if (get_colorbool_found < 0) | |
c659f55b | 362 | get_colorbool_found = get_color_ui_found; |
69243c2b JH |
363 | } |
364 | ||
b8612b4d MM |
365 | if (get_colorbool_found < 0) |
366 | /* default value if none found in config */ | |
4c7f1819 | 367 | get_colorbool_found = GIT_COLOR_AUTO; |
b8612b4d | 368 | |
daa0c3d9 JK |
369 | get_colorbool_found = want_color(get_colorbool_found); |
370 | ||
0e854a28 | 371 | if (print) { |
0f6f5a40 JH |
372 | printf("%s\n", get_colorbool_found ? "true" : "false"); |
373 | return 0; | |
0e854a28 FC |
374 | } else |
375 | return get_colorbool_found ? 0 : 1; | |
0f6f5a40 JH |
376 | } |
377 | ||
6aea9f0f | 378 | static void check_write(void) |
1bc88819 | 379 | { |
638fa623 JS |
380 | if (!given_config_source.file && !startup_info->have_repository) |
381 | die("not in a git directory"); | |
382 | ||
3caec73b KS |
383 | if (given_config_source.use_stdin) |
384 | die("writing to stdin is not supported"); | |
385 | ||
c8985ce0 | 386 | if (given_config_source.blob) |
1bc88819 HV |
387 | die("writing config blobs is not supported"); |
388 | } | |
389 | ||
d4770964 JH |
390 | struct urlmatch_current_candidate_value { |
391 | char value_is_null; | |
392 | struct strbuf value; | |
393 | }; | |
394 | ||
395 | static int urlmatch_collect_fn(const char *var, const char *value, void *cb) | |
396 | { | |
397 | struct string_list *values = cb; | |
398 | struct string_list_item *item = string_list_insert(values, var); | |
399 | struct urlmatch_current_candidate_value *matched = item->util; | |
400 | ||
401 | if (!matched) { | |
402 | matched = xmalloc(sizeof(*matched)); | |
403 | strbuf_init(&matched->value, 0); | |
404 | item->util = matched; | |
405 | } else { | |
406 | strbuf_reset(&matched->value); | |
407 | } | |
408 | ||
409 | if (value) { | |
410 | strbuf_addstr(&matched->value, value); | |
411 | matched->value_is_null = 0; | |
412 | } else { | |
413 | matched->value_is_null = 1; | |
414 | } | |
415 | return 0; | |
416 | } | |
417 | ||
d4770964 JH |
418 | static int get_urlmatch(const char *var, const char *url) |
419 | { | |
27b30be6 | 420 | int ret; |
d4770964 JH |
421 | char *section_tail; |
422 | struct string_list_item *item; | |
423 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; | |
424 | struct string_list values = STRING_LIST_INIT_DUP; | |
425 | ||
426 | config.collect_fn = urlmatch_collect_fn; | |
427 | config.cascade_fn = NULL; | |
428 | config.cb = &values; | |
429 | ||
430 | if (!url_normalize(url, &config.url)) | |
6667a6ac | 431 | die("%s", config.url.err); |
d4770964 | 432 | |
88d5a6f6 | 433 | config.section = xstrdup_tolower(var); |
d4770964 JH |
434 | section_tail = strchr(config.section, '.'); |
435 | if (section_tail) { | |
436 | *section_tail = '\0'; | |
437 | config.key = section_tail + 1; | |
438 | show_keys = 0; | |
439 | } else { | |
440 | config.key = NULL; | |
441 | show_keys = 1; | |
442 | } | |
443 | ||
444 | git_config_with_options(urlmatch_config_entry, &config, | |
c48f4b37 | 445 | &given_config_source, &config_options); |
d4770964 | 446 | |
27b30be6 JK |
447 | ret = !values.nr; |
448 | ||
d4770964 JH |
449 | for_each_string_list_item(item, &values) { |
450 | struct urlmatch_current_candidate_value *matched = item->util; | |
d4770964 JH |
451 | struct strbuf buf = STRBUF_INIT; |
452 | ||
a92330d2 | 453 | format_config(&buf, item->string, |
d4770964 JH |
454 | matched->value_is_null ? NULL : matched->value.buf); |
455 | fwrite(buf.buf, 1, buf.len, stdout); | |
d4770964 JH |
456 | strbuf_release(&buf); |
457 | ||
458 | strbuf_release(&matched->value); | |
459 | } | |
460 | string_list_clear(&config.vars, 1); | |
461 | string_list_clear(&values, 1); | |
462 | free(config.url.url); | |
463 | ||
464 | free((void *)config.section); | |
27b30be6 | 465 | return ret; |
d4770964 JH |
466 | } |
467 | ||
9830534e MM |
468 | static char *default_user_config(void) |
469 | { | |
470 | struct strbuf buf = STRBUF_INIT; | |
471 | strbuf_addf(&buf, | |
472 | _("# This is Git's per-user configuration file.\n" | |
7e110524 | 473 | "[user]\n" |
9830534e | 474 | "# Please adapt and uncomment the following lines:\n" |
7e110524 | 475 | "# name = %s\n" |
9830534e MM |
476 | "# email = %s\n"), |
477 | ident_default_name(), | |
478 | ident_default_email()); | |
479 | return strbuf_detach(&buf, NULL); | |
480 | } | |
481 | ||
3ba7e6e2 | 482 | int cmd_config(int argc, const char **argv, const char *prefix) |
1b1e59c5 | 483 | { |
3ba7e6e2 | 484 | int nongit = !startup_info->have_repository; |
4b951b7e | 485 | char *value; |
7162dff3 | 486 | |
c8985ce0 | 487 | given_config_source.file = getenv(CONFIG_ENVIRONMENT); |
dc871831 | 488 | |
37782920 SB |
489 | argc = parse_options(argc, argv, prefix, builtin_config_options, |
490 | builtin_config_usage, | |
d64ec16c FC |
491 | PARSE_OPT_STOP_AT_NON_OPTION); |
492 | ||
1bc88819 | 493 | if (use_global_config + use_system_config + use_local_config + |
c8985ce0 | 494 | !!given_config_source.file + !!given_config_source.blob > 1) { |
67052c9d FC |
495 | error("only one config file at a time."); |
496 | usage_with_options(builtin_config_usage, builtin_config_options); | |
497 | } | |
498 | ||
3caec73b KS |
499 | if (given_config_source.file && |
500 | !strcmp(given_config_source.file, "-")) { | |
501 | given_config_source.file = NULL; | |
502 | given_config_source.use_stdin = 1; | |
503 | } | |
504 | ||
d64ec16c | 505 | if (use_global_config) { |
4aad2f16 | 506 | char *user_config = expand_user_path("~/.gitconfig", 0); |
509adc33 | 507 | char *xdg_config = xdg_config_home("config"); |
21cf3227 | 508 | |
e3ebc35b MM |
509 | if (!user_config) |
510 | /* | |
511 | * It is unknown if HOME/.gitconfig exists, so | |
512 | * we do not know if we should write to XDG | |
513 | * location; error out even if XDG_CONFIG_HOME | |
514 | * is set and points at a sane location. | |
515 | */ | |
516 | die("$HOME not set"); | |
517 | ||
4698c8fe JN |
518 | if (access_or_warn(user_config, R_OK, 0) && |
519 | xdg_config && !access_or_warn(xdg_config, R_OK, 0)) | |
c8985ce0 | 520 | given_config_source.file = xdg_config; |
21cf3227 | 521 | else |
c8985ce0 | 522 | given_config_source.file = user_config; |
d64ec16c FC |
523 | } |
524 | else if (use_system_config) | |
c8985ce0 | 525 | given_config_source.file = git_etc_gitconfig(); |
57210a67 | 526 | else if (use_local_config) |
c8985ce0 KS |
527 | given_config_source.file = git_pathdup("config"); |
528 | else if (given_config_source.file) { | |
529 | if (!is_absolute_path(given_config_source.file) && prefix) | |
530 | given_config_source.file = | |
e4da43b1 | 531 | prefix_filename(prefix, given_config_source.file); |
d64ec16c FC |
532 | } |
533 | ||
c48f4b37 NTND |
534 | if (respect_includes_opt == -1) |
535 | config_options.respect_includes = !given_config_source.file; | |
536 | else | |
537 | config_options.respect_includes = respect_includes_opt; | |
9b25a0b5 | 538 | |
d64ec16c FC |
539 | if (end_null) { |
540 | term = '\0'; | |
541 | delim = '\n'; | |
542 | key_delim = '\n'; | |
543 | } | |
544 | ||
16c1e939 FC |
545 | if (HAS_MULTI_BITS(types)) { |
546 | error("only one type at a time."); | |
547 | usage_with_options(builtin_config_usage, builtin_config_options); | |
548 | } | |
549 | ||
d0e08d62 | 550 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) { |
c2387358 FC |
551 | error("--get-color and variable type are incoherent"); |
552 | usage_with_options(builtin_config_usage, builtin_config_options); | |
553 | } | |
554 | ||
d64ec16c FC |
555 | if (HAS_MULTI_BITS(actions)) { |
556 | error("only one action at a time."); | |
557 | usage_with_options(builtin_config_usage, builtin_config_options); | |
558 | } | |
559 | if (actions == 0) | |
560 | switch (argc) { | |
561 | case 1: actions = ACTION_GET; break; | |
562 | case 2: actions = ACTION_SET; break; | |
563 | case 3: actions = ACTION_SET_ALL; break; | |
564 | default: | |
565 | usage_with_options(builtin_config_usage, builtin_config_options); | |
db1696b8 | 566 | } |
578625fa SG |
567 | if (omit_values && |
568 | !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { | |
569 | error("--name-only is only applicable to --list or --get-regexp"); | |
570 | usage_with_options(builtin_config_usage, builtin_config_options); | |
571 | } | |
70bd879a LS |
572 | |
573 | if (show_origin && !(actions & | |
574 | (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { | |
575 | error("--show-origin is only applicable to --get, --get-all, " | |
576 | "--get-regexp, and --list."); | |
577 | usage_with_options(builtin_config_usage, builtin_config_options); | |
578 | } | |
579 | ||
d64ec16c | 580 | if (actions == ACTION_LIST) { |
225a9caf | 581 | check_argc(argc, 0, 0); |
270a3443 | 582 | if (git_config_with_options(show_all_config, NULL, |
c8985ce0 | 583 | &given_config_source, |
c48f4b37 | 584 | &config_options) < 0) { |
c8985ce0 | 585 | if (given_config_source.file) |
d824cbba | 586 | die_errno("unable to read config file '%s'", |
c8985ce0 | 587 | given_config_source.file); |
d64ec16c FC |
588 | else |
589 | die("error processing config file(s)"); | |
db1696b8 | 590 | } |
1b1e59c5 | 591 | } |
d64ec16c | 592 | else if (actions == ACTION_EDIT) { |
3696a7c2 MH |
593 | char *config_file; |
594 | ||
225a9caf | 595 | check_argc(argc, 0, 0); |
c8985ce0 | 596 | if (!given_config_source.file && nongit) |
d212ca17 | 597 | die("not in a git directory"); |
3caec73b KS |
598 | if (given_config_source.use_stdin) |
599 | die("editing stdin is not supported"); | |
c8985ce0 | 600 | if (given_config_source.blob) |
1bc88819 | 601 | die("editing blobs is not supported"); |
d64ec16c | 602 | git_config(git_default_config, NULL); |
d9c69644 JK |
603 | config_file = given_config_source.file ? |
604 | xstrdup(given_config_source.file) : | |
605 | git_pathdup("config"); | |
9830534e MM |
606 | if (use_global_config) { |
607 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
aabbd3f3 | 608 | if (fd >= 0) { |
9830534e MM |
609 | char *content = default_user_config(); |
610 | write_str_in_full(fd, content); | |
611 | free(content); | |
612 | close(fd); | |
613 | } | |
614 | else if (errno != EEXIST) | |
615 | die_errno(_("cannot create configuration file %s"), config_file); | |
616 | } | |
617 | launch_editor(config_file, NULL, NULL); | |
3696a7c2 | 618 | free(config_file); |
d64ec16c FC |
619 | } |
620 | else if (actions == ACTION_SET) { | |
5a2df368 | 621 | int ret; |
6aea9f0f | 622 | check_write(); |
d64ec16c FC |
623 | check_argc(argc, 2, 2); |
624 | value = normalize_value(argv[0], argv[1]); | |
30598ad0 | 625 | ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value); |
5a2df368 | 626 | if (ret == CONFIG_NOTHING_SET) |
ccf63801 VA |
627 | error(_("cannot overwrite multiple values with a single value\n" |
628 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); | |
5a2df368 | 629 | return ret; |
d64ec16c FC |
630 | } |
631 | else if (actions == ACTION_SET_ALL) { | |
6aea9f0f | 632 | check_write(); |
d64ec16c FC |
633 | check_argc(argc, 2, 3); |
634 | value = normalize_value(argv[0], argv[1]); | |
30598ad0 PS |
635 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
636 | argv[0], value, argv[2], 0); | |
d64ec16c FC |
637 | } |
638 | else if (actions == ACTION_ADD) { | |
6aea9f0f | 639 | check_write(); |
d64ec16c FC |
640 | check_argc(argc, 2, 2); |
641 | value = normalize_value(argv[0], argv[1]); | |
30598ad0 PS |
642 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
643 | argv[0], value, | |
644 | CONFIG_REGEX_NONE, 0); | |
d64ec16c FC |
645 | } |
646 | else if (actions == ACTION_REPLACE_ALL) { | |
6aea9f0f | 647 | check_write(); |
d64ec16c FC |
648 | check_argc(argc, 2, 3); |
649 | value = normalize_value(argv[0], argv[1]); | |
30598ad0 PS |
650 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
651 | argv[0], value, argv[2], 1); | |
d64ec16c FC |
652 | } |
653 | else if (actions == ACTION_GET) { | |
654 | check_argc(argc, 1, 2); | |
655 | return get_value(argv[0], argv[1]); | |
656 | } | |
657 | else if (actions == ACTION_GET_ALL) { | |
658 | do_all = 1; | |
659 | check_argc(argc, 1, 2); | |
660 | return get_value(argv[0], argv[1]); | |
661 | } | |
662 | else if (actions == ACTION_GET_REGEXP) { | |
663 | show_keys = 1; | |
664 | use_key_regexp = 1; | |
665 | do_all = 1; | |
666 | check_argc(argc, 1, 2); | |
667 | return get_value(argv[0], argv[1]); | |
668 | } | |
d4770964 JH |
669 | else if (actions == ACTION_GET_URLMATCH) { |
670 | check_argc(argc, 2, 2); | |
671 | return get_urlmatch(argv[0], argv[1]); | |
672 | } | |
d64ec16c | 673 | else if (actions == ACTION_UNSET) { |
6aea9f0f | 674 | check_write(); |
d64ec16c FC |
675 | check_argc(argc, 1, 2); |
676 | if (argc == 2) | |
30598ad0 PS |
677 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
678 | argv[0], NULL, argv[1], 0); | |
d64ec16c | 679 | else |
30598ad0 PS |
680 | return git_config_set_in_file_gently(given_config_source.file, |
681 | argv[0], NULL); | |
d64ec16c FC |
682 | } |
683 | else if (actions == ACTION_UNSET_ALL) { | |
6aea9f0f | 684 | check_write(); |
d64ec16c | 685 | check_argc(argc, 1, 2); |
30598ad0 PS |
686 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
687 | argv[0], NULL, argv[1], 1); | |
d64ec16c FC |
688 | } |
689 | else if (actions == ACTION_RENAME_SECTION) { | |
690 | int ret; | |
6aea9f0f | 691 | check_write(); |
d64ec16c | 692 | check_argc(argc, 2, 2); |
c8985ce0 | 693 | ret = git_config_rename_section_in_file(given_config_source.file, |
270a3443 | 694 | argv[0], argv[1]); |
d64ec16c FC |
695 | if (ret < 0) |
696 | return ret; | |
697 | if (ret == 0) | |
698 | die("No such section!"); | |
699 | } | |
700 | else if (actions == ACTION_REMOVE_SECTION) { | |
701 | int ret; | |
6aea9f0f | 702 | check_write(); |
d64ec16c | 703 | check_argc(argc, 1, 1); |
c8985ce0 | 704 | ret = git_config_rename_section_in_file(given_config_source.file, |
270a3443 | 705 | argv[0], NULL); |
d64ec16c FC |
706 | if (ret < 0) |
707 | return ret; | |
708 | if (ret == 0) | |
709 | die("No such section!"); | |
710 | } | |
711 | else if (actions == ACTION_GET_COLOR) { | |
d0e08d62 JK |
712 | check_argc(argc, 1, 2); |
713 | get_color(argv[0], argv[1]); | |
d64ec16c FC |
714 | } |
715 | else if (actions == ACTION_GET_COLORBOOL) { | |
d0e08d62 JK |
716 | check_argc(argc, 1, 2); |
717 | if (argc == 2) | |
718 | color_stdout_is_tty = git_config_bool("command line", argv[1]); | |
719 | return get_colorbool(argv[0], argc == 2); | |
d64ec16c FC |
720 | } |
721 | ||
1b1e59c5 JS |
722 | return 0; |
723 | } |