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