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