]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/config.c
config: pass kvi to die_bad_number()
[thirdparty/git.git] / builtin / config.c
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "alloc.h"
4 #include "config.h"
5 #include "color.h"
6 #include "editor.h"
7 #include "environment.h"
8 #include "repository.h"
9 #include "gettext.h"
10 #include "ident.h"
11 #include "parse-options.h"
12 #include "urlmatch.h"
13 #include "path.h"
14 #include "quote.h"
15 #include "setup.h"
16 #include "worktree.h"
17 #include "wrapper.h"
18
19 static const char *const builtin_config_usage[] = {
20 N_("git config [<options>]"),
21 NULL
22 };
23
24 static char *key;
25 static regex_t *key_regexp;
26 static const char *value_pattern;
27 static regex_t *regexp;
28 static int show_keys;
29 static int omit_values;
30 static int use_key_regexp;
31 static int do_all;
32 static int do_not_match;
33 static char delim = '=';
34 static char key_delim = ' ';
35 static char term = '\n';
36
37 static int use_global_config, use_system_config, use_local_config;
38 static int use_worktree_config;
39 static struct git_config_source given_config_source;
40 static int actions, type;
41 static char *default_value;
42 static int end_nul;
43 static int respect_includes_opt = -1;
44 static struct config_options config_options;
45 static int show_origin;
46 static int show_scope;
47 static int fixed_value;
48
49 #define ACTION_GET (1<<0)
50 #define ACTION_GET_ALL (1<<1)
51 #define ACTION_GET_REGEXP (1<<2)
52 #define ACTION_REPLACE_ALL (1<<3)
53 #define ACTION_ADD (1<<4)
54 #define ACTION_UNSET (1<<5)
55 #define ACTION_UNSET_ALL (1<<6)
56 #define ACTION_RENAME_SECTION (1<<7)
57 #define ACTION_REMOVE_SECTION (1<<8)
58 #define ACTION_LIST (1<<9)
59 #define ACTION_EDIT (1<<10)
60 #define ACTION_SET (1<<11)
61 #define ACTION_SET_ALL (1<<12)
62 #define ACTION_GET_COLOR (1<<13)
63 #define ACTION_GET_COLORBOOL (1<<14)
64 #define ACTION_GET_URLMATCH (1<<15)
65
66 /*
67 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
68 * one line of output and which should therefore be paged.
69 */
70 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
71 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
72
73 #define TYPE_BOOL 1
74 #define TYPE_INT 2
75 #define TYPE_BOOL_OR_INT 3
76 #define TYPE_PATH 4
77 #define TYPE_EXPIRY_DATE 5
78 #define TYPE_COLOR 6
79 #define TYPE_BOOL_OR_STR 7
80
81 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
82 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
83 PARSE_OPT_NONEG, option_parse_type, (i) }
84
85 static NORETURN void usage_builtin_config(void);
86
87 static int option_parse_type(const struct option *opt, const char *arg,
88 int unset)
89 {
90 int new_type, *to_type;
91
92 if (unset) {
93 *((int *) opt->value) = 0;
94 return 0;
95 }
96
97 /*
98 * To support '--<type>' style flags, begin with new_type equal to
99 * opt->defval.
100 */
101 new_type = opt->defval;
102 if (!new_type) {
103 if (!strcmp(arg, "bool"))
104 new_type = TYPE_BOOL;
105 else if (!strcmp(arg, "int"))
106 new_type = TYPE_INT;
107 else if (!strcmp(arg, "bool-or-int"))
108 new_type = TYPE_BOOL_OR_INT;
109 else if (!strcmp(arg, "bool-or-str"))
110 new_type = TYPE_BOOL_OR_STR;
111 else if (!strcmp(arg, "path"))
112 new_type = TYPE_PATH;
113 else if (!strcmp(arg, "expiry-date"))
114 new_type = TYPE_EXPIRY_DATE;
115 else if (!strcmp(arg, "color"))
116 new_type = TYPE_COLOR;
117 else
118 die(_("unrecognized --type argument, %s"), arg);
119 }
120
121 to_type = opt->value;
122 if (*to_type && *to_type != new_type) {
123 /*
124 * Complain when there is a new type not equal to the old type.
125 * This allows for combinations like '--int --type=int' and
126 * '--type=int --type=int', but disallows ones like '--type=bool
127 * --int' and '--type=bool
128 * --type=int'.
129 */
130 error(_("only one type at a time"));
131 usage_builtin_config();
132 }
133 *to_type = new_type;
134
135 return 0;
136 }
137
138 static struct option builtin_config_options[] = {
139 OPT_GROUP(N_("Config file location")),
140 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
141 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
142 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
143 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
144 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
145 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
146 OPT_GROUP(N_("Action")),
147 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
148 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
149 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
150 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
151 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
152 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
153 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
154 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
155 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
156 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
157 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
158 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
159 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
160 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
161 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
162 OPT_GROUP(N_("Type")),
163 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
164 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
165 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
166 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
167 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
168 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
169 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
170 OPT_GROUP(N_("Other")),
171 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
172 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
173 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
174 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
175 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
176 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
177 OPT_END(),
178 };
179
180 static NORETURN void usage_builtin_config(void)
181 {
182 usage_with_options(builtin_config_usage, builtin_config_options);
183 }
184
185 static void check_argc(int argc, int min, int max)
186 {
187 if (argc >= min && argc <= max)
188 return;
189 if (min == max)
190 error(_("wrong number of arguments, should be %d"), min);
191 else
192 error(_("wrong number of arguments, should be from %d to %d"),
193 min, max);
194 usage_builtin_config();
195 }
196
197 static void show_config_origin(const struct key_value_info *kvi,
198 struct strbuf *buf)
199 {
200 const char term = end_nul ? '\0' : '\t';
201
202 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
203 strbuf_addch(buf, ':');
204 if (end_nul)
205 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
206 else
207 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
208 strbuf_addch(buf, term);
209 }
210
211 static void show_config_scope(const struct key_value_info *kvi,
212 struct strbuf *buf)
213 {
214 const char term = end_nul ? '\0' : '\t';
215 const char *scope = config_scope_name(kvi->scope);
216
217 strbuf_addstr(buf, N_(scope));
218 strbuf_addch(buf, term);
219 }
220
221 static int show_all_config(const char *key_, const char *value_,
222 const struct config_context *ctx,
223 void *cb UNUSED)
224 {
225 const struct key_value_info *kvi = ctx->kvi;
226
227 if (show_origin || show_scope) {
228 struct strbuf buf = STRBUF_INIT;
229 if (show_scope)
230 show_config_scope(kvi, &buf);
231 if (show_origin)
232 show_config_origin(kvi, &buf);
233 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
234 fwrite(buf.buf, 1, buf.len, stdout);
235 strbuf_release(&buf);
236 }
237 if (!omit_values && value_)
238 printf("%s%c%s%c", key_, delim, value_, term);
239 else
240 printf("%s%c", key_, term);
241 return 0;
242 }
243
244 struct strbuf_list {
245 struct strbuf *items;
246 int nr;
247 int alloc;
248 };
249
250 static int format_config(struct strbuf *buf, const char *key_,
251 const char *value_, const struct key_value_info *kvi)
252 {
253 if (show_scope)
254 show_config_scope(kvi, buf);
255 if (show_origin)
256 show_config_origin(kvi, buf);
257 if (show_keys)
258 strbuf_addstr(buf, key_);
259 if (!omit_values) {
260 if (show_keys)
261 strbuf_addch(buf, key_delim);
262
263 if (type == TYPE_INT)
264 strbuf_addf(buf, "%"PRId64,
265 git_config_int64(key_, value_ ? value_ : "", kvi));
266 else if (type == TYPE_BOOL)
267 strbuf_addstr(buf, git_config_bool(key_, value_) ?
268 "true" : "false");
269 else if (type == TYPE_BOOL_OR_INT) {
270 int is_bool, v;
271 v = git_config_bool_or_int(key_, value_, kvi,
272 &is_bool);
273 if (is_bool)
274 strbuf_addstr(buf, v ? "true" : "false");
275 else
276 strbuf_addf(buf, "%d", v);
277 } else if (type == TYPE_BOOL_OR_STR) {
278 int v = git_parse_maybe_bool(value_);
279 if (v < 0)
280 strbuf_addstr(buf, value_);
281 else
282 strbuf_addstr(buf, v ? "true" : "false");
283 } else if (type == TYPE_PATH) {
284 const char *v;
285 if (git_config_pathname(&v, key_, value_) < 0)
286 return -1;
287 strbuf_addstr(buf, v);
288 free((char *)v);
289 } else if (type == TYPE_EXPIRY_DATE) {
290 timestamp_t t;
291 if (git_config_expiry_date(&t, key_, value_) < 0)
292 return -1;
293 strbuf_addf(buf, "%"PRItime, t);
294 } else if (type == TYPE_COLOR) {
295 char v[COLOR_MAXLEN];
296 if (git_config_color(v, key_, value_) < 0)
297 return -1;
298 strbuf_addstr(buf, v);
299 } else if (value_) {
300 strbuf_addstr(buf, value_);
301 } else {
302 /* Just show the key name; back out delimiter */
303 if (show_keys)
304 strbuf_setlen(buf, buf->len - 1);
305 }
306 }
307 strbuf_addch(buf, term);
308 return 0;
309 }
310
311 static int collect_config(const char *key_, const char *value_,
312 const struct config_context *ctx, void *cb)
313 {
314 struct strbuf_list *values = cb;
315 const struct key_value_info *kvi = ctx->kvi;
316
317 if (!use_key_regexp && strcmp(key_, key))
318 return 0;
319 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
320 return 0;
321 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
322 return 0;
323 if (regexp != NULL &&
324 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
325 return 0;
326
327 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
328 strbuf_init(&values->items[values->nr], 0);
329
330 return format_config(&values->items[values->nr++], key_, value_, kvi);
331 }
332
333 static int get_value(const char *key_, const char *regex_, unsigned flags)
334 {
335 int ret = CONFIG_GENERIC_ERROR;
336 struct strbuf_list values = {NULL};
337 int i;
338
339 if (use_key_regexp) {
340 char *tl;
341
342 /*
343 * NEEDSWORK: this naive pattern lowercasing obviously does not
344 * work for more complex patterns like "^[^.]*Foo.*bar".
345 * Perhaps we should deprecate this altogether someday.
346 */
347
348 key = xstrdup(key_);
349 for (tl = key + strlen(key) - 1;
350 tl >= key && *tl != '.';
351 tl--)
352 *tl = tolower(*tl);
353 for (tl = key; *tl && *tl != '.'; tl++)
354 *tl = tolower(*tl);
355
356 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
357 if (regcomp(key_regexp, key, REG_EXTENDED)) {
358 error(_("invalid key pattern: %s"), key_);
359 FREE_AND_NULL(key_regexp);
360 ret = CONFIG_INVALID_PATTERN;
361 goto free_strings;
362 }
363 } else {
364 if (git_config_parse_key(key_, &key, NULL)) {
365 ret = CONFIG_INVALID_KEY;
366 goto free_strings;
367 }
368 }
369
370 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
371 value_pattern = regex_;
372 else if (regex_) {
373 if (regex_[0] == '!') {
374 do_not_match = 1;
375 regex_++;
376 }
377
378 regexp = (regex_t*)xmalloc(sizeof(regex_t));
379 if (regcomp(regexp, regex_, REG_EXTENDED)) {
380 error(_("invalid pattern: %s"), regex_);
381 FREE_AND_NULL(regexp);
382 ret = CONFIG_INVALID_PATTERN;
383 goto free_strings;
384 }
385 }
386
387 config_with_options(collect_config, &values,
388 &given_config_source, the_repository,
389 &config_options);
390
391 if (!values.nr && default_value) {
392 struct key_value_info kvi = KVI_INIT;
393 struct strbuf *item;
394
395 kvi_from_param(&kvi);
396 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
397 item = &values.items[values.nr++];
398 strbuf_init(item, 0);
399 if (format_config(item, key_, default_value, &kvi) < 0)
400 die(_("failed to format default config value: %s"),
401 default_value);
402 }
403
404 ret = !values.nr;
405
406 for (i = 0; i < values.nr; i++) {
407 struct strbuf *buf = values.items + i;
408 if (do_all || i == values.nr - 1)
409 fwrite(buf->buf, 1, buf->len, stdout);
410 strbuf_release(buf);
411 }
412 free(values.items);
413
414 free_strings:
415 free(key);
416 if (key_regexp) {
417 regfree(key_regexp);
418 free(key_regexp);
419 }
420 if (regexp) {
421 regfree(regexp);
422 free(regexp);
423 }
424
425 return ret;
426 }
427
428 static char *normalize_value(const char *key, const char *value,
429 struct key_value_info *kvi)
430 {
431 if (!value)
432 return NULL;
433
434 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
435 /*
436 * We don't do normalization for TYPE_PATH here: If
437 * the path is like ~/foobar/, we prefer to store
438 * "~/foobar/" in the config file, and to expand the ~
439 * when retrieving the value.
440 * Also don't do normalization for expiry dates.
441 */
442 return xstrdup(value);
443 if (type == TYPE_INT)
444 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
445 if (type == TYPE_BOOL)
446 return xstrdup(git_config_bool(key, value) ? "true" : "false");
447 if (type == TYPE_BOOL_OR_INT) {
448 int is_bool, v;
449 v = git_config_bool_or_int(key, value, kvi, &is_bool);
450 if (!is_bool)
451 return xstrfmt("%d", v);
452 else
453 return xstrdup(v ? "true" : "false");
454 }
455 if (type == TYPE_BOOL_OR_STR) {
456 int v = git_parse_maybe_bool(value);
457 if (v < 0)
458 return xstrdup(value);
459 else
460 return xstrdup(v ? "true" : "false");
461 }
462 if (type == TYPE_COLOR) {
463 char v[COLOR_MAXLEN];
464 if (git_config_color(v, key, value))
465 die(_("cannot parse color '%s'"), value);
466
467 /*
468 * The contents of `v` now contain an ANSI escape
469 * sequence, not suitable for including within a
470 * configuration file. Treat the above as a
471 * "sanity-check", and return the given value, which we
472 * know is representable as valid color code.
473 */
474 return xstrdup(value);
475 }
476
477 BUG("cannot normalize type %d", type);
478 }
479
480 static int get_color_found;
481 static const char *get_color_slot;
482 static const char *get_colorbool_slot;
483 static char parsed_color[COLOR_MAXLEN];
484
485 static int git_get_color_config(const char *var, const char *value,
486 const struct config_context *ctx UNUSED,
487 void *cb UNUSED)
488 {
489 if (!strcmp(var, get_color_slot)) {
490 if (!value)
491 config_error_nonbool(var);
492 if (color_parse(value, parsed_color) < 0)
493 return -1;
494 get_color_found = 1;
495 }
496 return 0;
497 }
498
499 static void get_color(const char *var, const char *def_color)
500 {
501 get_color_slot = var;
502 get_color_found = 0;
503 parsed_color[0] = '\0';
504 config_with_options(git_get_color_config, NULL,
505 &given_config_source, the_repository,
506 &config_options);
507
508 if (!get_color_found && def_color) {
509 if (color_parse(def_color, parsed_color) < 0)
510 die(_("unable to parse default color value"));
511 }
512
513 fputs(parsed_color, stdout);
514 }
515
516 static int get_colorbool_found;
517 static int get_diff_color_found;
518 static int get_color_ui_found;
519 static int git_get_colorbool_config(const char *var, const char *value,
520 const struct config_context *ctx UNUSED,
521 void *data UNUSED)
522 {
523 if (!strcmp(var, get_colorbool_slot))
524 get_colorbool_found = git_config_colorbool(var, value);
525 else if (!strcmp(var, "diff.color"))
526 get_diff_color_found = git_config_colorbool(var, value);
527 else if (!strcmp(var, "color.ui"))
528 get_color_ui_found = git_config_colorbool(var, value);
529 return 0;
530 }
531
532 static int get_colorbool(const char *var, int print)
533 {
534 get_colorbool_slot = var;
535 get_colorbool_found = -1;
536 get_diff_color_found = -1;
537 get_color_ui_found = -1;
538 config_with_options(git_get_colorbool_config, NULL,
539 &given_config_source, the_repository,
540 &config_options);
541
542 if (get_colorbool_found < 0) {
543 if (!strcmp(get_colorbool_slot, "color.diff"))
544 get_colorbool_found = get_diff_color_found;
545 if (get_colorbool_found < 0)
546 get_colorbool_found = get_color_ui_found;
547 }
548
549 if (get_colorbool_found < 0)
550 /* default value if none found in config */
551 get_colorbool_found = GIT_COLOR_AUTO;
552
553 get_colorbool_found = want_color(get_colorbool_found);
554
555 if (print) {
556 printf("%s\n", get_colorbool_found ? "true" : "false");
557 return 0;
558 } else
559 return get_colorbool_found ? 0 : 1;
560 }
561
562 static void check_write(void)
563 {
564 if (!given_config_source.file && !startup_info->have_repository)
565 die(_("not in a git directory"));
566
567 if (given_config_source.use_stdin)
568 die(_("writing to stdin is not supported"));
569
570 if (given_config_source.blob)
571 die(_("writing config blobs is not supported"));
572 }
573
574 struct urlmatch_current_candidate_value {
575 char value_is_null;
576 struct strbuf value;
577 struct key_value_info kvi;
578 };
579
580 static int urlmatch_collect_fn(const char *var, const char *value,
581 const struct config_context *ctx,
582 void *cb)
583 {
584 struct string_list *values = cb;
585 struct string_list_item *item = string_list_insert(values, var);
586 struct urlmatch_current_candidate_value *matched = item->util;
587 const struct key_value_info *kvi = ctx->kvi;
588
589 if (!matched) {
590 matched = xmalloc(sizeof(*matched));
591 strbuf_init(&matched->value, 0);
592 item->util = matched;
593 } else {
594 strbuf_reset(&matched->value);
595 }
596 matched->kvi = *kvi;
597
598 if (value) {
599 strbuf_addstr(&matched->value, value);
600 matched->value_is_null = 0;
601 } else {
602 matched->value_is_null = 1;
603 }
604 return 0;
605 }
606
607 static int get_urlmatch(const char *var, const char *url)
608 {
609 int ret;
610 char *section_tail;
611 struct string_list_item *item;
612 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
613 struct string_list values = STRING_LIST_INIT_DUP;
614
615 config.collect_fn = urlmatch_collect_fn;
616 config.cascade_fn = NULL;
617 config.cb = &values;
618
619 if (!url_normalize(url, &config.url))
620 die("%s", config.url.err);
621
622 config.section = xstrdup_tolower(var);
623 section_tail = strchr(config.section, '.');
624 if (section_tail) {
625 *section_tail = '\0';
626 config.key = section_tail + 1;
627 show_keys = 0;
628 } else {
629 config.key = NULL;
630 show_keys = 1;
631 }
632
633 config_with_options(urlmatch_config_entry, &config,
634 &given_config_source, the_repository,
635 &config_options);
636
637 ret = !values.nr;
638
639 for_each_string_list_item(item, &values) {
640 struct urlmatch_current_candidate_value *matched = item->util;
641 struct strbuf buf = STRBUF_INIT;
642
643 format_config(&buf, item->string,
644 matched->value_is_null ? NULL : matched->value.buf,
645 &matched->kvi);
646 fwrite(buf.buf, 1, buf.len, stdout);
647 strbuf_release(&buf);
648
649 strbuf_release(&matched->value);
650 }
651 urlmatch_config_release(&config);
652 string_list_clear(&values, 1);
653 free(config.url.url);
654
655 free((void *)config.section);
656 return ret;
657 }
658
659 static char *default_user_config(void)
660 {
661 struct strbuf buf = STRBUF_INIT;
662 strbuf_addf(&buf,
663 _("# This is Git's per-user configuration file.\n"
664 "[user]\n"
665 "# Please adapt and uncomment the following lines:\n"
666 "# name = %s\n"
667 "# email = %s\n"),
668 ident_default_name(),
669 ident_default_email());
670 return strbuf_detach(&buf, NULL);
671 }
672
673 int cmd_config(int argc, const char **argv, const char *prefix)
674 {
675 int nongit = !startup_info->have_repository;
676 char *value = NULL;
677 int flags = 0;
678 int ret = 0;
679 struct key_value_info default_kvi = KVI_INIT;
680
681 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
682
683 argc = parse_options(argc, argv, prefix, builtin_config_options,
684 builtin_config_usage,
685 PARSE_OPT_STOP_AT_NON_OPTION);
686
687 if (use_global_config + use_system_config + use_local_config +
688 use_worktree_config +
689 !!given_config_source.file + !!given_config_source.blob > 1) {
690 error(_("only one config file at a time"));
691 usage_builtin_config();
692 }
693
694 if (nongit) {
695 if (use_local_config)
696 die(_("--local can only be used inside a git repository"));
697 if (given_config_source.blob)
698 die(_("--blob can only be used inside a git repository"));
699 if (use_worktree_config)
700 die(_("--worktree can only be used inside a git repository"));
701
702 }
703
704 if (given_config_source.file &&
705 !strcmp(given_config_source.file, "-")) {
706 given_config_source.file = NULL;
707 given_config_source.use_stdin = 1;
708 given_config_source.scope = CONFIG_SCOPE_COMMAND;
709 }
710
711 if (use_global_config) {
712 char *user_config, *xdg_config;
713
714 git_global_config(&user_config, &xdg_config);
715 if (!user_config)
716 /*
717 * It is unknown if HOME/.gitconfig exists, so
718 * we do not know if we should write to XDG
719 * location; error out even if XDG_CONFIG_HOME
720 * is set and points at a sane location.
721 */
722 die(_("$HOME not set"));
723
724 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
725
726 if (access_or_warn(user_config, R_OK, 0) &&
727 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
728 given_config_source.file = xdg_config;
729 free(user_config);
730 } else {
731 given_config_source.file = user_config;
732 free(xdg_config);
733 }
734 }
735 else if (use_system_config) {
736 given_config_source.file = git_system_config();
737 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
738 } else if (use_local_config) {
739 given_config_source.file = git_pathdup("config");
740 given_config_source.scope = CONFIG_SCOPE_LOCAL;
741 } else if (use_worktree_config) {
742 struct worktree **worktrees = get_worktrees();
743 if (the_repository->repository_format_worktree_config)
744 given_config_source.file = git_pathdup("config.worktree");
745 else if (worktrees[0] && worktrees[1])
746 die(_("--worktree cannot be used with multiple "
747 "working trees unless the config\n"
748 "extension worktreeConfig is enabled. "
749 "Please read \"CONFIGURATION FILE\"\n"
750 "section in \"git help worktree\" for details"));
751 else
752 given_config_source.file = git_pathdup("config");
753 given_config_source.scope = CONFIG_SCOPE_LOCAL;
754 free_worktrees(worktrees);
755 } else if (given_config_source.file) {
756 if (!is_absolute_path(given_config_source.file) && prefix)
757 given_config_source.file =
758 prefix_filename(prefix, given_config_source.file);
759 given_config_source.scope = CONFIG_SCOPE_COMMAND;
760 } else if (given_config_source.blob) {
761 given_config_source.scope = CONFIG_SCOPE_COMMAND;
762 }
763
764
765 if (respect_includes_opt == -1)
766 config_options.respect_includes = !given_config_source.file;
767 else
768 config_options.respect_includes = respect_includes_opt;
769 if (!nongit) {
770 config_options.commondir = get_git_common_dir();
771 config_options.git_dir = get_git_dir();
772 }
773
774 if (end_nul) {
775 term = '\0';
776 delim = '\n';
777 key_delim = '\n';
778 }
779
780 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
781 error(_("--get-color and variable type are incoherent"));
782 usage_builtin_config();
783 }
784
785 if (HAS_MULTI_BITS(actions)) {
786 error(_("only one action at a time"));
787 usage_builtin_config();
788 }
789 if (actions == 0)
790 switch (argc) {
791 case 1: actions = ACTION_GET; break;
792 case 2: actions = ACTION_SET; break;
793 case 3: actions = ACTION_SET_ALL; break;
794 default:
795 usage_builtin_config();
796 }
797 if (omit_values &&
798 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
799 error(_("--name-only is only applicable to --list or --get-regexp"));
800 usage_builtin_config();
801 }
802
803 if (show_origin && !(actions &
804 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
805 error(_("--show-origin is only applicable to --get, --get-all, "
806 "--get-regexp, and --list"));
807 usage_builtin_config();
808 }
809
810 if (default_value && !(actions & ACTION_GET)) {
811 error(_("--default is only applicable to --get"));
812 usage_builtin_config();
813 }
814
815 /* check usage of --fixed-value */
816 if (fixed_value) {
817 int allowed_usage = 0;
818
819 switch (actions) {
820 /* git config --get <name> <value-pattern> */
821 case ACTION_GET:
822 /* git config --get-all <name> <value-pattern> */
823 case ACTION_GET_ALL:
824 /* git config --get-regexp <name-pattern> <value-pattern> */
825 case ACTION_GET_REGEXP:
826 /* git config --unset <name> <value-pattern> */
827 case ACTION_UNSET:
828 /* git config --unset-all <name> <value-pattern> */
829 case ACTION_UNSET_ALL:
830 allowed_usage = argc > 1 && !!argv[1];
831 break;
832
833 /* git config <name> <value> <value-pattern> */
834 case ACTION_SET_ALL:
835 /* git config --replace-all <name> <value> <value-pattern> */
836 case ACTION_REPLACE_ALL:
837 allowed_usage = argc > 2 && !!argv[2];
838 break;
839
840 /* other options don't allow --fixed-value */
841 }
842
843 if (!allowed_usage) {
844 error(_("--fixed-value only applies with 'value-pattern'"));
845 usage_builtin_config();
846 }
847
848 flags |= CONFIG_FLAGS_FIXED_VALUE;
849 }
850
851 if (actions & PAGING_ACTIONS)
852 setup_auto_pager("config", 1);
853
854 if (actions == ACTION_LIST) {
855 check_argc(argc, 0, 0);
856 if (config_with_options(show_all_config, NULL,
857 &given_config_source, the_repository,
858 &config_options) < 0) {
859 if (given_config_source.file)
860 die_errno(_("unable to read config file '%s'"),
861 given_config_source.file);
862 else
863 die(_("error processing config file(s)"));
864 }
865 }
866 else if (actions == ACTION_EDIT) {
867 char *config_file;
868
869 check_argc(argc, 0, 0);
870 if (!given_config_source.file && nongit)
871 die(_("not in a git directory"));
872 if (given_config_source.use_stdin)
873 die(_("editing stdin is not supported"));
874 if (given_config_source.blob)
875 die(_("editing blobs is not supported"));
876 git_config(git_default_config, NULL);
877 config_file = given_config_source.file ?
878 xstrdup(given_config_source.file) :
879 git_pathdup("config");
880 if (use_global_config) {
881 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
882 if (fd >= 0) {
883 char *content = default_user_config();
884 write_str_in_full(fd, content);
885 free(content);
886 close(fd);
887 }
888 else if (errno != EEXIST)
889 die_errno(_("cannot create configuration file %s"), config_file);
890 }
891 launch_editor(config_file, NULL, NULL);
892 free(config_file);
893 }
894 else if (actions == ACTION_SET) {
895 check_write();
896 check_argc(argc, 2, 2);
897 value = normalize_value(argv[0], argv[1], &default_kvi);
898 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
899 if (ret == CONFIG_NOTHING_SET)
900 error(_("cannot overwrite multiple values with a single value\n"
901 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
902 }
903 else if (actions == ACTION_SET_ALL) {
904 check_write();
905 check_argc(argc, 2, 3);
906 value = normalize_value(argv[0], argv[1], &default_kvi);
907 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
908 argv[0], value, argv[2],
909 flags);
910 }
911 else if (actions == ACTION_ADD) {
912 check_write();
913 check_argc(argc, 2, 2);
914 value = normalize_value(argv[0], argv[1], &default_kvi);
915 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
916 argv[0], value,
917 CONFIG_REGEX_NONE,
918 flags);
919 }
920 else if (actions == ACTION_REPLACE_ALL) {
921 check_write();
922 check_argc(argc, 2, 3);
923 value = normalize_value(argv[0], argv[1], &default_kvi);
924 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
925 argv[0], value, argv[2],
926 flags | CONFIG_FLAGS_MULTI_REPLACE);
927 }
928 else if (actions == ACTION_GET) {
929 check_argc(argc, 1, 2);
930 return get_value(argv[0], argv[1], flags);
931 }
932 else if (actions == ACTION_GET_ALL) {
933 do_all = 1;
934 check_argc(argc, 1, 2);
935 return get_value(argv[0], argv[1], flags);
936 }
937 else if (actions == ACTION_GET_REGEXP) {
938 show_keys = 1;
939 use_key_regexp = 1;
940 do_all = 1;
941 check_argc(argc, 1, 2);
942 return get_value(argv[0], argv[1], flags);
943 }
944 else if (actions == ACTION_GET_URLMATCH) {
945 check_argc(argc, 2, 2);
946 return get_urlmatch(argv[0], argv[1]);
947 }
948 else if (actions == ACTION_UNSET) {
949 check_write();
950 check_argc(argc, 1, 2);
951 if (argc == 2)
952 return git_config_set_multivar_in_file_gently(given_config_source.file,
953 argv[0], NULL, argv[1],
954 flags);
955 else
956 return git_config_set_in_file_gently(given_config_source.file,
957 argv[0], NULL);
958 }
959 else if (actions == ACTION_UNSET_ALL) {
960 check_write();
961 check_argc(argc, 1, 2);
962 return git_config_set_multivar_in_file_gently(given_config_source.file,
963 argv[0], NULL, argv[1],
964 flags | CONFIG_FLAGS_MULTI_REPLACE);
965 }
966 else if (actions == ACTION_RENAME_SECTION) {
967 check_write();
968 check_argc(argc, 2, 2);
969 ret = git_config_rename_section_in_file(given_config_source.file,
970 argv[0], argv[1]);
971 if (ret < 0)
972 return ret;
973 else if (!ret)
974 die(_("no such section: %s"), argv[0]);
975 else
976 ret = 0;
977 }
978 else if (actions == ACTION_REMOVE_SECTION) {
979 check_write();
980 check_argc(argc, 1, 1);
981 ret = git_config_rename_section_in_file(given_config_source.file,
982 argv[0], NULL);
983 if (ret < 0)
984 return ret;
985 else if (!ret)
986 die(_("no such section: %s"), argv[0]);
987 else
988 ret = 0;
989 }
990 else if (actions == ACTION_GET_COLOR) {
991 check_argc(argc, 1, 2);
992 get_color(argv[0], argv[1]);
993 }
994 else if (actions == ACTION_GET_COLORBOOL) {
995 check_argc(argc, 1, 2);
996 if (argc == 2)
997 color_stdout_is_tty = git_config_bool("command line", argv[1]);
998 return get_colorbool(argv[0], argc == 2);
999 }
1000
1001 free(value);
1002 return ret;
1003 }