]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/config.c
config: refactor management of color.ui's default value
[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;
b8612b4d 332 get_color_ui_found = -1;
270a3443 333 git_config_with_options(git_get_colorbool_config, NULL,
9b25a0b5 334 given_config_file, respect_includes);
0f6f5a40 335
69243c2b 336 if (get_colorbool_found < 0) {
b408457f 337 if (!strcmp(get_colorbool_slot, "color.diff"))
69243c2b
JH
338 get_colorbool_found = get_diff_color_found;
339 if (get_colorbool_found < 0)
c659f55b 340 get_colorbool_found = get_color_ui_found;
69243c2b
JH
341 }
342
b8612b4d
MM
343 if (get_colorbool_found < 0)
344 /* default value if none found in config */
345 get_colorbool_found = 0;
346
daa0c3d9
JK
347 get_colorbool_found = want_color(get_colorbool_found);
348
0e854a28 349 if (print) {
0f6f5a40
JH
350 printf("%s\n", get_colorbool_found ? "true" : "false");
351 return 0;
0e854a28
FC
352 } else
353 return get_colorbool_found ? 0 : 1;
0f6f5a40
JH
354}
355
3ba7e6e2 356int cmd_config(int argc, const char **argv, const char *prefix)
1b1e59c5 357{
3ba7e6e2 358 int nongit = !startup_info->have_repository;
4b951b7e 359 char *value;
7162dff3 360
270a3443 361 given_config_file = getenv(CONFIG_ENVIRONMENT);
dc871831 362
37782920
SB
363 argc = parse_options(argc, argv, prefix, builtin_config_options,
364 builtin_config_usage,
d64ec16c
FC
365 PARSE_OPT_STOP_AT_NON_OPTION);
366
57210a67 367 if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
67052c9d
FC
368 error("only one config file at a time.");
369 usage_with_options(builtin_config_usage, builtin_config_options);
370 }
371
d64ec16c 372 if (use_global_config) {
21cf3227
HKNN
373 char *user_config = NULL;
374 char *xdg_config = NULL;
375
376 home_config_paths(&user_config, &xdg_config, "config");
377
e3ebc35b
MM
378 if (!user_config)
379 /*
380 * It is unknown if HOME/.gitconfig exists, so
381 * we do not know if we should write to XDG
382 * location; error out even if XDG_CONFIG_HOME
383 * is set and points at a sane location.
384 */
385 die("$HOME not set");
386
ba8bd830
JK
387 if (access_or_warn(user_config, R_OK) &&
388 xdg_config && !access_or_warn(xdg_config, R_OK))
21cf3227 389 given_config_file = xdg_config;
21cf3227 390 else
e3ebc35b 391 given_config_file = user_config;
d64ec16c
FC
392 }
393 else if (use_system_config)
270a3443 394 given_config_file = git_etc_gitconfig();
57210a67 395 else if (use_local_config)
270a3443 396 given_config_file = git_pathdup("config");
d64ec16c
FC
397 else if (given_config_file) {
398 if (!is_absolute_path(given_config_file) && prefix)
270a3443 399 given_config_file =
839de252
JK
400 xstrdup(prefix_filename(prefix,
401 strlen(prefix),
402 given_config_file));
d64ec16c
FC
403 }
404
9b25a0b5
JK
405 if (respect_includes == -1)
406 respect_includes = !given_config_file;
407
d64ec16c
FC
408 if (end_null) {
409 term = '\0';
410 delim = '\n';
411 key_delim = '\n';
412 }
413
16c1e939
FC
414 if (HAS_MULTI_BITS(types)) {
415 error("only one type at a time.");
416 usage_with_options(builtin_config_usage, builtin_config_options);
417 }
418
d64ec16c
FC
419 if (get_color_slot)
420 actions |= ACTION_GET_COLOR;
421 if (get_colorbool_slot)
422 actions |= ACTION_GET_COLORBOOL;
423
c2387358
FC
424 if ((get_color_slot || get_colorbool_slot) && types) {
425 error("--get-color and variable type are incoherent");
426 usage_with_options(builtin_config_usage, builtin_config_options);
427 }
428
d64ec16c
FC
429 if (HAS_MULTI_BITS(actions)) {
430 error("only one action at a time.");
431 usage_with_options(builtin_config_usage, builtin_config_options);
432 }
433 if (actions == 0)
434 switch (argc) {
435 case 1: actions = ACTION_GET; break;
436 case 2: actions = ACTION_SET; break;
437 case 3: actions = ACTION_SET_ALL; break;
438 default:
439 usage_with_options(builtin_config_usage, builtin_config_options);
db1696b8 440 }
d64ec16c
FC
441
442 if (actions == ACTION_LIST) {
225a9caf 443 check_argc(argc, 0, 0);
270a3443 444 if (git_config_with_options(show_all_config, NULL,
9b25a0b5
JK
445 given_config_file,
446 respect_includes) < 0) {
270a3443 447 if (given_config_file)
d824cbba 448 die_errno("unable to read config file '%s'",
270a3443 449 given_config_file);
d64ec16c
FC
450 else
451 die("error processing config file(s)");
db1696b8 452 }
1b1e59c5 453 }
d64ec16c 454 else if (actions == ACTION_EDIT) {
225a9caf 455 check_argc(argc, 0, 0);
270a3443 456 if (!given_config_file && nongit)
d212ca17 457 die("not in a git directory");
d64ec16c 458 git_config(git_default_config, NULL);
270a3443
JK
459 launch_editor(given_config_file ?
460 given_config_file : git_path("config"),
d64ec16c
FC
461 NULL, NULL);
462 }
463 else if (actions == ACTION_SET) {
5a2df368 464 int ret;
d64ec16c
FC
465 check_argc(argc, 2, 2);
466 value = normalize_value(argv[0], argv[1]);
270a3443 467 ret = git_config_set_in_file(given_config_file, argv[0], value);
5a2df368
MG
468 if (ret == CONFIG_NOTHING_SET)
469 error("cannot overwrite multiple values with a single value\n"
67e223ed 470 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
5a2df368 471 return ret;
d64ec16c
FC
472 }
473 else if (actions == ACTION_SET_ALL) {
474 check_argc(argc, 2, 3);
475 value = normalize_value(argv[0], argv[1]);
270a3443
JK
476 return git_config_set_multivar_in_file(given_config_file,
477 argv[0], value, argv[2], 0);
d64ec16c
FC
478 }
479 else if (actions == ACTION_ADD) {
480 check_argc(argc, 2, 2);
481 value = normalize_value(argv[0], argv[1]);
270a3443
JK
482 return git_config_set_multivar_in_file(given_config_file,
483 argv[0], value, "^$", 0);
d64ec16c
FC
484 }
485 else if (actions == ACTION_REPLACE_ALL) {
486 check_argc(argc, 2, 3);
487 value = normalize_value(argv[0], argv[1]);
270a3443
JK
488 return git_config_set_multivar_in_file(given_config_file,
489 argv[0], value, argv[2], 1);
d64ec16c
FC
490 }
491 else if (actions == ACTION_GET) {
492 check_argc(argc, 1, 2);
493 return get_value(argv[0], argv[1]);
494 }
495 else if (actions == ACTION_GET_ALL) {
496 do_all = 1;
497 check_argc(argc, 1, 2);
498 return get_value(argv[0], argv[1]);
499 }
500 else if (actions == ACTION_GET_REGEXP) {
501 show_keys = 1;
502 use_key_regexp = 1;
503 do_all = 1;
504 check_argc(argc, 1, 2);
505 return get_value(argv[0], argv[1]);
506 }
507 else if (actions == ACTION_UNSET) {
508 check_argc(argc, 1, 2);
509 if (argc == 2)
270a3443
JK
510 return git_config_set_multivar_in_file(given_config_file,
511 argv[0], NULL, argv[1], 0);
d64ec16c 512 else
270a3443
JK
513 return git_config_set_in_file(given_config_file,
514 argv[0], NULL);
d64ec16c
FC
515 }
516 else if (actions == ACTION_UNSET_ALL) {
517 check_argc(argc, 1, 2);
270a3443
JK
518 return git_config_set_multivar_in_file(given_config_file,
519 argv[0], NULL, argv[1], 1);
d64ec16c
FC
520 }
521 else if (actions == ACTION_RENAME_SECTION) {
522 int ret;
523 check_argc(argc, 2, 2);
270a3443
JK
524 ret = git_config_rename_section_in_file(given_config_file,
525 argv[0], argv[1]);
d64ec16c
FC
526 if (ret < 0)
527 return ret;
528 if (ret == 0)
529 die("No such section!");
530 }
531 else if (actions == ACTION_REMOVE_SECTION) {
532 int ret;
533 check_argc(argc, 1, 1);
270a3443
JK
534 ret = git_config_rename_section_in_file(given_config_file,
535 argv[0], NULL);
d64ec16c
FC
536 if (ret < 0)
537 return ret;
538 if (ret == 0)
539 die("No such section!");
540 }
541 else if (actions == ACTION_GET_COLOR) {
542 get_color(argv[0]);
543 }
544 else if (actions == ACTION_GET_COLORBOOL) {
545 if (argc == 1)
e269eb79 546 color_stdout_is_tty = git_config_bool("command line", argv[0]);
d64ec16c
FC
547 return get_colorbool(argc != 0);
548 }
549
1b1e59c5
JS
550 return 0;
551}
6390c905
RS
552
553int cmd_repo_config(int argc, const char **argv, const char *prefix)
554{
555 fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
556 return cmd_config(argc, argv, prefix);
557}