]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/config.c
config: parse http.<url>.<variable> using urlmatch
[thirdparty/git.git] / builtin / config.c
CommitLineData
e12c095a 1#include "builtin.h"
1b1e59c5 2#include "cache.h"
9ce03522 3#include "color.h"
d64ec16c 4#include "parse-options.h"
1b1e59c5 5
d64ec16c 6static const char *const builtin_config_usage[] = {
1bd31ce6 7 N_("git config [options]"),
d64ec16c
FC
8 NULL
9};
4ddba79d 10
96f1e58f
DR
11static char *key;
12static regex_t *key_regexp;
13static regex_t *regexp;
14static int show_keys;
15static int use_key_regexp;
16static int do_all;
17static int do_not_match;
2275d502
FL
18static char delim = '=';
19static char key_delim = ' ';
20static char term = '\n';
4ddba79d 21
57210a67 22static int use_global_config, use_system_config, use_local_config;
d64ec16c 23static const char *given_config_file;
16c1e939 24static int actions, types;
d64ec16c
FC
25static const char *get_color_slot, *get_colorbool_slot;
26static int end_null;
9b25a0b5 27static int respect_includes = -1;
d64ec16c
FC
28
29#define ACTION_GET (1<<0)
30#define ACTION_GET_ALL (1<<1)
31#define ACTION_GET_REGEXP (1<<2)
32#define ACTION_REPLACE_ALL (1<<3)
33#define ACTION_ADD (1<<4)
34#define ACTION_UNSET (1<<5)
35#define ACTION_UNSET_ALL (1<<6)
36#define ACTION_RENAME_SECTION (1<<7)
37#define ACTION_REMOVE_SECTION (1<<8)
38#define ACTION_LIST (1<<9)
39#define ACTION_EDIT (1<<10)
40#define ACTION_SET (1<<11)
41#define ACTION_SET_ALL (1<<12)
42#define ACTION_GET_COLOR (1<<13)
43#define ACTION_GET_COLORBOOL (1<<14)
44
16c1e939
FC
45#define TYPE_BOOL (1<<0)
46#define TYPE_INT (1<<1)
47#define TYPE_BOOL_OR_INT (1<<2)
1349484e 48#define TYPE_PATH (1<<3)
16c1e939 49
d64ec16c 50static struct option builtin_config_options[] = {
1bd31ce6
NTND
51 OPT_GROUP(N_("Config file location")),
52 OPT_BOOLEAN(0, "global", &use_global_config, N_("use global config file")),
53 OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")),
54 OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")),
55 OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
56 OPT_GROUP(N_("Action")),
57 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
58 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
59 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
60 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
f63cf8c9
NTND
61 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
62 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
63 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
1bd31ce6
NTND
64 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
65 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
66 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
f63cf8c9 67 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
1bd31ce6
NTND
68 OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
69 OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
70 OPT_GROUP(N_("Type")),
71 OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
72 OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
73 OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
74 OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
75 OPT_GROUP(N_("Other")),
76 OPT_BOOLEAN('z', "null", &end_null, N_("terminate values with NUL byte")),
77 OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
d64ec16c
FC
78 OPT_END(),
79};
80
81static void check_argc(int argc, int min, int max) {
82 if (argc >= min && argc <= max)
83 return;
84 error("wrong number of arguments");
85 usage_with_options(builtin_config_usage, builtin_config_options);
86}
87
ef90d6d4 88static int show_all_config(const char *key_, const char *value_, void *cb)
de791f15
PB
89{
90 if (value_)
2275d502 91 printf("%s%c%s%c", key_, delim, value_, term);
de791f15 92 else
2275d502 93 printf("%s%c", key_, term);
de791f15
PB
94 return 0;
95}
96
7acdd6f0
JK
97struct strbuf_list {
98 struct strbuf *items;
99 int nr;
100 int alloc;
101};
102
103static int collect_config(const char *key_, const char *value_, void *cb)
4ddba79d 104{
7acdd6f0
JK
105 struct strbuf_list *values = cb;
106 struct strbuf *buf;
e098c6f8
JH
107 char value[256];
108 const char *vptr = value;
1349484e 109 int must_free_vptr = 0;
008e3cc5 110 int must_print_delim = 0;
e098c6f8 111
8f5ff31f
JS
112 if (!use_key_regexp && strcmp(key_, key))
113 return 0;
114 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
115 return 0;
116 if (regexp != NULL &&
9e4bbeb9 117 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
8f5ff31f
JS
118 return 0;
119
7acdd6f0
JK
120 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
121 buf = &values->items[values->nr++];
122 strbuf_init(buf, 0);
123
b69ba460 124 if (show_keys) {
7acdd6f0 125 strbuf_addstr(buf, key_);
008e3cc5 126 must_print_delim = 1;
b69ba460 127 }
16c1e939 128 if (types == TYPE_INT)
acb70149 129 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
16c1e939 130 else if (types == TYPE_BOOL)
8f5ff31f 131 vptr = git_config_bool(key_, value_) ? "true" : "false";
16c1e939 132 else if (types == TYPE_BOOL_OR_INT) {
c35b0b58
JH
133 int is_bool, v;
134 v = git_config_bool_or_int(key_, value_, &is_bool);
135 if (is_bool)
136 vptr = v ? "true" : "false";
137 else
138 sprintf(value, "%d", v);
1349484e 139 } else if (types == TYPE_PATH) {
962c38ee
CMN
140 if (git_config_pathname(&vptr, key_, value_) < 0)
141 return -1;
1349484e 142 must_free_vptr = 1;
008e3cc5
MM
143 } else if (value_) {
144 vptr = value_;
145 } else {
146 /* Just show the key name */
147 vptr = "";
148 must_print_delim = 0;
c35b0b58 149 }
00b347d3
JK
150
151 if (must_print_delim)
152 strbuf_addch(buf, key_delim);
153 strbuf_addstr(buf, vptr);
154 strbuf_addch(buf, term);
155
1349484e
MM
156 if (must_free_vptr)
157 /* If vptr must be freed, it's a pointer to a
158 * dynamically allocated buffer, it's safe to cast to
159 * const.
160 */
161 free((char *)vptr);
8f5ff31f 162
4ddba79d
JS
163 return 0;
164}
165
4b951b7e 166static int get_value(const char *key_, const char *regex_)
4ddba79d 167{
9409c7a5 168 int ret = CONFIG_GENERIC_ERROR;
5ba1a8a7 169 struct strbuf_list values = {NULL};
7acdd6f0 170 int i;
4ddba79d 171
2fa9a0fb 172 if (use_key_regexp) {
b09c53a3
LP
173 char *tl;
174
175 /*
176 * NEEDSWORK: this naive pattern lowercasing obviously does not
177 * work for more complex patterns like "^[^.]*Foo.*bar".
178 * Perhaps we should deprecate this altogether someday.
179 */
180
181 key = xstrdup(key_);
182 for (tl = key + strlen(key) - 1;
183 tl >= key && *tl != '.';
184 tl--)
185 *tl = tolower(*tl);
186 for (tl = key; *tl && *tl != '.'; tl++)
187 *tl = tolower(*tl);
188
2d7320d0 189 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
2fa9a0fb 190 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 191 fprintf(stderr, "Invalid key pattern: %s\n", key_);
97ed50f9
JK
192 free(key_regexp);
193 key_regexp = NULL;
9409c7a5 194 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 195 goto free_strings;
2fa9a0fb 196 }
b09c53a3 197 } else {
9409c7a5
JH
198 if (git_config_parse_key(key_, &key, NULL)) {
199 ret = CONFIG_INVALID_KEY;
b09c53a3 200 goto free_strings;
9409c7a5 201 }
2fa9a0fb
JS
202 }
203
4ddba79d 204 if (regex_) {
f98d863d
JS
205 if (regex_[0] == '!') {
206 do_not_match = 1;
207 regex_++;
208 }
209
2d7320d0 210 regexp = (regex_t*)xmalloc(sizeof(regex_t));
0a152171 211 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d 212 fprintf(stderr, "Invalid pattern: %s\n", regex_);
97ed50f9
JK
213 free(regexp);
214 regexp = NULL;
9409c7a5 215 ret = CONFIG_INVALID_PATTERN;
5f1a63e0 216 goto free_strings;
4ddba79d
JS
217 }
218 }
219
e8955898
JK
220 git_config_with_options(collect_config, &values,
221 given_config_file, respect_includes);
5f1a63e0 222
00b347d3 223 ret = !values.nr;
9b25a0b5 224
7acdd6f0
JK
225 for (i = 0; i < values.nr; i++) {
226 struct strbuf *buf = values.items + i;
00b347d3
JK
227 if (do_all || i == values.nr - 1)
228 fwrite(buf->buf, 1, buf->len, stdout);
7acdd6f0
JK
229 strbuf_release(buf);
230 }
231 free(values.items);
5f1a63e0 232
97ed50f9 233free_strings:
4ddba79d 234 free(key);
35998c89
JK
235 if (key_regexp) {
236 regfree(key_regexp);
237 free(key_regexp);
238 }
0a152171
AW
239 if (regexp) {
240 regfree(regexp);
241 free(regexp);
4ddba79d
JS
242 }
243
5f1a63e0 244 return ret;
4ddba79d 245}
1b1e59c5 246
186458b1 247static char *normalize_value(const char *key, const char *value)
db1696b8
FL
248{
249 char *normalized;
250
251 if (!value)
252 return NULL;
253
1349484e
MM
254 if (types == 0 || types == TYPE_PATH)
255 /*
256 * We don't do normalization for TYPE_PATH here: If
257 * the path is like ~/foobar/, we prefer to store
258 * "~/foobar/" in the config file, and to expand the ~
259 * when retrieving the value.
260 */
db1696b8
FL
261 normalized = xstrdup(value);
262 else {
263 normalized = xmalloc(64);
16c1e939 264 if (types == TYPE_INT) {
db1696b8
FL
265 int v = git_config_int(key, value);
266 sprintf(normalized, "%d", v);
267 }
16c1e939 268 else if (types == TYPE_BOOL)
db1696b8
FL
269 sprintf(normalized, "%s",
270 git_config_bool(key, value) ? "true" : "false");
16c1e939 271 else if (types == TYPE_BOOL_OR_INT) {
c35b0b58
JH
272 int is_bool, v;
273 v = git_config_bool_or_int(key, value, &is_bool);
274 if (!is_bool)
275 sprintf(normalized, "%d", v);
276 else
277 sprintf(normalized, "%s", v ? "true" : "false");
278 }
db1696b8
FL
279 }
280
281 return normalized;
282}
283
9ce03522
JH
284static int get_color_found;
285static const char *get_color_slot;
b408457f 286static const char *get_colorbool_slot;
9ce03522
JH
287static char parsed_color[COLOR_MAXLEN];
288
ef90d6d4 289static int git_get_color_config(const char *var, const char *value, void *cb)
9ce03522
JH
290{
291 if (!strcmp(var, get_color_slot)) {
f769982d
JH
292 if (!value)
293 config_error_nonbool(var);
9ce03522
JH
294 color_parse(value, var, parsed_color);
295 get_color_found = 1;
296 }
297 return 0;
298}
299
0e854a28 300static void get_color(const char *def_color)
9ce03522 301{
9ce03522
JH
302 get_color_found = 0;
303 parsed_color[0] = '\0';
270a3443 304 git_config_with_options(git_get_color_config, NULL,
9b25a0b5 305 given_config_file, respect_includes);
9ce03522
JH
306
307 if (!get_color_found && def_color)
308 color_parse(def_color, "command line", parsed_color);
309
310 fputs(parsed_color, stdout);
9ce03522
JH
311}
312
0f6f5a40 313static int get_colorbool_found;
69243c2b 314static int get_diff_color_found;
c659f55b 315static int get_color_ui_found;
ef90d6d4
JS
316static int git_get_colorbool_config(const char *var, const char *value,
317 void *cb)
0f6f5a40 318{
e269eb79
JK
319 if (!strcmp(var, get_colorbool_slot))
320 get_colorbool_found = git_config_colorbool(var, value);
321 else if (!strcmp(var, "diff.color"))
322 get_diff_color_found = git_config_colorbool(var, value);
323 else if (!strcmp(var, "color.ui"))
c659f55b 324 get_color_ui_found = git_config_colorbool(var, value);
0f6f5a40
JH
325 return 0;
326}
327
0e854a28 328static int get_colorbool(int print)
0f6f5a40 329{
69243c2b
JH
330 get_colorbool_found = -1;
331 get_diff_color_found = -1;
270a3443 332 git_config_with_options(git_get_colorbool_config, NULL,
9b25a0b5 333 given_config_file, respect_includes);
0f6f5a40 334
69243c2b 335 if (get_colorbool_found < 0) {
b408457f 336 if (!strcmp(get_colorbool_slot, "color.diff"))
69243c2b
JH
337 get_colorbool_found = get_diff_color_found;
338 if (get_colorbool_found < 0)
c659f55b 339 get_colorbool_found = get_color_ui_found;
69243c2b
JH
340 }
341
daa0c3d9
JK
342 get_colorbool_found = want_color(get_colorbool_found);
343
0e854a28 344 if (print) {
0f6f5a40
JH
345 printf("%s\n", get_colorbool_found ? "true" : "false");
346 return 0;
0e854a28
FC
347 } else
348 return get_colorbool_found ? 0 : 1;
0f6f5a40
JH
349}
350
3ba7e6e2 351int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 352{
3ba7e6e2 353 int nongit = !startup_info->have_repository;
4b951b7e 354 char *value;
7162dff3 355
270a3443 356 given_config_file = getenv(CONFIG_ENVIRONMENT);
dc871831 357
37782920
SB
358 argc = parse_options(argc, argv, prefix, builtin_config_options,
359 builtin_config_usage,
d64ec16c
FC
360 PARSE_OPT_STOP_AT_NON_OPTION);
361
57210a67 362 if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
67052c9d
FC
363 error("only one config file at a time.");
364 usage_with_options(builtin_config_usage, builtin_config_options);
365 }
366
d64ec16c 367 if (use_global_config) {
21cf3227
HKNN
368 char *user_config = NULL;
369 char *xdg_config = NULL;
370
371 home_config_paths(&user_config, &xdg_config, "config");
372
e3ebc35b
MM
373 if (!user_config)
374 /*
375 * It is unknown if HOME/.gitconfig exists, so
376 * we do not know if we should write to XDG
377 * location; error out even if XDG_CONFIG_HOME
378 * is set and points at a sane location.
379 */
380 die("$HOME not set");
381
ba8bd830
JK
382 if (access_or_warn(user_config, R_OK) &&
383 xdg_config && !access_or_warn(xdg_config, R_OK))
21cf3227 384 given_config_file = xdg_config;
21cf3227 385 else
e3ebc35b 386 given_config_file = user_config;
d64ec16c
FC
387 }
388 else if (use_system_config)
270a3443 389 given_config_file = git_etc_gitconfig();
57210a67 390 else if (use_local_config)
270a3443 391 given_config_file = git_pathdup("config");
d64ec16c
FC
392 else if (given_config_file) {
393 if (!is_absolute_path(given_config_file) && prefix)
270a3443 394 given_config_file =
839de252
JK
395 xstrdup(prefix_filename(prefix,
396 strlen(prefix),
397 given_config_file));
d64ec16c
FC
398 }
399
9b25a0b5
JK
400 if (respect_includes == -1)
401 respect_includes = !given_config_file;
402
d64ec16c
FC
403 if (end_null) {
404 term = '\0';
405 delim = '\n';
406 key_delim = '\n';
407 }
408
16c1e939
FC
409 if (HAS_MULTI_BITS(types)) {
410 error("only one type at a time.");
411 usage_with_options(builtin_config_usage, builtin_config_options);
412 }
413
d64ec16c
FC
414 if (get_color_slot)
415 actions |= ACTION_GET_COLOR;
416 if (get_colorbool_slot)
417 actions |= ACTION_GET_COLORBOOL;
418
c2387358
FC
419 if ((get_color_slot || get_colorbool_slot) && types) {
420 error("--get-color and variable type are incoherent");
421 usage_with_options(builtin_config_usage, builtin_config_options);
422 }
423
d64ec16c
FC
424 if (HAS_MULTI_BITS(actions)) {
425 error("only one action at a time.");
426 usage_with_options(builtin_config_usage, builtin_config_options);
427 }
428 if (actions == 0)
429 switch (argc) {
430 case 1: actions = ACTION_GET; break;
431 case 2: actions = ACTION_SET; break;
432 case 3: actions = ACTION_SET_ALL; break;
433 default:
434 usage_with_options(builtin_config_usage, builtin_config_options);
db1696b8 435 }
d64ec16c
FC
436
437 if (actions == ACTION_LIST) {
225a9caf 438 check_argc(argc, 0, 0);
270a3443 439 if (git_config_with_options(show_all_config, NULL,
9b25a0b5
JK
440 given_config_file,
441 respect_includes) < 0) {
270a3443 442 if (given_config_file)
d824cbba 443 die_errno("unable to read config file '%s'",
270a3443 444 given_config_file);
d64ec16c
FC
445 else
446 die("error processing config file(s)");
db1696b8 447 }
1b1e59c5 448 }
d64ec16c 449 else if (actions == ACTION_EDIT) {
225a9caf 450 check_argc(argc, 0, 0);
270a3443 451 if (!given_config_file && nongit)
d212ca17 452 die("not in a git directory");
d64ec16c 453 git_config(git_default_config, NULL);
270a3443
JK
454 launch_editor(given_config_file ?
455 given_config_file : git_path("config"),
d64ec16c
FC
456 NULL, NULL);
457 }
458 else if (actions == ACTION_SET) {
5a2df368 459 int ret;
d64ec16c
FC
460 check_argc(argc, 2, 2);
461 value = normalize_value(argv[0], argv[1]);
270a3443 462 ret = git_config_set_in_file(given_config_file, argv[0], value);
5a2df368
MG
463 if (ret == CONFIG_NOTHING_SET)
464 error("cannot overwrite multiple values with a single value\n"
67e223ed 465 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
5a2df368 466 return ret;
d64ec16c
FC
467 }
468 else if (actions == ACTION_SET_ALL) {
469 check_argc(argc, 2, 3);
470 value = normalize_value(argv[0], argv[1]);
270a3443
JK
471 return git_config_set_multivar_in_file(given_config_file,
472 argv[0], value, argv[2], 0);
d64ec16c
FC
473 }
474 else if (actions == ACTION_ADD) {
475 check_argc(argc, 2, 2);
476 value = normalize_value(argv[0], argv[1]);
270a3443
JK
477 return git_config_set_multivar_in_file(given_config_file,
478 argv[0], value, "^$", 0);
d64ec16c
FC
479 }
480 else if (actions == ACTION_REPLACE_ALL) {
481 check_argc(argc, 2, 3);
482 value = normalize_value(argv[0], argv[1]);
270a3443
JK
483 return git_config_set_multivar_in_file(given_config_file,
484 argv[0], value, argv[2], 1);
d64ec16c
FC
485 }
486 else if (actions == ACTION_GET) {
487 check_argc(argc, 1, 2);
488 return get_value(argv[0], argv[1]);
489 }
490 else if (actions == ACTION_GET_ALL) {
491 do_all = 1;
492 check_argc(argc, 1, 2);
493 return get_value(argv[0], argv[1]);
494 }
495 else if (actions == ACTION_GET_REGEXP) {
496 show_keys = 1;
497 use_key_regexp = 1;
498 do_all = 1;
499 check_argc(argc, 1, 2);
500 return get_value(argv[0], argv[1]);
501 }
502 else if (actions == ACTION_UNSET) {
503 check_argc(argc, 1, 2);
504 if (argc == 2)
270a3443
JK
505 return git_config_set_multivar_in_file(given_config_file,
506 argv[0], NULL, argv[1], 0);
d64ec16c 507 else
270a3443
JK
508 return git_config_set_in_file(given_config_file,
509 argv[0], NULL);
d64ec16c
FC
510 }
511 else if (actions == ACTION_UNSET_ALL) {
512 check_argc(argc, 1, 2);
270a3443
JK
513 return git_config_set_multivar_in_file(given_config_file,
514 argv[0], NULL, argv[1], 1);
d64ec16c
FC
515 }
516 else if (actions == ACTION_RENAME_SECTION) {
517 int ret;
518 check_argc(argc, 2, 2);
270a3443
JK
519 ret = git_config_rename_section_in_file(given_config_file,
520 argv[0], argv[1]);
d64ec16c
FC
521 if (ret < 0)
522 return ret;
523 if (ret == 0)
524 die("No such section!");
525 }
526 else if (actions == ACTION_REMOVE_SECTION) {
527 int ret;
528 check_argc(argc, 1, 1);
270a3443
JK
529 ret = git_config_rename_section_in_file(given_config_file,
530 argv[0], NULL);
d64ec16c
FC
531 if (ret < 0)
532 return ret;
533 if (ret == 0)
534 die("No such section!");
535 }
536 else if (actions == ACTION_GET_COLOR) {
537 get_color(argv[0]);
538 }
539 else if (actions == ACTION_GET_COLORBOOL) {
540 if (argc == 1)
e269eb79 541 color_stdout_is_tty = git_config_bool("command line", argv[0]);
d64ec16c
FC
542 return get_colorbool(argc != 0);
543 }
544
1b1e59c5
JS
545 return 0;
546}
6390c905
RS
547
548int cmd_repo_config(int argc, const char **argv, const char *prefix)
549{
550 fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
551 return cmd_config(argc, argv, prefix);
552}